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