Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
103 lines
4.6 KiB
Python
103 lines
4.6 KiB
Python
"""Topology, provenance and fit bounds for automatic infrastructure consolidation."""
|
|
import copy
|
|
import sys
|
|
from pathlib import Path
|
|
import numpy as np
|
|
import pytest
|
|
pytest.importorskip('shapely')
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0,str(ROOT/'simulation/urban-mapping/vector'))
|
|
from refine_contours import consolidate
|
|
from contour_geometry import smooth, sample
|
|
from shapely.geometry import LineString
|
|
|
|
|
|
def feature(name, xy, cls='curb', score=.6, z=3, source=None):
|
|
return dict(type='Feature',id=name,geometry=dict(type='LineString',coordinates=[[x,y,z] for x,y in xy]),
|
|
properties={'class':cls,'source_class':source or ('curb' if cls=='curb' else 'lane'),
|
|
'geometry_role':'boundary' if cls=='curb' else 'paint-axis','confidence':score,
|
|
'authority':'observation-only','route_permission':'unassigned','review_status':'unreviewed'})
|
|
|
|
|
|
class Evidence:
|
|
def __init__(self, supported=True):self.supported=supported
|
|
def bridge(self,p,key):return self.supported
|
|
def support(self,p,key):return np.ones(len(p),bool),np.full(len(p),self.supported,bool)
|
|
|
|
|
|
def run(features,evidence=None):return consolidate(dict(features=features),evidence)
|
|
|
|
|
|
def test_tile_seams_join_and_original_is_immutable():
|
|
f=[feature('a',[(0,0),(4,0)]),feature('b',[(4,0),(8,.01)])]
|
|
original=copy.deepcopy(f);result,report=run(f)
|
|
assert f==original
|
|
assert len(result['features'])==1 and len(report['joins'])==1
|
|
assert result['features'][0]['properties']['consolidation']['source_ids']==['a','b']
|
|
assert result['features'][0]['geometry']['coordinates'][0]==[0.,0.,3.]
|
|
assert result['features'][0]['properties']['route_permission']=='unassigned'
|
|
|
|
|
|
def test_gap_requires_support_and_is_recorded_as_inference():
|
|
f=[feature('a',[(0,0),(4,0)]),feature('b',[(4.6,0),(8,0)])]
|
|
assert len(run(f)[0]['features'])==2
|
|
assert len(run(f,Evidence(False))[0]['features'])==2
|
|
result,report=run(f,Evidence())
|
|
assert len(result['features'])==1 and report['inferred_gap_m']==.6
|
|
assert result['features'][0]['properties']['consolidation']['inferred_gaps'][0]['evidence']=='image-and-ground'
|
|
|
|
|
|
def test_parallel_curbs_different_heights_and_classes_stay_separate():
|
|
f=[feature('a',[(0,0),(4,0)]),feature('b',[(0,.5),(4,.5)]),
|
|
feature('c',[(4.05,0),(8,0)],z=4),feature('d',[(4.05,0),(8,0)],cls='road_marking')]
|
|
assert len(run(f,Evidence())[0]['features'])==4
|
|
|
|
|
|
def test_branching_junction_is_not_shortcut():
|
|
f=[feature('a',[(0,0),(4,0)]),feature('b',[(4,0),(8,0)]),feature('c',[(4,0),(4,4)])]
|
|
result,report=run(f,Evidence())
|
|
assert len(result['features'])==3 and not report['joins']
|
|
|
|
|
|
def test_dashes_and_stopline_have_different_semantics():
|
|
f=[feature('a',[(0,0),(3,0)],cls='road_marking'),feature('b',[(4,0),(7,0)],cls='road_marking'),
|
|
feature('c',[(3,0),(3,1)],cls='road_marking',source='stopline')]
|
|
assert len(run(f,Evidence())[0]['features'])==3
|
|
|
|
|
|
def test_duplicate_removed_but_crossing_line_retained():
|
|
f=[feature('a',[(0,0),(8,0)],score=.8),feature('b',[(0,.04),(8,.04)],score=.4),
|
|
feature('c',[(4,-2),(4,2)])]
|
|
result,report=run(f)
|
|
assert len(result['features'])==2 and report['duplicates']==['b']
|
|
|
|
|
|
def test_short_noise_needs_negative_evidence_and_isolation():
|
|
f=[feature('a',[(0,0),(.7,0)],score=.4)]
|
|
assert len(run(f)[0]['features'])==1
|
|
assert len(run(f,Evidence())[0]['features'])==1
|
|
assert len(run(f,Evidence(False))[0]['features'])==0
|
|
|
|
|
|
def test_spline_is_bounded_preserves_corners_heights_and_endpoints():
|
|
p=np.array([[x,.06*np.sin(x*5),3+.02*x] for x in np.linspace(0,8,60)])
|
|
fitted,curves=smooth(p)
|
|
assert np.max(np.abs(fitted[[0,-1]]-p[[0,-1]]))==0
|
|
assert LineString(fitted[:,:2]).hausdorff_distance(LineString(p[:,:2]))<=.12
|
|
assert any(c['degree']==3 for c in curves)
|
|
corner=np.array([[0,0,3],[1,0,3],[2,0,3],[2,1,3],[2,2,3]],float)
|
|
fitted,_=smooth(corner)
|
|
assert any(np.array_equal(q,[2,0,3]) for q in fitted)
|
|
|
|
|
|
def test_bridge_cannot_cross_another_boundary_or_known_opening():
|
|
f=[feature('a',[(0,0),(4,0)]),feature('b',[(5,0),(8,0)]),feature('c',[(4.5,-2),(4.5,2)])]
|
|
assert len(run(f,Evidence())[0]['features'])==3
|
|
opening=dict(type='Feature',id='cut',properties={'class':'curb_cut'},geometry=dict(type='Polygon',coordinates=[[[4.2,-1,3],[4.8,-1,3],[4.8,1,3],[4.2,1,3],[4.2,-1,3]]]))
|
|
assert len(run(f[:2]+[opening],Evidence())[0]['features'])==3
|
|
|
|
|
|
def test_order_independent_geometry_and_ids():
|
|
f=[feature('a',[(0,0),(4,0)]),feature('b',[(4,0),(8,0)]),feature('c',[(0,.03),(4,.03)],score=.3)]
|
|
assert run(f)[0]==run(list(reversed(f)))[0]
|