Browse Source

Math: assert uniform scaling in Matrix[34]::rotation().

Needed to adjust the test cases slightly, because they were firing the
assert even if they shouldn't and the "expect fail" case wasn't working
at all. I now badly need proper floating-point equality comparison.
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
f2840e5880
  1. 6
      src/Math/Matrix3.h
  2. 7
      src/Math/Matrix4.h
  3. 15
      src/Math/Test/Matrix3Test.cpp
  4. 15
      src/Math/Test/Matrix4Test.cpp

6
src/Math/Matrix3.h

@ -191,12 +191,14 @@ template<class T> class Matrix3: public Matrix<3, T> {
/** /**
* @brief 2D rotation part of the matrix * @brief 2D rotation part of the matrix
* *
* Normalized upper-left 2x2 part of the matrix. * Normalized upper-left 2x2 part of the matrix. Expects uniform
* scaling.
* @see rotationNormalized(), rotationScaling(), @ref uniformScaling(), * @see rotationNormalized(), rotationScaling(), @ref uniformScaling(),
* rotation(T), Matrix4::rotation() const * rotation(T), Matrix4::rotation() const
* @todo assert uniform scaling (otherwise this would be garbage)
*/ */
Matrix<2, T> rotation() const { Matrix<2, T> rotation() const {
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[0].xy().dot(), (*this)[1].xy().dot()),
"Math::Matrix3::rotation(): the matrix doesn't have uniform scaling", {});
return {(*this)[0].xy().normalized(), return {(*this)[0].xy().normalized(),
(*this)[1].xy().normalized()}; (*this)[1].xy().normalized()};
} }

7
src/Math/Matrix4.h

@ -258,11 +258,11 @@ template<class T> class Matrix4: public Matrix<4, T> {
/** /**
* @brief 3D rotation part of the matrix * @brief 3D rotation part of the matrix
* *
* Normalized upper-left 3x3 part of the matrix. * Normalized upper-left 3x3 part of the matrix. Expects uniform
* scaling.
* @see rotationNormalized(), rotationScaling() const, * @see rotationNormalized(), rotationScaling() const,
* @ref uniformScaling(), rotation(T, const Vector3&), * @ref uniformScaling(), rotation(T, const Vector3&),
* Matrix3::rotation() const * Matrix3::rotation() const
* @todo assert uniform scaling (otherwise this would be garbage)
*/ */
/* Not Matrix3, because it is for affine 2D transformations */ /* Not Matrix3, because it is for affine 2D transformations */
Matrix<3, T> rotation() const; Matrix<3, T> rotation() const;
@ -452,6 +452,9 @@ template<class T> Matrix4<T> Matrix4<T>::perspectiveProjection(const Vector2<T>&
} }
template<class T> inline Matrix<3, T> Matrix4<T>::rotation() const { template<class T> inline Matrix<3, T> Matrix4<T>::rotation() const {
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[0].xyz().dot(), (*this)[1].xyz().dot()) &&
TypeTraits<T>::equals((*this)[1].xyz().dot(), (*this)[2].xyz().dot()),
"Math::Matrix4::rotation(): the matrix doesn't have uniform scaling", {});
return {(*this)[0].xyz().normalized(), return {(*this)[0].xyz().normalized(),
(*this)[1].xyz().normalized(), (*this)[1].xyz().normalized(),
(*this)[2].xyz().normalized()}; (*this)[2].xyz().normalized()};

15
src/Math/Test/Matrix3Test.cpp

