Subversion Repositories gelsvn

Rev

Rev 89 | Rev 133 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
39 bj 1
#ifndef __MANIFOLD_H
2
#define __MANIFOLD_H
3
 
4
#include <vector>
5
#include <list>
6
#include <map>
7
 
8
#include "Vertex.h"
9
#include "HalfEdge.h"
10
#include "Face.h"
11
 
12
namespace HMesh
13
{
14
	class VertexCirculator;
15
	class FaceCirculator;
16
 
89 jab 17
	/** \brief A Data structure representing an open or closed manifold.
18
 
39 bj 19
			Manifold keeps lists of the entities making up a halfedge mesh
20
			and provides basic functions for creating new faces, halfedges
21
			and vertices. There are also primitive operations for editing 
22
			such as edge collapse. */
23
	struct Manifold
24
	{
25
		std::list<Vertex> vertex_db;
26
		std::list<Face> face_db;
27
		std::list<HalfEdge> halfedge_db;
28
 
89 jab 29
		/** Remove a face if it contains only two edges.
30
 
31
		This is an auxiliary function called from collapse_halfedge. */
39 bj 32
		void remove_face_if_degenerate(HalfEdgeIter fi);
33
 
89 jab 34
		/** Empty copy constructor.
35
 
36
		Copying a manifold will not work, since faces, vertices, and
39 bj 37
				halfedges contain iterators pointing to other faces, vertices,
38
				and halfedges. These pointers would need to be changed if the 
39
				mesh were copied. In other words, we would need to walk the
40
				entire mesh. This may be required but it should not be an 
41
				operation that is easily invoked by calling the copy constructor.
42
				Hence, this operator is made private.		*/
43
		Manifold(const Manifold& m2) {}
44
 
89 jab 45
		/** Empty assignment operator.
46
 
47
		The assignment operator is private for the same reason that the
39 bj 48
				copy constructor is private. */
49
		const Manifold& operator=(const Manifold& m2) {}
50
 
51
		public:
52
 
53
		/// Construct an empty manifold.
54
		Manifold() {}
55
 
56
		/// Return the bounding box of the manifold.
57
		void get_bbox(CGLA::Vec3f&, CGLA::Vec3f&);
58
 
59
		/// Get a bounding sphere
60
    void get_bsphere(CGLA::Vec3f& c, float& r);
61
 
62
		/// Clear the manifold - removing all data.
63
		void clear()
64
		{
65
			vertex_db.clear();
66
			face_db.clear();
67
			halfedge_db.clear();
68
		}
69
 
70
		/// Return the number of faces.
71
		int no_faces() const {return face_db.size();}
72
 
73
		/// Return the number of halfedges.
74
		int no_halfedges() const {return halfedge_db.size();}
75
 
76
		/// Return the number of vertices
77
		int no_vertices() const {return vertex_db.size();}
78
 
79
		/// Create a new vertex at a given position.
80
		VertexIter create_vertex(const CGLA::Vec3f& pos)
81
		{
82
			vertex_db.push_back(Vertex(pos));
83
			return --vertex_db.end();
84
		}
85
 
86
		/// Create a new face 
87
		FaceIter create_face()
88
		{
89
			face_db.push_back(Face());
90
			return --face_db.end();
91
		}
92
 
93
		/// Create a new halfedge.
94
		HalfEdgeIter create_halfedge()
95
		{
96
			halfedge_db.push_back(HalfEdge());
97
			return --halfedge_db.end();
98
		}
99
 
100
		/// Erase halfedge. Assumes it is safe to do so. Use with care.
101
		void erase_halfedge(HalfEdgeIter he)
102
		{
103
			halfedge_db.erase(he);
104
		}
105
 
106
		/// Erase vertex. Assumes it is safe to do so. Use with care.
107
		void erase_vertex(VertexIter v)
108
		{
109
			vertex_db.erase(v);
110
		}
111
 
112
		/// Erase face. Assumes it is safe to do so. Use with care.
113
		void erase_face(FaceIter f)
114
		{
115
			face_db.erase(f);
116
		}
117
 
118
		/// Return iterator pointing to the first halfedge.
119
		HalfEdgeIter halfedges_begin() { return halfedge_db.begin();}
120
 
121
		/// Return iterator pointing to beyond the last halfedge.
122
		HalfEdgeIter halfedges_end() { return halfedge_db.end(); }
123
 
124
		/// Return iterator pointing to the first vertex.
125
		VertexIter vertices_begin() { return vertex_db.begin();}
126
 
127
		/// Return iterator pointing to beyond the last vertex.
128
		VertexIter vertices_end() { return vertex_db.end(); }
129
 
130
		/// Return iterator pointing to the first face.
131
		FaceIter faces_begin() { return face_db.begin();	}
132
 
133
		/// Return iterator pointing to beyond the last face.
134
		FaceIter faces_end() { return face_db.end(); }
135
 
113 jab 136
		/** \brief HalfEdge collapse precondition. 
137
 
39 bj 138
				If this function does not return true, it is illegal to collapse
139
				the halfedge given as second arg. The reason is that the collapse
113 jab 140
				would violate the manifold property of the mesh.
141
 
142
				The test is as follows.
143
 
144
				1. For the two vertices adjacent to the edge, we generate
145
				a list of all their neighbouring vertices. We then generate a 
146
				list of the vertices that occur in both these lists. That is, 
147
				we find all vertices connected by edges to both endpoints
148
				of the edge and store these in a list.
149
 
150
				2. For both faces incident on the edge, check whether they
151
				are triangular. If this is the case, the face will be removed,
152
				and it is ok that the the third vertex is connected to both 
153
				endpoints. Thus the third vertex in such a face is removed
154
				from the list generated in 1.
155
 
156
				3. If the list is now empty, all is well. Otherwise, there
157
				would be a vertex in the new mesh with two edges connecting
158
				it to the same vertex. Return false.
159
 
160
				4. TETRAHEDRON TEST:
161
				If the valency of both vertices is 
162
				three, and the incident faces are triangles, we also disallow
163
				the operation. Reason: It introduces a vertex of valency two
164
				and if the final two polygons incident on the vertices 
165
				that are adjacent to the edge being collapsed are triangles, then
166
				the construction will collapse
167
 
168
				5. VALENCY 4 TEST:
169
				If a face adjacent to the edge being collapsed is a triangle,
170
				it will disappear, and the valency of the final vertex beloning to
171
				this edge will be reduced by one. Hence this valency should be at
172
				least 4. A valency three vertex will be reduced to a valency two
173
				vertex which is considered illegal.
174
 
175
				6. PREVENT MERGING HOLES:
176
				We do not want to collapse an edge if its end points are boundary
177
				vertices, but its two adjacent faces are not NULL faces, because
178
				in that case we create a vertex where two holes meet. A non manifold
179
				situation.	*/
39 bj 180
		bool collapse_precond(VertexIter, HalfEdgeIter);
181
 
113 jab 182
		/** Collapse the halfedge given as second argument. 
183
 
184
		The first argument
185
				is the vertex that is being removed. The second is a halfedge pointing
186
				away from that vertex. This function is not guaranteed
39 bj 187
				to keep the mesh sane unless, collapse_precond has returned true. */
188
		void collapse_halfedge(VertexIter, HalfEdgeIter, bool=false);
189
 
190
		/** Split a face (first argument) by connecting two vertices (the
191
				next two arguments). */
192
		FaceIter split_face(FaceIter, VertexIter, VertexIter);
193
 
194
		/** Insert a new vertex on the halfedge given as argument. */
195
		VertexIter Manifold::split_edge(HalfEdgeIter h);
196
 
197
		/** Triangulate a polygonal face by repeatedly calling splitface. */
198
		void triangulate(FaceIter);
199
 
200
		/** Triangulate a polygon by inserting a vertex at the barycenter and 
201
				connecting all vertices to this vertex. */
202
		VertexIter safe_triangulate(FaceIter f);
203
 
204
		/** Insert a new vertex (second arg.) in a face (first arg.).
205
				This operation splits an N-gon into N triangles. */
206
		void face_insert_point(FaceIter f, VertexIter);
207
 
208
		/** Merges two faces into a single polygon. The first face is the first
209
				argument. The second face is adjacent to the first along the
210
				halfedge given as second argument. */
211
		bool merge_faces(FaceIter f, HalfEdgeIter h);
212
 
213
		/** flips an edge. Returns false if flipping cannot be performed.
214
				This is either because one of the two adjacent faces is not
215
				a triangle, or because either end point has valency three or
216
				because the vertices that will be connected already are. */
217
		bool flip(HalfEdgeIter);
218
 
219
		/** Performs a series of tests to check that this is a valid manifold.
220
				This function is not rigorously constructed but seems to catch
221
				all problems so far. */
222
		bool is_valid();
223
 
224
		/** Give each vertex a unique id corresponding to its iterator 
225
				position */
226
		void enumerate_vertices();
227
 
228
		/** Give each halfedge a unique id corresponding to its iterator 
229
				position */
230
		void enumerate_halfedges();
231
 
232
		/** Give each face a unique id corresponding to its iterator 
233
				position */
234
		void enumerate_faces();
235
 
236
	};
237
 
238
 
239
}
240
#endif