From 02ebf7aed62cd44475c93b8801da891c4b1c9ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 29 May 2014 22:47:56 +0200 Subject: [PATCH] Math: C++14 constexpr in Matrix3 and Matrix4. --- src/Magnum/Math/Matrix3.h | 12 ++++++------ src/Magnum/Math/Matrix4.h | 20 ++++++++++---------- src/Magnum/Math/Test/Matrix3Test.cpp | 3 ++- src/Magnum/Math/Test/Matrix4Test.cpp | 8 ++++++-- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Magnum/Math/Matrix3.h b/src/Magnum/Math/Matrix3.h index bf00f5b2d..37b4bec68 100644 --- a/src/Magnum/Math/Matrix3.h +++ b/src/Magnum/Math/Matrix3.h @@ -105,7 +105,7 @@ template class Matrix3: public Matrix3x3 { * @see @ref Matrix4::orthographicProjection(), * @ref Matrix4::perspectiveProjection() */ - static Matrix3 projection(const Vector2& size) { + constexpr static Matrix3 projection(const Vector2& size) { return scaling(2.0f/size); } @@ -247,7 +247,7 @@ template class Matrix3: public Matrix3x3 { * First two elements of first column. * @see @ref up(), @ref Vector2::xAxis(), @ref Matrix4::right() */ - Vector2& right() { return (*this)[0].xy(); } + constexpr Vector2& right() { return (*this)[0].xy(); } constexpr Vector2 right() const { return (*this)[0].xy(); } /**< @overload */ /** @@ -256,7 +256,7 @@ template class Matrix3: public Matrix3x3 { * First two elements of second column. * @see @ref right(), @ref Vector2::yAxis(), @ref Matrix4::up() */ - Vector2& up() { return (*this)[1].xy(); } + constexpr Vector2& up() { return (*this)[1].xy(); } constexpr Vector2 up() const { return (*this)[1].xy(); } /**< @overload */ /** @@ -267,7 +267,7 @@ template class Matrix3: public Matrix3x3 { * @ref translation(const Vector2&), * @ref Matrix4::translation() */ - Vector2& translation() { return (*this)[2].xy(); } + constexpr Vector2& translation() { return (*this)[2].xy(); } constexpr Vector2 translation() const { return (*this)[2].xy(); } /**< @overload */ /** @@ -297,7 +297,7 @@ template class Matrix3: public Matrix3x3 { * @ref Matrix4::transformVector() * @todo extract 2x2 matrix and multiply directly? (benchmark that) */ - Vector2 transformVector(const Vector2& vector) const { + constexpr Vector2 transformVector(const Vector2& vector) const { return ((*this)*Vector3(vector, T(0))).xy(); } @@ -311,7 +311,7 @@ template class Matrix3: public Matrix3x3 { * @see @ref DualComplex::transformPoint(), * @ref Matrix4::transformPoint() */ - Vector2 transformPoint(const Vector2& vector) const { + constexpr Vector2 transformPoint(const Vector2& vector) const { return ((*this)*Vector3(vector, T(1))).xy(); } diff --git a/src/Magnum/Math/Matrix4.h b/src/Magnum/Math/Matrix4.h index 46e34b665..d5d01b281 100644 --- a/src/Magnum/Math/Matrix4.h +++ b/src/Magnum/Math/Matrix4.h @@ -151,7 +151,7 @@ template class Matrix4: public Matrix4x4 { * * @see @ref perspectiveProjection(), @ref Matrix3::projection() */ - static Matrix4 orthographicProjection(const Vector2& size, T near, T far); + constexpr static Matrix4 orthographicProjection(const Vector2& size, T near, T far); /** * @brief 3D perspective projection matrix @@ -161,7 +161,7 @@ template class Matrix4: public Matrix4x4 { * * @see @ref orthographicProjection(), @ref Matrix3::projection() */ - static Matrix4 perspectiveProjection(const Vector2& size, T near, T far); + constexpr static Matrix4 perspectiveProjection(const Vector2& size, T near, T far); /** * @brief 3D perspective projection matrix @@ -309,7 +309,7 @@ template class Matrix4: public Matrix4x4 { * @see @ref up(), @ref backward(), @ref Vector3::xAxis(), * @ref Matrix3::right() */ - Vector3& right() { return (*this)[0].xyz(); } + constexpr Vector3& right() { return (*this)[0].xyz(); } constexpr Vector3 right() const { return (*this)[0].xyz(); } /**< @overload */ /** @@ -319,7 +319,7 @@ template class Matrix4: public Matrix4x4 { * @see @ref right(), @ref backward(), @ref Vector3::yAxis(), * @ref Matrix3::up() */ - Vector3& up() { return (*this)[1].xyz(); } + constexpr Vector3& up() { return (*this)[1].xyz(); } constexpr Vector3 up() const { return (*this)[1].xyz(); } /**< @overload */ /** @@ -328,7 +328,7 @@ template class Matrix4: public Matrix4x4 { * First three elements of third column. * @see @ref right(), @ref up(), @ref Vector3::yAxis() */ - Vector3& backward() { return (*this)[2].xyz(); } + constexpr Vector3& backward() { return (*this)[2].xyz(); } constexpr Vector3 backward() const { return (*this)[2].xyz(); } /**< @overload */ /** @@ -339,7 +339,7 @@ template class Matrix4: public Matrix4x4 { * @ref translation(const Vector3&), * @ref Matrix3::translation() */ - Vector3& translation() { return (*this)[3].xyz(); } + constexpr Vector3& translation() { return (*this)[3].xyz(); } constexpr Vector3 translation() const { return (*this)[3].xyz(); } /**< @overload */ /** @@ -369,7 +369,7 @@ template class Matrix4: public Matrix4x4 { * @ref Matrix3::transformVector() * @todo extract 3x3 matrix and multiply directly? (benchmark that) */ - Vector3 transformVector(const Vector3& vector) const { + constexpr Vector3 transformVector(const Vector3& vector) const { return ((*this)*Vector4(vector, T(0))).xyz(); } @@ -383,7 +383,7 @@ template class Matrix4: public Matrix4x4 { * @see @ref DualQuaternion::transformPoint(), * @ref Matrix3::transformPoint() */ - Vector3 transformPoint(const Vector3& vector) const { + constexpr Vector3 transformPoint(const Vector3& vector) const { return ((*this)*Vector4(vector, T(1))).xyz(); } @@ -466,7 +466,7 @@ template Matrix4 Matrix4::reflection(const Vector3& normal) { return from(Matrix3x3() - T(2)*normal*RectangularMatrix<1, 3, T>(normal).transposed(), {}); } -template Matrix4 Matrix4::orthographicProjection(const Vector2& size, const T near, const T far) { +template constexpr Matrix4 Matrix4::orthographicProjection(const Vector2& size, const T near, const T far) { const Vector2 xyScale = T(2.0)/size; const T zScale = T(2.0)/(near-far); @@ -476,7 +476,7 @@ template Matrix4 Matrix4::orthographicProjection(const Vector2 { T(0), T(0), near*zScale-T(1), T(1)}}; } -template Matrix4 Matrix4::perspectiveProjection(const Vector2& size, const T near, const T far) { +template constexpr Matrix4 Matrix4::perspectiveProjection(const Vector2& size, const T near, const T far) { Vector2 xyScale = 2*near/size; T zScale = T(1.0)/(near-far); diff --git a/src/Magnum/Math/Test/Matrix3Test.cpp b/src/Magnum/Math/Test/Matrix3Test.cpp index 61274b772..62b413831 100644 --- a/src/Magnum/Math/Test/Matrix3Test.cpp +++ b/src/Magnum/Math/Test/Matrix3Test.cpp @@ -259,7 +259,8 @@ void Matrix3Test::projection() { { 0.0f, 2.0f/3.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}); - CORRADE_COMPARE(Matrix3::projection({4.0f, 3.0f}), expected); + constexpr auto a = Matrix3::projection({4.0f, 3.0f}); + CORRADE_COMPARE(a, expected); } void Matrix3Test::fromParts() { diff --git a/src/Magnum/Math/Test/Matrix4Test.cpp b/src/Magnum/Math/Test/Matrix4Test.cpp index a22c00f62..32a63d5fd 100644 --- a/src/Magnum/Math/Test/Matrix4Test.cpp +++ b/src/Magnum/Math/Test/Matrix4Test.cpp @@ -318,19 +318,23 @@ void Matrix4Test::reflection() { } void Matrix4Test::orthographicProjection() { + constexpr auto a = Matrix4::orthographicProjection({5.0f, 4.0f}, 1, 9); + Matrix4 expected({0.4f, 0.0f, 0.0f, 0.0f}, {0.0f, 0.5f, 0.0f, 0.0f}, {0.0f, 0.0f, -0.25f, 0.0f}, {0.0f, 0.0f, -1.25f, 1.0f}); - CORRADE_COMPARE(Matrix4::orthographicProjection({5.0f, 4.0f}, 1, 9), expected); + CORRADE_COMPARE(a, expected); } void Matrix4Test::perspectiveProjection() { + constexpr auto a = Matrix4::perspectiveProjection({16.0f, 9.0f}, 32.0f, 100); + Matrix4 expected({4.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 7.111111f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.9411764f, -1.0f}, {0.0f, 0.0f, -94.1176452f, 0.0f}); - CORRADE_COMPARE(Matrix4::perspectiveProjection({16.0f, 9.0f}, 32.0f, 100), expected); + CORRADE_COMPARE(a, expected); } void Matrix4Test::perspectiveProjectionFov() {