From 3e2464cdede343a095624a15eba8e792b13cff3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 30 Nov 2016 14:08:08 +0100 Subject: [PATCH] Math: default/identity and NoInit constructors for Frustum, tests. --- src/Magnum/Math/Frustum.h | 21 +++++++- src/Magnum/Math/Test/FrustumTest.cpp | 71 ++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/Magnum/Math/Frustum.h b/src/Magnum/Math/Frustum.h index aaa74b078..f5155c8ae 100644 --- a/src/Magnum/Math/Frustum.h +++ b/src/Magnum/Math/Frustum.h @@ -58,7 +58,18 @@ template class Frustum { m.row(3) - m.row(2)}; } - /** @brief Constructor */ + /** + * @brief Identity constructor + * + * Equivalent to creating a frustum from an identity matrix. + * @see @ref fromMatrix() + */ + constexpr /*implicit*/ Frustum(IdentityInitT = IdentityInit) noexcept; + + /** @brief Construct a frustum without initializing the contents */ + explicit Frustum(NoInitT) noexcept: _data{Vector4{NoInit}, Vector4{NoInit}, Vector4{NoInit}, Vector4{NoInit}, Vector4{NoInit}, Vector4{NoInit}} {} + + /** @brief Construct a frustum from plane equations */ constexpr /*implicit*/ Frustum(const Vector4& left, const Vector4& right, const Vector4& bottom, const Vector4& top, const Vector4& near, const Vector4& far) noexcept: _data{left, right, bottom, top, near, far} {} /** @brief Equality comparison */ @@ -113,6 +124,14 @@ extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utili extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug&, const Frustum&); #endif +template constexpr Frustum::Frustum(IdentityInitT) noexcept: _data{ + { 1.0f, 0.0f, 0.0f, 1.0f}, + {-1.0f, 0.0f, 0.0f, 1.0f}, + { 0.0f, 1.0f, 0.0f, 1.0f}, + { 0.0f, -1.0f, 0.0f, 1.0f}, + { 0.0f, 0.0f, 1.0f, 1.0f}, + { 0.0f, 0.0f, -1.0f, 1.0f}} {} + }} #endif diff --git a/src/Magnum/Math/Test/FrustumTest.cpp b/src/Magnum/Math/Test/FrustumTest.cpp index 7f3b1d07d..1d2048915 100644 --- a/src/Magnum/Math/Test/FrustumTest.cpp +++ b/src/Magnum/Math/Test/FrustumTest.cpp @@ -36,6 +36,9 @@ struct FrustumTest: Corrade::TestSuite::Tester { explicit FrustumTest(); void construct(); + void constructIdentity(); + void constructNoInit(); + void constructCopy(); void constructFromMatrix(); void compare(); @@ -49,6 +52,9 @@ typedef Math::Frustum Frustum; FrustumTest::FrustumTest() { addTests({&FrustumTest::construct, + &FrustumTest::constructIdentity, + &FrustumTest::constructNoInit, + &FrustumTest::constructCopy, &FrustumTest::constructFromMatrix, &FrustumTest::compare, @@ -76,6 +82,67 @@ void FrustumTest::construct() { CORRADE_VERIFY((std::is_nothrow_constructible::value)); } +void FrustumTest::constructIdentity() { + Frustum expected{ + { 1.0f, 0.0f, 0.0f, 1.0f}, + {-1.0f, 0.0f, 0.0f, 1.0f}, + { 0.0f, 1.0f, 0.0f, 1.0f}, + { 0.0f, -1.0f, 0.0f, 1.0f}, + { 0.0f, 0.0f, 1.0f, 1.0f}, + { 0.0f, 0.0f, -1.0f, 1.0f}}; + + constexpr Frustum a; + constexpr Frustum b{IdentityInit}; + CORRADE_COMPARE(a, expected); + CORRADE_COMPARE(b, expected); + + CORRADE_VERIFY(std::is_nothrow_default_constructible::value); + CORRADE_VERIFY((std::is_nothrow_constructible::value)); +} + +void FrustumTest::constructNoInit() { + Frustum a{ + {-1.0f, 2.0f, -3.0f, 0.1f}, + { 1.0f, -2.0f, 3.0f, 0.2f}, + {-4.0f, 5.0f, -6.0f, 0.3f}, + { 4.0f, -5.0f, 6.0f, 0.4f}, + {-7.0f, 8.0f, -9.0f, 0.5f}, + { 7.0f, 8.0f, 9.0f, 0.6f}}; + + new(&a) Frustum{NoInit}; + { + #if defined(__GNUC__) && __GNUC__*100 + __GNUC_MINOR__ >= 601 && __OPTIMIZE__ + CORRADE_EXPECT_FAIL("GCC 6.1+ misoptimizes and overwrites the value."); + #endif + + CORRADE_COMPARE(a, (Frustum{ + {-1.0f, 2.0f, -3.0f, 0.1f}, + { 1.0f, -2.0f, 3.0f, 0.2f}, + {-4.0f, 5.0f, -6.0f, 0.3f}, + { 4.0f, -5.0f, 6.0f, 0.4f}, + {-7.0f, 8.0f, -9.0f, 0.5f}, + { 7.0f, 8.0f, 9.0f, 0.6f}})); + } + + /* Implicit construction is not allowed */ + CORRADE_VERIFY((std::is_nothrow_constructible::value)); +} + +void FrustumTest::constructCopy() { + constexpr Frustum a{ + {-1.0f, 2.0f, -3.0f, 0.1f}, + { 1.0f, -2.0f, 3.0f, 0.2f}, + {-4.0f, 5.0f, -6.0f, 0.3f}, + { 4.0f, -5.0f, 6.0f, 0.4f}, + {-7.0f, 8.0f, -9.0f, 0.5f}, + { 7.0f, 8.0f, 9.0f, 0.6f}}; + constexpr Frustum b{a}; + CORRADE_COMPARE(b, a); + + CORRADE_VERIFY(std::is_nothrow_copy_constructible::value); + CORRADE_VERIFY(std::is_nothrow_copy_assignable::value); +} + void FrustumTest::constructFromMatrix() { using namespace Magnum::Math::Literals; @@ -91,6 +158,10 @@ void FrustumTest::constructFromMatrix() { Matrix4::perspectiveProjection(90.0_degf, 1.0f, 1.0f, 10.0f)); CORRADE_COMPARE(frustum, expected); + + /* Constructing from a default-constructed matrix should be equivalent to + default constructor */ + CORRADE_COMPARE(Frustum::fromMatrix({}), Frustum{}); } void FrustumTest::compare() {