Loading [MathJax]/extensions/tex2jax.js
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Alpha_complex.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): Vincent Rouvreau
4  *
5  * Copyright (C) 2015 Inria
6  *
7  * Modification(s):
8  * - 2019/08 Vincent Rouvreau: Fix issue #10 for CGAL and Eigen3
9  * - YYYY/MM Author: Description of the modification
10  */
11 
12 #ifndef ALPHA_COMPLEX_H_
13 #define ALPHA_COMPLEX_H_
14 
15 #include <gudhi/Debug_utils.h>
16 // to construct Alpha_complex from a OFF file of points
17 #include <gudhi/Points_off_io.h>
18 
19 #include <stdlib.h>
20 #include <math.h> // isnan, fmax
21 
22 #include <CGAL/Delaunay_triangulation.h>
23 #include <CGAL/Epick_d.h>
24 #include <CGAL/Spatial_sort_traits_adapter_d.h>
25 #include <CGAL/property_map.h> // for CGAL::Identity_property_map
26 #include <CGAL/NT_converter.h>
27 #include <CGAL/version.h> // for CGAL_VERSION_NR
28 
29 #include <Eigen/src/Core/util/Macros.h> // for EIGEN_VERSION_AT_LEAST
30 
31 #include <iostream>
32 #include <vector>
33 #include <string>
34 #include <limits> // NaN
35 #include <map>
36 #include <utility> // std::pair
37 #include <stdexcept>
38 #include <numeric> // for std::iota
39 
40 // Make compilation fail - required for external projects - https://github.com/GUDHI/gudhi-devel/issues/10
41 #if CGAL_VERSION_NR < 1041101000
42 # error Alpha_complex_3d is only available for CGAL >= 4.11
43 #endif
44 
45 #if !EIGEN_VERSION_AT_LEAST(3,1,0)
46 # error Alpha_complex_3d is only available for Eigen3 >= 3.1.0 installed with CGAL
47 #endif
48 
49 namespace Gudhi {
50 
51 namespace alpha_complex {
52 
76 template<class Kernel = CGAL::Epick_d<CGAL::Dynamic_dimension_tag>>
78  public:
79  // Add an int in TDS to save point index in the structure
80  typedef CGAL::Triangulation_data_structure<typename Kernel::Dimension,
81  CGAL::Triangulation_vertex<Kernel, std::ptrdiff_t>,
82  CGAL::Triangulation_full_cell<Kernel> > TDS;
84  typedef CGAL::Delaunay_triangulation<Kernel, TDS> Delaunay_triangulation;
85 
87  typedef typename Kernel::Point_d Point_d;
90  typedef Kernel Geom_traits;
91 
92  private:
93  typedef typename Kernel::Compute_squared_radius_d Squared_Radius;
94  typedef typename Kernel::Side_of_bounded_sphere_d Is_Gabriel;
95  typedef typename Kernel::Point_dimension_d Point_Dimension;
96 
97  // Type required to compute squared radius, or side of bounded sphere on a vector of points.
98  typedef typename std::vector<Point_d> Vector_of_CGAL_points;
99 
100  // Vertex_iterator type from CGAL.
101  typedef typename Delaunay_triangulation::Vertex_iterator CGAL_vertex_iterator;
102 
103  // size_type type from CGAL.
104  typedef typename Delaunay_triangulation::size_type size_type;
105 
106  // Map type to switch from simplex tree vertex handle to CGAL vertex iterator.
107  typedef typename std::map< std::size_t, CGAL_vertex_iterator > Vector_vertex_iterator;
108 
109  private:
112  Vector_vertex_iterator vertex_handle_to_iterator_;
114  Delaunay_triangulation* triangulation_;
116  Kernel kernel_;
117 
118  public:
128  Alpha_complex(const std::string& off_file_name)
129  : triangulation_(nullptr) {
130  Gudhi::Points_off_reader<Point_d> off_reader(off_file_name);
131  if (!off_reader.is_valid()) {
132  std::cerr << "Alpha_complex - Unable to read file " << off_file_name << "\n";
133  exit(-1); // ----- >>
134  }
135 
136  init_from_range(off_reader.get_point_cloud());
137  }
138 
148  template<typename InputPointRange >
149  Alpha_complex(const InputPointRange& points)
150  : triangulation_(nullptr) {
151  init_from_range(points);
152  }
153 
157  delete triangulation_;
158  }
159 
160  // Forbid copy/move constructor/assignment operator
161  Alpha_complex(const Alpha_complex& other) = delete;
162  Alpha_complex& operator= (const Alpha_complex& other) = delete;
163  Alpha_complex (Alpha_complex&& other) = delete;
164  Alpha_complex& operator= (Alpha_complex&& other) = delete;
165 
172  const Point_d& get_point(std::size_t vertex) const {
173  return vertex_handle_to_iterator_.at(vertex)->point();
174  }
175 
180  std::size_t number_of_vertices() const {
181  return vertex_handle_to_iterator_.size();
182  }
183 
184  private:
185  template<typename InputPointRange >
186  void init_from_range(const InputPointRange& points) {
187  auto first = std::begin(points);
188  auto last = std::end(points);
189 
190  if (first != last) {
191  // point_dimension function initialization
192  Point_Dimension point_dimension = kernel_.point_dimension_d_object();
193 
194  // Delaunay triangulation is point dimension.
195  triangulation_ = new Delaunay_triangulation(point_dimension(*first));
196 
197  std::vector<Point_d> point_cloud(first, last);
198 
199  // Creates a vector {0, 1, ..., N-1}
200  std::vector<std::ptrdiff_t> indices(boost::counting_iterator<std::ptrdiff_t>(0),
201  boost::counting_iterator<std::ptrdiff_t>(point_cloud.size()));
202 
203  typedef boost::iterator_property_map<typename std::vector<Point_d>::iterator,
204  CGAL::Identity_property_map<std::ptrdiff_t>> Point_property_map;
205  typedef CGAL::Spatial_sort_traits_adapter_d<Kernel, Point_property_map> Search_traits_d;
206 
207  CGAL::spatial_sort(indices.begin(), indices.end(), Search_traits_d(std::begin(point_cloud)));
208 
209  typename Delaunay_triangulation::Full_cell_handle hint;
210  for (auto index : indices) {
211  typename Delaunay_triangulation::Vertex_handle pos = triangulation_->insert(point_cloud[index], hint);
212  // Save index value as data to retrieve it after insertion
213  pos->data() = index;
214  hint = pos->full_cell();
215  }
216  // --------------------------------------------------------------------------------------------
217  // double map to retrieve simplex tree vertex handles from CGAL vertex iterator and vice versa
218  // Loop on triangulation vertices list
219  for (CGAL_vertex_iterator vit = triangulation_->vertices_begin(); vit != triangulation_->vertices_end(); ++vit) {
220  if (!triangulation_->is_infinite(*vit)) {
221 #ifdef DEBUG_TRACES
222  std::cout << "Vertex insertion - " << vit->data() << " -> " << vit->point() << std::endl;
223 #endif // DEBUG_TRACES
224  vertex_handle_to_iterator_.emplace(vit->data(), vit);
225  }
226  }
227  // --------------------------------------------------------------------------------------------
228  }
229  }
230 
231  public:
248  template <typename SimplicialComplexForAlpha,
251  Filtration_value max_alpha_square = std::numeric_limits<Filtration_value>::infinity()) {
252  // From SimplicialComplexForAlpha type required to insert into a simplicial complex (with or without subfaces).
253  typedef typename SimplicialComplexForAlpha::Vertex_handle Vertex_handle;
254  typedef typename SimplicialComplexForAlpha::Simplex_handle Simplex_handle;
255  typedef std::vector<Vertex_handle> Vector_vertex;
256 
257  if (triangulation_ == nullptr) {
258  std::cerr << "Alpha_complex cannot create_complex from a NULL triangulation\n";
259  return false; // ----- >>
260  }
261  if (triangulation_->maximal_dimension() < 1) {
262  std::cerr << "Alpha_complex cannot create_complex from a zero-dimension triangulation\n";
263  return false; // ----- >>
264  }
265  if (complex.num_vertices() > 0) {
266  std::cerr << "Alpha_complex create_complex - complex is not empty\n";
267  return false; // ----- >>
268  }
269 
270  // --------------------------------------------------------------------------------------------
271  // Simplex_tree construction from loop on triangulation finite full cells list
272  if (triangulation_->number_of_vertices() > 0) {
273  for (auto cit = triangulation_->finite_full_cells_begin();
274  cit != triangulation_->finite_full_cells_end();
275  ++cit) {
276  Vector_vertex vertexVector;
277 #ifdef DEBUG_TRACES
278  std::cout << "Simplex_tree insertion ";
279 #endif // DEBUG_TRACES
280  for (auto vit = cit->vertices_begin(); vit != cit->vertices_end(); ++vit) {
281  if (*vit != nullptr) {
282 #ifdef DEBUG_TRACES
283  std::cout << " " << (*vit)->data();
284 #endif // DEBUG_TRACES
285  // Vector of vertex construction for simplex_tree structure
286  vertexVector.push_back((*vit)->data());
287  }
288  }
289 #ifdef DEBUG_TRACES
290  std::cout << std::endl;
291 #endif // DEBUG_TRACES
292  // Insert each simplex and its subfaces in the simplex tree - filtration is NaN
293  complex.insert_simplex_and_subfaces(vertexVector, std::numeric_limits<double>::quiet_NaN());
294  }
295  }
296  // --------------------------------------------------------------------------------------------
297 
298  // --------------------------------------------------------------------------------------------
299  // Will be re-used many times
300  Vector_of_CGAL_points pointVector;
301  // ### For i : d -> 0
302  for (int decr_dim = triangulation_->maximal_dimension(); decr_dim >= 0; decr_dim--) {
303  // ### Foreach Sigma of dim i
304  for (Simplex_handle f_simplex : complex.skeleton_simplex_range(decr_dim)) {
305  int f_simplex_dim = complex.dimension(f_simplex);
306  if (decr_dim == f_simplex_dim) {
307  pointVector.clear();
308 #ifdef DEBUG_TRACES
309  std::cout << "Sigma of dim " << decr_dim << " is";
310 #endif // DEBUG_TRACES
311  for (auto vertex : complex.simplex_vertex_range(f_simplex)) {
312  pointVector.push_back(get_point(vertex));
313 #ifdef DEBUG_TRACES
314  std::cout << " " << vertex;
315 #endif // DEBUG_TRACES
316  }
317 #ifdef DEBUG_TRACES
318  std::cout << std::endl;
319 #endif // DEBUG_TRACES
320  // ### If filt(Sigma) is NaN : filt(Sigma) = alpha(Sigma)
321  if (std::isnan(complex.filtration(f_simplex))) {
322  Filtration_value alpha_complex_filtration = 0.0;
323  // No need to compute squared_radius on a single point - alpha is 0.0
324  if (f_simplex_dim > 0) {
325  // squared_radius function initialization
326  Squared_Radius squared_radius = kernel_.compute_squared_radius_d_object();
327  CGAL::NT_converter<typename Geom_traits::FT, Filtration_value> cv;
328 
329  alpha_complex_filtration = cv(squared_radius(pointVector.begin(), pointVector.end()));
330  }
331  complex.assign_filtration(f_simplex, alpha_complex_filtration);
332 #ifdef DEBUG_TRACES
333  std::cout << "filt(Sigma) is NaN : filt(Sigma) =" << complex.filtration(f_simplex) << std::endl;
334 #endif // DEBUG_TRACES
335  }
336  // No need to propagate further, unweighted points all have value 0
337  if (decr_dim > 1)
338  propagate_alpha_filtration(complex, f_simplex);
339  }
340  }
341  }
342  // --------------------------------------------------------------------------------------------
343 
344  // --------------------------------------------------------------------------------------------
345  // As Alpha value is an approximation, we have to make filtration non decreasing while increasing the dimension
347  // Remove all simplices that have a filtration value greater than max_alpha_square
348  complex.prune_above_filtration(max_alpha_square);
349  // --------------------------------------------------------------------------------------------
350  return true;
351  }
352 
353  private:
354  template <typename SimplicialComplexForAlpha, typename Simplex_handle>
355  void propagate_alpha_filtration(SimplicialComplexForAlpha& complex, Simplex_handle f_simplex) {
356  // From SimplicialComplexForAlpha type required to assign filtration values.
357  typedef typename SimplicialComplexForAlpha::Filtration_value Filtration_value;
358 #ifdef DEBUG_TRACES
359  typedef typename SimplicialComplexForAlpha::Vertex_handle Vertex_handle;
360 #endif // DEBUG_TRACES
361 
362  // ### Foreach Tau face of Sigma
363  for (auto f_boundary : complex.boundary_simplex_range(f_simplex)) {
364 #ifdef DEBUG_TRACES
365  std::cout << " | --------------------------------------------------\n";
366  std::cout << " | Tau ";
367  for (auto vertex : complex.simplex_vertex_range(f_boundary)) {
368  std::cout << vertex << " ";
369  }
370  std::cout << "is a face of Sigma\n";
371  std::cout << " | isnan(complex.filtration(Tau)=" << std::isnan(complex.filtration(f_boundary)) << std::endl;
372 #endif // DEBUG_TRACES
373  // ### If filt(Tau) is not NaN
374  if (!std::isnan(complex.filtration(f_boundary))) {
375  // ### filt(Tau) = fmin(filt(Tau), filt(Sigma))
376  Filtration_value alpha_complex_filtration = fmin(complex.filtration(f_boundary),
377  complex.filtration(f_simplex));
378  complex.assign_filtration(f_boundary, alpha_complex_filtration);
379 #ifdef DEBUG_TRACES
380  std::cout << " | filt(Tau) = fmin(filt(Tau), filt(Sigma)) = " << complex.filtration(f_boundary) << std::endl;
381 #endif // DEBUG_TRACES
382  // ### Else
383  } else {
384  // insert the Tau points in a vector for is_gabriel function
385  Vector_of_CGAL_points pointVector;
386 #ifdef DEBUG_TRACES
387  Vertex_handle vertexForGabriel = Vertex_handle();
388 #endif // DEBUG_TRACES
389  for (auto vertex : complex.simplex_vertex_range(f_boundary)) {
390  pointVector.push_back(get_point(vertex));
391  }
392  // Retrieve the Sigma point that is not part of Tau - parameter for is_gabriel function
393  Point_d point_for_gabriel;
394  for (auto vertex : complex.simplex_vertex_range(f_simplex)) {
395  point_for_gabriel = get_point(vertex);
396  if (std::find(pointVector.begin(), pointVector.end(), point_for_gabriel) == pointVector.end()) {
397 #ifdef DEBUG_TRACES
398  // vertex is not found in Tau
399  vertexForGabriel = vertex;
400 #endif // DEBUG_TRACES
401  // No need to continue loop
402  break;
403  }
404  }
405  // is_gabriel function initialization
406  Is_Gabriel is_gabriel = kernel_.side_of_bounded_sphere_d_object();
407  bool is_gab = is_gabriel(pointVector.begin(), pointVector.end(), point_for_gabriel)
408  != CGAL::ON_BOUNDED_SIDE;
409 #ifdef DEBUG_TRACES
410  std::cout << " | Tau is_gabriel(Sigma)=" << is_gab << " - vertexForGabriel=" << vertexForGabriel << std::endl;
411 #endif // DEBUG_TRACES
412  // ### If Tau is not Gabriel of Sigma
413  if (false == is_gab) {
414  // ### filt(Tau) = filt(Sigma)
415  Filtration_value alpha_complex_filtration = complex.filtration(f_simplex);
416  complex.assign_filtration(f_boundary, alpha_complex_filtration);
417 #ifdef DEBUG_TRACES
418  std::cout << " | filt(Tau) = filt(Sigma) = " << complex.filtration(f_boundary) << std::endl;
419 #endif // DEBUG_TRACES
420  }
421  }
422  }
423  }
424 };
425 
426 } // namespace alpha_complex
427 
428 namespace alphacomplex = alpha_complex;
429 
430 } // namespace Gudhi
431 
432 #endif // ALPHA_COMPLEX_H_
OFF file reader implementation in order to read points from an OFF file.
Definition: Points_off_io.h:122
void insert_simplex_and_subfaces(std::vector< Vertex_handle > const &vertex_range, Filtration_value filtration)
Inserts a simplex with vertices from a given simplex (represented by a vector of Vertex_handle) in th...
Simplex_vertex_range simplex_vertex_range(Simplex_handle const &simplex)
Returns a range over vertices of a given simplex.
Definition: SimplicialComplexForAlpha.h:14
const std::vector< Point_d > & get_point_cloud() const
Point cloud getter.
Definition: Points_off_io.h:158
~Alpha_complex()
Alpha_complex destructor deletes the Delaunay triangulation.
Definition: Alpha_complex.h:156
Kernel::Point_d Point_d
A point in Euclidean space.
Definition: Alpha_complex.h:87
Alpha_complex(const InputPointRange &points)
Alpha_complex constructor from a list of points.
Definition: Alpha_complex.h:149
Alpha_complex(const std::string &off_file_name)
Alpha_complex constructor from an OFF file name.
Definition: Alpha_complex.h:128
unspecified Vertex_handle
Definition: SimplicialComplexForAlpha.h:25
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20
bool is_valid() const
Returns if the OFF file read operation was successful or not.
Definition: Points_off_io.h:150
The concept SimplicialComplexForAlpha describes the requirements for a type to implement a simplicial...
Definition: SimplicialComplexForAlpha.h:21
bool create_complex(SimplicialComplexForAlpha &complex, Filtration_value max_alpha_square=std::numeric_limits< Filtration_value >::infinity())
Inserts all Delaunay triangulation into the simplicial complex. It also computes the filtration value...
Definition: Alpha_complex.h:250
void prune_above_filtration(Filtration_value filtration)
Kernel Geom_traits
Geometric traits class that provides the geometric types and predicates needed by Delaunay triangulat...
Definition: Alpha_complex.h:90
Alpha complex data structure.
Definition: Alpha_complex.h:77
Skeleton_simplex_range skeleton_simplex_range
Returns a range over the simplices of the skeleton of the simplicial complex, for a given dimension...
Definition: SimplicialComplexForAlpha.h:70
CGAL::Delaunay_triangulation< Kernel, TDS > Delaunay_triangulation
A Delaunay triangulation of a set of points in .
Definition: Alpha_complex.h:84
int assign_filtration(Simplex_handle simplex, Filtration_value filtration)
unspecified Simplex_handle
Definition: SimplicialComplexForAlpha.h:23
Boundary_simplex_range boundary_simplex_range(Simplex_handle const &simplex)
Returns a range over boundaries of a given simplex.
std::size_t number_of_vertices() const
number_of_vertices returns the number of vertices (same as the number of points). ...
Definition: Alpha_complex.h:180
unspecified Filtration_value
Definition: SimplicialComplexForAlpha.h:27
const Point_d & get_point(std::size_t vertex) const
get_point returns the point corresponding to the vertex given as parameter.
Definition: Alpha_complex.h:172
GUDHI  Version 3.0.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Sep 24 2019 09:57:50 for GUDHI by Doxygen 1.8.13