summaryrefslogtreecommitdiff
path: root/src/Skeleton_blocker/include/gudhi/Skeleton_blocker/iterators/Skeleton_blockers_triangles_iterators.h
blob: 4b66ced58afa36fdc9f05a643d4842997a8b8bf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Skeleton_blockers_triangles_iterators.h
 *
 *  Created on: Aug 25, 2014
 *      Author: dsalinas
 */

#ifndef GUDHI_SKELETON_BLOCKERS_TRIANGLES_ITERATORS_H_
#define GUDHI_SKELETON_BLOCKERS_TRIANGLES_ITERATORS_H_

#include "boost/iterator/iterator_facade.hpp"
#include <memory>

namespace Gudhi{

namespace skbl {

//////////////////////////////////////////////////////////////////////
/**
 * \brief Iterator over the triangles that are
 * adjacent to a vertex of the simplicial complex.
 * \remark Will be removed soon -> dont look
 */
template<typename Complex,typename LinkType>
class Triangle_around_vertex_iterator : public boost::iterator_facade
< Triangle_around_vertex_iterator <Complex,LinkType>
, typename Complex::Simplex_handle const
, boost::forward_traversal_tag
, typename Complex::Simplex_handle const
>
{
	friend class boost::iterator_core_access;
	template<typename T> friend class Triangle_iterator;
private:
	typedef typename LinkType::Vertex_handle Vertex_handle;
	typedef typename LinkType::Root_vertex_handle Root_vertex_handle;
	typedef typename LinkType::Simplex_handle Simplex_handle;
	typedef Complex_edge_iterator<Complex> Complex_edge_iterator_;

	const Complex* complex_;
	Vertex_handle v_;
	std::shared_ptr<LinkType> link_;
	Complex_edge_iterator_ current_edge_;
	bool is_end_;
public:
	Triangle_around_vertex_iterator(const Complex* complex,Vertex_handle v):
		complex_(complex),v_(v),link_(new LinkType(*complex,v_)),
		current_edge_(link_->edge_range().begin()),
		is_end_(current_edge_ == link_->edge_range().end()){
	}

	/**
	 * @brief ugly hack to get an iterator to the end
	 */
	Triangle_around_vertex_iterator(const Complex* complex,Vertex_handle v,bool is_end):
		complex_(complex),v_(v),link_(0),is_end_(true){
	}

	/**
	 * @brief ugly hack to get an iterator to the end
	 */
	Triangle_around_vertex_iterator():
		complex_(0),v_(-1),link_(0),is_end_(true){
	}


	Triangle_around_vertex_iterator(const Triangle_around_vertex_iterator& other){
		v_ = other.v_;
		complex_ = other.complex_;
		is_end_ = other.is_end_;

		if(!is_end_){
			link_ = other.link_;
			current_edge_= other.current_edge_;
		}
	}

	bool equal(const Triangle_around_vertex_iterator& other) const{
		return (complex_==other.complex_) && ((finished() &&other.finished()) || current_edge_ == other.current_edge_);
	}

	Simplex_handle dereference() const{
		Root_vertex_handle v1 = (*link_)[*current_edge_].first();
		Root_vertex_handle v2 = (*link_)[*current_edge_].second();
		return Simplex_handle(v_,*(complex_->get_address(v1)),*(complex_->get_address(v2)));
	}

	void increment(){
		++current_edge_;
	}

private:
	bool finished() const{
		return is_end_ || (current_edge_ == link_->edge_range().end());
	}

};



/**
 * \brief Iterator over the triangles of the
 * simplicial complex.
 * \remark Will be removed soon -> dont look
 *
 */
template<typename SkeletonBlockerComplex>
class Triangle_iterator :
		public boost::iterator_facade<
		Triangle_iterator <SkeletonBlockerComplex>,
		typename SkeletonBlockerComplex::Simplex_handle const
		, boost::forward_traversal_tag
		, typename SkeletonBlockerComplex::Simplex_handle const
		>
{
	friend class boost::iterator_core_access;
private:
	typedef typename SkeletonBlockerComplex::Vertex_handle Vertex_handle;
	typedef typename SkeletonBlockerComplex::Root_vertex_handle Root_vertex_handle;
	typedef typename SkeletonBlockerComplex::Simplex_handle Simplex_handle;
	typedef typename SkeletonBlockerComplex::Superior_triangle_around_vertex_iterator STAVI;

	const SkeletonBlockerComplex* complex_;
	Complex_vertex_iterator<SkeletonBlockerComplex> current_vertex_;
	STAVI current_triangle_;
	bool is_end_;
public:

	/*
	 * @remark  assume that the complex is non-empty
	 */
	Triangle_iterator(const SkeletonBlockerComplex* complex):
		complex_(complex),
		current_vertex_(complex->vertex_range().begin()),
		current_triangle_(complex,*current_vertex_), // xxx this line is problematic is the complex is empty
		is_end_(false){

		assert(!complex->empty());
		gotoFirstTriangle();
	}

private:
	//goto to the first triangle or to the end if none
	void gotoFirstTriangle(){
		if(!is_finished() && current_triangle_.finished()){
			goto_next_vertex();
		}
	}
public:

	/**
	 * @brief ugly hack to get an iterator to the end
	 * @remark  assume that the complex is non-empty
	 */
	Triangle_iterator(const SkeletonBlockerComplex* complex,bool is_end):
		complex_(complex),
		current_vertex_(complex->vertex_range().end()),
		current_triangle_(), // xxx this line is problematic is the complex is empty
		is_end_(true){
	}


	Triangle_iterator& operator=(const Triangle_iterator & other){
		complex_ = other.complex_;
		Complex_vertex_iterator<SkeletonBlockerComplex> current_vertex_;
		STAVI current_triangle_;
		return *this;
	}


	bool equal(const Triangle_iterator& other) const{
		bool both_are_finished = is_finished() && other.is_finished();
		bool both_arent_finished = !is_finished() && !other.is_finished();
		// if the two iterators are not finished, they must have the same state
		return (complex_==other.complex_) &&
				(both_are_finished	||
						( (both_arent_finished) && current_vertex_ == other.current_vertex_ && current_triangle_ == other.current_triangle_));

	}

	Simplex_handle dereference() const{
		return *current_triangle_;
	}

private:

	// goto the next vertex that has a triangle pending or the
	// end vertex iterator if none exists
	void goto_next_vertex(){
		assert(current_triangle_.finished()); //we mush have consume all triangles passing through the vertex
		assert(!is_finished()); // we must not be done

		++current_vertex_;

		if(!is_finished()){
			current_triangle_ = STAVI(complex_, *current_vertex_);
			if(current_triangle_.finished())
				goto_next_vertex();
		}
	}
public:
	void increment(){
		if(!current_triangle_.finished()){
			++current_triangle_; // problem here
			if(current_triangle_.finished())
				goto_next_vertex();
		}
		else{
			assert(!is_finished());
			goto_next_vertex();
		}
	}

private:
	bool is_finished() const{
		return is_end_ || current_vertex_ == complex_->vertex_range().end();
	}
};

}

} // namespace GUDHI

#endif /* GUDHI_SKELETON_BLOCKERS_TRIANGLES_ITERATORS_H_ */