Browse Source

Math: no need for Complex::transformVectorNormalized().

Unlike quaternions it doesn't need inversion or normalization for
transforming vectors.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
7aac5bd70b
  1. 23
      src/Math/Complex.h
  2. 3
      src/Math/Quaternion.h
  3. 18
      src/Math/Test/ComplexTest.cpp
  4. 10
      src/MeshTools/Transform.h

23
src/Math/Complex.h

@ -102,7 +102,7 @@ template<class T> class Complex {
* To be used in transformations later. @f[ * To be used in transformations later. @f[
* c = v_x + iv_y * c = v_x + iv_y
* @f] * @f]
* @see operator Vector2(), transformVector(), transformVectorNormalized() * @see operator Vector2(), transformVector()
*/ */
inline constexpr explicit Complex(const Vector2<T>& vector): _real(vector.x()), _imaginary(vector.y()) {} inline constexpr explicit Complex(const Vector2<T>& vector): _real(vector.x()), _imaginary(vector.y()) {}
@ -345,29 +345,12 @@ template<class T> class Complex {
/** /**
* @brief Rotate vector with complex number * @brief Rotate vector with complex number
* *
* See transformVectorNormalized(), which is faster for normalized * @f[
* complex numbers. @f[ * v' = c v = c (v_x + iv_y)
* v' = \frac c {|c|} v = \frac c {|c|} (v_x + iv_y)
* @f] * @f]
* @see Complex(const Vector2&), operator Vector2(), Matrix3::transformVector() * @see Complex(const Vector2&), operator Vector2(), Matrix3::transformVector()
*/ */
inline Vector2<T> transformVector(const Vector2<T>& vector) const { inline Vector2<T> transformVector(const Vector2<T>& vector) const {
return Vector2<T>(normalized()*Complex<T>(vector));
}
/**
* @brief Rotate vector with normalized complex number
*
* Faster alternative to transformVector(), expects that the complex
* number is normalized. @f[
* v' = \frac c {|c|} v = cv = c(v_x + iv_y)
* @f]
* @see Complex(const Vector2&), operator Vector2(), Matrix3::transformVector()
*/
inline Vector2<T> transformVectorNormalized(const Vector2<T>& vector) const {
CORRADE_ASSERT(MathTypeTraits<T>::equals(dot(), T(1)),
"Math::Complex::transformVectorNormalized(): complex number must be normalized",
Vector2<T>(std::numeric_limits<T>::quiet_NaN()));
return Vector2<T>((*this)*Complex<T>(vector)); return Vector2<T>((*this)*Complex<T>(vector));
} }

3
src/Math/Quaternion.h

@ -423,8 +423,7 @@ template<class T> class Quaternion {
* v' = qvq^{-1} = qvq^* = q [\boldsymbol v, 0] q^* * v' = qvq^{-1} = qvq^* = q [\boldsymbol v, 0] q^*
* @f] * @f]
* @see Quaternion(const Vector3&), vector(), Matrix4::transformVector(), * @see Quaternion(const Vector3&), vector(), Matrix4::transformVector(),
* DualQuaternion::transformPointNormalized(), * DualQuaternion::transformPointNormalized(), Complex::transformVector()
* Complex::transformVectorNormalized()
*/ */
inline Vector3<T> transformVectorNormalized(const Vector3<T>& vector) const { inline Vector3<T> transformVectorNormalized(const Vector3<T>& vector) const {
CORRADE_ASSERT(MathTypeTraits<T>::equals(dot(), T(1)), CORRADE_ASSERT(MathTypeTraits<T>::equals(dot(), T(1)),

18
src/Math/Test/ComplexTest.cpp

@ -50,7 +50,6 @@ class ComplexTest: public Corrade::TestSuite::Tester {
void rotation(); void rotation();
void matrix(); void matrix();
void transformVector(); void transformVector();
void transformVectorNormalized();
void debug(); void debug();
}; };
@ -81,7 +80,6 @@ ComplexTest::ComplexTest() {
&ComplexTest::rotation, &ComplexTest::rotation,
&ComplexTest::matrix, &ComplexTest::matrix,
&ComplexTest::transformVector, &ComplexTest::transformVector,
&ComplexTest::transformVectorNormalized,
&ComplexTest::debug); &ComplexTest::debug);
} }
@ -285,22 +283,6 @@ void ComplexTest::transformVector() {
CORRADE_COMPARE(rotated, Vector2(-3.58733f, -0.762279f)); CORRADE_COMPARE(rotated, Vector2(-3.58733f, -0.762279f));
} }
void ComplexTest::transformVectorNormalized() {
Complex a = Complex::rotation(Deg(23.0f));
Matrix3 m = Matrix3::rotation(Deg(23.0f));
Vector2 v(-3.6f, 0.7f);
std::ostringstream o;
Error::setOutput(&o);
Vector2 notRotated = (a*2).transformVectorNormalized(v);
CORRADE_VERIFY(notRotated != notRotated);
CORRADE_COMPARE(o.str(), "Math::Complex::transformVectorNormalized(): complex number must be normalized\n");
Vector2 rotated = a.transformVectorNormalized(v);
CORRADE_COMPARE(rotated, m.transformVector(v));
CORRADE_COMPARE(rotated, a.transformVector(v));
}
void ComplexTest::debug() { void ComplexTest::debug() {
std::ostringstream o; std::ostringstream o;

10
src/MeshTools/Transform.h

@ -31,8 +31,8 @@ namespace Magnum { namespace MeshTools {
Usable for one-time mesh transformations that would otherwise negatively affect Usable for one-time mesh transformations that would otherwise negatively affect
dependent objects, such as (uneven) scaling. Accepts any forward-iterable type dependent objects, such as (uneven) scaling. Accepts any forward-iterable type
with compatible vector type as @p vectors. Expects that @ref Math::Quaternion "Quaternion" with compatible vector type as @p vectors. Expects that @ref Math::Quaternion "Quaternion"
or @ref Math::Complex "Complex" is normalized, no further requirements are for is normalized, no further requirements are for other transformation
transformation matrices. representations.
Unlike in transformPointsInPlace(), the transformation does not involve Unlike in transformPointsInPlace(), the transformation does not involve
translation. translation.
@ -53,8 +53,8 @@ template<class T, class U> void transformVectorsInPlace(const Math::Quaternion<T
} }
/** @overload */ /** @overload */
template<class T, class U> void transformVectorsInPlace(const Math::Complex<T>& normalizedComplex, U& vectors) { template<class T, class U> void transformVectorsInPlace(const Math::Complex<T>& complex, U& vectors) {
for(auto& vector: vectors) vector = normalizedComplex.transformVectorNormalized(vector); for(auto& vector: vectors) vector = complex.transformVector(vector);
} }
/** @overload */ /** @overload */
@ -86,7 +86,7 @@ Usable for one-time mesh transformations that would otherwise negatively affect
dependent objects, such as (uneven) scaling. Accepts any forward-iterable type dependent objects, such as (uneven) scaling. Accepts any forward-iterable type
with compatible vector type as @p vectors. Expects that with compatible vector type as @p vectors. Expects that
@ref Math::DualQuaternion "DualQuaternion" is normalized, no further @ref Math::DualQuaternion "DualQuaternion" is normalized, no further
requirements are for transformation matrices. requirements are for other transformation representations.
Unlike in transformVectorsInPlace(), the transformation also involves Unlike in transformVectorsInPlace(), the transformation also involves
translation. translation.

Loading…
Cancel
Save