"""Small synthetic geometry: no scene names, memorized turns or manual paths.""" import importlib.util from pathlib import Path import sys import numpy as np import pytest pytest.importorskip('scipy') ADAPTER=Path(__file__).parents[1]/'simulation/ai-polygon' sys.path.insert(0,str(ADAPTER)) from scene_route_field import ground_envelope, route_graph from k1link.simulation.ai_polygon.route_graph import build_route def street(*,car=False,gap=False): x,y=np.meshgrid(np.arange(0,24.01,.25),np.arange(-4,4.01,.25),indexing='ij') vertices=np.column_stack([x.ravel(),y.ravel(),np.zeros(x.size)]) n=y.shape[1] faces=[] for i in range(x.shape[0]-1): for j in range(n-1): a=i*n+j;b=a+n if (car and 9<=x[i,j]<13 and -1<=y[i,j]<1) or (gap and 9<=x[i,j]<13): continue faces.extend([[a,b,a+1],[a+1,b,b+1]]) if car: roof=vertices.copy();roof[:,2]=1.5 rows=np.argwhere((x>=9)&(x<13)&(y>=-1)&(y<1)) roof_faces=[] for i,j in rows: a=i*n+j;b=a+n roof_faces.extend([[a,b,a+1],[a+1,b,b+1]]) faces+= (np.asarray(roof_faces)+len(vertices)).tolist() vertices=np.concatenate([vertices,roof]) return vertices,np.array(faces) def test_small_enclosed_occlusion_does_not_sculpt_global_directions(): base=ground_envelope(*street(),[1,0,0]) occupied=ground_envelope(*street(car=True),[1,0,0]) assert np.array_equal(base[2],occupied[2]) assert not base[3].any() and occupied[3].any() source=dict(provider='synthetic',provider_sha256='a'*64,collider_sha256='b'*64) positions=[[1.,0.,0.],[22.,0.,0.]] def route(field): graph=route_graph(*field,positions,source,'c'*64) return build_route(dict(routing=graph,mission={'route_xy':[[22.,0.]]},robot={'geometry':{'length_m':1.,'width_m':1.}},sha256='d'*64))['legs'] assert route(base)==route(occupied) def test_open_gap_is_not_filled_to_make_a_successful_route(): field=ground_envelope(*street(gap=True),[1,0,0]) origin,height,envelope,unknown=field far=np.floor((np.array([20,0])-origin)/.5).astype(int) assert not envelope[tuple(far)] and not unknown[tuple(far)] with pytest.raises(ValueError,match='Endpoint'): route_graph(*field,[[1.,0.,0.],[22.,0.,0.]], dict(provider='synthetic',provider_sha256='a'*64,collider_sha256='b'*64),'c'*64) def test_goal_on_another_height_layer_does_not_attach_to_street(): field=ground_envelope(*street(),[1,0,0]) with pytest.raises(ValueError,match='Endpoint'): route_graph(*field,[[1.,0.,0.],[22.,0.,2.]], dict(provider='synthetic',provider_sha256='a'*64,collider_sha256='b'*64),'c'*64) def test_sufficient_clearance_does_not_attract_route_to_widest_surface(): # A long open street with A/B on the same side. Geometry has no lane labels; # extra width farther away must not bend this otherwise direct intention. origin=np.array([0.,0.]);height=np.zeros((125,65)) envelope=np.ones_like(height,dtype=bool);envelope[[0,-1],:]=False;envelope[:,[0,-1]]=False positions=[[2.25,2.25,0.],[58.25,2.25,0.]] graph=route_graph(origin,height,envelope,np.zeros_like(envelope),positions, dict(provider='synthetic',provider_sha256='a'*64,collider_sha256='b'*64),'c'*64, body_extent_m=1.2) route=build_route(dict(routing=graph,mission={'route_xy':[positions[-1][:2]]}, robot={'geometry':{'length_m':1.2,'width_m':.9}},sha256='d'*64))['legs'][0] assert route['status']=='ready' assert route['length_m']==pytest.approx(56.) assert all(segment['end_xy'][1]==2.25 for segment in route['segments']) # There is still a real penalty near an envelope boundary; equal straight # edge lengths in already generous space have the same price. nodes={n['id']:n for n in graph['map']['nodes']} for edge in graph['map']['edges']: length=np.linalg.norm(np.array(nodes[edge['start']]['xy'])-nodes[edge['end']]['xy']) assert edge['cost_m'] >= length if edge['half_width_m']>=1.1: assert edge['cost_m']==pytest.approx(length)