const std = @import("std"); const Vector2Int = @import("Vector2Int.zig").Vector2Int; const Vector3 = @import("Vector3.zig").Vector3; pub const Vector3Int = extern struct { vector: Vector, pub const Vector = @Vector(3, i32); pub const Array = [3]i32; pub const Mask = enum(i32) { x = 0, y = 1, z = 2 }; pub const zero = Vector3Int.initScalar(0); pub const one = Vector3Int.initScalar(1); pub const one_frac = Vector3Int.initScalar(std.math.maxInt(i32)); pub const unit_x = Vector3Int.init(1, 0, 0); pub const unit_y = Vector3Int.init(0, 1, 0); pub const unit_z = Vector3Int.init(0, 0, 1); pub const unit_nx = Vector3Int.init(-1, 0, 0); pub const unit_ny = Vector3Int.init(0, -1, 0); pub const unit_nz = Vector3Int.init(0, 0, -1); pub const unit_x_frac = Vector3Int.init(std.math.maxInt(i32), 0, 0); pub const unit_y_frac = Vector3Int.init(0, std.math.maxInt(i32), 0); pub const unit_z_frac = Vector3Int.init(0, 0, std.math.maxInt(i32)); pub const unit_nx_frac = Vector3Int.init(-std.math.maxInt(i32), 0, 0); pub const unit_ny_frac = Vector3Int.init(0, -std.math.maxInt(i32), 0); pub const unit_nz_frac = Vector3Int.init(0, 0, -std.math.maxInt(i32)); // --- INIT --- pub inline fn initScalar(scalar: i32) Vector3Int { return .{ .vector = @splat(scalar) }; } pub inline fn init(x: i32, y: i32, z: i32) Vector3Int { return .{ .vector = .{ x, y, z } }; } pub inline fn initArray(array: Array) Vector3Int { return .{ .vector = array }; } // --- CONVERSION --- pub inline fn asArray(self: Vector3Int) Array { return self.vector; } pub inline fn asVector3(self: Vector3Int) Vector3 { return .{ .vector = @floatFromInt(self.vector) }; } pub inline fn asVector3Frac(self: Vector3Int) Vector3 { const numerator: @Vector(3, f64) = @floatFromInt(self.vector); const denominator: @Vector(3, f64) = @splat(std.math.maxInt(i32)); return .{ .vector = @floatCast(numerator / denominator) }; } pub inline fn asVector2Int(self: Vector3Int) Vector2Int { return .{ .vector = @shuffle(i32, self.vector, undefined, [_]i32{ 0, 1 }) }; } // --- ACCESSORS --- pub inline fn getX(self: Vector3Int) i32 { return self.vector[0]; } pub inline fn getY(self: Vector3Int) i32 { return self.vector[1]; } pub inline fn getZ(self: Vector3Int) i32 { return self.vector[2]; } pub inline fn setX(self: Vector3Int, x: i32) Vector3Int { const x_vector: Vector = @splat(x); return .{ .vector = @shuffle(i32, self.vector, x_vector, [_]i32{ ~@as(i32, 0), 1, 2 }) }; } pub inline fn setY(self: Vector3Int, y: i32) Vector3Int { const y_vector: Vector = @splat(y); return .{ .vector = @shuffle(i32, self.vector, y_vector, [_]i32{ 0, ~@as(i32, 1), 2 }) }; } pub inline fn setZ(self: Vector3Int, z: i32) Vector3Int { const z_vector: Vector = @splat(z); return .{ .vector = @shuffle(i32, self.vector, z_vector, [_]i32{ 0, 1, ~@as(i32, 2) }) }; } // --- COMPONENT-WISE --- pub inline fn add(self: Vector3Int, other: Vector3Int) Vector3Int { return .{ .vector = self.vector + other.vector }; } pub inline fn sub(self: Vector3Int, other: Vector3Int) Vector3Int { return .{ .vector = self.vector - other.vector }; } pub inline fn mul(self: Vector3Int, other: Vector3Int) Vector3Int { return .{ .vector = self.vector * other.vector }; } pub inline fn mulFrac(self: Vector3Int, other: Vector3Int) Vector3Int { const denominator: @Vector(3, i64) = @splat(std.math.maxInt(i32)); const rounding_bias: @Vector(3, i64) = denominator >> @as(@Vector(2, u6), @splat(1)); const self_wide: @Vector(3, i64) = self.vector; const other_wide: @Vector(3, i64) = other.vector; return .{ .vector = @intCast(@divFloor(self_wide * other_wide + rounding_bias, denominator)) }; } pub inline fn mulFracFrac(self: Vector3Int, other: Vector3Int) Vector3Int { const denominator: @Vector(3, i64) = @splat(std.math.maxInt(i32) * std.math.maxInt(i32)); const rounding_bias: @Vector(3, i64) = denominator >> @as(@Vector(2, u6), @splat(1)); const self_wide: @Vector(3, i64) = self.vector; const other_wide: @Vector(3, i64) = other.vector; return .{ .vector = @intCast(@divFloor(self_wide * other_wide + rounding_bias, denominator)) }; } pub inline fn div(self: Vector3Int, other: Vector3Int) Vector3Int { return .{ .vector = @divFloor(self.vector, other.vector) }; } pub inline fn mod(self: Vector3Int, other: Vector3Int) Vector3Int { return .{ .vector = @mod(self.vector, other.vector) }; } pub inline fn negate(self: Vector3Int) Vector3Int { return .{ .vector = -self.vector }; } pub inline fn mulScalar(self: Vector3Int, scalar: i32) Vector3Int { const scalar_vector: Vector = @splat(scalar); return .{ .vector = self.vector * scalar_vector }; } pub inline fn mulScalarFloat(self: Vector3Int, scalar: f32) Vector3Int { const self_float: @Vector(3, f64) = @floatFromInt(self.vector); const scalar_vector_float: @Vector(3, f64) = @splat(scalar); return .{ .vector = @intFromFloat(@round(self_float * scalar_vector_float)) }; } pub inline fn mulScalarFrac(self: Vector3Int, scalar: i32) Vector3Int { const denominator: @Vector(3, i64) = @splat(std.math.maxInt(i32)); const rounding_bias: @Vector(3, i64) = denominator >> @as(@Vector(3, u6), @splat(1)); const self_wide: @Vector(3, i64) = self; const scalar_wide: @Vector(3, i64) = @splat(scalar); return .{ .vector = @intCast(@divFloor(self_wide * scalar_wide + rounding_bias, denominator)) }; } pub inline fn mulScalarFracFrac(self: Vector3Int, scalar: i32) Vector3Int { const denominator: @Vector(3, i64) = @splat(std.math.maxInt(i32) * std.math.maxInt(i32)); const rounding_bias: @Vector(3, i64) = denominator >> @as(@Vector(3, u6), @splat(1)); const self_wide: @Vector(3, i64) = self; const scalar_wide: @Vector(3, i64) = @splat(scalar); return .{ .vector = @intCast(@divFloor(self_wide * scalar_wide + rounding_bias, denominator)) }; } pub inline fn divScalar(self: Vector3Int, scalar: i32) Vector3Int { const scalar_vector: Vector = @splat(scalar); return .{ .vector = @divFloor(self.vector, scalar_vector) }; } pub inline fn divScalarFloat(self: Vector3Int, scalar: f32) Vector3Int { const self_float: @Vector(3, f64) = @floatFromInt(self.vector); const scalar_vector_float: @Vector(3, f64) = @splat(scalar); return .{ .vector = @intFromFloat(@round(self_float / scalar_vector_float)) }; } pub inline fn modScalar(self: Vector3Int, scalar: i32) Vector3Int { const scalar_vector: Vector = @splat(scalar); return .{ .vector = @mod(self.vector, scalar_vector) }; } // --- SWIZZLE --- pub inline fn swizzle2(self: Vector3Int, comptime mask: [2]Mask) Vector2Int { return .{ .vector = @shuffle(f32, self.vector, undefined, @as([2]i32, @bitCast(mask))) }; } pub inline fn swizzle3(self: Vector3Int, comptime mask: [3]Mask) Vector3Int { return .{ .vector = @shuffle(f32, self.vector, undefined, @as([3]i32, @bitCast(mask))) }; } // --- OTHER --- pub inline fn lenSquared(self: Vector3Int) i64 { const self_wide: @Vector(2, i64) = self.vector; return @reduce(.Add, self_wide * self_wide); } pub inline fn dot(self: Vector3Int, other: Vector3Int) i64 { const self_wide: @Vector(3, i64) = self.vector; const other_wide: @Vector(3, i64) = other.vector; return @reduce(.Add, self_wide * other_wide); } };