source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/mesh.cpp @ 2127

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