PGROUTING  3.2
bdDijkstra_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bdDijkstra_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2016 Celia Virginia Vergara Castillo
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 
31 
32 #include <sstream>
33 #include <deque>
34 #include <vector>
35 #include <algorithm>
36 
38 
39 
40 #include "cpp_common/pgr_alloc.hpp"
41 #include "cpp_common/pgr_assert.h"
42 
44 
45 
46 
47 
48 template < class G >
49 static
50 std::deque<Path>
52  G &graph,
53  std::vector < pgr_combination_t > &combinations,
54  std::vector < int64_t > sources,
55  std::vector < int64_t > targets,
56 
57  std::ostream &log,
58  bool only_cost) {
59  log << "entering static function\n";
60 
62  std::deque<Path> paths;
63 
64  if (combinations.empty()) {
65  std::sort(sources.begin(), sources.end());
66  sources.erase(
67  std::unique(sources.begin(), sources.end()),
68  sources.end());
69 
70  std::sort(targets.begin(), targets.end());
71  targets.erase(
72  std::unique(targets.begin(), targets.end()),
73  targets.end());
74 
75  for (const auto source : sources) {
76  for (const auto target : targets) {
77  fn_bdDijkstra.clear();
78 
79  if (!graph.has_vertex(source)
80  || !graph.has_vertex(target)) {
81  paths.push_back(Path(source, target));
82  continue;
83  }
84 
85  paths.push_back(fn_bdDijkstra.pgr_bdDijkstra(
86  graph.get_V(source), graph.get_V(target), only_cost));
87  }
88  }
89 
90  } else {
91  std::sort(combinations.begin(), combinations.end(),
92  [](const pgr_combination_t &lhs, const pgr_combination_t &rhs)->bool {
93  return lhs.target < rhs.target;
94  });
95  std::stable_sort(combinations.begin(), combinations.end(),
96  [](const pgr_combination_t &lhs, const pgr_combination_t &rhs)->bool {
97  return lhs.source < rhs.source;
98  });
99 
100  pgr_combination_t previousCombination{0, 0};
101 
102  for (const pgr_combination_t &comb : combinations) {
103  fn_bdDijkstra.clear();
104  if (comb.source == previousCombination.source &&
105  comb.target == previousCombination.target) {
106  continue;
107  }
108 
109  if (!graph.has_vertex(comb.source)
110  || !graph.has_vertex(comb.target)) {
111  paths.push_back(Path(comb.source, comb.target));
112  continue;
113  }
114 
115  paths.push_back(fn_bdDijkstra.pgr_bdDijkstra(
116  graph.get_V(comb.source), graph.get_V(comb.target), only_cost));
117 
118  previousCombination = comb;
119  }
120  }
121 
122  log << fn_bdDijkstra.log();
123 
124  return paths;
125 }
126 
127 
128 void
130  pgr_edge_t *data_edges,
131  size_t total_edges,
132  pgr_combination_t *combinations,
133  size_t total_combinations,
134  int64_t *start_vidsArr,
135  size_t size_start_vidsArr,
136  int64_t *end_vidsArr,
137  size_t size_end_vidsArr,
138 
139  bool directed,
140  bool only_cost,
141 
142  General_path_element_t **return_tuples,
143  size_t *return_count,
144  char **log_msg,
145  char **notice_msg,
146  char **err_msg) {
147  std::ostringstream log;
148  std::ostringstream err;
149  std::ostringstream notice;
150  try {
151  pgassert(total_edges != 0);
152  pgassert(!(*log_msg));
153  pgassert(!(*notice_msg));
154  pgassert(!(*err_msg));
155  pgassert(!(*return_tuples));
156  pgassert(*return_count == 0);
157 
158  log << "Inserting vertices into a c++ vector structure";
159  std::vector<int64_t>
160  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
161  std::vector< int64_t >
162  end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
163  std::vector< pgr_combination_t >
164  combinations_vector(combinations, combinations + total_combinations);
165 
166  graphType gType = directed? DIRECTED: UNDIRECTED;
167 
168  std::deque<Path> paths;
169 
170  log << "starting process\n";
171  if (directed) {
172  log << "Working with directed Graph\n";
173  pgrouting::DirectedGraph digraph(gType);
174  digraph.insert_edges(data_edges, total_edges);
175  paths = pgr_bdDijkstra(digraph,
176  combinations_vector,
177  start_vertices,
178  end_vertices,
179  log,
180  only_cost);
181  } else {
182  log << "Working with Undirected Graph\n";
183  pgrouting::UndirectedGraph undigraph(gType);
184  undigraph.insert_edges(data_edges, total_edges);
185  paths = pgr_bdDijkstra(
186  undigraph,
187  combinations_vector,
188  start_vertices,
189  end_vertices,
190  log,
191  only_cost);
192  }
193 
194  size_t count(0);
195  count = count_tuples(paths);
196 
197  if (count == 0) {
198  (*return_tuples) = NULL;
199  (*return_count) = 0;
200  notice <<
201  "No paths found";
202  *log_msg = pgr_msg(notice.str().c_str());
203  return;
204  }
205 
206  (*return_tuples) = pgr_alloc(count, (*return_tuples));
207  log << "\nConverting a set of paths into the tuples";
208  (*return_count) = (collapse_paths(return_tuples, paths));
209 
210  pgassert(*err_msg == NULL);
211  *log_msg = log.str().empty()?
212  *log_msg :
213  pgr_msg(log.str().c_str());
214  *notice_msg = notice.str().empty()?
215  *notice_msg :
216  pgr_msg(notice.str().c_str());
217  } catch (AssertFailedException &except) {
218  (*return_tuples) = pgr_free(*return_tuples);
219  (*return_count) = 0;
220  err << except.what();
221  *err_msg = pgr_msg(err.str().c_str());
222  *log_msg = pgr_msg(log.str().c_str());
223  } catch (std::exception &except) {
224  (*return_tuples) = pgr_free(*return_tuples);
225  (*return_count) = 0;
226  err << except.what();
227  *err_msg = pgr_msg(err.str().c_str());
228  *log_msg = pgr_msg(log.str().c_str());
229  } catch(...) {
230  (*return_tuples) = pgr_free(*return_tuples);
231  (*return_count) = 0;
232  err << "Caught unknown exception!";
233  *err_msg = pgr_msg(err.str().c_str());
234  *log_msg = pgr_msg(log.str().c_str());
235  }
236 }
Path
Definition: basePath_SSEC.hpp:47
do_pgr_bdDijkstra
void do_pgr_bdDijkstra(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: bdDijkstra_driver.cpp:129
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgr_base_graph.hpp
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
pgr_bdDijkstra.hpp
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgrouting::graph::Pgr_base_graph::insert_edges
void insert_edges(const T *edges, size_t count)
Inserts count edges of type T into the graph.
Definition: pgr_base_graph.hpp:357
pgr_alloc.hpp
pgr_combination_t
Definition: pgr_combination_t.h:43
bdDijkstra_driver.h
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
General_path_element_t
Definition: general_path_element_t.h:37
pgrouting::bidirectional::Pgr_bdDijkstra
Definition: pgr_bdDijkstra.hpp:49
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgr_bdDijkstra
static std::deque< Path > pgr_bdDijkstra(G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets, std::ostream &log, bool only_cost)
Definition: bdDijkstra_driver.cpp:51
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
pgrouting::bidirectional::Pgr_bidirectional::clear
void clear()
Definition: pgr_bidirectional.hpp:86
pgrouting::bidirectional::Pgr_bdDijkstra::pgr_bdDijkstra
Path pgr_bdDijkstra(V start_vertex, V end_vertex, bool only_cost)
Definition: pgr_bdDijkstra.hpp:82
pgrouting::bidirectional::Pgr_bidirectional::log
std::string log() const
Definition: pgr_bidirectional.hpp:84
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139