From fe7471e8f8a60c728be9e2d2d8a76eec7fe8be63 Mon Sep 17 00:00:00 2001 From: Vincent Davis Jr Date: Sat, 15 Jul 2023 13:51:19 -0400 Subject: [PATCH] add new matrix mat2x4 Initial function being glm_mat2x4_make Signed-off-by: Vincent Davis Jr --- CMakeLists.txt | 1 + Makefile.am | 4 +++ docs/source/api_inline_array.rst | 1 + docs/source/mat2x4.rst | 31 ++++++++++++++++++ include/cglm/call.h | 1 + include/cglm/call/mat2x4.h | 23 +++++++++++++ include/cglm/cglm.h | 1 + include/cglm/mat2x4.h | 47 +++++++++++++++++++++++++++ include/cglm/struct.h | 1 + include/cglm/struct/mat2x4.h | 46 ++++++++++++++++++++++++++ include/cglm/types-struct.h | 11 +++++++ include/cglm/types.h | 1 + meson.build | 1 + src/mat2x4.c | 15 +++++++++ test/src/test_common.c | 13 ++++++++ test/src/test_common.h | 3 ++ test/src/test_mat2x4.h | 55 ++++++++++++++++++++++++++++++++ test/src/test_struct.c | 6 ++++ test/src/tests.c | 2 ++ test/tests.h | 12 +++++++ win/cglm-test.vcxproj | 1 + win/cglm-test.vcxproj.filters | 3 ++ win/cglm.vcxproj | 4 +++ win/cglm.vcxproj.filters | 12 +++++++ 24 files changed, 295 insertions(+) create mode 100644 docs/source/mat2x4.rst create mode 100644 include/cglm/call/mat2x4.h create mode 100644 include/cglm/mat2x4.h create mode 100644 include/cglm/struct/mat2x4.h create mode 100644 src/mat2x4.c create mode 100644 test/src/test_mat2x4.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bb7bcba..3bafd43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,6 +80,7 @@ add_library(${PROJECT_NAME} src/ivec4.c src/mat2.c src/mat2x3.c + src/mat2x4.c src/mat3.c src/mat4.c src/plane.c diff --git a/Makefile.am b/Makefile.am index dd14edc..461d4fc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,6 +45,7 @@ cglm_HEADERS = include/cglm/version.h \ include/cglm/mat3.h \ include/cglm/mat2.h \ include/cglm/mat2x3.h \ + include/cglm/mat2x4.h \ include/cglm/affine-pre.h \ include/cglm/affine-post.h \ include/cglm/affine.h \ @@ -98,6 +99,7 @@ cglm_call_HEADERS = include/cglm/call/mat4.h \ include/cglm/call/mat3.h \ include/cglm/call/mat2.h \ include/cglm/call/mat2x3.h \ + include/cglm/call/mat2x4.h \ include/cglm/call/vec2.h \ include/cglm/call/vec3.h \ include/cglm/call/vec4.h \ @@ -163,6 +165,7 @@ cglm_struct_HEADERS = include/cglm/struct/mat4.h \ include/cglm/struct/mat3.h \ include/cglm/struct/mat2.h \ include/cglm/struct/mat2x3.h \ + include/cglm/struct/mat2x4.h \ include/cglm/struct/affine-pre.h \ include/cglm/struct/affine-post.h \ include/cglm/struct/affine-mat.h \ @@ -216,6 +219,7 @@ libcglm_la_SOURCES=\ src/ivec4.c \ src/mat2.c \ src/mat2x3.c \ + src/mat2x4.c \ src/mat3.c \ src/mat4.c \ src/plane.c \ diff --git a/docs/source/api_inline_array.rst b/docs/source/api_inline_array.rst index ddb2ec1..3a92840 100644 --- a/docs/source/api_inline_array.rst +++ b/docs/source/api_inline_array.rst @@ -47,6 +47,7 @@ Follow the :doc:`build` documentation for this euler mat2 mat2x3 + mat2x4 mat3 mat4 vec2 diff --git a/docs/source/mat2x4.rst b/docs/source/mat2x4.rst new file mode 100644 index 0000000..1c30d0e --- /dev/null +++ b/docs/source/mat2x4.rst @@ -0,0 +1,31 @@ +.. default-domain:: C + +mat2x4 +====== + +Header: cglm/mat2x4.h + +Table of contents (click to go): +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Macros: + +1. GLM_MAT2X4_ZERO_INIT +#. GLM_MAT2x4_ZERO + +Functions: + +1. :c:func:`glm_mat2x4_make` + +Functions documentation +~~~~~~~~~~~~~~~~~~~~~~~ + +.. c:function:: void glm_mat2x4_make(float * __restrict src, mat2x4 dest) + + Create mat2x4 matrix from pointer + + | NOTE: **@src** must contain at least 8 elements. + + Parameters: + | *[in]* **src** pointer to an array of floats + | *[out]* **dest** destination matrix2x4 diff --git a/include/cglm/call.h b/include/cglm/call.h index e932ac5..609de88 100644 --- a/include/cglm/call.h +++ b/include/cglm/call.h @@ -20,6 +20,7 @@ extern "C" { #include "call/ivec4.h" #include "call/mat2.h" #include "call/mat2x3.h" +#include "call/mat2x4.h" #include "call/mat3.h" #include "call/mat4.h" #include "call/affine.h" diff --git a/include/cglm/call/mat2x4.h b/include/cglm/call/mat2x4.h new file mode 100644 index 0000000..63c5cbe --- /dev/null +++ b/include/cglm/call/mat2x4.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_mat2x4_h +#define cglmc_mat2x4_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" + +CGLM_EXPORT +void +glmc_mat2x4_make(float * __restrict src, mat2x4 dest); + +#ifdef __cplusplus +} +#endif +#endif /* cglmc_mat2x4_h */ diff --git a/include/cglm/cglm.h b/include/cglm/cglm.h index eb208d7..7b72af0 100644 --- a/include/cglm/cglm.h +++ b/include/cglm/cglm.h @@ -19,6 +19,7 @@ #include "mat3.h" #include "mat2.h" #include "mat2x3.h" +#include "mat2x4.h" #include "affine.h" #include "cam.h" #include "frustum.h" diff --git a/include/cglm/mat2x4.h b/include/cglm/mat2x4.h new file mode 100644 index 0000000..7a05dad --- /dev/null +++ b/include/cglm/mat2x4.h @@ -0,0 +1,47 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Macros: + GLM_MAT2X4_ZERO_INIT + GLM_MAT2X4_ZERO + + Functions: + CGLM_INLINE void glm_mat2x4_make(float * restrict src, mat2x4 dest) + */ + +#ifndef cglm_mat2x4_h +#define cglm_mat2x4_h + +#include "common.h" + +#define GLM_MAT2X4_ZERO_INIT {{0.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f}} + +/* for C only */ +#define GLM_MAT2X4_ZERO GLM_MAT2X4_ZERO_INIT + +/*! + * @brief Create mat2x4 matrix from pointer + * + * @param[in] src pointer to an array of floats + * @param[out] dest matrix + */ +CGLM_INLINE +void +glm_mat2x4_make(float * __restrict src, mat2x4 dest) { + dest[0][0] = src[0]; + dest[0][1] = src[1]; + dest[0][2] = src[2]; + dest[0][3] = src[3]; + + dest[1][0] = src[4]; + dest[1][1] = src[5]; + dest[1][2] = src[6]; + dest[1][3] = src[7]; +} + +#endif diff --git a/include/cglm/struct.h b/include/cglm/struct.h index 0d88518..8c85005 100644 --- a/include/cglm/struct.h +++ b/include/cglm/struct.h @@ -18,6 +18,7 @@ extern "C" { #include "struct/vec4.h" #include "struct/mat2.h" #include "struct/mat2x3.h" +#include "struct/mat2x4.h" #include "struct/mat3.h" #include "struct/mat4.h" #include "struct/affine.h" diff --git a/include/cglm/struct/mat2x4.h b/include/cglm/struct/mat2x4.h new file mode 100644 index 0000000..91cbe03 --- /dev/null +++ b/include/cglm/struct/mat2x4.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_MAT2X4_ZERO_INIT + GLMS_MAT2X4_ZERO + + Functions: + CGLM_INLINE float glms_mat2x4_make(float * __restrict src); + */ + +#ifndef cglms_mat2x4_h +#define cglms_mat2x4_h + +#include "../common.h" +#include "../types-struct.h" +#include "../mat2x4.h" + +/* api definition */ +#define glms_mat2x4_(NAME) CGLM_STRUCTAPI(mat2x4, NAME) + +#define GLMS_MAT2X4_ZERO_INIT {GLM_MAT2X4_ZERO_INIT} + +/* for C only */ +#define GLMS_MAT2X4_ZERO ((mat2x4s)GLMS_MAT2X4_ZERO_INIT) + +/*! + * @brief Create mat2x4 matrix from pointer + * + * @param[in] src pointer to an array of floats + * @return constructed matrix from raw pointer + */ +CGLM_INLINE +mat2x4s +glms_mat2x4_(make)(float * __restrict src) { + mat2x4s r; + glm_mat2x4_make(src, r.raw); + return r; +} + +#endif /* cglms_mat2x4_h */ diff --git a/include/cglm/types-struct.h b/include/cglm/types-struct.h index 6ab34d2..6a7f505 100644 --- a/include/cglm/types-struct.h +++ b/include/cglm/types-struct.h @@ -208,6 +208,17 @@ typedef union mat2x3s { #endif } mat2x3s; +typedef union mat2x4s { + mat2x4 raw; + vec4s col[2]; /* col -> row | [row (2), col (4)] */ +#if CGLM_USE_ANONYMOUS_STRUCT + struct { + float m00, m01, m02, m03; + float m10, m11, m12, m13; + }; +#endif +} mat2x4s; + typedef union mat3s { mat3 raw; vec3s col[3]; diff --git a/include/cglm/types.h b/include/cglm/types.h index 3d6cb00..6cf3a1b 100644 --- a/include/cglm/types.h +++ b/include/cglm/types.h @@ -57,6 +57,7 @@ typedef vec4 versor; /* |x, y, z, w| -> w is the last */ typedef vec3 mat3[3]; typedef CGLM_ALIGN_IF(16) vec2 mat2[2]; typedef vec3 mat2x3[2]; /* [row (2), col (3)] */ +typedef vec4 mat2x4[2]; /* [row (2), col (4)] */ typedef CGLM_ALIGN_MAT vec4 mat4[4]; /* diff --git a/meson.build b/meson.build index b488aa7..3787046 100644 --- a/meson.build +++ b/meson.build @@ -40,6 +40,7 @@ cglm_src = files( 'src/ivec4.c', 'src/mat2.c', 'src/mat2x3.c', + 'src/mat2x4.c', 'src/mat3.c', 'src/mat4.c', 'src/plane.c', diff --git a/src/mat2x4.c b/src/mat2x4.c new file mode 100644 index 0000000..5fb2d53 --- /dev/null +++ b/src/mat2x4.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_mat2x4_make(float * __restrict src, mat2x4 dest) { + glm_mat2x4_make(src, dest); +} diff --git a/test/src/test_common.c b/test/src/test_common.c index 2289b24..81d2402 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -187,6 +187,19 @@ test_assert_mat2x3_eq_zero(mat2x3 m2x3) { TEST_SUCCESS } +test_status_t +test_assert_mat2x4_eq_zero(mat2x4 m2x4) { + int i, j; + + for (i = 0; i < 2; i++) { + for (j = 0; j < 4; j++) { + ASSERT(test_eq(m2x4[i][j], 0.0f)) + } + } + + TEST_SUCCESS +} + test_status_t test_assert_mat3_eq(mat3 m1, mat3 m2) { int i, j; diff --git a/test/src/test_common.h b/test/src/test_common.h index 4c6dc77..1f332c3 100644 --- a/test/src/test_common.h +++ b/test/src/test_common.h @@ -56,6 +56,9 @@ test_assert_mat2_eq_zero(mat2 m2); test_status_t test_assert_mat2x3_eq_zero(mat2x3 m2x3); +test_status_t +test_assert_mat2x4_eq_zero(mat2x4 m2x4); + test_status_t test_assert_mat3_eq(mat3 m1, mat3 m2); diff --git a/test/src/test_mat2x4.h b/test/src/test_mat2x4.h new file mode 100644 index 0000000..7348965 --- /dev/null +++ b/test/src/test_mat2x4.h @@ -0,0 +1,55 @@ + +/* + * 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_MAT2X4_ONCE +#define CGLM_TEST_MAT2X4_ONCE + +TEST_IMPL(MACRO_GLM_MAT2X4_ZERO_INIT) { + mat2x4 mat2x4_zero = GLM_MAT2X4_ZERO_INIT; + test_assert_mat2x4_eq_zero(mat2x4_zero); + TEST_SUCCESS +} + +TEST_IMPL(MACRO_GLM_MAT2X4_ZERO) { + mat2x4 mat2x4_zero = GLM_MAT2X4_ZERO; + test_assert_mat2x4_eq_zero(mat2x4_zero); + TEST_SUCCESS +} + +#endif /* CGLM_TEST_MAT2X4_ONCE */ + +TEST_IMPL(GLM_PREFIX, mat2x4_make) { + float src[24] = { + 0.5f, 1.7f, 10.3f, 4.2f, 8.9f, 1.1f, 77.3f, 88.4f, + 2.3f, 4.2f, 66.5f, 23.7f, 6.6f, 8.9f, 90.3f, 34.2f, + 5.3f, 4.8f, 96.3f, 13.7f, 4.7f, 5.5f, 22.9f, 5.5f + }; + + mat2x4 dest[3]; + + float *srcp = src; + unsigned int i, j, k; + + for (i = 0, j = 0, k = 0; i < sizeof(src) / sizeof(float); i+=8,j++) { + GLM(mat2x4_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][3])); + + ASSERT(test_eq(src[i+4], dest[j][k+1][0])); + ASSERT(test_eq(src[i+5], dest[j][k+1][1])); + ASSERT(test_eq(src[i+6], dest[j][k+1][2])); + ASSERT(test_eq(src[i+7], dest[j][k+1][3])); + } + + TEST_SUCCESS +} diff --git a/test/src/test_struct.c b/test/src/test_struct.c index 16ac389..83ac0f6 100644 --- a/test/src/test_struct.c +++ b/test/src/test_struct.c @@ -15,6 +15,12 @@ TEST_IMPL(mat2x3s_zero_init) { TEST_SUCCESS } +TEST_IMPL(mat2x4s_zero_init) { + mat2x4s mat2x4_zero = GLMS_MAT2X4_ZERO_INIT; + test_assert_mat2x4_eq_zero(mat2x4_zero.raw); + TEST_SUCCESS +} + TEST_IMPL(mat3s_identity_init) { mat3s mat3_identity = GLMS_MAT3_IDENTITY_INIT; mat3 mat3_identity_a = GLM_MAT3_IDENTITY_INIT; diff --git a/test/src/tests.c b/test/src/tests.c index bec1b62..1a80b84 100644 --- a/test/src/tests.c +++ b/test/src/tests.c @@ -20,6 +20,7 @@ #include "test_ivec4.h" #include "test_mat2.h" #include "test_mat2x3.h" +#include "test_mat2x4.h" #include "test_mat3.h" #include "test_mat4.h" #include "test_quat.h" @@ -52,6 +53,7 @@ #include "test_ivec4.h" #include "test_mat2.h" #include "test_mat2x3.h" +#include "test_mat2x4.h" #include "test_mat3.h" #include "test_mat4.h" #include "test_quat.h" diff --git a/test/tests.h b/test/tests.h index acdfb35..9e02be1 100644 --- a/test/tests.h +++ b/test/tests.h @@ -232,6 +232,12 @@ TEST_DECLARE(glm_mat2x3_make) TEST_DECLARE(glmc_mat2x3_make) +TEST_DECLARE(MACRO_GLM_MAT2X4_ZERO_INIT) +TEST_DECLARE(MACRO_GLM_MAT2X4_ZERO) +TEST_DECLARE(glm_mat2x4_make) + +TEST_DECLARE(glmc_mat2x4_make) + /* camera (incl [LR]H cross [NZ]O) */ TEST_DECLARE(glm_perspective_lh_zo) TEST_DECLARE(glm_perspective_rh_zo) @@ -1091,6 +1097,12 @@ TEST_LIST { TEST_ENTRY(glmc_mat2x3_make) + TEST_ENTRY(MACRO_GLM_MAT2X4_ZERO_INIT) + TEST_ENTRY(MACRO_GLM_MAT2X4_ZERO) + TEST_ENTRY(glm_mat2x4_make) + + TEST_ENTRY(glmc_mat2x4_make) + /* camera (incl [LR]H cross [NZ]O) */ TEST_ENTRY(glm_perspective_lh_zo) TEST_ENTRY(glm_perspective_rh_zo) diff --git a/win/cglm-test.vcxproj b/win/cglm-test.vcxproj index c32bac6..6edf969 100644 --- a/win/cglm-test.vcxproj +++ b/win/cglm-test.vcxproj @@ -67,6 +67,7 @@ + diff --git a/win/cglm-test.vcxproj.filters b/win/cglm-test.vcxproj.filters index 0180b18..08889ec 100644 --- a/win/cglm-test.vcxproj.filters +++ b/win/cglm-test.vcxproj.filters @@ -82,6 +82,9 @@ src + + src + src diff --git a/win/cglm.vcxproj b/win/cglm.vcxproj index 2ab9522..95603d6 100644 --- a/win/cglm.vcxproj +++ b/win/cglm.vcxproj @@ -72,6 +72,7 @@ + @@ -122,6 +123,7 @@ + @@ -163,6 +165,7 @@ + @@ -213,6 +216,7 @@ + diff --git a/win/cglm.vcxproj.filters b/win/cglm.vcxproj.filters index 1668718..a22c365 100644 --- a/win/cglm.vcxproj.filters +++ b/win/cglm.vcxproj.filters @@ -100,6 +100,9 @@ src + + src + src @@ -399,18 +402,27 @@ include\cglm\call + + include\cglm\call + include\cglm\call include\cglm\struct + + include\cglm\struct + include\cglm include\cglm + + include\cglm + include\cglm