diff --git a/src/Math/Quaternion.h b/src/Math/Quaternion.h index 81f0a7a88..da9ccf642 100644 --- a/src/Math/Quaternion.h +++ b/src/Math/Quaternion.h @@ -112,6 +112,17 @@ template class Quaternion { }; } + /** + * @brief Negated quaternion + * + * @f[ + * -q = [-\boldsymbol q_V, -q_S] + * @f] + */ + inline Quaternion operator-() const { + return {-_vector, -_scalar}; + } + /** * @brief Multiply with scalar and assign * diff --git a/src/Math/RectangularMatrix.h b/src/Math/RectangularMatrix.h index 7ff919cb2..e87e8c4bb 100644 --- a/src/Math/RectangularMatrix.h +++ b/src/Math/RectangularMatrix.h @@ -221,7 +221,7 @@ template class RectangularMatrix { return *this; } - /** @brief Negative matrix */ + /** @brief Negated matrix */ RectangularMatrix operator-() const { RectangularMatrix out; diff --git a/src/Math/Test/MathQuaternionTest.cpp b/src/Math/Test/MathQuaternionTest.cpp index fe2360964..15326582f 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 negated(); void multiplyDivideScalar(); void multiply(); void length(); @@ -45,6 +46,7 @@ typedef Math::Vector3 Vector3; QuaternionTest::QuaternionTest() { addTests(&QuaternionTest::construct, + &QuaternionTest::negated, &QuaternionTest::multiplyDivideScalar, &QuaternionTest::multiply, &QuaternionTest::length, @@ -65,6 +67,10 @@ void QuaternionTest::construct() { CORRADE_COMPARE(Quaternion(), Quaternion({0.0f, 0.0f, 0.0f}, {1.0f})); } +void QuaternionTest::negated() { + CORRADE_COMPARE(-Quaternion({1.0f, 2.0f, -3.0f}, -4.0f), Quaternion({-1.0f, -2.0f, 3.0f}, 4.0f)); +} + void QuaternionTest::multiplyDivideScalar() { Quaternion a({1.0f, 3.0f, -2.0f}, -4.0f); Quaternion b({-1.5f, -4.5f, 3.0f}, 6.0f); @@ -141,6 +147,9 @@ void QuaternionTest::matrix() { Quaternion q = Quaternion::fromRotation(angle, axis); Matrix<3, float> expected = Matrix4::rotation(angle, axis).rotationScaling(); CORRADE_COMPARE(q.matrix(), expected); + + /* Verify that negated quaternion gives the same rotation */ + CORRADE_COMPARE((-q).matrix(), expected); } void QuaternionTest::debug() {