From a0b8dcc067d70954b8dfe643a04b566bf95bb7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 14 Feb 2013 00:54:45 +0100 Subject: [PATCH] Math: rename Quaternion::fromRotation() to rotation(). Now it is similar in usage to Matrix4 functions and is now also crossreferenced in documentation. Also updated the test to also check assertion. --- src/Math/Matrix4.h | 13 ++++++++----- src/Math/Quaternion.h | 9 +++++---- src/Math/Test/QuaternionTest.cpp | 25 ++++++++++++++++--------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index e919c75f3..810493644 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -70,8 +70,8 @@ template class Matrix4: public Matrix<4, T> { * * Expects that the rotation axis is normalized. If possible, use * faster alternatives like rotationX(), rotationY() and rotationZ(). - * @see rotation() const, Matrix3::rotation(T), Vector3::xAxis(), - * Vector3::yAxis(), Vector3::zAxis(), deg(), rad() + * @see rotation() const, Quaternion::rotation(), Matrix3::rotation(T), + * Vector3::xAxis(), Vector3::yAxis(), Vector3::zAxis(), deg(), rad() */ static Matrix4 rotation(T angle, const Vector3& normalizedAxis) { CORRADE_ASSERT(MathTypeTraits::equals(normalizedAxis.dot(), T(1)), @@ -111,7 +111,8 @@ template class Matrix4: public Matrix<4, T> { * * Faster than calling `Matrix4::rotation(angle, Vector3::xAxis())`. * @see rotation(T, const Vector3&), rotationY(), rotationZ(), - * rotation() const, Matrix3::rotation(T), deg(), rad() + * rotation() const, Quaternion::rotation(), Matrix3::rotation(T), + * deg(), rad() */ static Matrix4 rotationX(T angle) { T sine = std::sin(angle); @@ -129,7 +130,8 @@ template class Matrix4: public Matrix<4, T> { * * Faster than calling `Matrix4::rotation(angle, Vector3::yAxis())`. * @see rotation(T, const Vector3&), rotationX(), rotationZ(), - * rotation() const, Matrix3::rotation(T), deg(), rad() + * rotation() const, Quaternion::rotation(), Matrix3::rotation(T), + * deg(), rad() */ static Matrix4 rotationY(T angle) { T sine = std::sin(angle); @@ -147,7 +149,8 @@ template class Matrix4: public Matrix<4, T> { * * Faster than calling `Matrix4::rotation(angle, Vector3::zAxis())`. * @see rotation(T, const Vector3&), rotationX(), rotationY(), - * rotation() const, Matrix3::rotation(T), deg(), rad() + * rotation() const, Quaternion::rotation(), Matrix3::rotation(T), + * deg(), rad() */ static Matrix4 rotationZ(T angle) { T sine = std::sin(angle); diff --git a/src/Math/Quaternion.h b/src/Math/Quaternion.h index 0280732e8..8caeb01a6 100644 --- a/src/Math/Quaternion.h +++ b/src/Math/Quaternion.h @@ -110,10 +110,11 @@ template class Quaternion { * Expects that the rotation axis is normalized. @f[ * q = [\boldsymbol a \cdot sin \frac \theta 2, cos \frac \theta 2] * @f] + * @see Matrix4::rotation() */ - inline static Quaternion fromRotation(T angle, const Vector3& normalizedAxis) { + inline static Quaternion rotation(T angle, const Vector3& normalizedAxis) { CORRADE_ASSERT(MathTypeTraits::equals(normalizedAxis.dot(), T(1)), - "Math::Quaternion::fromRotation(): axis must be normalized", {}); + "Math::Quaternion::rotation(): axis must be normalized", {}); return {normalizedAxis*std::sin(angle/2), std::cos(angle/2)}; } @@ -153,7 +154,7 @@ template class Quaternion { * Expects that the quaternion is normalized. @f[ * \theta = 2 \cdot acos q_S * @f] - * @see rotationAxis(), fromRotation() + * @see rotationAxis(), rotation() */ inline T rotationAngle() const { CORRADE_ASSERT(MathTypeTraits::equals(dot(), T(1)), @@ -168,7 +169,7 @@ template class Quaternion { * Expects that the quaternion is normalized. @f[ * \boldsymbol a = \frac{\boldsymbol q_V}{\sqrt{1 - q_S^2}} * @f] - * @see rotationAngle(), fromRotation() + * @see rotationAngle(), rotation() */ inline Vector3 rotationAxis() const { CORRADE_ASSERT(MathTypeTraits::equals(dot(), T(1)), diff --git a/src/Math/Test/QuaternionTest.cpp b/src/Math/Test/QuaternionTest.cpp index 2954c0c58..b73b223d8 100644 --- a/src/Math/Test/QuaternionTest.cpp +++ b/src/Math/Test/QuaternionTest.cpp @@ -175,16 +175,23 @@ void QuaternionTest::invertedNormalized() { } void QuaternionTest::rotation() { + std::ostringstream o; + Error::setOutput(&o); + float angle = deg(120.0f); Vector3 axis(1.0f/Constants::sqrt3()); - Quaternion q = Quaternion::fromRotation(angle, axis); + + CORRADE_COMPARE(Quaternion::rotation(deg(-74.0f), {-1.0f, 2.0f, 2.0f}), Quaternion()); + CORRADE_COMPARE(o.str(), "Math::Quaternion::rotation(): axis must be normalized\n"); + + Quaternion q = Quaternion::rotation(angle, axis); CORRADE_COMPARE(q, Quaternion(Vector3(0.5f, 0.5f, 0.5f), 0.5f)); CORRADE_COMPARE(q.rotationAngle(), angle); CORRADE_COMPARE(q.rotationAxis(), axis); CORRADE_COMPARE(q.rotationAxis().length(), 1.0f); /* Verify negative angle */ - Quaternion q2 = Quaternion::fromRotation(deg(-120.0f), axis); + Quaternion q2 = Quaternion::rotation(deg(-120.0f), axis); CORRADE_COMPARE(q2, Quaternion(Vector3(-0.5f, -0.5f, -0.5f), 0.5f)); CORRADE_COMPARE(q2.rotationAngle(), deg(120.0f)); CORRADE_COMPARE(q2.rotationAxis(), -axis); @@ -209,7 +216,7 @@ void QuaternionTest::angle() { void QuaternionTest::matrix() { float angle = deg(37.0f); Vector3 axis(1.0f/Constants::sqrt3()); - Quaternion q = Quaternion::fromRotation(angle, axis); + Quaternion q = Quaternion::rotation(angle, axis); Matrix<3, float> expected = Matrix4::rotation(angle, axis).rotationScaling(); CORRADE_COMPARE(q.matrix(), expected); @@ -218,8 +225,8 @@ void QuaternionTest::matrix() { } void QuaternionTest::lerp() { - Quaternion a = Quaternion::fromRotation(deg(15.0f), Vector3(1.0f/Constants::sqrt3())); - Quaternion b = Quaternion::fromRotation(deg(23.0f), Vector3::xAxis()); + Quaternion a = Quaternion::rotation(deg(15.0f), Vector3(1.0f/Constants::sqrt3())); + Quaternion b = Quaternion::rotation(deg(23.0f), Vector3::xAxis()); std::ostringstream o; Corrade::Utility::Error::setOutput(&o); @@ -240,8 +247,8 @@ void QuaternionTest::lerp() { } void QuaternionTest::slerp() { - Quaternion a = Quaternion::fromRotation(deg(15.0f), Vector3(1.0f/Constants::sqrt3())); - Quaternion b = Quaternion::fromRotation(deg(23.0f), Vector3::xAxis()); + Quaternion a = Quaternion::rotation(deg(15.0f), Vector3(1.0f/Constants::sqrt3())); + Quaternion b = Quaternion::rotation(deg(23.0f), Vector3::xAxis()); std::ostringstream o; Corrade::Utility::Error::setOutput(&o); @@ -262,7 +269,7 @@ void QuaternionTest::slerp() { } void QuaternionTest::rotateVector() { - Quaternion a = Quaternion::fromRotation(deg(23.0f), Vector3::xAxis()); + Quaternion a = Quaternion::rotation(deg(23.0f), Vector3::xAxis()); Vector3 v(0.0f, -3.6f, 0.7f); Vector3 rotated = a.rotateVector(v); @@ -272,7 +279,7 @@ void QuaternionTest::rotateVector() { } void QuaternionTest::rotateVectorNormalized() { - Quaternion a = Quaternion::fromRotation(deg(23.0f), Vector3::xAxis()); + Quaternion a = Quaternion::rotation(deg(23.0f), Vector3::xAxis()); Vector3 v(0.0f, -3.6f, 0.7f); std::ostringstream o;