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

102 lines
5.3 KiB
Python

"""Synthetic invariants for map geometry; no private scan fixtures."""
import unittest
import numpy as np
import shapely
from shapely.geometry import LineString,Polygon,box
from shapely.ops import unary_union
from affine import Affine
from plan_coverage import potts,vectorize
from plan_primitives import frontage_envelope,regularize_paint
class PlanCoverageTests(unittest.TestCase):
def test_unary_cost_can_change_any_class(self):
target=np.tile(np.arange(3,dtype='uint8'),(8,8))
costs=np.full((3,*target.shape),20.)
for k in range(3):costs[k,target==k]=0
for initial in range(3):
result,trace=potts(np.full_like(target,initial),costs,np.zeros((*target.shape,3),np.uint8),weight=0)
np.testing.assert_array_equal(result,target)
self.assertTrue(all(a>b for a,b in zip(trace,trace[1:])))
def test_noise_removed_but_observed_wall_and_passage_remain(self):
h,w=40,70;labels=np.ones((h,w),np.uint8);labels[:,10:25]=2;labels[:,45:60]=2;labels[15:25,18:21]=0
costs=np.full((3,h,w),12.)
for k in range(3):costs[k,labels==k]=0
noisy=labels.copy();noisy[10,13]=0;costs[:,10,13]=[0,1,1]
result,trace=potts(noisy,costs,np.zeros((h,w,3),np.uint8),weight=3)
self.assertEqual(result[10,13],2)
np.testing.assert_array_equal(result[15:25,18:21],0)
np.testing.assert_array_equal(result[:,45:60],2)
self.assertLess(trace[-1],trace[0])
def test_shared_t_junction_and_hole(self):
labels=np.ones((50,60),np.uint8);labels[:30,:20]=2;labels[:30,40:]=2;labels[38:44,25:31]=0
polygons,classes=vectorize(labels,.15,Affine(.1,0,0,0,-.1,5))
self.assertTrue(shapely.coverage_is_valid(polygons))
road=unary_union([p for p,c in zip(polygons,classes) if c==1]);walk=unary_union([p for p,c in zip(polygons,classes) if c==2])
self.assertEqual(road.intersection(walk).area,0)
self.assertGreater(road.boundary.intersection(walk.boundary).length,5)
self.assertFalse(road.contains(box(2.6,.7,3.,1.1)))
def test_invalid_evidence_rejected(self):
a=np.zeros((4,4),np.uint8);costs=np.zeros((3,4,4));costs[0,0,0]=np.nan
with self.assertRaises(ValueError):potts(a,costs,np.zeros((4,4,3),np.uint8))
class PlanObjectTests(unittest.TestCase):
def test_recess_hidden_only_where_front_plane_exists(self):
back=LineString([(0,0),(12,0)]);front=LineString([(2,.6),(10,.6)])
result,_=frontage_envelope([back,front],lambda p,n:1)
union=unary_union([p for p,_ in result])
self.assertLess(union.distance(LineString([(0,0),(1,0)])),.01)
self.assertLess(union.distance(LineString([(11,0),(12,0)])),.01)
self.assertGreater(union.distance(shapely.Point(6,0)),.5)
self.assertLess(union.distance(shapely.Point(6,.6)),.01)
def test_unobserved_large_gap_never_bridged(self):
result,_=frontage_envelope([LineString([(0,0),(4,0)]),LineString([(8,0),(12,0)])],lambda p,n:1)
self.assertEqual(len(result),2)
self.assertGreater(unary_union([p for p,_ in result]).distance(shapely.Point(6,0)),1.9)
def test_wavy_strip_becomes_constant_width(self):
x=np.linspace(0,12,160);jitter=.03*np.sin(x*9)
p=Polygon(np.r_[np.c_[x,.12+jitter],np.c_[x[::-1],-.12+jitter[::-1]]])
q,method=regularize_paint(p)
self.assertEqual(method,'parametric-paint-strip');self.assertTrue(q.is_valid)
self.assertLess(q.hausdorff_distance(p),.15)
self.assertLess(len(q.exterior.coords),12)
def test_unknown_symbol_keeps_hole(self):
p=Polygon([(0,0),(2,0),(1,3)],holes=[[(.7,.5),(1.3,.5),(1,1.5)]])
q,method=regularize_paint(p)
self.assertEqual(method,'observed-symbol');self.assertEqual(len(q.interiors),1)
self.assertLess(q.symmetric_difference(p).area,.01)
class PlanAcceptanceTests(unittest.TestCase):
@staticmethod
def feature(g,cls,**props):
from shapely.geometry import mapping
return dict(id=cls,type='Feature',geometry=mapping(shapely.force_3d(g)),properties=dict(**{'class':cls},authority='observation-only',route_permission='unassigned',**props))
def test_rejects_overlapping_surface_and_wall_inside_road(self):
from plan_acceptance import inspect_plan
a=self.feature(box(0,0,10,10),'carriageway');b=self.feature(box(9,0,12,10),'pedestrian_area');wall=self.feature(LineString([(5,2),(5,8)]),'building_facade')
result=inspect_plan(dict(features=[a,b,wall]));self.assertFalse(result['passed'])
self.assertIn('facade-inside-road',[e['kind'] for e in result['errors']])
self.assertIn('area-overlap-or-unmatched-edge',[e['kind'] for e in result['errors']])
def test_partial_paint_is_explicit_not_fabricated_polygon(self):
from plan_acceptance import inspect_plan
mark=self.feature(LineString([(1,1),(2,1),(2,2)]),'road_marking',geometry_role='partial-paint-footprint')
self.assertTrue(inspect_plan(dict(features=[mark]))['passed'])
del mark['properties']['geometry_role']
self.assertFalse(inspect_plan(dict(features=[mark]))['passed'])
def test_merge_must_not_create_crossing(self):
lines=[LineString([(0,0),(4,0)]),LineString([(5,0),(9,0)]),LineString([(4.5,-2),(4.5,2)])]
result,_=frontage_envelope(lines,lambda p,n:1)
self.assertEqual(len(result),3)
if __name__=='__main__':unittest.main()