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

Revision 2194, 21.4 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{
447        int i;
448
449        if (NULL != mesh)
450        {
451                for (i=0; i<mesh->numVertices; i++)
452                {
453                        if (mesh->vertices[i].triangles != NULL)
454                                free(mesh->vertices[i].triangles);
455
456                        if (mesh->vertices[i].edges != NULL)
457                                free(mesh->vertices[i].edges);
458                }
459
460                if (mesh->vertices != NULL) free(mesh->vertices);
461                mesh->numVertices = 0;
462
463                if (mesh->edges != NULL) free(mesh->edges);
464                mesh->numEdges = 0;
465
466                if (mesh->triangles != NULL) free(mesh->triangles);
467                mesh->numTriangles = 0;
468
469                free(mesh);
470
471                mesh = NULL;
472        }
473}
474
475int *VMI::trianglesAdjToEdge(Mesh *mesh, int e, int *n) {
476    int i, n0, n1, n2, num = 0, t,
477        u = mesh->edges[e].u,
478        v = mesh->edges[e].v;
479    int *triangles = NULL;
480
481        if ((mesh->vertices[u].numTriangles +
482         mesh->vertices[v].numTriangles) > 0)
483                 triangles = (int *)malloc((mesh->vertices[u].numTriangles +
484                                    mesh->vertices[v].numTriangles) * sizeof(int));
485
486    for (i=0; i<(int)mesh->vertices[u].numTriangles; i++) {
487
488        t = mesh->vertices[u].triangles[i];
489       
490        if (mesh->triangles[t].enable == TRUE) {
491           
492            n0 = mesh->triangles[t].indices[0];
493            n1 = mesh->triangles[t].indices[1];
494            n2 = mesh->triangles[t].indices[2];
495           
496            if ((n0 == v) || (n1 == v) || (n2 == v) ||
497                (n0 == u) || (n1 == u) || (n2 == u))
498               
499                addItem(triangles, &num, t);
500        }
501    }
502
503    for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) {
504
505        t = mesh->vertices[v].triangles[i];
506       
507        if (mesh->triangles[t].enable == TRUE) {
508           
509            n0 = mesh->triangles[t].indices[0];
510            n1 = mesh->triangles[t].indices[1];
511            n2 = mesh->triangles[t].indices[2];
512           
513            if ((n0 == v) || (n1 == v) || (n2 == v) ||
514                (n0 == u) || (n1 == u) || (n2 == u))
515               
516                addItem(triangles, &num, t);
517        }
518    }
519    *n = num;
520
521    return triangles;
522}
523
524int *VMI::verticesAdjToVertex(Mesh *mesh, int v, int *n) {
525    int *vertices = NULL;
526    int i, t, n0, n1, n2;
527    int num = 0;
528
529        if (mesh->vertices[v].numTriangles > 0)
530                vertices = (int*)malloc(sizeof(int) * mesh->vertices[v].numTriangles * 2);
531
532    for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) {
533        t = mesh->vertices[v].triangles[i];
534
535        n0 = mesh->triangles[t].indices[0];
536        n1 = mesh->triangles[t].indices[1];
537        n2 = mesh->triangles[t].indices[2];
538
539        if (n0 != v) addItem(vertices, &num, n0);
540        if (n1 != v) addItem(vertices, &num, n1);
541        if (n2 != v) addItem(vertices, &num, n2);
542    }
543    *n = num;
544
545    return vertices;
546}
547
548int *VMI::edgesAdjToVertices(Mesh *mesh, int *vertices, int numVertices, int *n) {
549    int *edges = NULL;
550    int i, j, num = 0;
551    int *list = NULL, numEdges;
552
553    if (numVertices > 0) {
554        // Add the first
555                    list = mesh->vertices[vertices[0]].edges;
556                    numEdges = mesh->vertices[vertices[0]].numEdges;
557
558        // Allocate memory
559        edges = (int *)malloc(numEdges * sizeof(int));
560
561        memcpy(edges, list, sizeof(int) * numEdges);
562
563        num = numEdges;
564
565        // Add the rest
566        for (i=1; i<numVertices; i++) {
567
568                              list = mesh->vertices[vertices[i]].edges;
569                        numEdges = mesh->vertices[vertices[i]].numEdges;
570
571            // Reallocate memory
572            if (numEdges > 0) {
573
574                edges = (int *)realloc(edges, (num + numEdges + 1) * sizeof(int));
575               
576                for (j=0; j<numEdges; j++)
577                    addItem(edges, &num, list[j]);
578            }
579        }
580    }
581    *n = num;
582
583    return edges;
584}
585
586GLboolean VMI::findVertex(float *vertices, int num, float x, float y, float z, int *pos) {
587    int i;
588    float _x, _y, _z;
589    GLboolean found = GL_FALSE;
590   
591    *pos = -1;
592
593    for (i=1; i<num; i++) { // i=0
594
595        _x = vertices[3 * i + 0];
596        _y = vertices[3 * i + 1];
597        _z = vertices[3 * i + 2];
598
599        if (((_x == x) && (_y == y) && (_z == z))) {
600            found = GL_TRUE;
601            *pos = (int)i;
602            break;
603        }
604    }
605
606    return found;
607}
608
609// Adds a new vertex at the end of the mesh vertex list
610Vertex *VMI::addVertex(Vertex *list, int *n, float x, float y, float z, int *pos) {
611          Vertex *newList;
612
613                // Reallocate memory for the new vertex
614    newList = (Vertex *)realloc(list, ((*n) + 1) * sizeof(Vertex));
615
616                // New Vertex coordinates
617                newList[*n].x = x;
618    newList[*n].y = y;
619    newList[*n].z = z;
620
621                // This vertex has no triangle adjancency
622                newList[*n].numTriangles = 0;
623                newList[*n].triangles = NULL;
624
625                newList[*n].numEdges= 0;
626                newList[*n].edges = NULL;
627       
628    // Enable the new vertex
629                newList[*n].enable = TRUE;
630
631                // New vertex position
632                *pos = *n;
633
634                // New number of vertices
635          (*n)++;
636               
637                return newList;
638}
639
640
641
642void VMI::updateModel(GLMmodel* pmodel, Mesh *mesh, int numVertices, int numTriangles) {
643    int i , v1, v2 ,v3, numV = 1, numT = 0;
644    int pos;
645    float x, y, z;
646   
647    if (pmodel->vertices != NULL) free(pmodel->vertices);
648    pmodel->vertices = (GLfloat *)calloc ((numVertices + 1) * 3,  sizeof(GLfloat));
649
650    if (pmodel->triangles != NULL) free(pmodel->triangles);
651    pmodel->triangles = (GLMtriangle *)malloc (numTriangles * sizeof(GLMtriangle));
652
653    for (i=0; i<mesh->numTriangles; i++) {
654        if (mesh->triangles[i].enable) {
655            v1 = mesh->triangles[i].indices[0];
656            v2 = mesh->triangles[i].indices[1];
657            v3 = mesh->triangles[i].indices[2];
658           
659            x = mesh->vertices[v1].x;
660            y = mesh->vertices[v1].y;
661            z = mesh->vertices[v1].z;
662           
663            if (!findVertex(pmodel->vertices, numV, x, y ,z, &pos)) {
664                pmodel->triangles[numT].vindices[0] = numV;
665               
666                pmodel->vertices[3 * numV + 0] = x;
667                pmodel->vertices[3 * numV + 1] = y;
668                pmodel->vertices[3 * numV + 2] = z;
669                numV++;
670            } else pmodel->triangles[numT].vindices[0] = (GLuint)pos;
671           
672            x = mesh->vertices[v2].x;
673            y = mesh->vertices[v2].y;
674            z = mesh->vertices[v2].z;
675           
676            if (!findVertex(pmodel->vertices, numV, x, y ,z, &pos)) {
677                pmodel->triangles[numT].vindices[1] = numV;
678               
679                pmodel->vertices[3 * numV + 0] = x;
680                pmodel->vertices[3 * numV + 1] = y;
681                pmodel->vertices[3 * numV + 2] = z;
682                numV++;
683            } else pmodel->triangles[numT].vindices[1] = (GLuint)pos;
684           
685            x = mesh->vertices[v3].x;
686            y = mesh->vertices[v3].y;
687            z = mesh->vertices[v3].z;
688           
689            if (!findVertex(pmodel->vertices, numV, x, y ,z, &pos)) {
690                pmodel->triangles[numT].vindices[2] = numV;
691               
692                pmodel->vertices[3 * numV + 0] = x;
693                pmodel->vertices[3 * numV + 1] = y;
694                pmodel->vertices[3 * numV + 2] = z;
695                numV++;
696            } else pmodel->triangles[numT].vindices[2] = (GLuint)pos;
697            numT++;
698        }
699    }
700    pmodel->numvertices  = numV - 1;
701    pmodel->numtriangles = numT;
702}
703
704void VMI::saveModel(char *filename, GLMmodel* pmodel, Mesh *mesh) {
705    int i, j, g, numT = 0;
706    GLMgroup *group;
707
708#ifdef OUTPUT_SEVERAL_GROUPS
709    // The output model maintains groups
710
711    // Empty all groups
712    group = pmodel->groups;
713    while(group) {
714        // Free memory for triangles
715        free(group->triangles);
716
717        // Reset group
718        group->triangles = NULL;
719        group->numtriangles = 0;
720
721        group = group->next;
722    }
723    // Fill all groups
724    for (i=0; i<mesh->numTriangles; i++) {
725
726        if (mesh->triangles[i].enable) {
727            g = mesh->triangles[i].group;
728
729            // Get group
730            group = pmodel->groups;
731            for (j=0; j <g; j++)
732                group = group->next;
733
734            // Reallocate memory for triangles
735            group->triangles = (GLuint *)realloc(group->triangles, (group->numtriangles + 1) * sizeof(GLuint));
736            // Add triangle into group
737            addItem((int *)group->triangles, (int *)&group->numtriangles, numT);
738
739            numT++;
740        }
741    }
742#else
743    // The output model contains only one group
744    pmodel->numgroups = 1;
745
746    if (pmodel->groups->name != NULL) free(pmodel->groups->name);
747    pmodel->groups->name = strdup("default");
748    pmodel->groups->material = 0;
749    pmodel->groups->next = NULL;
750    pmodel->groups->numtriangles = mesh->currentNumTriangles;
751
752    if (pmodel->groups->triangles != NULL) free(pmodel->groups->triangles);
753    pmodel->groups->triangles = (GLuint*)malloc(sizeof(GLuint) * mesh->currentNumTriangles);
754
755    for (i=0; i<mesh->currentNumTriangles; i++)
756        pmodel->groups->triangles[i] = i;
757#endif
758
759    updateModel(pmodel, mesh, mesh->currentNumTriangles * 3, mesh->currentNumTriangles);
760
761    printf("glmWriteOBJ...");
762   
763    glmWriteOBJ(pmodel, filename, GLM_NONE);
764    printf("Ok\n");
765}
Note: See TracBrowser for help on using the repository browser.