From 54d5f3eb216f70772aa350a1a174e48359588fd0 Mon Sep 17 00:00:00 2001 From: Peter Urban Date: Wed, 30 Oct 2024 23:15:35 +0100 Subject: [PATCH] removed non-const interpolation functions --- meson.build | 2 +- .../c_akimainterpolator.cpp | 11 +- .../c_linearinterpolator.cpp | 10 +- .../c_nearestinterpolator.cpp | 11 +- .../c_slerpinterpolator.cpp | 21 +-- src/tests/vectorinterpolators/linear.test.cpp | 14 +- .../vectorinterpolators/nearest.test.cpp | 10 +- .../.docstrings/akimainterpolator.doc.hpp | 4 +- .../.docstrings/i_interpolator.doc.hpp | 2 +- .../.docstrings/i_pairinterpolator.doc.hpp | 6 +- .../.docstrings/slerpinterpolator.doc.hpp | 14 +- .../vectorinterpolators/akimainterpolator.hpp | 12 +- .../vectorinterpolators/i_interpolator.hpp | 4 +- .../i_pairinterpolator.hpp | 133 ++---------------- .../vectorinterpolators/slerpinterpolator.hpp | 52 ++----- 15 files changed, 68 insertions(+), 238 deletions(-) diff --git a/meson.build b/meson.build index d84aad0..d41f480 100644 --- a/meson.build +++ b/meson.build @@ -9,7 +9,7 @@ project( 'cpp', license: 'MPL-2.0', - version: '0.27.0', + version: '0.28.0', default_options: ['warning_level=2', 'buildtype=release', 'cpp_std=c++20'], meson_version: '>=1.3.2' #first version with clang-cl openmp support, ) diff --git a/src/pymodule/vectorinterpolators/c_akimainterpolator.cpp b/src/pymodule/vectorinterpolators/c_akimainterpolator.cpp index 2894cb9..73cc98c 100644 --- a/src/pymodule/vectorinterpolators/c_akimainterpolator.cpp +++ b/src/pymodule/vectorinterpolators/c_akimainterpolator.cpp @@ -35,15 +35,16 @@ void init_akimainterpolator(pybind11::module& m, const std::string& name) py::arg("Y") = std::vector({}), py::arg("extrapolation_mode") = t_extr_mode::extrapolate) .def("__call__", - py::overload_cast(&t_AkimaInterpolator::operator()), + py::overload_cast(&t_AkimaInterpolator::operator(), py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, I_Interpolator, operator_call), py::arg("target_x")) - .def("get_y_const", - py::overload_cast(&t_AkimaInterpolator::get_y_const, py::const_), - DOC(themachinethatgoesping, tools, vectorinterpolators, AkimaInterpolator, get_y_const), + .def("get_y", + py::overload_cast(&t_AkimaInterpolator::get_y, py::const_), + DOC(themachinethatgoesping, tools, vectorinterpolators, AkimaInterpolator, get_y), py::arg("target_x")) .def("__call__", - py::overload_cast&>(&t_AkimaInterpolator::operator()), + py::overload_cast&>(&t_AkimaInterpolator::operator(), + py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, diff --git a/src/pymodule/vectorinterpolators/c_linearinterpolator.cpp b/src/pymodule/vectorinterpolators/c_linearinterpolator.cpp index 829e9df..d70eaf9 100644 --- a/src/pymodule/vectorinterpolators/c_linearinterpolator.cpp +++ b/src/pymodule/vectorinterpolators/c_linearinterpolator.cpp @@ -37,15 +37,15 @@ void init_linearinterpolator(pybind11::module& m, const std::string& name) py::arg("Y") = std::vector({}), py::arg("extrapolation_mode") = t_extr_mode::extrapolate) .def("__call__", - py::overload_cast(&t_LinearInterpolator::operator()), + py::overload_cast(&t_LinearInterpolator::operator(), py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, I_Interpolator, operator_call), py::arg("target_x")) - .def("get_y_const", - py::overload_cast(&t_LinearInterpolator::get_y_const, py::const_), - DOC(themachinethatgoesping, tools, vectorinterpolators, I_PairInterpolator, get_y_const), + .def("get_y", + py::overload_cast(&t_LinearInterpolator::get_y, py::const_), + DOC(themachinethatgoesping, tools, vectorinterpolators, I_PairInterpolator, get_y), py::arg("target_x")) .def("__call__", - py::overload_cast&>(&t_LinearInterpolator::operator()), + py::overload_cast&>(&t_LinearInterpolator::operator(), py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, diff --git a/src/pymodule/vectorinterpolators/c_nearestinterpolator.cpp b/src/pymodule/vectorinterpolators/c_nearestinterpolator.cpp index 67a05b1..b833dbb 100644 --- a/src/pymodule/vectorinterpolators/c_nearestinterpolator.cpp +++ b/src/pymodule/vectorinterpolators/c_nearestinterpolator.cpp @@ -37,15 +37,16 @@ void init_nearestinterpolator(pybind11::module& m, const std::string& name) py::arg("Y") = std::vector({}), py::arg("extrapolation_mode") = t_extr_mode::extrapolate) .def("__call__", - py::overload_cast(&t_NearestInterpolator::operator()), + py::overload_cast(&t_NearestInterpolator::operator(), py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, I_Interpolator, operator_call), py::arg("target_x")) - .def("get_y_const", - py::overload_cast(&t_NearestInterpolator::get_y_const, py::const_), - DOC(themachinethatgoesping, tools, vectorinterpolators, I_PairInterpolator, get_y_const), + .def("get_y", + py::overload_cast(&t_NearestInterpolator::get_y, py::const_), + DOC(themachinethatgoesping, tools, vectorinterpolators, I_PairInterpolator, get_y), py::arg("target_x")) .def("__call__", - py::overload_cast&>(&t_NearestInterpolator::operator()), + py::overload_cast&>(&t_NearestInterpolator::operator(), + py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, diff --git a/src/pymodule/vectorinterpolators/c_slerpinterpolator.cpp b/src/pymodule/vectorinterpolators/c_slerpinterpolator.cpp index 555925b..582c2c6 100644 --- a/src/pymodule/vectorinterpolators/c_slerpinterpolator.cpp +++ b/src/pymodule/vectorinterpolators/c_slerpinterpolator.cpp @@ -46,36 +46,27 @@ void init_slerpinterpolator(pybind11::module& m, const std::string& name) &t_SlerpInterpolator::empty, DOC(themachinethatgoesping, tools, vectorinterpolators, I_PairInterpolator, empty)) .def("__call__", - py::overload_cast(&t_SlerpInterpolator::ypr), + py::overload_cast(&t_SlerpInterpolator::ypr, py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, SlerpInterpolator, ypr), py::arg("target_x"), py::arg("output_in_degrees") = true) .def("__call__", - py::overload_cast&, bool>(&t_SlerpInterpolator::ypr), + py::overload_cast&, bool>(&t_SlerpInterpolator::ypr, + py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, SlerpInterpolator, ypr_2), py::arg("targets_x"), py::arg("output_in_degrees") = true) .def("ypr", - py::overload_cast(&t_SlerpInterpolator::ypr), + py::overload_cast(&t_SlerpInterpolator::ypr, py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, SlerpInterpolator, ypr), py::arg("target_x"), py::arg("output_in_degrees") = true) .def("ypr", - py::overload_cast&, bool>(&t_SlerpInterpolator::ypr), + py::overload_cast&, bool>(&t_SlerpInterpolator::ypr, + py::const_), DOC(themachinethatgoesping, tools, vectorinterpolators, SlerpInterpolator, ypr_2), py::arg("targets_x"), py::arg("output_in_degrees") = true) - .def("ypr_const", - py::overload_cast(&t_SlerpInterpolator::ypr_const, py::const_), - DOC(themachinethatgoesping, tools, vectorinterpolators, SlerpInterpolator, ypr_const), - py::arg("target_x"), - py::arg("output_in_degrees") = true) - // .def( - // "ypr_const", - // py::overload_cast&, bool>(&t_SlerpInterpolator::ypr_const, py::const_), - // DOC(themachinethatgoesping, tools, vectorinterpolators, SlerpInterpolator, ypr_const_2), - // py::arg("targets_x"), - // py::arg("output_in_degrees") = true) .def("set_extrapolation_mode", &t_SlerpInterpolator::set_extrapolation_mode, DOC(themachinethatgoesping, diff --git a/src/tests/vectorinterpolators/linear.test.cpp b/src/tests/vectorinterpolators/linear.test.cpp index e2982a3..0aaba50 100644 --- a/src/tests/vectorinterpolators/linear.test.cpp +++ b/src/tests/vectorinterpolators/linear.test.cpp @@ -39,7 +39,7 @@ TEST_CASE("LinearInterpolator: should perform basic interpolations correctly", T SECTION("const interpolation should produce the same results as classic interpolation") { for (double x_val = -10; x_val <= 12; x_val += 0.1) - REQUIRE(interpolator(x_val) == Catch::Approx(interpolator.get_y_const(x_val))); + REQUIRE(interpolator(x_val) == Catch::Approx(interpolator.get_y(x_val))); } SECTION("preset values should be interpolated correctly") @@ -82,8 +82,8 @@ TEST_CASE("LinearInterpolator: should perform basic interpolations correctly", T { REQUIRE_THROWS_AS(interpolator(-11), std::out_of_range); REQUIRE_THROWS_AS(interpolator(13), std::out_of_range); - REQUIRE_THROWS_AS(interpolator.get_y_const(-11), std::out_of_range); - REQUIRE_THROWS_AS(interpolator.get_y_const(13), std::out_of_range); + REQUIRE_THROWS_AS(interpolator.get_y(-11), std::out_of_range); + REQUIRE_THROWS_AS(interpolator.get_y(13), std::out_of_range); } break; @@ -92,8 +92,8 @@ TEST_CASE("LinearInterpolator: should perform basic interpolations correctly", T { REQUIRE(interpolator(-11) == Catch::Approx(1)); REQUIRE(interpolator(13) == Catch::Approx(y_append)); - REQUIRE(interpolator.get_y_const(-11) == Catch::Approx(1)); - REQUIRE(interpolator.get_y_const(13) == Catch::Approx(y_append)); + REQUIRE(interpolator.get_y(-11) == Catch::Approx(1)); + REQUIRE(interpolator.get_y(13) == Catch::Approx(y_append)); } break; @@ -101,8 +101,8 @@ TEST_CASE("LinearInterpolator: should perform basic interpolations correctly", T SECTION(" - extrapolation in all other cases") REQUIRE(interpolator(-11) == Catch::Approx(1.2)); REQUIRE(interpolator(14) == Catch::Approx(-4 / 3.)); - REQUIRE(interpolator.get_y_const(-11) == Catch::Approx(1.2)); - REQUIRE(interpolator.get_y_const(14) == Catch::Approx(-4 / 3.)); + REQUIRE(interpolator.get_y(-11) == Catch::Approx(1.2)); + REQUIRE(interpolator.get_y(14) == Catch::Approx(-4 / 3.)); break; } diff --git a/src/tests/vectorinterpolators/nearest.test.cpp b/src/tests/vectorinterpolators/nearest.test.cpp index d87fbf9..b1156ae 100644 --- a/src/tests/vectorinterpolators/nearest.test.cpp +++ b/src/tests/vectorinterpolators/nearest.test.cpp @@ -45,7 +45,7 @@ TEST_CASE("NearestInterpolator: should perform basic interpolations correctly", SECTION("const interpolation should produce the same results as classic interpolation") { for (double x_val = -10; x_val <= 12; x_val += 0.1) - REQUIRE(interpolator(x_val) == Catch::Approx(interpolator.get_y_const(x_val))); + REQUIRE(interpolator(x_val) == Catch::Approx(interpolator.get_y(x_val))); } REQUIRE(interpolator.binary_hash() == 10074301266414863605ULL); // lookup should not change the hash @@ -95,8 +95,8 @@ TEST_CASE("NearestInterpolator: should perform basic interpolations correctly", { REQUIRE_THROWS_AS(interpolator(-11), std::out_of_range); REQUIRE_THROWS_AS(interpolator(13), std::out_of_range); - REQUIRE_THROWS_AS(interpolator.get_y_const(-11), std::out_of_range); - REQUIRE_THROWS_AS(interpolator.get_y_const(13), std::out_of_range); + REQUIRE_THROWS_AS(interpolator.get_y(-11), std::out_of_range); + REQUIRE_THROWS_AS(interpolator.get_y(13), std::out_of_range); } break; @@ -104,8 +104,8 @@ TEST_CASE("NearestInterpolator: should perform basic interpolations correctly", SECTION(" - nearest extrapolation in all other cases") REQUIRE(interpolator(-11) == Catch::Approx(1)); REQUIRE(interpolator(13) == Catch::Approx(y_append)); - REQUIRE(interpolator.get_y_const(-11) == Catch::Approx(1)); - REQUIRE(interpolator.get_y_const(13) == Catch::Approx(y_append)); + REQUIRE(interpolator.get_y(-11) == Catch::Approx(1)); + REQUIRE(interpolator.get_y(13) == Catch::Approx(y_append)); break; } } diff --git a/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/akimainterpolator.doc.hpp b/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/akimainterpolator.doc.hpp index 1543890..10983c3 100644 --- a/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/akimainterpolator.doc.hpp +++ b/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/akimainterpolator.doc.hpp @@ -1,4 +1,4 @@ -//sourcehash: ed064a674b2936cc83bd14ff3ef722ec0d7671d201bb532268476ded39e7ef9d +//sourcehash: f2c90401a43df0683bdb25e8be6b64044cb280f9e2d9239f80cf43b92538bf3d /* This file contains docstrings for use in the Python bindings. @@ -109,7 +109,7 @@ R"doc(return the y component of the internal data vector Returns: std::vector)doc"; -static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_AkimaInterpolator_get_y_const = R"doc()doc"; +static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_AkimaInterpolator_get_y = R"doc()doc"; static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_AkimaInterpolator_info_string = R"doc(\ return an info string using the class __printer__ object \ diff --git a/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/i_interpolator.doc.hpp b/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/i_interpolator.doc.hpp index 691a69f..ea9cd8d 100644 --- a/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/i_interpolator.doc.hpp +++ b/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/i_interpolator.doc.hpp @@ -1,4 +1,4 @@ -//sourcehash: 5ddaae068f5b36105f921597593f0f8bc2e9ce00931d815570cf341fe2594edc +//sourcehash: f64d021ec0ed3dd9c872f966453efc20b45dd917ee1e2e6c5776bf11f1bc68d1 /* This file contains docstrings for use in the Python bindings. diff --git a/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/i_pairinterpolator.doc.hpp b/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/i_pairinterpolator.doc.hpp index b53b906..2a20e12 100644 --- a/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/i_pairinterpolator.doc.hpp +++ b/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/i_pairinterpolator.doc.hpp @@ -1,4 +1,4 @@ -//sourcehash: fa4fc2190912b87676f5155f663b329167d6494b5e38cf6990e6ba62caee7ba5 +//sourcehash: 2ea9486c0b7d77753818c25eb8f9084cb585df0bdaf3189a62ff0a0514e56ee8 /* This file contains docstrings for use in the Python bindings. @@ -97,7 +97,7 @@ R"doc(return the y component of the internal data vector Returns: std::vector)doc"; -static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_I_PairInterpolator_get_y_const = R"doc()doc"; +static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_I_PairInterpolator_get_y = R"doc()doc"; static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_I_PairInterpolator_insert = R"doc()doc"; @@ -118,8 +118,6 @@ Parameter ``y1``: Returns: interpolated y value)doc"; -static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_I_PairInterpolator_last_x_pair = R"doc(< last pair (for faster consecutive searches))doc"; - static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_I_PairInterpolator_operator_call = R"doc(get the interpolated y value for given x target diff --git a/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/slerpinterpolator.doc.hpp b/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/slerpinterpolator.doc.hpp index 4b3b971..395be18 100644 --- a/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/slerpinterpolator.doc.hpp +++ b/src/themachinethatgoesping/tools/vectorinterpolators/.docstrings/slerpinterpolator.doc.hpp @@ -1,4 +1,4 @@ -//sourcehash: 16fdb3d053d0dae3ec48396a5d10edefb693b0fa9a0400a99a93b74e86e63cdc +//sourcehash: d8b630d9b4aab99aa6397befd5baa274e95d3d0e0244a255c4e1f77f9c7e5df6 /* This file contains docstrings for use in the Python bindings. @@ -353,18 +353,6 @@ Parameter ``output_in_degrees``: Returns: corresponding y value)doc"; -static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_SlerpInterpolator_ypr_const = -R"doc(get the interpolated yaw, pitch and roll values for given x target - -Parameter ``target_x``: - find the corresponding y value for this x value - -Parameter ``output_in_degrees``: - if true, yaw pitch and roll input values are in ° otherwise rad - -Returns: - corresponding y value)doc"; - #if defined(__GNUG__) #pragma GCC diagnostic pop #endif diff --git a/src/themachinethatgoesping/tools/vectorinterpolators/akimainterpolator.hpp b/src/themachinethatgoesping/tools/vectorinterpolators/akimainterpolator.hpp index 86a00b7..92ae7ed 100644 --- a/src/themachinethatgoesping/tools/vectorinterpolators/akimainterpolator.hpp +++ b/src/themachinethatgoesping/tools/vectorinterpolators/akimainterpolator.hpp @@ -117,11 +117,11 @@ class AkimaInterpolator : public I_Interpolator return true; } - XYType get_y_const(XYType target_x) const + XYType get_y(XYType target_x) const { // if less than 4 values are present, act as linear interpolator if (_X.size() < 4) - return _min_linearextrapolator.get_y_const(target_x); + return _min_linearextrapolator.get_y(target_x); // check if _X (and _Y) are initialized (_X and _Y should always be the same size) if (_X.size() != _Y.size()) @@ -136,7 +136,7 @@ class AkimaInterpolator : public I_Interpolator return _Y[0]; case t_extr_mode::extrapolate: - return _min_linearextrapolator.get_y_const(target_x); + return _min_linearextrapolator.get_y(target_x); default: // fail // throw std::out_of_range("ERROR[INTERPOLATE]: x value out of range (too small), " @@ -157,7 +157,7 @@ class AkimaInterpolator : public I_Interpolator return _Y.back(); case t_extr_mode::extrapolate: - return _max_linearextrapolator.get_y_const(target_x); + return _max_linearextrapolator.get_y(target_x); default: // fail // throw std::out_of_range("ERROR[INTERPOLATE]: x value out of range (too large), " @@ -180,7 +180,7 @@ class AkimaInterpolator : public I_Interpolator * @param target_x find the corresponding y value for this x value * @return corresponding y value */ - XYType operator()(XYType target_x) final { return get_y_const(target_x); } + XYType operator()(XYType target_x) const final { return get_y(target_x); } /** * @brief get nearest y values for given x targets (vectorized call) @@ -189,7 +189,7 @@ class AkimaInterpolator : public I_Interpolator * corrsponding y value * @return corresponding y value */ - std::vector operator()(const std::vector& targetsX) + std::vector operator()(const std::vector& targetsX) const { return I_Interpolator::operator()(targetsX); } diff --git a/src/themachinethatgoesping/tools/vectorinterpolators/i_interpolator.hpp b/src/themachinethatgoesping/tools/vectorinterpolators/i_interpolator.hpp index ffd2904..21a7a4a 100644 --- a/src/themachinethatgoesping/tools/vectorinterpolators/i_interpolator.hpp +++ b/src/themachinethatgoesping/tools/vectorinterpolators/i_interpolator.hpp @@ -142,7 +142,7 @@ class I_Interpolator * @param target_x find the corresponding y value for this x value * @return corresponding y value */ - virtual YType operator()(XType target_x) = 0; + virtual YType operator()(XType target_x) const = 0; /** * @brief get nearest y values for given x targets (vectorized call) @@ -150,7 +150,7 @@ class I_Interpolator * @param targets_x vector of x values. For each of these values find the corrsponding y value * @return corresponding y value */ - std::vector operator()(const std::vector& targetsX) + std::vector operator()(const std::vector& targetsX) const { std::vector y_values; y_values.reserve(targetsX.size()); diff --git a/src/themachinethatgoesping/tools/vectorinterpolators/i_pairinterpolator.hpp b/src/themachinethatgoesping/tools/vectorinterpolators/i_pairinterpolator.hpp index 19c6a3b..6c9a3b9 100644 --- a/src/themachinethatgoesping/tools/vectorinterpolators/i_pairinterpolator.hpp +++ b/src/themachinethatgoesping/tools/vectorinterpolators/i_pairinterpolator.hpp @@ -75,7 +75,7 @@ class I_PairInterpolator : public I_Interpolator */ XType calc_target_x(XType target_x) { return (target_x - _xmin) * _xfactor; } - } _last_x_pair; ///< last pair (for faster consecutive searches) + }; ///< last pair (for faster consecutive searches) /** * @brief extrapolation mode type. @@ -99,14 +99,12 @@ class I_PairInterpolator : public I_Interpolator std::vector Y, t_extr_mode extrapolation_mode = t_extr_mode::extrapolate) : I_Interpolator(extrapolation_mode) - , _last_x_pair(0, 1, 0, 1) { set_data_XY(std::move(X), std::move(Y)); } I_PairInterpolator(t_extr_mode extrapolation_mode = t_extr_mode::extrapolate) : I_Interpolator(extrapolation_mode) - , _last_x_pair(0, 1, 0, 1) { } @@ -133,9 +131,6 @@ class I_PairInterpolator : public I_Interpolator _X = std::move(X); _Y = std::move(Y); - - if (_X.size() > 1) - _last_x_pair = _t_x_pair(0, 1, _X[0], _X[1]); } // ----------------------- @@ -158,7 +153,7 @@ class I_PairInterpolator : public I_Interpolator throw(std::domain_error( "ERROR[Interpolator::append]: Y contains NAN or INFINITE values!")); - // if the internal data is one element, call set data to initialize _last_x_pair + // if the internal data is one element, call set data to initialize if (_X.size() == 1) { set_data_XY(std::vector{ _X[0], x }, std::vector{ _Y[0], y }); @@ -275,7 +270,7 @@ class I_PairInterpolator : public I_Interpolator // interpolation functions //------------------------- - YType get_y_const(XType target_x) const + YType get_y(XType target_x) const { // check if _X (and _Y) are initialized (_X and _Y should always be the same size) if (_X.size() == 0) @@ -296,7 +291,8 @@ class I_PairInterpolator : public I_Interpolator switch (I_Interpolator::_extr_mode) { case t_extr_mode::fail: { - // throw std::out_of_range("ERROR[INTERPOLATE]: x value out of range (too small), " + // throw std::out_of_range("ERROR[INTERPOLATE]: x value out of range (too + // small), " // "while fail on extrapolate was set!"); std::string msg; msg += "ERROR[INTERPOLATE]: x value [" + std::to_string(target_x) + @@ -318,7 +314,8 @@ class I_PairInterpolator : public I_Interpolator switch (I_Interpolator::_extr_mode) { case t_extr_mode::fail: { - // throw std::out_of_range("ERROR[INTERPOLATE]: x value out of range (too large), " + // throw std::out_of_range("ERROR[INTERPOLATE]: x value out of range (too + // large), " // "while fail on extrapolate was set!"); std::string msg; msg += "ERROR[INTERPOLATE]: x value [" + std::to_string(target_x) + @@ -351,119 +348,7 @@ class I_PairInterpolator : public I_Interpolator * @param target_x find the corresponding y value for this x value * @return corresponding y value */ - YType operator()(XType target_x) final - { - // check if _X (and _Y) are initialized (_X and _Y should always be the same size) - if (_X.size() == 0) - throw(std::domain_error( - "ERROR[PairInterpolator::operator()]: data vectors are not initialized!")); - - // if size of _X is 1, return _Y[0] - if (_X.size() == 1) - return _Y[0]; - - /* find correct xpair */ - - // if target value is smaller than the min value of the last, decrement - // backwards - if (target_x > _X[_last_x_pair._xmax_index]) - { - size_t i = _last_x_pair._xmax_index; - while (true) - { - ++i; - - // if i is smaller than the index - if (size_t last = _X.size() - 1; i > last) - { - // set the new last pair (will be used for interpolation) - switch (I_Interpolator::_extr_mode) - { - case t_extr_mode::fail: { - // throw std::out_of_range("ERROR[INTERPOLATE]: x value out of range (too " - // "large), while fail on extrapolate was set!"); - std::string msg; - msg += "ERROR[INTERPOLATE]: x value [" + std::to_string(target_x) + - "] is out of range (too large)(" + std::to_string(_X.front()) + - ")! (and fail on extrapolate was set)"; - throw(std::out_of_range(msg)); - } - - case t_extr_mode::nearest: - _last_x_pair = _t_x_pair(last - 1, last, _X[last - 1], _X[last]); - return _Y[last]; - - default: - _last_x_pair = _t_x_pair(last - 1, last, _X[last - 1], _X[last]); - break; - } - break; - } - - // if target value is larger or equal than the value at i - if (target_x <= _X[i]) - { - // set the new last pair (will be used for interpolation) - _last_x_pair = _t_x_pair(i - 1, i, _X[i - 1], _X[i]); - break; - } - } - } - - // if target value is smaller than the min value of the last, decrement - // backwards - else if (target_x < _X[_last_x_pair._xmin_index]) - { - long int i = static_cast(_last_x_pair._xmin_index); - while (true) - { - --i; - - // if i is smaller than the index - if (i < 0) - { - - // set the new last pair (will be used for interpolation) - switch (I_Interpolator::_extr_mode) - { - case t_extr_mode::fail: { - // throw std::out_of_range("ERROR[INTERPOLATE]: x value out of range (too " - // "small), while fail on extrapolate was set!"); - std::string msg; - msg += "ERROR[INTERPOLATE]: x value [" + std::to_string(target_x) + - "] is out of range (too small)(" + std::to_string(_X.front()) + - ")! (and fail on extrapolate was set)"; - throw(std::out_of_range(msg)); - } - case t_extr_mode::nearest: - _last_x_pair = _t_x_pair(0, 1, _X[0], _X[1]); - return _Y[0]; - default: - _last_x_pair = _t_x_pair(0, 1, _X[0], _X[1]); - break; - } - break; - } - - // if target value is larger or equal than the value at i - if (target_x >= _X[i]) - { - _last_x_pair = _t_x_pair(i, i + 1, _X[i], _X[i + 1]); - break; - } - } - } - - else // target_x <= _XY[_lastXPair._xmax_index] && target_x >= - // _XY[_lastXPair._xmin_index] - { - } - - /* interpolate using the (new) last XPair (call function from derived class) */ - return interpolate_pair(_last_x_pair.calc_target_x(target_x), - _Y[_last_x_pair._xmin_index], - _Y[_last_x_pair._xmax_index]); - } + YType operator()(XType target_x) const final { return get_y(target_x); } /** * @brief get nearest y values for given x targets (vectorized call) @@ -471,7 +356,7 @@ class I_PairInterpolator : public I_Interpolator * @param targets_x vector of x values. For each of these values find the corrsponding y value * @return corresponding y value */ - std::vector operator()(const std::vector& targetsX) + std::vector operator()(const std::vector& targetsX) const { return I_Interpolator::operator()(targetsX); } diff --git a/src/themachinethatgoesping/tools/vectorinterpolators/slerpinterpolator.hpp b/src/themachinethatgoesping/tools/vectorinterpolators/slerpinterpolator.hpp index d06b762..6cb1e18 100644 --- a/src/themachinethatgoesping/tools/vectorinterpolators/slerpinterpolator.hpp +++ b/src/themachinethatgoesping/tools/vectorinterpolators/slerpinterpolator.hpp @@ -146,23 +146,10 @@ class SlerpInterpolator : public I_PairInterpolator ypr(XType target_x, bool output_in_degrees = true) + std::array ypr(XType target_x, bool output_in_degrees = true) const { return rotationfunctions::ypr_from_quaternion( - I_PairInterpolator::operator()(target_x), output_in_degrees); - } - - /** - * @brief get the interpolated yaw, pitch and roll values for given x target - * - * @param target_x find the corresponding y value for this x value - * @param output_in_degrees if true, yaw pitch and roll input values are in ° otherwise rad - * @return corresponding y value - */ - std::array ypr_const(XType target_x, bool output_in_degrees = true) const - { - return rotationfunctions::ypr_from_quaternion( - I_PairInterpolator::get_y_const(target_x), output_in_degrees); + I_PairInterpolator::get_y(target_x), output_in_degrees); } /** @@ -174,40 +161,19 @@ class SlerpInterpolator : public I_PairInterpolator> ypr(const std::vector& targets_x, - bool output_in_degrees = true) + bool output_in_degrees = true) const { - std::vector> y_values; - y_values.reserve(targets_x.size()); - for (const auto target_x : targets_x) + auto y_values = I_PairInterpolator::operator()(targets_x); + std::vector> ypr_values; + ypr_values.reserve(y_values.size()); + for (const auto& q : y_values) { - y_values.push_back(ypr(target_x, output_in_degrees)); + ypr_values.push_back(rotationfunctions::ypr_from_quaternion(q, output_in_degrees)); } - return y_values; + return ypr_values; } - // /** - // * @brief get the interpolated yaw, pitch and roll values for given x target (vectorized call) - // * - // * @param targets_x vector of x values. For each of these values find the corrsponding yaw, - // * pitch and roll value - // * @param output_in_degrees if true, yaw pitch and roll input values are in ° otherwise rad - // * @return corresponding y value - // */ - // std::vector> ypr_const(const std::vector& targets_x, - // bool output_in_degrees = true) const - // { - // auto y_values = I_PairInterpolator::get_y_const(targets_x); - // std::vector> ypr_values; - // ypr_values.reserve(y_values.size()); - // for (const auto& q : y_values) - // { - // ypr_values.push_back(rotationfunctions::ypr_from_quaternion(q, output_in_degrees)); - // } - - // return ypr_values; - // } - // ------------------ // set data functions // ------------------