PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
withPoints_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: one_to_many_withPoints_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
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 
31 
32 #include <algorithm>
33 #include <sstream>
34 #include <deque>
35 #include <vector>
36 #include <cassert>
37 
38 
40 #include "./pgr_withPoints.hpp"
41 
42 #include "cpp_common/pgr_alloc.hpp"
43 #include "cpp_common/pgr_assert.h"
44 
45 template < class G >
46 std::deque< Path >
48  G &graph,
49  std::vector < int64_t > sources,
50  std::vector < int64_t > targets,
51  bool only_cost,
52  bool normal) {
53  std::sort(sources.begin(), sources.end());
54  sources.erase(
55  std::unique(sources.begin(), sources.end()),
56  sources.end());
57 
58  std::sort(targets.begin(), targets.end());
59  targets.erase(
60  std::unique(targets.begin(), targets.end()),
61  targets.end());
62 
63  Pgr_dijkstra< G > fn_dijkstra;
64  auto paths = fn_dijkstra.dijkstra(graph, sources, targets, only_cost);
65 
66  if (!normal) {
67  for (auto &path : paths) {
68  path.reverse();
69  }
70  }
71  return paths;
72 }
73 
74 
75 // CREATE OR REPLACE FUNCTION pgr_withPoint(
76 // edges_sql TEXT,
77 // points_sql TEXT,
78 // start_pid ANYARRAY,
79 // end_pid BIGINT,
80 // directed BOOLEAN DEFAULT true
81 
82 void
84  pgr_edge_t *edges, size_t total_edges,
85  Point_on_edge_t *points_p, size_t total_points,
86  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
87  int64_t *start_pidsArr, size_t size_start_pidsArr,
88  int64_t *end_pidsArr, size_t size_end_pidsArr,
89 
90  char driving_side,
91  bool details,
92  bool directed,
93  bool only_cost,
94  bool normal,
95 
96  General_path_element_t **return_tuples, size_t *return_count,
97 
98  char** log_msg,
99  char** notice_msg,
100  char** err_msg) {
101  std::ostringstream log;
102  std::ostringstream notice;
103  std::ostringstream err;
104  try {
105  pgassert(!(*log_msg));
106  pgassert(!(*notice_msg));
107  pgassert(!(*err_msg));
108  pgassert(!(*return_tuples));
109  pgassert((*return_count) == 0);
110  pgassert(edges || edges_of_points);
111  pgassert(points_p);
112  pgassert(start_pidsArr);
113  pgassert(end_pidsArr);
114 
115  std::vector< Point_on_edge_t >
116  points(points_p, points_p + total_points);
117 
118  if (!normal) {
119  for (auto &point : points) {
120  if (point.side == 'r') {
121  point.side = 'l';
122  } else if (point.side == 'l') {
123  point.side = 'r';
124  }
125  point.fraction = 1 - point.fraction;
126  }
127  if (driving_side == 'r') {
128  driving_side = 'l';
129  } else if (driving_side == 'l') {
130  driving_side = 'r';
131  }
132  }
133 
134  int errcode = check_points(points, log);
135  if (errcode) {
136  *log_msg = strdup(log.str().c_str());
137  err << "Unexpected point(s) with same pid"
138  << " but different edge/fraction/side combination found.";
139  *err_msg = pgr_msg(err.str().c_str());
140  return;
141  }
142 
143 
144  std::vector< pgr_edge_t >
145  edges_to_modify(
146  edges_of_points, edges_of_points + total_edges_of_points);
147 
148  std::vector< pgr_edge_t > new_edges;
150  points,
151  edges_to_modify,
152  driving_side,
153  new_edges, log);
154 
155 
156  std::vector<int64_t>
157  start_vertices(start_pidsArr, start_pidsArr + size_start_pidsArr);
158  std::vector< int64_t >
159  end_vertices(end_pidsArr, end_pidsArr + size_end_pidsArr);
160 
161  auto vertices(pgrouting::extract_vertices(edges, total_edges));
162  vertices = pgrouting::extract_vertices(vertices, new_edges);
163 
164  graphType gType = directed? DIRECTED: UNDIRECTED;
165 
166  std::deque< Path > paths;
167 
168  if (directed) {
169  log << "Working with directed Graph\n";
170  pgrouting::DirectedGraph digraph(vertices, gType);
171  digraph.insert_edges(edges, total_edges);
172  digraph.insert_edges(new_edges);
173 
174  paths = pgr_dijkstra(
175  digraph,
176  start_vertices, end_vertices,
177  only_cost, normal);
178  } else {
179  log << "Working with Undirected Graph\n";
180  pgrouting::UndirectedGraph undigraph(vertices, gType);
181  undigraph.insert_edges(edges, total_edges);
182  undigraph.insert_edges(new_edges);
183  paths = pgr_dijkstra(
184  undigraph,
185  start_vertices, end_vertices,
186  only_cost, normal);
187  }
188 
189  if (!details) {
190  for (auto &path : paths) {
191  eliminate_details(path, edges_to_modify);
192  }
193  }
194 
195  /*
196  * order paths based on the start_pid, end_pid
197  */
198  std::sort(paths.begin(), paths.end(),
199  [](const Path &a, const Path &b)
200  -> bool {
201  if (b.start_id() != a.start_id()) {
202  return a.start_id() < b.start_id();
203  }
204  return a.end_id() < b.end_id();
205  });
206 
207  size_t count(0);
208  count = count_tuples(paths);
209 
210 
211  if (count == 0) {
212  (*return_tuples) = NULL;
213  (*return_count) = 0;
214 #if 0
215  log <<
216  "No paths found";
217  *err_msg = pgr_msg(log.str().c_str());
218 #endif
219  return;
220  }
221 
222  (*return_tuples) = pgr_alloc(count, (*return_tuples));
223  log << "Converting a set of paths into the tuples\n";
224  (*return_count) = (collapse_paths(return_tuples, paths));
225 
226  *log_msg = log.str().empty()?
227  *log_msg :
228  pgr_msg(log.str().c_str());
229  *notice_msg = notice.str().empty()?
230  *notice_msg :
231  pgr_msg(notice.str().c_str());
232  } catch (AssertFailedException &except) {
233  (*return_tuples) = pgr_free(*return_tuples);
234  (*return_count) = 0;
235  err << except.what();
236  *err_msg = pgr_msg(err.str().c_str());
237  *log_msg = pgr_msg(log.str().c_str());
238  } catch (std::exception &except) {
239  (*return_tuples) = pgr_free(*return_tuples);
240  (*return_count) = 0;
241  err << except.what();
242  *err_msg = pgr_msg(err.str().c_str());
243  *log_msg = pgr_msg(log.str().c_str());
244  } catch(...) {
245  (*return_tuples) = pgr_free(*return_tuples);
246  (*return_count) = 0;
247  err << "Caught unknown exception!";
248  *err_msg = pgr_msg(err.str().c_str());
249  *log_msg = pgr_msg(log.str().c_str());
250  }
251 }
static edge_t edges[22573]
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void eliminate_details(Path &path, const std::vector< pgr_edge_t > &point_edges)
void insert_edges(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
std::deque< Path > pgr_dijkstra(G &graph, std::vector< int64_t > sources, std::vector< int64_t > targets, bool only_cost, bool normal)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:75
int64_t end_id() const
int check_points(std::vector< Point_on_edge_t > &points, std::ostringstream &log)
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
void do_pgr_withPoints(pgr_edge_t *edges, size_t total_edges, Point_on_edge_t *points_p, size_t total_points, pgr_edge_t *edges_of_points, size_t total_edges_of_points, int64_t *start_pidsArr, size_t size_start_pidsArr, int64_t *end_pidsArr, size_t size_end_pidsArr, char driving_side, bool details, bool directed, bool only_cost, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
void reverse()
Path dijkstra(G &graph, int64_t start_vertex, int64_t end_vertex, bool only_cost=false)
Dijkstra 1 to 1.
bool create_new_edges(std::vector< Point_on_edge_t > &points, const std::vector< pgr_edge_t > &edges, char driving_side, std::vector< pgr_edge_t > &new_edges)
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
int64_t start_id() const
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:29
size_t count_tuples(const std::deque< Path > &paths)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:64
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53
graphType
Definition: graph_enum.h:30