PGROUTING  3.2
pgr_lineGraphFull.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_lineGraphFull.hpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2017 Anthony Nicola Tasca
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
30 #ifndef INCLUDE_LINEGRAPH_PGR_LINEGRAPHFULL_HPP_
31 #define INCLUDE_LINEGRAPH_PGR_LINEGRAPHFULL_HPP_
32 #pragma once
33 
34 
35 #include <vector>
36 #include <set>
37 #include <utility>
38 #include <map>
39 
41 #include "cpp_common/line_vertex.h"
42 
43 namespace pgrouting {
44 namespace graph {
45 
46 template <class G, typename T_V, typename T_E>
47 class Pgr_lineGraphFull : public Pgr_base_graph<G, T_V, T_E> {
48  public:
49  typedef typename boost::graph_traits < G >::vertex_descriptor V;
50  typedef typename boost::graph_traits < G >::edge_descriptor E;
51  typedef typename boost::graph_traits < G >::vertex_iterator V_i;
52  typedef typename boost::graph_traits < G >::edge_iterator E_i;
53  typedef typename boost::graph_traits < G >::out_edge_iterator EO_i;
54  typedef typename boost::graph_traits < G >::in_edge_iterator EI_i;
55 
56 
58  : Pgr_base_graph< G, T_V, T_E >(gtype),
59  m_num_edges(0) {
60  }
61 
63  : Pgr_base_graph< G, T_V, T_E >(graphType::DIRECTED) {
64  apply_transformation(digraph);
65  store_edge_costs(digraph);
66  }
67 
68  friend std::ostream& operator<<(
69  std::ostream &log, const Pgr_lineGraphFull< G, T_V, T_E > &g) {
70  typename Pgr_base_graph< G, T_V, T_E >::EO_i out, out_end;
71 
72  for (auto vi = vertices(g.graph).first;
73  vi != vertices(g.graph).second; ++vi) {
74  if ((*vi) >= g.num_vertices()) break;
75  log << (*vi) << ": " << " out_edges_of(" << g.graph[(*vi)] << "):";
76  for (boost::tie(out, out_end) = out_edges(*vi, g.graph);
77  out != out_end; ++out) {
78  log << ' '
79  << g.graph[*out].id << "=("
80  << g[g.source(*out)].id << ", "
81  << g[g.target(*out)].id << ")\t";
82  }
83  log << std::endl;
84  }
85  return log;
86  }
87 
88  std::vector< Line_graph_full_rt >
90  std::vector< Line_graph_full_rt > results;
91 
92  typename boost::graph_traits < G >::edge_iterator edgeIt, edgeEnd;
93  std::map <
94  std::pair<int64_t, int64_t >,
95  Line_graph_full_rt > unique;
96  auto count = 0;
97  auto vertex_count = 0;
98  std::map < int64_t, int64_t > vertex_id_map;
99  std::map < int64_t, int64_t > vertex_id_reverse_map;
100 
101  log << "\nPostgres results\n";
102  for (boost::tie(edgeIt, edgeEnd) = boost::edges(this->graph);
103  edgeIt != edgeEnd; edgeIt++) {
104  E e = *edgeIt;
105  auto e_source = this->graph[this->source(e)].vertex_id;
106  auto e_target = this->graph[this->target(e)].vertex_id;
107 
108  auto target_vertex_edge_pair = m_transformation_map[e_target];
109  auto target_vertex_id = target_vertex_edge_pair.first;
110  auto target_edge_id = target_vertex_edge_pair.second;
111  auto source_vertex_edge_pair = m_transformation_map[e_source];
112  auto source_vertex_id = source_vertex_edge_pair.first;
113  auto source_edge_id = source_vertex_edge_pair.second;
114 
115  int64_t edge_id = 0;
116  auto e_cost = 0.0;
117  if (source_edge_id == target_edge_id) {
118  e_cost = m_edge_costs[source_edge_id];
119  edge_id = source_edge_id;
120  }
121 
122  if (vertex_id_map.find(e_source) == vertex_id_map.end()) {
123  if (vertex_id_reverse_map.find(source_vertex_id) ==
124  vertex_id_reverse_map.end()) {
125  vertex_id_map[e_source] = source_vertex_id;
126  vertex_id_reverse_map[source_vertex_id] = e_source;
127  } else {
128  --vertex_count;
129  vertex_id_map[e_source] = vertex_count;
130  vertex_id_reverse_map[vertex_count] = e_source;
131  }
132  }
133 
134  if (vertex_id_map.find(e_target) == vertex_id_map.end()) {
135  if (vertex_id_reverse_map.find(target_vertex_id) ==
136  vertex_id_reverse_map.end()) {
137  vertex_id_map[e_target] = target_vertex_id;
138  vertex_id_reverse_map[target_vertex_id] = e_target;
139  } else {
140  --vertex_count;
141  vertex_id_map[e_target] = vertex_count;
142  vertex_id_reverse_map[vertex_count] = e_target;
143  }
144  }
145 
146 #if 0
147  log << "e_source = " << e_source
148  << " e_target = " << e_target
149  << "\n";
150 #endif
152  ++count,
153  vertex_id_map[e_source],
154  vertex_id_map[e_target],
155  e_cost,
156  edge_id
157  };
158 
159  unique[ std::pair<int64_t, int64_t>(e_source, e_target) ] =
160  edge;
161  }
162  for (const auto &edge : unique) {
163  results.push_back(edge.second);
164  }
165  return results;
166  }
167 
168  private:
170  int64_t original_vertex_id,
171  int64_t original_edge_id) {
172  auto new_id = static_cast<int64_t>(this->num_vertices() + 1);
173  m_transformation_map[new_id] =
174  std::pair<int64_t, int64_t>(original_vertex_id, original_edge_id);
175  m_vertex_map[std::pair<int64_t, int64_t>(original_vertex_id,
176  original_edge_id)] =
177  new_id;
178  auto v = add_vertex(this->graph);
179  this->graph[v].cp_members(original_vertex_id, original_edge_id);
180  this->graph[v].vertex_id = new_id;
181  this->vertices_map[new_id] = v;
182  }
183 
185  const pgrouting::DirectedGraph &digraph) {
186  E_i e_It, e_End;
187  for (boost::tie(e_It, e_End) = boost::edges(digraph.graph);
188  e_It != e_End; e_It++) {
189  m_edge_costs[digraph.graph[*e_It].id] = digraph.graph[*e_It].cost;
190  }
191  }
192 
193  template < typename T>
195  int64_t _id,
196  const T &source,
197  const T &target,
198  int64_t source_in_edge,
199  int64_t source_out_edge) {
200  bool inserted;
201  E e;
202 
203  pgassert(m_vertex_map.find({source, source_in_edge}) !=
204  m_vertex_map.end());
205  pgassert(m_vertex_map.find({target, source_out_edge}) !=
206  m_vertex_map.end());
207 
208  auto index_source_edge =
209  m_vertex_map[ std::pair<int64_t, int64_t>(source,
210  source_in_edge) ];
211  auto index_target_edge =
212  m_vertex_map[ std::pair<int64_t, int64_t>(target,
213  source_out_edge) ];
214 
215  auto vm_s = this->get_V(index_source_edge);
216  auto vm_t = this->get_V(index_target_edge);
217 
218  pgassert(this->vertices_map.find(index_source_edge) !=
219  this->vertices_map.end());
220  pgassert(this->vertices_map.find(index_target_edge) !=
221  this->vertices_map.end());
222 
223  boost::tie(e, inserted) =
224  boost::add_edge(vm_s, vm_t, this->graph);
225 
226  this->graph[e].id = _id;
227  }
228 
230  const pgrouting::DirectedGraph& digraph) {
231  V_i vertexIt, vertexEnd;
232  EO_i e_outIt, e_outEnd;
233  EI_i e_inIt, e_inEnd;
234 
235  // For every vertex in the original graph, create a line graph
236  // using the edges connected to that vertex
237  for (boost::tie(vertexIt, vertexEnd) = boost::vertices(digraph.graph);
238  vertexIt != vertexEnd; vertexIt++) {
239  V vertex = *vertexIt;
240  auto vertex_id = digraph[vertex].id;
241  for (boost::tie(e_outIt, e_outEnd) =
242  boost::out_edges(vertex, digraph.graph);
243  e_outIt != e_outEnd; e_outIt++) {
244  auto out_edge_id = digraph.graph[*e_outIt].id;
245  insert_vertex(vertex_id, out_edge_id);
246  }
247 
248  for (boost::tie(e_inIt, e_inEnd) =
249  boost::in_edges(vertex, digraph.graph);
250  e_inIt != e_inEnd; e_inIt++) {
251  auto in_edge_id = digraph.graph[*e_inIt].id;
252  insert_vertex(vertex_id, in_edge_id);
253 
254  for (boost::tie(e_outIt, e_outEnd) =
255  boost::out_edges(vertex, digraph.graph);
256  e_outIt != e_outEnd; e_outIt++) {
257  auto out_edge_id = digraph.graph[*e_outIt].id;
258 
259  ++m_num_edges;
260 
262  m_num_edges,
263  vertex_id,
264  vertex_id,
265  in_edge_id,
266  out_edge_id);
267  }
268  }
269  }
270 
271  // Connect all of the line graphs that were just created using the
272  // edges from the original graph
273  for (boost::tie(vertexIt, vertexEnd) =
274  boost::vertices(digraph.graph);
275  vertexIt != vertexEnd; vertexIt++) {
276  V vertex = *vertexIt;
277  auto vertex_id = digraph[vertex].id;
278 
279  for (boost::tie(e_inIt, e_inEnd) =
280  boost::in_edges(vertex, digraph.graph);
281  e_inIt != e_inEnd; e_inIt++) {
282  auto source_vertex_id = digraph[digraph.source(*e_inIt)].id;
283  auto in_edge_id = digraph.graph[*e_inIt].id;
284 
285  ++m_num_edges;
286 
288  m_num_edges,
289  source_vertex_id,
290  vertex_id,
291  in_edge_id,
292  in_edge_id);
293  }
294  }
295  }
296 
297  private:
298  int64_t m_num_edges;
299  std::map < int64_t, double > m_edge_costs;
300  std::map < int64_t, std::pair< int64_t, int64_t > > m_transformation_map;
301  std::map < std::pair< int64_t, int64_t >, int64_t > m_vertex_map;
302 
303  public:
304  std::ostringstream log;
305 };
306 } // namespace graph
307 } // namespace pgrouting
308 
309 #endif // INCLUDE_LINEGRAPH_PGR_LINEGRAPHFULL_HPP_
pgrouting::graph::Pgr_lineGraphFull::log
std::ostringstream log
Definition: pgr_lineGraphFull.hpp:304
pgrouting::graph::Pgr_lineGraphFull::m_num_edges
int64_t m_num_edges
Definition: pgr_lineGraphFull.hpp:298
pgrouting::graph::Pgr_lineGraphFull::insert_vertex
void insert_vertex(int64_t original_vertex_id, int64_t original_edge_id)
Definition: pgr_lineGraphFull.hpp:169
pgrouting::graph::Pgr_base_graph::graph
G graph
The graph.
Definition: pgr_base_graph.hpp:260
pgr_base_graph.hpp
pgrouting::graph::Pgr_lineGraphFull::V_i
boost::graph_traits< G >::vertex_iterator V_i
Definition: pgr_lineGraphFull.hpp:51
pgrouting::graph::Pgr_base_graph::target
V target(E e_idx) const
Definition: pgr_base_graph.hpp:561
pgrouting::graph::Pgr_base_graph::EI_i
boost::graph_traits< G >::in_edge_iterator EI_i
Definition: pgr_base_graph.hpp:234
pgrouting::graph::Pgr_lineGraphFull::store_edge_costs
void store_edge_costs(const pgrouting::DirectedGraph &digraph)
Definition: pgr_lineGraphFull.hpp:184
pgrouting::graph::Pgr_lineGraphFull::m_edge_costs
std::map< int64_t, double > m_edge_costs
Definition: pgr_lineGraphFull.hpp:299
edge
Definition: trsp.h:41
pgrouting::graph::Pgr_lineGraphFull::apply_transformation
void apply_transformation(const pgrouting::DirectedGraph &digraph)
Definition: pgr_lineGraphFull.hpp:229
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::graph::Pgr_lineGraphFull
Definition: pgr_lineGraphFull.hpp:47
pgrouting::graph::Pgr_lineGraphFull::m_transformation_map
std::map< int64_t, std::pair< int64_t, int64_t > > m_transformation_map
Definition: pgr_lineGraphFull.hpp:300
pgrouting::graph::Pgr_base_graph::get_V
V get_V(const T_V &vertex)
get the vertex descriptor of the vertex When the vertex does not exist
Definition: pgr_base_graph.hpp:512
pgrouting::graph::Pgr_base_graph::E_i
boost::graph_traits< G >::edge_iterator E_i
Definition: pgr_base_graph.hpp:232
pgrouting::graph::Pgr_base_graph::source
V source(E e_idx) const
Definition: pgr_base_graph.hpp:560
line_vertex.h
pgrouting::graph::Pgr_base_graph::E
boost::graph_traits< G >::edge_descriptor E
Definition: pgr_base_graph.hpp:230
pgrouting::graph::Pgr_lineGraphFull::E
boost::graph_traits< G >::edge_descriptor E
Definition: pgr_lineGraphFull.hpp:50
pgrouting::graph::Pgr_lineGraphFull::get_postgres_results_directed
std::vector< Line_graph_full_rt > get_postgres_results_directed()
Definition: pgr_lineGraphFull.hpp:89
pgrouting::graph::Pgr_base_graph::num_vertices
size_t num_vertices() const
Definition: pgr_base_graph.hpp:703
graphType
graphType
Definition: graph_enum.h:30
pgrouting::graph::Pgr_base_graph::EO_i
boost::graph_traits< G >::out_edge_iterator EO_i
Definition: pgr_base_graph.hpp:233
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
pgrouting::graph::Pgr_lineGraphFull::operator<<
friend std::ostream & operator<<(std::ostream &log, const Pgr_lineGraphFull< G, T_V, T_E > &g)
Definition: pgr_lineGraphFull.hpp:68
pgrouting::graph::Pgr_base_graph::V_i
boost::graph_traits< G >::vertex_iterator V_i
Definition: pgr_base_graph.hpp:231
pgrouting::graph::Pgr_lineGraphFull::EI_i
boost::graph_traits< G >::in_edge_iterator EI_i
Definition: pgr_lineGraphFull.hpp:54
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::graph::Pgr_base_graph::V
boost::graph_traits< G >::vertex_descriptor V
Definition: pgr_base_graph.hpp:229
pgrouting::graph::Pgr_lineGraphFull::V
boost::graph_traits< G >::vertex_descriptor V
Definition: pgr_lineGraphFull.hpp:49
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
pgrouting::graph::Pgr_lineGraphFull::E_i
boost::graph_traits< G >::edge_iterator E_i
Definition: pgr_lineGraphFull.hpp:52
pgrouting::graph::Pgr_lineGraphFull::EO_i
boost::graph_traits< G >::out_edge_iterator EO_i
Definition: pgr_lineGraphFull.hpp:53
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::graph::Pgr_lineGraphFull::m_vertex_map
std::map< std::pair< int64_t, int64_t >, int64_t > m_vertex_map
Definition: pgr_lineGraphFull.hpp:301
pgrouting::graph::Pgr_lineGraphFull::graph_add_edge
void graph_add_edge(int64_t _id, const T &source, const T &target, int64_t source_in_edge, int64_t source_out_edge)
Definition: pgr_lineGraphFull.hpp:194
Line_graph_full_rt
Definition: line_graph_full_rt.h:42
pgrouting::graph::Pgr_base_graph::vertices_map
id_to_V vertices_map
id -> graph id
Definition: pgr_base_graph.hpp:267