Browse Source

Math: linear interpolation of two Vectors.

pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
5f19343bee
  1. 9
      src/Math/Test/VectorTest.cpp
  2. 17
      src/Math/Vector.h

9
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);

17
src/Math/Vector.h

@ -67,6 +67,20 @@ template<std::size_t size, class T> 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<size, T> lerp(const Vector<size, T>& a, const Vector<size, T>& 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<class U> inline constexpr static Type<T> from(const Math::Vector<size, U>& other) { \
return Math::Vector<size, T>::from(other); \
} \
inline static const Type<T> lerp(const Math::Vector<size, T>& a, const Math::Vector<size, T>& b, T t) { \
return Math::Vector<size, T>::lerp(a, b, t); \
} \
\
inline Type<T>& operator=(const Type<T>& other) { \

Loading…
Cancel
Save