116 lines
4.0 KiB
Zig
116 lines
4.0 KiB
Zig
const std = @import("std");
|
|
const vm = @import("../root.zig");
|
|
|
|
pub const Vector3Int = extern struct {
|
|
x: i32,
|
|
y: i32,
|
|
z: i32,
|
|
|
|
pub const Array = [3]i32;
|
|
|
|
pub const zero = initScalar(0);
|
|
pub const one = initScalar(1);
|
|
pub const unit_x = init(1, 0, 0);
|
|
pub const unit_y = init(0, 1, 0);
|
|
pub const unit_z = init(0, 0, 1);
|
|
pub const unit_nx = init(-1, 0, 0);
|
|
pub const unit_ny = init(0, -1, 0);
|
|
pub const unit_nz = init(0, 0, -1);
|
|
|
|
// --- INIT ----------------------------------------------------------------
|
|
|
|
pub inline fn init(x: i32, y: i32, z: i32) Vector3Int {
|
|
return .{ .x = x, .y = y, .z = z };
|
|
}
|
|
|
|
pub inline fn initScalar(scalar: i32) Vector3Int {
|
|
return .{ .x = scalar, .y = scalar, .z = scalar };
|
|
}
|
|
|
|
pub inline fn initArray(array: Array) Vector3Int {
|
|
return @bitCast(array);
|
|
}
|
|
|
|
// --- CONVERSION ----------------------------------------------------------
|
|
|
|
pub inline fn asArray(self: Vector3Int) Array {
|
|
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 {
|
|
return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z };
|
|
}
|
|
|
|
pub inline fn sub(self: Vector3Int, other: Vector3Int) Vector3Int {
|
|
return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z };
|
|
}
|
|
|
|
pub inline fn mul(self: Vector3Int, other: Vector3Int) Vector3Int {
|
|
return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z };
|
|
}
|
|
|
|
pub inline fn mulScalar(self: Vector3Int, scalar: i32) Vector3Int {
|
|
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar };
|
|
}
|
|
|
|
pub inline fn div(self: Vector3Int, other: Vector3Int) Vector3Int {
|
|
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: i32) Vector3Int {
|
|
return .{ .x = @divFloor(self.x, scalar), .y = @divFloor(self.y, scalar), .z = @divFloor(self.z, scalar) };
|
|
}
|
|
|
|
pub inline fn mod(self: Vector3Int, other: Vector3Int) Vector3Int {
|
|
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: i32) Vector3Int {
|
|
return .{ .x = @mod(self.x, scalar), .y = @mod(self.y, scalar), .z = @mod(self.z, scalar) };
|
|
}
|
|
|
|
pub inline fn negate(self: Vector3Int) Vector3Int {
|
|
return .{ .x = -self.x, .y = -self.y, .z = -self.z };
|
|
}
|
|
|
|
pub inline fn abs(self: Vector3Int) Vector3Int {
|
|
return .{ .x = @intCast(@abs(self.x)), .y = @intCast(@abs(self.y)), .z = @intCast(@abs(self.z)) };
|
|
}
|
|
|
|
pub inline fn min(self: Vector3Int, other: Vector3Int) Vector3Int {
|
|
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) };
|
|
}
|
|
|
|
pub inline fn max(self: Vector3Int, other: Vector3Int) Vector3Int {
|
|
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) };
|
|
}
|
|
|
|
// --- OTHER ---------------------------------------------------------------
|
|
|
|
pub inline fn lenSquared(self: Vector3Int) i32 {
|
|
return self.x * self.x + self.y * self.y + self.z * self.z;
|
|
}
|
|
|
|
pub inline fn dot(self: Vector3Int, other: Vector3Int) i32 {
|
|
return self.x * other.x + self.y * other.y + self.z * other.z;
|
|
}
|
|
|
|
pub inline fn cross(self: Vector3Int, other: Vector3Int) Vector3Int {
|
|
return .{
|
|
.x = self.y * other.z - self.z * other.y,
|
|
.y = self.z * other.x - self.x * other.z,
|
|
.z = self.x * other.y - self.y * other.x,
|
|
};
|
|
}
|
|
};
|