all: use mach.math instead of std.math; fixes hexops/mach#1021
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
7904b74145
commit
89622810f8
7 changed files with 42 additions and 41 deletions
|
|
@ -20,6 +20,9 @@ const assert = std.debug.assert;
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const mach = @import("../../main.zig");
|
||||||
|
const math = mach.math;
|
||||||
|
|
||||||
const log = std.log.scoped(.atlas);
|
const log = std.log.scoped(.atlas);
|
||||||
|
|
||||||
/// Data is the raw texture data.
|
/// 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.
|
// Find the location in our nodes list to insert the new node for this region.
|
||||||
const best_idx: usize = best_idx: {
|
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 best_width: u32 = best_height;
|
||||||
var chosen: ?usize = null;
|
var chosen: ?usize = null;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
const std = @import("std");
|
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
|
/// 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.
|
/// 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,
|
next_sparse: IndexType = null_index,
|
||||||
};
|
};
|
||||||
|
|
||||||
const null_index: IndexType = std.math.maxInt(IndexType);
|
const null_index: IndexType = math.maxInt(IndexType);
|
||||||
|
|
||||||
vertices: []VertexType,
|
vertices: []VertexType,
|
||||||
indices: []IndexType,
|
indices: []IndexType,
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,7 @@ pub const inf = std.math.inf;
|
||||||
pub const sqrt = std.math.sqrt;
|
pub const sqrt = std.math.sqrt;
|
||||||
pub const sin = std.math.sin;
|
pub const sin = std.math.sin;
|
||||||
pub const cos = std.math.cos;
|
pub const cos = std.math.cos;
|
||||||
|
pub const acos = std.math.acos;
|
||||||
pub const isNan = std.math.isNan;
|
pub const isNan = std.math.isNan;
|
||||||
pub const isInf = std.math.isInf;
|
pub const isInf = std.math.isInf;
|
||||||
pub const pi = std.math.pi;
|
pub const pi = std.math.pi;
|
||||||
|
|
@ -143,6 +144,7 @@ pub const clamp = std.math.clamp;
|
||||||
pub const log10 = std.math.log10;
|
pub const log10 = std.math.log10;
|
||||||
pub const degreesToRadians = std.math.degreesToRadians;
|
pub const degreesToRadians = std.math.degreesToRadians;
|
||||||
pub const radiansToDegrees = std.math.radiansToDegrees;
|
pub const radiansToDegrees = std.math.radiansToDegrees;
|
||||||
|
pub const maxInt = std.math.maxInt;
|
||||||
|
|
||||||
/// 2/sqrt(π)
|
/// 2/sqrt(π)
|
||||||
pub const two_sqrtpi = std.math.two_sqrtpi;
|
pub const two_sqrtpi = std.math.two_sqrtpi;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
const std = @import("std");
|
|
||||||
|
|
||||||
const mach = @import("../main.zig");
|
const mach = @import("../main.zig");
|
||||||
const testing = mach.testing;
|
const testing = mach.testing;
|
||||||
const math = mach.math;
|
const math = mach.math;
|
||||||
|
|
@ -308,8 +306,8 @@ pub fn Mat(
|
||||||
|
|
||||||
/// Constructs a 3D matrix which rotates around the X axis by `angle_radians`.
|
/// Constructs a 3D matrix which rotates around the X axis by `angle_radians`.
|
||||||
pub inline fn rotateX(angle_radians: f32) Matrix {
|
pub inline fn rotateX(angle_radians: f32) Matrix {
|
||||||
const c = std.math.cos(angle_radians);
|
const c = math.cos(angle_radians);
|
||||||
const s = std.math.sin(angle_radians);
|
const s = math.sin(angle_radians);
|
||||||
return Matrix.init(
|
return Matrix.init(
|
||||||
&RowVec.init(1, 0, 0, 0),
|
&RowVec.init(1, 0, 0, 0),
|
||||||
&RowVec.init(0, c, -s, 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`.
|
/// Constructs a 3D matrix which rotates around the X axis by `angle_radians`.
|
||||||
pub inline fn rotateY(angle_radians: f32) Matrix {
|
pub inline fn rotateY(angle_radians: f32) Matrix {
|
||||||
const c = std.math.cos(angle_radians);
|
const c = math.cos(angle_radians);
|
||||||
const s = std.math.sin(angle_radians);
|
const s = math.sin(angle_radians);
|
||||||
return Matrix.init(
|
return Matrix.init(
|
||||||
&RowVec.init(c, 0, s, 0),
|
&RowVec.init(c, 0, s, 0),
|
||||||
&RowVec.init(0, 1, 0, 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`.
|
/// Constructs a 3D matrix which rotates around the Z axis by `angle_radians`.
|
||||||
pub inline fn rotateZ(angle_radians: f32) Matrix {
|
pub inline fn rotateZ(angle_radians: f32) Matrix {
|
||||||
const c = std.math.cos(angle_radians);
|
const c = math.cos(angle_radians);
|
||||||
const s = std.math.sin(angle_radians);
|
const s = math.sin(angle_radians);
|
||||||
return Matrix.init(
|
return Matrix.init(
|
||||||
&RowVec.init(c, -s, 0, 0),
|
&RowVec.init(c, -s, 0, 0),
|
||||||
&RowVec.init(s, c, 0, 0),
|
&RowVec.init(s, c, 0, 0),
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
const std = @import("std");
|
|
||||||
|
|
||||||
const mach = @import("../main.zig");
|
const mach = @import("../main.zig");
|
||||||
const testing = mach.testing;
|
const testing = mach.testing;
|
||||||
const math = mach.math;
|
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.
|
/// Creates a Quaternion based on the given `axis` and `angle`, and returns it.
|
||||||
pub inline fn fromAxisAngle(axis: Axis, angle: T) Quat(T) {
|
pub inline fn fromAxisAngle(axis: Axis, angle: T) Quat(T) {
|
||||||
const halfAngle = angle * 0.5;
|
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.
|
/// Calculates the angle between two given quaternions.
|
||||||
pub inline fn angleBetween(a: *const Quat(T), b: *const Quat(T)) T {
|
pub inline fn angleBetween(a: *const Quat(T), b: *const Quat(T)) T {
|
||||||
const d = Vec.dot(&a.v, &b.v);
|
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
|
/// Multiplies two quaternions
|
||||||
|
|
@ -97,8 +95,8 @@ pub fn Quat(comptime Scalar: type) type {
|
||||||
const qz = q.v.z();
|
const qz = q.v.z();
|
||||||
const qw = q.v.w();
|
const qw = q.v.w();
|
||||||
|
|
||||||
const bx = std.math.sin(halfAngle);
|
const bx = math.sin(halfAngle);
|
||||||
const bw = std.math.cos(halfAngle);
|
const bw = math.cos(halfAngle);
|
||||||
|
|
||||||
return init(qx * bw + qw * bx, qy * bw + qz * bx, qz * bw - qy * bx, qw * bw - qx * bx);
|
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 qz = q.v.z();
|
||||||
const qw = q.v.w();
|
const qw = q.v.w();
|
||||||
|
|
||||||
const by = std.math.sin(halfAngle);
|
const by = math.sin(halfAngle);
|
||||||
const bw = std.math.cos(halfAngle);
|
const bw = math.cos(halfAngle);
|
||||||
|
|
||||||
return init(qx * bw - qz * by, qy * bw + qw * by, qz * bw + qx * by, qw * bw - qy * by);
|
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 qz = q.v.z();
|
||||||
const qw = q.v.w();
|
const qw = q.v.w();
|
||||||
|
|
||||||
const bz = std.math.sin(halfAngle);
|
const bz = math.sin(halfAngle);
|
||||||
const bw = std.math.cos(halfAngle);
|
const bw = math.cos(halfAngle);
|
||||||
|
|
||||||
return init(qx * bw - qy * bz, qy * bw + qx * bz, qz * bw + qw * bz, qw * bw - qz * bz);
|
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;
|
var scale1: T = 0.0;
|
||||||
|
|
||||||
if (1.0 - cosOmega > math.eps(T)) {
|
if (1.0 - cosOmega > math.eps(T)) {
|
||||||
const omega = std.math.acos(cosOmega);
|
const omega = math.acos(cosOmega);
|
||||||
const sinOmega = std.math.sin(omega);
|
const sinOmega = math.sin(omega);
|
||||||
scale0 = std.math.sin((1.0 - t) * omega) / sinOmega;
|
scale0 = math.sin((1.0 - t) * omega) / sinOmega;
|
||||||
scale1 = std.math.sin(t * omega) / sinOmega;
|
scale1 = math.sin(t * omega) / sinOmega;
|
||||||
} else {
|
} else {
|
||||||
scale0 = 1.0 - t;
|
scale0 = 1.0 - t;
|
||||||
scale1 = 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];
|
const trace = m.v[0].v[0] + m.v[1].v[1] + m.v[2].v[2];
|
||||||
|
|
||||||
if (trace > 0) {
|
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;
|
dst.v.v[3] = 0.5 * root;
|
||||||
const rootInv = 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 j = (i + 1) % 3;
|
||||||
const k = (i + 2) % 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;
|
dst.v.v[i] = 0.5 * root;
|
||||||
|
|
||||||
const rootInv = 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 yHalf = y * 0.5;
|
||||||
const zHalf = z * 0.5;
|
const zHalf = z * 0.5;
|
||||||
|
|
||||||
const sx = std.math.sin(xHalf);
|
const sx = math.sin(xHalf);
|
||||||
const cx = std.math.cos(xHalf);
|
const cx = math.cos(xHalf);
|
||||||
const sy = std.math.sin(yHalf);
|
const sy = math.sin(yHalf);
|
||||||
const cy = std.math.cos(yHalf);
|
const cy = math.cos(yHalf);
|
||||||
const sz = std.math.sin(zHalf);
|
const sz = math.sin(zHalf);
|
||||||
const cz = std.math.cos(zHalf);
|
const cz = math.cos(zHalf);
|
||||||
|
|
||||||
const xRet = sx * cy * cz + cx * sy * sz;
|
const xRet = sx * cy * cz + cx * sy * sz;
|
||||||
const yRet = cx * sy * cz - sx * cy * 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.
|
/// Computes the length of a given quaternion.
|
||||||
pub inline fn len(q: *const Quat(T)) T {
|
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.
|
/// 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 q2 = q.v.z();
|
||||||
const q3 = q.v.w();
|
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) {
|
if (length > 0.00001) {
|
||||||
return init(q0 / length, q1 / length, q2 / length, q3 / length);
|
return init(q0 / length, q1 / length, q2 / length, q3 / length);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
const std = @import("std");
|
|
||||||
|
|
||||||
const mach = @import("../main.zig");
|
const mach = @import("../main.zig");
|
||||||
const testing = mach.testing;
|
const testing = mach.testing;
|
||||||
const math = mach.math;
|
const math = mach.math;
|
||||||
|
|
@ -162,7 +160,7 @@ pub fn Ray(comptime Vec3P: type) type {
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
std.math.inf(f32),
|
math.inf(f32),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (backface_culling) {
|
if (backface_culling) {
|
||||||
|
|
|
||||||
|
|
@ -493,7 +493,7 @@ test "normalize_example" {
|
||||||
|
|
||||||
test "normalize_accuracy" {
|
test "normalize_accuracy" {
|
||||||
const normalized = math.vec2(1, 1).normalize(0);
|
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);
|
try testing.expect(math.Vec2, math.Vec2.splat(norm_val)).eql(normalized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -818,7 +818,7 @@ test "dir_zero_vec4" {
|
||||||
test "dir_vec2" {
|
test "dir_vec2" {
|
||||||
const a: math.Vec2 = math.vec2(1, 2);
|
const a: math.Vec2 = math.vec2(1, 2);
|
||||||
const b: math.Vec2 = math.vec2(3, 4);
|
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));
|
.eql(a.dir(&b, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -992,7 +992,7 @@ test "Mat4x4_mulMat" {
|
||||||
test "mulQuat" {
|
test "mulQuat" {
|
||||||
const up = math.vec3(0, 1, 0);
|
const up = math.vec3(0, 1, 0);
|
||||||
const id = math.Quat.identity();
|
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));
|
try testing.expect(math.Vec3, math.vec3(1, 0, 0)).eql(up.mulQuat(&rot));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue