pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
astarOneToMany_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: astarOneToOne_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 
30 
31 #if defined(__MINGW32__) || defined(_MSC_VER)
32 #include <winsock2.h>
33 #include <windows.h>
34 #endif
35 
36 #include <sstream>
37 #include <deque>
38 #include <algorithm>
39 #include <vector>
41 
42 
43 #include "./../../common/src/pgr_types.h"
44 #include "./pgr_astar.hpp"
45 #include "./../../common/src/pgr_assert.h"
46 #include "./../../common/src/pgr_alloc.hpp"
47 
48 template < class G >
49 void
51  G &graph,
52  std::deque< Path > &paths,
53  int64_t source,
54  std::vector < int64_t > targets,
55  int heuristic,
56  double factor,
57  double epsilon,
58  bool only_cost = false) {
59  Pgr_astar< G > fn_astar;
60  fn_astar.astar(graph, paths, source, targets,
61  heuristic, factor, epsilon, only_cost);
62 }
63 
64 
65 /************************************************************
66  edges_sql TEXT,
67  vertex_table TEXT,
68  start_vid BIGINT,
69  end_vid BIGINT directed BOOLEAN DEFAULT true,
70  ***********************************************************/
73  size_t total_edges,
74  int64_t start_vid,
75  int64_t *end_vidsArr, size_t size_end_vidsArr,
76  bool directed,
77  int heuristic,
78  double factor,
79  double epsilon,
80  bool only_cost,
81  General_path_element_t **return_tuples,
82  size_t *return_count,
83  char ** log_msg,
84  char ** err_msg,
85  bool normal) {
86  std::ostringstream err;
87  std::ostringstream log;
88  try {
89  pgassert(!(*log_msg));
90  pgassert(!(*err_msg));
91  pgassert(!(*return_tuples));
92  pgassert(*return_count == 0);
93  pgassert(total_edges != 0);
94 
95 
96  if (total_edges <= 1) {
97  err << "Required: more than one edge\n";
98  (*return_tuples) = NULL;
99  (*return_count) = 0;
100  *err_msg = strdup(log.str().c_str());
101  return;
102  }
103 
104  std::deque< Path >paths;
105  log << "Inserting target vertices into a c++ vector structure\n";
106  std::vector< int64_t > end_vids(
107  end_vidsArr,
108  end_vidsArr + size_end_vidsArr);
109 
110  graphType gType = directed? DIRECTED: UNDIRECTED;
111 
112  if (directed) {
113  log << "Working with directed Graph\n";
115  pgrouting::extract_vertices(edges, total_edges),
116  gType);
117  digraph.graph_insert_data(edges, total_edges);
118  pgr_astar(digraph, paths, start_vid, end_vids,
119  heuristic, factor, epsilon, only_cost);
120  } else {
121  log << "Working with Undirected Graph\n";
123  pgrouting::extract_vertices(edges, total_edges),
124  gType);
125  undigraph.graph_insert_data(edges, total_edges);
126  pgr_astar(undigraph, paths, start_vid, end_vids,
127  heuristic, factor, epsilon, only_cost);
128  }
129  if (!normal) {
130  for (auto &path : paths) {
131  log << "reversing path\n";
132  log << path;
133  path.reverse();
134  log << "reversed path\n";
135  log << path;
136  }
137  }
138  log << "checking reversed paths\n";
139  for (auto path : paths) {
140  log << "reversed path\n";
141  log << path;
142  }
143 
144  size_t count(0);
145  count = count_tuples(paths);
146 
147 
148  if (count == 0) {
149  (*return_tuples) = NULL;
150  (*return_count) = 0;
151  log <<
152  "No paths found\n";
153  *err_msg = strdup(log.str().c_str());
154  return;
155  }
156 
157  (*return_tuples) = pgr_alloc(count, (*return_tuples));
158  log << "Converting a set of paths into the tuples\n";
159  (*return_count) = (collapse_paths(return_tuples, paths));
160 
161  *err_msg = NULL;
162  *log_msg = strdup(log.str().c_str());
163  } catch (AssertFailedException &except) {
164  if (*return_tuples) free(*return_tuples);
165  (*return_count) = 0;
166  err << except.what() << "\n";
167  *err_msg = strdup(err.str().c_str());
168  *log_msg = strdup(log.str().c_str());
169  } catch (std::exception& except) {
170  if (*return_tuples) free(*return_tuples);
171  (*return_count) = 0;
172  err << except.what() << "\n";
173  *err_msg = strdup(err.str().c_str());
174  *log_msg = strdup(log.str().c_str());
175  } catch(...) {
176  if (*return_tuples) free(*return_tuples);
177  (*return_count) = 0;
178  err << "Caught unknown exception!\n";
179  *err_msg = strdup(err.str().c_str());
180  *log_msg = strdup(log.str().c_str());
181  }
182 }
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void astar(G &graph, Path &path, int64_t start_vertex, int64_t end_vertex, int heuristic, double factor, double epsilon, bool only_cost=false)
one to one
Definition: pgr_astar.hpp:263
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
edge_astar_t * edges
Definition: BDATester.cpp:46
graphType
Definition: pgr_types.h:192
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
void pgr_astar(G &graph, std::deque< Path > &paths, int64_t source, std::vector< int64_t > targets, int heuristic, double factor, double epsilon, bool only_cost=false)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:52
path_element_t * path
Definition: BDATester.cpp:49
virtual const char * what() const
Definition: pgr_assert.cpp:62
char * err_msg
Definition: BDATester.cpp:50
void graph_insert_data(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
void do_pgr_astarOneToMany(Pgr_edge_xy_t *edges, size_t total_edges, int64_t start_vid, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg, bool normal)