diff --git a/src/Math/Quaternion.h b/src/Math/Quaternion.h index 7c9da9874..83f62f6f4 100644 --- a/src/Math/Quaternion.h +++ b/src/Math/Quaternion.h @@ -120,6 +120,28 @@ template 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& operator+=(const Quaternion& other) { + _vector += other._vector; + _scalar += other._scalar; + return *this; + } + + /** + * @brief Add quaternion + * + * @see operator+=() + */ + inline Quaternion operator+(const Quaternion& other) const { + return Quaternion(*this)+=other; + } + /** * @brief Negated quaternion * @@ -131,6 +153,28 @@ template class Quaternion { 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& operator-=(const Quaternion& other) { + _vector -= other._vector; + _scalar -= other._scalar; + return *this; + } + + /** + * @brief Subtract quaternion + * + * @see operator-=() + */ + inline Quaternion operator-(const Quaternion& other) const { + return Quaternion(*this)-=other; + } + /** * @brief Multiply with scalar and assign * diff --git a/src/Math/Test/MathQuaternionTest.cpp b/src/Math/Test/MathQuaternionTest.cpp index 33c37b490..99512883e 100644 --- a/src/Math/Test/MathQuaternionTest.cpp +++ b/src/Math/Test/MathQuaternionTest.cpp @@ -27,6 +27,7 @@ class QuaternionTest: public Corrade::TestSuite::Tester { explicit QuaternionTest(); void construct(); + void addSubtract(); void negated(); void multiplyDivideScalar(); void multiply(); @@ -46,6 +47,7 @@ typedef Math::Vector3 Vector3; QuaternionTest::QuaternionTest() { addTests(&QuaternionTest::construct, + &QuaternionTest::addSubtract, &QuaternionTest::negated, &QuaternionTest::multiplyDivideScalar, &QuaternionTest::multiply, @@ -67,6 +69,15 @@ void QuaternionTest::construct() { 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() { CORRADE_COMPARE(-Quaternion({1.0f, 2.0f, -3.0f}, -4.0f), Quaternion({-1.0f, -2.0f, 3.0f}, 4.0f)); }