From 5293429f31421efc2e91b7e48364a02630423dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 17 Apr 2012 00:26:04 +0200 Subject: [PATCH] Ability to construct Matrix from column vectors. --- src/Math/Matrix.h | 28 ++++++++++++++++++++++++++++ src/Math/Matrix3.h | 5 +++++ src/Math/Matrix4.h | 5 +++++ src/Math/Test/MatrixTest.cpp | 14 ++++++++++++++ src/Math/Test/MatrixTest.h | 1 + 5 files changed, 53 insertions(+) diff --git a/src/Math/Matrix.h b/src/Math/Matrix.h index 0b6603648..639748a29 100644 --- a/src/Math/Matrix.h +++ b/src/Math/Matrix.h @@ -25,6 +25,17 @@ namespace Magnum { namespace Math { namespace Implementation { template class MatrixDeterminant; + + #ifndef DOXYGEN_GENERATING_OUTPUT + template struct Sequence {}; + + template struct GenerateSequence: + GenerateSequence {}; + + template struct GenerateSequence<0, S...> { + typedef Sequence Type; + }; + #endif } /** @@ -52,6 +63,15 @@ template class Matrix { return *reinterpret_cast*>(data); } + /** + * @brief %Matrix from column vectors + * @param first First column vector + * @param next Next column vectors + */ + template inline constexpr static Matrix from(const Vector& first, const U&... next) { + return from(typename Implementation::GenerateSequence::Type(), first, next...); + } + /** * @brief Default constructor * @param identity Create identity matrix instead of zero matrix. @@ -189,6 +209,14 @@ template class Matrix { } private: + template inline constexpr static Matrix from(Implementation::Sequence s, const Vector& first, U... next) { + return from(s, next..., first[sequence]...); + } + + template inline constexpr static Matrix from(Implementation::Sequence s, T first, U... next) { + return Matrix(first, next...); + } + T _data[size*size]; }; diff --git a/src/Math/Matrix3.h b/src/Math/Matrix3.h index 24e533972..fca31ae8c 100644 --- a/src/Math/Matrix3.h +++ b/src/Math/Matrix3.h @@ -37,6 +37,11 @@ template class Matrix3: public Matrix { return *reinterpret_cast*>(data); } + /** @copydoc Matrix::from(const Vector&, const U&...) */ + template inline constexpr static Matrix3 from(const Vector& first, const U&... next) { + return Matrix::from(first, next...); + } + /** @copydoc Matrix::Matrix(bool) */ inline constexpr explicit Matrix3(bool identity = true): Matrix{ /** @todo Make this in Matrix itself, after it will be constexpr */ diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index 345fb7d37..0927efe57 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -43,6 +43,11 @@ template class Matrix4: public Matrix { return *reinterpret_cast*>(data); } + /** @copydoc Matrix::from(const Vector&, const U&...) */ + template inline constexpr static Matrix4 from(const Vector& first, const U&... next) { + return Matrix::from(first, next...); + } + /** * @brief Translation matrix * @param vec Translation vector diff --git a/src/Math/Test/MatrixTest.cpp b/src/Math/Test/MatrixTest.cpp index 1d3124a38..c5e96c9c7 100644 --- a/src/Math/Test/MatrixTest.cpp +++ b/src/Math/Test/MatrixTest.cpp @@ -49,6 +49,20 @@ void MatrixTest::construct() { QVERIFY(Matrix4::from(m) == expected); } +void MatrixTest::constructFromVectors() { + Matrix4 actual = Matrix4::from(Vector4(1.0f, 2.0f, 3.0f, 4.0f), + Vector4(5.0f, 6.0f, 7.0f, 8.0f), + Vector4(9.0f, 10.0f, 11.0f, 12.0f), + Vector4(13.0f, 14.0f, 15.0f, 16.0f)); + + Matrix4 expected(1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f, + 13.0f, 14.0f, 15.0f, 16.0f); + + QVERIFY(actual == expected); +} + void MatrixTest::constructIdentity() { Matrix4 identity; diff --git a/src/Math/Test/MatrixTest.h b/src/Math/Test/MatrixTest.h index ab51838e9..4fd5b4f76 100644 --- a/src/Math/Test/MatrixTest.h +++ b/src/Math/Test/MatrixTest.h @@ -24,6 +24,7 @@ class MatrixTest: public QObject { private slots: void construct(); + void constructFromVectors(); void constructIdentity(); void constructZero(); void data();