# PLY fixtures (tools/fixtures.py). # tests/gen/ply_scenes.luau scenes for the encoder (an indexed mesh with every attribute kind and custom properties, # polygons under a TRS node, a float-colour point cloud, two meshes merged, a mirrored # polygon) with the expected vertex properties (names, types, values) and faces, computed # here # tests/gen/ply.luau cases: PLY files written by plyfile (ascii / binary LE / BE, every property type, odd # orders, n-gons up to 300 vertices, face properties and lists, edge / material / tristrips # elements, comments, obj_info, point clouds, a 65k-vertex sphere) and by assimp (OFF / OBJ # -> PLY), each with reference values from plyfile mapped to the Scene conventions; # `ours`: our encoder's output (luau CLI: tools/fixtures/_ply_encode.luau) read back by # plyfile and assimp, as verdicts import json, math, os, shutil, struct, subprocess, tempfile INPUTS = ["src/Formats/model/ply", "tools/fixtures/_ply_reference.py", "tools/fixtures/_ply_encode.luau"] def f32(x): return struct.unpack(" 3 else {})} for p in sc["props"]] if "faces" in sc: sc["faceSizes"] = pack("I", [len(f) for f in sc["faces"]]) sc["faceCorners"] = pack("I", [c for f in sc["faces"] for c in f]) return out 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(47) tmp = tempfile.mkdtemp(prefix="formats-ply-") try: _generate(ctx, rng, tmp) finally: shutil.rmtree(tmp, ignore_errors=True) def _generate(ctx, rng, tmp): sc = scenes(rng) ctx.emit("ply_scenes", sc) make = ["cube-le.ply", "colors-be.ply", "ascii-mixed.ply", "points-le.ply", "points-ascii-float.ply", "types-be.ply", "big-le.ply", "tristrips.ply"] # assimp conversions: OFF -> PLY (ascii), OBJ -> PLY (binary) off = "OFF\n6 3 0\n" + "".join("%r %r %r\n" % (f32(rng.uniform(-5, 5)), f32(rng.uniform(-5, 5)), f32(rng.uniform(-5, 5))) for _ in range(6)) off += "3 0 1 2\n4 1 3 4 2\n3 4 5 2\n" 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(8)) obj += "vn 0 0 1\nf 1//1 2//1 3//1 4//1\nf 5//1 6//1 7//1\nf 1//1 8//1 5//1\n" with open(os.path.join(tmp, "in.off"), "w") as f: f.write(off) with open(os.path.join(tmp, "in.obj"), "w") as f: f.write(obj) ctx.nix("assimp", ["assimp", "export", os.path.join(tmp, "in.off"), os.path.join(tmp, "assimp-off.ply"), "-fply"]) ctx.nix("assimp", ["assimp", "export", os.path.join(tmp, "in.obj"), os.path.join(tmp, "assimp-obj-b.ply"), "-fplyb"]) refs = make + ["assimp-off.ply", "assimp-obj-b.ply"] enc = subprocess.run(luau_cmd() + [os.path.join(ctx.root, "tools", "fixtures", "_ply_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.ply" % (scene_name, encoding) with open(os.path.join(tmp, fn), "wb") as f: f.write(bytes.fromhex(hexdata.strip())) ours.append({"name": label, "file": fn, "props": [{**p, "values": p["values"].hex()} for p in s["props"]], "faces": s.get("faces")}) code = open(os.path.join(ctx.root, "tools", "fixtures", "_ply_reference.py")).read() try: res = json.loads(ctx.python(["plyfile", "numpy"], code, input=json.dumps({"dir": tmp, "make": make, "ref": refs, "ours": ours}).encode())) except subprocess.CalledProcessError as e: raise RuntimeError("ply reference: " + e.stderr.decode(errors="replace")[-3000:]) verdicts = [] for o in ours: ok, detail = res["ours"][o["name"]] if o["faces"]: want = sum(len(f) - 2 for f in o["faces"]) got = assimp_faces(ctx, os.path.join(tmp, o["file"])) if got != want: ok, detail = False, "assimp reads %s faces, want %d" % (got, want) verdicts.append({"name": o["name"], "ok": ok, "detail": detail}) cases = [] for name in refs: with open(os.path.join(tmp, name), "rb") as f: data = f.read() r = res["ref"][name] c = {"name": name, "data": data} c.update({k: v for k, v in r.items() if v is not None}) cases.append(c) ctx.emit("ply", {"cases": cases, "ours": verdicts})