diff --git a/uppsrc/Core/Util.h b/uppsrc/Core/Util.h index 45a87a532..74f0c3ce9 100644 --- a/uppsrc/Core/Util.h +++ b/uppsrc/Core/Util.h @@ -261,11 +261,21 @@ void SeedRandom(); // Math utils template -inline T Lerp(T a, T b, double t) +constexpr inline T Lerp(T a, T b, double t) noexcept { return T(a * (1.0 - t) + b * t); } +template +constexpr inline T Lerp(const T& a, const T& b, double t, const F& easefn) noexcept // Avoid copying large vector types. +{ + static_assert(std::is_invocable_r_v, + "Upp::Lerp: Easing function must take and return a double"); + + const double e = easefn(t); + return T(a * (1.0 - e) + b * e); +} + inline double sqr (double a) { return a * a; } inline double argsinh (double s) { return log(s + sqrt(s * s + 1)); } inline double argcosh (double c) { ASSERT(c >= 1); return log(c + sqrt(c * c - 1)); } diff --git a/uppsrc/Core/src.tpp/MathUtil_en-us.tpp b/uppsrc/Core/src.tpp/MathUtil_en-us.tpp index 9d03ef6b7..15a6be8fe 100644 --- a/uppsrc/Core/src.tpp/MathUtil_en-us.tpp +++ b/uppsrc/Core/src.tpp/MathUtil_en-us.tpp @@ -125,12 +125,29 @@ Random after seeding it with fixed value.&] [s3; &] [s4; &] [s5;:Upp`:`:Lerp`(T`,T`,double`): [@(0.0.255) template] <[@(0.0.255) typename] -T> T [* Lerp](T [*@3 a], T [*@3 b], [@(0.0.255) double] [*@3 t])&] +T>&] +[s5;:Upp`:`:Lerp`(T`,T`,double`): [@(0.0.255) constexpr] T [* Lerp](T +[*@3 a], T [*@3 b], [@(0.0.255) double] [*@3 t])&] [s2; Computes the linear interpolation between [*@3 a] and [*@3 b], if the parameter [*@3 t] is inside `[0,1`] (the linear extrapolation otherwise).&] [s3; &] [s4; &] +[s5;:Upp`:`:Lerp`(const T`&`,const T`&`,double`,const F`&`): [@(0.0.255) template] +<[@(0.0.255) typename] T, [@(0.0.255) typename] F>&] +[s5;:Upp`:`:Lerp`(const T`&`,const T`&`,double`,const F`&`): [@(0.0.255) constexpr] +T [* Lerp]([@(0.0.255) const] T[@(0.0.255) `&] [*@3 a], [@(0.0.255) const] +T[@(0.0.255) `&] [*@3 b], [@(0.0.255) double] [*@3 t], [@(0.0.255) const] +F[@(0.0.255) `&] [*@3 easefn])&] +[s2; Computes the linear interpolation between [*@3 a] and [*@3 b], if +the parameter [*@3 t] is inside `[0,1`] (the linear extrapolation +otherwise).[@N Easing function ][*@3 easefn][@N can be used to achieve +different effects (bounce, ease`-in, ease`-out, etc.). Easing +function must take and return a ][@(0.0.255) double][@N . This overload +can also be used with large types (such as vectors and matrices +with defined operators).]&] +[s3; &] +[s4; &] [s5;:ZeroArray`(x`): [* ZeroArray]([*@3 x])&] [s2;%% Fills C array [%-*@3 x] with zeros.&] [s3; &]