const std = @import("std"); const vm = @import("../root.zig"); pub const Vector4 = extern struct { x: f32, y: f32, z: f32, w: f32, pub const Array = [4]f32; pub const zero = initScalar(0); pub const one = initScalar(1); pub const unit_x = init(1, 0, 0, 0); pub const unit_y = init(0, 1, 0, 0); pub const unit_z = init(0, 0, 1, 0); pub const unit_w = init(0, 0, 0, 1); pub const unit_nx = init(-1, 0, 0, 0); pub const unit_ny = init(0, -1, 0, 0); pub const unit_nz = init(0, 0, -1, 0); pub const unit_nw = init(0, 0, 0, -1); // --- INIT ---------------------------------------------------------------- pub inline fn init(x: f32, y: f32, z: f32, w: f32) Vector4 { return .{ .x = x, .y = y, .z = z, .w = w }; } pub inline fn initScalar(scalar: f32) Vector4 { return .{ .x = scalar, .y = scalar, .z = scalar, .w = scalar }; } pub inline fn initArray(array: Array) Vector4 { return @bitCast(array); } // --- CONVERSION ---------------------------------------------------------- pub inline fn asArray(self: Vector4) Array { return @bitCast(self); } // --- COMPONENT-WISE ------------------------------------------------------ pub inline fn add(self: Vector4, other: Vector4) Vector4 { return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z, .w = self.w + other.w }; } pub inline fn sub(self: Vector4, other: Vector4) Vector4 { return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z, .w = self.w - other.w }; } pub inline fn mul(self: Vector4, other: Vector4) Vector4 { 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: Vector4, scalar: f32) Vector4 { return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar, .w = self.w * scalar }; } pub inline fn div(self: Vector4, other: Vector4) Vector4 { return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z, .w = self.w / other.w }; } pub inline fn divScalar(self: Vector4, scalar: f32) Vector4 { return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar, .w = self.w / scalar }; } pub inline fn negate(self: Vector4) Vector4 { return .{ .x = -self.x, .y = -self.y, .z = -self.z, .w = -self.w }; } pub inline fn abs(self: Vector4) Vector4 { return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z), .w = @abs(self.w) }; } pub inline fn floor(self: Vector4) Vector4 { return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z), .w = @floor(self.w) }; } pub inline fn ceil(self: Vector4) Vector4 { return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z), .w = @ceil(self.w) }; } pub inline fn round(self: Vector4) Vector4 { return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z), .w = @round(self.w) }; } pub inline fn min(self: Vector4, other: Vector4) Vector4 { return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z), .w = @min(self.w, other.w) }; } pub inline fn max(self: Vector4, other: Vector4) Vector4 { return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z), .w = @max(self.w, other.w) }; } // --- OTHER --------------------------------------------------------------- pub inline fn len(self: Vector4) f32 { return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w); } pub inline fn lenSquared(self: Vector4) f32 { return self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w; } pub inline fn dot(self: Vector4, other: Vector4) f32 { return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w; } pub inline fn lerp(a: Vector4, b: Vector4, t: f32) Vector4 { return .{ .x = @mulAdd(f32, t, b.x, @mulAdd(f32, -t, a.x, a.x)), .y = @mulAdd(f32, t, b.y, @mulAdd(f32, -t, a.y, a.y)), .z = @mulAdd(f32, t, b.z, @mulAdd(f32, -t, a.z, a.z)), .w = @mulAdd(f32, t, b.w, @mulAdd(f32, -t, a.w, a.w)), }; } };