Skip to content

Commit 5a75be2

Browse files
committed
fix float3/4 comparison
1 parent 7666e29 commit 5a75be2

4 files changed

Lines changed: 19 additions & 12 deletions

File tree

rts/System/float3.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "System/float3.h"
44
#include "System/creg/creg_cond.h"
55
#include "System/myMath.h"
6-
#include <cmath> // std::min, std::max, std::fabs
6+
#include <cmath> // std::fabs
7+
#include <algorithm> // std::{min,max}
78

89
CR_BIND(float3, )
910
CR_REG_METADATA(float3, (CR_MEMBER(x), CR_MEMBER(y), CR_MEMBER(z)))
@@ -66,3 +67,9 @@ float3 float3::fabs(const float3 v)
6667
return float3(std::fabs(v.x), std::fabs(v.y), std::fabs(v.z));
6768
}
6869

70+
bool float3::equals(const float3& f, const float3& eps /*= float3(CMP_EPS, CMP_EPS, CMP_EPS)*/) const
71+
{
72+
return math::fabs(x - f.x) <= eps.x * std::max(std::max(math::fabs(x), math::fabs(f.x)), 1.0f)
73+
&& math::fabs(y - f.y) <= eps.y * std::max(std::max(math::fabs(y), math::fabs(f.y)), 1.0f)
74+
&& math::fabs(z - f.z) <= eps.z * std::max(std::max(math::fabs(z), math::fabs(f.z)), 1.0f);
75+
}

rts/System/float3.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,7 @@ class float3
331331
/**
332332
* @see operator==
333333
*/
334-
bool equals(const float3& f, const float3& eps = float3(CMP_EPS, CMP_EPS, CMP_EPS)) const {
335-
return math::fabs(x - f.x) <= math::fabs(eps.x * x)
336-
&& math::fabs(y - f.y) <= math::fabs(eps.y * y)
337-
&& math::fabs(z - f.z) <= math::fabs(eps.z * z);
338-
}
334+
bool equals(const float3& f, const float3& eps = float3(CMP_EPS, CMP_EPS, CMP_EPS)) const;
339335

340336
/**
341337
* @brief dot product

rts/System/float4.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "System/float4.h"
44

55
#include "System/creg/creg_cond.h"
6+
#include <algorithm> // std::{min,max}
67

78
CR_BIND(float4, )
89
CR_REG_METADATA(float4,
@@ -18,3 +19,11 @@ float4::float4(): float3(), w(0.0f)
1819
assert(&z == &y + 1);
1920
assert(&w == &z + 1);
2021
}
22+
23+
bool float4::operator == (const float4& f) const
24+
{
25+
return math::fabs(x - f.x) <= float3::CMP_EPS * std::max(std::max(math::fabs(x), math::fabs(f.x)), 1.0f)
26+
&& math::fabs(y - f.y) <= float3::CMP_EPS * std::max(std::max(math::fabs(y), math::fabs(f.y)), 1.0f)
27+
&& math::fabs(z - f.z) <= float3::CMP_EPS * std::max(std::max(math::fabs(z), math::fabs(f.z)), 1.0f)
28+
&& math::fabs(w - f.w) <= float3::CMP_EPS * std::max(std::max(math::fabs(w), math::fabs(f.w)), 1.0f);
29+
}

rts/System/float4.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ struct float4 : public float3
8282
inline bool operator == (const float3& f) const { return (this->float3::operator == (f)); }
8383
inline bool operator != (const float3& f) const { return (this->float3::operator != (f)); }
8484

85-
inline bool operator == (const float4& f) const {
86-
return math::fabs(x - f.x) <= math::fabs(float3::CMP_EPS * x)
87-
&& math::fabs(y - f.y) <= math::fabs(float3::CMP_EPS * y)
88-
&& math::fabs(z - f.z) <= math::fabs(float3::CMP_EPS * z)
89-
&& math::fabs(w - f.w) <= math::fabs(float3::CMP_EPS * w);
90-
}
85+
bool operator == (const float4& f) const;
9186

9287
inline bool operator != (const float4& f) const {
9388
return !(*this == f);

0 commit comments

Comments
 (0)