diff --git a/docs/source/mat3.rst b/docs/source/mat3.rst index 5d590d9..8592c4e 100644 --- a/docs/source/mat3.rst +++ b/docs/source/mat3.rst @@ -29,6 +29,7 @@ Functions: #. :c:func:`glm_mat3_scale` #. :c:func:`glm_mat3_det` #. :c:func:`glm_mat3_inv` +#. :c:func:`glm_mat3_trace` #. :c:func:`glm_mat3_swap_col` #. :c:func:`glm_mat3_swap_row` @@ -133,6 +134,16 @@ Functions documentation | *[in]* **mat** matrix | *[out]* **dest** destination (inverse matrix) +.. c:function:: void glm_mat3_trace(mat3 m) + + | sum of the elements on the main diagonal from upper left to the lower right + + Parameters: + | *[in]* **m** matrix + + Returns: + trace of matrix + .. c:function:: void glm_mat3_swap_col(mat3 mat, int col1, int col2) swap two matrix columns diff --git a/docs/source/mat4.rst b/docs/source/mat4.rst index ae37147..30f48fe 100644 --- a/docs/source/mat4.rst +++ b/docs/source/mat4.rst @@ -33,6 +33,8 @@ Functions: #. :c:func:`glm_mat4_mulN` #. :c:func:`glm_mat4_mulv` #. :c:func:`glm_mat4_mulv3` +#. :c:func:`glm_mat3_trace` +#. :c:func:`glm_mat3_trace3` #. :c:func:`glm_mat4_quat` #. :c:func:`glm_mat4_transpose_to` #. :c:func:`glm_mat4_transpose` @@ -156,6 +158,27 @@ Functions documentation | *[in]* **v** vec3 (right, column vector) | *[out]* **dest** vec3 (result, column vector) +.. c:function:: void glm_mat4_trace(mat4 m) + + | sum of the elements on the main diagonal from upper left to the lower right + + Parameters: + | *[in]* **m** matrix + + Returns: + trace of matrix + +.. c:function:: void glm_mat4_trace3(mat4 m) + + | trace of matrix (rotation part) + | sum of the elements on the main diagonal from upper left to the lower right + + Parameters: + | *[in]* **m** matrix + + Returns: + trace of matrix + .. c:function:: void glm_mat4_quat(mat4 m, versor dest) convert mat4's rotation part to quaternion diff --git a/include/cglm/call/mat3.h b/include/cglm/call/mat3.h index 9270113..f27e187 100644 --- a/include/cglm/call/mat3.h +++ b/include/cglm/call/mat3.h @@ -44,6 +44,10 @@ CGLM_EXPORT void glmc_mat3_mulv(mat3 m, vec3 v, vec3 dest); +CGLM_EXPORT +float +glmc_mat3_trace(mat3 m); + CGLM_EXPORT void glmc_mat3_quat(mat3 m, versor dest); diff --git a/include/cglm/call/mat4.h b/include/cglm/call/mat4.h index daa7b7e..7e76f73 100644 --- a/include/cglm/call/mat4.h +++ b/include/cglm/call/mat4.h @@ -61,6 +61,14 @@ CGLM_EXPORT void glmc_mat4_mulv3(mat4 m, vec3 v, float last, vec3 dest); +CGLM_EXPORT +float +glmc_mat4_trace(mat4 m); + +CGLM_EXPORT +float +glmc_mat4_trace3(mat4 m); + CGLM_EXPORT void glmc_mat4_quat(mat4 m, versor dest); diff --git a/include/cglm/mat3.h b/include/cglm/mat3.h index 2d6523c..05bcf11 100644 --- a/include/cglm/mat3.h +++ b/include/cglm/mat3.h @@ -21,6 +21,7 @@ CGLM_INLINE void glm_mat3_transpose_to(mat3 m, mat3 dest); CGLM_INLINE void glm_mat3_transpose(mat3 m); CGLM_INLINE void glm_mat3_mulv(mat3 m, vec3 v, vec3 dest); + CGLM_INLINE float glm_mat3_trace(mat3 m); CGLM_INLINE void glm_mat3_scale(mat3 m, float s); CGLM_INLINE float glm_mat3_det(mat3 mat); CGLM_INLINE void glm_mat3_inv(mat3 mat, mat3 dest); @@ -207,6 +208,18 @@ glm_mat3_mulv(mat3 m, vec3 v, vec3 dest) { dest[2] = m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2]; } +/*! + * @brief trace of matrix + * + * sum of the elements on the main diagonal from upper left to the lower right + * + * @param[in] m matrix + */ +CGLM_INLINE +float +glm_mat3_trace(mat3 m) { + return m[0][0] + m[1][1] + m[2][2]; +} /*! * @brief convert mat3 to quaternion diff --git a/include/cglm/mat4.h b/include/cglm/mat4.h index 8ce027d..ea3b34e 100644 --- a/include/cglm/mat4.h +++ b/include/cglm/mat4.h @@ -29,6 +29,8 @@ CGLM_INLINE void glm_mat4_mulN(mat4 *matrices[], int len, mat4 dest); CGLM_INLINE void glm_mat4_mulv(mat4 m, vec4 v, vec4 dest); CGLM_INLINE void glm_mat4_mulv3(mat4 m, vec3 v, vec3 dest); + CGLM_INLINE float glm_mat4_trace(mat4 m); + CGLM_INLINE float glm_mat4_trace3(mat4 m); CGLM_INLINE void glm_mat4_transpose_to(mat4 m, mat4 dest); CGLM_INLINE void glm_mat4_transpose(mat4 m); CGLM_INLINE void glm_mat4_scale_p(mat4 m, float s); @@ -338,6 +340,32 @@ glm_mat4_mulv(mat4 m, vec4 v, vec4 dest) { #endif } +/*! + * @brief trace of matrix + * + * sum of the elements on the main diagonal from upper left to the lower right + * + * @param[in] m matrix + */ +CGLM_INLINE +float +glm_mat4_trace(mat4 m) { + return m[0][0] + m[1][1] + m[2][2] + m[3][3]; +} + +/*! + * @brief trace of matrix (rotation part) + * + * sum of the elements on the main diagonal from upper left to the lower right + * + * @param[in] m matrix + */ +CGLM_INLINE +float +glm_mat4_trace3(mat4 m) { + return m[0][0] + m[1][1] + m[2][2]; +} + /*! * @brief convert mat4's rotation part to quaternion * diff --git a/src/mat3.c b/src/mat3.c index be603e8..acb981b 100644 --- a/src/mat3.c +++ b/src/mat3.c @@ -50,6 +50,12 @@ glmc_mat3_mulv(mat3 m, vec3 v, vec3 dest) { glm_mat3_mulv(m, v, dest); } +CGLM_EXPORT +float +glmc_mat3_trace(mat3 m) { + return glm_mat3_trace(m); +} + CGLM_EXPORT void glmc_mat3_quat(mat3 m, versor dest) { diff --git a/src/mat4.c b/src/mat4.c index 7bb99e2..b62420e 100644 --- a/src/mat4.c +++ b/src/mat4.c @@ -74,6 +74,18 @@ glmc_mat4_mulv3(mat4 m, vec3 v, float last, vec3 dest) { glm_mat4_mulv3(m, v, last, dest); } +CGLM_EXPORT +float +glmc_mat4_trace(mat4 m) { + return glm_mat4_trace(m); +} + +CGLM_EXPORT +float +glmc_mat4_trace3(mat4 m) { + return glm_mat4_trace3(m); +} + CGLM_EXPORT void glmc_mat4_quat(mat4 m, versor dest) {