реорганизация функций проекта, уменьшение дублирующегося кода

This commit is contained in:
Andrey Pokidov 2025-02-04 13:20:46 +07:00
parent 6c0ae92ed4
commit 07623b2aa6
10 changed files with 84 additions and 67 deletions

View file

@ -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;
}

View file

@ -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 ================== //

View file

@ -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;

View file

@ -68,12 +68,10 @@ 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);
}
}
inline void bgc_tangent_set_values_fp64(const double x1, const double x2, BgcTangentFP64* tangent)
{
@ -84,12 +82,10 @@ 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);
}
}
// ==================== 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);
}

View file

@ -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);

View file

@ -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) {

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -73,12 +73,10 @@ 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);
}
}
inline void bgc_versor_set_values_fp64(const double s0, const double x1, const double x2, const double x3, BgcVersorFP64* versor)
{
@ -91,12 +89,10 @@ 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);
}
}
// ==================== 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 ============== //