PGROUTING  2.6-dev
dijkstraVia_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: dijkstraViaVertex_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 
7 Function's developer:
8 Copyright (c) 2015 Celia Virginia Vergara Castillo
9 
10 ------
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 
26 ********************************************************************PGR-GNU*/
27 
29 
30 #include <sstream>
31 #include <deque>
32 #include <vector>
33 
35 
36 #include "cpp_common/pgr_alloc.hpp"
37 #include "cpp_common/pgr_assert.h"
38 
39 
40 static
41 void
43  int route_id,
44  int path_id,
45  const Path &path,
46  Routes_t **postgres_data,
47  double &route_cost,
48  size_t &sequence) {
49  int i = 0;
50  for (const auto e : path) {
51  (*postgres_data)[sequence] = {
52  route_id,
53  path_id,
54  i,
55  path.start_id(),
56  path.end_id(),
57  e.node,
58  e.edge,
59  e.cost,
60  e.agg_cost,
61  route_cost};
62  route_cost += path[i].cost;
63  ++i;
64  ++sequence;
65  }
66 }
67 
68 
69 static
70 size_t
72  Routes_t **ret_path,
73  const std::deque< Path > &paths) {
74  size_t sequence = 0;
75  int path_id = 1;
76  int route_id = 1;
77  double route_cost = 0; // routes_agg_cost
78  for (const Path &path : paths) {
79  if (path.size() > 0)
80  get_path(route_id, path_id, path, ret_path, route_cost, sequence);
81  ++path_id;
82  }
83  return sequence;
84 }
85 
86 void
88  pgr_edge_t* data_edges, size_t total_edges,
89  int64_t* via_vidsArr, size_t size_via_vidsArr,
90  bool directed,
91  bool strict,
92  bool U_turn_on_edge,
93  Routes_t** return_tuples, size_t* return_count,
94 
95  char** log_msg,
96  char** notice_msg,
97  char** err_msg) {
98  std::ostringstream log;
99  std::ostringstream err;
100  std::ostringstream notice;
101 
102  try {
103  pgassert(total_edges != 0);
104  pgassert(!(*log_msg));
105  pgassert(!(*notice_msg));
106  pgassert(!(*err_msg));
107  pgassert(!(*return_tuples));
108  pgassert(*return_count == 0);
109 
110  graphType gType = directed? DIRECTED: UNDIRECTED;
111 
112  std::deque< Path >paths;
113  log << "\nInserting vertices into a c++ vector structure";
114  std::vector< int64_t > via_vertices(
115  via_vidsArr, via_vidsArr + size_via_vidsArr);
116 
117  if (directed) {
118  log << "\nWorking with directed Graph";
119  pgrouting::DirectedGraph digraph(gType);
120  digraph.insert_edges(data_edges, total_edges);
122  digraph,
123  via_vertices,
124  paths,
125  strict,
126  U_turn_on_edge,
127  log);
128  } else {
129  log << "\nWorking with Undirected Graph";
130  pgrouting::UndirectedGraph undigraph(gType);
131  undigraph.insert_edges(data_edges, total_edges);
133  undigraph,
134  via_vertices,
135  paths,
136  strict,
137  U_turn_on_edge,
138  log);
139  }
140 
141  size_t count(count_tuples(paths));
142 
143  if (count == 0) {
144  (*return_tuples) = NULL;
145  (*return_count) = 0;
146  notice <<
147  "No paths found";
148  *log_msg = pgr_msg(notice.str().c_str());
149  return;
150  }
151 
152  // get the space required to store all the paths
153  (*return_tuples) = pgr_alloc(count, (*return_tuples));
154  log << "\nConverting a set of paths into the tuples";
155  (*return_count) = (get_route(return_tuples, paths));
156  (*return_tuples)[count - 1].edge = -2;
157 
158  *log_msg = log.str().empty()?
159  *log_msg :
160  pgr_msg(log.str().c_str());
161  *notice_msg = notice.str().empty()?
162  *notice_msg :
163  pgr_msg(notice.str().c_str());
164  } catch (AssertFailedException &except) {
165  (*return_tuples) = pgr_free(*return_tuples);
166  (*return_count) = 0;
167  err << except.what();
168  *err_msg = pgr_msg(err.str().c_str());
169  *log_msg = pgr_msg(log.str().c_str());
170  } catch (std::exception &except) {
171  (*return_tuples) = pgr_free(*return_tuples);
172  (*return_count) = 0;
173  err << except.what();
174  *err_msg = pgr_msg(err.str().c_str());
175  *log_msg = pgr_msg(log.str().c_str());
176  } catch(...) {
177  (*return_tuples) = pgr_free(*return_tuples);
178  (*return_count) = 0;
179  err << "Caught unknown exception!";
180  *err_msg = pgr_msg(err.str().c_str());
181  *log_msg = pgr_msg(log.str().c_str());
182  }
183 }
184 
185 
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void insert_edges(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
void do_pgr_dijkstraVia(pgr_edge_t *data_edges, size_t total_edges, int64_t *via_vidsArr, size_t size_via_vidsArr, bool directed, bool strict, bool U_turn_on_edge, Routes_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
static size_t get_route(Routes_t **ret_path, const std::deque< Path > &paths)
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
size_t count_tuples(const std::deque< Path > &paths)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53
graphType
Definition: graph_enum.h:30
static void get_path(int route_id, int path_id, const Path &path, Routes_t **postgres_data, double &route_cost, size_t &sequence)
void pgr_dijkstraVia(G &graph, const std::vector< int64_t > via_vertices, std::deque< Path > &paths, bool strict, bool U_turn_on_edge, std::ostringstream &log)