From 702c9db6b47de040fcc8b683d56956509e489e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 20 Jun 2015 23:36:19 +0200 Subject: [PATCH] Math: ability to zero-initialize all classes. Previously only matrices allowed to be created either as an identity or zero-initialized. Now all Math classes support that, including (dual) complex numbers and quaternions. --- src/Magnum/Color.h | 3 +++ src/Magnum/Math/Complex.h | 3 +++ src/Magnum/Math/DualComplex.h | 3 +++ src/Magnum/Math/DualQuaternion.h | 3 +++ src/Magnum/Math/Quaternion.h | 3 +++ src/Magnum/Math/Test/ComplexTest.cpp | 7 +++++++ src/Magnum/Math/Test/DualComplexTest.cpp | 7 +++++++ src/Magnum/Math/Test/DualQuaternionTest.cpp | 7 +++++++ src/Magnum/Math/Test/QuaternionTest.cpp | 7 +++++++ src/Magnum/Test/ColorTest.cpp | 9 +++++++++ 10 files changed, 52 insertions(+) diff --git a/src/Magnum/Color.h b/src/Magnum/Color.h index 6ff9c2976..4c3b5d754 100644 --- a/src/Magnum/Color.h +++ b/src/Magnum/Color.h @@ -442,6 +442,9 @@ class BasicColor4: public Math::Vector4 { */ constexpr /*implicit*/ BasicColor4(): Math::Vector4(T(0), T(0), T(0), Implementation::fullChannel()) {} + /** @copydoc Vector4::Vector4(ZeroInitT) */ + constexpr explicit BasicColor4(Math::ZeroInitT): Math::Vector4{Math::ZeroInit} {} + /** * @copydoc BasicColor3::BasicColor3(T) * @param alpha Alpha value, defaults to `1.0` for floating-point types diff --git a/src/Magnum/Math/Complex.h b/src/Magnum/Math/Complex.h index 19ee98fbc..bd98c6f2a 100644 --- a/src/Magnum/Math/Complex.h +++ b/src/Magnum/Math/Complex.h @@ -142,6 +142,9 @@ template class Complex { */ constexpr /*implicit*/ Complex(IdentityInitT = IdentityInit): _real(T(1)), _imaginary(T(0)) {} + /** @brief Construct zero-initialized complex number */ + constexpr explicit Complex(ZeroInitT): _real{}, _imaginary{} {} + /** * @brief Construct complex number from real and imaginary part * diff --git a/src/Magnum/Math/DualComplex.h b/src/Magnum/Math/DualComplex.h index 7ffb80b28..763359036 100644 --- a/src/Magnum/Math/DualComplex.h +++ b/src/Magnum/Math/DualComplex.h @@ -109,6 +109,9 @@ template class DualComplex: public Dual> { constexpr /*implicit*/ DualComplex(IdentityInitT = IdentityInit): Dual>({}, {T(0), T(0)}) {} #endif + /** @brief Construct zero-initialized dual complex number */ + constexpr explicit DualComplex(ZeroInitT): Dual>{Complex{ZeroInit}, Complex{ZeroInit}} {} + /** * @brief Construct dual complex number from real and dual part * diff --git a/src/Magnum/Math/DualQuaternion.h b/src/Magnum/Math/DualQuaternion.h index 09cf6a4d1..b7b57bad0 100644 --- a/src/Magnum/Math/DualQuaternion.h +++ b/src/Magnum/Math/DualQuaternion.h @@ -115,6 +115,9 @@ template class DualQuaternion: public Dual> { constexpr /*implicit*/ DualQuaternion(IdentityInitT = IdentityInit): Dual>({}, {{}, T(0)}) {} #endif + /** @brief Construct zero-initialized dual quaternion */ + constexpr explicit DualQuaternion(ZeroInitT): Dual>{Quaternion{ZeroInit}, Quaternion{ZeroInit}} {} + /** * @brief Construct dual quaternion from real and dual part * diff --git a/src/Magnum/Math/Quaternion.h b/src/Magnum/Math/Quaternion.h index 07d35787c..fca917c68 100644 --- a/src/Magnum/Math/Quaternion.h +++ b/src/Magnum/Math/Quaternion.h @@ -199,6 +199,9 @@ template class Quaternion { */ constexpr /*implicit*/ Quaternion(IdentityInitT = IdentityInit): _vector{T(0)}, _scalar{T(1)} {} + /** @brief Construct zero-initialized quaternion */ + constexpr explicit Quaternion(ZeroInitT): _vector{ZeroInit}, _scalar{T{0}} {} + /** * @brief Construct quaternion from vector and scalar * diff --git a/src/Magnum/Math/Test/ComplexTest.cpp b/src/Magnum/Math/Test/ComplexTest.cpp index 995e7ecbe..3cc7c04e0 100644 --- a/src/Magnum/Math/Test/ComplexTest.cpp +++ b/src/Magnum/Math/Test/ComplexTest.cpp @@ -59,6 +59,7 @@ struct ComplexTest: Corrade::TestSuite::Tester { void construct(); void constructIdentity(); + void constructZero(); void constructFromVector(); void constructCopy(); void convert(); @@ -91,6 +92,7 @@ struct ComplexTest: Corrade::TestSuite::Tester { ComplexTest::ComplexTest() { addTests({&ComplexTest::construct, &ComplexTest::constructIdentity, + &ComplexTest::constructZero, &ComplexTest::constructFromVector, &ComplexTest::constructCopy, &ComplexTest::convert, @@ -146,6 +148,11 @@ void ComplexTest::constructIdentity() { CORRADE_COMPARE(b.length(), 1.0f); } +void ComplexTest::constructZero() { + constexpr Complex a{ZeroInit}; + CORRADE_COMPARE(a, Complex(0.0f, 0.0f)); +} + void ComplexTest::constructFromVector() { constexpr Vector2 vec(1.5f, -3.0f); diff --git a/src/Magnum/Math/Test/DualComplexTest.cpp b/src/Magnum/Math/Test/DualComplexTest.cpp index 8ecb51464..fb4365d71 100644 --- a/src/Magnum/Math/Test/DualComplexTest.cpp +++ b/src/Magnum/Math/Test/DualComplexTest.cpp @@ -59,6 +59,7 @@ struct DualComplexTest: Corrade::TestSuite::Tester { void construct(); void constructIdentity(); + void constructZero(); void constructFromVector(); void constructCopy(); void convert(); @@ -97,6 +98,7 @@ typedef Math::Vector2 Vector2; DualComplexTest::DualComplexTest() { addTests({&DualComplexTest::construct, &DualComplexTest::constructIdentity, + &DualComplexTest::constructZero, &DualComplexTest::constructFromVector, &DualComplexTest::constructCopy, &DualComplexTest::convert, @@ -146,6 +148,11 @@ void DualComplexTest::constructIdentity() { CORRADE_COMPARE(b.length(), 1.0f); } +void DualComplexTest::constructZero() { + constexpr DualComplex a{ZeroInit}; + CORRADE_COMPARE(a, DualComplex({0.0f, 0.0f}, {0.0f, 0.0f})); +} + void DualComplexTest::constructFromVector() { constexpr DualComplex a(Vector2(1.5f, -3.0f)); CORRADE_COMPARE(a, DualComplex({1.0f, 0.0f}, {1.5f, -3.0f})); diff --git a/src/Magnum/Math/Test/DualQuaternionTest.cpp b/src/Magnum/Math/Test/DualQuaternionTest.cpp index 556a82e19..dc20c9bbc 100644 --- a/src/Magnum/Math/Test/DualQuaternionTest.cpp +++ b/src/Magnum/Math/Test/DualQuaternionTest.cpp @@ -61,6 +61,7 @@ struct DualQuaternionTest: Corrade::TestSuite::Tester { void construct(); void constructIdentity(); + void constructZero(); void constructFromVector(); void constructCopy(); void convert(); @@ -98,6 +99,7 @@ typedef Math::Vector3 Vector3; DualQuaternionTest::DualQuaternionTest() { addTests({&DualQuaternionTest::construct, &DualQuaternionTest::constructIdentity, + &DualQuaternionTest::constructZero, &DualQuaternionTest::constructFromVector, &DualQuaternionTest::constructCopy, &DualQuaternionTest::convert, @@ -147,6 +149,11 @@ void DualQuaternionTest::constructIdentity() { CORRADE_COMPARE(b.length(), 1.0f); } +void DualQuaternionTest::constructZero() { + constexpr DualQuaternion a{ZeroInit}; + CORRADE_COMPARE(a, DualQuaternion({{0.0f, 0.0f, 0.0f}, 0.0f}, {{0.0f, 0.0f, 0.0f}, 0.0f})); +} + void DualQuaternionTest::constructFromVector() { constexpr DualQuaternion a(Vector3(1.0f, 2.0f, 3.0f)); CORRADE_COMPARE(a, DualQuaternion({{0.0f, 0.0f, 0.0f}, 1.0f}, {{1.0f, 2.0f, 3.0f}, 0.0f})); diff --git a/src/Magnum/Math/Test/QuaternionTest.cpp b/src/Magnum/Math/Test/QuaternionTest.cpp index e25d4f02c..d947161fb 100644 --- a/src/Magnum/Math/Test/QuaternionTest.cpp +++ b/src/Magnum/Math/Test/QuaternionTest.cpp @@ -59,6 +59,7 @@ struct QuaternionTest: Corrade::TestSuite::Tester { void construct(); void constructIdentity(); + void constructZero(); void constructFromVector(); void constructCopy(); void convert(); @@ -102,6 +103,7 @@ typedef Math::Vector4 Vector4; QuaternionTest::QuaternionTest() { addTests({&QuaternionTest::construct, &QuaternionTest::constructIdentity, + &QuaternionTest::constructZero, &QuaternionTest::constructFromVector, &QuaternionTest::constructCopy, &QuaternionTest::convert, @@ -153,6 +155,11 @@ void QuaternionTest::constructIdentity() { CORRADE_COMPARE(b.length(), 1.0f); } +void QuaternionTest::constructZero() { + constexpr Quaternion a{ZeroInit}; + CORRADE_COMPARE(a, Quaternion({0.0f, 0.0f, 0.0f}, 0.0f)); +} + void QuaternionTest::constructFromVector() { constexpr Quaternion a(Vector3(1.0f, 2.0f, 3.0f)); CORRADE_COMPARE(a, Quaternion({1.0f, 2.0f, 3.0f}, 0.0f)); diff --git a/src/Magnum/Test/ColorTest.cpp b/src/Magnum/Test/ColorTest.cpp index c9e30f681..a8632825b 100644 --- a/src/Magnum/Test/ColorTest.cpp +++ b/src/Magnum/Test/ColorTest.cpp @@ -36,6 +36,7 @@ struct ColorTest: TestSuite::Tester { void construct(); void constructDefault(); + void constructZero(); void constructOneValue(); void constructParts(); void constructConversion(); @@ -64,6 +65,7 @@ struct ColorTest: TestSuite::Tester { ColorTest::ColorTest() { addTests({&ColorTest::construct, &ColorTest::constructDefault, + &ColorTest::constructZero, &ColorTest::constructOneValue, &ColorTest::constructParts, &ColorTest::constructConversion, @@ -115,6 +117,13 @@ void ColorTest::constructDefault() { CORRADE_COMPARE(c, Color4ub(0, 0, 0, 255)); } +void ColorTest::constructZero() { + constexpr Color3 a{Math::ZeroInit}; + constexpr Color4 b{Math::ZeroInit}; + CORRADE_COMPARE(a, Color3(0.0f, 0.0f, 0.0f)); + CORRADE_COMPARE(b, Color4(0.0f, 0.0f, 0.0f, 0.0f)); +} + void ColorTest::constructOneValue() { constexpr Color3 a(0.25f); CORRADE_COMPARE(a, Color3(0.25f, 0.25f, 0.25f));