From e354b2425cb17854c1f864a341fea20612b873e1 Mon Sep 17 00:00:00 2001 From: Andrey Pokidov Date: Wed, 20 Nov 2024 01:21:40 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D0=B7=20=D0=BE=D1=82?= =?UTF-8?q?=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9=20getWeightedSum?= =?UTF-8?q?=20=D0=B2=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=20appendScaled?= =?UTF-8?q?;=20=D0=BE=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81=D0=BE=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=20/=20Replacing=20of=20getWeightedSum=20onto=20appendScaled;?= =?UTF-8?q?=20versor=20optimization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/matrix2x2.h | 144 +++++---------------------------- src/matrix2x3.c | 147 ---------------------------------- src/matrix2x3.h | 94 ++++++---------------- src/matrix3x2.c | 139 -------------------------------- src/matrix3x2.h | 92 ++++++--------------- src/matrix3x3.c | 176 +---------------------------------------- src/matrix3x3.h | 100 ++++++++--------------- src/vector2.h | 12 +-- src/vector3.h | 16 ++-- src/versor.h | 66 ++++++++-------- test/geometry_test.h | 36 +++++++++ test/sp_vector2_test.c | 136 ++++++++++++++++++++++++++++++- test/sp_vector2_test.h | 8 ++ 13 files changed, 331 insertions(+), 835 deletions(-) diff --git a/src/matrix2x2.h b/src/matrix2x2.h index 3dc8e7c..e85b6bb 100644 --- a/src/matrix2x2.h +++ b/src/matrix2x2.h @@ -345,6 +345,26 @@ static inline void dp_matrix2x2_set_column2(const double r1, const double r2, DP matrix->r2c2 = r2; } +// ================ Append scaled =============== // + +static inline void sp_matrix2x2_append_scaled(SPMatrix2x2* basic_vector, const SPMatrix2x2* scalable_vector, const float scale) +{ + basic_vector->r1c1 += scalable_vector->r1c1 * scale; + basic_vector->r1c2 += scalable_vector->r1c2 * scale; + + basic_vector->r2c1 += scalable_vector->r2c1 * scale; + basic_vector->r2c2 += scalable_vector->r2c2 * scale; +} + +static inline void dp_matrix2x2_append_scaled(DPMatrix2x2* basic_vector, const DPMatrix2x2* scalable_vector, const double scale) +{ + basic_vector->r1c1 += scalable_vector->r1c1 * scale; + basic_vector->r1c2 += scalable_vector->r1c2 * scale; + + basic_vector->r2c1 += scalable_vector->r2c1 * scale; + basic_vector->r2c2 += scalable_vector->r2c2 * scale; +} + // ================== Addition ================== // static inline void sp_matrix2x2_add(const SPMatrix2x2* matrix1, const SPMatrix2x2* matrix2, SPMatrix2x2* sum) @@ -425,130 +445,6 @@ static inline void dp_matrix2x2_divide(const DPMatrix2x2* dividend, const double quotient->r2c2 = dividend->r2c2 / divisor; } -// ============= Weighed Sum of two ============= // - -static inline void sp_matrix2x2_get_weighted_sum2( - const float weight1, const SPMatrix2x2* matrix1, - const float weight2, const SPMatrix2x2* matrix2, - SPMatrix2x2* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2; -} - -static inline void dp_matrix2x2_get_weighted_sum2( - const double weight1, const DPMatrix2x2* matrix1, - const double weight2, const DPMatrix2x2* matrix2, - DPMatrix2x2* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2; -} - -// ============ Weighed Sum of three ============ // - -static inline void sp_matrix2x2_get_weighted_sum3( - const float weight1, const SPMatrix2x2* matrix1, - const float weight2, const SPMatrix2x2* matrix2, - const float weight3, const SPMatrix2x2* matrix3, - SPMatrix2x2* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2 + matrix3->r1c1 * weight3; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2 + matrix3->r1c2 * weight3; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2 + matrix3->r2c1 * weight3; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2 + matrix3->r2c2 * weight3; -} - -static inline void dp_matrix2x2_get_weighted_sum3( - const double weight1, const DPMatrix2x2* matrix1, - const double weight2, const DPMatrix2x2* matrix2, - const double weight3, const DPMatrix2x2* matrix3, - DPMatrix2x2* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2 + matrix3->r1c1 * weight3; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2 + matrix3->r1c2 * weight3; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2 + matrix3->r2c1 * weight3; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2 + matrix3->r2c2 * weight3; -} - -// ============ Weighed Sum of four ============= // - -static inline void sp_matrix2x2_get_weighted_sum4( - const float weight1, const SPMatrix2x2* matrix1, - const float weight2, const SPMatrix2x2* matrix2, - const float weight3, const SPMatrix2x2* matrix3, - const float weight4, const SPMatrix2x2* matrix4, - SPMatrix2x2* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4); - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4); - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4); - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4); -} - -static inline void dp_matrix2x2_get_weighted_sum4( - const double weight1, const DPMatrix2x2* matrix1, - const double weight2, const DPMatrix2x2* matrix2, - const double weight3, const DPMatrix2x2* matrix3, - const double weight4, const DPMatrix2x2* matrix4, - DPMatrix2x2* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4); - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4); - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4); - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4); -} - -// ============ Weighed Sum of five ============= // - -static inline void sp_matrix2x2_get_weighted_sum5( - const float weight1, const SPMatrix2x2* matrix1, - const float weight2, const SPMatrix2x2* matrix2, - const float weight3, const SPMatrix2x2* matrix3, - const float weight4, const SPMatrix2x2* matrix4, - const float weight5, const SPMatrix2x2* matrix5, - SPMatrix2x2* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4) + matrix5->r1c1 * weight5; - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4) + matrix5->r1c2 * weight5; - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4) + matrix5->r2c1 * weight5; - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4) + matrix5->r2c2 * weight5; -} - -static inline void dp_matrix2x2_get_weighted_sum5( - const double weight1, const DPMatrix2x2* matrix1, - const double weight2, const DPMatrix2x2* matrix2, - const double weight3, const DPMatrix2x2* matrix3, - const double weight4, const DPMatrix2x2* matrix4, - const double weight5, const DPMatrix2x2* matrix5, - DPMatrix2x2* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4) + matrix5->r1c1 * weight5; - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4) + matrix5->r1c2 * weight5; - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4) + matrix5->r2c1 * weight5; - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4) + matrix5->r2c2 * weight5; -} - // ============ Left Vector Product ============= // static inline void sp_matrix2x2_left_product(const SPVector2* vector, const SPMatrix2x2* matrix, SPVector2* result) diff --git a/src/matrix2x3.c b/src/matrix2x3.c index 8f2f3f6..95b9a26 100644 --- a/src/matrix2x3.c +++ b/src/matrix2x3.c @@ -1,149 +1,2 @@ #include "matrix2x3.h" -// ============= Weighed Sum of two ============= // - -void sp_matrix2x3_get_weighted_sum2( - const float weight1, const SPMatrix2x3* matrix1, - const float weight2, const SPMatrix2x3* matrix2, - SPMatrix2x3* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2; - - sum->r3c1 = matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2; - sum->r3c2 = matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2; -} - -void dp_matrix2x3_get_weighted_sum2( - const double weight1, const DPMatrix2x3* matrix1, - const double weight2, const DPMatrix2x3* matrix2, - DPMatrix2x3* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2; - - sum->r3c1 = matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2; - sum->r3c2 = matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2; -} - -// ============ Weighed Sum of three ============ // - -void sp_matrix2x3_get_weighted_sum3( - const float weight1, const SPMatrix2x3* matrix1, - const float weight2, const SPMatrix2x3* matrix2, - const float weight3, const SPMatrix2x3* matrix3, - SPMatrix2x3* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2 + matrix3->r1c1 * weight3; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2 + matrix3->r1c2 * weight3; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2 + matrix3->r2c1 * weight3; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2 + matrix3->r2c2 * weight3; - - sum->r3c1 = matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2 + matrix3->r3c1 * weight3; - sum->r3c2 = matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2 + matrix3->r3c2 * weight3; -} - -void dp_matrix2x3_get_weighted_sum3( - const double weight1, const DPMatrix2x3* matrix1, - const double weight2, const DPMatrix2x3* matrix2, - const double weight3, const DPMatrix2x3* matrix3, - DPMatrix2x3* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2 + matrix3->r1c1 * weight3; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2 + matrix3->r1c2 * weight3; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2 + matrix3->r2c1 * weight3; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2 + matrix3->r2c2 * weight3; - - sum->r3c1 = matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2 + matrix3->r3c1 * weight3; - sum->r3c2 = matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2 + matrix3->r3c2 * weight3; -} - -// ============ Weighed Sum of four ============= // - -void sp_matrix2x3_get_weighted_sum4( - const float weight1, const SPMatrix2x3* matrix1, - const float weight2, const SPMatrix2x3* matrix2, - const float weight3, const SPMatrix2x3* matrix3, - const float weight4, const SPMatrix2x3* matrix4, - SPMatrix2x3* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4); - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4); - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4); - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4); - - sum->r3c1 = (matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2) + (matrix3->r3c1 * weight3 + matrix4->r3c1 * weight4); - sum->r3c2 = (matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2) + (matrix3->r3c2 * weight3 + matrix4->r3c2 * weight4); -} - -void dp_matrix2x3_get_weighted_sum4( - const double weight1, const DPMatrix2x3* matrix1, - const double weight2, const DPMatrix2x3* matrix2, - const double weight3, const DPMatrix2x3* matrix3, - const double weight4, const DPMatrix2x3* matrix4, - DPMatrix2x3* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4); - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4); - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4); - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4); - - sum->r3c1 = (matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2) + (matrix3->r3c1 * weight3 + matrix4->r3c1 * weight4); - sum->r3c2 = (matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2) + (matrix3->r3c2 * weight3 + matrix4->r3c2 * weight4); -} - -// ============ Weighed Sum of five ============= // - -void sp_matrix2x3_get_weighted_sum5( - const float weight1, const SPMatrix2x3* matrix1, - const float weight2, const SPMatrix2x3* matrix2, - const float weight3, const SPMatrix2x3* matrix3, - const float weight4, const SPMatrix2x3* matrix4, - const float weight5, const SPMatrix2x3* matrix5, - SPMatrix2x3* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4) + matrix5->r1c1 * weight5; - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4) + matrix5->r1c2 * weight5; - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4) + matrix5->r2c1 * weight5; - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4) + matrix5->r2c2 * weight5; - - sum->r3c1 = (matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2) + (matrix3->r3c1 * weight3 + matrix4->r3c1 * weight4) + matrix5->r3c1 * weight5; - sum->r3c2 = (matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2) + (matrix3->r3c2 * weight3 + matrix4->r3c2 * weight4) + matrix5->r3c2 * weight5; -} - -void dp_matrix2x3_get_weighted_sum5( - const double weight1, const DPMatrix2x3* matrix1, - const double weight2, const DPMatrix2x3* matrix2, - const double weight3, const DPMatrix2x3* matrix3, - const double weight4, const DPMatrix2x3* matrix4, - const double weight5, const DPMatrix2x3* matrix5, - DPMatrix2x3* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4) + matrix5->r1c1 * weight5; - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4) + matrix5->r1c2 * weight5; - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4) + matrix5->r2c1 * weight5; - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4) + matrix5->r2c2 * weight5; - - sum->r3c1 = (matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2) + (matrix3->r3c1 * weight3 + matrix4->r3c1 * weight4) + matrix5->r3c1 * weight5; - sum->r3c2 = (matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2) + (matrix3->r3c2 * weight3 + matrix4->r3c2 * weight4) + matrix5->r3c2 * weight5; -} diff --git a/src/matrix2x3.h b/src/matrix2x3.h index 36a569d..c980068 100644 --- a/src/matrix2x3.h +++ b/src/matrix2x3.h @@ -209,6 +209,32 @@ static inline void dp_matrix2x3_set_column2(const double r1, const double r2, co matrix->r3c2 = r3; } +// ================ Append scaled =============== // + +static inline void sp_matrix2x3_append_scaled(SPMatrix2x3* basic_vector, const SPMatrix2x3* scalable_vector, const float scale) +{ + basic_vector->r1c1 += scalable_vector->r1c1 * scale; + basic_vector->r1c2 += scalable_vector->r1c2 * scale; + + basic_vector->r2c1 += scalable_vector->r2c1 * scale; + basic_vector->r2c2 += scalable_vector->r2c2 * scale; + + basic_vector->r3c1 += scalable_vector->r3c1 * scale; + basic_vector->r3c2 += scalable_vector->r3c2 * scale; +} + +static inline void dp_matrix2x3_append_scaled(DPMatrix2x3* basic_vector, const DPMatrix2x3* scalable_vector, const double scale) +{ + basic_vector->r1c1 += scalable_vector->r1c1 * scale; + basic_vector->r1c2 += scalable_vector->r1c2 * scale; + + basic_vector->r2c1 += scalable_vector->r2c1 * scale; + basic_vector->r2c2 += scalable_vector->r2c2 * scale; + + basic_vector->r3c1 += scalable_vector->r3c1 * scale; + basic_vector->r3c2 += scalable_vector->r3c2 * scale; +} + // ================== Addition ================== // static inline void sp_matrix2x3_add(const SPMatrix2x3* matrix1, const SPMatrix2x3* matrix2, SPMatrix2x3* sum) @@ -313,74 +339,6 @@ static inline void dp_matrix2x3_divide(const DPMatrix2x3* dividend, const double quotient->r3c2 = dividend->r3c2 / divisor; } -// ============= Weighed Sum of two ============= // - -void sp_matrix2x3_get_weighted_sum2( - const float weight1, const SPMatrix2x3* matrix1, - const float weight2, const SPMatrix2x3* matrix2, - SPMatrix2x3* sum -); - -void dp_matrix2x3_get_weighted_sum2( - const double weight1, const DPMatrix2x3* matrix1, - const double weight2, const DPMatrix2x3* matrix2, - DPMatrix2x3* sum -); - -// ============ Weighed Sum of three ============ // - -void sp_matrix2x3_get_weighted_sum3( - const float weight1, const SPMatrix2x3* matrix1, - const float weight2, const SPMatrix2x3* matrix2, - const float weight3, const SPMatrix2x3* matrix3, - SPMatrix2x3* sum -); - -void dp_matrix2x3_get_weighted_sum3( - const double weight1, const DPMatrix2x3* matrix1, - const double weight2, const DPMatrix2x3* matrix2, - const double weight3, const DPMatrix2x3* matrix3, - DPMatrix2x3* sum -); - -// ============ Weighed Sum of four ============= // - -void sp_matrix2x3_get_weighted_sum4( - const float weight1, const SPMatrix2x3* matrix1, - const float weight2, const SPMatrix2x3* matrix2, - const float weight3, const SPMatrix2x3* matrix3, - const float weight4, const SPMatrix2x3* matrix4, - SPMatrix2x3* sum -); - -void dp_matrix2x3_get_weighted_sum4( - const double weight1, const DPMatrix2x3* matrix1, - const double weight2, const DPMatrix2x3* matrix2, - const double weight3, const DPMatrix2x3* matrix3, - const double weight4, const DPMatrix2x3* matrix4, - DPMatrix2x3* sum -); - -// ============ Weighed Sum of five ============= // - -void sp_matrix2x3_get_weighted_sum5( - const float weight1, const SPMatrix2x3* matrix1, - const float weight2, const SPMatrix2x3* matrix2, - const float weight3, const SPMatrix2x3* matrix3, - const float weight4, const SPMatrix2x3* matrix4, - const float weight5, const SPMatrix2x3* matrix5, - SPMatrix2x3* sum -); - -void dp_matrix2x3_get_weighted_sum5( - const double weight1, const DPMatrix2x3* matrix1, - const double weight2, const DPMatrix2x3* matrix2, - const double weight3, const DPMatrix2x3* matrix3, - const double weight4, const DPMatrix2x3* matrix4, - const double weight5, const DPMatrix2x3* matrix5, - DPMatrix2x3* sum -); - // ============ Left Vector Product ============= // static inline void sp_matrix2x3_left_product(const SPVector3* vector, const SPMatrix2x3* matrix, SPVector2* result) diff --git a/src/matrix3x2.c b/src/matrix3x2.c index 974e086..71834ad 100644 --- a/src/matrix3x2.c +++ b/src/matrix3x2.c @@ -1,141 +1,2 @@ #include "matrix3x2.h" -// ============= Weighed Sum of two ============= // - -void sp_matrix3x2_get_weighted_sum2( - const float weight1, const SPMatrix3x2* matrix1, - const float weight2, const SPMatrix3x2* matrix2, - SPMatrix3x2* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2; - sum->r1c3 = matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2; - sum->r2c3 = matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2; -} - -void dp_matrix3x2_get_weighted_sum2( - const double weight1, const DPMatrix3x2* matrix1, - const double weight2, const DPMatrix3x2* matrix2, - DPMatrix3x2* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2; - sum->r1c3 = matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2; - sum->r2c3 = matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2; -} - -// ============ Weighed Sum of three ============ // - -void sp_matrix3x2_get_weighted_sum3( - const float weight1, const SPMatrix3x2* matrix1, - const float weight2, const SPMatrix3x2* matrix2, - const float weight3, const SPMatrix3x2* matrix3, - SPMatrix3x2* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2 + matrix3->r1c1 * weight3; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2 + matrix3->r1c2 * weight3; - sum->r1c3 = matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2 + matrix3->r1c3 * weight3; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2 + matrix3->r2c1 * weight3; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2 + matrix3->r2c2 * weight3; - sum->r2c3 = matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2 + matrix3->r2c3 * weight3; -} - -void dp_matrix3x2_get_weighted_sum3( - const double weight1, const DPMatrix3x2* matrix1, - const double weight2, const DPMatrix3x2* matrix2, - const double weight3, const DPMatrix3x2* matrix3, - DPMatrix3x2* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2 + matrix3->r1c1 * weight3; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2 + matrix3->r1c2 * weight3; - sum->r1c3 = matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2 + matrix3->r1c3 * weight3; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2 + matrix3->r2c1 * weight3; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2 + matrix3->r2c2 * weight3; - sum->r2c3 = matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2 + matrix3->r2c3 * weight3; -} - -// ============ Weighed Sum of four ============= // - -void sp_matrix3x2_get_weighted_sum4( - const float weight1, const SPMatrix3x2* matrix1, - const float weight2, const SPMatrix3x2* matrix2, - const float weight3, const SPMatrix3x2* matrix3, - const float weight4, const SPMatrix3x2* matrix4, - SPMatrix3x2* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4); - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4); - sum->r1c3 = (matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2) + (matrix3->r1c3 * weight3 + matrix4->r1c3 * weight4); - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4); - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4); - sum->r2c3 = (matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2) + (matrix3->r2c3 * weight3 + matrix4->r2c3 * weight4); -} - -void dp_matrix3x2_get_weighted_sum4( - const double weight1, const DPMatrix3x2* matrix1, - const double weight2, const DPMatrix3x2* matrix2, - const double weight3, const DPMatrix3x2* matrix3, - const double weight4, const DPMatrix3x2* matrix4, - DPMatrix3x2* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4); - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4); - sum->r1c3 = (matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2) + (matrix3->r1c3 * weight3 + matrix4->r1c3 * weight4); - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4); - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4); - sum->r2c3 = (matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2) + (matrix3->r2c3 * weight3 + matrix4->r2c3 * weight4); -} - -// ============ Weighed Sum of five ============= // - -void sp_matrix3x2_get_weighted_sum5( - const float weight1, const SPMatrix3x2* matrix1, - const float weight2, const SPMatrix3x2* matrix2, - const float weight3, const SPMatrix3x2* matrix3, - const float weight4, const SPMatrix3x2* matrix4, - const float weight5, const SPMatrix3x2* matrix5, - SPMatrix3x2* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4) + matrix5->r1c1 * weight5; - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4) + matrix5->r1c2 * weight5; - sum->r1c3 = (matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2) + (matrix3->r1c3 * weight3 + matrix4->r1c3 * weight4) + matrix5->r1c3 * weight5; - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4) + matrix5->r2c1 * weight5; - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4) + matrix5->r2c2 * weight5; - sum->r2c3 = (matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2) + (matrix3->r2c3 * weight3 + matrix4->r2c3 * weight4) + matrix5->r2c3 * weight5; -} - -void dp_matrix3x2_get_weighted_sum5( - const double weight1, const DPMatrix3x2* matrix1, - const double weight2, const DPMatrix3x2* matrix2, - const double weight3, const DPMatrix3x2* matrix3, - const double weight4, const DPMatrix3x2* matrix4, - const double weight5, const DPMatrix3x2* matrix5, - DPMatrix3x2* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4) + matrix5->r1c1 * weight5; - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4) + matrix5->r1c2 * weight5; - sum->r1c3 = (matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2) + (matrix3->r1c3 * weight3 + matrix4->r1c3 * weight4) + matrix5->r1c3 * weight5; - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4) + matrix5->r2c1 * weight5; - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4) + matrix5->r2c2 * weight5; - sum->r2c3 = (matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2) + (matrix3->r2c3 * weight3 + matrix4->r2c3 * weight4) + matrix5->r2c3 * weight5; -} diff --git a/src/matrix3x2.h b/src/matrix3x2.h index 7fd5845..d77c09d 100644 --- a/src/matrix3x2.h +++ b/src/matrix3x2.h @@ -175,6 +175,30 @@ static inline void dp_matrix3x2_set_column3(const double r1, const double r2, DP matrix->r2c3 = r2; } +// ================ Append scaled =============== // + +static inline void sp_matrix3x2_append_scaled(SPMatrix3x2* basic_vector, const SPMatrix3x2* scalable_vector, const float scale) +{ + basic_vector->r1c1 += scalable_vector->r1c1 * scale; + basic_vector->r1c2 += scalable_vector->r1c2 * scale; + basic_vector->r1c3 += scalable_vector->r1c3 * scale; + + basic_vector->r2c1 += scalable_vector->r2c1 * scale; + basic_vector->r2c2 += scalable_vector->r2c2 * scale; + basic_vector->r2c3 += scalable_vector->r2c3 * scale; +} + +static inline void dp_matrix3x2_append_scaled(DPMatrix3x2* basic_vector, const DPMatrix3x2* scalable_vector, const double scale) +{ + basic_vector->r1c1 += scalable_vector->r1c1 * scale; + basic_vector->r1c2 += scalable_vector->r1c2 * scale; + basic_vector->r1c3 += scalable_vector->r1c3 * scale; + + basic_vector->r2c1 += scalable_vector->r2c1 * scale; + basic_vector->r2c2 += scalable_vector->r2c2 * scale; + basic_vector->r2c3 += scalable_vector->r2c3 * scale; +} + // ================== Addition ================== // static inline void sp_matrix3x2_add(const SPMatrix3x2* matrix1, const SPMatrix3x2* matrix2, SPMatrix3x2* sum) @@ -271,74 +295,6 @@ static inline void dp_matrix3x2_divide(const DPMatrix3x2* dividend, const double quotient->r2c3 = dividend->r2c3 / divisor; } -// ============= Weighed Sum of two ============= // - -void sp_matrix3x2_get_weighted_sum2( - const float weight1, const SPMatrix3x2* matrix1, - const float weight2, const SPMatrix3x2* matrix2, - SPMatrix3x2* sum -); - -void dp_matrix3x2_get_weighted_sum2( - const double weight1, const DPMatrix3x2* matrix1, - const double weight2, const DPMatrix3x2* matrix2, - DPMatrix3x2* sum -); - -// ============ Weighed Sum of three ============ // - -void sp_matrix3x2_get_weighted_sum3( - const float weight1, const SPMatrix3x2* matrix1, - const float weight2, const SPMatrix3x2* matrix2, - const float weight3, const SPMatrix3x2* matrix3, - SPMatrix3x2* sum -); - -void dp_matrix3x2_get_weighted_sum3( - const double weight1, const DPMatrix3x2* matrix1, - const double weight2, const DPMatrix3x2* matrix2, - const double weight3, const DPMatrix3x2* matrix3, - DPMatrix3x2* sum -); - -// ============ Weighed Sum of four ============= // - -void sp_matrix3x2_get_weighted_sum4( - const float weight1, const SPMatrix3x2* matrix1, - const float weight2, const SPMatrix3x2* matrix2, - const float weight3, const SPMatrix3x2* matrix3, - const float weight4, const SPMatrix3x2* matrix4, - SPMatrix3x2* sum -); - -void dp_matrix3x2_get_weighted_sum4( - const double weight1, const DPMatrix3x2* matrix1, - const double weight2, const DPMatrix3x2* matrix2, - const double weight3, const DPMatrix3x2* matrix3, - const double weight4, const DPMatrix3x2* matrix4, - DPMatrix3x2* sum -); - -// ============ Weighed Sum of five ============= // - -void sp_matrix3x2_get_weighted_sum5( - const float weight1, const SPMatrix3x2* matrix1, - const float weight2, const SPMatrix3x2* matrix2, - const float weight3, const SPMatrix3x2* matrix3, - const float weight4, const SPMatrix3x2* matrix4, - const float weight5, const SPMatrix3x2* matrix5, - SPMatrix3x2* sum -); - -void dp_matrix3x2_get_weighted_sum5( - const double weight1, const DPMatrix3x2* matrix1, - const double weight2, const DPMatrix3x2* matrix2, - const double weight3, const DPMatrix3x2* matrix3, - const double weight4, const DPMatrix3x2* matrix4, - const double weight5, const DPMatrix3x2* matrix5, - DPMatrix3x2* sum -); - // ============ Left Vector Product ============= // static inline void sp_matrix3x2_left_product(const SPVector2* vector, const SPMatrix3x2* matrix, SPVector3* result) diff --git a/src/matrix3x3.c b/src/matrix3x3.c index 8111347..e7c5933 100644 --- a/src/matrix3x3.c +++ b/src/matrix3x3.c @@ -74,7 +74,7 @@ int dp_matrix3x3_invert(DPMatrix3x3* matrix) // ================ Make Inverted =============== // -int sp_matrix3x3_make_inverted(const SPMatrix3x3* matrix, SPMatrix3x3* result) +int sp_matrix3x3_set_inverted(const SPMatrix3x3* matrix, SPMatrix3x3* result) { const float determinant = sp_matrix3x3_get_determinant(matrix); @@ -109,7 +109,7 @@ int sp_matrix3x3_make_inverted(const SPMatrix3x3* matrix, SPMatrix3x3* result) return 1; } -int dp_matrix3x3_make_inverted(const DPMatrix3x3* matrix, DPMatrix3x3* result) +int dp_matrix3x3_set_inverted(const DPMatrix3x3* matrix, DPMatrix3x3* result) { const double determinant = dp_matrix3x3_get_determinant(matrix); @@ -143,175 +143,3 @@ int dp_matrix3x3_make_inverted(const DPMatrix3x3* matrix, DPMatrix3x3* result) return 1; } - -// ============= Weighed Sum of two ============= // - -void sp_matrix3x3_get_weighted_sum2( - const float weight1, const SPMatrix3x3* matrix1, - const float weight2, const SPMatrix3x3* matrix2, - SPMatrix3x3* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2; - sum->r1c3 = matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2; - sum->r2c3 = matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2; - - sum->r3c1 = matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2; - sum->r3c2 = matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2; - sum->r3c3 = matrix1->r3c3 * weight1 + matrix2->r3c3 * weight2; -} - -void dp_matrix3x3_get_weighted_sum2( - const double weight1, const DPMatrix3x3* matrix1, - const double weight2, const DPMatrix3x3* matrix2, - DPMatrix3x3* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2; - sum->r1c3 = matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2; - sum->r2c3 = matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2; - - sum->r3c1 = matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2; - sum->r3c2 = matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2; - sum->r3c3 = matrix1->r3c3 * weight1 + matrix2->r3c3 * weight2; -} - -// ============ Weighed Sum of three ============ // - -void sp_matrix3x3_get_weighted_sum3( - const float weight1, const SPMatrix3x3* matrix1, - const float weight2, const SPMatrix3x3* matrix2, - const float weight3, const SPMatrix3x3* matrix3, - SPMatrix3x3* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2 + matrix3->r1c1 * weight3; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2 + matrix3->r1c2 * weight3; - sum->r1c3 = matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2 + matrix3->r1c3 * weight3; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2 + matrix3->r2c1 * weight3; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2 + matrix3->r2c2 * weight3; - sum->r2c3 = matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2 + matrix3->r2c3 * weight3; - - sum->r3c1 = matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2 + matrix3->r3c1 * weight3; - sum->r3c2 = matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2 + matrix3->r3c2 * weight3; - sum->r3c3 = matrix1->r3c3 * weight1 + matrix2->r3c3 * weight2 + matrix3->r3c3 * weight3; -} - -void dp_matrix3x3_get_weighted_sum3( - const double weight1, const DPMatrix3x3* matrix1, - const double weight2, const DPMatrix3x3* matrix2, - const double weight3, const DPMatrix3x3* matrix3, - DPMatrix3x3* sum -) -{ - sum->r1c1 = matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2 + matrix3->r1c1 * weight3; - sum->r1c2 = matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2 + matrix3->r1c2 * weight3; - sum->r1c3 = matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2 + matrix3->r1c3 * weight3; - - sum->r2c1 = matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2 + matrix3->r2c1 * weight3; - sum->r2c2 = matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2 + matrix3->r2c2 * weight3; - sum->r2c3 = matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2 + matrix3->r2c3 * weight3; - - sum->r3c1 = matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2 + matrix3->r3c1 * weight3; - sum->r3c2 = matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2 + matrix3->r3c2 * weight3; - sum->r3c3 = matrix1->r3c3 * weight1 + matrix2->r3c3 * weight2 + matrix3->r3c3 * weight3; -} - -// ============ Weighed Sum of four ============= // - -void sp_matrix3x3_get_weighted_sum4( - const float weight1, const SPMatrix3x3* matrix1, - const float weight2, const SPMatrix3x3* matrix2, - const float weight3, const SPMatrix3x3* matrix3, - const float weight4, const SPMatrix3x3* matrix4, - SPMatrix3x3* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4); - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4); - sum->r1c3 = (matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2) + (matrix3->r1c3 * weight3 + matrix4->r1c3 * weight4); - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4); - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4); - sum->r2c3 = (matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2) + (matrix3->r2c3 * weight3 + matrix4->r2c3 * weight4); - - sum->r3c1 = (matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2) + (matrix3->r3c1 * weight3 + matrix4->r3c1 * weight4); - sum->r3c2 = (matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2) + (matrix3->r3c2 * weight3 + matrix4->r3c2 * weight4); - sum->r3c3 = (matrix1->r3c3 * weight1 + matrix2->r3c3 * weight2) + (matrix3->r3c3 * weight3 + matrix4->r3c3 * weight4); -} - -void dp_matrix3x3_get_weighted_sum4( - const double weight1, const DPMatrix3x3* matrix1, - const double weight2, const DPMatrix3x3* matrix2, - const double weight3, const DPMatrix3x3* matrix3, - const double weight4, const DPMatrix3x3* matrix4, - DPMatrix3x3* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4); - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4); - sum->r1c3 = (matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2) + (matrix3->r1c3 * weight3 + matrix4->r1c3 * weight4); - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4); - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4); - sum->r2c3 = (matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2) + (matrix3->r2c3 * weight3 + matrix4->r2c3 * weight4); - - sum->r3c1 = (matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2) + (matrix3->r3c1 * weight3 + matrix4->r3c1 * weight4); - sum->r3c2 = (matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2) + (matrix3->r3c2 * weight3 + matrix4->r3c2 * weight4); - sum->r3c3 = (matrix1->r3c3 * weight1 + matrix2->r3c3 * weight2) + (matrix3->r3c3 * weight3 + matrix4->r3c3 * weight4); -} - -// ============ Weighed Sum of five ============= // - -void sp_matrix3x3_get_weighted_sum5( - const float weight1, const SPMatrix3x3* matrix1, - const float weight2, const SPMatrix3x3* matrix2, - const float weight3, const SPMatrix3x3* matrix3, - const float weight4, const SPMatrix3x3* matrix4, - const float weight5, const SPMatrix3x3* matrix5, - SPMatrix3x3* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4) + matrix5->r1c1 * weight5; - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4) + matrix5->r1c2 * weight5; - sum->r1c3 = (matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2) + (matrix3->r1c3 * weight3 + matrix4->r1c3 * weight4) + matrix5->r1c3 * weight5; - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4) + matrix5->r2c1 * weight5; - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4) + matrix5->r2c2 * weight5; - sum->r2c3 = (matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2) + (matrix3->r2c3 * weight3 + matrix4->r2c3 * weight4) + matrix5->r2c3 * weight5; - - sum->r3c1 = (matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2) + (matrix3->r3c1 * weight3 + matrix4->r3c1 * weight4) + matrix5->r3c1 * weight5; - sum->r3c2 = (matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2) + (matrix3->r3c2 * weight3 + matrix4->r3c2 * weight4) + matrix5->r3c2 * weight5; - sum->r3c3 = (matrix1->r3c3 * weight1 + matrix2->r3c3 * weight2) + (matrix3->r3c3 * weight3 + matrix4->r3c3 * weight4) + matrix5->r3c3 * weight5; -} - -void dp_matrix3x3_get_weighted_sum5( - const double weight1, const DPMatrix3x3* matrix1, - const double weight2, const DPMatrix3x3* matrix2, - const double weight3, const DPMatrix3x3* matrix3, - const double weight4, const DPMatrix3x3* matrix4, - const double weight5, const DPMatrix3x3* matrix5, - DPMatrix3x3* sum -) -{ - sum->r1c1 = (matrix1->r1c1 * weight1 + matrix2->r1c1 * weight2) + (matrix3->r1c1 * weight3 + matrix4->r1c1 * weight4) + matrix5->r1c1 * weight5; - sum->r1c2 = (matrix1->r1c2 * weight1 + matrix2->r1c2 * weight2) + (matrix3->r1c2 * weight3 + matrix4->r1c2 * weight4) + matrix5->r1c2 * weight5; - sum->r1c3 = (matrix1->r1c3 * weight1 + matrix2->r1c3 * weight2) + (matrix3->r1c3 * weight3 + matrix4->r1c3 * weight4) + matrix5->r1c3 * weight5; - - sum->r2c1 = (matrix1->r2c1 * weight1 + matrix2->r2c1 * weight2) + (matrix3->r2c1 * weight3 + matrix4->r2c1 * weight4) + matrix5->r2c1 * weight5; - sum->r2c2 = (matrix1->r2c2 * weight1 + matrix2->r2c2 * weight2) + (matrix3->r2c2 * weight3 + matrix4->r2c2 * weight4) + matrix5->r2c2 * weight5; - sum->r2c3 = (matrix1->r2c3 * weight1 + matrix2->r2c3 * weight2) + (matrix3->r2c3 * weight3 + matrix4->r2c3 * weight4) + matrix5->r2c3 * weight5; - - sum->r3c1 = (matrix1->r3c1 * weight1 + matrix2->r3c1 * weight2) + (matrix3->r3c1 * weight3 + matrix4->r3c1 * weight4) + matrix5->r3c1 * weight5; - sum->r3c2 = (matrix1->r3c2 * weight1 + matrix2->r3c2 * weight2) + (matrix3->r3c2 * weight3 + matrix4->r3c2 * weight4) + matrix5->r3c2 * weight5; - sum->r3c3 = (matrix1->r3c3 * weight1 + matrix2->r3c3 * weight2) + (matrix3->r3c3 * weight3 + matrix4->r3c3 * weight4) + matrix5->r3c3 * weight5; -} diff --git a/src/matrix3x3.h b/src/matrix3x3.h index ae195e8..58fb773 100644 --- a/src/matrix3x3.h +++ b/src/matrix3x3.h @@ -378,6 +378,38 @@ static inline void dp_matrix3x3_set_column3(const double r1, const double r2, co matrix->r3c3 = r3; } +// ================ Append scaled =============== // + +static inline void sp_matrix3x3_append_scaled(SPMatrix3x3* basic_vector, const SPMatrix3x3* scalable_vector, const float scale) +{ + basic_vector->r1c1 += scalable_vector->r1c1 * scale; + basic_vector->r1c2 += scalable_vector->r1c2 * scale; + basic_vector->r1c3 += scalable_vector->r1c3 * scale; + + basic_vector->r2c1 += scalable_vector->r2c1 * scale; + basic_vector->r2c2 += scalable_vector->r2c2 * scale; + basic_vector->r2c3 += scalable_vector->r2c3 * scale; + + basic_vector->r3c1 += scalable_vector->r3c1 * scale; + basic_vector->r3c2 += scalable_vector->r3c2 * scale; + basic_vector->r3c3 += scalable_vector->r3c3 * scale; +} + +static inline void dp_matrix3x3_append_scaled(DPMatrix3x3* basic_vector, const DPMatrix3x3* scalable_vector, const double scale) +{ + basic_vector->r1c1 += scalable_vector->r1c1 * scale; + basic_vector->r1c2 += scalable_vector->r1c2 * scale; + basic_vector->r1c3 += scalable_vector->r1c3 * scale; + + basic_vector->r2c1 += scalable_vector->r2c1 * scale; + basic_vector->r2c2 += scalable_vector->r2c2 * scale; + basic_vector->r2c3 += scalable_vector->r2c3 * scale; + + basic_vector->r3c1 += scalable_vector->r3c1 * scale; + basic_vector->r3c2 += scalable_vector->r3c2 * scale; + basic_vector->r3c3 += scalable_vector->r3c3 * scale; +} + // ================== Addition ================== // static inline void sp_matrix3x3_add(const SPMatrix3x3* matrix1, const SPMatrix3x3* matrix2, SPMatrix3x3* sum) @@ -506,74 +538,6 @@ static inline void dp_matrix3x3_divide(const DPMatrix3x3* dividend, const double quotient->r3c3 = dividend->r3c3 / divisor; } -// ============= Weighed Sum of two ============= // - -void sp_matrix3x3_get_weighted_sum2( - const float weight1, const SPMatrix3x3* matrix1, - const float weight2, const SPMatrix3x3* matrix2, - SPMatrix3x3* sum -); - -void dp_matrix3x3_get_weighted_sum2( - const double weight1, const DPMatrix3x3* matrix1, - const double weight2, const DPMatrix3x3* matrix2, - DPMatrix3x3* sum -); - -// ============ Weighed Sum of three ============ // - -void sp_matrix3x3_get_weighted_sum3( - const float weight1, const SPMatrix3x3* matrix1, - const float weight2, const SPMatrix3x3* matrix2, - const float weight3, const SPMatrix3x3* matrix3, - SPMatrix3x3* sum -); - -void dp_matrix3x3_get_weighted_sum3( - const double weight1, const DPMatrix3x3* matrix1, - const double weight2, const DPMatrix3x3* matrix2, - const double weight3, const DPMatrix3x3* matrix3, - DPMatrix3x3* sum -); - -// ============ Weighed Sum of four ============= // - -void sp_matrix3x3_get_weighted_sum4( - const float weight1, const SPMatrix3x3* matrix1, - const float weight2, const SPMatrix3x3* matrix2, - const float weight3, const SPMatrix3x3* matrix3, - const float weight4, const SPMatrix3x3* matrix4, - SPMatrix3x3* sum -); - -void dp_matrix3x3_get_weighted_sum4( - const double weight1, const DPMatrix3x3* matrix1, - const double weight2, const DPMatrix3x3* matrix2, - const double weight3, const DPMatrix3x3* matrix3, - const double weight4, const DPMatrix3x3* matrix4, - DPMatrix3x3* sum -); - -// ============ Weighed Sum of five ============= // - -void sp_matrix3x3_get_weighted_sum5( - const float weight1, const SPMatrix3x3* matrix1, - const float weight2, const SPMatrix3x3* matrix2, - const float weight3, const SPMatrix3x3* matrix3, - const float weight4, const SPMatrix3x3* matrix4, - const float weight5, const SPMatrix3x3* matrix5, - SPMatrix3x3* sum -); - -void dp_matrix3x3_get_weighted_sum5( - const double weight1, const DPMatrix3x3* matrix1, - const double weight2, const DPMatrix3x3* matrix2, - const double weight3, const DPMatrix3x3* matrix3, - const double weight4, const DPMatrix3x3* matrix4, - const double weight5, const DPMatrix3x3* matrix5, - DPMatrix3x3* sum -); - // ============ Left Vector Product ============= // static inline void sp_matrix3x3_left_product(const SPVector3* vector, const SPMatrix3x3* matrix, SPVector3* result) diff --git a/src/vector2.h b/src/vector2.h index ae380e4..f6ae864 100644 --- a/src/vector2.h +++ b/src/vector2.h @@ -206,16 +206,16 @@ static inline void dp_vector2_divide(const DPVector2* dividend, const double div // ================ Append scaled =============== // -static inline void sp_vector2_append_scaled(SPVector2* basic_vector, const SPVector2* scalable_summand, const float scale) +static inline void sp_vector2_append_scaled(SPVector2* basic_vector, const SPVector2* scalable_vector, const float scale) { - basic_vector->x1 += scalable_summand->x1 * scale; - basic_vector->x2 += scalable_summand->x2 * scale; + basic_vector->x1 += scalable_vector->x1 * scale; + basic_vector->x2 += scalable_vector->x2 * scale; } -static inline void dp_vector2_append_scaled(DPVector2* basic_vector, const DPVector2* scalable_summand, const double scale) +static inline void dp_vector2_append_scaled(DPVector2* basic_vector, const DPVector2* scalable_vector, const double scale) { - basic_vector->x1 += scalable_summand->x1 * scale; - basic_vector->x2 += scalable_summand->x2 * scale; + basic_vector->x1 += scalable_vector->x1 * scale; + basic_vector->x2 += scalable_vector->x2 * scale; } // ================== Average2 ================== // diff --git a/src/vector3.h b/src/vector3.h index 49c02e6..61b6f7c 100644 --- a/src/vector3.h +++ b/src/vector3.h @@ -228,18 +228,18 @@ static inline void dp_vector3_divide(const DPVector3* dividend, const double div // ================ Append scaled =============== // -static inline void sp_vector3_append_scaled(SPVector3* basic_vector, const SPVector3* scalable_summand, const float scale) +static inline void sp_vector3_append_scaled(SPVector3* basic_vector, const SPVector3* scalable_vector, const float scale) { - basic_vector->x1 += scalable_summand->x1 * scale; - basic_vector->x2 += scalable_summand->x2 * scale; - basic_vector->x3 += scalable_summand->x3 * scale; + basic_vector->x1 += scalable_vector->x1 * scale; + basic_vector->x2 += scalable_vector->x2 * scale; + basic_vector->x3 += scalable_vector->x3 * scale; } -static inline void dp_vector3_append_scaled(DPVector3* basic_vector, const DPVector3* scalable_summand, const double scale) +static inline void dp_vector3_append_scaled(DPVector3* basic_vector, const DPVector3* scalable_vector, const double scale) { - basic_vector->x1 += scalable_summand->x1 * scale; - basic_vector->x2 += scalable_summand->x2 * scale; - basic_vector->x3 += scalable_summand->x3 * scale; + basic_vector->x1 += scalable_vector->x1 * scale; + basic_vector->x2 += scalable_vector->x2 * scale; + basic_vector->x3 += scalable_vector->x3 * scale; } // ================== Average2 ================== // diff --git a/src/versor.h b/src/versor.h index d052f5a..5b9075b 100644 --- a/src/versor.h +++ b/src/versor.h @@ -117,16 +117,9 @@ static inline void sp_versor_set(const float s0, const float x1, const float x2, const float square_module = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); - if (1.0f - SP_TWO_EPSYLON <= square_module && square_module <= 1.0f + SP_TWO_EPSYLON) { - if (s0 > -1.0f + SP_EPSYLON) { - return; - } - - sp_versor_reset(result); - return; + if (square_module < 1.0f - SP_TWO_EPSYLON || 1.0f + SP_TWO_EPSYLON < square_module) { + __sp_versor_normalize(square_module, result); } - - __sp_versor_normalize(square_module, result); } static inline void dp_versor_set(const double s0, const double x1, const double x2, const double x3, DPVersor* result) @@ -138,16 +131,9 @@ static inline void dp_versor_set(const double s0, const double x1, const double const double square_module = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); - if (1.0 - DP_TWO_EPSYLON <= square_module && square_module <= 1.0 + DP_TWO_EPSYLON) { - if (s0 > -1.0 + DP_EPSYLON) { - return; - } - - dp_versor_reset(result); - return; + if (square_module < 1.0 - DP_TWO_EPSYLON || 1.0 + DP_TWO_EPSYLON < square_module) { + __dp_versor_normalize(square_module, result); } - - __dp_versor_normalize(square_module, result); } // ==================== Copy ==================== // @@ -268,24 +254,40 @@ static inline void dp_versor_make_inverted(const DPVersor* versor, DPVersor* res static inline void sp_versor_combine(const SPVersor* second, const SPVersor* first, SPVersor* result) { - sp_versor_set( - (second->_s0 * first->_s0 - second->_x1 * first->_x1) - (second->_x2 * first->_x2 + second->_x3 * first->_x3), - (second->_x1 * first->_s0 + second->_s0 * first->_x1) - (second->_x3 * first->_x2 - second->_x2 * first->_x3), - (second->_x2 * first->_s0 + second->_s0 * first->_x2) - (second->_x1 * first->_x3 - second->_x3 * first->_x1), - (second->_x3 * first->_s0 + second->_s0 * first->_x3) - (second->_x2 * first->_x1 - second->_x1 * first->_x2), - result - ); + const float s0 = (second->_s0 * first->_s0 - second->_x1 * first->_x1) - (second->_x2 * first->_x2 + second->_x3 * first->_x3); + const float x1 = (second->_x1 * first->_s0 + second->_s0 * first->_x1) - (second->_x3 * first->_x2 - second->_x2 * first->_x3); + const float x2 = (second->_x2 * first->_s0 + second->_s0 * first->_x2) - (second->_x1 * first->_x3 - second->_x3 * first->_x1); + const float x3 = (second->_x3 * first->_s0 + second->_s0 * first->_x3) - (second->_x2 * first->_x1 - second->_x1 * first->_x2); + + const float square_module = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); + + result->_s0 = s0; + result->_x1 = x1; + result->_x2 = x2; + result->_x3 = x3; + + if (square_module < 1.0f - SP_TWO_EPSYLON || 1.0f + SP_TWO_EPSYLON < square_module) { + __sp_versor_normalize(square_module, result); + } } static inline void dp_versor_combine(const DPVersor* second, const DPVersor* first, DPVersor* result) { - dp_versor_set( - (second->_s0 * first->_s0 - second->_x1 * first->_x1) - (second->_x2 * first->_x2 + second->_x3 * first->_x3), - (second->_x1 * first->_s0 + second->_s0 * first->_x1) - (second->_x3 * first->_x2 - second->_x2 * first->_x3), - (second->_x2 * first->_s0 + second->_s0 * first->_x2) - (second->_x1 * first->_x3 - second->_x3 * first->_x1), - (second->_x3 * first->_s0 + second->_s0 * first->_x3) - (second->_x2 * first->_x1 - second->_x1 * first->_x2), - result - ); + const double s0 = (second->_s0 * first->_s0 - second->_x1 * first->_x1) - (second->_x2 * first->_x2 + second->_x3 * first->_x3); + const double x1 = (second->_x1 * first->_s0 + second->_s0 * first->_x1) - (second->_x3 * first->_x2 - second->_x2 * first->_x3); + const double x2 = (second->_x2 * first->_s0 + second->_s0 * first->_x2) - (second->_x1 * first->_x3 - second->_x3 * first->_x1); + const double x3 = (second->_x3 * first->_s0 + second->_s0 * first->_x3) - (second->_x2 * first->_x1 - second->_x1 * first->_x2); + + const double square_module = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); + + result->_s0 = s0; + result->_x1 = x1; + result->_x2 = x2; + result->_x3 = x3; + + if (square_module < 1.0 - DP_TWO_EPSYLON || 1.0 + DP_TWO_EPSYLON < square_module) { + __dp_versor_normalize(square_module, result); + } } // ================= Rotation3 ================== // diff --git a/test/geometry_test.h b/test/geometry_test.h index f022a75..07449c7 100644 --- a/test/geometry_test.h +++ b/test/geometry_test.h @@ -6,6 +6,14 @@ #define TEST_RESULT_SUCCES 0 #define TEST_RESULT_FAILED 100 +#define TEST_SP_EPSYLON 1E-6f +#define TEST_SP_TWO_EPSYLON 2E-6f +#define TEST_SP_SQUARE_EPSYLON 1E-12f + +#define TEST_DP_EPSYLON 1E-13f +#define TEST_DP_TWO_EPSYLON 2E-13f +#define TEST_DP_SQUARE_EPSYLON 1E-26f + void print_test_section(const char * name); void print_test_name(const char * name); @@ -14,4 +22,32 @@ void print_test_success(); void print_test_failed(); +inline int test_sp_are_equal(const float value1, const float value2, const float epsylon) +{ + if (-1.0f <= value1 && value1 <= 1.0f) { + const float difference = value1 - value2; + return -epsylon <= difference && difference <= epsylon; + } + + if (value1 > 0.0f) { + return value1 <= value2 * (1.0f + epsylon) && value2 <= value1 * (1.0f + epsylon); + } + + return value1 * (1.0f + epsylon) <= value2 && value2 * (1.0f + epsylon) <= value1; +} + +inline int test_dp_are_equal(const double value1, const double value2, const double epsylon) +{ + if (-1.0 <= value1 && value1 <= 1.0) { + const double difference = value1 - value2; + return -epsylon <= difference && difference <= epsylon; + } + + if (value1 > 0.0) { + return value1 <= value2 * (1.0 + epsylon) && value2 <= value1 * (1.0 + epsylon); + } + + return value1 * (1.0 + epsylon) <= value2 && value2 * (1.0 + epsylon) <= value1; +} + #endif diff --git a/test/sp_vector2_test.c b/test/sp_vector2_test.c index 9c121a8..48b7c12 100644 --- a/test/sp_vector2_test.c +++ b/test/sp_vector2_test.c @@ -1,16 +1,150 @@ #include "sp_vector2_test.h" -int _test_sp_vector2_adding() +const int TEST_SP_VECTOR2_AMOUNT_1 = 5; + +const SPVector2 TEST_SP_VECTOR2_COMMON_1[] = { + { 3.0f, 4.0f }, + { -3.0f, -4.0f }, + { 10000.0f, -20000.0f }, + { 0.1f, -10.0f }, + { -123.5f, 3.7283f } +}; + +const SPVector2 TEST_SP_VECTOR2_COMMON_2[] = { + { -3.0f, -4.0f }, + { -3.0f, -4.0f }, + { 0.002f, -0.05f }, + { -0.2f, 12.0f }, + { 1.5f, -23.35f } +}; + +// =============== Square module ================ // + +const float SP_VECTOR2_SQUARE_MODULE_1[] = { 25.0f, 25.0f, 500000000.0f, 100.01f, 15266.150221f }; + +int test_sp_vector2_square_module() { + print_test_name("SPVector2 square module"); + + float square_module; + + for (int i = 0; i < TEST_SP_VECTOR2_AMOUNT_1; i++) { + square_module = sp_vector2_get_square_module(&TEST_SP_VECTOR2_COMMON_1[i]); + + if (!test_sp_are_equal(square_module, SP_VECTOR2_SQUARE_MODULE_1[i], TEST_SP_TWO_EPSYLON)) { + print_test_failed(); + return TEST_RESULT_FAILED; + } + } + + print_test_success(); return TEST_RESULT_SUCCES; } +// =================== Module =================== // + +const float SP_VECTOR2_MODULE_1[] = { 5.0f, 5.0f, 22360.68f, 10.0005f, 123.55626338f }; + +int test_sp_vector2_module() +{ + print_test_name("SPVector2 module"); + + float square_module; + + for (int i = 0; i < TEST_SP_VECTOR2_AMOUNT_1; i++) { + square_module = sp_vector2_get_module(&TEST_SP_VECTOR2_COMMON_1[i]); + + if (!test_sp_are_equal(square_module, SP_VECTOR2_MODULE_1[i], TEST_SP_EPSYLON)) { + print_test_failed(); + return TEST_RESULT_FAILED; + } + } + + print_test_success(); + return TEST_RESULT_SUCCES; +} + +// ===================== Add ==================== // + +const SPVector2 TEST_SP_VECTOR2_COMMON_1_2_SUM[] = { + { 0.0f, 0.0f }, + { -6.0f, -8.0f }, + { 10000.002f, -20000.05f }, + { -0.1f, 2.0f }, + { -122.0f, -19.6217f } +}; + +int test_sp_vector2_add() +{ + print_test_name("SPVector2 add"); + + SPVector2 vector; + + for (int i = 0; i < TEST_SP_VECTOR2_AMOUNT_1; i++) { + sp_vector2_add(&TEST_SP_VECTOR2_COMMON_1[i], &TEST_SP_VECTOR2_COMMON_2[i], &vector); + + if (!test_sp_are_equal(vector.x1, TEST_SP_VECTOR2_COMMON_1_2_SUM[i].x1, TEST_SP_EPSYLON) || + !test_sp_are_equal(vector.x2, TEST_SP_VECTOR2_COMMON_1_2_SUM[i].x2, TEST_SP_EPSYLON)) { + print_test_failed(); + return TEST_RESULT_FAILED; + } + } + + print_test_success(); + return TEST_RESULT_SUCCES; +} + +// ================== Subtract ================== // + +const SPVector2 TEST_SP_VECTOR2_COMMON_1_2_DIFF[] = { + { 6.0f, 8.0f }, + { 0.0f, 0.0f }, + { 9999.998f, -19999.95f }, + { 0.3f, -22.0f }, + { -125.0f, 27.0783f } +}; + +int test_sp_vector2_subtract() +{ + print_test_name("SPVector2 subtract"); + + SPVector2 vector; + + for (int i = 0; i < TEST_SP_VECTOR2_AMOUNT_1; i++) { + sp_vector2_subtract(&TEST_SP_VECTOR2_COMMON_1[i], &TEST_SP_VECTOR2_COMMON_2[i], &vector); + + if (!test_sp_are_equal(vector.x1, TEST_SP_VECTOR2_COMMON_1_2_DIFF[i].x1, TEST_SP_EPSYLON) || + !test_sp_are_equal(vector.x2, TEST_SP_VECTOR2_COMMON_1_2_DIFF[i].x2, TEST_SP_EPSYLON)) { + print_test_failed(); + return TEST_RESULT_FAILED; + } + } + + print_test_success(); + return TEST_RESULT_SUCCES; +} + +// ==================== 1234 ==================== // int test_sp_vector2() { print_test_section("SPVector2"); + if (test_sp_vector2_square_module() != TEST_RESULT_SUCCES) { + return TEST_RESULT_FAILED; + } + if (test_sp_vector2_module() != TEST_RESULT_SUCCES) { + return TEST_RESULT_FAILED; + } + + if (test_sp_vector2_add() != TEST_RESULT_SUCCES) { + return TEST_RESULT_FAILED; + } + + if (test_sp_vector2_subtract() != TEST_RESULT_SUCCES) { + return TEST_RESULT_FAILED; + } return TEST_RESULT_SUCCES; } diff --git a/test/sp_vector2_test.h b/test/sp_vector2_test.h index 1a61289..f12901f 100644 --- a/test/sp_vector2_test.h +++ b/test/sp_vector2_test.h @@ -5,4 +5,12 @@ int test_sp_vector2(); +int test_sp_vector2_square_module(); + +int test_sp_vector2_module(); + +int test_sp_vector2_add(); + +int test_sp_vector2_subtract(); + #endif