Skip to content

Commit

Permalink
[filter] kalman filter concept
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed Oct 30, 2024
1 parent 1685342 commit fa1dd5c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
20 changes: 8 additions & 12 deletions include/fcarouge/internal/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,38 @@ For more information, please refer to <https://unlicense.org> */
#include <format>
#include <type_traits>

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

template <typename Filter, typename Char>
template <fcarouge::kalman_filter 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<Filter>, Char> {
struct std::formatter<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 OutputIterator>
constexpr auto
format(const fcarouge::kalman<Filter> &filter,
format(const Filter &filter,
std::basic_format_context<OutputIterator, Char> &format_context) const
-> OutputIterator {

format_context.advance_to(
std::format_to(format_context.out(), R"({{)", filter.f()));

if constexpr (fcarouge::has_state_transition<Filter>) {
if constexpr (fcarouge::internal::has_state_transition_method<Filter>) {
format_context.advance_to(
std::format_to(format_context.out(), R"("f": {}, )", filter.f()));
}

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

if constexpr (fcarouge::has_output_model<Filter>) {
if constexpr (fcarouge::internal::has_output_model_method<Filter>) {
format_context.advance_to(
std::format_to(format_context.out(), R"("h": {}, )", filter.h()));
}
Expand All @@ -100,7 +96,7 @@ struct std::formatter<fcarouge::kalman<Filter>, Char> {
});
}

if constexpr (fcarouge::has_process_uncertainty<Filter>) {
if constexpr (fcarouge::internal::has_process_uncertainty_method<Filter>) {
format_context.advance_to(
std::format_to(format_context.out(), R"("q": {}, )", filter.q()));
}
Expand All @@ -115,7 +111,7 @@ struct std::formatter<fcarouge::kalman<Filter>, Char> {

//! @todo Generalize out internal method concept when MSVC has better
//! if-constexpr-requires support.
if constexpr (fcarouge::has_input<Filter>) {
if constexpr (fcarouge::internal::has_input_method<Filter>) {
format_context.advance_to(
std::format_to(format_context.out(), R"("u": {}, )", filter.u()));
}
Expand Down
8 changes: 8 additions & 0 deletions include/fcarouge/internal/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ For more information, please refer to <https://unlicense.org> */
#include <type_traits>

namespace fcarouge::internal {
template <typename Type>
concept kalman_filter = requires(Type value) {
//! @todo What should be a better concept of the Kalman filter of this
//! library?
typename Type::state;
typename Type::output;
};

template <typename Type>
concept arithmetic = std::integral<Type> || std::floating_point<Type>;

Expand Down
6 changes: 6 additions & 0 deletions include/fcarouge/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ For more information, please refer to <https://unlicense.org> */
namespace fcarouge {
//! @name Concepts
//! @{
//! @brief Kalman filter concept.
//!
//! @details This library's Kalman filters.
template <typename Type>
concept kalman_filter = internal::kalman_filter<Type>;

//! @brief Arithmetic concept.
//!
//! @details Any integer or floating point type.
Expand Down

0 comments on commit fa1dd5c

Please sign in to comment.