From e96b6a382d0d19b82f74cc9824979d63ac26b0d7 Mon Sep 17 00:00:00 2001 From: Artemii Miasoedov Date: Sun, 16 Feb 2025 15:50:53 +0300 Subject: [PATCH] Added tests for 2D affine post transformations (translate, rotate, scale) --- include/cglm/cglm.h | 1 + test/src/test_affine2d_post.h | 104 ++++++++++++++++++++++++++++++++++ test/src/test_common.c | 20 +++++-- test/src/test_common.h | 3 + test/src/tests.c | 1 + test/tests.h | 16 ++++++ 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 test/src/test_affine2d_post.h diff --git a/include/cglm/cglm.h b/include/cglm/cglm.h index 146ded0..3532830 100644 --- a/include/cglm/cglm.h +++ b/include/cglm/cglm.h @@ -43,5 +43,6 @@ #include "bezier.h" #include "ray.h" #include "affine2d.h" +#include "affine2d-post.h" #endif /* cglm_h */ diff --git a/test/src/test_affine2d_post.h b/test/src/test_affine2d_post.h new file mode 100644 index 0000000..881270b --- /dev/null +++ b/test/src/test_affine2d_post.h @@ -0,0 +1,104 @@ +/* + * 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" + +TEST_IMPL(GLM_PREFIX, translated2d) { + mat3 m1, m2, tmp; + vec2 v = { 1.2f, 3.4f }; + + test_rand_transform2d(m1); + glm_mat3_copy(m1, m2); + GLM(translated2d)(m2, v); + + glm_translate2d_make(tmp, v); + glm_mat3_mul(tmp, m1, m1); + + test_assert_mat3_eq(m1, m2); + + TEST_SUCCESS +} + +TEST_IMPL(GLM_PREFIX, translated2d_x) { + mat3 m1, m2, tmp; + float x = test_rand(); + + test_rand_transform2d(m1); + glm_mat3_copy(m1, m2); + GLM(translated2d_x)(m2, x); + + glm_translate2d_make(tmp, (vec2) { x, 0.0f }); + glm_mat3_mul(tmp, m1, m1); + + test_assert_mat3_eq(m1, m2); + + TEST_SUCCESS +} + +TEST_IMPL(GLM_PREFIX, translated2d_y) { + mat3 m1, m2, tmp; + float y = test_rand(); + + test_rand_transform2d(m1); + glm_mat3_copy(m1, m2); + GLM(translated2d_y)(m2, y); + + glm_translate2d_make(tmp, (vec2) { 0.0f, y }); + glm_mat3_mul(tmp, m1, m1); + + test_assert_mat3_eq(m1, m2); + + TEST_SUCCESS +} + +TEST_IMPL(GLM_PREFIX, rotated2d) { + mat3 m1, m2, tmp; + float a = test_rand(); + + test_rand_transform2d(m1); + glm_mat3_copy(m1, m2); + GLM(rotated2d)(m2, a); + + glm_rotate2d_make(tmp, a); + glm_mat3_mul(tmp, m1, m1); + + test_assert_mat3_eq(m1, m2); + + TEST_SUCCESS +} + +TEST_IMPL(GLM_PREFIX, scaled2d) { + mat3 m1, m2, tmp; + vec2 v = { test_rand(), test_rand() }; + + test_rand_transform2d(m1); + glm_mat3_copy(m1, m2); + GLM(scaled2d)(m2, v); + + glm_scale2d_make(tmp, v); + glm_mat3_mul(tmp, m1, m1); + + test_assert_mat3_eq(m1, m2); + + TEST_SUCCESS +} + +TEST_IMPL(GLM_PREFIX, scaled2d_uni) { + mat3 m1, m2, tmp; + float s = test_rand(); + + test_rand_transform2d(m1); + glm_mat3_copy(m1, m2); + GLM(scaled2d_uni)(m2, s); + + glm_scale2d_make(tmp, (vec2) { s, s }); + glm_mat3_mul(tmp, m1, m1); + + test_assert_mat3_eq(m1, m2); + + TEST_SUCCESS +} diff --git a/test/src/test_common.c b/test/src/test_common.c index a6c31c6..f79559c 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -16,7 +16,7 @@ test_rand_mat4(mat4 dest) { dest[3][2] = drand48(); /* random rotatation around random axis with random angle */ - glm_rotate(dest, drand48(), (vec3){drand48(), drand48(), drand48()}); + glm_rotate(dest, drand48(), (vec3) { drand48(), drand48(), drand48() }); /* random scale */ /* glm_scale(dest, (vec3){drand48(), drand48(), drand48()}); */ @@ -61,7 +61,7 @@ test_rand_mat3(mat3 dest) { mat4 m4; /* random rotatation around random axis with random angle */ - glm_rotate_make(m4, drand48(), (vec3){drand48(), drand48(), drand48()}); + glm_rotate_make(m4, drand48(), (vec3) { drand48(), drand48(), drand48() }); glm_mat4_pick3(m4, dest); } @@ -123,6 +123,13 @@ test_rand_mat2x4(mat2x4 dest) { dest[1][3] = drand48(); } +void +test_rand_transform2d(mat3 dest) { + glm_translate2d_make(dest, (vec2) { drand48(), drand48() }); + glm_rotate2d(dest, drand48()); + glm_scale2d(dest, (vec2) { drand48(), drand48() }); +} + void test_rand_vec3(vec3 dest) { dest[0] = drand48(); @@ -236,7 +243,8 @@ test_assert_mat2_eq_identity(mat2 m2) { for (j = 0; j < 2; j++) { if (i == j) { ASSERT(test_eq(m2[i][j], 1.0f)) - } else { + } + else { ASSERT(test_eq(m2[i][j], 0.0f)) } } @@ -344,7 +352,8 @@ test_assert_mat3_eq_identity(mat3 m3) { for (j = 0; j < 3; j++) { if (i == j) { ASSERT(test_eq(m3[i][j], 1.0f)) - } else { + } + else { ASSERT(test_eq(m3[i][j], 0.0f)) } } @@ -426,7 +435,8 @@ test_assert_mat4_eq_identity(mat4 m4) { for (j = 0; j < 4; j++) { if (i == j) { ASSERT(test_eq(m4[i][j], 1.0f)) - } else { + } + else { ASSERT(test_eq(m4[i][j], 0.0f)) } } diff --git a/test/src/test_common.h b/test/src/test_common.h index 4fff040..7471dbb 100644 --- a/test/src/test_common.h +++ b/test/src/test_common.h @@ -41,6 +41,9 @@ test_rand_mat2x3(mat2x3 dest); void test_rand_mat2x4(mat2x4 dest); +void +test_rand_transform2d(mat3 dest); + test_status_t test_assert_eqf(float a, float b); diff --git a/test/src/tests.c b/test/src/tests.c index bc24668..297d309 100644 --- a/test/src/tests.c +++ b/test/src/tests.c @@ -33,6 +33,7 @@ #include "test_noise.h" #include "test_affine.h" #include "test_affine2d.h" +#include "test_affine2d_post.h" #include "test_affine_mat.h" #include "test_aabb2d.h" #include "test_ray.h" diff --git a/test/tests.h b/test/tests.h index b959ad6..effe137 100644 --- a/test/tests.h +++ b/test/tests.h @@ -97,6 +97,14 @@ TEST_DECLARE(glmc_rotate2d_make) TEST_DECLARE(glmc_rotate2d) TEST_DECLARE(glmc_rotate2d_to) +/* affine 2d post */ +TEST_DECLARE(glm_translated2d) +TEST_DECLARE(glm_translated2d_x) +TEST_DECLARE(glm_translated2d_y) +TEST_DECLARE(glm_rotated2d) +TEST_DECLARE(glm_scaled2d) +TEST_DECLARE(glm_scaled2d_uni) + /* aabb2d */ TEST_DECLARE(glm_aabb2d_sizev) @@ -1308,6 +1316,14 @@ TEST_LIST { TEST_ENTRY(glmc_rotate2d) TEST_ENTRY(glmc_rotate2d_to) + /* affine 2d post */ + TEST_ENTRY(glm_translated2d) + TEST_ENTRY(glm_translated2d_x) + TEST_ENTRY(glm_translated2d_y) + TEST_ENTRY(glm_rotated2d) + TEST_ENTRY(glm_scaled2d) + TEST_ENTRY(glm_scaled2d_uni) + /* aabb2d */ TEST_ENTRY(glm_aabb2d_sizev)