Browse Source

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.
pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
12c786c408
  1. 30
      src/Math/DualQuaternion.h
  2. 21
      src/Math/Test/DualQuaternionTest.cpp

30
src/Math/DualQuaternion.h

@ -185,15 +185,27 @@ template<class T> class DualQuaternion: public Dual<Quaternion<T>> {
}
/**
* @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<T> lengthSquared() const {
return {this->real().dot(), T(2)*Quaternion<T>::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<T> norm() const {
T norm = this->real().length();
return {norm, Quaternion<T>::dot(this->real(), this->dual())/norm};
inline Dual<T> length() const {
return Math::sqrt(lengthSquared());
}
/**
@ -205,7 +217,7 @@ template<class T> class DualQuaternion: public Dual<Quaternion<T>> {
* @f]
*/
inline DualQuaternion<T> inverted() const {
return quaternionConjugated()/Math::pow<2>(norm());
return quaternionConjugated()/Math::pow<2>(length());
}
/**
@ -217,7 +229,7 @@ template<class T> class DualQuaternion: public Dual<Quaternion<T>> {
* @f]
*/
inline DualQuaternion<T> invertedNormalized() const {
CORRADE_ASSERT(MathTypeTraits<T>::equals(norm(), T(1)),
CORRADE_ASSERT(MathTypeTraits<T>::equals(lengthSquared(), T(1)),
"Math::DualQuaternion::invertedNormalized(): dual quaternion must be normalized", {});
return quaternionConjugated();
}
@ -231,7 +243,7 @@ template<class T> class DualQuaternion: public Dual<Quaternion<T>> {
* @see DualQuaternion(const Vector3&), Matrix4::transformPoint(), Quaternion::rotateVectorNormalized()
*/
inline Vector3<T> transformPointNormalized(const Vector3<T>& vector) const {
CORRADE_ASSERT(MathTypeTraits<Dual<T>>::equals(norm(), Dual<T>(1)),
CORRADE_ASSERT(MathTypeTraits<Dual<T>>::equals(lengthSquared(), Dual<T>(1)),
"Math::DualQuaternion::transformPointNormalized(): dual quaternion must be normalized",
Vector3<T>(std::numeric_limits<T>::quiet_NaN()));
return ((*this)*DualQuaternion<T>(vector)*conjugated()).dual().vector();

21
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() {

Loading…
Cancel
Save