diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index 0927efe57..f86e0895d 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -158,6 +158,14 @@ template class Matrix4: public Matrix { /** @copydoc Matrix::inversed() */ inline Matrix4 inversed() const { return Matrix::inversed(); } + + /** @brief Rotation part of the matrix */ + inline Matrix3 rotation() const { + return Matrix3::from( + (*this)[0].xyz().normalized(), + (*this)[1].xyz().normalized(), + (*this)[2].xyz().normalized()); + } }; #ifndef DOXYGEN_GENERATING_OUTPUT diff --git a/src/Math/Test/Matrix4Test.cpp b/src/Math/Test/Matrix4Test.cpp index ea0f25b35..62437f47f 100644 --- a/src/Math/Test/Matrix4Test.cpp +++ b/src/Math/Test/Matrix4Test.cpp @@ -29,6 +29,7 @@ using namespace Corrade::Utility; namespace Magnum { namespace Math { namespace Test { typedef Math::Matrix4 Matrix4; +typedef Math::Matrix3 Matrix3; void Matrix4Test::translation() { Matrix4 matrix( @@ -63,6 +64,20 @@ void Matrix4Test::rotation() { QVERIFY(Matrix4::rotation(deg(-74.0f), {-1.0f, 2.0f, 2.0f}) == matrix); } +void Matrix4Test::rotationPart() { + Matrix3 expectedRotationPart( + 0.35612214f, -0.80181062f, 0.47987163f, + 0.47987163f, 0.59757638f, 0.6423595f, + -0.80181062f, 0.0015183985f, 0.59757638f + ); + + Matrix4 rotation = Matrix4::rotation(deg(-74.0f), {-1.0f, 2.0f, 2.0f}); + QVERIFY(rotation.rotation() == expectedRotationPart); + + Matrix4 rotationTransformed = Matrix4::translation({2.0f, 5.0f, -3.0f})*rotation*Matrix4::scaling(Vector3(9.0f)); + QVERIFY(rotationTransformed.rotation() == expectedRotationPart); +} + void Matrix4Test::debug() { Matrix4 m( 3.0f, 5.0f, 8.0f, 4.0f, diff --git a/src/Math/Test/Matrix4Test.h b/src/Math/Test/Matrix4Test.h index 1aeb1d123..e4da204ea 100644 --- a/src/Math/Test/Matrix4Test.h +++ b/src/Math/Test/Matrix4Test.h @@ -26,6 +26,7 @@ class Matrix4Test: public QObject { void translation(); void scaling(); void rotation(); + void rotationPart(); void debug(); };