Alpha_complex.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Vincent Rouvreau
6  *
7  * Copyright (C) 2015 INRIA
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 3 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, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef ALPHA_COMPLEX_H_
24 #define ALPHA_COMPLEX_H_
25 
26 #include <gudhi/Debug_utils.h>
27 // to construct Alpha_complex from a OFF file of points
28 #include <gudhi/Points_off_io.h>
29 
30 #include <stdlib.h>
31 #include <math.h> // isnan, fmax
32 
33 #include <CGAL/Delaunay_triangulation.h>
34 #include <CGAL/Epick_d.h>
35 #include <CGAL/Spatial_sort_traits_adapter_d.h>
36 #include <CGAL/property_map.h> // for CGAL::Identity_property_map
37 
38 #include <iostream>
39 #include <vector>
40 #include <string>
41 #include <limits> // NaN
42 #include <map>
43 #include <utility> // std::pair
44 #include <stdexcept>
45 #include <numeric> // for std::iota
46 
47 namespace Gudhi {
48 
49 namespace alpha_complex {
50 
74 template<class Kernel = CGAL::Epick_d<CGAL::Dynamic_dimension_tag>>
76  public:
77  // Add an int in TDS to save point index in the structure
78  typedef CGAL::Triangulation_data_structure<typename Kernel::Dimension,
79  CGAL::Triangulation_vertex<Kernel, std::ptrdiff_t>,
80  CGAL::Triangulation_full_cell<Kernel> > TDS;
82  typedef CGAL::Delaunay_triangulation<Kernel, TDS> Delaunay_triangulation;
83 
85  typedef typename Kernel::Point_d Point_d;
88  typedef Kernel Geom_traits;
89 
90  private:
91  typedef typename Kernel::Compute_squared_radius_d Squared_Radius;
92  typedef typename Kernel::Side_of_bounded_sphere_d Is_Gabriel;
93  typedef typename Kernel::Point_dimension_d Point_Dimension;
94 
95  // Type required to compute squared radius, or side of bounded sphere on a vector of points.
96  typedef typename std::vector<Point_d> Vector_of_CGAL_points;
97 
98  // Vertex_iterator type from CGAL.
99  typedef typename Delaunay_triangulation::Vertex_iterator CGAL_vertex_iterator;
100 
101  // size_type type from CGAL.
102  typedef typename Delaunay_triangulation::size_type size_type;
103 
104  // Map type to switch from simplex tree vertex handle to CGAL vertex iterator.
105  typedef typename std::map< std::size_t, CGAL_vertex_iterator > Vector_vertex_iterator;
106 
107  private:
110  Vector_vertex_iterator vertex_handle_to_iterator_;
112  Delaunay_triangulation* triangulation_;
114  Kernel kernel_;
115 
116  public:
126  Alpha_complex(const std::string& off_file_name)
127  : triangulation_(nullptr) {
128  Gudhi::Points_off_reader<Point_d> off_reader(off_file_name);
129  if (!off_reader.is_valid()) {
130  std::cerr << "Alpha_complex - Unable to read file " << off_file_name << "\n";
131  exit(-1); // ----- >>
132  }
133 
134  init_from_range(off_reader.get_point_cloud());
135  }
136 
146  template<typename InputPointRange >
147  Alpha_complex(const InputPointRange& points)
148  : triangulation_(nullptr) {
149  init_from_range(points);
150  }
151 
155  delete triangulation_;
156  }
157 
158  // Forbid copy/move constructor/assignment operator
159  Alpha_complex(const Alpha_complex& other) = delete;
160  Alpha_complex& operator= (const Alpha_complex& other) = delete;
161  Alpha_complex (Alpha_complex&& other) = delete;
162  Alpha_complex& operator= (Alpha_complex&& other) = delete;
163 
170  const Point_d& get_point(std::size_t vertex) const {
171  return vertex_handle_to_iterator_.at(vertex)->point();
172  }
173 
178  std::size_t number_of_vertices() const {
179  return vertex_handle_to_iterator_.size();
180  }
181 
182  private:
183  template<typename InputPointRange >
184  void init_from_range(const InputPointRange& points) {
185  auto first = std::begin(points);
186  auto last = std::end(points);
187 
188  if (first != last) {
189  // point_dimension function initialization
190  Point_Dimension point_dimension = kernel_.point_dimension_d_object();
191 
192  // Delaunay triangulation is point dimension.
193  triangulation_ = new Delaunay_triangulation(point_dimension(*first));
194 
195  std::vector<Point_d> point_cloud(first, last);
196 
197  // Creates a vector {0, 1, ..., N-1}
198  std::vector<std::ptrdiff_t> indices(boost::counting_iterator<std::ptrdiff_t>(0),
199  boost::counting_iterator<std::ptrdiff_t>(point_cloud.size()));
200 
201  typedef boost::iterator_property_map<typename std::vector<Point_d>::iterator,
202  CGAL::Identity_property_map<std::ptrdiff_t>> Point_property_map;
203  typedef CGAL::Spatial_sort_traits_adapter_d<Kernel, Point_property_map> Search_traits_d;
204 
205  CGAL::spatial_sort(indices.begin(), indices.end(), Search_traits_d(std::begin(point_cloud)));
206 
207  typename Delaunay_triangulation::Full_cell_handle hint;
208  for (auto index : indices) {
209  typename Delaunay_triangulation::Vertex_handle pos = triangulation_->insert(point_cloud[index], hint);
210  // Save index value as data to retrieve it after insertion
211  pos->data() = index;
212  hint = pos->full_cell();
213  }
214  // --------------------------------------------------------------------------------------------
215  // double map to retrieve simplex tree vertex handles from CGAL vertex iterator and vice versa
216  // Loop on triangulation vertices list
217  for (CGAL_vertex_iterator vit = triangulation_->vertices_begin(); vit != triangulation_->vertices_end(); ++vit) {
218  if (!triangulation_->is_infinite(*vit)) {
219 #ifdef DEBUG_TRACES
220  std::cout << "Vertex insertion - " << vit->data() << " -> " << vit->point() << std::endl;
221 #endif // DEBUG_TRACES
222  vertex_handle_to_iterator_.emplace(vit->data(), vit);
223  }
224  }
225  // --------------------------------------------------------------------------------------------
226  }
227  }
228 
229  public:
230  template <typename SimplicialComplexForAlpha>
231  bool create_complex(SimplicialComplexForAlpha& complex) {
233  return create_complex(complex, std::numeric_limits<Filtration_value>::infinity());
234  }
235 
251  template <typename SimplicialComplexForAlpha, typename Filtration_value>
252  bool create_complex(SimplicialComplexForAlpha& complex, Filtration_value max_alpha_square) {
253  // From SimplicialComplexForAlpha type required to insert into a simplicial complex (with or without subfaces).
254  typedef typename SimplicialComplexForAlpha::Vertex_handle Vertex_handle;
255  typedef typename SimplicialComplexForAlpha::Simplex_handle Simplex_handle;
256  typedef std::vector<Vertex_handle> Vector_vertex;
257 
258  if (triangulation_ == nullptr) {
259  std::cerr << "Alpha_complex cannot create_complex from a NULL triangulation\n";
260  return false; // ----- >>
261  }
262  if (triangulation_->maximal_dimension() < 1) {
263  std::cerr << "Alpha_complex cannot create_complex from a zero-dimension triangulation\n";
264  return false; // ----- >>
265  }
266  if (complex.num_vertices() > 0) {
267  std::cerr << "Alpha_complex create_complex - complex is not empty\n";
268  return false; // ----- >>
269  }
270 
271  // --------------------------------------------------------------------------------------------
272  // Simplex_tree construction from loop on triangulation finite full cells list
273  if (triangulation_->number_of_vertices() > 0) {
274  for (auto cit = triangulation_->finite_full_cells_begin(); cit != triangulation_->finite_full_cells_end(); ++cit) {
275  Vector_vertex vertexVector;
276 #ifdef DEBUG_TRACES
277  std::cout << "Simplex_tree insertion ";
278 #endif // DEBUG_TRACES
279  for (auto vit = cit->vertices_begin(); vit != cit->vertices_end(); ++vit) {
280  if (*vit != nullptr) {
281 #ifdef DEBUG_TRACES
282  std::cout << " " << (*vit)->data();
283 #endif // DEBUG_TRACES
284  // Vector of vertex construction for simplex_tree structure
285  vertexVector.push_back((*vit)->data());
286  }
287  }
288 #ifdef DEBUG_TRACES
289  std::cout << std::endl;
290 #endif // DEBUG_TRACES
291  // Insert each simplex and its subfaces in the simplex tree - filtration is NaN
292  complex.insert_simplex_and_subfaces(vertexVector, std::numeric_limits<double>::quiet_NaN());
293  }
294  }
295  // --------------------------------------------------------------------------------------------
296 
297  // --------------------------------------------------------------------------------------------
298  // Will be re-used many times
299  Vector_of_CGAL_points pointVector;
300  // ### For i : d -> 0
301  for (int decr_dim = triangulation_->maximal_dimension(); decr_dim >= 0; decr_dim--) {
302  // ### Foreach Sigma of dim i
303  for (Simplex_handle f_simplex : complex.skeleton_simplex_range(decr_dim)) {
304  int f_simplex_dim = complex.dimension(f_simplex);
305  if (decr_dim == f_simplex_dim) {
306  pointVector.clear();
307 #ifdef DEBUG_TRACES
308  std::cout << "Sigma of dim " << decr_dim << " is";
309 #endif // DEBUG_TRACES
310  for (auto vertex : complex.simplex_vertex_range(f_simplex)) {
311  pointVector.push_back(get_point(vertex));
312 #ifdef DEBUG_TRACES
313  std::cout << " " << vertex;
314 #endif // DEBUG_TRACES
315  }
316 #ifdef DEBUG_TRACES
317  std::cout << std::endl;
318 #endif // DEBUG_TRACES
319  // ### If filt(Sigma) is NaN : filt(Sigma) = alpha(Sigma)
320  if (std::isnan(complex.filtration(f_simplex))) {
321  Filtration_value alpha_complex_filtration = 0.0;
322  // No need to compute squared_radius on a single point - alpha is 0.0
323  if (f_simplex_dim > 0) {
324  // squared_radius function initialization
325  Squared_Radius squared_radius = kernel_.compute_squared_radius_d_object();
326 
327  alpha_complex_filtration = squared_radius(pointVector.begin(), pointVector.end());
328  }
329  complex.assign_filtration(f_simplex, alpha_complex_filtration);
330 #ifdef DEBUG_TRACES
331  std::cout << "filt(Sigma) is NaN : filt(Sigma) =" << complex.filtration(f_simplex) << std::endl;
332 #endif // DEBUG_TRACES
333  }
334  propagate_alpha_filtration(complex, f_simplex, decr_dim);
335  }
336  }
337  }
338  // --------------------------------------------------------------------------------------------
339 
340  // --------------------------------------------------------------------------------------------
341  // As Alpha value is an approximation, we have to make filtration non decreasing while increasing the dimension
343  // Remove all simplices that have a filtration value greater than max_alpha_square
344  complex.prune_above_filtration(max_alpha_square);
345  // --------------------------------------------------------------------------------------------
346  return true;
347  }
348 
349  private:
350  template <typename SimplicialComplexForAlpha, typename Simplex_handle>
351  void propagate_alpha_filtration(SimplicialComplexForAlpha& complex, Simplex_handle f_simplex, int decr_dim) {
352  // From SimplicialComplexForAlpha type required to assign filtration values.
354 #ifdef DEBUG_TRACES
355  typedef typename SimplicialComplexForAlpha::Vertex_handle Vertex_handle;
356 #endif // DEBUG_TRACES
357 
358  // ### Foreach Tau face of Sigma
359  for (auto f_boundary : complex.boundary_simplex_range(f_simplex)) {
360 #ifdef DEBUG_TRACES
361  std::cout << " | --------------------------------------------------\n";
362  std::cout << " | Tau ";
363  for (auto vertex : complex.simplex_vertex_range(f_boundary)) {
364  std::cout << vertex << " ";
365  }
366  std::cout << "is a face of Sigma\n";
367  std::cout << " | isnan(complex.filtration(Tau)=" << std::isnan(complex.filtration(f_boundary)) << std::endl;
368 #endif // DEBUG_TRACES
369  // ### If filt(Tau) is not NaN
370  if (!std::isnan(complex.filtration(f_boundary))) {
371  // ### filt(Tau) = fmin(filt(Tau), filt(Sigma))
372  Filtration_value alpha_complex_filtration = fmin(complex.filtration(f_boundary),
373  complex.filtration(f_simplex));
374  complex.assign_filtration(f_boundary, alpha_complex_filtration);
375 #ifdef DEBUG_TRACES
376  std::cout << " | filt(Tau) = fmin(filt(Tau), filt(Sigma)) = " << complex.filtration(f_boundary) << std::endl;
377 #endif // DEBUG_TRACES
378  // ### Else
379  } else {
380  // No need to compute is_gabriel for dimension <= 2
381  // i.e. : Sigma = (3,1) => Tau = 1
382  if (decr_dim > 1) {
383  // insert the Tau points in a vector for is_gabriel function
384  Vector_of_CGAL_points pointVector;
385 #ifdef DEBUG_TRACES
386  Vertex_handle vertexForGabriel = Vertex_handle();
387 #endif // DEBUG_TRACES
388  for (auto vertex : complex.simplex_vertex_range(f_boundary)) {
389  pointVector.push_back(get_point(vertex));
390  }
391  // Retrieve the Sigma point that is not part of Tau - parameter for is_gabriel function
392  Point_d point_for_gabriel;
393  for (auto vertex : complex.simplex_vertex_range(f_simplex)) {
394  point_for_gabriel = get_point(vertex);
395  if (std::find(pointVector.begin(), pointVector.end(), point_for_gabriel) == pointVector.end()) {
396 #ifdef DEBUG_TRACES
397  // vertex is not found in Tau
398  vertexForGabriel = vertex;
399 #endif // DEBUG_TRACES
400  // No need to continue loop
401  break;
402  }
403  }
404  // is_gabriel function initialization
405  Is_Gabriel is_gabriel = kernel_.side_of_bounded_sphere_d_object();
406  bool is_gab = is_gabriel(pointVector.begin(), pointVector.end(), point_for_gabriel)
407  != CGAL::ON_BOUNDED_SIDE;
408 #ifdef DEBUG_TRACES
409  std::cout << " | Tau is_gabriel(Sigma)=" << is_gab << " - vertexForGabriel=" << vertexForGabriel << std::endl;
410 #endif // DEBUG_TRACES
411  // ### If Tau is not Gabriel of Sigma
412  if (false == is_gab) {
413  // ### filt(Tau) = filt(Sigma)
414  Filtration_value alpha_complex_filtration = complex.filtration(f_simplex);
415  complex.assign_filtration(f_boundary, alpha_complex_filtration);
416 #ifdef DEBUG_TRACES
417  std::cout << " | filt(Tau) = filt(Sigma) = " << complex.filtration(f_boundary) << std::endl;
418 #endif // DEBUG_TRACES
419  }
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:134
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...
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:178
Simplex_vertex_range simplex_vertex_range(Simplex_handle const &simplex)
Returns a range over vertices of a given simplex.
Definition: SimplicialComplexForAlpha.h:26
const std::vector< Point_d > & get_point_cloud() const
Point cloud getter.
Definition: Points_off_io.h:170
~Alpha_complex()
Alpha_complex destructor deletes the Delaunay triangulation.
Definition: Alpha_complex.h:154
Kernel::Point_d Point_d
A point in Euclidean space.
Definition: Alpha_complex.h:85
Alpha_complex(const InputPointRange &points)
Alpha_complex constructor from a list of points.
Definition: Alpha_complex.h:147
Alpha_complex(const std::string &off_file_name)
Alpha_complex constructor from an OFF file name.
Definition: Alpha_complex.h:126
unspecified Vertex_handle
Definition: SimplicialComplexForAlpha.h:37
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:32
The concept SimplicialComplexForAlpha describes the requirements for a type to implement a simplicial...
Definition: SimplicialComplexForAlpha.h:33
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:88
Alpha complex data structure.
Definition: Alpha_complex.h:75
CGAL::Delaunay_triangulation< Kernel, TDS > Delaunay_triangulation
A Delaunay triangulation of a set of points in .
Definition: Alpha_complex.h:82
bool create_complex(SimplicialComplexForAlpha &complex, Filtration_value max_alpha_square)
Inserts all Delaunay triangulation into the simplicial complex. It also computes the filtration value...
Definition: Alpha_complex.h:252
int assign_filtration(Simplex_handle simplex, Filtration_value filtration)
bool is_valid() const
Returns if the OFF file read operation was successful or not.
Definition: Points_off_io.h:162
unspecified Simplex_handle
Definition: SimplicialComplexForAlpha.h:35
Boundary_simplex_range boundary_simplex_range(Simplex_handle const &simplex)
Returns a range over boundaries of a given simplex.
unspecified Filtration_value
Definition: SimplicialComplexForAlpha.h:39
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:170
GUDHI  Version 2.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Wed Jan 31 2018 09:40:55 for GUDHI by Doxygen 1.8.11