"""Metric reconstruction rules independent of map names and coordinates.""" import numpy as np,shapely from shapely.geometry import shape from interval_profiles import rectangle,strip,runs from beta_profiles import describe VERSION='stroke-window-family-v3' POLICY=dict(min_continuous_section_m=1.,min_valid=.98,min_paint_support=.93,min_contrast=22.,min_dark_flank=.85,max_overlap_m2=.005,removed_dark_fraction=.90,retained_paint_tolerance_m=.06,profile_core_fraction=.35,profile_core_min_half_m=.125,reject_unresolved_seam=True,max_join_gap_m=.20,max_join_angle_deg=3.,max_join_offset_m=.06,single_edit_per_family=True,primary_and_secondary_required=True,manual_rois=[],point_cloud_input=False) def missing_parts(p,existing): d=describe(p) if d is None:return [] o,u=d['origin'],d['axis'];L=d['length'];occupied=p.intersection(existing.buffer(.10));intervals=[] for q in getattr(occupied,'geoms',[occupied]): if q.is_empty:continue coords=shapely.get_coordinates(q);t=(coords-o)@u;intervals.append((t.min(),t.max())) merged=[] for a,b in sorted(intervals): if merged and a<=merged[-1][1]:merged[-1][1]=max(b,merged[-1][1]) else:merged.append([a,b]) cursor=-L/2;out=[] for a,b in merged+[[L/2,L/2]]: if a-cursor>=POLICY['min_continuous_section_m']:out.append(rectangle(o,u,cursor,a,d['width'])) cursor=max(cursor,b) return out def plan_replacement(r,current): new=shapely.union_all(r['polygons']);domain=r['domain'];ids=[] for i,f in current.items(): if f['properties'].get('class')!='road_marking':continue p=shapely.force_2d(shape(f['geometry'])) if p.intersection(domain).area<.005:continue if f['properties'].get('subtype')!='linear_marking':return None if p.difference(domain).area>.005:return None ids.append(i) if not ids:return None old=shapely.union_all([shapely.force_2d(shape(current[i]['geometry']))for i in ids]) if old.symmetric_difference(new).area=.98 and e['dark']>=.90 for e in ev),ev def check_strip(p,readers): d=describe(p) if d is None:return False,[] ts=np.arange(-d['length']/2+.04,d['length']/2-.04,.025);ns=np.arange(-.7,.701,.025);core=abs(ns)<=max(.025,min(.04,d['width']*.2));flank=(abs(ns)>=d['width']/2+.08)&(abs(ns)<=d['width']/2+.14);ev=[] for read in readers: con,valid=strip(d['origin'],d['axis'],ts,ns,read);signal=np.median(con[:,core],axis=1);cut=max(15.,.35*float(np.median(signal)));e=dict(valid=float(valid[:,core].mean()),support=float((signal>cut).mean()),contrast=float(np.median(signal)),dark_flank=float((con[:,flank]=.98 and e['support']>=.93 and e['contrast']>=22 and e['dark_flank']>=.85 and e['flank_valid']>=.95 for e in ev),ev def join_continuous(p,current): """Merge a newly observed extension with aligned neighboring linear paint.""" d=describe(p) if d is None:return [],p o,u=d['origin'],d['axis'];n=np.array([-u[1],u[0]]);ids=[];pieces=[p];widths=[d['width']];axes=[u*d['length']];weights=[d['length']] for i,f in current.items(): if f['properties'].get('subtype')!='linear_marking':continue q=shapely.force_2d(shape(f['geometry']));v=describe(q) if v is None or q.distance(p)>POLICY['max_join_gap_m']:continue axis=v['axis']*np.sign(v['axis']@u) if axis@umax(d['width'],v['width'])/2+POLICY['max_join_offset_m']:continue if abs(d['width']-v['width'])>.08:continue ids.append(i);pieces.append(q);widths.append(v['width']);axes.append(axis*v['length']);weights.append(v['length']) if not ids:return [],p axis=np.sum(axes,axis=0);axis/=np.linalg.norm(axis);normal=np.array([-axis[1],axis[0]]) centers=np.array([list(q.centroid.coords)[0]for q in pieces]);origin=np.average(centers,axis=0,weights=weights);xy=np.concatenate([shapely.get_coordinates(q)for q in pieces]);t=(xy-origin)@axis;width=float(np.average(widths,weights=weights)) return ids,rectangle(origin,axis,float(t.min()),float(t.max()),width) def isolated_addition(p,current): """Reject unresolved end-to-end seams, not a distinct parallel paint track.""" candidate=describe(p) if candidate is None:return False for f in current.values(): if f['properties'].get('subtype')!='linear_marking':continue q=shapely.force_2d(shape(f['geometry'])) if p.distance(q)>POLICY['max_join_gap_m']:continue neighbor=describe(q) if neighbor is None:return False u=candidate['axis'];n=np.array([-u[1],u[0]]) # No merge is attempted across a direction change. An actual intersection # is rejected by the transaction's unchanged overlap gate. if abs(u@neighbor['axis'])max(candidate['width'],neighbor['width'])/2+POLICY['max_join_offset_m']:continue return False return True