Ignore:
Timestamp:
05/24/06 10:47:49 (18 years ago)
Author:
gumbau
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoMeshSimplifier.cpp

    r891 r980  
    88using namespace std; 
    99 
    10 MeshSimplifier::MeshSimplifier( const Geometry::Mesh    *m, 
    11                                                                                                                                 Geometry::TIPOFUNC              upb) 
     10//////////////////////////////////////////////////////////////////////////// 
     11//                                                                                                                                                                                                                                                                                              // 
     12//                                                                                      MeshSimplifier class                                                                                                                    // 
     13//                                                                                                                                                                                                                                                                                              // 
     14//////////////////////////////////////////////////////////////////////////// 
     15 
     16//--------------------------------------------------------------------------- 
     17//      MeshSimplifier constructor. 
     18//--------------------------------------------------------------------------- 
     19MeshSimplifier::MeshSimplifier( const Mesh      *m, 
     20                                                                                                                                TIPOFUNC                upb) 
    1221{ 
    1322        objmesh                                 =       m; 
    14         meshsalida                      =       NULL; 
     23        mGeoMesh                                =       NULL; 
    1524        msimpsequence           =       NULL; 
    1625        indexMeshLeaves =       -1; 
     
    2029} 
    2130 
     31//--------------------------------------------------------------------------- 
     32//      MeshSimplifier destroyer. 
     33//--------------------------------------------------------------------------- 
    2234MeshSimplifier::~MeshSimplifier() 
    2335{ 
     
    2537} 
    2638 
     39//--------------------------------------------------------------------------- 
    2740// Returns the simplified mesh. 
    28 Geometry::Mesh *MeshSimplifier::GetMesh () 
    29 { 
    30         return meshsalida; 
    31 } 
    32  
    33 void MeshSimplifier::setMeshLeaves(Geometry::Index index) 
     41//--------------------------------------------------------------------------- 
     42Mesh *  MeshSimplifier::GetMesh() 
     43{ 
     44        return mGeoMesh; 
     45} 
     46 
     47//--------------------------------------------------------------------------- 
     48//      Set submesh leaves 
     49//--------------------------------------------------------------------------- 
     50void MeshSimplifier::setMeshLeaves(Index index) 
    3451{ 
    3552        indexMeshLeaves =       index; 
    3653} 
    3754 
     55//--------------------------------------------------------------------------- 
    3856// Returns the simplification sequence for general meshes. 
    39 Geometry::MeshSimplificationSequence *MeshSimplifier::GetSimplificationSequence() 
     57//--------------------------------------------------------------------------- 
     58MeshSimplificationSequence *MeshSimplifier::GetSimplificationSequence() 
    4059{ 
    4160        return msimpsequence; 
    4261} 
    4362 
    44 GeometryBasedSimplifier::GeometryBasedSimplifier (const Geometry::Mesh  *m, 
    45                                                                                                                                                                                 Geometry::TIPOFUNC              upb) 
    46                                                                                                                                                                                 :MeshSimplifier(m,upb) 
    47 { 
    48 } 
    49  
     63//////////////////////////////////////////////////////////////////////////// 
     64//                                                                                                                                                                                                                                                                                              // 
     65//                                                                      GeometryBasedSimplifier class                                                                                                   // 
     66//                                                                                                                                                                                                                                                                                              // 
     67//////////////////////////////////////////////////////////////////////////// 
     68 
     69//--------------------------------------------------------------------------- 
     70//--------------------------------------------------------------------------- 
     71GeometryBasedSimplifier::GeometryBasedSimplifier(       const Mesh      *m, 
     72                                                                                                                                                                                                        TIPOFUNC                upb) 
     73                                                                                                                                                                                                :MeshSimplifier(m,upb) 
     74{ 
     75} 
     76 
     77//--------------------------------------------------------------------------- 
     78//--------------------------------------------------------------------------- 
    5079GeometryBasedSimplifier::~GeometryBasedSimplifier() 
    5180{ 
    5281} 
    5382 
     83//--------------------------------------------------------------------------- 
    5484// 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. 
    55 void GeometryBasedSimplifier::Simplify (Geometry::Real paramlod) 
     85//--------------------------------------------------------------------------- 
     86void GeometryBasedSimplifier::Simplify(Real paramlod) 
    5687{ 
    5788        SimplificationMethod *m_qslim   =       new SimplificationMethod(objmesh); 
     
    5990        m_qslim->setMeshLeaves(indexMeshLeaves); 
    6091         
    61         msimpsequence   =       m_qslim->Decimate(paramlod,&meshsalida,0,mUPB); 
     92        msimpsequence   =       m_qslim->Decimate(paramlod,0,mUPB); 
     93         
     94        mGeoMesh        =       m_qslim->GetMesh(); 
    6295         
    6396        delete  m_qslim; 
    6497} 
    6598 
     99//--------------------------------------------------------------------------- 
    66100// 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. 
    67 void GeometryBasedSimplifier::Simplify (Geometry::uint32 numvertices) 
     101//--------------------------------------------------------------------------- 
     102void GeometryBasedSimplifier::Simplify(uint32 numvertices) 
    68103{ 
    69104        SimplificationMethod *m_qslim   =       new SimplificationMethod(objmesh); 
     
    71106        m_qslim->setMeshLeaves(indexMeshLeaves); 
    72107         
    73         msimpsequence   =       m_qslim->Decimate((float)numvertices,&meshsalida,1,mUPB); 
     108        msimpsequence   =       m_qslim->Decimate((float)numvertices,1,mUPB); 
     109 
     110        mGeoMesh        =       m_qslim->GetMesh(); 
    74111         
    75112        delete m_qslim; 
    76113} 
    77114 
     115//////////////////////////////////////////////////////////////////////////// 
     116//                                                                                                                                                                                                                                                                                              // 
     117//                                                              ViewPointDrivenSimplifier class                                                                                                 // 
     118//                                                                                                                                                                                                                                                                                              // 
     119//////////////////////////////////////////////////////////////////////////// 
     120 
     121//--------------------------------------------------------------------------- 
     122/// Class constructor. Will call Simplifier class constructor. 
     123//--------------------------------------------------------------------------- 
     124ViewPointDrivenSimplifier::ViewPointDrivenSimplifier(   const Mesh      *m, 
     125                                                                                                                                                                                                                        TIPOFUNC                upb) 
     126                                                                                                                                                                                                                :MeshSimplifier(m,upb) 
     127{ 
     128        //char s[MAX_CHAR]; 
     129 
     130        //      Set progress update function 
     131        VMI::mUPB       =       upb; 
     132         
     133        VMI::width  = 256; 
     134        VMI::height = 256; 
     135 
     136        VMI::bEnableOffScreen     = GL_TRUE; 
     137        VMI::bBeQuiet             = GL_FALSE; 
     138        VMI::bSaveLog             = GL_FALSE; 
     139        VMI::bLoadCamerasFromFile = GL_FALSE; 
     140 
     141        VMI::cameraType = 1; 
     142        VMI::radius = 1.3; 
     143        VMI::fov = 60.0; 
     144 
     145        printf("w: %d h: %d\n", VMI::width, VMI::height); 
     146 
     147        printf( "t: %d c: %d o: %d r: %f\n", 
     148                        VMI::numDemandedTriangles, 
     149                        VMI::cameraType, 
     150                        VMI::bEnableOffScreen, 
     151                        VMI::radius); 
     152 
     153 
     154        mGeoMesh        =       new Mesh(); 
     155         
     156        *mGeoMesh       =       *m; 
     157         
     158        //      Transform NoSV Mesh to a SV Mesh. 
     159        mGeoMesh        =       mGeoMesh->toSharedVertex(); 
     160 
     161        //      Loads the vmi mesh structure for a geometry mesh given. 
     162        VMI::mesh = initMeshStructure(mGeoMesh); 
     163         
     164        //VMI::printMesh(mesh); 
     165        //getchar(); 
     166 
     167        // Get a filename without extension 
     168        //strncpy(VMI::filename, argv[argc - 1], strlen(argv[argc - 1]) - 4); 
     169 
     170        //glutInit(&argc, argv); 
     171        glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA); /* RGB and Alpha */ 
     172        glutInitWindowSize(VMI::width, VMI::height); 
     173        glutInitWindowPosition(100, 100); 
     174 
     175        //#ifdef KL // Kullback-Leibler 
     176        //    sprintf(s, "VKL - [%s]", argv[argc-1]);  
     177        //#endif 
     178        //#ifdef MI // Mutual Information 
     179        //    sprintf(s, "VMI - [%s]", argv[argc-1]);  
     180        //#endif 
     181        //#ifdef HE // Hellinger 
     182        //    sprintf(s, "VHE - [%s]", argv[argc-1]);  
     183        //#endif 
     184        //#ifdef CS // Chi-Square 
     185        //    sprintf(s, "VCS - [%s]", argv[argc-1]);  
     186        //#endif 
     187 
     188        VMI::vmiWin     =       glutCreateWindow("VMI"/*s*/); 
     189 
     190        glewInit(); 
     191 
     192        VMI::init(); 
     193 
     194        if (VMI::bLoadCamerasFromFile == GL_FALSE) 
     195        { 
     196                switch (VMI::cameraType) 
     197                { 
     198                        case 0: 
     199                                VMI::cameras = VMI::setCameras(VMI::radius, OCTAHEDRON, &VMI::numCameras); 
     200                                printf("Number of cameras: %d\n", OCTAHEDRON); 
     201                                break; 
     202                        case 1: 
     203                                VMI:: cameras = VMI::setCameras(VMI::radius, ICOSAHEDRON, &VMI::numCameras); 
     204                                printf("Number of cameras: %d\n", ICOSAHEDRON); 
     205                                break; 
     206                        case 2: 
     207                                VMI::cameras = VMI::setCameras(VMI::radius, DODECAHEDRON, &VMI::numCameras); 
     208                                printf("Number of cameras: %d\n", DODECAHEDRON); 
     209                                break; 
     210                        default: 
     211                                break; 
     212                } 
     213        } 
     214        else  
     215        { 
     216                // Loads a cameras file 
     217                //sprintf(s,"%s.cam", VMI::filename); 
     218 
     219                //VMI::cameras = VMI::loadCameras(VMI::radius, s, &VMI::numCameras); 
     220                //getchar(); 
     221        } 
     222 
     223        VMI::histogram  =       VMI::initHistogram(     VMI::mesh->currentNumTriangles, 
     224                                                                                                                                                                VMI::numCameras); 
     225 
     226        VMI::initialIs  =       VMI::initIs(VMI::numCameras); 
     227 
     228} 
     229 
     230//--------------------------------------------------------------------------- 
     231/// Class destructor. 
     232//--------------------------------------------------------------------------- 
     233ViewPointDrivenSimplifier::~ViewPointDrivenSimplifier(void) 
     234{ 
     235        // Free memory 
     236         VMI::freeMemory(); 
     237} 
     238 
     239/// Copy constructor 
     240//ViewPointDrivenSimplifier(const ViewPointDrivenSimplifier&); 
     241 
     242/// Assignment operator 
     243//ViewPointDrivenSimplifier& operator =(const ViewPointDrivenSimplifier&); 
     244 
     245/// 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. 
     246void ViewPointDrivenSimplifier::Simplify(Real percent) 
     247{ 
     248        // TODO: parametros libreria globales 
     249 
     250 
     251        VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * percent); 
     252 
     253        if ((VMI::numDemandedTriangles == 0) 
     254                        || 
     255                        (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles)) 
     256        { 
     257                printf("Illegal number of triangles.\n"); 
     258        } 
     259 
     260        VMI::display(); 
     261 
     262        //      Load a geometry mesh for vmi mesh. 
     263        loadMesh(); 
     264} 
     265 
     266//--------------------------------------------------------------------------- 
     267/// 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. 
     268//--------------------------------------------------------------------------- 
     269void ViewPointDrivenSimplifier::Simplify(uint32 numVertices) 
     270{ 
     271        float   percent; 
     272 
     273        percent =       (numVertices    *       100.0) / VMI::mesh->numVertices; 
     274 
     275        VMI::numDemandedTriangles = (int)(VMI::mesh->numTriangles * (percent / 100)); 
     276 
     277        if ((VMI::numDemandedTriangles == 0) 
     278                        || 
     279                        (VMI::numDemandedTriangles >= VMI::mesh->currentNumTriangles)) 
     280        { 
     281                printf("Illegal number of triangles.\n"); 
     282        } 
     283 
     284        VMI::display(); 
     285 
     286        //      Load a geometry mesh for vmi mesh. 
     287        loadMesh(); 
     288 
     289} 
     290 
     291// Returns the simplified mesh. 
     292//Mesh  *       ViewPointDrivenSimplifier::GetMesh() 
     293//{ 
     294//      return mGeoMesh; 
     295//} 
     296 
     297//--------------------------------------------------------------------------- 
     298//      Init vmi mesh structure for a geometry mesh given. 
     299//--------------------------------------------------------------------------- 
     300VMI::Mesh *     ViewPointDrivenSimplifier::initMeshStructure(Mesh       *geoMesh) 
     301{ 
     302        GLuint                          i, j, v1, v2, v3, n, e, t; 
     303        VMI::Mesh               *vmi_mesh; 
     304        SubMesh                         *geosubmesh; 
     305        VertexBuffer    *vertex_buffer; 
     306        int                                             mesh_index_count; 
     307        int                                             index; 
     308 
     309        //      Reallocate memory for vmi mesh object. 
     310        vmi_mesh = (VMI::Mesh *)malloc(sizeof(VMI::Mesh)); 
     311 
     312        if (vmi_mesh == NULL) 
     313        { 
     314                fprintf(stderr, "Error allocating memory\n"); 
     315                exit(1); 
     316        } 
     317 
     318        //      Shared vertex buffer. 
     319        vertex_buffer   =       geoMesh->mVertexBuffer; 
     320         
     321        //      Reallocate memory for vertices of mesh. 
     322        vmi_mesh->vertices = (VMI::Vertex *)malloc(sizeof(VMI::Vertex) 
     323                                                                                                                                                                * 
     324                                                                                                                                                                vertex_buffer->mVertexCount); 
     325 
     326        if (vmi_mesh->vertices == NULL) 
     327        { 
     328                fprintf(stderr, "Error allocating memory\n"); 
     329                exit(1); 
     330        } 
     331 
     332        //      Initialize mesh index count. 
     333        mesh_index_count        =       0; 
     334         
     335        for (int submesh        =       0; submesh < mGeoMesh->mSubMeshCount; submesh++) 
     336        { 
     337                mesh_index_count        +=      mGeoMesh->mSubMesh[submesh].mIndexCount; 
     338        } 
     339         
     340        //      Reallocate memory for indices. 
     341        vmi_mesh->triangles = (VMI::Triangle *)malloc(sizeof(VMI::Triangle) 
     342                                                                                                                                                                                * 
     343                                                                                                                                                                                (mesh_index_count / 3)); 
     344 
     345        if (vmi_mesh->triangles == NULL) 
     346        { 
     347                fprintf(stderr, "Error allocating memory\n"); 
     348                exit(1); 
     349        } 
     350 
     351        printf("Adding vertices..."); 
     352         
     353        //      Fill up vertices. 
     354        for (i = 0; i < vertex_buffer->mVertexCount; i++) 
     355        { 
     356                // Vertices start at 0. 
     357                vmi_mesh->vertices[i].x = vertex_buffer->mPosition[i].x; 
     358                vmi_mesh->vertices[i].y = vertex_buffer->mPosition[i].y; 
     359                vmi_mesh->vertices[i].z = vertex_buffer->mPosition[i].z; 
     360                 
     361                vmi_mesh->vertices[i].numTriangles = 0; 
     362                vmi_mesh->vertices[i].triangles = NULL; 
     363                vmi_mesh->vertices[i].enable = GL_TRUE; 
     364        } 
     365         
     366        printf("Ok\n"); 
     367        vmi_mesh->numVertices = vertex_buffer->mVertexCount; 
     368        vmi_mesh->currentNumVertices = vertex_buffer->mVertexCount; 
     369 
     370        //      Fill up triangles. 
     371        printf("Adding triangles..."); 
     372         
     373        //      Initialize index of triangle. 
     374        index   =       0; 
     375         
     376        //      For each submesh. 
     377        for (int submesh        =       0; submesh      <       mGeoMesh->mSubMeshCount; submesh++) 
     378        { 
     379                //      Gets actual submesh. 
     380                geosubmesh      =       &mGeoMesh->mSubMesh[submesh]; 
     381                 
     382                for (i = 0; i < (geosubmesh->mIndexCount / 3); i++) 
     383                { 
     384                        vmi_mesh->triangles[index].id                           = index; 
     385                        vmi_mesh->triangles[index].submesh              = submesh; 
     386                        vmi_mesh->triangles[index].indices[0] = geosubmesh->mIndex[(3 * i)]; 
     387                        vmi_mesh->triangles[index].indices[1] = geosubmesh->mIndex[(3 * i) + 1]; 
     388                        vmi_mesh->triangles[index].indices[2] = geosubmesh->mIndex[(3 * i) + 2]; 
     389 
     390                        vmi_mesh->triangles[index].area = computeTriangleArea(vmi_mesh->vertices,  
     391                                        &vmi_mesh->triangles[index]); 
     392                        //printf("\n%d a: %f",index , vmi_mesh->triangles[index].area); 
     393                        computeTriangleNormal(vmi_mesh->vertices, &vmi_mesh->triangles[index]); 
     394 
     395                        vmi_mesh->triangles[index].saliency = 0.0; 
     396 
     397                        vmi_mesh->triangles[index].enable = GL_TRUE; 
     398 
     399                        for (j = 0; j < 3; j++) 
     400                        { 
     401                                // Adding triangle index adjacent to 3 vertices 
     402                                v1 = vmi_mesh->triangles[index].indices[j]; 
     403 
     404                                // Reallocate memory for the new adjacent triangle 
     405                                vmi_mesh->vertices[v1].triangles = (GLuint *)realloc(vmi_mesh->vertices[v1].triangles, (vmi_mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); 
     406                                VMI::addItem((int *)vmi_mesh->vertices[v1].triangles, (int *)&vmi_mesh->vertices[v1].numTriangles, index); 
     407                        } 
     408 
     409                        //      Increments triangle count. 
     410                        index++; 
     411                } 
     412        } 
     413         
     414        //printf("\n"); 
     415        printf("Ok\n"); 
     416 
     417        vmi_mesh->numTriangles                          = mesh_index_count / 3; 
     418        vmi_mesh->currentNumTriangles = mesh_index_count / 3; 
     419 
     420        printf("Num Triangles: %d\n",vmi_mesh->numTriangles); 
     421         
     422        // E = 3 T / 2 
     423        vmi_mesh->edges = (VMI::Edge *)malloc(sizeof(VMI::Edge) 
     424                                                                                                                                                * 
     425                                                                                                                                                vmi_mesh->numTriangles * 3); 
     426 
     427        if (vmi_mesh->edges == NULL) 
     428        { 
     429                fprintf(stderr, "Error allocating memory\n"); 
     430                exit(1); 
     431        } 
     432 
     433        // Init edges 
     434        for (i=0; i<vmi_mesh->numTriangles * 3; i++) 
     435        { 
     436                vmi_mesh->edges[i].triangles    = NULL; 
     437                vmi_mesh->edges[i].numTriangles = 0; 
     438        } 
     439 
     440        printf("Adding edges..."); 
     441        n = 0; 
     442 
     443        for (i=0; i<vmi_mesh->numTriangles; i++) 
     444        { 
     445                t               =       0; 
     446                v1      =       vmi_mesh->triangles[i].indices[0]; 
     447                v2      =       vmi_mesh->triangles[i].indices[1]; 
     448                v3      =       vmi_mesh->triangles[i].indices[2]; 
     449 
     450                if ((e = findEdge(vmi_mesh->edges, n, v1, v2)) == -1) 
     451                { 
     452                        vmi_mesh->edges[n].u                    =       v1; 
     453                        vmi_mesh->edges[n].v                    =       v2; 
     454                        vmi_mesh->edges[n].enable =     GL_TRUE; 
     455 
     456                        // Reallocate memory for the new adjacent triangle 
     457                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
     458                        // Adding triangle i adjacent to edge n 
     459                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); 
     460                        //printf("n:%d i:%d\n", n, i); 
     461 
     462                        // Adding edge n adjacent to triangle i 
     463                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); 
     464                        n++; 
     465                } 
     466                else 
     467                { 
     468                        // Reallocate memory for the new adjacent triangle. 
     469                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
     470                        // Adding triangle i adjacent to edge e 
     471                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); 
     472                        //printf("n:%d i:%d\n", e, i); 
     473 
     474                        // Adding edge e adjacent to triangle i 
     475                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); 
     476                } 
     477                if ((e = findEdge(vmi_mesh->edges, n, v2, v3)) == -1) 
     478                { 
     479                        vmi_mesh->edges[n].u = v2; 
     480                        vmi_mesh->edges[n].v = v3; 
     481                        vmi_mesh->edges[n].enable = GL_TRUE; 
     482 
     483                        // Reallocate memory for the new adjacent triangle 
     484                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
     485                        // Adding triangle i adjacent to edge n 
     486                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); 
     487                        //printf("n:%d i:%d\n", n, i); 
     488 
     489                        // Adding edge n adjacent to triangle i 
     490                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); 
     491                        n++; 
     492                } 
     493                else 
     494                { 
     495                        // Reallocate memory for the new adjacent triangle 
     496                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
     497                        // Adding triangle i adjacent to edge e 
     498                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); 
     499                        //printf("n:%d i:%d\n", e, i); 
     500 
     501                        // Adding edge e adjacent to triangle i 
     502                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); 
     503                } 
     504                 
     505                if ((e = findEdge(vmi_mesh->edges, n, v3, v1)) == -1) 
     506                { 
     507                        vmi_mesh->edges[n].u = v3; 
     508                        vmi_mesh->edges[n].v = v1; 
     509                        vmi_mesh->edges[n].enable = GL_TRUE; 
     510 
     511                        // Reallocate memory for the new adjacent triangle 
     512                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
     513                        // Adding triangle i adjacent to edge n 
     514                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); 
     515                        //printf("n:%d i:%d\n", n, i); 
     516 
     517                        // Adding edge n adjacent to triangle i 
     518                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, n); 
     519                        n++; 
     520                } 
     521                else 
     522                { 
     523                        // Reallocate memory for the new adjacent triangle 
     524                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
     525                        // Adding triangle i adjacent to edge e 
     526                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); 
     527                        //printf("n:%d i:%d\n", e, i); 
     528 
     529                        // Adding edge e adjacent to triangle i 
     530                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); 
     531                } 
     532        } 
     533         
     534        printf("Ok\n"); 
     535        vmi_mesh->numEdges = n; 
     536 
     537        return vmi_mesh; 
     538} 
     539 
     540//--------------------------------------------------------------------------- 
     541//      Gets the geometry mesh of a  the vmi mesh given. 
     542//--------------------------------------------------------------------------- 
     543void    ViewPointDrivenSimplifier::loadMesh() 
     544{ 
     545        int                                             num_indices; 
     546        SubMesh                         *geosubmesh; 
     547        VertexBuffer    *vertex_buffer; 
     548         
     549        //      Gets old vertex buffer. 
     550        vertex_buffer   =       mGeoMesh->mVertexBuffer; 
     551         
     552        //      Initialize auxiliar vertex buffer. 
     553        mVB     =       new     VertexBuffer(); 
     554         
     555        mVB->mPosition          =       new     Vector3[VMI::mesh->currentNumVertices]; 
     556        mVB->mNormal                    =       new     Vector3[VMI::mesh->currentNumVertices]; 
     557        mVB->mTexCoords         =       new     Vector2[VMI::mesh->currentNumVertices]; 
     558 
     559        mVB->mVertexInfo        =       vertex_buffer->mVertexInfo; 
     560 
     561        //      For each submesh. 
     562        for (int submesh        =       0; submesh < mGeoMesh->mSubMeshCount; submesh++) 
     563        { 
     564                geosubmesh      =       &mGeoMesh->mSubMesh[submesh]; 
     565 
     566                delete  []geosubmesh->mIndex; 
     567 
     568                //      Initialize submesh index count; 
     569                num_indices     =       0; 
     570 
     571                //      For each triangle. 
     572                for (int i = 0; i < VMI::mesh->numTriangles; i++) 
     573                { 
     574                        //      If is enable and of the current submesh. 
     575                        if ((VMI::mesh->triangles[i].enable) 
     576                                        && 
     577                                        (VMI::mesh->triangles[i].submesh == submesh)) 
     578                        { 
     579                                //      Increments submesh index count. 
     580                                num_indices     +=      3; 
     581                        } 
     582                } 
     583 
     584                geosubmesh->mIndexCount =       num_indices; 
     585 
     586                geosubmesh->mIndex      =       new Index[geosubmesh->mIndexCount]; 
     587 
     588                //      Initialize number of indices. 
     589                num_indices     =       0; 
     590 
     591                //      Fill up indices. 
     592                for (int i = 0; i < VMI::mesh->numTriangles; i++) 
     593                { 
     594                        if ((VMI::mesh->triangles[i].enable) 
     595                                        && 
     596                                        (VMI::mesh->triangles[i].submesh == submesh)) 
     597                        { 
     598                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[0]; 
     599                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[1]; 
     600                                geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[2]; 
     601                        } 
     602                } 
     603 
     604                //      For each index. 
     605                for (int i = 0; i < geosubmesh->mIndexCount; i++) 
     606                { 
     607                        findVertex(submesh,i); 
     608                } 
     609 
     610                geosubmesh->mVertexBuffer       =       mVB; 
     611        } 
     612 
     613        delete  vertex_buffer; 
     614         
     615        mGeoMesh->mVertexBuffer =       mVB; 
     616 
     617} 
     618 
     619//--------------------------------------------------------------------------- 
     620//      Find vertex in auxiliar vertex buffer. 
     621//--------------------------------------------------------------------------- 
     622void    ViewPointDrivenSimplifier::findVertex(size_t    submesh, size_t elem) 
     623{ 
     624        bool                                    found; 
     625        int                                             index; 
     626        int                                             i; 
     627        int                                             new_elem; 
     628        VertexBuffer    *vertex_buffer; 
     629 
     630        found   =       false; 
     631 
     632        //      Shared vertex buffer. 
     633        vertex_buffer   =       mGeoMesh->mVertexBuffer; 
     634         
     635        index   =       mGeoMesh->mSubMesh[submesh].mIndex[elem]; 
     636         
     637        i       =       0; 
     638         
     639        while (!found && (i < mVB->mVertexCount)) 
     640        { 
     641                if ((VMI::mesh->vertices[index].x == mVB->mPosition[i].x) 
     642                                && 
     643                                (VMI::mesh->vertices[index].y == mVB->mPosition[i].y) 
     644                                && 
     645                                (VMI::mesh->vertices[index].z == mVB->mPosition[i].z)) 
     646                { 
     647                        found   =       true; 
     648 
     649                        //      Update index. 
     650                        mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        i; 
     651                } 
     652 
     653                //      Increments index. 
     654                i++; 
     655        } 
     656 
     657        if (!found) 
     658        { 
     659                //      Last element. 
     660                new_elem        =       mVB->mVertexCount; 
     661                 
     662                //      Add to last. 
     663                mVB->mPosition[new_elem].x      =       VMI::mesh->vertices[index].x; 
     664                mVB->mPosition[new_elem].y      =       VMI::mesh->vertices[index].y; 
     665                mVB->mPosition[new_elem].z      =       VMI::mesh->vertices[index].z; 
     666 
     667                mVB->mNormal[new_elem].x        =       vertex_buffer->mNormal[index].x; 
     668                mVB->mNormal[new_elem].y        =       vertex_buffer->mNormal[index].y; 
     669                mVB->mNormal[new_elem].z        =       vertex_buffer->mNormal[index].z; 
     670 
     671                mVB->mTexCoords[new_elem].x     =       vertex_buffer->mTexCoords[index].x; 
     672                mVB->mTexCoords[new_elem].y     =       vertex_buffer->mTexCoords[index].y; 
     673 
     674                //      Update index. 
     675                mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        new_elem; 
     676 
     677                //      Increments vertex count. 
     678                mVB->mVertexCount++; 
     679        } 
     680} 
     681 
Note: See TracChangeset for help on using the changeset viewer.