1 | #include "GeoMeshSimplifier.h" |
---|
2 | #include "GeoMeshSimpSequence.h" |
---|
3 | #include "SimplificationMethod.h" |
---|
4 | #include <iostream> |
---|
5 | #include <fstream> |
---|
6 | |
---|
7 | using namespace Geometry; |
---|
8 | using namespace std; |
---|
9 | |
---|
10 | //////////////////////////////////////////////////////////////////////////// |
---|
11 | // // |
---|
12 | // MeshSimplifier class // |
---|
13 | // // |
---|
14 | //////////////////////////////////////////////////////////////////////////// |
---|
15 | |
---|
16 | //--------------------------------------------------------------------------- |
---|
17 | // MeshSimplifier constructor. |
---|
18 | //--------------------------------------------------------------------------- |
---|
19 | MeshSimplifier::MeshSimplifier( const Mesh *m, |
---|
20 | TIPOFUNC upb) |
---|
21 | { |
---|
22 | // Saves initial state of mesh. |
---|
23 | mInitialMesh = new Mesh(); |
---|
24 | *mInitialMesh = *m; |
---|
25 | |
---|
26 | objmesh = m; |
---|
27 | mGeoMesh = NULL; |
---|
28 | msimpsequence = NULL; |
---|
29 | indexMeshLeaves = -1; |
---|
30 | |
---|
31 | // Sets the actual progress bar function. |
---|
32 | mUPB = upb; |
---|
33 | } |
---|
34 | |
---|
35 | //--------------------------------------------------------------------------- |
---|
36 | // MeshSimplifier destroyer. |
---|
37 | //--------------------------------------------------------------------------- |
---|
38 | MeshSimplifier::~MeshSimplifier() |
---|
39 | { |
---|
40 | delete mInitialMesh; |
---|
41 | delete msimpsequence; |
---|
42 | } |
---|
43 | |
---|
44 | //--------------------------------------------------------------------------- |
---|
45 | // Returns the simplified mesh. |
---|
46 | //--------------------------------------------------------------------------- |
---|
47 | Mesh * MeshSimplifier::GetMesh() |
---|
48 | { |
---|
49 | return mGeoMesh; |
---|
50 | } |
---|
51 | |
---|
52 | //--------------------------------------------------------------------------- |
---|
53 | // Set submesh leaves |
---|
54 | //--------------------------------------------------------------------------- |
---|
55 | void MeshSimplifier::setMeshLeaves(Index index) |
---|
56 | { |
---|
57 | indexMeshLeaves = index; |
---|
58 | } |
---|
59 | |
---|
60 | //--------------------------------------------------------------------------- |
---|
61 | // Returns the simplification sequence for general meshes. |
---|
62 | //--------------------------------------------------------------------------- |
---|
63 | MeshSimplificationSequence *MeshSimplifier::GetSimplificationSequence() |
---|
64 | { |
---|
65 | return msimpsequence; |
---|
66 | } |
---|
67 | |
---|
68 | //--------------------------------------------------------------------------- |
---|
69 | // Sort mesh bones. |
---|
70 | //--------------------------------------------------------------------------- |
---|
71 | void MeshSimplifier::sortBones() |
---|
72 | { |
---|
73 | VertexBuffer *mesh_vb; |
---|
74 | VertexBuffer *copy_vb; |
---|
75 | |
---|
76 | std::vector<VertexBoneAssignment> *mesh_bones; |
---|
77 | std::vector<VertexBoneAssignment> *copy_bones; |
---|
78 | |
---|
79 | // After simplifying, the object always is shared vertex |
---|
80 | // so, the bones must be placed in the GeoMesh |
---|
81 | mesh_bones = &mGeoMesh->mBones; |
---|
82 | mesh_vb = mGeoMesh->mVertexBuffer; |
---|
83 | |
---|
84 | // we assume the original mesh is shared vertex |
---|
85 | // because before the simplification process |
---|
86 | // the mesh becomes converted to shared vertex |
---|
87 | // so, the original bones must be searched in the |
---|
88 | // Mesh, not in every submesh |
---|
89 | |
---|
90 | mesh_bones->clear(); |
---|
91 | |
---|
92 | // Vertex buffers. |
---|
93 | copy_vb = mInitialMesh->mVertexBuffer; |
---|
94 | |
---|
95 | // Bones. |
---|
96 | copy_bones = &mInitialMesh->mBones; |
---|
97 | |
---|
98 | // If there are submesh bones. |
---|
99 | for (int b = 0; b < copy_bones->size(); b++) |
---|
100 | { |
---|
101 | VertexBoneAssignment assign; |
---|
102 | |
---|
103 | int n = (*copy_bones)[b].vertexIndex; |
---|
104 | |
---|
105 | // Initialize o. |
---|
106 | |
---|
107 | int o=0; |
---|
108 | for (o=0; o<mesh_vb->mVertexCount; o++) |
---|
109 | { |
---|
110 | if (mesh_vb->mPosition[o].x == copy_vb->mPosition[n].x && |
---|
111 | mesh_vb->mPosition[o].y == copy_vb->mPosition[n].y && |
---|
112 | mesh_vb->mPosition[o].z == copy_vb->mPosition[n].z) |
---|
113 | { |
---|
114 | assign.vertexIndex = o; |
---|
115 | assign.boneIndex = (*copy_bones)[b].boneIndex; |
---|
116 | assign.weight = 1.0f; |
---|
117 | mesh_bones->push_back(assign); |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | //////////////////////////////////////////////////////////////////////////// |
---|
124 | // // |
---|
125 | // GeometryBasedSimplifier class // |
---|
126 | // // |
---|
127 | //////////////////////////////////////////////////////////////////////////// |
---|
128 | |
---|
129 | //--------------------------------------------------------------------------- |
---|
130 | // Constructor. |
---|
131 | //--------------------------------------------------------------------------- |
---|
132 | GeometryBasedSimplifier::GeometryBasedSimplifier( const Mesh *m, |
---|
133 | TIPOFUNC upb) |
---|
134 | :MeshSimplifier(m,upb) |
---|
135 | { |
---|
136 | } |
---|
137 | |
---|
138 | //--------------------------------------------------------------------------- |
---|
139 | // Destroyer. |
---|
140 | //--------------------------------------------------------------------------- |
---|
141 | GeometryBasedSimplifier::~GeometryBasedSimplifier() |
---|
142 | { |
---|
143 | } |
---|
144 | |
---|
145 | //--------------------------------------------------------------------------- |
---|
146 | // Starts the simplification process. Receives as a parameter the LOD factor in a range of [0,1]. Implements the Simplifier::Simplify method to perform an image based simplification. |
---|
147 | //--------------------------------------------------------------------------- |
---|
148 | void GeometryBasedSimplifier::Simplify(Real paramlod) |
---|
149 | { |
---|
150 | SimplificationMethod *m_qslim = new SimplificationMethod(objmesh); |
---|
151 | |
---|
152 | m_qslim->setMeshLeaves(indexMeshLeaves); |
---|
153 | |
---|
154 | msimpsequence = m_qslim->Decimate(paramlod,0,mUPB); |
---|
155 | |
---|
156 | mGeoMesh = m_qslim->GetMesh(); |
---|
157 | |
---|
158 | // if the initial mesh had shared vertex, |
---|
159 | // convert the mesh to shared vertex |
---|
160 | for (int i=0; i<mInitialMesh->mSubMeshCount; i++) |
---|
161 | { |
---|
162 | if (mInitialMesh->mSubMesh[i].mSharedVertexBuffer) |
---|
163 | { |
---|
164 | Mesh *sharedMesh = mGeoMesh->toSharedVertex(); |
---|
165 | delete mGeoMesh; |
---|
166 | mGeoMesh = sharedMesh; |
---|
167 | break; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | // Sort bones. |
---|
172 | sortBones(); |
---|
173 | |
---|
174 | delete m_qslim; |
---|
175 | } |
---|
176 | |
---|
177 | //--------------------------------------------------------------------------- |
---|
178 | // Starts the simplification process. Receives as a parameter the number of vertices of the resulting mesh. Implements the Simplifier::Simplify method to perform an image based simplification. |
---|
179 | //--------------------------------------------------------------------------- |
---|
180 | void GeometryBasedSimplifier::Simplify(uint32 numvertices) |
---|
181 | { |
---|
182 | SimplificationMethod *m_qslim = new SimplificationMethod(objmesh); |
---|
183 | |
---|
184 | m_qslim->setMeshLeaves(indexMeshLeaves); |
---|
185 | |
---|
186 | msimpsequence = m_qslim->Decimate((float)numvertices,1,mUPB); |
---|
187 | |
---|
188 | mGeoMesh = m_qslim->GetMesh(); |
---|
189 | |
---|
190 | // if the initial mesh had shared vertex, |
---|
191 | // convert the mesh to shared vertex |
---|
192 | for (int i=0; i<mInitialMesh->mSubMeshCount; i++) |
---|
193 | { |
---|
194 | if (mInitialMesh->mSubMesh[i].mSharedVertexBuffer) |
---|
195 | { |
---|
196 | Mesh *sharedMesh = mGeoMesh->toSharedVertex(); |
---|
197 | delete mGeoMesh; |
---|
198 | mGeoMesh = sharedMesh; |
---|
199 | break; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | // Sort bones. |
---|
204 | sortBones(); |
---|
205 | |
---|
206 | delete m_qslim; |
---|
207 | } |
---|
208 | |
---|
209 | //////////////////////////////////////////////////////////////////////////// |
---|
210 | // // |
---|
211 | // ViewPointDrivenSimplifier class // |
---|
212 | // // |
---|
213 | //////////////////////////////////////////////////////////////////////////// |
---|
214 | |
---|
215 | //--------------------------------------------------------------------------- |
---|
216 | /// Class constructor. Will call Simplifier class constructor. |
---|
217 | //--------------------------------------------------------------------------- |
---|
218 | ViewPointDrivenSimplifier::ViewPointDrivenSimplifier( const Mesh *m, |
---|
219 | TIPOFUNC upb) |
---|
220 | :MeshSimplifier(m,upb) |
---|
221 | { |
---|
222 | // Set progress update function |
---|
223 | VMI::mUPB = upb; |
---|
224 | |
---|
225 | VMI::width = 256; |
---|
226 | VMI::height = 256; |
---|
227 | |
---|
228 | VMI::bEnableOffScreen = GL_TRUE; |
---|
229 | VMI::bBeQuiet = GL_FALSE; |
---|
230 | VMI::bSaveLog = GL_FALSE; |
---|
231 | VMI::bLoadCamerasFromFile = GL_FALSE; |
---|
232 | |
---|
233 | VMI::cameraType = 2; |
---|
234 | VMI::radius = 1.3; |
---|
235 | VMI::fov = 60.0; |
---|
236 | |
---|
237 | printf("w: %d h: %d\n", VMI::width, VMI::height); |
---|
238 | |
---|
239 | printf( "t: %d c: %d o: %d r: %f\n", |
---|
240 | VMI::numDemandedTriangles, |
---|
241 | VMI::cameraType, |
---|
242 | VMI::bEnableOffScreen, |
---|
243 | VMI::radius); |
---|
244 | |
---|
245 | |
---|
246 | mGeoMesh = new Mesh(); |
---|
247 | |
---|
248 | *mGeoMesh = *m; |
---|
249 | |
---|
250 | // Transform NoSV Mesh to a SV Mesh. |
---|
251 | mGeoMesh = mGeoMesh->toSharedVertex(); |
---|
252 | |
---|
253 | // Loads the vmi mesh structure for a geometry mesh given. |
---|
254 | VMI::mesh = initMeshStructure(mGeoMesh); |
---|
255 | |
---|
256 | // RGB and Alpha. |
---|
257 | glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA); |
---|
258 | glutInitWindowSize(VMI::width, VMI::height); |
---|
259 | glutInitWindowPosition(100, 100); |
---|
260 | |
---|
261 | VMI::vmiWin = glutCreateWindow("VMI"); |
---|
262 | |
---|
263 | glewInit(); |
---|
264 | |
---|
265 | VMI::init(); |
---|
266 | |
---|
267 | if (VMI::bLoadCamerasFromFile == GL_FALSE) |
---|
268 | { |
---|
269 | switch (VMI::cameraType) |
---|
270 | { |
---|
271 | case 0: |
---|
272 | VMI::cameras = VMI::setCameras(VMI::radius, OCTAHEDRON, &VMI::numCameras); |
---|
273 | printf("Number of cameras: %d\n", OCTAHEDRON); |
---|
274 | break; |
---|
275 | case 1: |
---|
276 | VMI:: cameras = VMI::setCameras(VMI::radius, ICOSAHEDRON, &VMI::numCameras); |
---|
277 | printf("Number of cameras: %d\n", ICOSAHEDRON); |
---|
278 | break; |
---|
279 | case 2: |
---|
280 | VMI::cameras = VMI::setCameras(VMI::radius, DODECAHEDRON, &VMI::numCameras); |
---|
281 | printf("Number of cameras: %d\n", DODECAHEDRON); |
---|
282 | break; |
---|
283 | default: |
---|
284 | break; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | VMI::histogram = VMI::initHistogram( VMI::mesh->currentNumTriangles, |
---|
289 | VMI::numCameras); |
---|
290 | |
---|
291 | VMI::initialIs = VMI::initIs(VMI::numCameras); |
---|
292 | } |
---|
293 | |
---|
294 | //--------------------------------------------------------------------------- |
---|
295 | /// Class destructor. |
---|
296 | //--------------------------------------------------------------------------- |
---|
297 | ViewPointDrivenSimplifier::~ViewPointDrivenSimplifier(void) |
---|
298 | { |
---|
299 | // Free memory |
---|
300 | VMI::freeMemory(); |
---|
301 | } |
---|
302 | |
---|
303 | /// Copy constructor |
---|
304 | //ViewPointDrivenSimplifier(const ViewPointDrivenSimplifier&); |
---|
305 | |
---|
306 | /// Assignment operator |
---|
307 | //ViewPointDrivenSimplifier& operator =(const ViewPointDrivenSimplifier&); |
---|
308 | |
---|
309 | /// Starts the simplification process. Receives as a parameter the LOD factor in a range of [0,1]. Implements the Simplifier::Simplify method to perform an image based simplification. |
---|
310 | void ViewPointDrivenSimplifier::Simplify(Real percent) |
---|
311 | { |
---|
312 | VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * percent); |
---|
313 | |
---|
314 | if ((VMI::numDemandedTriangles == 0) |
---|
315 | || |
---|
316 | (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles)) |
---|
317 | { |
---|
318 | printf("Illegal number of triangles.\n"); |
---|
319 | } |
---|
320 | |
---|
321 | VMI::display(); |
---|
322 | |
---|
323 | // Load a geometry mesh for vmi mesh. |
---|
324 | loadMesh(); |
---|
325 | |
---|
326 | // Sort bones. |
---|
327 | sortBones(); |
---|
328 | |
---|
329 | GetMeshSimpSequence(); |
---|
330 | } |
---|
331 | |
---|
332 | //--------------------------------------------------------------------------- |
---|
333 | /// Starts the simplification process. Receives as a parameter the number of vertices of the resulting mesh. Implements the Simplifier::Simplify method to perform an image based simplification. |
---|
334 | //--------------------------------------------------------------------------- |
---|
335 | void ViewPointDrivenSimplifier::Simplify(uint32 numVertices) |
---|
336 | { |
---|
337 | float percent; |
---|
338 | |
---|
339 | percent = (numVertices * 100.0) / VMI::mesh->numVertices; |
---|
340 | |
---|
341 | VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * (percent / 100)); |
---|
342 | |
---|
343 | if ((VMI::numDemandedTriangles == 0) |
---|
344 | || |
---|
345 | (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles)) |
---|
346 | { |
---|
347 | printf("Illegal number of triangles.\n"); |
---|
348 | } |
---|
349 | |
---|
350 | VMI::display(); |
---|
351 | |
---|
352 | // Load a geometry mesh for vmi mesh. |
---|
353 | loadMesh(); |
---|
354 | |
---|
355 | // Sort bones. |
---|
356 | sortBones(); |
---|
357 | |
---|
358 | GetMeshSimpSequence(); |
---|
359 | } |
---|
360 | |
---|
361 | // Returns the simplified mesh. |
---|
362 | //Mesh * ViewPointDrivenSimplifier::GetMesh() |
---|
363 | //{ |
---|
364 | // return mGeoMesh; |
---|
365 | //} |
---|
366 | |
---|
367 | //--------------------------------------------------------------------------- |
---|
368 | // Gets the VMI mesh simplification sequence. |
---|
369 | //--------------------------------------------------------------------------- |
---|
370 | void ViewPointDrivenSimplifier::GetMeshSimpSequence() |
---|
371 | { |
---|
372 | unsigned int j = 0; |
---|
373 | |
---|
374 | MeshSimplificationSequence::Step current_step; |
---|
375 | |
---|
376 | msimpsequence = new MeshSimplificationSequence(); |
---|
377 | |
---|
378 | // For each simplification step. |
---|
379 | for (unsigned int i = 0; i < VMI::mVMISteps.size(); i++) |
---|
380 | { |
---|
381 | current_step.mV0 = VMI::mVMISteps[i].mV0; |
---|
382 | current_step.mV1 = VMI::mVMISteps[i].mV1; |
---|
383 | current_step.mT0 = VMI::mVMISteps[i].mT0; |
---|
384 | current_step.mT1 = VMI::mVMISteps[i].mT1; |
---|
385 | |
---|
386 | current_step.x = VMI::mVMISteps[i].x; |
---|
387 | current_step.y = VMI::mVMISteps[i].y; |
---|
388 | current_step.z = VMI::mVMISteps[i].z; |
---|
389 | |
---|
390 | current_step.obligatory = VMI::mVMISteps[i].obligatory; |
---|
391 | |
---|
392 | // For each face modificated. |
---|
393 | while (j < VMI::mVMISteps[i].mModfaces.size()) |
---|
394 | { |
---|
395 | current_step.mModfaces.push_back(VMI::mVMISteps[i].mModfaces[j]); |
---|
396 | |
---|
397 | j++; |
---|
398 | } |
---|
399 | |
---|
400 | msimpsequence->mSteps.push_back(current_step); |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | //--------------------------------------------------------------------------- |
---|
405 | // Class for join triangle holes. |
---|
406 | //--------------------------------------------------------------------------- |
---|
407 | class _coord_ |
---|
408 | { |
---|
409 | public: |
---|
410 | float x,y,z; |
---|
411 | inline _coord_( float x = 0.0f, |
---|
412 | float y = 0.0f, |
---|
413 | float z = 0.0f) |
---|
414 | { this->x = x; this->y = y; this->z = z; } |
---|
415 | |
---|
416 | inline _coord_(const _coord_ &f) |
---|
417 | { |
---|
418 | x = f.x; |
---|
419 | y=f.y; |
---|
420 | z=f.z; |
---|
421 | } |
---|
422 | |
---|
423 | inline _coord_ & operator=(const _coord_ &f) |
---|
424 | { |
---|
425 | x = f.x; |
---|
426 | y = f.y; |
---|
427 | z = f.z; |
---|
428 | |
---|
429 | return *this; |
---|
430 | } |
---|
431 | |
---|
432 | inline bool operator<(const _coord_ &f) const |
---|
433 | { |
---|
434 | if (x<f.x-0.0001) return true; |
---|
435 | if (x>f.x+0.0001) return false; |
---|
436 | if (y<f.y-0.0001) return true; |
---|
437 | if (y>f.y+0.0001) return false; |
---|
438 | if (z<f.z-0.0001) return true; |
---|
439 | if (z>f.z+0.0001) return false; |
---|
440 | |
---|
441 | return false; |
---|
442 | } |
---|
443 | }; |
---|
444 | |
---|
445 | //--------------------------------------------------------------------------- |
---|
446 | // Initialize the mesh of VMI module. |
---|
447 | //--------------------------------------------------------------------------- |
---|
448 | VMI::Mesh * ViewPointDrivenSimplifier::initMeshStructure(Mesh *geoMesh) |
---|
449 | { |
---|
450 | GLuint i, j, v1, v2, v3, n, e, t; |
---|
451 | VMI::Mesh *vmi_mesh; |
---|
452 | SubMesh *geosubmesh; |
---|
453 | VertexBuffer *vertex_buffer; |
---|
454 | int mesh_index_count; |
---|
455 | int index; |
---|
456 | |
---|
457 | // Reallocate memory for vmi mesh object. |
---|
458 | vmi_mesh = (VMI::Mesh *)malloc(sizeof(VMI::Mesh)); |
---|
459 | |
---|
460 | if (vmi_mesh == NULL) |
---|
461 | { |
---|
462 | fprintf(stderr, "Error allocating memory\n"); |
---|
463 | exit(1); |
---|
464 | } |
---|
465 | |
---|
466 | // Shared vertex buffer. |
---|
467 | vertex_buffer = geoMesh->mVertexBuffer; |
---|
468 | |
---|
469 | // Reallocate memory for vertices of mesh. |
---|
470 | vmi_mesh->vertices = (VMI::Vertex *)malloc(sizeof(VMI::Vertex) |
---|
471 | * |
---|
472 | vertex_buffer->mVertexCount); |
---|
473 | |
---|
474 | if (vmi_mesh->vertices == NULL) |
---|
475 | { |
---|
476 | fprintf(stderr, "Error allocating memory\n"); |
---|
477 | exit(1); |
---|
478 | } |
---|
479 | |
---|
480 | // Initialize mesh index count. |
---|
481 | mesh_index_count = 0; |
---|
482 | |
---|
483 | for (unsigned int submesh = 0; submesh < mGeoMesh->mSubMeshCount; submesh++) |
---|
484 | { |
---|
485 | mesh_index_count += int(mGeoMesh->mSubMesh[submesh].mIndexCount); |
---|
486 | } |
---|
487 | |
---|
488 | // Reallocate memory for indices. |
---|
489 | vmi_mesh->triangles = (VMI::Triangle *)malloc(sizeof(VMI::Triangle) |
---|
490 | * |
---|
491 | (mesh_index_count / 3)); |
---|
492 | |
---|
493 | if (vmi_mesh->triangles == NULL) |
---|
494 | { |
---|
495 | fprintf(stderr, "Error allocating memory\n"); |
---|
496 | exit(1); |
---|
497 | } |
---|
498 | |
---|
499 | printf("Adding vertices..."); |
---|
500 | // Fill up vertices. |
---|
501 | std::map<_coord_, int> uniquevertices; |
---|
502 | std::map<int, int> newindices; |
---|
503 | for (i = 0; i < vertex_buffer->mVertexCount; i++) |
---|
504 | { |
---|
505 | _coord_ vertex_aux; |
---|
506 | vertex_aux.x= vertex_buffer->mPosition[i].x; |
---|
507 | vertex_aux.y= vertex_buffer->mPosition[i].y; |
---|
508 | vertex_aux.z= vertex_buffer->mPosition[i].z; |
---|
509 | //New index |
---|
510 | if (uniquevertices.find(vertex_aux)==uniquevertices.end()) |
---|
511 | { |
---|
512 | uniquevertices[vertex_aux]= i; |
---|
513 | newindices[i]= i; |
---|
514 | |
---|
515 | VMI::INTVECTOR intvectoraux; |
---|
516 | intvectoraux.push_back(i); |
---|
517 | VMI::inversemap[i]= intvectoraux; |
---|
518 | |
---|
519 | }else//The map of unique vertices already contains this vertex |
---|
520 | { |
---|
521 | int newindex= uniquevertices[vertex_aux]; |
---|
522 | newindices[i]= newindex; |
---|
523 | |
---|
524 | VMI::inversemap[newindex].push_back(i);; |
---|
525 | } |
---|
526 | |
---|
527 | // Vertices start at 0. |
---|
528 | vmi_mesh->vertices[i].x = vertex_buffer->mPosition[i].x; |
---|
529 | vmi_mesh->vertices[i].y = vertex_buffer->mPosition[i].y; |
---|
530 | vmi_mesh->vertices[i].z = vertex_buffer->mPosition[i].z; |
---|
531 | |
---|
532 | vmi_mesh->vertices[i].numTriangles = 0; |
---|
533 | vmi_mesh->vertices[i].triangles = NULL; |
---|
534 | vmi_mesh->vertices[i].enable = GL_TRUE; |
---|
535 | } |
---|
536 | |
---|
537 | printf("Ok\n"); |
---|
538 | vmi_mesh->numVertices = int(vertex_buffer->mVertexCount); |
---|
539 | vmi_mesh->currentNumVertices = int(vertex_buffer->mVertexCount); |
---|
540 | |
---|
541 | // Fill up triangles. |
---|
542 | printf("Adding triangles..."); |
---|
543 | |
---|
544 | // Initialize index of triangle. |
---|
545 | index = 0; |
---|
546 | |
---|
547 | // For each submesh. |
---|
548 | for (unsigned int submesh = 0; submesh < mGeoMesh->mSubMeshCount; submesh++) |
---|
549 | { |
---|
550 | // Gets actual submesh. |
---|
551 | geosubmesh = &mGeoMesh->mSubMesh[submesh]; |
---|
552 | |
---|
553 | for (i = 0; i < (geosubmesh->mIndexCount / 3); i++) |
---|
554 | { |
---|
555 | vmi_mesh->triangles[index].id = index; |
---|
556 | vmi_mesh->triangles[index].submesh = submesh; |
---|
557 | vmi_mesh->triangles[index].indices[0] = newindices[geosubmesh->mIndex[(3 * i)]]; |
---|
558 | vmi_mesh->triangles[index].indices[1] = newindices[geosubmesh->mIndex[(3 * i) + 1]]; |
---|
559 | vmi_mesh->triangles[index].indices[2] = newindices[geosubmesh->mIndex[(3 * i) + 2]]; |
---|
560 | |
---|
561 | vmi_mesh->triangles[index].area = computeTriangleArea(vmi_mesh->vertices, |
---|
562 | &vmi_mesh->triangles[index]); |
---|
563 | //printf("\n%d a: %f",index , vmi_mesh->triangles[index].area); |
---|
564 | computeTriangleNormal(vmi_mesh->vertices, &vmi_mesh->triangles[index]); |
---|
565 | |
---|
566 | vmi_mesh->triangles[index].saliency = 0.0; |
---|
567 | |
---|
568 | vmi_mesh->triangles[index].enable = GL_TRUE; |
---|
569 | |
---|
570 | for (j = 0; j < 3; j++) |
---|
571 | { |
---|
572 | // Adding triangle index adjacent to 3 vertices. |
---|
573 | v1 = vmi_mesh->triangles[index].indices[j]; |
---|
574 | |
---|
575 | // Reallocate memory for the new adjacent triangle. |
---|
576 | vmi_mesh->vertices[v1].triangles = (GLuint *)realloc(vmi_mesh->vertices[v1].triangles, (vmi_mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); |
---|
577 | VMI::addItem((int *)vmi_mesh->vertices[v1].triangles, (int *)&vmi_mesh->vertices[v1].numTriangles, index); |
---|
578 | } |
---|
579 | |
---|
580 | // Increments triangle count. |
---|
581 | index++; |
---|
582 | } |
---|
583 | } |
---|
584 | |
---|
585 | printf("Ok\n"); |
---|
586 | |
---|
587 | vmi_mesh->numTriangles = mesh_index_count / 3; |
---|
588 | vmi_mesh->currentNumTriangles = mesh_index_count / 3; |
---|
589 | |
---|
590 | printf("Num Triangles: %d\n",vmi_mesh->numTriangles); |
---|
591 | |
---|
592 | // E = 3 T / 2 |
---|
593 | vmi_mesh->edges = (VMI::Edge *)malloc(sizeof(VMI::Edge) |
---|
594 | * |
---|
595 | vmi_mesh->numTriangles * 3); |
---|
596 | |
---|
597 | if (vmi_mesh->edges == NULL) |
---|
598 | { |
---|
599 | fprintf(stderr, "Error allocating memory\n"); |
---|
600 | exit(1); |
---|
601 | } |
---|
602 | |
---|
603 | // Init edges |
---|
604 | for (i=0; i<vmi_mesh->numTriangles * 3; i++) |
---|
605 | { |
---|
606 | vmi_mesh->edges[i].triangles = NULL; |
---|
607 | vmi_mesh->edges[i].numTriangles = 0; |
---|
608 | } |
---|
609 | |
---|
610 | printf("Adding edges..."); |
---|
611 | n = 0; |
---|
612 | |
---|
613 | for (i=0; i<vmi_mesh->numTriangles; i++) |
---|
614 | { |
---|
615 | t = 0; |
---|
616 | v1 = vmi_mesh->triangles[i].indices[0]; |
---|
617 | v2 = vmi_mesh->triangles[i].indices[1]; |
---|
618 | v3 = vmi_mesh->triangles[i].indices[2]; |
---|
619 | |
---|
620 | if ((e = findEdge(vmi_mesh->edges, n, v1, v2)) == -1) |
---|
621 | { |
---|
622 | vmi_mesh->edges[n].u = v1; |
---|
623 | vmi_mesh->edges[n].v = v2; |
---|
624 | vmi_mesh->edges[n].enable = GL_TRUE; |
---|
625 | |
---|
626 | // Reallocate memory for the new adjacent triangle |
---|
627 | vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); |
---|
628 | // Adding triangle i adjacent to edge n |
---|
629 | VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); |
---|
630 | //printf("n:%d i:%d\n", n, i); |
---|
631 | |
---|
632 | // Adding edge n adjacent to triangle i |
---|
633 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); |
---|
634 | n++; |
---|
635 | } |
---|
636 | else |
---|
637 | { |
---|
638 | // Reallocate memory for the new adjacent triangle. |
---|
639 | vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); |
---|
640 | // Adding triangle i adjacent to edge e |
---|
641 | VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); |
---|
642 | //printf("n:%d i:%d\n", e, i); |
---|
643 | |
---|
644 | // Adding edge e adjacent to triangle i |
---|
645 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); |
---|
646 | } |
---|
647 | if ((e = findEdge(vmi_mesh->edges, n, v2, v3)) == -1) |
---|
648 | { |
---|
649 | vmi_mesh->edges[n].u = v2; |
---|
650 | vmi_mesh->edges[n].v = v3; |
---|
651 | vmi_mesh->edges[n].enable = GL_TRUE; |
---|
652 | |
---|
653 | // Reallocate memory for the new adjacent triangle |
---|
654 | vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); |
---|
655 | // Adding triangle i adjacent to edge n |
---|
656 | VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); |
---|
657 | //printf("n:%d i:%d\n", n, i); |
---|
658 | |
---|
659 | // Adding edge n adjacent to triangle i |
---|
660 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); |
---|
661 | n++; |
---|
662 | } |
---|
663 | else |
---|
664 | { |
---|
665 | // Reallocate memory for the new adjacent triangle |
---|
666 | vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); |
---|
667 | // Adding triangle i adjacent to edge e |
---|
668 | VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); |
---|
669 | //printf("n:%d i:%d\n", e, i); |
---|
670 | |
---|
671 | // Adding edge e adjacent to triangle i |
---|
672 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); |
---|
673 | } |
---|
674 | |
---|
675 | if ((e = findEdge(vmi_mesh->edges, n, v3, v1)) == -1) |
---|
676 | { |
---|
677 | vmi_mesh->edges[n].u = v3; |
---|
678 | vmi_mesh->edges[n].v = v1; |
---|
679 | vmi_mesh->edges[n].enable = GL_TRUE; |
---|
680 | |
---|
681 | // Reallocate memory for the new adjacent triangle |
---|
682 | vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); |
---|
683 | // Adding triangle i adjacent to edge n |
---|
684 | VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); |
---|
685 | //printf("n:%d i:%d\n", n, i); |
---|
686 | |
---|
687 | // Adding edge n adjacent to triangle i |
---|
688 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); |
---|
689 | n++; |
---|
690 | } |
---|
691 | else |
---|
692 | { |
---|
693 | // Reallocate memory for the new adjacent triangle |
---|
694 | vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); |
---|
695 | // Adding triangle i adjacent to edge e |
---|
696 | VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); |
---|
697 | //printf("n:%d i:%d\n", e, i); |
---|
698 | |
---|
699 | // Adding edge e adjacent to triangle i |
---|
700 | VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); |
---|
701 | } |
---|
702 | } |
---|
703 | |
---|
704 | printf("Ok\n"); |
---|
705 | vmi_mesh->numEdges = n; |
---|
706 | |
---|
707 | return vmi_mesh; |
---|
708 | } |
---|
709 | |
---|
710 | //--------------------------------------------------------------------------- |
---|
711 | // Gets the geometry mesh of a the vmi mesh given. |
---|
712 | //--------------------------------------------------------------------------- |
---|
713 | void ViewPointDrivenSimplifier::loadMesh() |
---|
714 | { |
---|
715 | int num_indices; |
---|
716 | SubMesh *geosubmesh; |
---|
717 | VertexBuffer *vertex_buffer; |
---|
718 | |
---|
719 | // Gets old vertex buffer. |
---|
720 | vertex_buffer = mGeoMesh->mVertexBuffer; |
---|
721 | |
---|
722 | // Initialize auxiliar vertex buffer. |
---|
723 | mVB = new VertexBuffer(); |
---|
724 | |
---|
725 | mVB->mPosition = new Vector3[VMI::mesh->currentNumVertices]; |
---|
726 | mVB->mNormal = new Vector3[VMI::mesh->currentNumVertices]; |
---|
727 | mVB->mTexCoords = new Vector2[VMI::mesh->currentNumVertices]; |
---|
728 | |
---|
729 | mVB->mVertexInfo = vertex_buffer->mVertexInfo; |
---|
730 | |
---|
731 | // For each submesh. |
---|
732 | for (unsigned int submesh = 0; submesh < mGeoMesh->mSubMeshCount; submesh++) |
---|
733 | { |
---|
734 | geosubmesh = &mGeoMesh->mSubMesh[submesh]; |
---|
735 | |
---|
736 | delete []geosubmesh->mIndex; |
---|
737 | |
---|
738 | // Initialize submesh index count; |
---|
739 | num_indices = 0; |
---|
740 | |
---|
741 | // For each triangle. |
---|
742 | for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++) |
---|
743 | { |
---|
744 | // If is enable and of the current submesh. |
---|
745 | if ((VMI::mesh->triangles[i].enable) |
---|
746 | && |
---|
747 | (VMI::mesh->triangles[i].submesh == submesh)) |
---|
748 | { |
---|
749 | // Increments submesh index count. |
---|
750 | num_indices += 3; |
---|
751 | } |
---|
752 | } |
---|
753 | |
---|
754 | geosubmesh->mIndexCount = num_indices; |
---|
755 | |
---|
756 | geosubmesh->mIndex = new Index[geosubmesh->mIndexCount]; |
---|
757 | |
---|
758 | // Initialize number of indices. |
---|
759 | num_indices = 0; |
---|
760 | |
---|
761 | // Fill up indices. |
---|
762 | for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++) |
---|
763 | { |
---|
764 | if ((VMI::mesh->triangles[i].enable) |
---|
765 | && |
---|
766 | (VMI::mesh->triangles[i].submesh == submesh)) |
---|
767 | { |
---|
768 | geosubmesh->mIndex[num_indices++] = VMI::mesh->triangles[i].indices[0]; |
---|
769 | geosubmesh->mIndex[num_indices++] = VMI::mesh->triangles[i].indices[1]; |
---|
770 | geosubmesh->mIndex[num_indices++] = VMI::mesh->triangles[i].indices[2]; |
---|
771 | } |
---|
772 | } |
---|
773 | |
---|
774 | // For each index. |
---|
775 | for (unsigned int i = 0; i < geosubmesh->mIndexCount; i++) |
---|
776 | { |
---|
777 | findVertex(submesh,i); |
---|
778 | } |
---|
779 | |
---|
780 | geosubmesh->mVertexBuffer = mVB; |
---|
781 | } |
---|
782 | |
---|
783 | delete vertex_buffer; |
---|
784 | |
---|
785 | mGeoMesh->mVertexBuffer = mVB; |
---|
786 | |
---|
787 | } |
---|
788 | |
---|
789 | //--------------------------------------------------------------------------- |
---|
790 | // Find vertex in auxiliar vertex buffer. |
---|
791 | //--------------------------------------------------------------------------- |
---|
792 | void ViewPointDrivenSimplifier::findVertex(size_t submesh, size_t elem) |
---|
793 | { |
---|
794 | bool found; |
---|
795 | int index; |
---|
796 | unsigned int i; |
---|
797 | int new_elem; |
---|
798 | VertexBuffer *vertex_buffer; |
---|
799 | |
---|
800 | found = false; |
---|
801 | |
---|
802 | // Shared vertex buffer. |
---|
803 | vertex_buffer = mGeoMesh->mVertexBuffer; |
---|
804 | |
---|
805 | index = mGeoMesh->mSubMesh[submesh].mIndex[elem]; |
---|
806 | |
---|
807 | i = 0; |
---|
808 | |
---|
809 | while (!found && (i < mVB->mVertexCount)) |
---|
810 | { |
---|
811 | if ((VMI::mesh->vertices[index].x == mVB->mPosition[i].x) |
---|
812 | && |
---|
813 | (VMI::mesh->vertices[index].y == mVB->mPosition[i].y) |
---|
814 | && |
---|
815 | (VMI::mesh->vertices[index].z == mVB->mPosition[i].z)) |
---|
816 | { |
---|
817 | found = true; |
---|
818 | |
---|
819 | // Update index. |
---|
820 | mGeoMesh->mSubMesh[submesh].mIndex[elem] = i; |
---|
821 | } |
---|
822 | |
---|
823 | // Increments index. |
---|
824 | i++; |
---|
825 | } |
---|
826 | |
---|
827 | if (!found) |
---|
828 | { |
---|
829 | // Last element. |
---|
830 | new_elem = int(mVB->mVertexCount); |
---|
831 | |
---|
832 | // Add to last. |
---|
833 | mVB->mPosition[new_elem].x = VMI::mesh->vertices[index].x; |
---|
834 | mVB->mPosition[new_elem].y = VMI::mesh->vertices[index].y; |
---|
835 | mVB->mPosition[new_elem].z = VMI::mesh->vertices[index].z; |
---|
836 | |
---|
837 | mVB->mNormal[new_elem].x = vertex_buffer->mNormal[index].x; |
---|
838 | mVB->mNormal[new_elem].y = vertex_buffer->mNormal[index].y; |
---|
839 | mVB->mNormal[new_elem].z = vertex_buffer->mNormal[index].z; |
---|
840 | |
---|
841 | mVB->mTexCoords[new_elem].x = vertex_buffer->mTexCoords[index].x; |
---|
842 | mVB->mTexCoords[new_elem].y = vertex_buffer->mTexCoords[index].y; |
---|
843 | |
---|
844 | // Update index. |
---|
845 | mGeoMesh->mSubMesh[submesh].mIndex[elem] = new_elem; |
---|
846 | |
---|
847 | // Increments vertex count. |
---|
848 | mVB->mVertexCount++; |
---|
849 | } |
---|
850 | } |
---|
851 | |
---|