Skip to content

Commit

Permalink
Add format function for vector and matrix types.
Browse files Browse the repository at this point in the history
  • Loading branch information
msg-programs authored and emidoots committed Feb 2, 2025
1 parent a812370 commit 6fd6a8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/math/mat.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const std = @import("std");

const mach = @import("../main.zig");
const testing = mach.testing;
const math = mach.math;
Expand Down Expand Up @@ -118,6 +120,7 @@ pub fn Mat2x2(

pub const mul = Shared.mul;
pub const mulVec = Shared.mulVec;
pub const format = Shared.format;
};
}

Expand Down Expand Up @@ -258,6 +261,7 @@ pub fn Mat3x3(

pub const mul = Shared.mul;
pub const mulVec = Shared.mulVec;
pub const format = Shared.format;
};
}

Expand Down Expand Up @@ -485,6 +489,7 @@ pub fn Mat4x4(
pub const mulVec = Shared.mulVec;
pub const eql = Shared.eql;
pub const eqlApprox = Shared.eqlApprox;
pub const format = Shared.format;
};
}

Expand Down Expand Up @@ -542,6 +547,24 @@ pub fn MatShared(comptime RowVec: type, comptime ColVec: type, comptime Matrix:
}
return true;
}

/// Custom format function for all matrix types.
pub inline fn format(
self: Matrix,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) @TypeOf(writer).Error!void {
const rows = @TypeOf(self).rows;
try writer.print("{{", .{});
inline for (0..rows) |r| {
try std.fmt.formatType(self.row(r), fmt, options, writer, 1);
if (r < rows - 1) {
try writer.print(", ", .{});
}
}
try writer.print("}}", .{});
}
};
}

Expand Down
13 changes: 13 additions & 0 deletions src/math/vec.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn Vec2(comptime Scalar: type) type {
pub const minScalar = Shared.minScalar;
pub const eqlApprox = Shared.eqlApprox;
pub const eql = Shared.eql;
pub const format = Shared.format;
};
}

Expand Down Expand Up @@ -190,6 +191,7 @@ pub fn Vec3(comptime Scalar: type) type {
pub const minScalar = Shared.minScalar;
pub const eqlApprox = Shared.eqlApprox;
pub const eql = Shared.eql;
pub const format = Shared.format;
};
}

Expand Down Expand Up @@ -269,6 +271,7 @@ pub fn Vec4(comptime Scalar: type) type {
pub const minScalar = Shared.minScalar;
pub const eqlApprox = Shared.eqlApprox;
pub const eql = Shared.eql;
pub const format = Shared.format;
};
}

Expand Down Expand Up @@ -519,6 +522,16 @@ pub fn VecShared(comptime Scalar: type, comptime VecN: type) type {
pub inline fn eql(a: *const VecN, b: *const VecN) bool {
return a.eqlApprox(b, math.eps(Scalar));
}

/// Custom format function for all vector types.
pub inline fn format(
self: VecN,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) @TypeOf(writer).Error!void {
try std.fmt.formatType(self.v, fmt, options, writer, 1);
}
};
}

Expand Down

0 comments on commit 6fd6a8f

Please sign in to comment.