Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
100 lines
5.7 KiB
Python
100 lines
5.7 KiB
Python
"""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<max(.03,old.area*.07):return None
|
|
return ids,r['polygons']
|
|
|
|
def validate_removed(old,new,r,readers):
|
|
removed=old.difference(new.buffer(POLICY['retained_paint_tolerance_m']))
|
|
if removed.area<.015:return True,[]
|
|
o,u=r['origin'],r['axis'];xy=shapely.get_coordinates(removed);t=(xy-o)@u;ts=np.arange(t.min(),t.max()+.025,.025);ns=np.arange(-.7,.701,.025);n=np.array([-u[1],u[0]]);points=o+ts[:,None,None]*u+ns[None,:,None]*n;sel=shapely.contains_xy(removed,points[:,:,0],points[:,:,1]);ev=[]
|
|
if sel.sum()<5:return False,[]
|
|
for read in readers:
|
|
con,valid=strip(o,u,ts,ns,read);e=dict(valid=float(valid[sel].mean()),dark=float((con[sel]<15).mean()));ev.append(e)
|
|
return len(ev)==2 and all(e['valid']>=.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]<cut).mean()),flank_valid=float(valid[:,flank].mean()));ev.append(e)
|
|
return len(ev)==2 and all(e['valid']>=.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@u<np.cos(np.deg2rad(POLICY['max_join_angle_deg'])):continue
|
|
# Measure the lateral displacement at the near endpoint, not a distant center.
|
|
from shapely.ops import nearest_points
|
|
near=np.array(nearest_points(q,p)[0].coords[0]);dist=abs((near-o)@n)
|
|
if dist>max(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'])<np.cos(np.deg2rad(POLICY['max_join_angle_deg'])):continue
|
|
from shapely.ops import nearest_points
|
|
near=np.asarray(nearest_points(q,p)[0].coords[0])
|
|
offset=abs((near-candidate['origin'])@n)
|
|
if offset>max(candidate['width'],neighbor['width'])/2+POLICY['max_join_offset_m']:continue
|
|
return False
|
|
return True
|