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 27, 2023
1 parent 643b640 commit e52994d
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 25 deletions.
1 change: 1 addition & 0 deletions include/fcarouge/internal/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ For more information, please refer to <https://unlicense.org> */
#include <format>
#include <type_traits>

//! @todo Type erase this to support all matching types?
namespace fcarouge {
template <typename, typename, typename, typename, typename> class kalman;
} // namespace fcarouge
Expand Down
2 changes: 2 additions & 0 deletions linalg/eigen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org> ]]

include(FetchContent)

FetchContent_Declare(
eigen
GIT_REPOSITORY "https://gitlab.com/libeigen/eigen"
Expand Down
20 changes: 16 additions & 4 deletions sample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org> ]]

include(FetchContent)
add_library(kalman_utility INTERFACE)
target_sources(
kalman_utility
INTERFACE FILE_SET
"kalman_utility_headers"
TYPE
"HEADERS"
FILES
"printer.hpp"
"plotter.hpp")
target_link_libraries(kalman_utility INTERFACE kalman_print)

foreach(SAMPLE "kf_1x1x0_building_height.cpp" "kf_1x1x0_liquid_temperature.cpp"
"kf_1x1x1_dog_position.cpp")
Expand All @@ -47,8 +57,9 @@ foreach(SAMPLE "kf_1x1x0_building_height.cpp" "kf_1x1x0_liquid_temperature.cpp"
PROPERTIES CXX_STANDARD 23
CXX_EXTENSIONS OFF
INTERPROCEDURAL_OPTIMIZATION TRUE)
target_link_libraries(kalman_sample_${NAME}_driver PRIVATE kalman kalman_main
kalman_options)
target_link_libraries(
kalman_sample_${NAME}_driver PRIVATE kalman kalman_main kalman_options
kalman_utility)
separate_arguments(TEST_COMMAND UNIX_COMMAND $ENV{COMMAND})
add_test(NAME kalman_sample_${NAME}
COMMAND ${TEST_COMMAND} $<TARGET_FILE:kalman_sample_${NAME}_driver>)
Expand All @@ -67,7 +78,8 @@ foreach(BACKEND IN ITEMS "eigen")
INTERPROCEDURAL_OPTIMIZATION TRUE)
target_link_libraries(
kalman_sample_${BACKEND}_${NAME}_driver
PRIVATE kalman kalman_main kalman_linalg_${BACKEND} kalman_options)
PRIVATE kalman kalman_main kalman_linalg_${BACKEND} kalman_options
kalman_utility)
separate_arguments(TEST_COMMAND UNIX_COMMAND $ENV{COMMAND})
add_test(NAME kalman_sample_${BACKEND}_${NAME}
COMMAND ${TEST_COMMAND}
Expand Down
12 changes: 8 additions & 4 deletions sample/kf_1x1x0_building_height.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include "fcarouge/kalman.hpp"
#include "plotter.hpp"
#include "printer.hpp"

#include <cassert>
#include <cmath>

#include <iostream>

namespace fcarouge::sample {
namespace {
//! @brief Estimating the height of a building.
Expand All @@ -24,7 +28,7 @@ namespace {
//! @example kf_1x1x0_building_height.cpp
[[maybe_unused]] auto sample{[] {
// A one-dimensional filter, constant system dynamic model.
kalman filter;
auto filter{printer{kalman{}}};

// Initialization
// One can estimate the building height simply by looking at it. The estimated
Expand Down Expand Up @@ -68,9 +72,9 @@ namespace {

// After 10 measurements the filter estimates the height of the building
// at 49.57m.
assert(std::abs(1 - filter.x() / 49.57) < 0.001 &&
"After 10 measurement and update iterations, the building estimated "
"height is: 49.57m.");
// 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;
}()};
Expand Down
130 changes: 130 additions & 0 deletions sample/plotter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
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_PLOTTER_HPP
#define FCAROUGE_PLOTTER_HPP

//! @file
//! @brief ...
//!
//! @details ...

// #include "internal/utility.hpp"

namespace fcarouge {

// Decorator?
template <typename KalmanFilter> class plotter final {

public:
using state = typename KalmanFilter::state;
using output = typename KalmanFilter::output;
using input = typename KalmanFilter::input;
using estimate_uncertainty = typename KalmanFilter::estimate_uncertainty;
using process_uncertainty = typename KalmanFilter::process_uncertainty;
using output_uncertainty = typename KalmanFilter::output_uncertainty;
using state_transition = typename KalmanFilter::state_transition;
using output_model = typename KalmanFilter::output_model;
using input_control = typename KalmanFilter::input_control;
using gain = typename KalmanFilter::gain;
using innovation = typename KalmanFilter::innovation;
using innovation_uncertainty = typename KalmanFilter::innovation_uncertainty;

inline constexpr explicit plotter(KalmanFilter &&kalman_filter)
: filter{std::forward<KalmanFilter>(kalman_filter)} {}

inline constexpr plotter(plotter &&other) noexcept = default;
inline constexpr auto operator=(plotter &&other) noexcept
-> plotter & = default;

// inline constexpr ~plotter() {}

inline constexpr auto x() const -> const state &;
inline constexpr auto x() -> state & { return filter.x(); }
inline constexpr void x(const auto &value, const auto &...values) {
filter.x(value, values...);
}
// inline constexpr auto z() const -> const output &;
// inline constexpr auto u() const
// -> const input &requires(not std::is_same_v<Input, void>);
inline constexpr auto p() const -> const estimate_uncertainty &;
inline constexpr auto p() -> estimate_uncertainty & { return filter.p(); }
inline constexpr void p(const auto &value, const auto &...values) {
filter.p(value, values...);
}
inline constexpr auto q() const -> const process_uncertainty &;
inline constexpr auto q() -> process_uncertainty &;
inline constexpr void q(const auto &value, const auto &...values);
inline constexpr auto r() const -> const output_uncertainty &;
inline constexpr auto r() -> output_uncertainty &;
inline constexpr void r(const auto &value, const auto &...values) {
filter.r(value, values...);
}
// inline constexpr auto f() const -> const state_transition &;
// inline constexpr auto f() -> state_transition &;
// inline constexpr void f(const auto &value, const auto &...values);
// inline constexpr auto h() const -> const output_model &;
// inline constexpr auto h() -> output_model &;
// inline constexpr void h(const auto &value, const auto &...values);
// inline constexpr auto g() const
// -> const input_control &requires(not std::is_same_v<Input, void>);
// inline constexpr auto g()
// -> input_control &requires(not std::is_same_v<Input, void>);
// inline constexpr void g(const auto &value, const auto &...values)
// requires(not std::is_same_v<Input, void>);
// inline constexpr auto k() const -> const gain &;
// inline constexpr auto y() const -> const innovation &;
// inline constexpr auto s() const -> const innovation_uncertainty &;
// inline constexpr void transition(const auto &callable);
// inline constexpr void observation(const auto &callable);
inline constexpr void predict(const auto &...arguments) {
filter.predict(arguments...);
}
// template <std::size_t Position> inline constexpr auto predict() const;
inline constexpr void update(const auto &...arguments) {
filter.update(arguments...);
}
// template <std::size_t Position> inline constexpr auto update() const;

private:
KalmanFilter filter;
};

} // namespace fcarouge

#endif // FCAROUGE_PLOTTER_HPP
143 changes: 143 additions & 0 deletions sample/printer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
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_PRINTER_HPP
#define FCAROUGE_PRINTER_HPP

//! @file
//! @brief ...
//!
//! @details ...

// #include "internal/utility.hpp"
#include <iostream>
#include <ostream>
#include <print>

namespace fcarouge {

// Decorator?
template <typename KalmanFilter> class printer final {

public:
using state = typename KalmanFilter::state;
using output = typename KalmanFilter::output;
using input = typename KalmanFilter::input;
using estimate_uncertainty = typename KalmanFilter::estimate_uncertainty;
using process_uncertainty = typename KalmanFilter::process_uncertainty;
using output_uncertainty = typename KalmanFilter::output_uncertainty;
using state_transition = typename KalmanFilter::state_transition;
using output_model = typename KalmanFilter::output_model;
using input_control = typename KalmanFilter::input_control;
using gain = typename KalmanFilter::gain;
using innovation = typename KalmanFilter::innovation;
using innovation_uncertainty = typename KalmanFilter::innovation_uncertainty;

inline constexpr explicit printer(KalmanFilter &&kalman_filter)
: filter{std::forward<KalmanFilter>(kalman_filter)} {
std::println("{{\"event\": \"construction\", \"filter\":{}}}", filter);
}

inline constexpr printer(printer &&other) noexcept = default;
inline constexpr auto operator=(printer &&other) noexcept
-> printer & = default;

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

inline constexpr auto x() const -> const state &;
inline constexpr auto x() -> state & { return filter.x(); }
inline constexpr void x(const auto &value, const auto &...values) {
filter.x(value, values...);
std::println("{{\"event\": \"x\", \"filter\":{}}}", filter);
}
// inline constexpr auto z() const -> const output &;
// inline constexpr auto u() const
// -> const input &requires(not std::is_same_v<Input, void>);
inline constexpr auto p() const -> const estimate_uncertainty &;
inline constexpr auto p() -> estimate_uncertainty & { return filter.p(); }
inline constexpr void p(const auto &value, const auto &...values) {
filter.p(value, values...);
std::println("{{\"event\": \"p\", \"filter\":{}}}", filter);
}
inline constexpr auto q() const -> const process_uncertainty &;
inline constexpr auto q() -> process_uncertainty &;
inline constexpr void q(const auto &value, const auto &...values);
inline constexpr auto r() const -> const output_uncertainty &;
inline constexpr auto r() -> output_uncertainty &;
inline constexpr void r(const auto &value, const auto &...values) {
filter.r(value, values...);
std::println("{{\"event\": \"r\", \"filter\":{}}}", filter);
}
// inline constexpr auto f() const -> const state_transition &;
// inline constexpr auto f() -> state_transition &;
// inline constexpr void f(const auto &value, const auto &...values);
// inline constexpr auto h() const -> const output_model &;
// inline constexpr auto h() -> output_model &;
// inline constexpr void h(const auto &value, const auto &...values);
// inline constexpr auto g() const
// -> const input_control &requires(not std::is_same_v<Input, void>);
// inline constexpr auto g()
// -> input_control &requires(not std::is_same_v<Input, void>);
// inline constexpr void g(const auto &value, const auto &...values)
// requires(not std::is_same_v<Input, void>);
// inline constexpr auto k() const -> const gain &;
// inline constexpr auto y() const -> const innovation &;
// inline constexpr auto s() const -> const innovation_uncertainty &;
// inline constexpr void transition(const auto &callable);
// inline constexpr void observation(const auto &callable);
inline constexpr void predict(const auto &...arguments) {
filter.predict(arguments...);
std::println("{{\"event\": \"predict\", \"filter\":{}}}", filter);
}
// template <std::size_t Position> inline constexpr auto predict() const;
inline constexpr void update(const auto &...arguments) {
filter.update(arguments...);
std::println("{{\"event\": \"update\", \"filter\":{}}}", filter);
}
// template <std::size_t Position> inline constexpr auto update() const;

private:
KalmanFilter filter;
};

} // namespace fcarouge

#endif // FCAROUGE_PRINTER_HPP
10 changes: 10 additions & 0 deletions support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ endif()
include(CheckSourceCompiles)
include(FetchContent)

add_library(kalman_print INTERFACE)
target_sources(kalman_print INTERFACE FILE_SET "kalman_print_headers" TYPE
"HEADERS" FILES "print")

install(
TARGETS kalman_print
EXPORT "kalman-target"
FILE_SET "kalman_print_headers"
DESTINATION "include/fcarouge")

add_library(kalman_generator INTERFACE)

check_source_compiles(
Expand Down
Loading

0 comments on commit e52994d

Please sign in to comment.