All Classes Functions Variables Typedefs Friends Groups Pages
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 Saclay (France)
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 // to construct a simplex_tree from Delaunay_triangulation
27 #include <gudhi/graph_simplicial_complex.h>
28 #include <gudhi/Simplex_tree.h>
29 #include <gudhi/Debug_utils.h>
30 // to construct Alpha_complex from a OFF file of points
31 #include <gudhi/Points_off_io.h>
32 
33 #include <stdlib.h>
34 #include <math.h> // isnan, fmax
35 
36 #include <CGAL/Delaunay_triangulation.h>
37 #include <CGAL/Epick_d.h>
38 #include <CGAL/Spatial_sort_traits_adapter_d.h>
39 
40 #include <iostream>
41 #include <vector>
42 #include <string>
43 #include <limits> // NaN
44 #include <map>
45 #include <utility> // std::pair
46 #include <stdexcept>
47 #include <numeric> // for std::iota
48 
49 namespace Gudhi {
50 
51 namespace alphacomplex {
52 
76 template<class Kernel = CGAL::Epick_d<CGAL::Dynamic_dimension_tag>>
77 class Alpha_complex : public Simplex_tree<> {
78  public:
79  // Add an int in TDS to save point index in the structure
80  typedef CGAL::Triangulation_data_structure<CGAL::Dynamic_dimension_tag,
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  // From Simplex_tree
94  // Type required to insert into a simplex_tree (with or without subfaces).
95  typedef std::vector<Vertex_handle> Vector_vertex;
96 
97  // Simplex_result is the type returned from simplex_tree insert function.
98  typedef typename std::pair<Simplex_handle, bool> Simplex_result;
99 
100  typedef typename Kernel::Compute_squared_radius_d Squared_Radius;
101  typedef typename Kernel::Side_of_bounded_sphere_d Is_Gabriel;
102  typedef typename Kernel::Point_dimension_d Point_Dimension;
103 
104  // Type required to compute squared radius, or side of bounded sphere on a vector of points.
105  typedef typename std::vector<Point_d> Vector_of_CGAL_points;
106 
107  // Vertex_iterator type from CGAL.
108  typedef typename Delaunay_triangulation::Vertex_iterator CGAL_vertex_iterator;
109 
110  // size_type type from CGAL.
111  typedef typename Delaunay_triangulation::size_type size_type;
112 
113  // Map type to switch from simplex tree vertex handle to CGAL vertex iterator.
114  typedef typename std::map< Vertex_handle, CGAL_vertex_iterator > Vector_vertex_iterator;
115 
116  private:
119  Vector_vertex_iterator vertex_handle_to_iterator_;
121  Delaunay_triangulation* triangulation_;
123  Kernel kernel_;
124 
125  public:
135  Alpha_complex(const std::string& off_file_name,
136  Filtration_value max_alpha_square = std::numeric_limits<Filtration_value>::infinity())
137  : triangulation_(nullptr) {
138  Gudhi::Points_off_reader<Point_d> off_reader(off_file_name);
139  if (!off_reader.is_valid()) {
140  std::cerr << "Alpha_complex - Unable to read file " << off_file_name << "\n";
141  exit(-1); // ----- >>
142  }
143 
144  init_from_range(off_reader.get_point_cloud(), max_alpha_square);
145  }
146 
159  template<typename InputPointRange >
160  Alpha_complex(const InputPointRange& points,
161  Filtration_value max_alpha_square = std::numeric_limits<Filtration_value>::infinity())
162  : triangulation_(nullptr) {
163  init_from_range(points, max_alpha_square);
164  }
165 
171  delete triangulation_;
172  }
173 
174  // Forbid copy/move constructor/assignment operator
175  Alpha_complex(const Alpha_complex& other) = delete;
176  Alpha_complex& operator= (const Alpha_complex& other) = delete;
177  Alpha_complex (Alpha_complex&& other) = delete;
178  Alpha_complex& operator= (Alpha_complex&& other) = delete;
179 
187  return vertex_handle_to_iterator_.at(vertex)->point();
188  }
189 
190  private:
191  template<typename InputPointRange >
192  void init_from_range(const InputPointRange& points, Filtration_value max_alpha_square) {
193  auto first = std::begin(points);
194  auto last = std::end(points);
195  if (first != last) {
196  // point_dimension function initialization
197  Point_Dimension point_dimension = kernel_.point_dimension_d_object();
198 
199  // Delaunay triangulation is point dimension.
200  triangulation_ = new Delaunay_triangulation(point_dimension(*first));
201 
202  std::vector<Point_d> points(first, last);
203 
204  // Creates a vector {0, 1, ..., N-1}
205  std::vector<std::ptrdiff_t> indices(boost::counting_iterator<std::ptrdiff_t>(0),
206  boost::counting_iterator<std::ptrdiff_t>(points.size()));
207 
208  // Sort indices considering CGAL spatial sort
209  typedef CGAL::Spatial_sort_traits_adapter_d<Kernel, Point_d*> Search_traits_d;
210  spatial_sort(indices.begin(), indices.end(), Search_traits_d(&(points[0])));
211 
212  typename Delaunay_triangulation::Full_cell_handle hint;
213  for (auto index : indices) {
214  typename Delaunay_triangulation::Vertex_handle pos = triangulation_->insert(points[index], hint);
215  // Save index value as data to retrieve it after insertion
216  pos->data() = index;
217  hint = pos->full_cell();
218  }
219  init(max_alpha_square);
220  }
221  }
222 
232  void init(Filtration_value max_alpha_square) {
233  if (triangulation_ == nullptr) {
234  std::cerr << "Alpha_complex init - Cannot init from a NULL triangulation\n";
235  return; // ----- >>
236  }
237  if (triangulation_->number_of_vertices() < 1) {
238  std::cerr << "Alpha_complex init - Cannot init from a triangulation without vertices\n";
239  return; // ----- >>
240  }
241  if (triangulation_->maximal_dimension() < 1) {
242  std::cerr << "Alpha_complex init - Cannot init from a zero-dimension triangulation\n";
243  return; // ----- >>
244  }
245  if (num_vertices() > 0) {
246  std::cerr << "Alpha_complex init - Cannot init twice\n";
247  return; // ----- >>
248  }
249 
250  set_dimension(triangulation_->maximal_dimension());
251 
252  // --------------------------------------------------------------------------------------------
253  // double map to retrieve simplex tree vertex handles from CGAL vertex iterator and vice versa
254  // Loop on triangulation vertices list
255  for (CGAL_vertex_iterator vit = triangulation_->vertices_begin(); vit != triangulation_->vertices_end(); ++vit) {
256  if (!triangulation_->is_infinite(*vit)) {
257 #ifdef DEBUG_TRACES
258  std::cout << "Vertex insertion - " << vit->data() << " -> " << vit->point() << std::endl;
259 #endif // DEBUG_TRACES
260  vertex_handle_to_iterator_.emplace(vit->data(), vit);
261  }
262  }
263  // --------------------------------------------------------------------------------------------
264 
265  // --------------------------------------------------------------------------------------------
266  // Simplex_tree construction from loop on triangulation finite full cells list
267  for (auto cit = triangulation_->finite_full_cells_begin(); cit != triangulation_->finite_full_cells_end(); ++cit) {
268  Vector_vertex vertexVector;
269 #ifdef DEBUG_TRACES
270  std::cout << "Simplex_tree insertion ";
271 #endif // DEBUG_TRACES
272  for (auto vit = cit->vertices_begin(); vit != cit->vertices_end(); ++vit) {
273  if (*vit != nullptr) {
274 #ifdef DEBUG_TRACES
275  std::cout << " " << (*vit)->data();
276 #endif // DEBUG_TRACES
277  // Vector of vertex construction for simplex_tree structure
278  vertexVector.push_back((*vit)->data());
279  }
280  }
281 #ifdef DEBUG_TRACES
282  std::cout << std::endl;
283 #endif // DEBUG_TRACES
284  // Insert each simplex and its subfaces in the simplex tree - filtration is NaN
285  insert_simplex_and_subfaces(vertexVector, std::numeric_limits<double>::quiet_NaN());
286  }
287  // --------------------------------------------------------------------------------------------
288 
289  // --------------------------------------------------------------------------------------------
290  // ### For i : d -> 0
291  for (int decr_dim = dimension(); decr_dim >= 0; decr_dim--) {
292  // ### Foreach Sigma of dim i
293  for (auto f_simplex : skeleton_simplex_range(decr_dim)) {
294  int f_simplex_dim = dimension(f_simplex);
295  if (decr_dim == f_simplex_dim) {
296  Vector_of_CGAL_points pointVector;
297 #ifdef DEBUG_TRACES
298  std::cout << "Sigma of dim " << decr_dim << " is";
299 #endif // DEBUG_TRACES
300  for (auto vertex : simplex_vertex_range(f_simplex)) {
301  pointVector.push_back(get_point(vertex));
302 #ifdef DEBUG_TRACES
303  std::cout << " " << vertex;
304 #endif // DEBUG_TRACES
305  }
306 #ifdef DEBUG_TRACES
307  std::cout << std::endl;
308 #endif // DEBUG_TRACES
309  // ### If filt(Sigma) is NaN : filt(Sigma) = alpha(Sigma)
310  if (isnan(filtration(f_simplex))) {
311  Filtration_value alpha_complex_filtration = 0.0;
312  // No need to compute squared_radius on a single point - alpha is 0.0
313  if (f_simplex_dim > 0) {
314  // squared_radius function initialization
315  Squared_Radius squared_radius = kernel_.compute_squared_radius_d_object();
316 
317  alpha_complex_filtration = squared_radius(pointVector.begin(), pointVector.end());
318  }
319  assign_filtration(f_simplex, alpha_complex_filtration);
320 #ifdef DEBUG_TRACES
321  std::cout << "filt(Sigma) is NaN : filt(Sigma) =" << filtration(f_simplex) << std::endl;
322 #endif // DEBUG_TRACES
323  }
324  propagate_alpha_filtration(f_simplex, decr_dim);
325  }
326  }
327  }
328  // --------------------------------------------------------------------------------------------
329 
330  // --------------------------------------------------------------------------------------------
331  // As Alpha value is an approximation, we have to make filtration non decreasing while increasing the dimension
332  bool modified_filt = make_filtration_non_decreasing();
333  // Remove all simplices that have a filtration value greater than max_alpha_square
334  // Remark: prune_above_filtration does not require initialize_filtration to be done before.
335  modified_filt |= prune_above_filtration(max_alpha_square);
336  if (modified_filt) {
338  }
339  // --------------------------------------------------------------------------------------------
340  }
341 
342  template<typename Simplex_handle>
343  void propagate_alpha_filtration(Simplex_handle f_simplex, int decr_dim) {
344  // ### Foreach Tau face of Sigma
345  for (auto f_boundary : boundary_simplex_range(f_simplex)) {
346 #ifdef DEBUG_TRACES
347  std::cout << " | --------------------------------------------------\n";
348  std::cout << " | Tau ";
349  for (auto vertex : simplex_vertex_range(f_boundary)) {
350  std::cout << vertex << " ";
351  }
352  std::cout << "is a face of Sigma\n";
353  std::cout << " | isnan(filtration(Tau)=" << isnan(filtration(f_boundary)) << std::endl;
354 #endif // DEBUG_TRACES
355  // ### If filt(Tau) is not NaN
356  if (!isnan(filtration(f_boundary))) {
357  // ### filt(Tau) = fmin(filt(Tau), filt(Sigma))
358  Filtration_value alpha_complex_filtration = fmin(filtration(f_boundary), filtration(f_simplex));
359  assign_filtration(f_boundary, alpha_complex_filtration);
360 #ifdef DEBUG_TRACES
361  std::cout << " | filt(Tau) = fmin(filt(Tau), filt(Sigma)) = " << filtration(f_boundary) << std::endl;
362 #endif // DEBUG_TRACES
363  // ### Else
364  } else {
365  // No need to compute is_gabriel for dimension <= 2
366  // i.e. : Sigma = (3,1) => Tau = 1
367  if (decr_dim > 1) {
368  // insert the Tau points in a vector for is_gabriel function
369  Vector_of_CGAL_points pointVector;
370 #ifdef DEBUG_TRACES
371  Vertex_handle vertexForGabriel = Vertex_handle();
372 #endif // DEBUG_TRACES
373  for (auto vertex : simplex_vertex_range(f_boundary)) {
374  pointVector.push_back(get_point(vertex));
375  }
376  // Retrieve the Sigma point that is not part of Tau - parameter for is_gabriel function
377  Point_d point_for_gabriel;
378  for (auto vertex : simplex_vertex_range(f_simplex)) {
379  point_for_gabriel = get_point(vertex);
380  if (std::find(pointVector.begin(), pointVector.end(), point_for_gabriel) == pointVector.end()) {
381 #ifdef DEBUG_TRACES
382  // vertex is not found in Tau
383  vertexForGabriel = vertex;
384 #endif // DEBUG_TRACES
385  // No need to continue loop
386  break;
387  }
388  }
389  // is_gabriel function initialization
390  Is_Gabriel is_gabriel = kernel_.side_of_bounded_sphere_d_object();
391  bool is_gab = is_gabriel(pointVector.begin(), pointVector.end(), point_for_gabriel)
392  != CGAL::ON_BOUNDED_SIDE;
393 #ifdef DEBUG_TRACES
394  std::cout << " | Tau is_gabriel(Sigma)=" << is_gab << " - vertexForGabriel=" << vertexForGabriel << std::endl;
395 #endif // DEBUG_TRACES
396  // ### If Tau is not Gabriel of Sigma
397  if (false == is_gab) {
398  // ### filt(Tau) = filt(Sigma)
399  Filtration_value alpha_complex_filtration = filtration(f_simplex);
400  assign_filtration(f_boundary, alpha_complex_filtration);
401 #ifdef DEBUG_TRACES
402  std::cout << " | filt(Tau) = filt(Sigma) = " << filtration(f_boundary) << std::endl;
403 #endif // DEBUG_TRACES
404  }
405  }
406  }
407  }
408  }
409 };
410 
411 } // namespace alphacomplex
412 
413 } // namespace Gudhi
414 
415 #endif // ALPHA_COMPLEX_H_
Dictionary::iterator Simplex_handle
Handle type to a simplex contained in the simplicial complex represented by the simplex tree...
Definition: Simplex_tree.h:164
bool prune_above_filtration(Filtration_value filtration)
Prune above filtration value given as parameter.
Definition: Simplex_tree.h:1192
Kernel Geom_traits
Geometric traits class that provides the geometric types and predicates needed by Delaunay triangulat...
Definition: Alpha_complex.h:90
Simplex Tree data structure for representing simplicial complexes.
Definition: Simplex_tree.h:102
std::pair< Simplex_handle, bool > insert_simplex_and_subfaces(const InputVertexRange &Nsimplex, Filtration_value filtration=0)
Insert a N-simplex and all his subfaces, from a N-simplex represented by a range of Vertex_handles...
Definition: Simplex_tree.h:693
OFF file reader implementation in order to read points from an OFF file.
Definition: Points_off_io.h:135
Point_d get_point(Vertex_handle vertex) const
get_point returns the point corresponding to the vertex given as parameter.
Definition: Alpha_complex.h:186
Alpha_complex(const InputPointRange &points, Filtration_value max_alpha_square=std::numeric_limits< Filtration_value >::infinity())
Alpha_complex constructor from a list of points.
Definition: Alpha_complex.h:160
const std::vector< Point_d > & get_point_cloud() const
Point cloud getter.
Definition: Points_off_io.h:171
Filtration_value filtration() const
Returns an upper bound of the filtration values of the simplices.
Definition: Simplex_tree.h:462
bool make_filtration_non_decreasing()
Browse the simplex tree to ensure the filtration is not decreasing. The simplex tree is browsed start...
Definition: Simplex_tree.h:1142
void initialize_filtration()
Initializes the filtrations, i.e. sort the simplices according to their order in the filtration and i...
Definition: Simplex_tree.h:811
void assign_filtration(Simplex_handle sh, Filtration_value fv)
Sets the filtration value of a simplex. In debug mode, if sh is a null_simplex.
Definition: Simplex_tree.h:455
Boundary_simplex_range boundary_simplex_range(SimplexHandle sh)
Returns a range over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:311
int dimension() const
Returns an upper bound on the dimension of the simplicial complex.
Definition: Simplex_tree.h:526
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh)
Returns a range over the vertices of a simplex.
Definition: Simplex_tree.h:290
Kernel::Point_d Point_d
A point in Euclidean space.
Definition: Alpha_complex.h:87
Options::Vertex_handle Vertex_handle
Type for the vertex handle.
Definition: Simplex_tree.h:117
~Alpha_complex()
Alpha_complex destructor.
Definition: Alpha_complex.h:170
Alpha complex data structure.
Definition: Alpha_complex.h:77
Skeleton_simplex_range skeleton_simplex_range(int dim)
Returns a range over the simplices of the dim-skeleton of the simplicial complex. ...
Definition: Simplex_tree.h:257
Alpha_complex(const std::string &off_file_name, Filtration_value max_alpha_square=std::numeric_limits< Filtration_value >::infinity())
Alpha_complex constructor from an OFF file name. Uses the Delaunay_triangulation_off_reader to constr...
Definition: Alpha_complex.h:135
void set_dimension(int dimension)
Definition: Simplex_tree.h:797
CGAL::Delaunay_triangulation< Kernel, TDS > Delaunay_triangulation
A Delaunay triangulation of a set of points in .
Definition: Alpha_complex.h:84
bool is_valid() const
Returns if the OFF file read operation was successful or not.
Definition: Points_off_io.h:163
size_t num_vertices() const
Returns the number of vertices in the complex.
Definition: Simplex_tree.h:487
Options::Filtration_value Filtration_value
Type for the value of the filtration function.
Definition: Simplex_tree.h:109
GUDHI  Version 1.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding. Generated on Fri Apr 15 2016 17:36:21 for GUDHI by doxygen 1.8.6