Compare commits

..

3 commits

Author SHA1 Message Date
880bbba491 Add 3-argument smoothstep and smootherstep
All checks were successful
Run tests / Run tests (push) Successful in 8m42s
2026-09-03 12:14:54 +03:00
2db6c32310 Add math::clamp(x, low, high) 2026-09-03 12:14:43 +03:00
7f69077c0a Fix math::clamp(x, interval) - use std::min/max as extension points 2026-09-03 12:14:27 +03:00
2 changed files with 23 additions and 1 deletions

View file

@ -287,7 +287,9 @@ namespace psemek::math
template <typename T>
T clamp(T x, interval<T> const & i)
{
return std::max(i.min, std::min(i.max, x));
using std::min;
using std::max;
return max(i.min, min(i.max, x));
}
template <typename T>

View file

@ -183,6 +183,14 @@ namespace psemek::math
return (x >= 0) ? std::fmod(x, m) : (m - std::fmod(-x, m));
}
template <typename T>
auto clamp(T const & x, T const & low, T const & high)
{
using std::min;
using std::max;
return min(high, max(low, x));
}
template <typename T>
bool make_min(T & target, T const & source)
{
@ -222,6 +230,12 @@ namespace psemek::math
return x * x * (T{3} - T{2} * x);
}
template <typename T>
T smoothstep(T edge0, T edge1, T x)
{
return smoothstep(clamp((x - edge0) / (edge1 - edge0), T{0}, T{1}));
}
template <typename T>
T smootherstep(T x)
{
@ -229,6 +243,12 @@ namespace psemek::math
return x * x2 * (T{10} - x * T{15} + x2 * T{6});
}
template <typename T>
T smootherstep(T edge0, T edge1, T x)
{
return smootherstep(clamp((x - edge0) / (edge1 - edge0), T{0}, T{1}));
}
template <typename T>
T inverse_smoothstep(T x)
{