瀏覽代碼

Float comparison epsilon

Thomas Buck 10 年之前
父節點
當前提交
e8d9eb4634
共有 8 個文件被更改,包括 31 次插入7 次删除
  1. 1
    0
      ChangeLog
  2. 8
    0
      include/MatMath.h
  3. 1
    2
      include/OpenGLMesh.h
  4. 7
    0
      src/MatMath.cpp
  5. 9
    0
      src/Matrix.cpp
  6. 1
    3
      src/OpenGLMesh.cpp
  7. 1
    1
      src/OpenRaider.cpp
  8. 3
    1
      src/Quaternion.cpp

+ 1
- 0
ChangeLog 查看文件

@@ -8,6 +8,7 @@
8 8
 	[ 20140129 ]
9 9
 	* Removed unused libferit stuff
10 10
 	* Changed code to generate much less warnings
11
+	* Using relative epsilon for float comparison
11 12
 
12 13
 	[ 20140124 ]
13 14
 	* Fixed some TombRaider.cpp warnings

+ 8
- 0
include/MatMath.h 查看文件

@@ -37,6 +37,14 @@ vec_t helIntersectionOfAbstractSpheres(vec3_t centerA, vec_t radiusA, vec3_t cen
37 37
 int helIntersectionOfAbstractSphereAndLine(vec3_t center, vec_t radius, vec3_t posA, vec3_t posB, vec3_t intersectionA, vec3_t intersectionB);
38 38
 
39 39
 /*!
40
+ * \brief Compare two floats with an Epsilon.
41
+ * \param a first float
42
+ * \param b second float
43
+ * \returns true if a and b are probably the same.
44
+ */
45
+bool equalEpsilon(vec_t a, vec_t b);
46
+
47
+/*!
40 48
  * \brief Calculate Intersection of a line and a polygon
41 49
  * \param intersect Where the intersection is stored, if it exists
42 50
  * \param p1 First point of line segment

+ 1
- 2
include/OpenGLMesh.h 查看文件

@@ -105,8 +105,7 @@ public:
105 105
 
106 106
     void allocateVertices(unsigned int n);
107 107
 
108
-    void bufferColorArray(unsigned int colorCount, vec_t *colors,
109
-                                 unsigned int colorWidth);
108
+    void bufferColorArray(unsigned int colorCount, vec_t *colors);
110 109
 
111 110
     void bufferNormalArray(unsigned int normalCount, vec_t *normals);
112 111
 

+ 7
- 0
src/MatMath.cpp 查看文件

@@ -8,11 +8,18 @@
8 8
 
9 9
 #include <stdlib.h>
10 10
 #include <math.h>
11
+#include <float.h>
11 12
 
12 13
 #include <MatMath.h>
13 14
 #include <Vector3d.h>
14 15
 #include <Matrix.h>
15 16
 
17
+bool equalEpsilon(vec_t a, vec_t b) {
18
+    vec_t epsilon = FLT_EPSILON;
19
+    if (fabs(a - b) <= (((fabs(b) > fabs(a)) ? fabs(b) : fabs(a)) * epsilon))
20
+        return true;
21
+    return false;
22
+}
16 23
 
17 24
 vec_t helIntersectionOfAbstractSpheres(vec3_t centerA, vec_t radiusA,
18 25
         vec3_t centerB, vec_t radiusB)

+ 9
- 0
src/Matrix.cpp 查看文件

@@ -254,6 +254,7 @@ void Matrix::print()
254 254
 bool Matrix::isIdentity()
255 255
 {
256 256
     // Hhhmm... floating point using direct comparisions
257
+    /*
257 258
     if (mMatrix[ 0] == 1 && mMatrix[ 1] == 0 && mMatrix[ 2] == 0 &&
258 259
             mMatrix[ 3] == 0 &&    mMatrix[ 4] == 0 && mMatrix[ 5] == 1 &&
259 260
             mMatrix[ 6] == 0 && mMatrix[ 7] == 0 && mMatrix[ 8] == 0 &&
@@ -261,6 +262,14 @@ bool Matrix::isIdentity()
261 262
             mMatrix[12] == 0 && mMatrix[13] == 0 && mMatrix[14] == 0 &&
262 263
             mMatrix[15] == 1)
263 264
         return true;
265
+    */
266
+    if (equalEpsilon(mMatrix[ 0], 1.0) && equalEpsilon(mMatrix[ 1], 0.0) && equalEpsilon(mMatrix[ 2], 0.0) &&
267
+        equalEpsilon(mMatrix[ 3], 0.0) && equalEpsilon(mMatrix[ 4], 0.0) && equalEpsilon(mMatrix[ 5], 1.0) &&
268
+        equalEpsilon(mMatrix[ 6], 0.0) && equalEpsilon(mMatrix[ 7], 0.0) && equalEpsilon(mMatrix[ 8], 0.0) &&
269
+        equalEpsilon(mMatrix[ 9], 0.0) && equalEpsilon(mMatrix[10], 1.0) && equalEpsilon(mMatrix[11], 0.0) &&
270
+        equalEpsilon(mMatrix[12], 0.0) && equalEpsilon(mMatrix[13], 0.0) && equalEpsilon(mMatrix[14], 0.0) &&
271
+        equalEpsilon(mMatrix[15], 1.0))
272
+        return true;
264 273
 
265 274
     return false;
266 275
 }

