From 12c786c4089c8804a6db07c4c1e78686bb50f05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 17 Feb 2013 13:45:06 +0100 Subject: [PATCH] Math: renamed DualQuaternion::norm() to length(), added lengthSquared(). Hopefully this is properly implemented and properly named. On the other hand, having length everywhere (vectors, quaternions, complex numbers) and norm only at one place is inconsistent. --- src/Math/DualQuaternion.h | 30 +++++++++++++++++++--------- src/Math/Test/DualQuaternionTest.cpp | 21 +++++++++++++------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/Math/DualQuaternion.h b/src/Math/DualQuaternion.h index 56039ea5d..10a75cc25 100644 --- a/src/Math/DualQuaternion.h +++ b/src/Math/DualQuaternion.h @@ -185,15 +185,27 @@ template class DualQuaternion: public Dual> { } /** - * @brief %Dual quaternion norm + * @brief %Dual quaternion length squared * - * @f[ - * ||\hat q|| = \sqrt{\hat q^* \hat q} = ||q_0|| + \epsilon \frac{q_0 \cdot q_\epsilon}{||q_0||} + * Should be used instead of length() for comparing dual quaternion + * length with other values, because it doesn't compute the square root. @f[ + * |\hat q|^2 = \sqrt{\hat q^* \hat q}^2 = q_0 \cdot q_0 + \epsilon 2 (q_0 \cdot q_\epsilon) + * @f] + */ + inline Dual lengthSquared() const { + return {this->real().dot(), T(2)*Quaternion::dot(this->real(), this->dual())}; + } + + /** + * @brief %Dual quaternion length + * + * See lengthSquared() which is faster for comparing length with other + * values. @f[ + * |\hat q| = \sqrt{\hat q^* \hat q} = |q_0| + \epsilon \frac{q_0 \cdot q_\epsilon}{|q_0|} * @f] */ - inline Dual norm() const { - T norm = this->real().length(); - return {norm, Quaternion::dot(this->real(), this->dual())/norm}; + inline Dual length() const { + return Math::sqrt(lengthSquared()); } /** @@ -205,7 +217,7 @@ template class DualQuaternion: public Dual> { * @f] */ inline DualQuaternion inverted() const { - return quaternionConjugated()/Math::pow<2>(norm()); + return quaternionConjugated()/Math::pow<2>(length()); } /** @@ -217,7 +229,7 @@ template class DualQuaternion: public Dual> { * @f] */ inline DualQuaternion invertedNormalized() const { - CORRADE_ASSERT(MathTypeTraits::equals(norm(), T(1)), + CORRADE_ASSERT(MathTypeTraits::equals(lengthSquared(), T(1)), "Math::DualQuaternion::invertedNormalized(): dual quaternion must be normalized", {}); return quaternionConjugated(); } @@ -231,7 +243,7 @@ template class DualQuaternion: public Dual> { * @see DualQuaternion(const Vector3&), Matrix4::transformPoint(), Quaternion::rotateVectorNormalized() */ inline Vector3 transformPointNormalized(const Vector3& vector) const { - CORRADE_ASSERT(MathTypeTraits>::equals(norm(), Dual(1)), + CORRADE_ASSERT(MathTypeTraits>::equals(lengthSquared(), Dual(1)), "Math::DualQuaternion::transformPointNormalized(): dual quaternion must be normalized", Vector3(std::numeric_limits::quiet_NaN())); return ((*this)*DualQuaternion(vector)*conjugated()).dual().vector(); diff --git a/src/Math/Test/DualQuaternionTest.cpp b/src/Math/Test/DualQuaternionTest.cpp index ed3f20e55..e18862dc5 100644 --- a/src/Math/Test/DualQuaternionTest.cpp +++ b/src/Math/Test/DualQuaternionTest.cpp @@ -29,7 +29,8 @@ class DualQuaternionTest: public Corrade::TestSuite::Tester { void constructDefault(); void constructFromVector(); - void norm(); + void lengthSquared(); + void length(); void quaternionConjugated(); void dualConjugated(); @@ -57,7 +58,8 @@ DualQuaternionTest::DualQuaternionTest() { &DualQuaternionTest::constructDefault, &DualQuaternionTest::constructFromVector, - &DualQuaternionTest::norm, + &DualQuaternionTest::lengthSquared, + &DualQuaternionTest::length, &DualQuaternionTest::quaternionConjugated, &DualQuaternionTest::dualConjugated, @@ -87,11 +89,18 @@ void DualQuaternionTest::constructFromVector() { CORRADE_COMPARE(DualQuaternion({1.0f, 2.0f, 3.0f}), DualQuaternion({{0.0f, 0.0f, 0.0f}, 1.0f}, {{1.0f, 2.0f, 3.0f}, 0.0f})); } -void DualQuaternionTest::norm() { - CORRADE_COMPARE(DualQuaternion().norm(), 1.0f); +void DualQuaternionTest::lengthSquared() { + CORRADE_COMPARE(DualQuaternion().lengthSquared(), 1.0f); - DualQuaternion a({{ 1.0f, 2.0f, 3.0f}, -4.0f}, {{ 0.5f, -3.0f, 3.0f}, 2.0f}); - CORRADE_COMPARE(a.norm(), Dual(5.477226f, -0.821584f)); + DualQuaternion a({{1.0f, 2.0f, 3.0f}, -4.0f}, {{0.5f, -3.0f, 3.0f}, 2.0f}); + CORRADE_COMPARE(a.lengthSquared(), Dual(30.0f, -9.0f)); +} + +void DualQuaternionTest::length() { + CORRADE_COMPARE(DualQuaternion().length(), 1.0f); + + DualQuaternion a({{1.0f, 2.0f, 3.0f}, -4.0f}, {{0.5f, -3.0f, 3.0f}, 2.0f}); + CORRADE_COMPARE(a.length(), Dual(5.477226f, -0.821584f)); } void DualQuaternionTest::quaternionConjugated() {