Browse Source

Math: test QR and SVD with rotation and scaling instead of shear.

QR gives a reasonable result this time, SVD gives kinda the same thing
but with the actual rotation in U instead of V.
pull/537/head
Vladimír Vondruš 5 years ago
parent
commit
87d8b33dbe
  1. 16
      src/Magnum/Math/Algorithms/Test/QrTest.cpp
  2. 25
      src/Magnum/Math/Algorithms/Test/SvdTest.cpp

16
src/Magnum/Math/Algorithms/Test/QrTest.cpp

@ -35,6 +35,7 @@ struct QrTest: Corrade::TestSuite::Tester {
explicit QrTest();
void test();
void decomposeRotationScaling();
void decomposeRotationShear();
};
@ -46,6 +47,7 @@ typedef Matrix4<Float> Matrix4;
QrTest::QrTest() {
addTests({&QrTest::test,
&QrTest::decomposeRotationScaling,
&QrTest::decomposeRotationShear});
}
@ -67,7 +69,21 @@ void QrTest::test() {
CORRADE_COMPARE(qr.second, rExpected);
}
void QrTest::decomposeRotationScaling() {
Matrix4 a = Matrix4::rotationZ(35.0_degf)*Matrix4::scaling({1.5f, 2.0f, 1.0f});
std::pair<Matrix3x3, Matrix3x3> qr = Algorithms::qr(a.rotationScaling());
CORRADE_COMPARE(qr.first*qr.second, a.rotationScaling());
auto q4 = Matrix4::from(qr.first, {});
auto r4 = Matrix4::from(qr.second, {});
CORRADE_COMPARE(q4, Matrix4::rotationZ(35.0_degf));
CORRADE_COMPARE(r4, Matrix4::scaling({1.5f, 2.0f, 1.0f}));
}
void QrTest::decomposeRotationShear() {
/* Like above, but with order flipped, which results in a shear */
Matrix4 a = Matrix4::scaling({1.5f, 2.0f, 1.0f})*Matrix4::rotationZ(35.0_degf);
std::pair<Matrix3x3, Matrix3x3> qr = Algorithms::qr(a.rotationScaling());

25
src/Magnum/Math/Algorithms/Test/SvdTest.cpp

@ -34,6 +34,7 @@ struct SvdTest: Corrade::TestSuite::Tester {
explicit SvdTest();
template<class T> void test();
void decomposeRotationScaling();
void decomposeRotationShear();
};
@ -46,6 +47,7 @@ template<class T> using Vector5 = Vector<5, T>;
SvdTest::SvdTest() {
addTests({&SvdTest::test<Float>,
&SvdTest::test<Double>,
&SvdTest::decomposeRotationScaling,
&SvdTest::decomposeRotationShear});
}
@ -94,6 +96,28 @@ template<class T> void SvdTest::test() {
}
}
void SvdTest::decomposeRotationScaling() {
typedef Math::Matrix4<Float> Matrix4;
typedef Math::Matrix3x3<Float> Matrix3x3;
typedef Math::Vector3<Float> Vector3;
using namespace Math::Literals;
Matrix4 a = Matrix4::rotationZ(35.0_degf)*Matrix4::scaling({1.5f, 2.0f, 1.0f});
Matrix3x3 u{Magnum::NoInit};
Vector3 w{Magnum::NoInit};
Matrix3x3 v{Magnum::NoInit};
std::tie(u, w, v) = Algorithms::svd(a.rotationScaling());
CORRADE_COMPARE(u*Matrix3x3::fromDiagonal(w)*v.transposed(), a.rotationScaling());
/* V contains flipped signs for the whole matrix, use it to fix the
signs for U */
CORRADE_COMPARE(w, (Vector3{1.5f, 2.0f, 1.0f}));
CORRADE_COMPARE(Matrix4::from(u*v.transposed(), {}), Matrix4::rotationZ(35.0_degf));
}
void SvdTest::decomposeRotationShear() {
typedef Math::Matrix4<Float> Matrix4;
typedef Math::Matrix3x3<Float> Matrix3x3;
@ -101,6 +125,7 @@ void SvdTest::decomposeRotationShear() {
using namespace Math::Literals;
/* Like above, but with order flipped, which results in a shear */
Matrix4 a = Matrix4::scaling({1.5f, 2.0f, 1.0f})*Matrix4::rotationZ(35.0_degf);
Matrix3x3 u{Magnum::NoInit};

Loading…
Cancel
Save