Skip to content

Commit 6fd6a8f

Browse files
msg-programsemidoots
authored andcommitted
Add format function for vector and matrix types.
1 parent a812370 commit 6fd6a8f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/math/mat.zig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const std = @import("std");
2+
13
const mach = @import("../main.zig");
24
const testing = mach.testing;
35
const math = mach.math;
@@ -118,6 +120,7 @@ pub fn Mat2x2(
118120

119121
pub const mul = Shared.mul;
120122
pub const mulVec = Shared.mulVec;
123+
pub const format = Shared.format;
121124
};
122125
}
123126

@@ -258,6 +261,7 @@ pub fn Mat3x3(
258261

259262
pub const mul = Shared.mul;
260263
pub const mulVec = Shared.mulVec;
264+
pub const format = Shared.format;
261265
};
262266
}
263267

@@ -485,6 +489,7 @@ pub fn Mat4x4(
485489
pub const mulVec = Shared.mulVec;
486490
pub const eql = Shared.eql;
487491
pub const eqlApprox = Shared.eqlApprox;
492+
pub const format = Shared.format;
488493
};
489494
}
490495

@@ -542,6 +547,24 @@ pub fn MatShared(comptime RowVec: type, comptime ColVec: type, comptime Matrix:
542547
}
543548
return true;
544549
}
550+
551+
/// Custom format function for all matrix types.
552+
pub inline fn format(
553+
self: Matrix,
554+
comptime fmt: []const u8,
555+
options: std.fmt.FormatOptions,
556+
writer: anytype,
557+
) @TypeOf(writer).Error!void {
558+
const rows = @TypeOf(self).rows;
559+
try writer.print("{{", .{});
560+
inline for (0..rows) |r| {
561+
try std.fmt.formatType(self.row(r), fmt, options, writer, 1);
562+
if (r < rows - 1) {
563+
try writer.print(", ", .{});
564+
}
565+
}
566+
try writer.print("}}", .{});
567+
}
545568
};
546569
}
547570

src/math/vec.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub fn Vec2(comptime Scalar: type) type {
6767
pub const minScalar = Shared.minScalar;
6868
pub const eqlApprox = Shared.eqlApprox;
6969
pub const eql = Shared.eql;
70+
pub const format = Shared.format;
7071
};
7172
}
7273

@@ -190,6 +191,7 @@ pub fn Vec3(comptime Scalar: type) type {
190191
pub const minScalar = Shared.minScalar;
191192
pub const eqlApprox = Shared.eqlApprox;
192193
pub const eql = Shared.eql;
194+
pub const format = Shared.format;
193195
};
194196
}
195197

@@ -269,6 +271,7 @@ pub fn Vec4(comptime Scalar: type) type {
269271
pub const minScalar = Shared.minScalar;
270272
pub const eqlApprox = Shared.eqlApprox;
271273
pub const eql = Shared.eql;
274+
pub const format = Shared.format;
272275
};
273276
}
274277

@@ -519,6 +522,16 @@ pub fn VecShared(comptime Scalar: type, comptime VecN: type) type {
519522
pub inline fn eql(a: *const VecN, b: *const VecN) bool {
520523
return a.eqlApprox(b, math.eps(Scalar));
521524
}
525+
526+
/// Custom format function for all vector types.
527+
pub inline fn format(
528+
self: VecN,
529+
comptime fmt: []const u8,
530+
options: std.fmt.FormatOptions,
531+
writer: anytype,
532+
) @TypeOf(writer).Error!void {
533+
try std.fmt.formatType(self.v, fmt, options, writer, 1);
534+
}
522535
};
523536
}
524537

0 commit comments

Comments
 (0)