From ff4c35fae1abe3774b676e2460fbc2f4c022d25e Mon Sep 17 00:00:00 2001 From: Tonis Date: Sun, 5 Mar 2023 21:39:55 +0200 Subject: [PATCH] Added Matrix identity macros and few matrix unit tests (#742) * Add matrix identity macros * Add matrix some matrix unit tests --------- Co-authored-by: Tonis --- lib/std/math/math.c3 | 3 ++ test/unit/stdlib/{ => math}/math.c3 | 0 test/unit/stdlib/math/matrix.c3 | 50 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) rename test/unit/stdlib/{ => math}/math.c3 (100%) create mode 100644 test/unit/stdlib/math/matrix.c3 diff --git a/lib/std/math/math.c3 b/lib/std/math/math.c3 index 49f8afe13..55937c2d1 100644 --- a/lib/std/math/math.c3 +++ b/lib/std/math/math.c3 @@ -105,6 +105,9 @@ define matrix4_ortho = matrix::ortho; define matrix4_perspective = matrix::perspective; define matrix4f_ortho = matrix::ortho; define matrix4f_perspective = matrix::perspective; +macro Matrix4 matrix4_indentity() => { .m = { [0] = 1, [5] = 1, [10] = 1, [15] = 1 } }; +macro Matrix3 matrix3_indentity() => { .m = { [0] = 1, [4] = 1, [8] = 1 } }; +macro Matrix2 matrix2_indentity() => { .m = { [0] = 1, [3] = 1 } }; /** * @require types::is_numerical($typeof(x)) `The input must be a numerical value or numerical vector` diff --git a/test/unit/stdlib/math.c3 b/test/unit/stdlib/math/math.c3 similarity index 100% rename from test/unit/stdlib/math.c3 rename to test/unit/stdlib/math/math.c3 diff --git a/test/unit/stdlib/math/matrix.c3 b/test/unit/stdlib/math/matrix.c3 new file mode 100644 index 000000000..a0787a6a8 --- /dev/null +++ b/test/unit/stdlib/math/matrix.c3 @@ -0,0 +1,50 @@ +module math_matrix @test; +import std::math; + +fn void test_mat4() +{ + {| + Matrix4 mat = math::matrix4_indentity(); + Matrix4 mat2 = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; + Matrix4 calc = mat.mul(mat2); + assert(calc.m == mat.m); + + Matrix4 translated = mat.translate(Vec3{0.0, 0.0, 0.0}); + assert(translated.m == mat.m); + |}; + + {| + Matrix4 mat = { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 }; + Matrix4 mat2 = { 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1 }; + Matrix4 calc = mat.mul(mat2); + Matrix4 value = { 56, 46, 36, 26, 152, 126, 100, 74, 56, 46, 36, 26, 152, 126, 100, 74 }; + assert(calc.m == value.m); + |}; +} + + +fn void test_mat3() +{ + Matrix3 mat = { 3, 5, 3, 5, 2, 6, 6, 2, 1 }; + Matrix3 mat2 = { 4, 2, 6, 7, 8, 9, 2, 3, 4 }; + Matrix3 calc = mat.mul(mat2); + Matrix3 value = { 53, 55, 75, 46, 44, 72, 40, 31, 58 }; + + assert(calc.m == value.m); +} + +fn void test_mat2() +{ + Matrix2 mat = { 3, 5, 5, 2}; + Matrix2 mat2 = { 4, 2, 7, 8 }; + Matrix2 calc = mat.mul(mat2); + Matrix2 value = { 47, 46, 34, 26 }; + + assert(calc.m == value.m); +} + +fn void test_vec3() +{ + Vec3 cross = Vec3{2,3,4}.cross(Vec3{5,6,7}); + assert(cross == Vec3{-3,6,-3}); +} \ No newline at end of file