Browse Source

Math: added Vector::lengthInverted().

Also because there are SIMD instructions for that.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
4ab3ab6534
  1. 2
      src/Math/Functions.h
  2. 6
      src/Math/Test/VectorTest.cpp
  3. 16
      src/Math/Vector.h

2
src/Math/Functions.h

@ -219,7 +219,7 @@ template<std::size_t size, class T> Vector<size, T> sqrt(const Vector<size, T>&
/**
@brief Inverse square root
@see sqrt()
@see sqrt(), Vector::lengthInverted()
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> inline T sqrtInverted(const T& a);

6
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));
}

16
src/Math/Vector.h

@ -457,20 +457,32 @@ template<std::size_t size, class T> 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<size, T> normalized() const {
return *this/length();
return *this*lengthInverted();
}
/**

Loading…
Cancel
Save