diff --git a/src/Math/Functions.h b/src/Math/Functions.h index d3a9be345..f3e2f091d 100644 --- a/src/Math/Functions.h +++ b/src/Math/Functions.h @@ -219,7 +219,7 @@ template Vector sqrt(const Vector& /** @brief Inverse square root -@see sqrt() +@see sqrt(), Vector::lengthInverted() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T sqrtInverted(const T& a); diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 583df6b19..7cc938656 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -78,6 +78,7 @@ class VectorTest: public Corrade::TestSuite::Tester { void dot(); void dotSelf(); void length(); + void lengthInverted(); void normalized(); void sum(); @@ -123,6 +124,7 @@ VectorTest::VectorTest() { &VectorTest::dot, &VectorTest::dotSelf, &VectorTest::length, + &VectorTest::lengthInverted, &VectorTest::normalized, &VectorTest::sum, @@ -310,6 +312,10 @@ void VectorTest::length() { CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).length(), 5.4772256f); } +void VectorTest::lengthInverted() { + CORRADE_COMPARE(Vector4(1.0f, 2.0f, 3.0f, 4.0f).lengthInverted(), 0.182574f); +} + void VectorTest::normalized() { CORRADE_COMPARE(Vector4(1.0f, 1.0f, 1.0f, 1.0f).normalized(), Vector4(0.5f, 0.5f, 0.5f, 0.5f)); } diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 3aacdc675..2fb6adfa0 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -457,20 +457,32 @@ template class Vector { * values. @f[ * |\boldsymbol a| = \sqrt{\boldsymbol a \cdot \boldsymbol a} * @f] - * @see Math::sqrt(), normalized() + * @see lengthInverted(), Math::sqrt(), normalized() * @todo something like std::hypot() for possibly better precision? */ inline T length() const { return std::sqrt(dot()); } + /** + * @brief Inverse vector length + * + * @f[ + * \frac{1}{|\boldsymbol a|} = \frac{1}{\sqrt{\boldsymbol a \cdot \boldsymbol a}} + * @f] + * @see length(), Math::sqrtInverted(), normalized() + */ + inline T lengthInverted() const { + return T(1)/length(); + } + /** * @brief Normalized vector (of unit length) * * @see isNormalized() */ inline Vector normalized() const { - return *this/length(); + return *this*lengthInverted(); } /**