From 1340b5d512fb3667c676080ba17070f203f79f51 Mon Sep 17 00:00:00 2001 From: Vincent Davis Jr Date: Sun, 31 Mar 2024 01:48:50 -0400 Subject: [PATCH 1/2] mat3x4: fix multiplication functions Signed-off-by: Vincent Davis Jr --- include/cglm/call/mat3x4.h | 4 +-- include/cglm/mat3x4.h | 60 ++++++++++++++++++++---------------- include/cglm/struct/mat3x4.h | 20 ++++++------ src/mat3x4.c | 4 +-- test/src/test_mat3x4.h | 24 +++++++-------- 5 files changed, 60 insertions(+), 52 deletions(-) diff --git a/include/cglm/call/mat3x4.h b/include/cglm/call/mat3x4.h index 5339e1661..ff23b4c4c 100644 --- a/include/cglm/call/mat3x4.h +++ b/include/cglm/call/mat3x4.h @@ -27,11 +27,11 @@ glmc_mat3x4_make(const float * __restrict src, mat3x4 dest); CGLM_EXPORT void -glmc_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat3 dest); +glmc_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat4 dest); CGLM_EXPORT void -glmc_mat3x4_mulv(mat3x4 m, vec4 v, vec3 dest); +glmc_mat3x4_mulv(mat3x4 m, vec3 v, vec4 dest); CGLM_EXPORT void diff --git a/include/cglm/mat3x4.h b/include/cglm/mat3x4.h index aec4295d7..8d14523c9 100644 --- a/include/cglm/mat3x4.h +++ b/include/cglm/mat3x4.h @@ -14,8 +14,8 @@ CGLM_INLINE void glm_mat3x4_copy(mat3x4 mat, mat3x4 dest); CGLM_INLINE void glm_mat3x4_zero(mat3x4 mat); CGLM_INLINE void glm_mat3x4_make(const float * __restrict src, mat3x4 dest); - CGLM_INLINE void glm_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat3 dest); - CGLM_INLINE void glm_mat3x4_mulv(mat3x4 m, vec4 v, vec3 dest); + CGLM_INLINE void glm_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat4 dest); + CGLM_INLINE void glm_mat3x4_mulv(mat3x4 m, vec3 v, vec4 dest); CGLM_INLINE void glm_mat3x4_transpose(mat3x4 m, mat4x3 dest); CGLM_INLINE void glm_mat3x4_scale(mat3x4 m, float s); */ @@ -87,16 +87,16 @@ glm_mat3x4_make(const float * __restrict src, mat3x4 dest) { * @brief multiply m1 and m2 to dest * * @code - * glm_mat3x4_mul(mat3x4, mat4x3, mat3); + * glm_mat3x4_mul(mat3x4, mat4x3, mat4); * @endcode * * @param[in] m1 left matrix (mat3x4) * @param[in] m2 right matrix (mat4x3) - * @param[out] dest destination matrix (mat3) + * @param[out] dest destination matrix (mat4) */ CGLM_INLINE void -glm_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat3 dest) { +glm_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat4 dest) { float a00 = m1[0][0], a01 = m1[0][1], a02 = m1[0][2], a03 = m1[0][3], a10 = m1[1][0], a11 = m1[1][1], a12 = m1[1][2], a13 = m1[1][3], a20 = m1[2][0], a21 = m1[2][1], a22 = m1[2][2], a23 = m1[2][3], @@ -106,21 +106,29 @@ glm_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat3 dest) { b20 = m2[2][0], b21 = m2[2][1], b22 = m2[2][2], b30 = m2[3][0], b31 = m2[3][1], b32 = m2[3][2]; - dest[0][0] = a00 * b00 + a01 * b10 + a02 * b20 + a03 * b30; - dest[0][1] = a00 * b01 + a01 * b11 + a02 * b21 + a03 * b31; - dest[0][2] = a00 * b02 + a01 * b12 + a02 * b22 + a03 * b32; - - dest[1][0] = a10 * b00 + a11 * b10 + a12 * b20 + a13 * b30; - dest[1][1] = a10 * b01 + a11 * b11 + a12 * b21 + a13 * b31; - dest[1][2] = a10 * b02 + a11 * b12 + a12 * b22 + a13 * b32; - - dest[2][0] = a20 * b00 + a21 * b10 + a22 * b20 + a23 * b30; - dest[2][1] = a20 * b01 + a21 * b11 + a22 * b21 + a23 * b31; - dest[2][2] = a20 * b02 + a21 * b12 + a22 * b22 + a23 * b32; + dest[0][0] = a00 * b00 + a10 * b01 + a20 * b02; + dest[0][1] = a01 * b00 + a11 * b01 + a21 * b02; + dest[0][2] = a02 * b00 + a12 * b01 + a22 * b02; + dest[0][3] = a03 * b00 + a13 * b01 + a23 * b02; + + dest[1][0] = a00 * b10 + a10 * b11 + a20 * b12; + dest[1][1] = a01 * b10 + a11 * b11 + a21 * b12; + dest[1][2] = a02 * b10 + a12 * b11 + a22 * b12; + dest[1][3] = a03 * b10 + a13 * b11 + a23 * b12; + + dest[2][0] = a00 * b20 + a10 * b21 + a20 * b22; + dest[2][1] = a01 * b20 + a11 * b21 + a21 * b22; + dest[2][2] = a02 * b20 + a12 * b21 + a22 * b22; + dest[2][3] = a03 * b20 + a13 * b21 + a23 * b22; + + dest[3][0] = a00 * b30 + a10 * b31 + a20 * b32; + dest[3][1] = a01 * b30 + a11 * b31 + a21 * b32; + dest[3][2] = a02 * b30 + a12 * b31 + a22 * b32; + dest[3][3] = a03 * b30 + a13 * b31 + a23 * b32; } /*! - * @brief multiply matrix with column vector and store in dest vector + * @brief multiply matrix with column vector and store in dest column vector * * @param[in] m matrix (left) * @param[in] v vector (right, column vector) @@ -128,12 +136,13 @@ glm_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat3 dest) { */ CGLM_INLINE void -glm_mat3x4_mulv(mat3x4 m, vec4 v, vec3 dest) { - float v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; +glm_mat3x4_mulv(mat3x4 m, vec3 v, vec4 dest) { + float v0 = v[0], v1 = v[1], v2 = v[2]; - dest[0] = m[0][0] * v0 + m[0][1] * v1 + m[0][2] * v2 + m[0][3] * v3; - dest[1] = m[1][0] * v0 + m[1][1] * v1 + m[1][2] * v2 + m[1][3] * v3; - dest[2] = m[2][0] * v0 + m[2][1] * v1 + m[2][2] * v2 + m[2][3] * v3; + dest[0] = m[0][0] * v0 + m[1][0] * v1 + m[2][0] * v2; + dest[1] = m[0][1] * v0 + m[1][1] * v1 + m[2][1] * v2; + dest[2] = m[0][2] * v0 + m[1][2] * v1 + m[2][2] * v2; + dest[3] = m[0][3] * v0 + m[1][3] * v1 + m[2][3] * v2; } /*! @@ -162,10 +171,9 @@ glm_mat3x4_transpose(mat3x4 m, mat4x3 dest) { CGLM_INLINE void glm_mat3x4_scale(mat3x4 m, float s) { - m[0][0] *= s; m[1][0] *= s; m[2][0] *= s; - m[0][1] *= s; m[1][1] *= s; m[2][1] *= s; - m[0][2] *= s; m[1][2] *= s; m[2][2] *= s; - m[0][3] *= s; m[1][3] *= s; m[2][3] *= s; + m[0][0] *= s; m[0][1] *= s; m[0][2] *= s; m[0][3] *= s; + m[1][0] *= s; m[1][1] *= s; m[1][2] *= s; m[1][3] *= s; + m[2][0] *= s; m[2][1] *= s; m[2][2] *= s; m[2][3] *= s; } #endif diff --git a/include/cglm/struct/mat3x4.h b/include/cglm/struct/mat3x4.h index 29dd87002..ee688f0bf 100644 --- a/include/cglm/struct/mat3x4.h +++ b/include/cglm/struct/mat3x4.h @@ -13,8 +13,8 @@ Functions: CGLM_INLINE mat3x4s glms_mat3x4_zero(void); CGLM_INLINE mat3x4s glms_mat3x4_make(const float * __restrict src); - CGLM_INLINE mat3s glms_mat3x4_mul(mat3x4s m1, mat4x3s m2); - CGLM_INLINE vec3s glms_mat3x4_mulv(mat3x4s m, vec4s v); + CGLM_INLINE mat4s glms_mat3x4_mul(mat3x4s m1, mat4x3s m2); + CGLM_INLINE vec4s glms_mat3x4_mulv(mat3x4s m, vec3s v); CGLM_INLINE mat4x3s glms_mat3x4_transpose(mat3x4s m); CGLM_INLINE mat3x4s glms_mat3x4_scale(mat3x4s m, float s); */ @@ -70,27 +70,27 @@ glms_mat3x4_(make)(const float * __restrict src) { * * @param[in] m1 left matrix (mat3x4s) * @param[in] m2 right matrix (mat4x3s) - * @returns destination matrix (mat3s) + * @returns destination matrix (mat4s) */ CGLM_INLINE -mat3s +mat4s glms_mat3x4_(mul)(mat3x4s m1, mat4x3s m2) { - mat3s r; + mat4s r; glm_mat3x4_mul(m1.raw, m2.raw, r.raw); return r; } /*! - * @brief multiply matrix with column vector and store in dest vector + * @brief multiply matrix with column vector and store in dest column vector * * @param[in] m matrix (left) * @param[in] v vector (right, column vector) - * @param[out] dest result vector + * @returns destination vector (vec4s) */ CGLM_INLINE -vec3s -glms_mat3x4_(mulv)(mat3x4s m, vec4s v) { - vec3s r; +vec4s +glms_mat3x4_(mulv)(mat3x4s m, vec3s v) { + vec4s r; glm_mat3x4_mulv(m.raw, v.raw, r.raw); return r; } diff --git a/src/mat3x4.c b/src/mat3x4.c index efc906241..75963c9be 100644 --- a/src/mat3x4.c +++ b/src/mat3x4.c @@ -28,13 +28,13 @@ glmc_mat3x4_make(const float * __restrict src, mat3x4 dest) { CGLM_EXPORT void -glmc_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat3 dest) { +glmc_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat4 dest) { glm_mat3x4_mul(m1, m2, dest); } CGLM_EXPORT void -glmc_mat3x4_mulv(mat3x4 m, vec4 v, vec3 dest) { +glmc_mat3x4_mulv(mat3x4 m, vec3 v, vec4 dest) { glm_mat3x4_mulv(m, v, dest); } diff --git a/test/src/test_mat3x4.h b/test/src/test_mat3x4.h index c7d704c52..16520ac19 100644 --- a/test/src/test_mat3x4.h +++ b/test/src/test_mat3x4.h @@ -90,40 +90,40 @@ TEST_IMPL(GLM_PREFIX, mat3x4_mul) { mat3x4 m1 = GLM_MAT3X4_ZERO_INIT; mat4x3 m2 = GLM_MAT4X3_ZERO_INIT; - mat3 m3 = GLM_MAT3_ZERO_INIT; - mat3 m4 = GLM_MAT3_ZERO_INIT; + mat4 m3 = GLM_MAT4_ZERO_INIT; + mat4 m4 = GLM_MAT4_ZERO_INIT; - int i, j, k; + int c, r, k; test_rand_mat3x4(m1); test_rand_mat4x3(m2); - for (i = 0; i < 3; i++) { - for (j = 0; j < 3; j++) { - for (k = 0; k < 4; k++) { - m4[i][j] += m1[i][k] * m2[k][j]; + for (r = 0; r < 4; r++) { + for (c = 0; c < 4; c++) { + for (k = 0; k < 3; k++) { + m4[c][r] += m1[k][r] * m2[c][k]; } } } GLM(mat3x4_mul)(m1, m2, m3); - ASSERTIFY(test_assert_mat3_eq(m3, m4)) + ASSERTIFY(test_assert_mat4_eq(m3, m4)) TEST_SUCCESS } TEST_IMPL(GLM_PREFIX, mat3x4_mulv) { mat3x4 mat = A_MATRIX3X4; - vec4 v = {11.0f, 21.0f, 31.0f, 41.0f}; + vec4 v = {11.0f, 21.0f, 31.0f}; int i; - vec3 dest; + vec4 dest; float res = 0.0; GLM(mat3x4_mulv)(mat, v, dest); - for (i = 0; i < 3; i++) { - res = mat[i][0] * v[0] + mat[i][1] * v[1] + mat[i][2] * v[2]; + for (i = 0; i < 4; i++) { + res = mat[0][i] * v[0] + mat[1][i] * v[1] + mat[2][i] * v[2]; ASSERT(test_eq(dest[i], res)) } From fc7f0e13fd71e3e978fcab3eb56764e3ffebddf4 Mon Sep 17 00:00:00 2001 From: Vincent Davis Jr Date: Sun, 31 Mar 2024 02:07:21 -0400 Subject: [PATCH 2/2] docs: mat3x4 account for latest mulitplication changes This also includes tables to explain how mat3x4, column vectors, and row vectors are represented. Also includes how resulting matrix or vector is formed. Signed-off-by: Vincent Davis Jr --- docs/source/mat3x4.rst | 69 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/docs/source/mat3x4.rst b/docs/source/mat3x4.rst index 9c23d06eb..cddce4789 100644 --- a/docs/source/mat3x4.rst +++ b/docs/source/mat3x4.rst @@ -23,6 +23,17 @@ Functions: #. :c:func:`glm_mat3x4_transpose` #. :c:func:`glm_mat3x4_scale` +Represented +~~~~~~~~~~~ + +.. csv-table:: mat3x4 + :header: "", "column 1", "column 2", "column 3" + + "row 1", "m00", "m10", "m20" + "row 2", "m01", "m11", "m21" + "row 3", "m02", "m12", "m22" + "row 4", "m03", "m13", "m23" + Functions documentation ~~~~~~~~~~~~~~~~~~~~~~~ @@ -51,28 +62,74 @@ Functions documentation | *[in]* **src** pointer to an array of floats | *[out]* **dest** destination matrix3x4 -.. c:function:: void glm_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat3 dest) +.. c:function:: void glm_mat3x4_mul(mat3x4 m1, mat4x3 m2, mat4 dest) multiply m1 and m2 to dest .. code-block:: c - glm_mat3x4_mul(mat3x4, mat4x3, mat3); + glm_mat3x4_mul(mat3x4, mat4x3, mat4); Parameters: | *[in]* **m1** left matrix (mat3x4) | *[in]* **m2** right matrix (mat4x3) - | *[out]* **dest** destination matrix (mat3) + | *[out]* **dest** destination matrix (mat4) + + .. csv-table:: mat3x4 + :header: "", "column 1", "column 2", "column 3" + + "row 1", "a00", "a10", "a20" + "row 2", "a01", "a11", "a21" + "row 3", "a02", "a12", "a22" + "row 4", "a03", "a13", "a23" + + .. csv-table:: mat4x3 + :header: "", "column 1", "column 2", "column 3", "column 4" + + "row 1", "b00", "b10", "b20", "b30" + "row 2", "b01", "b11", "b21", "b31" + "row 3", "b02", "b12", "b22", "b32" + + .. csv-table:: mat4x4 + :header: "", "column 1", "column 2", "column 3", "column 4" -.. c:function:: void glm_mat3x4_mulv(mat3x4 m, vec4 v, vec3 dest) + "row 1", "a00 * b00 + a10 * b01 + a20 * b02", "a00 * b10 + a10 * b11 + a20 * b12", "a00 * b20 + a10 * b21 + a20 * b22", "a00 * b30 + a10 * b31 + a20 * b32" + "row 2", "a01 * b00 + a11 * b01 + a21 * b02", "a01 * b10 + a11 * b11 + a21 * b12", "a01 * b20 + a11 * b21 + a21 * b22", "a01 * b30 + a11 * b31 + a21 * b32" + "row 3", "a02 * b00 + a12 * b01 + a22 * b02", "a02 * b10 + a12 * b11 + a22 * b12", "a02 * b20 + a12 * b21 + a22 * b22", "a02 * b30 + a12 * b31 + a22 * b32" + "row 4", "a03 * b00 + a13 * b01 + a23 * b02", "a03 * b10 + a13 * b11 + a23 * b12", "a03 * b20 + a13 * b21 + a23 * b22", "a03 * b30 + a13 * b31 + a23 * b32" - multiply mat3x4 with vec4 (column vector) and store in dest vector +.. c:function:: void glm_mat3x4_mulv(mat3x4 m, vec3 v, vec4 dest) + + multiply mat3x4 with vec3 (column vector) and store in dest vector Parameters: | *[in]* **m** mat3x4 (left) - | *[in]* **v** vec4 (right, column vector) + | *[in]* **v** vec3 (right, column vector) | *[out]* **dest** destination (result, column vector) + .. csv-table:: mat3x4 + :header: "", "column 1", "column 2", "column 3" + + "row 1", "m00", "m10", "m20" + "row 2", "m01", "m11", "m21" + "row 3", "m02", "m12", "m22" + "row 4", "m03", "m13", "m23" + + .. csv-table:: column vec3 (1x3) + :header: "", "column 1" + + "row 1", "v0" + "row 2", "v1" + "row 3", "v2" + + .. csv-table:: column vec4 (1x4) + :header: "", "column 1" + + "row 1", "m00 * v0 + m10 * v1 + m20 * v2" + "row 2", "m01 * v0 + m11 * v1 + m21 * v2" + "row 3", "m02 * v0 + m12 * v1 + m22 * v2" + "row 4", "m03 * v0 + m13 * v1 + m23 * v2" + .. c:function:: void glm_mat3x4_transpose(mat3x4 m, mat4x3 dest) transpose matrix and store in dest