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