Skeleton_blockers_vertices_iterators.h
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): David Salinas
4  *
5  * Copyright (C) 2014 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
12 #define SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
13 
14 #include <boost/iterator/iterator_facade.hpp>
15 
16 #include <utility> // for pair<>
17 
18 namespace Gudhi {
19 
20 namespace skeleton_blocker {
21 
27 template<typename SkeletonBlockerComplex>
28 class Vertex_iterator : public boost::iterator_facade< Vertex_iterator <SkeletonBlockerComplex>
29 , typename SkeletonBlockerComplex::Vertex_handle const
30 , boost::forward_traversal_tag
31 , typename SkeletonBlockerComplex::Vertex_handle const> {
32  friend class boost::iterator_core_access;
33 
34  typedef typename SkeletonBlockerComplex::boost_vertex_iterator boost_vertex_iterator;
35  typedef typename SkeletonBlockerComplex::Vertex_handle Vertex_handle;
36  private:
37  const SkeletonBlockerComplex* complex;
38  std::pair<boost_vertex_iterator, boost_vertex_iterator> vertexIterator;
39 
40 
41  public:
42  Vertex_iterator() : complex(NULL) { }
43 
44  Vertex_iterator(const SkeletonBlockerComplex* complex_) :
45  complex(complex_),
46  vertexIterator(vertices(complex_->skeleton)) {
47  if (!finished() && !is_active()) {
48  goto_next_valid();
49  }
50  }
51 
55  Vertex_iterator(const SkeletonBlockerComplex* complex_, int end) :
56  complex(complex_), vertexIterator(vertices(complex_->skeleton)) {
57  vertexIterator.first = vertexIterator.second;
58  }
59 
60  public:
61  void increment() {
62  goto_next_valid();
63  }
64 
65  Vertex_handle dereference() const {
66  return (Vertex_handle(*(vertexIterator.first)));
67  }
68 
69  bool equal(const Vertex_iterator& other) const {
70  return vertexIterator == other.vertexIterator && complex == other.complex;
71  }
72 
73  bool operator<(const Vertex_iterator& other) const {
74  return dereference() < other.dereference();
75  }
76 
77  private:
78  bool finished() const {
79  return vertexIterator.first == vertexIterator.second;
80  }
81 
82  void goto_next_valid() {
83  ++vertexIterator.first;
84  if (!finished() && !is_active()) {
85  goto_next_valid();
86  }
87  }
88 
89  bool is_active() const {
90  return ((*complex)[Vertex_handle(*vertexIterator.first)]).is_active();
91  }
92 };
93 
94 template<typename SkeletonBlockerComplex>
95 class Neighbors_vertices_iterator : public boost::iterator_facade < Neighbors_vertices_iterator<SkeletonBlockerComplex>
96 , typename SkeletonBlockerComplex::Vertex_handle const
97 , boost::forward_traversal_tag
98 , typename SkeletonBlockerComplex::Vertex_handle const> {
99  friend class boost::iterator_core_access;
100 
101  typedef SkeletonBlockerComplex Complex;
102  typedef typename Complex::boost_adjacency_iterator boost_adjacency_iterator;
103  typedef typename Complex::Vertex_handle Vertex_handle;
104  typedef typename Complex::Edge_handle Edge_handle;
105 
106  private:
107  const Complex* complex;
108  Vertex_handle v;
109 
110  boost_adjacency_iterator current_;
111  boost_adjacency_iterator end_;
112 
113  public:
114  Neighbors_vertices_iterator() : complex(NULL) { }
115 
116  Neighbors_vertices_iterator(const Complex* complex_, Vertex_handle v_) :
117  complex(complex_),
118  v(v_) {
119  tie(current_, end_) = adjacent_vertices(v.vertex, complex->skeleton);
120  }
121 
125  Neighbors_vertices_iterator(const Complex* complex_, Vertex_handle v_, int end) :
126  complex(complex_),
127  v(v_) {
128  tie(current_, end_) = adjacent_vertices(v.vertex, complex->skeleton);
129  set_end();
130  }
131 
132  void increment() {
133  if (current_ != end_)
134  ++current_;
135  }
136 
137  Vertex_handle dereference() const {
138  return (Vertex_handle(*current_));
139  }
140 
141  bool equal(const Neighbors_vertices_iterator& other) const {
142  return (complex == other.complex) && (v == other.v) && (current_ == other.current_) && (end_ == other.end_);
143  }
144 
145  private:
146  // TODO(DS): remove this ugly hack
147  void set_end() {
148  current_ = end_;
149  }
150 };
151 
152 } // namespace skeleton_blocker
153 
154 namespace skbl = skeleton_blocker;
155 
156 } // namespace Gudhi
157 
158 #endif // SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
Class that represents a geometric complex that can be simplified. The class allows access to points o...
Definition: Skeleton_blocker_geometric_complex.h:29
Iterator on the vertices of a simplicial complex.
Definition: Skeleton_blockers_vertices_iterators.h:31
Vertex_iterator(const SkeletonBlockerComplex *complex_, int end)
Definition: Skeleton_blockers_vertices_iterators.h:55
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14
Handle type for the vertices of a cell complex.
Definition: VertexHandle.h:15