| 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): Clément Maria |
| 4 |
* |
| 5 |
* Copyright (C) 2014 Inria |
| 6 |
* |
| 7 |
* Modification(s): |
| 8 |
* - YYYY/MM Author: Description of the modification |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <gudhi/graph_simplicial_complex.h> |
| 12 |
#include <gudhi/distance_functions.h> |
| 13 |
#include <gudhi/Simplex_tree.h> |
| 14 |
#include <gudhi/Persistent_cohomology.h> |
| 15 |
#include <gudhi/Points_off_io.h> |
| 16 |
|
| 17 |
#include <boost/program_options.hpp> |
| 18 |
|
| 19 |
#include <string> |
| 20 |
#include <vector> |
| 21 |
#include <limits> // infinity |
| 22 |
#include <utility> // for pair |
| 23 |
#include <map> |
| 24 |
|
| 25 |
// ---------------------------------------------------------------------------- |
| 26 |
// rips_persistence_step_by_step is an example of each step that is required to |
| 27 |
// build a Rips over a Simplex_tree. Please refer to rips_persistence to see |
| 28 |
// how to do the same thing with the Rips_complex wrapper for less detailed |
| 29 |
// steps. |
| 30 |
// ---------------------------------------------------------------------------- |
| 31 |
|
| 32 |
// Types definition |
| 33 |
using Simplex_tree = Gudhi::Simplex_tree<Gudhi::Simplex_tree_options_fast_persistence>; |
| 34 |
using Vertex_handle = Simplex_tree::Vertex_handle; |
| 35 |
using Filtration_value = Simplex_tree::Filtration_value; |
| 36 |
using Proximity_graph = Gudhi::Proximity_graph<Simplex_tree>; |
| 37 |
|
| 38 |
using Field_Zp = Gudhi::persistent_cohomology::Field_Zp; |
| 39 |
using Persistent_cohomology = Gudhi::persistent_cohomology::Persistent_cohomology<Simplex_tree, Field_Zp >; |
| 40 |
using Point = std::vector<double>; |
| 41 |
using Points_off_reader = Gudhi::Points_off_reader<Point>; |
| 42 |
|
| 43 |
void program_options(int argc, char * argv[] |
| 44 |
, std::string & off_file_points |
| 45 |
, std::string & filediag |
| 46 |
, Filtration_value & threshold |
| 47 |
, int & dim_max |
| 48 |
, int & p |
| 49 |
, Filtration_value & min_persistence); |
| 50 |
|
| 51 |
int main(int argc, char * argv[]) { |
| 52 |
std::string off_file_points; |
| 53 |
std::string filediag; |
| 54 |
Filtration_value threshold; |
| 55 |
int dim_max; |
| 56 |
int p; |
| 57 |
Filtration_value min_persistence; |
| 58 |
|
| 59 |
program_options(argc, argv, off_file_points, filediag, threshold, dim_max, p, min_persistence); |
| 60 |
|
| 61 |
// Extract the points from the file filepoints |
| 62 |
Points_off_reader off_reader(off_file_points); |
| 63 |
|
| 64 |
// Compute the proximity graph of the points |
| 65 |
Proximity_graph prox_graph = Gudhi::compute_proximity_graph<Simplex_tree>(off_reader.get_point_cloud(), |
| 66 |
threshold, |
| 67 |
Gudhi::Euclidean_distance()); |
| 68 |
|
| 69 |
// Construct the Rips complex in a Simplex Tree |
| 70 |
Simplex_tree st; |
| 71 |
// insert the proximity graph in the simplex tree |
| 72 |
st.insert_graph(prox_graph); |
| 73 |
// expand the graph until dimension dim_max |
| 74 |
st.expansion(dim_max); |
| 75 |
|
| 76 |
std::clog << "The complex contains " << st.num_simplices() << " simplices \n"; |
| 77 |
std::clog << " and has dimension " << st.dimension() << " \n"; |
| 78 |
|
| 79 |
// Compute the persistence diagram of the complex |
| 80 |
Persistent_cohomology pcoh(st); |
| 81 |
// initializes the coefficient field for homology |
| 82 |
pcoh.init_coefficients(p); |
| 83 |
|
| 84 |
pcoh.compute_persistent_cohomology(min_persistence); |
| 85 |
|
| 86 |
// Output the diagram in filediag |
| 87 |
if (filediag.empty()) { |
| 88 |
pcoh.output_diagram(); |
| 89 |
} else { |
| 90 |
std::ofstream out(filediag); |
| 91 |
pcoh.output_diagram(out); |
| 92 |
out.close(); |
| 93 |
} |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
void program_options(int argc, char * argv[] |
| 99 |
, std::string & off_file_points |
| 100 |
, std::string & filediag |
| 101 |
, Filtration_value & threshold |
| 102 |
, int & dim_max |
| 103 |
, int & p |
| 104 |
, Filtration_value & min_persistence) { |
| 105 |
namespace po = boost::program_options; |
| 106 |
po::options_description hidden("Hidden options"); |
| 107 |
hidden.add_options() |
| 108 |
("input-file", po::value<std::string>(&off_file_points), |
| 109 |
"Name of an OFF file containing a point set.\n"); |
| 110 |
|
| 111 |
po::options_description visible("Allowed options", 100); |
| 112 |
visible.add_options() |
| 113 |
("help,h", "produce help message") |
| 114 |
("output-file,o", po::value<std::string>(&filediag)->default_value(std::string()), |
| 115 |
"Name of file in which the persistence diagram is written. Default print in standard output") |
| 116 |
("max-edge-length,r", |
| 117 |
po::value<Filtration_value>(&threshold)->default_value(std::numeric_limits<Filtration_value>::infinity()), |
| 118 |
"Maximal length of an edge for the Rips complex construction.") |
| 119 |
("cpx-dimension,d", po::value<int>(&dim_max)->default_value(1), |
| 120 |
"Maximal dimension of the Rips complex we want to compute.") |
| 121 |
("field-charac,p", po::value<int>(&p)->default_value(11), |
| 122 |
"Characteristic p of the coefficient field Z/pZ for computing homology.") |
| 123 |
("min-persistence,m", po::value<Filtration_value>(&min_persistence), |
| 124 |
"Minimal lifetime of homology feature to be recorded. Default is 0. Enter a negative value to see zero length intervals"); |
| 125 |
|
| 126 |
po::positional_options_description pos; |
| 127 |
pos.add("input-file", 1); |
| 128 |
|
| 129 |
po::options_description all; |
| 130 |
all.add(visible).add(hidden); |
| 131 |
|
| 132 |
po::variables_map vm; |
| 133 |
po::store(po::command_line_parser(argc, argv). |
| 134 |
options(all).positional(pos).run(), vm); |
| 135 |
po::notify(vm); |
| 136 |
|
| 137 |
if (vm.count("help") || !vm.count("input-file")) { |
| 138 |
std::clog << std::endl; |
| 139 |
std::clog << "Compute the persistent homology with coefficient field Z/pZ \n"; |
| 140 |
std::clog << "of a Rips complex defined on a set of input points.\n \n"; |
| 141 |
std::clog << "The output diagram contains one bar per line, written with the convention: \n"; |
| 142 |
std::clog << " p dim b d \n"; |
| 143 |
std::clog << "where dim is the dimension of the homological feature,\n"; |
| 144 |
std::clog << "b and d are respectively the birth and death of the feature and \n"; |
| 145 |
std::clog << "p is the characteristic of the field Z/pZ used for homology coefficients." << std::endl << std::endl; |
| 146 |
|
| 147 |
std::clog << "Usage: " << argv[0] << " [options] input-file" << std::endl << std::endl; |
| 148 |
std::clog << visible << std::endl; |
| 149 |
exit(-1); |
| 150 |
} |
| 151 |
} |