import unittest import numpy as np from plan_junctions import build_chain,sample_primitives,bridge_short_seam,competing_fragment class JointGeometryTests(unittest.TestCase): def test_shared_tangent_endpoints(self): m=build_chain([[-10,0],[0,0],[0,10]],[3]) self.assertEqual([x['kind']for x in m],['line','arc','line']) np.testing.assert_allclose(m[0]['end'],m[1]['start'],atol=1e-12) np.testing.assert_allclose(m[1]['end'],m[2]['start'],atol=1e-12) np.testing.assert_allclose(m[1]['center'],[-3,3],atol=1e-12) np.testing.assert_allclose(m[0]['end'],[-3,0],atol=1e-12) np.testing.assert_allclose(m[2]['start'],[0,3],atol=1e-12) def test_left_normal_consistent_through_turn(self): m=build_chain([[-10,0],[0,0],[0,10]],[3]);p,n,_=sample_primitives(m,.05) t=np.gradient(p,axis=0);t/=np.linalg.norm(t,axis=1)[:,None] cross=t[:,0]*n[:,1]-t[:,1]*n[:,0] self.assertGreater(cross.min(),.999) def test_right_normal_consistent_through_turn(self): m=build_chain([[-10,0],[0,0],[0,-10]],[3]);p,n,_=sample_primitives(m,.05) t=np.gradient(p,axis=0);t/=np.linalg.norm(t,axis=1)[:,None] self.assertGreater((t[:,0]*n[:,1]-t[:,1]*n[:,0]).min(),.999) def test_zero_radius_is_exact_corner(self): m=build_chain([[-5,0],[0,0],[0,5]],[0]);self.assertEqual(len(m),2) np.testing.assert_array_equal(m[0]['end'],[0,0]);np.testing.assert_array_equal(m[1]['start'],[0,0]) def test_turn_cannot_consume_approach(self): with self.assertRaises(ValueError):build_chain([[-2,0],[0,0],[0,2]],[3]) def test_consecutive_turns_cannot_overlap(self): with self.assertRaises(ValueError):build_chain([[-5,0],[0,0],[0,2],[5,2]],[2,2]) def test_parking_pocket_has_two_local_turns(self): m=build_chain([[-10,0],[0,0],[0,4],[10,4]],[1,1]);p,_,_=sample_primitives(m) self.assertEqual(sum(x['kind']=='arc'for x in m),2) np.testing.assert_allclose(p[[0,-1]],[[-10,0],[10,4]],atol=1e-12) for a,b in zip(m[:-1],m[1:]):np.testing.assert_allclose(a['end'],b['start'],atol=1e-12) def test_rotation_and_translation_invariance(self): v=np.array([[-10,0],[0,0],[0,10]]);a=.6;R=np.array([[np.cos(a),-np.sin(a)],[np.sin(a),np.cos(a)]]);o=np.array([600.,-350.]) p,_,_=sample_primitives(build_chain(v,[3]),fixed=21);q,_,_=sample_primitives(build_chain(v@R.T+o,[3]),fixed=21) np.testing.assert_allclose(q,p@R.T+o,atol=1e-8) def test_negative_radius_rejected(self): with self.assertRaises(ValueError):build_chain([[-5,0],[0,0],[0,5]],[-1]) def test_finite_geometry_required(self): with self.assertRaises(ValueError):build_chain([[-5,0],[np.nan,0],[0,5]],[1]) def test_short_seam_keeps_external_anchors(self): a=np.array([[-8.,0.],[0.,0.]]);b=np.array([[0.,-.5],[2.,-1.],[8.,-1.]]) result=bridge_short_seam(a,b);self.assertIsNotNone(result) q,curve,gap=result;np.testing.assert_allclose(q[[0,-1]],[a[0],b[-1]]) self.assertAlmostEqual(gap,.5) from shapely.geometry import LineString self.assertTrue(LineString(q).is_simple) def test_short_seam_rejects_opposing_curbs(self): self.assertIsNone(bridge_short_seam([[-8,0],[0,0]],[[0,.5],[-8,.5]])) def test_short_seam_rejects_long_gap(self): self.assertIsNone(bridge_short_seam([[-8,0],[0,0]],[[2,0],[8,0]])) def test_short_seam_rejects_reverse_progress(self): self.assertIsNone(bridge_short_seam([[-8,0],[0,0]],[[-.5,0],[-8,0]])) def test_parallel_short_alternative_is_quarantined(self): self.assertTrue(competing_fragment([[2,2],[7,2]],[[0,0],[20,0]])) def test_collinear_continuation_is_not_a_duplicate(self): self.assertFalse(competing_fragment([[21,0],[25,0]],[[0,0],[20,0]])) def test_transverse_branch_is_not_a_duplicate(self): self.assertFalse(competing_fragment([[5,0],[5,2]],[[0,0],[20,0]])) def test_closed_island_is_not_a_fragment(self): self.assertFalse(competing_fragment([[2,1],[4,1],[4,2],[2,1]],[[0,0],[20,0]])) if __name__=='__main__':unittest.main(verbosity=2)