Debian Codegraph @

src/Persistent_cohomology/example/custom_persistence_sort.cpp

raw
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) 2014 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
11 #include <CGAL/Epick_d.h>
12 #include <CGAL/point_generators_d.h>
13 #include <CGAL/algorithm.h>
14 #include <CGAL/assertions.h>
15
16 #include <gudhi/Alpha_complex.h>
17 #include <gudhi/Persistent_cohomology.h>
18 // to construct a simplex_tree from alpha complex
19 #include <gudhi/Simplex_tree.h>
20
21 #include <iostream>
22 #include <iterator>
23 #include <vector>
24 #include <fstream> // for std::ofstream
25 #include <algorithm> // for std::sort
26
27
28 using Kernel = CGAL::Epick_d< CGAL::Dimension_tag<3> >;
29 using Point = Kernel::Point_d;
30 using Alpha_complex = Gudhi::alpha_complex::Alpha_complex<Kernel>;
31 using Simplex_tree = Gudhi::Simplex_tree<>;
32 using Persistent_cohomology = Gudhi::persistent_cohomology::Persistent_cohomology< Simplex_tree,
33 Gudhi::persistent_cohomology::Field_Zp >;
34
35 std::vector<Point> random_points() {
36 // Instantiate a random point generator
37 CGAL::Random rng(0);
38
39 // Generate "points_number" random points in a vector
40 std::vector<Point> points;
41
42 // Generates 1000 random 3D points on a sphere of radius 4.0
43 CGAL::Random_points_on_sphere_d<Point> rand_outside(3, 4.0, rng);
44 CGAL::cpp11::copy_n(rand_outside, 1000, std::back_inserter(points));
45 // Generates 2000 random 3D points in a sphere of radius 3.0
46 CGAL::Random_points_in_ball_d<Point> rand_inside(3, 3.0, rng);
47 CGAL::cpp11::copy_n(rand_inside, 2000, std::back_inserter(points));
48
49 return points;
50 }
51
52 /*
53 * Compare two intervals by dimension, then by length.
54 */
55 struct cmp_intervals_by_dim_then_length {
56 explicit cmp_intervals_by_dim_then_length(Simplex_tree * sc)
57 : sc_(sc) { }
58
59 template<typename Persistent_interval>
60 bool operator()(const Persistent_interval & p1, const Persistent_interval & p2) {
61 if (sc_->dimension(get < 0 > (p1)) == sc_->dimension(get < 0 > (p2)))
62 return (sc_->filtration(get < 1 > (p1)) - sc_->filtration(get < 0 > (p1))
63 > sc_->filtration(get < 1 > (p2)) - sc_->filtration(get < 0 > (p2)));
64 else
65 return (sc_->dimension(get < 0 > (p1)) > sc_->dimension(get < 0 > (p2)));
66 }
67 Simplex_tree* sc_;
68 };
69
70 int main(int argc, char **argv) {
71 std::vector<Point> points = random_points();
72
73 std::clog << "Points size=" << points.size() << std::endl;
74 // Alpha complex persistence computation from generated points
75 Alpha_complex alpha_complex_from_points(points);
76 std::clog << "alpha_complex_from_points" << std::endl;
77
78 Simplex_tree simplex;
79 std::clog << "simplex" << std::endl;
80 if (alpha_complex_from_points.create_complex(simplex, 0.6)) {
81 std::clog << "simplex" << std::endl;
82 // ----------------------------------------------------------------------------
83 // Display information about the alpha complex
84 // ----------------------------------------------------------------------------
85 std::clog << "Simplicial complex is of dimension " << simplex.dimension() <<
86 " - " << simplex.num_simplices() << " simplices - " <<
87 simplex.num_vertices() << " vertices." << std::endl;
88
89 std::clog << "Simplex_tree dim: " << simplex.dimension() << std::endl;
90
91 Persistent_cohomology pcoh(simplex);
92
93 // initializes the coefficient field for homology - Z/3Z
94 pcoh.init_coefficients(3);
95 pcoh.compute_persistent_cohomology(0.2);
96
97 // Custom sort and output persistence
98 cmp_intervals_by_dim_then_length cmp(&simplex);
99 auto persistent_pairs = pcoh.get_persistent_pairs();
100 std::sort(std::begin(persistent_pairs), std::end(persistent_pairs), cmp);
101 for (auto pair : persistent_pairs) {
102 std::clog << simplex.dimension(get<0>(pair)) << " "
103 << simplex.filtration(get<0>(pair)) << " "
104 << simplex.filtration(get<1>(pair)) << std::endl;
105 }
106
107 // Persistent Betti numbers
108 std::clog << "The persistent Betti numbers in interval [0.40, 0.41] are : ";
109 for (int dim = 0; dim < simplex.dimension(); dim++)
110 std::clog << "b" << dim << " = " << pcoh.persistent_betti_number(dim, 0.40, 0.41) << " ; ";
111 std::clog << std::endl;
112
113 // Betti numbers
114 std::vector<int> betti_numbers = pcoh.betti_numbers();
115 std::clog << "The Betti numbers are : ";
116 for (std::size_t i = 0; i < betti_numbers.size(); i++)
117 std::clog << "b" << i << " = " << betti_numbers[i] << " ; ";
118 std::clog << std::endl;
119 }
120 return 0;
121 }
122