Files
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

103 lines
5.6 KiB
Python

"""Joint line / circular-fillet geometry with exact shared junctions.
No scene coordinates or class masks are embedded in this module. Fillets are
constructed from the same line intersections used by both adjacent straights;
there is no independently movable arc endpoint. Image evidence is an adapter.
"""
import numpy as np
def unit(v):
v=np.asarray(v,float)
return v/max(float(np.linalg.norm(v)),1e-12)
def line_intersection(origin_a,axis_a,origin_b,axis_b):
a=np.column_stack([axis_a,-np.asarray(axis_b)])
if abs(np.linalg.det(a))<1e-5:return None
t=np.linalg.solve(a,np.asarray(origin_b)-origin_a)
return np.asarray(origin_a)+t[0]*np.asarray(axis_a)
def fillet(previous,vertex,following,radius):
p,x,q=map(lambda a:np.asarray(a,float),(previous,vertex,following))
u=unit(p-x);v=unit(q-x);half=np.arccos(np.clip(u@v,-1,1))/2
if radius<=1e-5 or half<.03 or half>np.pi/2-.01:
return dict(kind='corner',start=x,end=x,vertex=x,radius=0.)
reach=float(radius/np.tan(half))
start=x+reach*u;end=x+reach*v;center=x+unit(u+v)*radius/np.sin(half)
a=np.arctan2(*(start-center)[::-1]);b=np.arctan2(*(end-center)[::-1]);sweep=(b-a+np.pi)%(2*np.pi)-np.pi
return dict(kind='arc',start=start,end=end,center=center,radius=float(radius),angle=a,sweep=sweep,vertex=x,reach=reach)
def build_chain(vertices,radii):
vertices=np.asarray(vertices,float);radii=np.asarray(radii,float)
if len(vertices)<2 or len(radii)!=max(0,len(vertices)-2):raise ValueError('One radius per internal vertex is required')
if not np.isfinite(vertices).all() or not np.isfinite(radii).all() or (radii<0).any():raise ValueError('Finite vertices and nonnegative radii required')
turns=[fillet(*vertices[i-1:i+2],radii[i-1]) for i in range(1,len(vertices)-1)]
# Overlapping trims are a rejected hypothesis, never an implicit connector.
for i in range(len(vertices)-1):
start=vertices[0] if i==0 else turns[i-1]['end']
end=vertices[-1] if i==len(vertices)-2 else turns[i]['start']
if (end-start)@(vertices[i+1]-vertices[i]) < -1e-7:raise ValueError('Fillets overlap on a straight approach')
primitives=[]
for i in range(len(vertices)-1):
a=vertices[0] if i==0 else turns[i-1]['end'];b=vertices[-1] if i==len(vertices)-2 else turns[i]['start']
primitives.append(dict(kind='line',start=a,end=b))
if i<len(turns) and turns[i]['kind']=='arc':primitives.append(turns[i])
return primitives
def sample_primitives(primitives,step=.15,fixed=None):
points=[];normals=[];ids=[]
for i,m in enumerate(primitives):
if m['kind']=='line':
length=np.linalg.norm(m['end']-m['start']);count=fixed or max(2,int(np.ceil(length/step))+1)
p=np.linspace(m['start'],m['end'],count);u=unit(m['end']-m['start']);n=np.tile([-u[1],u[0]],(len(p),1))
else:
count=fixed or max(3,int(np.ceil(abs(m['sweep'])*m['radius']/step))+1);t=m['angle']+np.linspace(0,m['sweep'],count)
p=m['center']+m['radius']*np.c_[np.cos(t),np.sin(t)];n=-np.c_[np.cos(t),np.sin(t)]*np.sign(m['sweep'])
p[0]=m['start'];p[-1]=m['end']
if points:p=p[1:];n=n[1:]
points.extend(p);normals.extend(n);ids.extend([i]*len(p))
return np.asarray(points),np.asarray(normals),np.asarray(ids)
def bridge_short_seam(a,b,max_gap=.8,trim=1.5,step=.15):
"""Reconcile an internal sub-metre join; caller verifies same-edge identity.
Input chains are directed a -> b. Keep distant anchors fixed and rebuild
only the terminal neighbourhood. Opposing or backward chains are rejected.
"""
from scipy.interpolate import CubicHermiteSpline
from shapely.geometry import LineString
from plan_continuity import resample,distances
a,b=resample(np.asarray(a,float),step),resample(np.asarray(b,float),step)
gap=float(np.linalg.norm(a[-1,:2]-b[0,:2]))
if not 1e-5<gap<=max_gap or min(distances(a)[-1],distances(b)[-1])<trim+1:return None
ia=int(np.searchsorted(distances(a),distances(a)[-1]-trim));ib=int(np.searchsorted(distances(b),trim))
p,q=a[ia],b[ib];ta=unit(a[ia,:2]-a[max(0,ia-3),:2]);tb=unit(b[min(len(b)-1,ib+3),:2]-b[ib,:2]);delta=q[:2]-p[:2];length=np.linalg.norm(delta)
if ta@tb<.25 or ta@unit(delta)<.45 or tb@unit(delta)<.45:return None
derivatives=np.stack([ta,tb])
if a.shape[1]==3:derivatives=np.c_[derivatives,np.full(2,(q[2]-p[2])/length)]
spline=CubicHermiteSpline([0,length],np.stack([p,q]),derivatives)
curve=spline(np.linspace(0,length,max(4,int(np.ceil(length/step))+1)))
joined=np.vstack([a[:ia],curve,b[ib+1:]])
if not LineString(joined[:,:2]).is_simple or LineString(curve[:,:2]).length>1.3*length:return None
return joined,curve,gap
def competing_fragment(fragment,reference,max_length=8.,max_distance=3.):
"""Nominate a short detached parallel alternative for a review layer.
This is an ambiguity test, not a semantic deletion rule. Keep nominated
geometry available: a real narrow island can have the same 2D signature.
"""
from shapely.geometry import LineString,Point
from plan_continuity import resample
p=resample(np.asarray(fragment,float),.25)[:,:2];line=LineString(p);ref=LineString(np.asarray(reference)[:,:2])
if line.is_ring or not 0<line.length<max_length or ref.length<12:return False
if max(ref.distance(Point(q))for q in p)>max_distance:return False
projected=np.array([ref.project(Point(q))for q in p]);span=abs(projected[-1]-projected[0])
return bool(.8*line.length<span<1.1*line.length and (np.diff(projected)>=-.05).all()or .8*line.length<span<1.1*line.length and (np.diff(projected)<=.05).all())