PGROUTING  3.2
pgr_linearContraction.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_linear.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2016 Rohith Reddy
10 Mail:
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_CONTRACTION_PGR_LINEARCONTRACTION_HPP_
31 #define INCLUDE_CONTRACTION_PGR_LINEARCONTRACTION_HPP_
32 #pragma once
33 
34 
35 #include <boost/graph/iteration_macros.hpp>
36 #include <boost/graph/filtered_graph.hpp>
37 
38 #include <queue>
39 #include <functional>
40 #include <vector>
41 
43 
44 
45 namespace pgrouting {
46 namespace contraction {
47 
48 template < class G >
49 class Pgr_linear {
50  private:
51  typedef typename G::V V;
52  typedef typename G::V_i V_i;
53  typedef typename G::B_G B_G;
54 
55 
56  public:
57  void operator()(G &graph, Identifiers<V>& forbidden_vertices) {
58  doContraction(graph, forbidden_vertices);
59  }
60 
62 
63  private:
64  int64_t get_next_id() {
65  return --last_edge_id;
66  }
67 
68 
69  public:
71  Identifiers<V> forbidden_vertices) {
72  m_forbiddenVertices = forbidden_vertices;
73  }
74 
75  bool is_contractible(G &graph, V v) {
76  return graph.is_linear(v) && !m_forbiddenVertices.has(v);
77  }
78 
79  void calculateVertices(G &graph) {
81  V_i vi;
82  BGL_FORALL_VERTICES_T(v, graph.graph, B_G) {
83  if (is_contractible(graph, v)) {
84  m_linearVertices += v;
85  }
86  }
87  }
88 
89 
90 
91  void doContraction(G &graph, Identifiers<V> forbidden_vertices) {
92  m_forbiddenVertices = forbidden_vertices;
93  calculateVertices(graph);
94 
95  while (!m_linearVertices.empty()) {
96  V v = m_linearVertices.front();
97  m_linearVertices -= v;
98  pgassert(is_contractible(graph, v));
99  one_cycle(graph, v);
100  }
101  }
102 
103  void one_cycle(G &graph, V v) {
104  pgassert(is_contractible(graph, v));
105 
106  Identifiers<V> adjacent_vertices =
107  graph.find_adjacent_vertices(v);
108  pgassert(adjacent_vertices.size() == 2);
109 
110  V u = adjacent_vertices.front();
111  adjacent_vertices.pop_front();
112  V w = adjacent_vertices.front();
113  adjacent_vertices.pop_front();
114 
115  pgassert(v != u);
116  pgassert(v != w);
117  pgassert(u != w);
118 
119  if (graph.is_directed()) {
120  /*
121  * u --> v --> w
122  */
123  process_shortcut(graph, u, v, w);
124  /*
125  * w --> v --> u
126  */
127  process_shortcut(graph, w, v, u);
128 
129  } else {
130  pgassert(graph.is_undirected());
131  /*
132  * u - v - w
133  */
134  process_shortcut(graph, u, v, w);
135  }
136 
137  graph[v].contracted_vertices().clear();
138  boost::clear_vertex(v, graph.graph);
139  m_linearVertices -= v;
140 
141  if (is_contractible(graph, u)) {
142  one_cycle(graph, u);
143  } else {
144  m_linearVertices -= u;
145  }
146  if (is_contractible(graph, w)) {
147  one_cycle(graph, w);
148  } else {
149  m_linearVertices -= w;
150  }
151  }
152 
153 
154 
166  void process_shortcut(G &graph, V u, V v, V w) {
167  auto e1 = graph.get_min_cost_edge(u, v);
168  auto e2 = graph.get_min_cost_edge(v, w);
169 
170  if (std::get<2>(e1) && std::get<2>(e2)) {
171  auto contracted_vertices = std::get<1>(e1) + std::get<1>(e2);
172  double cost = std::get<0>(e1) + std::get<0>(e2);
173  contracted_vertices += graph[v].id;
174  contracted_vertices += graph[v].contracted_vertices();
175 
176  // Create shortcut
177  CH_edge shortcut(
178  get_next_id(),
179  graph[u].id,
180  graph[w].id,
181  cost);
182  shortcut.contracted_vertices() = contracted_vertices;
183 
184  graph.add_shortcut(shortcut, u, w);
185  }
186  }
187 
188 
189  private:
192 
193  int64_t last_edge_id;
194 };
195 
196 } // namespace contraction
197 } // namespace pgrouting
198 
199 #endif // INCLUDE_CONTRACTION_PGR_LINEARCONTRACTION_HPP_
pgrouting::contraction::Pgr_linear::m_linearVertices
Identifiers< V > m_linearVertices
Definition: pgr_linearContraction.hpp:190
pgrouting::contraction::Pgr_linear::is_contractible
bool is_contractible(G &graph, V v)
Definition: pgr_linearContraction.hpp:75
pgrouting::contraction::Pgr_linear
Definition: pgr_linearContraction.hpp:49
Identifiers::pop_front
void pop_front()
Definition: identifiers.hpp:83
pgrouting::contraction::Pgr_linear::V_i
G::V_i V_i
Definition: pgr_linearContraction.hpp:52
pgrouting::contraction::Pgr_linear::B_G
G::B_G B_G
Definition: pgr_linearContraction.hpp:53
pgrouting::contraction::Pgr_linear::calculateVertices
void calculateVertices(G &graph)
Definition: pgr_linearContraction.hpp:79
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
Identifiers::size
size_t size() const
Definition: identifiers.hpp:78
Identifiers::clear
void clear()
Definition: identifiers.hpp:84
pgrouting::contraction::Pgr_linear::last_edge_id
int64_t last_edge_id
Definition: pgr_linearContraction.hpp:193
pgrouting::contraction::Pgr_linear::doContraction
void doContraction(G &graph, Identifiers< V > forbidden_vertices)
Definition: pgr_linearContraction.hpp:91
pgrouting::contraction::Pgr_linear::V
G::V V
Definition: pgr_linearContraction.hpp:51
pgrouting::contraction::Pgr_linear::get_next_id
int64_t get_next_id()
Definition: pgr_linearContraction.hpp:64
Identifiers::empty
bool empty() const
Definition: identifiers.hpp:79
pgrouting::contraction::Pgr_linear::process_shortcut
void process_shortcut(G &graph, V u, V v, V w)
u -—e1{v1}-—> v -—e2{v2}-—> w
Definition: pgr_linearContraction.hpp:166
pgrouting::contraction::Pgr_linear::m_forbiddenVertices
Identifiers< V > m_forbiddenVertices
Definition: pgr_linearContraction.hpp:191
pgrouting::contraction::Pgr_linear::setForbiddenVertices
void setForbiddenVertices(Identifiers< V > forbidden_vertices)
Definition: pgr_linearContraction.hpp:70
pgrouting::contraction::Pgr_linear::operator()
void operator()(G &graph, Identifiers< V > &forbidden_vertices)
Definition: pgr_linearContraction.hpp:57
Identifiers::front
T front() const
Definition: identifiers.hpp:80
pgrouting::CH_edge::contracted_vertices
const Identifiers< int64_t > & contracted_vertices() const
Definition: ch_edge.cpp:50
Identifiers::has
bool has(const T other) const
true ids() has element
Definition: identifiers.hpp:98
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::contraction::Pgr_linear::Pgr_linear
Pgr_linear()
Definition: pgr_linearContraction.hpp:61
pgrouting::alphashape::V
boost::graph_traits< BG >::vertex_descriptor V
Definition: pgr_alphaShape.h:58
pgrouting::CH_edge
Definition: ch_edge.h:41
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
identifiers.hpp
Identifiers< V >
pgrouting::contraction::Pgr_linear::one_cycle
void one_cycle(G &graph, V v)
Definition: pgr_linearContraction.hpp:103