Subversion Repositories gelsvn

Rev

Rev 89 | Go to most recent revision | Details | 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
 
13
 
14
namespace HMesh
15
{
16
	class VertexCirculator;
17
	class FaceCirculator;
18
 
19
	/** A Data structure representing an open or closed manifold.
20
			Manifold keeps lists of the entities making up a halfedge mesh
21
			and provides basic functions for creating new faces, halfedges
22
			and vertices. There are also primitive operations for editing 
23
			such as edge collapse. */
24
 
25
	struct Manifold
26
	{
27
		std::list<Vertex> vertex_db;
28
		std::list<Face> face_db;
29
		std::list<HalfEdge> halfedge_db;
30
 
31
		/** Remove a face if it contains only two edges. This is an auxiliary
32
				function called from collapse_halfedge. */
33
		void remove_face_if_degenerate(HalfEdgeIter fi);
34
 
35
		/** Copying a manifold will not work, since faces, vertices, and
36
				halfedges contain iterators pointing to other faces, vertices,
37
				and halfedges. These pointers would need to be changed if the 
38
				mesh were copied. In other words, we would need to walk the
39
				entire mesh. This may be required but it should not be an 
40
				operation that is easily invoked by calling the copy constructor.
41
				Hence, this operator is made private.		*/
42
		Manifold(const Manifold& m2) {}
43
 
44
		/** The assignment operator is private for the same reason that the
45
				copy constructor is private. */
46
		const Manifold& operator=(const Manifold& m2) {}
47
 
48
		public:
49
 
50
		/// Construct an empty manifold.
51
		Manifold() {}
52
 
53
		/// Return the bounding box of the manifold.
54
		void get_bbox(CGLA::Vec3f&, CGLA::Vec3f&);
55
 
56
		/// Get a bounding sphere
57
    void get_bsphere(CGLA::Vec3f& c, float& r);
58
 
59
		/// Clear the manifold - removing all data.
60
		void clear()
61
		{
62
			vertex_db.clear();
63
			face_db.clear();
64
			halfedge_db.clear();
65
		}
66
 
67
		/// Return the number of faces.
68
		int no_faces() const {return face_db.size();}
69
 
70
		/// Return the number of halfedges.
71
		int no_halfedges() const {return halfedge_db.size();}
72
 
73
		/// Return the number of vertices
74
		int no_vertices() const {return vertex_db.size();}
75
 
76
		/// Create a new vertex at a given position.
77
		VertexIter create_vertex(const CGLA::Vec3f& pos)
78
		{
79
			vertex_db.push_back(Vertex(pos));
80
			return --vertex_db.end();
81
		}
82
 
83
		/// Create a new face 
84
		FaceIter create_face()
85
		{
86
			face_db.push_back(Face());
87
			return --face_db.end();
88
		}
89
 
90
		/// Create a new halfedge.
91
		HalfEdgeIter create_halfedge()
92
		{
93
			halfedge_db.push_back(HalfEdge());
94
			return --halfedge_db.end();
95
		}
96
 
97
		/// Erase halfedge. Assumes it is safe to do so. Use with care.
98
		void erase_halfedge(HalfEdgeIter he)
99
		{
100
			halfedge_db.erase(he);
101
		}
102
 
103
		/// Erase vertex. Assumes it is safe to do so. Use with care.
104
		void erase_vertex(VertexIter v)
105
		{
106
			vertex_db.erase(v);
107
		}
108
 
109
		/// Erase face. Assumes it is safe to do so. Use with care.
110
		void erase_face(FaceIter f)
111
		{
112
			face_db.erase(f);
113
		}
114
 
115
		/// Return iterator pointing to the first halfedge.
116
		HalfEdgeIter halfedges_begin() { return halfedge_db.begin();}
117
 
118
		/// Return iterator pointing to beyond the last halfedge.
119
		HalfEdgeIter halfedges_end() { return halfedge_db.end(); }
120
 
121
		/// Return iterator pointing to the first vertex.
122
		VertexIter vertices_begin() { return vertex_db.begin();}
123
 
124
		/// Return iterator pointing to beyond the last vertex.
125
		VertexIter vertices_end() { return vertex_db.end(); }
126
 
127
		/// Return iterator pointing to the first face.
128
		FaceIter faces_begin() { return face_db.begin();	}
129
 
130
		/// Return iterator pointing to beyond the last face.
131
		FaceIter faces_end() { return face_db.end(); }
132
 
133
		/** HalfEdge collapse precondition. 
134
				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
136
				would violate the manifold property of the mesh. */
137
		bool collapse_precond(VertexIter, HalfEdgeIter);
138
 
139
		/** Collapse the halfedge given as second argument. The first argument
140
				is the vertex that is being removed. This function is not guaranteed
141
				to keep the mesh sane unless, collapse_precond has returned true. */
142
		void collapse_halfedge(VertexIter, HalfEdgeIter, bool=false);
143
 
144
		/** Split a face (first argument) by connecting two vertices (the
145
				next two arguments). */
146
		FaceIter split_face(FaceIter, VertexIter, VertexIter);
147
 
148
		/** Insert a new vertex on the halfedge given as argument. */
149
		VertexIter Manifold::split_edge(HalfEdgeIter h);
150
 
151
		/** Triangulate a polygonal face by repeatedly calling splitface. */
152
		void triangulate(FaceIter);
153
 
154
		/** Triangulate a polygon by inserting a vertex at the barycenter and 
155
				connecting all vertices to this vertex. */
156
		VertexIter safe_triangulate(FaceIter f);
157
 
158
		/** Insert a new vertex (second arg.) in a face (first arg.).
159
				This operation splits an N-gon into N triangles. */
160
		void face_insert_point(FaceIter f, VertexIter);
161
 
162
		/** Merges two faces into a single polygon. The first face is the first
163
				argument. The second face is adjacent to the first along the
164
				halfedge given as second argument. */
165
		bool merge_faces(FaceIter f, HalfEdgeIter h);
166
 
167
		/** flips an edge. Returns false if flipping cannot be performed.
168
				This is either because one of the two adjacent faces is not
169
				a triangle, or because either end point has valency three or
170
				because the vertices that will be connected already are. */
171
		bool flip(HalfEdgeIter);
172
 
173
		/** Performs a series of tests to check that this is a valid manifold.
174
				This function is not rigorously constructed but seems to catch
175
				all problems so far. */
176
		bool is_valid();
177
 
178
		/** Give each vertex a unique id corresponding to its iterator 
179
				position */
180
		void enumerate_vertices();
181
 
182
		/** Give each halfedge a unique id corresponding to its iterator 
183
				position */
184
		void enumerate_halfedges();
185
 
186
		/** Give each face a unique id corresponding to its iterator 
187
				position */
188
		void enumerate_faces();
189
 
190
	};
191
 
192
 
193
}
194
#endif