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