Skip to content

Commit

Permalink
[plot] add basic plotter support
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed Oct 26, 2024
1 parent c768aab commit 6151e88
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 17 deletions.
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ target_sources(
"HEADERS"
FILES
"fcarouge/kalman.hpp"
"fcarouge/printer.hpp"
"fcarouge/utility.hpp")
install(
TARGETS kalman
Expand Down
10 changes: 3 additions & 7 deletions include/fcarouge/internal/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,21 @@ 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 {

Expand Down
33 changes: 29 additions & 4 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 Expand Up @@ -176,6 +184,14 @@ struct conditional_output_uncertainty<Filter> {
using output_uncertainty = Filter::output_uncertainty;
};

template <typename Filter> struct conditional_prediction_types {};

template <has_prediction_types Filter>
struct conditional_prediction_types<Filter> {
//! @brief Pack of the prediction parameters.
using prediction_types = Filter::prediction_types;
};

template <typename Filter> struct conditional_state_transition {};

template <has_state_transition Filter>
Expand All @@ -186,15 +202,24 @@ struct conditional_state_transition<Filter> {
using state_transition = Filter::state_transition;
};

template <typename Filter> struct conditional_update_types {};

template <has_update_types Filter> struct conditional_update_types<Filter> {
//! @brief Pack of the update parameters.
using update_types = Filter::update_types;
};

// The only way to have a conditional member type is to inherit from a template
// specialization on the member type.
template <typename Filter>
struct conditional_member_types : public conditional_input<Filter>,
conditional_input_control<Filter>,
struct conditional_member_types : public conditional_input_control<Filter>,
conditional_input<Filter>,
conditional_output_model<Filter>,
conditional_process_uncertainty<Filter>,
conditional_output_uncertainty<Filter>,
conditional_state_transition<Filter> {};
conditional_prediction_types<Filter>,
conditional_process_uncertainty<Filter>,
conditional_state_transition<Filter>,
conditional_update_types<Filter> {};

template <typename...> struct pack {};

Expand Down
3 changes: 2 additions & 1 deletion include/fcarouge/kalman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ For more information, please refer to <https://unlicense.org> */

#include "internal/factory.hpp"
#include "internal/format.hpp"
#include "printer.hpp"
#include "utility.hpp"

#include <concepts>
Expand Down Expand Up @@ -131,7 +132,7 @@ namespace fcarouge {
//! https://arxiv.org/pdf/1905.13002.pdf ? GPU implementation? Parallel
//! implementation?
template <typename Filter>
class kalman final : public internal::conditional_member_types<Filter> {
class kalman : public internal::conditional_member_types<Filter> {
private:
//! @name Private Member Types
//! @{
Expand Down
96 changes: 96 additions & 0 deletions include/fcarouge/printer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
Kalman Filter
Version 0.4.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_PRINTER_HPP
#define FCAROUGE_PRINTER_HPP

#include <print>

namespace fcarouge {
namespace decorator {
template <typename Filter> class printer : public Filter {
public:
using Filter::p;
using Filter::x;

inline constexpr explicit printer([[maybe_unused]] Filter &&filter)
: Filter{std::forward<Filter>(filter)} {
std::println("{{\"event\": \"construction\", \"filter\":{}}}", *this);
}

inline constexpr ~printer() {
std::println("{{\"event\": \"destruction\", \"filter\":{}}}", *this);
}

inline constexpr void x(const auto &value, const auto &...values) {
Filter::x(value, values...);
std::println("{{\"event\": \"x\", \"filter\":{}}}", *this);
}

//! @todo Implement the remaining events.
inline constexpr void p(const auto &value, const auto &...values) {
Filter::p(value, values...);
std::println("{{\"event\": \"p\", \"filter\":{}}}", *this);
}

inline constexpr void predict(const auto &...arguments) {
Filter::predict(arguments...);
std::println("{{\"event\": \"predict\", \"filter\":{}}}", *this);
}

inline constexpr void update(const auto &...arguments) {
Filter::update(arguments...);
std::println("{{\"event\": \"update\", \"filter\":{}}}", *this);
}
};
} // namespace decorator

//! @todo Support optional output streams?
struct printer_decorator {};

inline constexpr printer_decorator printer;

template <typename Filter>
inline constexpr auto
operator|(Filter &&filter, [[maybe_unused]] const printer_decorator decorator) {
return decorator::printer<Filter>(std::forward<Filter>(filter));
}

} // namespace fcarouge

#endif // FCAROUGE_PRINTER_HPP
23 changes: 20 additions & 3 deletions include/fcarouge/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ 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.
//!
Expand Down Expand Up @@ -92,6 +97,14 @@ concept has_process_uncertainty = internal::has_process_uncertainty<Filter>;
template <typename Filter>
concept has_output_uncertainty = internal::has_output_uncertainty<Filter>;

//! @brief Filter prediction pack support concept.
//!
//! @details The filter supports the prediction parameters related
//! functionality: `prediction_types` type member and parameters for the
//! `predict()` method.
template <typename Filter>
concept has_prediction_types = internal::has_prediction_types<Filter>;

//! @brief Filter input control support concept.
//!
//! @details The filter supports the input control related functionality:
Expand All @@ -106,6 +119,13 @@ concept has_input_control = internal::has_input_control<Filter>;
template <typename Filter>
concept has_state_transition = internal::has_state_transition<Filter>;

//! @brief Filter update pack support concept.
//!
//! @details The filter supports the update parameters related functionality:
//! `update_types` type member and parameters for the `update()` method.
template <typename Filter>
concept has_update_types = internal::has_update_types<Filter>;

//! @brief Filter output model support concept.
//!
//! @details The filter supports the output model related functionality:
Expand All @@ -116,7 +136,6 @@ concept has_output_model = internal::has_output_model<Filter>;

//! @name Types
//! @{

//! @brief Tuple-like pack type.
//!
//! @details An alternative to tuple-like types.
Expand Down Expand Up @@ -145,7 +164,6 @@ using quotient = internal::quotient<Numerator, Denominator>;

//! @name Functions
//! @{

//! @brief A user-definable algebraic division solution.
//!
//! @details Implemented for known libraries. User-definable in other cases.
Expand All @@ -159,7 +177,6 @@ constexpr auto operator/(const Numerator &lhs, const Denominator &rhs)

//! @name Algebraic Named Values
//! @{

//! @brief The identity matrix.
//!
//! @details User-defined.
Expand Down
6 changes: 4 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ foreach(
"kalman_format_arguments.cpp"
"kalman_format_float_1x1x1.cpp"
"kalman_format.cpp"
"kalman_println_1x1x0.cpp")
"kalman_println_1x1x0.cpp"
"printer_1x1x0.cpp")
get_filename_component(NAME ${TEST} NAME_WE)
add_executable(kalman_test_${NAME}_driver ${TEST})
target_link_libraries(kalman_test_${NAME}_driver PRIVATE kalman kalman_main
Expand Down Expand Up @@ -104,7 +105,8 @@ foreach(BACKEND IN ITEMS "eigen")
"kalman_f_5x4x3.cpp"
"kalman_h_5x4x3.cpp"
"kalman_format_1x4x3.cpp"
"kalman_format_5x4x3.cpp")
"kalman_format_5x4x3.cpp"
"printer_2x3x4.cpp")
get_filename_component(NAME ${TEST} NAME_WE)
add_executable(kalman_test_${BACKEND}_${NAME}_driver ${TEST})
target_link_libraries(
Expand Down
70 changes: 70 additions & 0 deletions test/printer_1x1x0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
Kalman Filter
Version 0.4.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> */

#include "fcarouge/kalman.hpp"

#include <cassert>
#include <cmath>

namespace fcarouge::sample {
namespace {
//! @brief Verifies the printer adaptor for single-dimension filters.
[[maybe_unused]] auto sample{[] {
auto filter{kalman{state{60.}, output<double>, estimate_uncertainty{225.},
output_uncertainty{25.}} |
printer};

filter.update(48.54);
filter.update(47.11);
filter.update(55.01);
filter.update(55.15);
filter.update(49.89);
filter.update(40.85);
filter.update(46.72);
filter.update(50.05);
filter.update(51.27);
filter.update(49.95);

assert(std::abs(1 - filter.x() / 49.57) < 0.001 &&
"After 10 measurement and update iterations, the building estimated "
"height is: 49.57m.");

return 0;
}()};
} // namespace
} // namespace fcarouge::sample
Loading

0 comments on commit 6151e88

Please sign in to comment.