| 1 | #include <topology/rips.h> |
| 2 | #include <topology/filtration.h> |
| 3 | |
| 4 | #include <geometry/l2distance.h> |
| 5 | #include <geometry/distances.h> |
| 6 | |
| 7 | #include <utilities/containers.h> // for BackInsertFunctor |
| 8 | |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <phat/boundary_matrix.h> |
| 12 | |
| 13 | typedef PairwiseDistances<PointContainer, L2Distance> Pair_distances; |
| 14 | typedef Pair_distances::DistanceType Distance_type; |
| 15 | typedef Pair_distances::IndexType Vertex; |
| 16 | |
| 17 | typedef Rips<Pair_distances> Generator; |
| 18 | typedef Generator::Simplex Smplx; |
| 19 | typedef Filtration<Smplx> Fltr; |
| 20 | |
| 21 | int main(int argc, char* argv[]) |
| 22 | { |
| 23 | Dimension skeleton; |
| 24 | Distance_type max_distance; |
| 25 | std::string infilename; |
| 26 | |
| 27 | if(argc<2) { |
| 28 | std::cerr << "Requires inputfile" << std::endl; |
| 29 | std::exit(1); |
| 30 | } |
| 31 | infilename = argv[1]; |
| 32 | |
| 33 | if(argc>=3) { |
| 34 | skeleton=atoi(argv[2]); |
| 35 | if(skeleton==0) { |
| 36 | std::cerr << "# Command line argument 0 ignored" << std::endl; |
| 37 | skeleton=std::numeric_limits<Dimension>::max(); |
| 38 | } |
| 39 | } else { |
| 40 | skeleton=std::numeric_limits<Dimension>::max(); |
| 41 | } |
| 42 | |
| 43 | if(argc>=4) { |
| 44 | max_distance=atof(argv[3]); |
| 45 | } else { |
| 46 | max_distance=std::numeric_limits<Distance_type>::max(); |
| 47 | } |
| 48 | |
| 49 | PointContainer points; |
| 50 | read_points(infilename, points); |
| 51 | |
| 52 | Pair_distances distances(points); |
| 53 | Generator rips(distances); |
| 54 | Generator::Evaluator size(distances); |
| 55 | Fltr f; |
| 56 | |
| 57 | // Generate 2-skeleton of the Rips complex for epsilon = 50 |
| 58 | rips.generate(skeleton, max_distance, make_push_back_functor(f)); |
| 59 | std::cerr << "# Generated complex of size: " << f.size() << std::endl; |
| 60 | |
| 61 | // Generate filtration with respect to distance and compute its persistence |
| 62 | f.sort(Generator::Comparison(distances)); |
| 63 | |
| 64 | |
| 65 | std::map<Smplx,phat::index,Smplx::VertexComparison> simplex_map; |
| 66 | phat::index size_of_simplex_map=0; |
| 67 | |
| 68 | phat::boundary_matrix< phat::vector_vector > boundary_matrix; |
| 69 | boundary_matrix.set_num_cols( f.size() ); |
| 70 | |
| 71 | for(Fltr::Index it=f.begin();it!=f.end();it++) { |
| 72 | phat::column boundary_indices; |
| 73 | const Smplx& c = f.simplex(it); |
| 74 | for(Smplx::BoundaryIterator bit = c.boundary_begin(); bit != c.boundary_end(); bit++) |
| 75 | boundary_indices.push_back( simplex_map[*bit] ); |
| 76 | std::sort(boundary_indices.begin(),boundary_indices.end()); |
| 77 | |
| 78 | boundary_matrix.set_col( size_of_simplex_map, boundary_indices ); |
| 79 | |
| 80 | phat::dimension dim_of_column = boundary_indices.size()==0 ? 0 : boundary_indices.size()-1; |
| 81 | |
| 82 | boundary_matrix.set_dim( size_of_simplex_map, dim_of_column ); |
| 83 | |
| 84 | simplex_map[c] = size_of_simplex_map++; |
| 85 | } |
| 86 | |
| 87 | boundary_matrix.save_binary( "rips.bin" ); |
| 88 | |
| 89 | return 1; |
| 90 | |
| 91 | } |