From 7e8103565d2d0eaeac8510b62dba59b6363c81cc Mon Sep 17 00:00:00 2001 From: Szymon Nowakowski Date: Sun, 4 Jan 2026 17:13:31 +0100 Subject: [PATCH] Add widening/shortening vector dimensions --- packages/vecmath/src/vectors/Vector2.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector2Int.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector2Int_x8.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector2x8.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector3.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector3Int.zig | 14 +++++++++++--- packages/vecmath/src/vectors/Vector3Int_x8.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector3x8.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector4.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector4Int.zig | 14 +++++++++++--- packages/vecmath/src/vectors/Vector4Int_x8.zig | 8 ++++++++ packages/vecmath/src/vectors/Vector4x8.zig | 8 ++++++++ 12 files changed, 102 insertions(+), 6 deletions(-) diff --git a/packages/vecmath/src/vectors/Vector2.zig b/packages/vecmath/src/vectors/Vector2.zig index d58f984..f7fa2e1 100644 --- a/packages/vecmath/src/vectors/Vector2.zig +++ b/packages/vecmath/src/vectors/Vector2.zig @@ -34,6 +34,14 @@ pub const Vector2 = extern struct { return @bitCast(self); } + pub inline fn withZ(self: Vector2, z: f32) vm.Vector3 { + return .{ .x = self.x, .y = self.y, .z = z }; + } + + pub inline fn withZW(self: Vector2, z: f32, w: f32) vm.Vector4 { + return .{ .x = self.x, .y = self.y, .z = z, .w = w }; + } + // --- COMPONENT-WISE ------------------------------------------------------ pub inline fn add(self: Vector2, other: Vector2) Vector2 { diff --git a/packages/vecmath/src/vectors/Vector2Int.zig b/packages/vecmath/src/vectors/Vector2Int.zig index 71d59ac..40a9bb4 100644 --- a/packages/vecmath/src/vectors/Vector2Int.zig +++ b/packages/vecmath/src/vectors/Vector2Int.zig @@ -34,6 +34,14 @@ pub const Vector2Int = extern struct { return @bitCast(self); } + pub inline fn withZ(self: Vector2Int, z: i32) vm.Vector3Int { + return .{ .x = self.x, .y = self.y, .z = z }; + } + + pub inline fn withZW(self: Vector2Int, z: i32, w: i32) vm.Vector4Int { + return .{ .x = self.x, .y = self.y, .z = z, .w = w }; + } + // --- COMPONENT-WISE ------------------------------------------------------ pub inline fn add(self: Vector2Int, other: Vector2Int) Vector2Int { diff --git a/packages/vecmath/src/vectors/Vector2Int_x8.zig b/packages/vecmath/src/vectors/Vector2Int_x8.zig index 9387385..d81af91 100644 --- a/packages/vecmath/src/vectors/Vector2Int_x8.zig +++ b/packages/vecmath/src/vectors/Vector2Int_x8.zig @@ -44,6 +44,14 @@ pub const Vector2Int_x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn withZ(self: Vector2Int_x8, z: vm.i32x8) vm.Vector3Int_x8 { + return .{ .x = self.x, .y = self.y, .z = z }; + } + + pub inline fn withZW(self: Vector2Int_x8, z: vm.i32x8, w: vm.i32x8) vm.Vector4Int_x8 { + return .{ .x = self.x, .y = self.y, .z = z, .w = w }; + } + pub inline fn asArrayOfVectors(self: Vector2Int_x8) [8]vm.Vector2Int { const vector: @Vector(16, i32) = self.x ++ self.y; return @bitCast(@as([16]i32, @shuffle(i32, vector, undefined, [_]i32{ diff --git a/packages/vecmath/src/vectors/Vector2x8.zig b/packages/vecmath/src/vectors/Vector2x8.zig index 82b2698..e0faee6 100644 --- a/packages/vecmath/src/vectors/Vector2x8.zig +++ b/packages/vecmath/src/vectors/Vector2x8.zig @@ -44,6 +44,14 @@ pub const Vector2x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn withZ(self: Vector2x8, z: vm.f32x8) vm.Vector3x8 { + return .{ .x = self.x, .y = self.y, .z = z }; + } + + pub inline fn withZW(self: Vector2x8, z: vm.f32x8, w: vm.f32x8) vm.Vector4x8 { + return .{ .x = self.x, .y = self.y, .z = z, .w = w }; + } + pub inline fn asArrayOfVectors(self: Vector2x8) [8]vm.Vector2 { const vector: @Vector(16, f32) = self.x ++ self.y; return @bitCast(@as([16]f32, @shuffle(f32, vector, undefined, [_]i32{ diff --git a/packages/vecmath/src/vectors/Vector3.zig b/packages/vecmath/src/vectors/Vector3.zig index a12a8f4..68cf51b 100644 --- a/packages/vecmath/src/vectors/Vector3.zig +++ b/packages/vecmath/src/vectors/Vector3.zig @@ -37,6 +37,14 @@ pub const Vector3 = extern struct { return @bitCast(self); } + pub inline fn dropZ(self: Vector3) vm.Vector2 { + return .{ .x = self.x, .y = self.y }; + } + + pub inline fn withW(self: Vector3, w: f32) vm.Vector4 { + return .{ .x = self.x, .y = self.y, .z = self.z, .w = w }; + } + // --- COMPONENT-WISE ------------------------------------------------------ pub inline fn add(self: Vector3, other: Vector3) Vector3 { diff --git a/packages/vecmath/src/vectors/Vector3Int.zig b/packages/vecmath/src/vectors/Vector3Int.zig index 2d4bfb4..26d0db6 100644 --- a/packages/vecmath/src/vectors/Vector3Int.zig +++ b/packages/vecmath/src/vectors/Vector3Int.zig @@ -37,6 +37,14 @@ pub const Vector3Int = extern struct { return @bitCast(self); } + pub inline fn dropZ(self: Vector3Int) vm.Vector2Int { + return .{ .x = self.x, .y = self.y }; + } + + pub inline fn withW(self: Vector3Int, w: i32) vm.Vector4Int { + return .{ .x = self.x, .y = self.y, .z = self.z, .w = w }; + } + // --- COMPONENT-WISE ------------------------------------------------------ pub inline fn add(self: Vector3Int, other: Vector3Int) Vector3Int { @@ -51,7 +59,7 @@ pub const Vector3Int = extern struct { return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z }; } - pub inline fn mulScalar(self: Vector3Int, scalar: f32) Vector3Int { + pub inline fn mulScalar(self: Vector3Int, scalar: i32) Vector3Int { return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar }; } @@ -59,7 +67,7 @@ pub const Vector3Int = extern struct { return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y), .z = @divFloor(self.z, other.z) }; } - pub inline fn divScalar(self: Vector3Int, scalar: f32) Vector3Int { + pub inline fn divScalar(self: Vector3Int, scalar: i32) Vector3Int { return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar) }; } @@ -67,7 +75,7 @@ pub const Vector3Int = extern struct { return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y), .z = @mod(self.z, other.z) }; } - pub inline fn modScalar(self: Vector3Int, scalar: f32) Vector3Int { + pub inline fn modScalar(self: Vector3Int, scalar: i32) Vector3Int { return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar) }; } diff --git a/packages/vecmath/src/vectors/Vector3Int_x8.zig b/packages/vecmath/src/vectors/Vector3Int_x8.zig index 0a8e181..91a97f2 100644 --- a/packages/vecmath/src/vectors/Vector3Int_x8.zig +++ b/packages/vecmath/src/vectors/Vector3Int_x8.zig @@ -48,6 +48,14 @@ pub const Vector3Int_x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn dropZ(self: Vector3Int_x8) vm.Vector2Int_x8 { + return .{ .x = self.x, .y = self.y }; + } + + pub inline fn withW(self: Vector3Int_x8, w: vm.i32x8) vm.Vector4Int_x8 { + return .{ .x = self.x, .y = self.y, .z = self.z, .w = w }; + } + pub inline fn asArrayOfVectors(self: Vector3Int_x8) [8]vm.Vector3Int { const vector: @Vector(24, i32) = self.x ++ self.y ++ self.z; return @bitCast(@as([16]i32, @shuffle(i32, vector, undefined, [_]i32{ diff --git a/packages/vecmath/src/vectors/Vector3x8.zig b/packages/vecmath/src/vectors/Vector3x8.zig index f66bcf2..f8bbaa9 100644 --- a/packages/vecmath/src/vectors/Vector3x8.zig +++ b/packages/vecmath/src/vectors/Vector3x8.zig @@ -48,6 +48,14 @@ pub const Vector3x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn dropZ(self: Vector3x8) vm.Vector2x8 { + return .{ .x = self.x, .y = self.y }; + } + + pub inline fn withW(self: Vector3x8, w: vm.f32x8) vm.Vector4x8 { + return .{ .x = self.x, .y = self.y, .z = self.z, .w = w }; + } + pub inline fn asArrayOfVectors(self: Vector3x8) [8]vm.Vector3 { const vector: @Vector(24, f32) = self.x ++ self.y ++ self.z; return @bitCast(@as([16]f32, @shuffle(f32, vector, undefined, [_]i32{ diff --git a/packages/vecmath/src/vectors/Vector4.zig b/packages/vecmath/src/vectors/Vector4.zig index 96f0e95..58b5881 100644 --- a/packages/vecmath/src/vectors/Vector4.zig +++ b/packages/vecmath/src/vectors/Vector4.zig @@ -40,6 +40,14 @@ pub const Vector4 = extern struct { return @bitCast(self); } + pub inline fn dropW(self: Vector4) vm.Vector3 { + return .{ .x = self.x, .y = self.y, .z = self.z }; + } + + pub inline fn dropZW(self: Vector4) vm.Vector2 { + return .{ .x = self.x, .y = self.y }; + } + // --- COMPONENT-WISE ------------------------------------------------------ pub inline fn add(self: Vector4, other: Vector4) Vector4 { diff --git a/packages/vecmath/src/vectors/Vector4Int.zig b/packages/vecmath/src/vectors/Vector4Int.zig index 2faad11..9721171 100644 --- a/packages/vecmath/src/vectors/Vector4Int.zig +++ b/packages/vecmath/src/vectors/Vector4Int.zig @@ -40,6 +40,14 @@ pub const Vector4Int = extern struct { return @bitCast(self); } + pub inline fn dropW(self: Vector4Int) vm.Vector3Int { + return .{ .x = self.x, .y = self.y, .z = self.z }; + } + + pub inline fn dropZW(self: Vector4Int) vm.Vector2Int { + return .{ .x = self.x, .y = self.y }; + } + // --- COMPONENT-WISE ------------------------------------------------------ pub inline fn add(self: Vector4Int, other: Vector4Int) Vector4Int { @@ -54,7 +62,7 @@ pub const Vector4Int = extern struct { return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z, .w = self.w * other.w }; } - pub inline fn mulScalar(self: Vector4Int, scalar: f32) Vector4Int { + pub inline fn mulScalar(self: Vector4Int, scalar: i32) Vector4Int { return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar }; } @@ -62,7 +70,7 @@ pub const Vector4Int = extern struct { return .{ .x = @divFloor(self.x, other.x), .y = @divFloor(self.y, other.y), .z = @divFloor(self.z, other.z), .w = @divFloor(self.w, other.w) }; } - pub inline fn divScalar(self: Vector4Int, scalar: f32) Vector4Int { + pub inline fn divScalar(self: Vector4Int, scalar: i32) Vector4Int { return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar), .w = @divFloor(self.w, scalar) }; } @@ -70,7 +78,7 @@ pub const Vector4Int = extern struct { return .{ .x = @mod(self.x, other.x), .y = @mod(self.y, other.y), .z = @mod(self.z, other.z), .w = @mod(self.w, other.w) }; } - pub inline fn modScalar(self: Vector4Int, scalar: f32) Vector4Int { + pub inline fn modScalar(self: Vector4Int, scalar: i32) Vector4Int { return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar), .w = @mod(self.w, scalar) }; } diff --git a/packages/vecmath/src/vectors/Vector4Int_x8.zig b/packages/vecmath/src/vectors/Vector4Int_x8.zig index e1a58b0..a5dd8c0 100644 --- a/packages/vecmath/src/vectors/Vector4Int_x8.zig +++ b/packages/vecmath/src/vectors/Vector4Int_x8.zig @@ -52,6 +52,14 @@ pub const Vector4Int_x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn dropW(self: Vector4Int_x8) vm.Vector3Int_x8 { + return .{ .x = self.x, .y = self.y, .z = self.z }; + } + + pub inline fn dropZW(self: Vector4Int_x8) vm.Vector2Int_x8 { + return .{ .x = self.x, .y = self.y }; + } + pub inline fn asArrayOfVectors(self: Vector4Int_x8) [8]vm.Vector4 { const vector: @Vector(32, i32) = self.x ++ self.y ++ self.z ++ self.w; return @bitCast(@as([32]i32, @shuffle(i32, vector, undefined, [_]i32{ diff --git a/packages/vecmath/src/vectors/Vector4x8.zig b/packages/vecmath/src/vectors/Vector4x8.zig index 2f889b5..543988c 100644 --- a/packages/vecmath/src/vectors/Vector4x8.zig +++ b/packages/vecmath/src/vectors/Vector4x8.zig @@ -52,6 +52,14 @@ pub const Vector4x8 = struct { // --- CONVERSION ---------------------------------------------------------- + pub inline fn dropW(self: Vector4x8) vm.Vector3x8 { + return .{ .x = self.x, .y = self.y, .z = self.z }; + } + + pub inline fn dropZW(self: Vector4x8) vm.Vector2x8 { + return .{ .x = self.x, .y = self.y }; + } + pub inline fn asArrayOfVectors(self: Vector4x8) [8]vm.Vector4 { const vector: @Vector(32, f32) = self.x ++ self.y ++ self.z ++ self.w; return @bitCast(@as([32]f32, @shuffle(f32, vector, undefined, [_]i32{