"""Experimental whole-object evidence, without admission threshold changes. Each branch must first have its own visible paint body. Another branch may explain a bright flank only inside its measured polygon. Remaining exterior samples still have to be dark and observed. This module creates no geometry. """ import numpy as np import shapely from beta_profiles import describe from interval_profiles import strip from marking_rules import POLICY def measure(p, readers): d = describe(p) if d is None: return None ts = np.arange(-d["length"] / 2 + 0.04, d["length"] / 2 - 0.04, 0.025) ns = np.arange(-0.7, 0.701, 0.025) core = abs(ns) <= max(0.025, min(0.04, d["width"] * 0.2)) body = abs(ns) <= d["width"] / 2 flank = (abs(ns) >= d["width"] / 2 + 0.08) & (abs(ns) <= d["width"] / 2 + 0.14) if not len(ts) or not body.any() or not flank.any(): return None normal = np.array([-d["axis"][1], d["axis"][0]]) xy = d["origin"] + ts[:, None, None] * d["axis"] + ns[None, :, None] * normal body_points = xy[:, body].reshape(-1, 2) # OpenCV's fitted rectangle is float32. Its boundary is not the polygon's # authority: a rounded endpoint just outside the actual body is context. inside = shapely.intersects_xy(p, body_points[:, 0], body_points[:, 1]) if not inside.any(): return None rows, bodies, sides = [], [], [] for read in readers: con, valid = strip(d["origin"], d["axis"], ts, ns, read) signal = np.median(con[:, core], axis=1) cut = max(15.0, 0.35 * float(np.median(signal))) row = dict( valid=float(valid[:, core].mean()), support=float((signal > cut).mean()), contrast=float(np.median(signal)), dark_flank=float((con[:, flank] < cut).mean()), flank_valid=float(valid[:, flank].mean()), ) rows.append(row) bodies.append( dict(valid=valid[:, body].ravel()[inside], paint=(con[:, body] > cut).ravel()[inside]) ) sides.append(dict(valid=valid[:, flank].ravel(), dark=(con[:, flank] < cut).ravel())) # Exact legacy metric reconstruction is checked in qualification. The body # condition is additional, not a substitute for the existing central test. legacy = len(rows) == 2 and all( r["valid"] >= 0.98 and r["support"] >= 0.93 and r["contrast"] >= 22 and r["dark_flank"] >= 0.85 and r["flank_valid"] >= 0.95 for r in rows ) body_ok = len(rows) == 2 and all( r["valid"] >= POLICY["min_valid"] and r["support"] >= POLICY["min_paint_support"] and r["contrast"] >= POLICY["min_contrast"] and b["valid"].mean() >= POLICY["min_valid"] and b["paint"].mean() >= POLICY["min_paint_support"] for r, b in zip(rows, bodies, strict=True) ) return dict( polygon=p, description=d, legacy=legacy, body_ok=body_ok, legacy_evidence=rows, body_points=body_points[inside], flank_points=xy[:, flank].reshape(-1, 2), bodies=bodies, flanks=sides, body_evidence=[ dict(valid=float(b["valid"].mean()), support=float(b["paint"].mean())) for b in bodies ], ) def relation(a, b): """A localized, measured intersection; no synthetic bridge or shape list.""" da, db = describe(a), describe(b) if da is None or db is None: return None if abs(da["axis"] @ db["axis"]) >= np.cos(np.deg2rad(POLICY["max_join_angle_deg"])): return None shared = a.intersection(b) if shared.area <= POLICY["max_overlap_m2"]: return None for d in [da, db]: cap = d["width"] + POLICY["retained_paint_tolerance_m"] if 2 * cap >= d["length"]: return None extent = (shapely.get_coordinates(shared) - d["origin"]) @ d["axis"] # Broad coincident alternatives cannot masquerade as a local junction. if np.ptp(extent) > 2 * cap: return None joined = a.union(b) return shared if joined.geom_type == "Polygon" and joined.is_valid else None def connection_supported(members, shared): proof = [] for m in members: points = m["body_points"] selected = shapely.intersects_xy(shared, points[:, 0], points[:, 1]) if not selected.any(): return False, proof for evidence in m["bodies"]: row = dict( samples=int(selected.sum()), valid=float(evidence["valid"][selected].mean()), support=float(evidence["paint"][selected].mean()), ) proof.append(row) return all( r["valid"] >= POLICY["min_valid"] and r["support"] >= POLICY["min_paint_support"] for r in proof ), proof def check(members): if len(members) < 2 or not all(m["body_ok"] for m in members): return False, dict(reason="unsupported-member-body") proof = [] for i, m in enumerate(members): others = shapely.union_all([x["polygon"] for j, x in enumerate(members) if j != i]) points = m["flank_points"] exterior = ~shapely.intersects_xy(others, points[:, 0], points[:, 1]) if not exterior.any(): return False, dict(reason="unmeasured-exterior") for original, side in zip(m["legacy_evidence"], m["flanks"], strict=True): row = dict( flank_valid=original["flank_valid"], external_valid=float(side["valid"][exterior].mean()), external_dark=float(side["dark"][exterior].mean()), explained_samples=int((~exterior).sum()), external_samples=int(exterior.sum()), ) proof.append(row) passed = all( r["flank_valid"] >= 0.95 and r["external_valid"] >= 0.95 and r["external_dark"] >= POLICY["min_dark_flank"] for r in proof ) return passed, dict( reason="observed-body-and-exterior" if passed else "unsupported-exterior", views=proof )