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/Epeck_d.h> // For EXACT or SAFE version
24 #include <CGAL/Epick_d.h> // For FAST version
25 #include <CGAL/Spatial_sort_traits_adapter_d.h>
26 #include <CGAL/property_map.h> // for CGAL::Identity_property_map
27 #include <CGAL/version.h> // for CGAL_VERSION_NR
28 #include <CGAL/NT_converter.h>
29 
30 #include <Eigen/src/Core/util/Macros.h> // for EIGEN_VERSION_AT_LEAST
31 
32 #include <iostream>
33 #include <vector>
34 #include <string>
35 #include <limits> // NaN
36 #include <map>
37 #include <utility> // std::pair
38 #include <stdexcept>
39 #include <numeric> // for std::iota
40 
41 // Make compilation fail - required for external projects - https://github.com/GUDHI/gudhi-devel/issues/10
42 #if CGAL_VERSION_NR < 1041101000
43 # error Alpha_complex is only available for CGAL >= 4.11
44 #endif
45 
46 #if !EIGEN_VERSION_AT_LEAST(3,1,0)
47 # error Alpha_complex is only available for Eigen3 >= 3.1.0 installed with CGAL
48 #endif
49 
50 namespace Gudhi {
51 
52 namespace alpha_complex {
53 
54 template<typename D> struct Is_Epeck_D { static const bool value = false; };
55 template<typename D> struct Is_Epeck_D<CGAL::Epeck_d<D>> { static const bool value = true; };
56 
94 template<class Kernel = CGAL::Epeck_d<CGAL::Dynamic_dimension_tag>>
96  public:
97  // Add an int in TDS to save point index in the structure
98  typedef CGAL::Triangulation_data_structure<typename Kernel::Dimension,
99  CGAL::Triangulation_vertex<Kernel, std::ptrdiff_t>,
100  CGAL::Triangulation_full_cell<Kernel> > TDS;
102  typedef CGAL::Delaunay_triangulation<Kernel, TDS> Delaunay_triangulation;
103 
105  typedef typename Kernel::Point_d Point_d;
108  typedef Kernel Geom_traits;
109 
110  private:
111  typedef typename Kernel::Compute_squared_radius_d Squared_Radius;
112  typedef typename Kernel::Side_of_bounded_sphere_d Is_Gabriel;
113  typedef typename Kernel::Point_dimension_d Point_Dimension;
114 
115  // Vertex_iterator type from CGAL.
116  typedef typename Delaunay_triangulation::Vertex_iterator CGAL_vertex_iterator;
117 
118  // size_type type from CGAL.
119  typedef typename Delaunay_triangulation::size_type size_type;
120 
121  // Structure to switch from simplex tree vertex handle to CGAL vertex iterator.
122  typedef typename std::vector< CGAL_vertex_iterator > Vector_vertex_iterator;
123 
124  // Numeric type of coordinates in the kernel
125  typedef typename Kernel::FT FT;
126 
127  private:
130  Vector_vertex_iterator vertex_handle_to_iterator_;
132  Delaunay_triangulation* triangulation_;
134  Kernel kernel_;
136  std::vector<std::pair<Point_d, FT>> cache_, old_cache_;
137 
138  public:
148  Alpha_complex(const std::string& off_file_name)
149  : triangulation_(nullptr) {
150  Gudhi::Points_off_reader<Point_d> off_reader(off_file_name);
151  if (!off_reader.is_valid()) {
152  std::cerr << "Alpha_complex - Unable to read file " << off_file_name << "\n";
153  exit(-1); // ----- >>
154  }
155 
156  init_from_range(off_reader.get_point_cloud());
157  }
158 
168  template<typename InputPointRange >
169  Alpha_complex(const InputPointRange& points)
170  : triangulation_(nullptr) {
171  init_from_range(points);
172  }
173 
177  delete triangulation_;
178  }
179 
180  // Forbid copy/move constructor/assignment operator
181  Alpha_complex(const Alpha_complex& other) = delete;
182  Alpha_complex& operator= (const Alpha_complex& other) = delete;
183  Alpha_complex (Alpha_complex&& other) = delete;
184  Alpha_complex& operator= (Alpha_complex&& other) = delete;
185 
192  const Point_d& get_point(std::size_t vertex) const {
193  return vertex_handle_to_iterator_.at(vertex)->point();
194  }
195 
196  private:
197  template<typename InputPointRange >
198  void init_from_range(const InputPointRange& points) {
199  #if CGAL_VERSION_NR < 1050000000
200  if (Is_Epeck_D<Kernel>::value)
201  std::cerr << "It is strongly advised to use a CGAL version >= 5.0 with Epeck_d Kernel for performance reasons."
202  << std::endl;
203  #endif
204 
205  auto first = std::begin(points);
206  auto last = std::end(points);
207 
208  if (first != last) {
209  // point_dimension function initialization
210  Point_Dimension point_dimension = kernel_.point_dimension_d_object();
211 
212  // Delaunay triangulation is point dimension.
213  triangulation_ = new Delaunay_triangulation(point_dimension(*first));
214 
215  std::vector<Point_d> point_cloud(first, last);
216 
217  // Creates a vector {0, 1, ..., N-1}
218  std::vector<std::ptrdiff_t> indices(boost::counting_iterator<std::ptrdiff_t>(0),
219  boost::counting_iterator<std::ptrdiff_t>(point_cloud.size()));
220 
221  typedef boost::iterator_property_map<typename std::vector<Point_d>::iterator,
222  CGAL::Identity_property_map<std::ptrdiff_t>> Point_property_map;
223  typedef CGAL::Spatial_sort_traits_adapter_d<Kernel, Point_property_map> Search_traits_d;
224 
225  CGAL::spatial_sort(indices.begin(), indices.end(), Search_traits_d(std::begin(point_cloud)));
226 
227  typename Delaunay_triangulation::Full_cell_handle hint;
228  for (auto index : indices) {
229  typename Delaunay_triangulation::Vertex_handle pos = triangulation_->insert(point_cloud[index], hint);
230  // Save index value as data to retrieve it after insertion
231  pos->data() = index;
232  hint = pos->full_cell();
233  }
234  // --------------------------------------------------------------------------------------------
235  // structure to retrieve CGAL points from vertex handle - one vertex handle per point.
236  // Needs to be constructed before as vertex handles arrives in no particular order.
237  vertex_handle_to_iterator_.resize(point_cloud.size());
238  // Loop on triangulation vertices list
239  for (CGAL_vertex_iterator vit = triangulation_->vertices_begin(); vit != triangulation_->vertices_end(); ++vit) {
240  if (!triangulation_->is_infinite(*vit)) {
241 #ifdef DEBUG_TRACES
242  std::clog << "Vertex insertion - " << vit->data() << " -> " << vit->point() << std::endl;
243 #endif // DEBUG_TRACES
244  vertex_handle_to_iterator_[vit->data()] = vit;
245  }
246  }
247  // --------------------------------------------------------------------------------------------
248  }
249  }
250 
257  const Point_d& get_point_(std::size_t vertex) const {
258  return vertex_handle_to_iterator_[vertex]->point();
259  }
260 
262  template<class SimplicialComplexForAlpha>
263  auto& get_cache(SimplicialComplexForAlpha& cplx, typename SimplicialComplexForAlpha::Simplex_handle s) {
264  auto k = cplx.key(s);
265  if(k==cplx.null_key()){
266  k = cache_.size();
267  cplx.assign_key(s, k);
268  // Using a transform_range is slower, currently.
269  thread_local std::vector<Point_d> v;
270  v.clear();
271  for (auto vertex : cplx.simplex_vertex_range(s))
272  v.push_back(get_point_(vertex));
273  Point_d c = kernel_.construct_circumcenter_d_object()(v.cbegin(), v.cend());
274  FT r = kernel_.squared_distance_d_object()(c, v[0]);
275  cache_.emplace_back(std::move(c), std::move(r));
276  }
277  return cache_[k];
278  }
279 
281  template<class SimplicialComplexForAlpha>
283  auto k = cplx.key(s);
284  if(k!=cplx.null_key())
285  return old_cache_[k].second;
286  // Using a transform_range is slower, currently.
287  thread_local std::vector<Point_d> v;
288  v.clear();
289  for (auto vertex : cplx.simplex_vertex_range(s))
290  v.push_back(get_point_(vertex));
291  return kernel_.compute_squared_radius_d_object()(v.cbegin(), v.cend());
292  }
293 
294  public:
318  template <typename SimplicialComplexForAlpha,
321  Filtration_value max_alpha_square = std::numeric_limits<Filtration_value>::infinity(),
322  bool exact = false,
323  bool default_filtration_value = false) {
324  // From SimplicialComplexForAlpha type required to insert into a simplicial complex (with or without subfaces).
326  typedef typename SimplicialComplexForAlpha::Simplex_handle Simplex_handle;
327  typedef std::vector<Vertex_handle> Vector_vertex;
328 
329  if (triangulation_ == nullptr) {
330  std::cerr << "Alpha_complex cannot create_complex from a NULL triangulation\n";
331  return false; // ----- >>
332  }
333  if (triangulation_->maximal_dimension() < 1) {
334  std::cerr << "Alpha_complex cannot create_complex from a zero-dimension triangulation\n";
335  return false; // ----- >>
336  }
337  if (complex.num_vertices() > 0) {
338  std::cerr << "Alpha_complex create_complex - complex is not empty\n";
339  return false; // ----- >>
340  }
341 
342  // --------------------------------------------------------------------------------------------
343  // Simplex_tree construction from loop on triangulation finite full cells list
344  if (triangulation_->number_of_vertices() > 0) {
345  for (auto cit = triangulation_->finite_full_cells_begin();
346  cit != triangulation_->finite_full_cells_end();
347  ++cit) {
348  Vector_vertex vertexVector;
349 #ifdef DEBUG_TRACES
350  std::clog << "Simplex_tree insertion ";
351 #endif // DEBUG_TRACES
352  for (auto vit = cit->vertices_begin(); vit != cit->vertices_end(); ++vit) {
353  if (*vit != nullptr) {
354 #ifdef DEBUG_TRACES
355  std::clog << " " << (*vit)->data();
356 #endif // DEBUG_TRACES
357  // Vector of vertex construction for simplex_tree structure
358  vertexVector.push_back((*vit)->data());
359  }
360  }
361 #ifdef DEBUG_TRACES
362  std::clog << std::endl;
363 #endif // DEBUG_TRACES
364  // Insert each simplex and its subfaces in the simplex tree - filtration is NaN
365  complex.insert_simplex_and_subfaces(vertexVector, std::numeric_limits<double>::quiet_NaN());
366  }
367  }
368  // --------------------------------------------------------------------------------------------
369 
370  if (!default_filtration_value) {
371  // --------------------------------------------------------------------------------------------
372  // ### For i : d -> 0
373  for (int decr_dim = triangulation_->maximal_dimension(); decr_dim >= 0; decr_dim--) {
374  // ### Foreach Sigma of dim i
375  for (Simplex_handle f_simplex : complex.skeleton_simplex_range(decr_dim)) {
376  int f_simplex_dim = complex.dimension(f_simplex);
377  if (decr_dim == f_simplex_dim) {
378  // ### If filt(Sigma) is NaN : filt(Sigma) = alpha(Sigma)
379  if (std::isnan(complex.filtration(f_simplex))) {
380  Filtration_value alpha_complex_filtration = 0.0;
381  // No need to compute squared_radius on a single point - alpha is 0.0
382  if (f_simplex_dim > 0) {
383  auto const& sqrad = radius(complex, f_simplex);
384 #if CGAL_VERSION_NR >= 1050000000
385  if(exact) CGAL::exact(sqrad);
386 #endif
387  CGAL::NT_converter<FT, Filtration_value> cv;
388  alpha_complex_filtration = cv(sqrad);
389  }
390  complex.assign_filtration(f_simplex, alpha_complex_filtration);
391 #ifdef DEBUG_TRACES
392  std::clog << "filt(Sigma) is NaN : filt(Sigma) =" << complex.filtration(f_simplex) << std::endl;
393 #endif // DEBUG_TRACES
394  }
395  // No need to propagate further, unweighted points all have value 0
396  if (decr_dim > 1)
397  propagate_alpha_filtration(complex, f_simplex);
398  }
399  }
400  old_cache_ = std::move(cache_);
401  cache_.clear();
402  }
403  // --------------------------------------------------------------------------------------------
404 
405  // --------------------------------------------------------------------------------------------
406  // As Alpha value is an approximation, we have to make filtration non decreasing while increasing the dimension
408  // Remove all simplices that have a filtration value greater than max_alpha_square
409  complex.prune_above_filtration(max_alpha_square);
410  // --------------------------------------------------------------------------------------------
411  }
412  return true;
413  }
414 
415  private:
416  template <typename SimplicialComplexForAlpha, typename Simplex_handle>
417  void propagate_alpha_filtration(SimplicialComplexForAlpha& complex, Simplex_handle f_simplex) {
418  // From SimplicialComplexForAlpha type required to assign filtration values.
419  typedef typename SimplicialComplexForAlpha::Filtration_value Filtration_value;
421 
422  // ### Foreach Tau face of Sigma
423  for (auto f_boundary : complex.boundary_simplex_range(f_simplex)) {
424 #ifdef DEBUG_TRACES
425  std::clog << " | --------------------------------------------------\n";
426  std::clog << " | Tau ";
427  for (auto vertex : complex.simplex_vertex_range(f_boundary)) {
428  std::clog << vertex << " ";
429  }
430  std::clog << "is a face of Sigma\n";
431  std::clog << " | isnan(complex.filtration(Tau)=" << std::isnan(complex.filtration(f_boundary)) << std::endl;
432 #endif // DEBUG_TRACES
433  // ### If filt(Tau) is not NaN
434  if (!std::isnan(complex.filtration(f_boundary))) {
435  // ### filt(Tau) = fmin(filt(Tau), filt(Sigma))
436  Filtration_value alpha_complex_filtration = fmin(complex.filtration(f_boundary),
437  complex.filtration(f_simplex));
438  complex.assign_filtration(f_boundary, alpha_complex_filtration);
439 #ifdef DEBUG_TRACES
440  std::clog << " | filt(Tau) = fmin(filt(Tau), filt(Sigma)) = " << complex.filtration(f_boundary) << std::endl;
441 #endif // DEBUG_TRACES
442  // ### Else
443  } else {
444  // Find which vertex of f_simplex is missing in f_boundary. We could actually write a variant of boundary_simplex_range that gives pairs (f_boundary, vertex). We rely on the fact that simplex_vertex_range is sorted.
445  auto longlist = complex.simplex_vertex_range(f_simplex);
446  auto shortlist = complex.simplex_vertex_range(f_boundary);
447  auto longiter = std::begin(longlist);
448  auto shortiter = std::begin(shortlist);
449  auto enditer = std::end(shortlist);
450  while(shortiter != enditer && *longiter == *shortiter) { ++longiter; ++shortiter; }
451  Vertex_handle extra = *longiter;
452  auto const& cache=get_cache(complex, f_boundary);
453  bool is_gab = kernel_.squared_distance_d_object()(cache.first, get_point_(extra)) >= cache.second;
454 #ifdef DEBUG_TRACES
455  std::clog << " | Tau is_gabriel(Sigma)=" << is_gab << " - vertexForGabriel=" << extra << std::endl;
456 #endif // DEBUG_TRACES
457  // ### If Tau is not Gabriel of Sigma
458  if (false == is_gab) {
459  // ### filt(Tau) = filt(Sigma)
460  Filtration_value alpha_complex_filtration = complex.filtration(f_simplex);
461  complex.assign_filtration(f_boundary, alpha_complex_filtration);
462 #ifdef DEBUG_TRACES
463  std::clog << " | filt(Tau) = filt(Sigma) = " << complex.filtration(f_boundary) << std::endl;
464 #endif // DEBUG_TRACES
465  }
466  }
467  }
468  }
469 };
470 
471 } // namespace alpha_complex
472 
473 namespace alphacomplex = alpha_complex;
474 
475 } // namespace Gudhi
476 
477 #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.
Simplex_key null_key()
Returns a constant dummy number that is either negative, or at least as large as the number of simpli...
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:176
Kernel::Point_d Point_d
A point in Euclidean space.
Definition: Alpha_complex.h:105
Alpha_complex(const InputPointRange &points)
Alpha_complex constructor from a list of points.
Definition: Alpha_complex.h:169
Alpha_complex(const std::string &off_file_name)
Alpha_complex constructor from an OFF file name.
Definition: Alpha_complex.h:148
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
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:108
Alpha complex data structure.
Definition: Alpha_complex.h:95
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:102
int assign_filtration(Simplex_handle simplex, Filtration_value filtration)
unspecified Simplex_handle
Definition: SimplicialComplexForAlpha.h:23
Simplex_key key(Simplex_handle sh)
Returns the number stored for a simplex by assign_key().
Boundary_simplex_range boundary_simplex_range(Simplex_handle const &simplex)
Returns a range over boundaries of a given simplex.
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15
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:192
void assign_key(Simplex_handle sh, Simplex_key n)
Store a number for a simplex, which can later be retrieved with key().
bool create_complex(SimplicialComplexForAlpha &complex, Filtration_value max_alpha_square=std::numeric_limits< Filtration_value >::infinity(), bool exact=false, bool default_filtration_value=false)
Inserts all Delaunay triangulation into the simplicial complex. It also computes the filtration value...
Definition: Alpha_complex.h:320
GUDHI  Version 3.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Aug 11 2020 11:09:13 for GUDHI by Doxygen 1.8.13