Files

205 lines
9.0 KiB
Luau

-- model/off tests: trimesh and generated OFF files against the values they were written from; trimesh / assimp read ours.
-- Fixtures: tools/fixtures/off.py.
local T = require("@tests/T")
local Off = require("@rosetta/model/off")
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/off")
local Scenes = require("@tests/gen/off_scenes")
local t = T.suite("off")
local function decode(data: any, opts: any?): (any, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Off.decode(Bytes.reader(b), opts)
end
local function same(a: any, b: any): boolean
if a == nil or b == nil then
return a == b
end
return a.type == b.type and a.components == b.components and a.count == b.count and buffer.tostring(a.data) == buffer.tostring(b.data)
end
t:eq(Off.name, "off", "name")
t:eq(Off.category, "model", "category")
t:check(table.find(Off.extensions, "off") ~= nil, "extensions")
t:check(Off.caps.decode and Off.caps.encode, "caps")
for _, c in Fx.cases do
local label = c.name
local scene, err = decode(c.data)
t:check(scene, label .. ": " .. tostring(err))
local src = scene.meta.source
t:check(src.format == "off" and src.keyword == c.keyword and src.vertices == c.vertices and src.faces == c.faces, label .. ": meta.source")
t:eq(src.colormap, c.colormap, label .. ": colormap flag")
local p = scene.meshes[1].primitives[1]
local A = p.attributes
t:eq(Crc32.of(A.POSITION.data), c.pos_crc, label .. ": POSITION crc")
t:check((A.NORMAL and Crc32.of(A.NORMAL.data)) == c.nrm_crc, label .. ": NORMAL")
t:check((A.TEXCOORD_0 and Crc32.of(A.TEXCOORD_0.data)) == c.tex_crc, label .. ": TEXCOORD_0")
if c.color then
t:check(A.COLOR_0 and A.COLOR_0.type == c.color.type and A.COLOR_0.components == 4, label .. ": COLOR_0 type")
t:eq(Crc32.of(A.COLOR_0.data), c.color.crc, label .. ": COLOR_0 crc")
else
t:check(A.COLOR_0 == nil, label .. ": no COLOR_0")
end
local ex = p.extras or {}
if c.faceColors then
t:check(ex.faceColors and ex.faceColors.type == c.faceColors.type and ex.faceColors.count == c.faces, label .. ": faceColors type")
t:eq(Crc32.of(ex.faceColors.data), c.faceColors.crc, label .. ": faceColors crc")
else
t:check(ex.faceColors == nil, label .. ": no faceColors")
end
if c.faces == 0 then
t:check(p.mode == "points" and p.indices == nil, label .. ": a point cloud")
else
t:eq(p.mode, "triangles", label .. ": mode")
t:eq(p.indices.count, c.tris * 3, label .. ": index count")
t:eq(Crc32.of(p.indices.data), if c.allTri then c.corners_crc else c.tri_crc, label .. ": indices crc")
if c.faceColors and not c.allTri then
t:eq(Crc32.of(ex.faceOfTriangle.data), c.fot_crc, label .. ": faceOfTriangle")
end
end
t:eq(Off.sniff(Bytes.reader(buffer.fromstring(c.data))), 0.9, label .. ": sniff")
local poly = decode(c.data, { triangulate = false })
local pp = poly.meshes[1].primitives[1]
if c.faces > 0 and not c.allTri then
t:eq(pp.mode, "polygons", label .. ": polygons")
t:eq(Crc32.of(pp.indices.data), c.corners_crc, label .. ": corners")
t:eq(Crc32.of(pp.faces.data), c.sizes_crc, label .. ": sizes")
end
-- round trips: polygons in, polygons out; triangles in, triangles out (face colours through faceOfTriangle)
for _, s in { poly, scene } do
local out, eerr = Off.encode(s)
t:check(out, label .. ": encode: " .. tostring(eerr))
local back, berr = decode(out, { triangulate = false })
t:check(back, label .. ": decode our output: " .. tostring(berr))
local bp, sp = back.meshes[1].primitives[1], s.meshes[1].primitives[1]
for name, a in sp.attributes do
t:check(same(bp.attributes[name], a), label .. ": " .. name .. " round trip")
end
t:eq(bp.mode, sp.mode, label .. ": mode round trip")
t:check(same(bp.indices, sp.indices) and same(bp.faces, sp.faces), label .. ": faces round trip")
local sfc = sp.extras and sp.extras.faceColors
local bfc = bp.extras and bp.extras.faceColors
if sfc and sp.extras.faceOfTriangle then
t:eq(bfc.count, sp.extras.faceOfTriangle.count, label .. ": a colour per triangle")
else
t:check(same(bfc, sfc), label .. ": faceColors round trip")
end
end
end
t:note("%d files match their source values and round-trip", #Fx.cases)
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, assimp and the generator", #Fx.ours)
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 attrs = {}
for name, a in p.attrs do
attrs[name] = Types.Scene.accessor(buffer.fromstring(a[3]), a[1], a[2], p.count)
end
local prim: any = { attributes = attrs, mode = p.mode }
if p.indices then
prim.indices = Types.Scene.accessor(buffer.fromstring(p.indices), "u32", 1, #p.indices // 4)
end
if p.faces then
prim.faces = Types.Scene.accessor(buffer.fromstring(p.faces), "u32", 1, #p.faces // 4)
end
if p.faceColors then
prim.extras = { faceColors = Types.Scene.accessor(buffer.fromstring(p.faceColors), "unorm8", 4, #p.faceColors // 4) }
end
table.insert(prims, prim)
end
scene.meshes[i] = { primitives = prims }
end
for i, n in spec.nodes do
scene.nodes[i] = { 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
for _, spec in Scenes do
local scene = build(spec)
local sp = scene.meshes[1].primitives[1]
local out = Off.encode(scene) :: buffer
local back = decode(out, { triangulate = false })
local bp = back.meshes[1].primitives[1]
if spec.name ~= "polys" then
for name, a in sp.attributes do
t:check(same(bp.attributes[name], a), spec.name .. ": " .. name .. " round trip")
end
end
t:eq(bp.mode, sp.mode, spec.name .. ": mode")
t:check(same(bp.indices, sp.indices) and same(bp.faces, sp.faces), spec.name .. ": faces")
t:check(same(bp.extras and bp.extras.faceColors, sp.extras and sp.extras.faceColors), spec.name .. ": face colours")
end
do
local scene = build(Scenes[1])
local plain = buffer.tostring(Off.encode(scene, { colors = false, normals = false, texcoords = false }) :: buffer)
t:eq(string.sub(plain, 1, 4), "OFF\n", "plain OFF")
t:eq(string.sub(buffer.tostring(Off.encode(scene, { colors = false }) :: buffer), 1, 6), "STNOFF", "no colours")
local cm = buffer.tostring(Off.encode(scene, { comment = "made\nhere" }) :: buffer)
t:check(string.find(cm, "STCNOFF\n# made here\n", 1, true) ~= nil, "comment option")
end
do
local function code(s: string, opts: any?): string?
local _, e = decode(s, opts)
return e and e.code
end
t:check(decode("OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n"), "a minimal file")
t:eq(code("PFF\n3 1 0\n"), Err.CORRUPT, "bad keyword")
t:eq(code("4OFF\n1 0 0\n0 0 0 1\n"), Err.UNSUPPORTED, "4OFF")
t:eq(code("nOFF\n4\n1 0 0\n0 0 0 0\n"), Err.UNSUPPORTED, "nOFF")
t:eq(code("OFF BINARY\n"), Err.UNSUPPORTED, "binary OFF")
t:eq(code("OFF\n"), Err.TRUNCATED, "no counts")
t:eq(code("# nothing\n\n"), Err.TRUNCATED, "empty")
t:eq(code("OFF\nx 1 0\n"), Err.CORRUPT, "bad counts")
t:eq(code("OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n"), Err.TRUNCATED, "face missing")
t:eq(code("OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 3\n"), Err.CORRUPT, "index out of range")
t:eq(code("OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n4 0 1 2\n"), Err.CORRUPT, "short face")
t:eq(code("OFF\n3 1 0\n0 0 0\n1 q 0\n0 1 0\n3 0 1 2\n"), Err.CORRUPT, "bad number")
t:eq(code("OFF\n3 1 0\n0 0 0\n1 0\n0 1 0\n3 0 1 2\n"), Err.CORRUPT, "short vertex")
t:eq(code("COFF\n3 1 0\n0 0 0 1 2\n1 0 0 1 2\n0 1 0 1 2\n3 0 1 2\n"), Err.CORRUPT, "two colour values")
t:eq(code("COFF\n3 1 0\n0 0 0 300 2 3\n1 0 0 1 2 3\n0 1 0 1 2 3\n3 0 1 2\n"), Err.CORRUPT, "colour over 255")
t:eq(code("OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n", { maxVertices = 2 }), Err.LIMIT, "maxVertices")
t:eq(code("OFF\n3 1 0\n0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n", { maxFaces = 0 }), Err.LIMIT, "maxFaces")
t:eq(code("OFF\n10000000 0 0\n1 2 3\n"), Err.TRUNCATED, "counts over the data")
local _, a1 = Off.encode({} :: any)
t:check(a1 and a1.code == Err.ARG, "encode a non-scene: " .. tostring(a1))
local nan = Types.Scene.fromPrimitive({ attributes = { POSITION = Types.Scene.accessor(nil, "f32", 3, 1) }, mode = "points" })
buffer.writef32(nan.meshes[1].primitives[1].attributes.POSITION.data, 0, math.huge)
local _, a2 = Off.encode(nan)
t:check(a2 and a2.code == Err.ARG, "infinite coordinate: " .. tostring(a2))
t:eq(Off.sniff(Bytes.reader(buffer.fromstring("solid x\n"))), 0, "sniff an STL")
end
do
local samples = {}
for _, c in Fx.cases do
table.insert(samples, c.data)
end
t:fuzz("decode", function(b: buffer)
return Off.decode(Bytes.reader(b))
end, samples, { mutations = 20 })
t:fuzz("decode polygons + re-encode", function(b: buffer)
local s = Off.decode(Bytes.reader(b), { triangulate = false })
if s then
return Off.encode(s)
end
return nil
end, samples, { mutations = 6, garbage = 20 })
end
t:done()