Skip to content
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
4 changes: 2 additions & 2 deletions components/math/example/main/math_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,15 @@ extern "C" void app_main(void) {
{1, 1},
// clang-format on
};
std::vector<std::pair<float, float>> aggressive = {
std::array<std::pair<float, float>, 4> aggressive = {{
// clang-format off
// input (x), output (y)
{0, 0},
{0.2, 0.2},
{0.6, 0.8},
{1, 1},
// clang-format on
};
}};
std::vector<std::pair<float, float>> delayed = {
// clang-format off
// input (x), output (y)
Expand Down
17 changes: 9 additions & 8 deletions components/math/include/fast_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,24 @@ template <typename T> 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<std::pair<float, float>> &points,
[[maybe_unused]] static float piecewise_linear(std::span<const std::pair<float, float>> 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;
}
Expand Down
Loading