PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
alpha_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: alpha_drivedist.cpp
3 
4 Copyright (c) 2006 Anton A. Patrushev, Orkney, Inc.
5 Mail: project@pgrouting.org
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  * As a special exception, you have permission to link this program
26  * with the CGAL library and distribute executables, as long as you
27  * follow the requirements of the GNU GPL in regard to all of the
28  * software in the executable aside from CGAL.
29  *
30  */
31 
32 
33 /***********************************************************************
34 Takes a list of points and returns a list of segments
35 corresponding to the Alpha shape.
36 ************************************************************************/
37 
38 #include "./alpha_driver.h"
39 
40 #include <CGAL/Simple_cartesian.h>
41 #include <CGAL/Filtered_kernel.h>
42 #include <CGAL/algorithm.h>
43 
44 #include <CGAL/Polygon_2.h>
45 #include <CGAL/Delaunay_triangulation_2.h>
46 #include <CGAL/Triangulation_2.h>
47 #include <CGAL/Triangulation_hierarchy_vertex_base_2.h>
48 #include <CGAL/Triangulation_hierarchy_2.h>
49 #include <CGAL/Triangulation_face_base_2.h>
50 #include <CGAL/Triangulation_euclidean_traits_2.h>
51 #include <CGAL/Alpha_shape_2.h>
52 #include <CGAL/Alpha_shape_face_base_2.h>
53 #include <CGAL/Alpha_shape_vertex_base_2.h>
54 
55 #include <vector>
56 #include <list>
57 #include <cmath>
58 #include <utility>
59 #include <algorithm>
60 #include <set>
61 
62 #include "./../../common/src/pgr_alloc.hpp"
63 
64 typedef double coord_type;
65 
66 typedef CGAL::Simple_cartesian<coord_type> SC;
67 typedef CGAL::Filtered_kernel<SC> K;
68 typedef K::Point_2 Point;
69 typedef K::Segment_2 Segment;
70 typedef K::Vector_2 Vector;
71 typedef CGAL::Polygon_2<K> Polygon_2;
72 
73 typedef CGAL::Alpha_shape_vertex_base_2<K> Avb;
74 typedef CGAL::Triangulation_hierarchy_vertex_base_2<Avb> Av;
75 
76 typedef CGAL::Triangulation_face_base_2<K> Tf;
77 typedef CGAL::Alpha_shape_face_base_2<K, Tf> Af;
78 
79 typedef CGAL::Triangulation_default_data_structure_2<K, Av, Af> Tds;
80 typedef CGAL::Delaunay_triangulation_2<K, Tds> Dt;
81 typedef CGAL::Triangulation_hierarchy_2<Dt> Ht;
82 typedef CGAL::Alpha_shape_2<Ht> Alpha_shape_2;
83 
86 
89 
90 // ---------------------------------------------------------------------
91 
92 double get_angle(Point p, Point q, Point r) {
93  double m_pi(3.14159265358979323846);
94  Vector v1(q, p);
95  Vector v2(q, r);
96  double cross = v1.x() * v2.y() - v1.y() * v2.x();
97  double dot = v1.x() * v2.x() + v1.y() * v2.y();
98  double angle = atan2(cross, dot);
99  if (angle < 0.0) {
100  angle += 2 * m_pi;
101  }
102  return angle;
103 }
104 
105 size_t prev_size = 0;
106 void find_next_edge(Segment s, std::vector<Segment>& segments,
107  std::set<int>& unusedIndexes, std::vector<Polygon_2>& rings) {
108  if (unusedIndexes.empty()
109  || prev_size == unusedIndexes.size()) {
110  return;
111  }
112 
113  prev_size = unusedIndexes.size();
114 
115  Point start = s.source();
116  Point end = s.target();
117  rings.back().push_back(end);
118 
119  std::vector<int> nextIndexes;
120  for (unsigned int i = 0; i < segments.size(); i++) {
121  if (unusedIndexes.find(i) != unusedIndexes.end()) {
122  Point source = segments.at(i).source();
123  if (source == end) {
124  nextIndexes.push_back(i);
125  }
126  }
127  }
128  if (nextIndexes.size() == 1) {
129  int i = nextIndexes.at(0);
130  unusedIndexes.erase(i);
131  find_next_edge(segments.at(i), segments, unusedIndexes, rings);
132  } else if (nextIndexes.size() > 1) {
133  std::vector< std::pair<double, int> > nextAngles;
134  for (unsigned int i = 0; i < nextIndexes.size(); i++) {
135  int j = nextIndexes.at(i);
136  Point target = segments.at(j).target();
137  double angle = get_angle(start, end, target);
138  nextAngles.push_back(std::pair<double, int>(angle, j));
139  }
140  std::sort(nextAngles.begin(), nextAngles.end());
141  int i = nextAngles.begin()->second;
142  unusedIndexes.erase(i);
143  find_next_edge(segments.at(i), segments, unusedIndexes, rings);
144  }
145 
146  if (!unusedIndexes.empty()) {
147  for (unsigned int i = 0; i < segments.size(); i++) {
148  if (unusedIndexes.find(i) != unusedIndexes.end()) {
149  Polygon_2 ring;
150  ring.push_back(segments.at(i).source());
151  rings.push_back(ring);
152  unusedIndexes.erase(i);
153  find_next_edge(segments.at(i), segments, unusedIndexes, rings);
154  }
155  }
156  }
157 }
158 
159 template <class OutputIterator>
160 void
162  OutputIterator out) {
163  for (Alpha_shape_edges_iterator it = A.alpha_shape_edges_begin();
164  it != A.alpha_shape_edges_end();
165  ++it) {
166  *out++ = A.segment(*it);
167  }
168 }
169 
170 
171 int alpha_shape(vertex_t *vertices, size_t count, double alpha,
172  vertex_t **res, size_t *res_count, char **err_msg) {
173  try {
174  std::list<Point> points;
175  {
176  std::vector<Point> pv;
177 
178  for (std::size_t j = 0; j < count; ++j) {
179  Point p(vertices[j].x, vertices[j].y);
180  pv.push_back(p);
181  }
182 
183  std::sort(pv.begin(), pv.end(),
184  [](const Point &e1, const Point &e2)->bool {
185  return e2.y() < e1.y();
186  });
187  std::stable_sort(pv.begin(), pv.end(),
188  [](const Point &e1, const Point &e2)->bool {
189  return e2.x() < e1.x();
190  });
191  pv.erase(std::unique(pv.begin(), pv.end()), pv.end());
192  if (pv.size() != count && pv.size() < 3) {
193  *err_msg = strdup("After eliminating duplicated points, less than 3 points remain!!. Alpha shape calculation needs at least 3 vertices.");
194  return -1;
195  }
196  points.insert(points.begin(), pv.begin(), pv.end());
197  }
198 
199  Alpha_shape_2 A(points.begin(), points.end(),
200  coord_type(10000),
201  Alpha_shape_2::REGULARIZED);
202 
203  std::vector<Segment> segments;
204  // std::vector<Segment> result;
205 
206  // Alpha_shape_2::Alpha_shape_vertices_iterator vit;
207  // Alpha_shape_2::Vertex_handle vertex;
208  // Alpha_shape_2::Alpha_shape_edges_iterator eit;
209  // Alpha_shape_2::Edge edge;
210  // Alpha_shape_2::Face_iterator fit;
211  // Alpha_shape_2::Face_handle face;
212 
213  if (alpha <= 0.0) {
214  alpha = *A.find_optimal_alpha(1);
215  }
216  A.set_alpha(alpha);
217 
218  alpha_edges(A, std::back_inserter(segments));
219 
220  // Segment s = segments.at(0);
221  // find_next_edge(s, segments, result);
222  if (segments.empty()) {
223  *res = NULL;
224  *res_count = 0;
225  } else {
226  std::set<int> unusedIndexes;
227  for (unsigned int i = 0; i < segments.size(); i++) {
228  unusedIndexes.insert(i);
229  }
230 
231  std::vector<Polygon_2> rings;
232  Polygon_2 ring;
233  ring.push_back(segments.at(0).source());
234  rings.push_back(ring);
235  unusedIndexes.erase(0);
236  find_next_edge(segments.at(0), segments, unusedIndexes, rings);
237 
238  size_t result_count = 0;
239  for (unsigned int i = 0; i < rings.size(); i++) {
240  Polygon_2 ring = rings.at(i);
241  result_count += ring.size();
242  }
243  result_count += rings.size() - 1;
244  *res = pgr_alloc(result_count, (*res));
245  *res_count = result_count;
246 
247  int idx = 0;
248  for (unsigned int i = 0; i < rings.size(); i++) {
249  if (i > 0) {
250  (*res)[idx].x = DBL_MAX;
251  (*res)[idx].y = DBL_MAX;
252  idx++;
253  }
254  Polygon_2 ring = rings.at(i);
255  for (unsigned int j = 0; j < ring.size(); j++) {
256  Point point = ring.vertex(j);
257  (*res)[idx].x = point.x();
258  (*res)[idx].y = point.y();
259  idx++;
260  }
261  }
262  }
263  *err_msg = NULL;
264 
265  return EXIT_SUCCESS;
266  } catch ( ... ) {
267  *err_msg = strdup("Caught unknown exception!");
268  }
269  return -1;
270 }
CGAL::Triangulation_face_base_2< K > Tf
size_t prev_size
K::Point_2 Point
CGAL::Triangulation_default_data_structure_2< K, Av, Af > Tds
void alpha_edges(const Alpha_shape_2 &A, OutputIterator out)
K::Segment_2 Segment
CGAL::Triangulation_hierarchy_vertex_base_2< Avb > Av
CGAL::Simple_cartesian< coord_type > SC
CGAL::Alpha_shape_vertex_base_2< K > Avb
double get_angle(Point p, Point q, Point r)
CGAL::Filtered_kernel< SC > K
CGAL::Alpha_shape_2< Ht > Alpha_shape_2
CGAL::Triangulation_hierarchy_2< Dt > Ht
double coord_type
Alpha_shape_2::Face_circulator Face_circulator
CGAL::Polygon_2< K > Polygon_2
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:62
CGAL::Delaunay_triangulation_2< K, Tds > Dt
Alpha_shape_2::Vertex_circulator Vertex_circulator
char * err_msg
Definition: BDATester.cpp:50
Alpha_shape_2::Alpha_iterator Alpha_iterator
K::Vector_2 Vector
void find_next_edge(Segment s, std::vector< Segment > &segments, std::set< int > &unusedIndexes, std::vector< Polygon_2 > &rings)
Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator
CGAL::Alpha_shape_face_base_2< K, Tf > Af
int alpha_shape(vertex_t *vertices, size_t count, double alpha, vertex_t **res, size_t *res_count, char **err_msg)