Subversion Repositories gelsvn

Rev

Rev 89 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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