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
xy_vertex.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2  *
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  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 #if defined(__MINGW32__) || defined(_MSC_VER)
25 #include <winsock2.h>
26 #include <windows.h>
27 #ifdef open
28 #undef open
29 #endif
30 #endif
31 
32 
33 #include <vector>
34 #include <algorithm>
35 
36 #include "./xy_vertex.h"
37 #include "./pgr_types.h"
38 #include "./pgr_assert.h"
39 
40 
41 namespace pgrouting {
42 
43 
44 std::ostream& operator<<(std::ostream& log, const XY_vertex &v) {
45  log << v.id << "(" << v.point.x() << "," << v.point.y() << ")";
46  return log;
47 }
48 
49 bool
50 XY_vertex::operator==(const XY_vertex &rhs) const {
51  if (&rhs == this) return true;
52  return this->id == rhs.id && point == rhs.point;
53 }
54 
55 
56 size_t
58  std::vector < XY_vertex > vertices) {
59  auto count(vertices.size());
60  std::stable_sort(
61  vertices.begin(), vertices.end(),
62  [](const XY_vertex &lhs, const XY_vertex &rhs)
63  {return lhs.id < rhs.id;});
64  vertices.erase(
65  std::unique(
66  vertices.begin(), vertices.end(),
67  [](const XY_vertex &lhs, const XY_vertex &rhs)
68  {return lhs.id == rhs.id;}), vertices.end());
69 
70  return count - vertices.size();
71 }
72 
73 std::vector < XY_vertex >
75  const std::vector <Pgr_edge_xy_t > &data_edges) {
76 
77  std::vector< XY_vertex > vertices;
78  if (data_edges.empty()) return vertices;
79 
80  vertices.reserve(data_edges.size() * 2);
81 
82  for (const auto edge : data_edges) {
83  XY_vertex v_source(edge, true);
84  vertices.push_back(v_source);
85 
86  XY_vertex v_target(edge, false);
87  vertices.push_back(v_target);
88  }
89 
90  /*
91  * sort and delete duplicates
92  */
93  std::stable_sort(
94  vertices.begin(), vertices.end(),
95  [](const XY_vertex &lhs, const XY_vertex &rhs)
96  {return lhs.id < rhs.id;});
97  vertices.erase(
98  std::unique(
99  vertices.begin(), vertices.end(),
100  [](const XY_vertex &lhs, const XY_vertex &rhs)
101  {return lhs.id == rhs.id;}), vertices.end());
102  return vertices;
103 }
104 
105 std::vector < XY_vertex >
107  const Pgr_edge_xy_t *data_edges, int64_t count) {
108  return extract_vertices(
109  std::vector < Pgr_edge_xy_t >(data_edges, data_edges + count));
110 }
111 
112 #if 0
113 /* the following might be needed when using withPoints */
114 std::vector < XY_vertex > extract_vertices(
115  std::vector < XY_vertex > vertices,
116  const std::vector < Pgr_edge_xy_t > data_edges) {
117  if (data_edges.empty()) return vertices;
118 
119  vertices.reserve(vertices.size() + data_edges.size() * 2);
120 
121  for (const auto edge : data_edges) {
122  vertices.push_back(XY_vertex(edge.source, edge.x1, edge.y1));
123  vertices.push_back(XY_vertex(edge.target, edge.x2, edge.y2));
124  }
125 
126  /*
127  * sort and delete duplicates
128  */
129  std::stable_sort(vertices.begin(), vertices.end(),
130  [](const XY_vertex &lhs, const XY_vertex &rhs)
131  {return lhs.id < rhs.id;});
132 
133  vertices.erase(
134  std::unique(vertices.begin(), vertices.end(),
135  [](const XY_vertex &lhs, const XY_vertex &rhs)
136  {return lhs.id == rhs.id;}), vertices.end());
137  return vertices;
138 }
139 std::vector < XY_vertex > extract_vertices(
140  std::vector < XY_vertex > vertices,
141  const Pgr_edge_xy_t *data_edges, int64_t count) {
142  return extract_vertices(vertices,
143  std::vector < Pgr_edge_xy_t >(data_edges, data_edges + count));
144 }
145 #endif
146 
147 } // namespace pgrouting
std::ostream & operator<<(std::ostream &log, const Basic_vertex &v)
bool operator==(const XY_vertex &rhs) const
Definition: xy_vertex.cpp:50
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
Definition: tsp.h:38
Assertions Handling.
size_t check_vertices(std::vector< Basic_vertex > vertices)