Something went wrong. Try again.
Builds CAD models from 3d scan data
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107#!/usr/bin/env python3"""Validation harness: upload each test STL, run /api/discover, and compare thediscovered holes/planes against the ground-truth JSON emitted by gen_test.
Usage: python3 scripts/validate.py [--server http://127.0.0.1:8765] [testdata_dir]"""import json, sys, glob, os, urllib.request, urllib.error
SERVER = "http://127.0.0.1:8765"args = [a for a in sys.argv[1:]]if "--server" in args: i = args.index("--server"); SERVER = args[i + 1]; del args[i:i + 2]TDIR = args[0] if args else "testdata"
POS_TOL = 2.0 # mm, hole-centre match radiusDIA_TOL_CLEAN = 0.4 # mmDIA_TOL_NOISE = 1.2 # mm
def post(path, data=None, files=None): url = SERVER + path if files: boundary = "----scan2cadBoundary" body = b"" for name, (fn, content) in files.items(): body += f"--{boundary}\r\n".encode() body += f'Content-Disposition: form-data; name="{name}"; filename="{fn}"\r\n'.encode() body += b"Content-Type: application/octet-stream\r\n\r\n" + content + b"\r\n" body += f"--{boundary}--\r\n".encode() req = urllib.request.Request(url, data=body) req.add_header("Content-Type", f"multipart/form-data; boundary={boundary}") else: req = urllib.request.Request(url, data=json.dumps(data or {}).encode()) req.add_header("Content-Type", "application/json") with urllib.request.urlopen(req, timeout=120) as r: return json.load(r)
def validate(stl, gtpath): gt = json.load(open(gtpath)) with open(stl, "rb") as f: up = post("/api/upload", files={"file": (os.path.basename(stl), f.read())}) if not up.get("ok"): return False, f"upload failed: {up}" disc = post("/api/discover", {}) if not disc.get("ok"): return False, f"discover failed: {disc}"
noise = gt.get("noiseSigmaMM", 0) > 0 dia_tol = DIA_TOL_NOISE if noise else DIA_TOL_CLEAN detected = [c for c in disc["cyls"] if c["isHole"]]
matched, details = 0, [] used = set() for h in gt["holes"]: best, bestd = None, 1e9 for i, c in enumerate(detected): if i in used: continue dx, dy = c["center"][0] - h["x"], c["center"][1] - h["y"] d = (dx * dx + dy * dy) ** 0.5 if d < bestd: bestd, best = d, i if best is not None and bestd <= POS_TOL: c = detected[best] ddia = abs(c["diameter"] - h["diameter"]) ok = ddia <= dia_tol if ok: matched += 1 used.add(best) details.append(f" Ø{h['diameter']:.1f}@({h['x']},{h['y']}) -> " f"Ø{c['diameter']:.2f} pos_err {bestd:.2f} dia_err {ddia:.2f} " f"{'OK' if ok else 'DIA_FAIL'}") else: details.append(f" Ø{h['diameter']:.1f}@({h['x']},{h['y']}) -> MISSED") extra = len(detected) - len(used) n_planes = len(disc["planes"]) datum_ok = n_planes >= 1 npass = matched == len(gt["holes"]) and datum_ok summary = (f"holes {matched}/{len(gt['holes'])} planes {n_planes} " f"extra_cyls {extra} ms {disc['ms']:.1f}") return npass, summary + "\n" + "\n".join(details)
def main(): stls = sorted(glob.glob(os.path.join(TDIR, "*.stl"))) if not stls: print(f"no STLs in {TDIR}"); return 1 allpass = True for stl in stls: gtpath = stl[:-4] + ".json" if not os.path.exists(gtpath): continue try: ok, msg = validate(stl, gtpath) except urllib.error.URLError as e: print(f"✗ {os.path.basename(stl)}: server error {e} (is scan2cad running?)") return 1 allpass &= ok print(f"{'✓' if ok else '✗'} {os.path.basename(stl):24s} {msg}\n") print("ALL PASS" if allpass else "SOME FAILURES") return 0 if allpass else 1
if __name__ == "__main__": sys.exit(main())