Skip to content

Commit

Permalink
Added convenience Matrix2x2, Matrix3x3 and Matrix4x4 aliases.
Browse files Browse the repository at this point in the history
Unlike Matrix3/Matrix4 these don't have any transformation-related
functions. Deprecated the old Matrix2 typedef, which is now replaced
with Matrix2x2 and will be removed in future release.
  • Loading branch information
mosra committed Oct 25, 2013
1 parent 7f9d0d1 commit f130ce5
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 209 deletions.
8 changes: 4 additions & 4 deletions src/DebugTools/Test/CapsuleRendererTest.cpp
Expand Up @@ -55,9 +55,9 @@ void CapsuleRendererTest::zeroLength2D() {
const Vector2 a(0.5f, 3.0f);
std::array<Matrix3, 3> transformation = Implementation::capsuleRendererTransformation<2>(a, a, 3.5f);

const auto scaling = Math::Matrix<2, Float>::fromDiagonal(Vector2(3.5f));
const auto scaling = Matrix2x2::fromDiagonal(Vector2(3.5f));
CORRADE_COMPARE(transformation[0].rotationScaling(), scaling);
CORRADE_COMPARE(transformation[1].rotationScaling(), (Math::Matrix<2, Float>::fromDiagonal({3.5f, 0.0f})));
CORRADE_COMPARE(transformation[1].rotationScaling(), Matrix2x2::fromDiagonal({3.5f, 0.0f}));
CORRADE_COMPARE(transformation[2].rotationScaling(), scaling);

CORRADE_COMPARE(transformation[0].translation(), a);
Expand Down Expand Up @@ -94,9 +94,9 @@ void CapsuleRendererTest::zeroLength3D() {
const Vector3 a(0.5f, 3.0f, 7.0f);
std::array<Matrix4, 3> transformation = Implementation::capsuleRendererTransformation<3>(a, a, 3.5f);

const auto scaling = Math::Matrix<3, Float>::fromDiagonal(Vector3(3.5f));
const auto scaling = Matrix3x3::fromDiagonal(Vector3(3.5f));
CORRADE_COMPARE(transformation[0].rotationScaling(), scaling);
CORRADE_COMPARE(transformation[1].rotationScaling(), (Math::Matrix<3, Float>::fromDiagonal({3.5f, 0.0f, 3.5f})));
CORRADE_COMPARE(transformation[1].rotationScaling(), Matrix3x3::fromDiagonal({3.5f, 0.0f, 3.5f}));
CORRADE_COMPARE(transformation[2].rotationScaling(), scaling);

CORRADE_COMPARE(transformation[0].translation(), a);
Expand Down
4 changes: 2 additions & 2 deletions src/DebugTools/Test/CylinderRendererTest.cpp
Expand Up @@ -55,7 +55,7 @@ void CylinderRendererTest::zeroLength2D() {
const Vector2 a(0.5f, 3.0f);
const Matrix3 transformation = Implementation::cylinderRendererTransformation<2>(a, a, 3.5f);

CORRADE_COMPARE(transformation.rotationScaling(), (Math::Matrix<2, Float>::fromDiagonal({3.5f, 0.0f})));
CORRADE_COMPARE(transformation.rotationScaling(), Matrix2x2::fromDiagonal({3.5f, 0.0f}));
CORRADE_COMPARE(transformation.translation(), a);
}

Expand All @@ -76,7 +76,7 @@ void CylinderRendererTest::zeroLength3D() {
const Vector3 a(0.5f, 3.0f, 7.0f);
const Matrix4 transformation = Implementation::cylinderRendererTransformation<3>(a, a, 3.5f);

CORRADE_COMPARE(transformation.rotationScaling(), (Math::Matrix<3, Float>::fromDiagonal({3.5f, 0.0f, 3.5f})));
CORRADE_COMPARE(transformation.rotationScaling(), Matrix3x3::fromDiagonal({3.5f, 0.0f, 3.5f}));
CORRADE_COMPARE(transformation.translation(), a);
}

Expand Down
104 changes: 96 additions & 8 deletions src/Magnum.h
Expand Up @@ -185,15 +185,59 @@ typedef Math::Vector3<Int> Vector3i;
/** @brief Four-component signed integer vector */
typedef Math::Vector4<Int> Vector4i;

/** @brief 2x2 float matrix */
typedef Math::Matrix<2, Float> Matrix2;
/**
@brief 3x3 float transformation matrix
/** @brief 3x3 float matrix */
@see @ref Matrix3x3
*/
typedef Math::Matrix3<Float> Matrix3;

/** @brief 4x4 float matrix */
/**
@brief 4x4 float transformation matrix
@see @ref Matrix4x4
*/
typedef Math::Matrix4<Float> Matrix4;

/** @brief 2x2 float matrix */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix2x2<Float> Matrix2x2;
#else
typedef Math::Matrix<2, Float> Matrix2x2;
#endif

#ifdef MAGNUM_BUILD_DEPRECATED
/**
@copybrief Matrix2x2
@deprecated Use @ref Magnum::Matrix2x2 "Matrix2x2" instead.
*/
typedef Math::Matrix<2, Float> Matrix2;
#endif

/**
@brief 3x3 float matrix
Note that this is different from @ref Matrix3, which contains additional
functions for transformations in 2D.
*/
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix3x3<Float> Matrix3x3;
#else
typedef Math::Matrix<3, Float> Matrix3x3;
#endif

/**
@brief 4x4 float matrix
Note that this is different from @ref Matrix4, which contains additional
functions for transformations in 3D.
*/
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix4x4<Float> Matrix4x4;
#else
typedef Math::Matrix<4, Float> Matrix4x4;
#endif

/** @brief Float matrix with 2 columns and 3 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix2x3<Float> Matrix2x3;
Expand Down Expand Up @@ -284,15 +328,59 @@ typedef Math::Vector3<Double> Vector3d;
/** @brief Four-component double vector */
typedef Math::Vector4<Double> Vector4d;

/** @brief 2x2 double matrix */
typedef Math::Matrix<2, Double> Matrix2d;
/**
@brief 3x3 double transformation matrix
/** @brief 3x3 double matrix */
@see @ref Matrix3x3d
*/
typedef Math::Matrix3<Double> Matrix3d;

/** @brief 4x4 double matrix */
/**
@brief 4x4 double transformation matrix
@see @ref Matrix4x4d
*/
typedef Math::Matrix4<Double> Matrix4d;

/** @brief 2x2 double matrix */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix2x2<Double> Matrix2x2d;
#else
typedef Math::Matrix<2, Double> Matrix2x2d;
#endif

#ifdef MAGNUM_BUILD_DEPRECATED
/**
@copybrief Matrix2x2d
@deprecated Use @ref Magnum::Matrix2x2d "Matrix2x2d" instead.
*/
typedef Math::Matrix<2, Double> Matrix2d;
#endif

/**
@brief 3x3 double matrix
Note that this is different from @ref Matrix3d, which contains additional
functions for transformations in 2D.
*/
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix3x3<Double> Matrix3x3d;
#else
typedef Math::Matrix<3, Double> Matrix3x3d;
#endif

/**
@brief 4x4 double matrix
Note that this is different from @ref Matrix4d, which contains additional
functions for transformations in 3D.
*/
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix4x4<Double> Matrix4x4d;
#else
typedef Math::Matrix<4, Double> Matrix4x4d;
#endif

/** @brief Double matrix with 2 columns and 3 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix2x3<Double> Matrix2x3d;
Expand Down
36 changes: 18 additions & 18 deletions src/Math/Algorithms/Test/GaussJordanTest.cpp
Expand Up @@ -36,7 +36,7 @@ class GaussJordanTest: public Corrade::TestSuite::Tester {
void invert();
};

typedef RectangularMatrix<4, 4, Float> Matrix4;
typedef RectangularMatrix<4, 4, Float> Matrix4x4;
typedef Vector<4, Float> Vector4;

GaussJordanTest::GaussJordanTest() {
Expand All @@ -45,32 +45,32 @@ GaussJordanTest::GaussJordanTest() {
}

void GaussJordanTest::singular() {
Matrix4 a(Vector4(1.0f, 2.0f, 3.0f, 4.0f),
Vector4(2.0f, 3.0f, -7.0f, 11.0f),
Vector4(2.0f, 4.0f, 6.0f, 8.0f),
Vector4(1.0f, 2.0f, 7.0f, 40.0f));
Matrix4x4 a(Vector4(1.0f, 2.0f, 3.0f, 4.0f),
Vector4(2.0f, 3.0f, -7.0f, 11.0f),
Vector4(2.0f, 4.0f, 6.0f, 8.0f),
Vector4(1.0f, 2.0f, 7.0f, 40.0f));
RectangularMatrix<4, 1, Float> t;

CORRADE_VERIFY(!gaussJordanInPlaceTransposed(a, t));
}

void GaussJordanTest::invert() {
Matrix4 a(Vector4(3.0f, 5.0f, 8.0f, 4.0f),
Vector4(4.0f, 4.0f, 7.0f, 3.0f),
Vector4(7.0f, -1.0f, 8.0f, 0.0f),
Vector4(9.0f, 4.0f, 5.0f, 9.0f));

Matrix4 expectedInverse(Vector4(-60/103.0f, 71/103.0f, -4/103.0f, 3/103.0f),
Vector4(-66/103.0f, 109/103.0f, -25/103.0f, -7/103.0f),
Vector4(177/412.0f, -97/206.0f, 53/412.0f, -7/206.0f),
Vector4(259/412.0f, -185/206.0f, 31/412.0f, 27/206.0f));

Matrix4 a2(a);
Matrix4 inverse = Matrix4::fromDiagonal(Vector4(1.0f));
Matrix4x4 a(Vector4(3.0f, 5.0f, 8.0f, 4.0f),
Vector4(4.0f, 4.0f, 7.0f, 3.0f),
Vector4(7.0f, -1.0f, 8.0f, 0.0f),
Vector4(9.0f, 4.0f, 5.0f, 9.0f));

Matrix4x4 expectedInverse(Vector4(-60/103.0f, 71/103.0f, -4/103.0f, 3/103.0f),
Vector4(-66/103.0f, 109/103.0f, -25/103.0f, -7/103.0f),
Vector4(177/412.0f, -97/206.0f, 53/412.0f, -7/206.0f),
Vector4(259/412.0f, -185/206.0f, 31/412.0f, 27/206.0f));

Matrix4x4 a2(a);
Matrix4x4 inverse = Matrix4x4::fromDiagonal(Vector4(1.0f));
CORRADE_VERIFY(gaussJordanInPlace(a2, inverse));

CORRADE_COMPARE(inverse, expectedInverse);
CORRADE_COMPARE(a*inverse, Matrix4::fromDiagonal(Vector4(1.0f)));
CORRADE_COMPARE(a*inverse, Matrix4x4::fromDiagonal(Vector4(1.0f)));
}

}}}}
Expand Down
30 changes: 15 additions & 15 deletions src/Math/Algorithms/Test/GramSchmidtTest.cpp
Expand Up @@ -36,7 +36,7 @@ class GramSchmidtTest: public Corrade::TestSuite::Tester {
void orthonormalize();
};

typedef RectangularMatrix<3, 3, Float> Matrix3;
typedef RectangularMatrix<3, 3, Float> Matrix3x3;
typedef Vector<3, Float> Vector3;

GramSchmidtTest::GramSchmidtTest() {
Expand All @@ -45,11 +45,11 @@ GramSchmidtTest::GramSchmidtTest() {
}

void GramSchmidtTest::orthogonalize() {
Matrix3 m(Vector3(3.0f, 5.0f, 1.0f),
Vector3(4.0f, 4.0f, 7.0f),
Vector3(7.0f, -1.0f, -3.0f));
Matrix3x3 m(Vector3(3.0f, 5.0f, 1.0f),
Vector3(4.0f, 4.0f, 7.0f),
Vector3(7.0f, -1.0f, -3.0f));

Matrix3 orthogonalized = Algorithms::gramSchmidtOrthogonalize(m);
Matrix3x3 orthogonalized = Algorithms::gramSchmidtOrthogonalize(m);

/* Verify the first vector is in direction of first original */
CORRADE_COMPARE(orthogonalized[0], m[0]);
Expand All @@ -62,18 +62,18 @@ void GramSchmidtTest::orthogonalize() {
CORRADE_COMPARE(Vector3::dot(orthogonalized[1], orthogonalized[2]), 0.0f);

/* Just to be sure */
Matrix3 expected(Vector3( 3.0f, 5.0f, 1.0f),
Vector3(0.657143f, -1.571429f, 5.885714f),
Vector3(6.086759f, -3.3379f, -1.570777f));
Matrix3x3 expected(Vector3( 3.0f, 5.0f, 1.0f),
Vector3(0.657143f, -1.571429f, 5.885714f),
Vector3(6.086759f, -3.3379f, -1.570777f));
CORRADE_COMPARE(orthogonalized, expected);
}

void GramSchmidtTest::orthonormalize() {
Matrix3 m(Vector3(3.0f, 5.0f, 8.0f),
Vector3(4.0f, 4.0f, 7.0f),
Vector3(7.0f, -1.0f, 8.0f));
Matrix3x3 m(Vector3(3.0f, 5.0f, 8.0f),
Vector3(4.0f, 4.0f, 7.0f),
Vector3(7.0f, -1.0f, 8.0f));

Matrix3 orthonormalized = Algorithms::gramSchmidtOrthonormalize(m);
Matrix3x3 orthonormalized = Algorithms::gramSchmidtOrthonormalize(m);

/* Verify the first vector is in direction of first original */
CORRADE_COMPARE(orthonormalized[0], m[0].normalized());
Expand All @@ -89,9 +89,9 @@ void GramSchmidtTest::orthonormalize() {
CORRADE_COMPARE(Vector3::dot(orthonormalized[1], orthonormalized[2]), 0.0f);

/* Just to be sure */
Matrix3 expected(Vector3( 0.303046f, 0.505076f, 0.808122f),
Vector3( 0.928316f, -0.348119f, -0.130544f),
Vector3(-0.215388f, -0.789754f, 0.574367f));
Matrix3x3 expected(Vector3( 0.303046f, 0.505076f, 0.808122f),
Vector3( 0.928316f, -0.348119f, -0.130544f),
Vector3(-0.215388f, -0.789754f, 0.574367f));
CORRADE_COMPARE(orthonormalized, expected);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Math/Math.h
Expand Up @@ -44,6 +44,12 @@ template<class> class DualComplex;
template<class> class DualQuaternion;

template<std::size_t, class> class Matrix;
#ifndef CORRADE_GCC46_COMPATIBILITY
template<class T> using Matrix2x2 = Matrix<2, T>;
template<class T> using Matrix3x3 = Matrix<3, T>;
template<class T> using Matrix4x4 = Matrix<4, T>;
#endif

template<class> class Matrix3;
template<class> class Matrix4;

Expand Down
40 changes: 37 additions & 3 deletions src/Math/Matrix.h
Expand Up @@ -44,7 +44,7 @@ namespace Implementation {
See @ref matrix-vector for brief introduction.
@configurationvalueref{Magnum::Math::Matrix}
@see Magnum::Matrix2, Magnum::Matrix2d
@see @ref Matrix2x2, @ref Matrix3x3, @ref Matrix4x4
*/
template<std::size_t size, class T> class Matrix: public RectangularMatrix<size, size, T> {
public:
Expand Down Expand Up @@ -89,9 +89,9 @@ template<std::size_t size, class T> class Matrix: public RectangularMatrix<size,
* Performs only default casting on the values, no rounding or
* anything else. Example usage:
* @code
* Matrix<2, Float> floatingPoint({1.3f, 2.7f},
* Matrix2x2<Float> floatingPoint({1.3f, 2.7f},
* {-15.0f, 7.0f});
* Matrix<2, Byte> integral(floatingPoint);
* Matrix2x2<Byte> integral(floatingPoint);
* // integral == {{1, 2}, {-15, 7}}
* @endcode
*/
Expand Down Expand Up @@ -180,6 +180,40 @@ template<std::size_t size, class T> class Matrix: public RectangularMatrix<size,
#endif
};

#ifndef CORRADE_GCC46_COMPATIBILITY
/**
@brief 2x2 matrix
Convenience alternative to <tt>%Matrix<2, T></tt>. See @ref Matrix for more
information.
@note Not available on GCC < 4.7. Use <tt>%Matrix<2, T></tt> instead.
@see @ref Magnum::Matrix2x2, @ref Magnum::Matrix2x2d
*/
template<class T> using Matrix2x2 = Matrix<2, T>;

/**
@brief 3x3 matrix
Convenience alternative to <tt>%Matrix<3, T></tt>. See @ref Matrix for more
information. Note that this is different from @ref Matrix3, which contains
additional functions for transformations in 2D.
@note Not available on GCC < 4.7. Use <tt>%Matrix<3, T></tt> instead.
@see @ref Magnum::Matrix3x3, @ref Magnum::Matrix3x3d
*/
template<class T> using Matrix3x3 = Matrix<3, T>;

/**
@brief 4x4 matrix
Convenience alternative to <tt>%Matrix<4, T></tt>. See @ref Matrix for more
information. Note that this is different from @ref Matrix4, which contains
additional functions for transformations in 3D.
@note Not available on GCC < 4.7. Use <tt>%Matrix<3, T></tt> instead.
@see @ref Magnum::Matrix4x4, @ref Magnum::Matrix4x4d
*/
template<class T> using Matrix4x4 = Matrix<4, T>;
#endif

MAGNUM_MATRIX_OPERATOR_IMPLEMENTATION(Matrix<size, T>)

/** @debugoperator{Magnum::Math::Matrix} */
Expand Down
4 changes: 2 additions & 2 deletions src/Math/Test/ComplexTest.cpp
Expand Up @@ -100,7 +100,7 @@ typedef Math::Rad<Float> Rad;
typedef Math::Complex<Float> Complex;
typedef Math::Vector2<Float> Vector2;
typedef Math::Matrix3<Float> Matrix3;
typedef Math::Matrix<2, Float> Matrix2;
typedef Math::Matrix<2, Float> Matrix2x2;

void ComplexTest::construct() {
constexpr Complex a(0.5f, -3.7f);
Expand Down Expand Up @@ -275,7 +275,7 @@ void ComplexTest::rotation() {

void ComplexTest::matrix() {
Complex a = Complex::rotation(Deg(37.0f));
Matrix2 m = Matrix3::rotation(Deg(37.0f)).rotationScaling();
Matrix2x2 m = Matrix3::rotation(Deg(37.0f)).rotationScaling();

CORRADE_COMPARE(a.toMatrix(), m);

Expand Down

0 comments on commit f130ce5

Please sign in to comment.