-- model/x3d tests: assimp's X3D models, VTK / assimp exports and hand-written files against assimp and exact values. -- Fixtures: tools/fixtures/x3d.py. local T = require("@tests/T") local F = require("@rosetta") local X3d = require("@rosetta/model/x3d") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local C = require("@tests/model/_vrml_common") local Cases = require("@tests/gen/x3d_cases") local VrmlCases = require("@tests/gen/vrml_cases") local GltfCases = require("@tests/gen/vrml_gltf") local Xcheck = require("@tests/gen/x3d_xcheck") local Gltf = require("@rosetta/model/gltf") local Vrml = require("@rosetta/model/vrml") local t = T.suite("x3d") local near = C.near local function decode(data: any, opts: any?): (any, any) local b = if type(data) == "string" then buffer.fromstring(data) else data return X3d.decode(Bytes.reader(b), opts) end t:eq(X3d.name, "x3d", "name") t:eq(X3d.category, "model", "category") t:check(X3d.caps.decode and X3d.caps.encode, "contract") -- assimp's own quirks (its reading is not the specification's): USE'd groups lose their parents' transforms; its X3D -- exporter writes points and lines as 1- / 2-vertex faces (not valid IndexedFaceSet faces, dropped here) local QUIRK = { ["assimp/X3D/ComputerKeyboard.x3d"] = "all", ["assimp-export/assimp_MeshPrimitiveModes.x3d"] = "all" } local scenes, byName = {}, {} local checked = { assimp = 0, none = 0 } for i, c in Cases do local scene, err = decode(c.data, { files = c.files, triangulate = false }) if string.find(c.name, "%.x3db$") then t:check(scene == nil and err and err.code == Err.UNSUPPORTED, c.name .. ": binary X3D is UNSUPPORTED: " .. tostring(err)) continue end t:check(scene, c.name .. ": decode: " .. tostring(err)) if not scene then continue end scenes[i], byName[c.name] = scene, scene local a = c.assimp -- (VTK's exporter leaves out: assimp then reads nothing) local unclosed = a.meshes == 0 and not string.find(c.data, "", 1, true) if a.skip or a.failed or unclosed or string.find(c.name, "^feature/") then checked.none += 1 continue end local s = C.stats(scene) local quirk = QUIRK[c.name] if quirk ~= "all" then if quirk ~= "counts" then t:eq(s.prims, a.meshes, c.name .. ": meshes (assimp: one per placed shape)") end if not c.primitives and quirk ~= "counts" then t:eq(s.polys + s.lines + s.points, a.faces, c.name .. ": faces (assimp)") end t:check(#scene.materials <= a.materials, c.name .. ": materials (assimp counts one per use)") if a.min then t:check(C.boundsNear(s.lo, s.hi, a.min, a.max, if c.primitives then 5e-3 else 1e-5), c.name .. ": world bounds (assimp) " .. C.fmt(s.lo, s.hi) .. " vs " .. C.fmt(a.min, a.max)) end end checked.assimp += 1 end t:note("%d files: %d against assimp, %d without a reference", #Cases, checked.assimp, checked.none) do local kb = byName["assimp/X3D/ComputerKeyboard.x3d"] if kb then local s = C.stats(kb) t:eq(#kb.meshes, 3, "keyboard: three extrusions (the keys are USE'd)") t:check(s.prims > 50, "keyboard: every USE placed: " .. s.prims) t:eq(#kb.animations, 1, "keyboard: the push animation") t:check(#kb.animations[1].channels == s.prims - 2, "keyboard: one channel per escape-button instance") t:check(near(s.hi[3], 0.980018, 1e-5) and near(s.lo[2], -0.0002, 1e-3), "keyboard: the base under its rotation") end local hw, hv = byName["assimp/X3D/HelloWorld.x3d"], byName["assimp/X3DV/HelloWorld.x3dv"] if hw and hv then local a, b = C.stats(hw), C.stats(hv) t:check(a.polys == b.polys and C.boundsNear(a.lo, a.hi, b.lo, b.hi, 1e-9), ".x3dv = .x3d: geometry") t:eq(hv.materials[1].baseColor[2], hw.materials[1].baseColor[2], ".x3dv = .x3d: material") t:check(hv.textures[1].data and hw.textures[1].data, ".x3dv / .x3d: texture from files") t:eq(hv.meta.source.profile, "Immersive", ".x3dv: PROFILE") t:eq(hw.meta.source.profile, "Immersive", ".x3d: profile") t:eq(hv.meta.source.meta.title, "HelloWorld.x3d", ".x3dv: META") t:eq(hw.meta.source.meta.creator, "Don Brutzman", ".x3d: head meta") end local ls = byName["assimp/X3D/IndexedLineSet.x3d"] if ls then local p = ls.meshes[1].primitives[1] t:check(p.mode == "lines" and p.indices.count == 8, "IndexedLineSet: a closed polyline as 4 segments") t:check(ls.materials[1].emissive[1] == 1, "IndexedLineSet: emissive colour") end local sets = byName["feature/feat_sets.x3d"] if sets then local m = sets.meshes local function tris(k: number): number local p = m[k].primitives[1] return if p.mode == "polygons" then p.faces.count else p.indices.count // 3 end t:eq(tris(1), 2, "IndexedTriangleSet") local col = m[1].primitives[1].attributes.COLOR_0 t:check(col and col.components == 4 and buffer.readf32(col.data, (2 * 4 + 3) * 4) == 0.5, "ColorRGBA alpha") t:check(sets.materials[m[1].primitives[1].material].doubleSided, "solid false") t:eq(tris(2), 3, "TriangleFanSet fanCount") t:eq(tris(3), 3, "IndexedTriangleStripSet runs") t:check(m[4].primitives[1].faces.count == 1 and m[4].primitives[1].indices.count == 4, "IndexedQuadSet: one quad") t:eq(tris(5), 2, "TriangleSet") t:check(m[6].primitives[1].mode == "lines" and m[6].primitives[1].indices.count == 6, "LineSet vertexCount") -- strips alternate their winding: every triangle of the strip faces +z local p = m[3].primitives[1] local ok = true local P = p.attributes.POSITION local function v(i: number): (number, number) local k = buffer.readu32(p.indices.data, i * 4) return buffer.readf32(P.data, k * 12), buffer.readf32(P.data, k * 12 + 4) end local at = 0 for f = 0, p.faces.count - 1 do local ax, ay = v(at) local bx, by = v(at + 1) local cx, cy = v(at + 2) ok = ok and (bx - ax) * (cy - ay) - (by - ay) * (cx - ax) > 0 at += buffer.readu32(p.faces.data, f * 4) end t:check(ok, "IndexedTriangleStripSet: counter-clockwise triangles") end local pr = byName["feature/feat_proto.x3d"] if pr then local s = C.stats(pr) t:check(C.boundsNear(s.lo, s.hi, { -1.1, -2, -0.5 }, { 1.1, 5, 0.5 }, 1e-6), "protos: instances placed and sized " .. C.fmt(s.lo, s.hi)) t:eq(pr.meta.unit, 0.01, "UNIT length") t:eq(pr.meta.source.meta.title, "protos", "head meta") local kinds = {} for _, m in pr.materials do if m.extensions and m.extensions.KHR_materials_unlit then kinds.unlit = m elseif m.metallic == 0.25 then kinds.physical = m elseif m.baseColor[3] == 1 and m.baseColor[1] == 0 then kinds.default = m end end t:check(kinds.physical and kinds.physical.roughness == 0.75, "PhysicalMaterial") t:check(kinds.unlit and kinds.unlit.baseColor[2] == 1, "UnlitMaterial") t:check(kinds.default ~= nil, "SFNode field default (the proto's appearance)") t:eq(#pr.materials, 3, "the default appearance is shared by both instances") local cam = pr.cameras[1] t:check(cam and cam.type == "orthographic" and cam.xmag == 2 and cam.ymag == 1, "OrthoViewpoint") local a = pr.animations[1] t:check(a and a.name == "Clock" and #a.channels == 1 and a.channels[1].path == "rotation", "ROUTE elements -> animation") t:check(a and near(buffer.readf32(a.channels[1].input.data, 8), 2, 1e-6), "key times x cycleInterval") -- quaternion keys stay on one hemisphere (0, pi, 2 pi about y) local q = a and a.channels[1].output t:check(q and buffer.readf32(q.data, 2 * 16 + 12) < 0, "rotation keys continuous") end local cl = byName["feature/feat_classic.x3dv"] if cl then local s = C.stats(cl) t:check(C.boundsNear(s.lo, s.hi, { 1, 2, 3 }, { 2, 3, 4 }, 1e-6), "classic: DEF Transform") t:eq(cl.meta.unit, 0.001, "classic: UNIT") t:eq(cl.meta.source.components[1], "Geometry2D:1", "classic: COMPONENT") t:eq(cl.meta.source.meta.title, "classic", "classic: META") t:eq(cl.meshes[1].primitives[1].attributes.COLOR_0.components, 4, "classic: ColorRGBA") end end local function compare(a: any, b: any, label: string) local sa, sb = C.stats(a), C.stats(b) t:eq(sb.prims, sa.prims, label .. ": placed primitives") t:eq(sb.polys, sa.polys, label .. ": faces") t:eq(sb.lines, sa.lines, label .. ": line segments") t:eq(sb.points, sa.points, label .. ": points") if sa.prims > 0 and sa.lo[1] <= sa.hi[1] then t:check(C.boundsNear(sa.lo, sa.hi, sb.lo, sb.hi, 1e-5), label .. ": bounds") t:check(near(sa.area, sb.area, 1e-4), label .. ": area") end t:eq(#b.materials, #a.materials, label .. ": materials") t:eq(#b.cameras, #a.cameras, label .. ": cameras") t:eq(#b.lights, #a.lights, label .. ": lights") t:eq(#b.animations, #a.animations, label .. ": animations") for k, cam in a.cameras do t:eq(b.cameras[k].type, cam.type, label .. ": camera type") end end local rt = 0 for i, c in Cases do local a = scenes[i] if a then for _, enc in { "xml", "x3dv" } do local files = {} local out, err = X3d.encode(a, { files = files, encoding = enc :: any }) t:check(out, c.name .. ": encode " .. enc .. ": " .. tostring(err)) local b = out and decode(out, { files = files, triangulate = false }) t:check(b, c.name .. ": decode our " .. enc) if b then compare(a, b, c.name .. " (" .. enc .. ")") rt += 1 end end end end t:note("%d round trips", rt) local sources: { [string]: { any } } = { xcase = {}, wcase = {}, xgltf = {} } for i, c in Cases do sources.xcase[i] = { scenes[i], c } end for i, c in VrmlCases do sources.wcase[i] = { Vrml.decode(Bytes.reader(buffer.fromstring(c.data)), { files = c.files, triangulate = false }), c } end for i, c in GltfCases do sources.xgltf[i] = { Gltf.decode(Bytes.reader(buffer.fromstring(c.data)), { files = c.files }), c } end -- assimp loses its place in the tree after an empty (a leaf node: an empty, a locator) local function hasEmpty(scene: any): boolean for _, n in scene.nodes do if #n.children == 0 and not n.mesh and not n.camera and not n.light then return true end end return false end local xc, skipped = 0, 0 for _, x in Xcheck do local a = x.assimp local kind = if x.xcase then "xcase" elseif x.wcase then "wcase" else "xgltf" local src = sources[kind][x[kind]] if src and src[1] and (a.failed or a.meshes == 0) and hasEmpty(src[1]) then skipped += 1 elseif src and src[1] and not a.failed then local s = C.stats(src[1]) t:eq(a.meshes, s.prims, x.name .. ": meshes (assimp reads our file)") t:eq(a.faces, s.polys + s.lines + s.points, x.name .. ": faces (assimp reads our file)") -- (shared meshes are written once and USE'd: assimp places USE'd nodes without their parents' transforms) -- (MotionCaptureROM: assimp misplaces this deep hierarchy; an independent evaluation of our file's transforms -- agrees with ours) local deep = src[2].name == "assimp/MotionCaptureROM.WRL" if a.min and s.lo[1] <= s.hi[1] and s.prims == s.unique and not deep then t:check(C.boundsNear(s.lo, s.hi, a.min, a.max, 1e-4), x.name .. ": bounds (assimp reads our file) " .. C.fmt(s.lo, s.hi) .. " vs " .. C.fmt(a.min, a.max)) end xc += 1 elseif src and src[1] then t:check(false, x.name .. ": assimp cannot read our file (" .. src[2].name .. ")") end end t:note("%d of our files read by assimp (%d skipped: empty nodes)", xc, skipped) do local hw = Cases[2] t:eq(F.detect(hw.data), "x3d", "F.detect .x3d") t:eq(F.detect(Cases[5].data), "x3d", "F.detect .x3dv") local s = F.load(hw.data, { files = hw.files }) t:check(s and s.meta.source.format == "x3d", "F.load") local x3dv = F.save(s, nil, { format = "x3dv" }) t:check(x3dv and buffer.readstring(x3dv, 0, 5) == "#X3D ", "F.save format x3dv: the classic encoding") local xml = F.save(s, nil, { format = "x3d" }) t:check(xml and string.find(buffer.tostring(xml), "") t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "mismatched XML: " .. tostring(e1)) local _, e2 = decode("") t:check(e2 and e2.code == Err.CORRUPT, "not X3D: " .. tostring(e2)) -- VTK leaves the root element open: read anyway local open = decode("\n") t:check(open and #open.meshes == 1, "an unclosed root (VTK's exporter)") local _, e3 = X3d.encode({} :: any) t:check(e3 and e3.code == Err.ARG, "encode junk") -- containerField and USE in XML local u = decode([[ ]]) local us = u and C.stats(u) t:check(us and #u.meshes == 2 and us.prims == 3 and near(us.hi[1], 6, 1e-9) and near(us.hi[2], 2, 1e-6), "USE / containerField / Switch in XML") end do local samples = {} for _, c in Cases do if #c.data < 40000 then table.insert(samples, c.data) end end t:fuzz("decode", function(b: buffer) local s = X3d.decode(Bytes.reader(b), { files = {} }) if s then X3d.encode(s) end return s end, samples, { mutations = 16, keepHeader = 16 }) end t:done()