From e7ceddf0fc2dd7fc02aeb31a665c0669bb7656f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 22 Feb 2013 17:55:00 +0100 Subject: [PATCH] Math: rotation complex number. --- src/Math/Complex.h | 24 ++++++++++++++++++++++++ src/Math/Matrix3.h | 3 ++- src/Math/Quaternion.h | 6 +++--- src/Math/Test/ComplexTest.cpp | 17 +++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/Math/Complex.h b/src/Math/Complex.h index 93122b74b..62053251c 100644 --- a/src/Math/Complex.h +++ b/src/Math/Complex.h @@ -66,6 +66,19 @@ template class Complex { return Rad(std::acos(normalizedA._real*normalizedB._real + normalizedA._imaginary*normalizedB._imaginary)); } + /** + * @brief Rotation complex number + * @param angle Rotation angle (counterclockwise) + * + * @f[ + * c = cos \theta + i sin \theta + * @f] + * @see rotationAngle(), Matrix3::rotation(), Quaternion::rotation() + */ + inline static Complex rotation(Rad angle) { + return {std::cos(T(angle)), std::sin(T(angle))}; + } + /** * @brief Default constructor * @@ -101,6 +114,17 @@ template class Complex { /** @brief Imaginary part */ inline constexpr T imaginary() const { return _imaginary; } + /** + * @brief Rotation angle of complex number + * + * @f[ + * \theta = atan2(b, a) + * @f] + */ + inline Rad rotationAngle() const { + return Rad(std::atan2(_imaginary, _real)); + } + /** * @brief Add complex number and assign * diff --git a/src/Math/Matrix3.h b/src/Math/Matrix3.h index d93050281..87be55128 100644 --- a/src/Math/Matrix3.h +++ b/src/Math/Matrix3.h @@ -64,7 +64,8 @@ template class Matrix3: public Matrix<3, T> { * @brief 2D rotation matrix * @param angle Rotation angle (counterclockwise) * - * @see rotation() const, Matrix4::rotation(Rad, const Vector3&) + * @see rotation() const, Complex::rotation(), + * Matrix4::rotation(Rad, const Vector3&) */ static Matrix3 rotation(Rad angle) { T sine = std::sin(T(angle)); diff --git a/src/Math/Quaternion.h b/src/Math/Quaternion.h index 5edfee668..6ce8ae078 100644 --- a/src/Math/Quaternion.h +++ b/src/Math/Quaternion.h @@ -108,15 +108,15 @@ template class Quaternion { /** * @brief Rotation quaternion - * @param angle Rotation angle (counterclockwise, in radians) + * @param angle Rotation angle (counterclockwise) * @param normalizedAxis Normalized rotation axis * * Expects that the rotation axis is normalized. @f[ * q = [\boldsymbol a \cdot sin \frac \theta 2, cos \frac \theta 2] * @f] * @see rotationAngle(), rotationAxis(), DualQuaternion::rotation(), - * Matrix4::rotation(), Vector3::xAxis(), Vector3::yAxis(), - * Vector3::zAxis() + * Matrix4::rotation(), Complex::rotation(), Vector3::xAxis(), + * Vector3::yAxis(), Vector3::zAxis() */ inline static Quaternion rotation(Rad angle, const Vector3& normalizedAxis) { CORRADE_ASSERT(MathTypeTraits::equals(normalizedAxis.dot(), T(1)), diff --git a/src/Math/Test/ComplexTest.cpp b/src/Math/Test/ComplexTest.cpp index f448f2003..c0a25c62a 100644 --- a/src/Math/Test/ComplexTest.cpp +++ b/src/Math/Test/ComplexTest.cpp @@ -46,6 +46,7 @@ class ComplexTest: public Corrade::TestSuite::Tester { void invertedNormalized(); void angle(); + void rotation(); void debug(); }; @@ -72,10 +73,12 @@ ComplexTest::ComplexTest() { &ComplexTest::invertedNormalized, &ComplexTest::angle, + &ComplexTest::rotation, &ComplexTest::debug); } +typedef Math::Deg Deg; typedef Math::Rad Rad; typedef Math::Complex Complex; typedef Math::Vector2 Vector2; @@ -228,6 +231,20 @@ void ComplexTest::angle() { CORRADE_COMPARE(angle, Rad(2.933128f)); } +void ComplexTest::rotation() { + Complex a = Complex::rotation(Deg(120.0f)); + CORRADE_COMPARE(a, Complex(-0.5f, 0.8660254f)); + CORRADE_COMPARE_AS(a.rotationAngle(), Deg(120.0f), Rad); + + /* Verify negative angle */ + Complex b = Complex::rotation(Deg(-240.0f)); + CORRADE_COMPARE(b, Complex(-0.5f, 0.8660254f)); + CORRADE_COMPARE_AS(b.rotationAngle(), Deg(120.0f), Rad); + + /* Default-constructed complex number has zero angle */ + CORRADE_COMPARE_AS(Complex().rotationAngle(), Deg(0.0f), Rad); +} + void ComplexTest::debug() { std::ostringstream o;