Added tests for 2D affine post transformations (translate, rotate, scale)

This commit is contained in:
Artemii Miasoedov
2025-02-16 15:50:53 +03:00
parent 091c475422
commit e96b6a382d
6 changed files with 140 additions and 5 deletions

View File

@@ -43,5 +43,6 @@
#include "bezier.h"
#include "ray.h"
#include "affine2d.h"
#include "affine2d-post.h"
#endif /* cglm_h */

View File

@@ -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
}

View File

@@ -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))
}
}

View File

@@ -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);

View File

@@ -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"

View File

@@ -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)