From 3d292c3a2ea4056f6b6458f6a73b1a8f8ecf94b0 Mon Sep 17 00:00:00 2001 From: Vincent Davis Jr Date: Mon, 17 Jul 2023 22:57:52 -0400 Subject: [PATCH] add new matrix mat4x3 Initial function being glm_mat4x3_make Signed-off-by: Vincent Davis Jr --- CMakeLists.txt | 1 + Makefile.am | 4 +++ docs/source/api_inline_array.rst | 1 + docs/source/mat4x3.rst | 30 ++++++++++++++++ include/cglm/call.h | 1 + include/cglm/call/mat4x3.h | 23 ++++++++++++ include/cglm/cglm.h | 1 + include/cglm/mat4x3.h | 54 ++++++++++++++++++++++++++++ include/cglm/struct.h | 1 + include/cglm/struct/mat4x3.h | 46 ++++++++++++++++++++++++ include/cglm/types-struct.h | 13 +++++++ include/cglm/types.h | 1 + meson.build | 1 + src/mat4x3.c | 15 ++++++++ test/src/test_common.c | 13 +++++++ test/src/test_common.h | 3 ++ test/src/test_mat4x3.h | 60 ++++++++++++++++++++++++++++++++ test/src/test_struct.c | 12 +++++++ test/src/tests.c | 2 ++ test/tests.h | 16 +++++++++ win/cglm-test.vcxproj | 1 + win/cglm-test.vcxproj.filters | 3 ++ win/cglm.vcxproj | 4 +++ win/cglm.vcxproj.filters | 12 +++++++ 24 files changed, 318 insertions(+) create mode 100644 docs/source/mat4x3.rst create mode 100644 include/cglm/call/mat4x3.h create mode 100644 include/cglm/mat4x3.h create mode 100644 include/cglm/struct/mat4x3.h create mode 100644 src/mat4x3.c create mode 100644 test/src/test_mat4x3.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 028b36c..cf5b832 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,7 @@ add_library(${PROJECT_NAME} src/mat3x4.c src/mat4.c src/mat4x2.c + src/mat4x3.c src/plane.c src/frustum.c src/box.c diff --git a/Makefile.am b/Makefile.am index 5ac1945..1d96ddc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,6 +43,7 @@ cglm_HEADERS = include/cglm/version.h \ include/cglm/io.h \ include/cglm/mat4.h \ include/cglm/mat4x2.h \ + include/cglm/mat4x3.h \ include/cglm/mat3.h \ include/cglm/mat3x2.h \ include/cglm/mat3x4.h \ @@ -100,6 +101,7 @@ cglm_clipspace_HEADERS = include/cglm/clipspace/persp.h \ cglm_calldir=$(includedir)/cglm/call cglm_call_HEADERS = include/cglm/call/mat4.h \ include/cglm/call/mat4x2.h \ + include/cglm/call/mat4x3.h \ include/cglm/call/mat3.h \ include/cglm/call/mat3x2.h \ include/cglm/call/mat3x4.h \ @@ -169,6 +171,7 @@ cglm_simd_neon_HEADERS = include/cglm/simd/neon/affine.h \ cglm_structdir=$(includedir)/cglm/struct cglm_struct_HEADERS = include/cglm/struct/mat4.h \ include/cglm/struct/mat4x2.h \ + include/cglm/struct/mat4x3.h \ include/cglm/struct/mat3.h \ include/cglm/struct/mat3x2.h \ include/cglm/struct/mat3x4.h \ @@ -234,6 +237,7 @@ libcglm_la_SOURCES=\ src/mat3x4.c \ src/mat4.c \ src/mat4x2.c \ + src/mat4x3.c \ src/plane.c \ src/frustum.c \ src/box.c \ diff --git a/docs/source/api_inline_array.rst b/docs/source/api_inline_array.rst index a7bcaf6..797cc80 100644 --- a/docs/source/api_inline_array.rst +++ b/docs/source/api_inline_array.rst @@ -53,6 +53,7 @@ Follow the :doc:`build` documentation for this mat3x4 mat4 mat4x2 + mat4x3 vec2 vec2-ext vec3 diff --git a/docs/source/mat4x3.rst b/docs/source/mat4x3.rst new file mode 100644 index 0000000..4e1cd5c --- /dev/null +++ b/docs/source/mat4x3.rst @@ -0,0 +1,30 @@ +.. default-domain:: C + +mat4x3 +====== + +Header: cglm/mat4x3.h + +Table of contents (click to go): +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Macros: + +1. GLM_MAT4X3_ZERO_INIT +#. GLM_MAT4X3_ZERO + +Functions: + +1. :c:func:`glm_mat4x3_make` + +Functions documentation +~~~~~~~~~~~~~~~~~~~~~~~ + +.. c:function:: void glm_mat4x3_make(float * __restrict src, mat4x3 dest) + + Create mat4x3 matrix from pointer + + | NOTE: **@src** must contain at least 12 elements. + Parameters: + | *[in]* **src** pointer to an array of floats + | *[out]* **dest** destination matrix4x3 diff --git a/include/cglm/call.h b/include/cglm/call.h index 9976852..c411b38 100644 --- a/include/cglm/call.h +++ b/include/cglm/call.h @@ -26,6 +26,7 @@ extern "C" { #include "call/mat3x4.h" #include "call/mat4.h" #include "call/mat4x2.h" +#include "call/mat4x3.h" #include "call/affine.h" #include "call/cam.h" #include "call/quat.h" diff --git a/include/cglm/call/mat4x3.h b/include/cglm/call/mat4x3.h new file mode 100644 index 0000000..a6b1cd3 --- /dev/null +++ b/include/cglm/call/mat4x3.h @@ -0,0 +1,23 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglmc_mat4x3_h +#define cglmc_mat4x3_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" + +CGLM_EXPORT +void +glmc_mat4x3_make(float * __restrict src, mat4x3 dest); + +#ifdef __cplusplus +} +#endif +#endif /* cglmc_mat4x3_h */ diff --git a/include/cglm/cglm.h b/include/cglm/cglm.h index 457d032..17db214 100644 --- a/include/cglm/cglm.h +++ b/include/cglm/cglm.h @@ -17,6 +17,7 @@ #include "ivec4.h" #include "mat4.h" #include "mat4x2.h" +#include "mat4x3.h" #include "mat3.h" #include "mat3x2.h" #include "mat3x4.h" diff --git a/include/cglm/mat4x3.h b/include/cglm/mat4x3.h new file mode 100644 index 0000000..bc69430 --- /dev/null +++ b/include/cglm/mat4x3.h @@ -0,0 +1,54 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Macros: + GLM_MAT4X3_ZERO_INIT + GLM_MAT4X3_ZERO + + Functions: + CGLM_INLINE void glm_mat4x3_make(float * restrict src, mat4x3 dest) + */ + +#ifndef cglm_mat4x3_h +#define cglm_mat4x3_h + +#include "common.h" + +#define GLM_MAT4X3_ZERO_INIT {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, \ + {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}} + +/* for C only */ +#define GLM_MAT4X3_ZERO GLM_MAT4X3_ZERO_INIT + +/*! + * @brief Create mat4x3 matrix from pointer + * + * @param[in] src pointer to an array of floats + * @param[out] dest matrix + */ +CGLM_INLINE +void +glm_mat4x3_make(float * __restrict src, mat4x3 dest) { + dest[0][0] = src[0]; + dest[0][1] = src[1]; + dest[0][2] = src[2]; + + dest[1][0] = src[3]; + dest[1][1] = src[4]; + dest[1][2] = src[5]; + + dest[2][0] = src[6]; + dest[2][1] = src[7]; + dest[2][2] = src[8]; + + dest[3][0] = src[9]; + dest[3][1] = src[10]; + dest[3][2] = src[11]; +} + +#endif diff --git a/include/cglm/struct.h b/include/cglm/struct.h index 46abae3..dad65d6 100644 --- a/include/cglm/struct.h +++ b/include/cglm/struct.h @@ -24,6 +24,7 @@ extern "C" { #include "struct/mat3x4.h" #include "struct/mat4.h" #include "struct/mat4x2.h" +#include "struct/mat4x3.h" #include "struct/affine.h" #include "struct/frustum.h" #include "struct/plane.h" diff --git a/include/cglm/struct/mat4x3.h b/include/cglm/struct/mat4x3.h new file mode 100644 index 0000000..97a7c32 --- /dev/null +++ b/include/cglm/struct/mat4x3.h @@ -0,0 +1,46 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Macros: + GLMS_MAT4X3_ZERO_INIT + GLMS_MAT4X3_ZERO + + Functions: + CGLM_INLINE mat4x3s glms_mat4x3_make(float * __restrict src); + */ + +#ifndef cglms_mat4x3_h +#define cglms_mat4x3_h + +#include "../common.h" +#include "../types-struct.h" +#include "../mat4x3.h" + +/* api definition */ +#define glms_mat4x3_(NAME) CGLM_STRUCTAPI(mat4x3, NAME) + +#define GLMS_MAT4X3_ZERO_INIT {GLM_MAT4X3_ZERO_INIT} + +/* for C only */ +#define GLMS_MAT4X3_ZERO ((mat4x3s)GLMS_MAT4X3_ZERO_INIT) + +/*! + * @brief Create mat4x3 matrix from pointer + * + * @param[in] src pointer to an array of floats + * @return constructed matrix from raw pointer + */ +CGLM_INLINE +mat4x3s +glms_mat4x3_(make)(float * __restrict src) { + mat4x3s r; + glm_mat4x3_make(src, r.raw); + return r; +} + +#endif /* cglms_mat4x3_h */ diff --git a/include/cglm/types-struct.h b/include/cglm/types-struct.h index 8aa7982..11c83a3 100644 --- a/include/cglm/types-struct.h +++ b/include/cglm/types-struct.h @@ -281,4 +281,17 @@ typedef union mat4x2s { #endif } mat4x2s; +typedef union mat4x3s { + mat4x3 raw; + vec3s col[4]; /* [col (4), row (3)] */ +#if CGLM_USE_ANONYMOUS_STRUCT + struct { + float m00, m01, m02; + float m10, m11, m12; + float m20, m21, m22; + float m30, m31, m32; + }; +#endif +} mat4x3s; + #endif /* cglm_types_struct_h */ diff --git a/include/cglm/types.h b/include/cglm/types.h index 8712546..67d41a1 100644 --- a/include/cglm/types.h +++ b/include/cglm/types.h @@ -62,6 +62,7 @@ typedef vec3 mat2x3[2]; /* [col (2), row (3)] */ typedef vec4 mat2x4[2]; /* [col (2), row (4)] */ typedef CGLM_ALIGN_MAT vec4 mat4[4]; typedef vec2 mat4x2[4]; /* [col (4), row (2)] */ +typedef vec3 mat4x3[4]; /* [col (4), row (3)] */ /* Important: cglm stores quaternion as [x, y, z, w] in memory since v0.4.0 diff --git a/meson.build b/meson.build index 5f3ab94..95edd70 100644 --- a/meson.build +++ b/meson.build @@ -46,6 +46,7 @@ cglm_src = files( 'src/mat3x4.c', 'src/mat4.c', 'src/mat4x2.c', + 'src/mat4x3.c', 'src/plane.c', 'src/frustum.c', 'src/box.c', diff --git a/src/mat4x3.c b/src/mat4x3.c new file mode 100644 index 0000000..6617b47 --- /dev/null +++ b/src/mat4x3.c @@ -0,0 +1,15 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "../include/cglm/cglm.h" +#include "../include/cglm/call.h" + +CGLM_EXPORT +void +glmc_mat4x3_make(float * __restrict src, mat4x3 dest) { + glm_mat4x3_make(src, dest); +} diff --git a/test/src/test_common.c b/test/src/test_common.c index 46337e4..d11ff6f 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -325,6 +325,19 @@ test_assert_mat4x2_eq_zero(mat4x2 m4x2) { TEST_SUCCESS } +test_status_t +test_assert_mat4x3_eq_zero(mat4x3 m4x3) { + int i, j; + + for (i = 0; i < 4; i++) { + for (j = 0; j < 3; j++) { + ASSERT(test_eq(m4x3[i][j], 0.0f)) + } + } + + TEST_SUCCESS +} + test_status_t test_assert_eqf(float a, float b) { ASSERT(fabsf(a - b) <= 0.000009); /* rounding errors */ diff --git a/test/src/test_common.h b/test/src/test_common.h index d7c6bc3..0338d15 100644 --- a/test/src/test_common.h +++ b/test/src/test_common.h @@ -44,6 +44,9 @@ test_assert_mat4_eq_zero(mat4 m4); test_status_t test_assert_mat4x2_eq_zero(mat4x2 m4x2); +test_status_t +test_assert_mat4x3_eq_zero(mat4x3 m4x3); + test_status_t test_assert_mat2_eqt(mat2 m1, mat2 m2); diff --git a/test/src/test_mat4x3.h b/test/src/test_mat4x3.h new file mode 100644 index 0000000..27b8795 --- /dev/null +++ b/test/src/test_mat4x3.h @@ -0,0 +1,60 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "test_common.h" + +#ifndef CGLM_TEST_MAT4X3_ONCE +#define CGLM_TEST_MAT4X3_ONCE + +TEST_IMPL(MACRO_GLM_MAT4X3_ZERO_INIT) { + mat4x3 mat4x3_zero = GLM_MAT4X3_ZERO_INIT; + test_assert_mat4x3_eq_zero(mat4x3_zero); + TEST_SUCCESS +} + +TEST_IMPL(MACRO_GLM_MAT4X3_ZERO) { + mat4x3 mat4x3_zero = GLM_MAT4X3_ZERO; + test_assert_mat4x3_eq_zero(mat4x3_zero); + TEST_SUCCESS +} + +#endif /* CGLM_TEST_MAT4X3_ONCE */ + +TEST_IMPL(GLM_PREFIX, mat4x3_make) { + float src[36] = { + 0.5f, 1.7f, 10.3f, 4.2f, 8.9f, 1.1f, 2.3f, 4.2f, 5.3f, 4.8f, 96.3f, 13.7f, + 4.7f, 5.5f, 2.3f, 4.2f, 2.3f, 4.2f, 66.5f, 23.7f, 6.6f, 8.9f, 0.5f, 1.7f, + 5.3f, 4.8f, 96.3f, 13.7f, 4.7f, 5.5f, 2.3f, 4.2f, 0.5f, 1.7f, 10.3f, 4.2f + }; + + mat4x3 dest[3]; + + float *srcp = src; + unsigned int i, j, k; + + for (i = 0, j = 0, k = 0; i < sizeof(src) / sizeof(float); i+=12,j++) { + GLM(mat4x3_make)(srcp + i, dest[j]); + + ASSERT(test_eq(src[ i ], dest[j][k][0])); + ASSERT(test_eq(src[i+1], dest[j][k][1])); + ASSERT(test_eq(src[i+2], dest[j][k][2])); + + ASSERT(test_eq(src[i+3], dest[j][k+1][0])); + ASSERT(test_eq(src[i+4], dest[j][k+1][1])); + ASSERT(test_eq(src[i+5], dest[j][k+1][2])); + + ASSERT(test_eq(src[i+6], dest[j][k+2][0])); + ASSERT(test_eq(src[i+7], dest[j][k+2][1])); + ASSERT(test_eq(src[i+8], dest[j][k+2][2])); + + ASSERT(test_eq(src[i+9], dest[j][k+3][0])); + ASSERT(test_eq(src[i+10], dest[j][k+3][1])); + ASSERT(test_eq(src[i+11], dest[j][k+3][2])); + } + + TEST_SUCCESS +} diff --git a/test/src/test_struct.c b/test/src/test_struct.c index 5c10780..d040225 100644 --- a/test/src/test_struct.c +++ b/test/src/test_struct.c @@ -95,6 +95,18 @@ TEST_IMPL(mat4x2s_zero) { TEST_SUCCESS } +TEST_IMPL(mat4x3s_zero_init) { + mat4x3s mat4x3_zero = GLMS_MAT4X3_ZERO_INIT; + test_assert_mat4x3_eq_zero(mat4x3_zero.raw); + TEST_SUCCESS +} + +TEST_IMPL(mat4x3s_zero) { + mat4x3s mat4x3_zero = GLMS_MAT4X3_ZERO; + test_assert_mat4x3_eq_zero(mat4x3_zero.raw); + TEST_SUCCESS +} + TEST_IMPL(quats_zero_init) { versors quat_zero = GLMS_QUAT_IDENTITY_INIT; versor quat_zero_a = GLM_QUAT_IDENTITY_INIT; diff --git a/test/src/tests.c b/test/src/tests.c index b874dd5..4d1e2c9 100644 --- a/test/src/tests.c +++ b/test/src/tests.c @@ -26,6 +26,7 @@ #include "test_mat3x4.h" #include "test_mat4.h" #include "test_mat4x2.h" +#include "test_mat4x3.h" #include "test_quat.h" #include "test_project.h" #include "test_plane.h" @@ -62,6 +63,7 @@ #include "test_mat3x4.h" #include "test_mat4.h" #include "test_mat4x2.h" +#include "test_mat4x3.h" #include "test_quat.h" #include "test_project.h" #include "test_plane.h" diff --git a/test/tests.h b/test/tests.h index 7edf436..aab760f 100644 --- a/test/tests.h +++ b/test/tests.h @@ -159,6 +159,12 @@ TEST_DECLARE(glm_mat4x2_make) TEST_DECLARE(glmc_mat4x2_make) +TEST_DECLARE(MACRO_GLM_MAT4X3_ZERO_INIT) +TEST_DECLARE(MACRO_GLM_MAT4X3_ZERO) +TEST_DECLARE(glm_mat4x3_make) + +TEST_DECLARE(glmc_mat4x3_make) + /* mat3 */ TEST_DECLARE(glm_mat3_copy) TEST_DECLARE(glm_mat3_identity) @@ -901,6 +907,8 @@ TEST_DECLARE(mat4s_identity_init) TEST_DECLARE(mat4s_zero_init) TEST_DECLARE(mat4x2s_zero_init) TEST_DECLARE(mat4x2s_zero) +TEST_DECLARE(mat4x3s_zero_init) +TEST_DECLARE(mat4x3s_zero) TEST_DECLARE(quats_zero_init) TEST_DECLARE(vec3s_one_init) TEST_DECLARE(vec3s_zero_init) @@ -1054,6 +1062,12 @@ TEST_LIST { TEST_ENTRY(glmc_mat4x2_make) + TEST_ENTRY(MACRO_GLM_MAT4X3_ZERO_INIT) + TEST_ENTRY(MACRO_GLM_MAT4X3_ZERO) + TEST_ENTRY(glm_mat4x3_make) + + TEST_ENTRY(glmc_mat4x3_make) + /* mat3 */ TEST_ENTRY(glm_mat3_copy) TEST_ENTRY(glm_mat3_identity) @@ -1793,6 +1807,8 @@ TEST_LIST { TEST_ENTRY(mat4s_zero_init) TEST_ENTRY(mat4x2s_zero_init) TEST_ENTRY(mat4x2s_zero) + TEST_ENTRY(mat4x3s_zero_init) + TEST_ENTRY(mat4x3s_zero) TEST_ENTRY(quats_zero_init) TEST_ENTRY(vec3s_one_init) TEST_ENTRY(vec3s_zero_init) diff --git a/win/cglm-test.vcxproj b/win/cglm-test.vcxproj index 61df3f4..3e7f8c8 100644 --- a/win/cglm-test.vcxproj +++ b/win/cglm-test.vcxproj @@ -73,6 +73,7 @@ + diff --git a/win/cglm-test.vcxproj.filters b/win/cglm-test.vcxproj.filters index 7cc7b90..7a787a4 100644 --- a/win/cglm-test.vcxproj.filters +++ b/win/cglm-test.vcxproj.filters @@ -100,6 +100,9 @@ src + + src + src diff --git a/win/cglm.vcxproj b/win/cglm.vcxproj index 736ceb0..8b71cb4 100644 --- a/win/cglm.vcxproj +++ b/win/cglm.vcxproj @@ -78,6 +78,7 @@ + @@ -132,6 +133,7 @@ + @@ -177,6 +179,7 @@ + @@ -231,6 +234,7 @@ + diff --git a/win/cglm.vcxproj.filters b/win/cglm.vcxproj.filters index 6f087da..7bb6316 100644 --- a/win/cglm.vcxproj.filters +++ b/win/cglm.vcxproj.filters @@ -70,6 +70,9 @@ src + + src + src @@ -204,6 +207,9 @@ include\cglm\call + + include\cglm\call + include\cglm\call @@ -279,6 +285,9 @@ include\cglm + + include\cglm + include\cglm @@ -405,6 +414,9 @@ include\cglm\struct + + include\cglm\struct + include\cglm\struct