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
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue