From 8d525be511de7ad8719e7ba38ba8542ae079d01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 30 Jan 2012 01:50:49 +0100 Subject: [PATCH] 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). --- src/Magnum.h | 1 + src/Math/Math.h | 3 --- src/Math/Matrix.h | 3 +-- src/Math/Test/Matrix4Test.cpp | 1 + src/Math/Test/VectorTest.cpp | 1 + src/Math/Vector.h | 5 ++--- src/MeshTools/Clean.h | 4 ++-- 7 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Magnum.h b/src/Magnum.h index 1922d8fc7..4c7d3f26a 100644 --- a/src/Magnum.h +++ b/src/Magnum.h @@ -21,6 +21,7 @@ #include +#include "Math/Math.h" #include "Math/Matrix4.h" #include "Math/Vector2.h" diff --git a/src/Math/Math.h b/src/Math/Math.h index 6043ed800..e4ffdc5a8 100644 --- a/src/Math/Math.h +++ b/src/Math/Math.h @@ -33,9 +33,6 @@ namespace Math { /** @brief Pi */ #define PI 3.1415926535 -/** @brief Maximal tolerance when comparing floats */ -#define EPSILON 1.0e-6 - /** * @brief Integral power * diff --git a/src/Math/Matrix.h b/src/Math/Matrix.h index 083f99126..bcf0d2ec0 100644 --- a/src/Math/Matrix.h +++ b/src/Math/Matrix.h @@ -102,10 +102,9 @@ template class Matrix { /** @brief Equality operator */ inline bool operator==(const Matrix& other) const { - /** @bug NaN comparisons! */ for(size_t row = 0; row != size; ++row) { for(size_t col = 0; col != size; ++col) - if(std::abs(at(row, col) - other.at(row, col)) >= EPSILON) return false; + if(!TypeTraits::equals(at(row, col), other.at(row, col))) return false; } return true; diff --git a/src/Math/Test/Matrix4Test.cpp b/src/Math/Test/Matrix4Test.cpp index 362cce00b..7cc38adcd 100644 --- a/src/Math/Test/Matrix4Test.cpp +++ b/src/Math/Test/Matrix4Test.cpp @@ -19,6 +19,7 @@ #include #include "Matrix4.h" +#include "Math.h" QTEST_APPLESS_MAIN(Magnum::Math::Test::Matrix4Test) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 06ed871af..f5be2e503 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -19,6 +19,7 @@ #include #include "Vector.h" +#include "Math.h" QTEST_APPLESS_MAIN(Magnum::Math::Test::VectorTest) diff --git a/src/Math/Vector.h b/src/Math/Vector.h index e1e9de98f..2b9fb6c87 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -23,7 +23,7 @@ #include #include "Utility/Debug.h" -#include "Math.h" +#include "TypeTraits.h" namespace Magnum { namespace Math { @@ -91,9 +91,8 @@ template class Vector { /** @brief Equality operator */ inline bool operator==(const Vector& other) const { - /** @bug NaN comparisons! */ for(size_t pos = 0; pos != size; ++pos) - if(std::abs(at(pos) - other.at(pos)) >= EPSILON) return false; + if(!TypeTraits::equals(at(pos), other.at(pos))) return false; return true; } diff --git a/src/MeshTools/Clean.h b/src/MeshTools/Clean.h index f8fbb98e4..b3331095d 100644 --- a/src/MeshTools/Clean.h +++ b/src/MeshTools/Clean.h @@ -38,7 +38,7 @@ template class Clean: public Abs * * See clean() for full documentation. */ - void run(typename Vertex::Type epsilon = EPSILON) { + void run(typename Vertex::Type epsilon = TypeTraits::epsilon()) { if(this->indices.empty()) return; /* Get mesh bounds */ @@ -148,7 +148,7 @@ to explicitly specify both of them: MeshTools::clean(builder, epsilon); @endcode */ -template inline void clean(MeshBuilder& builder, typename Vertex::Type epsilon = EPSILON) { +template inline void clean(MeshBuilder& builder, typename Vertex::Type epsilon = TypeTraits::epsilon()) { Clean(builder).run(epsilon); }