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