From 0f1142e5ed1b5216c05560e60e42e1c76e013499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 20 Aug 2013 22:10:30 +0200 Subject: [PATCH] Math: added also Matrix[34]::uniformScalingSquared(). Usable to avoid square root and then squaring the value again in spheroid collision detection algorithms. --- src/Math/Matrix3.h | 24 +++++++++++++++++++----- src/Math/Matrix4.h | 22 ++++++++++++++++++---- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/Math/Matrix3.h b/src/Math/Matrix3.h index 6b7398102..d9d9b94b0 100644 --- a/src/Math/Matrix3.h +++ b/src/Math/Matrix3.h @@ -204,21 +204,35 @@ template 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 - * the scaling is the same in all axes. + * Squared length of vectors in upper-left 2x2 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 Vector2&), * @ref Matrix4::uniformScaling() */ - T uniformScaling() const { + T uniformScalingSquared() const { const T scalingSquared = (*this)[0].xy().dot(); CORRADE_ASSERT(TypeTraits::equals((*this)[1].xy().dot(), scalingSquared), "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 * diff --git a/src/Math/Matrix4.h b/src/Math/Matrix4.h index 48f5c84db..be00c05f3 100644 --- a/src/Math/Matrix4.h +++ b/src/Math/Matrix4.h @@ -267,16 +267,30 @@ template class Matrix4: public Matrix<4, T> { /* Not Matrix3, because it is for affine 2D transformations */ 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 * * 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(), * @ref rotationNormalized(), @ref scaling(const Vector3&), * @ref Matrix3::uniformScaling() */ - T uniformScaling() const; + T uniformScaling() const { return std::sqrt(uniformScalingSquared()); } /** * @brief Right-pointing 3D vector @@ -460,12 +474,12 @@ template inline Matrix<3, T> Matrix4::rotation() const { (*this)[2].xyz().normalized()}; } -template T Matrix4::uniformScaling() const { +template T Matrix4::uniformScalingSquared() const { const T scalingSquared = (*this)[0].xyz().dot(); CORRADE_ASSERT(TypeTraits::equals((*this)[1].xyz().dot(), scalingSquared) && TypeTraits::equals((*this)[2].xyz().dot(), scalingSquared), "Math::Matrix4::uniformScaling(): the matrix doesn't have uniform scaling", {}); - return std::sqrt(scalingSquared); + return scalingSquared; } template Matrix4 Matrix4::invertedRigid() const {