From cd8e23983b945d98a2933cd57a5789b65c8e337a Mon Sep 17 00:00:00 2001 From: Takamasa Horibe Date: Fri, 18 Feb 2022 18:47:08 +0900 Subject: [PATCH] feat(interpolation): add float key lerp api Signed-off-by: Takamasa Horibe --- .../interpolation/linear_interpolation.hpp | 5 +++++ common/interpolation/src/linear_interpolation.cpp | 6 ++++++ .../test/src/test_linear_interpolation.cpp | 15 +++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/common/interpolation/include/interpolation/linear_interpolation.hpp b/common/interpolation/include/interpolation/linear_interpolation.hpp index 181e4dd302b65..1f8155613111e 100644 --- a/common/interpolation/include/interpolation/linear_interpolation.hpp +++ b/common/interpolation/include/interpolation/linear_interpolation.hpp @@ -26,6 +26,11 @@ double lerp(const double src_val, const double dst_val, const double ratio); std::vector lerp( const std::vector & base_keys, const std::vector & base_values, const std::vector & query_keys); + +double lerp( + const std::vector & base_keys, const std::vector & base_values, + const double query_key); + } // namespace interpolation #endif // INTERPOLATION__LINEAR_INTERPOLATION_HPP_ diff --git a/common/interpolation/src/linear_interpolation.cpp b/common/interpolation/src/linear_interpolation.cpp index 8f5544e74ebb1..c75565a1b809e 100644 --- a/common/interpolation/src/linear_interpolation.cpp +++ b/common/interpolation/src/linear_interpolation.cpp @@ -49,4 +49,10 @@ std::vector lerp( return query_values; } + +double lerp( + const std::vector & base_keys, const std::vector & base_values, double query_key) +{ + return lerp(base_keys, base_values, std::vector{query_key}).front(); +} } // namespace interpolation diff --git a/common/interpolation/test/src/test_linear_interpolation.cpp b/common/interpolation/test/src/test_linear_interpolation.cpp index 49a9352f3daaa..9c392943bd3c5 100644 --- a/common/interpolation/test/src/test_linear_interpolation.cpp +++ b/common/interpolation/test/src/test_linear_interpolation.cpp @@ -77,3 +77,18 @@ TEST(linear_interpolation, lerp_vector) } } } + +TEST(linear_interpolation, lerp_scalar_query) +{ + { // curve: query_keys is same as random + const std::vector base_keys{-1.5, 1.0, 5.0, 10.0, 15.0, 20.0}; + const std::vector base_values{-1.2, 0.5, 1.0, 1.2, 2.0, 1.0}; + const std::vector query_keys{0.0, 8.0, 18.0}; + const std::vector ans{-0.18, 1.12, 1.4}; + + for (size_t i = 0; i < query_keys.size(); ++i) { + const auto query_value = interpolation::lerp(base_keys, base_values, query_keys.at(i)); + EXPECT_NEAR(query_value, ans.at(i), epsilon); + } + } +}