Browse Source

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<T>` instead
of overly verbose `Math::RectangularMatrix<2, 3, T>`). GCC 4.7+ only.
pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
7f9d0d1752
  1. 8
      doc/matrix-vector.dox
  2. 48
      src/Magnum.h
  3. 10
      src/Math/Math.h
  4. 73
      src/Math/RectangularMatrix.h

8
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 It is possible to create matrices from other matrices and vectors with the same
row count; vectors from vector and scalar: row count; vectors from vector and scalar:
@code @code
Math::RectangularMatrix<2, 3, Int> a; Math::Matrix2x3<Int> a;
Math::Vector<3, Int> b, c; Math::Vector3<Int> b, c;
Math::Matrix3<Int> mat(a, b); Math::Matrix3<Int> mat(a, b);
Math::Vector<8, Int> vec(1, b, 2, c); Math::Vector<8, Int> vec(1, b, 2, c);
@endcode @endcode
@ -115,7 +115,7 @@ on the array itself:
@code @code
Int[] mat = { 2, 4, 6, Int[] mat = { 2, 4, 6,
1, 3, 5 }; 1, 3, 5 };
RectangularMatrix<2, 3, Int>::from(mat) *= 2; // mat == { 4, 8, 12, 2, 6, 10 } Math::Matrix2x3<Int>::from(mat) *= 2; // mat == { 4, 8, 12, 2, 6, 10 }
@endcode @endcode
Note that unlike constructors, this function has no way to check whether the 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. 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 - Order of template arguments in specification of @ref Math::RectangularMatrix
is also column-major: is also column-major:
@code @code
Math::RectangularMatrix<2, 3, Int> mat; // two columns, three rows Math::RectangularMatrix<2, 5, Int> mat; // two columns, five rows
@endcode @endcode
- Order of components in matrix constructors is also column-major, further - Order of components in matrix constructors is also column-major, further
emphasized by requirement that you have to pass directly column vectors: emphasized by requirement that you have to pass directly column vectors:

48
src/Magnum.h

@ -195,22 +195,46 @@ typedef Math::Matrix3<Float> Matrix3;
typedef Math::Matrix4<Float> Matrix4; typedef Math::Matrix4<Float> Matrix4;
/** @brief Float matrix with 2 columns and 3 rows */ /** @brief Float matrix with 2 columns and 3 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix2x3<Float> Matrix2x3;
#else
typedef Math::RectangularMatrix<2, 3, Float> Matrix2x3; typedef Math::RectangularMatrix<2, 3, Float> Matrix2x3;
#endif
/** @brief Float matrix with 3 columns and 2 rows */ /** @brief Float matrix with 3 columns and 2 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix3x2<Float> Matrix3x2;
#else
typedef Math::RectangularMatrix<3, 2, Float> Matrix3x2; typedef Math::RectangularMatrix<3, 2, Float> Matrix3x2;
#endif
/** @brief Float matrix with 2 columns and 4 rows */ /** @brief Float matrix with 2 columns and 4 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix2x4<Float> Matrix2x4;
#else
typedef Math::RectangularMatrix<2, 4, Float> Matrix2x4; typedef Math::RectangularMatrix<2, 4, Float> Matrix2x4;
#endif
/** @brief Float matrix with 4 columns and 2 rows */ /** @brief Float matrix with 4 columns and 2 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix4x2<Float> Matrix4x2;
#else
typedef Math::RectangularMatrix<4, 2, Float> Matrix4x2; typedef Math::RectangularMatrix<4, 2, Float> Matrix4x2;
#endif
/** @brief Float matrix with 3 columns and 4 rows */ /** @brief Float matrix with 3 columns and 4 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix3x4<Float> Matrix3x4;
#else
typedef Math::RectangularMatrix<3, 4, Float> Matrix3x4; typedef Math::RectangularMatrix<3, 4, Float> Matrix3x4;
#endif
/** @brief Float matrix with 4 columns and 3 rows */ /** @brief Float matrix with 4 columns and 3 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix4x3<Float> Matrix4x3;
#else
typedef Math::RectangularMatrix<4, 3, Float> Matrix4x3; typedef Math::RectangularMatrix<4, 3, Float> Matrix4x3;
#endif
/** @brief Float complex number */ /** @brief Float complex number */
typedef Math::Complex<Float> Complex; typedef Math::Complex<Float> Complex;
@ -270,22 +294,46 @@ typedef Math::Matrix3<Double> Matrix3d;
typedef Math::Matrix4<Double> Matrix4d; typedef Math::Matrix4<Double> Matrix4d;
/** @brief Double matrix with 2 columns and 3 rows */ /** @brief Double matrix with 2 columns and 3 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix2x3<Double> Matrix2x3d;
#else
typedef Math::RectangularMatrix<2, 3, Double> Matrix2x3d; typedef Math::RectangularMatrix<2, 3, Double> Matrix2x3d;
#endif
/** @brief Double matrix with 3 columns and 2 rows */ /** @brief Double matrix with 3 columns and 2 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix3x2<Double> Matrix3x2d;
#else
typedef Math::RectangularMatrix<3, 2, Double> Matrix3x2d; typedef Math::RectangularMatrix<3, 2, Double> Matrix3x2d;
#endif
/** @brief Double matrix with 2 columns and 4 rows */ /** @brief Double matrix with 2 columns and 4 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix2x4<Double> Matrix2x4d;
#else
typedef Math::RectangularMatrix<2, 4, Double> Matrix2x4d; typedef Math::RectangularMatrix<2, 4, Double> Matrix2x4d;
#endif
/** @brief Double matrix with 4 columns and 2 rows */ /** @brief Double matrix with 4 columns and 2 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix4x2<Double> Matrix4x2d;
#else
typedef Math::RectangularMatrix<4, 2, Double> Matrix4x2d; typedef Math::RectangularMatrix<4, 2, Double> Matrix4x2d;
#endif
/** @brief Double matrix with 3 columns and 4 rows */ /** @brief Double matrix with 3 columns and 4 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix3x4<Double> Matrix3x4d;
#else
typedef Math::RectangularMatrix<3, 4, Double> Matrix3x4d; typedef Math::RectangularMatrix<3, 4, Double> Matrix3x4d;
#endif
/** @brief Double matrix with 4 columns and 3 rows */ /** @brief Double matrix with 4 columns and 3 rows */
#ifndef CORRADE_GCC46_COMPATIBILITY
typedef Math::Matrix4x3<Double> Matrix4x3d;
#else
typedef Math::RectangularMatrix<4, 3, Double> Matrix4x3d; typedef Math::RectangularMatrix<4, 3, Double> Matrix4x3d;
#endif
/** @brief Double complex number */ /** @brief Double complex number */
typedef Math::Complex<Double> Complexd; typedef Math::Complex<Double> Complexd;

10
src/Math/Math.h

@ -30,6 +30,8 @@
#include <cstddef> #include <cstddef>
#include "corradeConfigure.h"
namespace Magnum { namespace Math { namespace Magnum { namespace Math {
/** @todo Denormals to zero */ /** @todo Denormals to zero */
@ -48,6 +50,14 @@ template<class> class Matrix4;
template<class> class Quaternion; template<class> class Quaternion;
template<std::size_t, std::size_t, class> class RectangularMatrix; template<std::size_t, std::size_t, class> class RectangularMatrix;
#ifndef CORRADE_GCC46_COMPATIBILITY
template<class T> using Matrix2x3 = RectangularMatrix<2, 3, T>;
template<class T> using Matrix3x2 = RectangularMatrix<3, 2, T>;
template<class T> using Matrix2x4 = RectangularMatrix<2, 4, T>;
template<class T> using Matrix4x2 = RectangularMatrix<4, 2, T>;
template<class T> using Matrix3x4 = RectangularMatrix<3, 4, T>;
template<class T> using Matrix4x3 = RectangularMatrix<4, 3, T>;
#endif
template<template<class> class, class> class Unit; template<template<class> class, class> class Unit;
template<class> class Deg; template<class> class Deg;

73
src/Math/RectangularMatrix.h

@ -48,9 +48,8 @@ Vector.
The data are stored in column-major order, to reflect that, all indices in 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 math formulas are in reverse order (i.e. @f$ \boldsymbol A_{ji} @f$ instead
of @f$ \boldsymbol A_{ij} @f$). of @f$ \boldsymbol A_{ij} @f$).
@see Magnum::Matrix2x3, Magnum::Matrix3x2, Magnum::Matrix2x4, Magnum::Matrix4x2, @see @ref Matrix2x3, @ref Matrix3x2, @ref Matrix2x4, @ref Matrix4x2,
Magnum::Matrix3x4, Magnum::Matrix4x3, Magnum::Matrix2x3d, Magnum::Matrix3x2d, @ref Matrix3x4, @ref Matrix4x3
Magnum::Matrix2x4d, Magnum::Matrix4x2d, Magnum::Matrix3x4d, Magnum::Matrix4x3d
*/ */
template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix { template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
static_assert(cols != 0 && rows != 0, "RectangularMatrix cannot have zero elements"); static_assert(cols != 0 && rows != 0, "RectangularMatrix cannot have zero elements");
@ -367,6 +366,74 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
Vector<rows, T> _data[cols]; Vector<rows, T> _data[cols];
}; };
#ifndef CORRADE_GCC46_COMPATIBILITY
/**
@brief Matrix with 2 columns and 3 rows
Convenience alternative to <tt>%RectangularMatrix<2, 3, T></tt>. See
@ref RectangularMatrix for more information.
@note Not available on GCC < 4.7. Use <tt>%RectangularMatrix<2, 3, T></tt>
instead.
@see @ref Magnum::Matrix2x3, @ref Magnum::Matrix2x3d
*/
template<class T> using Matrix2x3 = RectangularMatrix<2, 3, T>;
/**
@brief Matrix with 3 columns and 2 rows
Convenience alternative to <tt>%RectangularMatrix<3, 2, T></tt>. See
@ref RectangularMatrix for more information.
@note Not available on GCC < 4.7. Use <tt>%RectangularMatrix<3, 2, T></tt>
instead.
@see @ref Magnum::Matrix3x2, @ref Magnum::Matrix3x2d
*/
template<class T> using Matrix3x2 = RectangularMatrix<3, 2, T>;
/**
@brief Matrix with 2 columns and 4 rows
Convenience alternative to <tt>%RectangularMatrix<2, 4, T></tt>. See
@ref RectangularMatrix for more information.
@note Not available on GCC < 4.7. Use <tt>%RectangularMatrix<2, 4, T></tt>
instead.
@see @ref Magnum::Matrix2x4, @ref Magnum::Matrix2x4d
*/
template<class T> using Matrix2x4 = RectangularMatrix<2, 4, T>;
/**
@brief Matrix with 4 columns and 2 rows
Convenience alternative to <tt>%RectangularMatrix<4, 2, T></tt>. See
@ref RectangularMatrix for more information.
@note Not available on GCC < 4.7. Use <tt>%RectangularMatrix<4, 2, T></tt>
instead.
@see @ref Magnum::Matrix4x2, @ref Magnum::Matrix4x2d
*/
template<class T> using Matrix4x2 = RectangularMatrix<4, 2, T>;
/**
@brief Matrix with 3 columns and 4 rows
Convenience alternative to <tt>%RectangularMatrix<3, 4, T></tt>. See
@ref RectangularMatrix for more information.
@note Not available on GCC < 4.7. Use <tt>%RectangularMatrix<3, 4, T></tt>
instead.
@see @ref Magnum::Matrix3x4, @ref Magnum::Matrix3x4d
*/
template<class T> using Matrix3x4 = RectangularMatrix<3, 4, T>;
/**
@brief Matrix with 4 columns and 3 rows
Convenience alternative to <tt>%RectangularMatrix<4, 3, T></tt>. See
@ref RectangularMatrix for more information.
@note Not available on GCC < 4.7. Use <tt>%RectangularMatrix<4, 3, T></tt>
instead.
@see @ref Magnum::Matrix4x3, @ref Magnum::Matrix4x3d
*/
template<class T> using Matrix4x3 = RectangularMatrix<4, 3, T>;
#endif
/** @relates RectangularMatrix /** @relates RectangularMatrix
@brief Multiply number with matrix @brief Multiply number with matrix

Loading…
Cancel
Save