From 00124f65eb10bda02178abb98f8a2441742503ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 1 Jan 2015 14:39:06 +0100 Subject: [PATCH] Math: return unspecified value after an assertion. The user shouldn't expect anything after assertion anyway and it only adds unnecessary noise to the implementation and tests. --- src/Magnum/Math/Complex.h | 6 ++-- src/Magnum/Math/DualQuaternion.h | 3 +- src/Magnum/Math/Quaternion.h | 15 ++++------ src/Magnum/Math/Test/ComplexTest.cpp | 13 ++++---- src/Magnum/Math/Test/DualComplexTest.cpp | 3 +- src/Magnum/Math/Test/DualQuaternionTest.cpp | 3 +- src/Magnum/Math/Test/QuaternionTest.cpp | 33 +++++++-------------- src/Magnum/Math/Test/VectorTest.cpp | 11 +++---- src/Magnum/Math/Vector.h | 6 ++-- 9 files changed, 32 insertions(+), 61 deletions(-) diff --git a/src/Magnum/Math/Complex.h b/src/Magnum/Math/Complex.h index 1250ac619..01036bbb8 100644 --- a/src/Magnum/Math/Complex.h +++ b/src/Magnum/Math/Complex.h @@ -29,7 +29,6 @@ * @brief Class @ref Magnum::Math::Complex */ -#include #include #include @@ -80,7 +79,7 @@ template class Complex { */ static Rad angle(const Complex& normalizedA, const Complex& normalizedB) { CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(), - "Math::Complex::angle(): complex numbers must be normalized", Rad(std::numeric_limits::quiet_NaN())); + "Math::Complex::angle(): complex numbers must be normalized", {}); return Rad(std::acos(normalizedA._real*normalizedB._real + normalizedA._imaginary*normalizedB._imaginary)); } @@ -394,8 +393,7 @@ template class Complex { */ Complex invertedNormalized() const { CORRADE_ASSERT(isNormalized(), - "Math::Complex::invertedNormalized(): complex number must be normalized", - Complex(std::numeric_limits::quiet_NaN(), {})); + "Math::Complex::invertedNormalized(): complex number must be normalized", {}); return conjugated(); } diff --git a/src/Magnum/Math/DualQuaternion.h b/src/Magnum/Math/DualQuaternion.h index a9532a9e4..160bb5ab7 100644 --- a/src/Magnum/Math/DualQuaternion.h +++ b/src/Magnum/Math/DualQuaternion.h @@ -314,8 +314,7 @@ template class DualQuaternion: public Dual> { */ Vector3 transformPointNormalized(const Vector3& vector) const { CORRADE_ASSERT(isNormalized(), - "Math::DualQuaternion::transformPointNormalized(): dual quaternion must be normalized", - Vector3(std::numeric_limits::quiet_NaN())); + "Math::DualQuaternion::transformPointNormalized(): dual quaternion must be normalized", {}); return ((*this)*DualQuaternion(vector)*conjugated()).dual().vector(); } diff --git a/src/Magnum/Math/Quaternion.h b/src/Magnum/Math/Quaternion.h index b5cc94880..3751f726e 100644 --- a/src/Magnum/Math/Quaternion.h +++ b/src/Magnum/Math/Quaternion.h @@ -505,19 +505,19 @@ template Quaternion quaternionFromMatrix(const Matrix3x3& m) { template inline Rad Quaternion::angle(const Quaternion& normalizedA, const Quaternion& normalizedB) { CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(), - "Math::Quaternion::angle(): quaternions must be normalized", Rad(std::numeric_limits::quiet_NaN())); + "Math::Quaternion::angle(): quaternions must be normalized", {}); return Rad(angleInternal(normalizedA, normalizedB)); } template inline Quaternion Quaternion::lerp(const Quaternion& normalizedA, const Quaternion& normalizedB, const T t) { CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(), - "Math::Quaternion::lerp(): quaternions must be normalized", Quaternion({}, std::numeric_limits::quiet_NaN())); + "Math::Quaternion::lerp(): quaternions must be normalized", {}); return ((T(1) - t)*normalizedA + t*normalizedB).normalized(); } template inline Quaternion Quaternion::slerp(const Quaternion& normalizedA, const Quaternion& normalizedB, const T t) { CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(), - "Math::Quaternion::slerp(): quaternions must be normalized", Quaternion({}, std::numeric_limits::quiet_NaN())); + "Math::Quaternion::slerp(): quaternions must be normalized", {}); const T a = angleInternal(normalizedA, normalizedB); return (std::sin((T(1) - t)*a)*normalizedA + std::sin(t*a)*normalizedB)/std::sin(a); } @@ -534,8 +534,7 @@ template inline Quaternion Quaternion::fromMatrix(const Matrix3x3 } template inline Rad Quaternion::angle() const { - CORRADE_ASSERT(isNormalized(), "Math::Quaternion::angle(): quaternion must be normalized", - Rad(std::numeric_limits::quiet_NaN())); + CORRADE_ASSERT(isNormalized(), "Math::Quaternion::angle(): quaternion must be normalized", {}); return Rad(T(2)*std::acos(_scalar)); } @@ -564,14 +563,12 @@ template inline Quaternion Quaternion::operator*(const Quaternion } template inline Quaternion Quaternion::invertedNormalized() const { - CORRADE_ASSERT(isNormalized(), "Math::Quaternion::invertedNormalized(): quaternion must be normalized", - Quaternion({}, std::numeric_limits::quiet_NaN())); + CORRADE_ASSERT(isNormalized(), "Math::Quaternion::invertedNormalized(): quaternion must be normalized", {}); return conjugated(); } template inline Vector3 Quaternion::transformVectorNormalized(const Vector3< T >& vector) const { - CORRADE_ASSERT(isNormalized(), "Math::Quaternion::transformVectorNormalized(): quaternion must be normalized", - Vector3(std::numeric_limits::quiet_NaN())); + CORRADE_ASSERT(isNormalized(), "Math::Quaternion::transformVectorNormalized(): quaternion must be normalized", {}); return ((*this)*Quaternion(vector)*conjugated()).vector(); } diff --git a/src/Magnum/Math/Test/ComplexTest.cpp b/src/Magnum/Math/Test/ComplexTest.cpp index 387d85966..e17efcdb0 100644 --- a/src/Magnum/Math/Test/ComplexTest.cpp +++ b/src/Magnum/Math/Test/ComplexTest.cpp @@ -229,8 +229,7 @@ void ComplexTest::invertedNormalized() { Complex a(-0.6f, 0.8f); Complex b(-0.6f, -0.8f); - Complex notInverted = (a*2).invertedNormalized(); - CORRADE_VERIFY(notInverted != notInverted); + (a*2).invertedNormalized(); CORRADE_COMPARE(o.str(), "Math::Complex::invertedNormalized(): complex number must be normalized\n"); Complex inverted = a.invertedNormalized(); @@ -242,18 +241,16 @@ void ComplexTest::invertedNormalized() { void ComplexTest::angle() { std::ostringstream o; Error::setOutput(&o); - auto angle = Complex::angle(Complex(1.5f, -2.0f).normalized(), {-4.0f, 3.5f}); - CORRADE_VERIFY(angle != angle); + Complex::angle(Complex(1.5f, -2.0f).normalized(), {-4.0f, 3.5f}); CORRADE_COMPARE(o.str(), "Math::Complex::angle(): complex numbers must be normalized\n"); o.str({}); - angle = Complex::angle({1.5f, -2.0f}, Complex(-4.0f, 3.5f).normalized()); - CORRADE_VERIFY(angle != angle); + Complex::angle({1.5f, -2.0f}, Complex(-4.0f, 3.5f).normalized()); CORRADE_COMPARE(o.str(), "Math::Complex::angle(): complex numbers must be normalized\n"); /* Verify also that the angle is the same as angle between 2D vectors */ - angle = Complex::angle(Complex( 1.5f, -2.0f).normalized(), - Complex(-4.0f, 3.5f).normalized()); + Rad angle = Complex::angle(Complex( 1.5f, -2.0f).normalized(), + Complex(-4.0f, 3.5f).normalized()); CORRADE_COMPARE(angle, Vector2::angle(Vector2( 1.5f, -2.0f).normalized(), Vector2(-4.0f, 3.5f).normalized())); CORRADE_COMPARE(angle, Rad(2.933128f)); diff --git a/src/Magnum/Math/Test/DualComplexTest.cpp b/src/Magnum/Math/Test/DualComplexTest.cpp index f9bfc91dd..58fa4a40b 100644 --- a/src/Magnum/Math/Test/DualComplexTest.cpp +++ b/src/Magnum/Math/Test/DualComplexTest.cpp @@ -192,8 +192,7 @@ void DualComplexTest::invertedNormalized() { std::ostringstream o; Error::setOutput(&o); - DualComplex notInverted = DualComplex({-1.0f, -2.5f}, {}).invertedNormalized(); - CORRADE_VERIFY(notInverted != notInverted); + DualComplex({-1.0f, -2.5f}, {}).invertedNormalized(); CORRADE_COMPARE(o.str(), "Math::Complex::invertedNormalized(): complex number must be normalized\n"); DualComplex inverted = a.invertedNormalized(); diff --git a/src/Magnum/Math/Test/DualQuaternionTest.cpp b/src/Magnum/Math/Test/DualQuaternionTest.cpp index 2c4e91f52..eec288df8 100644 --- a/src/Magnum/Math/Test/DualQuaternionTest.cpp +++ b/src/Magnum/Math/Test/DualQuaternionTest.cpp @@ -283,8 +283,7 @@ void DualQuaternionTest::transformPointNormalized() { std::ostringstream o; Corrade::Utility::Error::setOutput(&o); - Vector3 notTransformed = (a*Dual(2)).transformPointNormalized(v); - CORRADE_VERIFY(notTransformed != notTransformed); + (a*Dual(2)).transformPointNormalized(v); CORRADE_COMPARE(o.str(), "Math::DualQuaternion::transformPointNormalized(): dual quaternion must be normalized\n"); Vector3 transformedA = a.transformPointNormalized(v); diff --git a/src/Magnum/Math/Test/QuaternionTest.cpp b/src/Magnum/Math/Test/QuaternionTest.cpp index 6ea6ba624..cf547031c 100644 --- a/src/Magnum/Math/Test/QuaternionTest.cpp +++ b/src/Magnum/Math/Test/QuaternionTest.cpp @@ -221,9 +221,7 @@ void QuaternionTest::invertedNormalized() { std::ostringstream o; Error::setOutput(&o); - Quaternion notInverted = a.invertedNormalized(); - CORRADE_COMPARE(notInverted.vector(), Vector3()); - CORRADE_COMPARE(notInverted.scalar(), std::numeric_limits::quiet_NaN()); + a.invertedNormalized(); CORRADE_COMPARE(o.str(), "Math::Quaternion::invertedNormalized(): quaternion must be normalized\n"); Quaternion aNormalized = a.normalized(); @@ -263,18 +261,16 @@ void QuaternionTest::rotation() { void QuaternionTest::angle() { std::ostringstream o; Error::setOutput(&o); - auto angle = Quaternion::angle(Quaternion({1.0f, 2.0f, -3.0f}, -4.0f).normalized(), {{4.0f, -3.0f, 2.0f}, -1.0f}); - CORRADE_VERIFY(angle != angle); + Quaternion::angle(Quaternion({1.0f, 2.0f, -3.0f}, -4.0f).normalized(), {{4.0f, -3.0f, 2.0f}, -1.0f}); CORRADE_COMPARE(o.str(), "Math::Quaternion::angle(): quaternions must be normalized\n"); o.str({}); - angle = Quaternion::angle({{1.0f, 2.0f, -3.0f}, -4.0f}, Quaternion({4.0f, -3.0f, 2.0f}, -1.0f).normalized()); - CORRADE_VERIFY(angle != angle); + Quaternion::angle({{1.0f, 2.0f, -3.0f}, -4.0f}, Quaternion({4.0f, -3.0f, 2.0f}, -1.0f).normalized()); CORRADE_COMPARE(o.str(), "Math::Quaternion::angle(): quaternions must be normalized\n"); /* Verify also that the angle is the same as angle between 4D vectors */ - angle = Quaternion::angle(Quaternion({1.0f, 2.0f, -3.0f}, -4.0f).normalized(), - Quaternion({4.0f, -3.0f, 2.0f}, -1.0f).normalized()); + Rad angle = Quaternion::angle(Quaternion({1.0f, 2.0f, -3.0f}, -4.0f).normalized(), + Quaternion({4.0f, -3.0f, 2.0f}, -1.0f).normalized()); CORRADE_COMPARE(angle, Vector4::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)); @@ -313,15 +309,11 @@ void QuaternionTest::lerp() { std::ostringstream o; Corrade::Utility::Error::setOutput(&o); - Quaternion notLerpA = Quaternion::lerp(a*3.0f, b, 0.35f); - CORRADE_COMPARE(notLerpA.vector(), Vector3()); - CORRADE_COMPARE(notLerpA.scalar(), std::numeric_limits::quiet_NaN()); + Quaternion::lerp(a*3.0f, b, 0.35f); CORRADE_COMPARE(o.str(), "Math::Quaternion::lerp(): quaternions must be normalized\n"); o.str({}); - Quaternion notLerpB = Quaternion::lerp(a, b*-3.0f, 0.35f); - CORRADE_COMPARE(notLerpB.vector(), Vector3()); - CORRADE_COMPARE(notLerpB.scalar(), std::numeric_limits::quiet_NaN()); + Quaternion::lerp(a, b*-3.0f, 0.35f); CORRADE_COMPARE(o.str(), "Math::Quaternion::lerp(): quaternions must be normalized\n"); Quaternion lerp = Quaternion::lerp(a, b, 0.35f); @@ -335,15 +327,11 @@ void QuaternionTest::slerp() { std::ostringstream o; Corrade::Utility::Error::setOutput(&o); - Quaternion notSlerpA = Quaternion::slerp(a*3.0f, b, 0.35f); - CORRADE_COMPARE(notSlerpA.vector(), Vector3()); - CORRADE_COMPARE(notSlerpA.scalar(), std::numeric_limits::quiet_NaN()); + Quaternion::slerp(a*3.0f, b, 0.35f); CORRADE_COMPARE(o.str(), "Math::Quaternion::slerp(): quaternions must be normalized\n"); o.str({}); - Quaternion notSlerpB = Quaternion::slerp(a, b*-3.0f, 0.35f); - CORRADE_COMPARE(notSlerpB.vector(), Vector3()); - CORRADE_COMPARE(notSlerpB.scalar(), std::numeric_limits::quiet_NaN()); + Quaternion::slerp(a, b*-3.0f, 0.35f); CORRADE_COMPARE(o.str(), "Math::Quaternion::slerp(): quaternions must be normalized\n"); Quaternion slerp = Quaternion::slerp(a, b, 0.35f); @@ -367,8 +355,7 @@ void QuaternionTest::transformVectorNormalized() { std::ostringstream o; Error::setOutput(&o); - Vector3 notRotated = (a*2).transformVectorNormalized(v); - CORRADE_VERIFY(notRotated != notRotated); + (a*2).transformVectorNormalized(v); CORRADE_COMPARE(o.str(), "Math::Quaternion::transformVectorNormalized(): quaternion must be normalized\n"); Vector3 rotated = a.transformVectorNormalized(v); diff --git a/src/Magnum/Math/Test/VectorTest.cpp b/src/Magnum/Math/Test/VectorTest.cpp index 90c5adfdd..e639775e5 100644 --- a/src/Magnum/Math/Test/VectorTest.cpp +++ b/src/Magnum/Math/Test/VectorTest.cpp @@ -410,11 +410,10 @@ void VectorTest::projectedOntoNormalized() { Vector3 vector(1.0f, 2.0f, 3.0f); Vector3 line(1.0f, -1.0f, 0.5f); - Vector3 projected = vector.projectedOntoNormalized(line); - CORRADE_VERIFY(projected != projected); + vector.projectedOntoNormalized(line); CORRADE_COMPARE(o.str(), "Math::Vector::projectedOntoNormalized(): line must be normalized\n"); - projected = vector.projectedOntoNormalized(line.normalized()); + Vector3 projected = vector.projectedOntoNormalized(line.normalized()); CORRADE_COMPARE(projected, Vector3(0.222222f, -0.222222f, 0.111111f)); CORRADE_COMPARE(projected.normalized(), line.normalized()); CORRADE_COMPARE(projected, vector.projected(line)); @@ -423,13 +422,11 @@ void VectorTest::projectedOntoNormalized() { void VectorTest::angle() { std::ostringstream o; Error::setOutput(&o); - auto angle = Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(), {1.0f, -2.0f, 3.0f}); - CORRADE_VERIFY(angle != angle); + Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(), {1.0f, -2.0f, 3.0f}); CORRADE_COMPARE(o.str(), "Math::Vector::angle(): vectors must be normalized\n"); o.str({}); - angle = Vector3::angle({2.0f, 3.0f, 4.0f}, Vector3(1.0f, -2.0f, 3.0f).normalized()); - CORRADE_VERIFY(angle != angle); + Vector3::angle({2.0f, 3.0f, 4.0f}, Vector3(1.0f, -2.0f, 3.0f).normalized()); CORRADE_COMPARE(o.str(), "Math::Vector::angle(): vectors must be normalized\n"); CORRADE_COMPARE(Vector3::angle(Vector3(2.0f, 3.0f, 4.0f).normalized(), diff --git a/src/Magnum/Math/Vector.h b/src/Magnum/Math/Vector.h index f23da2e00..cee9b0123 100644 --- a/src/Magnum/Math/Vector.h +++ b/src/Magnum/Math/Vector.h @@ -30,7 +30,6 @@ */ #include -#include #include #include #include @@ -1199,7 +1198,7 @@ extern template Corrade::Utility::Debug MAGNUM_EXPORT operator<<(Corrade::Utilit template inline Rad Vector::angle(const Vector& normalizedA, const Vector& normalizedB) { CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(), - "Math::Vector::angle(): vectors must be normalized", Rad(std::numeric_limits::quiet_NaN())); + "Math::Vector::angle(): vectors must be normalized", {}); return Rad(std::acos(dot(normalizedA, normalizedB))); } @@ -1249,8 +1248,7 @@ template inline Vector Vector::oper } template inline Vector Vector::projectedOntoNormalized(const Vector& line) const { - CORRADE_ASSERT(line.isNormalized(), "Math::Vector::projectedOntoNormalized(): line must be normalized", - (Vector(std::numeric_limits::quiet_NaN()))); + CORRADE_ASSERT(line.isNormalized(), "Math::Vector::projectedOntoNormalized(): line must be normalized", {}); return line*dot(*this, line); }