Browse Source

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.
pull/87/head
Vladimír Vondruš 12 years ago
parent
commit
00124f65eb
  1. 6
      src/Magnum/Math/Complex.h
  2. 3
      src/Magnum/Math/DualQuaternion.h
  3. 15
      src/Magnum/Math/Quaternion.h
  4. 13
      src/Magnum/Math/Test/ComplexTest.cpp
  5. 3
      src/Magnum/Math/Test/DualComplexTest.cpp
  6. 3
      src/Magnum/Math/Test/DualQuaternionTest.cpp
  7. 33
      src/Magnum/Math/Test/QuaternionTest.cpp
  8. 11
      src/Magnum/Math/Test/VectorTest.cpp
  9. 6
      src/Magnum/Math/Vector.h

6
src/Magnum/Math/Complex.h

@ -29,7 +29,6 @@
* @brief Class @ref Magnum::Math::Complex
*/
#include <limits>
#include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/Debug.h>
@ -80,7 +79,7 @@ template<class T> class Complex {
*/
static Rad<T> angle(const Complex<T>& normalizedA, const Complex<T>& normalizedB) {
CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(),
"Math::Complex::angle(): complex numbers must be normalized", Rad<T>(std::numeric_limits<T>::quiet_NaN()));
"Math::Complex::angle(): complex numbers must be normalized", {});
return Rad<T>(std::acos(normalizedA._real*normalizedB._real + normalizedA._imaginary*normalizedB._imaginary));
}
@ -394,8 +393,7 @@ template<class T> class Complex {
*/
Complex<T> invertedNormalized() const {
CORRADE_ASSERT(isNormalized(),
"Math::Complex::invertedNormalized(): complex number must be normalized",
Complex<T>(std::numeric_limits<T>::quiet_NaN(), {}));
"Math::Complex::invertedNormalized(): complex number must be normalized", {});
return conjugated();
}

3
src/Magnum/Math/DualQuaternion.h

@ -314,8 +314,7 @@ template<class T> class DualQuaternion: public Dual<Quaternion<T>> {
*/
Vector3<T> transformPointNormalized(const Vector3<T>& vector) const {
CORRADE_ASSERT(isNormalized(),
"Math::DualQuaternion::transformPointNormalized(): dual quaternion must be normalized",
Vector3<T>(std::numeric_limits<T>::quiet_NaN()));
"Math::DualQuaternion::transformPointNormalized(): dual quaternion must be normalized", {});
return ((*this)*DualQuaternion<T>(vector)*conjugated()).dual().vector();
}

15
src/Magnum/Math/Quaternion.h

@ -505,19 +505,19 @@ template<class T> Quaternion<T> quaternionFromMatrix(const Matrix3x3<T>& m) {
template<class T> inline Rad<T> Quaternion<T>::angle(const Quaternion<T>& normalizedA, const Quaternion<T>& normalizedB) {
CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(),
"Math::Quaternion::angle(): quaternions must be normalized", Rad<T>(std::numeric_limits<T>::quiet_NaN()));
"Math::Quaternion::angle(): quaternions must be normalized", {});
return Rad<T>(angleInternal(normalizedA, normalizedB));
}
template<class T> inline Quaternion<T> Quaternion<T>::lerp(const Quaternion<T>& normalizedA, const Quaternion<T>& normalizedB, const T t) {
CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(),
"Math::Quaternion::lerp(): quaternions must be normalized", Quaternion<T>({}, std::numeric_limits<T>::quiet_NaN()));
"Math::Quaternion::lerp(): quaternions must be normalized", {});
return ((T(1) - t)*normalizedA + t*normalizedB).normalized();
}
template<class T> inline Quaternion<T> Quaternion<T>::slerp(const Quaternion<T>& normalizedA, const Quaternion<T>& normalizedB, const T t) {
CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(),
"Math::Quaternion::slerp(): quaternions must be normalized", Quaternion<T>({}, std::numeric_limits<T>::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<class T> inline Quaternion<T> Quaternion<T>::fromMatrix(const Matrix3x3
}
template<class T> inline Rad<T> Quaternion<T>::angle() const {
CORRADE_ASSERT(isNormalized(), "Math::Quaternion::angle(): quaternion must be normalized",
Rad<T>(std::numeric_limits<T>::quiet_NaN()));
CORRADE_ASSERT(isNormalized(), "Math::Quaternion::angle(): quaternion must be normalized", {});
return Rad<T>(T(2)*std::acos(_scalar));
}
@ -564,14 +563,12 @@ template<class T> inline Quaternion<T> Quaternion<T>::operator*(const Quaternion
}
template<class T> inline Quaternion<T> Quaternion<T>::invertedNormalized() const {
CORRADE_ASSERT(isNormalized(), "Math::Quaternion::invertedNormalized(): quaternion must be normalized",
Quaternion<T>({}, std::numeric_limits<T>::quiet_NaN()));
CORRADE_ASSERT(isNormalized(), "Math::Quaternion::invertedNormalized(): quaternion must be normalized", {});
return conjugated();
}
template<class T> inline Vector3<T> Quaternion<T>::transformVectorNormalized(const Vector3< T >& vector) const {
CORRADE_ASSERT(isNormalized(), "Math::Quaternion::transformVectorNormalized(): quaternion must be normalized",
Vector3<T>(std::numeric_limits<T>::quiet_NaN()));
CORRADE_ASSERT(isNormalized(), "Math::Quaternion::transformVectorNormalized(): quaternion must be normalized", {});
return ((*this)*Quaternion<T>(vector)*conjugated()).vector();
}

13
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));

3
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();

3
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);

33
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<Float>::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<Float>::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<Float>::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<Float>::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<Float>::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);

11
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(),

6
src/Magnum/Math/Vector.h

@ -30,7 +30,6 @@
*/
#include <cmath>
#include <limits>
#include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/Debug.h>
#include <Corrade/Utility/ConfigurationValue.h>
@ -1199,7 +1198,7 @@ extern template Corrade::Utility::Debug MAGNUM_EXPORT operator<<(Corrade::Utilit
template<std::size_t size, class T> inline Rad<T> Vector<size, T>::angle(const Vector<size, T>& normalizedA, const Vector<size, T>& normalizedB) {
CORRADE_ASSERT(normalizedA.isNormalized() && normalizedB.isNormalized(),
"Math::Vector::angle(): vectors must be normalized", Rad<T>(std::numeric_limits<T>::quiet_NaN()));
"Math::Vector::angle(): vectors must be normalized", {});
return Rad<T>(std::acos(dot(normalizedA, normalizedB)));
}
@ -1249,8 +1248,7 @@ template<std::size_t size, class T> inline Vector<size, T> Vector<size, T>::oper
}
template<std::size_t size, class T> inline Vector<size, T> Vector<size, T>::projectedOntoNormalized(const Vector<size, T>& line) const {
CORRADE_ASSERT(line.isNormalized(), "Math::Vector::projectedOntoNormalized(): line must be normalized",
(Vector<size, T>(std::numeric_limits<T>::quiet_NaN())));
CORRADE_ASSERT(line.isNormalized(), "Math::Vector::projectedOntoNormalized(): line must be normalized", {});
return line*dot(*this, line);
}

Loading…
Cancel
Save