"""OpenCV proposes an observed short return at any existing zigzag endpoint. The old pattern is immutable. Two Gaussian views must support the entire new straight stroke and observed blank ground beyond its endpoint. No curb snap, location index, or inferred closure participates in this pass. """ import cv2,numpy as np from shapely.geometry import LineString,Point from paint_channels import paint_mask from registration import grids,CELL from zigzag_extension import supported,quiet_end from zigzag_mosaics import mosaic_views VERSION='observed-zigzag-terminal-return-v2' POLICY=dict(max_length_m=2.5,min_length_m=.45,max_origin_offset_m=.18,min_turn_deg=25.,max_turn_deg=155.,max_direction_hypotheses=6,extension_sample_m=.05) def directions(paint,known,lo,origin,incoming): cv2.setRNGSeed(2026) lines=cv2.HoughLinesP((paint&known).astype('uint8')*255,1,np.pi/360,threshold=12,minLineLength=.35/CELL,maxLineGap=.12/CELL) found=[] for line in []if lines is None else lines[:,0]: a,b=line.reshape(2,2)*CELL+lo if np.linalg.norm(a-origin)>np.linalg.norm(b-origin):a,b=b,a v=b-a;length=np.linalg.norm(v) if length<.35:continue v/=length;station=float((origin-a)@v);offset=np.linalg.norm(origin-(a+station*v)) if offset>POLICY['max_origin_offset_m'] or station<-.30 or station>length+.05:continue angle=np.rad2deg(np.arccos(np.clip(v@incoming,-1,1))) if not POLICY['min_turn_deg']<=angle<=POLICY['max_turn_deg']:continue end=float((b-origin)@v) if end<.35:continue if any(abs(v@d[0])>np.cos(np.deg2rad(5))for d in found):continue found.append((v,min(end+.20,POLICY['max_length_m']),length)) return sorted(found,key=lambda v:-v[2])[:POLICY['max_direction_hypotheses']] def fit_terminal(vertices,width,reverse,views,audit=None): audit={}if audit is None else audit audit.update(proposal_views=[],directions=0,geometry_rejections=0,support_rejections=0,endpoint_rejections=0) points=np.asarray(vertices,float) if reverse:points=points[::-1] if len(points)<6 or not .06<=width<=.22:return None origin=points[-1];incoming=origin-points[-2];incoming/=np.linalg.norm(incoming) old=LineString(points).buffer(width/2,cap_style=2,join_style=2) grid=grids(Point(origin).buffer(1.6),views,surface_classes=(1,)) candidates=[] for rank,v in enumerate(grid['views']): paint=paint_mask(v['rgb'],channel='yellow',cell=CELL,observed=v['observed'],calibration=v['allowed']) proposed=directions(paint,v['allowed']&v['observed'],grid['lo'],origin,incoming) audit['proposal_views'].append(dict(rank=rank,directions=len(proposed))) for proposal in proposed: # A poorer primary render must not prevent a second image from # proposing a complete stroke. Both images still verify admission. same=next((i for i,p in enumerate(candidates)if p[0]@proposal[0]>np.cos(np.deg2rad(5))),None) if same is None:candidates.append(proposal) elif proposal[1]>candidates[same][1]:candidates[same]=proposal candidates=sorted(candidates,key=lambda p:-p[2])[:POLICY['max_direction_hypotheses']] audit['directions']=len(candidates) for direction,maximum,_ in candidates: for length in np.arange(maximum,.44,-.05): end=origin+direction*length leg=LineString([origin,end]).buffer(width/2,cap_style=2,join_style=2) if leg.difference(old.buffer(.12)).area/leg.area<.70:audit['geometry_rejections']+=1;continue extended=np.vstack([points,end]);q=LineString(extended).buffer(width/2,cap_style=2,join_style=2) if not LineString(extended).is_simple:continue evidence,names=mosaic_views(leg,views,negative_context=q) if not supported(evidence):audit['support_rejections']+=1;continue probe=LineString([end+direction*.15,end+direction*.40]).buffer(width/2,cap_style=2) exterior,extra=mosaic_views(probe,views,negative_context=q) if not quiet_end(exterior):audit['endpoint_rejections']+=1;continue if reverse:extended=extended[::-1] return dict(points=extended.tolist(),polygon=q,leg=leg,evidence=evidence,names=sorted(set(names+extra)),exterior=exterior,fraction=length/maximum) return None