Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
39 bj 1
#ifndef __IMESH_TRIMESH_H__
2
#define __IMESH_TRIMESH_H__
3
 
4
#include <iostream>
5
#include "AttrVec.h"
6
 
7
namespace IMesh
8
{
9
 
10
	/** The TriMeshData class represents the data contents of a TriMesh, and
11
			it also contains almost all of the functions used to set or get values.
12
			TriMeshData is never used directly, but TriMesh and TriMeshBuilder 
13
			inherit from this class. */
14
	class TriMeshData
15
	{
16
	private:
17
		// We do not allow assignment.
18
		TriMeshData& operator=(const TriMeshData&);
19
	protected:
20
 
21
		// The following attribute handles contain the attribute numbers
22
		// of the default face attributes face normals and face colours.
23
		// If these attributes are unused, the integers are -1
24
		AttrHandle<CGLA::Vec3f> FA_NORM;
25
		AttrHandle<CGLA::Vec4f> FA_COL;
26
 
27
		// The following handles contain the attribute numbers of the
28
		// default vertex attributes: vertex position, normal, colour, and
29
		// texture coordinates.
30
		AttrHandle<CGLA::Vec3f> VA_POS;
31
		AttrHandle<CGLA::Vec3f> VA_NORM; 
32
		AttrHandle<CGLA::Vec4f> VA_COL;
33
		AttrHandle<CGLA::Vec4f> VA_TEX;
34
 
35
		/// number of faces.
36
		int faces_size;
37
 
38
		/// vector of face sets.
39
		std::vector<FaceSet*> face_sets;
40
 
41
		/// vector of face attribute vectors
42
		std::vector<AttrVecIf*> face_attr;
43
 
44
		/// Mapping from vertex attribute to face set.
45
		std::vector<int> face_set_mapping;
46
 
47
		/// vector of vertex attribute vectors.
48
		std::vector<AttrVecIf*> vert_attr;
49
 
50
		/** Construct an empty instance of TriMesh data. This constructor is used
51
				by the builder when creating a fresh TriMesh. */
52
		TriMeshData();
53
 
54
		/** Copy constructor. This is called from the TriMesh constructor when
55
				the TriMeshBuilder creates a new TriMesh. */
56
		TriMeshData(const TriMeshData& tmd);
57
 
58
		/** Resets all variables to the values initialized by the constructor
59
				but it does not remove dynamic memory. */
60
		void clear();
61
 
62
		/** Remove all dynamice memory used. */
63
		void clean_up();
64
 
65
	public:
66
 
67
		//----------------------------------------------------------------------
68
		// Functions that find handles
69
		//----------------------------------------------------------------------
70
 
71
		/** Get the handle corresponding to a given vertex attribute vector */
72
		template<class ATTR>
73
		bool get_vattr_handle(const std::string& name, 
74
													AttrHandle<ATTR>& handle) const
75
		{
76
			for(int i=0;i< vert_attr.size(); ++i)
77
				{
78
					if (vert_attr[i]->get_name() == name)
79
						{
80
							handle.idx = i;
81
							return true;
82
						}
83
				}
84
			return false;
85
		}
86
 
87
		/** Get the handle corresponding to a given face attribute vector */
88
		template<class ATTR>
89
		bool get_fattr_handle(const std::string& name, 
90
													AttrHandle<ATTR>& handle) const
91
		{
92
			for(int i=0;i< face_attr.size(); ++i)
93
				{
94
					if (face_attr[i]->get_name() == name)
95
						{
96
							handle.idx = i;
97
							return true;
98
						}
99
				}
100
			return false;
101
		}
102
 
103
				// The following functions return the number of faces, vertices, and attributes.
104
 
105
		/// Returns the number of faces
106
		int no_faces() const 
107
		{
108
			return faces_size;
109
		}
110
 
111
		/// Return the number of vertex attributes associated with the attribute handles
112
		template<class ATTR>
113
		int no_vattrib(const AttrHandle<ATTR>& handle) const 
114
		{
115
			assert(handle.idx>=0);
116
			return vert_attr[handle.idx]->size();
117
		}
118
 
119
		/// Return the number of vertices.
120
		int no_vertices() const 
121
		{
122
			return no_vattrib(VA_POS);
123
		}
124
 
125
		/// Return the number of vertex normals.
126
		int no_vnorm() const
127
		{
128
			if(VA_NORM.idx >=0)
129
				return no_vattrib(VA_NORM);
130
			return 0;
131
		}
132
 
133
		/// Return the number of vertex colours
134
		int no_vcol() const
135
		{
136
			if(VA_COL.idx >=0)
137
				return no_vattrib(VA_COL);
138
			return 0;
139
		}
140
 
141
		/// Return the number of vertex texture coords
142
		int no_vtex() const
143
		{
144
			if(VA_TEX.idx >=0)
145
				return no_vattrib(VA_TEX);
146
			return 0;
147
		}
148
 
149
 
150
 
151
		//----------------------------------------------------------------------
152
		// Set functions
153
		//----------------------------------------------------------------------
154
 
155
		/** Set face. The arguments are the face set, the face index and a Vec3i
156
				containing the three indices. */
157
		CGLA::Vec3i& face(int fidx, int face_set=0)
158
		{
159
			assert(fidx < face_sets[face_set]->size());
160
			return (*face_sets[face_set])[fidx];
161
		}
162
 
163
		/** Template function used to set a face attribute. The template argument
164
				is the attribute type. The arguments are the atttribute number, 
165
				the face index, and the value. */
166
		template<class ATTR>
167
		ATTR& fattr(AttrHandle<ATTR> handle, int fidx)
168
		{
169
			return attr<ATTR>(*face_attr[handle.idx],fidx);
170
		}
171
 
172
		/** Template function used to set a vertex attribute. The template
173
				argument is the attribute type. The arguments are the attribute
174
				number, the index of a face, the corresponding corner, and the
175
				value of the attribute belonging to the thus specified vertex. */
176
		template<class ATTR>
177
		ATTR& vattr(AttrHandle<ATTR> handle, int fidx, int crnr)
178
		{
179
			int face_set = face_set_mapping[handle.idx];
180
			int vidx = (*face_sets[face_set])[fidx][crnr];
181
			return attr<ATTR>(*vert_attr[handle.idx],vidx);
182
		}
183
 
184
		/** Template function used to set a vertex attribute. The template
185
				argument is the type of the attribute, and the arugments are the
186
				attribute number,	the vertex index, and the value. */
187
		template<class ATTR>
188
		ATTR& vattr(AttrHandle<ATTR> handle, int vidx)
189
		{
190
			return attr<ATTR>(*vert_attr[handle.idx],vidx);
191
		}
192
 
193
		//----------------------------------------------------------------------
194
		// Get functions
195
		//----------------------------------------------------------------------
196
 
197
		/** Get a face. Returns a Vec3i with the face's corner indices.
198
				The arguments are the "face set" and the face index. */
199
		const CGLA::Vec3i face(int fidx,int face_set=0) const 
200
		{
201
			assert(fidx < face_sets[face_set]->size());
202
			return (*face_sets[face_set])[fidx];
203
		}
204
 
205
		/// Template function that gets face attribute.
206
		template<class ATTR>
207
		const ATTR& fattr(AttrHandle<ATTR> handle, int fidx) const
208
		{
209
			return attr<ATTR>(*face_attr[handle.idx],fidx);
210
		}
211
 
212
		/// Template function that gets vertex attribute from face index and corner
213
		template<class ATTR>
214
		const ATTR& vattr(AttrHandle<ATTR> handle, int fidx, int crnr) const
215
		{
216
			int face_set = face_set_mapping[handle.idx];
217
			int vidx = (*face_sets[face_set])[fidx][crnr];
218
			return attr<ATTR>(*vert_attr[handle.idx],vidx);
219
		}
220
 
221
		/// Get vertex attribute from vertex index
222
		template<class ATTR>
223
		const ATTR& vattr(AttrHandle<ATTR> handle, int vidx) const
224
		{
225
			return attr<ATTR>(*vert_attr[handle.idx],vidx);
226
		}
227
 
228
		// ----------------------------------------------------------------------
229
		// Utility functions for getting and setting the face normal
230
		// ----------------------------------------------------------------------
231
 
232
		/// Set a face normal
233
		const CGLA::Vec3f& fnorm(int fidx)
234
		{
235
			return fattr(FA_NORM, fidx);
236
		}
237
 
238
		/// Get a face normal
239
		const CGLA::Vec3f& fnorm(int fidx) const
240
		{
241
			return fattr(FA_NORM, fidx);
242
		}
243
 
244
		// ----------------------------------------------------------------------
245
		// Utility functions for getting and setting the face colour
246
		// ----------------------------------------------------------------------
247
 
248
		/// Set a face colour
249
		CGLA::Vec4f& fcol(int fidx)
250
		{
251
			return fattr(FA_COL, fidx);
252
		}
253
 
254
		/// Get a face colour
255
		const CGLA::Vec4f& fcol(int fidx) const
256
		{
257
			return fattr(FA_COL, fidx);
258
		}
259
 
260
		// ----------------------------------------------------------------------
261
		// Utility functions for getting and setting the vertex position
262
		// ----------------------------------------------------------------------
263
 
264
		/// Set a vertex position using a face index and corner.
265
		CGLA::Vec3f& vpos(int fidx, int crnr)
266
		{
267
			return vattr(VA_POS, fidx, crnr);
268
		}
269
 
270
		/// Set a vertex position using a direct vertex index
271
		CGLA::Vec3f& vpos(int vidx)
272
		{
273
			return vattr(VA_POS, vidx);
274
		}
275
 
276
		/// Get a vertex position using a face index and a corner
277
		const CGLA::Vec3f& vpos(int fidx, int crnr) const
278
		{
279
			return vattr(VA_POS, fidx, crnr);
280
		}
281
 
282
		/// Get a vertex position using a direct index
283
		const CGLA::Vec3f& vpos(int vidx) const
284
		{
285
			return vattr(VA_POS, vidx);
286
		}
287
 
288
		// ----------------------------------------------------------------------
289
		// Utility functions for getting and setting the vertex normal
290
		// ----------------------------------------------------------------------
291
 
292
		/// Set a vertex normal using a face index and corner.
293
		CGLA::Vec3f& vnorm(int fidx, int crnr)
294
		{
295
			return vattr(VA_NORM, fidx, crnr);
296
		}
297
 
298
		/// Set a vertex normal using a direct vertex index
299
		CGLA::Vec3f& vnorm(int vidx)
300
		{
301
			return vattr(VA_NORM, vidx);
302
		}
303
 
304
		/// Get a vertex normal using a face index and a corner
305
		const CGLA::Vec3f& vnorm(int fidx, int crnr) const 
306
		{
307
			return vattr(VA_NORM, fidx, crnr);
308
		}
309
 
310
		/// Get a vertex normal using a direct index
311
		const CGLA::Vec3f& vnorm(int vidx) const 
312
		{
313
			return vattr(VA_NORM, vidx);
314
		}
315
 
316
		// ----------------------------------------------------------------------
317
		// Utility functions for getting and setting the vertex colour
318
		// ----------------------------------------------------------------------
319
 
320
		/// Set a vertex colour using a face index and corner.
321
		CGLA::Vec4f& vcol(int fidx, int crnr)
322
		{
323
			return vattr(VA_COL, fidx, crnr);
324
		}
325
 
326
		/// Set a vertex colour using a direct vertex index
327
		CGLA::Vec4f& vcol(int vidx)
328
		{
329
			return vattr(VA_COL, vidx);
330
		}
331
 
332
		/// Get a vertex colour using a face index and a corner
333
		const CGLA::Vec4f& vcol(int fidx, int crnr) const
334
		{
335
			return vattr(VA_COL, fidx, crnr);
336
		}
337
 
338
		/// Get a vertex colour using a direct index
339
		const CGLA::Vec4f& vcol(int vidx) const
340
		{
341
			return vattr(VA_COL, vidx);
342
		}
343
 
344
		// ----------------------------------------------------------------------
345
		// Utility functions for getting and setting the vertex tex coord
346
		// ----------------------------------------------------------------------
347
 
348
		/// Set a vertex texture coord using a face index and corner.
349
		CGLA::Vec4f& vtex(int fidx, int crnr)
350
		{
351
			return vattr(VA_TEX, fidx, crnr);
352
		}
353
 
354
		/// Set a vertex texture coord using a direct vertex index
355
		CGLA::Vec4f& vtex(int vidx)
356
		{
357
			return vattr(VA_TEX, vidx);
358
		}
359
 
360
		/// Get a vertex texture coord using a face index and a corner
361
		const CGLA::Vec4f& vtex(int fidx, int crnr) const
362
		{
363
			return vattr(VA_TEX, fidx, crnr);
364
		}
365
 
366
		/// Get a vertex texture coord using a direct index
367
		const CGLA::Vec4f& vtex(int vidx) const
368
		{
369
			return vattr(VA_TEX, vidx);
370
		}
371
	};
372
 
373
 
374
	class TriMeshBuilder;
375
 
376
	/** The TriMesh class represents a triangle mesh. It cannot be instantiated
377
			except by the TriMeshBuilder class. All data and methods are defined
378
			in the TriMeshData class from which this class inherits. */
379
	class TriMesh: public TriMeshData
380
	{
381
		friend class TriMeshBuilder;
382
	private:
383
		// The following constructors are private and undefined. Hence
384
		// they cannot be used.
385
		TriMesh(const TriMesh&);
386
		TriMesh& operator=(const TriMesh&);
387
		TriMesh();
388
 
389
		// The only legal constructor takes a TriMeshData object as argument
390
		// This constructor can only be called by the TriMeshBuilder since it
391
		// us a friend of TriMesh.
392
 
393
		/// Construct a TriMesh from an instance of TriMeshData
394
		TriMesh(const TriMeshData& tmd): TriMeshData(tmd) {}
395
		void *data;
396
	public:
397
 
398
		/// Destructor deletes all attribute vectors. 
399
		~TriMesh()
400
		{
401
			clean_up();
402
		}
403
 
404
    void get_bsphere(CGLA::Vec3f& c, float& r) const;
405
 
406
    void get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const;
407
 
408
		void set_data(void *data) { 
409
			this->data = data;
410
		}
411
 
412
		void* get_data() {
413
			return data;
414
		}
415
	};
416
}
417
 
418
#endif