Skip to content

Commit

Permalink
Merge pull request #411 from EasyIP2023/bugfix/mat4x3-multiplication
Browse files Browse the repository at this point in the history
mat4x3: fix multiplication functions
  • Loading branch information
recp authored Mar 31, 2024
2 parents f07d75c + 013ac5d commit 28142b5
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 62 deletions.
66 changes: 60 additions & 6 deletions docs/source/mat4x3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ Functions:
#. :c:func:`glm_mat4x3_transpose`
#. :c:func:`glm_mat4x3_scale`

Represented
~~~~~~~~~~~

.. csv-table:: mat4x3
:header: "", "column 1", "column 2", "column 3", "column4"

"row 1", "m00", "m10", "m20", "m30"
"row 2", "m01", "m11", "m21", "m31"
"row 3", "m02", "m12", "m22", "m32"

Functions documentation
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -51,28 +61,72 @@ Functions documentation
| *[in]* **src** pointer to an array of floats
| *[out]* **dest** destination matrix4x3
.. c:function:: void glm_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat4 dest)
.. c:function:: void glm_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat3 dest)
multiply m1 and m2 to dest
.. code-block:: c
glm_mat4x3_mul(mat4x3, mat3x4, mat4);
glm_mat4x3_mul(mat4x3, mat3x4, mat3);
Parameters:
| *[in]* **m1** left matrix (mat4x3)
| *[in]* **m2** right matrix (mat3x4)
| *[out]* **dest** destination matrix (mat4)
| *[out]* **dest** destination matrix (mat3)
.. csv-table:: mat4x3
:header: "", "column 1", "column 2", "column 3", "column 4"

"row 1", "a00", "a10", "a20", "a30"
"row 2", "a01", "a11", "a21", "a31"
"row 3", "a02", "a12", "a22", "a32"

.. csv-table:: mat3x4
:header: "", "column 1", "column 2", "column 3"

"row 1", "b00", "b10", "b20"
"row 2", "b01", "b11", "b21"
"row 3", "b02", "b12", "b22"
"row 4", "b03", "b13", "b23"

.. csv-table:: mat3x3
:header: "", "column 1", "column 2", "column 3"

.. c:function:: void glm_mat4x3_mulv(mat4x3 m, vec3 v, vec4 dest)
"row 1", "a00 * b00 + a10 * b01 + a20 * b02 + a30 * b03", "a00 * b10 + a10 * b11 + a20 * b12 + a30 * b13", "a00 * b20 + a10 * b21 + a20 * b22 + a30 * b23"
"row 2", "a01 * b00 + a11 * b01 + a21 * b02 + a31 * b03", "a01 * b10 + a11 * b11 + a21 * b12 + a31 * b13", "a01 * b20 + a11 * b21 + a21 * b22 + a31 * b23"
"row 3", "a02 * b00 + a12 * b01 + a22 * b02 + a32 * b03", "a02 * b10 + a12 * b11 + a22 * b12 + a32 * b13", "a02 * b20 + a12 * b21 + a22 * b22 + a32 * b23"

multiply mat4x3 with vec3 (column vector) and store in dest vector
.. c:function:: void glm_mat4x3_mulv(mat4x3 m, vec4 v, vec3 dest)
multiply mat4x3 with vec4 (column vector) and store in dest column vector
Parameters:
| *[in]* **m** mat4x3 (left)
| *[in]* **v** vec3 (right, column vector)
| *[in]* **v** vec4 (right, column vector)
| *[out]* **dest** destination (result, column vector)
.. csv-table:: mat4x3
:header: "", "column 1", "column 2", "column 3", "column 4"
"row 1", "m00", "m10", "m20", "m30"
"row 2", "m01", "m11", "m21", "m31"
"row 3", "m02", "m12", "m22", "m32"
.. csv-table:: column vec4 (1x4)
:header: "", "column 1"
"row 1", "v0"
"row 2", "v1"
"row 3", "v2"
"row 4", "v3"
.. csv-table:: column vec3 (1x3)
:header: "", "column 1"
"row 1", "m00 * v0 + m10 * v1 + m20 * v2 + m30 * v3"
"row 2", "m01 * v0 + m11 * v1 + m21 * v2 + m31 * v3"
"row 3", "m02 * v0 + m12 * v1 + m22 * v2 + m32 * v3"
.. c:function:: void glm_mat4x3_transpose(mat4x3 m, mat3x4 dest)
transpose matrix and store in dest
Expand Down
4 changes: 2 additions & 2 deletions include/cglm/call/mat4x3.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ glmc_mat4x3_make(const float * __restrict src, mat4x3 dest);

CGLM_EXPORT
void
glmc_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat4 dest);
glmc_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat3 dest);

CGLM_EXPORT
void
glmc_mat4x3_mulv(mat4x3 m, vec3 v, vec4 dest);
glmc_mat4x3_mulv(mat4x3 m, vec4 v, vec3 dest);

