Browse Source

Math: make it possible to create an identity RectangularMatrix as well.

pull/518/head
Vladimír Vondruš 5 years ago
parent
commit
a347a2b3bd
  1. 3
      doc/changelog.dox
  2. 2
      src/Magnum/Math/Matrix.h
  3. 22
      src/Magnum/Math/RectangularMatrix.h
  4. 26
      src/Magnum/Math/Test/RectangularMatrixTest.cpp

3
doc/changelog.dox

@ -119,6 +119,9 @@ See also:
create a transformation from a rotation and translation part (see create a transformation from a rotation and translation part (see
[mosra/magnum#471](https://github.com/mosra/magnum/pull/471)) [mosra/magnum#471](https://github.com/mosra/magnum/pull/471))
- Added @ref Math::Intersection::rayRange() (see [mosra/magnum#484](https://github.com/mosra/magnum/pull/484)) - Added @ref Math::Intersection::rayRange() (see [mosra/magnum#484](https://github.com/mosra/magnum/pull/484))
- Added @ref Math::RectangularMatrix::RectangularMatrix(IdentityInitT, T)
constructor as it might be useful to create non-square identity matrices as
well
@subsubsection changelog-latest-new-meshtools MeshTools library @subsubsection changelog-latest-new-meshtools MeshTools library

2
src/Magnum/Math/Matrix.h

@ -80,7 +80,7 @@ template<std::size_t size, class T> class Matrix: public RectangularMatrix<size,
* The @p value allows you to specify a value on diagonal. * The @p value allows you to specify a value on diagonal.
* @see @ref Matrix(ZeroInitT), @ref fromDiagonal() * @see @ref Matrix(ZeroInitT), @ref fromDiagonal()
*/ */
constexpr explicit Matrix(IdentityInitT, T value = T(1)) noexcept: RectangularMatrix<size, size, T>{typename Corrade::Containers::Implementation::GenerateSequence<size>::Type{}, Vector<size, T>(value)} {} constexpr explicit Matrix(IdentityInitT, T value = T(1)) noexcept: RectangularMatrix<size, size, T>{IdentityInit, value} {}
/** @copydoc RectangularMatrix::RectangularMatrix(ZeroInitT) */ /** @copydoc RectangularMatrix::RectangularMatrix(ZeroInitT) */
constexpr explicit Matrix(ZeroInitT) noexcept: RectangularMatrix<size, size, T>{ZeroInit} {} constexpr explicit Matrix(ZeroInitT) noexcept: RectangularMatrix<size, size, T>{ZeroInit} {}

22
src/Magnum/Math/RectangularMatrix.h

@ -124,9 +124,23 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
*/ */
constexpr /*implicit*/ RectangularMatrix() noexcept: RectangularMatrix<cols, rows, T>{typename Corrade::Containers::Implementation::GenerateSequence<cols>::Type{}, ZeroInit} {} constexpr /*implicit*/ RectangularMatrix() noexcept: RectangularMatrix<cols, rows, T>{typename Corrade::Containers::Implementation::GenerateSequence<cols>::Type{}, ZeroInit} {}
/** @brief Construct a zero-filled matrix */ /**
* @brief Construct a zero-filled matrix
*
* @see @ref RectangularMatrix(IdentityInitT, T)
*/
constexpr explicit RectangularMatrix(ZeroInitT) noexcept: RectangularMatrix<cols, rows, T>{typename Corrade::Containers::Implementation::GenerateSequence<cols>::Type{}, ZeroInit} {} constexpr explicit RectangularMatrix(ZeroInitT) noexcept: RectangularMatrix<cols, rows, T>{typename Corrade::Containers::Implementation::GenerateSequence<cols>::Type{}, ZeroInit} {}
/**
* @brief Construct an identity matrix
* @m_since_latest
*
* For non-square matrices, the diagonal has @ref DiagonalSize
* elements. The @p value allows you to specify a value on diagonal.
* @see @ref RectangularMatrix(ZeroInitT), @ref fromDiagonal()
*/
constexpr explicit RectangularMatrix(IdentityInitT, T value = T(1)) noexcept: RectangularMatrix<cols, rows, T>{typename Corrade::Containers::Implementation::GenerateSequence<DiagonalSize>::Type{}, Vector<DiagonalSize, T>(value)} {}
/** @brief Construct without initializing the contents */ /** @brief Construct without initializing the contents */
explicit RectangularMatrix(Magnum::NoInitT) noexcept: RectangularMatrix<cols, rows, T>{typename Corrade::Containers::Implementation::GenerateSequence<cols>::Type{}, Magnum::NoInit} {} explicit RectangularMatrix(Magnum::NoInitT) noexcept: RectangularMatrix<cols, rows, T>{typename Corrade::Containers::Implementation::GenerateSequence<cols>::Type{}, Magnum::NoInit} {}
@ -442,9 +456,6 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
#else #else
private: private:
#endif #endif
/* Implementation for RectangularMatrix<cols, rows, T>::fromDiagonal() and Matrix<size, T>(IdentityInitT, T) */
template<std::size_t ...sequence> constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence<sequence...>, const Vector<DiagonalSize, T>& diagonal);
/* Implementation for RectangularMatrix<cols, rows, T>::RectangularMatrix(T) and Matrix<size, T>(T) */ /* Implementation for RectangularMatrix<cols, rows, T>::RectangularMatrix(T) and Matrix<size, T>(T) */
/* MSVC 2015 can't handle {} here */ /* MSVC 2015 can't handle {} here */
template<std::size_t ...sequence> constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence<sequence...>, T value) noexcept: _data{Vector<rows, T>((static_cast<void>(sequence), value))...} {} template<std::size_t ...sequence> constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence<sequence...>, T value) noexcept: _data{Vector<rows, T>((static_cast<void>(sequence), value))...} {}
@ -455,6 +466,9 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
template<std::size_t, class> friend class Matrix; template<std::size_t, class> friend class Matrix;
template<std::size_t, class> friend struct Implementation::MatrixDeterminant; template<std::size_t, class> friend struct Implementation::MatrixDeterminant;
/* Implementation for RectangularMatrix<cols, rows, T>::fromDiagonal() and RectangularMatrix<cols, rows, T>(IdentityInitT, T) */
template<std::size_t ...sequence> constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence<sequence...>, const Vector<DiagonalSize, T>& diagonal);
/* Implementation for RectangularMatrix<cols, rows, T>::RectangularMatrix(const RectangularMatrix<cols, rows, U>&) */ /* Implementation for RectangularMatrix<cols, rows, T>::RectangularMatrix(const RectangularMatrix<cols, rows, U>&) */
template<class U, std::size_t ...sequence> constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence<sequence...>, const RectangularMatrix<cols, rows, U>& matrix) noexcept: _data{Vector<rows, T>(matrix[sequence])...} {} template<class U, std::size_t ...sequence> constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence<sequence...>, const RectangularMatrix<cols, rows, U>& matrix) noexcept: _data{Vector<rows, T>(matrix[sequence])...} {}

