Loading...
Searching...
No Matches
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/Alpha_complex/Alpha_kernel_d.h>
16#include <gudhi/Debug_utils.h>
17// to construct Alpha_complex from a OFF file of points
18#include <gudhi/Points_off_io.h>
19
20#include <cmath> // isnan, fmax
21#include <memory> // for std::unique_ptr
22#include <cstddef> // for std::size_t
23
24#include <CGAL/Delaunay_triangulation.h>
25#include <CGAL/Regular_triangulation.h> // aka. Weighted Delaunay triangulation
26#include <CGAL/Epeck_d.h> // For EXACT or SAFE version
27#include <CGAL/Epick_d.h> // For FAST version
28#include <CGAL/Spatial_sort_traits_adapter_d.h>
29#include <CGAL/property_map.h> // for CGAL::Identity_property_map
30#include <CGAL/version.h> // for CGAL_VERSION_NR
31#include <CGAL/NT_converter.h>
32
33#include <Eigen/src/Core/util/Macros.h> // for EIGEN_VERSION_AT_LEAST
34
35#include <boost/range/size.hpp>
36#include <boost/range/combine.hpp>
37#include <boost/range/adaptor/transformed.hpp>
38
39#include <iostream>
40#include <vector>
41#include <string>
42#include <limits> // NaN
43#include <map>
44#include <utility> // std::pair
45#include <stdexcept>
46#include <numeric> // for std::iota
47#include <algorithm> // for std::sort
48
49// Make compilation fail - required for external projects - https://github.com/GUDHI/gudhi-devel/issues/10
50#if CGAL_VERSION_NR < 1041101000
51# error Alpha_complex is only available for CGAL >= 4.11
52#endif
53
54#if !EIGEN_VERSION_AT_LEAST(3,1,0)
55# error Alpha_complex is only available for Eigen3 >= 3.1.0 installed with CGAL
56#endif
57
58namespace Gudhi {
59
60namespace alpha_complex {
61
62template<typename D> struct Is_Epeck_D { static const bool value = false; };
63template<typename D> struct Is_Epeck_D<CGAL::Epeck_d<D>> { static const bool value = true; };
64
102template<class Kernel = CGAL::Epeck_d<CGAL::Dynamic_dimension_tag>, bool Weighted = false>
104 private:
105 // Vertex_handle internal type (required by triangulation_ and vertices_).
106 using Internal_vertex_handle = std::ptrdiff_t;
107
108 public:
110 using Geom_traits = std::conditional_t<Weighted, CGAL::Regular_triangulation_traits_adapter<Kernel>, Kernel>;
111
112 // Add an int in TDS to save point index in the structure
113 using TDS = CGAL::Triangulation_data_structure<typename Geom_traits::Dimension,
114 CGAL::Triangulation_vertex<Geom_traits, Internal_vertex_handle>,
115 CGAL::Triangulation_full_cell<Geom_traits> >;
116
118 using Triangulation = std::conditional_t<Weighted, CGAL::Regular_triangulation<Kernel, TDS>,
119 CGAL::Delaunay_triangulation<Kernel, TDS>>;
120
123
124 // Numeric type of coordinates in the kernel
125 using FT = typename A_kernel_d::FT;
126
130 using Sphere = typename A_kernel_d::Sphere;
131
133 using Point_d = typename Geom_traits::Point_d;
134
135 private:
136 // Vertex_iterator type from CGAL.
137 using CGAL_vertex_iterator = typename Triangulation::Vertex_iterator;
138
139 // Structure to switch from simplex tree vertex handle to CGAL vertex iterator.
140 using Vector_vertex_iterator = std::vector< CGAL_vertex_iterator >;
141
142 private:
145 Vector_vertex_iterator vertex_handle_to_iterator_;
147 std::unique_ptr<Triangulation> triangulation_;
149 A_kernel_d kernel_;
153 std::vector<Internal_vertex_handle> vertices_;
154
156 std::vector<Sphere> cache_, old_cache_;
157
158 public:
168 Alpha_complex(const std::string& off_file_name) {
169 Gudhi::Points_off_reader<Point_d> off_reader(off_file_name);
170 if (!off_reader.is_valid()) {
171 std::cerr << "Alpha_complex - Unable to read file " << off_file_name << "\n";
172 exit(-1); // ----- >>
173 }
174
175 init_from_range(off_reader.get_point_cloud());
176 }
177
188 template<typename InputPointRange >
189 Alpha_complex(const InputPointRange& points) {
190 init_from_range(points);
191 }
192
205 template <typename InputPointRange, typename WeightRange>
206 Alpha_complex(const InputPointRange& points, WeightRange weights) {
207 static_assert(Weighted, "This constructor is not available for non-weighted versions of Alpha_complex");
208 // FIXME: this test is only valid if we have a forward range
209 GUDHI_CHECK(boost::size(weights) == boost::size(points),
210 std::invalid_argument("Points number in range different from weights range number"));
211 auto weighted_points = boost::range::combine(points, weights)
212 | boost::adaptors::transformed([](auto const&t){return Point_d(boost::get<0>(t), boost::get<1>(t));});
213 init_from_range(weighted_points);
214 }
215
216 // Forbid copy/move constructor/assignment operator
217 Alpha_complex(const Alpha_complex& other) = delete;
218 Alpha_complex& operator= (const Alpha_complex& other) = delete;
219 Alpha_complex (Alpha_complex&& other) = delete;
220 Alpha_complex& operator= (Alpha_complex&& other) = delete;
221
224 std::size_t num_vertices() const {
225 if (triangulation_ == nullptr)
226 return 0;
227 else
228 return triangulation_->number_of_vertices();
229 }
230
237 const Point_d& get_point(std::size_t vertex) const {
238 return vertex_handle_to_iterator_.at(vertex)->point();
239 }
240
241 private:
242 template<typename InputPointRange >
243 void init_from_range(const InputPointRange& points) {
244 #if CGAL_VERSION_NR < 1050000000
245 if (Is_Epeck_D<Kernel>::value)
246 std::cerr << "It is strongly advised to use a CGAL version >= 5.0 with Epeck_d Kernel for performance reasons."
247 << std::endl;
248 #endif
249
250#if CGAL_VERSION_NR < 1050101000
251 // Make compilation fail if weighted and CGAL < 5.1
252 static_assert(!Weighted, "Weighted Alpha_complex is only available for CGAL >= 5.1");
253#endif
254
255 auto first = std::begin(points);
256 auto last = std::end(points);
257
258 if (first != last) {
259 // Delaunay triangulation init with point dimension.
260 triangulation_ = std::make_unique<Triangulation>(kernel_.get_dimension(*first));
261
262 std::vector<Point_d> point_cloud(first, last);
263
264 // Creates a vector {0, 1, ..., N-1}
265 std::vector<Internal_vertex_handle> indices(boost::counting_iterator<Internal_vertex_handle>(0),
266 boost::counting_iterator<Internal_vertex_handle>(point_cloud.size()));
267
268 using Point_property_map = boost::iterator_property_map<typename std::vector<Point_d>::iterator,
269 CGAL::Identity_property_map<Internal_vertex_handle>>;
270 using Search_traits_d = CGAL::Spatial_sort_traits_adapter_d<Geom_traits, Point_property_map>;
271
272 CGAL::spatial_sort(indices.begin(), indices.end(), Search_traits_d(std::begin(point_cloud)));
273
274 typename Triangulation::Full_cell_handle hint;
275 for (auto index : indices) {
276 typename Triangulation::Vertex_handle pos = triangulation_->insert(point_cloud[index], hint);
277 if (pos != nullptr) {
278 // Save index value as data to retrieve it after insertion
279 pos->data() = index;
280 hint = pos->full_cell();
281 }
282 }
283 // --------------------------------------------------------------------------------------------
284 // structure to retrieve CGAL points from vertex handle - one vertex handle per point.
285 // Needs to be constructed before as vertex handles arrives in no particular order.
286 vertex_handle_to_iterator_.resize(point_cloud.size());
287 // List of sorted unique vertices in the triangulation. We take advantage of the existing loop to construct it
288 // Vertices list avoids quadratic complexity with the Simplex_tree. We should not fill it up with Toplex_map e.g.
289 vertices_.reserve(triangulation_->number_of_vertices());
290 // Loop on triangulation vertices list
291 for (CGAL_vertex_iterator vit = triangulation_->vertices_begin(); vit != triangulation_->vertices_end(); ++vit) {
292 if (!triangulation_->is_infinite(*vit)) {
293#ifdef DEBUG_TRACES
294 std::clog << "Vertex insertion - " << vit->data() << " -> " << vit->point() << std::endl;
295#endif // DEBUG_TRACES
296 vertex_handle_to_iterator_[vit->data()] = vit;
297 vertices_.push_back(vit->data());
298 }
299 }
300 std::sort(vertices_.begin(), vertices_.end());
301 // --------------------------------------------------------------------------------------------
302 }
303 }
304
311 const Point_d& get_point_(std::size_t vertex) const {
312 return vertex_handle_to_iterator_[vertex]->point();
313 }
314
316 template<class SimplicialComplexForAlpha>
317 auto& get_cache(SimplicialComplexForAlpha& cplx, typename SimplicialComplexForAlpha::Simplex_handle s) {
318 auto k = cplx.key(s);
319 if(k==cplx.null_key()){
320 k = cache_.size();
321 cplx.assign_key(s, k);
322 // Using a transform_range is slower, currently.
323 thread_local std::vector<Point_d> v;
324 v.clear();
325 for (auto vertex : cplx.simplex_vertex_range(s))
326 v.push_back(get_point_(vertex));
327 cache_.emplace_back(kernel_.get_sphere(v.cbegin(), v.cend()));
328 }
329 return cache_[k];
330 }
331
333 template<class SimplicialComplexForAlpha>
334 auto radius(SimplicialComplexForAlpha& cplx, typename SimplicialComplexForAlpha::Simplex_handle s) {
335 auto k = cplx.key(s);
336 if(k!=cplx.null_key())
337 return kernel_.get_squared_radius(old_cache_[k]);
338 // Using a transform_range is slower, currently.
339 thread_local std::vector<Point_d> v;
340 v.clear();
341 for (auto vertex : cplx.simplex_vertex_range(s))
342 v.push_back(get_point_(vertex));
343 return kernel_.get_squared_radius(v.cbegin(), v.cend());
344 }
345
346 public:
370 template <typename SimplicialComplexForAlpha,
373 Filtration_value max_alpha_square = std::numeric_limits<Filtration_value>::infinity(),
374 bool exact = false,
375 bool default_filtration_value = false) {
376 // From SimplicialComplexForAlpha type required to insert into a simplicial complex (with or without subfaces).
378 using Simplex_handle = typename SimplicialComplexForAlpha::Simplex_handle;
379 using Vector_vertex = std::vector<Vertex_handle>;
380
381 if (triangulation_ == nullptr) {
382 std::cerr << "Alpha_complex cannot create_complex from a NULL triangulation\n";
383 return false; // ----- >>
384 }
385 if (triangulation_->maximal_dimension() < 1) {
386 std::cerr << "Alpha_complex cannot create_complex from a zero-dimension triangulation\n";
387 return false; // ----- >>
388 }
389 if (complex.num_vertices() > 0) {
390 std::cerr << "Alpha_complex create_complex - complex is not empty\n";
391 return false; // ----- >>
392 }
393
394 // --------------------------------------------------------------------------------------------
395 // Simplex_tree construction from loop on triangulation finite full cells list
396 if (num_vertices() > 0) {
397 std::vector<Vertex_handle> one_vertex(1);
398 for (auto vertex : vertices_) {
399#ifdef DEBUG_TRACES
400 std::clog << "SimplicialComplex insertion " << vertex << std::endl;
401#endif // DEBUG_TRACES
402 one_vertex[0] = vertex;
403 complex.insert_simplex_and_subfaces(one_vertex, std::numeric_limits<double>::quiet_NaN());
404 }
405
406 for (auto cit = triangulation_->finite_full_cells_begin();
407 cit != triangulation_->finite_full_cells_end();
408 ++cit) {
409 Vector_vertex vertexVector;
410#ifdef DEBUG_TRACES
411 std::clog << "SimplicialComplex insertion ";
412#endif // DEBUG_TRACES
413 for (auto vit = cit->vertices_begin(); vit != cit->vertices_end(); ++vit) {
414 if (*vit != nullptr) {
415#ifdef DEBUG_TRACES
416 std::clog << " " << (*vit)->data();
417#endif // DEBUG_TRACES
418 // Vector of vertex construction for simplex_tree structure
419 vertexVector.push_back((*vit)->data());
420 }
421 }
422#ifdef DEBUG_TRACES
423 std::clog << std::endl;
424#endif // DEBUG_TRACES
425 // Insert each simplex and its subfaces in the simplex tree - filtration is NaN
426 complex.insert_simplex_and_subfaces(vertexVector, std::numeric_limits<double>::quiet_NaN());
427 }
428 }
429 // --------------------------------------------------------------------------------------------
430
431 if (!default_filtration_value) {
432 CGAL::NT_converter<FT, Filtration_value> cgal_converter;
433 // --------------------------------------------------------------------------------------------
434 // ### For i : d -> 0
435 for (int decr_dim = triangulation_->maximal_dimension(); decr_dim >= 0; decr_dim--) {
436 // ### Foreach Sigma of dim i
437 for (Simplex_handle f_simplex : complex.skeleton_simplex_range(decr_dim)) {
438 int f_simplex_dim = complex.dimension(f_simplex);
439 if (decr_dim == f_simplex_dim) {
440 // ### If filt(Sigma) is NaN : filt(Sigma) = alpha(Sigma)
441 if (std::isnan(complex.filtration(f_simplex))) {
442 Filtration_value alpha_complex_filtration = 0.0;
443 // No need to compute squared_radius on a non-weighted single point - alpha is 0.0
444 if (Weighted || f_simplex_dim > 0) {
445 auto const& sqrad = radius(complex, f_simplex);
446#if CGAL_VERSION_NR >= 1050000000
447 if(exact) CGAL::exact(sqrad);
448#endif
449 alpha_complex_filtration = cgal_converter(sqrad);
450 }
451 complex.assign_filtration(f_simplex, alpha_complex_filtration);
452#ifdef DEBUG_TRACES
453 std::clog << "filt(Sigma) is NaN : filt(Sigma) =" << complex.filtration(f_simplex) << std::endl;
454#endif // DEBUG_TRACES
455 }
456 // No need to propagate further, unweighted points all have value 0
457 if (decr_dim > !Weighted)
458 propagate_alpha_filtration(complex, f_simplex);
459 }
460 }
461 old_cache_ = std::move(cache_);
462 cache_.clear();
463 }
464 // --------------------------------------------------------------------------------------------
465
466 // --------------------------------------------------------------------------------------------
467 if (!exact)
468 // As Alpha value is an approximation, we have to make filtration non decreasing while increasing the dimension
469 // Only in not exact version, cf. https://github.com/GUDHI/gudhi-devel/issues/57
471 // Remove all simplices that have a filtration value greater than max_alpha_square
472 complex.prune_above_filtration(max_alpha_square);
473 // --------------------------------------------------------------------------------------------
474 }
475 return true;
476 }
477
478 private:
479 template <typename SimplicialComplexForAlpha, typename Simplex_handle>
480 void propagate_alpha_filtration(SimplicialComplexForAlpha& complex, Simplex_handle f_simplex) {
481 // From SimplicialComplexForAlpha type required to assign filtration values.
483
484 // ### Foreach Tau face of Sigma
485 for (auto face_opposite_vertex : complex.boundary_opposite_vertex_simplex_range(f_simplex)) {
486 auto f_boundary = face_opposite_vertex.first;
487#ifdef DEBUG_TRACES
488 std::clog << " | --------------------------------------------------\n";
489 std::clog << " | Tau ";
490 for (auto vertex : complex.simplex_vertex_range(f_boundary)) {
491 std::clog << vertex << " ";
492 }
493 std::clog << "is a face of Sigma\n";
494 std::clog << " | isnan(complex.filtration(Tau)=" << std::isnan(complex.filtration(f_boundary)) << std::endl;
495#endif // DEBUG_TRACES
496 // ### If filt(Tau) is not NaN
497 if (!std::isnan(complex.filtration(f_boundary))) {
498 // ### filt(Tau) = fmin(filt(Tau), filt(Sigma))
499 Filtration_value alpha_complex_filtration = fmin(complex.filtration(f_boundary),
500 complex.filtration(f_simplex));
501 complex.assign_filtration(f_boundary, alpha_complex_filtration);
502#ifdef DEBUG_TRACES
503 std::clog << " | filt(Tau) = fmin(filt(Tau), filt(Sigma)) = " << complex.filtration(f_boundary) << std::endl;
504#endif // DEBUG_TRACES
505 // ### Else
506 } else {
507 auto const& cache=get_cache(complex, f_boundary);
508 bool is_gab = kernel_.is_gabriel(cache, get_point_(face_opposite_vertex.second));
509#ifdef DEBUG_TRACES
510 std::clog << " | Tau is_gabriel(Sigma)=" << is_gab << " - vertexForGabriel=" << face_opposite_vertex.second << std::endl;
511#endif // DEBUG_TRACES
512 // ### If Tau is not Gabriel of Sigma
513 if (false == is_gab) {
514 // ### filt(Tau) = filt(Sigma)
515 Filtration_value alpha_complex_filtration = complex.filtration(f_simplex);
516 complex.assign_filtration(f_boundary, alpha_complex_filtration);
517#ifdef DEBUG_TRACES
518 std::clog << " | filt(Tau) = filt(Sigma) = " << complex.filtration(f_boundary) << std::endl;
519#endif // DEBUG_TRACES
520 }
521 }
522 }
523 }
524};
525
526} // namespace alpha_complex
527
528namespace alphacomplex = alpha_complex;
529
530} // namespace Gudhi
531
532#endif // ALPHA_COMPLEX_H_
OFF file reader implementation in order to read points from an OFF file.
Definition: Points_off_io.h:122
const std::vector< Point_d > & get_point_cloud() const
Point cloud getter.
Definition: Points_off_io.h:158
bool is_valid() const
Returns if the OFF file read operation was successful or not.
Definition: Points_off_io.h:150
Alpha complex data structure.
Definition: Alpha_complex.h:103
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:372
std::conditional_t< Weighted, CGAL::Regular_triangulation< Kernel, TDS >, CGAL::Delaunay_triangulation< Kernel, TDS > > Triangulation
A (Weighted or not) Delaunay triangulation of a set of points in .
Definition: Alpha_complex.h:119
typename A_kernel_d::Sphere Sphere
Sphere is a std::pair<Kernel::Point_d, Kernel::FT> (aka. circurmcenter and squared radius)....
Definition: Alpha_complex.h:130
Alpha_complex(const InputPointRange &points, WeightRange weights)
Alpha_complex constructor from a list of points and weights.
Definition: Alpha_complex.h:206
std::conditional_t< Weighted, CGAL::Regular_triangulation_traits_adapter< Kernel >, Kernel > Geom_traits
Geometric traits class that provides the geometric types and predicates needed by the triangulations.
Definition: Alpha_complex.h:110
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:237
Alpha_complex(const std::string &off_file_name)
Alpha_complex constructor from an OFF file name.
Definition: Alpha_complex.h:168
typename Geom_traits::Point_d Point_d
A point, or a weighted point in Euclidean space.
Definition: Alpha_complex.h:133
std::size_t num_vertices() const
Returns the number of finite vertices in the triangulation.
Definition: Alpha_complex.h:224
Alpha_complex(const InputPointRange &points)
Alpha_complex constructor from a list of points.
Definition: Alpha_complex.h:189
Alpha complex kernel container.
Definition: Alpha_kernel_d.h:42
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20
The concept SimplicialComplexForAlpha describes the requirements for a type to implement a simplicial...
Definition: SimplicialComplexForAlpha.h:21
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
int assign_filtration(Simplex_handle simplex, Filtration_value filtration)
void prune_above_filtration(Filtration_value filtration)
Simplex_vertex_range simplex_vertex_range(Simplex_handle const &simplex)
Returns a range over vertices of a given simplex.
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...
unspecified Simplex_handle
Definition: SimplicialComplexForAlpha.h:23
unspecified Vertex_handle
Definition: SimplicialComplexForAlpha.h:25
unspecified Filtration_value
Definition: SimplicialComplexForAlpha.h:27
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15