220 lines
7.9 KiB
Zig
220 lines
7.9 KiB
Zig
const std = @import("std");
|
|
const vm = @import("../root.zig");
|
|
|
|
pub const Vector3x8 = struct {
|
|
x: vm.f32x8,
|
|
y: vm.f32x8,
|
|
z: vm.f32x8,
|
|
|
|
pub const zero = initScalarSingle(0);
|
|
pub const one = initScalarSingle(1);
|
|
pub const unit_x = initSingle(1, 0, 0);
|
|
pub const unit_y = initSingle(0, 1, 0);
|
|
pub const unit_z = initSingle(0, 0, 1);
|
|
pub const unit_nx = initSingle(-1, 0, 0);
|
|
pub const unit_ny = initSingle(0, -1, 0);
|
|
pub const unit_nz = initSingle(0, 0, -1);
|
|
|
|
// --- INIT ----------------------------------------------------------------
|
|
|
|
pub inline fn init(x: vm.f32x8, y: vm.f32x8, z: vm.f32x8) Vector3x8 {
|
|
return .{ .x = x, .y = y, .z = z };
|
|
}
|
|
|
|
pub inline fn initSingle(x: f32, y: f32, z: f32) Vector3x8 {
|
|
return .{ .x = vm.ps(x), .y = vm.ps(y), .z = vm.ps(z) };
|
|
}
|
|
|
|
pub inline fn initScalar(scalar: vm.f32x8) Vector3x8 {
|
|
return .{ .x = scalar, .y = scalar, .z = scalar };
|
|
}
|
|
|
|
pub inline fn initScalarSingle(scalar: f32) Vector3x8 {
|
|
return .{ .x = vm.ps(scalar), .y = vm.ps(scalar), .z = vm.ps(scalar) };
|
|
}
|
|
|
|
pub inline fn splat(vector: vm.Vector3) Vector3x8 {
|
|
return .{ .x = vm.ps(vector.x), .y = vm.ps(vector.y), .z = vm.ps(vector.z) };
|
|
}
|
|
|
|
// --- 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 };
|
|
}
|
|
|
|
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 unpack(self: Vector3x8) [3]vm.f32x8 {
|
|
return .{ self.x, self.y, self.z };
|
|
}
|
|
|
|
// --- COMPONENT-WISE ------------------------------------------------------
|
|
|
|
pub inline fn add(self: Vector3x8, other: Vector3x8) Vector3x8 {
|
|
return .{ .x = self.x + other.x, .y = self.y + other.y, .z = self.z + other.z };
|
|
}
|
|
|
|
pub inline fn sub(self: Vector3x8, other: Vector3x8) Vector3x8 {
|
|
return .{ .x = self.x - other.x, .y = self.y - other.y, .z = self.z - other.z };
|
|
}
|
|
|
|
pub inline fn mul(self: Vector3x8, other: Vector3x8) Vector3x8 {
|
|
return .{ .x = self.x * other.x, .y = self.y * other.y, .z = self.z * other.z };
|
|
}
|
|
|
|
pub inline fn mulScalar(self: Vector3x8, scalar: vm.f32x8) Vector3x8 {
|
|
return .{ .x = self.x * scalar, .y = self.y * scalar, .z = self.z * scalar };
|
|
}
|
|
|
|
pub inline fn mulScalarSingle(self: Vector3x8, scalar: f32) Vector3x8 {
|
|
return .{ .x = self.x * vm.ps(scalar), .y = self.y * vm.ps(scalar), .z = self.z * vm.ps(scalar) };
|
|
}
|
|
|
|
pub inline fn div(self: Vector3x8, other: Vector3x8) Vector3x8 {
|
|
return .{ .x = self.x / other.x, .y = self.y / other.y, .z = self.z / other.z };
|
|
}
|
|
|
|
pub inline fn divScalar(self: Vector3x8, scalar: vm.f32x8) Vector3x8 {
|
|
return .{ .x = self.x / scalar, .y = self.y / scalar, .z = self.z / scalar };
|
|
}
|
|
|
|
pub inline fn divScalarSingle(self: Vector3x8, scalar: f32) Vector3x8 {
|
|
return .{ .x = self.x / vm.ps(scalar), .y = self.y / vm.ps(scalar), .z = self.z / vm.ps(scalar) };
|
|
}
|
|
|
|
pub inline fn negate(self: Vector3x8) Vector3x8 {
|
|
return .{ .x = -self.x, .y = -self.y, .z = -self.z };
|
|
}
|
|
|
|
pub inline fn abs(self: Vector3x8) Vector3x8 {
|
|
return .{ .x = @abs(self.x), .y = @abs(self.y), .z = @abs(self.z) };
|
|
}
|
|
|
|
pub inline fn floor(self: Vector3x8) Vector3x8 {
|
|
return .{ .x = @floor(self.x), .y = @floor(self.y), .z = @floor(self.z) };
|
|
}
|
|
|
|
pub inline fn ceil(self: Vector3x8) Vector3x8 {
|
|
return .{ .x = @ceil(self.x), .y = @ceil(self.y), .z = @ceil(self.z) };
|
|
}
|
|
|
|
pub inline fn round(self: Vector3x8) Vector3x8 {
|
|
return .{ .x = @round(self.x), .y = @round(self.y), .z = @round(self.z) };
|
|
}
|
|
|
|
pub inline fn min(self: Vector3x8, other: Vector3x8) Vector3x8 {
|
|
return .{ .x = @min(self.x, other.x), .y = @min(self.y, other.y), .z = @min(self.z, other.z) };
|
|
}
|
|
|
|
pub inline fn max(self: Vector3x8, other: Vector3x8) Vector3x8 {
|
|
return .{ .x = @max(self.x, other.x), .y = @max(self.y, other.y), .z = @max(self.z, other.z) };
|
|
}
|
|
|
|
// --- OTHER ---------------------------------------------------------------
|
|
|
|
pub inline fn len(self: Vector3x8) vm.f32x8 {
|
|
return @sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
|
|
}
|
|
|
|
pub inline fn lenSquared(self: Vector3x8) vm.f32x8 {
|
|
return self.x * self.x + self.y * self.y + self.z * self.z;
|
|
}
|
|
|
|
pub inline fn dot(self: Vector3x8, other: Vector3x8) vm.f32x8 {
|
|
return self.x * other.x + self.y * other.y + self.z * other.z;
|
|
}
|
|
|
|
pub inline fn cross(self: Vector3x8, other: Vector3x8) Vector3x8 {
|
|
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,
|
|
};
|
|
}
|
|
|
|
pub inline fn lerp(a: Vector3x8, b: Vector3x8, t: vm.f32x8) Vector3x8 {
|
|
return .{
|
|
.x = @mulAdd(vm.f32x8, t, b.x, @mulAdd(vm.f32x8, -t, a.x, a.x)),
|
|
.y = @mulAdd(vm.f32x8, t, b.y, @mulAdd(vm.f32x8, -t, a.y, a.y)),
|
|
.z = @mulAdd(vm.f32x8, t, b.z, @mulAdd(vm.f32x8, -t, a.z, a.z)),
|
|
};
|
|
}
|
|
|
|
pub inline fn lerpSingle(a: Vector3x8, b: Vector3x8, t: f32) Vector3x8 {
|
|
return .{
|
|
.x = @mulAdd(vm.f32x8, vm.ps(t), b.x, @mulAdd(vm.f32x8, -vm.ps(t), a.x, a.x)),
|
|
.y = @mulAdd(vm.f32x8, vm.ps(t), b.y, @mulAdd(vm.f32x8, -vm.ps(t), a.y, a.y)),
|
|
.z = @mulAdd(vm.f32x8, vm.ps(t), b.z, @mulAdd(vm.f32x8, -vm.ps(t), a.z, a.z)),
|
|
};
|
|
}
|
|
|
|
pub inline fn rotate(self: Vector3x8, quaternion: vm.Quaternion_x8) Vector3x8 {
|
|
const w = quaternion.getScalar();
|
|
const xyz = quaternion.getVector();
|
|
|
|
return add(
|
|
self,
|
|
cross(
|
|
add(xyz, xyz),
|
|
add(cross(xyz, self), self.mulScalar(w)),
|
|
),
|
|
);
|
|
}
|
|
|
|
pub inline fn rotateSingle(self: Vector3x8, quaternion: vm.Quaternion) Vector3x8 {
|
|
const w = quaternion.getScalar();
|
|
const xyz = quaternion.getVector();
|
|
|
|
return add(
|
|
self,
|
|
cross(
|
|
splat(add(xyz, xyz)),
|
|
add(cross(splat(xyz), self), self.mulScalarSingle(w)),
|
|
),
|
|
);
|
|
}
|
|
|
|
pub inline fn transformPoint(self: vm.Matrix4x4x8, p: Vector3x8) Vector3x8 {
|
|
return .{
|
|
.x = p.x * self.ix + p.y * self.jx + p.z * self.kx + self.tx,
|
|
.y = p.x * self.iy + p.y * self.jy + p.z * self.ky + self.ty,
|
|
.z = p.x * self.iz + p.y * self.jz + p.z * self.kz + self.tz,
|
|
};
|
|
}
|
|
|
|
pub inline fn transformPointSingle(self: vm.Matrix4x4, p: Vector3x8) Vector3x8 {
|
|
return .{
|
|
.x = p.x * vm.ps(self.ix) + p.y * vm.ps(self.jx) + p.z * vm.ps(self.kx) + vm.ps(self.tx),
|
|
.y = p.x * vm.ps(self.iy) + p.y * vm.ps(self.jy) + p.z * vm.ps(self.ky) + vm.ps(self.ty),
|
|
.z = p.x * vm.ps(self.iz) + p.y * vm.ps(self.jz) + p.z * vm.ps(self.kz) + vm.ps(self.tz),
|
|
};
|
|
}
|
|
|
|
pub inline fn transformVector(self: vm.Matrix4x4x8, p: Vector3x8) Vector3x8 {
|
|
return .{
|
|
.x = p.x * self.ix + p.y * self.jx + p.z * self.kx,
|
|
.y = p.x * self.iy + p.y * self.jy + p.z * self.ky,
|
|
.z = p.x * self.iz + p.y * self.jz + p.z * self.kz,
|
|
};
|
|
}
|
|
|
|
pub inline fn transformVectorSingle(self: vm.Matrix4x4, v: Vector3x8) Vector3x8 {
|
|
return .{
|
|
.x = v.x * vm.ps(self.ix) + v.y * vm.ps(self.jx) + v.z * vm.ps(self.kx),
|
|
.y = v.x * vm.ps(self.iy) + v.y * vm.ps(self.jy) + v.z * vm.ps(self.ky),
|
|
.z = v.x * vm.ps(self.iz) + v.y * vm.ps(self.jz) + v.z * vm.ps(self.kz),
|
|
};
|
|
}
|
|
|
|
test "refAllDecls" {
|
|
std.testing.refAllDecls(@This());
|
|
}
|
|
};
|