Fast map matching  0.1.0
composite_graph.cpp
1 #include "mm/composite_graph.hpp"
2 #include "util/debug.hpp"
3 
4 using namespace FMM;
5 using namespace FMM::CORE;
6 using namespace FMM::NETWORK;
7 using namespace FMM::MM;
8 
9 DummyGraph::DummyGraph(){}
10 
11 DummyGraph::DummyGraph(const Traj_Candidates &traj_candidates){
12  if (traj_candidates.empty()) return;
13  int N = traj_candidates.size();
14  std::unordered_map<EdgeIndex,const Candidate*> ca;
15  std::unordered_map<EdgeIndex,const Candidate*> cb;
16  std::unordered_map<EdgeIndex,const Candidate*> *prev_cmap = &ca;
17  std::unordered_map<EdgeIndex,const Candidate*> *cur_cmap = &cb;
18  for (int i=0; i<N; ++i) {
19  const Point_Candidates &pcs = traj_candidates[i];
20  for (const Candidate &c:pcs) {
21  NodeIndex n = c.index;
22  add_edge(c.edge->source, n, c.edge->index, c.offset);
23  add_edge(n,c.edge->target, c.edge->index, c.edge->length - c.offset);
24  cur_cmap->insert(std::make_pair(c.edge->index,&c));
25  auto iter = prev_cmap->find(c.edge->index);
26  if (iter!=prev_cmap->end()) {
27  if (iter->second->offset <= c.offset) {
28  add_edge(iter->second->index,
29  n, c.edge->index, c.offset-iter->second->offset);
30  }
31  }
32  }
33  std::unordered_map<EdgeIndex,const Candidate*> *temp = prev_cmap;
34  prev_cmap = cur_cmap;
35  cur_cmap = temp;
36  cur_cmap->clear();
37  }
38 }
39 
40 Graph_T *DummyGraph::get_graph_ptr(){
41  return &g;
42 }
43 
44 const Graph_T &DummyGraph::get_boost_graph() const {
45  return g;
46 }
47 
48 int DummyGraph::get_num_vertices() const {
49  return boost::num_vertices(g);
50 }
51 
52 bool DummyGraph::containNodeIndex(NodeIndex external_index) const {
53  return internal_index_map.find(external_index)!=internal_index_map.end();
54 }
55 
56 NodeIndex DummyGraph::get_external_index(DummyIndex inner_index) const {
57  return external_index_vec[inner_index];
58 }
59 
60 DummyIndex DummyGraph::get_internal_index(NodeIndex external_index) const {
61  return internal_index_map.at(external_index);
62 }
63 
64 int DummyGraph::get_edge_index(NodeIndex source,NodeIndex target,double cost)
65 const {
66  SPDLOG_TRACE("Dummy graph get edge index {} {} cost {}",source,target,cost);
68  OutEdgeIterator out_i, out_end;
69  NodeIndex source_idx = get_internal_index(source);
70  NodeIndex target_idx = get_internal_index(target);
71  for (boost::tie(out_i, out_end) = boost::out_edges(source_idx,g);
72  out_i != out_end; ++out_i) {
73  e = *out_i;
74  SPDLOG_TRACE("Target index {} {} id {} e length {} {}",
75  target_idx, boost::target(e,g),
76  g[e].index,
77  g[e].length,
78  std::abs(g[e].length - cost));
79  if (target_idx == boost::target(e,g) &&
80  (std::abs(g[e].length - cost) <= DOUBLE_MIN)) {
81  return g[e].index;
82  }
83  }
84  return -1;
85 }
86 
87 void DummyGraph::print_node_index_map() const {
88  std::cout<<"Inner index map\n";
89  for (auto const& pair:internal_index_map) {
90  std::cout << "{" << pair.first << ": " << pair.second << "}\n";
91  }
92 }
93 
94 void DummyGraph::add_edge(NodeIndex source, NodeIndex target,
95  EdgeIndex edge_index, double cost) {
96  // SPDLOG_TRACE(" Add edge {} {} e {} cost {}",
97  // source,target,edge_index,cost);
98  auto search1 = internal_index_map.find(source);
99  auto search2 = internal_index_map.find(target);
100  DummyIndex source_idx, target_idx;
101  if (search1!=internal_index_map.end()) {
102  source_idx = search1->second;
103  } else {
104  source_idx = external_index_vec.size();
105  external_index_vec.push_back(source);
106  internal_index_map.insert({source,source_idx});
107  }
108  if (search2!=internal_index_map.end()) {
109  target_idx = search2->second;
110  } else {
111  target_idx = external_index_vec.size();
112  external_index_vec.push_back(target);
113  internal_index_map.insert({target,target_idx});
114  }
115  EdgeDescriptor e;
116  bool inserted;
117  boost::tie(e, inserted) = boost::add_edge(source_idx,target_idx,g);
118  // id is the FID read, id_attr is the external property in SHP
119  g[e].index = edge_index;
120  g[e].length = cost;
121 }
122 
123 CompositeGraph::CompositeGraph(const NetworkGraph &g,const DummyGraph &dg) :
124  g_(g),dg_(dg){
125  num_vertices = g_.get_num_vertices();
126 }
127 
129  return num_vertices;
130 }
131 
133 const {
134  if (u >= num_vertices || v >= num_vertices) {
135  return dg_.get_edge_index(u,v,cost);
136  } else {
137  return g_.get_edge_index(u,v,cost);
138  }
139 }
140 
142 const {
143  return g_.get_edge_id(get_edge_index(u,v,cost));
144 }
145 
146 std::vector<CompEdgeProperty> CompositeGraph::out_edges(NodeIndex u) const {
147  std::vector<CompEdgeProperty> out_edges;
148  OutEdgeIterator out_i, out_end;
149  EdgeDescriptor e;
150  if (dg_.containNodeIndex(u)) {
151  const Graph_T &dg = dg_.get_boost_graph();
152  NodeIndex u_internal = dg_.get_internal_index(u);
153  for (boost::tie(out_i, out_end) = boost::out_edges(u_internal,dg);
154  out_i != out_end; ++out_i) {
155  e = *out_i;
156  NodeIndex v_internal = boost::target(e, dg);
157  NodeIndex v = dg_.get_external_index(v_internal);
158  out_edges.push_back(CompEdgeProperty{v,dg[e].length});
159  }
160  }
161  if (u < num_vertices) {
162  const Graph_T &g = g_.get_boost_graph();
163  for (boost::tie(out_i, out_end)=boost::out_edges(u,g);
164  out_i != out_end; ++out_i) {
165  e = *out_i;
166  NodeIndex v = boost::target(e, g);
167  // Export a pair of (v,cost)
168  out_edges.push_back(CompEdgeProperty{v,g[e].length});
169  }
170  }
171  return out_edges;
172 }
173 
175  return u>=num_vertices;
176 }
FMM::MM::DummyGraph::get_edge_index
int get_edge_index(NETWORK::NodeIndex source, NETWORK::NodeIndex target, double cost) const
Get the edge index in the original network graph.
Definition: composite_graph.cpp:64
FMM::NETWORK::Graph_T
boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProperty > Graph_T
Boost graph type.
Definition: graph.hpp:36
FMM::MM::Traj_Candidates
std::vector< Point_Candidates > Traj_Candidates
trajectory candidates
Definition: mm_type.hpp:38
FMM::MM::DummyGraph::get_external_index
NETWORK::NodeIndex get_external_index(DummyIndex inner_index) const
Get the NodeIndex of a node according to the inner index of the dummy graph.
Definition: composite_graph.cpp:56
FMM::NETWORK::EdgeDescriptor
Graph_T::edge_descriptor EdgeDescriptor
Boost graph edge type.
Definition: graph.hpp:41
FMM::MM::CompositeGraph::get_dummy_node_start_index
unsigned int get_dummy_node_start_index() const
Get the starting node index corresponding to the the first dummy node in the dummy graph.
Definition: composite_graph.cpp:128
FMM::MM::CompositeGraph::get_edge_index
int get_edge_index(NETWORK::NodeIndex u, NETWORK::NodeIndex v, double cost) const
Get edge index from node index u,v and cost.
Definition: composite_graph.cpp:132
FMM::NETWORK::NetworkGraph::get_edge_id
int get_edge_id(EdgeIndex idx) const
Get the edge ID from edge index.
Definition: network_graph.hpp:98
FMM
Fast map matching.
Definition: geom_algorithm.hpp:17
FMM::MM::CompositeGraph::out_edges
std::vector< CompEdgeProperty > out_edges(NETWORK::NodeIndex u) const
Get out edges leaving a node u in the composite graph.
Definition: composite_graph.cpp:146
FMM::NETWORK::NetworkGraph::get_boost_graph
const Graph_T & get_boost_graph() const
Get inner boost graph.
Definition: network_graph.cpp:43
FMM::NETWORK::EdgeID
int EdgeID
Edge ID in the network, can be discontinuous int.
Definition: type.hpp:23
FMM::MM::Candidate
Candidate edge matched to a GPS point
Definition: mm_type.hpp:26
FMM::NETWORK::NetworkGraph::get_num_vertices
unsigned int get_num_vertices() const
Get number of vertices in the graph.
Definition: network_graph.cpp:50
FMM::NETWORK::OutEdgeIterator
boost::graph_traits< Graph_T >::out_edge_iterator OutEdgeIterator
Boost graph out edge iterator.
Definition: graph.hpp:49
FMM::MM::Point_Candidates
std::vector< Candidate > Point_Candidates
Point candidates.
Definition: mm_type.hpp:37
FMM::NETWORK::NetworkGraph
Graph class of the network.
Definition: network_graph.hpp:33
FMM::CORE
Core data types.
Definition: geometry.hpp:22
FMM::MM::DummyGraph::get_boost_graph
const NETWORK::Graph_T & get_boost_graph() const
Get a const reference to the inner graph data.
Definition: composite_graph.cpp:44
FMM::MM
Class related with map matching.
Definition: composite_graph.hpp:18
FMM::MM::DummyGraph::get_internal_index
DummyIndex get_internal_index(NETWORK::NodeIndex external_index) const
Get the internal index of a node in dummy graph If the node is not contained in the dummy graph,...
Definition: composite_graph.cpp:60
FMM::NETWORK
Classes related with network and graph.
Definition: graph.hpp:21
FMM::MM::DummyGraph
A graph containing dummy nodes and edges used in map matching.
Definition: composite_graph.hpp:31
FMM::NETWORK::NetworkGraph::get_edge_index
int get_edge_index(NodeIndex source, NodeIndex target, double cost) const
Find the edge ID given a pair of nodes and its cost, if not found, return -1.
Definition: network_graph.cpp:188
FMM::MM::CompositeGraph::get_edge_id
NETWORK::EdgeID get_edge_id(NETWORK::NodeIndex u, NETWORK::NodeIndex v, double cost) const
Get edge id from node index u,v and cost.
Definition: composite_graph.cpp:141
FMM::MM::DummyGraph::containNodeIndex
bool containNodeIndex(NETWORK::NodeIndex external_index) const
Check if a node is contained in the dummy graph.
Definition: composite_graph.cpp:52
FMM::NETWORK::NodeIndex
unsigned int NodeIndex
Node Index in the network, range from [0,num_vertices-1 ].
Definition: type.hpp:24
FMM::NETWORK::EdgeIndex
unsigned int EdgeIndex
Edge Index in the network, range from [0,num_edges-1 ].
Definition: type.hpp:26
FMM::MM::DummyIndex
unsigned int DummyIndex
This is an index used in the dummy graph.
Definition: composite_graph.hpp:23
FMM::MM::CompEdgeProperty
Property of an edge in the composite graph.
Definition: composite_graph.hpp:128
FMM::MM::CompositeGraph::check_dummy_node
bool check_dummy_node(NETWORK::NodeIndex u) const
Check if a node u is dummy node, namely representing a candidate point.
Definition: composite_graph.cpp:174