PGROUTING  2.6
alpha_driver.cpp File Reference
#include "drivers/alpha_shape/alpha_driver.h"
#include <vector>
#include <list>
#include <cmath>
#include <utility>
#include <algorithm>
#include <set>
#include "cpp_common/pgr_assert.h"
#include "CGAL/Simple_cartesian.h"
#include "CGAL/Filtered_kernel.h"
#include "CGAL/algorithm.h"
#include "CGAL/Polygon_2.h"
#include "CGAL/Delaunay_triangulation_2.h"
#include "CGAL/Triangulation_2.h"
#include "CGAL/Triangulation_hierarchy_vertex_base_2.h"
#include "CGAL/Triangulation_hierarchy_2.h"
#include "CGAL/Triangulation_face_base_2.h"
#include "CGAL/Triangulation_euclidean_traits_2.h"
#include "CGAL/Alpha_shape_2.h"
#include "CGAL/Alpha_shape_face_base_2.h"
#include "CGAL/Alpha_shape_vertex_base_2.h"
#include "cpp_common/pgr_alloc.hpp"
Include dependency graph for alpha_driver.cpp:

Go to the source code of this file.

Typedefs

typedef CGAL::Alpha_shape_face_base_2< K, TfAf
 
typedef Alpha_shape_2::Alpha_iterator Alpha_iterator
 
typedef CGAL::Alpha_shape_2< HtAlpha_shape_2
 
typedef Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator
 
typedef CGAL::Triangulation_hierarchy_vertex_base_2< AvbAv
 
typedef CGAL::Alpha_shape_vertex_base_2< KAvb
 
typedef double coord_type
 
typedef CGAL::Delaunay_triangulation_2< K, TdsDt
 
typedef Alpha_shape_2::Face_circulator Face_circulator
 
typedef CGAL::Triangulation_hierarchy_2< DtHt
 
typedef CGAL::Filtered_kernel< SCK
 
typedef K::Point_2 Point
 
typedef CGAL::Polygon_2< KPolygon_2
 
typedef CGAL::Simple_cartesian< coord_typeSC
 
typedef K::Segment_2 Segment
 
typedef CGAL::Triangulation_default_data_structure_2< K, Av, AfTds
 
typedef CGAL::Triangulation_face_base_2< KTf
 
typedef K::Vector_2 Vector
 
typedef Alpha_shape_2::Vertex_circulator Vertex_circulator
 

Functions

template<class OutputIterator >
void alpha_edges (const Alpha_shape_2 &A, OutputIterator out)
 
int alpha_shape (vertex_t *vertices, size_t count, double alpha, vertex_t **return_tuples, size_t *res_count, char **err_msg)
 
void find_next_edge (Segment s, std::vector< Segment > &segments, std::set< int > &unusedIndexes, std::vector< Polygon_2 > &rings)
 
double get_angle (Point p, Point q, Point r)
 

Variables

size_t prev_size = 0
 

Typedef Documentation

typedef CGAL::Alpha_shape_face_base_2<K, Tf> Af

Definition at line 80 of file alpha_driver.cpp.

typedef Alpha_shape_2::Alpha_iterator Alpha_iterator

Definition at line 90 of file alpha_driver.cpp.

typedef CGAL::Alpha_shape_2<Ht> Alpha_shape_2

Definition at line 85 of file alpha_driver.cpp.

typedef Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator

Definition at line 91 of file alpha_driver.cpp.

typedef CGAL::Triangulation_hierarchy_vertex_base_2<Avb> Av

Definition at line 77 of file alpha_driver.cpp.

typedef CGAL::Alpha_shape_vertex_base_2<K> Avb

Definition at line 76 of file alpha_driver.cpp.

typedef double coord_type

Definition at line 67 of file alpha_driver.cpp.

typedef CGAL::Delaunay_triangulation_2<K, Tds> Dt

Definition at line 83 of file alpha_driver.cpp.

typedef Alpha_shape_2::Face_circulator Face_circulator

Definition at line 87 of file alpha_driver.cpp.

typedef CGAL::Triangulation_hierarchy_2<Dt> Ht

Definition at line 84 of file alpha_driver.cpp.

typedef CGAL::Filtered_kernel<SC> K

Definition at line 70 of file alpha_driver.cpp.

typedef K::Point_2 Point

Definition at line 71 of file alpha_driver.cpp.

typedef CGAL::Polygon_2<K> Polygon_2

Definition at line 74 of file alpha_driver.cpp.

typedef CGAL::Simple_cartesian<coord_type> SC

Definition at line 69 of file alpha_driver.cpp.

typedef K::Segment_2 Segment

Definition at line 72 of file alpha_driver.cpp.

typedef CGAL::Triangulation_default_data_structure_2<K, Av, Af> Tds

