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
*
* Normalized upper-left 2x2 part of the matrix.
* Normalized upper-left 2x2 part of the matrix. Expects uniform
* scaling.
* @see rotationNormalized(), rotationScaling(), @ref uniformScaling(),
* rotation(T), Matrix4::rotation() const
* @todo assert uniform scaling (otherwise this would be garbage)
*/
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(),
(*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
*
* Normalized upper-left 3x3 part of the matrix.
* Normalized upper-left 3x3 part of the matrix. Expects uniform
* scaling.
* @see rotationNormalized(), rotationScaling() const,
* @ref uniformScaling(), rotation(T, const Vector3&),
* Matrix3::rotation() const
* @todo assert uniform scaling (otherwise this would be garbage)
*/
/* Not Matrix3, because it is for affine 2D transformations */
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 {
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(),
(*this)[1].xyz().normalized(),
(*this)[2].xyz().normalized()};

15
src/Math/Test/Matrix3Test.cpp

@ -319,21 +319,18 @@ void Matrix3Test::rotationPart() {
CORRADE_COMPARE(rotationTranslationPart, expectedRotationPart);
/* Test uniform scaling */
Matrix3 rotationScaling = rotation*Matrix3::scaling(Vector2(9.0f));
Matrix3 rotationScaling = rotation*Matrix3::scaling(Vector2(3.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));
}
std::ostringstream o;
Error::setOutput(&o);
Matrix2 rotationScaling2 = (rotation*Matrix3::scaling(Vector2::yScale(3.5f))).rotation();
CORRADE_COMPARE(o.str(), "Math::Matrix3::rotation(): the matrix doesn't have uniform scaling\n");
CORRADE_COMPARE(rotationScaling2, Matrix2());
}
void Matrix3Test::uniformScalingPart() {

15
src/Math/Test/Matrix4Test.cpp

@ -405,21 +405,18 @@ void Matrix4Test::rotationPart() {
CORRADE_COMPARE(rotationTranslationPart, expectedRotationPart);
/* Test uniform scaling */
Matrix4 rotationScaling = rotation*Matrix4::scaling(Vector3(9.0f));
Matrix4 rotationScaling = rotation*Matrix4::scaling(Vector3(3.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));
}
std::ostringstream o;
Error::setOutput(&o);
Matrix3 rotationScaling2 = (rotation*Matrix4::scaling(Vector3::yScale(3.5f))).rotation();
CORRADE_COMPARE(o.str(), "Math::Matrix4::rotation(): the matrix doesn't have uniform scaling\n");
CORRADE_COMPARE(rotationScaling2, Matrix3());
}
void Matrix4Test::uniformScalingPart() {

Loading…
Cancel
Save