1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <memory.h> |
---|
4 | #include <string.h> |
---|
5 | #include <math.h> |
---|
6 | |
---|
7 | #include "../include/mesh.h" |
---|
8 | #include "../include/area.h" |
---|
9 | #include "../include/global.h" |
---|
10 | |
---|
11 | using namespace VMI; |
---|
12 | |
---|
13 | #define OUTPUT_SEVERAL_GROUPS |
---|
14 | |
---|
15 | /////////////////////////////////////////////////////////////////////////////// |
---|
16 | // list of integers |
---|
17 | void VMI::printItemList(int *list, int n) { |
---|
18 | int i; |
---|
19 | |
---|
20 | for (i=0; i<n; i++) |
---|
21 | printf("%d ",list[i]); |
---|
22 | |
---|
23 | printf("\n"); |
---|
24 | } |
---|
25 | |
---|
26 | int VMI::findItem(int *list, int n, int item) { |
---|
27 | int i; |
---|
28 | |
---|
29 | for (i=0; i<n; i++) |
---|
30 | if (list[i] == item) return TRUE; |
---|
31 | return FALSE; |
---|
32 | } |
---|
33 | |
---|
34 | void VMI::addItem(int *list, int *n, int item) { |
---|
35 | |
---|
36 | if (!findItem(list, *n, item)) |
---|
37 | list[(*n)++] = item; |
---|
38 | } |
---|
39 | |
---|
40 | void VMI::delItem(int *list, int *n, int item) { |
---|
41 | int i, j; |
---|
42 | |
---|
43 | for (i=0; i<*n; i++) { |
---|
44 | if (list[i] == item) { |
---|
45 | // delete it |
---|
46 | for (j =i + 1 ; j<*n; j++) |
---|
47 | list[j - 1] = list[j]; |
---|
48 | (*n)--; |
---|
49 | i--; // Delete all ocurrencies of an item |
---|
50 | } |
---|
51 | } |
---|
52 | } |
---|
53 | /////////////////////////////////////////////////////////////////////////////// |
---|
54 | |
---|
55 | int VMI::findEdge(Edge *e, GLuint num, GLuint _u, GLuint _v) { |
---|
56 | GLuint i, u, v; |
---|
57 | int found = -1; |
---|
58 | |
---|
59 | for (i=0; i<num; i++) { |
---|
60 | |
---|
61 | if (e[i].enable == TRUE) { |
---|
62 | |
---|
63 | u = e[i].u; |
---|
64 | v = e[i].v; |
---|
65 | |
---|
66 | if (((u == _u) && (v == _v)) || |
---|
67 | ((u == _v) && (v == _u))) { |
---|
68 | found = i; |
---|
69 | break; |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | return found; |
---|
75 | } |
---|
76 | |
---|
77 | // Adds a new edge at the end of the mesh edge list |
---|
78 | Edge *VMI::addEdge(Edge *list, int *n, int u, int v, int *pos) { |
---|
79 | Edge *newList; |
---|
80 | |
---|
81 | // Reallocate memory for the new edge |
---|
82 | newList = (Edge *)realloc(list, ((*n) + 1) * sizeof(Edge)); |
---|
83 | |
---|
84 | // New vertex indices |
---|
85 | newList[*n].u = u; |
---|
86 | newList[*n].v = v; |
---|
87 | |
---|
88 | // This edge has no triangle adjancency |
---|
89 | newList[*n].numTriangles = 0; |
---|
90 | newList[*n].triangles = NULL; |
---|
91 | |
---|
92 | // Enable the new edge |
---|
93 | newList[*n].enable = TRUE; |
---|
94 | |
---|
95 | // New edge position |
---|
96 | *pos = *n; |
---|
97 | |
---|
98 | // New number of edges |
---|
99 | (*n)++; |
---|
100 | |
---|
101 | return newList; |
---|
102 | } |
---|
103 | |
---|
104 | GLdouble VMI::computeTriangleArea(Vertex *vertices, Triangle *t) { |
---|
105 | GLuint i; |
---|
106 | GLdouble v0[3], v1[3], v2[3], a; |
---|
107 | |
---|
108 | // Compute triangle area |
---|
109 | i = t->indices[0]; |
---|
110 | v0[0] = vertices[i].x; |
---|
111 | v0[1] = vertices[i].y; |
---|
112 | v0[2] = vertices[i].z; |
---|
113 | |
---|
114 | i = t->indices[1]; |
---|
115 | v1[0] = vertices[i].x; |
---|
116 | v1[1] = vertices[i].y; |
---|
117 | v1[2] = vertices[i].z; |
---|
118 | |
---|
119 | i = t->indices[2]; |
---|
120 | v2[0] = vertices[i].x; |
---|
121 | v2[1] = vertices[i].y; |
---|
122 | v2[2] = vertices[i].z; |
---|
123 | |
---|
124 | a = triangleArea(v0, v1, v2); |
---|
125 | |
---|
126 | //printf("Triangle area: %f\n", a) |
---|
127 | return a; |
---|
128 | } |
---|
129 | |
---|
130 | void VMI::computeTriangleNormal(Vertex *vertices, Triangle *t) { |
---|
131 | GLuint i; |
---|
132 | GLfloat x0, y0, z0, |
---|
133 | x1, y1, z1, |
---|
134 | x2, y2, z2; |
---|
135 | GLfloat x10, y10, z10, |
---|
136 | x12, y12, z12, |
---|
137 | cpx, cpy, cpz, r; |
---|
138 | |
---|
139 | i = t->indices[0]; |
---|
140 | x0 = vertices[i].x; |
---|
141 | y0 = vertices[i].y; |
---|
142 | z0 = vertices[i].z; |
---|
143 | |
---|
144 | i = t->indices[1]; |
---|
145 | x1 = vertices[i].x; |
---|
146 | y1 = vertices[i].y; |
---|
147 | z1 = vertices[i].z; |
---|
148 | |
---|
149 | i = t->indices[2]; |
---|
150 | x2 = vertices[i].x; |
---|
151 | y2 = vertices[i].y; |
---|
152 | z2 = vertices[i].z; |
---|
153 | |
---|
154 | /* Compute edge vectors */ |
---|
155 | x10 = x1 - x0; |
---|
156 | y10 = y1 - y0; |
---|
157 | z10 = z1 - z0; |
---|
158 | x12 = x1 - x2; |
---|
159 | y12 = y1 - y2; |
---|
160 | z12 = z1 - z2; |
---|
161 | |
---|
162 | /* Compute the cross product */ |
---|
163 | cpx = (z10 * y12) - (y10 * z12); |
---|
164 | cpy = (x10 * z12) - (z10 * x12); |
---|
165 | cpz = (y10 * x12) - (x10 * y12); |
---|
166 | |
---|
167 | /* Normalize the result to get the unit-length facet normal */ |
---|
168 | r = sqrt(cpx * cpx + cpy * cpy + cpz * cpz); |
---|
169 | |
---|
170 | t->normal[0] = cpx / r; |
---|
171 | t->normal[1] = cpy / r; |
---|
172 | t->normal[2] = cpz / r; |
---|
173 | } |
---|
174 | |
---|
175 | Mesh *VMI::initMesh(GLMmodel* pmodel) { |
---|
176 | |
---|
177 | GLuint i, j, curGroup, v1, v2, v3, n, m, t; |
---|
178 | int e; |
---|
179 | Mesh *mesh; |
---|
180 | GLMgroup *group; |
---|
181 | |
---|
182 | mesh = (Mesh *)malloc (sizeof(Mesh)); |
---|
183 | |
---|
184 | if (mesh == NULL) { |
---|
185 | fprintf(stderr, "Error allocating memory\n"); |
---|
186 | exit(1); |
---|
187 | } |
---|
188 | |
---|
189 | mesh->vertices = (Vertex *)malloc (sizeof(Vertex) * pmodel->numvertices); |
---|
190 | |
---|
191 | if (mesh->vertices == NULL) { |
---|
192 | fprintf(stderr, "Error allocating memory\n"); |
---|
193 | exit(1); |
---|
194 | } |
---|
195 | |
---|
196 | mesh->triangles = (Triangle *)malloc (sizeof(Triangle) * pmodel->numtriangles); |
---|
197 | |
---|
198 | if (mesh->triangles == NULL) { |
---|
199 | fprintf(stderr, "Error allocating memory\n"); |
---|
200 | exit(1); |
---|
201 | } |
---|
202 | |
---|
203 | printf("Adding vertices..."); |
---|
204 | for (i=1; i<=pmodel->numvertices; i++) { // Vertices start at 1 |
---|
205 | mesh->vertices[i - 1].x = pmodel->vertices[3 * i + 0]; |
---|
206 | mesh->vertices[i - 1].y = pmodel->vertices[3 * i + 1]; |
---|
207 | mesh->vertices[i - 1].z = pmodel->vertices[3 * i + 2]; |
---|
208 | mesh->vertices[i - 1].numTriangles = 0; |
---|
209 | mesh->vertices[i - 1].triangles = NULL; |
---|
210 | mesh->vertices[i - 1].enable = GL_TRUE; |
---|
211 | } |
---|
212 | printf("Ok\n"); |
---|
213 | mesh->numVertices = pmodel->numvertices; |
---|
214 | mesh->currentNumVertices = pmodel->numvertices; |
---|
215 | |
---|
216 | printf("Adding triangles..."); |
---|
217 | |
---|
218 | #ifdef OUTPUT_SEVERAL_GROUPS |
---|
219 | group = pmodel->groups; |
---|
220 | curGroup=0; // current group |
---|
221 | while(group) { |
---|
222 | |
---|
223 | for (i=0; i<group->numtriangles; i++) { |
---|
224 | |
---|
225 | t = group->triangles[i]; |
---|
226 | |
---|
227 | mesh->triangles[t].id = t; |
---|
228 | mesh->triangles[t].group = curGroup; // set group |
---|
229 | mesh->triangles[t].indices[0] = pmodel->triangles[t].vindices[0] - 1; |
---|
230 | mesh->triangles[t].indices[1] = pmodel->triangles[t].vindices[1] - 1; |
---|
231 | mesh->triangles[t].indices[2] = pmodel->triangles[t].vindices[2] - 1; |
---|
232 | mesh->triangles[t].area = computeTriangleArea(mesh->vertices, &mesh->triangles[t]); |
---|
233 | //printf("\n%d a: %f",i , mesh->triangles[i].area); |
---|
234 | computeTriangleNormal(mesh->vertices, &mesh->triangles[t]); |
---|
235 | |
---|
236 | mesh->triangles[t].saliency = 0.0; |
---|
237 | |
---|
238 | mesh->triangles[t].enable = GL_TRUE; |
---|
239 | |
---|
240 | for (j=0; j<3; j++) { |
---|
241 | // Adding triangle i adjacent to 3 vertices |
---|
242 | v1 = mesh->triangles[t].indices[j]; |
---|
243 | |
---|
244 | // Reallocate memory for the new adjacent triangle |
---|
245 | mesh->vertices[v1].triangles = |
---|
246 | (GLuint *)realloc(mesh->vertices[v1].triangles, (mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); |
---|
247 | addItem((int *)mesh->vertices[v1].triangles, (int *)&mesh->vertices[v1].numTriangles, t); |
---|
248 | } |
---|
249 | } |
---|
250 | curGroup++; |
---|
251 | group = group->next; |
---|
252 | } |
---|
253 | #else // One single group |
---|
254 | for (i=0; i<pmodel->numtriangles; i++) { |
---|
255 | mesh->triangles[i].id = i; |
---|
256 | mesh->triangles[i].indices[0] = pmodel->triangles[i].vindices[0] - 1; |
---|
257 | mesh->triangles[i].indices[1] = pmodel->triangles[i].vindices[1] - 1; |
---|
258 | mesh->triangles[i].indices[2] = pmodel->triangles[i].vindices[2] - 1; |
---|
259 | mesh->triangles[i].area = computeTriangleArea(mesh->vertices, &mesh->triangles[i]); |
---|
260 | //printf("\n%d a: %f",i , mesh->triangles[i].area); |
---|
261 | computeTriangleNormal(mesh->vertices, &mesh->triangles[i]); |
---|
262 | |
---|
263 | mesh->triangles[i].saliency = 0.0; |
---|
264 | |
---|
265 | mesh->triangles[i].enable = GL_TRUE; |
---|
266 | |
---|
267 | for (j=0; j<3; j++) { |
---|
268 | // Adding triangle i adjacent to 3 vertices |
---|
269 | v1 = mesh->triangles[i].indices[j]; |
---|
270 | |
---|
271 | // Reallocate memory for the new adjacent triangle |
---|
272 | mesh->vertices[v1].triangles = |
---|
273 | (GLuint *)realloc(mesh->vertices[v1].triangles, (mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); |
---|
274 | addItem((int *)mesh->vertices[v1].triangles, (int *)&mesh->vertices[v1].numTriangles, i); |
---|
275 | } |
---|
276 | } |
---|
277 | printf("\n"); |
---|
278 | #endif |
---|
279 | printf("Ok\n"); |
---|
280 | |
---|
281 | mesh->numTriangles = pmodel->numtriangles; |
---|
282 | mesh->currentNumTriangles = pmodel->numtriangles; |
---|
283 | printf("Number of triangles: %d\n", mesh->numTriangles); |
---|
284 | |
---|
285 | mesh->edges = (Edge *)malloc (sizeof(Edge) * mesh->numTriangles * 3); // E = 3 T / 2 |
---|
286 | |
---|
287 | if (mesh->edges == NULL) { |
---|
288 | fprintf(stderr, "Error allocating memory\n"); |
---|
289 | exit(1); |
---|
290 | } |
---|
291 | |
---|
292 | printf("Adding edges..."); |
---|
293 | n = 0; |
---|
294 | |
---|
295 | for (i=0; i<mesh->numTriangles; i++) { |
---|
296 | t = 0; |
---|
297 | v1 = mesh->triangles[i].indices[0]; |
---|
298 | v2 = mesh->triangles[i].indices[1]; |
---|
299 | v3 = mesh->triangles[i].indices[2]; |
---|
300 | |
---|
301 | ///////////////////////////////////////////////////////////////////////////////// |
---|
302 | // edge (v1,v2) |
---|
303 | if ((e = findEdge(mesh->edges, n, v1, v2)) == -1) { |
---|
304 | mesh->edges[n].u = v1; |
---|
305 | mesh->edges[n].v = v2; |
---|
306 | mesh->edges[n].triangles = NULL; |
---|
307 | mesh->edges[n].numTriangles = 0; |
---|
308 | mesh->edges[n].enable = GL_TRUE; |
---|
309 | m = n; |
---|
310 | n++; |
---|
311 | } else m = e; |
---|
312 | // Reallocate memory for the new adjacent triangle |
---|
313 | mesh->edges[m].triangles = |
---|
314 | (GLuint *)realloc(mesh->edges[m].triangles, (mesh->edges[m].numTriangles + 1) * sizeof(GLuint)); |
---|
315 | // Adding triangle i adjacent to edge m |
---|
316 | addItem((int *)mesh->edges[m].triangles, (int *)&mesh->edges[m].numTriangles, i); |
---|
317 | //printf("n:%d i:%d\n", m, i); |
---|
318 | |
---|
319 | // Adding edge m adjacent to triangle i |
---|
320 | addItem((int *)mesh->triangles[i].edges, (int *)&t, m); |
---|
321 | |
---|
322 | ///////////////////////////////////////////////////////////////////////////////// |
---|
323 | // edge (v2,v3) |
---|
324 | if (( e = findEdge(mesh->edges, n, v2, v3)) == -1) { |
---|
325 | mesh->edges[n].u = v2; |
---|
326 | mesh->edges[n].v = v3; |
---|
327 | mesh->edges[n].triangles = NULL; |
---|
328 | mesh->edges[n].numTriangles = 0; |
---|
329 | mesh->edges[n].enable = GL_TRUE; |
---|
330 | m = n; |
---|
331 | n++; |
---|
332 | } else m = e; |
---|
333 | // Reallocate memory for the new adjacent triangle |
---|
334 | mesh->edges[m].triangles = |
---|
335 | (GLuint *)realloc(mesh->edges[m].triangles, (mesh->edges[m].numTriangles + 1) * sizeof(GLuint)); |
---|
336 | // Adding triangle i adjacent to edge m |
---|
337 | addItem((int *)mesh->edges[m].triangles, (int *)&mesh->edges[m].numTriangles, i); |
---|
338 | //printf("n:%d i:%d\n", m, i); |
---|
339 | |
---|
340 | // Adding edge m adjacent to triangle i |
---|
341 | addItem((int *)mesh->triangles[i].edges, (int *)&t, m); |
---|
342 | |
---|
343 | ///////////////////////////////////////////////////////////////////////////////// |
---|
344 | // edge (v3,v1) |
---|
345 | if ((e = findEdge(mesh->edges, n, v3, v1)) == -1) { |
---|
346 | mesh->edges[n].u = v3; |
---|
347 | mesh->edges[n].v = v1; |
---|
348 | mesh->edges[n].triangles = NULL; |
---|
349 | mesh->edges[n].numTriangles = 0; |
---|
350 | mesh->edges[n].enable = GL_TRUE; |
---|
351 | m = n; |
---|
352 | n++; |
---|
353 | } else m = e; |
---|
354 | // Reallocate memory for the new adjacent triangle |
---|
355 | mesh->edges[m].triangles = |
---|
356 | (GLuint *)realloc(mesh->edges[m].triangles, (mesh->edges[m].numTriangles + 1) * sizeof(GLuint)); |
---|
357 | // Adding triangle i adjacent to edge m |
---|
358 | addItem((int *)mesh->edges[m].triangles, (int *)&mesh->edges[m].numTriangles, i); |
---|
359 | //printf("n:%d i:%d\n", m, i); |
---|
360 | |
---|
361 | // Adding edge m adjacent to triangle i |
---|
362 | addItem((int *)mesh->triangles[i].edges, (int *)&t, m); |
---|
363 | } |
---|
364 | printf("Ok\n"); |
---|
365 | mesh->numEdges = n; |
---|
366 | |
---|
367 | return mesh; |
---|
368 | } |
---|
369 | |
---|
370 | void VMI::printMesh(Mesh *mesh) { |
---|
371 | GLuint i, j; |
---|
372 | |
---|
373 | printf("Vertices (%d)\n", mesh->numVertices); |
---|
374 | printf("------------------------\n"); |
---|
375 | |
---|
376 | for (i=0; i<mesh->numVertices; i++) { |
---|
377 | printf("v%d (%f,%f,%f)\n", i, mesh->vertices[i].x, mesh->vertices[i].y, |
---|
378 | mesh->vertices[i].z); |
---|
379 | printf(" t(%d)[", mesh->vertices[i].numTriangles); |
---|
380 | if (mesh->vertices[i].triangles != NULL) { |
---|
381 | printf("%d", mesh->vertices[i].triangles[0]); |
---|
382 | for (j=1; j<mesh->vertices[i].numTriangles; j++) { |
---|
383 | printf(",%d", mesh->vertices[i].triangles[j]); |
---|
384 | } |
---|
385 | } |
---|
386 | printf("]\n"); |
---|
387 | } |
---|
388 | |
---|
389 | printf("Edges (%d)\n", mesh->numEdges); |
---|
390 | printf("------------------------\n"); |
---|
391 | for (i=0; i<mesh->numEdges; i++) { |
---|
392 | printf("e%d (%d,%d) %d\n", i, mesh->edges[i].u, mesh->edges[i].v, mesh->edges[i].enable); |
---|
393 | printf(" t(%d)[", mesh->edges[i].numTriangles); |
---|
394 | if (mesh->edges[i].triangles != NULL) { |
---|
395 | printf("%d", mesh->edges[i].triangles[0]); |
---|
396 | for (j=1; j<mesh->edges[i].numTriangles; j++) { |
---|
397 | printf(",%d", mesh->edges[i].triangles[j]); |
---|
398 | } |
---|
399 | } |
---|
400 | printf("]\n"); |
---|
401 | } |
---|
402 | |
---|
403 | printf("Triangles (%d)\n", mesh->numTriangles); |
---|
404 | printf("------------------------\n"); |
---|
405 | for (i=0; i<mesh->numTriangles; i++) { |
---|
406 | printf("t%d (%d,%d,%d) %d\n", i, mesh->triangles[i].indices[0], mesh->triangles[i].indices[1], |
---|
407 | mesh->triangles[i].indices[2], mesh->triangles[i].enable); |
---|
408 | printf(" e(3)["); |
---|
409 | printf("%d", mesh->triangles[i].edges[0]); |
---|
410 | for (j=1; j<3; j++) { |
---|
411 | printf(",%d", mesh->triangles[i].edges[j]); |
---|
412 | } |
---|
413 | printf("]\n"); |
---|
414 | |
---|
415 | printf(" n(3)["); |
---|
416 | printf("%f", mesh->triangles[i].normal[0]); |
---|
417 | for (j=1; j<3; j++) { |
---|
418 | printf(",%f", mesh->triangles[i].normal[j]); |
---|
419 | } |
---|
420 | printf("]\n"); |
---|
421 | |
---|
422 | printf(" area: %f\n", mesh->triangles[i].area); |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | void VMI::deleteMesh(Mesh *mesh) { |
---|
427 | GLuint i; |
---|
428 | |
---|
429 | if (NULL != mesh) { |
---|
430 | for (i=0; i<mesh->numVertices; i++) { |
---|
431 | if (mesh->vertices[i].triangles != NULL) |
---|
432 | free(mesh->vertices[i].triangles); |
---|
433 | |
---|
434 | } |
---|
435 | if (mesh->vertices != NULL) free(mesh->vertices); |
---|
436 | mesh->numVertices = 0; |
---|
437 | |
---|
438 | for (i=0; i<mesh->numEdges; i++) { |
---|
439 | if (mesh->edges[i].triangles != NULL) |
---|
440 | free(mesh->edges[i].triangles); |
---|
441 | } |
---|
442 | if (mesh->edges != NULL) free(mesh->edges); |
---|
443 | mesh->numEdges = 0; |
---|
444 | |
---|
445 | if (mesh->triangles != NULL) free(mesh->triangles); |
---|
446 | mesh->numTriangles = 0; |
---|
447 | |
---|
448 | free(mesh); |
---|
449 | |
---|
450 | mesh = NULL; |
---|
451 | } |
---|
452 | } |
---|
453 | |
---|
454 | int *VMI::trianglesAdjToEdge(Mesh *mesh, int e, int *n) { |
---|
455 | int i, n0, n1, n2, num = 0, t, |
---|
456 | u = mesh->edges[e].u, |
---|
457 | v = mesh->edges[e].v; |
---|
458 | int *triangles = NULL; |
---|
459 | |
---|
460 | if ((mesh->vertices[u].numTriangles + |
---|
461 | mesh->vertices[v].numTriangles) > 0) |
---|
462 | triangles = (int *)malloc((mesh->vertices[u].numTriangles + |
---|
463 | mesh->vertices[v].numTriangles) * sizeof(int)); |
---|
464 | |
---|
465 | for (i=0; i<(int)mesh->vertices[u].numTriangles; i++) { |
---|
466 | |
---|
467 | t = mesh->vertices[u].triangles[i]; |
---|
468 | |
---|
469 | if (mesh->triangles[t].enable == TRUE) { |
---|
470 | |
---|
471 | n0 = mesh->triangles[t].indices[0]; |
---|
472 | n1 = mesh->triangles[t].indices[1]; |
---|
473 | n2 = mesh->triangles[t].indices[2]; |
---|
474 | |
---|
475 | if ((n0 == v) || (n1 == v) || (n2 == v) || |
---|
476 | (n0 == u) || (n1 == u) || (n2 == u)) |
---|
477 | |
---|
478 | addItem(triangles, &num, t); |
---|
479 | } |
---|
480 | } |
---|
481 | |
---|
482 | for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) { |
---|
483 | |
---|
484 | t = mesh->vertices[v].triangles[i]; |
---|
485 | |
---|
486 | if (mesh->triangles[t].enable == TRUE) { |
---|
487 | |
---|
488 | n0 = mesh->triangles[t].indices[0]; |
---|
489 | n1 = mesh->triangles[t].indices[1]; |
---|
490 | n2 = mesh->triangles[t].indices[2]; |
---|
491 | |
---|
492 | if ((n0 == v) || (n1 == v) || (n2 == v) || |
---|
493 | (n0 == u) || (n1 == u) || (n2 == u)) |
---|
494 | |
---|
495 | addItem(triangles, &num, t); |
---|
496 | } |
---|
497 | } |
---|
498 | *n = num; |
---|
499 | |
---|
500 | return triangles; |
---|
501 | } |
---|
502 | |
---|
503 | int *VMI::verticesAdjToVertex(Mesh *mesh, int v, int *n) { |
---|
504 | int *vertices = NULL; |
---|
505 | int i, t, n0, n1, n2; |
---|
506 | int num = 0; |
---|
507 | |
---|
508 | if (mesh->vertices[v].numTriangles > 0) |
---|
509 | vertices = (int*)malloc(sizeof(int) * mesh->vertices[v].numTriangles * 2); |
---|
510 | |
---|
511 | for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) { |
---|
512 | t = mesh->vertices[v].triangles[i]; |
---|
513 | |
---|
514 | n0 = mesh->triangles[t].indices[0]; |
---|
515 | n1 = mesh->triangles[t].indices[1]; |
---|
516 | n2 = mesh->triangles[t].indices[2]; |
---|
517 | |
---|
518 | if (n0 != v) addItem(vertices, &num, n0); |
---|
519 | if (n1 != v) addItem(vertices, &num, n1); |
---|
520 | if (n2 != v) addItem(vertices, &num, n2); |
---|
521 | } |
---|
522 | *n = num; |
---|
523 | |
---|
524 | return vertices; |
---|
525 | } |
---|
526 | |
---|
527 | int *VMI::edgesAdjToVertex(Mesh *mesh, int v, int *n) { |
---|
528 | int *edges = NULL; |
---|
529 | int i, t, e; |
---|
530 | #ifndef USE_EDGE_ADJACENCY |
---|
531 | int n0, n1, n2; |
---|
532 | #else |
---|
533 | int j; |
---|
534 | #endif |
---|
535 | int num = 0; |
---|
536 | |
---|
537 | if (mesh->vertices[v].numTriangles > 0) |
---|
538 | edges = (int*)malloc(sizeof(int) * mesh->vertices[v].numTriangles * 2); |
---|
539 | |
---|
540 | for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) { |
---|
541 | |
---|
542 | t = mesh->vertices[v].triangles[i]; |
---|
543 | |
---|
544 | #ifdef USE_EDGE_ADJACENCY |
---|
545 | for (j=0; j<3; j++) { |
---|
546 | e = mesh->triangles[t].edges[j]; |
---|
547 | |
---|
548 | if ((int)mesh->edges[e].u == v || (int)mesh->edges[e].v == v) |
---|
549 | |
---|
550 | addItem(edges, &num, e); |
---|
551 | } |
---|
552 | #else |
---|
553 | n0 = mesh->triangles[t].indices[0]; |
---|
554 | n1 = mesh->triangles[t].indices[1]; |
---|
555 | n2 = mesh->triangles[t].indices[2]; |
---|
556 | |
---|
557 | if (((n0 == v) || (n1 == v)) && |
---|
558 | ((e = findEdge(mesh->edges, mesh->numEdges, n0, n1)) != -1)) addItem(edges, &num, e); |
---|
559 | |
---|
560 | if (((n1 == v) || (n2 == v)) && |
---|
561 | ((e = findEdge(mesh->edges, mesh->numEdges, n1, n2)) != -1)) addItem(edges, &num, e); |
---|
562 | |
---|
563 | if (((n0 == v) || (n2 == v)) && |
---|
564 | ((e = findEdge(mesh->edges, mesh->numEdges, n0, n2)) != -1)) addItem(edges, &num, e); |
---|
565 | |
---|
566 | #endif |
---|
567 | } |
---|
568 | *n = num; |
---|
569 | |
---|
570 | return edges; |
---|
571 | } |
---|
572 | |
---|
573 | int *VMI::edgesAdjToVertices(Mesh *mesh, int *vertices, int numVertices, int *n) { |
---|
574 | int *edges = NULL; |
---|
575 | int i, j, num = 0; |
---|
576 | int *list = NULL, numEdges; |
---|
577 | |
---|
578 | if (numVertices > 0) { |
---|
579 | // Add the first |
---|
580 | list = edgesAdjToVertex(mesh, vertices[0], &numEdges); |
---|
581 | |
---|
582 | // Allocate memory |
---|
583 | edges = (int *)malloc(numEdges * sizeof(int)); |
---|
584 | |
---|
585 | memcpy(edges, list, sizeof(int) * numEdges); |
---|
586 | |
---|
587 | free(list); |
---|
588 | |
---|
589 | num = numEdges; |
---|
590 | |
---|
591 | // Add the rest |
---|
592 | for (i=1; i<numVertices; i++) { |
---|
593 | |
---|
594 | list = edgesAdjToVertex(mesh, vertices[i], &numEdges); |
---|
595 | |
---|
596 | // Reallocate memory |
---|
597 | if (numEdges > 0) { |
---|
598 | |
---|
599 | edges = (int *)realloc(edges, (num + numEdges + 1) * sizeof(int)); |
---|
600 | |
---|
601 | for (j=0; j<numEdges; j++) |
---|
602 | addItem(edges, &num, list[j]); |
---|
603 | } |
---|
604 | free(list); |
---|
605 | } |
---|
606 | } |
---|
607 | *n = num; |
---|
608 | |
---|
609 | return edges; |
---|
610 | } |
---|
611 | |
---|
612 | GLboolean VMI::findVertex(GLfloat *vertices, GLuint num, GLfloat x, GLfloat y, GLfloat z, int *pos) { |
---|
613 | GLuint i; |
---|
614 | GLfloat _x, _y, _z; |
---|
615 | GLboolean found = GL_FALSE; |
---|
616 | |
---|
617 | *pos = -1; |
---|
618 | |
---|
619 | for (i=1; i<num; i++) { // i=0 |
---|
620 | |
---|
621 | _x = vertices[3 * i + 0]; |
---|
622 | _y = vertices[3 * i + 1]; |
---|
623 | _z = vertices[3 * i + 2]; |
---|
624 | |
---|
625 | if (((_x == x) && (_y == y) && (_z == z))) { |
---|
626 | found = GL_TRUE; |
---|
627 | *pos = (int)i; |
---|
628 | break; |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | return found; |
---|
633 | } |
---|
634 | |
---|
635 | // Adds a new vertex at the end of the mesh vertex list |
---|
636 | Vertex *VMI::addVertex(Vertex *list, int *n, float x, float y, float z, int *pos) { |
---|
637 | Vertex *newList; |
---|
638 | |
---|
639 | // Reallocate memory for the new vertex |
---|
640 | newList = (Vertex *)realloc(list, ((*n) + 1) * sizeof(Vertex)); |
---|
641 | |
---|
642 | // New Vertex coordinates |
---|
643 | newList[*n].x = x; |
---|
644 | newList[*n].y = y; |
---|
645 | newList[*n].z = z; |
---|
646 | |
---|
647 | // This vertex has no triangle adjancency |
---|
648 | newList[*n].numTriangles = 0; |
---|
649 | newList[*n].triangles = NULL; |
---|
650 | |
---|
651 | // Enable the new vertex |
---|
652 | newList[*n].enable = TRUE; |
---|
653 | |
---|
654 | // New vertex position |
---|
655 | *pos = *n; |
---|
656 | |
---|
657 | // New number of vertices |
---|
658 | (*n)++; |
---|
659 | |
---|
660 | return newList; |
---|
661 | } |
---|
662 | |
---|
663 | |
---|
664 | |
---|
665 | void VMI::updateModel(GLMmodel* pmodel, Mesh *mesh, GLuint numVertices, GLuint numTriangles) { |
---|
666 | GLuint i , v1, v2 ,v3, numV = 1, numT = 0; |
---|
667 | int pos; |
---|
668 | GLfloat x, y, z; |
---|
669 | |
---|
670 | if (pmodel->vertices != NULL) free(pmodel->vertices); |
---|
671 | pmodel->vertices = (GLfloat *)calloc ((numVertices + 1) * 3, sizeof(GLfloat)); |
---|
672 | |
---|
673 | if (pmodel->triangles != NULL) free(pmodel->triangles); |
---|
674 | pmodel->triangles = (GLMtriangle *)malloc (numTriangles * sizeof(GLMtriangle)); |
---|
675 | |
---|
676 | for (i=0; i<mesh->numTriangles; i++) { |
---|
677 | if (mesh->triangles[i].enable) { |
---|
678 | v1 = mesh->triangles[i].indices[0]; |
---|
679 | v2 = mesh->triangles[i].indices[1]; |
---|
680 | v3 = mesh->triangles[i].indices[2]; |
---|
681 | |
---|
682 | x = mesh->vertices[v1].x; |
---|
683 | y = mesh->vertices[v1].y; |
---|
684 | z = mesh->vertices[v1].z; |
---|
685 | |
---|
686 | if (!findVertex(pmodel->vertices, numV, x, y ,z, &pos)) { |
---|
687 | pmodel->triangles[numT].vindices[0] = numV; |
---|
688 | |
---|
689 | pmodel->vertices[3 * numV + 0] = x; |
---|
690 | pmodel->vertices[3 * numV + 1] = y; |
---|
691 | pmodel->vertices[3 * numV + 2] = z; |
---|
692 | numV++; |
---|
693 | } else pmodel->triangles[numT].vindices[0] = (GLuint)pos; |
---|
694 | |
---|
695 | x = mesh->vertices[v2].x; |
---|
696 | y = mesh->vertices[v2].y; |
---|
697 | z = mesh->vertices[v2].z; |
---|
698 | |
---|
699 | if (!findVertex(pmodel->vertices, numV, x, y ,z, &pos)) { |
---|
700 | pmodel->triangles[numT].vindices[1] = numV; |
---|
701 | |
---|
702 | pmodel->vertices[3 * numV + 0] = x; |
---|
703 | pmodel->vertices[3 * numV + 1] = y; |
---|
704 | pmodel->vertices[3 * numV + 2] = z; |
---|
705 | numV++; |
---|
706 | } else pmodel->triangles[numT].vindices[1] = (GLuint)pos; |
---|
707 | |
---|
708 | x = mesh->vertices[v3].x; |
---|
709 | y = mesh->vertices[v3].y; |
---|
710 | z = mesh->vertices[v3].z; |
---|
711 | |
---|
712 | if (!findVertex(pmodel->vertices, numV, x, y ,z, &pos)) { |
---|
713 | pmodel->triangles[numT].vindices[2] = numV; |
---|
714 | |
---|
715 | pmodel->vertices[3 * numV + 0] = x; |
---|
716 | pmodel->vertices[3 * numV + 1] = y; |
---|
717 | pmodel->vertices[3 * numV + 2] = z; |
---|
718 | numV++; |
---|
719 | } else pmodel->triangles[numT].vindices[2] = (GLuint)pos; |
---|
720 | numT++; |
---|
721 | } |
---|
722 | } |
---|
723 | pmodel->numvertices = numV - 1; |
---|
724 | pmodel->numtriangles = numT; |
---|
725 | } |
---|
726 | |
---|
727 | void VMI::saveModel(char *filename, GLMmodel* pmodel, Mesh *mesh) { |
---|
728 | GLuint i, j, g, numT = 0; |
---|
729 | GLMgroup *group; |
---|
730 | |
---|
731 | #ifdef OUTPUT_SEVERAL_GROUPS |
---|
732 | // The output model maintains groups |
---|
733 | |
---|
734 | // Empty all groups |
---|
735 | group = pmodel->groups; |
---|
736 | while(group) { |
---|
737 | // Free memory for triangles |
---|
738 | free(group->triangles); |
---|
739 | |
---|
740 | // Reset group |
---|
741 | group->triangles = NULL; |
---|
742 | group->numtriangles = 0; |
---|
743 | |
---|
744 | group = group->next; |
---|
745 | } |
---|
746 | // Fill all groups |
---|
747 | for (i=0; i<mesh->numTriangles; i++) { |
---|
748 | |
---|
749 | if (mesh->triangles[i].enable) { |
---|
750 | g = mesh->triangles[i].group; |
---|
751 | |
---|
752 | // Get group |
---|
753 | group = pmodel->groups; |
---|
754 | for (j=0; j <g; j++) |
---|
755 | group = group->next; |
---|
756 | |
---|
757 | // Reallocate memory for triangles |
---|
758 | group->triangles = (GLuint *)realloc(group->triangles, (group->numtriangles + 1) * sizeof(GLuint)); |
---|
759 | // Add triangle into group |
---|
760 | addItem((int *)group->triangles, (int *)&group->numtriangles, numT); |
---|
761 | |
---|
762 | numT++; |
---|
763 | } |
---|
764 | } |
---|
765 | #else |
---|
766 | // The output model contains only one group |
---|
767 | pmodel->numgroups = 1; |
---|
768 | |
---|
769 | if (pmodel->groups->name != NULL) free(pmodel->groups->name); |
---|
770 | pmodel->groups->name = strdup("default"); |
---|
771 | pmodel->groups->material = 0; |
---|
772 | pmodel->groups->next = NULL; |
---|
773 | pmodel->groups->numtriangles = mesh->currentNumTriangles; |
---|
774 | |
---|
775 | if (pmodel->groups->triangles != NULL) free(pmodel->groups->triangles); |
---|
776 | pmodel->groups->triangles = (GLuint*)malloc(sizeof(GLuint) * mesh->currentNumTriangles); |
---|
777 | |
---|
778 | for (i=0; i<mesh->currentNumTriangles; i++) |
---|
779 | pmodel->groups->triangles[i] = i; |
---|
780 | #endif |
---|
781 | |
---|
782 | updateModel(pmodel, mesh, mesh->currentNumTriangles * 3, mesh->currentNumTriangles); |
---|
783 | |
---|
784 | printf("glmWriteOBJ..."); |
---|
785 | |
---|
786 | glmWriteOBJ(pmodel, filename, GLM_NONE); |
---|
787 | printf("Ok\n"); |
---|
788 | } |
---|