pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
basePath_SSEC.cpp
1 /*PGR-GNU*****************************************************************
2 File: basePath_SSEC.cpp
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 
22 ********************************************************************PGR-GNU*/
23 
24 #ifdef __MINGW32__
25 #include <winsock2.h>
26 #include <windows.h>
27 #endif
28 
29 
30 #include "basePath_SSEC.hpp"
31 #include <deque>
32 #include <iostream>
33 #include <algorithm>
34 #include "postgres.h"
35 #include "./pgr_types.h"
36 
37 
38 void Path::push_front(Path_t data) {
39  path.push_front(data);
40  m_tot_cost += data.cost;
41 }
42 
43 void Path::push_back(Path_t data) {
44  path.push_back(data);
45  m_tot_cost += data.cost;
46 }
47 
48 
49 
50 void Path::clear() {
51  path.clear();
52  m_tot_cost = 0;
53  m_start_id = 0;
54  m_end_id = 0;
55 }
56 
57 std::ostream& operator<<(std::ostream &log, const Path &path) {
58  log << "Path: " << path.start_id() << " -> " << path.end_id() << "\n"
59  << "seq\tnode\tedge\tcost\tagg_cost\n";
60  int64_t i = 0;
61  for (const auto &e : path) {
62  log << i << "\t"
63  << e.node << "\t"
64  << e.edge << "\t"
65  << e.cost << "\t"
66  << e.agg_cost << "\n";
67  ++i;
68  }
69  return log;
70 }
71 
72 
73 
74 Path Path::getSubpath(unsigned int j) const {
75  Path result(start_id(), end_id());
76  if (j == 0) return result;
77  for (auto i = path.begin(); i != path.begin() + j; ++i) {
78  result.push_back((*i));
79  }
80  return result;
81 }
82 
83 
84 bool Path::isEqual(const Path &subpath) const {
85  if (subpath.empty()) return true;
86  if (subpath.size() >= path.size()) return false;
87  std::deque< Path_t >::const_iterator i, j;
88  for (i = path.begin(), j = subpath.begin();
89  j != subpath.end();
90  ++i, ++j)
91  if ((*i).node != (*j).node) return false;
92  return true;
93 }
94 
95 void Path::appendPath(const Path &o_path) {
96  path.insert(path.end(), o_path.path.begin(), o_path.path.end());
97  m_tot_cost += o_path.m_tot_cost;
98 }
99 
100 
101 void Path::generate_postgres_data(
102  General_path_element_t **postgres_data,
103  size_t &sequence) const{
104  int i = 1;
105  for (const auto e : path) {
106  (*postgres_data)[sequence] =
107  {i, start_id(), end_id(), e.node, e.edge, e.cost, e.agg_cost};
108  ++i;
109  ++sequence;
110  }
111 }
112 
113 /* used by driving distance */
114 void Path::get_pg_dd_path(
115  General_path_element_t **ret_path,
116  size_t &sequence) const {
117 
118  for (unsigned int i = 0; i < path.size(); i++) {
119  (*ret_path)[sequence].seq = i;
120  (*ret_path)[sequence].start_id = start_id();
121  (*ret_path)[sequence].end_id = start_id();
122  (*ret_path)[sequence].node = path[i].node;
123  (*ret_path)[sequence].edge = path[i].edge;
124  (*ret_path)[sequence].cost = path[i].cost;
125  (*ret_path)[sequence].agg_cost = path[i].agg_cost;
126  sequence++;
127  }
128 }
129 
130 /* used by ksp */
131 void Path::get_pg_ksp_path(
132  General_path_element_t **ret_path,
133  size_t &sequence, int routeId) const {
134 
135  for (unsigned int i = 0; i < path.size(); i++) {
136  (*ret_path)[sequence].seq = i + 1;
137  (*ret_path)[sequence].start_id = routeId;
138  (*ret_path)[sequence].end_id = end_id();
139  (*ret_path)[sequence].node = path[i].node;
140  (*ret_path)[sequence].edge = path[i].edge;
141  (*ret_path)[sequence].cost = path[i].cost;
142  (*ret_path)[sequence].agg_cost = (i == 0)? 0: (*ret_path)[sequence-1].agg_cost + path[i-1].cost;
143  sequence++;
144  }
145 }