/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. * Author(s): Vincent Rouvreau * * Copyright (C) 2016 Inria * * Modification(s): * - YYYY/MM Author: Description of the modification */ #ifndef INCLUDE_PERSISTENT_COHOMOLOGY_INTERFACE_H_ #define INCLUDE_PERSISTENT_COHOMOLOGY_INTERFACE_H_ #include #include #include // for std::pair #include // for sort namespace Gudhi { template class Persistent_cohomology_interface : public persistent_cohomology::Persistent_cohomology { private: /* * Compare two intervals by dimension, then by length. */ struct cmp_intervals_by_dim_then_length { explicit cmp_intervals_by_dim_then_length(FilteredComplex * sc) : sc_(sc) { } template bool operator()(const Persistent_interval & p1, const Persistent_interval & p2) { if (sc_->dimension(get < 0 > (p1)) == sc_->dimension(get < 0 > (p2))) return (sc_->filtration(get < 1 > (p1)) - sc_->filtration(get < 0 > (p1)) > sc_->filtration(get < 1 > (p2)) - sc_->filtration(get < 0 > (p2))); else return (sc_->dimension(get < 0 > (p1)) > sc_->dimension(get < 0 > (p2))); } FilteredComplex* sc_; }; public: Persistent_cohomology_interface(FilteredComplex* stptr) : persistent_cohomology::Persistent_cohomology(*stptr), stptr_(stptr) { } Persistent_cohomology_interface(FilteredComplex* stptr, bool persistence_dim_max) : persistent_cohomology::Persistent_cohomology(*stptr, persistence_dim_max), stptr_(stptr) { } std::vector>> get_persistence(int homology_coeff_field, double min_persistence) { persistent_cohomology::Persistent_cohomology::init_coefficients(homology_coeff_field); persistent_cohomology::Persistent_cohomology::compute_persistent_cohomology(min_persistence); // Custom sort and output persistence cmp_intervals_by_dim_then_length cmp(stptr_); auto persistent_pairs = persistent_cohomology::Persistent_cohomology::get_persistent_pairs(); std::sort(std::begin(persistent_pairs), std::end(persistent_pairs), cmp); std::vector>> persistence; for (auto pair : persistent_pairs) { persistence.push_back(std::make_pair(stptr_->dimension(get<0>(pair)), std::make_pair(stptr_->filtration(get<0>(pair)), stptr_->filtration(get<1>(pair))))); } return persistence; } int top_dimensional_coface(int splx){ if (stptr_->dimension(splx) == stptr_->dimension()){return splx;} else{ for (auto v : stptr_->get_coboundary_of_a_cell(splx)){ if(stptr_->filtration(v) == stptr_->filtration(splx)){ return top_dimensional_coface(v); } } } return splx; } std::vector> cofaces_of_cubical_persistence_pairs() { // Warning: this function is meant to be used with CubicalComplex only!! auto pairs = persistent_cohomology::Persistent_cohomology::get_persistent_pairs(); // Gather all top-dimensional cells and store their simplex handles std::vector max_splx; for (auto splx : stptr_->top_dimensional_cells_range()){ max_splx.push_back(splx); } // Sort these simplex handles and compute the ordering function // This function allows to go directly from the simplex handle to the position of the corresponding top-dimensional cell in the input data std::map order; std::sort(max_splx.begin(), max_splx.end()); for (unsigned int i = 0; i < max_splx.size(); i++) order.insert(std::make_pair(max_splx[i], i)); std::vector> persistence_pairs; for (auto pair : pairs) { int h = stptr_->dimension(get<0>(pair)); // Recursively get the top-dimensional cell / coface associated to the persistence generator int face0 = top_dimensional_coface(get<0>(pair)); // Retrieve the index of the corresponding top-dimensional cell in the input data int splx0 = order[face0]; int splx1 = -1; if (isfinite(stptr_->filtration(get<1>(pair)))){ // Recursively get the top-dimensional cell / coface associated to the persistence generator int face1 = top_dimensional_coface(get<1>(pair)); // Retrieve the index of the corresponding top-dimensional cell in the input data splx1 = order[face1]; } std::vector vect{ h, splx0, splx1}; persistence_pairs.push_back(vect); } return persistence_pairs; } std::vector, std::vector>> persistence_pairs() { auto pairs = persistent_cohomology::Persistent_cohomology::get_persistent_pairs(); std::vector, std::vector>> persistence_pairs; persistence_pairs.reserve(pairs.size()); for (auto pair : pairs) { std::vector birth; if (get<0>(pair) != stptr_->null_simplex()) { for (auto vertex : stptr_->simplex_vertex_range(get<0>(pair))) { birth.push_back(vertex); } } std::vector death; if (get<1>(pair) != stptr_->null_simplex()) { for (auto vertex : stptr_->simplex_vertex_range(get<1>(pair))) { death.push_back(vertex); } } persistence_pairs.push_back(std::make_pair(birth, death)); } return persistence_pairs; } private: // A copy FilteredComplex* stptr_; }; } // namespace Gudhi #endif // INCLUDE_PERSISTENT_COHOMOLOGY_INTERFACE_H_