| 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) 2017 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <gudhi/Alpha_complex.h> |
| 12 |
#include <gudhi/Rips_complex.h> |
| 13 |
#include <gudhi/distance_functions.h> |
| 14 |
#include <gudhi/Simplex_tree.h> |
| 15 |
#include <gudhi/Persistent_cohomology.h> |
| 16 |
#include <gudhi/Points_off_io.h> |
| 17 |
#include <gudhi/Bottleneck.h> |
| 18 |
|
| 19 |
#include <CGAL/Epick_d.h> |
| 20 |
|
| 21 |
#include <boost/program_options.hpp> |
| 22 |
|
| 23 |
#include <string> |
| 24 |
#include <vector> |
| 25 |
#include <limits> // infinity |
| 26 |
#include <utility> // for pair |
| 27 |
#include <algorithm> // for transform |
| 28 |
|
| 29 |
|
| 30 |
// Types definition |
| 31 |
using Simplex_tree = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; |
| 32 |
using Filtration_value = Simplex_tree::Filtration_value; |
| 33 |
using Rips_complex = Gudhi::rips_complex::Rips_complex<Filtration_value>; |
| 34 |
using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; |
| 35 |
using Persistent_cohomology = Gudhi::persistent_cohomology::Persistent_cohomology<Simplex_tree, Field_Zp >; |
| 36 |
using Kernel = CGAL::Epick_d< CGAL::Dynamic_dimension_tag >; |
| 37 |
using Point_d = Kernel::Point_d; |
| 38 |
using Points_off_reader = Gudhi::Points_off_reader<Point_d>; |
| 39 |
|
| 40 |
void program_options(int argc, char * argv[] |
| 41 |
, std::string & off_file_points |
| 42 |
, Filtration_value & threshold |
| 43 |
, int & dim_max |
| 44 |
, int & p |
| 45 |
, Filtration_value & min_persistence); |
| 46 |
|
| 47 |
static inline std::pair<double, double> compute_root_square(std::pair<double, double> input) { |
| 48 |
return std::make_pair(std::sqrt(input.first), std::sqrt(input.second)); |
| 49 |
} |
| 50 |
|
| 51 |
int main(int argc, char * argv[]) { |
| 52 |
std::string off_file_points; |
| 53 |
Filtration_value threshold; |
| 54 |
int dim_max; |
| 55 |
int p; |
| 56 |
Filtration_value min_persistence; |
| 57 |
|
| 58 |
program_options(argc, argv, off_file_points, threshold, dim_max, p, min_persistence); |
| 59 |
|
| 60 |
Points_off_reader off_reader(off_file_points); |
| 61 |
|
| 62 |
// -------------------------------------------- |
| 63 |
// Rips persistence |
| 64 |
// -------------------------------------------- |
| 65 |
Rips_complex rips_complex(off_reader.get_point_cloud(), threshold, Gudhi::Euclidean_distance()); |
| 66 |
|
| 67 |
// Construct the Rips complex in a Simplex Tree |
| 68 |
Simplex_tree rips_stree; |
| 69 |
|
| 70 |
rips_complex.create_complex(rips_stree, dim_max); |
| 71 |
std::clog << "The Rips complex contains " << rips_stree.num_simplices() << " simplices and has dimension " |
| 72 |
<< rips_stree.dimension() << " \n"; |
| 73 |
|
| 74 |
// Compute the persistence diagram of the complex |
| 75 |
Persistent_cohomology rips_pcoh(rips_stree); |
| 76 |
// initializes the coefficient field for homology |
| 77 |
rips_pcoh.init_coefficients(p); |
| 78 |
rips_pcoh.compute_persistent_cohomology(min_persistence); |
| 79 |
|
| 80 |
// rips_pcoh.output_diagram(); |
| 81 |
|
| 82 |
// -------------------------------------------- |
| 83 |
// Alpha persistence |
| 84 |
// -------------------------------------------- |
| 85 |
Gudhi::alpha_complex::Alpha_complex<Kernel> alpha_complex(off_reader.get_point_cloud()); |
| 86 |
|
| 87 |
Simplex_tree alpha_stree; |
| 88 |
alpha_complex.create_complex(alpha_stree, threshold * threshold); |
| 89 |
std::clog << "The Alpha complex contains " << alpha_stree.num_simplices() << " simplices and has dimension " |
| 90 |
<< alpha_stree.dimension() << " \n"; |
| 91 |
|
| 92 |
// Compute the persistence diagram of the complex |
| 93 |
Persistent_cohomology alpha_pcoh(alpha_stree); |
| 94 |
// initializes the coefficient field for homology |
| 95 |
alpha_pcoh.init_coefficients(p); |
| 96 |
alpha_pcoh.compute_persistent_cohomology(min_persistence * min_persistence); |
| 97 |
|
| 98 |
// alpha_pcoh.output_diagram(); |
| 99 |
|
| 100 |
// -------------------------------------------- |
| 101 |
// Bottleneck distance between both persistence |
| 102 |
// -------------------------------------------- |
| 103 |
double max_b_distance {}; |
| 104 |
for (int dim = 0; dim < dim_max; dim ++) { |
| 105 |
std::vector< std::pair< Filtration_value , Filtration_value > > rips_intervals; |
| 106 |
std::vector< std::pair< Filtration_value , Filtration_value > > alpha_intervals; |
| 107 |
rips_intervals = rips_pcoh.intervals_in_dimension(dim); |
| 108 |
alpha_intervals = alpha_pcoh.intervals_in_dimension(dim); |
| 109 |
std::transform(alpha_intervals.begin(), alpha_intervals.end(), alpha_intervals.begin(), compute_root_square); |
| 110 |
|
| 111 |
double bottleneck_distance = Gudhi::persistence_diagram::bottleneck_distance(rips_intervals, alpha_intervals); |
| 112 |
std::clog << "In dimension " << dim << ", bottleneck distance = " << bottleneck_distance << std::endl; |
| 113 |
if (bottleneck_distance > max_b_distance) |
| 114 |
max_b_distance = bottleneck_distance; |
| 115 |
} |
| 116 |
std::clog << "================================================================================" << std::endl; |
| 117 |
std::clog << "Bottleneck distance is " << max_b_distance << std::endl; |
| 118 |
|
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
void program_options(int argc, char * argv[] |
| 123 |
, std::string & off_file_points |
| 124 |
, Filtration_value & threshold |
| 125 |
, int & dim_max |
| 126 |
, int & p |
| 127 |
, Filtration_value & min_persistence) { |
| 128 |
namespace po = boost::program_options; |
| 129 |
po::options_description hidden("Hidden options"); |
| 130 |
hidden.add_options() |
| 131 |
("input-file", po::value<std::string>(&off_file_points), |
| 132 |
"Name of an OFF file containing a point set.\n"); |
| 133 |
|
| 134 |
po::options_description visible("Allowed options", 100); |
| 135 |
visible.add_options() |
| 136 |
("help,h", "produce help message") |
| 137 |
("max-edge-length,r", |
| 138 |
po::value<Filtration_value>(&threshold)->default_value(std::numeric_limits<Filtration_value>::infinity()), |
| 139 |
"Maximal length of an edge for the Rips complex construction.") |
| 140 |
("cpx-dimension,d", po::value<int>(&dim_max)->default_value(1), |
| 141 |
"Maximal dimension of the Rips complex we want to compute.") |
| 142 |
("field-charac,p", po::value<int>(&p)->default_value(11), |
| 143 |
"Characteristic p of the coefficient field Z/pZ for computing homology.") |
| 144 |
("min-persistence,m", po::value<Filtration_value>(&min_persistence), |
| 145 |
"Minimal lifetime of homology feature to be recorded. Default is 0. Enter a negative value to see zero length intervals"); |
| 146 |
|
| 147 |
po::positional_options_description pos; |
| 148 |
pos.add("input-file", 1); |
| 149 |
|
| 150 |
po::options_description all; |
| 151 |
all.add(visible).add(hidden); |
| 152 |
|
| 153 |
po::variables_map vm; |
| 154 |
po::store(po::command_line_parser(argc, argv). |
| 155 |
options(all).positional(pos).run(), vm); |
| 156 |
po::notify(vm); |
| 157 |
|
| 158 |
if (vm.count("help") || !vm.count("input-file")) { |
| 159 |
std::clog << std::endl; |
| 160 |
std::clog << "Compute the persistent homology with coefficient field Z/pZ \n"; |
| 161 |
std::clog << "of a Rips complex defined on a set of input points.\n \n"; |
| 162 |
std::clog << "The output diagram contains one bar per line, written with the convention: \n"; |
| 163 |
std::clog << " p dim b d \n"; |
| 164 |
std::clog << "where dim is the dimension of the homological feature,\n"; |
| 165 |
std::clog << "b and d are respectively the birth and death of the feature and \n"; |
| 166 |
std::clog << "p is the characteristic of the field Z/pZ used for homology coefficients." << std::endl << std::endl; |
| 167 |
|
| 168 |
std::clog << "Usage: " << argv[0] << " [options] input-file" << std::endl << std::endl; |
| 169 |
std::clog << visible << std::endl; |
| 170 |
exit(-1); |
| 171 |
} |
| 172 |
} |