12 #ifndef ALPHA_COMPLEX_H_
13 #define ALPHA_COMPLEX_H_
15 #include <gudhi/Debug_utils.h>
17 #include <gudhi/Points_off_io.h>
22 #include <CGAL/Delaunay_triangulation.h>
23 #include <CGAL/Epeck_d.h>
24 #include <CGAL/Epick_d.h>
25 #include <CGAL/Spatial_sort_traits_adapter_d.h>
26 #include <CGAL/property_map.h>
27 #include <CGAL/version.h>
28 #include <CGAL/NT_converter.h>
30 #include <Eigen/src/Core/util/Macros.h>
42 #if CGAL_VERSION_NR < 1041101000
43 # error Alpha_complex is only available for CGAL >= 4.11
46 #if !EIGEN_VERSION_AT_LEAST(3,1,0)
47 # error Alpha_complex is only available for Eigen3 >= 3.1.0 installed with CGAL
52 namespace alpha_complex {
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; };
94 template<
class Kernel = CGAL::Epeck_d<CGAL::Dynamic_dimension_tag>>
98 typedef CGAL::Triangulation_data_structure<
typename Kernel::Dimension,
99 CGAL::Triangulation_vertex<Kernel, std::ptrdiff_t>,
100 CGAL::Triangulation_full_cell<Kernel> > TDS;
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;
116 typedef typename std::vector<Point_d> Vector_of_CGAL_points;
119 typedef typename Delaunay_triangulation::Vertex_iterator CGAL_vertex_iterator;
122 typedef typename Delaunay_triangulation::size_type size_type;
125 typedef typename std::vector< CGAL_vertex_iterator > Vector_vertex_iterator;
130 Vector_vertex_iterator vertex_handle_to_iterator_;
147 : triangulation_(nullptr) {
150 std::cerr <<
"Alpha_complex - Unable to read file " << off_file_name <<
"\n";
166 template<
typename InputPo
intRange >
168 : triangulation_(nullptr) {
169 init_from_range(points);
175 delete triangulation_;
191 return vertex_handle_to_iterator_.at(vertex)->point();
195 template<
typename InputPo
intRange >
196 void init_from_range(
const InputPointRange& points) {
197 #if CGAL_VERSION_NR < 1050000000
198 if (Is_Epeck_D<Kernel>::value)
199 std::cerr <<
"It is strongly advised to use a CGAL version >= 5.0 with Epeck_d Kernel for performance reasons."
203 auto first = std::begin(points);
204 auto last = std::end(points);
208 Point_Dimension point_dimension = kernel_.point_dimension_d_object();
213 std::vector<Point_d> point_cloud(first, last);
216 std::vector<std::ptrdiff_t> indices(boost::counting_iterator<std::ptrdiff_t>(0),
217 boost::counting_iterator<std::ptrdiff_t>(point_cloud.size()));
219 typedef boost::iterator_property_map<typename std::vector<Point_d>::iterator,
220 CGAL::Identity_property_map<std::ptrdiff_t>> Point_property_map;
221 typedef CGAL::Spatial_sort_traits_adapter_d<Kernel, Point_property_map> Search_traits_d;
223 CGAL::spatial_sort(indices.begin(), indices.end(), Search_traits_d(std::begin(point_cloud)));
225 typename Delaunay_triangulation::Full_cell_handle hint;
226 for (
auto index : indices) {
227 typename Delaunay_triangulation::Vertex_handle pos = triangulation_->insert(point_cloud[index], hint);
230 hint = pos->full_cell();
235 vertex_handle_to_iterator_.resize(point_cloud.size());
237 for (CGAL_vertex_iterator vit = triangulation_->vertices_begin(); vit != triangulation_->vertices_end(); ++vit) {
238 if (!triangulation_->is_infinite(*vit)) {
240 std::cout <<
"Vertex insertion - " << vit->data() <<
" -> " << vit->point() << std::endl;
241 #endif // DEBUG_TRACES
242 vertex_handle_to_iterator_[vit->data()] = vit;
268 template <
typename SimplicialComplexForAlpha,
271 Filtration_value max_alpha_square = std::numeric_limits<Filtration_value>::infinity(),
272 bool exact =
false) {
276 typedef std::vector<Vertex_handle> Vector_vertex;
278 if (triangulation_ ==
nullptr) {
279 std::cerr <<
"Alpha_complex cannot create_complex from a NULL triangulation\n";
282 if (triangulation_->maximal_dimension() < 1) {
283 std::cerr <<
"Alpha_complex cannot create_complex from a zero-dimension triangulation\n";
287 std::cerr <<
"Alpha_complex create_complex - complex is not empty\n";
293 if (triangulation_->number_of_vertices() > 0) {
294 for (
auto cit = triangulation_->finite_full_cells_begin();
295 cit != triangulation_->finite_full_cells_end();
297 Vector_vertex vertexVector;
299 std::cout <<
"Simplex_tree insertion ";
300 #endif // DEBUG_TRACES
301 for (
auto vit = cit->vertices_begin(); vit != cit->vertices_end(); ++vit) {
302 if (*vit !=
nullptr) {
304 std::cout <<
" " << (*vit)->data();
305 #endif // DEBUG_TRACES
307 vertexVector.push_back((*vit)->data());
311 std::cout << std::endl;
312 #endif // DEBUG_TRACES
321 Vector_of_CGAL_points pointVector;
323 for (
int decr_dim = triangulation_->maximal_dimension(); decr_dim >= 0; decr_dim--) {
326 int f_simplex_dim = complex.
dimension(f_simplex);
327 if (decr_dim == f_simplex_dim) {
330 std::cout <<
"Sigma of dim " << decr_dim <<
" is";
331 #endif // DEBUG_TRACES
333 pointVector.push_back(
get_point(vertex));
335 std::cout <<
" " << vertex;
336 #endif // DEBUG_TRACES
339 std::cout << std::endl;
340 #endif // DEBUG_TRACES
342 if (std::isnan(complex.filtration(f_simplex))) {
345 if (f_simplex_dim > 0) {
347 Squared_Radius squared_radius = kernel_.compute_squared_radius_d_object();
349 CGAL::NT_converter<typename Geom_traits::FT, Filtration_value> cv;
350 auto sqrad = squared_radius(pointVector.begin(), pointVector.end());
351 #if CGAL_VERSION_NR >= 1050000000
352 if(exact) CGAL::exact(sqrad);
354 alpha_complex_filtration = cv(sqrad);
358 std::cout <<
"filt(Sigma) is NaN : filt(Sigma) =" << complex.filtration(f_simplex) << std::endl;
359 #endif // DEBUG_TRACES
363 propagate_alpha_filtration(complex, f_simplex);
379 template <
typename SimplicialComplexForAlpha,
typename Simplex_handle>
385 #endif // DEBUG_TRACES
390 std::cout <<
" | --------------------------------------------------\n";
391 std::cout <<
" | Tau ";
393 std::cout << vertex <<
" ";
395 std::cout <<
"is a face of Sigma\n";
396 std::cout <<
" | isnan(complex.filtration(Tau)=" << std::isnan(complex.filtration(f_boundary)) << std::endl;
397 #endif // DEBUG_TRACES
399 if (!std::isnan(complex.filtration(f_boundary))) {
401 Filtration_value alpha_complex_filtration = fmin(complex.filtration(f_boundary),
402 complex.filtration(f_simplex));
405 std::cout <<
" | filt(Tau) = fmin(filt(Tau), filt(Sigma)) = " << complex.filtration(f_boundary) << std::endl;
406 #endif // DEBUG_TRACES
410 Vector_of_CGAL_points pointVector;
412 Vertex_handle vertexForGabriel = Vertex_handle();
413 #endif // DEBUG_TRACES
415 pointVector.push_back(
get_point(vertex));
421 if (std::find(pointVector.begin(), pointVector.end(), point_for_gabriel) == pointVector.end()) {
424 vertexForGabriel = vertex;
425 #endif // DEBUG_TRACES
431 Is_Gabriel is_gabriel = kernel_.side_of_bounded_sphere_d_object();
432 bool is_gab = is_gabriel(pointVector.begin(), pointVector.end(), point_for_gabriel)
433 != CGAL::ON_BOUNDED_SIDE;
435 std::cout <<
" | Tau is_gabriel(Sigma)=" << is_gab <<
" - vertexForGabriel=" << vertexForGabriel << std::endl;
436 #endif // DEBUG_TRACES
438 if (
false == is_gab) {
443 std::cout <<
" | filt(Tau) = filt(Sigma) = " << complex.filtration(f_boundary) << std::endl;
444 #endif // DEBUG_TRACES
453 namespace alphacomplex = alpha_complex;
457 #endif // ALPHA_COMPLEX_H_