Subversion Repositories gelsvn

Rev

Rev 89 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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