+ 1
- 3
src/OpenGLMesh.cpp 查看文件

@@ -498,8 +498,7 @@ void OpenGLMesh::allocateVertices(unsigned int n)
498 498
 }
499 499
 
500 500
 
501
-void OpenGLMesh::bufferColorArray(unsigned int colorCount, vec_t *colors,
502
-        unsigned int colorWidth)
501
+void OpenGLMesh::bufferColorArray(unsigned int colorCount, vec_t *colors)
503 502
 {
504 503
     if (mColors)
505 504
     {
@@ -512,7 +511,6 @@ void OpenGLMesh::bufferColorArray(unsigned int colorCount, vec_t *colors,
512 511
         return;
513 512
     }
514 513
 
515
-    //mColorWidth = colorWidth;  // for now assume 4 always
516 514
     mNumColors = colorCount;
517 515
     mColorArray = colors;
518 516
 }

+ 1
- 1
src/OpenRaider.cpp 查看文件

@@ -2440,7 +2440,7 @@ void OpenRaider::processRoom(int index)
2440 2440
 
2441 2441
     rRoom->mesh.bufferVertexArray(vertexCount, (vec_t *)vertexArray);
2442 2442
     rRoom->mesh.bufferNormalArray(normalCount, (vec_t *)normalArray);
2443
-    rRoom->mesh.bufferColorArray(vertexCount, (vec_t *)colorArray, 4);
2443
+    rRoom->mesh.bufferColorArray(vertexCount, (vec_t *)colorArray);
2444 2444
 
2445 2445
     m_tombraider.getRoomTriangles(index, m_texOffset,
2446 2446
             &triCount, &indices, &texCoords, &textures,

+ 3
- 1
src/Quaternion.cpp 查看文件

@@ -125,7 +125,9 @@ Quaternion Quaternion::operator -(const Quaternion &q)
125 125
 
126 126
 bool Quaternion::operator ==(const Quaternion &q)
127 127
 {
128
-    return (mX == q.mX && mY == q.mY && mZ == q.mZ && mW == q.mW);
128
+    //return (mX == q.mX && mY == q.mY && mZ == q.mZ && mW == q.mW);
129
+    return (equalEpsilon(mX, q.mX) && equalEpsilon(mY, q.mY) &&
130
+            equalEpsilon(mZ, q.mZ) && equalEpsilon(mW, q.mW));
129 131
 }
130 132
 
131 133
 

Loading…
取消
儲存