343 lines
16 KiB
Luau
343 lines
16 KiB
Luau
-- model/stl tests: files made by trimesh, assimp and by hand (binary starting with "solid", VisCAM / Magics colours,
|
|
-- raw attribute words, odd ASCII, several solids, quads, no endsolid, trailing bytes) against trimesh's raw reader
|
|
-- (CRC-32 of the f32 vertices / normals, area, bounds), our encoder's output read back by trimesh and assimp (verdicts
|
|
-- from the fixture step), round trips of every encoding and option, the contract, error codes, the fuzz no-throw pass
|
|
-- and a binary decode throughput note. Fixtures: tools/fixtures/stl.py.
|
|
local T = require("@tests/T")
|
|
local Stl = require("@rosetta/model/stl")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Fx = require("@tests/gen/stl")
|
|
local Scenes = require("@tests/gen/stl_scenes")
|
|
|
|
local t = T.suite("stl")
|
|
|
|
local function decode(data: any, opts: any?): (any, any)
|
|
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
|
return Stl.decode(Bytes.reader(b), opts)
|
|
end
|
|
|
|
-- every primitive's accessor `name` concatenated (nil when a primitive lacks it)
|
|
local function concat(scene: any, name: string): buffer?
|
|
local parts = {}
|
|
for _, p in scene.meshes[1].primitives do
|
|
local a = p.attributes[name]
|
|
if not a then
|
|
return nil
|
|
end
|
|
table.insert(parts, buffer.tostring(a.data))
|
|
end
|
|
return buffer.fromstring(table.concat(parts))
|
|
end
|
|
|
|
-- 1. the contract
|
|
t:eq(Stl.name, "stl", "name")
|
|
t:eq(Stl.category, "model", "category")
|
|
t:check(table.find(Stl.extensions, "stl") ~= nil, "extensions")
|
|
t:check(Stl.caps.decode and Stl.caps.encode, "caps")
|
|
|
|
-- 2. every fixture against the reference
|
|
for _, c in Fx.cases do
|
|
local label = c.name
|
|
local scene, err = decode(c.data)
|
|
t:check(scene, label .. ": " .. tostring(err))
|
|
t:check(scene.kind == "scene" and #scene.meshes == 1 and #scene.nodes == 1 and scene.roots[1] == 1, label .. ": a one-node scene")
|
|
t:eq(scene.meta.up, "z", label .. ": up")
|
|
t:eq(scene.meta.source.format, "stl", label .. ": meta.source.format")
|
|
t:eq(scene.meta.source.binary, c.binary, label .. ": binary")
|
|
t:eq(scene.meta.source.facets, c.facets, label .. ": facet count")
|
|
local pos = concat(scene, "POSITION") :: buffer
|
|
t:eq(buffer.len(pos), c.facets * 36, label .. ": POSITION size")
|
|
t:eq(Crc32.of(pos), c.pos_crc, label .. ": POSITION crc")
|
|
for _, p in scene.meshes[1].primitives do
|
|
t:check(p.mode == "triangles" and p.indices == nil and p.attributes.POSITION.type == "f32" and p.attributes.POSITION.components == 3, label .. ": a triangle soup")
|
|
end
|
|
if c.nrm_crc then
|
|
t:eq(Crc32.of(concat(scene, "NORMAL") :: buffer), c.nrm_crc, label .. ": NORMAL crc")
|
|
end
|
|
if c.area then
|
|
local area = 0
|
|
local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
|
|
for f = 0, c.facets - 1 do
|
|
local v = {}
|
|
for k = 0, 8 do
|
|
v[k + 1] = buffer.readf32(pos, f * 36 + k * 4)
|
|
end
|
|
for k = 0, 2 do
|
|
for a = 1, 3 do
|
|
lo[a] = math.min(lo[a], v[k * 3 + a])
|
|
hi[a] = math.max(hi[a], v[k * 3 + a])
|
|
end
|
|
end
|
|
local ux, uy, uz = v[4] - v[1], v[5] - v[2], v[6] - v[3]
|
|
local wx, wy, wz = v[7] - v[1], v[8] - v[2], v[9] - v[3]
|
|
local cx, cy, cz = uy * wz - uz * wy, uz * wx - ux * wz, ux * wy - uy * wx
|
|
area += math.sqrt(cx * cx + cy * cy + cz * cz) / 2
|
|
end
|
|
t:check(math.abs(area - c.area) <= 1e-6 * math.max(1, c.area), string.format("%s: area %.9g, want %.9g", label, area, c.area))
|
|
if c.facets > 0 then
|
|
-- trimesh keeps ASCII coordinates as doubles: compare with them rounded to f32
|
|
local function f32(x: number): number
|
|
local sb = buffer.create(4)
|
|
buffer.writef32(sb, 0, x)
|
|
return buffer.readf32(sb, 0)
|
|
end
|
|
for a = 1, 3 do
|
|
t:check(lo[a] == f32(c.bounds[a]) and hi[a] == f32(c.bounds[a + 3]), label .. ": bounds")
|
|
end
|
|
end
|
|
end
|
|
local p1 = scene.meshes[1].primitives[1]
|
|
if c.col_crc then
|
|
t:eq(scene.meta.source.colors, c.colors, label .. ": colour convention")
|
|
t:check(p1.attributes.COLOR_0 and p1.attributes.COLOR_0.type == "unorm8" and p1.attributes.COLOR_0.components == 4, label .. ": COLOR_0 unorm8 x4")
|
|
t:eq(Crc32.of(p1.attributes.COLOR_0.data), c.col_crc, label .. ": COLOR_0 crc")
|
|
else
|
|
t:check(p1.attributes.COLOR_0 == nil, label .. ": no colours")
|
|
end
|
|
if c.defaultColor then
|
|
local d = scene.meta.source.defaultColor
|
|
t:check(d and d[1] == c.defaultColor[1] and d[2] == c.defaultColor[2] and d[3] == c.defaultColor[3] and d[4] == c.defaultColor[4], label .. ": magics default colour")
|
|
t:check(scene.meta.source.material and scene.meta.source.material.specular[1] == 4, label .. ": magics material")
|
|
end
|
|
if c.attr_crc then
|
|
local fa = p1.extras and p1.extras.faceAttributes and p1.extras.faceAttributes.attribute
|
|
t:check(fa and fa.type == "u16" and fa.count == c.facets, label .. ": raw attribute words")
|
|
t:eq(Crc32.of(fa.data), c.attr_crc, label .. ": attribute words crc")
|
|
end
|
|
if c.solids then
|
|
local prims = scene.meshes[1].primitives
|
|
t:eq(#prims, #c.solids, label .. ": one primitive per solid")
|
|
for i, name in c.solids do
|
|
t:eq(prims[i].extras.solid, name, label .. ": solid name")
|
|
t:eq(prims[i].attributes.POSITION.count, c.solidFacets[i] * 3, label .. ": solid facets")
|
|
end
|
|
end
|
|
-- inside a bigger buffer (the Reader's range)
|
|
local big = buffer.create(#c.data + 11)
|
|
buffer.writestring(big, 5, c.data)
|
|
local s2 = Stl.decode(Bytes.reader(big, 5, #c.data))
|
|
t:check(s2 and Crc32.of(concat(s2, "POSITION") :: buffer) == c.pos_crc, label .. ": decode from a sub-range")
|
|
if label ~= "bin-trailing.stl" then -- no magic, and the size does not match: not recognisable
|
|
t:check(Stl.sniff(Bytes.reader(buffer.fromstring(c.data))) >= 0.5, label .. ": sniff")
|
|
end
|
|
-- re-encode (binary with the file's normals, and ASCII): the same triangles and normals
|
|
for _, ascii in { false, true } do
|
|
local opts: any = { ascii = ascii, normals = "attribute", colors = if c.col_crc then c.colors else nil }
|
|
if c.defaultColor then
|
|
opts.defaultColor = c.defaultColor
|
|
end
|
|
local out, eerr = Stl.encode(scene, opts)
|
|
t:check(out, label .. ": encode: " .. tostring(eerr))
|
|
local back = decode(out)
|
|
t:check(back, label .. ": decode our output")
|
|
t:check(buffer.tostring(concat(back, "POSITION") :: buffer) == buffer.tostring(pos), label .. ": re-encoded POSITION (ascii " .. tostring(ascii) .. ")")
|
|
t:check(buffer.tostring(concat(back, "NORMAL") :: buffer) == buffer.tostring(concat(scene, "NORMAL") :: buffer), label .. ": re-encoded NORMAL")
|
|
if c.col_crc and not ascii then
|
|
t:eq(Crc32.of(back.meshes[1].primitives[1].attributes.COLOR_0.data), c.col_crc, label .. ": re-encoded colours")
|
|
end
|
|
if c.attr_crc and not ascii then
|
|
t:eq(Crc32.of(back.meshes[1].primitives[1].extras.faceAttributes.attribute.data), c.attr_crc, label .. ": re-encoded attribute words")
|
|
end
|
|
if c.solids and ascii then
|
|
t:eq(back.meshes[1].primitives[1].extras.solid, c.solids[1], label .. ": solid name round trip")
|
|
end
|
|
end
|
|
end
|
|
t:note("%d files match trimesh / the generator", #Fx.cases)
|
|
|
|
-- 3. the reference tools read our output
|
|
for _, v in Fx.ours do
|
|
t:check(v.ok, "trimesh / assimp read our " .. v.name .. ": " .. v.detail)
|
|
end
|
|
t:note("%d encodings read back by trimesh and assimp", #Fx.ours)
|
|
|
|
-- round trips of the encoder scenes: world-space soup as the generator computed it, colour words, idempotence
|
|
local function acc(s: string, ty: string, comps: number, count: number): Types.Accessor
|
|
return Types.Scene.accessor(buffer.fromstring(s), ty, comps, count)
|
|
end
|
|
local function build(spec: any): Types.Scene
|
|
local scene = Types.Scene.new()
|
|
for i, m in spec.meshes do
|
|
local prims = {}
|
|
for _, p in m.prims do
|
|
local prim: any = { attributes = { POSITION = acc(p.pos, p.posType or "f32", 3, p.count) }, mode = p.mode }
|
|
if p.indices then
|
|
prim.indices = acc(p.indices, "u32", 1, #p.indices // 4)
|
|
end
|
|
if p.faces then
|
|
prim.faces = acc(p.faces, "u32", 1, #p.faces // 4)
|
|
end
|
|
if p.color then
|
|
prim.attributes.COLOR_0 = acc(p.color, "unorm8", 4, p.count)
|
|
end
|
|
table.insert(prims, prim)
|
|
end
|
|
scene.meshes[i] = { name = m.name, primitives = prims }
|
|
end
|
|
for i, n in spec.nodes do
|
|
scene.nodes[i] = { name = n.name, mesh = n.mesh, children = n.children or {}, translation = n.translation, rotation = n.rotation, scale = n.scale, matrix = n.matrix }
|
|
end
|
|
scene.roots = spec.roots
|
|
return scene
|
|
end
|
|
local OPTS: { [string]: any } = { binary = {}, ascii = { ascii = true }, viscam = { colors = "viscam" }, magics = { colors = "magics" } }
|
|
for _, spec in Scenes do
|
|
local scene = build(spec)
|
|
local want = buffer.fromstring(spec.expect)
|
|
local nf = buffer.len(want) // 36
|
|
for _, e in spec.encodings do
|
|
local label = spec.name .. "/" .. e
|
|
local out, err = Stl.encode(scene, OPTS[e])
|
|
t:check(out, label .. ": encode: " .. tostring(err))
|
|
local back, berr = decode(out)
|
|
t:check(back, label .. ": decode: " .. tostring(berr))
|
|
local got = concat(back, "POSITION") :: buffer
|
|
t:eq(buffer.len(got), nf * 36, label .. ": facets")
|
|
local worst = 0
|
|
for i = 0, nf * 9 - 1 do
|
|
worst = math.max(worst, math.abs(buffer.readf32(got, i * 4) - buffer.readf32(want, i * 4)))
|
|
end
|
|
t:check(if spec.exact then worst == 0 else worst < 1e-4, string.format("%s: positions off by %g", label, worst))
|
|
-- facet normals: unit length, right-hand rule
|
|
local nrm = concat(back, "NORMAL") :: buffer
|
|
for f = 0, nf - 1 do
|
|
local o = f * 36
|
|
local n = { buffer.readf32(nrm, o), buffer.readf32(nrm, o + 4), buffer.readf32(nrm, o + 8) }
|
|
local len = math.sqrt(n[1] ^ 2 + n[2] ^ 2 + n[3] ^ 2)
|
|
t:check(math.abs(len - 1) < 1e-5, label .. ": unit normal")
|
|
local p = {}
|
|
for k = 0, 8 do
|
|
p[k + 1] = buffer.readf32(got, o + k * 4)
|
|
end
|
|
local cx = (p[5] - p[2]) * (p[9] - p[3]) - (p[6] - p[3]) * (p[8] - p[2])
|
|
local cy = (p[6] - p[3]) * (p[7] - p[1]) - (p[4] - p[1]) * (p[9] - p[3])
|
|
local cz = (p[4] - p[1]) * (p[8] - p[2]) - (p[5] - p[2]) * (p[7] - p[1])
|
|
t:check(cx * n[1] + cy * n[2] + cz * n[3] > 0, label .. ": normal follows the winding")
|
|
end
|
|
if e == "viscam" or e == "magics" then
|
|
local words = if e == "viscam" then spec.viscamWords else spec.magicsWords
|
|
for f = 0, nf - 1 do
|
|
t:eq(buffer.readu16(out :: buffer, 84 + f * 50 + 48), words[f + 1], label .. ": colour word")
|
|
end
|
|
t:eq(back.meta.source.colors, e, label .. ": detected convention")
|
|
-- the colours are on 5-bit steps: decode(encode()) gives the first vertex colour of each facet back
|
|
local col = back.meshes[1].primitives[1].attributes.COLOR_0.data
|
|
local src = scene.meshes[1].primitives[1]
|
|
local idx = src.indices :: Types.Accessor
|
|
for f = 0, nf - 1 do
|
|
local a = buffer.readu32(idx.data, f * 12)
|
|
t:eq(buffer.readu32(col, f * 12), buffer.readu32(src.attributes.COLOR_0.data, a * 4), label .. ": facet colour")
|
|
end
|
|
end
|
|
-- idempotent: encoding the decoded soup again gives the same file
|
|
local again = Stl.encode(back, { ascii = OPTS[e].ascii, colors = OPTS[e].colors, normals = "attribute" }) :: buffer
|
|
t:check(buffer.tostring(again) == buffer.tostring(out :: buffer), label .. ": re-encode is identical")
|
|
end
|
|
end
|
|
-- options: header, name, computed vs attribute normals
|
|
do
|
|
local scene = build(Scenes[1])
|
|
local out = Stl.encode(scene, { header = "my header", ascii = false }) :: buffer
|
|
t:eq(buffer.readstring(out, 0, 9), "my header", "header option")
|
|
t:eq(decode(out).meta.source.header, "my header", "meta.source.header")
|
|
local a = buffer.tostring(Stl.encode(scene, { ascii = true, name = "part 7" }) :: buffer)
|
|
t:check(string.sub(a, 1, 12) == "solid part 7" and string.find(a, "endsolid part 7", 1, true) ~= nil, "name option")
|
|
local m = Stl.encode(scene, { colors = "magics", defaultColor = { 1, 2, 3, 4 } }) :: buffer
|
|
local d = decode(m).meta.source.defaultColor
|
|
t:check(d[1] == 1 and d[2] == 2 and d[3] == 3 and d[4] == 4, "defaultColor option")
|
|
-- points / lines only: an empty STL
|
|
local pts = Types.Scene.fromPrimitive({ attributes = { POSITION = Types.Scene.accessor(nil, "f32", 3, 4) }, mode = "points" })
|
|
local e0 = Stl.encode(pts) :: buffer
|
|
t:eq(buffer.len(e0), 84, "points only: no facets")
|
|
-- a scene without nodes uses its meshes
|
|
local bare = Types.Scene.new()
|
|
bare.meshes[1] = scene.meshes[1]
|
|
t:eq(buffer.len(Stl.encode(bare) :: buffer), 84 + 50 * 60, "a scene without nodes")
|
|
end
|
|
|
|
-- 4. error codes
|
|
do
|
|
local cube = Fx.cases[1].data
|
|
local _, e1 = decode(string.sub(cube, 1, 84 + 50 * 3 + 7))
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "cut binary: " .. tostring(e1))
|
|
local _, e2 = decode("solid x\nfacet normal 1 2 zz\n")
|
|
t:check(e2 and e2.code == Err.CORRUPT, "bad number: " .. tostring(e2))
|
|
local _, e3 = decode("solid x\nfacet normal 1 2 3\nouter loop\nvertex 1 2 3\n")
|
|
t:check(e3 and e3.code == Err.TRUNCATED, "cut ascii: " .. tostring(e3))
|
|
local _, e4 = decode("solid x\nfacet normal 0 0 1\nouter loop\nvertex 1 2 3\nvertex 1 2 3\nendloop\nendfacet\nendsolid\n")
|
|
t:check(e4 and e4.code == Err.CORRUPT, "two-vertex facet: " .. tostring(e4))
|
|
local _, e5 = decode("hello")
|
|
t:check(e5 and e5.code == Err.TRUNCATED, "tiny junk: " .. tostring(e5))
|
|
local _, e6 = decode(cube, { maxFaces = 10 })
|
|
t:check(e6 and e6.code == Err.LIMIT, "maxFaces: " .. tostring(e6))
|
|
local _, e7 = decode(cube, { colors = "rainbow" })
|
|
t:check(e7 and e7.code == Err.ARG, "bad colors option: " .. tostring(e7))
|
|
local _, e8 = decode("solid x\nbanana\n")
|
|
t:check(e8 and e8.code == Err.CORRUPT, "bad keyword: " .. tostring(e8))
|
|
local hdr = buffer.create(84)
|
|
buffer.writeu32(hdr, 80, 1000000)
|
|
local _, e9 = Stl.decode(Bytes.reader(hdr))
|
|
t:check(e9 and e9.code == Err.TRUNCATED, "huge count: " .. tostring(e9))
|
|
local _, a1 = Stl.encode({} :: any)
|
|
t:check(a1 and a1.code == Err.ARG, "encode a non-scene: " .. tostring(a1))
|
|
local scene = build(Scenes[1])
|
|
local _, a2 = Stl.encode(scene, { colors = "rgb" } :: any)
|
|
t:check(a2 and a2.code == Err.ARG, "bad colors: " .. tostring(a2))
|
|
local bad = Types.Scene.fromPrimitive({ attributes = { POSITION = Types.Scene.accessor(nil, "f32", 3, 3) }, indices = Types.Scene.accessor(nil, "u32", 1, 3), mode = "triangles" })
|
|
buffer.writeu32((bad.meshes[1].primitives[1].indices :: Types.Accessor).data, 4, 7)
|
|
local _, a3 = Stl.encode(bad)
|
|
t:check(a3 and a3.code == Err.ARG, "index out of range: " .. tostring(a3))
|
|
local nan = Types.Scene.fromPrimitive({ attributes = { POSITION = Types.Scene.accessor(nil, "f32", 3, 3) }, mode = "triangles" })
|
|
buffer.writef32(nan.meshes[1].primitives[1].attributes.POSITION.data, 0, 0 / 0)
|
|
local _, a4 = Stl.encode(nan, { ascii = true })
|
|
t:check(a4 and a4.code == Err.ARG, "NaN in ASCII: " .. tostring(a4))
|
|
t:check(Stl.encode(nan) ~= nil, "NaN in binary is fine")
|
|
local cyc = build(Scenes[3])
|
|
cyc.nodes[2].children = { 1 }
|
|
local _, a5 = Stl.encode(cyc)
|
|
t:check(a5 and a5.code == Err.ARG, "cyclic nodes: " .. tostring(a5))
|
|
t:eq(Stl.sniff(Bytes.reader(buffer.fromstring("ply\nformat ascii 1.0\n"))), 0, "sniff a PLY")
|
|
end
|
|
|
|
-- 5. fuzz
|
|
do
|
|
local samples = {}
|
|
for i, c in Fx.cases do
|
|
if #c.data < 20000 or i % 3 == 0 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Stl.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 12 })
|
|
t:fuzz("decode re-encode", function(b: buffer)
|
|
local s = Stl.decode(Bytes.reader(b))
|
|
if s then
|
|
return Stl.encode(s, { ascii = true })
|
|
end
|
|
return nil
|
|
end, samples, { mutations = 4, garbage = 20 })
|
|
end
|
|
|
|
-- throughput: a synthetic binary STL of 200k facets
|
|
do
|
|
local n = 200000
|
|
local b = buffer.create(84 + 50 * n)
|
|
buffer.writeu32(b, 80, n)
|
|
for i = 0, n - 1 do
|
|
for k = 0, 11 do
|
|
buffer.writef32(b, 84 + i * 50 + k * 4, (i * 12 + k) % 1000 * 0.25)
|
|
end
|
|
end
|
|
local t0 = os.clock()
|
|
local s = Stl.decode(Bytes.reader(b))
|
|
local dt = os.clock() - t0
|
|
t:check(s and s.meshes[1].primitives[1].attributes.POSITION.count == n * 3, "bench decode")
|
|
t:note("binary decode: %d facets (%.1f MB) in %.3f s, %.0f MB/s", n, buffer.len(b) / 1e6, dt, buffer.len(b) / 1e6 / math.max(dt, 1e-6))
|
|
end
|
|
|
|
t:done()
|