Subversion Repositories gelsvn

Rev

Rev 62 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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