Browse Source

Math: added lerpInverted().

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
186c522fe5
  1. 24
      src/Math/Functions.h
  2. 12
      src/Math/Test/FunctionsTest.cpp

24
src/Math/Functions.h

@ -279,7 +279,7 @@ template<std::size_t size, class T> Vector<size, T> clamp(const Vector<size, T>&
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<std::size_t size, class T, class U> inline Vector<size, T> 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<class T> inline T lerpInverted(const T& a, const T& b, const T& lerp);
#else
template<class T> inline T lerpInverted(T a, T b, T lerp) {
return (lerp - a)/(b - a);
}
template<std::size_t size, class T, class U> inline Vector<size, T> lerpInverted(const Vector<size, T>& a, const Vector<size, T>& b, const Vector<size, T>& lerp) {
return (lerp - a)/(b - a);
}
#endif
/**
@brief Fused multiply-add

12
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),

Loading…
Cancel
Save