/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include "Magnum/Math/DualComplex.h" #include "Magnum/Math/DualQuaternion.h" struct DualCmpl { float re, im, x, y; }; namespace Magnum { namespace Math { namespace Implementation { template<> struct DualComplexConverter { constexpr static DualComplex from(const DualCmpl& other) { return {{other.re, other.im}, {other.x, other.y}}; } constexpr static DualCmpl to(const DualComplex& other) { return {other.real().real(), other.real().imaginary(), other.dual().real(), other.dual().imaginary() }; } }; } namespace Test { struct DualComplexTest: Corrade::TestSuite::Tester { explicit DualComplexTest(); void construct(); void constructIdentity(); void constructZero(); void constructNoInit(); void constructFromVector(); void constructConversion(); void constructCopy(); void convert(); void isNormalized(); template void isNormalizedEpsilonRotation(); template void isNormalizedEpsilonTranslation(); void multiply(); void lengthSquared(); void length(); void normalized(); template void normalizedIterative(); void complexConjugated(); void dualConjugated(); void conjugated(); void inverted(); void invertedNormalized(); void rotation(); void translation(); void combinedTransformParts(); void matrix(); void transformPoint(); void debug(); void configuration(); }; typedef Math::Deg Deg; typedef Math::Rad Rad; typedef Math::Complex Complex; typedef Math::Dual Dual; typedef Math::DualComplex DualComplex; typedef Math::Matrix3 Matrix3; typedef Math::Vector2 Vector2; DualComplexTest::DualComplexTest() { addTests({&DualComplexTest::construct, &DualComplexTest::constructIdentity, &DualComplexTest::constructZero, &DualComplexTest::constructNoInit, &DualComplexTest::constructFromVector, &DualComplexTest::constructConversion, &DualComplexTest::constructCopy, &DualComplexTest::convert, &DualComplexTest::isNormalized, &DualComplexTest::isNormalizedEpsilonRotation, &DualComplexTest::isNormalizedEpsilonRotation, &DualComplexTest::isNormalizedEpsilonTranslation, &DualComplexTest::isNormalizedEpsilonTranslation, &DualComplexTest::multiply, &DualComplexTest::lengthSquared, &DualComplexTest::length, &DualComplexTest::normalized}); addRepeatedTests({ &DualComplexTest::normalizedIterative, &DualComplexTest::normalizedIterative}, 1000); addTests({&DualComplexTest::complexConjugated, &DualComplexTest::dualConjugated, &DualComplexTest::conjugated, &DualComplexTest::inverted, &DualComplexTest::invertedNormalized, &DualComplexTest::rotation, &DualComplexTest::translation, &DualComplexTest::combinedTransformParts, &DualComplexTest::matrix, &DualComplexTest::transformPoint, &DualComplexTest::debug, &DualComplexTest::configuration}); } void DualComplexTest::construct() { constexpr DualComplex a = {{-1.0f, 2.5f}, {3.0f, -7.5f}}; CORRADE_COMPARE(a, DualComplex({-1.0f, 2.5f}, {3.0f, -7.5f})); constexpr Complex b = a.real(); constexpr Complex c = a.dual(); CORRADE_COMPARE(b, Complex(-1.0f, 2.5f)); CORRADE_COMPARE(c, Complex(3.0f, -7.5f)); constexpr DualComplex d(Complex(-1.0f, 2.5f)); CORRADE_COMPARE(d, DualComplex({-1.0f, 2.5f}, {0.0f, 0.0f})); CORRADE_VERIFY((std::is_nothrow_constructible::value)); } void DualComplexTest::constructIdentity() { constexpr DualComplex a; constexpr DualComplex b{IdentityInit}; CORRADE_COMPARE(a, DualComplex({1.0f, 0.0f}, {0.0f, 0.0f})); CORRADE_COMPARE(b, DualComplex({1.0f, 0.0f}, {0.0f, 0.0f})); CORRADE_COMPARE(a.length(), 1.0f); CORRADE_COMPARE(b.length(), 1.0f); CORRADE_VERIFY(std::is_nothrow_default_constructible::value); CORRADE_VERIFY((std::is_nothrow_constructible::value)); } void DualComplexTest::constructZero() { constexpr DualComplex a{ZeroInit}; CORRADE_COMPARE(a, DualComplex({0.0f, 0.0f}, {0.0f, 0.0f})); CORRADE_VERIFY((std::is_nothrow_constructible::value)); } void DualComplexTest::constructNoInit() { DualComplex a{{-1.0f, 2.5f}, {3.0f, -7.5f}}; new(&a) DualComplex{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, DualComplex({-1.0f, 2.5f}, {3.0f, -7.5f})); } CORRADE_VERIFY((std::is_nothrow_constructible::value)); /* Implicit construction is not allowed */ CORRADE_VERIFY(!(std::is_convertible::value)); } void DualComplexTest::constructFromVector() { constexpr DualComplex a(Vector2(1.5f, -3.0f)); CORRADE_COMPARE(a, DualComplex({1.0f, 0.0f}, {1.5f, -3.0f})); /* Implicit conversion is not allowed */ CORRADE_VERIFY(!(std::is_convertible::value)); CORRADE_VERIFY((std::is_nothrow_constructible::value)); } void DualComplexTest::constructConversion() { typedef Math::DualComplex DualComplexi; constexpr DualComplex a{{1.3f, 2.7f}, {-15.0f, 7.0f}}; constexpr DualComplexi b{a}; CORRADE_COMPARE(b, (DualComplexi{{1, 2}, {-15, 7}})); /* Implicit conversion is not allowed */ CORRADE_VERIFY(!(std::is_convertible::value)); CORRADE_VERIFY((std::is_nothrow_constructible::value)); } void DualComplexTest::constructCopy() { constexpr Math::Dual a({-1.0f, 2.5f}, {3.0f, -7.5f}); #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Why can't be copy constexpr? */ constexpr #endif DualComplex b(a); CORRADE_COMPARE(b, DualComplex({-1.0f, 2.5f}, {3.0f, -7.5f})); CORRADE_VERIFY(std::is_nothrow_copy_constructible::value); CORRADE_VERIFY(std::is_nothrow_copy_assignable::value); } void DualComplexTest::convert() { constexpr DualCmpl a{1.5f, -3.5f, 7.0f, -0.5f}; constexpr DualComplex b{{1.5f, -3.5f}, {7.0f, -0.5f}}; /* GCC 5.1 had a bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66450 Hopefully this does not reappear. */ constexpr DualComplex c{a}; CORRADE_COMPARE(c, b); constexpr DualCmpl d(b); CORRADE_COMPARE(d.re, a.re); CORRADE_COMPARE(d.im, a.im); CORRADE_COMPARE(d.x, a.x); CORRADE_COMPARE(d.y, a.y); /* Implicit conversion is not allowed */ CORRADE_VERIFY(!(std::is_convertible::value)); CORRADE_VERIFY(!(std::is_convertible::value)); } void DualComplexTest::isNormalized() { CORRADE_VERIFY(!DualComplex({2.0f, 1.0f}, {}).isNormalized()); CORRADE_VERIFY((DualComplex::rotation(Deg(23.0f))*DualComplex::translation({6.0f, 3.0f})).isNormalized()); } template void DualComplexTest::isNormalizedEpsilonRotation() { setTestCaseName(std::string{"isNormalizedEpsilonRotation<"} + TypeTraits::name() + ">"); CORRADE_VERIFY((Math::DualComplex{{T(0.801775644243754) + TypeTraits::epsilon()/T(2.0), T(0.597625146975521)}, {T(8018055.25501103), T(5975850.58193309)}}.isNormalized())); CORRADE_VERIFY(!(Math::DualComplex{{T(0.801775644243754) + TypeTraits::epsilon()*T(2.0), T(0.597625146975521)}, {T(8018055.25501103), T(5975850.58193309)}}.isNormalized())); } template void DualComplexTest::isNormalizedEpsilonTranslation() { setTestCaseName(std::string{"isNormalizedEpsilonTranslation<"} + TypeTraits::name() + ">"); /* Translation does not affect normalization */ CORRADE_VERIFY((Math::DualComplex{{T(0.801775644243754), T(0.597625146975521)}, {T(8018055.25501103), T(20.5)}}.isNormalized())); CORRADE_VERIFY((Math::DualComplex{{T(0.801775644243754), T(0.597625146975521)}, {T(8018055.25501103), T(-200000000.0)}}.isNormalized())); } void DualComplexTest::multiply() { DualComplex a({-1.5f, 2.0f}, { 3.0f, -6.5f}); DualComplex b({ 2.0f, -7.5f}, {-0.5f, 1.0f});; CORRADE_COMPARE(a*b, DualComplex({12.0f, 15.25f}, {1.75f, -9.0f})); } void DualComplexTest::lengthSquared() { DualComplex a({-1.0f, 3.0f}, {0.5f, -2.0f}); CORRADE_COMPARE(a.lengthSquared(), 10.0f); } void DualComplexTest::length() { DualComplex a({-1.0f, 3.0f}, {0.5f, -2.0f}); CORRADE_COMPARE(a.length(), 3.162278f); } void DualComplexTest::normalized() { DualComplex a({-1.0f, 3.0f}, {0.5f, -2.0f}); DualComplex b({-0.316228f, 0.948683f}, {0.5f, -2.0f}); CORRADE_COMPARE(a.normalized().length(), 1.0f); CORRADE_COMPARE(a.normalized(), b); } namespace { template struct NormalizedIterativeData; template<> struct NormalizedIterativeData { static Math::Vector2 translation() { return {10000.0f, -50.0f}; } }; template<> struct NormalizedIterativeData { static Math::Vector2 translation() { return {10000000.0, -500.0}; } }; } template void DualComplexTest::normalizedIterative() { setTestCaseName(std::string{"normalizedIterative<"} + TypeTraits::name() + ">"); auto a = Math::DualComplex::rotation(Math::Deg{T(36.7)})*Math::DualComplex::translation(NormalizedIterativeData::translation()); for(std::size_t i = 0; i != testCaseRepeatId(); ++i) { a = Math::DualComplex::rotation(Math::Deg{T(87.1)})*a; a = a.normalized(); } CORRADE_VERIFY(a.isNormalized()); } void DualComplexTest::complexConjugated() { DualComplex a({-1.0f, 2.5f}, {3.0f, -7.5f}); DualComplex b({-1.0f, -2.5f}, {3.0f, 7.5f}); CORRADE_COMPARE(a.complexConjugated(), b); } void DualComplexTest::dualConjugated() { DualComplex a({-1.0f, 2.5f}, { 3.0f, -7.5f}); DualComplex b({-1.0f, 2.5f}, {-3.0f, 7.5f}); CORRADE_COMPARE(a.dualConjugated(), b); } void DualComplexTest::conjugated() { DualComplex a({-1.0f, 2.5f}, { 3.0f, -7.5f}); DualComplex b({-1.0f, -2.5f}, {-3.0f, -7.5f}); CORRADE_COMPARE(a.conjugated(), b); } void DualComplexTest::inverted() { DualComplex a({-1.0f, 1.5f}, {3.0f, -7.5f}); DualComplex b({-0.307692f, -0.461538f}, {4.384616f, -0.923077f}); CORRADE_COMPARE(a*a.inverted(), DualComplex()); CORRADE_COMPARE(a.inverted(), b); } void DualComplexTest::invertedNormalized() { DualComplex a({-0.316228f, 0.9486831f}, { 3.0f, -2.5f}); DualComplex b({-0.316228f, -0.9486831f}, {3.320391f, 2.05548f}); std::ostringstream o; Error redirectError{&o}; DualComplex({-1.0f, -2.5f}, {}).invertedNormalized(); CORRADE_COMPARE(o.str(), "Math::Complex::invertedNormalized(): complex number must be normalized\n"); DualComplex inverted = a.invertedNormalized(); CORRADE_COMPARE(a*inverted, DualComplex()); CORRADE_COMPARE(inverted*a, DualComplex()); CORRADE_COMPARE(inverted, b); } void DualComplexTest::rotation() { DualComplex a = DualComplex::rotation(Deg(120.0f)); CORRADE_COMPARE(a.length(), 1.0f); CORRADE_COMPARE(a, DualComplex({-0.5f, 0.8660254f}, {0.0f, 0.0f})); CORRADE_COMPARE_AS(a.rotation().angle(), Deg(120.0f), Rad); /* Constexpr access to rotation */ constexpr DualComplex b({-1.0f, 2.0f}, {}); constexpr Complex c = b.rotation(); CORRADE_COMPARE(c, Complex(-1.0f, 2.0f)); } void DualComplexTest::translation() { Vector2 vec(1.5f, -3.5f); DualComplex a = DualComplex::translation(vec); CORRADE_COMPARE(a.length(), 1.0f); CORRADE_COMPARE(a, DualComplex({}, {1.5f, -3.5f})); CORRADE_COMPARE(a.translation(), vec); } void DualComplexTest::combinedTransformParts() { Vector2 translation = Vector2(-1.5f, 2.75f); DualComplex a = DualComplex::translation(translation)*DualComplex::rotation(Deg(23.0f)); DualComplex b = DualComplex::rotation(Deg(23.0f))*DualComplex::translation(translation); CORRADE_COMPARE_AS(a.rotation().angle(), Deg(23.0f), Rad); CORRADE_COMPARE_AS(b.rotation().angle(), Deg(23.0f), Rad); CORRADE_COMPARE(a.translation(), translation); CORRADE_COMPARE(b.translation(), Complex::rotation(Deg(23.0f)).transformVector(translation)); } void DualComplexTest::matrix() { DualComplex a = DualComplex::rotation(Deg(23.0f))*DualComplex::translation({2.0f, 3.0f}); Matrix3 m = Matrix3::rotation(Deg(23.0f))*Matrix3::translation({2.0f, 3.0f}); CORRADE_COMPARE(a.toMatrix(), m); std::ostringstream o; Error redirectError{&o}; DualComplex::fromMatrix(m*2); CORRADE_COMPARE(o.str(), "Math::DualComplex::fromMatrix(): the matrix doesn't represent rigid transformation\n"); DualComplex b = DualComplex::fromMatrix(m); CORRADE_COMPARE(b, a); } void DualComplexTest::transformPoint() { DualComplex a = DualComplex::translation({2.0f, 3.0f})*DualComplex::rotation(Deg(23.0f)); DualComplex b = DualComplex::rotation(Deg(23.0f))*DualComplex::translation({2.0f, 3.0f}); Matrix3 m = Matrix3::translation({2.0f, 3.0f})*Matrix3::rotation(Deg(23.0f)); Matrix3 n = Matrix3::rotation(Deg(23.0f))*Matrix3::translation({2.0f, 3.0f}); Vector2 v(-3.6f, 0.7f); Vector2 transformedA = a.transformPoint(v); CORRADE_COMPARE(transformedA, m.transformPoint(v)); CORRADE_COMPARE(transformedA, Vector2(-1.58733f, 2.237721f)); Vector2 transformedB = b.transformPoint(v); CORRADE_COMPARE(transformedB, n.transformPoint(v)); CORRADE_COMPARE(transformedB, Vector2(-2.918512f, 2.780698f)); } void DualComplexTest::debug() { std::ostringstream o; Debug(&o) << DualComplex({-1.0f, -2.5f}, {-3.0f, -7.5f}); CORRADE_COMPARE(o.str(), "DualComplex({-1, -2.5}, {-3, -7.5})\n"); } void DualComplexTest::configuration() { Corrade::Utility::Configuration c; DualComplex a{{3.0f, 3.125f}, {9.0f, 9.55f}}; std::string value("3 3.125 9 9.55"); c.setValue("dualcomplex", a); CORRADE_COMPARE(c.value("dualcomplex"), value); CORRADE_COMPARE(c.value("dualcomplex"), a); /* Underflow */ c.setValue("underflow", "2.1 8.9"); CORRADE_COMPARE(c.value("underflow"), (DualComplex{{2.1f, 8.9f}, {0.0f, 0.0f}})); /* Overflow */ c.setValue("overflow", "2 1 8 9 16 33"); CORRADE_COMPARE(c.value("overflow"), (DualComplex{{2.0f, 1.0f}, {8.0f, 9.0f}})); } }}} CORRADE_TEST_MAIN(Magnum::Math::Test::DualComplexTest)