"""Independent QC: re-derives every module from the CSV (centre, bearing, orientation, pitch, azimuth) with its own math - it does not use the layout code's transforms - and checks it against the face outlines and rules.""" import csv, json, math, re, sys import numpy as np from shapely.geometry import Polygon, Point, box FACES = json.load(open("model.json"))["faces"] VENTS = json.load(open("model.json"))["vents"] L, W = 1.722, 1.134 CLR = {"eave": 0.25, "rake": 0.25, "wall": 0.25, "hip": 0.46, "valley": 0.46, "ridge": 0.46} rows = [] def add(i, status, evidence, sev, fix=""): rows.append(dict(id=i, status=status, evidence=evidence, sev=sev, fix=fix)) def plane(f, p_deg): az = math.radians(f["az"]); p = math.radians(p_deg) down = np.array([math.sin(az), math.cos(az)]) i = f["edges"].index("eave"); e0 = np.array(f["pts"][i]) def z(x, y): # height from uphill plan distance to the eave line return f["eave_h"] + (-(np.array([x, y]) - e0) @ down) * math.tan(p) h = np.array([math.cos(az), -math.sin(az), 0.0]) # horizontal, along eave s = np.array([-math.sin(az)*math.cos(p), -math.cos(az)*math.cos(p), math.sin(p)]) # up-slope unit return z, h, s def corners(r, f): p = float(r["pitch_deg"]); z, h, s = plane(f, p) c = np.array([float(r["centre_east_m"]), float(r["centre_north_m"]), float(r["centre_height_m"])]) bearing = float(r["long_side_bearing_deg"]); az = f["az"] long_horizontal = abs(((bearing - (az + 90)) + 90) % 180 - 90) < 1e-6 a, b = (h, s) if long_horizontal else (s, h) return [c + sa*L/2*a + sb*W/2*b for sa, sb in [(-1,-1),(1,-1),(1,1),(-1,1)]], a, b, h, s, z def dist_pt_line3(p, a, b): d = (b-a)/np.linalg.norm(b-a); v = p-a; return np.linalg.norm(v - (v@d)*d) META = json.load(open("model.json"))["meta"] def run(design): global CLR CLR = META[design]["clear"] strips = [Polygon(k) for k in META[design]["keepouts"]] strip_hits = 0 R = list(csv.DictReader(open(f"modules_design_{design}.csv"))) per = {} worst_edge = []; worst_h = 0; worst_sq = 0; overlaps = 0; min_gap = 9; vent_hits = [] inplane = {} for r in R: f = FACES[r["face"]]; per[r["face"]] = per.get(r["face"], 0) + 1 C, a, b, h, s, z = corners(r, f) # plane / height check for c in C: worst_h = max(worst_h, abs(c[2] - z(c[0], c[1]))) # inside + clearances (3D in-plane distance to each 3D edge) P3 = [np.array([x, y, z(x, y)]) for x, y in f["pts"]] poly = Polygon(f["pts"]) for c in C: if not poly.buffer(1e-6).contains(Point(c[0], c[1])): worst_edge.append((r["id"], "outside face", 0)) for k in range(len(P3)): d = dist_pt_line3(c, P3[k], P3[(k+1) % len(P3)]); need = CLR[f["edges"][k]] if d < need - 0.005: worst_edge.append((r["id"], f["edges"][k], round(d, 2))) # squareness: long side vs eave (h) or slope (s) ang = min(math.degrees(math.acos(min(1, abs(a @ h)))), math.degrees(math.acos(min(1, abs(a @ s))))) worst_sq = max(worst_sq, ang) # in-plane 2D rectangle for overlap / gaps / vents o = P3[0]; uv = [((c-o) @ h, (c-o) @ s) for c in C] rect = Polygon(uv); inplane.setdefault(r["face"], []).append((r["id"], rect)) plan_rect = Polygon([(c[0], c[1]) for c in C]) strip_hits += sum(1 for st in strips if plan_rect.intersection(st).area > 1e-4) for v in VENTS: if poly.contains(Point(v)): vp = np.array([v[0], v[1], z(*v)]); vv = Point((vp-o) @ h, (vp-o) @ s) d = rect.distance(vv) if d < 0.35: vent_hits.append((r["id"], v, round(d, 2))) for fid, lst in inplane.items(): for i in range(len(lst)): for j in range(i+1, len(lst)): if lst[i][1].intersection(lst[j][1]).area > 1e-6: overlaps += 1 min_gap = min(min_gap, lst[i][1].distance(lst[j][1])) return R, per, worst_edge, worst_h, worst_sq, overlaps, min_gap, vent_hits, strip_hits def street(R): fs = ft = 0 for r in R: dlt = abs((float(r["azimuth_deg"]) - 90 + 180) % 360 - 180) if dlt <= 45: fs += 1 elif dlt <= 100: ft += 1 return fs, ft def adjacent(fa, fb): A = [tuple(p) for p in FACES[fa]["pts"]]; B = [tuple(p) for p in FACES[fb]["pts"]] sa = {frozenset((A[i], A[(i+1) % len(A)])) for i in range(len(A))} sb = {frozenset((B[i], B[(i+1) % len(B)])) for i in range(len(B))} return bool(sa & sb) or Polygon(A).intersection(Polygon(B)).length > 0.5 if __name__ == "__main__": res = {} for dsg in ("A", "B", "C", "D", "E"): R, per, we, wh, wsq, ov, mg, vh, sh = run(dsg) n = len(R); kw = round(n*0.43, 2); fs, ft = street(R) res[dsg] = dict(n=n, kw=kw, per=per, edge_issues=we, height_dev=round(wh, 3), square=round(wsq, 2), overlaps=ov, min_gap=round(mg, 3), vents=vh, street=fs, angle=ft, strip_hits=sh, ridge=CLR["ridge"]) # fire: plan view share roof_plan = Polygon() for f in FACES.values(): roof_plan = roof_plan.union(Polygon(f["pts"])) M = json.load(open("model.json")) roof_plan = roof_plan.union(Polygon(M["front"])) cosp = math.cos(math.radians(22.6)) for dsg in ("A", "B", "C", "D", "E"): res[dsg]["array_plan_pct"] = round(100*res[dsg]["n"]*L*W*cosp/roof_plan.area, 1) used = set(res[dsg]["per"]) lack = [] for fid in used: if fid == "L-SE": continue free_adj = [g for g in FACES if g not in used and g != fid and adjacent(fid, g) and not g.startswith("G-")] if not free_adj: # own-plane strip: gap >= 0.914 m along the eave, free of modules, eave to ridge f = FACES[fid]; az = math.radians(f["az"]); hx = np.array([math.cos(az), -math.sin(az)]) i = f["edges"].index("eave"); e0 = np.array(f["pts"][i]); e1 = np.array(f["pts"][(i+1) % len(f["pts"])]) lo, hi = sorted([(e0-e0) @ hx, (e1-e0) @ hx]) iv = [] for r in csv.DictReader(open(f"modules_design_{dsg}.csv")): if r["face"] != fid: continue C, *_ = corners(r, f) xs = [(np.array(c[:2])-e0) @ hx for c in C]; iv.append((min(xs), max(xs))) iv.sort(); best = 0; cur = lo for a_, b_ in iv: best = max(best, a_-cur); cur = max(cur, b_) best = max(best, hi-cur) res[dsg].setdefault("own_strip", {})[fid] = round(best, 2) if best < 0.914: lack.append(fid) res[dsg]["no_free_neighbour"] = lack res["roof_plan_m2"] = round(roof_plan.area, 1) json.dump(res, open("qc.json", "w"), indent=1, default=str) print(json.dumps(res, indent=1, default=str))