ws-3d3: codecs/model/Vrml (VRML97 / X3D classic parser, X3D XML adapter, node graph -> Scene builder, writer) + model/vrml (.wrl read / write) + model/x3d (.x3d / .x3dv read / write; .x3db UNSUPPORTED); tests against VTK and assimp
This commit is contained in:
1 parent
4c74171631
commit
76128bf381
20 files changed
+4906
-1
No files matched your search
@@ -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
|
||||
@@ -257,7 +257,7 @@ do
|
||||
MeshOps.merge(s)
|
||||
end
|
||||
return s
|
||||
end, samples, { mutations = 16, keepHeader = 17 })
|
||||
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