diff --git a/src/Math/Matrix.h b/src/Math/Matrix.h index 075326f79..5baa1ff34 100644 --- a/src/Math/Matrix.h +++ b/src/Math/Matrix.h @@ -90,12 +90,13 @@ template class Matrix { * @brief Default constructor * * You can also explicitly call this constructor with - * `Matrix m(Matrix::Identity);`. + * `Matrix m(Matrix::Identity);`. Optional parameter @p value allows + * you to specify value on diagonal. */ - inline explicit Matrix(IdentityType = Identity): _data() { + inline explicit Matrix(IdentityType = Identity, T value = T(1)): _data() { /** @todo constexpr how? */ for(size_t i = 0; i != size; ++i) - _data[size*i+i] = static_cast(1); + _data[size*i+i] = value; } #ifndef DOXYGEN_GENERATING_OUTPUT diff --git a/src/Math/Matrix3.h b/src/Math/Matrix3.h index a3ebb5fc1..f54174700 100644 --- a/src/Math/Matrix3.h +++ b/src/Math/Matrix3.h @@ -46,11 +46,11 @@ template class Matrix3: public Matrix { inline constexpr explicit Matrix3(typename Matrix::ZeroType): Matrix(Matrix::Zero) {} /** @copydoc Matrix::Matrix(IdentityType) */ - inline constexpr explicit Matrix3(typename Matrix::IdentityType = Matrix::Identity): Matrix{ + inline constexpr explicit Matrix3(typename Matrix::IdentityType = Matrix::Identity, T value = T(1)): Matrix{ /** @todo Make this in Matrix itself, after it will be constexpr */ - 1.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 1.0f + value, 0.0f, 0.0f, + 0.0f, value, 0.0f, + 0.0f, 0.0f, value } {} #ifndef DOXYGEN_GENERATING_OUTPUT diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index 26e5e7072..d5f8bfc75 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -114,12 +114,12 @@ template class Matrix4: public Matrix { inline constexpr explicit Matrix4(typename Matrix::ZeroType): Matrix(Matrix::Zero) {} /** @copydoc Matrix::Matrix(IdentityType) */ - inline constexpr explicit Matrix4(typename Matrix::IdentityType = Matrix::Identity): Matrix{ + inline constexpr explicit Matrix4(typename Matrix::IdentityType = Matrix::Identity, T value = T(1)): Matrix{ /** @todo Make this in Matrix itself, after it will be constexpr */ - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f + value, 0.0f, 0.0f, 0.0f, + 0.0f, value, 0.0f, 0.0f, + 0.0f, 0.0f, value, 0.0f, + 0.0f, 0.0f, 0.0f, value } {} #ifndef DOXYGEN_GENERATING_OUTPUT diff --git a/src/Math/Test/Matrix3Test.cpp b/src/Math/Test/Matrix3Test.cpp index fb9b7881a..ab550d6a8 100644 --- a/src/Math/Test/Matrix3Test.cpp +++ b/src/Math/Test/Matrix3Test.cpp @@ -29,6 +29,28 @@ namespace Magnum { namespace Math { namespace Test { typedef Math::Matrix3 Matrix3; +void Matrix3Test::constructIdentity() { + Matrix3 identity; + Matrix3 identity2(Matrix3::Identity); + Matrix3 identity3(Matrix3::Identity, 4.0f); + + Matrix3 identityExpected( + 1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f + ); + + Matrix3 identity3Expected( + 4.0f, 0.0f, 0.0f, + 0.0f, 4.0f, 0.0f, + 0.0f, 0.0f, 4.0f + ); + + QVERIFY(identity == identityExpected); + QVERIFY(identity2 == identityExpected); + QVERIFY(identity3 == identity3Expected); +} + void Matrix3Test::debug() { Matrix3 m( 3.0f, 5.0f, 8.0f, diff --git a/src/Math/Test/Matrix3Test.h b/src/Math/Test/Matrix3Test.h index eec4091cf..d482a6a0c 100644 --- a/src/Math/Test/Matrix3Test.h +++ b/src/Math/Test/Matrix3Test.h @@ -23,6 +23,8 @@ class Matrix3Test: public QObject { Q_OBJECT private slots: + void constructIdentity(); + void debug(); }; diff --git a/src/Math/Test/Matrix4Test.cpp b/src/Math/Test/Matrix4Test.cpp index 62437f47f..ecbac538f 100644 --- a/src/Math/Test/Matrix4Test.cpp +++ b/src/Math/Test/Matrix4Test.cpp @@ -31,6 +31,30 @@ namespace Magnum { namespace Math { namespace Test { typedef Math::Matrix4 Matrix4; typedef Math::Matrix3 Matrix3; +void Matrix4Test::constructIdentity() { + Matrix4 identity; + Matrix4 identity2(Matrix4::Identity); + Matrix4 identity3(Matrix4::Identity, 4.0f); + + Matrix4 identityExpected( + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f + ); + + Matrix4 identity3Expected( + 4.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 4.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 4.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 4.0f + ); + + QVERIFY(identity == identityExpected); + QVERIFY(identity2 == identityExpected); + QVERIFY(identity3 == identity3Expected); +} + void Matrix4Test::translation() { Matrix4 matrix( 1.0f, 0.0f, 0.0f, 0.0f, diff --git a/src/Math/Test/Matrix4Test.h b/src/Math/Test/Matrix4Test.h index e4da204ea..857ace581 100644 --- a/src/Math/Test/Matrix4Test.h +++ b/src/Math/Test/Matrix4Test.h @@ -23,6 +23,8 @@ class Matrix4Test: public QObject { Q_OBJECT private slots: + void constructIdentity(); + void translation(); void scaling(); void rotation(); diff --git a/src/Math/Test/MatrixTest.cpp b/src/Math/Test/MatrixTest.cpp index 998d1b66e..d1e3a7ea9 100644 --- a/src/Math/Test/MatrixTest.cpp +++ b/src/Math/Test/MatrixTest.cpp @@ -66,6 +66,7 @@ void MatrixTest::constructFromVectors() { void MatrixTest::constructIdentity() { Matrix4 identity; Matrix4 identity2(Matrix4::Identity); + Matrix4 identity3(Matrix4::Identity, 4.0f); Matrix4 identityExpected( 1.0f, 0.0f, 0.0f, 0.0f, @@ -74,8 +75,16 @@ void MatrixTest::constructIdentity() { 0.0f, 0.0f, 0.0f, 1.0f ); + Matrix4 identity3Expected( + 4.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 4.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 4.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 4.0f + ); + QVERIFY(identity == identityExpected); QVERIFY(identity2 == identityExpected); + QVERIFY(identity3 == identity3Expected); } void MatrixTest::constructZero() {