Browse Source

Using TypeTraits::epsilon and TypeTraits::equals instead of EPSILON.

Improves comparison performance on integral Matrix and Vector, fixes NaN
comparison bug (but infinity bug is still present).
pull/279/head
Vladimír Vondruš 15 years ago
parent
commit
8d525be511
  1. 1
      src/Magnum.h
  2. 3
      src/Math/Math.h
  3. 3
      src/Math/Matrix.h
  4. 1
      src/Math/Test/Matrix4Test.cpp
  5. 1
      src/Math/Test/VectorTest.cpp
  6. 5
      src/Math/Vector.h
  7. 4
      src/MeshTools/Clean.h

1
src/Magnum.h

@ -21,6 +21,7 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "Math/Math.h"
#include "Math/Matrix4.h" #include "Math/Matrix4.h"
#include "Math/Vector2.h" #include "Math/Vector2.h"

3
src/Math/Math.h

@ -33,9 +33,6 @@ namespace Math {
/** @brief Pi */ /** @brief Pi */
#define PI 3.1415926535 #define PI 3.1415926535
/** @brief Maximal tolerance when comparing floats */
#define EPSILON 1.0e-6
/** /**
* @brief Integral power * @brief Integral power
* *

3
src/Math/Matrix.h

@ -102,10 +102,9 @@ template<class T, size_t size> class Matrix {
/** @brief Equality operator */ /** @brief Equality operator */
inline bool operator==(const Matrix<T, size>& other) const { inline bool operator==(const Matrix<T, size>& other) const {
/** @bug NaN comparisons! */
for(size_t row = 0; row != size; ++row) { for(size_t row = 0; row != size; ++row) {
for(size_t col = 0; col != size; ++col) for(size_t col = 0; col != size; ++col)
if(std::abs(at(row, col) - other.at(row, col)) >= EPSILON) return false; if(!TypeTraits<T>::equals(at(row, col), other.at(row, col))) return false;
} }
return true; return true;

1
src/Math/Test/Matrix4Test.cpp

@ -19,6 +19,7 @@
#include <QtTest/QTest> #include <QtTest/QTest>
#include "Matrix4.h" #include "Matrix4.h"
#include "Math.h"
QTEST_APPLESS_MAIN(Magnum::Math::Test::Matrix4Test) QTEST_APPLESS_MAIN(Magnum::Math::Test::Matrix4Test)

1
src/Math/Test/VectorTest.cpp

@ -19,6 +19,7 @@
#include <QtTest/QTest> #include <QtTest/QTest>
#include "Vector.h" #include "Vector.h"
#include "Math.h"
QTEST_APPLESS_MAIN(Magnum::Math::Test::VectorTest) QTEST_APPLESS_MAIN(Magnum::Math::Test::VectorTest)

5
src/Math/Vector.h

@ -23,7 +23,7 @@
#include <cmath> #include <cmath>
#include "Utility/Debug.h" #include "Utility/Debug.h"
#include "Math.h" #include "TypeTraits.h"
namespace Magnum { namespace Math { namespace Magnum { namespace Math {
@ -91,9 +91,8 @@ template<class T, size_t size> class Vector {
/** @brief Equality operator */ /** @brief Equality operator */
inline bool operator==(const Vector<T, size>& other) const { inline bool operator==(const Vector<T, size>& other) const {
/** @bug NaN comparisons! */
for(size_t pos = 0; pos != size; ++pos) for(size_t pos = 0; pos != size; ++pos)
if(std::abs(at(pos) - other.at(pos)) >= EPSILON) return false; if(!TypeTraits<T>::equals(at(pos), other.at(pos))) return false;
return true; return true;
} }

4
src/MeshTools/Clean.h

@ -38,7 +38,7 @@ template<class Vertex, size_t vertexSize = Vertex::Size> class Clean: public Abs
* *
* See clean() for full documentation. * See clean() for full documentation.
*/ */
void run(typename Vertex::Type epsilon = EPSILON) { void run(typename Vertex::Type epsilon = TypeTraits<typename Vertex::Type>::epsilon()) {
if(this->indices.empty()) return; if(this->indices.empty()) return;
/* Get mesh bounds */ /* Get mesh bounds */
@ -148,7 +148,7 @@ to explicitly specify both of them:
MeshTools::clean<T, 3>(builder, epsilon); MeshTools::clean<T, 3>(builder, epsilon);
@endcode @endcode
*/ */
template<class Vertex, size_t vertexSize = Vertex::Size> inline void clean(MeshBuilder<Vertex>& builder, typename Vertex::Type epsilon = EPSILON) { template<class Vertex, size_t vertexSize = Vertex::Size> inline void clean(MeshBuilder<Vertex>& builder, typename Vertex::Type epsilon = TypeTraits<typename Vertex::Type>::epsilon()) {
Clean<Vertex, vertexSize>(builder).run(epsilon); Clean<Vertex, vertexSize>(builder).run(epsilon);
} }

Loading…
Cancel
Save