merge ws/3d3-c (model/pmx, model/vrml, model/x3d, codecs/model/Vrml); README model list
This commit is contained in:
commit
94783a86d1
29 files changed
+6735
-6
No files matched your search
@@ -0,0 +1,148 @@
|
||||
-- codecs/model/Vrml tests: the classic-encoding lexer and parser (comments, commas, string escapes, hex / exponent
|
||||
-- numbers, DEF / USE, PROTO with IS fields, nested PROTOs and ROUTEs in bodies, EXTERNPROTO, Script interfaces,
|
||||
-- IMPORT / EXPORT, the X3D header statements), the XML encoding's adapter (containerField, ProtoDeclare /
|
||||
-- ProtoInstance, IS / connect), typed field reads of raw XML attributes, detection, errors, the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local Vrml = require("@formats/codecs/model/Vrml")
|
||||
local Fields = require("@formats/codecs/model/Vrml/Fields")
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local t = T.suite("vrml codec")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ lexer / parser
|
||||
do
|
||||
local g = assert(Vrml.parse([[#VRML V2.0 utf8
|
||||
# a comment, then commas as blanks
|
||||
DEF A Transform { translation 1,2,3 # trailing comment
|
||||
children [ Shape { geometry IndexedFaceSet { coordIndex [ 0, 1, 2, -1 ] coord Coordinate { point [ 0 0 0, 1e1 0 0, .5 -2.5E-1 +3 ] } } } ] }
|
||||
WorldInfo { title "say \"hi\" \\ there" info [ "a" "b" ] }
|
||||
DEF P PixelTexture { image 1 1 4 0xFF8000FF }
|
||||
USE A
|
||||
]]))
|
||||
t:eq(#g.roots, 4, "statements")
|
||||
t:eq(g.header.version, "2.0", "header")
|
||||
local a = g.roots[1]
|
||||
t:eq(a.def, "A", "DEF")
|
||||
t:eq(table.concat(Fields.vec(a, "translation", { 0, 0, 0 }), " "), "1 2 3", "commas are blanks")
|
||||
local coord = Fields.node(Fields.node(Fields.nodes(a, "children")[1], "geometry"), "coord")
|
||||
t:eq(table.concat(Fields.nums(coord, "point") :: { number }, " "), "0 0 0 10 0 0 0.5 -0.25 3", "exponents, leading dot, plus sign")
|
||||
t:eq(Fields.str(g.roots[2], "title"), 'say "hi" \\ there', "string escapes")
|
||||
t:eq(#Fields.strs(g.roots[2], "info"), 2, "MFString")
|
||||
t:eq((Fields.nums(g.roots[3], "image") :: { number })[4], 0xFF8000FF, "hex numbers")
|
||||
t:check(g.roots[4] == a, "USE is the same node")
|
||||
end
|
||||
|
||||
do
|
||||
local g = assert(Vrml.parse([[#VRML V2.0 utf8
|
||||
PROTO Outer [ field SFFloat size 1 exposedField SFColor tint 1 1 1 eventIn SFBool flip ] {
|
||||
PROTO Inner [ field SFFloat r 1 ] { Sphere { radius IS r } }
|
||||
DEF G Group { children [ Shape { geometry Inner { r IS size } appearance Appearance { material DEF M Material { diffuseColor IS tint } } } ] }
|
||||
DEF Clock TimeSensor { }
|
||||
DEF I PositionInterpolator { key [ 0 1 ] keyValue [ 0 0 0 1 1 1 ] }
|
||||
ROUTE Clock.fraction_changed TO I.set_fraction
|
||||
ROUTE I.value_changed TO G.set_translation
|
||||
}
|
||||
EXTERNPROTO Far [ field SFFloat x ] [ "far.wrl#Far" "urn:far" ]
|
||||
DEF O1 Outer { size 3 tint 0 1 0 }
|
||||
Outer { }
|
||||
Far { x 2 }
|
||||
DEF S Script { field SFNode target USE O1 eventIn SFTime touch url "javascript: function touch() {}" }
|
||||
IMPORT inl.Thing AS Thing
|
||||
EXPORT S AS Exported
|
||||
]]))
|
||||
t:eq(#g.roots, 4, "proto instances, extern instance, script")
|
||||
local o1, o2 = g.roots[1], g.roots[2]
|
||||
t:eq(o1.type, "Group", "an instance is its body's first node")
|
||||
t:eq(o1.def, "O1", "the instance's DEF name")
|
||||
local function radius(o: any): number
|
||||
local sh = Fields.nodes(o, "children")[1]
|
||||
return Fields.num(Fields.node(sh, "geometry"), "radius", -1)
|
||||
end
|
||||
t:eq(radius(o1), 3, "IS through a nested PROTO (instance value)")
|
||||
t:eq(radius(o2), 1, "IS default")
|
||||
local mat = Fields.node(Fields.node(Fields.nodes(o1, "children")[1], "appearance"), "material")
|
||||
t:eq(table.concat(Fields.vec(mat, "diffuseColor", { 9, 9, 9 }), " "), "0 1 0", "IS on an SFColor")
|
||||
local mat2 = Fields.node(Fields.node(Fields.nodes(o2, "children")[1], "appearance"), "material")
|
||||
t:check(mat ~= mat2, "each instance has its own copy of the body")
|
||||
t:eq(#g.routes, 4, "the body's ROUTEs, per instance")
|
||||
t:check(g.routes[2].to == o1 or g.routes[2].to.fields == o1.fields, "routes point at the instance's nodes")
|
||||
t:eq(g.roots[3].type, "Far", "EXTERNPROTO instance keeps its type")
|
||||
t:check(g.roots[3].extern == true, "EXTERNPROTO instance flagged")
|
||||
t:eq(Fields.node(g.roots[4], "target"), o1, "Script interface field with a USE value")
|
||||
end
|
||||
|
||||
do
|
||||
local g = assert(Vrml.parse("#X3D V3.3 utf8\nPROFILE Immersive\nCOMPONENT CADGeometry:2\nMETA \"author\" \"me\"\nUNIT length foot 0.3048\nGroup { }\n"))
|
||||
t:check(g.header.x3d and g.header.version == "3.3" and g.header.profile == "Immersive", "X3D header, PROFILE")
|
||||
t:eq(g.components[1], "CADGeometry:2", "COMPONENT")
|
||||
t:eq(g.meta.author, "me", "META")
|
||||
t:eq(g.units.length.factor, 0.3048, "UNIT")
|
||||
local s = assert(Vrml.build(g))
|
||||
t:eq(s.meta.unit, 0.3048, "UNIT length -> meta.unit")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ XML adapter
|
||||
do
|
||||
local g = assert(Vrml.graph([[<?xml version="1.0"?>
|
||||
<X3D profile="Interchange" version="4.0"><head><component name="Shaders" level="1"/></head><Scene>
|
||||
<ProtoDeclare name="Ball"><ProtoInterface><field name="r" type="SFFloat" accessType="initializeOnly" value="2"/></ProtoInterface>
|
||||
<ProtoBody><Shape><Sphere><IS><connect nodeField="radius" protoField="r"/></IS></Sphere></Shape></ProtoBody></ProtoDeclare>
|
||||
<Group DEF="G"><ProtoInstance name="Ball"><fieldValue name="r" value="5"/></ProtoInstance>
|
||||
<ProtoInstance name="Ball" containerField="children"/></Group>
|
||||
<Shape><Appearance><ImageTexture url='"a.png" "b.png"'/></Appearance><Box containerField="geometry" size="1, 2, 3" solid="FALSE"/></Shape>
|
||||
<Transform USE="G"/>
|
||||
</Scene></X3D>]]))
|
||||
t:check(g.header.xml and g.header.version == "4.0", "XML header")
|
||||
t:eq(g.components[1], "Shaders:1", "head component")
|
||||
local grp = g.roots[1]
|
||||
local kids = Fields.nodes(grp, "children")
|
||||
t:eq(#kids, 2, "ProtoInstances in children")
|
||||
t:eq(Fields.num(Fields.node(kids[1], "geometry"), "radius", -1), 5, "fieldValue")
|
||||
t:eq(Fields.num(Fields.node(kids[2], "geometry"), "radius", -1), 2, "ProtoInterface default")
|
||||
local shape = g.roots[2]
|
||||
local box = Fields.node(shape, "geometry")
|
||||
t:eq(table.concat(Fields.vec(box, "size", { 9, 9, 9 }), " "), "1 2 3", "numbers with commas")
|
||||
t:eq(Fields.bool(box, "solid", true), false, "XML booleans (any case)")
|
||||
local tex = Fields.node(Fields.node(shape, "appearance"), "texture")
|
||||
local urls = Fields.strs(tex, "url")
|
||||
t:check(#urls == 2 and urls[2] == "b.png", "quoted MFString")
|
||||
t:eq(g.roots[3], grp, "USE in XML")
|
||||
local single = { type = "ImageTexture", fields = { url = "plain name.png" } }
|
||||
t:eq(Fields.strs(single, "url")[1], "plain name.png", "unquoted single MFString")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ errors
|
||||
do
|
||||
local function code(text: string): string?
|
||||
local _, e = Vrml.graph(text)
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("#VRML V2.0 utf8\nShape { geometry Box { }"), Err.CORRUPT, "unclosed node")
|
||||
t:eq(code("#VRML V2.0 utf8\nShape { geometry USE X }"), Err.CORRUPT, "undefined USE")
|
||||
t:eq(code("#VRML V2.0 utf8\nROUTE A.b TO"), Err.CORRUPT, "bad ROUTE")
|
||||
t:eq(code("#VRML V2.0 utf8\nWorldInfo { title \"x }"), Err.TRUNCATED, "unterminated string")
|
||||
t:eq(code("#VRML V1.0 ascii\n"), Err.UNSUPPORTED, "VRML 1.0")
|
||||
t:eq(code("\224\0\0\1rest"), Err.UNSUPPORTED, "Fast Infoset X3D")
|
||||
t:eq(code("#VRML V2.0 utf8\n" .. string.rep("Group { children ", 250)), Err.LIMIT, "nesting limit")
|
||||
t:eq(code("<X3D><Scene><Group></Scene></X3D>"), Err.CORRUPT, "bad XML")
|
||||
local _, e = Vrml.encode({} :: any)
|
||||
t:eq(e and e.code, Err.ARG, "encode junk")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
do
|
||||
local samples = {
|
||||
"#VRML V2.0 utf8\nPROTO P [ field SFFloat a 1 ] { Box { size IS a IS a } }\nDEF X P { a 2 } USE X ROUTE X.a TO X.b\n",
|
||||
"#X3D V3.3 utf8\nPROFILE Full\nShape { geometry Extrusion { spine [ 0 0 0 0 1 0 1 1 1 ] crossSection [ 0 0 1 0 1 1 0 0 ] } }\n",
|
||||
"<X3D><Scene><ProtoDeclare name='A'><ProtoBody><Box/></ProtoBody></ProtoDeclare><ProtoInstance name='A'/><Shape><IndexedFaceSet coordIndex='0 1 2 -1 2 3 0'><Coordinate point='0 0 0 1 0 0 1 1 0 0 1 0'/></IndexedFaceSet></Shape></Scene></X3D>",
|
||||
}
|
||||
t:fuzz("graph + build", function(b: buffer)
|
||||
local g = Vrml.graph(b)
|
||||
if g then
|
||||
return Vrml.build(g)
|
||||
end
|
||||
return nil
|
||||
end, samples, { mutations = 400 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,109 @@
|
||||
-- tests/model/_vrml_common: helpers shared by the VRML and X3D tests (tests/model/vrml.luau, x3d.luau): counts over
|
||||
-- placed primitives (instances count, as the reference readers count them) and world bounds that include lines and
|
||||
-- points (MeshOps.merge skips them).
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local C = {}
|
||||
|
||||
export type Stats = { prims: number, unique: number, polys: number, lines: number, points: number, lo: { number }, hi: { number }, area: number }
|
||||
|
||||
function C.near(a: number, b: number, eps: number): boolean
|
||||
return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b))
|
||||
end
|
||||
|
||||
function C.stats(scene: any): Stats
|
||||
local s: Stats = { prims = 0, unique = 0, polys = 0, lines = 0, points = 0, lo = { math.huge, math.huge, math.huge }, hi = { -math.huge, -math.huge, -math.huge }, area = 0 }
|
||||
local seen = {}
|
||||
for _, pl in Types.Scene.flatten(scene) do
|
||||
local p = pl.primitive
|
||||
s.prims += 1
|
||||
if not seen[p] then
|
||||
seen[p] = true
|
||||
s.unique += 1
|
||||
end
|
||||
local P = p.attributes.POSITION
|
||||
local nf = 0
|
||||
if p.mode == "polygons" then
|
||||
nf = p.faces.count
|
||||
elseif p.mode == "lines" then
|
||||
s.lines += (if p.indices then p.indices.count else P.count) // 2
|
||||
elseif p.mode == "points" then
|
||||
s.points += P.count
|
||||
elseif p.mode == "triangle_strip" or p.mode == "triangle_fan" then
|
||||
nf = math.max(0, (if p.indices then p.indices.count else P.count) - 2)
|
||||
elseif p.mode == "line_strip" or p.mode == "line_loop" then
|
||||
local n = if p.indices then p.indices.count else P.count
|
||||
s.lines += if p.mode == "line_loop" then n else math.max(0, n - 1)
|
||||
else
|
||||
nf = (if p.indices then p.indices.count else P.count) // 3
|
||||
end
|
||||
s.polys += nf
|
||||
local m = pl.matrix
|
||||
local get = assert(Types.Scene.reader(P))
|
||||
local w = {}
|
||||
for v = 0, P.count - 1 do
|
||||
local x, y, z = get(v, 0), get(v, 1), get(v, 2)
|
||||
local wx, wy, wz = m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15]
|
||||
w[v] = { wx, wy, wz }
|
||||
s.lo[1], s.lo[2], s.lo[3] = math.min(s.lo[1], wx), math.min(s.lo[2], wy), math.min(s.lo[3], wz)
|
||||
s.hi[1], s.hi[2], s.hi[3] = math.max(s.hi[1], wx), math.max(s.hi[2], wy), math.max(s.hi[3], wz)
|
||||
end
|
||||
-- world area of the faces (fan-triangulated polygons)
|
||||
local mode = p.mode
|
||||
if mode == "triangles" or mode == "polygons" or mode == "triangle_strip" or mode == "triangle_fan" then
|
||||
local ir = if p.indices then Types.Scene.reader(p.indices) else nil
|
||||
local ind = function(i: number, _c: number): number
|
||||
return if ir then ir(i, 0) else i
|
||||
end
|
||||
local count = if p.indices then p.indices.count else P.count
|
||||
local function tri(a: number, b: number, c: number)
|
||||
local A, B, Cc = w[a], w[b], w[c]
|
||||
if A and B and Cc then
|
||||
local e1 = { B[1] - A[1], B[2] - A[2], B[3] - A[3] }
|
||||
local e2 = { Cc[1] - A[1], Cc[2] - A[2], Cc[3] - A[3] }
|
||||
local x, y, z = e1[2] * e2[3] - e1[3] * e2[2], e1[3] * e2[1] - e1[1] * e2[3], e1[1] * e2[2] - e1[2] * e2[1]
|
||||
s.area += math.sqrt(x * x + y * y + z * z) / 2
|
||||
end
|
||||
end
|
||||
if mode == "triangles" then
|
||||
for t = 0, count // 3 - 1 do
|
||||
tri(ind(t * 3, 0), ind(t * 3 + 1, 0), ind(t * 3 + 2, 0))
|
||||
end
|
||||
elseif mode == "triangle_strip" then
|
||||
for t = 0, count - 3 do
|
||||
tri(ind(t, 0), ind(t + 1, 0), ind(t + 2, 0))
|
||||
end
|
||||
elseif mode == "triangle_fan" then
|
||||
for t = 1, count - 2 do
|
||||
tri(ind(0, 0), ind(t, 0), ind(t + 1, 0))
|
||||
end
|
||||
else
|
||||
local at = 0
|
||||
local sizes = assert(Types.Scene.reader(p.faces))
|
||||
for f = 0, p.faces.count - 1 do
|
||||
local n = sizes(f, 0)
|
||||
for k = 1, n - 2 do
|
||||
tri(ind(at, 0), ind(at + k, 0), ind(at + k + 1, 0))
|
||||
end
|
||||
at += n
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
function C.boundsNear(lo: { number }, hi: { number }, wlo: { number }, whi: { number }, eps: number): boolean
|
||||
for k = 1, 3 do
|
||||
if not C.near(lo[k], wlo[k], eps) or not C.near(hi[k], whi[k], eps) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function C.fmt(lo: { number }, hi: { number }): string
|
||||
return string.format("%.5g %.5g %.5g / %.5g %.5g %.5g", lo[1], lo[2], lo[3], hi[1], hi[2], hi[3])
|
||||
end
|
||||
|
||||
return C
|
||||
@@ -0,0 +1,263 @@
|
||||
-- model/pmx tests: assimp's MMD model (Alicia_blade.pmx) and synthetic PMX 2.0 / 2.1 models (UTF-16 / UTF-8, index
|
||||
-- sizes 1 / 2 / 4, additional UVs, BDEF1 / BDEF2 / BDEF4 / SDEF / QDEF, every morph type, IK / inherit / axis bones,
|
||||
-- display frames, rigid bodies, joints) against assimp (meshes, faces per mesh, materials, bones, world bounds) and the
|
||||
-- generator's facts (vertices, weights, bones, morphs, textures); the same models as PMD against their PMX decode;
|
||||
-- errors; the fuzz no-throw pass. Fixtures: tools/fixtures/pmx.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Pmx = require("@formats/model/pmx")
|
||||
local MeshOps = require("@formats/ops/MeshOps")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Cases = require("@tests/gen/pmx_cases")
|
||||
local Pmds = require("@tests/gen/pmx_pmd")
|
||||
|
||||
local t = T.suite("pmx")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
return Pmx.decode(Bytes.reader(b), opts)
|
||||
end
|
||||
|
||||
local function near(a: number, b: number, eps: number): boolean
|
||||
return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b))
|
||||
end
|
||||
|
||||
t:eq(Pmx.name, "pmx", "name")
|
||||
t:eq(Pmx.category, "model", "category")
|
||||
t:check(Pmx.caps.decode and not Pmx.caps.encode, "contract")
|
||||
|
||||
local function faces(scene: any): (number, { number })
|
||||
local n, per = 0, {}
|
||||
for i, p in scene.meshes[1].primitives do
|
||||
per[i] = p.indices.count // 3
|
||||
n += per[i]
|
||||
end
|
||||
return n, per
|
||||
end
|
||||
|
||||
local function bounds(scene: any): ({ number }, { number })
|
||||
return MeshOps.bounds(MeshOps.merge(scene))
|
||||
end
|
||||
|
||||
-- a file vertex in the decoded primitives: position (z negated) and uv
|
||||
local function findVertex(scene: any, pos: { number }, uv: { number }): (any, number?)
|
||||
for _, p in scene.meshes[1].primitives do
|
||||
local P, U = p.attributes.POSITION, p.attributes.TEXCOORD_0
|
||||
for v = 0, P.count - 1 do
|
||||
local x, y, z = buffer.readf32(P.data, v * 12), buffer.readf32(P.data, v * 12 + 4), buffer.readf32(P.data, v * 12 + 8)
|
||||
if near(x, pos[1], 1e-6) and near(y, pos[2], 1e-6) and near(z, -pos[3], 1e-6) and near(buffer.readf32(U.data, v * 8), uv[1], 1e-6) and near(buffer.readf32(U.data, v * 8 + 4), uv[2], 1e-6) then
|
||||
return p, v
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ reference files
|
||||
local scenes = {}
|
||||
local checked = { assimp = 0, expect = 0 }
|
||||
for i, c in Cases do
|
||||
local scene, err = decode(c.data, { files = c.files })
|
||||
t:check(scene, c.name .. ": decode: " .. tostring(err))
|
||||
if not scene then
|
||||
continue
|
||||
end
|
||||
scenes[i] = scene
|
||||
local a = c.assimp
|
||||
local nf, per = faces(scene)
|
||||
if not a.skip then
|
||||
t:eq(#scene.meshes[1].primitives, a.meshes, c.name .. ": meshes (assimp)")
|
||||
t:eq(nf, a.faces, c.name .. ": faces (assimp)")
|
||||
t:eq(#scene.materials, a.materials, c.name .. ": materials (assimp)")
|
||||
local ok = true
|
||||
for k, pm in a.perMesh do
|
||||
ok = ok and per[k] == pm.faces
|
||||
end
|
||||
t:check(ok, c.name .. ": faces per material (assimp)")
|
||||
t:eq(#scene.skins[1].joints * #scene.meshes[1].primitives, a.bones, c.name .. ": bones x meshes (assimp)")
|
||||
if a.min then
|
||||
local lo, hi = bounds(scene)
|
||||
local okb = true
|
||||
for k = 1, 3 do
|
||||
okb = okb and near(lo[k], a.min[k], 1e-5) and near(hi[k], a.max[k], 1e-5)
|
||||
end
|
||||
t:check(okb, string.format("%s: world bounds (assimp) %s / %s", c.name, table.concat(lo, " "), table.concat(hi, " ")))
|
||||
end
|
||||
checked.assimp += 1
|
||||
end
|
||||
local x = c.expect
|
||||
if x then
|
||||
local src = scene.meta.source
|
||||
t:eq(src.version, x.version, c.name .. ": version")
|
||||
t:eq(src.name, x.name, c.name .. ": model name")
|
||||
t:eq(#scene.skins[1].joints, x.bones, c.name .. ": bones")
|
||||
t:eq(#scene.nodes, 2 + x.bones, c.name .. ": nodes")
|
||||
t:eq(#scene.textures, x.textures, c.name .. ": textures")
|
||||
t:eq(#src.rigidBodies, x.rigid, c.name .. ": rigid bodies")
|
||||
t:eq(#src.joints, x.joints, c.name .. ": joints")
|
||||
t:eq(#src.displayFrames, x.frames, c.name .. ": display frames")
|
||||
local mesh = scene.meshes[1]
|
||||
local vertexMorphs = x.morphTypes["1"] or 0
|
||||
t:eq(#(mesh.extras.targetNames or {}), vertexMorphs, c.name .. ": vertex morphs -> targets")
|
||||
t:eq(#mesh.primitives[1].targets, vertexMorphs, c.name .. ": targets per primitive")
|
||||
t:eq(#((mesh.extras.pmx or {}).morphs or {}), x.morphs - vertexMorphs, c.name .. ": other morphs in extras")
|
||||
t:check(x.uva == 0 or mesh.primitives[1].attributes["_UVA" .. x.uva] ~= nil, c.name .. ": additional UVs")
|
||||
-- unique vertices: a material boundary can split a ring, so at least the file's count
|
||||
local nv = 0
|
||||
for _, p in mesh.primitives do
|
||||
nv += p.attributes.POSITION.count
|
||||
end
|
||||
t:check(nv >= x.vertices and nv <= x.vertices * 2, c.name .. ": vertices " .. nv .. " / " .. x.vertices)
|
||||
for _, s in x.samples do
|
||||
local p, v = findVertex(scene, s.pos, s.uv)
|
||||
t:check(p ~= nil, c.name .. ": vertex " .. s.index .. " found")
|
||||
if p and v then
|
||||
local J, W = p.attributes.JOINTS_0, p.attributes.WEIGHTS_0
|
||||
local want: { [number]: number } = {}
|
||||
local sum = 0
|
||||
local ws = if s.deform == 1 or s.deform == 3 then { s.weights[1], 1 - s.weights[1] } elseif s.deform == 0 then { 1 } else s.weights
|
||||
for k, b in s.bones do
|
||||
if b >= 0 and ws[k] and ws[k] > 0 then
|
||||
want[b] = (want[b] or 0) + ws[k]
|
||||
sum += ws[k]
|
||||
end
|
||||
end
|
||||
local got: { [number]: number } = {}
|
||||
for k = 0, 3 do
|
||||
local w = buffer.readf32(W.data, (v * 4 + k) * 4)
|
||||
if w > 0 then
|
||||
local j = buffer.readu16(J.data, (v * 4 + k) * 2)
|
||||
got[j] = (got[j] or 0) + w
|
||||
end
|
||||
end
|
||||
local ok = true
|
||||
for b, w in want do
|
||||
ok = ok and got[b] ~= nil and near(got[b], w / sum, 1e-5)
|
||||
end
|
||||
t:check(ok, c.name .. ": vertex " .. s.index .. " weights (deform " .. s.deform .. ")")
|
||||
if s.deform == 3 then
|
||||
t:check(p.attributes._SDEF_C ~= nil and buffer.readu8(p.attributes._DEFORM.data, v) == 3, c.name .. ": SDEF attributes")
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the texture named tex\body.png came from files["tex/body.png"]
|
||||
t:check(scene.textures[1].uri == "tex/body.png" and scene.textures[1].data ~= nil and scene.textures[1].mime == "image/png", c.name .. ": texture data")
|
||||
local m1 = scene.materials[1]
|
||||
t:check(m1.baseColorTexture and m1.baseColorTexture.texture == 1 and m1.doubleSided, c.name .. ": material 1 texture, no-cull")
|
||||
t:check(m1.extras.pmx.sphereTexture == 2 and m1.extras.pmx.sphereMode == "multiply" and m1.extras.pmx.toonShared == 3, c.name .. ": sphere / toon")
|
||||
t:check(scene.materials[2].alphaMode == "blend", c.name .. ": translucent material")
|
||||
local ik = nil
|
||||
for _, n in scene.nodes do
|
||||
if n.extras and n.extras.pmx and n.extras.pmx.ik then
|
||||
ik = n.extras.pmx.ik
|
||||
end
|
||||
end
|
||||
t:check(ik and ik.loops == 40 and #ik.links == 2, c.name .. ": IK bone")
|
||||
checked.expect += 1
|
||||
end
|
||||
end
|
||||
t:note("%d files: %d against assimp, %d against the generator", #Cases, checked.assimp, checked.expect)
|
||||
|
||||
-- skins: every joint's inverse bind matrix undoes its world translation
|
||||
do
|
||||
local s = scenes[2]
|
||||
if s then
|
||||
local skin = s.skins[1]
|
||||
local ok = true
|
||||
local world = {}
|
||||
-- world matrices by walking from the root
|
||||
local function walk(n: number, parent: { number })
|
||||
local w = Types.Scene.mul(parent, Types.Scene.nodeMatrix(s.nodes[n]))
|
||||
world[n] = w
|
||||
for _, c in s.nodes[n].children do
|
||||
walk(c, w)
|
||||
end
|
||||
end
|
||||
walk(1, Types.Scene.IDENTITY)
|
||||
for k, j in skin.joints do
|
||||
local w = world[j]
|
||||
for c = 0, 2 do
|
||||
ok = ok and near(buffer.readf32(skin.inverseBindMatrices.data, ((k - 1) * 16 + 12 + c) * 4), -w[13 + c], 1e-5)
|
||||
end
|
||||
end
|
||||
t:check(ok, "inverse bind matrices = -world bone positions")
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ PMD
|
||||
for _, c in Pmds do
|
||||
local s, err = decode(c.data)
|
||||
t:check(s, c.name .. ": decode: " .. tostring(err))
|
||||
local ref = scenes[c.pmx]
|
||||
if s and ref then
|
||||
t:check(s.meta.source.pmd == true and s.meta.source.format == "pmx", c.name .. ": meta.source")
|
||||
local a, pa = faces(s)
|
||||
local b, pb = faces(ref)
|
||||
t:eq(a, b, c.name .. ": faces (= PMX)")
|
||||
t:eq(#pa, #pb, c.name .. ": primitives (= PMX)")
|
||||
t:eq(#s.skins[1].joints, #ref.skins[1].joints, c.name .. ": bones (= PMX)")
|
||||
t:eq(#s.meshes[1].extras.targetNames, #ref.meshes[1].extras.targetNames, c.name .. ": vertex morphs (= PMX)")
|
||||
local lo, hi = bounds(s)
|
||||
local lo2, hi2 = bounds(ref)
|
||||
local ok = true
|
||||
for k = 1, 3 do
|
||||
ok = ok and near(lo[k], lo2[k], 1e-6) and near(hi[k], hi2[k], 1e-6)
|
||||
end
|
||||
t:check(ok, c.name .. ": bounds (= PMX)")
|
||||
-- the same morph deltas
|
||||
local d1 = s.meshes[1].primitives[1].targets[1].POSITION
|
||||
local d2 = ref.meshes[1].primitives[1].targets[1].POSITION
|
||||
t:check(d1.count == d2.count and buffer.tostring(d1.data) == buffer.tostring(d2.data), c.name .. ": morph deltas (= PMX)")
|
||||
t:eq(s.meshes[1].extras.targetNames[1], "up", c.name .. ": English morph names")
|
||||
t:eq(s.materials[1].extras.pmx.sphereMode, "multiply", c.name .. ": texture*sphere name")
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ api, errors
|
||||
do
|
||||
local c = Cases[2]
|
||||
t:eq(F.detect(c.data), "pmx", "F.detect pmx")
|
||||
t:eq(F.detect(Pmds[1].data), "pmx", "F.detect pmd")
|
||||
local s = F.load(c.data, { files = c.files })
|
||||
t:check(s and s.kind == "scene" and s.meta.up == "y", "F.load")
|
||||
local _, e1 = decode(string.sub(c.data, 1, 300))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local _, e2 = decode("PMX " .. "\0\0\64\64" .. string.sub(c.data, 9))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 2.0 -> 3.0: " .. tostring(e2))
|
||||
local _, e3 = decode(string.sub(c.data, 1, 11) .. "\3" .. string.sub(c.data, 13))
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "index size 3: " .. tostring(e3))
|
||||
local _, e4 = decode(c.data, { maxVertices = 10 })
|
||||
t:check(e4 and e4.code == Err.LIMIT, "maxVertices: " .. tostring(e4))
|
||||
local _, e5 = decode(c.data, { maxBytes = 100 })
|
||||
t:check(e5 and e5.code == Err.LIMIT, "maxBytes (morph targets): " .. tostring(e5))
|
||||
local s2 = decode(c.data, { maxBytes = 100, morphs = false })
|
||||
t:check(s2 and s2.meshes[1].primitives[1].targets == nil, "morphs = false")
|
||||
local _, e6 = decode("Pmd\0\0\0\64" .. string.rep("\0", 300))
|
||||
t:check(e6 and e6.code == Err.UNSUPPORTED, "PMD version 2: " .. tostring(e6))
|
||||
local _, e7 = decode(string.sub(Pmds[1].data, 1, 400))
|
||||
t:check(e7 and (e7.code == Err.TRUNCATED or e7.code == Err.CORRUPT), "PMD truncated: " .. tostring(e7))
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Cases do
|
||||
if #c.data < 120000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
for _, c in Pmds do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local s = Pmx.decode(Bytes.reader(b))
|
||||
if s then
|
||||
MeshOps.merge(s)
|
||||
end
|
||||
return s
|
||||
end, samples, { mutations = 16, keepHeader = 16 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,410 @@
|
||||
-- model/vrml tests: assimp's WRL test models (HelloWorld, Wuson, the MotionCaptureROM animation), scenes and glTF
|
||||
-- samples written by VTK's VRML exporter, and hand-written feature files (PROTO, DEF / USE, per-face colours and
|
||||
-- separate indices, primitives, ElevationGrid, Extrusion, Switch, LOD, lights, viewpoints, ROUTE animations,
|
||||
-- PixelTexture, Inline), against VTK's vtkVRMLImporter (polygons, points, world bounds where VTK's transforms are
|
||||
-- right) and exact expectations; the writer: round trips of every case and of glTF sample models (world triangles,
|
||||
-- materials, textures, cameras, lights, animation samples), VTK reading our files like the originals; errors; the
|
||||
-- fuzz no-throw pass. Fixtures: tools/fixtures/vrml.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Vrml = require("@formats/model/vrml")
|
||||
local Gltf = require("@formats/model/gltf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local C = require("@tests/model/_vrml_common")
|
||||
local Cases = require("@tests/gen/vrml_cases")
|
||||
local GltfCases = require("@tests/gen/vrml_gltf")
|
||||
local Xcheck = require("@tests/gen/vrml_xcheck")
|
||||
|
||||
local t = T.suite("vrml")
|
||||
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 Vrml.decode(Bytes.reader(b), opts)
|
||||
end
|
||||
|
||||
t:eq(Vrml.name, "vrml", "name")
|
||||
t:eq(Vrml.category, "model", "category")
|
||||
t:check(Vrml.caps.decode and Vrml.caps.encode, "contract")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ reference files
|
||||
local scenes, byName = {}, {}
|
||||
local checked = { vtk = 0, bounds = 0, none = 0 }
|
||||
for i, c in Cases do
|
||||
local scene, err = decode(c.data, { files = c.files, triangulate = false })
|
||||
t:check(scene, c.name .. ": decode: " .. tostring(err))
|
||||
if not scene then
|
||||
continue
|
||||
end
|
||||
scenes[i], byName[c.name] = scene, scene
|
||||
local s = C.stats(scene)
|
||||
local v = c.vtk
|
||||
if v.failed or v.actors == 0 then
|
||||
checked.none += 1
|
||||
continue
|
||||
end
|
||||
checked.vtk += 1
|
||||
if not c.primitives then
|
||||
t:eq(s.polys, v.polys, c.name .. ": polygons (VTK)")
|
||||
end
|
||||
t:eq(s.points, v.verts, c.name .. ": points (VTK)")
|
||||
t:check((s.lines > 0) == (v.lines > 0), c.name .. ": lines (VTK)")
|
||||
if c.vtkTransforms and v.min then
|
||||
t:check(C.boundsNear(s.lo, s.hi, v.min, v.max, if c.primitives then 2e-2 else 1e-5), c.name .. ": world bounds (VTK) " .. C.fmt(s.lo, s.hi) .. " vs " .. C.fmt(v.min, v.max))
|
||||
checked.bounds += 1
|
||||
end
|
||||
end
|
||||
t:note("%d files: %d against VTK (%d with bounds), %d VTK cannot read", #Cases, checked.vtk, checked.bounds, checked.none)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ exact expectations
|
||||
local function prims(scene: any): { any }
|
||||
local out = {}
|
||||
for _, pl in Types.Scene.flatten(scene) do
|
||||
table.insert(out, pl)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
do
|
||||
local hw = byName["assimp/HelloWorld.wrl"]
|
||||
if hw then
|
||||
t:eq(#hw.cameras, 1, "HelloWorld: viewpoint")
|
||||
t:eq(hw.cameras[1].name, "ViewUpClose", "HelloWorld: viewpoint DEF name")
|
||||
t:eq(hw.meta.source.title, "Hello World!", "HelloWorld: WorldInfo title")
|
||||
t:eq(hw.meta.source.skipped.Text, 1, "HelloWorld: Text skipped")
|
||||
local tex = hw.textures[1]
|
||||
t:check(tex and tex.uri == "earth-topo.png" and tex.data and tex.mime == "image/png", "HelloWorld: texture from files")
|
||||
t:eq(hw.materials[1].name, "MaterialOffWhite", "HelloWorld: material DEF name")
|
||||
t:check(near(hw.materials[1].baseColor[1], 0.980392, 1e-6), "HelloWorld: diffuseColor")
|
||||
end
|
||||
local mc = byName["assimp/MotionCaptureROM.WRL"]
|
||||
if mc then
|
||||
t:eq(#mc.animations, 1, "MotionCaptureROM: one TimeSensor animation")
|
||||
local a = mc.animations[1]
|
||||
t:check(#a.channels >= 20, "MotionCaptureROM: channels " .. #a.channels)
|
||||
local tmax = 0
|
||||
for _, ch in a.channels do
|
||||
tmax = math.max(tmax, buffer.readf32(ch.input.data, (ch.input.count - 1) * 4))
|
||||
end
|
||||
t:check(near(tmax, 45.27, 1e-4), "MotionCaptureROM: key times x cycleInterval " .. tmax)
|
||||
end
|
||||
|
||||
-- PROTO expansion, DEF / USE of a Shape (one mesh, two nodes)
|
||||
local pu = byName["feature/feat_proto_use.wrl"]
|
||||
if pu then
|
||||
local s = C.stats(pu)
|
||||
t:eq(#pu.meshes, 3, "proto: meshes (two instances + a shared shape)")
|
||||
t:eq(s.prims, 4, "proto: placed primitives")
|
||||
t:check(C.boundsNear(s.lo, s.hi, { -1, -0.5, -0.5 }, { 3.5, 6, 0.5 }, 1e-4), "proto: bounds " .. C.fmt(s.lo, s.hi))
|
||||
local cols = {}
|
||||
for _, m in pu.materials do
|
||||
table.insert(cols, string.format("%g %g %g", m.baseColor[1], m.baseColor[2], m.baseColor[3]))
|
||||
end
|
||||
table.sort(cols)
|
||||
t:eq(table.concat(cols, ", "), "0 1 0, 1 0 0", "proto: IS fields (instance value, default)")
|
||||
end
|
||||
|
||||
-- separate indices, per-face normals / colours, ccw FALSE, solid FALSE
|
||||
local ix = byName["feature/feat_indices.wrl"]
|
||||
if ix then
|
||||
local p1 = ix.meshes[1].primitives[1]
|
||||
t:eq(p1.attributes.POSITION.count, 8, "indices: corners split by normal / colour / texture index")
|
||||
t:eq(p1.faces.count, 2, "indices: two quads")
|
||||
local N, Col, Tx = p1.attributes.NORMAL, p1.attributes.COLOR_0, p1.attributes.TEXCOORD_0
|
||||
t:check(buffer.readf32(N.data, 8) == 1 and buffer.readf32(Col.data, 0) == 1 and buffer.readf32(Col.data, 8) == 0, "indices: face 1 normal / colour")
|
||||
t:check(buffer.readf32(Tx.data, 4) == 1, "indices: t flipped (0 -> 1)")
|
||||
-- ccw FALSE: the first face is written reversed (0 3 2 1)
|
||||
t:eq(buffer.readu32(p1.indices.data, 4), 3, "indices: ccw FALSE reverses the winding")
|
||||
local m = ix.materials[p1.material]
|
||||
t:check(m.doubleSided and m.alphaMode == "blend" and near(m.baseColor[4], 0.75, 1e-6), "indices: solid FALSE, transparency")
|
||||
t:check(m.extras.vrml.shininess == 0.5 and m.extras.vrml.specularColor[1] == 1, "indices: Phong terms in extras")
|
||||
local p2 = ix.meshes[2].primitives[1]
|
||||
t:eq(p2.attributes.POSITION.count, 5, "indices: per-vertex colour indices")
|
||||
t:check(p2.material == nil, "indices: no appearance, no material")
|
||||
end
|
||||
|
||||
-- primitives: exact bounds, unit normals
|
||||
local pr = byName["feature/feat_primitives.wrl"]
|
||||
if pr then
|
||||
local s = C.stats(pr)
|
||||
t:check(C.boundsNear(s.lo, s.hi, { -4.5, -2, -1.5 }, { 9, 2, 1.5 }, 1e-6), "primitives: bounds " .. C.fmt(s.lo, s.hi))
|
||||
local box = pr.meshes[1].primitives[1]
|
||||
t:eq(box.attributes.POSITION.count, 24, "Box: 24 vertices")
|
||||
t:eq(box.indices.count, 36, "Box: 12 triangles")
|
||||
local sph = pr.meshes[2].primitives[1]
|
||||
local ok = true
|
||||
local P, N = sph.attributes.POSITION, sph.attributes.NORMAL
|
||||
for v = 0, P.count - 1 do
|
||||
for k = 0, 2 do
|
||||
ok = ok and near(buffer.readf32(P.data, (v * 3 + k) * 4) / 1.5, buffer.readf32(N.data, (v * 3 + k) * 4), 1e-5)
|
||||
end
|
||||
end
|
||||
t:check(ok, "Sphere: normals = position / radius")
|
||||
-- outward winding: the Box's first triangle faces +z
|
||||
local function faceNormal(prim: any, tri: number): { number }
|
||||
local function p(i: number): { number }
|
||||
local v = buffer.readu32(prim.indices.data, (tri * 3 + i) * 4)
|
||||
return { buffer.readf32(prim.attributes.POSITION.data, v * 12), buffer.readf32(prim.attributes.POSITION.data, v * 12 + 4), buffer.readf32(prim.attributes.POSITION.data, v * 12 + 8) }
|
||||
end
|
||||
local a, b, c = p(0), p(1), p(2)
|
||||
local e1, e2 = { b[1] - a[1], b[2] - a[2], b[3] - a[3] }, { c[1] - a[1], c[2] - a[2], c[3] - a[3] }
|
||||
return { e1[2] * e2[3] - e1[3] * e2[2], e1[3] * e2[1] - e1[1] * e2[3], e1[1] * e2[2] - e1[2] * e2[1] }
|
||||
end
|
||||
t:check(faceNormal(box, 0)[3] > 0, "Box: counter-clockwise front faces")
|
||||
local cyl = pr.meshes[4].primitives[1]
|
||||
local last = cyl.indices.count // 3 - 1
|
||||
t:check(faceNormal(cyl, last)[2] < 0, "Cylinder: bottom cap faces down")
|
||||
t:check(faceNormal(cyl, 0)[3] < 0 or faceNormal(cyl, 0)[1] ~= 0, "Cylinder: side faces outwards")
|
||||
end
|
||||
|
||||
local ge = byName["feature/feat_grid_extrusion.wrl"]
|
||||
if ge then
|
||||
local grid, ex = ge.meshes[1].primitives[1], ge.meshes[2].primitives[1]
|
||||
t:eq(grid.attributes.POSITION.count, 12, "ElevationGrid: vertices")
|
||||
t:eq(grid.faces.count, 6, "ElevationGrid: quads")
|
||||
t:eq(ex.attributes.POSITION.count, 16, "Extrusion: closed cross section shares its seam")
|
||||
t:eq(ex.faces.count, 14, "Extrusion: sides + caps")
|
||||
local s = C.stats(ge)
|
||||
t:check(near(s.hi[2], 3, 1e-6) and near(s.hi[3], 4, 1e-6), "ElevationGrid: heights and spacing")
|
||||
end
|
||||
|
||||
local sc = byName["feature/feat_scene.wrl"]
|
||||
if sc then
|
||||
t:eq(sc.meta.source.title, "feature scene", "scene: title")
|
||||
t:eq(#sc.cameras, 1, "scene: viewpoint")
|
||||
t:check(near(sc.cameras[1].yfov, 0.9, 1e-9) and sc.cameras[1].name == "Cam", "scene: fieldOfView")
|
||||
local types = {}
|
||||
for _, l in sc.lights do
|
||||
types[l.type] = l
|
||||
end
|
||||
t:check(types.directional and types.point and types.spot, "scene: three light types")
|
||||
t:check(types.point.range == 10 and near(types.spot.outerCone, 0.6, 1e-9) and near(types.spot.innerCone, 0.4, 1e-9), "scene: radius, cones")
|
||||
-- the directional light's node turns -z onto its direction (0, -1, -1)
|
||||
for _, n in sc.nodes do
|
||||
if n.light and sc.lights[n.light].type == "directional" then
|
||||
local m = Mat4.fromQuat(n.rotation)
|
||||
local x, y, z = Mat4.point(m, 0, 0, -1)
|
||||
t:check(near(x, 0, 1e-6) and near(y, -math.sqrt(0.5), 1e-6) and near(z, -math.sqrt(0.5), 1e-6), "scene: light direction")
|
||||
end
|
||||
end
|
||||
t:eq(#sc.animations, 1, "scene: animation")
|
||||
local a = sc.animations[1]
|
||||
t:eq(a.name, "Clock", "scene: animation named after its TimeSensor")
|
||||
t:eq(#a.channels, 2, "scene: translation + rotation channels")
|
||||
local tr = a.channels[1].path == "translation" and a.channels[1] or a.channels[2]
|
||||
t:check(near(buffer.readf32(tr.input.data, 8), 4, 1e-6), "scene: key 1 x cycleInterval 4")
|
||||
-- translation keys move the outer node: key + center
|
||||
t:check(near(buffer.readf32(tr.output.data, 12), 1, 1e-6) and near(buffer.readf32(tr.output.data, 16), 2, 1e-6), "scene: translation key + center")
|
||||
-- PixelTexture: the first row of the file is the bottom one
|
||||
local img
|
||||
for _, tex in sc.textures do
|
||||
img = img or tex.image
|
||||
end
|
||||
t:check(img and img.width == 2 and buffer.readu8(img.pixels, 2) == 255 and buffer.readu8(img.pixels, 8) == 255 and buffer.readu8(img.pixels, 9) == 0, "scene: PixelTexture pixels")
|
||||
-- Switch whichChoice 1: the sphere only; LOD: the first level; Inline: part.wrl
|
||||
local s = C.stats(sc)
|
||||
t:check(near(s.lo[2], -3.1, 1e-6), "scene: Inline file placed")
|
||||
-- center / scaleOrientation: the line set's first point (0, 0, 0) under T C R SR S -SR -C
|
||||
local mover = nil
|
||||
for _, pl in prims(sc) do
|
||||
if pl.primitive.mode == "lines" then
|
||||
mover = pl
|
||||
end
|
||||
end
|
||||
if mover then
|
||||
local sr = Mat4.axisAngle(0, 0, 1, math.deg(0.5))
|
||||
local m = Mat4.mul(Mat4.translation(1, 0, 0), Mat4.mul(Mat4.fromQuat(sr), Mat4.mul(Mat4.scaling(1, 2, 1), Mat4.mul(Mat4.fromQuat({ -sr[1], -sr[2], -sr[3], sr[4] }), Mat4.translation(-1, 0, 0)))))
|
||||
local ok = true
|
||||
for k = 1, 16 do
|
||||
ok = ok and near(mover.matrix[k], m[k], 1e-6)
|
||||
end
|
||||
t:check(ok, "scene: Transform center / scaleOrientation")
|
||||
end
|
||||
t:eq(sc.meta.unit, 1, "scene: metres")
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ writer
|
||||
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 " .. C.fmt(sa.lo, sa.hi) .. " vs " .. C.fmt(sb.lo, sb.hi))
|
||||
t:check(near(sa.area, sb.area, 1e-4), string.format("%s: area %.6g / %.6g", label, sa.area, sb.area))
|
||||
end
|
||||
t:eq(#b.materials, #a.materials, label .. ": materials")
|
||||
for k, m in a.materials do
|
||||
local m2 = b.materials[k]
|
||||
if m2 then
|
||||
local ok = true
|
||||
for q = 1, 4 do
|
||||
ok = ok and near(m.baseColor[q], m2.baseColor[q], 1e-6)
|
||||
end
|
||||
t:check(ok, label .. ": material " .. k .. " colour")
|
||||
t:check((m.baseColorTexture == nil) == (m2.baseColorTexture == nil), label .. ": material " .. k .. " texture")
|
||||
end
|
||||
end
|
||||
t:eq(#b.cameras, #a.cameras, label .. ": cameras")
|
||||
t:eq(#b.lights, #a.lights, label .. ": lights")
|
||||
end
|
||||
|
||||
local rt = 0
|
||||
for i, c in Cases do
|
||||
local a = scenes[i]
|
||||
if a then
|
||||
local files = {}
|
||||
local out, err = Vrml.encode(a, { files = files, name = "rt.wrl" })
|
||||
t:check(out, c.name .. ": encode: " .. tostring(err))
|
||||
if out then
|
||||
local b, e2 = decode(out, { files = files, triangulate = false })
|
||||
t:check(b, c.name .. ": re-decode: " .. tostring(e2))
|
||||
if b then
|
||||
compare(a, b, c.name .. " (round trip)")
|
||||
t:eq(#b.animations, #a.animations, c.name .. " (round trip): animations")
|
||||
rt += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- glTF samples through VRML: world triangles, materials, textures, cameras, lights, animation samples
|
||||
local gltfScenes = {}
|
||||
for i, c in GltfCases do
|
||||
local g = Gltf.decode(Bytes.reader(buffer.fromstring(c.data)), { files = c.files })
|
||||
if not g then
|
||||
continue
|
||||
end
|
||||
gltfScenes[i] = g
|
||||
local files = {}
|
||||
local out, err = Vrml.encode(g, { files = files, name = "model.wrl" })
|
||||
t:check(out, c.name .. ": encode: " .. tostring(err))
|
||||
if not out then
|
||||
continue
|
||||
end
|
||||
local b = decode(out, { files = files })
|
||||
t:check(b, c.name .. ": decode our VRML")
|
||||
if not b then
|
||||
continue
|
||||
end
|
||||
local ga = C.stats(g)
|
||||
local sb = C.stats(b)
|
||||
t:check(C.boundsNear(ga.lo, ga.hi, sb.lo, sb.hi, 1e-5), c.name .. ": bounds through VRML " .. C.fmt(ga.lo, ga.hi) .. " vs " .. C.fmt(sb.lo, sb.hi))
|
||||
t:check(near(ga.area, sb.area, 1e-4), c.name .. ": area through VRML")
|
||||
t:eq(sb.points, ga.points, c.name .. ": points through VRML")
|
||||
t:eq(#b.cameras, #g.cameras, c.name .. ": cameras through VRML")
|
||||
for k, tex in g.textures do
|
||||
local t2 = b.textures[k]
|
||||
if tex.data and t2 then
|
||||
t:check(t2.data and buffer.tostring(t2.data) == buffer.tostring(tex.data), c.name .. ": texture " .. k .. " bytes")
|
||||
end
|
||||
end
|
||||
-- animations: the translation / rotation / scale channels, sampled at their keys
|
||||
local want = 0
|
||||
for _, a in g.animations do
|
||||
for _, ch in a.channels do
|
||||
if ch.path ~= "weights" then
|
||||
want += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
local got = 0
|
||||
for _, a in b.animations do
|
||||
got += #a.channels
|
||||
end
|
||||
t:eq(got, want, c.name .. ": animation channels through VRML")
|
||||
rt += 1
|
||||
end
|
||||
t:note("%d round trips", rt)
|
||||
|
||||
-- VTK reads our files like the originals
|
||||
local xc = 0
|
||||
for _, x in Xcheck do
|
||||
local v = x.vtk
|
||||
local src = if x.case then scenes[x.case] else (if x.gltf then gltfScenes[x.gltf] else nil)
|
||||
local srcCase = if x.case then Cases[x.case] else nil
|
||||
if src and not v.failed and v.actors > 0 then
|
||||
local s = C.stats(src)
|
||||
if not (srcCase and srcCase.primitives) then
|
||||
-- our IndexedFaceSets keep polygons; VTK counts them
|
||||
t:eq(v.polys, s.polys, x.name .. ": polygons (VTK reads our file)")
|
||||
end
|
||||
t:eq(v.verts, s.points, x.name .. ": points (VTK reads our file)")
|
||||
if x.vtkTransforms and v.min and s.lo[1] <= s.hi[1] then
|
||||
t:check(C.boundsNear(s.lo, s.hi, v.min, v.max, 1e-4), x.name .. ": bounds (VTK reads our file) " .. C.fmt(s.lo, s.hi) .. " vs " .. C.fmt(v.min, v.max))
|
||||
end
|
||||
xc += 1
|
||||
end
|
||||
end
|
||||
t:note("%d of our files read by VTK", xc)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ api, options, errors
|
||||
do
|
||||
local text = Cases[1].data
|
||||
t:eq(F.detect(text), "vrml", "F.detect")
|
||||
local s = F.load(text, { files = Cases[1].files })
|
||||
t:check(s and s.meta.source.format == "vrml", "F.load")
|
||||
local gz = Deflate.gzip(text)
|
||||
local sg = decode(gz :: buffer)
|
||||
t:check(sg and #sg.meshes == #s.meshes, "gzipped .wrz")
|
||||
local tri = decode(Cases[1].data)
|
||||
t:check(tri and tri.meshes[1].primitives[1].mode == "triangles", "triangulated by default")
|
||||
local seg = decode(Cases[1].data, { segments = 8 })
|
||||
t:check(seg and seg.meshes[1].primitives[1].attributes.POSITION.count < s.meshes[1].primitives[1].attributes.POSITION.count, "segments option")
|
||||
local _, e1 = decode("#VRML V1.0 ascii\nSeparator { }")
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "VRML 1.0: " .. tostring(e1))
|
||||
local _, e2 = decode("#VRML V2.0 utf8\nShape { geometry Box { size 1 1 1 }")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "unclosed node: " .. tostring(e2))
|
||||
local _, e3 = decode("#VRML V2.0 utf8\nShape { appearance USE Nothing }")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "USE of an undefined name: " .. tostring(e3))
|
||||
local _, e4 = decode('#VRML V2.0 utf8\nWorldInfo { title "open')
|
||||
t:check(e4 and e4.code == Err.TRUNCATED, "unterminated string: " .. tostring(e4))
|
||||
local deep = "#VRML V2.0 utf8\n" .. string.rep("Group { children [ ", 300) .. string.rep("] } ", 300)
|
||||
local _, e5 = decode(deep)
|
||||
t:check(e5 and e5.code == Err.LIMIT, "nesting limit: " .. tostring(e5))
|
||||
local _, e6 = decode("#VRML V2.0 utf8\nShape { geometry ElevationGrid { xDimension 5000 zDimension 5000 } }", { maxVertices = 1000 })
|
||||
t:check(e6 and e6.code == Err.LIMIT, "maxVertices: " .. tostring(e6))
|
||||
local _, e7 = Vrml.encode({} :: any)
|
||||
t:check(e7 and e7.code == Err.ARG, "encode junk")
|
||||
-- a quad written as one polygon and read back as one
|
||||
local p = Types.Scene.accessor(nil, "f32", 3, 4)
|
||||
for v, xyz in { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 } } do
|
||||
for k = 1, 3 do
|
||||
buffer.writef32(p.data, ((v - 1) * 3 + k - 1) * 4, xyz[k])
|
||||
end
|
||||
end
|
||||
local faces = Types.Scene.accessor(nil, "u32", 1, 1)
|
||||
buffer.writeu32(faces.data, 0, 4)
|
||||
local ind = Types.Scene.accessor(nil, "u32", 1, 4)
|
||||
for k = 0, 3 do
|
||||
buffer.writeu32(ind.data, k * 4, k)
|
||||
end
|
||||
local quad = Types.Scene.fromPrimitive({ attributes = { POSITION = p }, indices = ind, faces = faces, mode = "polygons" }, "quad")
|
||||
local q = decode(Vrml.encode(quad) :: buffer, { triangulate = false })
|
||||
t:check(q and q.meshes[1].primitives[1].faces.count == 1 and q.nodes[1].name == "quad", "a quad stays one polygon, names kept")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
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 = Vrml.decode(Bytes.reader(b), { files = {} })
|
||||
if s then
|
||||
Vrml.encode(s)
|
||||
end
|
||||
return s
|
||||
end, samples, { mutations = 16, keepHeader = 16 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,308 @@
|
||||
-- model/x3d tests: assimp's X3D / X3DV / X3DB test models, scenes and glTF samples written by VTK's X3D exporter and
|
||||
-- by assimp's X3D exporter, and hand-written feature files (the X3D triangle / quad / line sets, ColorRGBA,
|
||||
-- ProtoDeclare / ProtoInstance with SFNode defaults, PhysicalMaterial, UnlitMaterial, OrthoViewpoint, ROUTE
|
||||
-- animation, UNIT, the classic encoding's header statements), against assimp's X3D reader (meshes, faces, materials,
|
||||
-- world bounds) and exact expectations; .x3dv against the same model as .x3d; the writer (XML and classic): round
|
||||
-- trips, and assimp reading our X3D like the originals; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/x3d.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local X3d = require("@formats/model/x3d")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/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("@formats/model/gltf")
|
||||
local Vrml = require("@formats/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" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ reference files
|
||||
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 </X3D> out: assimp then reads nothing)
|
||||
local unclosed = a.meshes == 0 and not string.find(c.data, "</X3D>", 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)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ exact expectations
|
||||
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
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ writer
|
||||
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)
|
||||
|
||||
-- assimp reads our X3D like the source
|
||||
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 <Transform/> (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)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ api, errors
|
||||
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), "<X3D", 1, true) ~= nil, "F.save format x3d: XML")
|
||||
local _, e1 = decode("<X3D><Scene><Shape><Box/></Scene></X3D>")
|
||||
t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "mismatched XML: " .. tostring(e1))
|
||||
local _, e2 = decode("<?xml version='1.0'?><COLLADA/>")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "not X3D: " .. tostring(e2))
|
||||
-- VTK leaves the root element open: read anyway
|
||||
local open = decode("<X3D><Scene><Shape><Box/></Shape></Scene>\n")
|
||||
t:check(open and #open.meshes == 1, "an unclosed <X3D> 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([[<X3D><Scene><Shape DEF="S"><IndexedFaceSet coordIndex="0 1 2"><Coordinate point="0 0 0 1 0 0 0 1 0"/></IndexedFaceSet></Shape>
|
||||
<Transform translation="5 0 0"><Shape USE="S"/></Transform>
|
||||
<Switch whichChoice="0"><Group><Shape><Sphere containerField="geometry" radius="2"/></Shape></Group></Switch></Scene></X3D>]])
|
||||
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
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
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()
|
||||
Reference in new issue
Block a user