diff --git a/src/Math/Matrix3.h b/src/Math/Matrix3.h index f9b1b09b5..3cd3f6135 100644 --- a/src/Math/Matrix3.h +++ b/src/Math/Matrix3.h @@ -157,12 +157,15 @@ template class Matrix3: public Matrix<3, T> { * * Normalized upper-left 2x2 part of the matrix. * @see rotationScaling() const, rotation(T), Matrix4::rotation() const + * @todo assert uniform scaling (otherwise this would be garbage) */ inline Matrix<2, T> rotation() const { return {(*this)[0].xy().normalized(), (*this)[1].xy().normalized()}; } + /** @todo uniform scaling extraction */ + /** * @brief Right-pointing 2D vector * diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index 198aa8c65..36af432c4 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -293,6 +293,7 @@ template class Matrix4: public Matrix<4, T> { * Normalized upper-left 3x3 part of the matrix. * @see rotationScaling() const, rotation(T, const Vector3&), * Matrix3::rotation() const + * @todo assert uniform scaling (otherwise this would be garbage) */ inline Matrix<3, T> rotation() const { /* Not Matrix3, because it is for affine 2D transformations */ @@ -301,6 +302,8 @@ template class Matrix4: public Matrix<4, T> { (*this)[2].xyz().normalized()}; } + /** @todo uniform scaling extraction */ + /** * @brief Right-pointing 3D vector * diff --git a/src/Math/Test/Matrix3Test.cpp b/src/Math/Test/Matrix3Test.cpp index 2739fccf1..5cf07abb9 100644 --- a/src/Math/Test/Matrix3Test.cpp +++ b/src/Math/Test/Matrix3Test.cpp @@ -161,14 +161,32 @@ void Matrix3Test::rotationScalingPart() { } void Matrix3Test::rotationPart() { + Matrix3 rotation = Matrix3::rotation(Deg(15.0f)); Matrix2 expectedRotationPart(Vector2( 0.965926f, 0.258819f), Vector2(-0.258819f, 0.965926f)); - Matrix3 rotation = Matrix3::rotation(Deg(15.0f)); - CORRADE_COMPARE(rotation.rotation(), expectedRotationPart); - - Matrix3 rotationTransformed = Matrix3::translation({2.0f, 5.0f})*rotation*Matrix3::scaling(Vector2(9.0f)); - CORRADE_COMPARE(rotationTransformed.rotation(), expectedRotationPart); + /* For rotation and translation this is the same as rotationScaling() */ + Matrix3 rotationTranslation = rotation*Matrix3::translation({2.0f, 5.0f}); + Matrix2 rotationTranslationPart = rotationTranslation.rotation(); + CORRADE_COMPARE(rotationTranslationPart, rotationTranslation.rotationScaling()); + CORRADE_COMPARE(rotationTranslationPart, expectedRotationPart); + + /* Test uniform scaling */ + Matrix3 rotationScaling = rotation*Matrix3::scaling(Vector2(9.0f)); + Matrix2 rotationScalingPart = rotationScaling.rotation(); + CORRADE_COMPARE(rotationScalingPart.determinant(), 1.0f); + CORRADE_COMPARE(rotationScalingPart*rotationScalingPart.transposed(), Matrix2()); + CORRADE_COMPARE(rotationScalingPart, expectedRotationPart); + + /* Fails on non-uniform scaling */ + { + CORRADE_EXPECT_FAIL("Assertion on uniform scaling is not implemented yet."); + std::ostringstream o; + Error::setOutput(&o); + Matrix3 rotationScaling2 = rotation*Matrix3::scaling(Vector2::yScale(3.5f)); + CORRADE_COMPARE(o.str(), "Math::Matrix3::rotation(): the matrix doesn't have uniform scaling\n"); + CORRADE_COMPARE(rotationScaling2, Matrix3(Matrix3::Zero)); + } } void Matrix3Test::vectorParts() { diff --git a/src/Math/Test/Matrix4Test.cpp b/src/Math/Test/Matrix4Test.cpp index f997450bb..da5f96e57 100644 --- a/src/Math/Test/Matrix4Test.cpp +++ b/src/Math/Test/Matrix4Test.cpp @@ -228,19 +228,33 @@ void Matrix4Test::rotationScalingPart() { } void Matrix4Test::rotationPart() { + Matrix4 rotation = Matrix4::rotation(Deg(-74.0f), Vector3(-1.0f, 2.0f, 2.0f).normalized()); Matrix3 expectedRotationPart(Vector3( 0.35612214f, -0.80181062f, 0.47987163f), Vector3( 0.47987163f, 0.59757638f, 0.6423595f), Vector3(-0.80181062f, 0.0015183985f, 0.59757638f)); - Matrix4 rotation = Matrix4::rotation(Deg(-74.0f), Vector3(-1.0f, 2.0f, 2.0f).normalized()); - CORRADE_COMPARE(rotation.rotation().determinant(), 1.0f); - CORRADE_COMPARE(rotation.rotation()*rotation.rotation().transposed(), Matrix3()); - CORRADE_COMPARE(rotation.rotation(), expectedRotationPart); - - Matrix4 rotationTransformed = Matrix4::translation({2.0f, 5.0f, -3.0f})*rotation*Matrix4::scaling(Vector3(9.0f)); - CORRADE_COMPARE(rotationTransformed.rotation().determinant(), 1.0f); - CORRADE_COMPARE(rotationTransformed.rotation()*rotationTransformed.rotation().transposed(), Matrix3()); - CORRADE_COMPARE(rotationTransformed.rotation(), expectedRotationPart); + /* For rotation and translation this is the same as rotationScaling() */ + Matrix4 rotationTranslation = rotation*Matrix4::translation({2.0f, 5.0f, -3.0f}); + Matrix3 rotationTranslationPart = rotationTranslation.rotation(); + CORRADE_COMPARE(rotationTranslationPart, rotationTranslation.rotationScaling()); + CORRADE_COMPARE(rotationTranslationPart, expectedRotationPart); + + /* Test uniform scaling */ + Matrix4 rotationScaling = rotation*Matrix4::scaling(Vector3(9.0f)); + Matrix3 rotationScalingPart = rotationScaling.rotation(); + CORRADE_COMPARE(rotationScalingPart.determinant(), 1.0f); + CORRADE_COMPARE(rotationScalingPart*rotationScalingPart.transposed(), Matrix3()); + CORRADE_COMPARE(rotationScalingPart, expectedRotationPart); + + /* Fails on non-uniform scaling */ + { + CORRADE_EXPECT_FAIL("Assertion on uniform scaling is not implemented yet."); + std::ostringstream o; + Error::setOutput(&o); + Matrix4 rotationScaling2 = rotation*Matrix4::scaling(Vector3::yScale(3.5f)); + CORRADE_COMPARE(o.str(), "Math::Matrix4::rotation(): the matrix doesn't have uniform scaling\n"); + CORRADE_COMPARE(rotationScaling2, Matrix4(Matrix4::Zero)); + } } void Matrix4Test::vectorParts() {