Subversion Repositories gelsvn

Rev

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

Rev Author Line No. Line
39 bj 1
#ifndef __IMESH_TRIMESHBUILDER_H__
2
#define __IMESH_TRIMESHBUILDER_H__
3
 
4
#include "TriMesh.h"
5
 
6
namespace IMesh
7
{
8
 
89 jab 9
	/** \brief The TriMeshBuilder constructs a TriMesh. 
10
 
11
      The reason to have this
39 bj 12
			class is that indexed face sets are very "undynamic" to begin with.
13
			Typically, we store all data in vectors for efficiency, and it is
14
			difficult to add and especially remove data from these vectors.
15
			By having a builder class, we can separate concerns so that the 
16
			content adding is taken care of before the mesh is used. */
17
	class TriMeshBuilder: public TriMeshData
18
	{
19
		// A TriMeshBuilder is only constructed using the default constructor.
20
		TriMeshBuilder(const TriMeshBuilder&);
21
		bool did_call_get_trimesh;
22
	public:
23
 
24
 
25
		/** The constructor begins by registering the basic data
26
				types, i.e. the */
27
		TriMeshBuilder();
28
 
29
		~TriMeshBuilder();
30
 
31
		void clear();
32
 
33
		//----------------------------------------------------------------------
34
		// Register face sets and attributes
35
		//----------------------------------------------------------------------
36
 
37
		/** Register a new face set. If this is not the first face set, the 
38
				first face set is copied into the presently registered face set. */
39
		int register_face_set();
40
 
41
		/** Register a face attribute of type ATTR. If there is an existing 
42
				attribute of the same name, then the handle passed as second
43
				argument is made to point to this attribute and no new attribute
44
				is created. */
45
		template<class ATTR>
46
		void register_face_attribute(const std::string& attr_name,
47
																 AttrHandle<ATTR>& handle)
48
		{
49
			assert(!did_call_get_trimesh);
50
			if(!get_fattr_handle(attr_name, handle))
51
				{
52
					handle.idx = face_attr.size();
53
					face_attr.push_back(new AttrVec<ATTR>(attr_name));
54
				}
55
		}
56
 
57
		/// Utility function, register face colours.
58
		void register_face_colors();
59
 
60
		/// Utility function that registers the use of face normals.
61
		void register_face_normals();
62
 
63
		/** Register a vertex attribute of type ATTR, indexed via FACESET.
64
		If there is an existing attribute of the same name, then the
65
		handle passed as second argument is made to point to this
66
		attribute and no new attribute is created. */
67
		template<class ATTR>
68
		void register_vertex_attribute(const std::string& attr_name,
69
																	 AttrHandle<ATTR>& handle, int face_set=0)
70
		{
71
			assert(!did_call_get_trimesh);
72
			assert(face_set < face_sets.size());
73
			if(!get_vattr_handle(attr_name, handle))
74
				{
75
					handle.idx = vert_attr.size();
76
					vert_attr.push_back(new AttrVec<ATTR>(attr_name));
77
					face_set_mapping.push_back(face_set);
78
				}
79
		}
80
 
81
		// ----------------------------------------------------------------------
82
		// utility functions. These are used to register standard resources.
83
		// ----------------------------------------------------------------------
84
 
85
		/// Register vertex normals (of type Vec3f)
86
		void register_vertex_normals(int face_set = 0);
87
 
88
		/// Register vertex normals (of type Vec4f)
89
		void register_vertex_colors(int face_set = 0);
90
 
91
		/// Register vertex texture coords (of type Vec4f)
92
		void register_vertex_texcoords(int face_set = 0);
93
 
94
		/** Functions to add a face. When a face is added, the same face
95
				is added to all facesets. Afterwards, use set_face to alter a face
96
				in a given face_set. */
97
		void add_face(const CGLA::Vec3i& face);
98
 
99
		/// Add a vertex attribute
100
		template<class ATTR>
101
		int add_vattr(AttrHandle<ATTR> handle, const ATTR& val)
102
		{
103
			int n = vert_attr[handle.idx]->size();
104
			push_back_attr(*vert_attr[handle.idx],val);
105
			return n;
106
		}
107
 
108
		/// Add a face attribute
109
		template<class ATTR>
110
		int add_fattr(AttrHandle<ATTR> handle, const ATTR& val)
111
		{
112
			int n = face_attr[handle.idx]->size();
113
			push_back_attr(*face_attr[handle.idx],val);
114
			return n;
115
		}
116
 
117
		/// Add a face normal
118
		int add_fnorm(const CGLA::Vec3f& norm)
119
		{
120
			return add_fattr(FA_NORM, norm);
121
		}
122
 
123
		/// Add a face colour
124
		int add_fcol(const CGLA::Vec4f& col)
125
		{
126
			return add_fattr(FA_COL, col);
127
		}
128
 
129
		/// Add a vertex position to the mesh.
130
		int add_vpos(const CGLA::Vec3f& pos)
131
		{
132
			return add_vattr(VA_POS, pos);
133
		}
134
 
135
		/// Add a vertex normal to the mesh.
136
		int add_vnorm(const CGLA::Vec3f& norm)
137
		{
138
			return add_vattr(VA_NORM, norm);
139
		}
140
 
141
		/// Add a vertex colour to the mesh.
142
		int add_vcol(const CGLA::Vec4f& col)
143
		{
144
			return add_vattr(VA_COL, col);
145
		}
146
 
147
		/// Add a vertex texture coordinate to the mesh.
148
		int add_vtex(const CGLA::Vec4f& tex)
149
		{
150
			return add_vattr(VA_TEX, tex);
151
		}
152
 
153
		/** Build and return a resource ptr to the trimesh. After this function
154
				has been called, the destructor is the only function which it is
155
				allowed to call - unless the clear function is called.  */
156
		TriMesh* get_trimesh();
157
	};
158
 
159
	/** Register and compute face normals. if do_flip is true, the normals 
160
			point opposite the ccw direction. */
161
	void compute_face_normals(TriMeshBuilder& bldr, bool do_flip=false);
162
 
163
 
164
	/** This function computes vertex normals for a mesh. If no vertex normals
165
			have been registered, compute_vertex_normals will first register and
166
			add vertex normals. The normals are registered to the default face set
167
			meaning that there is a one to one relationship between vertices and
168
			vertex normals. If vertex_normals are already registered, it is assumed
169
			that the correct number of vertex normals have also been added.
170
 
171
			Vertex normals are then computed by adding the face normal weighted by
172
			angle to the normal corresponding to each vertex of the face. Finally,
173
			all vertex normals are normalized. */
174
	void compute_vertex_normals(TriMeshBuilder& bldr);
175
 
176
	/** This function is passed a builder and the handle of the smoothing group
177
			attribute. vertex_normals are registered to a new face set, and the 
178
			normal faces are computed using the smoothing group information. 
179
			Finally, compute_vertex_normals(bldr) is called to generated the actual
180
			normals. */
181
	void compute_vertex_normals(TriMeshBuilder& bldr, 
182
															AttrHandle<int> smooth_group_attr);
183
 
184
	/** This function is passed a builder and the cosine of a crease angle.
185
			vertex_normals are registered to a new face set, and the 
186
			normal faces are computed using the smoothing group information. 
187
			Finally, compute_vertex_normals(bldr) is called to generated the actual
188
			normals. */
189
	void compute_vertex_normals(TriMeshBuilder& bldr, float);
190
 
191
}
192
 
193
#endif