CGLM_EXPORT
void
Expand Down
53 changes: 22 additions & 31 deletions include/cglm/mat4x3.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
CGLM_INLINE void glm_mat4x3_copy(mat4x3 mat, mat4x3 dest);
CGLM_INLINE void glm_mat4x3_zero(mat4x3 mat);
CGLM_INLINE void glm_mat4x3_make(const float * __restrict src, mat4x3 dest);
CGLM_INLINE void glm_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat4 dest);
CGLM_INLINE void glm_mat4x3_mulv(mat4x3 m, vec3 v, vec4 dest);
CGLM_INLINE void glm_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat3 dest);
CGLM_INLINE void glm_mat4x3_mulv(mat4x3 m, vec4 v, vec3 dest);
CGLM_INLINE void glm_mat4x3_transpose(mat4x3 m, mat3x4 dest);
CGLM_INLINE void glm_mat4x3_scale(mat4x3 m, float s);
*/
Expand Down Expand Up @@ -99,16 +99,16 @@ glm_mat4x3_make(const float * __restrict src, mat4x3 dest) {
* @brief multiply m1 and m2 to dest
*
* @code
* glm_mat4x3_mul(mat4x3, mat3x4, mat4);
* glm_mat4x3_mul(mat4x3, mat3x4, mat3);
* @endcode
*
* @param[in] m1 left matrix (mat4x3)
* @param[in] m2 right matrix (mat3x4)
* @param[out] dest destination matrix (mat4)
* @param[out] dest destination matrix (mat3)
*/
CGLM_INLINE
void
glm_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat4 dest) {
glm_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat3 dest) {
float a00 = m1[0][0], a01 = m1[0][1], a02 = m1[0][2],
a10 = m1[1][0], a11 = m1[1][1], a12 = m1[1][2],
a20 = m1[2][0], a21 = m1[2][1], a22 = m1[2][2],
Expand All @@ -118,43 +118,34 @@ glm_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat4 dest) {
b10 = m2[1][0], b11 = m2[1][1], b12 = m2[1][2], b13 = m2[1][3],
b20 = m2[2][0], b21 = m2[2][1], b22 = m2[2][2], b23 = m2[2][3];

dest[0][0] = a00 * b00 + a01 * b10 + a02 * b20;
dest[0][1] = a00 * b01 + a01 * b11 + a02 * b21;
dest[0][2] = a00 * b02 + a01 * b12 + a02 * b22;
dest[0][3] = a00 * b03 + a01 * b13 + a02 * b23;

dest[1][0] = a10 * b00 + a11 * b10 + a12 * b20;
dest[1][1] = a10 * b01 + a11 * b11 + a12 * b21;
dest[1][2] = a10 * b02 + a11 * b12 + a12 * b22;
dest[1][3] = a10 * b03 + a11 * b13 + a12 * b23;

dest[2][0] = a20 * b00 + a21 * b10 + a22 * b20;
dest[2][1] = a20 * b01 + a21 * b11 + a22 * b21;
dest[2][2] = a20 * b02 + a21 * b12 + a22 * b22;
dest[2][3] = a20 * b03 + a21 * b13 + a22 * b23;

dest[3][0] = a30 * b00 + a31 * b10 + a32 * b20;
dest[3][1] = a30 * b01 + a31 * b11 + a32 * b21;
dest[3][2] = a30 * b02 + a31 * b12 + a32 * b22;
dest[3][3] = a30 * b03 + a31 * b13 + a32 * b23;
dest[0][0] = a00 * b00 + a10 * b01 + a20 * b02 + a30 * b03;
dest[0][1] = a01 * b00 + a11 * b01 + a21 * b02 + a31 * b03;
dest[0][2] = a02 * b00 + a12 * b01 + a22 * b02 + a32 * b03;

dest[1][0] = a00 * b10 + a10 * b11 + a20 * b12 + a30 * b13;
dest[1][1] = a01 * b10 + a11 * b11 + a21 * b12 + a31 * b13;
dest[1][2] = a02 * b10 + a12 * b11 + a22 * b12 + a32 * b13;

dest[2][0] = a00 * b20 + a10 * b21 + a20 * b22 + a30 * b23;
dest[2][1] = a01 * b20 + a11 * b21 + a21 * b22 + a31 * b23;
dest[2][2] = a02 * b20 + a12 * b21 + a22 * b22 + a32 * b23;
}

