From 8c7c74c22e50e0bacb22ad38204675173eb6f149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 31 Aug 2018 20:53:53 +0200 Subject: [PATCH] Math: mutable getters for Complex, Dual and Quaternion. Not sure why these were omitted. Everything else has them. --- doc/changelog.dox | 4 ++++ src/Magnum/Math/Complex.h | 6 ++++-- src/Magnum/Math/Dual.h | 8 ++++++-- src/Magnum/Math/Quaternion.h | 8 ++++++-- src/Magnum/Math/Test/ComplexTest.cpp | 24 +++++++++++++++++++----- src/Magnum/Math/Test/DualTest.cpp | 23 +++++++++++++++++++---- src/Magnum/Math/Test/QuaternionTest.cpp | 24 +++++++++++++++++++----- 7 files changed, 77 insertions(+), 20 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index dd81f0fca..f2ffda900 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -88,6 +88,10 @@ See also: @ref Math::Distance::pointPlane() and others - Ability to convert @ref Math::BoolVector from and to external representation +- Mutable overloads for @ref Math::Complex::real(), + @ref Math::Complex::imaginary(), @ref Math::Dual::real(), + @ref Math::Dual::dual(), @ref Math::Quaternion::vector() and + @ref Math::Quaternion::scalar() - Ability to use @ref Math::Complex, @ref Math::DualComplex, @ref Math::Quaternion, @ref Math::DualQuaternion with @ref Corrade::Utility::Configuration and @ref Corrade::Utility::Arguments diff --git a/src/Magnum/Math/Complex.h b/src/Magnum/Math/Complex.h index 465d70b7b..9299826df 100644 --- a/src/Magnum/Math/Complex.h +++ b/src/Magnum/Math/Complex.h @@ -192,10 +192,12 @@ template class Complex { } /** @brief Real part */ - constexpr T real() const { return _real; } + T& real() { return _real; } + constexpr T real() const { return _real; } /**< @overload */ /** @brief Imaginary part */ - constexpr T imaginary() const { return _imaginary; } + T& imaginary() { return _imaginary; } + constexpr T imaginary() const { return _imaginary; } /**< @overload */ /** * @brief Convert complex number to vector diff --git a/src/Magnum/Math/Dual.h b/src/Magnum/Math/Dual.h index 5cf8c04ee..151db9099 100644 --- a/src/Magnum/Math/Dual.h +++ b/src/Magnum/Math/Dual.h @@ -123,11 +123,15 @@ template class Dual { /** @brief Real part */ T& real() { return _real; } - constexpr T real() const { return _real; } /**< @overload */ + /* Returning const so it's possible to call constexpr functions on the + result. WTF, C++?! */ + constexpr const T real() const { return _real; } /**< @overload */ /** @brief Dual part */ T& dual() { return _dual; } - constexpr T dual() const { return _dual; } /**< @overload */ + /* Returning const so it's possible to call constexpr functions on the + result. WTF, C++?! */ + constexpr const T dual() const { return _dual; } /**< @overload */ /** * @brief Add and assign dual number diff --git a/src/Magnum/Math/Quaternion.h b/src/Magnum/Math/Quaternion.h index 7286c33ac..2256104a4 100644 --- a/src/Magnum/Math/Quaternion.h +++ b/src/Magnum/Math/Quaternion.h @@ -239,10 +239,14 @@ template class Quaternion { } /** @brief Vector part */ - constexpr const Vector3 vector() const { return _vector; } + Vector3& vector() { return _vector; } + /* Returning const so it's possible to call constexpr functions on the + result. WTF, C++?! */ + constexpr const Vector3 vector() const { return _vector; } /**< @overload */ /** @brief Scalar part */ - constexpr T scalar() const { return _scalar; } + T& scalar() { return _scalar; } + constexpr T scalar() const { return _scalar; } /**< @overload */ /** * @brief Rotation angle of unit quaternion diff --git a/src/Magnum/Math/Test/ComplexTest.cpp b/src/Magnum/Math/Test/ComplexTest.cpp index c434f57ec..fd9c100a0 100644 --- a/src/Magnum/Math/Test/ComplexTest.cpp +++ b/src/Magnum/Math/Test/ComplexTest.cpp @@ -64,6 +64,8 @@ struct ComplexTest: Corrade::TestSuite::Tester { void constructCopy(); void convert(); + void data(); + void compare(); void isNormalized(); template void isNormalizedEpsilon(); @@ -106,6 +108,8 @@ ComplexTest::ComplexTest() { &ComplexTest::constructCopy, &ComplexTest::convert, + &ComplexTest::data, + &ComplexTest::compare, &ComplexTest::isNormalized, &ComplexTest::isNormalizedEpsilon, @@ -154,11 +158,8 @@ using namespace Math::Literals; void ComplexTest::construct() { constexpr Complex a = {0.5f, -3.7f}; CORRADE_COMPARE(a, Complex(0.5f, -3.7f)); - - constexpr Float b = a.real(); - constexpr Float c = a.imaginary(); - CORRADE_COMPARE(b, 0.5f); - CORRADE_COMPARE(c, -3.7f); + CORRADE_COMPARE(a.real(), 0.5f); + CORRADE_COMPARE(a.imaginary(), -3.7f); CORRADE_VERIFY((std::is_nothrow_constructible::value)); } @@ -255,6 +256,19 @@ void ComplexTest::convert() { CORRADE_VERIFY(!(std::is_convertible::value)); } +void ComplexTest::data() { + constexpr Complex ca{1.5f, -3.5f}; + constexpr Float real = ca.real(); + constexpr Float imaginary = ca.imaginary(); + CORRADE_COMPARE(real, 1.5f); + CORRADE_COMPARE(imaginary, -3.5f); + + Complex a{1.5f, -3.5f}; + a.real() = 2.0f; + a.imaginary() = -3.5f; + CORRADE_COMPARE(a, (Complex{2.0f, -3.5f})); +} + void ComplexTest::compare() { CORRADE_VERIFY(Complex(3.7f, -1.0f+TypeTraits::epsilon()/2) == Complex(3.7f, -1.0f)); CORRADE_VERIFY(Complex(3.7f, -1.0f+TypeTraits::epsilon()*2) != Complex(3.7f, -1.0f)); diff --git a/src/Magnum/Math/Test/DualTest.cpp b/src/Magnum/Math/Test/DualTest.cpp index e44362de4..f53a42981 100644 --- a/src/Magnum/Math/Test/DualTest.cpp +++ b/src/Magnum/Math/Test/DualTest.cpp @@ -42,6 +42,8 @@ struct DualTest: Corrade::TestSuite::Tester { void constructConversion(); void constructCopy(); + void data(); + void compare(); void addSubtract(); @@ -79,6 +81,8 @@ DualTest::DualTest() { &DualTest::constructConversion, &DualTest::constructCopy, + &DualTest::data, + &DualTest::compare, &DualTest::addSubtract, @@ -101,10 +105,8 @@ DualTest::DualTest() { void DualTest::construct() { constexpr Dual a = {2.0f, -7.5f}; - constexpr Float b = a.real(); - constexpr Float c = a.dual(); - CORRADE_COMPARE(b, 2.0f); - CORRADE_COMPARE(c, -7.5f); + CORRADE_COMPARE(a.real(), 2.0f); + CORRADE_COMPARE(a.dual(), -7.5f); constexpr Dual d(3.0f); CORRADE_COMPARE(d.real(), 3.0f); @@ -176,6 +178,19 @@ void DualTest::constructCopy() { CORRADE_VERIFY(std::is_nothrow_copy_assignable::value); } +void DualTest::data() { + constexpr Dual ca{1.5f, -3.5f}; + constexpr Float real = ca.real(); + constexpr Float dual = ca.dual(); + CORRADE_COMPARE(real, 1.5f); + CORRADE_COMPARE(dual, -3.5f); + + Dual a{1.5f, -3.5f}; + a.real() = 2.0f; + a.dual() = -3.5f; + CORRADE_COMPARE(a, (Dual{2.0f, -3.5f})); +} + void DualTest::compare() { CORRADE_VERIFY(Dual(1.0f, 1.0f+TypeTraits::epsilon()/2) == Dual(1.0f, 1.0f)); CORRADE_VERIFY(Dual(1.0f, 1.0f+TypeTraits::epsilon()*2) != Dual(1.0f, 1.0f)); diff --git a/src/Magnum/Math/Test/QuaternionTest.cpp b/src/Magnum/Math/Test/QuaternionTest.cpp index 662e0a7d4..202697898 100644 --- a/src/Magnum/Math/Test/QuaternionTest.cpp +++ b/src/Magnum/Math/Test/QuaternionTest.cpp @@ -65,6 +65,8 @@ struct QuaternionTest: Corrade::TestSuite::Tester { void constructCopy(); void convert(); + void data(); + void compare(); void isNormalized(); template void isNormalizedEpsilon(); @@ -120,6 +122,8 @@ QuaternionTest::QuaternionTest() { &QuaternionTest::constructCopy, &QuaternionTest::convert, + &QuaternionTest::data, + &QuaternionTest::compare, &QuaternionTest::isNormalized, &QuaternionTest::isNormalizedEpsilon, @@ -162,11 +166,8 @@ QuaternionTest::QuaternionTest() { void QuaternionTest::construct() { constexpr Quaternion a = {{1.0f, 2.0f, 3.0f}, -4.0f}; CORRADE_COMPARE(a, Quaternion({1.0f, 2.0f, 3.0f}, -4.0f)); - - constexpr Vector3 b = a.vector(); - constexpr Float c = a.scalar(); - CORRADE_COMPARE(b, Vector3(1.0f, 2.0f, 3.0f)); - CORRADE_COMPARE(c, -4.0f); + CORRADE_COMPARE(a.vector(), Vector3(1.0f, 2.0f, 3.0f)); + CORRADE_COMPARE(a.scalar(), -4.0f); CORRADE_VERIFY((std::is_nothrow_constructible::value)); } @@ -259,6 +260,19 @@ void QuaternionTest::convert() { CORRADE_VERIFY(!(std::is_convertible::value)); } +void QuaternionTest::data() { + constexpr Quaternion ca{{1.0f, 2.0f, 3.0f}, -4.0f}; + constexpr Vector3 vector = ca.vector(); + constexpr Float scalar = ca.scalar(); + CORRADE_COMPARE(vector, (Vector3{1.0f, 2.0f, 3.0f})); + CORRADE_COMPARE(scalar, -4.0f); + + Quaternion a{{1.0f, 2.0f, 3.0f}, -4.0f}; + a.vector().y() = 4.3f; + a.scalar() = 1.1f; + CORRADE_COMPARE(a, (Quaternion{{1.0f, 4.3f, 3.0f}, 1.1f})); +} + void QuaternionTest::compare() { CORRADE_VERIFY(Quaternion({1.0f+TypeTraits::epsilon()/2, 2.0f, 3.0f}, -4.0f) == Quaternion({1.0f, 2.0f, 3.0f}, -4.0f)); CORRADE_VERIFY(Quaternion({1.0f+TypeTraits::epsilon()*2, 2.0f, 3.0f}, -4.0f) != Quaternion({1.0f, 2.0f, 3.0f}, -4.0f));