/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 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 "Magnum/SceneGraph/DualComplexTransformation.h" #include "Magnum/SceneGraph/Object.hpp" #include "Magnum/SceneGraph/Scene.h" namespace Magnum { namespace SceneGraph { namespace Test { namespace { struct DualComplexTransformationTest: TestSuite::Tester { explicit DualComplexTransformationTest(); template void fromMatrix(); template void toMatrix(); template void compose(); template void inverted(); template void setTransformation(); template void setTransformationInvalid(); template void resetTransformation(); template void transform(); template void transformInvalid(); template void translate(); template void rotate(); template void normalizeRotation(); }; DualComplexTransformationTest::DualComplexTransformationTest() { addTests({ &DualComplexTransformationTest::fromMatrix, &DualComplexTransformationTest::fromMatrix, &DualComplexTransformationTest::toMatrix, &DualComplexTransformationTest::toMatrix, &DualComplexTransformationTest::compose, &DualComplexTransformationTest::compose, &DualComplexTransformationTest::inverted, &DualComplexTransformationTest::inverted, &DualComplexTransformationTest::setTransformation, &DualComplexTransformationTest::setTransformation, &DualComplexTransformationTest::setTransformationInvalid, &DualComplexTransformationTest::setTransformationInvalid, &DualComplexTransformationTest::resetTransformation, &DualComplexTransformationTest::resetTransformation, &DualComplexTransformationTest::transform, &DualComplexTransformationTest::transform, &DualComplexTransformationTest::transformInvalid, &DualComplexTransformationTest::transformInvalid, &DualComplexTransformationTest::translate, &DualComplexTransformationTest::translate, &DualComplexTransformationTest::rotate, &DualComplexTransformationTest::rotate, &DualComplexTransformationTest::normalizeRotation, &DualComplexTransformationTest::normalizeRotation}); } using namespace Math::Literals; template using Object2D = Object>; template using Scene2D = Scene>; template void DualComplexTransformationTest::fromMatrix() { setTestCaseTemplateName(Math::TypeTraits::name()); Math::Matrix3 m = Math::Matrix3::rotation(Math::Deg{T(17.0)})*Math::Matrix3::translation({T(1.0), T(-0.3)}); Math::DualComplex c = Math::DualComplex::rotation(Math::Deg{T(17.0)})*Math::DualComplex::translation({1.0, T(-0.3)}); CORRADE_COMPARE(Implementation::Transformation>::fromMatrix(m), c); } template void DualComplexTransformationTest::toMatrix() { setTestCaseTemplateName(Math::TypeTraits::name()); Math::DualComplex c = Math::DualComplex::rotation(Math::Deg{T(17.0)})*Math::DualComplex::translation({T(1.0), T(-0.3)}); Math::Matrix3 m = Math::Matrix3::rotation(Math::Deg{T(17.0)})*Math::Matrix3::translation({T(1.0), T(-0.3)}); CORRADE_COMPARE(Implementation::Transformation>::toMatrix(c), m); } template void DualComplexTransformationTest::compose() { setTestCaseTemplateName(Math::TypeTraits::name()); Math::DualComplex parent = Math::DualComplex::rotation(Math::Deg{T(17.0)}); Math::DualComplex child = Math::DualComplex::translation({T(1.0), T(-0.3)}); CORRADE_COMPARE(Implementation::Transformation>::compose(parent, child), parent*child); } template void DualComplexTransformationTest::inverted() { setTestCaseTemplateName(Math::TypeTraits::name()); Math::DualComplex c = Math::DualComplex::rotation(Math::Deg{T(17.0)})*Math::DualComplex::translation({T(1.0), T(-0.3)}); CORRADE_COMPARE(Implementation::Transformation>::inverted(c)*c, Math::DualComplex{}); } template void DualComplexTransformationTest::setTransformation() { setTestCaseTemplateName(Math::TypeTraits::name()); Object2D o; /* Dirty after setting transformation */ o.setClean(); CORRADE_VERIFY(!o.isDirty()); o.setTransformation(Math::DualComplex::rotation(Math::Deg{T(17.0)})); CORRADE_VERIFY(o.isDirty()); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3::rotation(Math::Deg{T(17.0)})); /* Scene cannot be transformed */ Scene2D s; s.setClean(); s.setTransformation(Math::DualComplex::rotation(Math::Deg{T(17.0)})); CORRADE_VERIFY(!s.isDirty()); CORRADE_COMPARE(s.transformationMatrix(), Math::Matrix3{}); } template void DualComplexTransformationTest::setTransformationInvalid() { setTestCaseTemplateName(Math::TypeTraits::name()); CORRADE_SKIP_IF_NO_ASSERT(); Object2D o; /* Can't transform with non-rigid transformation */ Containers::String out; Error redirectError{&out}; o.setTransformation(Math::DualComplex({T(1.0), T(2.0)}, {})); CORRADE_COMPARE(out, "SceneGraph::DualComplexTransformation::setTransformation(): the dual complex number is not normalized\n"); } template void DualComplexTransformationTest::resetTransformation() { setTestCaseTemplateName(Math::TypeTraits::name()); Object2D o; o.setTransformation(Math::DualComplex::rotation(Math::Deg{T(17.0)})); CORRADE_VERIFY(o.transformationMatrix() != Math::Matrix3{}); o.resetTransformation(); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3{}); } template void DualComplexTransformationTest::transform() { setTestCaseTemplateName(Math::TypeTraits::name()); { Object2D o; o.setTransformation(Math::DualComplex::rotation(Math::Deg{T(17.0)})); o.transform(Math::DualComplex::translation({T(1.0), T(-0.3)})); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3::translation({T(1.0), T(-0.3)})*Math::Matrix3::rotation(Math::Deg{T(17.0)})); } { Object2D o; o.setTransformation(Math::DualComplex::rotation(Math::Deg{T(17.0)})); o.transformLocal(Math::DualComplex::translation({T(1.0), T(-0.3)})); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3::rotation(Math::Deg{T(17.0)})*Math::Matrix3::translation({T(1.0), T(-0.3)})); } } template void DualComplexTransformationTest::transformInvalid() { setTestCaseTemplateName(Math::TypeTraits::name()); CORRADE_SKIP_IF_NO_ASSERT(); /* Can't transform with non-rigid transformation */ Object2D o; Containers::String out; Error redirectError{&out}; o.transform(Math::DualComplex{{T(1.0), T(2.0)}, {}}); CORRADE_COMPARE(out, "SceneGraph::DualComplexTransformation::transform(): the dual complex number is not normalized\n"); } template void DualComplexTransformationTest::translate() { setTestCaseTemplateName(Math::TypeTraits::name()); { Object2D o; o.setTransformation(Math::DualComplex::rotation(Math::Deg{T(17.0)})); o.translate({T(1.0), T(-0.3)}); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3::translation({T(1.0), T(-0.3)})*Math::Matrix3::rotation(Math::Deg{T(17.0)})); } { Object2D o; o.setTransformation(Math::DualComplex::rotation(Math::Deg{T(17.0)})); o.translateLocal({T(1.0), T(-0.3)}); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3::rotation(Math::Deg{T(17.0)})*Math::Matrix3::translation({T(1.0), T(-0.3)})); } } template void DualComplexTransformationTest::rotate() { setTestCaseTemplateName(Math::TypeTraits::name()); { Object2D o; o.setTransformation(Math::DualComplex::translation({T(1.0), T(-0.3)})) .rotate(Math::Complex::rotation(Math::Deg{T(7.0)})) .rotate(Math::Deg{T(10.0)}); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3::rotation(Math::Deg{T(17.0)})*Math::Matrix3::translation({T(1.0), T(-0.3)})); } { Object2D o; o.setTransformation(Math::DualComplex::translation({T(1.0), T(-0.3)})) .rotateLocal(Math::Complex::rotation(Math::Deg{T(7.0)})) .rotateLocal(Math::Deg{T(10.0)}); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3::translation({T(1.0), T(-0.3)})*Math::Matrix3::rotation(Math::Deg{T(17.0)})); } } template void DualComplexTransformationTest::normalizeRotation() { setTestCaseTemplateName(Math::TypeTraits::name()); Object2D o; o.setTransformation(Math::DualComplex::rotation(Math::Deg{T(17.0)})); o.normalizeRotation(); CORRADE_COMPARE(o.transformationMatrix(), Math::Matrix3::rotation(Math::Deg{T(17.0)})); } }}}} CORRADE_TEST_MAIN(Magnum::SceneGraph::Test::DualComplexTransformationTest)