PGROUTING  2.6-dev
astar_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 
31 
32 #include <sstream>
33 #include <deque>
34 #include <algorithm>
35 #include <vector>
36 
37 #include "astar/pgr_astar.hpp"
38 
39 #include "cpp_common/pgr_alloc.hpp"
40 #include "cpp_common/pgr_assert.h"
41 
42 
43 
44 template < class G >
45 std::deque<Path>
47  G &graph,
48  std::vector<int64_t> sources,
49  std::vector<int64_t> targets,
50  int heuristic,
51  double factor,
52  double epsilon,
53  bool only_cost,
54  bool normal) {
55  std::sort(sources.begin(), sources.end());
56  sources.erase(
57  std::unique(sources.begin(), sources.end()),
58  sources.end());
59 
60  std::sort(targets.begin(), targets.end());
61  targets.erase(
62  std::unique(targets.begin(), targets.end()),
63  targets.end());
64 
66  auto paths = fn_astar.astar(graph, sources, targets,
67  heuristic, factor, epsilon, only_cost);
68 
69  if (!normal) {
70  for (auto &path : paths) {
71  path.reverse();
72  }
73  }
74  return paths;
75 }
76 
77 
78 /************************************************************
79  edges_sql TEXT,
80  vertex_table TEXT,
81  start_vid BIGINT,
82  end_vid BIGINT directed BOOLEAN DEFAULT true,
83  ***********************************************************/
85  Pgr_edge_xy_t *edges, size_t total_edges,
86  int64_t *start_vidsArr, size_t size_start_vidsArr,
87  int64_t *end_vidsArr, size_t size_end_vidsArr,
88  bool directed,
89  int heuristic,
90  double factor,
91  double epsilon,
92  bool only_cost,
93  bool normal,
94  General_path_element_t **return_tuples,
95  size_t *return_count,
96  char** log_msg,
97  char** notice_msg,
98  char** err_msg) {
99  std::ostringstream log;
100  std::ostringstream notice;
101  std::ostringstream err;
102  try {
103  pgassert(!(*log_msg));
104  pgassert(!(*err_msg));
105  pgassert(!(*return_tuples));
106  pgassert(*return_count == 0);
107  pgassert(total_edges != 0);
108 
109 
110  log << "Inserting target vertices into a c++ vector structure\n";
111  std::vector< int64_t > end_vids(
112  end_vidsArr,
113  end_vidsArr + size_end_vidsArr);
114  std::vector< int64_t > start_vids(
115  start_vidsArr,
116  start_vidsArr + size_start_vidsArr);
117 
118  graphType gType = directed? DIRECTED: UNDIRECTED;
119 
120  std::deque< Path >paths;
121  if (directed) {
122  log << "Working with directed Graph\n";
124  pgrouting::extract_vertices(edges, total_edges),
125  gType);
126  digraph.insert_edges(edges, total_edges);
127  paths = pgr_astar(digraph, start_vids, end_vids,
128  heuristic, factor, epsilon, only_cost, normal);
129  } else {
130  log << "Working with Undirected Graph\n";
132  pgrouting::extract_vertices(edges, total_edges),
133  gType);
134  undigraph.insert_edges(edges, total_edges);
135  paths = pgr_astar(undigraph, start_vids, end_vids,
136  heuristic, factor, epsilon, only_cost, normal);
137  }
138 
139  size_t count(0);
140  count = count_tuples(paths);
141 
142 
143  if (count == 0) {
144  (*return_tuples) = NULL;
145  (*return_count) = 0;
146  notice <<
147  "No paths found\n";
148  *log_msg = pgr_msg(notice.str().c_str());
149  return;
150  }
151 
152  (*return_tuples) = pgr_alloc(count, (*return_tuples));
153  log << "Converting a set of paths into the tuples\n";
154  (*return_count) = (collapse_paths(return_tuples, paths));
155 
156  *log_msg = log.str().empty()?
157  *log_msg :
158  pgr_msg(log.str().c_str());
159  *notice_msg = notice.str().empty()?
160  *notice_msg :
161  pgr_msg(notice.str().c_str());
162  } catch (AssertFailedException &except) {
163  (*return_tuples) = pgr_free(*return_tuples);
164  (*return_count) = 0;
165  err << except.what();
166  *err_msg = pgr_msg(err.str().c_str());
167  *log_msg = pgr_msg(log.str().c_str());
168  } catch (std::exception &except) {
169  (*return_tuples) = pgr_free(*return_tuples);
170  (*return_count) = 0;
171  err << except.what();
172  *err_msg = pgr_msg(err.str().c_str());
173  *log_msg = pgr_msg(log.str().c_str());
174  } catch(...) {
175  (*return_tuples) = pgr_free(*return_tuples);
176  (*return_count) = 0;
177  err << "Caught unknown exception!";
178  *err_msg = pgr_msg(err.str().c_str());
179  *log_msg = pgr_msg(log.str().c_str());
180  }
181 }
182 
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void insert_edges(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
Path astar(G &graph, int64_t start_vertex, int64_t end_vertex, int heuristic, double factor, double epsilon, bool only_cost)
one to one astar 1 to 1
Definition: pgr_astar.hpp:67
void reverse()
std::deque< Path > pgr_astar(G &graph, std::vector< int64_t > sources, std::vector< int64_t > targets, int heuristic, double factor, double epsilon, bool only_cost, bool normal)
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
size_t count_tuples(const std::deque< Path > &paths)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53
graphType
Definition: graph_enum.h:30
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, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)