23 #ifndef HASSE_COMPLEX_H_ 24 #define HASSE_COMPLEX_H_ 26 #include <gudhi/allocator.h> 28 #include <boost/iterator/counting_iterator.hpp> 36 #include <tbb/parallel_for.h> 41 template <
class HasseCpx >
42 struct Hasse_simplex {
45 template<
class Complex_ds >
46 Hasse_simplex(Complex_ds & cpx
47 ,
typename Complex_ds::Simplex_handle sh)
48 : filtration_(cpx.filtration(sh))
50 boundary_.reserve(cpx.dimension(sh) + 1);
51 for (
auto b_sh : cpx.boundary_simplex_range(sh)) {
52 boundary_.push_back(cpx.key(b_sh));
56 Hasse_simplex(
typename HasseCpx::Simplex_key key
57 ,
typename HasseCpx::Filtration_value fil
58 , std::vector<typename HasseCpx::Simplex_handle>
const& boundary)
61 , boundary_(boundary) { }
63 typename HasseCpx::Simplex_key key_;
64 typename HasseCpx::Filtration_value filtration_;
65 std::vector<typename HasseCpx::Simplex_handle> boundary_;
82 typedef Hasse_simplex<Hasse_complex> Hasse_simp;
84 typedef SimplexKey Simplex_key;
85 typedef int Simplex_handle;
87 typedef boost::counting_iterator< Simplex_handle > Filtration_simplex_iterator;
88 typedef boost::iterator_range<Filtration_simplex_iterator> Filtration_simplex_range;
90 typedef typename std::vector< Simplex_handle >::iterator Boundary_simplex_iterator;
91 typedef boost::iterator_range<Boundary_simplex_iterator> Boundary_simplex_range;
93 typedef typename std::vector< Simplex_handle >::iterator Skeleton_simplex_iterator;
94 typedef boost::iterator_range< Skeleton_simplex_iterator > Skeleton_simplex_range;
97 Skeleton_simplex_range skeleton_simplex_range(
int dim = 0) {
99 std::cerr <<
"Dimension must be 0 \n";
101 return Skeleton_simplex_range(vertices_.begin(), vertices_.end());
104 template <
class Complex_ds >
105 Hasse_complex(Complex_ds & cpx)
106 : complex_(cpx.num_simplices())
109 , dim_max_(cpx.dimension()) {
110 int size = complex_.size();
112 tbb::parallel_for(0, size, [&](
int idx){
new (&complex_[idx]) Hasse_simp(cpx, cpx.simplex(idx));});
113 for (
int idx=0; idx < size; ++idx)
114 if (complex_[idx].boundary_.empty())
115 vertices_.push_back(idx);
117 for (
int idx=0; idx < size; ++idx) {
118 new (&complex_[idx]) Hasse_simp(cpx, cpx.simplex(idx));
119 if (complex_[idx].boundary_.empty())
120 vertices_.push_back(idx);
131 size_t num_simplices() {
132 return complex_.size();
135 Filtration_simplex_range filtration_simplex_range() {
136 return Filtration_simplex_range(Filtration_simplex_iterator(0)
137 , Filtration_simplex_iterator(complex_.size()));
140 Simplex_key key(Simplex_handle sh) {
141 return complex_[sh].key_;
144 Simplex_key null_key() {
148 Simplex_handle simplex(Simplex_key key) {
149 if (key == null_key())
return null_simplex();
153 Simplex_handle null_simplex() {
157 Filtration_value filtration(Simplex_handle sh) {
158 if (sh == null_simplex()) {
159 return std::numeric_limits<Filtration_value>::infinity();
161 return complex_[sh].filtration_;
164 int dimension(Simplex_handle sh) {
165 if (complex_[sh].boundary_.empty())
return 0;
166 return complex_[sh].boundary_.size() - 1;
173 std::pair<Simplex_handle, Simplex_handle> endpoints(Simplex_handle sh) {
174 return std::pair<Simplex_handle, Simplex_handle>(complex_[sh].boundary_[0]
175 , complex_[sh].boundary_[1]);
178 void assign_key(Simplex_handle sh, Simplex_key key) {
179 complex_[sh].key_ = key;
182 Boundary_simplex_range boundary_simplex_range(Simplex_handle sh) {
183 return Boundary_simplex_range(complex_[sh].boundary_.begin()
184 , complex_[sh].boundary_.end());
187 void display_simplex(Simplex_handle sh) {
188 std::cout << dimension(sh) <<
" ";
189 for (
auto sh_b : boundary_simplex_range(sh)) std::cout << sh_b <<
" ";
190 std::cout <<
" " << filtration(sh) <<
" key=" << key(sh);
193 void initialize_filtration() {
197 for (
auto & h_simp : complex_)
202 std::vector< Hasse_simp, Gudhi::no_init_allocator<Hasse_simp> > complex_;
203 std::vector<Simplex_handle> vertices_;
204 size_t num_vertices_;
208 template<
typename T1,
typename T2,
typename T3 >
209 std::istream& operator>>(std::istream & is
210 , Hasse_complex< T1, T2, T3 > & hcpx) {
211 assert(hcpx.num_simplices() == 0);
215 hcpx.complex_.reserve(num_simp);
217 std::vector< typename Hasse_complex<T1, T2, T3>::Simplex_key > boundary;
218 typename Hasse_complex<T1, T2, T3>::Filtration_value fil;
219 typename Hasse_complex<T1, T2, T3>::Filtration_value max_fil = 0;
225 hcpx.complex_.emplace_back(key, fil, boundary);
227 if (max_dim < hcpx.dimension(key)) {
228 max_dim = hcpx.dimension(key);
230 if (hcpx.dimension(key) == 0) {
231 hcpx.vertices_.push_back(key);
241 hcpx.dim_max_ = max_dim;
248 #endif // HASSE_COMPLEX_H_ Definition: SimplicialComplexForAlpha.h:26
Key type used as simplex identifier.
Definition: SimplexKey.h:27
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:32
bool read_hasse_simplex(std::istream &in_, std::vector< Simplex_key > &boundary, Filtration_value &fil)
Read a hasse simplex from a file.
Definition: reader_utils.h:195
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:27