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