From 9e67c864734724af12d9e1c77b95fb709ef465a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 29 May 2014 22:06:26 +0200 Subject: [PATCH] Math: C++14 constexpr in Vector2, Vector3 and Vector4. --- src/Magnum/Math/Test/Vector2Test.cpp | 24 ++++++++++------- src/Magnum/Math/Test/Vector3Test.cpp | 32 +++++++++++----------- src/Magnum/Math/Test/Vector4Test.cpp | 40 ++++++++++++++-------------- src/Magnum/Math/Vector2.h | 10 +++---- src/Magnum/Math/Vector3.h | 14 +++++----- src/Magnum/Math/Vector4.h | 16 +++++------ 6 files changed, 71 insertions(+), 65 deletions(-) diff --git a/src/Magnum/Math/Test/Vector2Test.cpp b/src/Magnum/Math/Test/Vector2Test.cpp index 2f44ea828..e3c2be12e 100644 --- a/src/Magnum/Math/Test/Vector2Test.cpp +++ b/src/Magnum/Math/Test/Vector2Test.cpp @@ -157,17 +157,19 @@ void Vector2Test::access() { CORRADE_COMPARE(vec.y(), -2.0f); constexpr Vector2 cvec(1.0f, -2.0f); - constexpr Float x = cvec.x(); - constexpr Float y = cvec.y(); - CORRADE_COMPARE(x, 1.0f); - CORRADE_COMPARE(y, -2.0f); + constexpr auto cx = cvec.x(); + constexpr auto cy = cvec.y(); + CORRADE_COMPARE(cx, 1.0f); + CORRADE_COMPARE(cy, -2.0f); } void Vector2Test::cross() { - Vector2i a(1, -1); - Vector2i b(4, 3); + constexpr Vector2i a(1, -1); + constexpr Vector2i b(4, 3); - CORRADE_COMPARE(Vector2i::cross(a, b), 7); + constexpr auto c = Vector2i::cross(a, b); + + CORRADE_COMPARE(c, 7); CORRADE_COMPARE(Vector3i::cross({a, 0}, {b, 0}), Vector3i(0, 0, Vector2i::cross(a, b))); } @@ -186,14 +188,16 @@ void Vector2Test::scales() { } void Vector2Test::perpendicular() { - const Vector2 a(0.5f, -15.0f); - CORRADE_COMPARE(a.perpendicular(), Vector2(15.0f, 0.5f)); + constexpr Vector2 a(0.5f, -15.0f); + constexpr auto b = a.perpendicular(); + CORRADE_COMPARE(b, Vector2(15.0f, 0.5f)); CORRADE_COMPARE(Vector2::dot(a.perpendicular(), a), 0.0f); CORRADE_COMPARE(Vector2::xAxis().perpendicular(), Vector2::yAxis()); } void Vector2Test::aspectRatio() { - CORRADE_COMPARE(Vector2(3.0f, 4.0f).aspectRatio(), 0.75f); + constexpr auto a = Vector2(3.0f, 4.0f).aspectRatio(); + CORRADE_COMPARE(a, 0.75f); } void Vector2Test::minmax() { diff --git a/src/Magnum/Math/Test/Vector3Test.cpp b/src/Magnum/Math/Test/Vector3Test.cpp index ad987b913..bd1712c37 100644 --- a/src/Magnum/Math/Test/Vector3Test.cpp +++ b/src/Magnum/Math/Test/Vector3Test.cpp @@ -166,25 +166,27 @@ void Vector3Test::access() { CORRADE_COMPARE(vec.b(), 5.0f); constexpr Vector3 cvec(1.0f, -2.0f, 5.0f); - constexpr Float x = cvec.x(); - constexpr Float r = cvec.r(); - constexpr Float y = cvec.y(); - constexpr Float g = cvec.g(); - constexpr Float z = cvec.z(); - constexpr Float b = cvec.b(); - CORRADE_COMPARE(x, 1.0f); - CORRADE_COMPARE(r, 1.0f); - CORRADE_COMPARE(y, -2.0f); - CORRADE_COMPARE(g, -2.0f); - CORRADE_COMPARE(z, 5.0f); - CORRADE_COMPARE(b, 5.0f); + constexpr auto cx = cvec.x(); + constexpr auto cr = cvec.r(); + constexpr auto cy = cvec.y(); + constexpr auto cg = cvec.g(); + constexpr auto cz = cvec.z(); + constexpr auto cb = cvec.b(); + CORRADE_COMPARE(cx, 1.0f); + CORRADE_COMPARE(cr, 1.0f); + CORRADE_COMPARE(cy, -2.0f); + CORRADE_COMPARE(cg, -2.0f); + CORRADE_COMPARE(cz, 5.0f); + CORRADE_COMPARE(cb, 5.0f); } void Vector3Test::cross() { - Vector3i a(1, -1, 1); - Vector3i b(4, 3, 7); + constexpr Vector3i a(1, -1, 1); + constexpr Vector3i b(4, 3, 7); - CORRADE_COMPARE(Vector3i::cross(a, b), Vector3i(-10, -3, 7)); + constexpr auto c = Vector3i::cross(a, b); + + CORRADE_COMPARE(c, Vector3i(-10, -3, 7)); } void Vector3Test::axes() { diff --git a/src/Magnum/Math/Test/Vector4Test.cpp b/src/Magnum/Math/Test/Vector4Test.cpp index 7a9a3372a..e8d5cc142 100644 --- a/src/Magnum/Math/Test/Vector4Test.cpp +++ b/src/Magnum/Math/Test/Vector4Test.cpp @@ -155,7 +155,7 @@ void Vector4Test::convert() { } void Vector4Test::access() { - Vector4 vec(1.0f, -2.0f, 5.0f, 0.5f); + constexpr Vector4 vec(1.0f, -2.0f, 5.0f, 0.5f); CORRADE_COMPARE(vec.x(), 1.0f); CORRADE_COMPARE(vec.r(), 1.0f); CORRADE_COMPARE(vec.y(), -2.0f); @@ -165,23 +165,23 @@ void Vector4Test::access() { CORRADE_COMPARE(vec.w(), 0.5f); CORRADE_COMPARE(vec.a(), 0.5f); - constexpr Vector4 cvec(1.0f, -2.0f, 5.0f, 0.5f); - constexpr Float x = cvec.x(); - constexpr Float r = cvec.r(); - constexpr Float y = cvec.y(); - constexpr Float g = cvec.g(); - constexpr Float z = cvec.z(); - constexpr Float b = cvec.b(); - constexpr Float w = cvec.w(); - constexpr Float a = cvec.a(); - CORRADE_COMPARE(x, 1.0f); - CORRADE_COMPARE(r, 1.0f); - CORRADE_COMPARE(y, -2.0f); - CORRADE_COMPARE(g, -2.0f); - CORRADE_COMPARE(z, 5.0f); - CORRADE_COMPARE(b, 5.0f); - CORRADE_COMPARE(w, 0.5f); - CORRADE_COMPARE(a, 0.5f); + constexpr const Vector4 cvec(1.0f, -2.0f, 5.0f, 0.5f); + constexpr auto cx = cvec.x(); + constexpr auto cr = cvec.r(); + constexpr auto cy = cvec.y(); + constexpr auto cg = cvec.g(); + constexpr auto cz = cvec.z(); + constexpr auto cb = cvec.b(); + constexpr auto cw = cvec.w(); + constexpr auto ca = cvec.a(); + CORRADE_COMPARE(cx, 1.0f); + CORRADE_COMPARE(cr, 1.0f); + CORRADE_COMPARE(cy, -2.0f); + CORRADE_COMPARE(cg, -2.0f); + CORRADE_COMPARE(cz, 5.0f); + CORRADE_COMPARE(cb, 5.0f); + CORRADE_COMPARE(cw, 0.5f); + CORRADE_COMPARE(ca, 0.5f); } void Vector4Test::threeComponent() { @@ -189,7 +189,7 @@ void Vector4Test::threeComponent() { CORRADE_COMPARE(a.xyz(), Vector3(1.0f, 2.0f, 3.0f)); CORRADE_COMPARE(a.rgb(), Vector3(1.0f, 2.0f, 3.0f)); - constexpr Vector4 b(1.0f, 2.0f, 3.0f, 4.0f); + constexpr const Vector4 b(1.0f, 2.0f, 3.0f, 4.0f); constexpr Vector3 c = b.xyz(); constexpr Float d = b.xyz().y(); CORRADE_COMPARE(c, Vector3(1.0f, 2.0f, 3.0f)); @@ -200,7 +200,7 @@ void Vector4Test::twoComponent() { Vector4 a(1.0f, 2.0f, 3.0f, 4.0f); CORRADE_COMPARE(a.xy(), Vector2(1.0f, 2.0f)); - constexpr Vector4 b(1.0f, 2.0f, 3.0f, 4.0f); + constexpr const Vector4 b(1.0f, 2.0f, 3.0f, 4.0f); constexpr Vector2 c = b.xy(); constexpr Float d = b.xy().x(); CORRADE_COMPARE(c, Vector2(1.0f, 2.0f)); diff --git a/src/Magnum/Math/Vector2.h b/src/Magnum/Math/Vector2.h index d40435e62..3ce3e5e1b 100644 --- a/src/Magnum/Math/Vector2.h +++ b/src/Magnum/Math/Vector2.h @@ -94,7 +94,7 @@ template class Vector2: public Vector<2, T> { * @see @ref perpendicular(), * @ref dot(const Vector&, const Vector&) */ - static T cross(const Vector2& a, const Vector2& b) { + constexpr static T cross(const Vector2& a, const Vector2& b) { return Vector<2, T>::dot(a.perpendicular(), b); } @@ -122,9 +122,9 @@ template class Vector2: public Vector<2, T> { /** @brief Copy constructor */ constexpr Vector2(const Vector<2, T>& other): Vector<2, T>(other) {} - T& x() { return (*this)[0]; } /**< @brief X component */ + constexpr T& x() { return (*this)[0]; } /**< @brief X component */ constexpr T x() const { return (*this)[0]; } /**< @overload */ - T& y() { return (*this)[1]; } /**< @brief Y component */ + constexpr T& y() { return (*this)[1]; } /**< @brief Y component */ constexpr T y() const { return (*this)[1]; } /**< @overload */ /** @@ -137,7 +137,7 @@ template class Vector2: public Vector<2, T> { * @ref dot(const Vector&, const Vector&), * @ref operator-() const */ - Vector2 perpendicular() const { return {-y(), x()}; } + constexpr Vector2 perpendicular() const { return {-y(), x()}; } /** * @brief Aspect ratio @@ -146,7 +146,7 @@ template class Vector2: public Vector<2, T> { * a = \frac{v_x}{v_y} * @f] */ - T aspectRatio() const { return x()/y(); } + constexpr T aspectRatio() const { return x()/y(); } /** * @brief Minimum and maximum value diff --git a/src/Magnum/Math/Vector3.h b/src/Magnum/Math/Vector3.h index 0bb553cc4..f8df6830a 100644 --- a/src/Magnum/Math/Vector3.h +++ b/src/Magnum/Math/Vector3.h @@ -110,7 +110,7 @@ template class Vector3: public Vector<3, T> { * @f] * @see @ref Vector2::cross() */ - static Vector3 cross(const Vector3& a, const Vector3& b) { + constexpr static Vector3 cross(const Vector3& a, const Vector3& b) { return swizzle<'y', 'z', 'x'>(a)*swizzle<'z', 'x', 'y'>(b) - swizzle<'z', 'x', 'y'>(a)*swizzle<'y', 'z', 'x'>(b); } @@ -153,7 +153,7 @@ template class Vector3: public Vector<3, T> { * * @see @ref r() */ - T& x() { return (*this)[0]; } + constexpr T& x() { return (*this)[0]; } constexpr T x() const { return (*this)[0]; } /**< @overload */ /** @@ -161,7 +161,7 @@ template class Vector3: public Vector<3, T> { * * @see @ref g() */ - T& y() { return (*this)[1]; } + constexpr T& y() { return (*this)[1]; } constexpr T y() const { return (*this)[1]; } /**< @overload */ /** @@ -169,7 +169,7 @@ template class Vector3: public Vector<3, T> { * * @see @ref b() */ - T& z() { return (*this)[2]; } + constexpr T& z() { return (*this)[2]; } constexpr T z() const { return (*this)[2]; } /**< @overload */ /** @@ -177,7 +177,7 @@ template class Vector3: public Vector<3, T> { * * Equivalent to @ref x(). */ - T& r() { return x(); } + constexpr T& r() { return x(); } constexpr T r() const { return x(); } /**< @overload */ /** @@ -185,7 +185,7 @@ template class Vector3: public Vector<3, T> { * * Equivalent to @ref y(). */ - T& g() { return y(); } + constexpr T& g() { return y(); } constexpr T g() const { return y(); } /**< @overload */ /** @@ -193,7 +193,7 @@ template class Vector3: public Vector<3, T> { * * Equivalent to @ref z(). */ - T& b() { return z(); } + constexpr T& b() { return z(); } constexpr T b() const { return z(); } /**< @overload */ /** diff --git a/src/Magnum/Math/Vector4.h b/src/Magnum/Math/Vector4.h index fbedacf28..ccfd9346b 100644 --- a/src/Magnum/Math/Vector4.h +++ b/src/Magnum/Math/Vector4.h @@ -82,7 +82,7 @@ template class Vector4: public Vector<4, T> { * * @see @ref r() */ - T& x() { return (*this)[0]; } + constexpr T& x() { return (*this)[0]; } constexpr T x() const { return (*this)[0]; } /**< @overload */ /** @@ -90,7 +90,7 @@ template class Vector4: public Vector<4, T> { * * @see @ref g() */ - T& y() { return (*this)[1]; } + constexpr T& y() { return (*this)[1]; } constexpr T y() const { return (*this)[1]; } /**< @overload */ /** @@ -98,7 +98,7 @@ template class Vector4: public Vector<4, T> { * * @see @ref b() */ - T& z() { return (*this)[2]; } + constexpr T& z() { return (*this)[2]; } constexpr T z() const { return (*this)[2]; } /**< @overload */ /** @@ -106,7 +106,7 @@ template class Vector4: public Vector<4, T> { * * @see @ref a() */ - T& w() { return (*this)[3]; } + constexpr T& w() { return (*this)[3]; } constexpr T w() const { return (*this)[3]; } /**< @overload */ /** @@ -114,7 +114,7 @@ template class Vector4: public Vector<4, T> { * * Equivalent to @ref x(). */ - T& r() { return x(); } + constexpr T& r() { return x(); } constexpr T r() const { return x(); } /**< @overload */ /** @@ -122,7 +122,7 @@ template class Vector4: public Vector<4, T> { * * Equivalent to @ref y(). */ - T& g() { return y(); } + constexpr T& g() { return y(); } constexpr T g() const { return y(); } /**< @overload */ /** @@ -130,7 +130,7 @@ template class Vector4: public Vector<4, T> { * * Equivalent to @ref z(). */ - T& b() { return z(); } + constexpr T& b() { return z(); } constexpr T b() const { return z(); } /**< @overload */ /** @@ -138,7 +138,7 @@ template class Vector4: public Vector<4, T> { * * Equivalent to @ref w(). */ - T& a() { return w(); } + constexpr T& a() { return w(); } constexpr T a() const { return w(); } /**< @overload */ /**