Skip to content

Commit

Permalink
Merge pull request #408 from EasyIP2023/bugfix/mat3x4-multiplication
Browse files Browse the repository at this point in the history
mat3x4: fix multiplication functions
  • Loading branch information
recp authored Mar 31, 2024
2 parents e3ed983 + fc7f0e1 commit 17d8b83
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 58 deletions.
69 changes: 63 additions & 6 deletions docs/source/mat3x4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions include/cglm/call/mat3x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 34 additions & 26 deletions include/cglm/mat3x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
*/
Expand Down Expand Up @@ -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],
Expand All @@ -106,34 +106,43 @@ 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)
* @param[out] dest result vector
*/
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;
}

/*!
Expand Down Expand Up @@ -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
20 changes: 10 additions & 10 deletions include/cglm/struct/mat3x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
*/
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mat3x4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
24 changes: 12 additions & 12 deletions test/src/test_mat3x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down

0 comments on commit 17d8b83

Please sign in to comment.