work
This commit is contained in:
parent
000d233f50
commit
cefc7f4552
2 changed files with 126 additions and 0 deletions
125
src/matrix.zig
Normal file
125
src/matrix.zig
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const expect = std.testing.expect;
|
||||||
|
|
||||||
|
/// A 3D matrix.
|
||||||
|
pub const Mat3 = struct {
|
||||||
|
m: [3][3]f32,
|
||||||
|
|
||||||
|
/// Suitable constructor?
|
||||||
|
// pub fn new(m: f32[3][3]) Mat3 {}
|
||||||
|
|
||||||
|
/// Create new identity matrix
|
||||||
|
pub fn identity() Mat3 {
|
||||||
|
return Mat3{
|
||||||
|
.m = [3][3]f32{
|
||||||
|
[_]f32{ 1.0, 0.0, 0.0 },
|
||||||
|
[_]f32{ 0.0, 1.0, 0.0 },
|
||||||
|
[_]f32{ 0.0, 0.0, 1.0 },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create scaled matrix
|
||||||
|
pub fn scale(x: f32, y: f32, z: f32) Mat3 {
|
||||||
|
return Mat3{
|
||||||
|
.m = [3][3]f32{
|
||||||
|
[_]f32{ x, 0.0, 0.0 },
|
||||||
|
[_]f32{ 0.0, y, 0.0 },
|
||||||
|
[_]f32{ 0.0, 0.0, z },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Matrix multiplication
|
||||||
|
pub fn matmul(self: *const Mat3, other: Mat3) Mat3 {
|
||||||
|
return Mat3{
|
||||||
|
.m = [3][3]f32{
|
||||||
|
[_]f32{
|
||||||
|
self.m[0][0] * other.m[0][0] + self.m[0][1] * other.m[1][0] + self.m[0][2] * other.m[2][0],
|
||||||
|
self.m[0][0] * other.m[0][1] + self.m[0][1] * other.m[1][1] + self.m[0][2] * other.m[2][1],
|
||||||
|
self.m[0][0] * other.m[0][2] + self.m[0][1] * other.m[1][2] + self.m[0][2] * other.m[2][2],
|
||||||
|
},
|
||||||
|
[_]f32{
|
||||||
|
self.m[1][0] * other.m[0][0] + self.m[1][1] * other.m[1][0] + self.m[1][2] * other.m[2][0],
|
||||||
|
self.m[1][0] * other.m[0][1] + self.m[1][1] * other.m[1][1] + self.m[1][2] * other.m[2][1],
|
||||||
|
self.m[1][0] * other.m[0][2] + self.m[1][1] * other.m[1][2] + self.m[1][2] * other.m[2][2],
|
||||||
|
},
|
||||||
|
[_]f32{
|
||||||
|
self.m[2][0] * other.m[0][0] + self.m[2][1] * other.m[1][0] + self.m[2][2] * other.m[2][0],
|
||||||
|
self.m[2][0] * other.m[0][1] + self.m[2][1] * other.m[1][1] + self.m[2][2] * other.m[2][1],
|
||||||
|
self.m[2][0] * other.m[0][2] + self.m[2][1] * other.m[1][2] + self.m[2][2] * other.m[2][2],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// pub fn normalize(self: Mat3) Mat3 {}
|
||||||
|
// pub fn norm(self: *const Mat3) f32 {}
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
/// Custom formatter
|
||||||
|
pub fn format(
|
||||||
|
self: Mat3,
|
||||||
|
comptime fmt: []const f32,
|
||||||
|
options: std.fmt.FormatOptions,
|
||||||
|
writer: anytype,
|
||||||
|
) !void {
|
||||||
|
_ = fmt;
|
||||||
|
_ = options;
|
||||||
|
|
||||||
|
try writer.print("{s} ({}-", .{
|
||||||
|
self.name, self.birth_year,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (self.death_year) |year| {
|
||||||
|
try writer.print("{}", .{year});
|
||||||
|
}
|
||||||
|
|
||||||
|
try writer.writeAll(")");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "Identity" {
|
||||||
|
const a = Mat3.identity();
|
||||||
|
|
||||||
|
try expect(a.m[0][0] == 1.0);
|
||||||
|
try expect(a.m[1][1] == 1.0);
|
||||||
|
try expect(a.m[2][2] == 1.0);
|
||||||
|
|
||||||
|
try expect(a.m[0][1] == 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Matmul" {
|
||||||
|
const a = Mat3{
|
||||||
|
.m = [3][3]f32{
|
||||||
|
[_]f32{ 1.0, 2.0, 3.0 },
|
||||||
|
[_]f32{ 4.0, 5.0, 6.0 },
|
||||||
|
[_]f32{ 7.0, 8.0, 9.0 },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const b = Mat3{
|
||||||
|
.m = [3][3]f32{
|
||||||
|
[_]f32{ 1.0, 2.0, 3.0 },
|
||||||
|
[_]f32{ 4.0, 5.0, 6.0 },
|
||||||
|
[_]f32{ 7.0, 8.0, 9.0 },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const c = a.matmul(b);
|
||||||
|
|
||||||
|
try expect(c.m[0][0] == 30.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Scale" {
|
||||||
|
const a = Mat3.scale(2.0, 3.0, 4.0);
|
||||||
|
|
||||||
|
try expect(a.m[0][0] == 2.0);
|
||||||
|
try expect(a.m[1][1] == 3.0);
|
||||||
|
try expect(a.m[2][2] == 4.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// test "Invert" {}
|
||||||
|
// test "Norm" {}
|
||||||
|
// test "Normalize" {}
|
|
@ -13,6 +13,7 @@ pub const Vec3 = struct {
|
||||||
|
|
||||||
/// Returns the dot product of this vector and another.
|
/// Returns the dot product of this vector and another.
|
||||||
pub fn dot(self: Vec3, other: Vec3) f32 {
|
pub fn dot(self: Vec3, other: Vec3) f32 {
|
||||||
|
// Sum of dimension-wise multiplication
|
||||||
return self.x * other.x + self.y * other.y + self.z * other.z;
|
return self.x * other.x + self.y * other.y + self.z * other.z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue