math: zig fmt

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-10-13 19:27:20 -07:00
parent e1ce5c5662
commit c03673ff05
2 changed files with 11 additions and 22 deletions

View file

@ -362,7 +362,7 @@ pub fn Mat(
/// Matrix * Vector multiplication /// Matrix * Vector multiplication
pub inline fn mulVec(matrix: *const Matrix, vector: *const ColVec) ColVec { pub inline fn mulVec(matrix: *const Matrix, vector: *const ColVec) ColVec {
var result = [_]ColVec.T{0}**ColVec.n; var result = [_]ColVec.T{0} ** ColVec.n;
inline for (0..Matrix.rows) |row| { inline for (0..Matrix.rows) |row| {
inline for (0..ColVec.n) |i| { inline for (0..ColVec.n) |i| {
result[i] += matrix.v[row].v[i] * vector.v[row]; result[i] += matrix.v[row].v[i] * vector.v[row];
@ -371,7 +371,6 @@ pub fn Mat(
return vec.Vec(ColVec.n, ColVec.T){ .v = result }; return vec.Vec(ColVec.n, ColVec.T){ .v = result };
} }
// TODO: the below code was correct in our old implementation, it just needs to be updated // TODO: the below code was correct in our old implementation, it just needs to be updated
// to work with this new Mat approach, swapping f32 for the generic T float type, moving 3x3 // to work with this new Mat approach, swapping f32 for the generic T float type, moving 3x3
// and 4x4 specific functions into the mixin above, writing new tests, etc. // and 4x4 specific functions into the mixin above, writing new tests, etc.
@ -687,6 +686,3 @@ test "Mat4x4_mulVec_vec4" {
const expected = math.vec4(4, 47, 5, 68); const expected = math.vec4(4, 47, 5, 68);
try testing.expect(math.Vec4, expected).eql(m); try testing.expect(math.Vec4, expected).eql(m);
} }

View file

@ -73,18 +73,15 @@ pub fn Vec(comptime n_value: usize, comptime Scalar: type) type {
} }
/// Vector * Matrix multiplication /// Vector * Matrix multiplication
pub inline fn mulMat(vector: *const VecN, matrix: *const mat.Mat(3, 3, Vec(4, T) )) VecN{ pub inline fn mulMat(vector: *const VecN, matrix: *const mat.Mat(3, 3, Vec(4, T))) VecN {
var result = [_]VecN.T{0}**3; var result = [_]VecN.T{0} ** 3;
inline for (0..3) |i|{ inline for (0..3) |i| {
inline for (0..3) |j|{ inline for (0..3) |j| {
result[i] += vector.v[j] * matrix.v[i].v[j]; result[i] += vector.v[j] * matrix.v[i].v[j];
} }
} }
return .{ return .{ .v = result };
.v = result
};
} }
}, },
inline 4 => struct { inline 4 => struct {
pub inline fn init(xs: Scalar, ys: Scalar, zs: Scalar, ws: Scalar) VecN { pub inline fn init(xs: Scalar, ys: Scalar, zs: Scalar, ws: Scalar) VecN {
@ -104,16 +101,14 @@ pub fn Vec(comptime n_value: usize, comptime Scalar: type) type {
} }
/// Vector * Matrix multiplication /// Vector * Matrix multiplication
pub inline fn mulMat(vector: *const VecN, matrix: *const mat.Mat(4, 4, Vec(4, T) )) VecN{ pub inline fn mulMat(vector: *const VecN, matrix: *const mat.Mat(4, 4, Vec(4, T))) VecN {
var result = [_]VecN.T{0}**4; var result = [_]VecN.T{0} ** 4;
inline for (0..4) |i|{ inline for (0..4) |i| {
inline for (0..4) |j|{ inline for (0..4) |j| {
result[i] += vector.v[j] * matrix.v[i].v[j]; result[i] += vector.v[j] * matrix.v[i].v[j];
} }
} }
return .{ return .{ .v = result };
.v = result
};
} }
}, },
else => @compileError("Expected Vec2, Vec3, Vec4, found '" ++ @typeName(VecN) ++ "'"), else => @compileError("Expected Vec2, Vec3, Vec4, found '" ++ @typeName(VecN) ++ "'"),
@ -346,8 +341,6 @@ pub fn Vec(comptime n_value: usize, comptime Scalar: type) type {
return min_scalar; return min_scalar;
} }
}; };
} }