From 34f0b1fb893ddaa8d8cefb81c49c4a551589c51d Mon Sep 17 00:00:00 2001 From: Szymon Nowakowski Date: Tue, 6 Jan 2026 21:14:12 +0100 Subject: [PATCH] Vector int-float conversion methods --- packages/vecmath/src/colors/Color.zig | 8 ++++---- packages/vecmath/src/vectors/Vector2.zig | 4 ++++ packages/vecmath/src/vectors/Vector2Int.zig | 4 ++++ packages/vecmath/src/vectors/Vector2Int_x8.zig | 4 ++++ packages/vecmath/src/vectors/Vector2x8.zig | 4 ++++ packages/vecmath/src/vectors/Vector3.zig | 6 +++++- packages/vecmath/src/vectors/Vector3Int.zig | 4 ++++ packages/vecmath/src/vectors/Vector3Int_x8.zig | 4 ++++ packages/vecmath/src/vectors/Vector3x8.zig | 4 ++++ packages/vecmath/src/vectors/Vector4.zig | 4 ++++ packages/vecmath/src/vectors/Vector4Int.zig | 4 ++++ packages/vecmath/src/vectors/Vector4Int_x8.zig | 4 ++++ packages/vecmath/src/vectors/Vector4x8.zig | 4 ++++ 13 files changed, 53 insertions(+), 5 deletions(-) diff --git a/packages/vecmath/src/colors/Color.zig b/packages/vecmath/src/colors/Color.zig index 15323f9..4536aee 100644 --- a/packages/vecmath/src/colors/Color.zig +++ b/packages/vecmath/src/colors/Color.zig @@ -64,25 +64,25 @@ pub const Color = extern struct { } test l { - const i: Color = .l("#012"); + const i = Color.l("#012"); try std.testing.expectEqual(0x00, i.r); try std.testing.expectEqual(0x11, i.g); try std.testing.expectEqual(0x22, i.b); try std.testing.expectEqual(0xFF, i.a); - const j: Color = .l("#3456"); + const j = Color.l("#3456"); try std.testing.expectEqual(0x33, j.r); try std.testing.expectEqual(0x44, j.g); try std.testing.expectEqual(0x55, j.b); try std.testing.expectEqual(0x66, j.a); - const k: Color = .l("#F08040"); + const k = Color.l("#F08040"); try std.testing.expectEqual(0xF0, k.r); try std.testing.expectEqual(0x80, k.g); try std.testing.expectEqual(0x40, k.b); try std.testing.expectEqual(0xFF, k.a); - const m: Color = .l("#20304050"); + const m = Color.l("#20304050"); try std.testing.expectEqual(0x20, m.r); try std.testing.expectEqual(0x30, m.g); try std.testing.expectEqual(0x40, m.b); diff --git a/packages/vecmath/src/vectors/Vector2.zig b/packages/vecmath/src/vectors/Vector2.zig index 5776211..2187dcc 100644 --- a/packages/vecmath/src/vectors/Vector2.zig +++ b/packages/vecmath/src/vectors/Vector2.zig @@ -34,6 +34,10 @@ pub const Vector2 = extern struct { return @bitCast(self); } + pub inline fn asInt(self: Vector2) vm.Vector2Int { + return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y) }; + } + pub inline fn withZ(self: Vector2, z: f32) vm.Vector3 { return .{ .x = self.x, .y = self.y, .z = z }; } diff --git a/packages/vecmath/src/vectors/Vector2Int.zig b/packages/vecmath/src/vectors/Vector2Int.zig index 05b58e7..cc7095c 100644 --- a/packages/vecmath/src/vectors/Vector2Int.zig +++ b/packages/vecmath/src/vectors/Vector2Int.zig @@ -34,6 +34,10 @@ pub const Vector2Int = extern struct { return @bitCast(self); } + pub inline fn asFloat(self: Vector2Int) vm.Vector2 { + return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y) }; + } + pub inline fn withZ(self: Vector2Int, z: i32) vm.Vector3Int { return .{ .x = self.x, .y = self.y, .z = z }; } diff --git a/packages/vecmath/src/vectors/Vector2Int_x8.zig b/packages/vecmath/src/vectors/Vector2Int_x8.zig index d81af91..2c7f93e 100644 --- a/packages/vecmath/src/vectors/Vector2Int_x8.zig +++ b/packages/vecmath/src/vectors/Vector2Int_x8.zig @@ -44,6 +44,10 @@ pub const Vector2Int_x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn asFloat(self: Vector2Int_x8) vm.Vector2x8 { + return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y) }; + } + pub inline fn withZ(self: Vector2Int_x8, z: vm.i32x8) vm.Vector3Int_x8 { return .{ .x = self.x, .y = self.y, .z = z }; } diff --git a/packages/vecmath/src/vectors/Vector2x8.zig b/packages/vecmath/src/vectors/Vector2x8.zig index 7a2d837..ac9c4fb 100644 --- a/packages/vecmath/src/vectors/Vector2x8.zig +++ b/packages/vecmath/src/vectors/Vector2x8.zig @@ -44,6 +44,10 @@ pub const Vector2x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn asInt(self: Vector2x8) vm.Vector2Int_x8 { + return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y) }; + } + pub inline fn withZ(self: Vector2x8, z: vm.f32x8) vm.Vector3x8 { return .{ .x = self.x, .y = self.y, .z = z }; } diff --git a/packages/vecmath/src/vectors/Vector3.zig b/packages/vecmath/src/vectors/Vector3.zig index f9ad8a3..43746cd 100644 --- a/packages/vecmath/src/vectors/Vector3.zig +++ b/packages/vecmath/src/vectors/Vector3.zig @@ -37,6 +37,10 @@ pub const Vector3 = extern struct { return @bitCast(self); } + pub inline fn asInt(self: Vector3) vm.Vector3Int { + return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y), .z = @intFromFloat(self.z) }; + } + pub inline fn dropZ(self: Vector3) vm.Vector2 { return .{ .x = self.x, .y = self.y }; } @@ -145,7 +149,7 @@ pub const Vector3 = extern struct { pub inline fn rotate_x8(self: Vector3, quaternion: vm.Quaternion_x8) vm.Vector3x8 { const w = quaternion.getScalar(); const xyz = quaternion.getVector(); - const self_x8: vm.Vector3x8 = .splat(self); + const self_x8 = vm.Vector3x8.splat(self); return .add( self_x8, diff --git a/packages/vecmath/src/vectors/Vector3Int.zig b/packages/vecmath/src/vectors/Vector3Int.zig index 27fc9d1..110a953 100644 --- a/packages/vecmath/src/vectors/Vector3Int.zig +++ b/packages/vecmath/src/vectors/Vector3Int.zig @@ -37,6 +37,10 @@ pub const Vector3Int = extern struct { return @bitCast(self); } + pub inline fn asFloat(self: Vector3Int) vm.Vector3 { + return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y), .z = @floatFromInt(self.z) }; + } + pub inline fn dropZ(self: Vector3Int) vm.Vector2Int { return .{ .x = self.x, .y = self.y }; } diff --git a/packages/vecmath/src/vectors/Vector3Int_x8.zig b/packages/vecmath/src/vectors/Vector3Int_x8.zig index 91a97f2..e44f737 100644 --- a/packages/vecmath/src/vectors/Vector3Int_x8.zig +++ b/packages/vecmath/src/vectors/Vector3Int_x8.zig @@ -48,6 +48,10 @@ pub const Vector3Int_x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn asFloat(self: Vector3Int_x8) vm.Vector3x8 { + return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y), .z = @floatFromInt(self.z) }; + } + pub inline fn dropZ(self: Vector3Int_x8) vm.Vector2Int_x8 { return .{ .x = self.x, .y = self.y }; } diff --git a/packages/vecmath/src/vectors/Vector3x8.zig b/packages/vecmath/src/vectors/Vector3x8.zig index afac0b7..3949015 100644 --- a/packages/vecmath/src/vectors/Vector3x8.zig +++ b/packages/vecmath/src/vectors/Vector3x8.zig @@ -48,6 +48,10 @@ pub const Vector3x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn asInt(self: Vector3x8) vm.Vector3Int_x8 { + return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y), .z = @intFromFloat(self.z) }; + } + pub inline fn dropZ(self: Vector3x8) vm.Vector2x8 { return .{ .x = self.x, .y = self.y }; } diff --git a/packages/vecmath/src/vectors/Vector4.zig b/packages/vecmath/src/vectors/Vector4.zig index 8d6fb11..40ebecf 100644 --- a/packages/vecmath/src/vectors/Vector4.zig +++ b/packages/vecmath/src/vectors/Vector4.zig @@ -36,6 +36,10 @@ pub const Vector4 = extern struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn asInt(self: Vector4) vm.Vector4Int { + return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y), .z = @intFromFloat(self.z), .w = @intFromFloat(self.w) }; + } + pub inline fn asArray(self: Vector4) Array { return @bitCast(self); } diff --git a/packages/vecmath/src/vectors/Vector4Int.zig b/packages/vecmath/src/vectors/Vector4Int.zig index a2a24f3..39e39b1 100644 --- a/packages/vecmath/src/vectors/Vector4Int.zig +++ b/packages/vecmath/src/vectors/Vector4Int.zig @@ -40,6 +40,10 @@ pub const Vector4Int = extern struct { return @bitCast(self); } + pub inline fn asFloat(self: Vector4Int) vm.Vector4 { + return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y), .z = @floatFromInt(self.z), .w = @floatFromInt(self.w) }; + } + pub inline fn dropW(self: Vector4Int) vm.Vector3Int { return .{ .x = self.x, .y = self.y, .z = self.z }; } diff --git a/packages/vecmath/src/vectors/Vector4Int_x8.zig b/packages/vecmath/src/vectors/Vector4Int_x8.zig index a5dd8c0..092d97d 100644 --- a/packages/vecmath/src/vectors/Vector4Int_x8.zig +++ b/packages/vecmath/src/vectors/Vector4Int_x8.zig @@ -52,6 +52,10 @@ pub const Vector4Int_x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn asFloat(self: Vector4Int_x8) vm.Vector4x8 { + return .{ .x = @floatFromInt(self.x), .y = @floatFromInt(self.y), .z = @floatFromInt(self.z), .w = @floatFromInt(self.w) }; + } + pub inline fn dropW(self: Vector4Int_x8) vm.Vector3Int_x8 { return .{ .x = self.x, .y = self.y, .z = self.z }; } diff --git a/packages/vecmath/src/vectors/Vector4x8.zig b/packages/vecmath/src/vectors/Vector4x8.zig index d9458ef..c50a5f3 100644 --- a/packages/vecmath/src/vectors/Vector4x8.zig +++ b/packages/vecmath/src/vectors/Vector4x8.zig @@ -52,6 +52,10 @@ pub const Vector4x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn asInt(self: Vector4x8) vm.Vector4Int_x8 { + return .{ .x = @intFromFloat(self.x), .y = @intFromFloat(self.y), .z = @intFromFloat(self.z), .w = @intFromFloat(self.w) }; + } + pub inline fn dropW(self: Vector4x8) vm.Vector3x8 { return .{ .x = self.x, .y = self.y, .z = self.z }; }