Skip to content

Commit

Permalink
[filter] declarative paradigm
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed May 19, 2024
1 parent acd4e03 commit 0e0f871
Show file tree
Hide file tree
Showing 42 changed files with 1,502 additions and 814 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ Thanks everyone!
<br>
[![License Scan](https://app.fossa.com/api/projects/git%2Bgithub.com%2FFrancoisCarouge%2FKalman.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FFrancoisCarouge%2FKalman?ref=badge_shield)
<br>
[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/8933/badge)](https://www.bestpractices.dev/projects/8933)
<br>
<br>
[![Deploy Doxygen](https://github.com/FrancoisCarouge/Kalman/actions/workflows/deploy_doxygen.yml/badge.svg)](https://github.com/FrancoisCarouge/Kalman/actions/workflows/deploy_doxygen.yml)
<br>
Expand Down
7 changes: 6 additions & 1 deletion include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ target_sources(
"HEADERS"
FILES
"fcarouge/algorithm.hpp"
"fcarouge/internal/factory.hpp"
"fcarouge/internal/format.hpp"
"fcarouge/internal/function.hpp"
"fcarouge/internal/kalman.hpp"
"fcarouge/internal/kalman.tpp"
"fcarouge/internal/type.hpp"
"fcarouge/internal/utility.hpp"
"fcarouge/internal/x_z_p_q_r_us_ps.hpp"
"fcarouge/internal/x_z_p_r_f.hpp"
"fcarouge/internal/x_z_u_p_q_r_f_g_ps.hpp"
"fcarouge/internal/x_z_u_p_q_r_us_ps.hpp"
"fcarouge/kalman.hpp"
"fcarouge/utility.hpp")
install(
Expand Down
235 changes: 235 additions & 0 deletions include/fcarouge/internal/factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
Kalman Filter
Version 0.3.0
https://github.com/FrancoisCarouge/Kalman
SPDX-License-Identifier: Unlicense
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org> */

#ifndef FCAROUGE_INTERNAL_FACTORY_HPP
#define FCAROUGE_INTERNAL_FACTORY_HPP

#include "fcarouge/internal/type.hpp"
#include "fcarouge/internal/x_z_p_q_r_us_ps.hpp"
#include "fcarouge/internal/x_z_p_r_f.hpp"
#include "fcarouge/internal/x_z_u_p_q_r_f_g_ps.hpp"
#include "fcarouge/internal/x_z_u_p_q_r_us_ps.hpp"

#include <concepts>
#include <type_traits>

namespace fcarouge::internal {
//! @todo Support arbritary order of configuration parameters?
//! @todo Support user defined types by type name reflection? Case, naming
//! convention insensitive?
//! @todo Some of these overload should probably be removed to guide the user
//! towards better initialization practices?
template <typename Filter = void> struct make_filter {
template <typename... Arguments>
inline constexpr void
operator()([[maybe_unused]] Arguments... arguments) const
requires(std::same_as<Filter, void>)
{
static_assert(false,
"This requested filter configuration is not yet supported. "
"Please, submit a pull request or feature request.");
}

//! @todo Rename, clean the concet: undeduced?
inline constexpr auto operator()() const -> x_z_p_r_f<double, double>
requires(std::same_as<Filter, void>);

inline constexpr auto operator()() const { return Filter{}; }

template <typename State, typename Output>
inline constexpr auto operator()(state<State> x,
[[maybe_unused]] output_t<Output> z) const {
return x_z_p_r_f<State, Output>{x.value};
}

template <typename State, typename Output, typename Input>
inline constexpr auto operator()(state<State> x,
[[maybe_unused]] output_t<Output> z,
[[maybe_unused]] input_t<Input> u) const {
using kt = x_z_u_p_q_r_us_ps<State, Output, Input, empty_pack, empty_pack>;
return kt{typename kt::state{x.value}};
}

template <typename State, typename Output, typename Input,
typename EstimateUncertainty, typename ProcessUncertainty,
typename OutputUncertainty, typename StateTransition,
typename InputControl, typename... Ps>
//! @todo Simplify the require clause?
requires requires(ProcessUncertainty q) {
requires std::invocable<ProcessUncertainty, State, Ps...>;
}
inline constexpr auto
operator()(state<State> x, [[maybe_unused]] output_t<Output> z,
[[maybe_unused]] input_t<Input> u,
estimate_uncertainty<EstimateUncertainty> p,
[[maybe_unused]] process_uncertainty<ProcessUncertainty> q,
[[maybe_unused]] output_uncertainty<OutputUncertainty> r,
[[maybe_unused]] state_transition<StateTransition> f,
[[maybe_unused]] input_control<InputControl> g,
[[maybe_unused]] prediction_types_t<Ps...> pts) const {
using kt = x_z_u_p_q_r_f_g_ps<State, Output, Input, empty_pack,
repack_t<prediction_types_t<Ps...>>>;
return kt{typename kt::state{x.value},
typename kt::estimate_uncertainty{p.value},
typename kt::noise_process_function{q.value},
typename kt::output_uncertainty{r.value},
typename kt::transition_state_function{f.value},
typename kt::transition_control_function{g.value}};
}

template <typename State, typename Output, typename Input, typename... Us,
typename... Ps>
inline constexpr auto
operator()(state<State> x, [[maybe_unused]] output_t<Output> z,
[[maybe_unused]] input_t<Input> u,
[[maybe_unused]] update_types_t<Us...> uts,
[[maybe_unused]] prediction_types_t<Ps...> pts) const {
using kt =
x_z_u_p_q_r_us_ps<State, Output, Input, repack_t<update_types_t<Us...>>,
repack_t<prediction_types_t<Ps...>>>;
return kt{typename kt::state{x.value}};
}

template <typename State, typename Output, typename EstimateUncertainty,
typename ProcessUncertainty, typename OutputUncertainty,
typename... Us, typename... Ps>
inline constexpr auto
operator()(state<State> x, [[maybe_unused]] output_t<Output> z,
estimate_uncertainty<EstimateUncertainty> p,
process_uncertainty<ProcessUncertainty> q,
output_uncertainty<OutputUncertainty> r,
[[maybe_unused]] update_types_t<Us...> uts,
[[maybe_unused]] prediction_types_t<Ps...> pts) const {
using kt = x_z_p_q_r_us_ps<State, Output, repack_t<update_types_t<Us...>>,
repack_t<prediction_types_t<Ps...>>>;
return kt{typename kt::state{x.value},
typename kt::estimate_uncertainty{p.value},
typename kt::process_uncertainty{q.value},
typename kt::output_uncertainty{r.value}};
}

template <typename State, typename Output, typename EstimateUncertainty,
typename ProcessUncertainty, typename OutputUncertainty,
typename OutputModel, typename StateTransition>
inline constexpr auto
operator()(state<State> x, [[maybe_unused]] output_t<Output> z,
estimate_uncertainty<EstimateUncertainty> p,
process_uncertainty<ProcessUncertainty> q,
output_uncertainty<OutputUncertainty> r,
output_model<OutputModel> h,
[[maybe_unused]] state_transition<StateTransition> f) const {
using kt = x_z_p_q_r_us_ps<State, Output, empty_pack, empty_pack>;
return kt{typename kt::state{x.value},
typename kt::estimate_uncertainty{p.value},
typename kt::process_uncertainty{q.value},
typename kt::output_uncertainty{r.value},
typename kt::output_model{h.value},
typename kt::state_transition{f.value}};
}

template <typename State, typename Output, typename EstimateUncertainty,
typename OutputUncertainty>
inline constexpr auto
operator()(state<State> x, [[maybe_unused]] output_t<Output> z,
estimate_uncertainty<EstimateUncertainty> p,
output_uncertainty<OutputUncertainty> r) const {
using kt = x_z_p_r_f<State, Output>;
return kt{typename kt::state{x.value},
typename kt::estimate_uncertainty{p.value},
typename kt::output_uncertainty{r.value}};
}

template <typename State, typename Output, typename EstimateUncertainty,
typename OutputUncertainty, typename ProcessUncertainty>
inline constexpr auto
operator()(state<State> x, [[maybe_unused]] output_t<Output> z,
estimate_uncertainty<EstimateUncertainty> p,
process_uncertainty<ProcessUncertainty> q,
output_uncertainty<OutputUncertainty> r) const {
using kt = x_z_p_q_r_us_ps<State, Output, empty_pack, empty_pack>;
return kt{typename kt::state{x.value},
typename kt::estimate_uncertainty{p.value},
typename kt::process_uncertainty{q.value},
typename kt::output_uncertainty{r.value}};
}

template <typename State, typename Output, typename Input,
typename EstimateUncertainty, typename ProcessUncertainty,
typename OutputUncertainty>
inline constexpr auto
operator()(state<State> x, [[maybe_unused]] output_t<Output> z,
[[maybe_unused]] input_t<Input> u,
estimate_uncertainty<EstimateUncertainty> p,
process_uncertainty<ProcessUncertainty> q,
output_uncertainty<OutputUncertainty> r) const {
using kt = x_z_u_p_q_r_us_ps<State, Output, Input, empty_pack, empty_pack>;
return kt{typename kt::state{x.value},
typename kt::estimate_uncertainty{p.value},
typename kt::process_uncertainty{q.value},
typename kt::output_uncertainty{r.value}};
}

template <typename State, typename Output, typename Input,
typename EstimateUncertainty, typename OutputUncertainty,
typename ProcessUncertainty, typename... Us, typename... Ps>
inline constexpr auto
operator()(state<State> x, [[maybe_unused]] output_t<Output> z,
[[maybe_unused]] input_t<Input> u,
estimate_uncertainty<EstimateUncertainty> p,
process_uncertainty<ProcessUncertainty> q,
output_uncertainty<OutputUncertainty> r,
[[maybe_unused]] update_types_t<Us...> uts,
[[maybe_unused]] prediction_types_t<Ps...> pts) const {

using kt =
x_z_u_p_q_r_us_ps<State, Output, Input, repack_t<update_types_t<Us...>>,
repack_t<prediction_types_t<Ps...>>>;
return kt{typename kt::state{x.value},
typename kt::estimate_uncertainty{p.value},
typename kt::process_uncertainty{q.value},
typename kt::output_uncertainty{r.value}};
}
};

template <typename Filter> inline constexpr make_filter<Filter> filter{};

template <typename... Arguments>
using filter_t = std::invoke_result_t<make_filter<>, Arguments...>;
} // namespace fcarouge::internal

#endif // FCAROUGE_INTERNAL_FACTORY_HPP
40 changes: 20 additions & 20 deletions include/fcarouge/internal/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,44 @@ For more information, please refer to <https://unlicense.org> */
#include <type_traits>

namespace fcarouge {
template <typename, typename, typename, typename, typename> class kalman;
template <typename> class kalman;
} // namespace fcarouge

template <typename State, typename Output, typename Input, typename UpdateTypes,
typename PredictionTypes, typename Char>
template <typename Filter, typename Char>
// It is allowed to add template specializations for any standard library class
// template to the namespace std only if the declaration depends on at least one
// program-defined type and the specialization satisfies all requirements for
// the original template, except where such specializations are prohibited.
// NOLINTNEXTLINE(cert-dcl58-cpp)
struct std::formatter<
fcarouge::kalman<State, Output, Input, UpdateTypes, PredictionTypes>,
Char> {
using kalman =
fcarouge::kalman<State, Output, Input, UpdateTypes, PredictionTypes>;

struct std::formatter<fcarouge::kalman<Filter>, Char> {
constexpr auto parse(std::basic_format_parse_context<Char> &parse_context) {
return parse_context.begin();
}

//! @todo P2585 may be useful in simplifying and standardizing the support.
template <typename OutputIt>
auto format(const kalman &filter,
auto format(const fcarouge::kalman<Filter> &filter,
std::basic_format_context<OutputIt, Char> &format_context) const
-> OutputIt {
format_context.advance_to(
format_to(format_context.out(), R"({{"f": {}, )", filter.f()));

if constexpr (fcarouge::internal::has_input_control_method<kalman>) {
if constexpr (fcarouge::has_input_control<Filter>) {
format_context.advance_to(
format_to(format_context.out(), R"("g": {}, )", filter.g()));
}

if constexpr (fcarouge::internal::has_output_model_method<kalman>) {
if constexpr (fcarouge::has_output_model<Filter>) {
format_context.advance_to(
format_to(format_context.out(), R"("h": {}, )", filter.h()));
}

format_context.advance_to(format_to(
format_context.out(), R"("k": {}, "p": {}, )", filter.k(), filter.p()));

{
constexpr auto end{fcarouge::internal::repack_s<PredictionTypes>};
if constexpr (fcarouge::internal::has_prediction_types<Filter>) {
constexpr auto end{
fcarouge::internal::repack_s<typename Filter::prediction_types>};
constexpr decltype(end) begin{0};
constexpr decltype(end) next{1};
fcarouge::internal::for_constexpr<begin, end, next>(
Expand All @@ -98,19 +93,24 @@ struct std::formatter<
});
}

format_context.advance_to(format_to(format_context.out(),
R"("q": {}, "r": {}, "s": {}, )",
filter.q(), filter.r(), filter.s()));
if constexpr (fcarouge::has_process_uncertainty<Filter>) {
format_context.advance_to(
format_to(format_context.out(), R"("q": {}, )", filter.q()));
}

format_context.advance_to(format_to(
format_context.out(), R"("r": {}, "s": {}, )", filter.r(), filter.s()));

//! @todo Generalize out internal method concept when MSVC has better
//! if-constexpr-requires support.
if constexpr (fcarouge::internal::has_input_method<kalman>) {
if constexpr (fcarouge::has_input<Filter>) {
format_context.advance_to(
format_to(format_context.out(), R"("u": {}, )", filter.u()));
}

{
constexpr auto end{fcarouge::internal::repack_s<UpdateTypes>};
if constexpr (fcarouge::internal::has_update_types<Filter>) {
constexpr auto end{
fcarouge::internal::repack_s<typename Filter::update_types>};
constexpr decltype(end) begin{0};
constexpr decltype(end) next{1};
fcarouge::internal::for_constexpr<begin, end, next>(
Expand Down

0 comments on commit 0e0f871

Please sign in to comment.