ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4

C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 12:25:03 -05:00
1 parent 9c553b9d1a
commit 434a304315
283 files changed
+1877 -1660

No files matched your search

+14 -172
View File
@@ -1,80 +1,15 @@
--!strict
-- model/fbx/Xform: the FBX transform model (FBX SDK "Computing transformation matrix"): the local matrix of a Model
-- node T * Roff * Rp * Rpre * R * Rpost^-1 * Rp^-1 * Soff * Sp * S * Sp^-1, Euler rotations in degrees in the six
-- orders, column-major 4x4 matrices (flat arrays of 16, translation at 13..15 as glTF and FBX store them), inverse,
-- TRS decomposition (quaternion x, y, z, w) and Euler XYZ from a quaternion (the writer).
-- node T * Roff * Rp * Rpre * R * Rpost^-1 * Rp^-1 * Soff * Sp * S * Sp^-1, with FBX's numbered Euler orders. The
-- matrix / quaternion arithmetic is codecs/model/Mat4.
local Mat4 = require("@formats/codecs/model/Mat4")
local Xform = {}
export type M4 = { number }
local rad = math.pi / 180
function Xform.identity(): M4
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
end
function Xform.mul(a: M4, b: M4): M4
local m = table.create(16, 0)
for col = 0, 3 do
for row = 0, 3 do
m[col * 4 + row + 1] = a[row + 1] * b[col * 4 + 1] + a[4 + row + 1] * b[col * 4 + 2] + a[8 + row + 1] * b[col * 4 + 3] + a[12 + row + 1] * b[col * 4 + 4]
end
end
return m
end
function Xform.translate(x: number, y: number, z: number): M4
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
end
function Xform.scale(x: number, y: number, z: number): M4
return { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
end
local function rx(a: number): M4
local c, s = math.cos(a), math.sin(a)
return { 1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1 }
end
local function ry(a: number): M4
local c, s = math.cos(a), math.sin(a)
return { c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1 }
end
local function rz(a: number): M4
local c, s = math.cos(a), math.sin(a)
return { c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
end
-- FBX RotationOrder: 0 XYZ (R = Rz * Ry * Rx: X applied first), 1 XZY, 2 YZX, 3 YXZ, 4 ZXY, 5 ZYX, 6 spheric (= XYZ)
local ORDERS = { [0] = "xyz", "xzy", "yzx", "yxz", "zxy", "zyx", "xyz" }
function Xform.euler(x: number, y: number, z: number, order: number?): M4
local o = ORDERS[order or 0] or "xyz"
local R = { x = rx(x * rad), y = ry(y * rad), z = rz(z * rad) }
local a, b, c = string.sub(o, 1, 1), string.sub(o, 2, 2), string.sub(o, 3, 3)
return Xform.mul(R[c], Xform.mul(R[b], R[a]))
end
-- inverse of an affine matrix (general 3x3 part) | nil when singular
function Xform.inverse(m: M4): M4?
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 det == 0 or det ~= det then
return nil
end
local k = 1 / det
local r = {
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,
}
local tx, ty, tz = m[13], m[14], m[15]
r[13] = -(r[1] * tx + r[5] * ty + r[9] * tz)
r[14] = -(r[2] * tx + r[6] * ty + r[10] * tz)
r[15] = -(r[3] * tx + r[7] * ty + r[11] * tz)
return r
function Xform.euler(x: number, y: number, z: number, order: number?): Mat4.M4
return Mat4.euler(x, y, z, ORDERS[order or 0] or "xyz")
end
-- the local matrix of a node from its (already defaulted) FBX properties p: vectors of 3 numbers
@@ -86,113 +21,20 @@ export type Props = {
local function isZero(v: { number }): boolean
return v[1] == 0 and v[2] == 0 and v[3] == 0
end
function Xform.localMatrix(p: Props): M4
local m = Xform.translate(p.T[1] + p.Roff[1] + p.Rp[1], p.T[2] + p.Roff[2] + p.Rp[2], p.T[3] + p.Roff[3] + p.Rp[3])
function Xform.localMatrix(p: Props): Mat4.M4
local m = Mat4.translation(p.T[1] + p.Roff[1] + p.Rp[1], p.T[2] + p.Roff[2] + p.Rp[2], p.T[3] + p.Roff[3] + p.Rp[3])
if not isZero(p.Rpre) then
m = Xform.mul(m, Xform.euler(p.Rpre[1], p.Rpre[2], p.Rpre[3], 0))
m = Mat4.mul(m, Xform.euler(p.Rpre[1], p.Rpre[2], p.Rpre[3], 0))
end
m = Xform.mul(m, Xform.euler(p.R[1], p.R[2], p.R[3], p.order))
m = Mat4.mul(m, Xform.euler(p.R[1], p.R[2], p.R[3], p.order))
if not isZero(p.Rpost) then
local post = Xform.euler(p.Rpost[1], p.Rpost[2], p.Rpost[3], 0)
-- the inverse of a rotation is its transpose
local tr = { post[1], post[5], post[9], 0, post[2], post[6], post[10], 0, post[3], post[7], post[11], 0, 0, 0, 0, 1 }
m = Xform.mul(m, tr)
m = Mat4.mul(m, Mat4.transpose3(Xform.euler(p.Rpost[1], p.Rpost[2], p.Rpost[3], 0)))
end
m = Xform.mul(m, Xform.translate(-p.Rp[1] + p.Soff[1] + p.Sp[1], -p.Rp[2] + p.Soff[2] + p.Sp[2], -p.Rp[3] + p.Soff[3] + p.Sp[3]))
m = Xform.mul(m, Xform.scale(p.S[1], p.S[2], p.S[3]))
m = Mat4.mul(m, Mat4.translation(-p.Rp[1] + p.Soff[1] + p.Sp[1], -p.Rp[2] + p.Soff[2] + p.Sp[2], -p.Rp[3] + p.Soff[3] + p.Sp[3]))
m = Mat4.mul(m, Mat4.scaling(p.S[1], p.S[2], p.S[3]))
if not isZero(p.Sp) then
m = Xform.mul(m, Xform.translate(-p.Sp[1], -p.Sp[2], -p.Sp[3]))
end
return m
end
-- quaternion (x, y, z, w) of a pure rotation matrix
local function quat(m: M4): { number }
local m00, m01, m02 = m[1], m[5], m[9]
local m10, m11, m12 = m[2], m[6], m[10]
local m20, m21, m22 = m[3], m[7], m[11]
local tr = m00 + m11 + m22
local x, y, z, w
if tr > 0 then
local s = math.sqrt(tr + 1) * 2
w, x, y, z = s / 4, (m21 - m12) / s, (m02 - m20) / s, (m10 - m01) / s
elseif m00 > m11 and m00 > m22 then
local s = math.sqrt(math.max(1 + m00 - m11 - m22, 1e-30)) * 2
w, x, y, z = (m21 - m12) / s, s / 4, (m01 + m10) / s, (m02 + m20) / s
elseif m11 > m22 then
local s = math.sqrt(math.max(1 + m11 - m00 - m22, 1e-30)) * 2
w, x, y, z = (m02 - m20) / s, (m01 + m10) / s, s / 4, (m12 + m21) / s
else
local s = math.sqrt(math.max(1 + m22 - m00 - m11, 1e-30)) * 2
w, x, y, z = (m10 - m01) / s, (m02 + m20) / s, (m12 + m21) / s, s / 4
end
local n = math.sqrt(x * x + y * y + z * z + w * w)
if n == 0 or n ~= n then
return { 0, 0, 0, 1 }
end
return { x / n, y / n, z / n, w / n }
end
Xform.quat = quat
-- decompose(m) -> translation, rotation (quaternion), scale (a negative determinant flips the x scale)
function Xform.decompose(m: M4): ({ number }, { number }, { number })
local t = { m[13], m[14], m[15] }
local sx = math.sqrt(m[1] * m[1] + m[2] * m[2] + m[3] * m[3])
local sy = math.sqrt(m[5] * m[5] + m[6] * m[6] + m[7] * m[7])
local sz = math.sqrt(m[9] * m[9] + m[10] * m[10] + m[11] * m[11])
local det = m[1] * (m[6] * m[11] - m[10] * m[7]) - m[5] * (m[2] * m[11] - m[10] * m[3]) + m[9] * (m[2] * m[7] - m[6] * m[3])
if det < 0 then
sx = -sx
end
local ix = if sx ~= 0 then 1 / sx else 0
local iy = if sy ~= 0 then 1 / sy else 0
local iz = if sz ~= 0 then 1 / sz else 0
local r = { m[1] * ix, m[2] * ix, m[3] * ix, 0, m[5] * iy, m[6] * iy, m[7] * iy, 0, m[9] * iz, m[10] * iz, m[11] * iz, 0, 0, 0, 0, 1 }
return t, quat(r), { sx, sy, sz }
end
-- the rotation matrix of a quaternion
function Xform.fromQuat(q: { number }): M4
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
-- Euler XYZ degrees (R = Rz * Ry * Rx) of a quaternion
function Xform.eulerXYZ(q: { number }): { number }
local m = Xform.fromQuat(q)
-- m[3] = r20 = -sin(y)
local sy = -m[3]
sy = math.clamp(sy, -1, 1)
local y = math.asin(sy)
local x, z
if math.abs(sy) < 0.9999999 then
x = math.atan2(m[7], m[11])
z = math.atan2(m[2], m[1])
else
x = math.atan2(-m[10], m[6])
z = 0
end
return { x / rad, y / rad, z / rad }
end
-- TRS -> matrix (glTF semantics)
function Xform.compose(t: { number }?, r: { number }?, s: { number }?): M4
local m = if r then Xform.fromQuat(r) else Xform.identity()
if s then
for k = 0, 2 do
m[k * 4 + 1] *= s[k + 1]
m[k * 4 + 2] *= s[k + 1]
m[k * 4 + 3] *= s[k + 1]
end
end
if t then
m[13], m[14], m[15] = t[1], t[2], t[3]
m = Mat4.mul(m, Mat4.translation(-p.Sp[1], -p.Sp[2], -p.Sp[3]))
end
return m
end