All Classes Functions Variables Typedefs Friends Groups Pages
Simplex_tree.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Clément Maria
6  *
7  * Copyright (C) 2014 INRIA Sophia Antipolis-Méditerranée (France)
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef SIMPLEX_TREE_H_
24 #define SIMPLEX_TREE_H_
25 
26 #include <gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h>
27 #include <gudhi/Simplex_tree/Simplex_tree_siblings.h>
28 #include <gudhi/Simplex_tree/Simplex_tree_iterators.h>
29 #include <gudhi/Simplex_tree/indexing_tag.h>
30 
31 #include <gudhi/reader_utils.h>
32 #include <gudhi/graph_simplicial_complex.h>
33 #include <gudhi/Debug_utils.h>
34 
35 #include <boost/container/flat_map.hpp>
36 #include <boost/iterator/transform_iterator.hpp>
37 #include <boost/graph/adjacency_list.hpp>
38 #include <boost/range/adaptor/reversed.hpp>
39 
40 #ifdef GUDHI_USE_TBB
41 #include <tbb/parallel_sort.h>
42 #endif
43 
44 #include <utility>
45 #include <vector>
46 #include <functional> // for greater<>
47 #include <stdexcept>
48 #include <limits> // Inf
49 #include <initializer_list>
50 #include <algorithm> // for std::max
51 
52 namespace Gudhi {
86 struct Simplex_tree_options_full_featured;
87 
101 template<typename SimplexTreeOptions = Simplex_tree_options_full_featured>
103  public:
104  typedef SimplexTreeOptions Options;
105  typedef typename Options::Indexing_tag Indexing_tag;
109  typedef typename Options::Filtration_value Filtration_value;
113  typedef typename Options::Simplex_key Simplex_key;
117  typedef typename Options::Vertex_handle Vertex_handle;
118 
119  /* Type of node in the simplex tree. */
120  typedef Simplex_tree_node_explicit_storage<Simplex_tree> Node;
121  /* Type of dictionary Vertex_handle -> Node for traversing the simplex tree. */
122  // Note: this wastes space when Vertex_handle is 32 bits and Node is aligned on 64 bits. It would be better to use a
123  // flat_set (with our own comparator) where we can control the layout of the struct (put Vertex_handle and
124  // Simplex_key next to each other).
125  typedef typename boost::container::flat_map<Vertex_handle, Node> Dictionary;
126 
127  /* \brief Set of nodes sharing a same parent in the simplex tree. */
128  /* \brief Set of nodes sharing a same parent in the simplex tree. */
129  typedef Simplex_tree_siblings<Simplex_tree, Dictionary> Siblings;
130 
131  struct Key_simplex_base_real {
132  Key_simplex_base_real() : key_(-1) {}
133  void assign_key(Simplex_key k) { key_ = k; }
134  Simplex_key key() const { return key_; }
135  private:
136  Simplex_key key_;
137  };
138  struct Key_simplex_base_dummy {
139  Key_simplex_base_dummy() {}
140  void assign_key(Simplex_key) { }
141  Simplex_key key() const { assert(false); return -1; }
142  };
143  typedef typename std::conditional<Options::store_key, Key_simplex_base_real, Key_simplex_base_dummy>::type
144  Key_simplex_base;
145 
146  struct Filtration_simplex_base_real {
147  Filtration_simplex_base_real() : filt_(0) {}
148  void assign_filtration(Filtration_value f) { filt_ = f; }
149  Filtration_value filtration() const { return filt_; }
150  private:
151  Filtration_value filt_;
152  };
153  struct Filtration_simplex_base_dummy {
154  Filtration_simplex_base_dummy() {}
155  void assign_filtration(Filtration_value f) { assert(f == 0); }
156  Filtration_value filtration() const { return 0; }
157  };
158  typedef typename std::conditional<Options::store_filtration, Filtration_simplex_base_real,
159  Filtration_simplex_base_dummy>::type Filtration_simplex_base;
160 
161  public:
164  typedef typename Dictionary::iterator Simplex_handle;
165 
166  private:
167  typedef typename Dictionary::iterator Dictionary_it;
168  typedef typename Dictionary_it::value_type Dit_value_t;
169 
170  struct return_first {
171  Vertex_handle operator()(const Dit_value_t& p_sh) const {
172  return p_sh.first;
173  }
174  };
175 
176  public:
188  typedef boost::transform_iterator<return_first, Dictionary_it> Complex_vertex_iterator;
190  typedef boost::iterator_range<Complex_vertex_iterator> Complex_vertex_range;
194  typedef Simplex_tree_simplex_vertex_iterator<Simplex_tree> Simplex_vertex_iterator;
196  typedef boost::iterator_range<Simplex_vertex_iterator> Simplex_vertex_range;
198  typedef std::vector<Simplex_handle> Cofaces_simplex_range;
202  typedef Simplex_tree_boundary_simplex_iterator<Simplex_tree> Boundary_simplex_iterator;
204  typedef boost::iterator_range<Boundary_simplex_iterator> Boundary_simplex_range;
208  typedef Simplex_tree_complex_simplex_iterator<Simplex_tree> Complex_simplex_iterator;
210  typedef boost::iterator_range<Complex_simplex_iterator> Complex_simplex_range;
215  typedef Simplex_tree_skeleton_simplex_iterator<Simplex_tree> Skeleton_simplex_iterator;
218  typedef boost::iterator_range<Skeleton_simplex_iterator> Skeleton_simplex_range;
220  typedef std::vector<Simplex_handle> Filtration_simplex_range;
224  typedef typename Filtration_simplex_range::const_iterator Filtration_simplex_iterator;
225 
226  /* @} */ // end name range and iterator types
233  return Complex_vertex_range(
234  boost::make_transform_iterator(root_.members_.begin(), return_first()),
235  boost::make_transform_iterator(root_.members_.end(), return_first()));
236  }
237 
246  }
247 
260  }
261 
278  if (filtration_vect_.empty()) {
280  }
281  return filtration_vect_;
282  }
283 
291  assert(sh != null_simplex()); // Empty simplex
294  }
295 
310  template<class SimplexHandle>
314  }
315  // end range and iterator methods
322  : null_vertex_(-1),
323  threshold_(0),
324  root_(nullptr, null_vertex_),
325  filtration_vect_(),
326  dimension_(-1) { }
327 
329  Simplex_tree(const Simplex_tree& simplex_source)
330  : null_vertex_(simplex_source.null_vertex_),
331  threshold_(simplex_source.threshold_),
332  root_(nullptr, null_vertex_ , simplex_source.root_.members_),
333  filtration_vect_(),
334  dimension_(simplex_source.dimension_) {
335  auto root_source = simplex_source.root_;
336  rec_copy(&root_, &root_source);
337  }
338 
340  void rec_copy(Siblings *sib, Siblings *sib_source) {
341  for (auto sh = sib->members().begin(), sh_source = sib_source->members().begin();
342  sh != sib->members().end(); ++sh, ++sh_source) {
343  if (has_children(sh_source)) {
344  Siblings * newsib = new Siblings(sib, sh_source->first);
345  newsib->members_.reserve(sh_source->second.children()->members().size());
346  for (auto & child : sh_source->second.children()->members())
347  newsib->members_.emplace_hint(newsib->members_.end(), child.first, Node(newsib, child.second.filtration()));
348  rec_copy(newsib, sh_source->second.children());
349  sh->second.assign_children(newsib);
350  }
351  }
352  }
353 
356  : null_vertex_(std::move(old.null_vertex_)),
357  threshold_(std::move(old.threshold_)),
358  root_(std::move(old.root_)),
359  filtration_vect_(std::move(old.filtration_vect_)),
360  dimension_(std::move(old.dimension_)) {
361  old.dimension_ = -1;
362  old.threshold_ = 0;
363  old.root_ = Siblings(nullptr, null_vertex_);
364  }
365 
368  for (auto sh = root_.members().begin(); sh != root_.members().end(); ++sh) {
369  if (has_children(sh)) {
370  rec_delete(sh->second.children());
371  }
372  }
373  } // end constructor/destructor
375  private:
376  // Recursive deletion
377  void rec_delete(Siblings * sib) {
378  for (auto sh = sib->members().begin(); sh != sib->members().end(); ++sh) {
379  if (has_children(sh)) {
380  rec_delete(sh->second.children());
381  }
382  }
383  delete sib;
384  }
385 
386  public:
389  if ((null_vertex_ != st2.null_vertex_) ||
390  (threshold_ != st2.threshold_) ||
391  (dimension_ != st2.dimension_))
392  return false;
393  return rec_equal(&root_, &st2.root_);
394  }
395 
398  return (!(*this == st2));
399  }
400 
401  private:
403  bool rec_equal(Siblings* s1, Siblings* s2) {
404  if (s1->members().size() != s2->members().size())
405  return false;
406  for (auto sh1 = s1->members().begin(), sh2 = s2->members().begin();
407  (sh1 != s1->members().end() && sh2 != s2->members().end()); ++sh1, ++sh2) {
408  if (sh1->first != sh2->first || sh1->second.filtration() != sh2->second.filtration())
409  return false;
410  if (has_children(sh1) != has_children(sh2))
411  return false;
412  // Recursivity on children only if both have children
413  else if (has_children(sh1))
414  if (!rec_equal(sh1->second.children(), sh2->second.children()))
415  return false;
416  }
417  return true;
418  }
419 
420  public:
427  return sh->second.key();
428  }
429 
436  return filtration_vect_[key];
437  }
438 
445  if (sh != null_simplex()) {
446  return sh->second.filtration();
447  } else {
448  return INFINITY;
449  }
450  }
451 
456  GUDHI_CHECK(sh != null_simplex(),
457  std::invalid_argument("Simplex_tree::assign_filtration - cannot assign filtration on null_simplex"));
458  sh->second.assign_filtration(fv);
459  }
460 
463  return threshold_;
464  }
465 
471  return Dictionary_it(nullptr);
472  }
473 
477  return -1;
478  }
479 
483  return null_vertex_;
484  }
485 
487  size_t num_vertices() const {
488  return root_.members_.size();
489  }
490 
491  public:
493  size_t num_simplices() {
494  return num_simplices(&root_);
495  }
496 
497  private:
499  size_t num_simplices(Siblings * sib) {
500  auto sib_begin = sib->members().begin();
501  auto sib_end = sib->members().end();
502  size_t simplices_number = sib_end - sib_begin;
503  for (auto sh = sib_begin; sh != sib_end; ++sh) {
504  if (has_children(sh)) {
505  simplices_number += num_simplices(sh->second.children());
506  }
507  }
508  return simplices_number;
509  }
510 
511  public:
516  Siblings * curr_sib = self_siblings(sh);
517  int dim = 0;
518  while (curr_sib != nullptr) {
519  ++dim;
520  curr_sib = curr_sib->oncles();
521  }
522  return dim - 1;
523  }
524 
526  int dimension() const {
527  return dimension_;
528  }
529 
532  template<class SimplexHandle>
533  bool has_children(SimplexHandle sh) const {
534  return (sh->second.children()->parent() == sh->first);
535  }
536 
544  template<class InputVertexRange = std::initializer_list<Vertex_handle>>
545  Simplex_handle find(const InputVertexRange & s) {
546  auto first = std::begin(s);
547  auto last = std::end(s);
548 
549  if (first == last)
550  return null_simplex(); // ----->>
551 
552  // Copy before sorting
553  std::vector<Vertex_handle> copy(first, last);
554  std::sort(std::begin(copy), std::end(copy));
555  return find_simplex(copy);
556  }
557 
558  private:
560  Simplex_handle find_simplex(const std::vector<Vertex_handle> & simplex) {
561  Siblings * tmp_sib = &root_;
562  Dictionary_it tmp_dit;
563  Vertex_handle last = simplex.back();
564  for (auto v : simplex) {
565  tmp_dit = tmp_sib->members_.find(v);
566  if (tmp_dit == tmp_sib->members_.end()) {
567  return null_simplex();
568  }
569  if (!has_children(tmp_dit) && v != last) {
570  return null_simplex();
571  }
572  tmp_sib = tmp_dit->second.children();
573  }
574  return tmp_dit;
575  }
576 
579  Simplex_handle find_vertex(Vertex_handle v) {
581  assert(contiguous_vertices());
582  return root_.members_.begin() + v;
583  } else {
584  return root_.members_.find(v);
585  }
586  }
587 
588  public:
590  bool contiguous_vertices() const {
591  if (root_.members_.empty()) return true;
592  if (root_.members_.begin()->first != 0) return false;
593  if (std::prev(root_.members_.end())->first != static_cast<Vertex_handle>(root_.members_.size() - 1)) return false;
594  return true;
595  }
596 
597  private:
612  std::pair<Simplex_handle, bool> insert_vertex_vector(const std::vector<Vertex_handle>& simplex,
613  Filtration_value filtration) {
614  Siblings * curr_sib = &root_;
615  std::pair<Simplex_handle, bool> res_insert;
616  auto vi = simplex.begin();
617  for (; vi != simplex.end() - 1; ++vi) {
618  res_insert = curr_sib->members_.emplace(*vi, Node(curr_sib, filtration));
619  if (!(has_children(res_insert.first))) {
620  res_insert.first->second.assign_children(new Siblings(curr_sib, *vi));
621  }
622  curr_sib = res_insert.first->second.children();
623  }
624  res_insert = curr_sib->members_.emplace(*vi, Node(curr_sib, filtration));
625  if (!res_insert.second) {
626  // if already in the complex
627  if (res_insert.first->second.filtration() > filtration) {
628  // if filtration value modified
629  res_insert.first->second.assign_filtration(filtration);
630  return res_insert;
631  }
632  // if filtration value unchanged
633  return std::pair<Simplex_handle, bool>(null_simplex(), false);
634  }
635  // otherwise the insertion has succeeded
636  return res_insert;
637  }
638 
639  public:
663  template<class InputVertexRange = std::initializer_list<Vertex_handle>>
664  std::pair<Simplex_handle, bool> insert_simplex(const InputVertexRange & simplex,
665  Filtration_value filtration = 0) {
666  auto first = std::begin(simplex);
667  auto last = std::end(simplex);
668 
669  if (first == last)
670  return std::pair<Simplex_handle, bool>(null_simplex(), true); // ----->>
671 
672  // Copy before sorting
673  std::vector<Vertex_handle> copy(first, last);
674  std::sort(std::begin(copy), std::end(copy));
675  return insert_vertex_vector(copy, filtration);
676  }
677 
692  template<class InputVertexRange = std::initializer_list<Vertex_handle>>
693  std::pair<Simplex_handle, bool> insert_simplex_and_subfaces(const InputVertexRange& Nsimplex,
694  Filtration_value filtration = 0) {
695  auto first = std::begin(Nsimplex);
696  auto last = std::end(Nsimplex);
697 
698  if (first == last)
699  return std::pair<Simplex_handle, bool>(null_simplex(), true); // ----->>
700 
701  // Copy before sorting
702  std::vector<Vertex_handle> copy(first, last);
703  std::sort(std::begin(copy), std::end(copy));
704 
705  std::vector<std::vector<Vertex_handle>> to_be_inserted;
706  std::vector<std::vector<Vertex_handle>> to_be_propagated;
707  return rec_insert_simplex_and_subfaces(copy, to_be_inserted, to_be_propagated, filtration);
708  }
709 
710  private:
711  std::pair<Simplex_handle, bool> rec_insert_simplex_and_subfaces(std::vector<Vertex_handle>& the_simplex,
712  std::vector<std::vector<Vertex_handle>>& to_be_inserted,
713  std::vector<std::vector<Vertex_handle>>& to_be_propagated,
714  Filtration_value filtration = 0.0) {
715  std::pair<Simplex_handle, bool> insert_result;
716  if (the_simplex.size() > 1) {
717  // Get and remove last vertex
718  Vertex_handle last_vertex = the_simplex.back();
719  the_simplex.pop_back();
720  // Recursive call after last vertex removal
721  insert_result = rec_insert_simplex_and_subfaces(the_simplex, to_be_inserted, to_be_propagated, filtration);
722 
723  // Concatenation of to_be_inserted and to_be_propagated
724  to_be_inserted.insert(to_be_inserted.begin(), to_be_propagated.begin(), to_be_propagated.end());
725  to_be_propagated = to_be_inserted;
726 
727  // to_be_inserted treatment
728  for (auto& simplex_tbi : to_be_inserted) {
729  simplex_tbi.push_back(last_vertex);
730  }
731  std::vector<Vertex_handle> last_simplex(1, last_vertex);
732  to_be_inserted.insert(to_be_inserted.begin(), last_simplex);
733  // i.e. (0,1,2) =>
734  // [to_be_inserted | to_be_propagated] = [(1) (0,1) | (0)]
735  // [to_be_inserted | to_be_propagated] = [(2) (0,2) (1,2) (0,1,2) | (0) (1) (0,1)]
736  // N.B. : it is important the last inserted to be the highest in dimension
737  // in order to return the "last" insert_simplex result
738 
739  // insert all to_be_inserted
740  for (auto& simplex_tbi : to_be_inserted) {
741  insert_result = insert_vertex_vector(simplex_tbi, filtration);
742  }
743  } else if (the_simplex.size() == 1) {
744  // When reaching the end of recursivity, vector of simplices shall be empty and filled on back recursive
745  if ((to_be_inserted.size() != 0) || (to_be_propagated.size() != 0)) {
746  std::cerr << "Simplex_tree::rec_insert_simplex_and_subfaces - Error vector not empty\n";
747  exit(-1);
748  }
749  std::vector<Vertex_handle> first_simplex(1, the_simplex.back());
750  // i.e. (0,1,2) => [to_be_inserted | to_be_propagated] = [(0) | ]
751  to_be_inserted.push_back(first_simplex);
752 
753  insert_result = insert_vertex_vector(first_simplex, filtration);
754  } else {
755  std::cerr << "Simplex_tree::rec_insert_simplex_and_subfaces - Recursivity error\n";
756  exit(-1);
757  }
758  return insert_result;
759  }
760 
761  public:
765  sh->second.assign_key(key);
766  }
767 
771  std::pair<Simplex_handle, Simplex_handle> endpoints(Simplex_handle sh) {
772  assert(dimension(sh) == 1);
773  return { find_vertex(sh->first), find_vertex(self_siblings(sh)->parent()) };
774  }
775 
777  template<class SimplexHandle>
778  Siblings* self_siblings(SimplexHandle sh) {
779  if (sh->second.children()->parent() == sh->first)
780  return sh->second.children()->oncles();
781  else
782  return sh->second.children();
783  }
784 
785  public:
787  Siblings * root() {
788  return &root_;
789  }
790 
793  threshold_ = fil;
794  }
795 
798  dimension_ = dimension;
799  }
800 
801  public:
812  filtration_vect_.clear();
813  filtration_vect_.reserve(num_simplices());
815  filtration_vect_.push_back(sh);
816 
817  /* We use stable_sort here because with libstdc++ it is faster than sort.
818  * is_before_in_filtration is now a total order, but we used to call
819  * stable_sort for the following heuristic:
820  * The use of a depth-first traversal of the simplex tree, provided by
821  * complex_simplex_range(), combined with a stable sort is meant to
822  * optimize the order of simplices with same filtration value. The
823  * heuristic consists in inserting the cofaces of a simplex as soon as
824  * possible.
825  */
826 #ifdef GUDHI_USE_TBB
827  tbb::parallel_sort(filtration_vect_.begin(), filtration_vect_.end(), is_before_in_filtration(this));
828 #else
829  std::stable_sort(filtration_vect_.begin(), filtration_vect_.end(), is_before_in_filtration(this));
830 #endif
831  }
832 
833  private:
846  void rec_coface(std::vector<Vertex_handle> &vertices, Siblings *curr_sib, int curr_nbVertices,
847  std::vector<Simplex_handle>& cofaces, bool star, int nbVertices) {
848  if (!(star || curr_nbVertices <= nbVertices)) // dimension of actual simplex <= nbVertices
849  return;
850  for (Simplex_handle simplex = curr_sib->members().begin(); simplex != curr_sib->members().end(); ++simplex) {
851  if (vertices.empty()) {
852  // If we reached the end of the vertices, and the simplex has more vertices than the given simplex
853  // => we found a coface
854 
855  // Add a coface if we wan't the star or if the number of vertices of the current simplex matches with nbVertices
856  bool addCoface = (star || curr_nbVertices == nbVertices);
857  if (addCoface)
858  cofaces.push_back(simplex);
859  if ((!addCoface || star) && has_children(simplex)) // Rec call
860  rec_coface(vertices, simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
861  } else {
862  if (simplex->first == vertices.back()) {
863  // If curr_sib matches with the top vertex
864  bool equalDim = (star || curr_nbVertices == nbVertices); // dimension of actual simplex == nbVertices
865  bool addCoface = vertices.size() == 1 && equalDim;
866  if (addCoface)
867  cofaces.push_back(simplex);
868  if ((!addCoface || star) && has_children(simplex)) {
869  // Rec call
870  Vertex_handle tmp = vertices.back();
871  vertices.pop_back();
872  rec_coface(vertices, simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
873  vertices.push_back(tmp);
874  }
875  } else if (simplex->first > vertices.back()) {
876  return;
877  } else {
878  // (simplex->first < vertices.back()
879  if (has_children(simplex))
880  rec_coface(vertices, simplex->second.children(), curr_nbVertices + 1, cofaces, star, nbVertices);
881  }
882  }
883  }
884  }
885 
886  public:
893  return cofaces_simplex_range(simplex, 0);
894  }
895 
904  Cofaces_simplex_range cofaces;
905  // codimension must be positive or null integer
906  assert(codimension >= 0);
908  std::vector<Vertex_handle> copy(rg.begin(), rg.end());
909  if (codimension + static_cast<int>(copy.size()) > dimension_ + 1 ||
910  (codimension == 0 && static_cast<int>(copy.size()) > dimension_)) // n+codimension greater than dimension_
911  return cofaces;
912  // must be sorted in decreasing order
913  assert(std::is_sorted(copy.begin(), copy.end(), std::greater<Vertex_handle>()));
914  bool star = codimension == 0;
915  rec_coface(copy, &root_, 1, cofaces, star, codimension + static_cast<int>(copy.size()));
916  return cofaces;
917  }
918 
919  private:
927  bool reverse_lexicographic_order(Simplex_handle sh1, Simplex_handle sh2) {
930  Simplex_vertex_iterator it1 = rg1.begin();
931  Simplex_vertex_iterator it2 = rg2.begin();
932  while (it1 != rg1.end() && it2 != rg2.end()) {
933  if (*it1 == *it2) {
934  ++it1;
935  ++it2;
936  } else {
937  return *it1 < *it2;
938  }
939  }
940  return ((it1 == rg1.end()) && (it2 != rg2.end()));
941  }
942 
949  struct is_before_in_filtration {
950  explicit is_before_in_filtration(Simplex_tree * st)
951  : st_(st) { }
952 
953  bool operator()(const Simplex_handle sh1, const Simplex_handle sh2) const {
954  // Not using st_->filtration(sh1) because it uselessly tests for null_simplex.
955  if (sh1->second.filtration() != sh2->second.filtration()) {
956  return sh1->second.filtration() < sh2->second.filtration();
957  }
958  // is sh1 a proper subface of sh2
959  return st_->reverse_lexicographic_order(sh1, sh2);
960  }
961 
962  Simplex_tree * st_;
963  };
964 
965  public:
984  template<class OneSkeletonGraph>
985  void insert_graph(const OneSkeletonGraph& skel_graph) {
986  // the simplex tree must be empty
987  assert(num_simplices() == 0);
988 
989  if (boost::num_vertices(skel_graph) == 0) {
990  return;
991  }
992  if (num_edges(skel_graph) == 0) {
993  dimension_ = 0;
994  } else {
995  dimension_ = 1;
996  }
997 
998  root_.members_.reserve(boost::num_vertices(skel_graph));
999 
1000  typename boost::graph_traits<OneSkeletonGraph>::vertex_iterator v_it,
1001  v_it_end;
1002  for (std::tie(v_it, v_it_end) = boost::vertices(skel_graph); v_it != v_it_end;
1003  ++v_it) {
1004  root_.members_.emplace_hint(
1005  root_.members_.end(), *v_it,
1006  Node(&root_, boost::get(vertex_filtration_t(), skel_graph, *v_it)));
1007  }
1008  typename boost::graph_traits<OneSkeletonGraph>::edge_iterator e_it,
1009  e_it_end;
1010  for (std::tie(e_it, e_it_end) = boost::edges(skel_graph); e_it != e_it_end;
1011  ++e_it) {
1012  auto u = source(*e_it, skel_graph);
1013  auto v = target(*e_it, skel_graph);
1014  if (u < v) {
1015  // count edges only once { std::swap(u,v); } // u < v
1016  auto sh = find_vertex(u);
1017  if (!has_children(sh)) {
1018  sh->second.assign_children(new Siblings(&root_, sh->first));
1019  }
1020 
1021  sh->second.children()->members().emplace(
1022  v,
1023  Node(sh->second.children(),
1024  boost::get(edge_filtration_t(), skel_graph, *e_it)));
1025  }
1026  }
1027  }
1028 
1040  void expansion(int max_dim) {
1041  dimension_ = max_dim;
1042  for (Dictionary_it root_it = root_.members_.begin();
1043  root_it != root_.members_.end(); ++root_it) {
1044  if (has_children(root_it)) {
1045  siblings_expansion(root_it->second.children(), max_dim - 1);
1046  }
1047  }
1048  dimension_ = max_dim - dimension_;
1049  }
1050 
1051  private:
1053  void siblings_expansion(Siblings * siblings, // must contain elements
1054  int k) {
1055  if (dimension_ > k) {
1056  dimension_ = k;
1057  }
1058  if (k == 0)
1059  return;
1060  Dictionary_it next = siblings->members().begin();
1061  ++next;
1062 
1063  static std::vector<std::pair<Vertex_handle, Node> > inter; // static, not thread-safe.
1064  for (Dictionary_it s_h = siblings->members().begin();
1065  s_h != siblings->members().end(); ++s_h, ++next) {
1066  Simplex_handle root_sh = find_vertex(s_h->first);
1067  if (has_children(root_sh)) {
1068  intersection(
1069  inter, // output intersection
1070  next, // begin
1071  siblings->members().end(), // end
1072  root_sh->second.children()->members().begin(),
1073  root_sh->second.children()->members().end(),
1074  s_h->second.filtration());
1075  if (inter.size() != 0) {
1076  Siblings * new_sib = new Siblings(siblings, // oncles
1077  s_h->first, // parent
1078  inter); // boost::container::ordered_unique_range_t
1079  inter.clear();
1080  s_h->second.assign_children(new_sib);
1081  siblings_expansion(new_sib, k - 1);
1082  } else {
1083  // ensure the children property
1084  s_h->second.assign_children(siblings);
1085  inter.clear();
1086  }
1087  }
1088  }
1089  }
1090 
1093  static void intersection(std::vector<std::pair<Vertex_handle, Node> >& intersection,
1094  Dictionary_it begin1, Dictionary_it end1,
1095  Dictionary_it begin2, Dictionary_it end2,
1096  Filtration_value filtration_) {
1097  if (begin1 == end1 || begin2 == end2)
1098  return; // ----->>
1099  while (true) {
1100  if (begin1->first == begin2->first) {
1101  Filtration_value filt = (std::max)({begin1->second.filtration(), begin2->second.filtration(), filtration_});
1102  intersection.emplace_back(begin1->first, Node(nullptr, filt));
1103  if (++begin1 == end1 || ++begin2 == end2)
1104  return; // ----->>
1105  } else if (begin1->first < begin2->first) {
1106  if (++begin1 == end1)
1107  return;
1108  } else /* begin1->first > begin2->first */ {
1109  if (++begin2 == end2)
1110  return; // ----->>
1111  }
1112  }
1113  }
1114 
1115  public:
1122  void print_hasse(std::ostream& os) {
1123  os << num_simplices() << " " << std::endl;
1124  for (auto sh : filtration_simplex_range()) {
1125  os << dimension(sh) << " ";
1126  for (auto b_sh : boundary_simplex_range(sh)) {
1127  os << key(b_sh) << " ";
1128  }
1129  os << filtration(sh) << " \n";
1130  }
1131  }
1132 
1133  public:
1143  bool modified = false;
1144  // Loop must be from the end to the beginning, as higher dimension simplex are always on the left part of the tree
1145  for (auto& simplex : boost::adaptors::reverse(root_.members())) {
1146  if (has_children(&simplex)) {
1147  modified |= rec_make_filtration_non_decreasing(simplex.second.children());
1148  }
1149  }
1150  return modified;
1151  }
1152 
1153  private:
1158  bool rec_make_filtration_non_decreasing(Siblings * sib) {
1159  bool modified = false;
1160 
1161  // Loop must be from the end to the beginning, as higher dimension simplex are always on the left part of the tree
1162  for (auto& simplex : boost::adaptors::reverse(sib->members())) {
1163  // Find the maximum filtration value in the border
1164  Boundary_simplex_range boundary = boundary_simplex_range(&simplex);
1165  Boundary_simplex_iterator max_border = std::max_element(std::begin(boundary), std::end(boundary),
1166  [](Simplex_handle sh1, Simplex_handle sh2) {
1167  return filtration(sh1) < filtration(sh2);
1168  });
1169 
1170  Filtration_value max_filt_border_value = filtration(*max_border);
1171  if (simplex.second.filtration() < max_filt_border_value) {
1172  // Store the filtration modification information
1173  modified = true;
1174  simplex.second.assign_filtration(max_filt_border_value);
1175  }
1176  if (has_children(&simplex)) {
1177  modified |= rec_make_filtration_non_decreasing(simplex.second.children());
1178  }
1179  }
1180  // Make the modified information to be traced by upper call
1181  return modified;
1182  }
1183 
1184  public:
1193  return rec_prune_above_filtration(root(), filtration);
1194  }
1195 
1196  private:
1197  bool rec_prune_above_filtration(Siblings* sib, Filtration_value filt) {
1198  auto&& list = sib->members();
1199  auto last = std::remove_if(list.begin(), list.end(), [=](Dit_value_t& simplex) {
1200  if (simplex.second.filtration() <= filt) return false;
1201  if (has_children(&simplex)) rec_delete(simplex.second.children());
1202  return true;
1203  });
1204 
1205  bool modified = (last != list.end());
1206  if (last == list.begin() && sib != root()) {
1207  // Removing the whole siblings, parent becomes a leaf.
1208  sib->oncles()->members()[sib->parent()].assign_children(sib->oncles());
1209  delete sib;
1210  return true;
1211  } else {
1212  // Keeping some elements of siblings. Remove the others, and recurse in the remaining ones.
1213  list.erase(last, list.end());
1214  for (auto&& simplex : list)
1215  if (has_children(&simplex))
1216  modified |= rec_prune_above_filtration(simplex.second.children(), filt);
1217  }
1218  return modified;
1219  }
1220 
1221  public:
1229  // Guarantee the simplex has no children
1230  GUDHI_CHECK(!has_children(sh),
1231  std::invalid_argument("Simplex_tree::remove_maximal_simplex - argument has children"));
1232 
1233  // Simplex is a leaf, it means the child is the Siblings owning the leaf
1234  Siblings* child = sh->second.children();
1235 
1236  if ((child->size() > 1) || (child == root())) {
1237  // Not alone, just remove it from members
1238  // Special case when child is the root of the simplex tree, just remove it from members
1239  child->erase(sh);
1240  } else {
1241  // Sibling is emptied : must be deleted, and its parent must point on his own Sibling
1242  child->oncles()->members().at(child->parent()).assign_children(child->oncles());
1243  delete child;
1244  }
1245  }
1246 
1247  private:
1248  Vertex_handle null_vertex_;
1250  Filtration_value threshold_;
1253  Siblings root_;
1255  std::vector<Simplex_handle> filtration_vect_;
1257  int dimension_;
1258 };
1259 
1260 // Print a Simplex_tree in os.
1261 template<typename...T>
1262 std::ostream& operator<<(std::ostream & os, Simplex_tree<T...> & st) {
1263  for (auto sh : st.filtration_simplex_range()) {
1264  os << st.dimension(sh) << " ";
1265  for (auto v : st.simplex_vertex_range(sh)) {
1266  os << v << " ";
1267  }
1268  os << st.filtration(sh) << "\n"; // TODO(VR): why adding the key ?? not read ?? << " " << st.key(sh) << " \n";
1269  }
1270  return os;
1271 }
1272 
1273 template<typename...T>
1274 std::istream& operator>>(std::istream & is, Simplex_tree<T...> & st) {
1275  typedef Simplex_tree<T...> ST;
1276  std::vector<typename ST::Vertex_handle> simplex;
1277  typename ST::Filtration_value fil;
1278  typename ST::Filtration_value max_fil = 0;
1279  int max_dim = -1;
1280  while (read_simplex(is, simplex, fil)) {
1281  // read all simplices in the file as a list of vertices
1282  // Warning : simplex_size needs to be casted in int - Can be 0
1283  int dim = static_cast<int> (simplex.size() - 1);
1284  if (max_dim < dim) {
1285  max_dim = dim;
1286  }
1287  if (max_fil < fil) {
1288  max_fil = fil;
1289  }
1290  // insert every simplex in the simplex tree
1291  st.insert_simplex(simplex, fil);
1292  simplex.clear();
1293  }
1294  st.set_dimension(max_dim);
1295  st.set_filtration(max_fil);
1296 
1297  return is;
1298 }
1299 
1303  typedef int Vertex_handle;
1304  typedef double Filtration_value;
1305  typedef int Simplex_key;
1306  static const bool store_key = true;
1307  static const bool store_filtration = true;
1308  static const bool contiguous_vertices = false;
1309 };
1310 
1316  typedef int Vertex_handle;
1317  typedef float Filtration_value;
1318  typedef int Simplex_key;
1319  static const bool store_key = true;
1320  static const bool store_filtration = true;
1321  static const bool contiguous_vertices = true;
1322 };
1323  // end defgroup simplex_tree
1325 
1326 } // namespace Gudhi
1327 
1328 #endif // SIMPLEX_TREE_H_
Simplex_tree_skeleton_simplex_iterator< Simplex_tree > Skeleton_simplex_iterator
Iterator over the simplices of the skeleton of the simplicial complex, for a given dimension...
Definition: Simplex_tree.h:215
Dictionary::iterator Simplex_handle
Handle type to a simplex contained in the simplicial complex represented by the simplex tree...
Definition: Simplex_tree.h:164
bool prune_above_filtration(Filtration_value filtration)
Prune above filtration value given as parameter.
Definition: Simplex_tree.h:1192
void expansion(int max_dim)
Expands the Simplex_tree containing only its one skeleton until dimension max_dim.
Definition: Simplex_tree.h:1040
Filtration_simplex_range::const_iterator Filtration_simplex_iterator
Iterator over the simplices of the simplicial complex, ordered by the filtration. ...
Definition: Simplex_tree.h:224
Tag for a linear ordering of simplices.
Definition: indexing_tag.h:32
Simplex Tree data structure for representing simplicial complexes.
Definition: Simplex_tree.h:102
Siblings * self_siblings(SimplexHandle sh)
Definition: Simplex_tree.h:778
std::pair< Simplex_handle, bool > insert_simplex_and_subfaces(const InputVertexRange &Nsimplex, Filtration_value filtration=0)
Insert a N-simplex and all his subfaces, from a N-simplex represented by a range of Vertex_handles...
Definition: Simplex_tree.h:693
void assign_key(Simplex_handle sh, Simplex_key key)
Assign a value 'key' to the key of the simplex represented by the Simplex_handle 'sh'.
Definition: Simplex_tree.h:764
bool operator==(Simplex_tree &st2)
Checks if two simplex trees are equal.
Definition: Simplex_tree.h:388
Concept describing an indexing scheme (see FilteredComplex) for applying continuous maps to a cell co...
Definition: IndexingTag.h:30
static Simplex_key key(Simplex_handle sh)
Returns the key associated to a simplex.
Definition: Simplex_tree.h:426
static Simplex_handle null_simplex()
Returns a Simplex_handle different from all Simplex_handles associated to the simplices in the simpli...
Definition: Simplex_tree.h:470
static constexpr bool contiguous_vertices
If true, the list of vertices present in the complex must always be 0, ..., num_vertices-1, without any hole.
Definition: SimplexTreeOptions.h:41
boost::iterator_range< Complex_vertex_iterator > Complex_vertex_range
Range over the vertices of the simplicial complex.
Definition: Simplex_tree.h:190
Simplex_tree_simplex_vertex_iterator< Simplex_tree > Simplex_vertex_iterator
Iterator over the vertices of a simplex.
Definition: Simplex_tree.h:194
Vertex_handle null_vertex() const
Returns a Vertex_handle different from all Vertex_handles associated to the vertices of the simplicia...
Definition: Simplex_tree.h:482
void print_hasse(std::ostream &os)
Write the hasse diagram of the simplicial complex in os.
Definition: Simplex_tree.h:1122
Siblings * root()
Definition: Simplex_tree.h:787
Simplex_tree(Simplex_tree &&old)
User-defined move constructor moves the whole tree structure.
Definition: Simplex_tree.h:355
Filtration_value filtration() const
Returns an upper bound of the filtration values of the simplices.
Definition: Simplex_tree.h:462
Options::Simplex_key Simplex_key
Key associated to each simplex.
Definition: Simplex_tree.h:113
bool operator!=(Simplex_tree &st2)
Checks if two simplex trees are different.
Definition: Simplex_tree.h:397
bool make_filtration_non_decreasing()
Browse the simplex tree to ensure the filtration is not decreasing. The simplex tree is browsed start...
Definition: Simplex_tree.h:1142
std::vector< Simplex_handle > Filtration_simplex_range
Range over the simplices of the simplicial complex, ordered by the filtration.
Definition: Simplex_tree.h:220
Filtration_simplex_range const & filtration_simplex_range(Indexing_tag=Indexing_tag())
Returns a range over the simplices of the simplicial complex, in the order of the filtration...
Definition: Simplex_tree.h:277
void initialize_filtration()
Initializes the filtrations, i.e. sort the simplices according to their order in the filtration and i...
Definition: Simplex_tree.h:811
Simplex_handle find(const InputVertexRange &s)
Given a range of Vertex_handles, returns the Simplex_handle of the simplex in the simplicial complex ...
Definition: Simplex_tree.h:545
Cofaces_simplex_range cofaces_simplex_range(const Simplex_handle simplex, int codimension)
Compute the cofaces of a n simplex.
Definition: Simplex_tree.h:903
Complex_simplex_range complex_simplex_range()
Returns a range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:243
Concept of the template parameter for the class Gudhi::Simplex_tree<SimplexTreeOptions>.
Definition: SimplexTreeOptions.h:27
void assign_filtration(Simplex_handle sh, Filtration_value fv)
Sets the filtration value of a simplex. In debug mode, if sh is a null_simplex.
Definition: Simplex_tree.h:455
std::pair< Simplex_handle, Simplex_handle > endpoints(Simplex_handle sh)
Definition: Simplex_tree.h:771
Boundary_simplex_range boundary_simplex_range(SimplexHandle sh)
Returns a range over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:311
int dimension() const
Returns an upper bound on the dimension of the simplicial complex.
Definition: Simplex_tree.h:526
Complex_vertex_range complex_vertex_range()
Returns a range over the vertices of the simplicial complex. The order is increasing according to < o...
Definition: Simplex_tree.h:232
Simplex_vertex_range simplex_vertex_range(Simplex_handle sh)
Returns a range over the vertices of a simplex.
Definition: Simplex_tree.h:290
Options::Vertex_handle Vertex_handle
Type for the vertex handle.
Definition: Simplex_tree.h:117
Skeleton_simplex_range skeleton_simplex_range(int dim)
Returns a range over the simplices of the dim-skeleton of the simplicial complex. ...
Definition: Simplex_tree.h:257
boost::iterator_range< Boundary_simplex_iterator > Boundary_simplex_range
Range over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:204
void set_filtration(Filtration_value fil)
Definition: Simplex_tree.h:792
Cofaces_simplex_range star_simplex_range(const Simplex_handle simplex)
Compute the star of a n simplex.
Definition: Simplex_tree.h:892
void set_dimension(int dimension)
Definition: Simplex_tree.h:797
bool has_children(SimplexHandle sh) const
Returns true if the node in the simplex tree pointed by sh has children.
Definition: Simplex_tree.h:533
Simplex_handle simplex(Simplex_key key) const
Returns the simplex associated to a key.
Definition: Simplex_tree.h:435
int dimension(Simplex_handle sh)
Returns the dimension of a simplex.
Definition: Simplex_tree.h:515
static Simplex_key null_key()
Returns a key different for all keys associated to the simplices of the simplicial complex...
Definition: Simplex_tree.h:476
static Filtration_value filtration(Simplex_handle sh)
Returns the filtration value of a simplex.
Definition: Simplex_tree.h:444
Definition: Simplex_tree.h:1314
boost::transform_iterator< return_first, Dictionary_it > Complex_vertex_iterator
Iterator over the vertices of the simplicial complex.
Definition: Simplex_tree.h:188
~Simplex_tree()
Destructor; deallocates the whole tree structure.
Definition: Simplex_tree.h:367
size_t num_vertices() const
Returns the number of vertices in the complex.
Definition: Simplex_tree.h:487
void remove_maximal_simplex(Simplex_handle sh)
Remove a maximal simplex.
Definition: Simplex_tree.h:1228
static const bool store_filtration
If true, each simplex has extra storage for one Filtration_value, and this value is propagated by ope...
Definition: SimplexTreeOptions.h:39
size_t num_simplices()
returns the number of simplices in the simplex_tree.
Definition: Simplex_tree.h:493
boost::iterator_range< Complex_simplex_iterator > Complex_simplex_range
Range over the simplices of the simplicial complex.
Definition: Simplex_tree.h:210
Options::Filtration_value Filtration_value
Type for the value of the filtration function.
Definition: Simplex_tree.h:109
Simplex_tree()
Constructs an empty simplex tree.
Definition: Simplex_tree.h:321
Simplex_tree_boundary_simplex_iterator< Simplex_tree > Boundary_simplex_iterator
Iterator over the simplices of the boundary of a simplex.
Definition: Simplex_tree.h:202
boost::iterator_range< Skeleton_simplex_iterator > Skeleton_simplex_range
Range over the simplices of the skeleton of the simplicial complex, for a given dimension.
Definition: Simplex_tree.h:218
Simplex_tree_complex_simplex_iterator< Simplex_tree > Complex_simplex_iterator
Iterator over the simplices of the simplicial complex.
Definition: Simplex_tree.h:208
void insert_graph(const OneSkeletonGraph &skel_graph)
Inserts a 1-skeleton in an empty Simplex_tree.
Definition: Simplex_tree.h:985
Simplex_tree(const Simplex_tree &simplex_source)
User-defined copy constructor reproduces the whole tree structure.
Definition: Simplex_tree.h:329
boost::iterator_range< Simplex_vertex_iterator > Simplex_vertex_range
Range over the vertices of a simplex.
Definition: Simplex_tree.h:196
std::pair< Simplex_handle, bool > insert_simplex(const InputVertexRange &simplex, Filtration_value filtration=0)
Insert a simplex, represented by a range of Vertex_handles, in the simplicial complex.
Definition: Simplex_tree.h:664
void rec_copy(Siblings *sib, Siblings *sib_source)
depth first search, inserts simplices when reaching a leaf.
Definition: Simplex_tree.h:340
std::vector< Simplex_handle > Cofaces_simplex_range
Range over the cofaces of a simplex.
Definition: Simplex_tree.h:198
GUDHI  Version 1.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding. Generated on Fri Apr 15 2016 17:36:21 for GUDHI by doxygen 1.8.6