Files

136 lines
7.6 KiB
Python

# X3D fixtures (tools/fixtures.py): assimp's X3D / X3DV / X3DB test models (test/models/X3D*, read from the nixpkgs
# assimp source, never committed), scenes and glTF sample models written by VTK's vtkX3DExporter, glTF sample models
# written by assimp's X3D exporter, and hand-written feature files (below), with assimp's X3D reader as the reference
# (`assimp info -r`: meshes, faces, materials; world bounds from its PreTransformVertices export). Then our encoder's
# X3D output (tools/fixtures/_vrml_encode.luau) read back by assimp.
# tests/gen/x3d_cases.luau { name, data, files = { name -> bytes }, assimp, primitives (bool) }
# tests/gen/x3d_xcheck.luau { name, case? / gltf? / wcase?, assimp }
import importlib.util, os, re, subprocess
INPUTS = ["src/Rosetta/codecs/model/Vrml", "src/Rosetta/model/x3d", "src/Rosetta/model/vrml", "tests/fixtures/gltf",
"tools/fixtures/_vrml_ref.py", "tools/fixtures/_model_ref.py", "tools/fixtures/fbx.py", "tools/fixtures/vrml.py",
"tools/fixtures/_vrml_encode.luau"]
FEATURES = {
"feat_sets.x3d": """<?xml version="1.0" encoding="UTF-8"?>
<X3D profile="Interchange" version="3.3"><Scene>
<Transform translation="0 0 0"><Shape><Appearance><Material diffuseColor="1 0 0"/></Appearance>
<IndexedTriangleSet index="0 1 2 0 2 3" solid="false"><Coordinate point="0 0 0 1 0 0 1 1 0 0 1 0"/>
<ColorRGBA color="1 0 0 1 0 1 0 1 0 0 1 0.5 1 1 1 1"/></IndexedTriangleSet></Shape></Transform>
<Transform translation="2 0 0"><Shape><TriangleFanSet fanCount="5"><Coordinate point="0 0 0 1 0 0 1 1 0 0 1 0 -1 1 0"/></TriangleFanSet></Shape></Transform>
<Transform translation="4 0 0"><Shape><IndexedTriangleStripSet index="0 1 2 3 -1 4 5 6"><Coordinate point="0 0 0 1 0 0 0 1 0 1 1 0 0 2 0 1 2 0 0 3 0"/></IndexedTriangleStripSet></Shape></Transform>
<Transform translation="6 0 0"><Shape><IndexedQuadSet index="0 1 2 3"><Coordinate point="0 0 0 1 0 0 1 1 0 0 1 0"/></IndexedQuadSet></Shape></Transform>
<Transform translation="8 0 0"><Shape><TriangleSet><Coordinate point="0 0 0 1 0 0 0 1 0 0 0 1 1 0 1 0 1 1"/></TriangleSet></Shape></Transform>
<Transform translation="0 3 0"><Shape><LineSet vertexCount="3 2"><Coordinate point="0 0 0 1 0 0 1 1 0 2 2 2 3 3 3"/></LineSet></Shape></Transform>
</Scene></X3D>
""",
"feat_proto.x3d": """<?xml version="1.0" encoding="UTF-8"?>
<X3D profile="Immersive" version="3.3"><head><meta name="title" content="protos"/><unit category="length" name="centimetre" conversionFactor="0.01"/></head>
<Scene>
<ProtoDeclare name="Post"><ProtoInterface>
<field name="height" type="SFFloat" accessType="initializeOnly" value="2"/>
<field name="where" type="SFVec3f" accessType="inputOutput" value="0 0 0"/>
<field name="look" type="SFNode" accessType="inputOutput"><Appearance><Material diffuseColor="0 0 1"/></Appearance></field>
</ProtoInterface>
<ProtoBody><Transform><IS><connect nodeField="translation" protoField="where"/></IS>
<Shape><IS><connect nodeField="appearance" protoField="look"/></IS>
<Cylinder radius="0.1"><IS><connect nodeField="height" protoField="height"/></IS></Cylinder></Shape></Transform></ProtoBody>
</ProtoDeclare>
<ProtoInstance name="Post" DEF="P1"><fieldValue name="where" value="1 0 0"/></ProtoInstance>
<ProtoInstance name="Post"><fieldValue name="where" value="-1 0 0"/><fieldValue name="height" value="4"/></ProtoInstance>
<Transform DEF="Spin"><Shape><Appearance><PhysicalMaterial baseColor="0.5 0.5 0.5" metallic="0.25" roughness="0.75"/></Appearance>
<Box size="1 1 1"/></Shape></Transform>
<TimeSensor DEF="Clock" cycleInterval="2" loop="true"/>
<OrientationInterpolator DEF="Rot" key="0 0.5 1" keyValue="0 1 0 0 0 1 0 3.14159 0 1 0 6.28318"/>
<ROUTE fromNode="Clock" fromField="fraction_changed" toNode="Rot" toField="set_fraction"/>
<ROUTE fromNode="Rot" fromField="value_changed" toNode="Spin" toField="set_rotation"/>
<OrthoViewpoint fieldOfView="-2 -1 2 1" position="0 0 5"/>
<Shape><Appearance><UnlitMaterial emissiveColor="0 1 0"/></Appearance><PointSet><Coordinate point="0 5 0 1 5 0"/><Color color="1 0 0 0 1 0"/></PointSet></Shape>
</Scene></X3D>
""",
"feat_classic.x3dv": """#X3D V3.3 utf8
PROFILE Interchange
COMPONENT Geometry2D:1
META "title" "classic"
UNIT length millimetre 0.001
DEF Root Transform { translation 1 2 3 children [
Shape { appearance Appearance { material Material { diffuseColor 0 1 0 } }
geometry IndexedFaceSet { coord Coordinate { point [ 0 0 0 1 0 0 1 1 0 0 1 0 ] } coordIndex [ 0 1 2 3 -1 ]
color ColorRGBA { color [ 1 0 0 1, 0 1 0 1, 0 0 1 1, 1 1 1 0.5 ] } } }
Shape { geometry IndexedTriangleSet { index [ 0 1 2 ] coord Coordinate { point [ 0 0 1 1 0 1 0 1 1 ] } } }
] }
""",
}
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 reference(ctx, M, Fbx, path, tmp):
a = M.assimp_info(ctx, [path], raw=True)[0]
if not a.get("failed"):
a.pop("min", None)
a.pop("max", None)
a.update(Fbx.assimp_extra(ctx, path))
a.update(Fbx.ptv_bounds(ctx, path, tmp))
return a
def generate(ctx):
M = load(ctx, "_model_ref")
Fbx = load(ctx, "fbx")
R = load(ctx, "_vrml_ref")
V = load(ctx, "vrml")
tmp = M.tempdir()
cases = []
root = os.path.join(Fbx.assimp_src(ctx), "test", "models")
for sub in ("X3D", "X3DV", "X3DB"):
d = os.path.join(root, sub)
for f in sorted(os.listdir(d)):
if f.lower().endswith((".x3d", ".x3dv", ".x3db")):
files = {}
if f.startswith("HelloWorld"):
files["earth-topo.png"] = open(os.path.join(d, "earth-topo.png"), "rb").read()
cases.append({"name": "assimp/%s/%s" % (sub, f), "path": os.path.join(d, f), "files": files})
picks = V.gltf_picks(ctx)
for name in R.vtk_export(ctx, tmp, [p["path"] for p in picks]):
cases.append({"name": "vtk/" + name + ".x3d", "path": os.path.join(tmp, name + ".x3d"), "files": {}})
for p in picks:
out = os.path.join(tmp, "assimp_" + p["name"].split("/")[0] + ".x3d")
try:
ctx.nix("assimp", ["assimp", "export", p["path"], out, "-fx3d"])
except subprocess.CalledProcessError:
continue
cases.append({"name": "assimp-export/" + os.path.basename(out), "path": out, "files": dict(p["files"])})
for name, text in sorted(FEATURES.items()):
path = os.path.join(tmp, name)
with open(path, "w") as f:
f.write(text)
cases.append({"name": "feature/" + name, "path": path, "files": {}})
for c in cases:
c["data"] = open(c["path"], "rb").read()
c["primitives"] = V.primitives(c["data"])
c["assimp"] = reference(ctx, M, Fbx, c["path"], tmp) if c["path"].endswith(".x3d") else {"skip": True}
del c["path"]
ctx.emit("x3d_cases", cases)
# assimp reads our X3D output
ours = M.luau_encode(ctx, "tools/fixtures/_vrml_encode.luau")
xtmp = M.tempdir()
out = []
for name in sorted(n for n in ours if n.endswith(".x3d")):
path = os.path.join(xtmp, name)
with open(path, "wb") as f:
# assimp 6 cannot read quoted MFString urls (url='"a.png"', which the X3D encoding requires)
f.write(re.sub(rb'url="&quot;([^&"]*)&quot;"', rb'url="\1"', ours[name]))
e = {"name": name, "assimp": reference(ctx, M, Fbx, path, xtmp)}
m = re.match(r"(xcase|xgltf|wcase)_(\d+)\.x3d$", name)
if m:
e[m.group(1)] = int(m.group(2))
out.append(e)
ctx.emit("x3d_xcheck", out)