PGROUTING  3.2
pgr_lineGraph.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_lineGraph.hpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2017 Vidhan Jain
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_LINEGRAPH_HPP_
31 #define INCLUDE_LINEGRAPH_PGR_LINEGRAPH_HPP_
32 #pragma once
33 
34 
35 #include <vector>
36 #include <set>
37 #include <utility>
38 #include <map>
39 #include <algorithm>
40 
42 #include "cpp_common/line_vertex.h"
43 
44 namespace pgrouting {
45 
46 namespace graph {
47 
48 template <class G, typename T_V, typename T_E>
49 class Pgr_lineGraph : public Pgr_base_graph<G, T_V, T_E> {
50  public:
51  typedef typename boost::graph_traits < G >::vertex_descriptor V;
52  typedef typename boost::graph_traits < G >::edge_descriptor E;
53  typedef typename boost::graph_traits < G >::vertex_iterator V_i;
54  typedef typename boost::graph_traits < G >::out_edge_iterator EO_i;
55  typedef typename boost::graph_traits < G >::in_edge_iterator EI_i;
56 
57 
59  : Pgr_base_graph< G, T_V, T_E >(gtype) {
60  }
61 
63  : Pgr_base_graph< G, T_V, T_E >(graphType::DIRECTED) {
64  insert_vertices(digraph);
65  create_edges(digraph);
66  }
67 
68 
69  friend std::ostream& operator<<(
70  std::ostream &log, const Pgr_lineGraph< G, T_V, T_E > &g) {
71  typename Pgr_base_graph< G, T_V, T_E >::EO_i out, out_end;
72 
73  for (auto vi = vertices(g.graph).first;
74  vi != vertices(g.graph).second; ++vi) {
75  if ((*vi) >= g.num_vertices()) break;
76  log << (*vi) << ": " << " out_edges_of(" << g.graph[(*vi)] << "):";
77  for (boost::tie(out, out_end) = out_edges(*vi, g.graph);
78  out != out_end; ++out) {
79  log << ' '
80  << g.graph[*out].id << "=("
81  << g[g.source(*out)].id << ", "
82  << g[g.target(*out)].id << ")\t";
83  }
84  log << std::endl;
85  }
86  return log;
87  }
88 
89 
90  std::vector< Line_graph_rt >
92  std::vector< Line_graph_rt > results;
93 
94  typename boost::graph_traits < G >::edge_iterator edgeIt, edgeEnd;
95  std::map < std::pair<int64_t, int64_t >, Line_graph_rt > unique;
96  int64_t count = 0;
97 
98  for (boost::tie(edgeIt, edgeEnd) = boost::edges(this->graph);
99  edgeIt != edgeEnd; edgeIt++) {
100  E e = *edgeIt;
101  auto e_source = this->graph[this->source(e)].vertex_id;
102  auto e_target = this->graph[this->target(e)].vertex_id;
103 
104  if (unique.find({e_target, e_source}) != unique.end()) {
105  unique[std::pair<int64_t, int64_t>(e_target,
106  e_source)].reverse_cost = 1.0;
107  continue;
108  }
109  e_source *= -1;
110  e_target *= -1;
111  if (unique.find({e_target, e_source}) != unique.end()) {
112  unique[std::pair<int64_t, int64_t>(e_target,
113  e_source)].reverse_cost = 1.0;
114  continue;
115  }
116  e_source *= -1;
117  e_target *= -1;
118 
119  Line_graph_rt edge = {
120  ++count,
121  e_source,
122  e_target,
123  1.0,
124  -1.0
125  };
126  unique[std::pair<int64_t, int64_t>(e_source, e_target)] = edge;
127  }
128  for (const auto &edge : unique) {
129  results.push_back(edge.second);
130  }
131  return results;
132  }
133 
135  const pgrouting::DirectedGraph& digraph) {
136  auto es = boost::edges(digraph.graph);
137  for (auto eit = es.first; eit != es.second; ++eit) {
138  auto edge = *eit;
139  Line_vertex vertex({
140  digraph[edge].id,
141  digraph[boost::source(edge, digraph.graph)].id,
142  digraph[boost::target(edge, digraph.graph)].id,
143  digraph[edge].cost,
144  -1});
145  add_one_vertex(vertex);
146  }
147  }
148 
149 
150 
151  private:
152  template < typename T>
153  void
155  const T &source,
156  const T &target) {
157  bool inserted;
158  E e;
159 
160  auto vm_s = this->get_V(source);
161  auto vm_t = this->get_V(target);
162 
163  boost::tie(e, inserted) =
164  boost::add_edge(vm_s, vm_t, this->graph);
165 
166  this->graph[e].id = static_cast<int64_t>(this->num_edges());
167  }
168 
170  const pgrouting::DirectedGraph& digraph) {
171  V_i vertexIt, vertexEnd;
172  EO_i e_outIt, e_outEnd;
173  EI_i e_inIt, e_inEnd;
174 
175 
176  /* for (each vertex v in original graph) */
177  for (boost::tie(vertexIt, vertexEnd) = boost::vertices(digraph.graph);
178  vertexIt != vertexEnd; vertexIt++) {
179  auto vertex = *vertexIt;
180 
181  /* for( all incoming edges in to vertex v) */
182  for (boost::tie(e_outIt, e_outEnd) =
183  boost::out_edges(vertex, digraph.graph);
184  e_outIt != e_outEnd;
185  e_outIt++) {
186  /* for( all outgoing edges out from vertex v) */
187  for (boost::tie(e_inIt, e_inEnd) =
188  boost::in_edges(vertex, digraph.graph);
189  e_inIt != e_inEnd; e_inIt++) {
190  /*
191  Prevent self-edges from being created in the Line Graph
192  */
193 
195  (digraph.graph[*e_inIt]).id,
196  (digraph.graph[*e_outIt]).id);
197  }
198  }
199  }
200  }
201 
203  T_V vertex) {
204  auto v = add_vertex(this->graph);
205  this->vertices_map[vertex.id] = v;
206  this->graph[v].cp_members(vertex);
207 
208  pgassert(boost::num_vertices(this->graph) == this->num_vertices());
209  return v;
210  }
211 
212  private:
213  std::map < int64_t, pgr_edge_t > m_edges;
214 
215  public:
216  std::ostringstream log;
217 };
218 } // namespace graph
219 } // namespace pgrouting
220 
221 #endif // INCLUDE_LINEGRAPH_PGR_LINEGRAPH_HPP_
pgrouting::graph::Pgr_base_graph::graph
G graph
The graph.
Definition: pgr_base_graph.hpp:260
pgr_base_graph.hpp
pgrouting::graph::Pgr_lineGraph::get_postgres_results_directed
std::vector< Line_graph_rt > get_postgres_results_directed()
Definition: pgr_lineGraph.hpp:91
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_base_graph::num_edges
size_t num_edges() const
Definition: pgr_base_graph.hpp:704
pgrouting::graph::Pgr_lineGraph::m_edges
std::map< int64_t, pgr_edge_t > m_edges
Definition: pgr_lineGraph.hpp:213
edge
Definition: trsp.h:41
Line_graph_rt
Definition: line_graph_rt.h:42
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::graph::Pgr_lineGraph::E
boost::graph_traits< G >::edge_descriptor E
Definition: pgr_lineGraph.hpp:52
pgrouting::graph::Pgr_lineGraph::EO_i
boost::graph_traits< G >::out_edge_iterator EO_i
Definition: pgr_lineGraph.hpp:54
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_lineGraph
Definition: pgr_lineGraph.hpp:49
pgrouting::graph::Pgr_lineGraph::add_one_vertex
V add_one_vertex(T_V vertex)
Definition: pgr_lineGraph.hpp:202
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_lineGraph::log
std::ostringstream log
Definition: pgr_lineGraph.hpp:216
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_base_graph::V_i
boost::graph_traits< G >::vertex_iterator V_i
Definition: pgr_base_graph.hpp:231
pgrouting::graph::Pgr_lineGraph::V
boost::graph_traits< G >::vertex_descriptor V
Definition: pgr_lineGraph.hpp:51
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::graph::Pgr_lineGraph::operator<<
friend std::ostream & operator<<(std::ostream &log, const Pgr_lineGraph< G, T_V, T_E > &g)
Definition: pgr_lineGraph.hpp:69
pgrouting::graph::Pgr_base_graph::V
boost::graph_traits< G >::vertex_descriptor V
Definition: pgr_base_graph.hpp:229
pgrouting::Line_vertex
Definition: line_vertex.h:45
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
pgrouting::graph::Pgr_lineGraph::V_i
boost::graph_traits< G >::vertex_iterator V_i
Definition: pgr_lineGraph.hpp:53
pgrouting::graph::Pgr_lineGraph::create_edges
void create_edges(const pgrouting::DirectedGraph &digraph)
Definition: pgr_lineGraph.hpp:169
pgrouting::graph::Pgr_lineGraph::graph_add_edge
void graph_add_edge(const T &source, const T &target)
Definition: pgr_lineGraph.hpp:154
pgrouting::graph::Pgr_lineGraph::EI_i
boost::graph_traits< G >::in_edge_iterator EI_i
Definition: pgr_lineGraph.hpp:55
pgrouting::graph::Pgr_lineGraph::insert_vertices
void insert_vertices(const pgrouting::DirectedGraph &digraph)
Definition: pgr_lineGraph.hpp:134
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::graph::Pgr_base_graph::vertices_map
id_to_V vertices_map
id -> graph id
Definition: pgr_base_graph.hpp:267