diff --git a/inc/hgl/math/MathConst.h b/inc/hgl/math/MathConst.h index 93f12fd..88adaba 100644 --- a/inc/hgl/math/MathConst.h +++ b/inc/hgl/math/MathConst.h @@ -14,15 +14,15 @@ namespace hgl constexpr const double HGL_DOUBLE_MAX =std::numeric_limits::max(); ///<最大双精度浮点数 constexpr const double HGL_DOUBLE_EPSILON =std::numeric_limits::epsilon(); ///<双精度浮点数精度最小值 - template bool IsNearlyZero(const T value); + constexpr const float HGL_HALF_FLOAT_ERROR=0.001f; ///<半精度浮点数最小误差值 + constexpr const float HGL_FLOAT_ERROR =0.0001f; ///<浮点数最小误差值 + constexpr const double HGL_DOUBLE_ERROR =0.00000001; ///<双精度浮点数最小误差值 - template<> inline bool IsNearlyZero(const float value){return(fabsf(value) <= HGL_FLOAT_EPSILON);} - template<> inline bool IsNearlyZero(const double value){return(fabs(value) <= HGL_DOUBLE_EPSILON);} + inline bool IsNearlyZero(const float value,const float err=HGL_FLOAT_ERROR){return(fabsf(value) <= err);} + inline bool IsNearlyZero(const double value,const double err=HGL_DOUBLE_ERROR){return(fabs(value) <= err);} - template bool IsNearlyEqual(const T a,const T b); - - template<> inline bool IsNearlyEqual(const float a,const float b){return(fabsf(a - b) <= HGL_FLOAT_EPSILON);} - template<> inline bool IsNearlyEqual(const double a,const double b){return(fabs(a - b) <= HGL_DOUBLE_EPSILON); } + inline bool IsNearlyEqual(const float a,const float b,const float err=HGL_FLOAT_ERROR){return(fabsf(a - b) <= err);} + inline bool IsNearlyEqual(const double a,const double b,const double err=HGL_DOUBLE_ERROR){return(fabs(a - b) <= err); } constexpr const double HGL_E =2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274; //欧拉数(自然对数的底数) constexpr const double HGL_LOG2E =1.44269504088896340736; diff --git a/inc/hgl/math/Matrix.h b/inc/hgl/math/Matrix.h index 801a49c..5c84df0 100644 --- a/inc/hgl/math/Matrix.h +++ b/inc/hgl/math/Matrix.h @@ -20,14 +20,14 @@ namespace hgl #define DEFINE_MATRIX(num) using Matrix##num##f=glm::mat##num; \ const Matrix##num##f Identity##num##f=Matrix##num##f(1.0f); \ inline bool IsIdentityMatrix(const Matrix##num##f &m){return(hgl_cmp(m,Identity##num##f)==0);} \ - inline bool IsNearlyEqual(const Matrix##num##f &m1,const Matrix##num##f &m2) \ + inline bool IsNearlyEqual(const Matrix##num##f &m1,const Matrix##num##f &m2,const float err=HGL_FLOAT_ERROR) \ { \ float *f1=(float *)&m1;\ float *f2=(float *)&m2;\ \ for(int i=0;i())); + return glm::all(glm::epsilonEqual(q1,q2,err)); } inline Matrix4f inverse(const Matrix4f &m) diff --git a/inc/hgl/math/Vector.h b/inc/hgl/math/Vector.h index 766a01e..6382b4e 100644 --- a/inc/hgl/math/Vector.h +++ b/inc/hgl/math/Vector.h @@ -263,37 +263,38 @@ namespace hgl return from + (to - from) * alpha; } - inline bool IsNearlyEqual(const float v1,const float v2,const float epsilon=HGL_FLOAT_EPSILON) + inline bool IsNearlyEqual(const Vector2f &v1,const Vector2f &v2,const float err=HGL_FLOAT_ERROR) { - return glm::epsilonEqual(v1,v2,epsilon); + if(!IsNearlyEqual(v1.x,v2.x,err))return(false); + if(!IsNearlyEqual(v1.y,v2.y,err))return(false); + + return(true); } - inline bool IsNearlyEqual(const Vector2f &v1,const Vector2f &v2,const float epsilon=HGL_FLOAT_EPSILON) + inline bool IsNearlyEqual(const Vector2d &v1,const Vector2d &v2,const double err=HGL_DOUBLE_ERROR) { - glm::bvec2 result=glm::epsilonEqual(v1,v2,epsilon); + if(!IsNearlyEqual(v1.x,v2.x,err))return(false); + if(!IsNearlyEqual(v1.y,v2.y,err))return(false); - return result.x && result.y; + return(true); } - inline bool IsNearlyEqual(const Vector2d &v1,const Vector2d &v2,const double epsilon=HGL_DOUBLE_EPSILON) + inline bool IsNearlyEqual(const Vector3f &v1,const Vector3f &v2,const float err=HGL_FLOAT_ERROR) { - glm::bvec2 result=glm::epsilonEqual(v1,v2,epsilon); + if(!IsNearlyEqual(v1.x,v2.x,err))return(false); + if(!IsNearlyEqual(v1.y,v2.y,err))return(false); + if(!IsNearlyEqual(v1.z,v2.z,err))return(false); - return result.x && result.y; + return(true); } - inline bool IsNearlyEqual(const Vector3f &v1,const Vector3f &v2,const float epsilon=HGL_FLOAT_EPSILON) + inline bool IsNearlyEqual(const Vector3d &v1,const Vector3d &v2,const double err=HGL_DOUBLE_ERROR) { - glm::bvec3 result=glm::epsilonEqual(v1,v2,epsilon); + if(!IsNearlyEqual(v1.x,v2.x,err))return(false); + if(!IsNearlyEqual(v1.y,v2.y,err))return(false); + if(!IsNearlyEqual(v1.z,v2.z,err))return(false); - return result.x && result.y && result.z; - } - - inline bool IsNearlyEqual(const Vector3d &v1,const Vector3d &v2,const double epsilon=HGL_DOUBLE_EPSILON) - { - glm::bvec3 result=glm::epsilonEqual(v1,v2,epsilon); - - return result.x && result.y && result.z; + return(true); } template