"""One observed leg at a periodic pattern end; never invent an unseen zigzag.""" import numpy as np from shapely.geometry import LineString VERSION='observed-periodic-zigzag-end-v2' POLICY=dict(min_existing_legs=5,max_leg_residual_m=.15,min_support=.93,min_recall=.85,min_valid=.97,max_negative_paint=.08,max_new_legs_per_end=1) def proposal(vertices,width,reverse=False): points=np.asarray(vertices,float) if reverse:points=points[::-1] if len(points)POLICY['max_leg_residual_m']:return None if any(not .7<=np.linalg.norm(v)<=4.5 for v in means):return None cosine=float(means[0]@means[1]/np.prod([np.linalg.norm(v)for v in means])) if not -.65<=cosine<=.65:return None extended=np.vstack([points,points[-1]+means[1]]) leg=LineString(extended[-2:]).buffer(width/2,cap_style=2,join_style=2) if reverse:extended=extended[::-1] line=LineString(extended) if not line.is_simple:return None return extended.tolist(),line.buffer(width/2,cap_style=2,join_style=2),leg def supported(evidence): return len(evidence)>=2 and all(e['paint_coverage']==1. and e['paint_valid']>=POLICY['min_valid'] and e['observed_support']>=POLICY['min_support'] and e['recall']>=POLICY['min_recall'] and e['negative_paint']<=POLICY['max_negative_paint']for e in evidence) def shortened(vertices,width,reverse,fraction): """Preserve the periodic direction; shorten only the new terminal segment.""" if not .20 <= fraction <= 1.:return None full=proposal(vertices,width,reverse) if full is None:return None points=np.asarray(full[0]);oriented=points[::-1].copy()if reverse else points.copy() origin=oriented[-2];vector=oriented[-1]-origin if np.linalg.norm(vector)*fraction < .45:return None oriented[-1]=origin+fraction*vector leg=LineString(oriented[-2:]).buffer(width/2,cap_style=2,join_style=2) points=oriented[::-1]if reverse else oriented return points.tolist(),LineString(points).buffer(width/2,cap_style=2,join_style=2),leg def quiet_end(evidence): # A worn/occluded continuation is not a measured endpoint. return len(evidence)==2 and all(e['paint_coverage']==1. and e['paint_valid']>=.97 and e['observed_support']<.15 for e in evidence) def fit_end(vertices,width,reverse,measure): full=proposal(vertices,width,reverse) if full is None:return None ev,names=measure(full[2],full[1]) if supported(ev):return dict(points=full[0],polygon=full[1],leg=full[2],fraction=1.,evidence=ev,names=names,exterior=[]) points=np.asarray(full[0]);points=points[::-1]if reverse else points origin=points[-2];vector=points[-1]-origin;direction=vector/np.linalg.norm(vector) # Longest observed prefix wins. Fixed 5 cm sampling depends on metric # length only, never on scene identity or a manually selected endpoint. for length in np.arange(np.linalg.norm(vector)-.05,.44,-.05): fraction=length/np.linalg.norm(vector);p=shortened(vertices,width,reverse,fraction) if p is None:continue ev,names=measure(p[2],p[1]) if not supported(ev):continue end=origin+direction*length exterior=LineString([end+direction*.15,end+direction*.40]).buffer(width/2,cap_style=2) stop,extra=measure(exterior,p[1]) if quiet_end(stop):return dict(points=p[0],polygon=p[1],leg=p[2],fraction=fraction,evidence=ev,names=sorted(set(names+extra)),exterior=stop) return None