pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
trsp_core.cpp
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
4 Mail: project@pgrouting.org
5 
6 ------
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 
22 ********************************************************************PGR-GNU*/
23 #ifdef __MINGW32__
24 #include <winsock2.h>
25 #include <windows.h>
26 #endif
27 
28 #include <string.h>
29 #include <sstream>
30 #include "GraphDefinition.h"
31 
32 
33 int trsp_node_wrapper(
34  edge_t *edges, size_t edge_count,
35  restrict_t *restricts, size_t restrict_count,
36  int start_vertex,
37  int end_vertex,
38  bool directed,
39  bool has_reverse_cost,
40  path_element_t **path, size_t *path_count,
41  char **err_msg) {
42 
43  std::ostringstream log;
44  try {
45 
46  std::vector<PDVI> ruleTable;
47 
48  int j;
49  ruleTable.clear();
50  for (size_t i=0; i<restrict_count; i++) {
51  std::vector<int64_t> seq;
52  seq.clear();
53  seq.push_back(restricts[i].target_id);
54  for(j = 0; j<MAX_RULE_LENGTH && restricts[i].via[j]>-1; j++)
55  {
56  seq.push_back(restricts[i].via[j]);
57  }
58  ruleTable.push_back(make_pair(restricts[i].to_cost, seq));
59  }
60 
61  GraphDefinition gdef(edges, static_cast<unsigned int>(edge_count), directed, has_reverse_cost);
62  gdef.set_restrictions(start_vertex, end_vertex, ruleTable);
63  int res = gdef.my_dijkstra(start_vertex, end_vertex, path, path_count, log);
64 
65 
66  if (res < 0)
67  return res;
68  else
69  return EXIT_SUCCESS;
70  }
71  catch(std::exception& e) {
72  log << e.what();
73  *err_msg = strdup(log.str().c_str());
74  return -1;
75  }
76  catch(...) {
77  log << "Caught unknown exception!";
78  *err_msg = strdup(log.str().c_str());
79  return -1;
80  }
81 }
82 
83 int trsp_edge_wrapper(
84  edge_t *edges, size_t edge_count,
85  restrict_t *restricts, size_t restrict_count,
86  int start_edge,
87  double start_pos,
88  int end_edge,
89  double end_pos,
90  bool directed,
91  bool has_reverse_cost,
92  path_element_t **path, size_t *path_count,
93  char **err_msg) {
94  std::ostringstream log;
95  try {
96 
97  std::vector<PDVI> ruleTable;
98 
99  int j;
100  ruleTable.clear();
101  for (size_t i=0; i<restrict_count; i++) {
102  std::vector<int64_t> seq;
103  seq.clear();
104  seq.push_back(restricts[i].target_id);
105  for(j = 0; j<MAX_RULE_LENGTH && restricts[i].via[j]>-1; j++)
106  {
107  seq.push_back(restricts[i].via[j]);
108  }
109  ruleTable.push_back(make_pair(restricts[i].to_cost, seq));
110  }
111 
112  GraphDefinition gdef(edges, static_cast<unsigned int>(edge_count), directed, has_reverse_cost);
113 
114  int64_t start_vertex = 0;
115  int64_t end_vertex = 0;
116  gdef.add_virtual_vertices(
117  start_edge, start_pos,
118  end_edge, end_pos,
119  start_vertex, end_vertex);
120 
121  gdef.set_restrictions(start_vertex, end_vertex, ruleTable);
122  int res = gdef.my_dijkstra(start_vertex, end_vertex, path, path_count, log);
123 
124 
125  if (res < 0)
126  return res;
127  else
128  return EXIT_SUCCESS;
129  }
130  catch(std::exception& e) {
131  log << e.what();
132  *err_msg = strdup(log.str().c_str());
133  return -1;
134  }
135  catch(...) {
136  log << "Caught unknown exception!";
137  *err_msg = strdup(log.str().c_str());
138  return -1;
139  }
140 }