Subversion Repositories gelsvn

Rev

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

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