@ -319,21 +319,18 @@ void Matrix3Test::rotationPart() {
CORRADE_COMPARE(rotationTranslationPart, expectedRotationPart); CORRADE_COMPARE(rotationTranslationPart, expectedRotationPart);
/* Test uniform scaling */ /* Test uniform scaling */
Matrix3 rotationScaling = rotation*Matrix3::scaling(Vector2(9.0f)); Matrix3 rotationScaling = rotation*Matrix3::scaling(Vector2(3.0f));
Matrix2 rotationScalingPart = rotationScaling.rotation(); Matrix2 rotationScalingPart = rotationScaling.rotation();
CORRADE_COMPARE(rotationScalingPart.determinant(), 1.0f); CORRADE_COMPARE(rotationScalingPart.determinant(), 1.0f);
CORRADE_COMPARE(rotationScalingPart*rotationScalingPart.transposed(), Matrix2()); CORRADE_COMPARE(rotationScalingPart*rotationScalingPart.transposed(), Matrix2());
CORRADE_COMPARE(rotationScalingPart, expectedRotationPart); CORRADE_COMPARE(rotationScalingPart, expectedRotationPart);
/* Fails on non-uniform scaling */ /* Fails on non-uniform scaling */
{ std::ostringstream o;
CORRADE_EXPECT_FAIL("Assertion on uniform scaling is not implemented yet."); Error::setOutput(&o);
std::ostringstream o; Matrix2 rotationScaling2 = (rotation*Matrix3::scaling(Vector2::yScale(3.5f))).rotation();
Error::setOutput(&o); CORRADE_COMPARE(o.str(), "Math::Matrix3::rotation(): the matrix doesn't have uniform scaling\n");
Matrix3 rotationScaling2 = rotation*Matrix3::scaling(Vector2::yScale(3.5f)); CORRADE_COMPARE(rotationScaling2, Matrix2());
CORRADE_COMPARE(o.str(), "Math::Matrix3::rotation(): the matrix doesn't have uniform scaling\n");
CORRADE_COMPARE(rotationScaling2, Matrix3(Matrix3::Zero));
}
} }
void Matrix3Test::uniformScalingPart() { void Matrix3Test::uniformScalingPart() {

15
src/Math/Test/Matrix4Test.cpp

@ -405,21 +405,18 @@ void Matrix4Test::rotationPart() {
CORRADE_COMPARE(rotationTranslationPart, expectedRotationPart); CORRADE_COMPARE(rotationTranslationPart, expectedRotationPart);
/* Test uniform scaling */ /* Test uniform scaling */
Matrix4 rotationScaling = rotation*Matrix4::scaling(Vector3(9.0f)); Matrix4 rotationScaling = rotation*Matrix4::scaling(Vector3(3.0f));
Matrix3 rotationScalingPart = rotationScaling.rotation(); Matrix3 rotationScalingPart = rotationScaling.rotation();
CORRADE_COMPARE(rotationScalingPart.determinant(), 1.0f); CORRADE_COMPARE(rotationScalingPart.determinant(), 1.0f);
CORRADE_COMPARE(rotationScalingPart*rotationScalingPart.transposed(), Matrix3()); CORRADE_COMPARE(rotationScalingPart*rotationScalingPart.transposed(), Matrix3());
CORRADE_COMPARE(rotationScalingPart, expectedRotationPart); CORRADE_COMPARE(rotationScalingPart, expectedRotationPart);
/* Fails on non-uniform scaling */ /* Fails on non-uniform scaling */
{ std::ostringstream o;
CORRADE_EXPECT_FAIL("Assertion on uniform scaling is not implemented yet."); Error::setOutput(&o);
std::ostringstream o; Matrix3 rotationScaling2 = (rotation*Matrix4::scaling(Vector3::yScale(3.5f))).rotation();
Error::setOutput(&o); CORRADE_COMPARE(o.str(), "Math::Matrix4::rotation(): the matrix doesn't have uniform scaling\n");
Matrix4 rotationScaling2 = rotation*Matrix4::scaling(Vector3::yScale(3.5f)); CORRADE_COMPARE(rotationScaling2, Matrix3());
CORRADE_COMPARE(o.str(), "Math::Matrix4::rotation(): the matrix doesn't have uniform scaling\n");
CORRADE_COMPARE(rotationScaling2, Matrix4(Matrix4::Zero));
}
} }
void Matrix4Test::uniformScalingPart() { void Matrix4Test::uniformScalingPart() {

Loading…
Cancel
Save