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
astarManyToMany_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 // #define DEBUG
43 
44 #include "./../../common/src/pgr_types.h"
45 
46 #include "./pgr_astar.hpp"
47 #include "./../../common/src/pgr_assert.h"
48 #include "./../../common/src/pgr_alloc.hpp"
49 
50 template < class G >
51 void
53  G &graph,
54  std::deque< Path > &paths,
55  std::vector < int64_t > sources,
56  std::vector < int64_t > targets,
57  int heuristic,
58  double factor,
59  double epsilon,
60  bool only_cost = false) {
61  Pgr_astar< G > fn_astar;
62  fn_astar.astar(graph, paths, sources, targets,
63  heuristic, factor, epsilon, only_cost);
64 }
65 
66 
67 /************************************************************
68  edges_sql TEXT,
69  vertex_table TEXT,
70  start_vid BIGINT,
71  end_vid BIGINT directed BOOLEAN DEFAULT true,
72  ***********************************************************/
75  size_t total_edges,
76  int64_t *start_vidsArr, size_t size_start_vidsArr,
77  int64_t *end_vidsArr, size_t size_end_vidsArr,
78  bool directed,
79  int heuristic,
80  double factor,
81  double epsilon,
82  bool only_cost,
83  General_path_element_t **return_tuples,
84  size_t *return_count,
85  char ** log_msg,
86  char ** err_msg) {
87  std::ostringstream err;
88  std::ostringstream log;
89  try {
90  pgassert(!(*log_msg));
91  pgassert(!(*err_msg));
92  pgassert(!(*return_tuples));
93  pgassert(*return_count == 0);
94  pgassert(total_edges != 0);
95 
96 
97  if (total_edges <= 1) {
98  err << "Required: more than one edge\n";
99  (*return_tuples) = NULL;
100  (*return_count) = 0;
101  *err_msg = strdup(log.str().c_str());
102  return;
103  }
104 
105  std::deque< Path >paths;
106  log << "Inserting target vertices into a c++ vector structure\n";
107  std::vector< int64_t > end_vids(
108  end_vidsArr,
109  end_vidsArr + size_end_vidsArr);
110  std::vector< int64_t > start_vids(
111  start_vidsArr,
112  start_vidsArr + size_start_vidsArr);
113 
114  graphType gType = directed? DIRECTED: UNDIRECTED;
115 
116  if (directed) {
117  log << "Working with directed Graph\n";
119  pgrouting::extract_vertices(edges, total_edges),
120  gType);
121  digraph.graph_insert_data(edges, total_edges);
122  pgr_astar(digraph, paths, start_vids, end_vids,
123  heuristic, factor, epsilon, only_cost);
124  } else {
125  log << "Working with Undirected Graph\n";
127  pgrouting::extract_vertices(edges, total_edges),
128  gType);
129  undigraph.graph_insert_data(edges, total_edges);
130  pgr_astar(undigraph, paths, start_vids, end_vids,
131  heuristic, factor, epsilon, only_cost);
132  }
133 
134  size_t count(0);
135  count = count_tuples(paths);
136 
137 
138  if (count == 0) {
139  (*return_tuples) = NULL;
140  (*return_count) = 0;
141  log <<
142  "No paths found\n";
143  *err_msg = strdup(log.str().c_str());
144  return;
145  }
146 
147  (*return_tuples) = pgr_alloc(count, (*return_tuples));
148  log << "Converting a set of paths into the tuples\n";
149  (*return_count) = (collapse_paths(return_tuples, paths));
150 
151  *err_msg = NULL;
152  *log_msg = strdup(log.str().c_str());
153  } catch (AssertFailedException &except) {
154  if (*return_tuples) free(*return_tuples);
155  (*return_count) = 0;
156  err << except.what() << "\n";
157  *err_msg = strdup(err.str().c_str());
158  *log_msg = strdup(log.str().c_str());
159  } catch (std::exception& except) {
160  if (*return_tuples) free(*return_tuples);
161  (*return_count) = 0;
162  err << except.what() << "\n";
163  *err_msg = strdup(err.str().c_str());
164  *log_msg = strdup(log.str().c_str());
165  } catch(...) {
166  if (*return_tuples) free(*return_tuples);
167  (*return_count) = 0;
168  err << "Caught unknown exception!\n";
169  *err_msg = strdup(err.str().c_str());
170  *log_msg = strdup(log.str().c_str());
171  }
172 }
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void pgr_astar(G &graph, std::deque< Path > &paths, std::vector< int64_t > sources, std::vector< int64_t > targets, int heuristic, double factor, double epsilon, bool only_cost=false)
void do_pgr_astarManyToMany(Pgr_edge_xy_t *edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, 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)
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)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:52
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.