From 34ee460873d0972788e037806bb43d6e643c32fa Mon Sep 17 00:00:00 2001 From: Andrey Pokidov Date: Wed, 26 Feb 2025 00:25:17 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B8=D0=B7=D0=B1=D1=8B=D1=82=D0=BE=D1=87=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9,=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=84?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BF=D0=BB=D0=B5=D0=BA=D1=81=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D1=87=D0=B8=D1=81=D0=B5=D0=BB=20=D0=B8=20=D0=BA=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D1=80=D0=BD=D0=B8=D0=BE=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/vector2/vector2_is_zero.c | 2 +- basic-geometry/matrix2x2.c | 23 +- basic-geometry/matrix2x2.h | 160 ++---- basic-geometry/matrix2x3.c | 14 +- basic-geometry/matrix2x3.h | 109 ++-- basic-geometry/matrix3x2.c | 14 +- basic-geometry/matrix3x2.h | 104 ++-- basic-geometry/matrix3x3.c | 135 +---- basic-geometry/matrix3x3.h | 180 +++---- basic-geometry/quaternion.c | 36 +- basic-geometry/quaternion.h | 508 ++++++++++-------- basic-geometry/rotation3.h | 8 +- basic-geometry/tangent-pair.c | 15 +- basic-geometry/tangent-pair.h | 56 +- basic-geometry/utilities.h | 30 +- basic-geometry/vector2.c | 176 +++--- basic-geometry/vector2.h | 198 ++++--- basic-geometry/vector3.c | 78 ++- basic-geometry/vector3.h | 122 ++--- basic-geometry/versor.c | 22 +- basic-geometry/versor.h | 144 ++--- 21 files changed, 976 insertions(+), 1158 deletions(-) diff --git a/basic-geometry-test/tests/vector2/vector2_is_zero.c b/basic-geometry-test/tests/vector2/vector2_is_zero.c index 3b96962..c189b98 100644 --- a/basic-geometry-test/tests/vector2/vector2_is_zero.c +++ b/basic-geometry-test/tests/vector2/vector2_is_zero.c @@ -21,7 +21,7 @@ static const BgcVector2FP32 _TEST_FP32_NONZERO_VECTOR2_LIST[] = { { -1.25f * BGC_EPSYLON_FP32, 0.0f }, { 0.0f, 1.25f * BGC_EPSYLON_FP32 }, { 0.0f, -1.25f * BGC_EPSYLON_FP32 }, - { 1.25f * BGC_EPSYLON_FP32, 1.25 * BGC_EPSYLON_FP32 }, + { 1.25f * BGC_EPSYLON_FP32, 1.25f * BGC_EPSYLON_FP32 }, { -1.25f * BGC_EPSYLON_FP32, -1.25f * BGC_EPSYLON_FP32 } }; diff --git a/basic-geometry/matrix2x2.c b/basic-geometry/matrix2x2.c index fb6d1e7..bb0e5c1 100644 --- a/basic-geometry/matrix2x2.c +++ b/basic-geometry/matrix2x2.c @@ -18,20 +18,20 @@ extern inline double bgc_matrix2x2_get_determinant_fp64(const BgcMatrix2x2FP64* extern inline int bgc_matrix2x2_is_singular_fp32(const BgcMatrix2x2FP32* matrix); extern inline int bgc_matrix2x2_is_singular_fp64(const BgcMatrix2x2FP64* matrix); -extern inline void bgc_matrix2x2_copy_fp32(const BgcMatrix2x2FP32* from, BgcMatrix2x2FP32* to); -extern inline void bgc_matrix2x2_copy_fp64(const BgcMatrix2x2FP64* from, BgcMatrix2x2FP64* to); +extern inline void bgc_matrix2x2_copy_fp32(const BgcMatrix2x2FP32* source, BgcMatrix2x2FP32* destination); +extern inline void bgc_matrix2x2_copy_fp64(const BgcMatrix2x2FP64* source, BgcMatrix2x2FP64* destination); extern inline void bgc_matrix2x2_swap_fp32(BgcMatrix2x2FP32* matrix1, BgcMatrix2x2FP32* matrix2); extern inline void bgc_matrix2x2_swap_fp64(BgcMatrix2x2FP64* matrix1, BgcMatrix2x2FP64* matrix2); -extern inline void bgc_matrix2x2_convert_fp64_to_fp32(const BgcMatrix2x2FP64* from, BgcMatrix2x2FP32* to); -extern inline void bgc_matrix2x2_convert_fp32_to_fp64(const BgcMatrix2x2FP32* from, BgcMatrix2x2FP64* to); +extern inline void bgc_matrix2x2_convert_fp64_to_fp32(const BgcMatrix2x2FP64* source, BgcMatrix2x2FP32* destination); +extern inline void bgc_matrix2x2_convert_fp32_to_fp64(const BgcMatrix2x2FP32* source, BgcMatrix2x2FP64* destination); -extern inline int bgc_matrix2x2_invert_fp32(BgcMatrix2x2FP32* matrix); -extern inline int bgc_matrix2x2_invert_fp64(BgcMatrix2x2FP64* matrix); +extern inline int bgc_matrix2x2_invert_fp32(const BgcMatrix2x2FP32* matrix, BgcMatrix2x2FP32* inverted); +extern inline int bgc_matrix2x2_invert_fp64(const BgcMatrix2x2FP64* matrix, BgcMatrix2x2FP64* inverted); -extern inline void bgc_matrix2x2_transpose_fp32(BgcMatrix2x2FP32* matrix); -extern inline void bgc_matrix2x2_transpose_fp64(BgcMatrix2x2FP64* matrix); +extern inline void bgc_matrix2x2_transpose_fp32(const BgcMatrix2x2FP32* matrix, BgcMatrix2x2FP32* transposed); +extern inline void bgc_matrix2x2_transpose_fp64(const BgcMatrix2x2FP64* matrix, BgcMatrix2x2FP64* transposed); extern inline void bgc_matrix2x2_set_row1_fp32(const float c1, const float c2, BgcMatrix2x2FP32* matrix); extern inline void bgc_matrix2x2_set_row1_fp64(const double c1, const double c2, BgcMatrix2x2FP64* matrix); @@ -45,12 +45,6 @@ extern inline void bgc_matrix2x2_set_column1_fp64(const double r1, const double extern inline void bgc_matrix2x2_set_column2_fp32(const float r1, const float r2, BgcMatrix2x2FP32* matrix); extern inline void bgc_matrix2x2_set_column2_fp64(const double r1, const double r2, BgcMatrix2x2FP64* matrix); -extern inline int bgc_matrix2x2_get_inverted_fp32(const BgcMatrix2x2FP32* from, BgcMatrix2x2FP32* to); -extern inline int bgc_matrix2x2_get_inverted_fp64(const BgcMatrix2x2FP64* from, BgcMatrix2x2FP64* to); - -extern inline void bgc_matrix2x2_get_transposed_fp32(const BgcMatrix2x2FP32* from, BgcMatrix2x2FP32* to); -extern inline void bgc_matrix2x2_get_transposed_fp64(const BgcMatrix2x2FP64* from, BgcMatrix2x2FP64* to); - extern inline void bgc_matrix2x2_add_fp32(const BgcMatrix2x2FP32* matrix1, const BgcMatrix2x2FP32* matrix2, BgcMatrix2x2FP32* sum); extern inline void bgc_matrix2x2_add_fp64(const BgcMatrix2x2FP64* matrix1, const BgcMatrix2x2FP64* matrix2, BgcMatrix2x2FP64* sum); @@ -74,4 +68,3 @@ extern inline void bgc_matrix2x2_get_left_product_fp64(const BgcVector2FP64* vec extern inline void bgc_matrix2x2_get_right_product_fp32(const BgcMatrix2x2FP32* matrix, const BgcVector2FP32* vector, BgcVector2FP32* product); extern inline void bgc_matrix2x2_get_right_product_fp64(const BgcMatrix2x2FP64* matrix, const BgcVector2FP64* vector, BgcVector2FP64* product); - diff --git a/basic-geometry/matrix2x2.h b/basic-geometry/matrix2x2.h index 82c037a..ad46eff 100644 --- a/basic-geometry/matrix2x2.h +++ b/basic-geometry/matrix2x2.h @@ -111,22 +111,22 @@ inline int bgc_matrix2x2_is_singular_fp64(const BgcMatrix2x2FP64* matrix) // ==================== Copy ==================== // -inline void bgc_matrix2x2_copy_fp32(const BgcMatrix2x2FP32* from, BgcMatrix2x2FP32* to) +inline void bgc_matrix2x2_copy_fp32(const BgcMatrix2x2FP32* source, BgcMatrix2x2FP32* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; } -inline void bgc_matrix2x2_copy_fp64(const BgcMatrix2x2FP64* from, BgcMatrix2x2FP64* to) +inline void bgc_matrix2x2_copy_fp64(const BgcMatrix2x2FP64* source, BgcMatrix2x2FP64* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; } // ==================== Swap ==================== // @@ -175,27 +175,27 @@ inline void bgc_matrix2x2_swap_fp64(BgcMatrix2x2FP64* matrix1, BgcMatrix2x2FP64* // ================== Convert =================== // -inline void bgc_matrix2x2_convert_fp64_to_fp32(const BgcMatrix2x2FP64* from, BgcMatrix2x2FP32* to) +inline void bgc_matrix2x2_convert_fp64_to_fp32(const BgcMatrix2x2FP64* source, BgcMatrix2x2FP32* destination) { - to->r1c1 = (float)from->r1c1; - to->r1c2 = (float)from->r1c2; + destination->r1c1 = (float)source->r1c1; + destination->r1c2 = (float)source->r1c2; - to->r2c1 = (float)from->r2c1; - to->r2c2 = (float)from->r2c2; + destination->r2c1 = (float)source->r2c1; + destination->r2c2 = (float)source->r2c2; } -inline void bgc_matrix2x2_convert_fp32_to_fp64(const BgcMatrix2x2FP32* from, BgcMatrix2x2FP64* to) +inline void bgc_matrix2x2_convert_fp32_to_fp64(const BgcMatrix2x2FP32* source, BgcMatrix2x2FP64* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; } // =================== Invert =================== // -inline int bgc_matrix2x2_invert_fp32(BgcMatrix2x2FP32* matrix) +inline int bgc_matrix2x2_invert_fp32(const BgcMatrix2x2FP32* matrix, BgcMatrix2x2FP32* inverted) { const float determinant = bgc_matrix2x2_get_determinant_fp32(matrix); @@ -211,16 +211,16 @@ inline int bgc_matrix2x2_invert_fp32(BgcMatrix2x2FP32* matrix) const float multiplier = 1.0f / determinant; - matrix->r1c1 = r1c1 * multiplier; - matrix->r1c2 = r1c2 * multiplier; + inverted->r1c1 = r1c1 * multiplier; + inverted->r1c2 = r1c2 * multiplier; - matrix->r2c1 = r2c1 * multiplier; - matrix->r2c2 = r2c2 * multiplier; + inverted->r2c1 = r2c1 * multiplier; + inverted->r2c2 = r2c2 * multiplier; return 1; } -inline int bgc_matrix2x2_invert_fp64(BgcMatrix2x2FP64* matrix) +inline int bgc_matrix2x2_invert_fp64(const BgcMatrix2x2FP64* matrix, BgcMatrix2x2FP64* inverted) { const double determinant = bgc_matrix2x2_get_determinant_fp64(matrix); @@ -236,29 +236,37 @@ inline int bgc_matrix2x2_invert_fp64(BgcMatrix2x2FP64* matrix) const double multiplier = 1.0 / determinant; - matrix->r1c1 = r1c1 * multiplier; - matrix->r1c2 = r1c2 * multiplier; + inverted->r1c1 = r1c1 * multiplier; + inverted->r1c2 = r1c2 * multiplier; - matrix->r2c1 = r2c1 * multiplier; - matrix->r2c2 = r2c2 * multiplier; + inverted->r2c1 = r2c1 * multiplier; + inverted->r2c2 = r2c2 * multiplier; return 1; } // ================= Transpose ================== // -inline void bgc_matrix2x2_transpose_fp32(BgcMatrix2x2FP32* matrix) +inline void bgc_matrix2x2_transpose_fp32(const BgcMatrix2x2FP32* matrix, BgcMatrix2x2FP32* transposed) { - const float tmp = matrix->r1c2; - matrix->r1c2 = matrix->r2c1; - matrix->r2c1 = tmp; + const float r1c2 = matrix->r1c2; + + transposed->r1c1 = matrix->r1c1; + transposed->r1c2 = matrix->r2c1; + + transposed->r2c1 = r1c2; + transposed->r2c2 = matrix->r2c2; } -inline void bgc_matrix2x2_transpose_fp64(BgcMatrix2x2FP64* matrix) +inline void bgc_matrix2x2_transpose_fp64(const BgcMatrix2x2FP64* matrix, BgcMatrix2x2FP64* transposed) { - const double tmp = matrix->r1c2; - matrix->r1c2 = matrix->r2c1; - matrix->r2c1 = tmp; + const double r1c2 = matrix->r1c2; + + transposed->r1c1 = matrix->r1c1; + transposed->r1c2 = matrix->r2c1; + + transposed->r2c1 = r1c2; + transposed->r2c2 = matrix->r2c2; } // ================= Set Row 1 ================== // @@ -317,82 +325,6 @@ inline void bgc_matrix2x2_set_column2_fp64(const double r1, const double r2, Bgc matrix->r2c2 = r2; } -// ================ Get Inverted ================ // - -inline int bgc_matrix2x2_get_inverted_fp32(const BgcMatrix2x2FP32* matrix, BgcMatrix2x2FP32* inverted) -{ - const float determinant = bgc_matrix2x2_get_determinant_fp32(matrix); - - if (bgc_is_zero_fp32(determinant)) { - return 0; - } - - const float r1c1 = matrix->r2c2; - const float r1c2 = -matrix->r1c2; - - const float r2c1 = -matrix->r2c1; - const float r2c2 = matrix->r1c1; - - const float multiplier = 1.0f / determinant; - - inverted->r1c1 = r1c1 * multiplier; - inverted->r1c2 = r1c2 * multiplier; - - inverted->r2c1 = r2c1 * multiplier; - inverted->r2c2 = r2c2 * multiplier; - - return 1; -} - -inline int bgc_matrix2x2_get_inverted_fp64(const BgcMatrix2x2FP64* matrix, BgcMatrix2x2FP64* inverted) -{ - const double determinant = bgc_matrix2x2_get_determinant_fp64(matrix); - - if (bgc_is_zero_fp64(determinant)) { - return 0; - } - - const double r1c1 = matrix->r2c2; - const double r1c2 = -matrix->r1c2; - - const double r2c1 = -matrix->r2c1; - const double r2c2 = matrix->r1c1; - - const double multiplier = 1.0 / determinant; - - inverted->r1c1 = r1c1 * multiplier; - inverted->r1c2 = r1c2 * multiplier; - - inverted->r2c1 = r2c1 * multiplier; - inverted->r2c2 = r2c2 * multiplier; - - return 1; -} - -// =============== Get Transposed =============== // - -inline void bgc_matrix2x2_get_transposed_fp32(const BgcMatrix2x2FP32* matrix, BgcMatrix2x2FP32* transposed) -{ - float tmp = matrix->r1c2; - - transposed->r1c1 = matrix->r1c1; - transposed->r1c2 = matrix->r2c1; - - transposed->r2c1 = tmp; - transposed->r2c2 = matrix->r2c2; -} - -inline void bgc_matrix2x2_get_transposed_fp64(const BgcMatrix2x2FP64* matrix, BgcMatrix2x2FP64* transposed) -{ - double tmp = matrix->r1c2; - - transposed->r1c1 = matrix->r1c1; - transposed->r1c2 = matrix->r2c1; - - transposed->r2c1 = tmp; - transposed->r2c2 = matrix->r2c2; -} - // ==================== Add ===================== // inline void bgc_matrix2x2_add_fp32(const BgcMatrix2x2FP32* matrix1, const BgcMatrix2x2FP32* matrix2, BgcMatrix2x2FP32* sum) diff --git a/basic-geometry/matrix2x3.c b/basic-geometry/matrix2x3.c index 52a4489..4c02038 100644 --- a/basic-geometry/matrix2x3.c +++ b/basic-geometry/matrix2x3.c @@ -3,14 +3,17 @@ extern inline void bgc_matrix2x3_reset_fp32(BgcMatrix2x3FP32* matrix); extern inline void bgc_matrix2x3_reset_fp64(BgcMatrix2x3FP64* matrix); -extern inline void bgc_matrix2x3_copy_fp32(const BgcMatrix2x3FP32* from, BgcMatrix2x3FP32* to); -extern inline void bgc_matrix2x3_copy_fp64(const BgcMatrix2x3FP64* from, BgcMatrix2x3FP64* to); +extern inline void bgc_matrix2x3_copy_fp32(const BgcMatrix2x3FP32* source, BgcMatrix2x3FP32* destination); +extern inline void bgc_matrix2x3_copy_fp64(const BgcMatrix2x3FP64* source, BgcMatrix2x3FP64* destination); extern inline void bgc_matrix2x3_swap_fp32(BgcMatrix2x3FP32* matrix1, BgcMatrix2x3FP32* matrix2); extern inline void bgc_matrix2x3_swap_fp64(BgcMatrix2x3FP64* matrix1, BgcMatrix2x3FP64* matrix2); -extern inline void bgc_matrix2x3_convert_fp64_to_fp32(const BgcMatrix2x3FP64* from, BgcMatrix2x3FP32* to); -extern inline void bgc_matrix2x3_convert_fp32_to_fp64(const BgcMatrix2x3FP32* from, BgcMatrix2x3FP64* to); +extern inline void bgc_matrix2x3_convert_fp64_to_fp32(const BgcMatrix2x3FP64* source, BgcMatrix2x3FP32* destination); +extern inline void bgc_matrix2x3_convert_fp32_to_fp64(const BgcMatrix2x3FP32* source, BgcMatrix2x3FP64* destination); + +extern inline void bgc_matrix2x3_transpose_fp32(const BgcMatrix3x2FP32* matrix, BgcMatrix2x3FP32* transposed); +extern inline void bgc_matrix2x3_transpose_fp64(const BgcMatrix3x2FP64* matrix, BgcMatrix2x3FP64* transposed); extern inline void bgc_matrix2x3_set_row1_fp32(const float c1, const float c2, BgcMatrix2x3FP32* matrix); extern inline void bgc_matrix2x3_set_row1_fp64(const double c1, const double c2, BgcMatrix2x3FP64* matrix); @@ -27,9 +30,6 @@ extern inline void bgc_matrix2x3_set_column1_fp64(const double r1, const double extern inline void bgc_matrix2x3_set_column2_fp32(const float r1, const float r2, const float r3, BgcMatrix2x3FP32* matrix); extern inline void bgc_matrix2x3_set_column2_fp64(const double r1, const double r2, const double r3, BgcMatrix2x3FP64* matrix); -extern inline void bgc_matrix2x3_get_transposed_fp32(const BgcMatrix3x2FP32* from, BgcMatrix2x3FP32* to); -extern inline void bgc_matrix2x3_get_transposed_fp64(const BgcMatrix3x2FP64* from, BgcMatrix2x3FP64* to); - extern inline void bgc_matrix2x3_add_fp32(const BgcMatrix2x3FP32* matrix1, const BgcMatrix2x3FP32* matrix2, BgcMatrix2x3FP32* sum); extern inline void bgc_matrix2x3_add_fp64(const BgcMatrix2x3FP64* matrix1, const BgcMatrix2x3FP64* matrix2, BgcMatrix2x3FP64* sum); diff --git a/basic-geometry/matrix2x3.h b/basic-geometry/matrix2x3.h index f025950..ef63d8b 100644 --- a/basic-geometry/matrix2x3.h +++ b/basic-geometry/matrix2x3.h @@ -33,28 +33,28 @@ inline void bgc_matrix2x3_reset_fp64(BgcMatrix2x3FP64* matrix) // ==================== Copy ==================== // -inline void bgc_matrix2x3_copy_fp32(const BgcMatrix2x3FP32* from, BgcMatrix2x3FP32* to) +inline void bgc_matrix2x3_copy_fp32(const BgcMatrix2x3FP32* source, BgcMatrix2x3FP32* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; - to->r3c1 = from->r3c1; - to->r3c2 = from->r3c2; + destination->r3c1 = source->r3c1; + destination->r3c2 = source->r3c2; } -inline void bgc_matrix2x3_copy_fp64(const BgcMatrix2x3FP64* from, BgcMatrix2x3FP64* to) +inline void bgc_matrix2x3_copy_fp64(const BgcMatrix2x3FP64* source, BgcMatrix2x3FP64* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; - to->r3c1 = from->r3c1; - to->r3c2 = from->r3c2; + destination->r3c1 = source->r3c1; + destination->r3c2 = source->r3c2; } // ==================== Swap ==================== // @@ -121,28 +121,54 @@ inline void bgc_matrix2x3_swap_fp64(BgcMatrix2x3FP64* matrix1, BgcMatrix2x3FP64* // ================== Convert =================== // -inline void bgc_matrix2x3_convert_fp64_to_fp32(const BgcMatrix2x3FP64* from, BgcMatrix2x3FP32* to) +inline void bgc_matrix2x3_convert_fp64_to_fp32(const BgcMatrix2x3FP64* source, BgcMatrix2x3FP32* destination) { - to->r1c1 = (float) from->r1c1; - to->r1c2 = (float) from->r1c2; + destination->r1c1 = (float)source->r1c1; + destination->r1c2 = (float)source->r1c2; - to->r2c1 = (float) from->r2c1; - to->r2c2 = (float) from->r2c2; + destination->r2c1 = (float)source->r2c1; + destination->r2c2 = (float)source->r2c2; - to->r3c1 = (float) from->r3c1; - to->r3c2 = (float) from->r3c2; + destination->r3c1 = (float)source->r3c1; + destination->r3c2 = (float)source->r3c2; } -inline void bgc_matrix2x3_convert_fp32_to_fp64(const BgcMatrix2x3FP32* from, BgcMatrix2x3FP64* to) +inline void bgc_matrix2x3_convert_fp32_to_fp64(const BgcMatrix2x3FP32* source, BgcMatrix2x3FP64* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; - to->r3c1 = from->r3c1; - to->r3c2 = from->r3c2; + destination->r3c1 = source->r3c1; + destination->r3c2 = source->r3c2; +} + +// ================= Transpose ================== // + +inline void bgc_matrix2x3_transpose_fp32(const BgcMatrix3x2FP32* matrix, BgcMatrix2x3FP32* transposed) +{ + transposed->r1c1 = matrix->r1c1; + transposed->r1c2 = matrix->r2c1; + + transposed->r2c1 = matrix->r1c2; + transposed->r2c2 = matrix->r2c2; + + transposed->r3c1 = matrix->r1c3; + transposed->r3c2 = matrix->r2c3; +} + +inline void bgc_matrix2x3_transpose_fp64(const BgcMatrix3x2FP64* matrix, BgcMatrix2x3FP64* transposed) +{ + transposed->r1c1 = matrix->r1c1; + transposed->r1c2 = matrix->r2c1; + + transposed->r2c1 = matrix->r1c2; + transposed->r2c2 = matrix->r2c2; + + transposed->r3c1 = matrix->r1c3; + transposed->r3c2 = matrix->r2c3; } // ================= Set Row 1 ================== // @@ -219,33 +245,6 @@ inline void bgc_matrix2x3_set_column2_fp64(const double r1, const double r2, con matrix->r3c2 = r3; } -// =============== Get transposed =============== // - -inline void bgc_matrix2x3_get_transposed_fp32(const BgcMatrix3x2FP32* matrix, BgcMatrix2x3FP32* transposed) -{ - transposed->r1c1 = matrix->r1c1; - transposed->r1c2 = matrix->r2c1; - - transposed->r2c1 = matrix->r1c2; - transposed->r2c2 = matrix->r2c2; - - transposed->r3c1 = matrix->r1c3; - transposed->r3c2 = matrix->r2c3; -} - -inline void bgc_matrix2x3_get_transposed_fp64(const BgcMatrix3x2FP64* matrix, BgcMatrix2x3FP64* transposed) -{ - transposed->r1c1 = matrix->r1c1; - transposed->r1c2 = matrix->r2c1; - - transposed->r2c1 = matrix->r1c2; - transposed->r2c2 = matrix->r2c2; - - transposed->r3c1 = matrix->r1c3; - transposed->r3c2 = matrix->r2c3; -} - - // ==================== Add ===================== // inline void bgc_matrix2x3_add_fp32(const BgcMatrix2x3FP32* matrix1, const BgcMatrix2x3FP32* matrix2, BgcMatrix2x3FP32* sum) diff --git a/basic-geometry/matrix3x2.c b/basic-geometry/matrix3x2.c index ea53612..e93d9a8 100644 --- a/basic-geometry/matrix3x2.c +++ b/basic-geometry/matrix3x2.c @@ -3,14 +3,17 @@ extern inline void bgc_matrix3x2_reset_fp32(BgcMatrix3x2FP32* matrix); extern inline void bgc_matrix3x2_reset_fp64(BgcMatrix3x2FP64* matrix); -extern inline void bgc_matrix3x2_copy_fp32(const BgcMatrix3x2FP32* from, BgcMatrix3x2FP32* to); -extern inline void bgc_matrix3x2_copy_fp64(const BgcMatrix3x2FP64* from, BgcMatrix3x2FP64* to); +extern inline void bgc_matrix3x2_copy_fp32(const BgcMatrix3x2FP32* source, BgcMatrix3x2FP32* destination); +extern inline void bgc_matrix3x2_copy_fp64(const BgcMatrix3x2FP64* source, BgcMatrix3x2FP64* destination); extern inline void bgc_matrix3x2_swap_fp32(BgcMatrix3x2FP32* matrix1, BgcMatrix3x2FP32* matrix2); extern inline void bgc_matrix3x2_swap_fp64(BgcMatrix3x2FP64* matrix1, BgcMatrix3x2FP64* matrix2); -extern inline void bgc_matrix3x2_convert_fp64_to_fp32(const BgcMatrix3x2FP64* from, BgcMatrix3x2FP32* to); -extern inline void bgc_matrix3x2_convert_fp32_to_fp64(const BgcMatrix3x2FP32* from, BgcMatrix3x2FP64* to); +extern inline void bgc_matrix3x2_convert_fp64_to_fp32(const BgcMatrix3x2FP64* source, BgcMatrix3x2FP32* destination); +extern inline void bgc_matrix3x2_convert_fp32_to_fp64(const BgcMatrix3x2FP32* source, BgcMatrix3x2FP64* destination); + +extern inline void bgc_matrix3x2_transpose_fp32(const BgcMatrix2x3FP32* matrix, BgcMatrix3x2FP32* transposed); +extern inline void bgc_matrix3x2_transpose_fp64(const BgcMatrix2x3FP64* matrix, BgcMatrix3x2FP64* transposed); extern inline void bgc_matrix3x2_set_row1_fp32(const float c1, const float c2, const float c3, BgcMatrix3x2FP32* matrix); extern inline void bgc_matrix3x2_set_row1_fp64(const double c1, const double c2, const double c3, BgcMatrix3x2FP64* matrix); @@ -27,9 +30,6 @@ extern inline void bgc_matrix3x2_set_column2_fp64(const double r1, const double extern inline void bgc_matrix3x2_set_column3_fp32(const float r1, const float r2, BgcMatrix3x2FP32* matrix); extern inline void bgc_matrix3x2_set_column3_fp64(const double r1, const double r2, BgcMatrix3x2FP64* matrix); -extern inline void bgc_matrix3x2_get_transposed_fp32(const BgcMatrix2x3FP32* from, BgcMatrix3x2FP32* to); -extern inline void bgc_matrix3x2_get_transposed_fp64(const BgcMatrix2x3FP64* from, BgcMatrix3x2FP64* to); - extern inline void bgc_matrix3x2_add_fp32(const BgcMatrix3x2FP32* matrix1, const BgcMatrix3x2FP32* matrix2, BgcMatrix3x2FP32* sum); extern inline void bgc_matrix3x2_add_fp64(const BgcMatrix3x2FP64* matrix1, const BgcMatrix3x2FP64* matrix2, BgcMatrix3x2FP64* sum); diff --git a/basic-geometry/matrix3x2.h b/basic-geometry/matrix3x2.h index 4e9197d..cf90ff6 100644 --- a/basic-geometry/matrix3x2.h +++ b/basic-geometry/matrix3x2.h @@ -31,26 +31,26 @@ inline void bgc_matrix3x2_reset_fp64(BgcMatrix3x2FP64* matrix) // ==================== Copy ==================== // -inline void bgc_matrix3x2_copy_fp32(const BgcMatrix3x2FP32* from, BgcMatrix3x2FP32* to) +inline void bgc_matrix3x2_copy_fp32(const BgcMatrix3x2FP32* source, BgcMatrix3x2FP32* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; - to->r1c3 = from->r1c3; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; + destination->r1c3 = source->r1c3; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; - to->r2c3 = from->r2c3; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; + destination->r2c3 = source->r2c3; } -inline void bgc_matrix3x2_copy_fp64(const BgcMatrix3x2FP64* from, BgcMatrix3x2FP64* to) +inline void bgc_matrix3x2_copy_fp64(const BgcMatrix3x2FP64* source, BgcMatrix3x2FP64* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; - to->r1c3 = from->r1c3; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; + destination->r1c3 = source->r1c3; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; - to->r2c3 = from->r2c3; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; + destination->r2c3 = source->r2c3; } // ==================== Swap ==================== // @@ -111,26 +111,50 @@ inline void bgc_matrix3x2_swap_fp64(BgcMatrix3x2FP64* matrix1, BgcMatrix3x2FP64* // ================== Convert =================== // -inline void bgc_matrix3x2_convert_fp64_to_fp32(const BgcMatrix3x2FP64* from, BgcMatrix3x2FP32* to) +inline void bgc_matrix3x2_convert_fp64_to_fp32(const BgcMatrix3x2FP64* source, BgcMatrix3x2FP32* destination) { - to->r1c1 = (float) from->r1c1; - to->r1c2 = (float) from->r1c2; - to->r1c3 = (float) from->r1c3; + destination->r1c1 = (float)source->r1c1; + destination->r1c2 = (float)source->r1c2; + destination->r1c3 = (float)source->r1c3; - to->r2c1 = (float) from->r2c1; - to->r2c2 = (float) from->r2c2; - to->r2c3 = (float) from->r2c3; + destination->r2c1 = (float)source->r2c1; + destination->r2c2 = (float)source->r2c2; + destination->r2c3 = (float)source->r2c3; } -inline void bgc_matrix3x2_convert_fp32_to_fp64(const BgcMatrix3x2FP32* from, BgcMatrix3x2FP64* to) +inline void bgc_matrix3x2_convert_fp32_to_fp64(const BgcMatrix3x2FP32* source, BgcMatrix3x2FP64* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; - to->r1c3 = from->r1c3; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; + destination->r1c3 = source->r1c3; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; - to->r2c3 = from->r2c3; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; + destination->r2c3 = source->r2c3; +} + +// ================= Transpose ================== // + +inline void bgc_matrix3x2_transpose_fp32(const BgcMatrix2x3FP32* matrix, BgcMatrix3x2FP32* transposed) +{ + transposed->r1c1 = matrix->r1c1; + transposed->r1c2 = matrix->r2c1; + transposed->r1c3 = matrix->r3c1; + + transposed->r2c1 = matrix->r1c2; + transposed->r2c2 = matrix->r2c2; + transposed->r2c3 = matrix->r3c2; +} + +inline void bgc_matrix3x2_transpose_fp64(const BgcMatrix2x3FP64* matrix, BgcMatrix3x2FP64* transposed) +{ + transposed->r1c1 = matrix->r1c1; + transposed->r1c2 = matrix->r2c1; + transposed->r1c3 = matrix->r3c1; + + transposed->r2c1 = matrix->r1c2; + transposed->r2c2 = matrix->r2c2; + transposed->r2c3 = matrix->r3c2; } // ================= Set Row 1 ================== // @@ -207,30 +231,6 @@ inline void bgc_matrix3x2_set_column3_fp64(const double r1, const double r2, Bgc matrix->r2c3 = r2; } -// =============== Get transposed =============== // - -inline void bgc_matrix3x2_get_transposed_fp32(const BgcMatrix2x3FP32* from, BgcMatrix3x2FP32* to) -{ - to->r1c1 = from->r1c1; - to->r1c2 = from->r2c1; - to->r1c3 = from->r3c1; - - to->r2c1 = from->r1c2; - to->r2c2 = from->r2c2; - to->r2c3 = from->r3c2; -} - -inline void bgc_matrix3x2_get_transposed_fp64(const BgcMatrix2x3FP64* from, BgcMatrix3x2FP64* to) -{ - to->r1c1 = from->r1c1; - to->r1c2 = from->r2c1; - to->r1c3 = from->r3c1; - - to->r2c1 = from->r1c2; - to->r2c2 = from->r2c2; - to->r2c3 = from->r3c2; -} - // ==================== Add ===================== // inline void bgc_matrix3x2_add_fp32(const BgcMatrix3x2FP32* matrix1, const BgcMatrix3x2FP32* matrix2, BgcMatrix3x2FP32* sum) diff --git a/basic-geometry/matrix3x3.c b/basic-geometry/matrix3x3.c index 3a32e53..a67f481 100644 --- a/basic-geometry/matrix3x3.c +++ b/basic-geometry/matrix3x3.c @@ -9,14 +9,14 @@ extern inline void bgc_matrix3x3_set_to_identity_fp64(BgcMatrix3x3FP64* matrix); extern inline void bgc_matrix3x3_set_to_diagonal_fp32(const float d1, const float d2, const float d3, BgcMatrix3x3FP32* matrix); extern inline void bgc_matrix3x3_set_to_diagonal_fp64(const double d1, const double d2, const double d3, BgcMatrix3x3FP64* matrix); -extern inline void bgc_matrix3x3_copy_fp32(const BgcMatrix3x3FP32* from, BgcMatrix3x3FP32* to); -extern inline void bgc_matrix3x3_copy_fp64(const BgcMatrix3x3FP64* from, BgcMatrix3x3FP64* to); +extern inline void bgc_matrix3x3_copy_fp32(const BgcMatrix3x3FP32* source, BgcMatrix3x3FP32* destination); +extern inline void bgc_matrix3x3_copy_fp64(const BgcMatrix3x3FP64* source, BgcMatrix3x3FP64* destination); extern inline void bgc_matrix3x3_swap_fp32(BgcMatrix3x3FP32* matrix1, BgcMatrix3x3FP32* matrix2); extern inline void bgc_matrix3x3_swap_fp64(BgcMatrix3x3FP64* matrix1, BgcMatrix3x3FP64* matrix2); -extern inline void bgc_matrix3x3_convert_fp64_to_fp32(const BgcMatrix3x3FP64* from, BgcMatrix3x3FP32* to); -extern inline void bgc_matrix3x3_convert_fp32_to_fp64(const BgcMatrix3x3FP32* from, BgcMatrix3x3FP64* to); +extern inline void bgc_matrix3x3_convert_fp64_to_fp32(const BgcMatrix3x3FP64* source, BgcMatrix3x3FP32* destination); +extern inline void bgc_matrix3x3_convert_fp32_to_fp64(const BgcMatrix3x3FP32* source, BgcMatrix3x3FP64* destination); extern inline float bgc_matrix3x3_get_determinant_fp32(const BgcMatrix3x3FP32* matrix); extern inline double bgc_matrix3x3_get_determinant_fp64(const BgcMatrix3x3FP64* matrix); @@ -24,8 +24,8 @@ extern inline double bgc_matrix3x3_get_determinant_fp64(const BgcMatrix3x3FP64* extern inline int bgc_matrix3x3_is_singular_fp32(const BgcMatrix3x3FP32* matrix); extern inline int bgc_matrix3x3_is_singular_fp64(const BgcMatrix3x3FP64* matrix); -extern inline void bgc_matrix3x3_transpose_fp32(BgcMatrix3x3FP32* matrix); -extern inline void bgc_matrix3x3_transpose_fp64(BgcMatrix3x3FP64* matrix); +extern inline void bgc_matrix3x3_transpose_fp32(const BgcMatrix3x3FP32* matrix, BgcMatrix3x3FP32* transposed); +extern inline void bgc_matrix3x3_transpose_fp64(const BgcMatrix3x3FP64* matrix, BgcMatrix3x3FP64* transposed); extern inline void bgc_matrix3x3_set_row1_fp32(const float c1, const float c2, const float c3, BgcMatrix3x3FP32* matrix); extern inline void bgc_matrix3x3_set_row1_fp64(const double c1, const double c2, const double c3, BgcMatrix3x3FP64* matrix); @@ -45,9 +45,6 @@ extern inline void bgc_matrix3x3_set_column2_fp64(const double r1, const double extern inline void bgc_matrix3x3_set_column3_fp32(const float r1, const float r2, const float r3, BgcMatrix3x3FP32* matrix); extern inline void bgc_matrix3x3_set_column3_fp64(const double r1, const double r2, const double r3, BgcMatrix3x3FP64* matrix); -extern inline void bgc_matrix3x3_get_transposed_fp32(const BgcMatrix3x3FP32* matrix, BgcMatrix3x3FP32* result); -extern inline void bgc_matrix3x3_get_transposed_fp64(const BgcMatrix3x3FP64* matrix, BgcMatrix3x3FP64* result); - extern inline void bgc_matrix3x3_add_fp32(const BgcMatrix3x3FP32* matrix1, const BgcMatrix3x3FP32* matrix2, BgcMatrix3x3FP32* sum); extern inline void bgc_matrix3x3_add_fp64(const BgcMatrix3x3FP64* matrix1, const BgcMatrix3x3FP64* matrix2, BgcMatrix3x3FP64* sum); @@ -74,11 +71,11 @@ extern inline void bgc_matrix3x3_get_right_product_fp64(const BgcMatrix3x3FP64* // =================== Invert =================== // -int bgc_matrix3x3_invert_fp32(BgcMatrix3x3FP32* matrix) +int bgc_matrix3x3_invert_fp32(const BgcMatrix3x3FP32* matrix, BgcMatrix3x3FP32* inverted) { const float determinant = bgc_matrix3x3_get_determinant_fp32(matrix); - if (-BGC_EPSYLON_FP32 <= determinant && determinant <= BGC_EPSYLON_FP32) { + if (bgc_is_zero_fp32(determinant)) { return 0; } @@ -96,26 +93,26 @@ int bgc_matrix3x3_invert_fp32(BgcMatrix3x3FP32* matrix) const float multiplier = 1.0f / determinant; - matrix->r1c1 = r1c1 * multiplier; - matrix->r1c2 = r1c2 * multiplier; - matrix->r1c3 = r1c3 * multiplier; + inverted->r1c1 = r1c1 * multiplier; + inverted->r1c2 = r1c2 * multiplier; + inverted->r1c3 = r1c3 * multiplier; - matrix->r2c1 = r2c1 * multiplier; - matrix->r2c2 = r2c2 * multiplier; - matrix->r2c3 = r2c3 * multiplier; + inverted->r2c1 = r2c1 * multiplier; + inverted->r2c2 = r2c2 * multiplier; + inverted->r2c3 = r2c3 * multiplier; - matrix->r3c1 = r3c1 * multiplier; - matrix->r3c2 = r3c2 * multiplier; - matrix->r3c3 = r3c3 * multiplier; + inverted->r3c1 = r3c1 * multiplier; + inverted->r3c2 = r3c2 * multiplier; + inverted->r3c3 = r3c3 * multiplier; return 1; } -int bgc_matrix3x3_invert_fp64(BgcMatrix3x3FP64* matrix) +int bgc_matrix3x3_invert_fp64(const BgcMatrix3x3FP64* matrix, BgcMatrix3x3FP64* inverted) { const double determinant = bgc_matrix3x3_get_determinant_fp64(matrix); - if (-BGC_EPSYLON_FP64 <= determinant && determinant <= BGC_EPSYLON_FP64) { + if (bgc_is_zero_fp64(determinant)) { return 0; } @@ -133,93 +130,17 @@ int bgc_matrix3x3_invert_fp64(BgcMatrix3x3FP64* matrix) const double multiplier = 1.0 / determinant; - matrix->r1c1 = r1c1 * multiplier; - matrix->r1c2 = r1c2 * multiplier; - matrix->r1c3 = r1c3 * multiplier; + inverted->r1c1 = r1c1 * multiplier; + inverted->r1c2 = r1c2 * multiplier; + inverted->r1c3 = r1c3 * multiplier; - matrix->r2c1 = r2c1 * multiplier; - matrix->r2c2 = r2c2 * multiplier; - matrix->r2c3 = r2c3 * multiplier; + inverted->r2c1 = r2c1 * multiplier; + inverted->r2c2 = r2c2 * multiplier; + inverted->r2c3 = r2c3 * multiplier; - matrix->r3c1 = r3c1 * multiplier; - matrix->r3c2 = r3c2 * multiplier; - matrix->r3c3 = r3c3 * multiplier; - - return 1; -} - -// ================ Get Inverted ================ // - -int bgc_matrix3x3_get_inverted_fp32(const BgcMatrix3x3FP32* matrix, BgcMatrix3x3FP32* result) -{ - const float determinant = bgc_matrix3x3_get_determinant_fp32(matrix); - - if (-BGC_EPSYLON_FP32 <= determinant && determinant <= BGC_EPSYLON_FP32) { - return 0; - } - - const float r1c1 = matrix->r2c2 * matrix->r3c3 - matrix->r2c3 * matrix->r3c2; - const float r1c2 = matrix->r1c3 * matrix->r3c2 - matrix->r1c2 * matrix->r3c3; - const float r1c3 = matrix->r1c2 * matrix->r2c3 - matrix->r1c3 * matrix->r2c2; - - const float r2c1 = matrix->r2c3 * matrix->r3c1 - matrix->r2c1 * matrix->r3c3; - const float r2c2 = matrix->r1c1 * matrix->r3c3 - matrix->r1c3 * matrix->r3c1; - const float r2c3 = matrix->r1c3 * matrix->r2c1 - matrix->r1c1 * matrix->r2c3; - - const float r3c1 = matrix->r2c1 * matrix->r3c2 - matrix->r2c2 * matrix->r3c1; - const float r3c2 = matrix->r1c2 * matrix->r3c1 - matrix->r1c1 * matrix->r3c2; - const float r3c3 = matrix->r1c1 * matrix->r2c2 - matrix->r1c2 * matrix->r2c1; - - const float multiplier = 1.0f / determinant; - - result->r1c1 = r1c1 * multiplier; - result->r1c2 = r1c2 * multiplier; - result->r1c3 = r1c3 * multiplier; - - result->r2c1 = r2c1 * multiplier; - result->r2c2 = r2c2 * multiplier; - result->r2c3 = r2c3 * multiplier; - - result->r3c1 = r3c1 * multiplier; - result->r3c2 = r3c2 * multiplier; - result->r3c3 = r3c3 * multiplier; - - return 1; -} - -int bgc_matrix3x3_get_inverted_fp64(const BgcMatrix3x3FP64* matrix, BgcMatrix3x3FP64* result) -{ - const double determinant = bgc_matrix3x3_get_determinant_fp64(matrix); - - if (-BGC_EPSYLON_FP64 <= determinant && determinant <= BGC_EPSYLON_FP64) { - return 0; - } - - const double r1c1 = matrix->r2c2 * matrix->r3c3 - matrix->r2c3 * matrix->r3c2; - const double r1c2 = matrix->r1c3 * matrix->r3c2 - matrix->r1c2 * matrix->r3c3; - const double r1c3 = matrix->r1c2 * matrix->r2c3 - matrix->r1c3 * matrix->r2c2; - - const double r2c1 = matrix->r2c3 * matrix->r3c1 - matrix->r2c1 * matrix->r3c3; - const double r2c2 = matrix->r1c1 * matrix->r3c3 - matrix->r1c3 * matrix->r3c1; - const double r2c3 = matrix->r1c3 * matrix->r2c1 - matrix->r1c1 * matrix->r2c3; - - const double r3c1 = matrix->r2c1 * matrix->r3c2 - matrix->r2c2 * matrix->r3c1; - const double r3c2 = matrix->r1c2 * matrix->r3c1 - matrix->r1c1 * matrix->r3c2; - const double r3c3 = matrix->r1c1 * matrix->r2c2 - matrix->r1c2 * matrix->r2c1; - - const double multiplier = 1.0 / determinant; - - result->r1c1 = r1c1 * multiplier; - result->r1c2 = r1c2 * multiplier; - result->r1c3 = r1c3 * multiplier; - - result->r2c1 = r2c1 * multiplier; - result->r2c2 = r2c2 * multiplier; - result->r2c3 = r2c3 * multiplier; - - result->r3c1 = r3c1 * multiplier; - result->r3c2 = r3c2 * multiplier; - result->r3c3 = r3c3 * multiplier; + inverted->r3c1 = r3c1 * multiplier; + inverted->r3c2 = r3c2 * multiplier; + inverted->r3c3 = r3c3 * multiplier; return 1; } diff --git a/basic-geometry/matrix3x3.h b/basic-geometry/matrix3x3.h index 4a7c77b..c1ab5c2 100644 --- a/basic-geometry/matrix3x3.h +++ b/basic-geometry/matrix3x3.h @@ -102,34 +102,34 @@ inline void bgc_matrix3x3_set_to_diagonal_fp64(const double d1, const double d2, // ==================== Copy ==================== // -inline void bgc_matrix3x3_copy_fp32(const BgcMatrix3x3FP32* from, BgcMatrix3x3FP32* to) +inline void bgc_matrix3x3_copy_fp32(const BgcMatrix3x3FP32* source, BgcMatrix3x3FP32* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; - to->r1c3 = from->r1c3; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; + destination->r1c3 = source->r1c3; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; - to->r2c3 = from->r2c3; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; + destination->r2c3 = source->r2c3; - to->r3c1 = from->r3c1; - to->r3c2 = from->r3c2; - to->r3c3 = from->r3c3; + destination->r3c1 = source->r3c1; + destination->r3c2 = source->r3c2; + destination->r3c3 = source->r3c3; } -inline void bgc_matrix3x3_copy_fp64(const BgcMatrix3x3FP64* from, BgcMatrix3x3FP64* to) +inline void bgc_matrix3x3_copy_fp64(const BgcMatrix3x3FP64* source, BgcMatrix3x3FP64* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; - to->r1c3 = from->r1c3; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; + destination->r1c3 = source->r1c3; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; - to->r2c3 = from->r2c3; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; + destination->r2c3 = source->r2c3; - to->r3c1 = from->r3c1; - to->r3c2 = from->r3c2; - to->r3c3 = from->r3c3; + destination->r3c1 = source->r3c1; + destination->r3c2 = source->r3c2; + destination->r3c3 = source->r3c3; } // ==================== Swap ==================== // @@ -214,34 +214,34 @@ inline void bgc_matrix3x3_swap_fp64(BgcMatrix3x3FP64* matrix1, BgcMatrix3x3FP64* // ================== Convert =================== // -inline void bgc_matrix3x3_convert_fp64_to_fp32(const BgcMatrix3x3FP64* from, BgcMatrix3x3FP32* to) +inline void bgc_matrix3x3_convert_fp64_to_fp32(const BgcMatrix3x3FP64* source, BgcMatrix3x3FP32* destination) { - to->r1c1 = (float) from->r1c1; - to->r1c2 = (float) from->r1c2; - to->r1c3 = (float) from->r1c3; + destination->r1c1 = (float)source->r1c1; + destination->r1c2 = (float)source->r1c2; + destination->r1c3 = (float)source->r1c3; - to->r2c1 = (float) from->r2c1; - to->r2c2 = (float) from->r2c2; - to->r2c3 = (float) from->r2c3; + destination->r2c1 = (float)source->r2c1; + destination->r2c2 = (float)source->r2c2; + destination->r2c3 = (float)source->r2c3; - to->r3c1 = (float) from->r3c1; - to->r3c2 = (float) from->r3c2; - to->r3c3 = (float) from->r3c3; + destination->r3c1 = (float)source->r3c1; + destination->r3c2 = (float)source->r3c2; + destination->r3c3 = (float)source->r3c3; } -inline void bgc_matrix3x3_convert_fp32_to_fp64(const BgcMatrix3x3FP32* from, BgcMatrix3x3FP64* to) +inline void bgc_matrix3x3_convert_fp32_to_fp64(const BgcMatrix3x3FP32* source, BgcMatrix3x3FP64* destination) { - to->r1c1 = from->r1c1; - to->r1c2 = from->r1c2; - to->r1c3 = from->r1c3; + destination->r1c1 = source->r1c1; + destination->r1c2 = source->r1c2; + destination->r1c3 = source->r1c3; - to->r2c1 = from->r2c1; - to->r2c2 = from->r2c2; - to->r2c3 = from->r2c3; + destination->r2c1 = source->r2c1; + destination->r2c2 = source->r2c2; + destination->r2c3 = source->r2c3; - to->r3c1 = from->r3c1; - to->r3c2 = from->r3c2; - to->r3c3 = from->r3c3; + destination->r3c1 = source->r3c1; + destination->r3c2 = source->r3c2; + destination->r3c3 = source->r3c3; } // ================ Determinant ================= // @@ -274,40 +274,48 @@ inline int bgc_matrix3x3_is_singular_fp64(const BgcMatrix3x3FP64* matrix) // =================== Invert =================== // -int bgc_matrix3x3_invert_fp32(BgcMatrix3x3FP32* matrix); +int bgc_matrix3x3_invert_fp32(const BgcMatrix3x3FP32* matrix, BgcMatrix3x3FP32* inverted); -int bgc_matrix3x3_invert_fp64(BgcMatrix3x3FP64* matrix); +int bgc_matrix3x3_invert_fp64(const BgcMatrix3x3FP64* matrix, BgcMatrix3x3FP64* inverted); // ================= Transpose ================== // -inline void bgc_matrix3x3_transpose_fp32(BgcMatrix3x3FP32* matrix) +inline void bgc_matrix3x3_transpose_fp32(const BgcMatrix3x3FP32* matrix, BgcMatrix3x3FP32* transposed) { - float tmp = matrix->r1c2; - matrix->r1c2 = matrix->r2c1; - matrix->r2c1 = tmp; + transposed->r1c1 = matrix->r1c1; + transposed->r2c2 = matrix->r2c2; + transposed->r3c3 = matrix->r3c3; - tmp = matrix->r1c3; - matrix->r1c3 = matrix->r3c1; - matrix->r3c1 = tmp; + const float r1c2 = matrix->r1c2; + const float r1c3 = matrix->r1c3; + const float r2c3 = matrix->r2c3; - tmp = matrix->r2c3; - matrix->r2c3 = matrix->r3c2; - matrix->r3c2 = tmp; + transposed->r1c2 = matrix->r2c1; + transposed->r1c3 = matrix->r3c1; + transposed->r2c3 = matrix->r3c2; + + transposed->r2c1 = r1c2; + transposed->r3c1 = r1c3; + transposed->r3c2 = r2c3; } -inline void bgc_matrix3x3_transpose_fp64(BgcMatrix3x3FP64* matrix) +inline void bgc_matrix3x3_transpose_fp64(const BgcMatrix3x3FP64* matrix, BgcMatrix3x3FP64* transposed) { - double tmp = matrix->r1c2; - matrix->r1c2 = matrix->r2c1; - matrix->r2c1 = tmp; + transposed->r1c1 = matrix->r1c1; + transposed->r2c2 = matrix->r2c2; + transposed->r3c3 = matrix->r3c3; - tmp = matrix->r1c3; - matrix->r1c3 = matrix->r3c1; - matrix->r3c1 = tmp; + const double r1c2 = matrix->r1c2; + const double r1c3 = matrix->r1c3; + const double r2c3 = matrix->r2c3; - tmp = matrix->r2c3; - matrix->r2c3 = matrix->r3c2; - matrix->r3c2 = tmp; + transposed->r1c2 = matrix->r2c1; + transposed->r1c3 = matrix->r3c1; + transposed->r2c3 = matrix->r3c2; + + transposed->r2c1 = r1c2; + transposed->r3c1 = r1c3; + transposed->r3c2 = r2c3; } // ================= Set Row 1 ================== // @@ -406,54 +414,6 @@ inline void bgc_matrix3x3_set_column3_fp64(const double r1, const double r2, con matrix->r3c3 = r3; } -// ================ Get Inverted ================ // - -int bgc_matrix3x3_get_inverted_fp32(const BgcMatrix3x3FP32* matrix, BgcMatrix3x3FP32* result); - -int bgc_matrix3x3_get_inverted_fp64(const BgcMatrix3x3FP64* matrix, BgcMatrix3x3FP64* result); - -// =============== Get Transposed =============== // - -inline void bgc_matrix3x3_get_transposed_fp32(const BgcMatrix3x3FP32* matrix, BgcMatrix3x3FP32* result) -{ - if (matrix == result) { - bgc_matrix3x3_transpose_fp32(result); - return; - } - - result->r1c1 = matrix->r1c1; - result->r1c2 = matrix->r2c1; - result->r1c3 = matrix->r3c1; - - result->r2c1 = matrix->r1c2; - result->r2c2 = matrix->r2c2; - result->r2c3 = matrix->r3c2; - - result->r3c1 = matrix->r1c3; - result->r3c2 = matrix->r2c3; - result->r3c3 = matrix->r3c3; -} - -inline void bgc_matrix3x3_get_transposed_fp64(const BgcMatrix3x3FP64* matrix, BgcMatrix3x3FP64* result) -{ - if (matrix == result) { - bgc_matrix3x3_transpose_fp64(result); - return; - } - - result->r1c1 = matrix->r1c1; - result->r1c2 = matrix->r2c1; - result->r1c3 = matrix->r3c1; - - result->r2c1 = matrix->r1c2; - result->r2c2 = matrix->r2c2; - result->r2c3 = matrix->r3c2; - - result->r3c1 = matrix->r1c3; - result->r3c2 = matrix->r2c3; - result->r3c3 = matrix->r3c3; -} - // ==================== Add ===================== // inline void bgc_matrix3x3_add_fp32(const BgcMatrix3x3FP32* matrix1, const BgcMatrix3x3FP32* matrix2, BgcMatrix3x3FP32* sum) diff --git a/basic-geometry/quaternion.c b/basic-geometry/quaternion.c index 6ff23c7..6694803 100644 --- a/basic-geometry/quaternion.c +++ b/basic-geometry/quaternion.c @@ -21,35 +21,29 @@ extern inline int bgc_quaternion_is_zero_fp64(const BgcQuaternionFP64* quaternio extern inline int bgc_quaternion_is_unit_fp32(const BgcQuaternionFP32* quaternion); extern inline int bgc_quaternion_is_unit_fp64(const BgcQuaternionFP64* quaternion); -extern inline void bgc_quaternion_copy_fp32(const BgcQuaternionFP32* from, BgcQuaternionFP32* to); -extern inline void bgc_quaternion_copy_fp64(const BgcQuaternionFP64* from, BgcQuaternionFP64* to); +extern inline void bgc_quaternion_copy_fp32(const BgcQuaternionFP32* source, BgcQuaternionFP32* destination); +extern inline void bgc_quaternion_copy_fp64(const BgcQuaternionFP64* source, BgcQuaternionFP64* destination); extern inline void bgc_quaternion_swap_fp32(BgcQuaternionFP32* quarternion1, BgcQuaternionFP32* quarternion2); extern inline void bgc_quaternion_swap_fp64(BgcQuaternionFP64* quarternion1, BgcQuaternionFP64* quarternion2); -extern inline void bgc_quaternion_convert_fp64_to_fp32(const BgcQuaternionFP64* quaternion, BgcQuaternionFP32* result); -extern inline void bgc_quaternion_convert_fp32_to_fp64(const BgcQuaternionFP32* quaternion, BgcQuaternionFP64* result); +extern inline void bgc_quaternion_convert_fp64_to_fp32(const BgcQuaternionFP64* source, BgcQuaternionFP32* destination); +extern inline void bgc_quaternion_convert_fp32_to_fp64(const BgcQuaternionFP32* source, BgcQuaternionFP64* destination); -extern inline void bgc_quaternion_conjugate_fp32(BgcQuaternionFP32* quaternion); -extern inline void bgc_quaternion_conjugate_fp64(BgcQuaternionFP64* quaternion); +extern inline void bgc_quaternion_conjugate_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* conjugate); +extern inline void bgc_quaternion_conjugate_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* conjugate); -extern inline int bgc_quaternion_normalize_fp32(BgcQuaternionFP32* quaternion); -extern inline int bgc_quaternion_normalize_fp64(BgcQuaternionFP64* quaternion); +extern inline int bgc_quaternion_invert_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* inverted); +extern inline int bgc_quaternion_invert_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* inverted); -extern inline void bgc_quaternion_get_conjugate_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* conjugate); -extern inline void bgc_quaternion_get_conjugate_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* conjugate); - -extern inline int bgc_quaternion_get_normalized_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* normalized); -extern inline int bgc_quaternion_get_normalized_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* normalized); +extern inline int bgc_quaternion_normalize_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* normalized); +extern inline int bgc_quaternion_normalize_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* normalized); extern inline void bgc_quaternion_get_product_fp32(const BgcQuaternionFP32* left, const BgcQuaternionFP32* right, BgcQuaternionFP32* product); extern inline void bgc_quaternion_get_product_fp64(const BgcQuaternionFP64* left, const BgcQuaternionFP64* right, BgcQuaternionFP64* product); -extern inline void bgc_quaternion_get_rotation_matrix_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* rotation); -extern inline void bgc_quaternion_get_rotation_matrix_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* rotation); - -extern inline void bgc_quaternion_get_reverse_matrix_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* reverse); -extern inline void bgc_quaternion_get_reverse_matrix_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* reverse); +extern inline int bgc_quaternion_get_ratio_fp32(const BgcQuaternionFP32* divident, const BgcQuaternionFP32* divisor, BgcQuaternionFP32* quotient); +extern inline int bgc_quaternion_get_ratio_fp64(const BgcQuaternionFP64* divident, const BgcQuaternionFP64* divisor, BgcQuaternionFP64* quotient); extern inline void bgc_quaternion_add_fp32(const BgcQuaternionFP32* quaternion1, const BgcQuaternionFP32* quaternion2, BgcQuaternionFP32* sum); extern inline void bgc_quaternion_add_fp64(const BgcQuaternionFP64* quaternion1, const BgcQuaternionFP64* quaternion2, BgcQuaternionFP64* sum); @@ -72,5 +66,11 @@ extern inline void bgc_quaternion_divide_fp64(const BgcQuaternionFP64* dividend, extern inline void bgc_quaternion_get_linear_interpolation_fp32(const BgcQuaternionFP32* vector1, const BgcQuaternionFP32* vector2, const float phase, BgcQuaternionFP32* interpolation); extern inline void bgc_quaternion_get_linear_interpolation_fp64(const BgcQuaternionFP64* vector1, const BgcQuaternionFP64* vector2, const double phase, BgcQuaternionFP64* interpolation); +extern inline int bgc_quaternion_get_rotation_matrix_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* rotation); +extern inline int bgc_quaternion_get_rotation_matrix_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* rotation); + +extern inline int bgc_quaternion_get_reverse_matrix_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* reverse); +extern inline int bgc_quaternion_get_reverse_matrix_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* reverse); + extern inline int bgc_quaternion_are_close_fp32(const BgcQuaternionFP32* quaternion1, const BgcQuaternionFP32* quaternion2); extern inline int bgc_quaternion_are_close_fp32(const BgcQuaternionFP32* quaternion1, const BgcQuaternionFP32* quaternion2); diff --git a/basic-geometry/quaternion.h b/basic-geometry/quaternion.h index 830b00c..c88b66b 100644 --- a/basic-geometry/quaternion.h +++ b/basic-geometry/quaternion.h @@ -119,20 +119,20 @@ inline int bgc_quaternion_is_unit_fp64(const BgcQuaternionFP64* quaternion) // ==================== Copy ==================== // -inline void bgc_quaternion_copy_fp32(const BgcQuaternionFP32* from, BgcQuaternionFP32* to) +inline void bgc_quaternion_copy_fp32(const BgcQuaternionFP32* source, BgcQuaternionFP32* destination) { - to->s0 = from->s0; - to->x1 = from->x1; - to->x2 = from->x2; - to->x3 = from->x3; + destination->s0 = source->s0; + destination->x1 = source->x1; + destination->x2 = source->x2; + destination->x3 = source->x3; } -inline void bgc_quaternion_copy_fp64(const BgcQuaternionFP64* from, BgcQuaternionFP64* to) +inline void bgc_quaternion_copy_fp64(const BgcQuaternionFP64* source, BgcQuaternionFP64* destination) { - to->s0 = from->s0; - to->x1 = from->x1; - to->x2 = from->x2; - to->x3 = from->x3; + destination->s0 = source->s0; + destination->x1 = source->x1; + destination->x2 = source->x2; + destination->x3 = source->x3; } // ==================== Swap ==================== // @@ -175,119 +175,127 @@ inline void bgc_quaternion_swap_fp64(BgcQuaternionFP64* quarternion1, BgcQuatern // ================== Convert =================== // -inline void bgc_quaternion_convert_fp64_to_fp32(const BgcQuaternionFP64* quaternion, BgcQuaternionFP32* result) +inline void bgc_quaternion_convert_fp64_to_fp32(const BgcQuaternionFP64* source, BgcQuaternionFP32* destination) { - result->s0 = (float) quaternion->s0; - result->x1 = (float) quaternion->x1; - result->x2 = (float) quaternion->x2; - result->x3 = (float) quaternion->x3; + destination->s0 = (float)source->s0; + destination->x1 = (float)source->x1; + destination->x2 = (float)source->x2; + destination->x3 = (float)source->x3; } -inline void bgc_quaternion_convert_fp32_to_fp64(const BgcQuaternionFP32* quaternion, BgcQuaternionFP64* result) +inline void bgc_quaternion_convert_fp32_to_fp64(const BgcQuaternionFP32* source, BgcQuaternionFP64* destination) { - result->s0 = quaternion->s0; - result->x1 = quaternion->x1; - result->x2 = quaternion->x2; - result->x3 = quaternion->x3; + destination->s0 = source->s0; + destination->x1 = source->x1; + destination->x2 = source->x2; + destination->x3 = source->x3; } // ================= Conjugate ================== // -inline void bgc_quaternion_conjugate_fp32(BgcQuaternionFP32* quaternion) +inline void bgc_quaternion_conjugate_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* conjugate) { - quaternion->x1 = -quaternion->x1; - quaternion->x2 = -quaternion->x2; - quaternion->x3 = -quaternion->x3; + conjugate->s0 = quaternion->s0; + conjugate->x1 = -quaternion->x1; + conjugate->x2 = -quaternion->x2; + conjugate->x3 = -quaternion->x3; } -inline void bgc_quaternion_conjugate_fp64(BgcQuaternionFP64* quaternion) +inline void bgc_quaternion_conjugate_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* conjugate) { - quaternion->x1 = -quaternion->x1; - quaternion->x2 = -quaternion->x2; - quaternion->x3 = -quaternion->x3; + conjugate->s0 = quaternion->s0; + conjugate->x1 = -quaternion->x1; + conjugate->x2 = -quaternion->x2; + conjugate->x3 = -quaternion->x3; +} + +// =================== Invert =================== // + +inline int bgc_quaternion_invert_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* inverted) +{ + const float square_modulus = bgc_quaternion_get_square_modulus_fp32(quaternion); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) { + return 0; + } + + const float multiplicand = 1.0f / square_modulus; + + inverted->s0 = quaternion->s0 * multiplicand; + inverted->x1 = -quaternion->x1 * multiplicand; + inverted->x2 = -quaternion->x2 * multiplicand; + inverted->x3 = -quaternion->x3 * multiplicand; + + return 1; +} + +inline int bgc_quaternion_invert_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* inverted) +{ + const double square_modulus = bgc_quaternion_get_square_modulus_fp64(quaternion); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) { + return 0; + } + + const double multiplicand = 1.0 / square_modulus; + + inverted->s0 = quaternion->s0 * multiplicand; + inverted->x1 = -quaternion->x1 * multiplicand; + inverted->x2 = -quaternion->x2 * multiplicand; + inverted->x3 = -quaternion->x3 * multiplicand; + + return 1; } // ================= Normalize ================== // -inline int bgc_quaternion_normalize_fp32(BgcQuaternionFP32* quaternion) +inline int bgc_quaternion_normalize_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* normalized) { const float square_modulus = bgc_quaternion_get_square_modulus_fp32(quaternion); if (bgc_is_sqare_unit_fp32(square_modulus)) { + bgc_quaternion_copy_fp32(quaternion, normalized); return 1; } if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) { - bgc_quaternion_reset_fp32(quaternion); return 0; } const float multiplier = sqrtf(1.0f / square_modulus); - quaternion->s0 *= multiplier; - quaternion->x1 *= multiplier; - quaternion->x2 *= multiplier; - quaternion->x3 *= multiplier; + normalized->s0 = quaternion->s0 * multiplier; + normalized->x1 = quaternion->x1 * multiplier; + normalized->x2 = quaternion->x2 * multiplier; + normalized->x3 = quaternion->x3 * multiplier; return 1; } -inline int bgc_quaternion_normalize_fp64(BgcQuaternionFP64* quaternion) +inline int bgc_quaternion_normalize_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* normalized) { const double square_modulus = bgc_quaternion_get_square_modulus_fp64(quaternion); if (bgc_is_sqare_unit_fp64(square_modulus)) { + bgc_quaternion_copy_fp64(quaternion, normalized); return 1; } if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) { - bgc_quaternion_reset_fp64(quaternion); return 0; } const double multiplier = sqrt(1.0 / square_modulus); - quaternion->s0 *= multiplier; - quaternion->x1 *= multiplier; - quaternion->x2 *= multiplier; - quaternion->x3 *= multiplier; + normalized->s0 *= multiplier; + normalized->x1 *= multiplier; + normalized->x2 *= multiplier; + normalized->x3 *= multiplier; return 1; } -// =============== Get Conjugate ================ // - -inline void bgc_quaternion_get_conjugate_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* conjugate) -{ - conjugate->s0 = quaternion->s0; - conjugate->x1 = -quaternion->x1; - conjugate->x2 = -quaternion->x2; - conjugate->x3 = -quaternion->x3; -} - -inline void bgc_quaternion_get_conjugate_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* conjugate) -{ - conjugate->s0 = quaternion->s0; - conjugate->x1 = -quaternion->x1; - conjugate->x2 = -quaternion->x2; - conjugate->x3 = -quaternion->x3; -} - -// =============== Get Normalized =============== // - -inline int bgc_quaternion_get_normalized_fp32(const BgcQuaternionFP32* quaternion, BgcQuaternionFP32* normalized) -{ - bgc_quaternion_copy_fp32(quaternion, normalized); - return bgc_quaternion_normalize_fp32(normalized); -} - -inline int bgc_quaternion_get_normalized_fp64(const BgcQuaternionFP64* quaternion, BgcQuaternionFP64* normalized) -{ - bgc_quaternion_copy_fp64(quaternion, normalized); - return bgc_quaternion_normalize_fp64(normalized); -} - -// ================== Product =================== // +// ================ Get Product ================= // inline void bgc_quaternion_get_product_fp32(const BgcQuaternionFP32* left, const BgcQuaternionFP32* right, BgcQuaternionFP32* product) { @@ -315,164 +323,52 @@ inline void bgc_quaternion_get_product_fp64(const BgcQuaternionFP64* left, const product->x3 = x3; } -// ============ Get Rotation Matrix ============= // +// ================= Get Ratio ================== // -inline void bgc_quaternion_get_rotation_matrix_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* rotation) +inline int bgc_quaternion_get_ratio_fp32(const BgcQuaternionFP32* divident, const BgcQuaternionFP32* divisor, BgcQuaternionFP32* quotient) { - const float s0s0 = quaternion->s0 * quaternion->s0; - const float x1x1 = quaternion->x1 * quaternion->x1; - const float x2x2 = quaternion->x2 * quaternion->x2; - const float x3x3 = quaternion->x3 * quaternion->x3; + const float square_modulus = bgc_quaternion_get_square_modulus_fp32(divisor); - const float square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); - - if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) - { - bgc_matrix3x3_set_to_identity_fp32(rotation); - return; + if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) { + return 0; } - const float corrector1 = 1.0f / square_modulus; + const float s0 = (divident->s0 * divisor->s0 + divident->x1 * divisor->x1) + (divident->x2 * divisor->x2 + divident->x3 * divisor->x3); + const float x1 = (divident->x1 * divisor->s0 + divident->x3 * divisor->x2) - (divident->s0 * divisor->x1 + divident->x2 * divisor->x3); + const float x2 = (divident->x2 * divisor->s0 + divident->x1 * divisor->x3) - (divident->s0 * divisor->x2 + divident->x3 * divisor->x1); + const float x3 = (divident->x3 * divisor->s0 + divident->x2 * divisor->x1) - (divident->s0 * divisor->x3 + divident->x1 * divisor->x2); - const float s0x1 = quaternion->s0 * quaternion->x1; - const float s0x2 = quaternion->s0 * quaternion->x2; - const float s0x3 = quaternion->s0 * quaternion->x3; - const float x1x2 = quaternion->x1 * quaternion->x2; - const float x1x3 = quaternion->x1 * quaternion->x3; - const float x2x3 = quaternion->x2 * quaternion->x3; + const float multiplicand = 1.0f / square_modulus; - const float corrector2 = 2.0f * corrector1; + quotient->s0 = s0 * multiplicand; + quotient->x1 = x1 * multiplicand; + quotient->x2 = x2 * multiplicand; + quotient->x3 = x3 * multiplicand; - rotation->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3)); - rotation->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3)); - rotation->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2)); - - rotation->r1c2 = corrector2 * (x1x2 - s0x3); - rotation->r2c3 = corrector2 * (x2x3 - s0x1); - rotation->r3c1 = corrector2 * (x1x3 - s0x2); - - rotation->r2c1 = corrector2 * (x1x2 + s0x3); - rotation->r3c2 = corrector2 * (x2x3 + s0x1); - rotation->r1c3 = corrector2 * (x1x3 + s0x2); + return 1; } -inline void bgc_quaternion_get_rotation_matrix_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* rotation) +inline int bgc_quaternion_get_ratio_fp64(const BgcQuaternionFP64* divident, const BgcQuaternionFP64* divisor, BgcQuaternionFP64* quotient) { - const double s0s0 = quaternion->s0 * quaternion->s0; - const double x1x1 = quaternion->x1 * quaternion->x1; - const double x2x2 = quaternion->x2 * quaternion->x2; - const double x3x3 = quaternion->x3 * quaternion->x3; + const double square_modulus = bgc_quaternion_get_square_modulus_fp64(divisor); - const double square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); - - if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) - { - bgc_matrix3x3_set_to_identity_fp64(rotation); - return; + if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) { + return 0; } - const double corrector1 = 1.0f / square_modulus; + const double s0 = (divident->s0 * divisor->s0 + divident->x1 * divisor->x1) + (divident->x2 * divisor->x2 + divident->x3 * divisor->x3); + const double x1 = (divident->x1 * divisor->s0 + divident->x3 * divisor->x2) - (divident->s0 * divisor->x1 + divident->x2 * divisor->x3); + const double x2 = (divident->x2 * divisor->s0 + divident->x1 * divisor->x3) - (divident->s0 * divisor->x2 + divident->x3 * divisor->x1); + const double x3 = (divident->x3 * divisor->s0 + divident->x2 * divisor->x1) - (divident->s0 * divisor->x3 + divident->x1 * divisor->x2); - const double s0x1 = quaternion->s0 * quaternion->x1; - const double s0x2 = quaternion->s0 * quaternion->x2; - const double s0x3 = quaternion->s0 * quaternion->x3; - const double x1x2 = quaternion->x1 * quaternion->x2; - const double x1x3 = quaternion->x1 * quaternion->x3; - const double x2x3 = quaternion->x2 * quaternion->x3; + const double multiplicand = 1.0 / square_modulus; - const double corrector2 = 2.0f * corrector1; + quotient->s0 = s0 * multiplicand; + quotient->x1 = x1 * multiplicand; + quotient->x2 = x2 * multiplicand; + quotient->x3 = x3 * multiplicand; - rotation->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3)); - rotation->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3)); - rotation->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2)); - - rotation->r1c2 = corrector2 * (x1x2 - s0x3); - rotation->r2c3 = corrector2 * (x2x3 - s0x1); - rotation->r3c1 = corrector2 * (x1x3 - s0x2); - - rotation->r2c1 = corrector2 * (x1x2 + s0x3); - rotation->r3c2 = corrector2 * (x2x3 + s0x1); - rotation->r1c3 = corrector2 * (x1x3 + s0x2); -} - -// ============= Get Reverse Matrix ============= // - -inline void bgc_quaternion_get_reverse_matrix_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* reverse) -{ - const float s0s0 = quaternion->s0 * quaternion->s0; - const float x1x1 = quaternion->x1 * quaternion->x1; - const float x2x2 = quaternion->x2 * quaternion->x2; - const float x3x3 = quaternion->x3 * quaternion->x3; - - const float square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); - - if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) - { - bgc_matrix3x3_set_to_identity_fp32(reverse); - return; - } - - const float corrector1 = 1.0f / square_modulus; - - const float s0x1 = quaternion->s0 * quaternion->x1; - const float s0x2 = quaternion->s0 * quaternion->x2; - const float s0x3 = quaternion->s0 * quaternion->x3; - const float x1x2 = quaternion->x1 * quaternion->x2; - const float x1x3 = quaternion->x1 * quaternion->x3; - const float x2x3 = quaternion->x2 * quaternion->x3; - - const float corrector2 = 2.0f * corrector1; - - reverse->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3)); - reverse->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3)); - reverse->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2)); - - reverse->r1c2 = corrector2 * (x1x2 + s0x3); - reverse->r2c3 = corrector2 * (x2x3 + s0x1); - reverse->r3c1 = corrector2 * (x1x3 + s0x2); - - reverse->r2c1 = corrector2 * (x1x2 - s0x3); - reverse->r3c2 = corrector2 * (x2x3 - s0x1); - reverse->r1c3 = corrector2 * (x1x3 - s0x2); -} - -inline void bgc_quaternion_get_reverse_matrix_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* reverse) -{ - const double s0s0 = quaternion->s0 * quaternion->s0; - const double x1x1 = quaternion->x1 * quaternion->x1; - const double x2x2 = quaternion->x2 * quaternion->x2; - const double x3x3 = quaternion->x3 * quaternion->x3; - - const double square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); - - if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) - { - bgc_matrix3x3_set_to_identity_fp64(reverse); - return; - } - - const double corrector1 = 1.0f / square_modulus; - - const double s0x1 = quaternion->s0 * quaternion->x1; - const double s0x2 = quaternion->s0 * quaternion->x2; - const double s0x3 = quaternion->s0 * quaternion->x3; - const double x1x2 = quaternion->x1 * quaternion->x2; - const double x1x3 = quaternion->x1 * quaternion->x3; - const double x2x3 = quaternion->x2 * quaternion->x3; - - const double corrector2 = 2.0f * corrector1; - - reverse->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3)); - reverse->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3)); - reverse->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2)); - - reverse->r1c2 = corrector2 * (x1x2 + s0x3); - reverse->r2c3 = corrector2 * (x2x3 + s0x1); - reverse->r3c1 = corrector2 * (x1x3 + s0x2); - - reverse->r2c1 = corrector2 * (x1x2 - s0x3); - reverse->r3c2 = corrector2 * (x2x3 - s0x1); - reverse->r1c3 = corrector2 * (x1x3 - s0x2); + return 1; } // ==================== Add ===================== // @@ -599,6 +495,190 @@ inline void bgc_quaternion_get_linear_interpolation_fp64(const BgcQuaternionFP64 interpolation->x3 = quaternion1->x3 * counterphase + quaternion2->x3 * phase; } +// ============ Get Rotation Matrix ============= // + +inline int bgc_quaternion_get_rotation_matrix_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* rotation) +{ + const float s0s0 = quaternion->s0 * quaternion->s0; + const float x1x1 = quaternion->x1 * quaternion->x1; + const float x2x2 = quaternion->x2 * quaternion->x2; + const float x3x3 = quaternion->x3 * quaternion->x3; + + const float square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) + { + return 0; + } + + const float corrector1 = 1.0f / square_modulus; + + const float s0x1 = quaternion->s0 * quaternion->x1; + const float s0x2 = quaternion->s0 * quaternion->x2; + const float s0x3 = quaternion->s0 * quaternion->x3; + const float x1x2 = quaternion->x1 * quaternion->x2; + const float x1x3 = quaternion->x1 * quaternion->x3; + const float x2x3 = quaternion->x2 * quaternion->x3; + + const float corrector2 = 2.0f * corrector1; + + rotation->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3)); + rotation->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3)); + rotation->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2)); + + rotation->r1c2 = corrector2 * (x1x2 - s0x3); + rotation->r2c3 = corrector2 * (x2x3 - s0x1); + rotation->r3c1 = corrector2 * (x1x3 - s0x2); + + rotation->r2c1 = corrector2 * (x1x2 + s0x3); + rotation->r3c2 = corrector2 * (x2x3 + s0x1); + rotation->r1c3 = corrector2 * (x1x3 + s0x2); + + return 1; +} + +inline int bgc_quaternion_get_rotation_matrix_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* rotation) +{ + const double s0s0 = quaternion->s0 * quaternion->s0; + const double x1x1 = quaternion->x1 * quaternion->x1; + const double x2x2 = quaternion->x2 * quaternion->x2; + const double x3x3 = quaternion->x3 * quaternion->x3; + + const double square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) + { + return 0; + } + + const double corrector1 = 1.0f / square_modulus; + + const double s0x1 = quaternion->s0 * quaternion->x1; + const double s0x2 = quaternion->s0 * quaternion->x2; + const double s0x3 = quaternion->s0 * quaternion->x3; + const double x1x2 = quaternion->x1 * quaternion->x2; + const double x1x3 = quaternion->x1 * quaternion->x3; + const double x2x3 = quaternion->x2 * quaternion->x3; + + const double corrector2 = 2.0f * corrector1; + + rotation->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3)); + rotation->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3)); + rotation->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2)); + + rotation->r1c2 = corrector2 * (x1x2 - s0x3); + rotation->r2c3 = corrector2 * (x2x3 - s0x1); + rotation->r3c1 = corrector2 * (x1x3 - s0x2); + + rotation->r2c1 = corrector2 * (x1x2 + s0x3); + rotation->r3c2 = corrector2 * (x2x3 + s0x1); + rotation->r1c3 = corrector2 * (x1x3 + s0x2); + + return 1; +} + +// ============= Get Reverse Matrix ============= // + +inline int bgc_quaternion_get_reverse_matrix_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* reverse) +{ + const float s0s0 = quaternion->s0 * quaternion->s0; + const float x1x1 = quaternion->x1 * quaternion->x1; + const float x2x2 = quaternion->x2 * quaternion->x2; + const float x3x3 = quaternion->x3 * quaternion->x3; + + const float square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) + { + return 0; + } + + const float corrector1 = 1.0f / square_modulus; + + const float s0x1 = quaternion->s0 * quaternion->x1; + const float s0x2 = quaternion->s0 * quaternion->x2; + const float s0x3 = quaternion->s0 * quaternion->x3; + const float x1x2 = quaternion->x1 * quaternion->x2; + const float x1x3 = quaternion->x1 * quaternion->x3; + const float x2x3 = quaternion->x2 * quaternion->x3; + + const float corrector2 = 2.0f * corrector1; + + reverse->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3)); + reverse->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3)); + reverse->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2)); + + reverse->r1c2 = corrector2 * (x1x2 + s0x3); + reverse->r2c3 = corrector2 * (x2x3 + s0x1); + reverse->r3c1 = corrector2 * (x1x3 + s0x2); + + reverse->r2c1 = corrector2 * (x1x2 - s0x3); + reverse->r3c2 = corrector2 * (x2x3 - s0x1); + reverse->r1c3 = corrector2 * (x1x3 - s0x2); + + return 1; +} + +inline int bgc_quaternion_get_reverse_matrix_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* reverse) +{ + const double s0s0 = quaternion->s0 * quaternion->s0; + const double x1x1 = quaternion->x1 * quaternion->x1; + const double x2x2 = quaternion->x2 * quaternion->x2; + const double x3x3 = quaternion->x3 * quaternion->x3; + + const double square_modulus = (s0s0 + x1x1) + (x2x2 + x3x3); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) + { + return 0; + } + + const double corrector1 = 1.0f / square_modulus; + + const double s0x1 = quaternion->s0 * quaternion->x1; + const double s0x2 = quaternion->s0 * quaternion->x2; + const double s0x3 = quaternion->s0 * quaternion->x3; + const double x1x2 = quaternion->x1 * quaternion->x2; + const double x1x3 = quaternion->x1 * quaternion->x3; + const double x2x3 = quaternion->x2 * quaternion->x3; + + const double corrector2 = 2.0f * corrector1; + + reverse->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3)); + reverse->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3)); + reverse->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2)); + + reverse->r1c2 = corrector2 * (x1x2 + s0x3); + reverse->r2c3 = corrector2 * (x2x3 + s0x1); + reverse->r3c1 = corrector2 * (x1x3 + s0x2); + + reverse->r2c1 = corrector2 * (x1x2 - s0x3); + reverse->r3c2 = corrector2 * (x2x3 - s0x1); + reverse->r1c3 = corrector2 * (x1x3 - s0x2); + + return 1; +} + +inline int bgc_quaternion_get_both_matrixes_fp32(const BgcQuaternionFP32* quaternion, BgcMatrix3x3FP32* rotation, BgcMatrix3x3FP32* reverse) +{ + if (bgc_quaternion_get_reverse_matrix_fp32(quaternion, reverse)) { + bgc_matrix3x3_transpose_fp32(reverse, rotation); + return 1; + } + + return 0; +} + +inline int bgc_quaternion_get_both_matrixes_fp64(const BgcQuaternionFP64* quaternion, BgcMatrix3x3FP64* rotation, BgcMatrix3x3FP64* reverse) +{ + if (bgc_quaternion_get_reverse_matrix_fp64(quaternion, reverse)) { + bgc_matrix3x3_transpose_fp64(reverse, rotation); + return 1; + } + + return 0; +} + // ================== Are Close ================= // inline int bgc_quaternion_are_close_fp32(const BgcQuaternionFP32* quaternion1, const BgcQuaternionFP32* quaternion2) diff --git a/basic-geometry/rotation3.h b/basic-geometry/rotation3.h index 9564d37..3ba45d1 100644 --- a/basic-geometry/rotation3.h +++ b/basic-geometry/rotation3.h @@ -47,7 +47,7 @@ inline void bgc_rotation3_set_values_fp32(const float x1, const float x2, const rotation->axis.x2 = x2; rotation->axis.x3 = x3; - if (bgc_vector3_normalize_fp32(&rotation->axis)) { + if (bgc_vector3_normalize_fp32(&rotation->axis, &rotation->axis)) { rotation->radians = bgc_angle_to_radians_fp32(angle, unit); } else { @@ -62,7 +62,7 @@ inline void bgc_rotation3_set_values_fp64(const double x1, const double x2, cons rotation->axis.x2 = x2; rotation->axis.x3 = x3; - if (bgc_vector3_normalize_fp64(&rotation->axis)) { + if (bgc_vector3_normalize_fp64(&rotation->axis, &rotation->axis)) { rotation->radians = bgc_angle_to_radians_fp64(angle, unit); } else { @@ -76,7 +76,7 @@ inline void bgc_rotation3_set_with_axis_fp32(const BgcVector3FP32* axis, const f rotation->axis.x2 = axis->x2; rotation->axis.x3 = axis->x3; - if (bgc_vector3_normalize_fp32(&rotation->axis)) { + if (bgc_vector3_normalize_fp32(&rotation->axis, &rotation->axis)) { rotation->radians = bgc_angle_to_radians_fp32(angle, unit); } else { @@ -90,7 +90,7 @@ inline void bgc_rotation3_set_with_axis_fp64(const BgcVector3FP64* axis, const d rotation->axis.x2 = axis->x2; rotation->axis.x3 = axis->x3; - if (bgc_vector3_normalize_fp64(&rotation->axis)) { + if (bgc_vector3_normalize_fp64(&rotation->axis, &rotation->axis)) { rotation->radians = bgc_angle_to_radians_fp64(angle, unit); } else { diff --git a/basic-geometry/tangent-pair.c b/basic-geometry/tangent-pair.c index 5eb29ad..c38571a 100644 --- a/basic-geometry/tangent-pair.c +++ b/basic-geometry/tangent-pair.c @@ -16,24 +16,21 @@ extern inline void bgc_tangent_pair_set_turn_fp64(const double angle, const BgcA extern inline float bgc_tangent_pair_get_angle_fp32(const BgcTangentPairFP32* tangent, const BgcAngleUnitEnum unit); extern inline double bgc_tangent_pair_get_angle_fp64(const BgcTangentPairFP64* tangent, const BgcAngleUnitEnum unit); -extern inline void bgc_tangent_pair_copy_fp32(const BgcTangentPairFP32* from, BgcTangentPairFP32* to); -extern inline void bgc_tangent_pair_copy_fp64(const BgcTangentPairFP64* from, BgcTangentPairFP64* to); +extern inline void bgc_tangent_pair_copy_fp32(const BgcTangentPairFP32* source, BgcTangentPairFP32* destination); +extern inline void bgc_tangent_pair_copy_fp64(const BgcTangentPairFP64* source, BgcTangentPairFP64* destination); extern inline void bgc_tangent_pair_swap_fp32(BgcTangentPairFP32* tangent1, BgcTangentPairFP32* tangent2); extern inline void bgc_tangent_pair_swap_fp64(BgcTangentPairFP64* tangent1, BgcTangentPairFP64* tangent2); -extern inline void bgc_tangent_pair_convert_fp64_to_fp32(const BgcTangentPairFP64* from, BgcTangentPairFP32* to); -extern inline void bgc_tangent_pair_convert_fp32_to_fp64(const BgcTangentPairFP32* from, BgcTangentPairFP64* to); +extern inline void bgc_tangent_pair_convert_fp64_to_fp32(const BgcTangentPairFP64* source, BgcTangentPairFP32* destination); +extern inline void bgc_tangent_pair_convert_fp32_to_fp64(const BgcTangentPairFP32* source, BgcTangentPairFP64* destination); -extern inline void bgc_tangent_pair_invert_fp32(BgcTangentPairFP32* tangent); -extern inline void bgc_tangent_pair_invert_fp64(BgcTangentPairFP64* tangent); +extern inline void bgc_tangent_pair_invert_fp32(const BgcTangentPairFP32* tangent, BgcTangentPairFP32* inverted); +extern inline void bgc_tangent_pair_invert_fp64(const BgcTangentPairFP64* tangent, BgcTangentPairFP64* inverted); extern inline void bgc_tangent_pair_combine_fp32(const BgcTangentPairFP32* tangent1, const BgcTangentPairFP32* tangent2, BgcTangentPairFP32* result); extern inline void bgc_tangent_pair_combine_fp64(const BgcTangentPairFP64* tangent1, const BgcTangentPairFP64* tangent2, BgcTangentPairFP64* result); -extern inline void bgc_tangent_pair_get_inverted_fp32(const BgcTangentPairFP32* tangent, BgcTangentPairFP32* result); -extern inline void bgc_tangent_pair_get_inverted_fp64(const BgcTangentPairFP64* tangent, BgcTangentPairFP64* result); - extern inline void bgc_tangent_pair_get_rotation_matrix_fp32(const BgcTangentPairFP32* tangent, BgcMatrix2x2FP32* matrix); extern inline void bgc_tangent_pair_get_rotation_matrix_fp64(const BgcTangentPairFP64* tangent, BgcMatrix2x2FP64* matrix); diff --git a/basic-geometry/tangent-pair.h b/basic-geometry/tangent-pair.h index 8305fbf..40c5c15 100644 --- a/basic-geometry/tangent-pair.h +++ b/basic-geometry/tangent-pair.h @@ -155,20 +155,20 @@ inline double bgc_tangent_pair_get_angle_fp64(const BgcTangentPairFP64* tangent, // ==================== Copy ==================== // -inline void bgc_tangent_pair_copy_fp32(const BgcTangentPairFP32* from, BgcTangentPairFP32* to) +inline void bgc_tangent_pair_copy_fp32(const BgcTangentPairFP32* source, BgcTangentPairFP32* destination) { - _BgcTwinTangentPairFP32* twin = (_BgcTwinTangentPairFP32*)to; + _BgcTwinTangentPairFP32* twin = (_BgcTwinTangentPairFP32*)destination; - twin->cos = from->cos; - twin->sin = from->sin; + twin->cos = source->cos; + twin->sin = source->sin; } -inline void bgc_tangent_pair_copy_fp64(const BgcTangentPairFP64* from, BgcTangentPairFP64* to) +inline void bgc_tangent_pair_copy_fp64(const BgcTangentPairFP64* source, BgcTangentPairFP64* destination) { - _BgcTwinTangentPairFP64* twin = (_BgcTwinTangentPairFP64*)to; + _BgcTwinTangentPairFP64* twin = (_BgcTwinTangentPairFP64*)destination; - twin->cos = from->cos; - twin->sin = from->sin; + twin->cos = source->cos; + twin->sin = source->sin; } // ==================== Swap ==================== // @@ -207,26 +207,32 @@ inline void bgc_tangent_pair_swap_fp64(BgcTangentPairFP64* tangent1, BgcTangentP // ================== Convert =================== // -inline void bgc_tangent_pair_convert_fp64_to_fp32(const BgcTangentPairFP64* from, BgcTangentPairFP32* to) +inline void bgc_tangent_pair_convert_fp64_to_fp32(const BgcTangentPairFP64* source, BgcTangentPairFP32* destination) { - bgc_tangent_pair_set_values_fp32((float)from->cos, (float)from->sin, to); + bgc_tangent_pair_set_values_fp32((float)source->cos, (float)source->sin, destination); } -inline void bgc_tangent_pair_convert_fp32_to_fp64(const BgcTangentPairFP32* from, BgcTangentPairFP64* to) +inline void bgc_tangent_pair_convert_fp32_to_fp64(const BgcTangentPairFP32* source, BgcTangentPairFP64* destination) { - bgc_tangent_pair_set_values_fp64((double)from->cos, (double)from->sin, to); + bgc_tangent_pair_set_values_fp64((double)source->cos, (double)source->sin, destination); } // =================== Invert =================== // -inline void bgc_tangent_pair_invert_fp32(BgcTangentPairFP32* tangent) +inline void bgc_tangent_pair_invert_fp32(const BgcTangentPairFP32* tangent, BgcTangentPairFP32* inverted) { - ((_BgcTwinTangentPairFP32*)tangent)->sin = -tangent->sin; + _BgcTwinTangentPairFP32* twin = (_BgcTwinTangentPairFP32*)inverted; + + twin->cos = tangent->cos; + twin->sin = -tangent->sin; } -inline void bgc_tangent_pair_invert_fp64(BgcTangentPairFP64* tangent) +inline void bgc_tangent_pair_invert_fp64(const BgcTangentPairFP64* tangent, BgcTangentPairFP64* inverted) { - ((_BgcTwinTangentPairFP64*)tangent)->sin = -tangent->sin; + _BgcTwinTangentPairFP64* twin = (_BgcTwinTangentPairFP64*)inverted; + + twin->cos = tangent->cos; + twin->sin = -tangent->sin; } // ================ Combination ================= // @@ -249,24 +255,6 @@ inline void bgc_tangent_pair_combine_fp64(const BgcTangentPairFP64* tangent1, co ); } -// ================ Set Inverted ================ // - -inline void bgc_tangent_pair_get_inverted_fp32(const BgcTangentPairFP32* tangent, BgcTangentPairFP32* result) -{ - _BgcTwinTangentPairFP32* twin = (_BgcTwinTangentPairFP32*)result; - - twin->cos = tangent->cos; - twin->sin = -tangent->sin; -} - -inline void bgc_tangent_pair_get_inverted_fp64(const BgcTangentPairFP64* tangent, BgcTangentPairFP64* result) -{ - _BgcTwinTangentPairFP64* twin = (_BgcTwinTangentPairFP64*)result; - - twin->cos = tangent->cos; - twin->sin = -tangent->sin; -} - // ============== Rotation Matrix =============== // inline void bgc_tangent_pair_get_rotation_matrix_fp32(const BgcTangentPairFP32* tangent, BgcMatrix2x2FP32* matrix) diff --git a/basic-geometry/utilities.h b/basic-geometry/utilities.h index 4b2887d..09d4866 100644 --- a/basic-geometry/utilities.h +++ b/basic-geometry/utilities.h @@ -6,9 +6,10 @@ #define BGC_EPSYLON_FP32 4.76837E-7f #define BGC_SQUARE_EPSYLON_FP32 2.27373906E-13f -#define BGC_ONE_THIRD_FP32 0.333333333f -#define BGC_ONE_SIXTH_FP32 0.166666667f -#define BGC_ONE_NINETH_FP32 0.111111111f +#define BGC_ONE_THIRD_FP32 0.3333333333f +#define BGC_ONE_SIXTH_FP32 0.1666666667f +#define BGC_ONE_SEVENTH_FP32 0.142857142857f +#define BGC_ONE_NINETH_FP32 0.1111111111f #define BGC_GOLDEN_RATIO_HIGH_FP32 1.618034f #define BGC_GOLDEN_RATIO_LOW_FP32 0.618034f @@ -18,32 +19,33 @@ #define BGC_EPSYLON_FP64 4.996003611E-14 #define BGC_SQUARE_EPSYLON_FP64 2.496005208112504E-27 -#define BGC_ONE_THIRD_FP64 0.333333333333333333 -#define BGC_ONE_SIXTH_FP64 0.166666666666666667 -#define BGC_ONE_NINETH_FP64 0.111111111111111111 +#define BGC_ONE_THIRD_FP64 0.3333333333333333333 +#define BGC_ONE_SIXTH_FP64 0.1666666666666666667 +#define BGC_ONE_SEVENTH_FP64 0.142857142857142857 +#define BGC_ONE_NINETH_FP64 0.1111111111111111111 #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) +inline int bgc_is_zero_fp32(const float value) { - return (-BGC_EPSYLON_FP32 <= square_value) && (square_value <= BGC_EPSYLON_FP32); + return (-BGC_EPSYLON_FP32 <= value) && (value <= BGC_EPSYLON_FP32); } -inline int bgc_is_zero_fp64(const double square_value) +inline int bgc_is_zero_fp64(const double value) { - return (-BGC_EPSYLON_FP64 <= square_value) && (square_value <= BGC_EPSYLON_FP64); + return (-BGC_EPSYLON_FP64 <= value) && (value <= BGC_EPSYLON_FP64); } -inline int bgc_is_unit_fp32(const float square_value) +inline int bgc_is_unit_fp32(const float value) { - return (1.0f - BGC_EPSYLON_FP32 <= square_value) && (square_value <= 1.0f + BGC_EPSYLON_FP32); + return (1.0f - BGC_EPSYLON_FP32 <= value) && (value <= 1.0f + BGC_EPSYLON_FP32); } -inline int bgc_is_unit_fp64(const double square_value) +inline int bgc_is_unit_fp64(const double value) { - return (1.0 - BGC_EPSYLON_FP64 <= square_value) && (square_value <= 1.0 + BGC_EPSYLON_FP64); + return (1.0 - BGC_EPSYLON_FP64 <= value) && (value <= 1.0 + BGC_EPSYLON_FP64); } diff --git a/basic-geometry/vector2.c b/basic-geometry/vector2.c index f88cea8..af738d4 100644 --- a/basic-geometry/vector2.c +++ b/basic-geometry/vector2.c @@ -3,45 +3,42 @@ extern inline void bgc_vector2_reset_fp32(BgcVector2FP32* vector); extern inline void bgc_vector2_reset_fp64(BgcVector2FP64* vector); -extern inline void bgc_vector2_set_values_fp32(const float x1, const float x2, BgcVector2FP32* to); -extern inline void bgc_vector2_set_values_fp64(const double x1, const double x2, BgcVector2FP64* to); - -extern inline float bgc_vector2_get_square_modulus_fp32(const BgcVector2FP32* vector); -extern inline double bgc_vector2_get_square_modulus_fp64(const BgcVector2FP64* vector); - -extern inline float bgc_vector2_get_modulus_fp32(const BgcVector2FP32* vector); -extern inline double bgc_vector2_get_modulus_fp64(const BgcVector2FP64* vector); - -extern inline int bgc_vector2_is_zero_fp32(const BgcVector2FP32* vector); -extern inline int bgc_vector2_is_zero_fp64(const BgcVector2FP64* vector); - -extern inline int bgc_vector2_is_unit_fp32(const BgcVector2FP32* vector); -extern inline int bgc_vector2_is_unit_fp64(const BgcVector2FP64* vector); +extern inline void bgc_vector2_set_values_fp32(const float x1, const float x2, BgcVector2FP32* destination); +extern inline void bgc_vector2_set_values_fp64(const double x1, const double x2, BgcVector2FP64* destination); -extern inline void bgc_vector2_copy_fp32(const BgcVector2FP32* from, BgcVector2FP32* to); -extern inline void bgc_vector2_copy_fp64(const BgcVector2FP64* from, BgcVector2FP64* to); +extern inline float bgc_vector2_get_square_modulus_fp32(const BgcVector2FP32* vector); +extern inline double bgc_vector2_get_square_modulus_fp64(const BgcVector2FP64* vector); + +extern inline float bgc_vector2_get_modulus_fp32(const BgcVector2FP32* vector); +extern inline double bgc_vector2_get_modulus_fp64(const BgcVector2FP64* vector); + +extern inline int bgc_vector2_is_zero_fp32(const BgcVector2FP32* vector); +extern inline int bgc_vector2_is_zero_fp64(const BgcVector2FP64* vector); + +extern inline int bgc_vector2_is_unit_fp32(const BgcVector2FP32* vector); +extern inline int bgc_vector2_is_unit_fp64(const BgcVector2FP64* vector); + +extern inline void bgc_vector2_copy_fp32(const BgcVector2FP32* source, BgcVector2FP32* destination); +extern inline void bgc_vector2_copy_fp64(const BgcVector2FP64* source, BgcVector2FP64* destination); extern inline void bgc_vector2_swap_fp32(BgcVector2FP32* vector1, BgcVector2FP32* vector2); extern inline void bgc_vector2_swap_fp64(BgcVector2FP64* vector1, BgcVector2FP64* vector2); -extern inline void bgc_vector2_convert_fp64_to_fp32(const BgcVector2FP64* from, BgcVector2FP32* to); -extern inline void bgc_vector2_convert_fp32_to_fp64(const BgcVector2FP32* from, BgcVector2FP64* to); +extern inline void bgc_vector2_convert_fp64_to_fp32(const BgcVector2FP64* source, BgcVector2FP32* destination); +extern inline void bgc_vector2_convert_fp32_to_fp64(const BgcVector2FP32* source, BgcVector2FP64* destination); + +extern inline void bgc_vector2_reverse_fp32(const BgcVector2FP32* vector, BgcVector2FP32* reverse); +extern inline void bgc_vector2_reverse_fp64(const BgcVector2FP64* vector, BgcVector2FP64* reverse); + +extern inline int bgc_vector2_normalize_fp32(const BgcVector2FP32* vector, BgcVector2FP32* normalized); +extern inline int bgc_vector2_normalize_fp64(const BgcVector2FP64* vector, BgcVector2FP64* normalized); + +extern inline void bgc_vector2_complex_conjugate_fp32(const BgcVector2FP32* vector, BgcVector2FP32* conjugate); +extern inline void bgc_vector2_complex_conjugate_fp64(const BgcVector2FP64* vector, BgcVector2FP64* conjugate); + +extern inline int bgc_vector2_complex_invert_fp32(const BgcVector2FP32* vector, BgcVector2FP32* inverted); +extern inline int bgc_vector2_complex_invert_fp64(const BgcVector2FP64* vector, BgcVector2FP64* inverted); -extern inline void bgc_vector2_reverse_fp32(BgcVector2FP32* vector); -extern inline void bgc_vector2_reverse_fp64(BgcVector2FP64* vector); - -extern inline int bgc_vector2_normalize_fp32(BgcVector2FP32* vector); -extern inline int bgc_vector2_normalize_fp64(BgcVector2FP64* vector); - -extern inline void bgc_vector2_get_reverse_fp32(const BgcVector2FP32* vector, BgcVector2FP32* reverse); -extern inline void bgc_vector2_get_reverse_fp64(const BgcVector2FP64* vector, BgcVector2FP64* reverse); - -extern inline int bgc_vector2_get_normalized_fp32(const BgcVector2FP32* vector, BgcVector2FP32* result); -extern inline int bgc_vector2_get_normalized_fp64(const BgcVector2FP64* vector, BgcVector2FP64* result); - -extern inline void bgc_vector2_get_complex_conjugate_fp32(const BgcVector2FP32* vector, BgcVector2FP32* conjugate); -extern inline void bgc_vector2_get_complex_conjugate_fp64(const BgcVector2FP64* vector, BgcVector2FP64* conjugate); - extern inline void bgc_vector2_add_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, BgcVector2FP32* sum); extern inline void bgc_vector2_add_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2, BgcVector2FP64* sum); @@ -51,9 +48,9 @@ extern inline void bgc_vector2_add_scaled_fp64(const BgcVector2FP64* basic_vecto extern inline void bgc_vector2_subtract_fp32(const BgcVector2FP32* minuend, const BgcVector2FP32* subtrahend, BgcVector2FP32* difference); extern inline void bgc_vector2_subtract_fp64(const BgcVector2FP64* minuend, const BgcVector2FP64* subtrahend, BgcVector2FP64* difference); -extern inline void bgc_vector2_subtract_scaled_fp32(const BgcVector2FP32* basic_vector, const BgcVector2FP32* scalable_vector, const float scale, BgcVector2FP32* difference); -extern inline void bgc_vector2_subtract_scaled_fp64(const BgcVector2FP64* basic_vector, const BgcVector2FP64* scalable_vector, const double scale, BgcVector2FP64* difference); - +extern inline void bgc_vector2_subtract_scaled_fp32(const BgcVector2FP32* basic_vector, const BgcVector2FP32* scalable_vector, const float scale, BgcVector2FP32* difference); +extern inline void bgc_vector2_subtract_scaled_fp64(const BgcVector2FP64* basic_vector, const BgcVector2FP64* scalable_vector, const double scale, BgcVector2FP64* difference); + extern inline void bgc_vector2_multiply_fp32(const BgcVector2FP32* multiplicand, const float multiplier, BgcVector2FP32* product); extern inline void bgc_vector2_multiply_fp64(const BgcVector2FP64* multiplicand, const double multiplier, BgcVector2FP64* product); @@ -64,16 +61,16 @@ extern inline void bgc_vector2_get_mean_of_two_fp32(const BgcVector2FP32* vector extern inline void bgc_vector2_get_mean_of_two_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2, BgcVector2FP64* mean); extern inline void bgc_vector2_get_mean_of_three_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, const BgcVector2FP32* vector3, BgcVector2FP32* mean); -extern inline void bgc_vector2_get_mean_of_three_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2, const BgcVector2FP64* vector3, BgcVector2FP64* mean); - -extern inline void bgc_vector2_get_linear_interpolation_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, const float phase, BgcVector2FP32* interpolation); -extern inline void bgc_vector2_get_linear_interpolation_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2, const double phase, BgcVector2FP64* interpolation); - -extern inline void bgc_vector2_minimize_fp32(const BgcVector2FP32* vector, BgcVector2FP32* minimal); -extern inline void bgc_vector2_minimize_fp64(const BgcVector2FP64* vector, BgcVector2FP64* minimal); - -extern inline void bgc_vector2_maximize_fp32(const BgcVector2FP32* vector, BgcVector2FP32* maximal); -extern inline void bgc_vector2_maximize_fp64(const BgcVector2FP64* vector, BgcVector2FP64* maximal); +extern inline void bgc_vector2_get_mean_of_three_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2, const BgcVector2FP64* vector3, BgcVector2FP64* mean); + +extern inline void bgc_vector2_get_linear_interpolation_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, const float phase, BgcVector2FP32* interpolation); +extern inline void bgc_vector2_get_linear_interpolation_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2, const double phase, BgcVector2FP64* interpolation); + +extern inline void bgc_vector2_minimize_fp32(const BgcVector2FP32* vector, BgcVector2FP32* minimal); +extern inline void bgc_vector2_minimize_fp64(const BgcVector2FP64* vector, BgcVector2FP64* minimal); + +extern inline void bgc_vector2_maximize_fp32(const BgcVector2FP32* vector, BgcVector2FP32* maximal); +extern inline void bgc_vector2_maximize_fp64(const BgcVector2FP64* vector, BgcVector2FP64* maximal); extern inline float bgc_vector2_get_scalar_product_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2); extern inline double bgc_vector2_get_scalar_product_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2); @@ -84,6 +81,9 @@ extern inline double bgc_vector2_get_cross_product_fp64(const BgcVector2FP64* ve extern inline void bgc_vector2_get_complex_product_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, BgcVector2FP32* product); extern inline void bgc_vector2_get_complex_product_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2, BgcVector2FP64* product); +extern inline int bgc_vector2_get_complex_ratio_fp32(const BgcVector2FP32* divident, const BgcVector2FP32* divisor, BgcVector2FP32* quotient); +extern inline int bgc_vector2_get_complex_ratio_fp64(const BgcVector2FP64* divident, const BgcVector2FP64* divisor, BgcVector2FP64* quotient); + extern inline float bgc_vector2_get_square_distance_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2); extern inline double bgc_vector2_get_square_distance_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2); @@ -96,48 +96,48 @@ extern inline int bgc_vector2_are_close_enough_fp64(const BgcVector2FP64* vector extern inline int bgc_vector2_are_close_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2); extern inline int bgc_vector2_are_close_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2); -// =============== Complex Power ================ // - -void bgc_vector2_get_complex_power_fp32(const BgcVector2FP32* base, const BgcVector2FP32* power, BgcVector2FP32* result) -{ - const float base_square_modulus = bgc_vector2_get_square_modulus_fp32(base); - - if (base_square_modulus <= BGC_SQUARE_EPSYLON_FP32) { - result->x1 = 0.0f; - result->x2 = 0.0f; - return; - } - - const float log_modulus = logf(base_square_modulus) * 0.5f; - const float angle = atan2f(base->x2, base->x1); - - const float result_modulus = expf(power->x1 * log_modulus - power->x2 * angle); - const float result_angle = power->x1 * angle + power->x2 * log_modulus; - - result->x1 = result_modulus * cosf(result_angle); - result->x2 = result_modulus * sinf(result_angle); -} - -void bgc_vector2_get_complex_power_fp64(const BgcVector2FP64* base, const BgcVector2FP64* power, BgcVector2FP64* result) -{ - const double base_square_modulus = bgc_vector2_get_square_modulus_fp64(base); - - if (base_square_modulus <= BGC_SQUARE_EPSYLON_FP64) { - result->x1 = 0.0; - result->x2 = 0.0; - return; - } - - const double log_modulus = log(base_square_modulus) * 0.5; - const double angle = atan2(base->x2, base->x1); - - const double result_modulus = exp(power->x1 * log_modulus - power->x2 * angle); - const double result_angle = power->x1 * angle + power->x2 * log_modulus; - - result->x1 = result_modulus * cos(result_angle); - result->x2 = result_modulus * sin(result_angle); -} - +// =============== Complex Power ================ // + +void bgc_vector2_get_complex_power_fp32(const BgcVector2FP32* base, const BgcVector2FP32* power, BgcVector2FP32* result) +{ + const float base_square_modulus = bgc_vector2_get_square_modulus_fp32(base); + + if (base_square_modulus <= BGC_SQUARE_EPSYLON_FP32) { + result->x1 = 0.0f; + result->x2 = 0.0f; + return; + } + + const float log_modulus = logf(base_square_modulus) * 0.5f; + const float angle = atan2f(base->x2, base->x1); + + const float result_modulus = expf(power->x1 * log_modulus - power->x2 * angle); + const float result_angle = power->x1 * angle + power->x2 * log_modulus; + + result->x1 = result_modulus * cosf(result_angle); + result->x2 = result_modulus * sinf(result_angle); +} + +void bgc_vector2_get_complex_power_fp64(const BgcVector2FP64* base, const BgcVector2FP64* power, BgcVector2FP64* result) +{ + const double base_square_modulus = bgc_vector2_get_square_modulus_fp64(base); + + if (base_square_modulus <= BGC_SQUARE_EPSYLON_FP64) { + result->x1 = 0.0; + result->x2 = 0.0; + return; + } + + const double log_modulus = log(base_square_modulus) * 0.5; + const double angle = atan2(base->x2, base->x1); + + const double result_modulus = exp(power->x1 * log_modulus - power->x2 * angle); + const double result_angle = power->x1 * angle + power->x2 * log_modulus; + + result->x1 = result_modulus * cos(result_angle); + result->x2 = result_modulus * sin(result_angle); +} + // =================== Angle ==================== // float bgc_vector2_get_angle_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, const BgcAngleUnitEnum unit) diff --git a/basic-geometry/vector2.h b/basic-geometry/vector2.h index e085ad6..dc8ff3c 100644 --- a/basic-geometry/vector2.h +++ b/basic-geometry/vector2.h @@ -32,16 +32,16 @@ inline void bgc_vector2_reset_fp64(BgcVector2FP64* vector) // ==================== Set ===================== // -inline void bgc_vector2_set_values_fp32(const float x1, const float x2, BgcVector2FP32* to) +inline void bgc_vector2_set_values_fp32(const float x1, const float x2, BgcVector2FP32* destination) { - to->x1 = x1; - to->x2 = x2; + destination->x1 = x1; + destination->x2 = x2; } -inline void bgc_vector2_set_values_fp64(const double x1, const double x2, BgcVector2FP64* to) +inline void bgc_vector2_set_values_fp64(const double x1, const double x2, BgcVector2FP64* destination) { - to->x1 = x1; - to->x2 = x2; + destination->x1 = x1; + destination->x2 = x2; } // ================== Modulus =================== // @@ -90,16 +90,16 @@ inline int bgc_vector2_is_unit_fp64(const BgcVector2FP64* vector) // ==================== Copy ==================== // -inline void bgc_vector2_copy_fp32(const BgcVector2FP32* from, BgcVector2FP32* to) +inline void bgc_vector2_copy_fp32(const BgcVector2FP32* source, BgcVector2FP32* destination) { - to->x1 = from->x1; - to->x2 = from->x2; + destination->x1 = source->x1; + destination->x2 = source->x2; } -inline void bgc_vector2_copy_fp64(const BgcVector2FP64* from, BgcVector2FP64* to) +inline void bgc_vector2_copy_fp64(const BgcVector2FP64* source, BgcVector2FP64* destination) { - to->x1 = from->x1; - to->x2 = from->x2; + destination->x1 = source->x1; + destination->x2 = source->x2; } // ==================== Swap ==================== // @@ -130,132 +130,126 @@ inline void bgc_vector2_swap_fp64(BgcVector2FP64* vector1, BgcVector2FP64* vecto // ================== Convert =================== // -inline void bgc_vector2_convert_fp64_to_fp32(const BgcVector2FP64* from, BgcVector2FP32* to) +inline void bgc_vector2_convert_fp64_to_fp32(const BgcVector2FP64* source, BgcVector2FP32* destination) { - to->x1 = (float)from->x1; - to->x2 = (float)from->x2; + destination->x1 = (float)source->x1; + destination->x2 = (float)source->x2; } -inline void bgc_vector2_convert_fp32_to_fp64(const BgcVector2FP32* from, BgcVector2FP64* to) +inline void bgc_vector2_convert_fp32_to_fp64(const BgcVector2FP32* source, BgcVector2FP64* destination) { - to->x1 = from->x1; - to->x2 = from->x2; + destination->x1 = source->x1; + destination->x2 = source->x2; } // ================== Reverse =================== // -inline void bgc_vector2_reverse_fp32(BgcVector2FP32* vector) +inline void bgc_vector2_reverse_fp32(const BgcVector2FP32* vector, BgcVector2FP32* reverse) { - vector->x1 = -vector->x1; - vector->x2 = -vector->x2; + reverse->x1 = -vector->x1; + reverse->x2 = -vector->x2; } -inline void bgc_vector2_reverse_fp64(BgcVector2FP64* vector) +inline void bgc_vector2_reverse_fp64(const BgcVector2FP64* vector, BgcVector2FP64* reverse) { - vector->x1 = -vector->x1; - vector->x2 = -vector->x2; + reverse->x1 = -vector->x1; + reverse->x2 = -vector->x2; } // ================= Normalize ================== // -inline int bgc_vector2_normalize_fp32(BgcVector2FP32* vector) +inline int bgc_vector2_normalize_fp32(const BgcVector2FP32* vector, BgcVector2FP32* normalized) { const float square_modulus = bgc_vector2_get_square_modulus_fp32(vector); if (bgc_is_sqare_unit_fp32(square_modulus)) { + normalized->x1 = vector->x1; + normalized->x2 = vector->x2; return 1; } if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) { - vector->x1 = 0.0f; - vector->x2 = 0.0f; return 0; } const float multiplicand = sqrtf(1.0f / square_modulus); - vector->x1 *= multiplicand; - vector->x2 *= multiplicand; + normalized->x1 = vector->x1 * multiplicand; + normalized->x2 = vector->x2 * multiplicand; return 1; } -inline int bgc_vector2_normalize_fp64(BgcVector2FP64* vector) +inline int bgc_vector2_normalize_fp64(const BgcVector2FP64* vector, BgcVector2FP64* normalized) { const double square_modulus = bgc_vector2_get_square_modulus_fp64(vector); if (bgc_is_sqare_unit_fp64(square_modulus)) { + normalized->x1 = vector->x1; + normalized->x2 = vector->x2; return 1; } if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) { - vector->x1 = 0.0; - vector->x2 = 0.0; return 0; } const double multiplicand = sqrt(1.0 / square_modulus); - vector->x1 *= multiplicand; - vector->x2 *= multiplicand; + normalized->x1 = vector->x1 * multiplicand; + normalized->x2 = vector->x2 * multiplicand; return 1; } // ============= Complex Conjugate ============== // -inline void bgc_vector2_complex_conjugate_fp32(BgcVector2FP32* vector) -{ - vector->x2 = -vector->x2; -} - -inline void bgc_vector2_complex_conjugate_fp64(BgcVector2FP64* vector) -{ - vector->x2 = -vector->x2; -} - -// ================ Get Reverse ================= // - -inline void bgc_vector2_get_reverse_fp32(const BgcVector2FP32* vector, BgcVector2FP32* reverse) -{ - reverse->x1 = -vector->x1; - reverse->x2 = -vector->x2; -} - -inline void bgc_vector2_get_reverse_fp64(const BgcVector2FP64* vector, BgcVector2FP64* reverse) -{ - reverse->x1 = -vector->x1; - reverse->x2 = -vector->x2; -} - -// =============== Get Normalized =============== // - -inline int bgc_vector2_get_normalized_fp32(const BgcVector2FP32* vector, BgcVector2FP32* normalized) -{ - bgc_vector2_copy_fp32(vector, normalized); - return bgc_vector2_normalize_fp32(normalized); -} - -inline int bgc_vector2_get_normalized_fp64(const BgcVector2FP64* vector, BgcVector2FP64* normalized) -{ - bgc_vector2_copy_fp64(vector, normalized); - return bgc_vector2_normalize_fp64(normalized); -} - -// =========== Get Complex Conjugate ============ // - -inline void bgc_vector2_get_complex_conjugate_fp32(const BgcVector2FP32* vector, BgcVector2FP32* conjugate) +inline void bgc_vector2_complex_conjugate_fp32(const BgcVector2FP32* vector, BgcVector2FP32* conjugate) { conjugate->x1 = vector->x1; conjugate->x2 = -vector->x2; } -inline void bgc_vector2_get_complex_conjugate_fp64(const BgcVector2FP64* vector, BgcVector2FP64* conjugate) +inline void bgc_vector2_complex_conjugate_fp64(const BgcVector2FP64* vector, BgcVector2FP64* conjugate) { conjugate->x1 = vector->x1; conjugate->x2 = -vector->x2; } +// =============== Complex Invert =============== // + +inline int bgc_vector2_complex_invert_fp32(const BgcVector2FP32* vector, BgcVector2FP32* inverted) +{ + const float square_modulus = bgc_vector2_get_square_modulus_fp32(vector); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) { + return 0; + } + + const float multiplicand = 1.0f / square_modulus; + + inverted->x1 = vector->x1 * multiplicand; + inverted->x2 = -vector->x2 * multiplicand; + + return 1; +} + +inline int bgc_vector2_complex_invert_fp64(const BgcVector2FP64* vector, BgcVector2FP64* inverted) +{ + const double square_modulus = bgc_vector2_get_square_modulus_fp64(vector); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) { + return 0; + } + + const double multiplicand = 1.0 / square_modulus; + + inverted->x1 = vector->x1 * multiplicand; + inverted->x2 = -vector->x2 * multiplicand; + + return 1; +} + // ==================== Add ===================== // inline void bgc_vector2_add_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, BgcVector2FP32* sum) @@ -432,7 +426,7 @@ inline void bgc_vector2_maximize_fp64(const BgcVector2FP64* vector, BgcVector2FP } } -// =============== Scalar Product =============== // +// ============= Get Scalar Product ============= // inline float bgc_vector2_get_scalar_product_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2) { @@ -444,7 +438,7 @@ inline double bgc_vector2_get_scalar_product_fp64(const BgcVector2FP64* vector1, return vector1->x1 * vector2->x1 + vector1->x2 * vector2->x2; } -// =============== Cross Product ================ // +// ============= Get Cross Product ============== // inline float bgc_vector2_get_cross_product_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2) { @@ -456,7 +450,7 @@ inline double bgc_vector2_get_cross_product_fp64(const BgcVector2FP64* vector1, return vector1->x1 * vector2->x2 - vector1->x2 * vector2->x1; } -// ============== Complex Product =============== // +// ============ Get Complex Product ============= // inline void bgc_vector2_get_complex_product_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, BgcVector2FP32* result) { @@ -476,19 +470,59 @@ inline void bgc_vector2_get_complex_product_fp64(const BgcVector2FP64* vector1, result->x2 = x2; } -// =============== Complex Power ================ // +// ============= Get Complex Ratio ============== // + +inline int bgc_vector2_get_complex_ratio_fp32(const BgcVector2FP32* divident, const BgcVector2FP32* divisor, BgcVector2FP32* quotient) +{ + const float square_modulus = bgc_vector2_get_square_modulus_fp32(divisor); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP32) { + return 0; + } + + const float x1 = divident->x1 * divisor->x1 + divident->x2 * divisor->x2; + const float x2 = divident->x2 * divisor->x1 - divident->x1 * divisor->x2; + + const float multiplier = 1.0f / square_modulus; + + quotient->x1 = x1 * multiplier; + quotient->x2 = x2 * multiplier; + + return 1; +} + +inline int bgc_vector2_get_complex_ratio_fp64(const BgcVector2FP64* divident, const BgcVector2FP64* divisor, BgcVector2FP64* quotient) +{ + const double square_modulus = bgc_vector2_get_square_modulus_fp64(divisor); + + if (square_modulus <= BGC_SQUARE_EPSYLON_FP64) { + return 0; + } + + const double x1 = divident->x1 * divisor->x1 + divident->x2 * divisor->x2; + const double x2 = divident->x2 * divisor->x1 - divident->x1 * divisor->x2; + + const double multiplier = 1.0 / square_modulus; + + quotient->x1 = x1 * multiplier; + quotient->x2 = x2 * multiplier; + + return 1; +} + +// ============= Get Complex Power ============== // void bgc_vector2_get_complex_power_fp32(const BgcVector2FP32* base, const BgcVector2FP32* power, BgcVector2FP32* result); void bgc_vector2_get_complex_power_fp64(const BgcVector2FP64* base, const BgcVector2FP64* power, BgcVector2FP64* result); -// =================== Angle ==================== // +// ================= Get Angle ================== // float bgc_vector2_get_angle_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2, const BgcAngleUnitEnum unit); double bgc_vector2_get_angle_fp64(const BgcVector2FP64* vector1, const BgcVector2FP64* vector2, const BgcAngleUnitEnum unit); -// =============== Square Distance ============== // +// ============= Get Square Distance ============ // inline float bgc_vector2_get_square_distance_fp32(const BgcVector2FP32* vector1, const BgcVector2FP32* vector2) { diff --git a/basic-geometry/vector3.c b/basic-geometry/vector3.c index 92ac8b6..e15ac8d 100644 --- a/basic-geometry/vector3.c +++ b/basic-geometry/vector3.c @@ -3,42 +3,36 @@ extern inline void bgc_vector3_reset_fp32(BgcVector3FP32* vector); extern inline void bgc_vector3_reset_fp64(BgcVector3FP64* vector); -extern inline void bgc_vector3_set_values_fp32(const float x1, const float x2, const float x3, BgcVector3FP32* to); -extern inline void bgc_vector3_set_values_fp64(const double x1, const double x2, const double x3, BgcVector3FP64* to); - -extern inline float bgc_vector3_get_square_modulus_fp32(const BgcVector3FP32* vector); -extern inline double bgc_vector3_get_square_modulus_fp64(const BgcVector3FP64* vector); - -extern inline float bgc_vector3_get_modulus_fp32(const BgcVector3FP32* vector); -extern inline double bgc_vector3_get_modulus_fp64(const BgcVector3FP64* vector); - -extern inline int bgc_vector3_is_zero_fp32(const BgcVector3FP32* vector); -extern inline int bgc_vector3_is_zero_fp64(const BgcVector3FP64* vector); - -extern inline int bgc_vector3_is_unit_fp32(const BgcVector3FP32* vector); -extern inline int bgc_vector3_is_unit_fp64(const BgcVector3FP64* vector); +extern inline void bgc_vector3_set_values_fp32(const float x1, const float x2, const float x3, BgcVector3FP32* destination); +extern inline void bgc_vector3_set_values_fp64(const double x1, const double x2, const double x3, BgcVector3FP64* destination); -extern inline void bgc_vector3_copy_fp32(const BgcVector3FP32* from, BgcVector3FP32* to); -extern inline void bgc_vector3_copy_fp64(const BgcVector3FP64* from, BgcVector3FP64* to); +extern inline float bgc_vector3_get_square_modulus_fp32(const BgcVector3FP32* vector); +extern inline double bgc_vector3_get_square_modulus_fp64(const BgcVector3FP64* vector); -extern inline void bgc_vector3_convert_fp64_to_fp32(const BgcVector3FP64* from, BgcVector3FP32* to); -extern inline void bgc_vector3_convert_fp32_to_fp64(const BgcVector3FP32* from, BgcVector3FP64* to); +extern inline float bgc_vector3_get_modulus_fp32(const BgcVector3FP32* vector); +extern inline double bgc_vector3_get_modulus_fp64(const BgcVector3FP64* vector); + +extern inline int bgc_vector3_is_zero_fp32(const BgcVector3FP32* vector); +extern inline int bgc_vector3_is_zero_fp64(const BgcVector3FP64* vector); + +extern inline int bgc_vector3_is_unit_fp32(const BgcVector3FP32* vector); +extern inline int bgc_vector3_is_unit_fp64(const BgcVector3FP64* vector); + +extern inline void bgc_vector3_copy_fp32(const BgcVector3FP32* source, BgcVector3FP32* destination); +extern inline void bgc_vector3_copy_fp64(const BgcVector3FP64* source, BgcVector3FP64* destination); + +extern inline void bgc_vector3_convert_fp64_to_fp32(const BgcVector3FP64* source, BgcVector3FP32* destination); +extern inline void bgc_vector3_convert_fp32_to_fp64(const BgcVector3FP32* source, BgcVector3FP64* destination); extern inline void bgc_vector3_swap_fp32(BgcVector3FP32* vector1, BgcVector3FP32* vector2); extern inline void bgc_vector3_swap_fp64(BgcVector3FP64* vector1, BgcVector3FP64* vector2); -extern inline void bgc_vector3_reverse_fp32(BgcVector3FP32* vector); -extern inline void bgc_vector3_reverse_fp64(BgcVector3FP64* vector); - -extern inline int bgc_vector3_normalize_fp32(BgcVector3FP32* vector); -extern inline int bgc_vector3_normalize_fp64(BgcVector3FP64* vector); - -extern inline void bgc_vector3_get_reverse_fp32(const BgcVector3FP32* vector, BgcVector3FP32* reverse); -extern inline void bgc_vector3_get_reverse_fp64(const BgcVector3FP64* vector, BgcVector3FP64* reverse); - -extern inline int bgc_vector3_get_normalized_fp32(const BgcVector3FP32* vector, BgcVector3FP32* normalized); -extern inline int bgc_vector3_get_normalized_fp64(const BgcVector3FP64* vector, BgcVector3FP64* normalized); - +extern inline void bgc_vector3_reverse_fp32(const BgcVector3FP32* vector, BgcVector3FP32* reverse); +extern inline void bgc_vector3_reverse_fp64(const BgcVector3FP64* vector, BgcVector3FP64* reverse); + +extern inline int bgc_vector3_normalize_fp32(const BgcVector3FP32* vector, BgcVector3FP32* normalized); +extern inline int bgc_vector3_normalize_fp64(const BgcVector3FP64* vector, BgcVector3FP64* normalized); + extern inline void bgc_vector3_add_fp32(const BgcVector3FP32* vector1, const BgcVector3FP32* vector2, BgcVector3FP32* sum); extern inline void bgc_vector3_add_fp64(const BgcVector3FP64* vector1, const BgcVector3FP64* vector2, BgcVector3FP64* sum); @@ -48,9 +42,9 @@ extern inline void bgc_vector3_add_scaled_fp64(const BgcVector3FP64* basic_vecto extern inline void bgc_vector3_subtract_fp32(const BgcVector3FP32* minuend, const BgcVector3FP32* subtrahend, BgcVector3FP32* difference); extern inline void bgc_vector3_subtract_fp64(const BgcVector3FP64* minuend, const BgcVector3FP64* subtrahend, BgcVector3FP64* difference); -extern inline void bgc_vector3_subtract_scaled_fp32(const BgcVector3FP32* basic_vector, const BgcVector3FP32* scalable_vector, const float scale, BgcVector3FP32* difference); -extern inline void bgc_vector3_subtract_scaled_fp64(const BgcVector3FP64* basic_vector, const BgcVector3FP64* scalable_vector, const double scale, BgcVector3FP64* difference); - +extern inline void bgc_vector3_subtract_scaled_fp32(const BgcVector3FP32* basic_vector, const BgcVector3FP32* scalable_vector, const float scale, BgcVector3FP32* difference); +extern inline void bgc_vector3_subtract_scaled_fp64(const BgcVector3FP64* basic_vector, const BgcVector3FP64* scalable_vector, const double scale, BgcVector3FP64* difference); + extern inline void bgc_vector3_multiply_fp32(const BgcVector3FP32* multiplicand, const float multiplier, BgcVector3FP32* product); extern inline void bgc_vector3_multiply_fp64(const BgcVector3FP64* multiplicand, const double multiplier, BgcVector3FP64* product); @@ -62,16 +56,16 @@ extern inline void bgc_vector3_get_mean_of_two_fp64(const BgcVector3FP64* vector extern inline void bgc_vector3_get_mean_of_three_fp32(const BgcVector3FP32* vector1, const BgcVector3FP32* vector2, const BgcVector3FP32* vector3, BgcVector3FP32* result); extern inline void bgc_vector3_get_mean_of_three_fp64(const BgcVector3FP64* vector1, const BgcVector3FP64* vector2, const BgcVector3FP64* vector3, BgcVector3FP64* result); - -extern inline void bgc_vector3_get_linear_interpolation_fp32(const BgcVector3FP32* vector1, const BgcVector3FP32* vector2, const float phase, BgcVector3FP32* interpolation); -extern inline void bgc_vector3_get_linear_interpolation_fp64(const BgcVector3FP64* vector1, const BgcVector3FP64* vector2, const double phase, BgcVector3FP64* interpolation); -extern inline void bgc_vector3_minimize_fp32(const BgcVector3FP32* vector, BgcVector3FP32* minimal); -extern inline void bgc_vector3_minimize_fp64(const BgcVector3FP64* vector, BgcVector3FP64* minimal); - -extern inline void bgc_vector3_maximize_fp32(const BgcVector3FP32* vector, BgcVector3FP32* maximal); -extern inline void bgc_vector3_maximize_fp64(const BgcVector3FP64* vector, BgcVector3FP64* maximal); - +extern inline void bgc_vector3_get_linear_interpolation_fp32(const BgcVector3FP32* vector1, const BgcVector3FP32* vector2, const float phase, BgcVector3FP32* interpolation); +extern inline void bgc_vector3_get_linear_interpolation_fp64(const BgcVector3FP64* vector1, const BgcVector3FP64* vector2, const double phase, BgcVector3FP64* interpolation); + +extern inline void bgc_vector3_minimize_fp32(const BgcVector3FP32* vector, BgcVector3FP32* minimal); +extern inline void bgc_vector3_minimize_fp64(const BgcVector3FP64* vector, BgcVector3FP64* minimal); + +extern inline void bgc_vector3_maximize_fp32(const BgcVector3FP32* vector, BgcVector3FP32* maximal); +extern inline void bgc_vector3_maximize_fp64(const BgcVector3FP64* vector, BgcVector3FP64* maximal); + extern inline float bgc_vector3_get_scalar_product_fp32(const BgcVector3FP32* vector1, const BgcVector3FP32* vector2); extern inline double bgc_vector3_get_scalar_product_fp64(const BgcVector3FP64* vector1, const BgcVector3FP64* vector2); diff --git a/basic-geometry/vector3.h b/basic-geometry/vector3.h index bf7e794..33451fe 100644 --- a/basic-geometry/vector3.h +++ b/basic-geometry/vector3.h @@ -36,18 +36,18 @@ inline void bgc_vector3_reset_fp64(BgcVector3FP64* vector) // ==================== Set ===================== // -inline void bgc_vector3_set_values_fp32(const float x1, const float x2, const float x3, BgcVector3FP32* to) +inline void bgc_vector3_set_values_fp32(const float x1, const float x2, const float x3, BgcVector3FP32* destination) { - to->x1 = x1; - to->x2 = x2; - to->x3 = x3; + destination->x1 = x1; + destination->x2 = x2; + destination->x3 = x3; } -inline void bgc_vector3_set_values_fp64(const double x1, const double x2, const double x3, BgcVector3FP64* to) +inline void bgc_vector3_set_values_fp64(const double x1, const double x2, const double x3, BgcVector3FP64* destination) { - to->x1 = x1; - to->x2 = x2; - to->x3 = x3; + destination->x1 = x1; + destination->x2 = x2; + destination->x3 = x3; } // ================== Modulus =================== // @@ -96,18 +96,18 @@ inline int bgc_vector3_is_unit_fp64(const BgcVector3FP64* vector) // ==================== Copy ==================== // -inline void bgc_vector3_copy_fp32(const BgcVector3FP32* from, BgcVector3FP32* to) +inline void bgc_vector3_copy_fp32(const BgcVector3FP32* source, BgcVector3FP32* destination) { - to->x1 = from->x1; - to->x2 = from->x2; - to->x3 = from->x3; + destination->x1 = source->x1; + destination->x2 = source->x2; + destination->x3 = source->x3; } -inline void bgc_vector3_copy_fp64(const BgcVector3FP64* from, BgcVector3FP64* to) +inline void bgc_vector3_copy_fp64(const BgcVector3FP64* source, BgcVector3FP64* destination) { - to->x1 = from->x1; - to->x2 = from->x2; - to->x3 = from->x3; + destination->x1 = source->x1; + destination->x2 = source->x2; + destination->x3 = source->x3; } // ==================== Swap ==================== // @@ -144,116 +144,86 @@ inline void bgc_vector3_swap_fp64(BgcVector3FP64* vector1, BgcVector3FP64* vecto // ================== Convert =================== // -inline void bgc_vector3_convert_fp64_to_fp32(const BgcVector3FP64* from, BgcVector3FP32* to) +inline void bgc_vector3_convert_fp64_to_fp32(const BgcVector3FP64* source, BgcVector3FP32* destination) { - to->x1 = (float) from->x1; - to->x2 = (float) from->x2; - to->x3 = (float) from->x3; + destination->x1 = (float)source->x1; + destination->x2 = (float)source->x2; + destination->x3 = (float)source->x3; } -inline void bgc_vector3_convert_fp32_to_fp64(const BgcVector3FP32* from, BgcVector3FP64* to) +inline void bgc_vector3_convert_fp32_to_fp64(const BgcVector3FP32* source, BgcVector3FP64* destination) { - to->x1 = from->x1; - to->x2 = from->x2; - to->x3 = from->x3; + destination->x1 = source->x1; + destination->x2 = source->x2; + destination->x3 = source->x3; } // ================== Reverse =================== // -inline void bgc_vector3_reverse_fp32(BgcVector3FP32* vector) +inline void bgc_vector3_reverse_fp32(const BgcVector3FP32* vector, BgcVector3FP32* reverse) { - vector->x1 = -vector->x1; - vector->x2 = -vector->x2; - vector->x3 = -vector->x3; + reverse->x1 = -vector->x1; + reverse->x2 = -vector->x2; + reverse->x3 = -vector->x3; } -inline void bgc_vector3_reverse_fp64(BgcVector3FP64* vector) +inline void bgc_vector3_reverse_fp64(const BgcVector3FP64* vector, BgcVector3FP64* reverse) { - vector->x1 = -vector->x1; - vector->x2 = -vector->x2; - vector->x3 = -vector->x3; + reverse->x1 = -vector->x1; + reverse->x2 = -vector->x2; + reverse->x3 = -vector->x3; } // ================= Normalize ================== // -inline int bgc_vector3_normalize_fp32(BgcVector3FP32* vector) +inline int bgc_vector3_normalize_fp32(const BgcVector3FP32* vector, BgcVector3FP32* normalized) { const float square_modulus = bgc_vector3_get_square_modulus_fp32(vector); if (bgc_is_sqare_unit_fp32(square_modulus)) { + normalized->x1 = vector->x1; + normalized->x2 = vector->x2; + normalized->x3 = vector->x3; return 1; } if (square_modulus <= BGC_SQUARE_EPSYLON_FP32 || square_modulus != square_modulus) { - vector->x1 = 0.0f; - vector->x2 = 0.0f; - vector->x3 = 0.0f; return 0; } const float multiplicand = sqrtf(1.0f / square_modulus); - vector->x1 *= multiplicand; - vector->x2 *= multiplicand; - vector->x3 *= multiplicand; + normalized->x1 = vector->x1 * multiplicand; + normalized->x2 = vector->x2 * multiplicand; + normalized->x3 = vector->x3 * multiplicand; return 1; } -inline int bgc_vector3_normalize_fp64(BgcVector3FP64* vector) +inline int bgc_vector3_normalize_fp64(const BgcVector3FP64* vector, BgcVector3FP64* normalized) { const double square_modulus = bgc_vector3_get_square_modulus_fp64(vector); if (bgc_is_sqare_unit_fp64(square_modulus)) { + normalized->x1 = vector->x1; + normalized->x2 = vector->x2; + normalized->x3 = vector->x3; return 1; } if (square_modulus <= BGC_SQUARE_EPSYLON_FP64 || square_modulus != square_modulus) { - vector->x1 = 0.0; - vector->x2 = 0.0; - vector->x3 = 0.0; return 0; } const double multiplicand = sqrt(1.0 / square_modulus); - vector->x1 *= multiplicand; - vector->x2 *= multiplicand; - vector->x3 *= multiplicand; + normalized->x1 = vector->x1 * multiplicand; + normalized->x2 = vector->x2 * multiplicand; + normalized->x3 = vector->x3 * multiplicand; return 1; } -// ================ Get Reverse ================= // - -inline void bgc_vector3_get_reverse_fp32(const BgcVector3FP32* vector, BgcVector3FP32* reverse) -{ - reverse->x1 = -vector->x1; - reverse->x2 = -vector->x2; - reverse->x3 = -vector->x3; -} - -inline void bgc_vector3_get_reverse_fp64(const BgcVector3FP64* vector, BgcVector3FP64* reverse) -{ - reverse->x1 = -vector->x1; - reverse->x2 = -vector->x2; - reverse->x3 = -vector->x3; -} - -// =============== Get Normalized =============== // - -inline int bgc_vector3_get_normalized_fp32(const BgcVector3FP32* vector, BgcVector3FP32* normalized) -{ - bgc_vector3_copy_fp32(vector, normalized); - return bgc_vector3_normalize_fp32(normalized); -} - -inline int bgc_vector3_get_normalized_fp64(const BgcVector3FP64* vector, BgcVector3FP64* normalized) -{ - bgc_vector3_copy_fp64(vector, normalized); - return bgc_vector3_normalize_fp64(normalized); -} - // ==================== Add ===================== // inline void bgc_vector3_add_fp32(const BgcVector3FP32* vector1, const BgcVector3FP32* vector2, BgcVector3FP32* sum) diff --git a/basic-geometry/versor.c b/basic-geometry/versor.c index 2a79b7c..393b9ea 100644 --- a/basic-geometry/versor.c +++ b/basic-geometry/versor.c @@ -16,8 +16,8 @@ extern inline void bgc_versor_set_values_fp64(const double s0, const double x1, extern inline void bgc_versor_set_rotation_fp32(const BgcRotation3FP32* rotation, BgcVersorFP32* result); extern inline void bgc_versor_set_rotation_fp64(const BgcRotation3FP64* rotation, BgcVersorFP64* result); -extern inline void bgc_versor_copy_fp32(const BgcVersorFP32* from, BgcVersorFP32* to); -extern inline void bgc_versor_copy_fp64(const BgcVersorFP64* from, BgcVersorFP64* to); +extern inline void bgc_versor_copy_fp32(const BgcVersorFP32* source, BgcVersorFP32* destination); +extern inline void bgc_versor_copy_fp64(const BgcVersorFP64* source, BgcVersorFP64* destination); extern inline void bgc_versor_swap_fp32(BgcVersorFP32* versor1, BgcVersorFP32* versor2); extern inline void bgc_versor_swap_fp64(BgcVersorFP64* versor1, BgcVersorFP64* versor2); @@ -25,14 +25,14 @@ extern inline void bgc_versor_swap_fp64(BgcVersorFP64* versor1, BgcVersorFP64* v extern inline int bgc_versor_is_identity_fp32(const BgcVersorFP32* versor); extern inline int bgc_versor_is_identity_fp64(const BgcVersorFP64* versor); -extern inline void bgc_versor_convert_fp64_to_fp32(const BgcVersorFP64* versor, BgcVersorFP32* result); -extern inline void bgc_versor_convert_fp32_to_fp64(const BgcVersorFP32* versor, BgcVersorFP64* result); +extern inline void bgc_versor_convert_fp64_to_fp32(const BgcVersorFP64* source, BgcVersorFP32* destination); +extern inline void bgc_versor_convert_fp32_to_fp64(const BgcVersorFP32* source, BgcVersorFP64* destination); -extern inline void bgc_versor_shorten_fp32(BgcVersorFP32* versor); -extern inline void bgc_versor_shorten_fp64(BgcVersorFP64* versor); +extern inline void bgc_versor_shorten_fp32(const BgcVersorFP32* versor, BgcVersorFP32* shortened); +extern inline void bgc_versor_shorten_fp64(const BgcVersorFP64* versor, BgcVersorFP64* shortened); -extern inline void bgc_versor_invert_fp32(BgcVersorFP32* versor); -extern inline void bgc_versor_invert_fp64(BgcVersorFP64* versor); +extern inline void bgc_versor_invert_fp32(const BgcVersorFP32* versor, BgcVersorFP32* inverted); +extern inline void bgc_versor_invert_fp64(const BgcVersorFP64* versor, BgcVersorFP64* inverted); extern inline void bgc_versor_combine_fp32(const BgcVersorFP32* second, const BgcVersorFP32* first, BgcVersorFP32* result); extern inline void bgc_versor_combine_fp64(const BgcVersorFP64* second, const BgcVersorFP64* first, BgcVersorFP64* result); @@ -40,12 +40,6 @@ extern inline void bgc_versor_combine_fp64(const BgcVersorFP64* second, const Bg extern inline void bgc_versor_combine3_fp32(const BgcVersorFP32* third, const BgcVersorFP32* second, const BgcVersorFP32* first, BgcVersorFP32* result); extern inline void bgc_versor_combine3_fp64(const BgcVersorFP64* third, const BgcVersorFP64* second, const BgcVersorFP64* first, BgcVersorFP64* result); -extern inline void bgc_versor_get_shortened_fp32(const BgcVersorFP32* versor, BgcVersorFP32* shortened); -extern inline void bgc_versor_get_shortened_fp64(const BgcVersorFP64* versor, BgcVersorFP64* shortened); - -extern inline void bgc_versor_get_inverted_fp32(const BgcVersorFP32* versor, BgcVersorFP32* to); -extern inline void bgc_versor_get_inverted_fp64(const BgcVersorFP64* versor, BgcVersorFP64* to); - extern inline void bgc_versor_get_rotation_matrix_fp32(const BgcVersorFP32* versor, BgcMatrix3x3FP32* matrix); extern inline void bgc_versor_get_rotation_matrix_fp64(const BgcVersorFP64* versor, BgcMatrix3x3FP64* matrix); diff --git a/basic-geometry/versor.h b/basic-geometry/versor.h index bedd34d..e5ea79e 100644 --- a/basic-geometry/versor.h +++ b/basic-geometry/versor.h @@ -114,24 +114,24 @@ inline void bgc_versor_set_rotation_fp64(const BgcRotation3FP64* rotation, BgcVe // ==================== Copy ==================== // -inline void bgc_versor_copy_fp32(const BgcVersorFP32* from, BgcVersorFP32* to) +inline void bgc_versor_copy_fp32(const BgcVersorFP32* source, BgcVersorFP32* destination) { - _BgcDarkTwinVersorFP32* twin = (_BgcDarkTwinVersorFP32*)to; + _BgcDarkTwinVersorFP32* twin = (_BgcDarkTwinVersorFP32*)destination; - twin->s0 = from->s0; - twin->x1 = from->x1; - twin->x2 = from->x2; - twin->x3 = from->x3; + twin->s0 = source->s0; + twin->x1 = source->x1; + twin->x2 = source->x2; + twin->x3 = source->x3; } -inline void bgc_versor_copy_fp64(const BgcVersorFP64* from, BgcVersorFP64* to) +inline void bgc_versor_copy_fp64(const BgcVersorFP64* source, BgcVersorFP64* destination) { - _BgcDarkTwinVersorFP64* twin = (_BgcDarkTwinVersorFP64*)to; + _BgcDarkTwinVersorFP64* twin = (_BgcDarkTwinVersorFP64*)destination; - twin->s0 = from->s0; - twin->x1 = from->x1; - twin->x2 = from->x2; - twin->x3 = from->x3; + twin->s0 = source->s0; + twin->x1 = source->x1; + twin->x2 = source->x2; + twin->x3 = source->x3; } // ==================== Swap ==================== // @@ -194,51 +194,61 @@ inline int bgc_versor_is_identity_fp64(const BgcVersorFP64* versor) // ================== Convert =================== // -inline void bgc_versor_convert_fp64_to_fp32(const BgcVersorFP64* versor, BgcVersorFP32* result) +inline void bgc_versor_convert_fp64_to_fp32(const BgcVersorFP64* source, BgcVersorFP32* destination) { bgc_versor_set_values_fp32( - (float) versor->s0, - (float) versor->x1, - (float) versor->x2, - (float) versor->x3, - result + (float)source->s0, + (float)source->x1, + (float)source->x2, + (float)source->x3, + destination ); } -inline void bgc_versor_convert_fp32_to_fp64(const BgcVersorFP32* versor, BgcVersorFP64* result) +inline void bgc_versor_convert_fp32_to_fp64(const BgcVersorFP32* source, BgcVersorFP64* destination) { bgc_versor_set_values_fp64( - versor->s0, - versor->x1, - versor->x2, - versor->x3, - result + source->s0, + source->x1, + source->x2, + source->x3, + destination ); } // ================== Shorten =================== // -inline void bgc_versor_shorten_fp32(BgcVersorFP32* versor) +inline void bgc_versor_shorten_fp32(const BgcVersorFP32* versor, BgcVersorFP32* shortened) { + _BgcDarkTwinVersorFP32* twin = (_BgcDarkTwinVersorFP32*)shortened; + if (versor->s0 >= 0.0f) { + twin->x1 = versor->s0; + twin->x1 = versor->x1; + twin->x2 = versor->x2; + twin->x3 = versor->x3; return; } - _BgcDarkTwinVersorFP32* twin = (_BgcDarkTwinVersorFP32*)versor; - twin->s0 = -versor->s0; + twin->x1 = -versor->s0; twin->x1 = -versor->x1; twin->x2 = -versor->x2; twin->x3 = -versor->x3; } -inline void bgc_versor_shorten_fp64(BgcVersorFP64* versor) +inline void bgc_versor_shorten_fp64(const BgcVersorFP64* versor, BgcVersorFP64* shortened) { - if (versor->s0 >= 0.0f) { + _BgcDarkTwinVersorFP64* twin = (_BgcDarkTwinVersorFP64*)shortened; + + if (versor->s0 >= 0.0) { + twin->x1 = versor->s0; + twin->x1 = versor->x1; + twin->x2 = versor->x2; + twin->x3 = versor->x3; return; } - _BgcDarkTwinVersorFP64* twin = (_BgcDarkTwinVersorFP64*)versor; - twin->s0 = -versor->s0; + twin->x1 = -versor->s0; twin->x1 = -versor->x1; twin->x2 = -versor->x2; twin->x3 = -versor->x3; @@ -246,17 +256,19 @@ inline void bgc_versor_shorten_fp64(BgcVersorFP64* versor) // =================== Invert =================== // -inline void bgc_versor_invert_fp32(BgcVersorFP32* versor) +inline void bgc_versor_invert_fp32(const BgcVersorFP32* versor, BgcVersorFP32* inverted) { - _BgcDarkTwinVersorFP32* twin = (_BgcDarkTwinVersorFP32*)versor; + _BgcDarkTwinVersorFP32* twin = (_BgcDarkTwinVersorFP32*)inverted; + twin->s0 = versor->s0; twin->x1 = -versor->x1; twin->x2 = -versor->x2; twin->x3 = -versor->x3; } -inline void bgc_versor_invert_fp64(BgcVersorFP64* versor) +inline void bgc_versor_invert_fp64(const BgcVersorFP64* versor, BgcVersorFP64* inverted) { - _BgcDarkTwinVersorFP64* twin = (_BgcDarkTwinVersorFP64*)versor; + _BgcDarkTwinVersorFP64* twin = (_BgcDarkTwinVersorFP64*)inverted; + twin->s0 = versor->s0; twin->x1 = -versor->x1; twin->x2 = -versor->x2; twin->x3 = -versor->x3; @@ -326,64 +338,6 @@ void bgc_versor_get_rotation_fp32(const BgcVersorFP32* versor, BgcRotation3FP32* void bgc_versor_get_rotation_fp64(const BgcVersorFP64* versor, BgcRotation3FP64* result); -// =============== Get Shortened ================ // - -inline void bgc_versor_get_shortened_fp32(const BgcVersorFP32* versor, BgcVersorFP32* shortened) -{ - _BgcDarkTwinVersorFP32* twin = (_BgcDarkTwinVersorFP32*)shortened; - - if (versor->s0 >= 0.0f) { - twin->x1 = versor->s0; - twin->x1 = versor->x1; - twin->x2 = versor->x2; - twin->x3 = versor->x3; - return; - } - - twin->x1 = -versor->s0; - twin->x1 = -versor->x1; - twin->x2 = -versor->x2; - twin->x3 = -versor->x3; -} - -inline void bgc_versor_get_shortened_fp64(const BgcVersorFP64* versor, BgcVersorFP64* shortened) -{ - _BgcDarkTwinVersorFP64* twin = (_BgcDarkTwinVersorFP64*)shortened; - - if (versor->s0 >= 0.0) { - twin->x1 = versor->s0; - twin->x1 = versor->x1; - twin->x2 = versor->x2; - twin->x3 = versor->x3; - return; - } - - twin->x1 = -versor->s0; - twin->x1 = -versor->x1; - twin->x2 = -versor->x2; - twin->x3 = -versor->x3; -} - -// ================ Get Inverted ================ // - -inline void bgc_versor_get_inverted_fp32(const BgcVersorFP32* versor, BgcVersorFP32* to) -{ - _BgcDarkTwinVersorFP32* twin = (_BgcDarkTwinVersorFP32*)to; - twin->s0 = versor->s0; - twin->x1 = -versor->x1; - twin->x2 = -versor->x2; - twin->x3 = -versor->x3; -} - -inline void bgc_versor_get_inverted_fp64(const BgcVersorFP64* versor, BgcVersorFP64* to) -{ - _BgcDarkTwinVersorFP64* twin = (_BgcDarkTwinVersorFP64*)to; - twin->s0 = versor->s0; - twin->x1 = -versor->x1; - twin->x2 = -versor->x2; - twin->x3 = -versor->x3; -} - // ============ Get Rotation Matrix ============= // inline void bgc_versor_get_rotation_matrix_fp32(const BgcVersorFP32* versor, BgcMatrix3x3FP32* matrix) @@ -505,13 +459,13 @@ inline void bgc_versor_get_reverse_matrix_fp64(const BgcVersorFP64* versor, BgcM inline void bgc_versor_get_both_matrixes_fp32(const BgcVersorFP32* versor, BgcMatrix3x3FP32* rotation, BgcMatrix3x3FP32* reverse) { bgc_versor_get_reverse_matrix_fp32(versor, reverse); - bgc_matrix3x3_get_transposed_fp32(reverse, rotation); + bgc_matrix3x3_transpose_fp32(reverse, rotation); } inline void bgc_versor_get_both_matrixes_fp64(const BgcVersorFP64* versor, BgcMatrix3x3FP64* rotation, BgcMatrix3x3FP64* reverse) { bgc_versor_get_reverse_matrix_fp64(versor, reverse); - bgc_matrix3x3_get_transposed_fp64(reverse, rotation); + bgc_matrix3x3_transpose_fp64(reverse, rotation); } // ================ Turn Vector ================= //