PGROUTING  3.2
turnRestrictedPath_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: turnRestrictedPath_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2017 Vidhan Jain
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 <string>
36 
37 #include "cpp_common/pgr_alloc.hpp"
38 #include "cpp_common/pgr_assert.h"
39 
40 #include "cpp_common/rule.h"
41 
44 
47 
48 template < class G >
49 static
50 std::deque<Path>
52  G &graph,
53  const std::vector<pgrouting::trsp::Rule> &restrictions,
54  int64_t source,
55  int64_t target,
56  std::string& log,
57  size_t k,
58  bool heap_paths,
59  bool stop_on_first,
60  bool strict) {
62 
63  auto paths = fn_TRSP.turnRestrictedPath(graph,
64  restrictions,
65  source,
66  target,
67  k,
68  heap_paths,
69  stop_on_first,
70  strict);
71 
72  log += fn_TRSP.get_log();
73  return paths;
74 }
75 
76 void
78  pgr_edge_t *data_edges,
79  size_t total_edges,
80 
81  Restriction_t *restrictions,
82  size_t total_restrictions,
83 
84  int64_t start_vid,
85  int64_t end_vid,
86 
87  size_t k,
88  bool directed,
89  bool heap_paths,
90  bool stop_on_first,
91  bool strict,
92 
93  General_path_element_t **return_tuples,
94  size_t *return_count,
95 
96  char ** log_msg,
97  char ** notice_msg,
98  char ** err_msg) {
99  std::ostringstream log;
100  std::ostringstream err;
101  std::ostringstream notice;
102  try {
103  pgassert(!(*log_msg));
104  pgassert(!(*notice_msg));
105  pgassert(!(*err_msg));
106  pgassert(!(*return_tuples));
107  pgassert(*return_count == 0);
108  pgassert(total_edges != 0);
109 
110  std::vector<pgrouting::trsp::Rule> ruleList;
111  for (size_t i = 0; i < total_restrictions; ++i) {
112  ruleList.push_back(Rule(*(restrictions + i)));
113  }
114 
115  log << "\n---------------------------------------\nRestrictions data\n";
116  for (const auto &r : ruleList) {
117  log << r << "\n";
118  }
119  log <<"------------------------------------------------------------\n";
120 
121  graphType gType = directed? DIRECTED: UNDIRECTED;
122 
123  std::vector < pgr_edge_t > edges(data_edges, data_edges + total_edges);
124 
125  std::deque<Path> paths;
126 
127  std::string logstr;
128  if (directed) {
129  log << "Working with directed Graph\n";
130  pgrouting::DirectedGraph digraph(gType);
132  digraph.insert_edges(edges);
133  log << digraph;
134  paths = pgr_dijkstraTR(digraph,
135  ruleList,
136 
137  start_vid,
138  end_vid,
139 
140  logstr,
141  k,
142  heap_paths,
143  stop_on_first,
144  strict);
145  } else {
146  log << "TODO Working with Undirected Graph\n";
147  pgrouting::UndirectedGraph undigraph(gType);
149  undigraph.insert_edges(data_edges, total_edges);
150  paths = pgr_dijkstraTR(undigraph,
151  ruleList,
152 
153  start_vid,
154  end_vid,
155 
156  logstr,
157 
158  k,
159  heap_paths,
160  stop_on_first,
161  strict);
162  }
163 
164  log << logstr;
165 
166  auto count(count_tuples(paths));
167  log << "\nCount = " << count;
168 
169  if (!(count == 0)) {
170  *return_tuples = NULL;
171  *return_tuples = pgr_alloc(count, (*return_tuples));
172 
173  size_t sequence = 0;
174  int route_id = 0;
175  for (const auto &path : paths) {
176  if (path.size() > 0) {
177  path.get_pg_turn_restricted_path(
178  return_tuples,
179  sequence,
180  route_id);
181  }
182  log << "the agg cost" << (*return_tuples)[0].agg_cost;
183  ++route_id;
184  }
185  }
186  *return_count = count;
187 
188 
189  pgassert(*err_msg == NULL);
190  *log_msg = log.str().empty()?
191  *log_msg :
192  pgr_msg(log.str().c_str());
193  *notice_msg = notice.str().empty()?
194  *notice_msg :
195  pgr_msg(notice.str().c_str());
196  pgassert(!log.str().empty());
197  } catch (AssertFailedException &except) {
198  (*return_tuples) = pgr_free(*return_tuples);
199  (*return_count) = 0;
200  err << except.what();
201  *err_msg = pgr_msg(err.str().c_str());
202  *log_msg = pgr_msg(log.str().c_str());
203  } catch (std::exception &except) {
204  (*return_tuples) = pgr_free(*return_tuples);
205  (*return_count) = 0;
206  err << except.what();
207  *err_msg = pgr_msg(err.str().c_str());
208  *log_msg = pgr_msg(log.str().c_str());
209  } catch(...) {
210  (*return_tuples) = pgr_free(*return_tuples);
211  (*return_count) = 0;
212  err << "Caught unknown exception!";
213  *err_msg = pgr_msg(err.str().c_str());
214  *log_msg = pgr_msg(log.str().c_str());
215  }
216 }
Rule
struct Rule Rule
turnRestrictedPath_driver.h
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
do_pgr_turnRestrictedPath
void do_pgr_turnRestrictedPath(pgr_edge_t *data_edges, size_t total_edges, Restriction_t *restrictions, size_t total_restrictions, int64_t start_vid, int64_t end_vid, size_t k, bool directed, bool heap_paths, bool stop_on_first, bool strict, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: turnRestrictedPath_driver.cpp:77
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
pgrouting::yen::Pgr_turnRestrictedPath
Definition: pgr_turnRestrictedPath.hpp:52
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
pgrouting::Pgr_messages::get_log
std::string get_log() const
get_log
Definition: pgr_messages.cpp:36
pgr_turnRestrictedPath.hpp
rule.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::yen::Pgr_turnRestrictedPath::turnRestrictedPath
std::deque< Path > turnRestrictedPath(G &graph, const std::vector< pgrouting::trsp::Rule > &restrictions, int64_t source, int64_t target, size_t k, bool heap_paths, bool stop_on_first, bool strict)
Definition: pgr_turnRestrictedPath.hpp:106
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
Restriction_t
Definition: restriction_t.h:37
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
pgr_dijkstraTR
static std::deque< Path > pgr_dijkstraTR(G &graph, const std::vector< pgrouting::trsp::Rule > &restrictions, int64_t source, int64_t target, std::string &log, size_t k, bool heap_paths, bool stop_on_first, bool strict)
Definition: turnRestrictedPath_driver.cpp:51
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139
basePath_SSEC.hpp