Reorganize and fix
This commit is contained in:
@@ -83,6 +83,20 @@ pub const Matrix4x4 = extern struct {
|
||||
};
|
||||
}
|
||||
|
||||
test initRotation {
|
||||
const q = vm.Quaternion.initRotation(.XY, 0.5);
|
||||
const m = initRotation(q);
|
||||
|
||||
try std.testing.expectEqual(init(
|
||||
// zig fmt: off
|
||||
-1, 0, 0, 0,
|
||||
0, -1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
// zig fmt: on
|
||||
), m);
|
||||
}
|
||||
|
||||
pub inline fn initScale(s: vm.Vector3) Matrix4x4 {
|
||||
return .{
|
||||
// zig fmt: off
|
||||
@@ -261,64 +275,6 @@ pub const Matrix4x4 = extern struct {
|
||||
};
|
||||
}
|
||||
|
||||
// --- TRANSFORM -----------------------------------------------------------
|
||||
|
||||
// TODO Move to vm.Vector3
|
||||
pub inline fn transformPoint(self: Matrix4x4, p: vm.Vector3) vm.Vector3 {
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
// TODO Move to vm.Vector3x8
|
||||
pub inline fn transformPoint_x8(self: Matrix4x4, p: vm.Vector3x8) vm.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),
|
||||
};
|
||||
}
|
||||
|
||||
// TODO Move to vm.Vector3
|
||||
pub inline fn transformVector(self: Matrix4x4, v: vm.Vector3) vm.Vector3 {
|
||||
return .{
|
||||
.x = v.x * self.ix + v.y * self.jx + v.z * self.kx,
|
||||
.y = v.x * self.iy + v.y * self.jy + v.z * self.ky,
|
||||
.z = v.x * self.iz + v.y * self.jz + v.z * self.kz,
|
||||
};
|
||||
}
|
||||
|
||||
// TODO Move to vm.Vector3x8
|
||||
pub inline fn transformVector_x8(self: Matrix4x4, v: vm.Vector3x8) vm.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),
|
||||
};
|
||||
}
|
||||
|
||||
// TODO Move to vm.Vector4
|
||||
pub inline fn transformHomogeneous(self: Matrix4x4, h: vm.Vector4) vm.Vector4 {
|
||||
return .{
|
||||
.x = h.x * self.ix + h.y * self.jx + h.z * self.kx + h.w * self.tx,
|
||||
.y = h.x * self.iy + h.y * self.jy + h.z * self.ky + h.w * self.ty,
|
||||
.z = h.x * self.iz + h.y * self.jz + h.z * self.kz + h.w * self.tz,
|
||||
.w = h.x * self.iw + h.y * self.jw + h.z * self.kw + h.w * self.tw,
|
||||
};
|
||||
}
|
||||
|
||||
// TODO Move to vm.Vector4x8
|
||||
pub inline fn transformHomogeneous_x8(self: Matrix4x4, h: vm.Vector4x8) vm.Vector4x8 {
|
||||
return .{
|
||||
.x = h.x * vm.ps(self.ix) + h.y * vm.ps(self.jx) + h.z * vm.ps(self.kx) + h.w * vm.ps(self.tx),
|
||||
.y = h.x * vm.ps(self.iy) + h.y * vm.ps(self.jy) + h.z * vm.ps(self.ky) + h.w * vm.ps(self.ty),
|
||||
.z = h.x * vm.ps(self.iz) + h.y * vm.ps(self.jz) + h.z * vm.ps(self.kz) + h.w * vm.ps(self.tz),
|
||||
.w = h.x * vm.ps(self.iw) + h.y * vm.ps(self.jw) + h.z * vm.ps(self.kw) + h.w * vm.ps(self.tw),
|
||||
};
|
||||
}
|
||||
|
||||
// --- COMPOSE -------------------------------------------------------------
|
||||
|
||||
/// The caller asserts that W rows of all matrices are equal to [0 0 0 1].
|
||||
@@ -338,38 +294,13 @@ pub const Matrix4x4 = extern struct {
|
||||
.ky = other.kx * self.iy + other.ky * self.jy + other.kz * self.ky,
|
||||
.kz = other.kx * self.iz + other.ky * self.jz + other.kz * self.kz,
|
||||
.kw = 0,
|
||||
.tx = other.tx * self.tx + other.ty * self.jx + other.tz * self.kx + self.tx,
|
||||
.ty = other.tx * self.ty + other.ty * self.jy + other.tz * self.ky + self.ty,
|
||||
.tz = other.tx * self.tz + other.ty * self.jz + other.tz * self.kz + self.tz,
|
||||
.tx = other.tx * self.ix + other.ty * self.jx + other.tz * self.kx + self.tx,
|
||||
.ty = other.tx * self.iy + other.ty * self.jy + other.tz * self.ky + self.ty,
|
||||
.tz = other.tx * self.iz + other.ty * self.jz + other.tz * self.kz + self.tz,
|
||||
.tw = 1,
|
||||
};
|
||||
}
|
||||
|
||||
// TODO Move to vm.Matrix4x4x8
|
||||
/// The caller asserts that W rows of all matrices are equal to [0 0 0 1].
|
||||
pub fn mulMatrixAffine_x8(self: Matrix4x4, other: vm.Matrix4x4x8) vm.Matrix4x4x8 {
|
||||
std.debug.assert(self.iw == 0 and self.jw == 0 and self.kw == 0 and self.tw == 1);
|
||||
std.debug.assert(@reduce(.And, (other.iw == vm.ps(0)) & (other.jw == vm.ps(0)) & (other.kw == vm.ps(0)) & (other.tw == vm.ps(1))));
|
||||
return .{
|
||||
.ix = other.ix * vm.ps(self.ix) + other.iy * vm.ps(self.jx) + other.iz * vm.ps(self.kx),
|
||||
.iy = other.ix * vm.ps(self.iy) + other.iy * vm.ps(self.jy) + other.iz * vm.ps(self.ky),
|
||||
.iz = other.ix * vm.ps(self.iz) + other.iy * vm.ps(self.jz) + other.iz * vm.ps(self.kz),
|
||||
.iw = vm.ps(0),
|
||||
.jx = other.jx * vm.ps(self.ix) + other.jy * vm.ps(self.jx) + other.jz * vm.ps(self.kx),
|
||||
.jy = other.jx * vm.ps(self.iy) + other.jy * vm.ps(self.jy) + other.jz * vm.ps(self.ky),
|
||||
.jz = other.jx * vm.ps(self.iz) + other.jy * vm.ps(self.jz) + other.jz * vm.ps(self.kz),
|
||||
.jw = vm.ps(0),
|
||||
.kx = other.kx * vm.ps(self.ix) + other.ky * vm.ps(self.jx) + other.kz * vm.ps(self.kx),
|
||||
.ky = other.kx * vm.ps(self.iy) + other.ky * vm.ps(self.jy) + other.kz * vm.ps(self.ky),
|
||||
.kz = other.kx * vm.ps(self.iz) + other.ky * vm.ps(self.jz) + other.kz * vm.ps(self.kz),
|
||||
.kw = vm.ps(0),
|
||||
.tx = other.tx * vm.ps(self.tx) + other.ty * vm.ps(self.jx) + other.tz * vm.ps(self.kx) + vm.ps(self.tx),
|
||||
.ty = other.tx * vm.ps(self.ty) + other.ty * vm.ps(self.jy) + other.tz * vm.ps(self.ky) + vm.ps(self.ty),
|
||||
.tz = other.tx * vm.ps(self.tz) + other.ty * vm.ps(self.jz) + other.tz * vm.ps(self.kz) + vm.ps(self.tz),
|
||||
.tw = vm.ps(1),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn mulMatrixFull(self: Matrix4x4, other: Matrix4x4) Matrix4x4 {
|
||||
return .{
|
||||
.ix = other.ix * self.ix + other.iy * self.jx + other.iz * self.kx + other.iw * self.tx,
|
||||
@@ -391,28 +322,6 @@ pub const Matrix4x4 = extern struct {
|
||||
};
|
||||
}
|
||||
|
||||
// TODO Move to vm.Matrix4x4x8
|
||||
pub fn mulMatrixFull_x8(self: Matrix4x4, other: vm.Matrix4x4x8) vm.Matrix4x4x8 {
|
||||
return .{
|
||||
.ix = other.ix * vm.ps(self.ix) + other.iy * vm.ps(self.jx) + other.iz * vm.ps(self.kx) + other.iw * vm.ps(self.tx),
|
||||
.iy = other.ix * vm.ps(self.iy) + other.iy * vm.ps(self.jy) + other.iz * vm.ps(self.ky) + other.iw * vm.ps(self.ty),
|
||||
.iz = other.ix * vm.ps(self.iz) + other.iy * vm.ps(self.jz) + other.iz * vm.ps(self.kz) + other.iw * vm.ps(self.tz),
|
||||
.iw = other.ix * vm.ps(self.iw) + other.iy * vm.ps(self.jw) + other.iz * vm.ps(self.kw) + other.iw * vm.ps(self.tw),
|
||||
.jx = other.jx * vm.ps(self.ix) + other.jy * vm.ps(self.jx) + other.jz * vm.ps(self.kx) + other.jw * vm.ps(self.tx),
|
||||
.jy = other.jx * vm.ps(self.iy) + other.jy * vm.ps(self.jy) + other.jz * vm.ps(self.ky) + other.jw * vm.ps(self.ty),
|
||||
.jz = other.jx * vm.ps(self.iz) + other.jy * vm.ps(self.jz) + other.jz * vm.ps(self.kz) + other.jw * vm.ps(self.tz),
|
||||
.jw = other.jx * vm.ps(self.iw) + other.jy * vm.ps(self.jw) + other.jz * vm.ps(self.kw) + other.jw * vm.ps(self.tw),
|
||||
.kx = other.kx * vm.ps(self.ix) + other.ky * vm.ps(self.jx) + other.kz * vm.ps(self.kx) + other.kw * vm.ps(self.tx),
|
||||
.ky = other.kx * vm.ps(self.iy) + other.ky * vm.ps(self.jy) + other.kz * vm.ps(self.ky) + other.kw * vm.ps(self.ty),
|
||||
.kz = other.kx * vm.ps(self.iz) + other.ky * vm.ps(self.jz) + other.kz * vm.ps(self.kz) + other.kw * vm.ps(self.tz),
|
||||
.kw = other.kx * vm.ps(self.iw) + other.ky * vm.ps(self.jw) + other.kz * vm.ps(self.kw) + other.kw * vm.ps(self.tw),
|
||||
.tx = other.tx * vm.ps(self.ix) + other.ty * vm.ps(self.jx) + other.tz * vm.ps(self.kx) + other.tw * vm.ps(self.tx),
|
||||
.ty = other.tx * vm.ps(self.iy) + other.ty * vm.ps(self.jy) + other.tz * vm.ps(self.ky) + other.tw * vm.ps(self.ty),
|
||||
.tz = other.tx * vm.ps(self.iz) + other.ty * vm.ps(self.jz) + other.tz * vm.ps(self.kz) + other.tw * vm.ps(self.tz),
|
||||
.tw = other.tx * vm.ps(self.iw) + other.ty * vm.ps(self.jw) + other.tz * vm.ps(self.kw) + other.tw * vm.ps(self.tw),
|
||||
};
|
||||
}
|
||||
|
||||
// INVERSION DERIVATION (affine and orthonormal case)
|
||||
//
|
||||
// We assume the matrix looks like so:
|
||||
|
||||
Reference in New Issue
Block a user