Definition at line 82 of file alpha_driver.cpp.

typedef CGAL::Triangulation_face_base_2<K> Tf

Definition at line 79 of file alpha_driver.cpp.

typedef K::Vector_2 Vector

Definition at line 73 of file alpha_driver.cpp.

typedef Alpha_shape_2::Vertex_circulator Vertex_circulator

Definition at line 88 of file alpha_driver.cpp.

Function Documentation

template<class OutputIterator >
void alpha_edges ( const Alpha_shape_2 A,
OutputIterator  out 
)

Definition at line 164 of file alpha_driver.cpp.

Referenced by alpha_shape().

165  {
166  for (Alpha_shape_edges_iterator it = A.alpha_shape_edges_begin();
167  it != A.alpha_shape_edges_end();
168  ++it) {
169  *out++ = A.segment(*it);
170  }
171 }
Alpha_shape_2::Alpha_shape_edges_iterator Alpha_shape_edges_iterator

Here is the caller graph for this function:

int alpha_shape ( vertex_t vertices,
size_t  count,
double  alpha,
vertex_t **  return_tuples,
size_t *  res_count,
char **  err_msg 
)

Definition at line 174 of file alpha_driver.cpp.

References alpha_edges(), find_next_edge(), pgr_alloc(), pgr_free(), pgr_msg(), and AssertFailedException::what().

Referenced by compute_alpha_shape().

175  {
176  std::ostringstream err;
177 
178  try {
179  std::list<Point> points;
180  {
181  std::vector<Point> pv;
182 
183  for (std::size_t j = 0; j < count; ++j) {
184  Point p(vertices[j].x, vertices[j].y);
185  pv.push_back(p);
186  }
187 
188  std::sort(pv.begin(), pv.end(),
189  [](const Point &e1, const Point &e2)->bool {
190  return e2.y() < e1.y();
191  });
192  std::stable_sort(pv.begin(), pv.end(),
193  [](const Point &e1, const Point &e2)->bool {
194  return e2.x() < e1.x();
195  });
196  pv.erase(std::unique(pv.begin(), pv.end()), pv.end());
197  if (pv.size() != count && pv.size() < 3) {
198  err << "After eliminating duplicated points,"
199  " less than 3 points remain!!."
200  " Alpha shape calculation needs at least 3 vertices.";
201  *err_msg = pgr_msg(err.str().c_str());
202  return -1;
203  }
204  points.insert(points.begin(), pv.begin(), pv.end());
205  }
206 
207  Alpha_shape_2 A(points.begin(), points.end(),
208  coord_type(10000),
209  Alpha_shape_2::REGULARIZED);
210 
211  std::vector<Segment> segments;
212  // std::vector<Segment> result;
213 
214  // Alpha_shape_2::Alpha_shape_vertices_iterator vit;
215  // Alpha_shape_2::Vertex_handle vertex;
216  // Alpha_shape_2::Alpha_shape_edges_iterator eit;
217  // Alpha_shape_2::Edge edge;
218  // Alpha_shape_2::Face_iterator fit;
219  // Alpha_shape_2::Face_handle face;
220 
221  if (alpha <= 0.0) {
222  alpha = *A.find_optimal_alpha(1);
223  }
224  A.set_alpha(alpha);
225 
226  alpha_edges(A, std::back_inserter(segments));
227 
228  // Segment s = segments.at(0);
229  // find_next_edge(s, segments, result);
230  if (segments.empty()) {
231  *return_tuples = NULL;
232  *res_count = 0;
233  } else {
234  std::set<int> unusedIndexes;
235  for (unsigned int i = 0; i < segments.size(); i++) {
236  unusedIndexes.insert(i);
237  }
238 
239  std::vector<Polygon_2> rings;
240  Polygon_2 ring;
241  ring.push_back(segments.at(0).source());
242  rings.push_back(ring);
243  unusedIndexes.erase(0);
244  find_next_edge(segments.at(0), segments, unusedIndexes, rings);
245 
246  size_t result_count = 0;
247  for (unsigned int i = 0; i < rings.size(); i++) {
248  Polygon_2 ring = rings.at(i);
249  result_count += ring.size();
250  }
251  result_count += rings.size() - 1;
252  *return_tuples = pgr_alloc(result_count, (*return_tuples));
253  *res_count = result_count;
254 
255  int idx = 0;
256  for (unsigned int i = 0; i < rings.size(); i++) {
257  if (i > 0) {
258  (*return_tuples)[idx].x = DBL_MAX;
259  (*return_tuples)[idx].y = DBL_MAX;
260  idx++;
261  }
262  Polygon_2 ring = rings.at(i);
263  for (unsigned int j = 0; j < ring.size(); j++) {
264  Point point = ring.vertex(j);
265  (*return_tuples)[idx].x = point.x();
266  (*return_tuples)[idx].y = point.y();
267  idx++;
268  }
269  }
270  }
271  *err_msg = NULL;
272 
273  return EXIT_SUCCESS;
274  } catch (AssertFailedException &except) {
275  (*return_tuples) = pgr_free(*return_tuples);
276  (*res_count) = 0;
277  err << except.what();
278  *err_msg = pgr_msg(err.str().c_str());
279  } catch (std::exception &except) {
280  (*return_tuples) = pgr_free(*return_tuples);
281  (*res_count) = 0;
282  err << except.what();
283  *err_msg = pgr_msg(err.str().c_str());
284  } catch(...) {
285  (*return_tuples) = pgr_free(*return_tuples);
286  (*res_count) = 0;
287  err << "Caught unknown exception!";
288  *err_msg = pgr_msg(err.str().c_str());
289  }
290  return -1;
291 }
K::Point_2 Point
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void alpha_edges(const Alpha_shape_2 &A, OutputIterator out)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
CGAL::Alpha_shape_2< Ht > Alpha_shape_2
double coord_type
CGAL::Polygon_2< K > Polygon_2
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
virtual const char * what() const
Definition: pgr_assert.cpp:53
void find_next_edge(Segment s, std::vector< Segment > &segments, std::set< int > &unusedIndexes, std::vector< Polygon_2 > &rings)

