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

[decorator] add printer support #409

Merged
merged 1 commit into from
Oct 30, 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
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
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
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
61 changes: 61 additions & 0 deletions test/printer_2x3x4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|

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 "fcarouge/linalg.hpp"

#include <cassert>
#include <cmath>

namespace fcarouge::sample {
namespace {
template <auto Size> using vector = column_vector<double, Size>;

//! @brief Verifies the printer adaptor for multi-dimension filters with input
//! control without additional arguments.
[[maybe_unused]] auto sample{[] {
auto filter{kalman{state{vector<5>{0., 0., 0., 0., 0.}}, output<vector<4>>,
input<vector<3>>} |
printer};

filter.predict(0., 0., 0.);

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