Vector int-float conversion methods

This commit is contained in:
2026-01-06 21:14:12 +01:00
parent 7963813034
commit 34f0b1fb89
13 changed files with 53 additions and 5 deletions

View File

@@ -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);

View File

@@ -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 };
}

View File

@@ -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 };
}

View File

@@ -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 };
}

View File

@@ -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 };
}

View File

@@ -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,

View File

@@ -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 };
}

View File

@@ -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 };
}

View File

@@ -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 };
}

View File

@@ -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);
}

View File

@@ -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 };
}

View File

@@ -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 };
}

View File

@@ -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 };
}