PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lineGraph_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: lineGraph_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) 2017 Vidhan Jain
10 Mail: vidhanj1307@gmail.com
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 
37 
38 #include "cpp_common/pgr_alloc.hpp"
39 #include "cpp_common/pgr_assert.h"
40 
42 
44  std::vector< Line_graph_rt > edge_result,
45  Line_graph_rt **return_tuples,
46  size_t &sequence) {
47  (*return_tuples) = pgr_alloc(
48  (int)edge_result.size(),
49  (*return_tuples));
50 
51  for (const auto &edge: edge_result) {
52  (*return_tuples)[sequence] = {edge.id, edge.source, edge.target, edge.cost, edge.reverse_cost};
53  sequence++;
54  }
55 }
56 
57 void
59  pgr_edge_t *data_edges,
60  size_t total_edges,
61  bool directed,
62  Line_graph_rt **return_tuples,
63  size_t *return_count,
64  char ** log_msg,
65  char ** notice_msg,
66  char ** err_msg) {
67  std::ostringstream log;
68  std::ostringstream err;
69  std::ostringstream notice;
70  try {
71  pgassert(!(*log_msg));
72  pgassert(!(*notice_msg));
73  pgassert(!(*err_msg));
74  pgassert(!(*return_tuples));
75  pgassert(*return_count == 0);
76  pgassert(total_edges != 0);
77 
78  std::vector< Line_graph_rt > results;
79  graphType gType = directed?DIRECTED:UNDIRECTED;
80 
81  pgrouting::DirectedGraph digraph(gType);
82  digraph.insert_edges(data_edges, total_edges);
83  if (!directed) {
84  for (size_t ind = 0; ind < total_edges; ind++) {
85  std::swap(data_edges[ind].source, data_edges[ind].target);
86  data_edges[ind].id *= -1;
87  }
88 
89  digraph.insert_edges(data_edges, total_edges);
90 
91  for (size_t ind = 0;ind < total_edges; ind++) {
92  std::swap(data_edges[ind].source, data_edges[ind].target);
93  data_edges[ind].id *= -1;
94  }
95  }
96 
97  digraph.m_num_vertices = 1000;
98  log << digraph << "\n";
99 
100  pgrouting::LinearDirectedGraph line(gType);
101  line.insert_vertices(data_edges, total_edges);
102  line.transform(digraph);
103 
104  std::vector< Line_graph_rt > line_graph_edges;
105  if (directed) {
106  line_graph_edges = line.get_postgres_results_directed();
107  } else {
108  line_graph_edges = line.get_postgres_results_undirected();
109  }
110 
111  auto count = line_graph_edges.size();
112 
113  if (count == 0) {
114  (*return_tuples) = NULL;
115  (*return_count) = 0;
116  notice <<
117  "No paths found between start_vid and end_vid vertices";
118  } else {
119  size_t sequence = 0;
120 
122  line_graph_edges,
123  return_tuples,
124  sequence
125  );
126  (*return_count) = sequence;
127  }
128  log << line.log.str().c_str() << "\n\n\n";
129  log << line << "\n";
130 
131  pgassert(*err_msg == NULL);
132  *log_msg = log.str().empty()?
133  *log_msg :
134  pgr_msg(log.str().c_str());
135  *notice_msg = notice.str().empty()?
136  *notice_msg :
137  pgr_msg(notice.str().c_str());
138  } catch (AssertFailedException &except) {
139  (*return_tuples) = pgr_free(*return_tuples);
140  (*return_count) = 0;
141  err << except.what();
142  *err_msg = pgr_msg(err.str().c_str());
143  *log_msg = pgr_msg(log.str().c_str());
144  } catch (std::exception &except) {
145  (*return_tuples) = pgr_free(*return_tuples);
146  (*return_count) = 0;
147  err << except.what();
148  *err_msg = pgr_msg(err.str().c_str());
149  *log_msg = pgr_msg(log.str().c_str());
150  } catch(...) {
151  (*return_tuples) = pgr_free(*return_tuples);
152  (*return_count) = 0;
153  err << "Caught unknown exception!";
154  *err_msg = pgr_msg(err.str().c_str());
155  *log_msg = pgr_msg(log.str().c_str());
156  }
157 }
std::vector< Line_graph_rt > get_postgres_results_undirected()
float8 cost
Definition: trsp.h:35
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
Definition: trsp.h:31
void insert_edges(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
void transform(pgrouting::DirectedGraph &digraph)
long id
Definition: trsp.h:32
void insert_vertices(const T *edges, int64_t count)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:75
std::vector< Line_graph_rt > get_postgres_results_directed()
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
int64_t id
Definition: pgr_edge_t.h:59
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:29
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:64
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53
void get_postgres_result(std::vector< Line_graph_rt > edge_result, Line_graph_rt **return_tuples, size_t &sequence)
graphType
Definition: graph_enum.h:30
long target
Definition: trsp.h:34
void do_pgr_lineGraph(pgr_edge_t *data_edges, size_t total_edges, bool directed, Line_graph_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
float8 reverse_cost
Definition: trsp.h:36
long source
Definition: trsp.h:33