From 89622810f83826acb8689145ebbd271d1a077dc9 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Thu, 18 Jan 2024 22:34:12 -0700 Subject: [PATCH] all: use mach.math instead of std.math; fixes hexops/mach#1021 Signed-off-by: Stephen Gutekanst --- src/gfx/atlas/Atlas.zig | 5 ++++- src/gfx/util.zig | 4 +++- src/math/main.zig | 2 ++ src/math/mat.zig | 14 ++++++------ src/math/quat.zig | 48 ++++++++++++++++++++--------------------- src/math/ray.zig | 4 +--- src/math/vec.zig | 6 +++--- 7 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/gfx/atlas/Atlas.zig b/src/gfx/atlas/Atlas.zig index c8b68d0d..875b844b 100644 --- a/src/gfx/atlas/Atlas.zig +++ b/src/gfx/atlas/Atlas.zig @@ -20,6 +20,9 @@ const assert = std.debug.assert; const Allocator = std.mem.Allocator; const testing = std.testing; +const mach = @import("../../main.zig"); +const math = mach.math; + const log = std.log.scoped(.atlas); /// Data is the raw texture data. @@ -145,7 +148,7 @@ pub fn reserve(self: *Atlas, alloc: Allocator, width: u32, height: u32) !Region // Find the location in our nodes list to insert the new node for this region. const best_idx: usize = best_idx: { - var best_height: u32 = std.math.maxInt(u32); + var best_height: u32 = math.maxInt(u32); var best_width: u32 = best_height; var chosen: ?usize = null; diff --git a/src/gfx/util.zig b/src/gfx/util.zig index 1610981e..d2bf4358 100644 --- a/src/gfx/util.zig +++ b/src/gfx/util.zig @@ -1,4 +1,6 @@ const std = @import("std"); +const mach = @import("../main.zig"); +const math = mach.math; /// Vertex writer manages the placement of vertices by tracking which are unique. If a duplicate vertex is added /// with `put`, only it's index will be written to the index buffer. @@ -10,7 +12,7 @@ pub fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type { next_sparse: IndexType = null_index, }; - const null_index: IndexType = std.math.maxInt(IndexType); + const null_index: IndexType = math.maxInt(IndexType); vertices: []VertexType, indices: []IndexType, diff --git a/src/math/main.zig b/src/math/main.zig index 9b2d362e..7762f9b4 100644 --- a/src/math/main.zig +++ b/src/math/main.zig @@ -136,6 +136,7 @@ pub const inf = std.math.inf; pub const sqrt = std.math.sqrt; pub const sin = std.math.sin; pub const cos = std.math.cos; +pub const acos = std.math.acos; pub const isNan = std.math.isNan; pub const isInf = std.math.isInf; pub const pi = std.math.pi; @@ -143,6 +144,7 @@ pub const clamp = std.math.clamp; pub const log10 = std.math.log10; pub const degreesToRadians = std.math.degreesToRadians; pub const radiansToDegrees = std.math.radiansToDegrees; +pub const maxInt = std.math.maxInt; /// 2/sqrt(π) pub const two_sqrtpi = std.math.two_sqrtpi; diff --git a/src/math/mat.zig b/src/math/mat.zig index 3b832577..5e30e088 100644 --- a/src/math/mat.zig +++ b/src/math/mat.zig @@ -1,5 +1,3 @@ -const std = @import("std"); - const mach = @import("../main.zig"); const testing = mach.testing; const math = mach.math; @@ -308,8 +306,8 @@ pub fn Mat( /// Constructs a 3D matrix which rotates around the X axis by `angle_radians`. pub inline fn rotateX(angle_radians: f32) Matrix { - const c = std.math.cos(angle_radians); - const s = std.math.sin(angle_radians); + const c = math.cos(angle_radians); + const s = math.sin(angle_radians); return Matrix.init( &RowVec.init(1, 0, 0, 0), &RowVec.init(0, c, -s, 0), @@ -320,8 +318,8 @@ pub fn Mat( /// Constructs a 3D matrix which rotates around the X axis by `angle_radians`. pub inline fn rotateY(angle_radians: f32) Matrix { - const c = std.math.cos(angle_radians); - const s = std.math.sin(angle_radians); + const c = math.cos(angle_radians); + const s = math.sin(angle_radians); return Matrix.init( &RowVec.init(c, 0, s, 0), &RowVec.init(0, 1, 0, 0), @@ -332,8 +330,8 @@ pub fn Mat( /// Constructs a 3D matrix which rotates around the Z axis by `angle_radians`. pub inline fn rotateZ(angle_radians: f32) Matrix { - const c = std.math.cos(angle_radians); - const s = std.math.sin(angle_radians); + const c = math.cos(angle_radians); + const s = math.sin(angle_radians); return Matrix.init( &RowVec.init(c, -s, 0, 0), &RowVec.init(s, c, 0, 0), diff --git a/src/math/quat.zig b/src/math/quat.zig index 9a980e95..c11ae917 100644 --- a/src/math/quat.zig +++ b/src/math/quat.zig @@ -1,5 +1,3 @@ -const std = @import("std"); - const mach = @import("../main.zig"); const testing = mach.testing; const math = mach.math; @@ -38,15 +36,15 @@ pub fn Quat(comptime Scalar: type) type { /// Creates a Quaternion based on the given `axis` and `angle`, and returns it. pub inline fn fromAxisAngle(axis: Axis, angle: T) Quat(T) { const halfAngle = angle * 0.5; - const s = std.math.sin(halfAngle); + const s = math.sin(halfAngle); - return init(s * axis.x(), s * axis.y(), s * axis.z(), std.math.cos(halfAngle)); + return init(s * axis.x(), s * axis.y(), s * axis.z(), math.cos(halfAngle)); } /// Calculates the angle between two given quaternions. pub inline fn angleBetween(a: *const Quat(T), b: *const Quat(T)) T { const d = Vec.dot(&a.v, &b.v); - return std.math.acos(2 * d * d - 1); + return math.acos(2 * d * d - 1); } /// Multiplies two quaternions @@ -97,8 +95,8 @@ pub fn Quat(comptime Scalar: type) type { const qz = q.v.z(); const qw = q.v.w(); - const bx = std.math.sin(halfAngle); - const bw = std.math.cos(halfAngle); + const bx = math.sin(halfAngle); + const bw = math.cos(halfAngle); return init(qx * bw + qw * bx, qy * bw + qz * bx, qz * bw - qy * bx, qw * bw - qx * bx); } @@ -112,8 +110,8 @@ pub fn Quat(comptime Scalar: type) type { const qz = q.v.z(); const qw = q.v.w(); - const by = std.math.sin(halfAngle); - const bw = std.math.cos(halfAngle); + const by = math.sin(halfAngle); + const bw = math.cos(halfAngle); return init(qx * bw - qz * by, qy * bw + qw * by, qz * bw + qx * by, qw * bw - qy * by); } @@ -127,8 +125,8 @@ pub fn Quat(comptime Scalar: type) type { const qz = q.v.z(); const qw = q.v.w(); - const bz = std.math.sin(halfAngle); - const bw = std.math.cos(halfAngle); + const bz = math.sin(halfAngle); + const bw = math.cos(halfAngle); return init(qx * bw - qy * bz, qy * bw + qx * bz, qz * bw + qw * bz, qw * bw - qz * bz); } @@ -158,10 +156,10 @@ pub fn Quat(comptime Scalar: type) type { var scale1: T = 0.0; if (1.0 - cosOmega > math.eps(T)) { - const omega = std.math.acos(cosOmega); - const sinOmega = std.math.sin(omega); - scale0 = std.math.sin((1.0 - t) * omega) / sinOmega; - scale1 = std.math.sin(t * omega) / sinOmega; + const omega = math.acos(cosOmega); + const sinOmega = math.sin(omega); + scale0 = math.sin((1.0 - t) * omega) / sinOmega; + scale1 = math.sin(t * omega) / sinOmega; } else { scale0 = 1.0 - t; scale1 = t; @@ -181,7 +179,7 @@ pub fn Quat(comptime Scalar: type) type { const trace = m.v[0].v[0] + m.v[1].v[1] + m.v[2].v[2]; if (trace > 0) { - const root = std.math.sqrt(trace + 1.0); + const root = math.sqrt(trace + 1.0); dst.v.v[3] = 0.5 * root; const rootInv = 0.5 / root; @@ -202,7 +200,7 @@ pub fn Quat(comptime Scalar: type) type { const j = (i + 1) % 3; const k = (i + 2) % 3; - const root = std.math.sqrt(m.v[i].v[i] - m.v[j].v[j] - m.v[k].v[k] + 1.0); + const root = math.sqrt(m.v[i].v[i] - m.v[j].v[j] - m.v[k].v[k] + 1.0); dst.v.v[i] = 0.5 * root; const rootInv = 0.5 / root; @@ -221,12 +219,12 @@ pub fn Quat(comptime Scalar: type) type { const yHalf = y * 0.5; const zHalf = z * 0.5; - const sx = std.math.sin(xHalf); - const cx = std.math.cos(xHalf); - const sy = std.math.sin(yHalf); - const cy = std.math.cos(yHalf); - const sz = std.math.sin(zHalf); - const cz = std.math.cos(zHalf); + const sx = math.sin(xHalf); + const cx = math.cos(xHalf); + const sy = math.sin(yHalf); + const cy = math.cos(yHalf); + const sz = math.sin(zHalf); + const cz = math.cos(zHalf); const xRet = sx * cy * cz + cx * sy * sz; const yRet = cx * sy * cz - sx * cy * sz; @@ -258,7 +256,7 @@ pub fn Quat(comptime Scalar: type) type { /// Computes the length of a given quaternion. pub inline fn len(q: *const Quat(T)) T { - return std.math.sqrt(q.v.x() * q.v.x() + q.v.y() * q.v.y() + q.v.z() * q.v.z() + q.v.w() * q.v.w()); + return math.sqrt(q.v.x() * q.v.x() + q.v.y() * q.v.y() + q.v.z() * q.v.z() + q.v.w() * q.v.w()); } /// Computes the normalized version of a given quaternion. @@ -268,7 +266,7 @@ pub fn Quat(comptime Scalar: type) type { const q2 = q.v.z(); const q3 = q.v.w(); - const length = std.math.sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); + const length = math.sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); if (length > 0.00001) { return init(q0 / length, q1 / length, q2 / length, q3 / length); diff --git a/src/math/ray.zig b/src/math/ray.zig index b69a8450..83b3c104 100644 --- a/src/math/ray.zig +++ b/src/math/ray.zig @@ -1,5 +1,3 @@ -const std = @import("std"); - const mach = @import("../main.zig"); const testing = mach.testing; const math = mach.math; @@ -162,7 +160,7 @@ pub fn Ray(comptime Vec3P: type) type { undefined, undefined, undefined, - std.math.inf(f32), + math.inf(f32), ); if (backface_culling) { diff --git a/src/math/vec.zig b/src/math/vec.zig index 054037d0..3b751e08 100644 --- a/src/math/vec.zig +++ b/src/math/vec.zig @@ -493,7 +493,7 @@ test "normalize_example" { test "normalize_accuracy" { const normalized = math.vec2(1, 1).normalize(0); - const norm_val = std.math.sqrt1_2; // 1 / sqrt(2) + const norm_val = math.sqrt1_2; // 1 / sqrt(2) try testing.expect(math.Vec2, math.Vec2.splat(norm_val)).eql(normalized); } @@ -818,7 +818,7 @@ test "dir_zero_vec4" { test "dir_vec2" { const a: math.Vec2 = math.vec2(1, 2); const b: math.Vec2 = math.vec2(3, 4); - try testing.expect(math.Vec2, math.vec2(std.math.sqrt1_2, std.math.sqrt1_2)) + try testing.expect(math.Vec2, math.vec2(math.sqrt1_2, math.sqrt1_2)) .eql(a.dir(&b, 0)); } @@ -992,7 +992,7 @@ test "Mat4x4_mulMat" { test "mulQuat" { const up = math.vec3(0, 1, 0); const id = math.Quat.identity(); - const rot = math.Quat.rotateZ(&id, -std.math.pi / 2.0); + const rot = math.Quat.rotateZ(&id, -math.pi / 2.0); try testing.expect(math.Vec3, math.vec3(1, 0, 0)).eql(up.mulQuat(&rot)); }