feat: harden CAD showcase skill and delivery pipeline
This commit is contained in:
@@ -39,6 +39,8 @@ import math
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import showcase_schema
|
||||
|
||||
|
||||
def dist_pt_seg(p, a, b) -> float:
|
||||
ax, ay = a
|
||||
@@ -66,8 +68,7 @@ def rot(p, th, c):
|
||||
return (x * ct - y * st, x * st + y * ct)
|
||||
|
||||
|
||||
def register(model_polys, site_pts, angle_window=None, coarse=1.0, refine=0.05,
|
||||
trim=0.9):
|
||||
def register(model_polys, site_pts, angle_window=None, coarse=1.0, refine=0.05, trim=0.9):
|
||||
"""Best (theta_deg, translation, score) overlaying model polygons on points.
|
||||
|
||||
score = trimmed mean distance from each site point to the nearest
|
||||
@@ -80,9 +81,10 @@ def register(model_polys, site_pts, angle_window=None, coarse=1.0, refine=0.05,
|
||||
|
||||
def score_at(th_deg, t):
|
||||
th = math.radians(th_deg)
|
||||
edges = [((rot(a, th, mc)[0] + t[0], rot(a, th, mc)[1] + t[1]),
|
||||
(rot(b, th, mc)[0] + t[0], rot(b, th, mc)[1] + t[1]))
|
||||
for a, b in edges_m]
|
||||
edges = [
|
||||
((rot(a, th, mc)[0] + t[0], rot(a, th, mc)[1] + t[1]), (rot(b, th, mc)[0] + t[0], rot(b, th, mc)[1] + t[1]))
|
||||
for a, b in edges_m
|
||||
]
|
||||
ds = sorted(min(dist_pt_seg(p, a, b) for a, b in edges) for p in site_pts)
|
||||
k = max(1, int(len(ds) * trim))
|
||||
return sum(ds[:k]) / k
|
||||
@@ -99,9 +101,13 @@ def register(model_polys, site_pts, angle_window=None, coarse=1.0, refine=0.05,
|
||||
t = list(best[1])
|
||||
for _ in range(4):
|
||||
thr = math.radians(th0)
|
||||
edges = [((rot(a, thr, mc)[0] + t[0], rot(a, thr, mc)[1] + t[1]),
|
||||
(rot(b, thr, mc)[0] + t[0], rot(b, thr, mc)[1] + t[1]))
|
||||
for a, b in edges_m]
|
||||
edges = [
|
||||
(
|
||||
(rot(a, thr, mc)[0] + t[0], rot(a, thr, mc)[1] + t[1]),
|
||||
(rot(b, thr, mc)[0] + t[0], rot(b, thr, mc)[1] + t[1]),
|
||||
)
|
||||
for a, b in edges_m
|
||||
]
|
||||
vecs = []
|
||||
for p in site_pts:
|
||||
dmin, q = 1e9, None
|
||||
@@ -134,20 +140,26 @@ def register(model_polys, site_pts, angle_window=None, coarse=1.0, refine=0.05,
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("--spec", required=True, help="building_spec.json")
|
||||
parser.add_argument("--floors", nargs="+", required=True,
|
||||
help="floor keys whose footprints form the registration target")
|
||||
parser.add_argument("--site-points", required=True,
|
||||
help="JSON: [[x,y],...] site outline vertices / corner callouts (m)")
|
||||
parser.add_argument("--angle-window", nargs=2, type=float, default=None,
|
||||
metavar=("LO", "HI"), help="restrict rotation search (deg)")
|
||||
parser.add_argument(
|
||||
"--floors", nargs="+", required=True, help="floor keys whose footprints form the registration target"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--site-points", required=True, help="JSON: [[x,y],...] site outline vertices / corner callouts (m)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--angle-window", nargs=2, type=float, default=None, metavar=("LO", "HI"), help="restrict rotation search (deg)"
|
||||
)
|
||||
parser.add_argument("--out", required=True)
|
||||
parser.add_argument("--prefix", default="", help="building prefix for the manifest entry")
|
||||
args = parser.parse_args()
|
||||
|
||||
spec = json.loads(Path(args.spec).read_text(encoding="utf-8"))
|
||||
spec = showcase_schema.normalize_spec(json.loads(Path(args.spec).read_text(encoding="utf-8")))
|
||||
try:
|
||||
showcase_schema.validate_spec(spec)
|
||||
except showcase_schema.ValidationFailure as exc:
|
||||
raise SystemExit(str(exc)) from exc
|
||||
polys = []
|
||||
for fk in args.floors:
|
||||
floor = spec.get("floors", {}).get(fk)
|
||||
@@ -155,8 +167,9 @@ def main() -> int:
|
||||
raise SystemExit(f"floor {fk!r} not in spec")
|
||||
polys.extend((floor.get("footprint") or {}).get("polygons") or [])
|
||||
if not polys:
|
||||
raise SystemExit("no footprint polygons on the requested floors — "
|
||||
"enable generate_footprints or pick other floors")
|
||||
raise SystemExit(
|
||||
"no footprint polygons on the requested floors — enable generate_footprints or pick other floors"
|
||||
)
|
||||
raw = json.loads(Path(args.site_points).read_text(encoding="utf-8"))
|
||||
site_pts = raw.get("points") if isinstance(raw, dict) else raw
|
||||
site_pts = [(float(p[0]), float(p[1])) for p in site_pts]
|
||||
@@ -185,12 +198,19 @@ def main() -> int:
|
||||
out = Path(args.out)
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
out.write_text(json.dumps(result, ensure_ascii=False, indent=1), encoding="utf-8")
|
||||
print(json.dumps({"out": str(out), "rotation_deg": result["rotation_deg"],
|
||||
"residual_m": result["residual_m"]}, ensure_ascii=False))
|
||||
print(
|
||||
json.dumps(
|
||||
{"out": str(out), "rotation_deg": result["rotation_deg"], "residual_m": result["residual_m"]},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
)
|
||||
if score > 2.0:
|
||||
print(f"WARNING residual {score:.2f} m is high — wrong outline, wrong floors, "
|
||||
"or the site points include strays (garage outline / fire pads); "
|
||||
"plot the overlay before trusting this placement.", file=sys.stderr)
|
||||
print(
|
||||
f"WARNING residual {score:.2f} m is high — wrong outline, wrong floors, "
|
||||
"or the site points include strays (garage outline / fire pads); "
|
||||
"plot the overlay before trusting this placement.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user