From 186c522fe5f26e283209d1084f3eff5b79311470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 11 Jul 2013 14:40:39 +0200 Subject: [PATCH] Math: added lerpInverted(). --- src/Math/Functions.h | 24 +++++++++++++++++++++++- src/Math/Test/FunctionsTest.cpp | 12 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Math/Functions.h b/src/Math/Functions.h index bb1947d38..cef9fde4e 100644 --- a/src/Math/Functions.h +++ b/src/Math/Functions.h @@ -279,7 +279,7 @@ template Vector clamp(const Vector& The interpolation for vectors is done as in following, similarly for scalars: @f[ \boldsymbol v_{LERP} = (1 - t) \boldsymbol v_A + t \boldsymbol v_B @f] -@see Quaternion::lerp() +@see lerpInverted(), Quaternion::lerp() @todo http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ (when SIMD is in place) */ @@ -294,6 +294,28 @@ template inline Vector lerp(const V } #endif +/** +@brief Inverse linear interpolation of two values +@param a First value +@param b Second value +@param lerp Interpolated value + +Returns interpolation phase *t*: @f[ + t = \frac{\boldsymbol v_{LERP} - \boldsymbol v_A}{\boldsymbol v_B - \boldsymbol v_A} +@f] +@see lerp() +*/ +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline T lerpInverted(const T& a, const T& b, const T& lerp); +#else +template inline T lerpInverted(T a, T b, T lerp) { + return (lerp - a)/(b - a); +} +template inline Vector lerpInverted(const Vector& a, const Vector& b, const Vector& lerp) { + return (lerp - a)/(b - a); +} +#endif + /** @brief Fused multiply-add diff --git a/src/Math/Test/FunctionsTest.cpp b/src/Math/Test/FunctionsTest.cpp index bc23f09ea..2d83cf349 100644 --- a/src/Math/Test/FunctionsTest.cpp +++ b/src/Math/Test/FunctionsTest.cpp @@ -43,6 +43,7 @@ class FunctionsTest: public Corrade::TestSuite::Tester { void sqrtInverted(); void clamp(); void lerp(); + void lerpInverted(); void fma(); void normalizeUnsigned(); void normalizeSigned(); @@ -79,6 +80,7 @@ FunctionsTest::FunctionsTest() { &FunctionsTest::sqrtInverted, &FunctionsTest::clamp, &FunctionsTest::lerp, + &FunctionsTest::lerpInverted, &FunctionsTest::fma, &FunctionsTest::normalizeUnsigned, &FunctionsTest::normalizeSigned, @@ -171,6 +173,16 @@ void FunctionsTest::lerp() { CORRADE_COMPARE(Math::lerp(a, b, Vector3(0.25f, 0.5f, 0.75f)), Vector3(0.0f, 0.0f, 9.0f)); } +void FunctionsTest::lerpInverted() { + /* Floating-point scalar */ + CORRADE_COMPARE(Math::lerpInverted(2.0f, 5.0f, 3.5f), 0.5f); + + /* Floating-point vector */ + Vector3 a(-1.0f, 2.0f, 3.0f); + Vector3 b(3.0f, -2.0f, 11.0f); + CORRADE_COMPARE(Math::lerpInverted(a, b, Vector3(0.0f, 0.0f, 9.0f)), Vector3(0.25f, 0.5f, 0.75f)); +} + void FunctionsTest::fma() { CORRADE_COMPARE(Math::fma(2.0f, 3.0f, 0.75f), 6.75f); CORRADE_COMPARE(Math::fma(Vector3( 2.0f, 1.5f, 0.5f),