From 7f9d0d17521e798d9f7c88915600753545c3bc95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 25 Oct 2013 20:42:18 +0200 Subject: [PATCH] Math: added convenience MatrixNxN aliases for RectangularMatrix. They were already in Magnum namespace for floats and doubles, now they can be used also for generic type (e.g. use `Math::Matrix2x3` instead of overly verbose `Math::RectangularMatrix<2, 3, T>`). GCC 4.7+ only. --- doc/matrix-vector.dox | 8 ++-- src/Magnum.h | 48 ++++++++++++++++++++++++ src/Math/Math.h | 10 +++++ src/Math/RectangularMatrix.h | 73 ++++++++++++++++++++++++++++++++++-- 4 files changed, 132 insertions(+), 7 deletions(-) diff --git a/doc/matrix-vector.dox b/doc/matrix-vector.dox index 52c792b04..3470c6996 100644 --- a/doc/matrix-vector.dox +++ b/doc/matrix-vector.dox @@ -101,8 +101,8 @@ auto diag2 = Matrix3::fromDiagonal({3.0f, 2.0f, 1.0f}); It is possible to create matrices from other matrices and vectors with the same row count; vectors from vector and scalar: @code -Math::RectangularMatrix<2, 3, Int> a; -Math::Vector<3, Int> b, c; +Math::Matrix2x3 a; +Math::Vector3 b, c; Math::Matrix3 mat(a, b); Math::Vector<8, Int> vec(1, b, 2, c); @endcode @@ -115,7 +115,7 @@ on the array itself: @code Int[] mat = { 2, 4, 6, 1, 3, 5 }; -RectangularMatrix<2, 3, Int>::from(mat) *= 2; // mat == { 4, 8, 12, 2, 6, 10 } +Math::Matrix2x3::from(mat) *= 2; // mat == { 4, 8, 12, 2, 6, 10 } @endcode Note that unlike constructors, this function has no way to check whether the array is long enough to contain all elements, so use with caution. @@ -235,7 +235,7 @@ implications and it may differ from what is common in mathematics: - Order of template arguments in specification of @ref Math::RectangularMatrix is also column-major: @code -Math::RectangularMatrix<2, 3, Int> mat; // two columns, three rows +Math::RectangularMatrix<2, 5, Int> mat; // two columns, five rows @endcode - Order of components in matrix constructors is also column-major, further emphasized by requirement that you have to pass directly column vectors: diff --git a/src/Magnum.h b/src/Magnum.h index c38936b9c..30b593c41 100644 --- a/src/Magnum.h +++ b/src/Magnum.h @@ -195,22 +195,46 @@ typedef Math::Matrix3 Matrix3; typedef Math::Matrix4 Matrix4; /** @brief Float matrix with 2 columns and 3 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix2x3 Matrix2x3; +#else typedef Math::RectangularMatrix<2, 3, Float> Matrix2x3; +#endif /** @brief Float matrix with 3 columns and 2 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix3x2 Matrix3x2; +#else typedef Math::RectangularMatrix<3, 2, Float> Matrix3x2; +#endif /** @brief Float matrix with 2 columns and 4 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix2x4 Matrix2x4; +#else typedef Math::RectangularMatrix<2, 4, Float> Matrix2x4; +#endif /** @brief Float matrix with 4 columns and 2 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix4x2 Matrix4x2; +#else typedef Math::RectangularMatrix<4, 2, Float> Matrix4x2; +#endif /** @brief Float matrix with 3 columns and 4 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix3x4 Matrix3x4; +#else typedef Math::RectangularMatrix<3, 4, Float> Matrix3x4; +#endif /** @brief Float matrix with 4 columns and 3 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix4x3 Matrix4x3; +#else typedef Math::RectangularMatrix<4, 3, Float> Matrix4x3; +#endif /** @brief Float complex number */ typedef Math::Complex Complex; @@ -270,22 +294,46 @@ typedef Math::Matrix3 Matrix3d; typedef Math::Matrix4 Matrix4d; /** @brief Double matrix with 2 columns and 3 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix2x3 Matrix2x3d; +#else typedef Math::RectangularMatrix<2, 3, Double> Matrix2x3d; +#endif /** @brief Double matrix with 3 columns and 2 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix3x2 Matrix3x2d; +#else typedef Math::RectangularMatrix<3, 2, Double> Matrix3x2d; +#endif /** @brief Double matrix with 2 columns and 4 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix2x4 Matrix2x4d; +#else typedef Math::RectangularMatrix<2, 4, Double> Matrix2x4d; +#endif /** @brief Double matrix with 4 columns and 2 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix4x2 Matrix4x2d; +#else typedef Math::RectangularMatrix<4, 2, Double> Matrix4x2d; +#endif /** @brief Double matrix with 3 columns and 4 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix3x4 Matrix3x4d; +#else typedef Math::RectangularMatrix<3, 4, Double> Matrix3x4d; +#endif /** @brief Double matrix with 4 columns and 3 rows */ +#ifndef CORRADE_GCC46_COMPATIBILITY +typedef Math::Matrix4x3 Matrix4x3d; +#else typedef Math::RectangularMatrix<4, 3, Double> Matrix4x3d; +#endif /** @brief Double complex number */ typedef Math::Complex Complexd; diff --git a/src/Math/Math.h b/src/Math/Math.h index 77b237b9a..ea5f9c59c 100644 --- a/src/Math/Math.h +++ b/src/Math/Math.h @@ -30,6 +30,8 @@ #include +#include "corradeConfigure.h" + namespace Magnum { namespace Math { /** @todo Denormals to zero */ @@ -48,6 +50,14 @@ template class Matrix4; template class Quaternion; template class RectangularMatrix; +#ifndef CORRADE_GCC46_COMPATIBILITY +template using Matrix2x3 = RectangularMatrix<2, 3, T>; +template using Matrix3x2 = RectangularMatrix<3, 2, T>; +template using Matrix2x4 = RectangularMatrix<2, 4, T>; +template using Matrix4x2 = RectangularMatrix<4, 2, T>; +template using Matrix3x4 = RectangularMatrix<3, 4, T>; +template using Matrix4x3 = RectangularMatrix<4, 3, T>; +#endif template class, class> class Unit; template class Deg; diff --git a/src/Math/RectangularMatrix.h b/src/Math/RectangularMatrix.h index 901dbb0cd..12e57610a 100644 --- a/src/Math/RectangularMatrix.h +++ b/src/Math/RectangularMatrix.h @@ -48,9 +48,8 @@ Vector. The data are stored in column-major order, to reflect that, all indices in math formulas are in reverse order (i.e. @f$ \boldsymbol A_{ji} @f$ instead of @f$ \boldsymbol A_{ij} @f$). -@see Magnum::Matrix2x3, Magnum::Matrix3x2, Magnum::Matrix2x4, Magnum::Matrix4x2, - Magnum::Matrix3x4, Magnum::Matrix4x3, Magnum::Matrix2x3d, Magnum::Matrix3x2d, - Magnum::Matrix2x4d, Magnum::Matrix4x2d, Magnum::Matrix3x4d, Magnum::Matrix4x3d +@see @ref Matrix2x3, @ref Matrix3x2, @ref Matrix2x4, @ref Matrix4x2, + @ref Matrix3x4, @ref Matrix4x3 */ template class RectangularMatrix { static_assert(cols != 0 && rows != 0, "RectangularMatrix cannot have zero elements"); @@ -367,6 +366,74 @@ template class RectangularMatrix { Vector _data[cols]; }; +#ifndef CORRADE_GCC46_COMPATIBILITY +/** +@brief Matrix with 2 columns and 3 rows + +Convenience alternative to %RectangularMatrix<2, 3, T>. See +@ref RectangularMatrix for more information. +@note Not available on GCC < 4.7. Use %RectangularMatrix<2, 3, T> + instead. +@see @ref Magnum::Matrix2x3, @ref Magnum::Matrix2x3d +*/ +template using Matrix2x3 = RectangularMatrix<2, 3, T>; + +/** +@brief Matrix with 3 columns and 2 rows + +Convenience alternative to %RectangularMatrix<3, 2, T>. See +@ref RectangularMatrix for more information. +@note Not available on GCC < 4.7. Use %RectangularMatrix<3, 2, T> + instead. +@see @ref Magnum::Matrix3x2, @ref Magnum::Matrix3x2d +*/ +template using Matrix3x2 = RectangularMatrix<3, 2, T>; + +/** +@brief Matrix with 2 columns and 4 rows + +Convenience alternative to %RectangularMatrix<2, 4, T>. See +@ref RectangularMatrix for more information. +@note Not available on GCC < 4.7. Use %RectangularMatrix<2, 4, T> + instead. +@see @ref Magnum::Matrix2x4, @ref Magnum::Matrix2x4d +*/ +template using Matrix2x4 = RectangularMatrix<2, 4, T>; + +/** +@brief Matrix with 4 columns and 2 rows + +Convenience alternative to %RectangularMatrix<4, 2, T>. See +@ref RectangularMatrix for more information. +@note Not available on GCC < 4.7. Use %RectangularMatrix<4, 2, T> + instead. +@see @ref Magnum::Matrix4x2, @ref Magnum::Matrix4x2d +*/ +template using Matrix4x2 = RectangularMatrix<4, 2, T>; + +/** +@brief Matrix with 3 columns and 4 rows + +Convenience alternative to %RectangularMatrix<3, 4, T>. See +@ref RectangularMatrix for more information. +@note Not available on GCC < 4.7. Use %RectangularMatrix<3, 4, T> + instead. +@see @ref Magnum::Matrix3x4, @ref Magnum::Matrix3x4d +*/ +template using Matrix3x4 = RectangularMatrix<3, 4, T>; + +/** +@brief Matrix with 4 columns and 3 rows + +Convenience alternative to %RectangularMatrix<4, 3, T>. See +@ref RectangularMatrix for more information. +@note Not available on GCC < 4.7. Use %RectangularMatrix<4, 3, T> + instead. +@see @ref Magnum::Matrix4x3, @ref Magnum::Matrix4x3d +*/ +template using Matrix4x3 = RectangularMatrix<4, 3, T>; +#endif + /** @relates RectangularMatrix @brief Multiply number with matrix