ShortestPath.h
Go to the documentation of this file.
1 #pragma once
2 #include <boost/graph/dijkstra_shortest_paths_no_color_map.hpp>
3 #include <boost/graph/filtered_graph.hpp>
4 #include <boost/property_map/property_map.hpp>
5 
9 
10 namespace lanelet {
11 namespace routing {
12 namespace internal {
13 
18  double cost{};
19  size_t length{};
20  size_t numLaneChanges{};
21 };
22 
23 struct VertexState {
25  double cost{};
26  size_t length{};
27  size_t numLaneChanges{};
28  bool predicate{true};
29  bool isLeaf{true};
30 };
31 
32 template <typename VertexT>
33 using DijkstraSearchMap = std::map<VertexT, VertexState>;
34 
35 template <typename VertexT>
37  using key_type = VertexT; // NOLINT
38  using value_type = double; // NOLINT
39  using reference = void; // NOLINT
40  using category = boost::read_write_property_map_tag; // NOLINT
42 };
43 
44 template <typename VertexT>
45 inline typename DijkstraCostMap<VertexT>::value_type get(const DijkstraCostMap<VertexT>& map, VertexT key) {
46  auto val = map.map->find(key);
47  if (val != map.map->end()) {
48  return val->second.cost;
49  }
50  return std::numeric_limits<double>::infinity();
51 }
52 
53 template <typename VertexT>
54 inline void put(DijkstraCostMap<VertexT>& map, VertexT key, typename DijkstraCostMap<VertexT>::value_type value) {
55  (*map.map)[key].cost = value;
56 }
57 
58 template <typename G>
60  public:
61  using VertexType = typename boost::graph_traits<G>::vertex_descriptor;
62  using EdgeType = typename boost::graph_traits<G>::edge_descriptor;
64  using VisitCallback = std::function<bool(const VertexVisitInformation&)>;
65 
66  private:
67  class LeafFilter {
68  public:
69  LeafFilter() = default;
70  LeafFilter(const DijkstraSearchMapType& m, const G& g) : m_{&m}, g_{&g} {}
71  bool operator()(EdgeType e) const { return (*m_).at(boost::source(e, *g_)).predicate; }
72 
73  private:
75  const G* g_{};
76  };
77  using SearchGraph = boost::filtered_graph<G, LeafFilter>;
78 
79  template <typename Func>
80  class DijkstraStyleVisitor : public boost::default_dijkstra_visitor {
81  using FuncT = std::remove_reference_t<Func>;
82 
83  public:
84  DijkstraStyleVisitor() = default;
86 
87  // called whenever a minimal edge is discovered
88  void examine_vertex(VertexType v, const SearchGraph& /*g*/) { // NOLINT
89  auto& state = map_->at(v);
90  state.predicate =
91  ((*cb_)(VertexVisitInformation{v, state.predecessor, state.cost, state.length, state.numLaneChanges}));
92  map_->at(state.predecessor).isLeaf = v == state.predecessor; // necessary for the initial vertex
93  }
94 
95  void edge_relaxed(EdgeType e, const SearchGraph& g) { // NOLINT
96  // called whenever a shorter path to e is discovered and before "examine vertex" is called
97  // cost is automatically updated by the dijkstra algorithm
98  auto& predecessor = (*map_).at(boost::source(e, g));
99  auto& follower = (*map_)[boost::target(e, g)];
100  follower.length = predecessor.length + 1;
101  follower.predecessor = boost::source(e, g);
102  follower.numLaneChanges = predecessor.numLaneChanges + (g[e].relation != RelationType::Successor);
103  }
104 
105  private:
107  FuncT* cb_{};
108  };
109 
110  public:
112  explicit DijkstraStyleSearch(const G& graph) : graph_{graph, LeafFilter{vertices_, graph}} {}
113 
116  template <typename Func>
117  void query(VertexType start, Func&& func) {
118  vertices_.clear();
119  vertices_.emplace(start, VertexState{start, 0., 1, 0, true, true});
120  DijkstraStyleVisitor<decltype(func)> visitor{vertices_, &func};
121  auto inf = std::numeric_limits<double>::infinity();
122  // offering sane defaults seems to have been impossible...
123  boost::dijkstra_shortest_paths_no_color_map_no_init(
124  graph_, start, boost::dummy_property_map{}, DijkstraCostMap<VertexType>{&vertices_},
125  boost::get(&EdgeInfo::routingCost, graph_), boost::get(boost::vertex_index, graph_), std::less<double>{},
126  boost::closed_plus<double>{}, inf, 0., visitor);
127  }
128 
130  const DijkstraSearchMapType& getMap() const { return vertices_; }
131 
132  private:
135 };
136 
137 } // namespace internal
138 } // namespace routing
139 } // namespace lanelet
lanelet::routing::internal::DijkstraStyleSearch::query
void query(VertexType start, Func &&func)
Definition: ShortestPath.h:117
lanelet
lanelet::routing::internal::VertexVisitInformation::cost
double cost
Definition: ShortestPath.h:18
lanelet::routing::internal::DijkstraStyleSearch::LeafFilter::LeafFilter
LeafFilter()=default
lanelet::routing::internal::DijkstraStyleSearch::vertices_
DijkstraSearchMapType vertices_
Definition: ShortestPath.h:134
lanelet::routing::internal::DijkstraStyleSearch::LeafFilter
Definition: ShortestPath.h:67
lanelet::routing::internal::DijkstraCostMap::key_type
VertexT key_type
Definition: ShortestPath.h:37
lanelet::routing::internal::EdgeInfo::routingCost
double routingCost
Calculcated routing cost. Infinity if not routable.
Definition: Graph.h:37
lanelet::routing::internal::DijkstraStyleSearch::LeafFilter::operator()
bool operator()(EdgeType e) const
Definition: ShortestPath.h:71
lanelet::routing::internal::DijkstraCostMap::value_type
double value_type
Definition: ShortestPath.h:38
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleVisitor::map_
DijkstraSearchMapType * map_
Definition: ShortestPath.h:106
lanelet::routing::internal::VertexState::predecessor
RoutingGraphGraph::Vertex predecessor
The vertex this refers to.
Definition: ShortestPath.h:24
lanelet::routing::internal::VertexState::numLaneChanges
size_t numLaneChanges
Required lane changes along the shortest path in order to get here.
Definition: ShortestPath.h:27
lanelet::routing::RelationType::Successor
@ Successor
A (the only) direct, reachable successor. Not merging and not diverging.
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleVisitor::examine_vertex
void examine_vertex(VertexType v, const SearchGraph &)
Definition: ShortestPath.h:88
lanelet::routing::internal::VertexVisitInformation::predecessor
RoutingGraphGraph::Vertex predecessor
Definition: ShortestPath.h:17
lanelet::routing::internal::VertexVisitInformation::length
size_t length
Definition: ShortestPath.h:19
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleVisitor
Definition: ShortestPath.h:80
lanelet::routing::internal::DijkstraStyleSearch::getMap
const DijkstraSearchMapType & getMap() const
Returns the result.
Definition: ShortestPath.h:130
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleVisitor::edge_relaxed
void edge_relaxed(EdgeType e, const SearchGraph &g)
Definition: ShortestPath.h:95
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleVisitor::FuncT
std::remove_reference_t< Func > FuncT
Definition: ShortestPath.h:81
lanelet::routing::internal::get
SparseColorMap::value_type get(const SparseColorMap &map, LaneletVertexId key)
Definition: GraphUtils.h:304
lanelet::routing::internal::VertexState::predicate
bool predicate
False if disabled by predicate.
Definition: ShortestPath.h:28
lanelet::routing::internal::Graph< GraphType >::Vertex
typename boost::graph_traits< GraphType >::vertex_descriptor Vertex
Definition: Graph.h:111
lanelet::routing::internal::DijkstraCostMap::reference
void reference
Definition: ShortestPath.h:39
lanelet::routing::internal::DijkstraStyleSearch::EdgeType
typename boost::graph_traits< G >::edge_descriptor EdgeType
Definition: ShortestPath.h:62
lanelet::routing::internal::VertexState
Definition: ShortestPath.h:23
lanelet::routing::internal::DijkstraStyleSearch::VertexType
typename boost::graph_traits< G >::vertex_descriptor VertexType
Definition: ShortestPath.h:61
lanelet::routing::internal::VertexState::isLeaf
bool isLeaf
True if it has no successor that is on the shortest path.
Definition: ShortestPath.h:29
lanelet::routing::internal::VertexVisitInformation
This object carries the required information for the graph neighbourhood search.
Definition: ShortestPath.h:15
GraphUtils.h
lanelet::routing::internal::DijkstraStyleSearch::LeafFilter::LeafFilter
LeafFilter(const DijkstraSearchMapType &m, const G &g)
Definition: ShortestPath.h:70
lanelet::routing::internal::DijkstraStyleSearch::LeafFilter::m_
const DijkstraSearchMapType * m_
Definition: ShortestPath.h:74
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleSearch
DijkstraStyleSearch(const G &graph)
Constructor for the graph search.
Definition: ShortestPath.h:112
lanelet::routing::internal::DijkstraSearchMap
std::map< VertexT, VertexState > DijkstraSearchMap
Definition: ShortestPath.h:33
lanelet::routing::internal::DijkstraCostMap::category
boost::read_write_property_map_tag category
Definition: ShortestPath.h:40
lanelet::routing::internal::DijkstraStyleSearch::LeafFilter::g_
const G * g_
Definition: ShortestPath.h:75
lanelet::routing::internal::DijkstraCostMap
Definition: ShortestPath.h:36
lanelet::routing::internal::DijkstraCostMap::map
DijkstraSearchMap< VertexT > * map
Definition: ShortestPath.h:41
Graph.h
lanelet::routing::internal::DijkstraStyleSearch::DijkstraSearchMapType
DijkstraSearchMap< VertexType > DijkstraSearchMapType
Definition: ShortestPath.h:63
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleVisitor::cb_
FuncT * cb_
Definition: ShortestPath.h:107
lanelet::routing::internal::DijkstraStyleSearch
Definition: ShortestPath.h:59
lanelet::routing::internal::VertexState::cost
double cost
Current accumulated cost.
Definition: ShortestPath.h:25
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleVisitor::DijkstraStyleVisitor
DijkstraStyleVisitor()=default
lanelet::routing::internal::DijkstraStyleSearch::VisitCallback
std::function< bool(const VertexVisitInformation &)> VisitCallback
Definition: ShortestPath.h:64
lanelet::routing::internal::VertexState::length
size_t length
Number of vertices to this vertex (including this one)
Definition: ShortestPath.h:26
lanelet::routing::internal::DijkstraStyleSearch::DijkstraStyleVisitor::DijkstraStyleVisitor
DijkstraStyleVisitor(DijkstraSearchMapType &map, FuncT *cb)
Definition: ShortestPath.h:85
lanelet::routing::internal::DijkstraStyleSearch::graph_
SearchGraph graph_
Definition: ShortestPath.h:133
lanelet::routing::internal::DijkstraStyleSearch::SearchGraph
boost::filtered_graph< G, LeafFilter > SearchGraph
Definition: ShortestPath.h:77
lanelet::routing::internal::VertexVisitInformation::numLaneChanges
size_t numLaneChanges
Definition: ShortestPath.h:20
lanelet::routing::internal::get
DijkstraCostMap< VertexT >::value_type get(const DijkstraCostMap< VertexT > &map, VertexT key)
Definition: ShortestPath.h:45
lanelet::routing::internal::put
void put(SparseColorMap &map, LaneletVertexId key, SparseColorMap::value_type value)
Definition: GraphUtils.h:312
Exceptions.h
lanelet::routing::internal::VertexVisitInformation::vertex
RoutingGraphGraph::Vertex vertex
Definition: ShortestPath.h:16


lanelet2_routing
Author(s): Matthias Mayr
autogenerated on Sun Oct 27 2024 02:27:49