From c5afb407dbcb5b573303db8c41c888469afb4abb Mon Sep 17 00:00:00 2001 From: AzaezelX Date: Sun, 21 May 2023 12:10:33 -0500 Subject: [PATCH] inverse lerp function --- Engine/source/math/mConsoleFunctions.cpp | 11 +++++++++++ Engine/source/math/mMathFn.h | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/Engine/source/math/mConsoleFunctions.cpp b/Engine/source/math/mConsoleFunctions.cpp index 0113e9126..0abe94dbe 100644 --- a/Engine/source/math/mConsoleFunctions.cpp +++ b/Engine/source/math/mConsoleFunctions.cpp @@ -353,6 +353,17 @@ DefineEngineFunction( mLerp, F32, ( F32 v1, F32 v2, F32 time ),, return mLerp( v1, v2, time ); } +DefineEngineFunction(mInvLerp, F32, (F32 v1, F32 v2, F32 point), , + "Calculate the percentage of a point along a line.\n" + "@param v1 Interpolate From Input value." + "@param v2 Interpolate To Input value." + "@param point Point along range." + "@returns normalized time used to interpolate values" + "@ingroup Math") +{ + return mInvLerp(v1, v2, point); +} + DefineEngineFunction( mPi, F32, (),, "Return the value of PI (half-circle in radians).\n" "@returns The value of PI." diff --git a/Engine/source/math/mMathFn.h b/Engine/source/math/mMathFn.h index a6668cc53..6dffd5d3d 100644 --- a/Engine/source/math/mMathFn.h +++ b/Engine/source/math/mMathFn.h @@ -265,6 +265,14 @@ inline T mLerp( const T &v1, const T &v2, F32 factor ) return ( v1 * ( 1.0f - factor ) ) + ( v2 * factor ); } +/// Template function for determining a percentage of interpolation between any two +/// types which implement operators for scalar multiply and addition. +template +inline T mInvLerp(const T& v1, const T& v2, F32 point) +{ + return (point - v1) / (v2 - v1); +} + inline S32 mMulDiv(S32 a, S32 b, S32 c) { return m_mulDivS32(a, b, c);