Browse Source

Vector::angle() now expects normalized vectors.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
37a5d6347e
  1. 1
      src/Math/Test/CMakeLists.txt
  2. 9
      src/Math/Test/VectorTest.cpp
  3. 11
      src/Math/Vector.h

1
src/Math/Test/CMakeLists.txt

@ -1,6 +1,7 @@
corrade_add_test2(MathMathTypeTraitsTest MathTypeTraitsTest.cpp)
corrade_add_test2(MathVectorTest VectorTest.cpp)
set_target_properties(MathVectorTest PROPERTIES COMPILE_FLAGS -DCORRADE_GRACEFUL_ASSERT)
corrade_add_test2(MathVector2Test Vector2Test.cpp)
corrade_add_test2(MathVector3Test Vector3Test.cpp)
corrade_add_test2(MathVector4Test Vector4Test.cpp)

9
src/Math/Test/VectorTest.cpp

@ -131,7 +131,14 @@ void VectorTest::product() {
}
void VectorTest::angle() {
CORRADE_COMPARE(Vector3::angle({2.0f, 3.0f, 4.0f}, {1.0f, -2.0f, 3.0f}), rad(1.162514f));
ostringstream o;
Error::setOutput(&o);
/* Both vectors must be normalized, otherwise NaN is returned */
CORRADE_COMPARE(Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(), {1.0f, -2.0f, 3.0f}), numeric_limits<Vector3::Type>::quiet_NaN());
CORRADE_COMPARE(o.str(), "Math::Vector::angle(): vectors must be normalized!\n");
CORRADE_COMPARE(Vector3::angle({2.0f, 3.0f, 4.0f}, Vector3(1.0f, -2.0f, 3.0f).normalized()), numeric_limits<Vector3::Type>::quiet_NaN());
CORRADE_COMPARE(Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(), Vector3(1.0f, -2.0f, 3.0f).normalized()), rad(1.162514f));
}
void VectorTest::negative() {

11
src/Math/Vector.h

@ -20,6 +20,7 @@
*/
#include <cmath>
#include <limits>
#include <Utility/Debug.h>
#include "MathTypeTraits.h"
@ -82,16 +83,18 @@ template<size_t size, class T> class Vector {
}
/**
* @brief Angle between vectors
* @brief Angle between normalized vectors
*
* @f[
* \phi = \frac{a \cdot b}{|a| \cdot |b|}
* @f]
*
* @todo optimize - Assume the vectors are normalized?
* @attention If any of the parameters is not normalized (and
* assertions are enabled), returns NaN.
*/
inline static T angle(const Vector<size, T>& a, const Vector<size, T>& b) {
return std::acos(dot(a, b)/(a.length()*b.length()));
CORRADE_ASSERT(MathTypeTraits<T>::equals(a.dot(), T(1)) && MathTypeTraits<T>::equals(b.dot(), T(1)),
"Math::Vector::angle(): vectors must be normalized!", std::numeric_limits<T>::quiet_NaN());
return std::acos(dot(a, b));
}
/** @brief Default constructor */

Loading…
Cancel
Save