From cefc7f4552da8e5573982d75a48bb9b7fb4f19c5 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 16 Aug 2024 17:32:38 +0200 Subject: [PATCH] work --- src/matrix.zig | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ src/vector.zig | 1 + 2 files changed, 126 insertions(+) create mode 100644 src/matrix.zig diff --git a/src/matrix.zig b/src/matrix.zig new file mode 100644 index 0000000..d738795 --- /dev/null +++ b/src/matrix.zig @@ -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" {} diff --git a/src/vector.zig b/src/vector.zig index 7993532..628f86f 100644 --- a/src/vector.zig +++ b/src/vector.zig @@ -13,6 +13,7 @@ pub const Vec3 = struct { /// Returns the dot product of this vector and another. 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; }