/*!
* @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_mat4x3_mulv(mat4x3 m, vec3 v, vec4 dest) {
float v0 = v[0], v1 = v[1], v2 = v[2];
glm_mat4x3_mulv(mat4x3 m, vec4 v, vec3 dest) {
float v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];

dest[0] = m[0][0] * v0 + m[0][1] * v1 + m[0][2] * v2;
dest[1] = m[1][0] * v0 + m[1][1] * v1 + m[1][2] * v2;
dest[2] = m[2][0] * v0 + m[2][1] * v1 + m[2][2] * v2;
dest[3] = m[3][0] * v0 + m[3][1] * v1 + m[3][2] * v2;
dest[0] = m[0][0] * v0 + m[1][0] * v1 + m[2][0] * v2 + m[3][0] * v3;
dest[1] = m[0][1] * v0 + m[1][1] * v1 + m[2][1] * v2 + m[3][1] * v3;
dest[2] = m[0][2] * v0 + m[1][2] * v1 + m[2][2] * v2 + m[3][2] * v3;
}

/*!
Expand Down
18 changes: 9 additions & 9 deletions include/cglm/struct/mat4x3.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
Functions:
CGLM_INLINE mat4x3s glms_mat4x3_zero(void);
CGLM_INLINE mat4x3s glms_mat4x3_make(const float * __restrict src);
CGLM_INLINE mat4s glms_mat4x3_mul(mat4x3s m1, mat3x4s m2);
CGLM_INLINE vec4s glms_mat4x3_mulv(mat4x3s m, vec3s v);
CGLM_INLINE mat3s glms_mat4x3_mul(mat4x3s m1, mat3x4s m2);
CGLM_INLINE vec3s glms_mat4x3_mulv(mat4x3s m, vec4s v);
CGLM_INLINE mat3x4s glms_mat4x3_transpose(mat4x3s m);
CGLM_INLINE mat4x3s glms_mat4x3_scale(mat4x3s m, float s);
*/
Expand Down Expand Up @@ -70,12 +70,12 @@ glms_mat4x3_(make)(const float * __restrict src) {
*
* @param[in] m1 left matrix (mat4x3s)
* @param[in] m2 right matrix (mat3x4s)
* @returns destination matrix (mat4s)
* @returns destination matrix (mat3s)
*/
CGLM_INLINE
mat4s
mat3s
glms_mat4x3_(mul)(mat4x3s m1, mat3x4s m2) {
mat4s r;
mat3s r;
glm_mat4x3_mul(m1.raw, m2.raw, r.raw);
return r;
}
Expand All @@ -85,12 +85,12 @@ glms_mat4x3_(mul)(mat4x3s m1, mat3x4s m2) {
*
* @param[in] m matrix (left)
* @param[in] v vector (right, column vector)
* @param[out] dest result vector
* @returns destination vector (vec3s)
*/
CGLM_INLINE
vec4s
glms_mat4x3_(mulv)(mat4x3s m, vec3s v) {
vec4s r;
vec3s
glms_mat4x3_(mulv)(mat4x3s m, vec4s v) {
vec3s r;
glm_mat4x3_mulv(m.raw, v.raw, r.raw);
return r;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mat4x3.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ glmc_mat4x3_make(const float * __restrict src, mat4x3 dest) {

CGLM_EXPORT
void
glmc_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat4 dest) {
glmc_mat4x3_mul(mat4x3 m1, mat3x4 m2, mat3 dest) {
glm_mat4x3_mul(m1, m2, dest);
}

CGLM_EXPORT
void
glmc_mat4x3_mulv(mat4x3 m, vec3 v, vec4 dest) {
glmc_mat4x3_mulv(mat4x3 m, vec4 v, vec3 dest) {
glm_mat4x3_mulv(m, v, dest);
}

Expand Down
24 changes: 12 additions & 12 deletions test/src/test_mat4x3.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,40 +91,40 @@ TEST_IMPL(GLM_PREFIX, mat4x3_mul) {
mat4x3 m1 = GLM_MAT4X3_ZERO_INIT;
mat3x4 m2 = GLM_MAT3X4_ZERO_INIT;

mat4 m3 = GLM_MAT4_ZERO_INIT;
mat4 m4 = GLM_MAT4_ZERO_INIT;
mat3 m3 = GLM_MAT3_ZERO_INIT;
mat3 m4 = GLM_MAT3_ZERO_INIT;

int i, j, k;
int c, r, k;

test_rand_mat4x3(m1);
test_rand_mat3x4(m2);

for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 3; k++) {
m4[i][j] += m1[i][k] * m2[k][j];
for (r = 0; r < 3; r++) {
for (c = 0; c < 3; c++) {
for (k = 0; k < 4; k++) {
m4[c][r] += m1[k][r] * m2[c][k];
}
}
}

GLM(mat4x3_mul)(m1, m2, m3);
ASSERTIFY(test_assert_mat4_eq(m3, m4))
ASSERTIFY(test_assert_mat3_eq(m3, m4))

TEST_SUCCESS
}

TEST_IMPL(GLM_PREFIX, mat4x3_mulv) {
mat4x3 mat = A_MATRIX4X3;
vec3 v = {11.0f, 21.0f, 31.0f};
vec4 v = {11.0f, 21.0f, 31.0f, 41.0f};

int i;
vec4 dest;
vec3 dest;
float res = 0.0;

GLM(mat4x3_mulv)(mat, v, dest);

for (i = 0; i < 4; i++) {
res = mat[i][0] * v[0] + mat[i][1] * v[1] + mat[i][2] * v[2];
for (i = 0; i < 3; i++) {
res = mat[0][i] * v[0] + mat[1][i] * v[1] + mat[2][i] * v[2] + mat[3][i] * v[3];
ASSERT(test_eq(dest[i], res))
}

Expand Down

0 comments on commit 28142b5

Please sign in to comment.