From cc94199131cd397cbdf3873fbd75ec7c6c9ddb28 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 3 Mar 2025 18:13:39 +0100 Subject: [PATCH] Remove Vec2 and other aliases from std::math. --- lib/std/math/math_vector.c3 | 92 ++++++++-------------- releasenotes.md | 1 + test/test_suite/define/weak_alias_fails.c3 | 3 + test/test_suite/define/weak_aliases.c3 | 3 + test/unit/stdlib/math/math_vector.c3 | 80 +++++++++---------- test/unit/stdlib/math/matrix.c3 | 2 +- 6 files changed, 82 insertions(+), 99 deletions(-) diff --git a/lib/std/math/math_vector.c3 b/lib/std/math/math_vector.c3 index 00d9490f9..86d9fd208 100644 --- a/lib/std/math/math_vector.c3 +++ b/lib/std/math/math_vector.c3 @@ -1,79 +1,55 @@ module std::math::vector; import std::math; -def Vec2f = float[<2>]; -def Vec3f = float[<3>]; -def Vec4f = float[<4>]; +macro double[<*>].length_sq(self) => self.dot(self); +macro float[<*>].length_sq(self) => self.dot(self); -def Vec2 = double[<2>]; -def Vec3 = double[<3>]; -def Vec4 = double[<4>]; +macro double[<*>].distance_sq(self, double[<*>] v2) => (self - v2).length_sq(); +macro float[<*>].distance_sq(self, float[<*>] v2) => (self - v2).length_sq(); -macro Vec2f.length_sq(self) => self.dot(self); -macro Vec3f.length_sq(self) => self.dot(self); -macro Vec4f.length_sq(self) => self.dot(self); -macro Vec2.length_sq(self) => self.dot(self); -macro Vec3.length_sq(self) => self.dot(self); -macro Vec4.length_sq(self) => self.dot(self); +macro float[<2>].transform(self, Matrix4f mat) => transform2(self, mat); +macro float[<2>].rotate(self, float angle) => rotate(self, angle); +macro float[<2>].angle(self, float[<2>] v2) => math::atan2(v2.y, v2.x) - math::atan2(self.y, self.x); -macro Vec2f.distance_sq(self, Vec2f v2) => (self - v2).length_sq(); -macro Vec3f.distance_sq(self, Vec3f v2) => (self - v2).length_sq(); -macro Vec4f.distance_sq(self, Vec4f v2) => (self - v2).length_sq(); -macro Vec2.distance_sq(self, Vec2 v2) => (self - v2).length_sq(); -macro Vec3.distance_sq(self, Vec3 v2) => (self - v2).length_sq(); -macro Vec4.distance_sq(self, Vec4 v2) => (self - v2).length_sq(); +macro double[<2>].transform(self, Matrix4 mat) => transform2(self, mat); +macro double[<2>].rotate(self, double angle) => rotate(self, angle); +macro double[<2>].angle(self, double[<2>] v2) => math::atan2(v2.y, v2.x) - math::atan2(self.y, self.x); -macro Vec2f.transform(self, Matrix4f mat) => transform2(self, mat); -macro Vec2f.rotate(self, float angle) => rotate(self, angle); -macro Vec2f.angle(self, Vec2f v2) => math::atan2(v2.y, v2.x) - math::atan2(self.y, self.x); +macro float[<*>].clamp_mag(self, float min, float max) => clamp_magnitude(self, min, max); +macro double[<*>].clamp_mag(self, double min, double max) => clamp_magnitude(self, min, max); -macro Vec2.transform(self, Matrix4 mat) => transform2(self, mat); -macro Vec2.rotate(self, double angle) => rotate(self, angle); -macro Vec2.angle(self, Vec2 v2) => math::atan2(v2.y, v2.x) - math::atan2(self.y, self.x); +macro float[<*>].towards(self, float[<*>] target, float max_distance) => towards(self, target, max_distance); +macro double[<*>].towards(self, double[<*>] target, double max_distance) => towards(self, target, max_distance); -macro Vec2f.clamp_mag(self, float min, float max) => clamp_magnitude(self, min, max); -macro Vec3f.clamp_mag(self, float min, float max) => clamp_magnitude(self, min, max); -macro Vec4f.clamp_mag(self, float min, float max) => clamp_magnitude(self, min, max); -macro Vec2.clamp_mag(self, double min, double max) => clamp_magnitude(self, min, max); -macro Vec3.clamp_mag(self, double min, double max) => clamp_magnitude(self, min, max); -macro Vec4.clamp_mag(self, double min, double max) => clamp_magnitude(self, min, max); +fn float[<3>] float[<3>].cross(self, float[<3>] v2) => cross3(self, v2); +fn double[<3>] double[<3>].cross(self, double[<3>] v2) => cross3(self, v2); -fn Vec2f Vec2f.towards(self, Vec2f target, float max_distance) => towards(self, target, max_distance); -fn Vec3f Vec3f.towards(self, Vec3f target, float max_distance) => towards(self, target, max_distance); -fn Vec4f Vec4f.towards(self, Vec4f target, float max_distance) => towards(self, target, max_distance); -fn Vec2 Vec2.towards(self, Vec2 target, double max_distance) => towards(self, target, max_distance); -fn Vec3 Vec3.towards(self, Vec3 target, double max_distance) => towards(self, target, max_distance); -fn Vec4 Vec4.towards(self, Vec4 target, double max_distance) => towards(self, target, max_distance); +fn float[<3>] float[<3>].perpendicular(self) => perpendicular3(self); +fn double[<3>] double[<3>].perpendicular(self) => perpendicular3(self); -fn Vec3f Vec3f.cross(self, Vec3f v2) => cross3(self, v2); -fn Vec3 Vec3.cross(self, Vec3 v2) => cross3(self, v2); +fn float[<3>] float[<3>].barycenter(self, float[<3>] a, float[<3>] b, float[<3>] c) => barycenter3(self, a, b, c); +fn double[<3>] double[<3>].barycenter(self, double[<3>] a, double[<3>] b, double[<3>] c) => barycenter3(self, a, b, c); -fn Vec3f Vec3f.perpendicular(self) => perpendicular3(self); -fn Vec3 Vec3.perpendicular(self) => perpendicular3(self); +fn float[<3>] float[<3>].transform(self, Matrix4f mat) => transform3(self, mat); +fn double[<3>] double[<3>].transform(self, Matrix4 mat) => transform3(self, mat); -fn Vec3f Vec3f.barycenter(self, Vec3f a, Vec3f b, Vec3f c) => barycenter3(self, a, b, c); -fn Vec3 Vec3.barycenter(self, Vec3 a, Vec3 b, Vec3 c) => barycenter3(self, a, b, c); +fn float float[<3>].angle(self, float[<3>] v2) => angle3(self, v2); +fn double double[<3>].angle(self, double[<3>] v2) => angle3(self, v2); -fn Vec3f Vec3f.transform(self, Matrix4f mat) => transform3(self, mat); -fn Vec3 Vec3.transform(self, Matrix4 mat) => transform3(self, mat); +fn float[<3>] float[<3>].refract(self, float[<3>] n, float r) => refract3(self, n, r); +fn double[<3>] double[<3>].refract(self, double[<3>] n, double r) => refract3(self, n, r); -fn float Vec3f.angle(self, Vec3f v2) => angle3(self, v2); -fn double Vec3.angle(self, Vec3 v2) => angle3(self, v2); +fn float[<3>] float[<3>].rotate_quat(self, Quaternionf q) => rotate_by_quat3(self, q); +fn double[<3>] double[<3>].rotate_quat(self, Quaternion q) => rotate_by_quat3(self, q); -fn Vec3f Vec3f.refract(self, Vec3f n, float r) => refract3(self, n, r); -fn Vec3 Vec3.refract(self, Vec3 n, double r) => refract3(self, n, r); +fn float[<3>] float[<3>].rotate_axis(self, float[<3>] axis, float angle) => rotate_axis_angle(self, axis, angle); +fn double[<3>] double[<3>].rotate_axis(self, double[<3>] axis, double angle) => rotate_axis_angle(self, axis, angle); -fn void ortho_normalize(Vec3f* v1, Vec3f* v2) => ortho_normalize3(v1, v2); -fn void ortho_normalized(Vec3* v1, Vec3* v2) => ortho_normalize3(v1, v2); +fn float[<3>] float[<3>].unproject(self, Matrix4f projection, Matrix4f view) => unproject3(self, projection, view); +fn double[<3>] double[<3>].unproject(self, Matrix4 projection, Matrix4 view) => unproject3(self, projection, view); -fn Vec3f Vec3f.rotate_quat(self, Quaternionf q) => rotate_by_quat3(self, q); -fn Vec3 Vec3.rotate_quat(self, Quaternion q) => rotate_by_quat3(self, q); - -fn Vec3f Vec3f.rotate_axis(self, Vec3f axis, float angle) => rotate_axis_angle(self, axis, angle); -fn Vec3 Vec3.rotate_axis(self, Vec3 axis, double angle) => rotate_axis_angle(self, axis, angle); - -fn Vec3f Vec3f.unproject(self, Matrix4f projection, Matrix4f view) => unproject3(self, projection, view); -fn Vec3 Vec3.unproject(self, Matrix4 projection, Matrix4 view) => unproject3(self, projection, view); +fn void ortho_normalize(float[<3>]* v1, float[<3>]* v2) => ortho_normalize3(v1, v2); +fn void ortho_normalized(double[<3>]* v1, double[<3>]* v2) => ortho_normalize3(v1, v2); macro towards(v, target, max_distance) @private { diff --git a/releasenotes.md b/releasenotes.md index cc6553ed1..da16e5bef 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -31,6 +31,7 @@ - `mem::temp_alloc` and related changed to `mem::talloc`. - `mem::temp_new_array` changed to `mem::temp_array`. - Add `ONHEAP` variants for List/HashMap for initializing global maps on the heap. +- Remove Vec2 and other aliases from std::math. ## 0.6.8 Change list diff --git a/test/test_suite/define/weak_alias_fails.c3 b/test/test_suite/define/weak_alias_fails.c3 index 522da97e6..c80849511 100644 --- a/test/test_suite/define/weak_alias_fails.c3 +++ b/test/test_suite/define/weak_alias_fails.c3 @@ -1,3 +1,6 @@ +module std::math::vector; +def Vec2f = float[<2>]; + module abc; import std::math; def Vec2f = std::math::vector::Vec2f @weak; diff --git a/test/test_suite/define/weak_aliases.c3 b/test/test_suite/define/weak_aliases.c3 index 591b8c029..77171d0a0 100644 --- a/test/test_suite/define/weak_aliases.c3 +++ b/test/test_suite/define/weak_aliases.c3 @@ -1,3 +1,6 @@ +module std::math::vector; +def Vec2f = float[<2>]; + module abc; import std::math; def Vec2f = std::math::vector::Vec2f @weak; diff --git a/test/unit/stdlib/math/math_vector.c3 b/test/unit/stdlib/math/math_vector.c3 index 08be5ea49..61ab8b469 100644 --- a/test/unit/stdlib/math/math_vector.c3 +++ b/test/unit/stdlib/math/math_vector.c3 @@ -6,8 +6,8 @@ const EPSILON_D = 1e-12; fn void test_vec2_init() @test { - Vec2f v1 = { 1.0f, 2.0f }; - Vec2 v2 = { 1.0, 2.0 }; + float[<2>] v1 = { 1.0f, 2.0f }; + double[<2>] v2 = { 1.0, 2.0 }; $assert @typeis(v1[0], float); $assert @typeis(v2[0], double); assert(v1[0] == 1.0f && v1[1] == 2.0f); @@ -16,32 +16,32 @@ fn void test_vec2_init() @test fn void test_vec2_arithmetic() @test { - Vec2f v1 = { 1.0f, 2.0f }; - Vec2f v2 = { 2.0f, 3.0f }; - Vec2f sum = v1 + v2; + float[<2>] v1 = { 1.0f, 2.0f }; + float[<2>] v2 = { 2.0f, 3.0f }; + float[<2>] sum = v1 + v2; assert(math::is_approx_rel(sum[0], 3.0f, EPSILON_F)); assert(math::is_approx_rel(sum[1], 5.0f, EPSILON_F)); - Vec2f diff = v2 - v1; + float[<2>] diff = v2 - v1; assert(math::is_approx_rel(diff[0], 1.0f, EPSILON_F)); assert(math::is_approx_rel(diff[1], 1.0f, EPSILON_F)); - Vec2f scaled = v1 * 2.0f; + float[<2>] scaled = v1 * 2.0f; assert(math::is_approx_rel(scaled[0], 2.0f, EPSILON_F)); assert(math::is_approx_rel(scaled[1], 4.0f, EPSILON_F)); } fn void test_vec2_length() @test { - Vec2f v = { 3.0f, 4.0f }; + float[<2>] v = { 3.0f, 4.0f }; assert(math::is_approx_rel(v.length_sq(), 25.0f, EPSILON_F)); - Vec2f v2 = { 0.0f, 0.0f }; + float[<2>] v2 = { 0.0f, 0.0f }; assert(math::is_approx_rel(v.distance_sq(v2), 25.0f, EPSILON_F)); } fn void test_vec3_operations() @test { - Vec3f v1 = { 1.0f, 0.0f, 0.0f }; - Vec3f v2 = { 0.0f, 1.0f, 0.0f }; - Vec3f cross = v1.cross(v2); + float[<3>] v1 = { 1.0f, 0.0f, 0.0f }; + float[<3>] v2 = { 0.0f, 1.0f, 0.0f }; + float[<3>] cross = v1.cross(v2); assert(math::is_approx_rel(cross[0], 0.0f, EPSILON_F)); assert(math::is_approx_rel(cross[1], 0.0f, EPSILON_F)); assert(math::is_approx_rel(cross[2], 1.0f, EPSILON_F)); @@ -51,35 +51,35 @@ fn void test_vec3_operations() @test fn void test_vec3_transform() @test { - Vec3f v = { 1.0f, 0.0f, 0.0f }; - Vec3f axis = { 0.0f, 0.0f, 1.0f }; - Vec3f rotated = v.rotate_axis(axis, math::PI_2); + float[<3>] v = { 1.0f, 0.0f, 0.0f }; + float[<3>] axis = { 0.0f, 0.0f, 1.0f }; + float[<3>] rotated = v.rotate_axis(axis, math::PI_2); assert(math::is_approx_rel(rotated[0] * rotated[0] + rotated[1] * rotated[1], 1.0f, EPSILON_F)); assert(math::is_approx_rel(rotated[2], 0.0f, EPSILON_F)); - Vec3f perp = v.perpendicular(); + float[<3>] perp = v.perpendicular(); assert(math::is_approx_rel(v.dot(perp), 0.0f, EPSILON_F)); assert(math::is_approx_rel(perp.length_sq(), v.length_sq(), EPSILON_F)); } fn void test_vec_magnitude() @test { - Vec2f v2 = { 3.0f, 4.0f }; - Vec3f v3 = { 2.0f, 2.0f, 1.0f }; - Vec2f clamped2 = v2.clamp_mag(1.0f, 3.0f); + float[<2>] v2 = { 3.0f, 4.0f }; + float[<3>] v3 = { 2.0f, 2.0f, 1.0f }; + float[<2>] clamped2 = v2.clamp_mag(1.0f, 3.0f); assert(math::is_approx_rel(clamped2.length(), 3.0f, EPSILON_F)); - Vec3f clamped3 = v3.clamp_mag(2.0f, 4.0f); + float[<3>] clamped3 = v3.clamp_mag(2.0f, 4.0f); assert(math::is_approx_rel(clamped3.length(), 3.0f, EPSILON_F)); } fn void test_vec_interpolation() @test { - Vec2f start = { 0.0f, 0.0f }; - Vec2f target = { 10.0f, 0.0f }; + float[<2>] start = { 0.0f, 0.0f }; + float[<2>] target = { 10.0f, 0.0f }; float max_dist = 2.0f; - Vec2f result = start.towards(target, max_dist); + float[<2>] result = start.towards(target, max_dist); assert(math::is_approx_rel(result[0], 2.0f, EPSILON_F)); assert(math::is_approx_rel(result[1], 0.0f, EPSILON_F)); - Vec2f close_start = { 9.0f, 0.0f }; + float[<2>] close_start = { 9.0f, 0.0f }; result = close_start.towards(target, max_dist); assert(math::is_approx_rel(result[0], target[0], EPSILON_F)); assert(math::is_approx_rel(result[1], target[1], EPSILON_F)); @@ -87,39 +87,39 @@ fn void test_vec_interpolation() @test fn void test_vec3_advanced() @test { - Vec3f v = { 1.0f, 1.0f, 1.0f }; - Vec3f n = { 0.0f, 1.0f, 0.0f }; + float[<3>] v = { 1.0f, 1.0f, 1.0f }; + float[<3>] n = { 0.0f, 1.0f, 0.0f }; float r = 1.5f; - Vec3f refracted = v.refract(n, r); + float[<3>] refracted = v.refract(n, r); assert(refracted.length_sq() > 0.0f); - Vec3f a = { 0.0f, 0.0f, 0.0f }; - Vec3f b = { 1.0f, 0.0f, 0.0f }; - Vec3f c = { 0.0f, 1.0f, 0.0f }; - Vec3f p = { 0.25f, 0.25f, 0.0f }; - Vec3f bary = p.barycenter(a, b, c); + float[<3>] a = { 0.0f, 0.0f, 0.0f }; + float[<3>] b = { 1.0f, 0.0f, 0.0f }; + float[<3>] c = { 0.0f, 1.0f, 0.0f }; + float[<3>] p = { 0.25f, 0.25f, 0.0f }; + float[<3>] bary = p.barycenter(a, b, c); assert(math::is_approx_rel(bary[0] + bary[1] + bary[2], 1.0f, EPSILON_F)); } fn void test_edge_cases() @test { - Vec2f zero2 = { 0.0f, 0.0f }; - Vec3f zero3 = { 0.0f, 0.0f, 0.0f }; + float[<2>] zero2 = { 0.0f, 0.0f }; + float[<3>] zero3 = { 0.0f, 0.0f, 0.0f }; assert(zero2.length_sq() == 0.0f); assert(zero3.length_sq() == 0.0f); - Vec3f perp = zero3.perpendicular(); + float[<3>] perp = zero3.perpendicular(); assert(perp.length_sq() <= 1.0f + EPSILON_F); - Vec2f clamped = zero2.clamp_mag(1.0f, 2.0f); + float[<2>] clamped = zero2.clamp_mag(1.0f, 2.0f); assert(clamped.length_sq() == 0.0f); } fn void test_type_consistency() @test { - Vec2f vf = { 1.0f, 2.0f }; - Vec2 vd = { 1.0, 2.0 }; + float[<2>] vf = { 1.0f, 2.0f }; + double[<2>] vd = { 1.0, 2.0 }; $assert @typeis(vf.length_sq(), float); $assert @typeis(vd.length_sq(), double); - Vec3f v3f = { 1.0f, 2.0f, 3.0f }; - Vec3 v3d = { 1.0, 2.0, 3.0 }; + float[<3>] v3f = { 1.0f, 2.0f, 3.0f }; + double[<3>] v3d = { 1.0, 2.0, 3.0 }; $assert @typeis(v3f.cross(v3f)[0], float); $assert @typeis(v3d.cross(v3d)[0], double); } \ No newline at end of file diff --git a/test/unit/stdlib/math/matrix.c3 b/test/unit/stdlib/math/matrix.c3 index 7bc568410..ff2831905 100644 --- a/test/unit/stdlib/math/matrix.c3 +++ b/test/unit/stdlib/math/matrix.c3 @@ -117,6 +117,6 @@ fn void test_mat2_inverse() fn void test_vec3() { - Vec3 cross = (Vec3){2,3,4}.cross({5,6,7}); + double[<3>] cross = (double[<3>]){2,3,4}.cross({5,6,7}); assert(cross == {-3,6,-3}); }