diff --git a/components/math/example/main/math_example.cpp b/components/math/example/main/math_example.cpp index 6fdc851af..5cee80fcf 100644 --- a/components/math/example/main/math_example.cpp +++ b/components/math/example/main/math_example.cpp @@ -348,7 +348,7 @@ extern "C" void app_main(void) { {1, 1}, // clang-format on }; - std::vector> aggressive = { + std::array, 4> aggressive = {{ // clang-format off // input (x), output (y) {0, 0}, @@ -356,7 +356,7 @@ extern "C" void app_main(void) { {0.6, 0.8}, {1, 1}, // clang-format on - }; + }}; std::vector> delayed = { // clang-format off // input (x), output (y) diff --git a/components/math/include/fast_math.hpp b/components/math/include/fast_math.hpp index 20a2baef9..b4d3f2569 100644 --- a/components/math/include/fast_math.hpp +++ b/components/math/include/fast_math.hpp @@ -100,23 +100,24 @@ template int sgn(T x) { return (T(0) < x) - (x < T(0)); } * last y value. * @return Interpolated value at x. */ -[[maybe_unused]] static float piecewise_linear(const std::vector> &points, +[[maybe_unused]] static float piecewise_linear(std::span> points, float x) { if (points.size() == 0) { return 0.0f; } - if (x <= points[0].first) { - return points[0].second; + if (x <= points.front().first) { + return points.front().second; } - if (x >= points[points.size() - 1].first) { - return points[points.size() - 1].second; + if (x >= points.back().first) { + return points.back().second; } const auto it = std::find_if(points.begin() + 1, points.end(), [x](const auto &point) { return x <= point.first; }); if (it != points.end()) { - const auto prev = std::prev(it); - float t = inv_lerp(prev->first, it->first, x); - return lerp(prev->second, it->second, t); + const auto &[x0, y0] = *std::prev(it); + const auto &[x1, y1] = *it; + float t = inv_lerp(x0, x1, x); + return lerp(y0, y1, t); } return 0.0f; }