Subversion Repositories gelsvn

Rev

Rev 62 | Go to most recent revision | Details | Compare with Previous | 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. */
89 jab 33
 
34
		/** \struct Vertex
35
		 */
62 jab 36
		template<class R>
39 bj 37
		struct Vertex_template
38
		{
62 jab 39
				typedef typename R::VertexIter VertexIter;
40
				typedef typename R::FaceIter FaceIter;
41
				typedef typename R::HalfEdgeIter HalfEdgeIter;
39 bj 42
 
62 jab 43
				/// Constructor, argument is vertex position
44
				Vertex_template(const CGLA::Vec3f&);
39 bj 45
 
62 jab 46
				/// Outgoing halfedge
47
				HalfEdgeIter he;
39 bj 48
 
62 jab 49
				/// Geometric vertex position
50
				CGLA::Vec3f pos;
39 bj 51
 
62 jab 52
				/// General purpose integer. Normally used as index or flag.
53
				int touched;
39 bj 54
		};
55
 
62 jab 56
		template<class R>
39 bj 57
		struct HalfEdge_template
58
		{
62 jab 59
				typedef typename R::VertexIter VertexIter;
60
				typedef typename R::FaceIter FaceIter;
61
				typedef typename R::HalfEdgeIter HalfEdgeIter;
39 bj 62
 
62 jab 63
				/// Constructor
64
				HalfEdge_template();
39 bj 65
 
62 jab 66
				/// Vertex pointed to by this halfedge.
67
				VertexIter   vert;
39 bj 68
 
62 jab 69
				/// Next halfedge in face loop.
70
				HalfEdgeIter next;
39 bj 71
 
62 jab 72
				/// Previous halfedge in face loop.
73
				HalfEdgeIter prev;
74
 
75
				/// Face owning this halfedge
76
				FaceIter     face;
77
 
78
				/// Opposite halfedge
79
				HalfEdgeIter opp;
80
 
81
				/// General purpose integer. Normally used as index or flag.
82
				int touched;
83
 
39 bj 84
		};
85
 
62 jab 86
		template<class R>
39 bj 87
		struct Face_template
88
		{
62 jab 89
				typedef typename R::VertexIter VertexIter;
90
				typedef typename R::FaceIter FaceIter;
91
				typedef typename R::HalfEdgeIter HalfEdgeIter;
39 bj 92
 
62 jab 93
				/// Constructor
94
				Face_template();
39 bj 95
 
62 jab 96
				/// Last is just some halfedge in the face loop.
97
				HalfEdgeIter last;
98
 
99
				/// General purpose integer. Normally used as index or flag.
100
				int touched;
39 bj 101
		};
102
 
62 jab 103
		struct Iters
104
		{
105
				typedef Vertex_template<Iters> V;
106
				typedef HalfEdge_template<Iters> HE;
107
				typedef Face_template<Iters> F;
39 bj 108
 
62 jab 109
				typedef std::list<V> VertexList;
110
				typedef std::list<HE> HalfEdgeList;
111
				typedef std::list<F> FaceList;
39 bj 112
 
62 jab 113
				typedef VertexList::iterator VertexIter;
114
				typedef HalfEdgeList::iterator HalfEdgeIter;
115
				typedef FaceList::iterator FaceIter;
116
		};
39 bj 117
 
118
 
119
 
120
}
121
#endif