test_lanelet2_matching.py
Go to the documentation of this file.
1 import unittest
2 import lanelet2 # if we fail here, there is something wrong with lanelet2 registration
3 from lanelet2.core import AttributeMap, getId, BasicPoint2d, Point3d, LineString3d, Lanelet, RegulatoryElement, TrafficLight, LaneletMap, createMapFromLanelets, ConstLanelet, ConstLineString3d
4 from lanelet2.geometry import distance, intersects2d, boundingBox2d, to2D, equals
5 from lanelet2.matching import Pose2d, getDeterministicMatches, getProbabilisticMatches, Object2d, ObjectWithCovariance2d, PositionCovariance2d, removeNonRuleCompliantMatches
6 from lanelet2.matching import ConstLaneletMatch, ConstLaneletMatchProbabilistic
7 
8 
10  mymap = LaneletMap()
11  x_left = 2
12  x_right = 0
13  ls_left = LineString3d(getId(), [Point3d(getId(), x_left, i, 0) for i in range(0, 3)])
14  ls_right = LineString3d(getId(), [Point3d(getId(), x_right, i, 0) for i in range(0, 3)])
15  llet = Lanelet(getId(), ls_left, ls_right)
16  mymap.add(llet)
17  return mymap
18 
19 
20 class ObjectsApiTestCase(unittest.TestCase):
21  def test_default_init(self):
22  obj_2d = Object2d()
23  self.assertTrue(isinstance(obj_2d, Object2d))
24  self.assertEqual(len(obj_2d.absoluteHull), 0)
25 
26  obj_with_cov_2d = ObjectWithCovariance2d()
27  self.assertTrue(isinstance(obj_with_cov_2d, ObjectWithCovariance2d))
28  self.assertEqual(len(obj_with_cov_2d.absoluteHull), 0)
29  self.assertEqual(obj_with_cov_2d.vonMisesKappa, 0)
30 
31  pos_cov_2d = PositionCovariance2d()
32  self.assertTrue(isinstance(pos_cov_2d, PositionCovariance2d))
33 
34  pose_2d = Pose2d()
35  self.assertTrue(isinstance(pose_2d, Pose2d))
36 
37  def test_custom_init(self):
38  obj_2d = Object2d(1, Pose2d(), [])
39  self.assertTrue(isinstance(obj_2d, Object2d))
40  self.assertEqual(len(obj_2d.absoluteHull), 0)
41 
42  obj_with_cov_2d = ObjectWithCovariance2d(1, Pose2d(), [], PositionCovariance2d(), 2)
43  self.assertTrue(isinstance(obj_with_cov_2d, ObjectWithCovariance2d))
44  self.assertEqual(len(obj_with_cov_2d.absoluteHull), 0)
45  self.assertEqual(obj_with_cov_2d.vonMisesKappa, 2)
46 
47  pos_cov_2d = PositionCovariance2d(1., 2., 3.)
48  self.assertTrue(isinstance(pos_cov_2d, PositionCovariance2d))
49  self.assertEqual("1 3\n3 2", str(pos_cov_2d))
50 
51  pose_2d = Pose2d(1., 2., 3.)
52  self.assertTrue(isinstance(pose_2d, Pose2d))
53  self.assertEqual("x: 1\ny: 2\nphi: 3", str(pose_2d))
54 
55 
56 class MatchingApiTestCase(unittest.TestCase):
57  def test_matching(self):
58  mymap = get_sample_lanelet_map()
59 
60  obj = Object2d(1, Pose2d(1, 1, 0), [])
61  obj_with_cov = ObjectWithCovariance2d(1, Pose2d(1, 1, 0), [], PositionCovariance2d(1., 1., 0.), 2)
62 
63  obj_matches = getDeterministicMatches(mymap, obj, 1.)
64  self.assertEqual(len(obj_matches), 2) # lanelet in both directions
65  self.assertTrue(isinstance(obj_matches[0], ConstLaneletMatch))
66 
67  obj_with_cov_matches = getProbabilisticMatches(mymap, obj_with_cov, 1.)
68  self.assertEqual(len(obj_with_cov_matches), 2) # lanelet in both directions
69  self.assertTrue(isinstance(obj_with_cov_matches[0], ConstLaneletMatchProbabilistic))
70 
71  traffic_rules = lanelet2.traffic_rules.create(lanelet2.traffic_rules.Locations.Germany,
72  lanelet2.traffic_rules.Participants.Vehicle)
73 
74  obj_matches_rule_compliant = removeNonRuleCompliantMatches(obj_matches, traffic_rules)
75  self.assertEqual(len(obj_matches_rule_compliant), 1) # lanelet only in one direction
76  self.assertTrue(isinstance(obj_matches_rule_compliant[0], ConstLaneletMatch))
77 
78  obj_with_cov_matches_rule_compliant = removeNonRuleCompliantMatches(obj_with_cov_matches, traffic_rules)
79  self.assertEqual(len(obj_with_cov_matches_rule_compliant), 1) # lanelet only in one direction
80  self.assertTrue(isinstance(obj_with_cov_matches_rule_compliant[0], ConstLaneletMatchProbabilistic))
81  self.assertTrue(isinstance(obj_with_cov_matches_rule_compliant[0].lanelet, ConstLanelet))
82  self.assertEqual(obj_with_cov_matches_rule_compliant[0].distance, 0)
83  self.assertTrue(isinstance(obj_with_cov_matches_rule_compliant[0].mahalanobisDistSq, float))
84 
85  # empty matches list
86  empty_matches_rule_compliant = removeNonRuleCompliantMatches(list(), traffic_rules)
87  self.assertEqual(len(empty_matches_rule_compliant), 0)
88 
89  # list of neither matches nor probabilistic matches
90  self.assertRaises(RuntimeError, removeNonRuleCompliantMatches, [obj], traffic_rules)
91 
92 
93 if __name__ == '__main__':
94  unittest.main()
test_lanelet2_matching.get_sample_lanelet_map
def get_sample_lanelet_map()
Definition: test_lanelet2_matching.py:9
test_lanelet2_matching.ObjectsApiTestCase.test_custom_init
def test_custom_init(self)
Definition: test_lanelet2_matching.py:37
test_lanelet2_matching.ObjectsApiTestCase.test_default_init
def test_default_init(self)
Definition: test_lanelet2_matching.py:21
test_lanelet2_matching.ObjectsApiTestCase
Definition: test_lanelet2_matching.py:20
test_lanelet2_matching.MatchingApiTestCase
Definition: test_lanelet2_matching.py:56
test_lanelet2_matching.MatchingApiTestCase.test_matching
def test_matching(self)
Definition: test_lanelet2_matching.py:57


lanelet2_python
Author(s): Fabian Poggenhans
autogenerated on Thu Mar 6 2025 03:26:14