Browse Source

Math: use Rad for Vector::angle().

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
1d40254676
  1. 14
      src/Math/Test/VectorTest.cpp
  2. 9
      src/Math/Vector.h

14
src/Math/Test/VectorTest.cpp

@ -63,6 +63,7 @@ class VectorTest: public Corrade::TestSuite::Tester {
void configuration(); void configuration();
}; };
typedef Math::Rad<float> Rad;
typedef Vector<3, float> Vector3; typedef Vector<3, float> Vector3;
typedef Vector<4, float> Vector4; typedef Vector<4, float> Vector4;
typedef Vector<4, std::int32_t> Vector4i; typedef Vector<4, std::int32_t> Vector4i;
@ -305,17 +306,18 @@ void VectorTest::projectedOntoNormalized() {
void VectorTest::angle() { void VectorTest::angle() {
std::ostringstream o; std::ostringstream o;
Error::setOutput(&o); Error::setOutput(&o);
CORRADE_COMPARE(Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(), {1.0f, -2.0f, 3.0f}), auto angle = Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(), {1.0f, -2.0f, 3.0f});
std::numeric_limits<Vector3::Type>::quiet_NaN()); CORRADE_VERIFY(angle != angle);
CORRADE_COMPARE(o.str(), "Math::Vector::angle(): vectors must be normalized\n"); CORRADE_COMPARE(o.str(), "Math::Vector::angle(): vectors must be normalized\n");
o.str(""); o.str("");
CORRADE_COMPARE(Vector3::angle({2.0f, 3.0f, 4.0f}, Vector3(1.0f, -2.0f, 3.0f).normalized()), angle = Vector3::angle({2.0f, 3.0f, 4.0f}, Vector3(1.0f, -2.0f, 3.0f).normalized());
std::numeric_limits<Vector3::Type>::quiet_NaN()); CORRADE_VERIFY(angle != angle);
CORRADE_COMPARE(o.str(), "Math::Vector::angle(): vectors must be normalized\n"); CORRADE_COMPARE(o.str(), "Math::Vector::angle(): vectors must be normalized\n");
CORRADE_COMPARE(Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(), Vector3(1.0f, -2.0f, 3.0f).normalized()), CORRADE_COMPARE(Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(),
rad(1.162514f)); Vector3(1.0f, -2.0f, 3.0f).normalized()),
Rad(1.162514f));
} }
void VectorTest::debug() { void VectorTest::debug() {

9
src/Math/Vector.h

@ -25,6 +25,7 @@
#include <Utility/Debug.h> #include <Utility/Debug.h>
#include <Utility/ConfigurationValue.h> #include <Utility/ConfigurationValue.h>
#include "Math/Angle.h"
#include "Math/BoolVector.h" #include "Math/BoolVector.h"
#include "Math/MathTypeTraits.h" #include "Math/MathTypeTraits.h"
@ -78,16 +79,16 @@ template<std::size_t size, class T> class Vector {
} }
/** /**
* @brief Angle between normalized vectors (in radians) * @brief Angle between normalized vectors
* *
* Expects that both vectors are normalized. @f[ * Expects that both vectors are normalized. @f[
* \theta = acos \left( \frac{\boldsymbol a \cdot \boldsymbol b}{|\boldsymbol a| \cdot |\boldsymbol b|} \right) = acos (\boldsymbol a \cdot \boldsymbol b) * \theta = acos \left( \frac{\boldsymbol a \cdot \boldsymbol b}{|\boldsymbol a| \cdot |\boldsymbol b|} \right) = acos (\boldsymbol a \cdot \boldsymbol b)
* @f] * @f]
*/ */
inline static T angle(const Vector<size, T>& normalizedA, const Vector<size, T>& normalizedB) { inline static Rad<T> angle(const Vector<size, T>& normalizedA, const Vector<size, T>& normalizedB) {
CORRADE_ASSERT(MathTypeTraits<T>::equals(normalizedA.dot(), T(1)) && MathTypeTraits<T>::equals(normalizedB.dot(), T(1)), CORRADE_ASSERT(MathTypeTraits<T>::equals(normalizedA.dot(), T(1)) && MathTypeTraits<T>::equals(normalizedB.dot(), T(1)),
"Math::Vector::angle(): vectors must be normalized", std::numeric_limits<T>::quiet_NaN()); "Math::Vector::angle(): vectors must be normalized", Rad<T>(std::numeric_limits<T>::quiet_NaN()));
return std::acos(dot(normalizedA, normalizedB)); return Rad<T>(std::acos(dot(normalizedA, normalizedB)));
} }
/** /**

Loading…
Cancel
Save