Here is the call graph for this function:

Here is the caller graph for this function:

void find_next_edge ( Segment  s,
std::vector< Segment > &  segments,
std::set< int > &  unusedIndexes,
std::vector< Polygon_2 > &  rings 
)

Definition at line 109 of file alpha_driver.cpp.

References get_angle(), and prev_size.

Referenced by alpha_shape().

110  {
111  if (unusedIndexes.empty()
112  || prev_size == unusedIndexes.size()) {
113  return;
114  }
115 
116  prev_size = unusedIndexes.size();
117 
118  Point start = s.source();
119  Point end = s.target();
120  rings.back().push_back(end);
121 
122  std::vector<int> nextIndexes;
123  for (unsigned int i = 0; i < segments.size(); i++) {
124  if (unusedIndexes.find(i) != unusedIndexes.end()) {
125  Point source = segments.at(i).source();
126  if (source == end) {
127  nextIndexes.push_back(i);
128  }
129  }
130  }
131  if (nextIndexes.size() == 1) {
132  int i = nextIndexes.at(0);
133  unusedIndexes.erase(i);
134  find_next_edge(segments.at(i), segments, unusedIndexes, rings);
135  } else if (nextIndexes.size() > 1) {
136  std::vector< std::pair<double, int> > nextAngles;
137  for (unsigned int i = 0; i < nextIndexes.size(); i++) {
138  int j = nextIndexes.at(i);
139  Point target = segments.at(j).target();
140  double angle = get_angle(start, end, target);
141  nextAngles.push_back(std::pair<double, int>(angle, j));
142  }
143  std::sort(nextAngles.begin(), nextAngles.end());
144  int i = nextAngles.begin()->second;
145  unusedIndexes.erase(i);
146  find_next_edge(segments.at(i), segments, unusedIndexes, rings);
147  }
148 
149  if (!unusedIndexes.empty()) {
150  for (unsigned int i = 0; i < segments.size(); i++) {
151  if (unusedIndexes.find(i) != unusedIndexes.end()) {
152  Polygon_2 ring;
153  ring.push_back(segments.at(i).source());
154  rings.push_back(ring);
155  unusedIndexes.erase(i);
156  find_next_edge(segments.at(i), segments, unusedIndexes, rings);
157  }
158  }
159  }
160 }
size_t prev_size
K::Point_2 Point
double get_angle(Point p, Point q, Point r)
CGAL::Polygon_2< K > Polygon_2
void find_next_edge(Segment s, std::vector< Segment > &segments, std::set< int > &unusedIndexes, std::vector< Polygon_2 > &rings)

Here is the call graph for this function:

Here is the caller graph for this function:

double get_angle ( Point  p,
Point  q,
Point  r 
)

Definition at line 95 of file alpha_driver.cpp.

Referenced by find_next_edge().

95  {
96  double m_pi(3.14159265358979323846);
97  Vector v1(q, p);
98  Vector v2(q, r);
99  double cross = v1.x() * v2.y() - v1.y() * v2.x();
100  double dot = v1.x() * v2.x() + v1.y() * v2.y();
101  double angle = atan2(cross, dot);
102  if (angle < 0.0) {
103  angle += 2 * m_pi;
104  }
105  return angle;
106 }
K::Vector_2 Vector

Here is the caller graph for this function:

Variable Documentation

size_t prev_size = 0

Definition at line 108 of file alpha_driver.cpp.

Referenced by find_next_edge().