Files
rosetta/src/Formats/model/dae/Math.luau
T

189 lines
6.4 KiB
Luau

--!strict
-- model/dae/Math: the matrix helpers the Collada reader and writer share. Matrices are flat arrays of 16 in glTF's
-- column-major order (Collada writes row-major: fromRows / toRows convert). Quaternions are { x, y, z, w }.
local DaeMath = {}
local sqrt, abs, rad, sin, cos = math.sqrt, math.abs, math.rad, math.sin, math.cos
function DaeMath.identity(): { number }
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
end
-- fromRows(v, at?) -> column-major from 16 row-major values starting at v[at] (default 1)
function DaeMath.fromRows(v: { number }, at: number?): { number }
local o = (at or 1) - 1
local m = table.create(16, 0)
for r = 0, 3 do
for c = 0, 3 do
m[c * 4 + r + 1] = v[o + r * 4 + c + 1] or (if r == c then 1 else 0)
end
end
return m
end
function DaeMath.toRows(m: { number }): { number }
local v = table.create(16, 0)
for r = 0, 3 do
for c = 0, 3 do
v[r * 4 + c + 1] = m[c * 4 + r + 1]
end
end
return v
end
function DaeMath.mul(a: { number }, b: { number }): { number }
local m = table.create(16, 0)
for col = 0, 3 do
for row = 0, 3 do
local s = 0
for k = 0, 3 do
s += a[k * 4 + row + 1] * b[col * 4 + k + 1]
end
m[col * 4 + row + 1] = s
end
end
return m
end
function DaeMath.translation(x: number, y: number, z: number): { number }
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
end
function DaeMath.scaling(x: number, y: number, z: number): { number }
return { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
end
-- axisAngle(x, y, z, degrees) -> quaternion
function DaeMath.axisAngle(x: number, y: number, z: number, degrees: number): { number }
local l = sqrt(x * x + y * y + z * z)
if l < 1e-12 then
return { 0, 0, 0, 1 }
end
local h = rad(degrees) / 2
local s = sin(h) / l
return { x * s, y * s, z * s, cos(h) }
end
function DaeMath.quatMatrix(q: { number }): { number }
local x, y, z, w = q[1], q[2], q[3], q[4]
local xx, yy, zz, xy, xz, yz, wx, wy, wz = x * x, y * y, z * z, x * y, x * z, y * z, w * x, w * y, w * z
return {
1 - 2 * (yy + zz), 2 * (xy + wz), 2 * (xz - wy), 0,
2 * (xy - wz), 1 - 2 * (xx + zz), 2 * (yz + wx), 0,
2 * (xz + wy), 2 * (yz - wx), 1 - 2 * (xx + yy), 0,
0, 0, 0, 1,
}
end
local function cross(a: { number }, b: { number }): { number }
return { a[2] * b[3] - a[3] * b[2], a[3] * b[1] - a[1] * b[3], a[1] * b[2] - a[2] * b[1] }
end
local function norm(a: { number }): { number }
local l = sqrt(a[1] * a[1] + a[2] * a[2] + a[3] * a[3])
if l < 1e-20 then
return { 0, 0, 0 }
end
return { a[1] / l, a[2] / l, a[3] / l }
end
-- lookat(eye, interest, up) -> the placement of an object at eye whose -Z looks at interest
function DaeMath.lookat(v: { number }): { number }
local ex, ey, ez = v[1] or 0, v[2] or 0, v[3] or 0
local z = norm({ ex - (v[4] or 0), ey - (v[5] or 0), ez - (v[6] or 0) })
local x = norm(cross({ v[7] or 0, v[8] or 1, v[9] or 0 }, z))
local y = cross(z, x)
return { x[1], x[2], x[3], 0, y[1], y[2], y[3], 0, z[1], z[2], z[3], 0, ex, ey, ez, 1 }
end
-- skew(angle, rotation axis, translation axis) (RenderMan's skew)
function DaeMath.skew(v: { number }): { number }
local a = rad(v[1] or 0)
local r = norm({ v[2] or 0, v[3] or 0, v[4] or 0 })
local t = norm({ v[5] or 0, v[6] or 0, v[7] or 0 })
local s = math.tan(a)
local m = DaeMath.identity()
-- M = I + s * t * r^T (column-major: m[col*4 + row])
for row = 0, 2 do
for col = 0, 2 do
m[col * 4 + row + 1] += s * t[row + 1] * r[col + 1]
end
end
return m
end
-- decompose(m) -> translation, rotation, scale | nil (shear, a projective row, or a singular matrix)
function DaeMath.decompose(m: { number }): ({ number }?, { number }?, { number }?)
if abs(m[4]) > 1e-6 or abs(m[8]) > 1e-6 or abs(m[12]) > 1e-6 or abs(m[16] - 1) > 1e-6 then
return nil, nil, nil
end
local c1 = { m[1], m[2], m[3] }
local c2 = { m[5], m[6], m[7] }
local c3 = { m[9], m[10], m[11] }
local sx = sqrt(c1[1] * c1[1] + c1[2] * c1[2] + c1[3] * c1[3])
local sy = sqrt(c2[1] * c2[1] + c2[2] * c2[2] + c2[3] * c2[3])
local sz = sqrt(c3[1] * c3[1] + c3[2] * c3[2] + c3[3] * c3[3])
if sx < 1e-12 or sy < 1e-12 or sz < 1e-12 then
return nil, nil, nil
end
local x, y, z = { c1[1] / sx, c1[2] / sx, c1[3] / sx }, { c2[1] / sy, c2[2] / sy, c2[3] / sy }, { c3[1] / sz, c3[2] / sz, c3[3] / sz }
local dxy = x[1] * y[1] + x[2] * y[2] + x[3] * y[3]
local dxz = x[1] * z[1] + x[2] * z[2] + x[3] * z[3]
local dyz = y[1] * z[1] + y[2] * z[2] + y[3] * z[3]
if abs(dxy) > 1e-4 or abs(dxz) > 1e-4 or abs(dyz) > 1e-4 then
return nil, nil, nil
end
local cr = cross(x, y)
if cr[1] * z[1] + cr[2] * z[2] + cr[3] * z[3] < 0 then
sx = -sx
x = { -x[1], -x[2], -x[3] }
end
-- rotation matrix (columns x, y, z) -> quaternion
local m00, m01, m02 = x[1], y[1], z[1]
local m10, m11, m12 = x[2], y[2], z[2]
local m20, m21, m22 = x[3], y[3], z[3]
local tr = m00 + m11 + m22
local qx, qy, qz, qw
if tr > 0 then
local s = sqrt(tr + 1) * 2
qw, qx, qy, qz = s / 4, (m21 - m12) / s, (m02 - m20) / s, (m10 - m01) / s
elseif m00 > m11 and m00 > m22 then
local s = sqrt(1 + m00 - m11 - m22) * 2
qw, qx, qy, qz = (m21 - m12) / s, s / 4, (m01 + m10) / s, (m02 + m20) / s
elseif m11 > m22 then
local s = sqrt(1 + m11 - m00 - m22) * 2
qw, qx, qy, qz = (m02 - m20) / s, (m01 + m10) / s, s / 4, (m12 + m21) / s
else
local s = sqrt(1 + m22 - m00 - m11) * 2
qw, qx, qy, qz = (m10 - m01) / s, (m02 + m20) / s, (m12 + m21) / s, s / 4
end
local l = sqrt(qx * qx + qy * qy + qz * qz + qw * qw)
return { m[13], m[14], m[15] }, { qx / l, qy / l, qz / l, qw / l }, { sx, sy, sz }
end
-- transformPoint / transformDir (column-major m)
function DaeMath.point(m: { number }, x: number, y: number, z: number): (number, number, number)
return m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15]
end
-- the inverse transpose of the upper 3x3 (for normals), as a column-major 4x4
function DaeMath.normalMatrix(m: { number }): { number }
local a, b, c = m[1], m[5], m[9]
local d, e, f = m[2], m[6], m[10]
local g, h, i = m[3], m[7], m[11]
local A, B, C = e * i - f * h, -(d * i - f * g), d * h - e * g
local det = a * A + b * B + c * C
if abs(det) < 1e-30 then
return DaeMath.identity()
end
local k = 1 / det
-- inverse = adj / det; its transpose has rows = cofactors
return {
A * k, B * k, C * k, 0,
-(b * i - c * h) * k, (a * i - c * g) * k, -(a * h - b * g) * k, 0,
(b * f - c * e) * k, -(a * f - c * d) * k, (a * e - b * d) * k, 0,
0, 0, 0, 1,
}
end
return DaeMath