diff --git a/basic-geometry/matrix2x2.h b/basic-geometry/matrix2x2.h index 55d2b91..a6f8fce 100644 --- a/basic-geometry/matrix2x2.h +++ b/basic-geometry/matrix2x2.h @@ -185,16 +185,12 @@ inline double bgc_matrix2x2_get_determinant_fp64(const BgcMatrix2x2FP64* matrix) inline int bgc_matrix2x2_is_singular_fp32(const BgcMatrix2x2FP32* matrix) { - const float determinant = bgc_matrix2x2_get_determinant_fp32(matrix); - - return -BGC_EPSYLON_FP32 <= determinant && determinant <= BGC_EPSYLON_FP32; + return bgc_is_zero_fp32(bgc_matrix2x2_get_determinant_fp32(matrix)); } inline int bgc_matrix2x2_is_singular_fp64(const BgcMatrix2x2FP64* matrix) { - const double determinant = bgc_matrix2x2_get_determinant_fp64(matrix); - - return -BGC_EPSYLON_FP64 <= determinant && determinant <= BGC_EPSYLON_FP64; + return bgc_is_zero_fp64(bgc_matrix2x2_get_determinant_fp64(matrix)); } // =============== Transposition ================ // @@ -219,7 +215,7 @@ inline int bgc_matrix2x2_invert_fp32(BgcMatrix2x2FP32* matrix) { const float determinant = bgc_matrix2x2_get_determinant_fp32(matrix); - if (-BGC_EPSYLON_FP32 <= determinant && determinant <= BGC_EPSYLON_FP32) { + if (bgc_is_zero_fp32(determinant)) { return 0; } @@ -244,7 +240,7 @@ inline int bgc_matrix2x2_invert_fp64(BgcMatrix2x2FP64* matrix) { const double determinant = bgc_matrix2x2_get_determinant_fp64(matrix); - if (-BGC_EPSYLON_FP64 <= determinant && determinant <= BGC_EPSYLON_FP64) { + if (bgc_is_zero_fp64(determinant)) { return 0; } @@ -295,7 +291,7 @@ inline int bgc_matrix2x2_set_inverted_fp32(const BgcMatrix2x2FP32* from, BgcMatr { const float determinant = bgc_matrix2x2_get_determinant_fp32(from); - if (-BGC_EPSYLON_FP32 <= determinant && determinant <= BGC_EPSYLON_FP32) { + if (bgc_is_zero_fp32(determinant)) { return 0; } @@ -320,7 +316,7 @@ inline int bgc_matrix2x2_set_inverted_fp64(const BgcMatrix2x2FP64* from, BgcMatr { const double determinant = bgc_matrix2x2_get_determinant_fp64(from); - if (-BGC_EPSYLON_FP64 <= determinant && determinant <= BGC_EPSYLON_FP64) { + if (bgc_is_zero_fp64(determinant)) { return 0; } diff --git a/basic-geometry/matrix3x3.h b/basic-geometry/matrix3x3.h index ec62db3..3454b04 100644 --- a/basic-geometry/matrix3x3.h +++ b/basic-geometry/matrix3x3.h @@ -264,16 +264,12 @@ inline double bgc_matrix3x3_get_determinant_fp64(const BgcMatrix3x3FP64* matrix) inline int bgc_matrix3x3_is_singular_fp32(const BgcMatrix3x3FP32* matrix) { - const float determinant = bgc_matrix3x3_get_determinant_fp32(matrix); - - return -BGC_EPSYLON_FP32 <= determinant && determinant <= BGC_EPSYLON_FP32; + return bgc_is_zero_fp32(bgc_matrix3x3_get_determinant_fp32(matrix)); } inline int bgc_matrix3x3_is_singular_fp64(const BgcMatrix3x3FP64* matrix) { - const double determinant = bgc_matrix3x3_get_determinant_fp64(matrix); - - return -BGC_EPSYLON_FP64 <= determinant && determinant <= BGC_EPSYLON_FP64; + return bgc_is_zero_fp64(bgc_matrix3x3_get_determinant_fp64(matrix)); } // ================= Inversion ================== // diff --git a/basic-geometry/quaternion.h b/basic-geometry/quaternion.h index 28af837..b4f2c7f 100644 --- a/basic-geometry/quaternion.h +++ b/basic-geometry/quaternion.h @@ -225,7 +225,7 @@ inline int bgc_quaternion_normalize_fp32(BgcQuaternionFP32* quaternion) { const float square_modulus = bgc_quaternion_get_square_modulus_fp32(quaternion); - if (1.0f - BGC_TWO_EPSYLON_FP32 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP32) { + if (bgc_is_sqare_value_unit_fp32(square_modulus)) { return 1; } @@ -248,7 +248,7 @@ inline int bgc_quaternion_normalize_fp64(BgcQuaternionFP64* quaternion) { const double square_modulus = bgc_quaternion_get_square_modulus_fp64(quaternion); - if (1.0 - BGC_TWO_EPSYLON_FP64 <= square_modulus && square_modulus <= 1.0 + BGC_TWO_EPSYLON_FP64) { + if (bgc_is_sqare_value_unit_fp64(square_modulus)) { return 1; } @@ -278,7 +278,7 @@ inline void bgc_quaternion_get_rotation_matrix_fp32(const BgcQuaternionFP32* qua const float square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); - if (-BGC_EPSYLON_FP32 <= square_modulus && square_modulus <= BGC_EPSYLON_FP32) + if (bgc_is_zero_fp32(square_modulus)) { bgc_matrix3x3_set_to_identity_fp32(matrix); return; @@ -316,7 +316,7 @@ inline void bgc_quaternion_get_rotation_matrix_fp64(const BgcQuaternionFP64* qua const double square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); - if (-BGC_EPSYLON_FP64 <= square_modulus && square_modulus <= BGC_EPSYLON_FP64) + if (bgc_is_zero_fp64(square_modulus)) { bgc_matrix3x3_set_to_identity_fp64(matrix); return; diff --git a/basic-geometry/tangent.h b/basic-geometry/tangent.h index ea49f79..a323083 100644 --- a/basic-geometry/tangent.h +++ b/basic-geometry/tangent.h @@ -68,11 +68,9 @@ inline void bgc_tangent_set_values_fp32(const float x1, const float x2, BgcTange twin->cos = x1; twin->sin = x2; - if (1.0f - BGC_TWO_EPSYLON_FP32 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP32) { - return; + if (!bgc_is_sqare_value_unit_fp32(square_modulus)) { + _bgc_tangent_normalize_fp32(square_modulus, twin); } - - _bgc_tangent_normalize_fp32(square_modulus, twin); } inline void bgc_tangent_set_values_fp64(const double x1, const double x2, BgcTangentFP64* tangent) @@ -84,11 +82,9 @@ inline void bgc_tangent_set_values_fp64(const double x1, const double x2, BgcTan twin->cos = x1; twin->sin = x2; - if (1.0 - BGC_TWO_EPSYLON_FP64 <= square_modulus && square_modulus <= 1.0 + BGC_TWO_EPSYLON_FP64) { - return; + if (!bgc_is_sqare_value_unit_fp64(square_modulus)) { + _bgc_tangent_normalize_fp64(square_modulus, twin); } - - _bgc_tangent_normalize_fp64(square_modulus, twin); } // ==================== Copy ==================== // @@ -247,19 +243,19 @@ inline void bgc_tangent_make_reverse_matrix_fp64(const BgcTangentFP64* tangent, inline float bgc_tangent_get_angle_fp32(const BgcTangentFP32* tangent, const BgcAngleUnitEnum unit) { - if (tangent->cos >= 1.0f - BGC_TWO_EPSYLON_FP32) { + if (tangent->cos >= 1.0f - BGC_EPSYLON_FP32) { return 0.0f; } - if (tangent->cos <= -1.0f + BGC_TWO_EPSYLON_FP32) { + if (tangent->cos <= -1.0f + BGC_EPSYLON_FP32) { return bgc_angle_get_half_circle_fp32(unit); } - if (tangent->sin >= 1.0f - BGC_TWO_EPSYLON_FP32) { + if (tangent->sin >= 1.0f - BGC_EPSYLON_FP32) { return bgc_angle_get_quater_circle_fp32(unit); } - if (tangent->sin <= -1.0f + BGC_TWO_EPSYLON_FP32) { + if (tangent->sin <= -1.0f + BGC_EPSYLON_FP32) { return 0.75f * bgc_angle_get_full_circle_fp32(unit); } @@ -268,19 +264,19 @@ inline float bgc_tangent_get_angle_fp32(const BgcTangentFP32* tangent, const Bgc inline double bgc_tangent_get_angle_fp64(const BgcTangentFP64* tangent, const BgcAngleUnitEnum unit) { - if (tangent->cos >= 1.0 - BGC_TWO_EPSYLON_FP64) { + if (tangent->cos >= 1.0 - BGC_EPSYLON_FP64) { return 0.0; } - if (tangent->cos <= -1.0 + BGC_TWO_EPSYLON_FP64) { + if (tangent->cos <= -1.0 + BGC_EPSYLON_FP64) { return bgc_angle_get_half_circle_fp64(unit); } - if (tangent->sin >= 1.0 - BGC_TWO_EPSYLON_FP64) { + if (tangent->sin >= 1.0 - BGC_EPSYLON_FP64) { return bgc_angle_get_quater_circle_fp64(unit); } - if (tangent->sin <= -1.0 + BGC_TWO_EPSYLON_FP64) { + if (tangent->sin <= -1.0 + BGC_EPSYLON_FP64) { return 0.75 * bgc_angle_get_full_circle_fp64(unit); } diff --git a/basic-geometry/utilities.c b/basic-geometry/utilities.c index 3f56fca..30e034f 100644 --- a/basic-geometry/utilities.c +++ b/basic-geometry/utilities.c @@ -1,4 +1,14 @@ #include "utilities.h" + +extern inline int bgc_is_zero_fp32(const float square_value); +extern inline int bgc_is_zero_fp64(const double square_value); + +extern inline int bgc_is_unit_fp32(const float square_value); +extern inline int bgc_is_unit_fp64(const double square_value); + +extern inline int bgc_is_sqare_value_unit_fp32(const float square_value); +extern inline int bgc_is_sqare_value_unit_fp64(const double square_value); + extern inline int bgc_are_equal_fp32(const float value1, const float value2); extern inline int bgc_are_equal_fp64(const double value1, const double value2); diff --git a/basic-geometry/utilities.h b/basic-geometry/utilities.h index d49feeb..d2fe3f4 100644 --- a/basic-geometry/utilities.h +++ b/basic-geometry/utilities.h @@ -4,7 +4,6 @@ #define BGC_EPSYLON_EFFECTIVENESS_LIMIT_FP32 10.0f #define BGC_EPSYLON_FP32 5E-7f -#define BGC_TWO_EPSYLON_FP32 1E-6f #define BGC_SQUARE_EPSYLON_FP32 2.5E-13f #define BGC_ONE_THIRD_FP32 0.333333333f @@ -17,7 +16,6 @@ #define BGC_EPSYLON_EFFECTIVENESS_LIMIT_FP64 10.0 #define BGC_EPSYLON_FP64 5E-14 -#define BGC_TWO_EPSYLON_FP64 1E-13 #define BGC_SQUARE_EPSYLON_FP64 2.5E-27 #define BGC_ONE_THIRD_FP64 0.333333333333333333 @@ -27,6 +25,39 @@ #define BGC_GOLDEN_RATIO_HIGH_FP64 1.61803398874989485 #define BGC_GOLDEN_RATIO_LOW_FP64 0.61803398874989485 +inline int bgc_is_zero_fp32(const float square_value) +{ + return (-BGC_EPSYLON_FP32 <= square_value) && (square_value <= BGC_EPSYLON_FP32); +} + +inline int bgc_is_zero_fp64(const double square_value) +{ + return (-BGC_EPSYLON_FP64 <= square_value) && (square_value <= BGC_EPSYLON_FP64); +} + + +inline int bgc_is_unit_fp32(const float square_value) +{ + return (1.0f - BGC_EPSYLON_FP32 <= square_value) && (square_value <= 1.0f + BGC_EPSYLON_FP32); +} + +inline int bgc_is_unit_fp64(const double square_value) +{ + return (1.0 - BGC_EPSYLON_FP64 <= square_value) && (square_value <= 1.0 + BGC_EPSYLON_FP64); +} + + +inline int bgc_is_sqare_value_unit_fp32(const float square_value) +{ + return (1.0f - 2.0f * BGC_EPSYLON_FP32 <= square_value) && (square_value <= 1.0f + 2.0f * BGC_EPSYLON_FP32); +} + +inline int bgc_is_sqare_value_unit_fp64(const double square_value) +{ + return (1.0 - 2.0 * BGC_EPSYLON_FP64 <= square_value) && (square_value <= 1.0 + 2.0 * BGC_EPSYLON_FP64); +} + + inline int bgc_are_equal_fp32(const float value1, const float value2) { if (-BGC_EPSYLON_EFFECTIVENESS_LIMIT_FP32 < value1 && value1 < BGC_EPSYLON_EFFECTIVENESS_LIMIT_FP32) { diff --git a/basic-geometry/vector2.h b/basic-geometry/vector2.h index 490bcae..39c6e04 100644 --- a/basic-geometry/vector2.h +++ b/basic-geometry/vector2.h @@ -162,16 +162,12 @@ inline int bgc_vector2_is_zero_fp64(const BgcVector2FP64* vector) inline int bgc_vector2_is_unit_fp32(const BgcVector2FP32* vector) { - const float square_modulus = bgc_vector2_get_square_modulus_fp32(vector); - - return 1.0f - BGC_TWO_EPSYLON_FP32 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP32; + return bgc_is_sqare_value_unit_fp32(bgc_vector2_get_square_modulus_fp32(vector)); } inline int bgc_vector2_is_unit_fp64(const BgcVector2FP64* vector) { - const double square_modulus = bgc_vector2_get_square_modulus_fp64(vector); - - return 1.0f - BGC_TWO_EPSYLON_FP64 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP64; + return bgc_is_sqare_value_unit_fp64(bgc_vector2_get_square_modulus_fp64(vector)); } // ==================== Add ===================== // @@ -320,7 +316,7 @@ inline int bgc_vector2_normalize_fp32(BgcVector2FP32* vector) { const float square_modulus = bgc_vector2_get_square_modulus_fp32(vector); - if (1.0f - BGC_TWO_EPSYLON_FP32 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP32) { + if (bgc_is_sqare_value_unit_fp32(square_modulus)) { return 1; } @@ -337,7 +333,7 @@ inline int bgc_vector2_normalize_fp64(BgcVector2FP64* vector) { const double square_modulus = bgc_vector2_get_square_modulus_fp64(vector); - if (1.0 - BGC_TWO_EPSYLON_FP64 <= square_modulus && square_modulus <= 1.0 + BGC_TWO_EPSYLON_FP64) { + if (bgc_is_sqare_value_unit_fp64(square_modulus)) { return 1; } diff --git a/basic-geometry/vector3.h b/basic-geometry/vector3.h index 146a648..16f6fd7 100644 --- a/basic-geometry/vector3.h +++ b/basic-geometry/vector3.h @@ -198,16 +198,12 @@ inline int bgc_vector3_is_zero_fp64(const BgcVector3FP64* vector) inline int bgc_vector3_is_unit_fp32(const BgcVector3FP32* vector) { - const float square_modulus = bgc_vector3_get_square_modulus_fp32(vector); - - return 1.0f - BGC_TWO_EPSYLON_FP32 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP32; + return bgc_is_sqare_value_unit_fp32(bgc_vector3_get_square_modulus_fp32(vector)); } inline int bgc_vector3_is_unit_fp64(const BgcVector3FP64* vector) { - const double square_modulus = bgc_vector3_get_square_modulus_fp64(vector); - - return 1.0f - BGC_TWO_EPSYLON_FP64 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP64; + return bgc_is_sqare_value_unit_fp64(bgc_vector3_get_square_modulus_fp64(vector)); } // ==================== Add ===================== // @@ -398,7 +394,7 @@ inline int bgc_vector3_normalize_fp32(BgcVector3FP32* vector) { const float square_modulus = bgc_vector3_get_square_modulus_fp32(vector); - if (1.0f - BGC_TWO_EPSYLON_FP32 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP32) { + if (bgc_is_sqare_value_unit_fp32(square_modulus)) { return 1; } @@ -415,7 +411,7 @@ inline int bgc_vector3_normalize_fp64(BgcVector3FP64* vector) { const double square_modulus = bgc_vector3_get_square_modulus_fp64(vector); - if (1.0 - BGC_TWO_EPSYLON_FP64 <= square_modulus && square_modulus <= 1.0 + BGC_TWO_EPSYLON_FP64) { + if (bgc_is_sqare_value_unit_fp64(square_modulus)) { return 1; } diff --git a/basic-geometry/versor.c b/basic-geometry/versor.c index a5055fb..ce57261 100644 --- a/basic-geometry/versor.c +++ b/basic-geometry/versor.c @@ -122,7 +122,7 @@ void bgc_versor_set_crude_turn_fp32(const float x1, const float x2, const float const float sine = sinf(half_angle); - if (-BGC_EPSYLON_FP32 <= sine && sine <= BGC_EPSYLON_FP32) { + if (bgc_is_zero_fp32(sine)) { bgc_versor_reset_fp32(result); return; } @@ -145,7 +145,7 @@ void bgc_versor_set_crude_turn_fp64(const double x1, const double x2, const doub const double sine = sin(half_angle); - if (-BGC_EPSYLON_FP64 <= sine && sine <= BGC_EPSYLON_FP64) { + if (bgc_is_zero_fp64(sine)) { bgc_versor_reset_fp64(result); return; } diff --git a/basic-geometry/versor.h b/basic-geometry/versor.h index 7b815ea..06f5bdb 100644 --- a/basic-geometry/versor.h +++ b/basic-geometry/versor.h @@ -73,11 +73,9 @@ inline void bgc_versor_set_values_fp32(const float s0, const float x1, const flo const float square_modulus = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); - if (1.0f - BGC_TWO_EPSYLON_FP32 <= square_modulus && square_modulus <= 1.0f + BGC_TWO_EPSYLON_FP32) { - return; + if (!bgc_is_sqare_value_unit_fp32(square_modulus)) { + _bgc_versor_normalize_fp32(square_modulus, twin); } - - _bgc_versor_normalize_fp32(square_modulus, twin); } inline void bgc_versor_set_values_fp64(const double s0, const double x1, const double x2, const double x3, BgcVersorFP64* versor) @@ -91,11 +89,9 @@ inline void bgc_versor_set_values_fp64(const double s0, const double x1, const d const double square_modulus = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); - if (1.0 - BGC_TWO_EPSYLON_FP64 <= square_modulus && square_modulus <= 1.0 + BGC_TWO_EPSYLON_FP64) { - return; + if (!bgc_is_sqare_value_unit_fp64(square_modulus)) { + _bgc_versor_normalize_fp64(square_modulus, twin); } - - _bgc_versor_normalize_fp64(square_modulus, twin); } // ==================== Copy ==================== // @@ -200,12 +196,12 @@ inline void bgc_versor_set_rotation_fp64(const BgcRotation3FP64* rotation, BgcVe inline int bgc_versor_is_idle_fp32(const BgcVersorFP32* versor) { - return 1.0f - BGC_EPSYLON_FP32 <= versor->s0 || versor->s0 <= -(1.0 - BGC_EPSYLON_FP32); + return (1.0f - BGC_EPSYLON_FP32 <= versor->s0) | (versor->s0 <= -(1.0 - BGC_EPSYLON_FP32)); } inline int bgc_versor_is_idle_fp64(const BgcVersorFP64* versor) { - return 1.0 - BGC_EPSYLON_FP64 <= versor->s0 || versor->s0 <= -(1.0 - BGC_EPSYLON_FP64); + return (1.0 - BGC_EPSYLON_FP64 <= versor->s0) | (versor->s0 <= -(1.0 - BGC_EPSYLON_FP64)); } // ============= Copy to twin type ============== // @@ -296,7 +292,7 @@ inline void bgc_versor_set_shortened_fp64(const BgcVersorFP64* versor, BgcVersor twin->x1 = -versor->x1; twin->x2 = -versor->x2; twin->x3 = -versor->x3; - } +} // ================= Inversion ================== //