26
src/Magnum/Math/Test/RectangularMatrixTest.cpp

@ -59,7 +59,8 @@ struct RectangularMatrixTest: Corrade::TestSuite::Tester {
explicit RectangularMatrixTest(); explicit RectangularMatrixTest();
void construct(); void construct();
void constructDefault(); void constructZero();
void constructIdentity();
void constructNoInit(); void constructNoInit();
void constructOneValue(); void constructOneValue();
void constructOneComponent(); void constructOneComponent();
@ -112,7 +113,8 @@ typedef Vector<2, Int> Vector2i;
RectangularMatrixTest::RectangularMatrixTest() { RectangularMatrixTest::RectangularMatrixTest() {
addTests({&RectangularMatrixTest::construct, addTests({&RectangularMatrixTest::construct,
&RectangularMatrixTest::constructDefault, &RectangularMatrixTest::constructZero,
&RectangularMatrixTest::constructIdentity,
&RectangularMatrixTest::constructNoInit, &RectangularMatrixTest::constructNoInit,
&RectangularMatrixTest::constructOneValue, &RectangularMatrixTest::constructOneValue,
&RectangularMatrixTest::constructOneComponent, &RectangularMatrixTest::constructOneComponent,
@ -160,7 +162,7 @@ void RectangularMatrixTest::construct() {
CORRADE_VERIFY(std::is_nothrow_constructible<Matrix3x4, Vector4, Vector4, Vector4>::value); CORRADE_VERIFY(std::is_nothrow_constructible<Matrix3x4, Vector4, Vector4, Vector4>::value);
} }
void RectangularMatrixTest::constructDefault() { void RectangularMatrixTest::constructZero() {
constexpr Matrix4x3 a; constexpr Matrix4x3 a;
constexpr Matrix4x3 b{ZeroInit}; constexpr Matrix4x3 b{ZeroInit};
CORRADE_COMPARE(a, Matrix4x3(Vector3(0.0f, 0.0f, 0.0f), CORRADE_COMPARE(a, Matrix4x3(Vector3(0.0f, 0.0f, 0.0f),
@ -179,6 +181,24 @@ void RectangularMatrixTest::constructDefault() {
CORRADE_VERIFY(!std::is_convertible<ZeroInitT, Matrix4x3>::value); CORRADE_VERIFY(!std::is_convertible<ZeroInitT, Matrix4x3>::value);
} }
void RectangularMatrixTest::constructIdentity() {
constexpr Matrix4x3 a{IdentityInit};
constexpr Matrix4x3 b{IdentityInit, 4.0f};
CORRADE_COMPARE(a, (Matrix4x3{Vector3{1.0f, 0.0f, 0.0f},
Vector3{0.0f, 1.0f, 0.0f},
Vector3{0.0f, 0.0f, 1.0f},
Vector3{0.0f, 0.0f, 0.0f}}));
CORRADE_COMPARE(b, (Matrix4x3{Vector3{4.0f, 0.0f, 0.0f},
Vector3{0.0f, 4.0f, 0.0f},
Vector3{0.0f, 0.0f, 4.0f},
Vector3{0.0f, 0.0f, 0.0f}}));
CORRADE_VERIFY(std::is_nothrow_constructible<Matrix4x3, IdentityInitT>::value);
/* Implicit construction is not allowed */
CORRADE_VERIFY(!std::is_convertible<IdentityInitT, Matrix4x3>::value);
}
void RectangularMatrixTest::constructNoInit() { void RectangularMatrixTest::constructNoInit() {
Matrix3x4 a{Vector4(1.0f, 2.0f, 3.0f, 4.0f), Matrix3x4 a{Vector4(1.0f, 2.0f, 3.0f, 4.0f),
Vector4(5.0f, 6.0f, 7.0f, 8.0f), Vector4(5.0f, 6.0f, 7.0f, 8.0f),

Loading…
Cancel
Save