# STL fixtures (tools/fixtures.py). # tests/gen/stl_scenes.luau scenes for the encoder (indexed grid with colours, polygons under a TRS node, a node tree # with a matrix, a mirror and u16 positions) with the expected world-space triangle soup # (computed here with numpy) and the expected colour attribute words # tests/gen/stl.luau cases: STL files made by trimesh (binary / ascii), by assimp (obj / ply -> stl / stlb) # and by hand here (binary starting with "solid", VisCAM and Magics colours, raw attribute # words, odd ASCII: case, tabs, CRLF, several solids, quads, no endsolid, trailing bytes, # empty), each with reference values from trimesh's raw STL reader (facets, CRC-32 of the # f32 vertices and normals, area, bounds, attribute words); `ours`: our encoder's output # (run through the luau CLI: tools/fixtures/_stl_encode.luau) read back by trimesh and # assimp, as verdicts import json, math, os, shutil, struct, subprocess, tempfile, zlib INPUTS = ["src/Formats/model/stl", "tools/fixtures/_stl_reference.py", "tools/fixtures/_stl_encode.luau"] def f32(x): return struct.unpack(" ASCII and binary STL obj = "".join("v %r %r %r\n" % (f32(rng.uniform(-5, 5)), f32(rng.uniform(-5, 5)), f32(rng.uniform(-5, 5))) for _ in range(12)) obj += "f 1 2 3 4\nf 5 6 7\nf 8 9 10 11 12\nf 2 4 6\n" ply = "ply\nformat ascii 1.0\nelement vertex 5\nproperty float x\nproperty float y\nproperty float z\nelement face 3\nproperty list uchar int vertex_indices\nend_header\n" ply += "".join("%r %r %r\n" % (f32(rng.uniform(-5, 5)), f32(rng.uniform(-5, 5)), f32(rng.uniform(-5, 5))) for _ in range(5)) ply += "3 0 1 2\n4 0 2 3 4\n3 4 3 1\n" with open(os.path.join(tmp, "in.obj"), "w") as f: f.write(obj) with open(os.path.join(tmp, "in.ply"), "w") as f: f.write(ply) for src, dst, fid in (("in.obj", "assimp-obj.stl", "stl"), ("in.obj", "assimp-obj-b.stl", "stlb"), ("in.ply", "assimp-ply-b.stl", "stlb")): ctx.nix("assimp", ["assimp", "export", os.path.join(tmp, src), os.path.join(tmp, dst), "-f" + fid]) for name, data in files.items(): with open(os.path.join(tmp, name), "wb") as f: f.write(data) make = [["trimesh-icosphere.stl", "icosphere", "stl", 1], ["trimesh-icosphere-ascii.stl", "icosphere", "stl_ascii", 1], ["trimesh-box.stl", "box", "stl", 2], ["trimesh-capsule-ascii.stl", "capsule", "stl_ascii", 3], ["trimesh-soup.stl", "soup", "stl", 4], ["trimesh-soup-ascii.stl", "soup", "stl_ascii", 5]] refnames = [m[0] for m in make] + ["assimp-obj.stl", "assimp-obj-b.stl", "assimp-ply-b.stl"] refnames += [n for n, _, e in hand if not e.get("self")] # our encoder's output (the luau CLI prints "/ " lines) enc = subprocess.run(luau_cmd() + [os.path.join(ctx.root, "tools", "fixtures", "_stl_encode.luau")], capture_output=True, check=True, cwd=ctx.root).stdout.decode() ours = [] byname = {s["name"]: s for s in sc} for line in enc.split("\n"): if not line.strip(): continue label, hexdata = line.split(" ", 1) scene_name, encoding = label.split("/") s = byname[scene_name] fn = "ours-%s-%s.stl" % (scene_name, encoding) with open(os.path.join(tmp, fn), "wb") as f: f.write(bytes.fromhex(hexdata.strip())) o = {"name": label, "file": fn, "expect": s["expect"].hex(), "exact": s["exact"]} if encoding == "viscam": o["colors"] = s["viscamWords"] elif encoding == "magics": o["colors"] = s["magicsWords"] ours.append(o) code = open(os.path.join(ctx.root, "tools", "fixtures", "_stl_reference.py")).read() try: res = json.loads(ctx.python(["trimesh", "numpy"], code, input=json.dumps({"dir": tmp, "make": make, "ref": refnames, "ours": ours}).encode())) except subprocess.CalledProcessError as e: raise RuntimeError("stl reference: " + e.stderr.decode(errors="replace")[-3000:]) verdicts = [] for o in ours: ok, detail = res["ours"][o["name"]] # assimp reads it too faces, _ = assimp_info(ctx, os.path.join(tmp, o["file"])) want = len(bytes.fromhex(o["expect"])) // 36 if faces != want: ok, detail = False, "assimp reads %s faces, want %d" % (faces, want) verdicts.append({"name": o["name"], "ok": ok, "detail": detail}) cases = [] for name in refnames: with open(os.path.join(tmp, name), "rb") as f: data = f.read() r = res["ref"][name] c = {"name": name, "data": data, "facets": r["facets"], "pos_crc": r["pos_crc"], "area": r["area"], "bounds": r["bounds"]} extra = next((e for n, _, e in hand if n == name), {}) if "nrm_crc" in r: c["nrm_crc"] = r["nrm_crc"] elif "normals" in extra: # the normals written here (%.6e / repr of f32 values, read as f32) c["nrm_crc"] = crc(b"".join(struct.pack("<3f", *n) * 3 for n in extra["normals"])) if "cols" in extra: c["colors"] = extra["colors"] c["col_crc"] = crc(b"".join(bytes(col) * 3 for col in extra["cols"])) # the colour words trimesh read must be the ones written assert r.get("attr") is not None if "defaultColor" in extra: c["defaultColor"] = extra["defaultColor"] if "words" in extra: assert r["attr"] == extra["words"] c["attr_crc"] = crc(struct.pack("<%dH" % len(extra["words"]), *extra["words"])) if "solids" in extra: c["solids"] = extra["solids"] c["solidFacets"] = extra["solidFacets"] c["binary"] = not data.lstrip()[:5].lower() == b"solid" or (len(data) >= 84 and 84 + 50 * struct.unpack("