Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
39 bj 1
#ifndef __ITERS_H
2
#define __ITERS_H
3
 
4
#include "CGLA/Vec3f.h"
5
#include <list>
6
 
7
 
8
namespace HMesh
9
{
62 jab 10
		/* A nasty template trick:
39 bj 11
 
62 jab 12
		The halfedge data structure is based on pointers. Thus, for instance,
13
		a halfedge contains pointers to several other halfedges, a face, and
14
		a vertex. However, the entities (faces, edges, vertices) are stored in
15
		linked lists. This enables easy insertion and removal, but we would
16
		like to be able to take the halfedge pointer stored in, say, a 
17
		halfedge and find the position in the linked list in order to remove the
18
		halfedge from the list. This is not possible since the list is implemented
19
		using STL and what we need is really an iterator to the list element
20
		containing the halfedge.
39 bj 21
 
62 jab 22
		The solution is, of course, to store iterators instead of pointers, but
23
		that solution is problematic because we cannot define an iterator to a 
24
		list of halfedges in the definition of the halfedge class. The solution
25
		is to let a template argument define the halfedge iterator type.
39 bj 26
 
62 jab 27
		Thus the Iters class instantiates HalfEdge_template with itself while it 
28
		also defines the required iterators etc. Strange but legal C++.
39 bj 29
 
62 jab 30
		This is the best solution I could come up with - since I really wanted 
31
		to use STL to store the elements. The solution is similar to what is 
32
		used in CGAL and OpenMesh both of which provided inspiration. */
39 bj 33
 
62 jab 34
		template<class R>
39 bj 35
		struct Vertex_template
36
		{
62 jab 37
				typedef typename R::VertexIter VertexIter;
38
				typedef typename R::FaceIter FaceIter;
39
				typedef typename R::HalfEdgeIter HalfEdgeIter;
39 bj 40
 
62 jab 41
				/// Constructor, argument is vertex position
42
				Vertex_template(const CGLA::Vec3f&);
39 bj 43
 
62 jab 44
				/// Outgoing halfedge
45
				HalfEdgeIter he;
39 bj 46
 
62 jab 47
				/// Geometric vertex position
48
				CGLA::Vec3f pos;
39 bj 49
 
62 jab 50
				/// General purpose integer. Normally used as index or flag.
51
				int touched;
39 bj 52
		};
53
 
62 jab 54
		template<class R>
39 bj 55
		struct HalfEdge_template
56
		{
62 jab 57
				typedef typename R::VertexIter VertexIter;
58
				typedef typename R::FaceIter FaceIter;
59
				typedef typename R::HalfEdgeIter HalfEdgeIter;
39 bj 60
 
62 jab 61
				/// Constructor
62
				HalfEdge_template();
39 bj 63
 
62 jab 64
				/// Vertex pointed to by this halfedge.
65
				VertexIter   vert;
39 bj 66
 
62 jab 67
				/// Next halfedge in face loop.
68
				HalfEdgeIter next;
39 bj 69
 
62 jab 70
				/// Previous halfedge in face loop.
71
				HalfEdgeIter prev;
72
 
73
				/// Face owning this halfedge
74
				FaceIter     face;
75
 
76
				/// Opposite halfedge
77
				HalfEdgeIter opp;
78
 
79
				/// General purpose integer. Normally used as index or flag.
80
				int touched;
81
 
39 bj 82
		};
83
 
62 jab 84
		template<class R>
39 bj 85
		struct Face_template
86
		{
62 jab 87
				typedef typename R::VertexIter VertexIter;
88
				typedef typename R::FaceIter FaceIter;
89
				typedef typename R::HalfEdgeIter HalfEdgeIter;
39 bj 90
 
62 jab 91
				/// Constructor
92
				Face_template();
39 bj 93
 
62 jab 94
				/// Last is just some halfedge in the face loop.
95
				HalfEdgeIter last;
96
 
97
				/// General purpose integer. Normally used as index or flag.
98
				int touched;
39 bj 99
		};
100
 
62 jab 101
		struct Iters
102
		{
103
				typedef Vertex_template<Iters> V;
104
				typedef HalfEdge_template<Iters> HE;
105
				typedef Face_template<Iters> F;
39 bj 106
 
62 jab 107
				typedef std::list<V> VertexList;
108
				typedef std::list<HE> HalfEdgeList;
109
				typedef std::list<F> FaceList;
39 bj 110
 
62 jab 111
				typedef VertexList::iterator VertexIter;
112
				typedef HalfEdgeList::iterator HalfEdgeIter;
113
				typedef FaceList::iterator FaceIter;
114
		};
39 bj 115
 
116
 
117
 
118
}
119
#endif