Browse Source

Math: added also Matrix[34]::uniformScalingSquared().

Usable to avoid square root and then squaring the value again in
spheroid collision detection algorithms.
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
0f1142e5ed
  1. 24
      src/Math/Matrix3.h
  2. 22
      src/Math/Matrix4.h

24
src/Math/Matrix3.h

@ -204,21 +204,35 @@ template<class T> class Matrix3: public Matrix<3, T> {
} }
/** /**
* @brief Uniform scaling part of the matrix * @brief Uniform scaling part of the matrix, squared
* *
* Length of vectors in upper-left 2x2 part of the matrix. Expects that * Squared length of vectors in upper-left 2x2 part of the matrix.
* the scaling is the same in all axes. * Expects that the scaling is the same in all axes. Faster alternative
* to @ref uniformScaling(), because it doesn't compute the square
* root.
* @see @ref rotationScaling(), @ref rotation(), * @see @ref rotationScaling(), @ref rotation(),
* @ref rotationNormalized(), @ref scaling(const Vector2&), * @ref rotationNormalized(), @ref scaling(const Vector2&),
* @ref Matrix4::uniformScaling() * @ref Matrix4::uniformScaling()
*/ */
T uniformScaling() const { T uniformScalingSquared() const {
const T scalingSquared = (*this)[0].xy().dot(); const T scalingSquared = (*this)[0].xy().dot();
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[1].xy().dot(), scalingSquared), CORRADE_ASSERT(TypeTraits<T>::equals((*this)[1].xy().dot(), scalingSquared),
"Math::Matrix3::uniformScaling(): the matrix doesn't have uniform scaling", {}); "Math::Matrix3::uniformScaling(): the matrix doesn't have uniform scaling", {});
return std::sqrt(scalingSquared); return scalingSquared;
} }
/**
* @brief Uniform scaling part of the matrix
*
* Length of vectors in upper-left 2x2 part of the matrix. Expects that
* the scaling is the same in all axes. Use faster alternative
* @ref uniformScalingSquared() where possible.
* @see @ref rotationScaling(), @ref rotation(),
* @ref rotationNormalized(), @ref scaling(const Vector2&),
* @ref Matrix4::uniformScaling()
*/
T uniformScaling() const { return std::sqrt(uniformScalingSquared()); }
/** /**
* @brief Right-pointing 2D vector * @brief Right-pointing 2D vector
* *

22
src/Math/Matrix4.h

@ -267,16 +267,30 @@ template<class T> class Matrix4: public Matrix<4, T> {
/* 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;
/**
* @brief Uniform scaling part of the matrix, squared
*
* Squared length of vectors in upper-left 3x3 part of the matrix.
* Expects that the scaling is the same in all axes. Faster alternative
* to @ref uniformScaling(), because it doesn't compute the square
* root.
* @see @ref rotationScaling(), @ref rotation(),
* @ref rotationNormalized(), @ref scaling(const Vector3&),
* @ref Matrix3::uniformScaling()
*/
T uniformScalingSquared() const;
/** /**
* @brief Uniform scaling part of the matrix * @brief Uniform scaling part of the matrix
* *
* Length of vectors in upper-left 3x3 part of the matrix. Expects that * Length of vectors in upper-left 3x3 part of the matrix. Expects that
* the scaling is the same in all axes. * the scaling is the same in all axes. Use faster alternative
* @ref uniformScalingSquared() where possible.
* @see @ref rotationScaling(), @ref rotation(), * @see @ref rotationScaling(), @ref rotation(),
* @ref rotationNormalized(), @ref scaling(const Vector3&), * @ref rotationNormalized(), @ref scaling(const Vector3&),
* @ref Matrix3::uniformScaling() * @ref Matrix3::uniformScaling()
*/ */
T uniformScaling() const; T uniformScaling() const { return std::sqrt(uniformScalingSquared()); }
/** /**
* @brief Right-pointing 3D vector * @brief Right-pointing 3D vector
@ -460,12 +474,12 @@ template<class T> inline Matrix<3, T> Matrix4<T>::rotation() const {
(*this)[2].xyz().normalized()}; (*this)[2].xyz().normalized()};
} }
template<class T> T Matrix4<T>::uniformScaling() const { template<class T> T Matrix4<T>::uniformScalingSquared() const {
const T scalingSquared = (*this)[0].xyz().dot(); const T scalingSquared = (*this)[0].xyz().dot();
CORRADE_ASSERT(TypeTraits<T>::equals((*this)[1].xyz().dot(), scalingSquared) && CORRADE_ASSERT(TypeTraits<T>::equals((*this)[1].xyz().dot(), scalingSquared) &&
TypeTraits<T>::equals((*this)[2].xyz().dot(), scalingSquared), TypeTraits<T>::equals((*this)[2].xyz().dot(), scalingSquared),
"Math::Matrix4::uniformScaling(): the matrix doesn't have uniform scaling", {}); "Math::Matrix4::uniformScaling(): the matrix doesn't have uniform scaling", {});
return std::sqrt(scalingSquared); return scalingSquared;
} }
template<class T> Matrix4<T> Matrix4<T>::invertedRigid() const { template<class T> Matrix4<T> Matrix4<T>::invertedRigid() const {

Loading…
Cancel
Save