Browse Source

Math: improve and clean up angle() tests.

pull/433/head
Vladimír Vondruš 6 years ago
parent
commit
94cba139ff
  1. 19
      src/Magnum/Math/Test/ComplexTest.cpp
  2. 22
      src/Magnum/Math/Test/QuaternionTest.cpp
  3. 19
      src/Magnum/Math/Test/VectorTest.cpp

19
src/Magnum/Math/Test/ComplexTest.cpp

@ -425,12 +425,21 @@ void ComplexTest::invertedNormalizedNotNormalized() {
}
void ComplexTest::angle() {
auto a = Complex{ 1.5f, -2.0f}.normalized();
auto b = Complex{-4.0f, 3.5f}.normalized();
/* Verify also that the angle is the same as angle between 2D vectors */
Rad angle = Math::angle(Complex( 1.5f, -2.0f).normalized(),
Complex(-4.0f, 3.5f).normalized());
CORRADE_COMPARE(angle, Math::angle(Vector2( 1.5f, -2.0f).normalized(),
Vector2(-4.0f, 3.5f).normalized()));
CORRADE_COMPARE(angle, Rad(2.933128f));
CORRADE_COMPARE(Math::angle(a, b), Math::angle(
Vector2{ 1.5f, -2.0f}.normalized(),
Vector2{-4.0f, 3.5f}.normalized()));
CORRADE_COMPARE(Math::angle(a, b), 2.933128_radf);
CORRADE_COMPARE(Math::angle(-a, -b), 2.933128_radf);
CORRADE_COMPARE(Math::angle(-a, b), Rad(180.0_degf) - 2.933128_radf);
CORRADE_COMPARE(Math::angle(a, -b), Rad(180.0_degf) - 2.933128_radf);
/* Same / opposite */
CORRADE_COMPARE(Math::angle(a, a), 0.0_radf);
CORRADE_COMPARE(Math::angle(a, -a), 180.0_degf);
}
void ComplexTest::angleNotNormalized() {

22
src/Magnum/Math/Test/QuaternionTest.cpp

@ -491,12 +491,24 @@ void QuaternionTest::rotationNotNormalized() {
}
void QuaternionTest::angle() {
auto a = Quaternion({1.0f, 2.0f, -3.0f}, -4.0f).normalized();
auto b = Quaternion({4.0f, -3.0f, 2.0f}, -1.0f).normalized();
/* Verify also that the angle is the same as angle between 4D vectors */
Rad angle = Math::angle(Quaternion({1.0f, 2.0f, -3.0f}, -4.0f).normalized(),
Quaternion({4.0f, -3.0f, 2.0f}, -1.0f).normalized());
CORRADE_COMPARE(angle, Math::angle(Vector4(1.0f, 2.0f, -3.0f, -4.0f).normalized(),
Vector4(4.0f, -3.0f, 2.0f, -1.0f).normalized()));
CORRADE_COMPARE(angle, Rad(1.704528f));
CORRADE_COMPARE(Math::angle(a, b), Math::angle(
Vector4{1.0f, 2.0f, -3.0f, -4.0f}.normalized(),
Vector4{4.0f, -3.0f, 2.0f, -1.0f}.normalized()));
CORRADE_COMPARE(Math::angle(a, b), 1.704528_radf);
CORRADE_COMPARE(Math::angle(-a, -b), 1.704528_radf);
CORRADE_COMPARE(Math::angle(-a, b), Rad(180.0_degf) - 1.704528_radf);
CORRADE_COMPARE(Math::angle(a, -b), Rad(180.0_degf) - 1.704528_radf);
/* Same / opposite. Well, almost. It's interesting how imprecise
normalization can get. */
CORRADE_COMPARE_WITH(Math::angle(a, a), 0.0_radf,
Corrade::TestSuite::Compare::around(0.0005_radf));
CORRADE_COMPARE_WITH(Math::angle(a, -a), 180.0_degf,
Corrade::TestSuite::Compare::around(0.0005_radf));
}
void QuaternionTest::angleNotNormalized() {

19
src/Magnum/Math/Test/VectorTest.cpp

@ -25,6 +25,7 @@
#include <sstream>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/TestSuite/Compare/Numeric.h>
#include <Corrade/Utility/DebugStl.h>
#include "Magnum/Math/Half.h"
@ -126,6 +127,8 @@ typedef Vector<4, Float> Vector4;
typedef Vector<4, Half> Vector4h;
typedef Vector<4, Int> Vector4i;
using namespace Literals;
VectorTest::VectorTest() {
addTests({&VectorTest::construct,
&VectorTest::constructFromData,
@ -565,9 +568,19 @@ void VectorTest::flipped() {
}
void VectorTest::angle() {
CORRADE_COMPARE(Math::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(),
Vector3(1.0f, -2.0f, 3.0f).normalized()),
Rad(1.162514f));
auto a = Vector3{2.0f, 3.0f, 4.0f}.normalized();
auto b = Vector3{1.0f, -2.0f, 3.0f}.normalized();
CORRADE_COMPARE(Math::angle(a, b), 1.162514_radf);
CORRADE_COMPARE(Math::angle(-a, -b), 1.162514_radf);
CORRADE_COMPARE(Math::angle(-a, b), Rad(180.0_degf) - 1.162514_radf);
CORRADE_COMPARE(Math::angle(a, -b), Rad(180.0_degf) - 1.162514_radf);
/* Same / opposite. Well, almost. It's interesting how imprecise
normalization can get. */
CORRADE_COMPARE_WITH(Math::angle(a, a), 0.0_radf,
Corrade::TestSuite::Compare::around(0.0005_radf));
CORRADE_COMPARE_WITH(Math::angle(a, -a), 180.0_degf,
Corrade::TestSuite::Compare::around(0.0005_radf));
}
void VectorTest::angleNotNormalized() {

Loading…
Cancel
Save