Browse Source

Math: added Quaternion addition and subtraction.

pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
eaa819755f
  1. 44
      src/Math/Quaternion.h
  2. 11
      src/Math/Test/MathQuaternionTest.cpp

44
src/Math/Quaternion.h

@ -120,6 +120,28 @@ template<class T> class Quaternion {
}; };
} }
/**
* @brief Add and assign quaternion
*
* The computation is done in-place. @f[
* p + q = [\boldsymbol p_V + \boldsymbol q_V, p_S + q_S]
* @f]
*/
inline Quaternion<T>& operator+=(const Quaternion<T>& other) {
_vector += other._vector;
_scalar += other._scalar;
return *this;
}
/**
* @brief Add quaternion
*
* @see operator+=()
*/
inline Quaternion<T> operator+(const Quaternion<T>& other) const {
return Quaternion<T>(*this)+=other;
}
/** /**
* @brief Negated quaternion * @brief Negated quaternion
* *
@ -131,6 +153,28 @@ template<class T> class Quaternion {
return {-_vector, -_scalar}; return {-_vector, -_scalar};
} }
/**
* @brief Subtract and assign quaternion
*
* The computation is done in-place. @f[
* p - q = [\boldsymbol p_V - \boldsymbol q_V, p_S - q_S]
* @f]
*/
inline Quaternion<T>& operator-=(const Quaternion<T>& other) {
_vector -= other._vector;
_scalar -= other._scalar;
return *this;
}
/**
* @brief Subtract quaternion
*
* @see operator-=()
*/
inline Quaternion<T> operator-(const Quaternion<T>& other) const {
return Quaternion<T>(*this)-=other;
}
/** /**
* @brief Multiply with scalar and assign * @brief Multiply with scalar and assign
* *

11
src/Math/Test/MathQuaternionTest.cpp

@ -27,6 +27,7 @@ class QuaternionTest: public Corrade::TestSuite::Tester {
explicit QuaternionTest(); explicit QuaternionTest();
void construct(); void construct();
void addSubtract();
void negated(); void negated();
void multiplyDivideScalar(); void multiplyDivideScalar();
void multiply(); void multiply();
@ -46,6 +47,7 @@ typedef Math::Vector3<float> Vector3;
QuaternionTest::QuaternionTest() { QuaternionTest::QuaternionTest() {
addTests(&QuaternionTest::construct, addTests(&QuaternionTest::construct,
&QuaternionTest::addSubtract,
&QuaternionTest::negated, &QuaternionTest::negated,
&QuaternionTest::multiplyDivideScalar, &QuaternionTest::multiplyDivideScalar,
&QuaternionTest::multiply, &QuaternionTest::multiply,
@ -67,6 +69,15 @@ void QuaternionTest::construct() {
CORRADE_COMPARE(Quaternion(), Quaternion({0.0f, 0.0f, 0.0f}, {1.0f})); CORRADE_COMPARE(Quaternion(), Quaternion({0.0f, 0.0f, 0.0f}, {1.0f}));
} }
void QuaternionTest::addSubtract() {
Quaternion a({1.0f, 3.0f, -2.0f}, -4.0f);
Quaternion b({-0.5f, 1.4f, 3.0f}, 12.0f);
Quaternion c({0.5f, 4.4f, 1.0f}, 8.0f);
CORRADE_COMPARE(a+b, c);
CORRADE_COMPARE(c-b, a);
}
void QuaternionTest::negated() { void QuaternionTest::negated() {
CORRADE_COMPARE(-Quaternion({1.0f, 2.0f, -3.0f}, -4.0f), Quaternion({-1.0f, -2.0f, 3.0f}, 4.0f)); CORRADE_COMPARE(-Quaternion({1.0f, 2.0f, -3.0f}, -4.0f), Quaternion({-1.0f, -2.0f, 3.0f}, 4.0f));
} }

Loading…
Cancel
Save