routing.cpp
Go to the documentation of this file.
3 
4 #include <boost/make_shared.hpp>
5 
7 
8 using namespace boost::python;
9 using namespace lanelet;
10 
12  const ConstLanelet& from, const ConstLanelet& to,
14  bool withLaneChange) {
15  auto route = self.getRoute(from, to, costId, withLaneChange);
16  if (!route) {
17  return {};
18  }
19  return std::make_shared<lanelet::routing::Route>(std::move(*route));
20 }
21 
23  const ConstLanelet& from,
24  const ConstLanelets& via, const ConstLanelet& to,
26  bool withLaneChange) {
27  auto route = self.getRouteVia(from, via, to, costId, withLaneChange);
28  if (!route) {
29  return {};
30  }
31  return std::make_shared<lanelet::routing::Route>(std::move(*route));
32 }
33 
35  const routing::RoutingCostPtrs& routingCosts) {
36  return routing::RoutingGraph::build(laneletMap, trafficRules, routingCosts);
37 }
38 
40  const traffic_rules::TrafficRules& trafficRules,
41  const routing::RoutingCostPtrs& routingCosts) {
42  return routing::RoutingGraph::build(laneletMap, trafficRules, routingCosts);
43 }
44 
45 template <typename T>
46 object optionalToObject(const Optional<T>& v) {
47  return v ? object(*v) : object();
48 }
49 
50 template <typename T>
51 Optional<T> objectToOptional(const object& o) {
52  return o == object() ? Optional<T>{} : Optional<T>{extract<T>(o)()};
53 }
54 
55 BOOST_PYTHON_MODULE(PYTHON_API_MODULE_NAME) { // NOLINT
56  auto trafficRules = import("lanelet2.traffic_rules");
57  using namespace lanelet::routing;
58 
62  OptionalConverter<Route>();
63  OptionalConverter<std::shared_ptr<Route>>();
64  OptionalConverter<RelationType>();
65  OptionalConverter<LaneletRelation>();
66  OptionalConverter<LaneletPath>();
67 
68  VectorToListConverter<LaneletRelations>();
69  VectorToListConverter<RoutingCostPtrs>();
70  VectorToListConverter<LaneletPaths>();
71 
72  // Register interable conversions.
73  IterableConverter().fromPython<RoutingCostPtrs>();
74 
75  implicitly_convertible<std::shared_ptr<RoutingCostDistance>, RoutingCostPtr>();
76  implicitly_convertible<std::shared_ptr<RoutingCostTravelTime>, RoutingCostPtr>();
77  implicitly_convertible<LaneletMapPtr, LaneletMapConstPtr>();
78 
79  // Routing costs
80  class_<RoutingCost, boost::noncopyable, std::shared_ptr<RoutingCost>>( // NOLINT
81  "RoutingCost", "Object for calculating routing costs between lanelets", no_init);
82 
83  class_<RoutingCostDistance, bases<RoutingCost>, std::shared_ptr<RoutingCostDistance>>( // NOLINT
84  "RoutingCostDistance", "Distance based routing cost calculation object",
85  init<double, double>((arg("laneChangeCost"), arg("minLaneChangeDistance") = 0)));
86 
87  class_<RoutingCostTravelTime, bases<RoutingCost>, std::shared_ptr<RoutingCostTravelTime>>( // NOLINT
88  "RoutingCostTravelTime", "Travel time based routing cost calculation object",
89  init<double, double>((arg("laneChangeCost"), arg("minLaneChangeTime") = 0)));
90 
91  auto possPCost = static_cast<LaneletPaths (RoutingGraph::*)(const ConstLanelet&, double, RoutingCostId, bool) const>(
92  &RoutingGraph::possiblePaths);
93  auto possPLen = static_cast<LaneletPaths (RoutingGraph::*)(const ConstLanelet&, uint32_t, bool, RoutingCostId) const>(
94  &RoutingGraph::possiblePaths);
95  auto possPParam = static_cast<LaneletPaths (RoutingGraph::*)(const ConstLanelet&, const PossiblePathsParams&) const>(
96  &RoutingGraph::possiblePaths);
97  auto possPToCost =
98  static_cast<LaneletPaths (RoutingGraph::*)(const ConstLanelet&, double, RoutingCostId, bool) const>(
99  &RoutingGraph::possiblePathsTowards);
100  auto possPToLen =
101  static_cast<LaneletPaths (RoutingGraph::*)(const ConstLanelet&, uint32_t, bool, RoutingCostId) const>(
102  &RoutingGraph::possiblePathsTowards);
103  auto possPToParam =
104  static_cast<LaneletPaths (RoutingGraph::*)(const ConstLanelet&, const PossiblePathsParams&) const>(
105  &RoutingGraph::possiblePathsTowards);
106 
107  class_<LaneletPath>("LaneletPath",
108  "A set of consecutive lanelets connected in straight "
109  "directon or by lane changes",
110  init<ConstLanelets>(arg("lanelets")))
111  .def("__iter__", iterator<LaneletPath>())
112  .def("__len__", &LaneletPath::size)
113  .def("__getitem__", wrappers::getItem<LaneletPath>, return_internal_reference<>())
114  .def(
115  "getRemainingLane",
116  +[](const LaneletPath& self, const ConstLanelet& llt) { return self.getRemainingLane(llt); },
117  "get the sequence of remaining lanelets without a lane change")
118  .def(self == self) // NOLINT
119  .def(self != self); // NOLINT
120 
121  class_<LaneletVisitInformation>("LaneletVisitInformation",
122  "Object passed as input for the forEachSuccessor function of the routing graph")
123  .def_readwrite("lanelet", &LaneletVisitInformation::lanelet, "the currently visited lanelet")
124  .def_readwrite("predecessor", &LaneletVisitInformation::predecessor, "the predecessor within the shortest path")
125  .def_readwrite("length", &LaneletVisitInformation::length,
126  "The length of the shortest path to this lanelet (including lanelet")
127  .def_readwrite("cost", &LaneletVisitInformation::cost, "The cost along the shortest path")
128  .def_readwrite("numLaneChanges", &LaneletVisitInformation::numLaneChanges,
129  "The number of lane changes necessary along the shortest path");
130 
131  class_<LaneletOrAreaVisitInformation>(
132  "LaneletOrAreaVisitInformation",
133  "Object passed as input for the forEachSuccessorIncludingAreas function of the routing graph")
134  .def_readwrite("laneletOrArea", &LaneletOrAreaVisitInformation::laneletOrArea,
135  "the currently visited lanelet/area")
136  .def_readwrite("predecessor", &LaneletOrAreaVisitInformation::predecessor,
137  "the predecessor within the shortest path")
138  .def_readwrite("length", &LaneletOrAreaVisitInformation::length,
139  "The length of the shortest path to this lanelet (including lanelet")
140  .def_readwrite("cost", &LaneletOrAreaVisitInformation::cost, "The cost along the shortest path")
141  .def_readwrite("numLaneChanges", &LaneletOrAreaVisitInformation::numLaneChanges,
142  "The number of lane changes necessary along the shortest path");
143 
144  class_<PossiblePathsParams>(
145  "PossiblePathsParams", "Parameters for configuring the behaviour of the possible path algorithms of RoutingGraph")
146  .def("__init__",
147  make_constructor(
148  +[](const object& costLimit, const object& elemLimit, RoutingCostId costId, bool includeLc,
149  bool includeShorter) {
150  return boost::make_shared<PossiblePathsParams>(PossiblePathsParams{
151  objectToOptional<double>(costLimit), objectToOptional<std::uint32_t>(elemLimit), costId, includeLc,
152  includeShorter});
153  },
154  default_call_policies{},
155  (arg("routingCostLimit") = object(), arg("elementLimit") = object(), arg("routingCostId") = 0,
156  arg("includeLaneChanges") = false, arg("includeShorterPaths") = false)))
157  .add_property(
158  "routingCostLimit", +[](const PossiblePathsParams& self) { return optionalToObject(self.routingCostLimit); },
159  +[](PossiblePathsParams& self, const object& value) {
160  self.routingCostLimit = objectToOptional<double>(value);
161  })
162  .add_property(
163  "elementLimit", +[](const PossiblePathsParams& self) { return optionalToObject(self.elementLimit); },
164  +[](PossiblePathsParams& self, const object& value) {
165  self.elementLimit = objectToOptional<std::uint32_t>(value);
166  })
167  .def_readwrite("routingCostId", &PossiblePathsParams::routingCostId)
168  .def_readwrite("includeLaneChanges", &PossiblePathsParams::includeLaneChanges)
169  .def_readwrite("includeShorterPaths", &PossiblePathsParams::includeShorterPaths);
170 
171  auto lltAndLc = (arg("lanelet"), arg("withLaneChanges") = false);
172  auto lltAndRoutingCost = (arg("lanelet"), arg("routingCostId") = 0);
173  class_<RoutingGraph, boost::noncopyable, RoutingGraphPtr>(
174  "RoutingGraph",
175  "Main class of the routing module that holds routing information and can "
176  "be queried",
177  no_init)
178  .def("__init__",
179  make_constructor(makeRoutingGraph, default_call_policies(),
180  (arg("laneletMap"), arg("trafficRules"), arg("routingCost") = defaultRoutingCosts())),
181  "Initialization with default routing costs")
182  .def("__init__",
183  make_constructor(makeRoutingGraphSubmap, default_call_policies(),
184  (arg("laneletSubmap"), arg("trafficRules"), arg("routingCost") = defaultRoutingCosts())),
185  "Initialization from a submap")
186  .def("getRoute", getRouteWrapper, "driving route from 'start' to 'end' lanelet",
187  (arg("from"), arg("to"), arg("routingCostId") = 0, arg("withLaneChanges") = true))
188  .def("getRouteVia", getRouteViaWrapper, "driving route from 'start' to 'end' lanelet using the 'via' lanelets",
189  (arg("from"), arg("via"), arg("to"), arg("routingCostId") = 0, arg("withLaneChanges") = true))
190  .def("shortestPath", &RoutingGraph::shortestPath, "shortest path between 'start' and 'end'",
191  (arg("from"), arg("to"), arg("routingCostId") = 0, arg("withLaneChanges") = true))
192  .def("shortestPathWithVia", &RoutingGraph::shortestPathVia,
193  "shortest path between 'start' and 'end' using intermediate points",
194  (arg("start"), arg("via"), arg("end"), arg("routingCostId") = 0, arg("withLaneChanges") = true))
195  .def("routingRelation", &RoutingGraph::routingRelation, "relation between two lanelets excluding 'conflicting'",
196  (arg("from"), arg("to")))
197  .def("following", &RoutingGraph::following, "lanelets that can be reached from this lanelet", lltAndLc)
198  .def("followingRelations", &RoutingGraph::followingRelations, "relations to following lanelets", lltAndLc)
199  .def("previous", &RoutingGraph::previous, "previous lanelets of this lanelet", lltAndLc)
200  .def("previousRelations", &RoutingGraph::previousRelations, "relations to preceding lanelets", lltAndLc)
201  .def("besides", &RoutingGraph::besides,
202  "all reachable left and right lanelets, including lanelet, from "
203  "left to right",
204  lltAndRoutingCost)
205  .def("left", &RoutingGraph::left, "left (routable)lanelet, if exists", lltAndRoutingCost)
206  .def("right", &RoutingGraph::right, "right (routable)lanelet, if it exists", lltAndRoutingCost)
207  .def("adjacentLeft", &RoutingGraph::adjacentLeft, "left non-routable lanelet", lltAndRoutingCost)
208  .def("adjacentRight", &RoutingGraph::adjacentRight, "right non-routable lanelet", lltAndRoutingCost)
209  .def("lefts", &RoutingGraph::lefts, "all left (routable) lanelets", lltAndRoutingCost)
210  .def("rights", &RoutingGraph::rights, "all right (routable) lanelets", lltAndRoutingCost)
211  .def("adjacentLefts", &RoutingGraph::adjacentLefts, "all left (non-routable) lanelets", lltAndRoutingCost)
212  .def("adjacentRights", &RoutingGraph::adjacentRights, "all right (non-routable) lanelets", lltAndRoutingCost)
213  .def("leftRelations", &RoutingGraph::leftRelations, "relations to left lanelets", lltAndRoutingCost)
214  .def("rightRelations", &RoutingGraph::rightRelations, "relations to right lanelets", lltAndRoutingCost)
215  .def("conflicting", &RoutingGraph::conflicting, "Conflicting lanelets", arg("lanelet"))
216  .def("reachableSet", &RoutingGraph::reachableSet, "set of lanelets that can be reached from a given lanelet",
217  (arg("lanelet"), arg("maxRoutingCost"), arg("RoutingCostId") = 0, arg("allowLaneChanges") = true))
218  .def("reachableSetTowards", &RoutingGraph::reachableSetTowards, "set of lanelets that can reach a given lanelet",
219  (arg("lanelet"), arg("maxRoutingCost"), arg("RoutingCostId") = 0, arg("allowLaneChanges") = true))
220  .def("possiblePaths", possPCost, "possible paths from a given start lanelet that are 'minRoutingCost'-long",
221  (arg("lanelet"), arg("minRoutingCost"), arg("RoutingCostId") = 0, arg("allowLaneChanges") = false,
222  arg("routingCostId") = 0))
223  .def("possiblePaths", possPParam, "possible paths from a given start lanelet as configured in parameters",
224  (arg("lanelet"), arg("parameters")))
225  .def("possiblePathsTowards", possPToCost,
226  "possible paths to a given start lanelet that are 'minRoutingCost'-long",
227  (arg("lanelet"), arg("minRoutingCost"), arg("RoutingCostId") = 0, arg("allowLaneChanges") = false,
228  arg("routingCostId") = 0))
229  .def("possiblePathsTowards", possPToParam, "possible paths to a given lanelet as configured in parameters",
230  (arg("lanelet"), arg("parameters")))
231  .def("possiblePathsMinLen", possPLen, "possible routes from a given start lanelet that are 'minLanelets'-long",
232  (arg("lanelet"), arg("minLanelets"), arg("allowLaneChanges") = false, arg("routingCostId") = 0))
233  .def("possiblePathsTowardsMinLen", possPToLen,
234  "possible routes from a given start lanelet that are 'minLanelets'-long",
235  (arg("lanelet"), arg("minLanelets"), arg("allowLaneChanges") = false, arg("routingCostId") = 0))
236  .def(
237  "forEachSuccessor",
238  +[](RoutingGraph& self, const ConstLanelet& from, object func, bool lc, RoutingCostId costId) {
239  self.forEachSuccessor(from, std::move(func), lc, costId);
240  },
241  "calls a function on each successor of lanelet with increasing cost. The function must receives a "
242  "LaneletVisitInformation object as input and must return a bool whether followers of the current lanelet "
243  "should be visited as well. The function can raise an exception if an early exit is desired",
244  (arg("lanelet"), arg("func"), arg("allowLaneChanges") = true, arg("routingCostId") = 0))
245  .def(
246  "forEachSuccessorIncludingAreas",
247  +[](RoutingGraph& self, const ConstLaneletOrArea& from, object func, bool lc, RoutingCostId costId) {
248  self.forEachSuccessorIncludingAreas(from, std::move(func), lc, costId);
249  },
250  "similar to forEachSuccessor but also includes areas into the search",
251  (arg("lanelet"), arg("func"), arg("allowLaneChanges") = true, arg("routingCostId") = 0))
252  .def(
253  "forEachPredecessor",
254  +[](RoutingGraph& self, const ConstLanelet& from, object func, bool lc, RoutingCostId costId) {
255  self.forEachPredecessor(from, std::move(func), lc, costId);
256  },
257  "similar to forEachSuccessor but instead goes backwards along the routing graph",
258  (arg("lanelet"), arg("func"), arg("allowLaneChanges") = true, arg("routingCostId") = 0))
259  .def(
260  "forEachPredecessorIncludingAreas",
261  +[](RoutingGraph& self, const ConstLaneletOrArea& from, object func, bool lc, RoutingCostId costId) {
262  self.forEachPredecessorIncludingAreas(from, std::move(func), lc, costId);
263  },
264  "calls a function on each successor of lanelet. The function must receives a LaneletVisitInformation object "
265  "as input and must return a bool whether followers of the current lanelet should be visited as well. The "
266  "function can throw an exception if an early exit is desired",
267  (arg("lanelet"), arg("func"), arg("allowLaneChanges") = true, arg("routingCostId") = 0))
268  .def(
269  "exportGraphML", +[](RoutingGraph& self, const std::string& path) { self.exportGraphML(path); },
270  "Export the internal graph to graphML (xml-based) file format")
271  .def(
272  "exportGraphViz", +[](RoutingGraph& self, const std::string& path) { self.exportGraphViz(path); },
273  "Export the internal graph to graphViz (DOT) file format")
274  .def("getDebugLaneletMap", &RoutingGraph::getDebugLaneletMap,
275  "abstract lanelet map holding the information of the routing graph",
276  (arg("routingCostId") = 0, arg("includeAdjacent") = false, arg("includeConflicting") = false))
277  .def("passableLaneletSubmap", &RoutingGraph::passableSubmap, "LaneletMap that includes all passable lanelets")
278  .def("checkValidity", &RoutingGraph::checkValidity, "Performs some basic sanity checks",
279  (arg("throwOnError") = true));
280 
281  class_<LaneletRelation>("LaneletRelation")
282  .def_readwrite("lanelet", &LaneletRelation::lanelet)
283  .def_readwrite("relationType", &LaneletRelation::relationType);
284 
285  enum_<RelationType>("RelationType")
286  .value("Successor", RelationType::Successor)
287  .value("Left", RelationType::Left)
288  .value("Right", RelationType::Right)
289  .value("Conflicting", RelationType::Conflicting)
290  .value("AdjacentLeft", RelationType::AdjacentLeft)
291  .value("AdjacentRight", RelationType::AdjacentRight)
292  .export_values();
293 
294  class_<Route, boost::noncopyable, std::shared_ptr<Route>>("Route",
295  "The famous route object that marks a route from A to B, "
296  "including all lanelets that can be used",
297  no_init)
298  .def("shortestPath", &Route::shortestPath, "Returns the shortest path along this route",
299  return_internal_reference<>())
300  .def("fullLane", &Route::fullLane, "Returns the complete lane a Lanelet belongs to")
301  .def("remainingLane", &Route::remainingLane, "Returns that remaining lane a Lanelet belongs to")
302  .def("remainingShortestPath", &Route::remainingShortestPath,
303  "Returns all lanelets on the shortest path that follow the input lanelet")
304  .def("length2d", &Route::length2d, "2d length of shortest path")
305  .def("numLanes", &Route::numLanes, "Number of inidividual lanes")
306  .def(
307  "laneletSubmap", +[](const Route& r) { return std::const_pointer_cast<LaneletSubmap>(r.laneletSubmap()); },
308  "laneletSubmap with all lanelets that are part of the route")
309  .def("getDebugLaneletMap", &Route::getDebugLaneletMap,
310  "laneletMap that represents the Lanelets of the Route and their relationship")
311  .def("size", &Route::size, "Number of lanelets")
312  .def("followingRelations", &Route::followingRelations, "Provides the following lanelets within the Route")
313  .def("previousRelations", &Route::previousRelations, "Provides the previous lanelets within the Route")
314  .def("leftRelation", &Route::leftRelation, "Provides the lanelet left of a given lanelet within the Route")
315  .def("leftRelations", &Route::leftRelations, "*all* lanelets left of a given lanelet within the Route")
316  .def("rightRelation", &Route::rightRelation, "Provides the lanelet right of a given lanelet within the Route")
317  .def("rightRelations", &Route::rightRelations, "*all* lanelets right of a given lanelet within the Route")
318  .def("conflictingInRoute", &Route::conflictingInRoute, "conflicting lanelets of a lanelet within the route")
319  .def("conflictingInMap", &Route::conflictingInMap,
320  "conflicting lanelets of a lanelet within all passable lanelets in the laneletMap")
321  .def("allConflictingInMap", &Route::allConflictingInMap,
322  "all lanelets in the map that conflict with any lanelet in the route")
323  .def(
324  "forEachSuccessor",
325  +[](Route& self, const ConstLanelet& from, object func) { self.forEachSuccessor(from, std::move(func)); },
326  "calls a function on each successor of lanelet with increasing cost. The function must receives a "
327  "LaneletVisitInformation object as input and must return a bool whether followers of the current lanelet "
328  "should be visited as well. The function can raise an exception if an early exit is desired",
329  (arg("lanelet"), arg("func")))
330  .def(
331  "forEachPredecessor",
332  +[](Route& self, const ConstLanelet& from, object func) { self.forEachPredecessor(from, std::move(func)); },
333  "similar to forEachSuccessor but instead goes backwards along the routing graph",
334  (arg("lanelet"), arg("func")))
335  .def("checkValidity", &Route::checkValidity, "perform sanity check on the route");
336 }
Optional< T > objectToOptional(const object &o)
Definition: routing.cpp:51
Optional< std::shared_ptr< lanelet::routing::Route > > getRouteWrapper(const lanelet::routing::RoutingGraph &self, const ConstLanelet &from, const ConstLanelet &to, lanelet::routing::RoutingCostId costId, bool withLaneChange)
Definition: routing.cpp:11
object optionalToObject(const Optional< T > &v)
Definition: routing.cpp:46
routing::RoutingGraphPtr makeRoutingGraph(LaneletMap &laneletMap, const traffic_rules::TrafficRules &trafficRules, const routing::RoutingCostPtrs &routingCosts)
Definition: routing.cpp:34
py::to_python_converter< T, VectorToList< T > > VectorToListConverter
Definition: converter.h:219
std::vector< LaneletPath > LaneletPaths
boost::optional< T > Optional
std::shared_ptr< RoutingGraph > RoutingGraphPtr
std::shared_ptr< RoutingCost > RoutingCostPtr
BOOST_PYTHON_MODULE(PYTHON_API_MODULE_NAME)
Definition: routing.cpp:55
routing::RoutingGraphPtr makeRoutingGraphSubmap(LaneletSubmap &laneletMap, const traffic_rules::TrafficRules &trafficRules, const routing::RoutingCostPtrs &routingCosts)
Definition: routing.cpp:39
RoutingCostPtrs defaultRoutingCosts()
py::to_python_converter< lanelet::Optional< T >, OptionalToObject< T > > OptionalConverter
Definition: converter.h:222
std::vector< RoutingCostPtr > RoutingCostPtrs
uint16_t RoutingCostId
LaneletSubmapConstPtr laneletSubmap() const noexcept
Optional< std::shared_ptr< lanelet::routing::Route > > getRouteViaWrapper(const lanelet::routing::RoutingGraph &self, const ConstLanelet &from, const ConstLanelets &via, const ConstLanelet &to, lanelet::routing::RoutingCostId costId, bool withLaneChange)
Definition: routing.cpp:22
std::vector< ConstLanelet > ConstLanelets


lanelet2_python
Author(s): Fabian Poggenhans
autogenerated on Tue Jun 6 2023 02:23:59