Files

100 lines
4.5 KiB
Python

# VRML97 fixtures (tools/fixtures.py): assimp's WRL test models (test/models/WRL, read from the nixpkgs assimp source,
# never committed; assimp itself is built without its VRML reader), scenes and glTF sample models written by VTK's
# vtkVRMLExporter, and the hand-written feature files of tools/fixtures/_vrml_ref.py, with VTK's vtkVRMLImporter as
# the reference (polygons, lines, world bounds). Then our encoder's output (tools/fixtures/_vrml_encode.luau) read back
# by VTK.
# tests/gen/vrml_cases.luau { name, data, files = { name -> bytes }, vtk, primitives (bool: tessellated shapes) }
# tests/gen/vrml_gltf.luau glTF sample models for the writer round trips: { name, data, files }
# tests/gen/vrml_xcheck.luau { name, case?, gltf?, vtk }
import importlib.util, os, re
INPUTS = ["src/Rosetta/codecs/model/Vrml", "src/Rosetta/model/vrml", "tests/fixtures/gltf", "tools/fixtures/_vrml_ref.py",
"tools/fixtures/_model_ref.py", "tools/fixtures/fbx.py", "tools/fixtures/_vrml_encode.luau"]
GLTF = ["Box", "BoxTextured", "BoxVertexColors", "Cameras", "MeshPrimitiveModes", "SimpleMeshes", "AnimatedTriangle", "InterpolationTest",
"NegativeScaleTest", "TextureCoordinateTest", "MultiUVTest"]
def load(ctx, name):
spec = importlib.util.spec_from_file_location(name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def primitives(data):
return bool(re.search(rb"\b(Box|Sphere|Cone|Cylinder|Extrusion|ElevationGrid|Text)\s*\{|<(Box|Sphere|Cone|Cylinder|Extrusion|ElevationGrid|Text)\b", data))
def gltf_picks(ctx):
gl = os.path.join(ctx.root, "tests", "fixtures", "gltf")
picks = []
for model in GLTF:
d0 = os.path.join(gl, model)
if not os.path.isdir(d0):
continue
for variant in ("glTF", "glTF-Embedded", "glTF-Binary"):
d = os.path.join(d0, variant)
if not os.path.isdir(d):
continue
mains = [f for f in sorted(os.listdir(d)) if f.endswith(".gltf") or f.endswith(".glb")]
if mains:
picks.append({"name": model + "/" + variant, "path": os.path.join(d, mains[0]), "data": open(os.path.join(d, mains[0]), "rb").read(),
"files": {f: open(os.path.join(d, f), "rb").read() for f in sorted(os.listdir(d)) if f != mains[0]}})
break
return picks
def generate(ctx):
M = load(ctx, "_model_ref")
Fbx = load(ctx, "fbx")
R = load(ctx, "_vrml_ref")
tmp = M.tempdir()
cases = []
src = os.path.join(Fbx.assimp_src(ctx), "test", "models", "WRL")
for f in sorted(os.listdir(src)):
if f.lower().endswith(".wrl"):
files = {}
if f == "HelloWorld.wrl":
files["earth-topo.png"] = open(os.path.join(src, "earth-topo.png"), "rb").read()
cases.append({"name": "assimp/" + f, "path": os.path.join(src, f), "files": files})
picks = gltf_picks(ctx)
for name in R.vtk_export(ctx, tmp, [p["path"] for p in picks if p["path"].endswith((".gltf", ".glb"))]):
cases.append({"name": "vtk/" + name + ".wrl", "path": os.path.join(tmp, name + ".wrl"), "files": {}})
for name, text in sorted(R.FEATURES.items()):
p = os.path.join(tmp, name)
with open(p, "w") as f:
f.write(text)
cases.append({"name": "feature/" + name, "path": p, "files": dict(R.INLINE)})
for name, text in R.INLINE.items():
with open(os.path.join(tmp, name), "w") as f:
f.write(text)
refs = R.vtk_read(ctx, [c["path"] for c in cases])
for c, v in zip(cases, refs):
c["data"] = open(c["path"], "rb").read()
c["vtk"] = v
c["primitives"] = primitives(c["data"])
c["vtkTransforms"] = R.vtk_xform_ok(c["data"])
del c["path"]
ctx.emit("vrml_cases", cases)
for p in picks:
del p["path"]
ctx.emit("vrml_gltf", picks)
# VTK reads our output
ours = M.luau_encode(ctx, "tools/fixtures/_vrml_encode.luau")
xtmp = M.tempdir()
names = sorted(n for n in ours if n.endswith(".wrl"))
for name, data in ours.items():
with open(os.path.join(xtmp, name), "wb") as f:
f.write(data)
refs = R.vtk_read(ctx, [os.path.join(xtmp, n) for n in names])
out = []
for n, v in zip(names, refs):
e = {"name": n, "vtk": v, "vtkTransforms": R.vtk_xform_ok(ours[n])}
m = re.match(r"(case|gltf)_(\d+)\.wrl$", n)
if m:
e[m.group(1)] = int(m.group(2))
out.append(e)
ctx.emit("vrml_xcheck", out)