Browse Source

Math: ability to create Complex and DualComplex from matrix.

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
9128820e35
  1. 24
      src/Math/Complex.h
  2. 15
      src/Math/DualComplex.h
  3. 8
      src/Math/Test/ComplexTest.cpp
  4. 8
      src/Math/Test/DualComplexTest.cpp

24
src/Math/Complex.h

@ -37,6 +37,15 @@
namespace Magnum { namespace Math {
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace Implementation {
/* No assertions fired, for internal use */
template<class T> inline static Complex<T> complexFromMatrix(const Matrix<2, T>& matrix) {
return {matrix[0][0], matrix[0][1]};
}
}
#endif
/**
@brief %Complex number
@tparam T Data type
@ -87,6 +96,18 @@ template<class T> class Complex {
return {std::cos(T(angle)), std::sin(T(angle))};
}
/**
* @brief Create complex number from rotation matrix
*
* Expects that the matrix is orthogonal (i.e. pure rotation).
* @see toMatrix(), DualComplex::fromMatrix(), Matrix::isOrthogonal()
*/
inline static Complex<T> fromMatrix(const Matrix<2, T>& matrix) {
CORRADE_ASSERT(matrix.isOrthogonal(),
"Math::Complex::fromMatrix(): the matrix is not orthogonal", {});
return Implementation::complexFromMatrix(matrix);
}
/**
* @brief Default constructor
*
@ -164,7 +185,8 @@ template<class T> class Complex {
* b & a
* \end{pmatrix}
* @f]
* @see DualComplex::toMatrix(), Matrix3::from(const Matrix<2, T>&, const Vector2<T>&)
* @see fromMatrix(), DualComplex::toMatrix(),
* Matrix3::from(const Matrix<2, T>&, const Vector2<T>&)
*/
Matrix<2, T> toMatrix() const {
return {Vector<2, T>(_real, _imaginary),

15
src/Math/DualComplex.h

@ -76,6 +76,19 @@ template<class T> class DualComplex: public Dual<Complex<T>> {
return {{}, {vector.x(), vector.y()}};
}
/**
* @brief Create dual complex number from rotation matrix
*
* Expects that the matrix represents rigid transformation.
* @see toMatrix(), Complex::fromMatrix(),
* Matrix3::isRigidTransformation()
*/
inline static DualComplex<T> fromMatrix(const Matrix3<T>& matrix) {
CORRADE_ASSERT(matrix.isRigidTransformation(),
"Math::DualComplex::fromMatrix(): the matrix doesn't represent rigid transformation", {});
return {Implementation::complexFromMatrix(matrix.rotationScaling()), Complex<T>(matrix.translation())};
}
/**
* @brief Default constructor
*
@ -140,7 +153,7 @@ template<class T> class DualComplex: public Dual<Complex<T>> {
/**
* @brief Convert dual complex number to transformation matrix
*
* @see Complex::toMatrix()
* @see fromMatrix(), Complex::toMatrix()
*/
inline Matrix3<T> toMatrix() const {
return Matrix3<T>::from(this->real().toMatrix(), translation());

8
src/Math/Test/ComplexTest.cpp

@ -280,6 +280,14 @@ void ComplexTest::matrix() {
Matrix2 m = Matrix3::rotation(Deg(37.0f)).rotationScaling();
CORRADE_COMPARE(a.toMatrix(), m);
std::ostringstream o;
Error::setOutput(&o);
Complex::fromMatrix(m*2);
CORRADE_COMPARE(o.str(), "Math::Complex::fromMatrix(): the matrix is not orthogonal\n");
Complex b = Complex::fromMatrix(m);
CORRADE_COMPARE(b, a);
}
void ComplexTest::transformVector() {

8
src/Math/Test/DualComplexTest.cpp

@ -225,6 +225,14 @@ void DualComplexTest::matrix() {
Matrix3 m = Matrix3::rotation(Deg(23.0f))*Matrix3::translation({2.0f, 3.0f});
CORRADE_COMPARE(a.toMatrix(), m);
std::ostringstream o;
Error::setOutput(&o);
DualComplex::fromMatrix(m*2);
CORRADE_COMPARE(o.str(), "Math::DualComplex::fromMatrix(): the matrix doesn't represent rigid transformation\n");
DualComplex b = DualComplex::fromMatrix(m);
CORRADE_COMPARE(b, a);
}
void DualComplexTest::transformPoint() {

Loading…
Cancel
Save