From a347a2b3bdee20422f78bf02c3e0cc052e74f4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 2 May 2021 16:05:54 +0200 Subject: [PATCH] Math: make it possible to create an identity RectangularMatrix as well. --- doc/changelog.dox | 3 +++ src/Magnum/Math/Matrix.h | 2 +- src/Magnum/Math/RectangularMatrix.h | 22 +++++++++++++--- .../Math/Test/RectangularMatrixTest.cpp | 26 ++++++++++++++++--- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 55de0d867..971bdaec3 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -119,6 +119,9 @@ See also: create a transformation from a rotation and translation part (see [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::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 diff --git a/src/Magnum/Math/Matrix.h b/src/Magnum/Math/Matrix.h index 098640728..9487065a5 100644 --- a/src/Magnum/Math/Matrix.h +++ b/src/Magnum/Math/Matrix.h @@ -80,7 +80,7 @@ template class Matrix: public RectangularMatrix{typename Corrade::Containers::Implementation::GenerateSequence::Type{}, Vector(value)} {} + constexpr explicit Matrix(IdentityInitT, T value = T(1)) noexcept: RectangularMatrix{IdentityInit, value} {} /** @copydoc RectangularMatrix::RectangularMatrix(ZeroInitT) */ constexpr explicit Matrix(ZeroInitT) noexcept: RectangularMatrix{ZeroInit} {} diff --git a/src/Magnum/Math/RectangularMatrix.h b/src/Magnum/Math/RectangularMatrix.h index 64e5cdda1..be3469295 100644 --- a/src/Magnum/Math/RectangularMatrix.h +++ b/src/Magnum/Math/RectangularMatrix.h @@ -124,9 +124,23 @@ template class RectangularMatrix { */ constexpr /*implicit*/ RectangularMatrix() noexcept: RectangularMatrix{typename Corrade::Containers::Implementation::GenerateSequence::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{typename Corrade::Containers::Implementation::GenerateSequence::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{typename Corrade::Containers::Implementation::GenerateSequence::Type{}, Vector(value)} {} + /** @brief Construct without initializing the contents */ explicit RectangularMatrix(Magnum::NoInitT) noexcept: RectangularMatrix{typename Corrade::Containers::Implementation::GenerateSequence::Type{}, Magnum::NoInit} {} @@ -442,9 +456,6 @@ template class RectangularMatrix { #else private: #endif - /* Implementation for RectangularMatrix::fromDiagonal() and Matrix(IdentityInitT, T) */ - template constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence, const Vector& diagonal); - /* Implementation for RectangularMatrix::RectangularMatrix(T) and Matrix(T) */ /* MSVC 2015 can't handle {} here */ template constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence, T value) noexcept: _data{Vector((static_cast(sequence), value))...} {} @@ -455,6 +466,9 @@ template class RectangularMatrix { template friend class Matrix; template friend struct Implementation::MatrixDeterminant; + /* Implementation for RectangularMatrix::fromDiagonal() and RectangularMatrix(IdentityInitT, T) */ + template constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence, const Vector& diagonal); + /* Implementation for RectangularMatrix::RectangularMatrix(const RectangularMatrix&) */ template constexpr explicit RectangularMatrix(Corrade::Containers::Implementation::Sequence, const RectangularMatrix& matrix) noexcept: _data{Vector(matrix[sequence])...} {} diff --git a/src/Magnum/Math/Test/RectangularMatrixTest.cpp b/src/Magnum/Math/Test/RectangularMatrixTest.cpp index b91c4fd78..948add31d 100644 --- a/src/Magnum/Math/Test/RectangularMatrixTest.cpp +++ b/src/Magnum/Math/Test/RectangularMatrixTest.cpp @@ -59,7 +59,8 @@ struct RectangularMatrixTest: Corrade::TestSuite::Tester { explicit RectangularMatrixTest(); void construct(); - void constructDefault(); + void constructZero(); + void constructIdentity(); void constructNoInit(); void constructOneValue(); void constructOneComponent(); @@ -112,7 +113,8 @@ typedef Vector<2, Int> Vector2i; RectangularMatrixTest::RectangularMatrixTest() { addTests({&RectangularMatrixTest::construct, - &RectangularMatrixTest::constructDefault, + &RectangularMatrixTest::constructZero, + &RectangularMatrixTest::constructIdentity, &RectangularMatrixTest::constructNoInit, &RectangularMatrixTest::constructOneValue, &RectangularMatrixTest::constructOneComponent, @@ -160,7 +162,7 @@ void RectangularMatrixTest::construct() { CORRADE_VERIFY(std::is_nothrow_constructible::value); } -void RectangularMatrixTest::constructDefault() { +void RectangularMatrixTest::constructZero() { constexpr Matrix4x3 a; constexpr Matrix4x3 b{ZeroInit}; CORRADE_COMPARE(a, Matrix4x3(Vector3(0.0f, 0.0f, 0.0f), @@ -179,6 +181,24 @@ void RectangularMatrixTest::constructDefault() { CORRADE_VERIFY(!std::is_convertible::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::value); + + /* Implicit construction is not allowed */ + CORRADE_VERIFY(!std::is_convertible::value); +} + void RectangularMatrixTest::constructNoInit() { Matrix3x4 a{Vector4(1.0f, 2.0f, 3.0f, 4.0f), Vector4(5.0f, 6.0f, 7.0f, 8.0f),