# OFF fixtures (tools/fixtures.py). # tests/gen/off_scenes.luau scenes for the encoder (STCNOFF mesh, polygons under a TRS node, a float-colour point # cloud, polygons with face colours) with the expected file content, computed here # tests/gen/off.luau cases: OFF files written by trimesh and by hand here (COFF with integer / float colours, # RGB / RGBA, NOFF, STOFF, STCNOFF, face colours of both kinds, colormap indices, comments # everywhere, counts on the header line, CRLF and tabs, n-gons, a point cloud), with the # values they were written from (positions cross-checked with trimesh's OFF reader); # `ours`: our encoder's output (luau CLI: tools/fixtures/_off_encode.luau) read back by # trimesh, assimp and a small reader here, as verdicts import json, math, os, shutil, struct, subprocess, tempfile, zlib INPUTS = ["src/Rosetta/model/off", "tools/fixtures/_off_reference.py", "tools/fixtures/_off_encode.luau"] def f32(x): return struct.unpack(" tol * max(1, abs(e))) if tol else f32(g) != f32(e): return False, "vertex value %r, want %r" % (g, e) if faces != want["faces"]: return False, "faces differ" if "faceColors" in want: if [list(map(int, c)) for c in fcols] != want["faceColors"]: return False, "face colours differ" elif any(fcols): return False, "unexpected face colours" return True, "%d vertices, %d faces" % (len(verts), len(faces)) def luau_cmd(): exe = shutil.which("luau") return [exe] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau"] def assimp_faces(ctx, path): txt = ctx.nix("assimp", ["assimp", "info", path]).decode(errors="replace") for line in txt.split("\n"): if line.startswith("Faces:"): return int(line.split()[1]) return None def generate(ctx): rng = ctx.rng(53) tmp = tempfile.mkdtemp(prefix="formats-off-") try: _generate(ctx, rng, tmp) finally: shutil.rmtree(tmp, ignore_errors=True) def _generate(ctx, rng, tmp): sc = scenes(rng) ctx.emit("off_scenes", sc) cases = handmade(rng) for c in cases: with open(os.path.join(tmp, c["name"]), "wb") as f: f.write(c["data"]) make = ["trimesh-ico.off", "trimesh-capsule.off"] enc = subprocess.run(luau_cmd() + [os.path.join(ctx.root, "tools", "fixtures", "_off_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("/") fn = "ours-%s-%s.off" % (scene_name, encoding) data = bytes.fromhex(hexdata.strip()) with open(os.path.join(tmp, fn), "wb") as f: f.write(data) ours.append((label, fn, data, byname[scene_name]["encodings"][encoding])) names = [c["name"] for c in cases if c["vertices"] > 0] + make + [o[1] for o in ours] code = open(os.path.join(ctx.root, "tools", "fixtures", "_off_reference.py")).read() try: ref = json.loads(ctx.python(["trimesh", "numpy"], code, input=json.dumps({"dir": tmp, "make": make, "read": names}).encode())) except subprocess.CalledProcessError as e: raise RuntimeError("off reference: " + e.stderr.decode(errors="replace")[-3000:]) # trimesh agrees with the values the files were written from for c in cases: r = ref.get(c["name"]) if r: assert r["vertices"] == c["vertices"] and r["pos_crc"] == c["pos_crc"], c["name"] for name in make: with open(os.path.join(tmp, name), "rb") as f: data = f.read() r = ref[name] tris = [x for f in r["faces"] for x in f] cases.append({"name": name, "data": data, "keyword": "OFF", "vertices": r["vertices"], "faces": r["tris"], "pos_crc": r["pos_crc"], "allTri": True, "tris": r["tris"], "tri_crc": crc(pack("I", tris)), "corners_crc": crc(pack("I", tris)), "sizes_crc": crc(pack("I", [3] * r["tris"]))}) verdicts = [] for label, fn, data, want in ours: ok, detail = check_ours(data.decode(), want) r = ref.get(fn) if ok and r and not want.get("tol"): exp = pack("f", [c for v in want["vertices"] for c in v[:3]]) if r["pos_crc"] != crc(exp): ok, detail = False, "trimesh reads other positions" if ok and want["faces"]: got = assimp_faces(ctx, os.path.join(tmp, fn)) exp_f = sum(len(f) - 2 for f in want["faces"]) if got != exp_f: ok, detail = False, "assimp reads %s faces, want %d" % (got, exp_f) verdicts.append({"name": label, "ok": ok, "detail": detail}) ctx.emit("off", {"cases": cases, "ours": verdicts})