From 5f19343bee432bc81ccf49bffaebc697389fd626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 7 Jan 2013 01:22:51 +0100 Subject: [PATCH] Math: linear interpolation of two Vectors. --- src/Math/Test/VectorTest.cpp | 9 +++++++++ src/Math/Vector.h | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index d8d9f303f..b1f93a472 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -41,6 +41,7 @@ class VectorTest: public Corrade::TestSuite::Tester { void min(); void max(); void angle(); + void lerp(); void debug(); void configuration(); @@ -63,6 +64,7 @@ VectorTest::VectorTest() { &VectorTest::min, &VectorTest::max, &VectorTest::angle, + &VectorTest::lerp, &VectorTest::debug, &VectorTest::configuration); } @@ -153,6 +155,13 @@ void VectorTest::angle() { rad(1.162514f)); } +void VectorTest::lerp() { + Vector3 a(-1.0f, 2.0f, 3.0f); + Vector3 b(3.0f, -2.0f, 11.0f); + + CORRADE_COMPARE(Vector3::lerp(a, b, 0.25f), Vector3(0.0f, 1.0f, 5.0f)); +} + void VectorTest::debug() { std::ostringstream o; Debug(&o) << Vector4(0.5f, 15.0f, 1.0f, 1.0f); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 5cb7d25c2..d21fde555 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -67,6 +67,20 @@ template class Vector: public RectangularMatrix<1, si return std::acos(dot(normalizedA, normalizedB)); } + /** + * @brief Linear interpolation of two vectors + * @param a First vector + * @param b Second vector + * @param t Interpolation phase (from range @f$ [0; 1] @f$) + * + * The interpolation is done as in following: @f[ + * v_{LERP} = (1 - t) \boldsymbol v_A + t \boldsymbol v_B + * @f] + */ + inline static Vector lerp(const Vector& a, const Vector& b, T t) { + return (T(1) - t)*a + t*b; + } + /** @brief Default constructor */ inline constexpr /*implicit*/ Vector() {} @@ -345,6 +359,9 @@ extern template Corrade::Utility::Debug MAGNUM_EXPORT operator<<(Corrade::Utilit } \ template inline constexpr static Type from(const Math::Vector& other) { \ return Math::Vector::from(other); \ + } \ + inline static const Type lerp(const Math::Vector& a, const Math::Vector& b, T t) { \ + return Math::Vector::lerp(a, b, t); \ } \ \ inline Type& operator=(const Type& other) { \