-- model/ply tests: files written by plyfile (ascii / binary LE / BE, every property type and spelling, odd orders, -- n-gons up to 300 vertices, face properties and lists, extra elements, comments, point clouds) and by assimp, against -- plyfile's reading mapped to the Scene conventions (CRC-32 of every attribute, the indices triangulated or as -- polygons, face data, raw elements); our encoder's output read back by plyfile and assimp (verdicts from the fixture -- step); round trips in every encoding (fixtures and generated scenes, polygons and points); the contract, error -- codes, the fuzz no-throw pass and a binary decode throughput note. Fixtures: tools/fixtures/ply.py. local T = require("@tests/T") local Ply = require("@formats/model/ply") local Bytes = require("@formats/core/Bytes") local Crc32 = require("@formats/core/Crc32") local Err = require("@formats/core/Err") local Types = require("@formats/Types") local Fx = require("@tests/gen/ply") local Scenes = require("@tests/gen/ply_scenes") local t = T.suite("ply") local function decode(data: any, opts: any?): (any, any) local b = if type(data) == "string" then buffer.fromstring(data) else data return Ply.decode(Bytes.reader(b), opts) end local ENCODINGS = { "ascii", "binary_little_endian", "binary_big_endian" } -- 1. the contract t:eq(Ply.name, "ply", "name") t:eq(Ply.category, "model", "category") t:check(table.find(Ply.extensions, "ply") ~= nil, "extensions") t:check(Ply.caps.decode and Ply.caps.encode, "caps") -- a decoded scene against a reference case (triangulated) local function verify(scene: any, c: any, label: string) t:check(scene.kind == "scene" and #scene.meshes == 1 and #scene.meshes[1].primitives == 1, label .. ": one primitive") local p = scene.meshes[1].primitives[1] local n = 0 for name, want in c.attrs do local a = p.attributes[name] t:check(a ~= nil, label .. ": attribute " .. name) t:eq(a.type, want.type, label .. ": " .. name .. " type") t:eq(a.components, want.components, label .. ": " .. name .. " components") t:eq(a.count, c.vertices, label .. ": " .. name .. " count") t:eq(Crc32.of(a.data), want.crc, label .. ": " .. name .. " crc") n += 1 end local m = 0 for _ in p.attributes do m += 1 end t:eq(m, n, label .. ": no other attributes") if c.faces > 0 then t:eq(p.mode, "triangles", label .. ": mode") t:eq(Crc32.of(p.indices.data), if c.allTri then c.corners_crc else c.tri_crc, label .. ": indices crc") t:eq(p.indices.count, c.tris * 3, label .. ": index count") local fa = c.faceAttributes local ex = p.extras or {} if next(fa) ~= nil and not c.allTri then t:eq(Crc32.of(ex.faceOfTriangle.data), c.fot_crc, label .. ": faceOfTriangle") else t:check(ex.faceOfTriangle == nil, label .. ": no faceOfTriangle") end for name, want in fa do if want.list then local fl = ex.faceLists[name] t:eq(Crc32.of(fl.sizes.data), want.sizes_crc, label .. ": face list " .. name .. " sizes") t:eq(Crc32.of(fl.items.data), want.items_crc, label .. ": face list " .. name .. " items") else local a = ex.faceAttributes[name] t:eq(a.type, want.type, label .. ": face attribute " .. name .. " type") t:eq(Crc32.of(a.data), want.crc, label .. ": face attribute " .. name) end end else t:eq(p.mode, "points", label .. ": a point cloud") t:check(p.indices == nil, label .. ": no indices") end local meta = scene.meta.ply t:eq(#meta.comments, #c.comments, label .. ": comment count") for i, s in c.comments do t:eq(meta.comments[i], s, label .. ": comment") end t:eq(#meta.objInfo, #c.objInfo, label .. ": obj_info count") for i, s in c.objInfo do t:eq(meta.objInfo[i], s, label .. ": obj_info") end t:eq(table.concat(meta.order, ","), table.concat(c.order, ","), label .. ": element order") t:eq(#meta.elements, #c.elements, label .. ": raw elements") for i, want in c.elements do local el = meta.elements[i] t:eq(el.name, want.name, label .. ": element name") t:eq(el.count, want.count, label .. ": element count") for k, wp in want.props do local pr = el.properties[k] t:eq(pr.name, wp.name, label .. ": element property") if wp.list then t:eq(Crc32.of(pr.sizes), wp.sizes_crc, label .. ": " .. el.name .. "." .. pr.name .. " sizes") t:eq(Crc32.of(pr.values), wp.values_crc, label .. ": " .. el.name .. "." .. pr.name .. " items") else t:eq(Crc32.of(pr.values), wp.crc, label .. ": " .. el.name .. "." .. pr.name) end end end end -- 2. every fixture against plyfile; 3. round trips of every fixture in every encoding for _, c in Fx.cases do local label = c.name local scene, err = decode(c.data) t:check(scene, label .. ": " .. tostring(err)) t:eq(scene.meta.source.format, "ply", label .. ": meta.source.format") t:eq(scene.meta.source.encoding, c.encoding, label .. ": encoding") t:eq(scene.meta.source.vertices, c.vertices, label .. ": vertices") t:eq(scene.meta.source.faces, c.faces, label .. ": faces") verify(scene, c, label) t:eq(Ply.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, label .. ": sniff") local big = buffer.create(#c.data + 9) buffer.writestring(big, 4, c.data) local s2 = Ply.decode(Bytes.reader(big, 4, #c.data)) t:check(s2 and Crc32.of(s2.meshes[1].primitives[1].attributes.POSITION.data) == c.attrs.POSITION.crc, label .. ": decode from a sub-range") -- polygons kept 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 mode") t:eq(Crc32.of(pp.indices.data), c.corners_crc, label .. ": polygon corners") t:eq(Crc32.of(pp.faces.data), c.sizes_crc, label .. ": polygon sizes") end for _, enc in ENCODINGS do local out, eerr = Ply.encode(scene, { format = enc }) t:check(out, label .. " -> " .. enc .. ": " .. tostring(eerr)) local back, berr = decode(out) t:check(back, label .. " -> " .. enc .. ": decode: " .. tostring(berr)) t:eq(back.meta.source.encoding, enc, label .. " -> " .. enc .. ": encoding") local cc = table.clone(c) if c.faces > 0 and not c.allTri then -- written as triangles: the face data goes through faceOfTriangle, one row per triangle cc.faces, cc.allTri, cc.corners_crc = c.tris, true, c.tri_crc cc.faceAttributes = {} local bp = back.meshes[1].primitives[1] local fa = bp.extras and bp.extras.faceAttributes for name in c.faceAttributes do if not c.faceAttributes[name].list then t:check(fa and fa[name] and fa[name].count == c.tris, label .. ": face attribute per triangle") end end end verify(back, cc, label .. " -> " .. enc) -- polygons in, polygons out local pout = Ply.encode(poly, { format = enc }) :: buffer local pback = decode(pout, { triangulate = false }) verify(decode(pout), c, label .. " -> " .. enc .. " (polygons)") local bp = pback.meshes[1].primitives[1] t:eq(bp.mode, pp.mode, label .. ": polygons round trip mode") if pp.indices then t:check(buffer.tostring(bp.indices.data) == buffer.tostring(pp.indices.data), label .. ": polygons round trip indices") end if pp.faces then t:check(buffer.tostring(bp.faces.data) == buffer.tostring(pp.faces.data), label .. ": polygons round trip sizes") end -- binary output of a binary input with the same byte order is the same file, header included if enc == c.encoding and enc ~= "ascii" and string.sub(label, 1, 6) ~= "assimp" then t:check(buffer.tostring(pout) == c.data, label .. ": byte-exact re-encode") end end end t:note("%d files match plyfile and round-trip in 3 encodings", #Fx.cases) for _, v in Fx.ours do t:check(v.ok, "plyfile / assimp read our " .. v.name .. ": " .. v.detail) end t:note("%d encodings read back by plyfile and assimp", #Fx.ours) -- the encoder scenes: decode(encode()) against the properties computed by the generator 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 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 local ATTR_OF: { [string]: { string | number } } = { x = { "POSITION", 0 }, y = { "POSITION", 1 }, z = { "POSITION", 2 }, nx = { "NORMAL", 0 }, ny = { "NORMAL", 1 }, nz = { "NORMAL", 2 }, red = { "COLOR_0", 0 }, green = { "COLOR_0", 1 }, blue = { "COLOR_0", 2 }, alpha = { "COLOR_0", 3 }, s = { "TEXCOORD_0", 0 }, t = { "TEXCOORD_0", 1 }, } local READ: { [string]: (buffer, number) -> number } = { ["