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

feat(interpolation): add float key lerp api #416

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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ double lerp(const double src_val, const double dst_val, const double ratio);
std::vector<double> lerp(
const std::vector<double> & base_keys, const std::vector<double> & base_values,
const std::vector<double> & query_keys);

double lerp(
const std::vector<double> & base_keys, const std::vector<double> & base_values,
const double query_key);

} // namespace interpolation

#endif // INTERPOLATION__LINEAR_INTERPOLATION_HPP_
6 changes: 6 additions & 0 deletions common/interpolation/src/linear_interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ std::vector<double> lerp(

return query_values;
}

double lerp(
const std::vector<double> & base_keys, const std::vector<double> & base_values, double query_key)
{
return lerp(base_keys, base_values, std::vector<double>{query_key}).front();
}
} // namespace interpolation
15 changes: 15 additions & 0 deletions common/interpolation/test/src/test_linear_interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> base_keys{-1.5, 1.0, 5.0, 10.0, 15.0, 20.0};
const std::vector<double> base_values{-1.2, 0.5, 1.0, 1.2, 2.0, 1.0};
const std::vector<double> query_keys{0.0, 8.0, 18.0};
const std::vector<double> 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);
}
}
}