Files
rosetta/tests/model/ply.luau
T

380 lines
17 KiB
Luau

-- 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("@rosetta/model/ply")
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/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 } = { ["<f4"] = buffer.readf32, u1 = buffer.readu8, ["<u2"] = buffer.readu16 }
local SIZE: { [string]: number } = { ["<f4"] = 4, u1 = 1, ["<u2"] = 2 }
for _, spec in Scenes do
local scene = build(spec)
for _, enc in ENCODINGS do
local label = spec.name .. "/" .. enc
local out, err = Ply.encode(scene, { format = enc })
t:check(out, label .. ": " .. tostring(err))
local back = decode(out, { triangulate = false })
local p = back.meshes[1].primitives[1]
for _, prop in spec.props do
local where = ATTR_OF[prop.name]
local a = if where then p.attributes[where[1]] else p.attributes["_" .. prop.name]
t:check(a ~= nil, label .. ": property " .. prop.name)
local comp = if where then where[2] :: number else 0
local rd, sz = READ[prop.dtype], SIZE[prop.dtype]
local vals = buffer.fromstring(prop.values)
local n = buffer.len(vals) // sz
t:eq(a.count, n, label .. ": " .. prop.name .. " count")
local csz = Types.COMPONENT_SIZE[a.type]
for i = 0, n - 1 do
local got = rd(a.data, (i * a.components + comp) * csz)
local want = rd(vals, i * sz)
if prop.tol then
t:check(math.abs(got - want) <= prop.tol, label .. ": " .. prop.name .. " value")
else
t:eq(got, want, label .. ": " .. prop.name .. " value")
end
end
end
if spec.faces then
local sizes = buffer.fromstring(spec.faceSizes)
local corners = buffer.fromstring(spec.faceCorners)
t:check(buffer.tostring(p.indices.data) == buffer.tostring(corners), label .. ": corners")
if p.mode == "polygons" then
t:check(buffer.tostring(p.faces.data) == buffer.tostring(sizes), label .. ": face sizes")
else
t:eq(p.mode, "triangles", label .. ": mode")
t:eq(p.indices.count, buffer.len(sizes) // 4 * 3, label .. ": all triangles")
end
else
t:eq(p.mode, "points", label .. ": points")
end
-- identity scenes: every attribute comes back as it was (COLOR_0 of the merged scene filled with white)
if not spec.transformed and #spec.meshes == 1 then
for name, a in scene.meshes[1].primitives[1].attributes do
t:check(buffer.tostring(p.attributes[name].data) == buffer.tostring(a.data), label .. ": " .. name .. " round trip")
t:eq(p.attributes[name].type, a.type, label .. ": " .. name .. " type")
end
end
end
end
-- options and meta: comments, extra elements written back, faceAttributes of a triangulated mesh
do
local scene = build(Scenes[1])
scene.meta.ply = { comments = { "first" }, objInfo = { "info line" } }
local out = Ply.encode(scene, { format = "ascii", comments = { "second" } }) :: buffer
local s = buffer.tostring(out)
t:check(string.find(s, "comment first\ncomment second\nobj_info info line\n", 1, true) ~= nil, "comments and obj_info")
local back = decode(out)
t:eq(back.meta.ply.comments[2], "second", "comment option")
local _, e = Ply.encode(scene, { format = "binary" } :: any)
t:check(e and e.code == Err.ARG, "bad format option: " .. tostring(e))
-- the default is binary little endian
t:check(string.find(buffer.tostring(Ply.encode(scene) :: buffer), "format binary_little_endian 1.0", 1, true) ~= nil, "default encoding")
-- a scene without nodes uses its meshes
local bare = Types.Scene.new()
bare.meshes[1] = scene.meshes[1]
t:eq(decode(Ply.encode(bare) :: buffer).meta.source.vertices, 20, "a scene without nodes")
end
-- 4. error codes
do
local function code(s: string, opts: any?): string?
local _, e = decode(s, opts)
return e and e.code
end
local H = "ply\nformat ascii 1.0\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\nelement face 1\nproperty list uchar int vertex_indices\nend_header\n"
t:check(decode(H .. "0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n"), "a minimal ascii file")
t:eq(code("plx\n"), Err.CORRUPT, "bad signature")
t:eq(code("ply\nformat ascii 2.0\nend_header\n"), Err.UNSUPPORTED, "version 2")
t:eq(code("ply\nformat text 1.0\nend_header\n"), Err.UNSUPPORTED, "unknown format")
t:eq(code("ply\nformat ascii 1.0\nelement vertex 1\n"), Err.TRUNCATED, "no end_header")
t:eq(code("ply\nformat ascii 1.0\nproperty float x\nend_header\n"), Err.CORRUPT, "property before element")
t:eq(code("ply\nformat ascii 1.0\nelement vertex 1\nproperty float x\nproperty float x\nend_header\n0 0\n"), Err.CORRUPT, "duplicate property")
t:eq(code("ply\nformat ascii 1.0\nelement vertex 1\nproperty quad x\nend_header\n"), Err.CORRUPT, "unknown type")
t:eq(code("ply\nformat ascii 1.0\nelement vertex 1\nproperty list uchar float x\nend_header\n1 0\n"), Err.UNSUPPORTED, "list in the vertex element")
t:eq(code("ply\nformat ascii 1.0\nbanana\nend_header\n"), Err.CORRUPT, "unknown header line")
t:eq(code("ply\nformat ascii 1.0\nelement face 0\nend_header\n"), Err.CORRUPT, "no vertex element")
t:eq(code(H .. "0 0 0\n1 0 0\n0 1 0\n3 0 1 3\n"), Err.CORRUPT, "ascii index out of range")
t:eq(code(H .. "0 0 0\n1 0 0\n0 1 0\n3 0 1 -1\n"), Err.CORRUPT, "negative index")
t:eq(code(H .. "0 0 0\n1 0 0\n0 1 0\n3 0 1\n"), Err.TRUNCATED, "ascii cut")
t:eq(code(H .. "0 0 0\n1 zz 0\n0 1 0\n3 0 1 2\n"), Err.CORRUPT, "bad number")
t:eq(code(H .. "0 0 0\n1 0 0\n0 1 0\n300 0 1 2\n"), Err.CORRUPT, "uchar count over 255")
t:eq(code(H .. "0 0 0\n1 0 0\n0 1 0\n3 0 1 2.5\n"), Err.CORRUPT, "fractional index")
t:eq(code(H .. "0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n", { maxVertices = 2 }), Err.LIMIT, "maxVertices")
t:eq(code(H .. "0 0 0\n1 0 0\n0 1 0\n3 0 1 2\n", { maxFaces = 0 }), Err.LIMIT, "maxFaces")
t:eq(code("ply\nformat ascii 1.0\nelement vertex 1000000\nproperty float x\nend_header\n1\n"), Err.TRUNCATED, "ascii count over the data")
local B = "ply\nformat binary_little_endian 1.0\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\nelement face 1\nproperty list uchar int vertex_indices\nend_header\n"
local verts = string.rep("\0", 36)
t:check(decode(B .. verts .. "\3\0\0\0\0\1\0\0\0\2\0\0\0"), "a minimal binary file")
t:eq(code(B .. verts .. "\3\0\0\0\0\1\0\0\0\3\0\0\0"), Err.CORRUPT, "binary index out of range")
t:eq(code(B .. verts .. "\3\0\0\0\0\1\0\0\0\2\0\0"), Err.TRUNCATED, "binary list cut")
t:eq(code(B .. string.sub(verts, 1, 30)), Err.TRUNCATED, "binary rows cut")
t:eq(code("ply\nformat binary_big_endian 1.0\nelement vertex 4000000000\nproperty float x\nend_header\n", { maxVertices = 2 ^ 40, maxFaces = 2 ^ 40 }), Err.TRUNCATED, "huge count")
local _, a1 = Ply.encode({} :: any)
t:check(a1 and a1.code == Err.ARG, "encode a non-scene: " .. tostring(a1))
local noPos = Types.Scene.fromPrimitive({ attributes = {}, mode = "points" })
local _, a2 = Ply.encode(noPos)
t:check(a2 and a2.code == Err.ARG, "no POSITION: " .. tostring(a2))
end
-- 5. fuzz
do
local samples = {}
for _, c in Fx.cases do
if #c.data < 30000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Ply.decode(Bytes.reader(b))
end, samples, { mutations = 30 })
t:fuzz("decode polygons + re-encode", function(b: buffer)
local s = Ply.decode(Bytes.reader(b), { triangulate = false })
if s then
return Ply.encode(s, { format = "ascii" })
end
return nil
end, samples, { mutations = 6, garbage = 20 })
end
-- throughput: a 500 x 500 grid (250k vertices, 498k triangles), binary little endian
do
local W = 500
local pos = buffer.create(W * W * 12)
for i = 0, W * W - 1 do
buffer.writef32(pos, i * 12, i % W)
buffer.writef32(pos, i * 12 + 4, i // W)
buffer.writef32(pos, i * 12 + 8, (i * 7) % 13)
end
local nt = (W - 1) * (W - 1) * 2
local idx = buffer.create(nt * 12)
local k = 0
for y = 0, W - 2 do
for x = 0, W - 2 do
local a = y * W + x
for _, v in { a, a + 1, a + W + 1, a, a + W + 1, a + W } do
buffer.writeu32(idx, k * 4, v)
k += 1
end
end
end
local scene = Types.Scene.fromPrimitive({ attributes = { POSITION = Types.Scene.accessor(pos, "f32", 3, W * W) }, indices = Types.Scene.accessor(idx, "u32", 1, nt * 3), mode = "triangles" })
local t0 = os.clock()
local data = Ply.encode(scene) :: buffer
local te = os.clock() - t0
t0 = os.clock()
local back = Ply.decode(Bytes.reader(data))
local dt = os.clock() - t0
t:check(back and back.meshes[1].primitives[1].indices.count == nt * 3, "bench decode")
t:check(buffer.tostring(back.meshes[1].primitives[1].indices.data) == buffer.tostring(idx), "bench round trip")
t:note("binary LE decode: %d vertices + %d triangles (%.1f MB) in %.3f s, %.0f MB/s (encode %.2f s)", W * W, nt, buffer.len(data) / 1e6, dt, buffer.len(data) / 1e6 / math.max(dt, 1e-6), te)
end
t:done()