Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[filter] support input optional api #464

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions include/fcarouge/internal/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ template <typename State, typename Output, typename Input, typename UpdateTypes,
struct std::formatter<
fcarouge::kalman<State, Output, Input, UpdateTypes, PredictionTypes>,
Char> {
using kalman =
fcarouge::kalman<State, Output, Input, UpdateTypes, PredictionTypes>;

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 fcarouge::kalman<State, Output, Input, UpdateTypes,
PredictionTypes> &filter,
auto format(const kalman &filter,
std::basic_format_context<OutputIt, Char> &format_context) const
-> OutputIt {
format_context.advance_to(
Expand Down Expand Up @@ -96,7 +98,9 @@ struct std::formatter<
R"("q": {}, "r": {}, "s": {}, )",
filter.q(), filter.r(), filter.s()));

if constexpr (requires { filter.u(); }) {
//! @todo Generalize out internal method concept when MSVC has better
//! if-constexpr-requires support.
if constexpr (fcarouge::internal::has_input_method<kalman>) {
format_context.advance_to(
format_to(format_context.out(), R"("u": {}, )", filter.u()));
}
Expand Down
14 changes: 8 additions & 6 deletions include/fcarouge/internal/kalman.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,19 @@ kalman<State, Output, Input, UpdateTypes, PredictionTypes>::z() const
template <typename State, typename Output, typename Input, typename UpdateTypes,
typename PredictionTypes>
[[nodiscard("The returned control column vector U is unexpectedly "
"discarded.")]] inline constexpr auto
"discarded.")]] inline constexpr const auto &
kalman<State, Output, Input, UpdateTypes, PredictionTypes>::u() const
-> const input &requires(requires { filter.u; }) { return filter.u; }
requires(has_input<implementation>)
{
return filter.u;
}

template <typename State, typename Output, typename Input, typename UpdateTypes,
typename PredictionTypes>
[[nodiscard("The returned estimated covariance matrix P is unexpectedly "
"discarded.")]] inline constexpr auto kalman<State, Output, Input,
UpdateTypes,
PredictionTypes>::p()
const -> const estimate_uncertainty & {
"discarded.")]] inline constexpr auto
kalman<State, Output, Input, UpdateTypes, PredictionTypes>::p() const
-> const estimate_uncertainty & {
return filter.p;
}

Expand Down
10 changes: 10 additions & 0 deletions include/fcarouge/internal/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ concept algebraic = not arithmetic<Type>;
template <typename Type>
concept eigen = requires { typename Type::PlainMatrix; };

template <typename Filter>
concept has_input_member = requires(Filter filter) { filter.u; };

template <typename Filter>
concept has_input_method = requires(Filter filter) { filter.u(); };

//! @todo Shorten when MSVC has better if-constexpr-requires support.
template <typename Filter>
concept has_input = has_input_member<Filter> || has_input_method<Filter>;

struct empty {
inline constexpr explicit empty([[maybe_unused]] auto &&...any) noexcept {
// Constructs from anything for all initializations compatibility.
Expand Down
4 changes: 2 additions & 2 deletions include/fcarouge/kalman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ class kalman final {
//! @return The last control column vector U.
//!
//! @complexity Constant.
inline constexpr auto u() const
-> const input &requires(requires { filter.u; });
inline constexpr const auto &u() const
requires(has_input<implementation>);

//! @brief Returns the estimated covariance matrix P.
//!
Expand Down
7 changes: 7 additions & 0 deletions include/fcarouge/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ concept algebraic = internal::algebraic<Type>;
//! @details A third party Eigen3 algebraic concept.
template <typename Type>
concept eigen = internal::eigen<Type>;

//! @brief Filter input support concept.
//!
//! @details The filter supports the input related functionality: `input` type
//! member and `u()` input method.
template <typename Filter>
concept has_input = internal::has_input<Filter>;
//! @}

//! @name Types
Expand Down