Files
NODEDC_MISSION_CORE/simulation/urban-plan-research/driveway_refinement.py
T
DCCONSTRUCTIONS 69dc566665 feat(research): preserve urban geometry pipelines and qualification suites
Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
2026-10-29 09:10:00 +03:00

56 lines
4.5 KiB
Python

"""Paired straight access boundaries from LSD anchors in a reviewed region."""
import cv2,numpy as np
from shapely.geometry import LineString,Point
from shapely.ops import substring
from plan_junctions import unit,line_intersection
import qualify_junctions as q
def fit_access(line,center,e,bounds):
station=line.project(Point(center));origin=np.asarray(line.interpolate(station).coords[0]);segment=np.array(substring(line,max(0,station-3),min(line.length,station+3)).coords)
u=unit(segment[-1,:2]-segment[0,:2]);v=np.array([-u[1],u[0]])
if np.mean(e.occupancy(origin[None,:2]+v)-e.occupancy(origin[None,:2]-v))>0:v=-v
raw=cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD).detect(cv2.cvtColor(e.image,cv2.COLOR_RGB2GRAY))[0]
anchors=[];transverse=[]
for r in ([]if raw is None else raw[:,0,:]):
ends=q.world(r.reshape(2,2),e.g);mid=ends.mean(0);length=np.linalg.norm(ends[1]-ends[0]);direction=unit(ends[1]-ends[0]);depth=float((mid-origin[:2])@v)
if length<.8 or not(bounds[0]<mid[0]<bounds[2]and bounds[1]<mid[1]<bounds[3]):continue
if abs(direction@v)>.97 and .5<depth<6.5:
if direction@v<0:direction=-direction
anchors.append(dict(ends=ends,direction=direction,length=length,mid=mid))
elif abs(direction@u)>.97 and depth>3:transverse.append(dict(ends=ends,length=length,mid=mid,depth=depth))
choices=[]
for i,a in enumerate(anchors):
for b in anchors[i+1:]:
if a['direction']@b['direction']<.99:continue
direction=unit(a['direction']*a['length']+b['direction']*b['length']);normal=np.array([-direction[1],direction[0]])
if normal@u<0:normal=-normal
origins=sorted([a['mid'],b['mid']],key=lambda p:p@normal);width=float((origins[1]-origins[0])@normal)
if not 2<width<6.5:continue
starts=[line_intersection(origin[:2],u,p,direction)for p in origins]
t=np.linspace(.7,4.2,36);pp=[p+t[:,None]*direction for p in starts];centerline=(pp[0]+pp[1])/2
inside=np.concatenate([centerline+k*width*normal for k in(-.28,0,.28)]);outside=np.concatenate([pp[0]-.4*normal,pp[1]+.4*normal])
region=float(np.mean(np.clip(q.sample(e.texture,q.project(inside,e.g)),0,4))-np.mean(np.clip(q.sample(e.texture,q.project(outside,e.g)),0,4)))
objective=region-.15*(a['length']+b['length']);choices.append((objective,a,b,direction,normal,width,starts))
if not choices:raise ValueError('No supported parallel access sides in the reviewed region')
objective,a,b,direction,normal,width,starts=min(choices,key=lambda x:x[0]);anchor_depth=max(float((p-origin[:2])@direction)for anchor in(a,b)for p in anchor['ends'])
midpoint=np.mean(starts,axis=0);caps=[x for x in transverse if x['depth']>anchor_depth+.4 and x['length']>.45*width and abs((x['mid']-midpoint)@normal)<width*.4]
# The first supported transverse gate/far frontier bounds the visible strip.
# The boundary ends remain open; no cross-strip cap is added to the map.
extent=min(float((x['mid']-midpoint)@direction)for x in caps)if caps else anchor_depth
corners=[];ends=[]
for start in starts:
c=np.asarray(line.interpolate(line.project(Point(start))).coords[0]);end=c.copy();end[:2]+=direction*extent;corners.append(c);ends.append(end)
model=dict(method='opencv-lsd-paired-access-anchors',width_m=width,extent_m=extent,objective=objective,station_m=station,anchor_segments=[x['ends'].tolist()for x in(a,b)],candidate_pairs=len(choices),direction=direction.tolist(),extent_evidence='transverse-frontier'if caps else'last-side-anchor')
return np.array(corners),np.array(ends),model
def split_access(points,corners,ends):
"""Remove a known opening and continue its two sides with unjoined ends."""
line=LineString(points);stations=np.array([line.project(Point(c[:2]))for c in corners]);order=np.argsort(stations);stations=stations[order];corners=np.asarray(corners)[order];ends=np.asarray(ends)[order]
if not 0<stations[0]<stations[1]<line.length:raise ValueError('Opening must be internal')
a=np.array(substring(line,0,stations[0]).coords);b=np.array(substring(line,stations[1],line.length).coords)
aa=np.vstack([a,ends[0]]);bb=np.vstack([ends[1],b])
if not LineString(aa).is_simple or not LineString(bb).is_simple or LineString(aa).intersects(LineString(bb)):raise ValueError('Access boundaries intersect')
return aa,bb,dict(source_interval_m=stations.tolist(),removed_frontier=list(substring(line,*stations).coords),status='known-opening-from-owner-review',open_ends=ends.tolist())