PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
withPoints_ksp_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: 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 
33 #include <sstream>
34 #include <deque>
35 #include <vector>
36 
37 #include "./pgr_ksp.hpp"
38 
39 #include "./../../withPoints/src/pgr_withPoints.hpp"
40 #include "./../../common/src/pgr_alloc.hpp"
41 #include "./../../common/src/pgr_assert.h"
42 
43 
44 // CREATE OR REPLACE FUNCTION pgr_withPointsKSP(
45 // edges_sql TEXT,
46 // points_sql TEXT,
47 // start_pid BIGINT,
48 // end_pid BIGINT,
49 // directed BOOLEAN DEFAULT true
50 
51 
52 int
54  pgr_edge_t *edges, size_t total_edges,
55  Point_on_edge_t *points_p, size_t total_points,
56  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
57  int64_t start_pid,
58  int64_t end_pid,
59  int k,
60  bool directed,
61  bool heap_paths,
62  char driving_side,
63  bool details,
64  General_path_element_t **return_tuples,
65  size_t *return_count,
66  char ** log_msg,
67  char ** notice_msg,
68  char ** err_msg) {
69 
70  std::ostringstream log;
71  std::ostringstream err;
72  std::ostringstream notice;
73  try {
74  pgassert(!(*log_msg));
75  pgassert(!(*notice_msg));
76  pgassert(!(*err_msg));
77  pgassert(!(*return_tuples));
78  pgassert(*return_count == 0);
79  pgassert(total_edges != 0);
80 
81  log << "entering do_pgr_withPointsKsp\n";
82 
83  std::vector< Point_on_edge_t >
84  points(points_p, points_p + total_points);
85 
86  log << "total points" << points.size() << "\n";
87 
88  auto errcode = check_points(points, log);
89  if (errcode) {
90  *log_msg = strdup(log.str().c_str());
91  err << "Unexpected point(s) with same pid but different edge/fraction/side combination found.";
92  *err_msg = strdup(err.str().c_str());
93  return -1;
94  }
95 
96  std::vector< pgr_edge_t >
97  edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points);
98 
99  std::vector< pgr_edge_t > new_edges;
100 
102  points,
103  edges_to_modify,
104  driving_side,
105  new_edges,
106  log);
107 
108 
109 
110  int64_t start_vid(start_pid);
111  int64_t end_vid(end_pid);
112 
113  log << "start_pid" << start_pid << "\n";
114  log << "end_pid" << end_pid << "\n";
115  log << "driving_side" << driving_side << "\n";
116  log << "start_vid" << start_vid << "\n";
117  log << "end_vid" << end_vid << "\n";
118  graphType gType = directed? DIRECTED: UNDIRECTED;
119 
120  std::deque< Path > paths;
121 
122  auto vertices(pgrouting::extract_vertices(edges, total_edges));
123  vertices = pgrouting::extract_vertices(vertices, new_edges);
124 
125  log << "extracted vertices: ";
126  for (const auto v : vertices) {
127  log << v.id << ", ";
128  }
129  log << "\n";
130 
131  if (directed) {
132  log << "Working with directed Graph\n";
133  pgrouting::DirectedGraph digraph(vertices, gType);
134  digraph.insert_edges(edges, total_edges);
135  log << "graph after inserting edges\n";
136  log << digraph << "\n";
137 
138  digraph.insert_edges(new_edges);
139  log << "graph after inserting new edges\n";
140  log << digraph << "\n";
141 
143  paths = fn_yen.Yen(digraph, start_vid, end_vid, k, heap_paths);
144  // pgassert(true==false);
145  } else {
146  log << "Working with undirected Graph\n";
147  pgrouting::UndirectedGraph undigraph(gType);
148  undigraph.insert_edges(edges, total_edges);
149  undigraph.insert_edges(new_edges);
151  paths = fn_yen.Yen(undigraph, start_vid, end_vid, k, heap_paths);
152  }
153 
154 
155  if (!details) {
156  for (auto &path : paths) {
157  eliminate_details(path, edges_to_modify);
158  }
159  }
160 
161  auto count(count_tuples(paths));
162 
163  if (count == 0) {
164  return 0;
165  }
166 
167 
168  *return_tuples = NULL;
169  *return_tuples = pgr_alloc(count, (*return_tuples));
170 
171  size_t sequence = 0;
172  int route_id = 0;
173  for (const auto &path : paths) {
174  if (path.size() > 0)
175  path.get_pg_ksp_path(return_tuples, sequence, route_id);
176  ++route_id;
177  }
178 
179  if (count != sequence) {
180  return 2;
181  }
182  (*return_count) = sequence;
183 
184 
185  *log_msg = strdup(log.str().c_str());
186  return 0;
187  } catch (AssertFailedException &except) {
188  if (*return_tuples) free(*return_tuples);
189  (*return_count) = 0;
190  err << except.what();
191  *err_msg = strdup(err.str().c_str());
192  *log_msg = strdup(log.str().c_str());
193  } catch (std::exception &except) {
194  if (*return_tuples) free(*return_tuples);
195  (*return_count) = 0;
196  err << except.what();
197  *err_msg = strdup(err.str().c_str());
198  *log_msg = strdup(log.str().c_str());
199  } catch(...) {
200  if (*return_tuples) free(*return_tuples);
201  (*return_count) = 0;
202  err << "Caught unknown exception!";
203  *err_msg = strdup(err.str().c_str());
204  *log_msg = strdup(log.str().c_str());
205  }
206  return 1000;
207 }
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.
int do_pgr_withPointsKsp(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_pid, int64_t end_pid, int k, bool directed, bool heap_paths, char driving_side, bool details, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
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
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
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
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
std::deque< Path > Yen(G &graph, int64_t source, int64_t target, int K, bool heap_paths)
Definition: pgr_ksp.cpp:51
char * err_msg
Definition: BDATester.cpp:50