ws-3d: model/stl (ASCII + binary, VisCAM / Magics colours), model/ply (ASCII + binary LE/BE, arbitrary properties, point clouds, raw extra elements), model/off (OFF/COFF/NOFF/STOFF, face colours), read + write; codecs/MeshFlatten (Scene -> one world-space mesh for the mesh-only writers, shared instead of a copy per format); tests vs assimp / trimesh / plyfile, reference tools reading our output
This commit is contained in:
1 parent
fb909438a5
commit
35891a06d1
18 files changed
+5408
No files matched your search
@@ -0,0 +1,112 @@
|
||||
# STL reference values (run by tools/fixtures/stl.py through ctx.python(["trimesh", "numpy"], ...), python -I).
|
||||
# stdin: JSON {"dir": tmp, "make": [[name, kind, file_type, seed]], "ref": [name], "ours": [{name, file, ...}]}
|
||||
# stdout: JSON {"ref": {name: {...}}, "ours": {name: [ok, detail]}}
|
||||
# make: meshes made and exported by trimesh itself (binary / ascii STL)
|
||||
# ref: every file read with trimesh.exchange.stl.load_stl (the raw arrays, no processing): facet count, CRC-32 of
|
||||
# the f32 triangle vertices / facet normals (as the file has them), area, bounds, the raw attribute words
|
||||
# ours: our encoder's output read back by trimesh and compared with the expected world-space triangles
|
||||
import json, os, sys, zlib
|
||||
import numpy as np
|
||||
import trimesh
|
||||
from trimesh.exchange import stl as tstl
|
||||
|
||||
req = json.load(sys.stdin)
|
||||
d = req["dir"]
|
||||
out = {"ref": {}, "ours": {}}
|
||||
|
||||
|
||||
def path(n):
|
||||
return os.path.join(d, n)
|
||||
|
||||
|
||||
for name, kind, ftype, seed in req["make"]:
|
||||
rng = np.random.default_rng(seed)
|
||||
if kind == "icosphere":
|
||||
m = trimesh.creation.icosphere(subdivisions=3, radius=2.5)
|
||||
elif kind == "box":
|
||||
m = trimesh.creation.box(extents=(1.5, 2.0, 0.25))
|
||||
elif kind == "capsule":
|
||||
m = trimesh.creation.capsule(height=3.0, radius=0.7)
|
||||
else:
|
||||
tri = (rng.random((1500, 3, 3)) * 200 - 100).astype(np.float32)
|
||||
m = trimesh.Trimesh(vertices=tri.reshape(-1, 3), faces=np.arange(4500).reshape(-1, 3), process=False)
|
||||
data = m.export(file_type=ftype)
|
||||
with open(path(name), "wb") as f:
|
||||
f.write(data.encode() if isinstance(data, str) else data)
|
||||
|
||||
|
||||
def load(name):
|
||||
with open(path(name), "rb") as f:
|
||||
r = tstl.load_stl(f)
|
||||
if "geometry" in r: # several solids
|
||||
parts = list(r["geometry"].values())
|
||||
if not parts:
|
||||
return np.zeros((0, 3)), np.zeros((0, 3), dtype=np.int64), np.zeros((0, 3)), None
|
||||
vs, fs, ns, off = [], [], [], 0
|
||||
for g in parts:
|
||||
vs.append(g["vertices"])
|
||||
fs.append(np.asarray(g["faces"]) + off)
|
||||
ns.append(g["face_normals"])
|
||||
off += len(g["vertices"])
|
||||
# trimesh does not read normals written on the "outer loop" line: no normals then
|
||||
nrm = None if any(x is None for x in ns) else np.concatenate(ns)
|
||||
return np.concatenate(vs), np.concatenate(fs), nrm, None
|
||||
attr = r.get("face_attributes", {}).get("stl")
|
||||
return np.asarray(r["vertices"]), np.asarray(r["faces"]), np.asarray(r["face_normals"]), attr
|
||||
|
||||
|
||||
def crc(a, dt):
|
||||
return zlib.crc32(np.ascontiguousarray(np.asarray(a).astype(dt)).tobytes())
|
||||
|
||||
|
||||
for name in req["ref"]:
|
||||
v, f, n, attr = load(name)
|
||||
tri = v[f].astype(np.float32) # (n, 3, 3)
|
||||
m = trimesh.Trimesh(vertices=v, faces=f, process=False)
|
||||
res = {
|
||||
"facets": int(len(f)),
|
||||
"pos_crc": crc(tri, "<f4"),
|
||||
"area": float(m.area) if len(f) else 0.0,
|
||||
"bounds": [float(x) for x in (m.bounds.reshape(-1) if len(f) else np.zeros(6))],
|
||||
}
|
||||
if n is not None:
|
||||
res["nrm_crc"] = crc(np.repeat(np.asarray(n, dtype=np.float32), 3, axis=0), "<f4")
|
||||
if attr is not None:
|
||||
res["attr"] = [int(x) for x in attr]
|
||||
out["ref"][name] = res
|
||||
|
||||
# our encoder's files
|
||||
for o in req["ours"]:
|
||||
name = o["name"]
|
||||
try:
|
||||
v, f, n, attr = load(o["file"])
|
||||
tri = v[f].astype(np.float32).reshape(-1, 9)
|
||||
want = np.frombuffer(bytes.fromhex(o["expect"]), dtype="<f4").reshape(-1, 9)
|
||||
if tri.shape != want.shape:
|
||||
out["ours"][name] = [False, "facet count %s, want %s" % (tri.shape, want.shape)]
|
||||
continue
|
||||
tol = 0 if o["exact"] else 1e-5 * max(1.0, float(np.abs(want).max()))
|
||||
err = float(np.abs(tri.astype(np.float64) - want.astype(np.float64)).max()) if len(want) else 0.0
|
||||
if err > tol:
|
||||
out["ours"][name] = [False, "positions differ by %g" % err]
|
||||
continue
|
||||
# the stored facet normals: the right-hand rule normal of each triangle
|
||||
t = want.astype(np.float64).reshape(-1, 3, 3)
|
||||
cn = np.cross(t[:, 1] - t[:, 0], t[:, 2] - t[:, 0])
|
||||
ln = np.linalg.norm(cn, axis=1)
|
||||
cn[ln > 0] /= ln[ln > 0, None]
|
||||
nerr = float(np.abs(np.asarray(n, dtype=np.float64) - cn).max()) if len(want) else 0.0
|
||||
if nerr > 1e-4:
|
||||
out["ours"][name] = [False, "facet normals differ by %g" % nerr]
|
||||
continue
|
||||
if "colors" in o:
|
||||
want_words = o["colors"]
|
||||
got = [int(x) for x in attr] if attr is not None else []
|
||||
if got != want_words:
|
||||
out["ours"][name] = [False, "attribute words differ"]
|
||||
continue
|
||||
out["ours"][name] = [True, "%d facets, max error %g" % (len(want), err)]
|
||||
except Exception as e: # noqa: BLE001 - a reference failure is a verdict
|
||||
out["ours"][name] = [False, "trimesh: %s" % e]
|
||||
|
||||
json.dump(out, sys.stdout)
|
||||
Reference in new issue
Block a user