Changeset 2081


Ignore:
Timestamp:
02/05/07 13:24:07 (17 years ago)
Author:
gumbau
Message:
 
File:
1 edited

Legend:

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

    r1578 r2081  
    88using namespace std; 
    99 
    10 //////////////////////////////////////////////////////////////////////////// 
    11 //                                                                                                                                                                                                                                                                                              // 
    12 //                                                                                      MeshSimplifier class                                                                                                                    // 
    13 //                                                                                                                                                                                                                                                                                              // 
    14 //////////////////////////////////////////////////////////////////////////// 
    15  
    16 //--------------------------------------------------------------------------- 
     10////////////////////////////////////////////////////////////////////////// 
     11//                                                                                                                                                                                                                                                                                      // 
     12//                                                                                      MeshSimplifier class                                                                                                            // 
     13//                                                                                                                                                                                                                                                                                      // 
     14////////////////////////////////////////////////////////////////////////// 
     15 
     16//------------------------------------------------------------------------- 
    1717//      MeshSimplifier constructor. 
    18 //--------------------------------------------------------------------------- 
     18//------------------------------------------------------------------------- 
    1919MeshSimplifier::MeshSimplifier( const Mesh      *m, 
    2020                                                                                                                                TIPOFUNC                upb) 
     
    3333} 
    3434 
    35 //--------------------------------------------------------------------------- 
     35//------------------------------------------------------------------------- 
    3636//      MeshSimplifier destroyer. 
    37 //--------------------------------------------------------------------------- 
     37//------------------------------------------------------------------------- 
    3838MeshSimplifier::~MeshSimplifier() 
    3939{ 
     
    4242} 
    4343 
    44 //--------------------------------------------------------------------------- 
     44//------------------------------------------------------------------------- 
    4545// Returns the simplified mesh. 
    46 //--------------------------------------------------------------------------- 
     46//------------------------------------------------------------------------- 
    4747Mesh *  MeshSimplifier::GetMesh() 
    4848{ 
     
    5050} 
    5151 
    52 //--------------------------------------------------------------------------- 
     52//------------------------------------------------------------------------- 
    5353//      Set submesh leaves 
    54 //--------------------------------------------------------------------------- 
    55 void MeshSimplifier::setMeshLeaves(Index index) 
     54//------------------------------------------------------------------------- 
     55void MeshSimplifier::setMeshLeaves(Geometry::Index index) 
    5656{ 
    5757        indexMeshLeaves =       index; 
    5858} 
    5959 
    60 //--------------------------------------------------------------------------- 
     60//------------------------------------------------------------------------- 
    6161// Returns the simplification sequence for general meshes. 
    62 //--------------------------------------------------------------------------- 
     62//------------------------------------------------------------------------- 
    6363MeshSimplificationSequence *MeshSimplifier::GetSimplificationSequence() 
    6464{ 
     
    6666} 
    6767 
    68 //--------------------------------------------------------------------------- 
    69 //      Sort mesh bones. 
    70 //--------------------------------------------------------------------------- 
    71 void    MeshSimplifier::sortBones() 
    72 { 
     68//------------------------------------------------------------------------- 
     69//    Sort mesh bones. 
     70//------------------------------------------------------------------------- 
     71void MeshSimplifier::sortBones() 
     72{ 
     73<<<<<<< .mine 
     74        VertexBuffer    *simplified_vb; 
     75        VertexBuffer    *initial_vb; 
     76======= 
    7377        VertexBuffer                                    *mesh_vb; 
    7478        VertexBuffer                                    *copy_vb; 
    75  
     79>>>>>>> .r2080 
     80 
     81<<<<<<< .mine 
     82        std::vector<VertexBoneAssignment>       *simplified_bones; 
     83        std::vector<VertexBoneAssignment>       *initial_bones; 
     84======= 
    7685        std::vector<VertexBoneAssignment>       *mesh_bones; 
    7786        std::vector<VertexBoneAssignment>       *copy_bones; 
    78  
     87>>>>>>> .r2080 
     88 
     89<<<<<<< .mine 
     90        // After simplifying, the object always is shared vertex 
     91        // so, the bones must be placed in the GeoMesh 
     92        simplified_bones        =       &mGeoMesh->mBones; 
     93        simplified_vb                   =       mGeoMesh->mVertexBuffer; 
     94======= 
    7995        // After simplifying, the object always is shared vertex 
    8096        // so, the bones must be placed in the GeoMesh 
    8197        mesh_bones      =       &mGeoMesh->mBones; 
    8298        mesh_vb =       mGeoMesh->mVertexBuffer; 
    83  
     99>>>>>>> .r2080 
     100 
     101<<<<<<< .mine 
     102        // we assume the original mesh is shared vertex 
     103        // because before the simplification process 
     104        // the mesh becomes converted to shared vertex 
     105        // so, the original bones must be searched in the 
     106        // Mesh, not in every submesh 
     107======= 
    84108        // we assume the original mesh is shared vertex  
    85109        // because before the simplification process 
     
    87111        // so, the original bones must be searched in the 
    88112        // Mesh, not in every submesh 
    89  
     113>>>>>>> .r2080 
     114 
     115<<<<<<< .mine 
     116        simplified_bones->clear(); 
     117======= 
    90118        mesh_bones->clear(); 
    91  
     119>>>>>>> .r2080 
     120 
     121<<<<<<< .mine 
     122        //    Vertex buffers. 
     123        initial_vb      =       mInitialMesh->mVertexBuffer; 
     124======= 
    92125        //      Vertex buffers. 
    93126        copy_vb =       mInitialMesh->mVertexBuffer; 
    94  
     127>>>>>>> .r2080 
     128 
     129<<<<<<< .mine 
     130        //    Bones. 
     131        initial_bones   =       &mInitialMesh->mBones; 
     132======= 
    95133        //      Bones. 
    96134        copy_bones = &mInitialMesh->mBones; 
     
    100138        { 
    101139                VertexBoneAssignment    assign; 
    102  
     140>>>>>>> .r2080 
     141 
     142<<<<<<< .mine 
     143        //  For each bone assignment. 
     144        for (int b = 0; b < initial_bones->size(); b++) 
     145        { 
     146                VertexBoneAssignment    assign; 
     147======= 
    103148                int n   =       (*copy_bones)[b].vertexIndex; 
    104  
     149>>>>>>> .r2080 
     150 
     151<<<<<<< .mine 
     152                int n   =       (*initial_bones)[b].vertexIndex; 
     153 
     154                //      For each vertex. 
     155                for (int i      =       0;      i < simplified_vb->mVertexCount;        i++) 
     156                { 
     157                        if (simplified_vb->mPosition[i].x == initial_vb->mPosition[n].x && 
     158                                        simplified_vb->mPosition[i].y == initial_vb->mPosition[n].y && 
     159                                        simplified_vb->mPosition[i].z == initial_vb->mPosition[n].z) 
     160                        { 
     161                                assign.vertexIndex      =       i; 
     162                                assign.boneIndex                =       (*initial_bones)[b].boneIndex; 
     163                                assign.weight                           =       (*initial_bones)[b].weight; 
     164 
     165                                simplified_bones->push_back(assign); 
     166                        } 
     167======= 
    105168                //      Initialize o. 
    106169                 
     
    117180                                mesh_bones->push_back(assign); 
    118181                        }                        
    119                 } 
    120         } 
    121 } 
    122  
    123 //////////////////////////////////////////////////////////////////////////// 
    124 //                                                                                                                                                                                                                                                                                              // 
    125 //                                                                      GeometryBasedSimplifier class                                                                                                   // 
    126 //                                                                                                                                                                                                                                                                                              // 
    127 //////////////////////////////////////////////////////////////////////////// 
    128  
    129 //--------------------------------------------------------------------------- 
     182>>>>>>> .r2080 
     183                } 
     184        } 
     185} 
     186 
     187 
     188////////////////////////////////////////////////////////////////////////// 
     189//                                                                                                                                                                                                                                                                                      // 
     190//                                                                      GeometryBasedSimplifier class                                                                                           // 
     191//                                                                                                                                                                                                                                                                                      // 
     192////////////////////////////////////////////////////////////////////////// 
     193 
     194//------------------------------------------------------------------------- 
    130195//      Constructor. 
    131 //--------------------------------------------------------------------------- 
     196//------------------------------------------------------------------------- 
    132197GeometryBasedSimplifier::GeometryBasedSimplifier(       const Mesh      *m, 
    133198                                                                                                                                                                                                        TIPOFUNC                upb) 
     
    136201} 
    137202 
    138 //--------------------------------------------------------------------------- 
     203//------------------------------------------------------------------------- 
    139204//      Destroyer. 
    140 //--------------------------------------------------------------------------- 
     205//------------------------------------------------------------------------- 
    141206GeometryBasedSimplifier::~GeometryBasedSimplifier() 
    142207{ 
    143208} 
    144209 
    145 //--------------------------------------------------------------------------- 
    146 // 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. 
    147 //--------------------------------------------------------------------------- 
     210//------------------------------------------------------------------------- 
     211// Starts the simplification process. Receives as a parameter  
     212// the LOD factor in a range of [0,1]. Implements the Simplifier::Simplify 
     213// method to perform an image based simplification. 
     214//------------------------------------------------------------------------- 
    148215void GeometryBasedSimplifier::Simplify(Real paramlod) 
    149216{ 
    150217        SimplificationMethod *m_qslim   =       new SimplificationMethod(objmesh); 
    151          
     218 
    152219        m_qslim->setMeshLeaves(indexMeshLeaves); 
    153          
     220 
    154221        msimpsequence   =       m_qslim->Decimate(paramlod,0,mUPB); 
    155          
     222 
    156223        mGeoMesh        =       m_qslim->GetMesh(); 
    157          
     224 
     225        //      Debug. 
     226        cout    <<      "Number of bone assignaments: " 
     227                                <<      mGeoMesh->mBones.size() 
     228                                <<      endl; 
     229 
     230        // if the initial mesh had shared vertex, 
     231        // convert the mesh to shared vertex 
     232        //Mesh *sharedMesh = mGeoMesh->toSharedVertex(); 
     233        //delete mGeoMesh; 
     234        //mGeoMesh = sharedMesh; 
     235 
    158236        // if the initial mesh had shared vertex,  
    159237        // convert the mesh to shared vertex 
     
    170248 
    171249        //      Sort bones. 
    172         sortBones(); 
    173          
     250        //sortBones(); 
     251 
    174252        delete  m_qslim; 
    175253} 
    176254 
    177 //--------------------------------------------------------------------------- 
    178 // 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. 
    179 //--------------------------------------------------------------------------- 
     255//------------------------------------------------------------------------- 
     256// Starts the simplification process. Receives as a parameter the  
     257// number of vertices of the resulting mesh. 
     258// Implements the Simplifier::Simplify method to  
     259// perform an image based simplification. 
     260//------------------------------------------------------------------------- 
    180261void GeometryBasedSimplifier::Simplify(uint32 numvertices) 
    181262{ 
     
    187268 
    188269        mGeoMesh        =       m_qslim->GetMesh(); 
     270 
     271        // if the initial mesh had shared vertex, 
     272        // convert the mesh to shared vertex 
     273        //Mesh *sharedMesh = mGeoMesh->toSharedVertex(); 
     274        //delete mGeoMesh; 
     275        //mGeoMesh = sharedMesh; 
    189276         
    190277        // if the initial mesh had shared vertex,  
     
    202289 
    203290        //      Sort bones. 
    204         sortBones(); 
     291        //sortBones(); 
    205292         
    206293        delete m_qslim; 
    207294} 
    208295 
    209 //////////////////////////////////////////////////////////////////////////// 
    210 //                                                                                                                                                                                                                                                                                              // 
    211 //                                                              ViewPointDrivenSimplifier class                                                                                                 // 
    212 //                                                                                                                                                                                                                                                                                              // 
    213 //////////////////////////////////////////////////////////////////////////// 
    214  
    215 //--------------------------------------------------------------------------- 
    216 /// Class constructor. Will call Simplifier class constructor. 
    217 //--------------------------------------------------------------------------- 
     296////////////////////////////////////////////////////////////////////////// 
     297//                                                                                                                                                                                                                                                                                      // 
     298//                                                              ViewPointDrivenSimplifier class                                                                                         // 
     299//                                                                                                                                                                                                                                                                                      // 
     300////////////////////////////////////////////////////////////////////////// 
     301 
     302//------------------------------------------------------------------------ 
     303// Class constructor. Will call Simplifier class constructor. 
     304//------------------------------------------------------------------------ 
    218305ViewPointDrivenSimplifier::ViewPointDrivenSimplifier(   const Mesh      *m, 
    219306                                                                                                                                                                                                                        TIPOFUNC                upb) 
     
    222309        //      Set progress update function 
    223310        VMI::mUPB       =       upb; 
    224          
     311 
    225312        VMI::width  = 256; 
    226313        VMI::height = 256; 
    227314 
    228         VMI::bEnableOffScreen     = GL_TRUE; 
    229         VMI::bBeQuiet             = GL_FALSE; 
    230         VMI::bSaveLog             = GL_FALSE; 
    231         VMI::bLoadCamerasFromFile = GL_FALSE; 
    232  
    233         VMI::cameraType = 2; 
     315        VMI::bEnableOffScreen                   = GL_TRUE; 
     316        VMI::bBeQuiet                           = GL_FALSE; 
     317        VMI::bSaveLog                           = GL_FALSE; 
     318        VMI::bLoadCamerasFromFile               = GL_FALSE; 
     319        VMI::bRemoveRedundantVertices = GL_TRUE; 
     320 
     321        VMI::cameraType = 3; // 20 viewpoints 
    234322        VMI::radius = 1.3; 
    235323        VMI::fov = 60.0; 
     
    243331                        VMI::radius); 
    244332 
     333        //      Fill up attributes. 
     334        fillUpPosNorTC(mInitialMesh); 
     335 
     336        VMI::vPositions =       vPositions; 
     337        VMI::vNormals           =       vNormals; 
     338        VMI::vTexCoords =       vTexCoords; 
    245339 
    246340        mGeoMesh        =       new Mesh(); 
    247          
     341 
    248342        *mGeoMesh       =       *m; 
    249          
     343 
    250344        //      Transform NoSV Mesh to a SV Mesh. 
    251         mGeoMesh        =       mGeoMesh->toSharedVertex(); 
     345        //mGeoMesh      =       mGeoMesh->toSharedVertex(); 
     346         
     347        //      Join Vertices. 
     348        mTexConserver   =        new    TextureConserver(); 
     349        mTexConserver->JoinVertices(mGeoMesh); 
    252350 
    253351        //      Loads the vmi mesh structure for a geometry mesh given. 
    254352        VMI::mesh = initMeshStructure(mGeoMesh); 
    255          
     353 
    256354        // RGB and Alpha. 
    257         glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA);  
     355        glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA); 
    258356        glutInitWindowSize(VMI::width, VMI::height); 
    259357        glutInitWindowPosition(100, 100); 
     
    266364 
    267365        if (VMI::bLoadCamerasFromFile == GL_FALSE) 
    268         { 
    269                 switch (VMI::cameraType) 
    270                 { 
    271                         case 0: 
    272                                 VMI::cameras = VMI::setCameras(VMI::radius, OCTAHEDRON, &VMI::numCameras); 
    273                                 printf("Number of cameras: %d\n", OCTAHEDRON); 
    274                                 break; 
    275                         case 1: 
    276                                 VMI:: cameras = VMI::setCameras(VMI::radius, ICOSAHEDRON, &VMI::numCameras); 
    277                                 printf("Number of cameras: %d\n", ICOSAHEDRON); 
    278                                 break; 
    279                         case 2: 
    280                                 VMI::cameras = VMI::setCameras(VMI::radius, DODECAHEDRON, &VMI::numCameras); 
    281                                 printf("Number of cameras: %d\n", DODECAHEDRON); 
    282                                 break; 
    283                         default: 
    284                                 break; 
    285                 } 
    286         } 
     366                VMI::cameras = VMI::setCameras(VMI::radius, VMI::cameraType, &VMI::numCameras); 
    287367 
    288368        VMI::histogram  =       VMI::initHistogram(     VMI::mesh->currentNumTriangles, 
    289                                                                                                                                                                 VMI::numCameras); 
     369                        VMI::numCameras); 
    290370 
    291371        VMI::initialIs  =       VMI::initIs(VMI::numCameras); 
    292372} 
    293373 
    294 //--------------------------------------------------------------------------- 
     374//------------------------------------------------------------------------- 
    295375/// Class destructor. 
    296 //--------------------------------------------------------------------------- 
     376//------------------------------------------------------------------------- 
    297377ViewPointDrivenSimplifier::~ViewPointDrivenSimplifier(void) 
    298378{ 
    299379        // Free memory 
    300          VMI::freeMemory(); 
    301 } 
    302  
    303 /// Copy constructor 
    304 //ViewPointDrivenSimplifier(const ViewPointDrivenSimplifier&); 
    305  
    306 /// Assignment operator 
    307 //ViewPointDrivenSimplifier& operator =(const ViewPointDrivenSimplifier&); 
    308  
    309 /// 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. 
     380        VMI::freeMemory(); 
     381} 
     382 
     383/// Starts the simplification process. Receives as a parameter the  
     384///     LOD factor in a range of [0,1]. Implements the  
     385///     Simplifier::Simplify method to perform an image based simplification. 
    310386void ViewPointDrivenSimplifier::Simplify(Real percent) 
    311387{ 
     
    321397        VMI::display(); 
    322398 
     399        //      Cleans firts simplification. 
     400        VMI::freeMemory(); 
     401 
     402        VMI::mesh =     initMeshStructure(mInitialMesh); 
     403 
     404        VMI::contractInitialMesh(VMI::mesh); 
     405 
    323406        //      Load a geometry mesh for vmi mesh. 
    324407        loadMesh(); 
     408         
     409        GetMeshSimpSequence(); 
    325410 
    326411        //      Sort bones. 
    327         sortBones(); 
    328          
    329         GetMeshSimpSequence(); 
    330 } 
    331  
    332 //--------------------------------------------------------------------------- 
    333 /// 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. 
    334 //--------------------------------------------------------------------------- 
     412        bonesReassignament(); 
     413} 
     414 
     415//------------------------------------------------------------------------- 
     416/// Starts the simplification process. Receives as a parameter  
     417///     the number of vertices of the resulting mesh. 
     418/// Implements the Simplifier::Simplify method to perform  
     419///     an image based simplification. 
     420//------------------------------------------------------------------------- 
    335421void ViewPointDrivenSimplifier::Simplify(uint32 numVertices) 
    336422{ 
     
    350436        VMI::display(); 
    351437 
     438        //      Cleans firts simplification. 
     439        VMI::freeMemory(); 
     440 
     441        VMI::mesh =     initMeshStructure(mInitialMesh); 
     442 
     443        VMI::contractInitialMesh(VMI::mesh); 
     444 
    352445        //      Load a geometry mesh for vmi mesh. 
    353446        loadMesh(); 
     447         
     448        GetMeshSimpSequence(); 
    354449 
    355450        //      Sort bones. 
    356         sortBones(); 
    357          
    358         GetMeshSimpSequence(); 
    359 } 
    360  
    361 // Returns the simplified mesh. 
    362 //Mesh  *       ViewPointDrivenSimplifier::GetMesh() 
    363 //{ 
    364 //      return mGeoMesh; 
    365 //} 
    366  
    367 //--------------------------------------------------------------------------- 
     451        bonesReassignament(); 
     452} 
     453 
     454//------------------------------------------------------------------------- 
    368455//      Gets the VMI mesh simplification sequence. 
    369 //--------------------------------------------------------------------------- 
     456//------------------------------------------------------------------------- 
    370457void    ViewPointDrivenSimplifier::GetMeshSimpSequence() 
    371458{ 
    372         unsigned        int j = 0; 
     459        size_t  j = 0; 
    373460         
    374461        MeshSimplificationSequence::Step        current_step; 
    375462         
    376463        msimpsequence   =       new MeshSimplificationSequence(); 
    377          
    378         //      For each simplification step. 
    379         for (unsigned   int i = 0;      i < VMI::mVMISteps.size();      i++) 
    380         { 
    381                 current_step.mV0        =       VMI::mVMISteps[i].mV0; 
    382                 current_step.mV1        =       VMI::mVMISteps[i].mV1; 
    383                 current_step.mT0        =       VMI::mVMISteps[i].mT0; 
    384                 current_step.mT1        =       VMI::mVMISteps[i].mT1; 
    385                  
    386                 current_step.x  =       VMI::mVMISteps[i].x; 
    387                 current_step.y  =       VMI::mVMISteps[i].y; 
    388                 current_step.z  =       VMI::mVMISteps[i].z; 
    389  
    390                 current_step.obligatory =       VMI::mVMISteps[i].obligatory; 
    391  
    392                 //      For each face modificated. 
    393                 while (j < VMI::mVMISteps[i].mModfaces.size()) 
    394                 { 
    395                         current_step.mModfaces.push_back(VMI::mVMISteps[i].mModfaces[j]); 
    396                          
    397                         j++; 
    398                 } 
    399                  
    400                 msimpsequence->mSteps.push_back(current_step); 
    401         } 
    402 } 
    403  
    404 //--------------------------------------------------------------------------- 
    405 //      Class for join triangle holes. 
    406 //--------------------------------------------------------------------------- 
    407 class _coord_ 
    408 { 
    409         public: 
    410                 float x,y,z; 
    411                 inline _coord_( float x =       0.0f, 
    412                                                                                 float y =       0.0f, 
    413                                                                                 float z =       0.0f) 
    414                 { this->x = x; this->y = y; this->z = z; } 
    415                  
    416                 inline _coord_(const _coord_ &f) 
    417                 { 
    418                         x       =       f.x; 
    419                         y=f.y; 
    420                         z=f.z; 
    421                 } 
    422                  
    423                 inline _coord_ & operator=(const _coord_ &f) 
    424                 { 
    425                         x       =       f.x; 
    426                         y       =       f.y; 
    427                         z       =       f.z; 
    428                          
    429                         return *this; 
    430                 } 
    431                  
    432                 inline bool operator<(const _coord_ &f) const 
    433                 { 
    434                         if (x<f.x-0.0001) return true; 
    435                         if (x>f.x+0.0001) return false; 
    436                         if (y<f.y-0.0001) return true; 
    437                         if (y>f.y+0.0001) return false; 
    438                         if (z<f.z-0.0001) return true; 
    439                         if (z>f.z+0.0001) return false; 
    440                          
    441                         return  false; 
    442                 } 
    443 }; 
    444  
    445 //--------------------------------------------------------------------------- 
     464 
     465        //      Copy the mesh simplification sequence. 
     466        msimpsequence->mSteps                           =       VMI::mSequence->mSteps; 
     467        msimpsequence->mNewVertices     =       VMI::mSequence->mNewVertices; 
     468} 
     469 
     470//------------------------------------------------------------------------- 
    446471//      Initialize the mesh of VMI module. 
    447 //--------------------------------------------------------------------------- 
     472//------------------------------------------------------------------------- 
    448473VMI::Mesh *     ViewPointDrivenSimplifier::initMeshStructure(Mesh       *geoMesh) 
    449474{ 
     
    481506        mesh_index_count        =       0; 
    482507         
    483         for (unsigned int submesh       =       0; submesh < mGeoMesh->mSubMeshCount; submesh++) 
    484         { 
    485                 mesh_index_count        +=      int(mGeoMesh->mSubMesh[submesh].mIndexCount); 
     508        for (   unsigned int submesh    =       0; 
     509                                submesh < geoMesh->mSubMeshCount; 
     510                                submesh++) 
     511        { 
     512                mesh_index_count        +=      int(geoMesh->mSubMesh[submesh].mIndexCount); 
    486513        } 
    487514         
     
    498525 
    499526        printf("Adding vertices..."); 
     527 
    500528        //      Fill up vertices. 
    501         std::map<_coord_, int> uniquevertices; 
    502         std::map<int, int> newindices; 
    503529        for (i = 0; i < vertex_buffer->mVertexCount; i++) 
    504530        { 
    505                 _coord_ vertex_aux; 
    506                 vertex_aux.x= vertex_buffer->mPosition[i].x; 
    507                 vertex_aux.y= vertex_buffer->mPosition[i].y; 
    508                 vertex_aux.z= vertex_buffer->mPosition[i].z; 
    509                 //New index 
    510                 if (uniquevertices.find(vertex_aux)==uniquevertices.end()) 
    511                 { 
    512                         uniquevertices[vertex_aux]= i; 
    513                         newindices[i]= i; 
    514  
    515                         VMI::INTVECTOR intvectoraux; 
    516                         intvectoraux.push_back(i); 
    517                         VMI::inversemap[i]= intvectoraux; 
    518  
    519                 }else//The map of unique vertices already contains this vertex 
    520                 { 
    521                         int newindex= uniquevertices[vertex_aux]; 
    522                         newindices[i]= newindex; 
    523  
    524                         VMI::inversemap[newindex].push_back(i);; 
    525                 } 
    526  
    527531                // Vertices start at 0. 
    528532                vmi_mesh->vertices[i].x = vertex_buffer->mPosition[i].x; 
     
    532536                vmi_mesh->vertices[i].numTriangles = 0; 
    533537                vmi_mesh->vertices[i].triangles = NULL; 
    534                 vmi_mesh->vertices[i].enable = GL_TRUE; 
     538                vmi_mesh->vertices[i].enable    = GL_TRUE; 
     539                vmi_mesh->vertices[i].movable = GL_TRUE; 
    535540        } 
    536541         
     
    539544        vmi_mesh->currentNumVertices = int(vertex_buffer->mVertexCount); 
    540545 
     546        printf("Vertices: %d\n",vmi_mesh->numVertices); 
     547 
    541548        //      Fill up triangles. 
    542549        printf("Adding triangles..."); 
     
    546553         
    547554        //      For each submesh. 
    548         for (unsigned int submesh       =       0; submesh      <       mGeoMesh->mSubMeshCount; submesh++) 
     555        for (   unsigned int submesh    =       0; 
     556                                submesh <       geoMesh->mSubMeshCount; 
     557                                submesh++) 
    549558        { 
    550559                //      Gets actual submesh. 
    551                 geosubmesh      =       &mGeoMesh->mSubMesh[submesh]; 
     560                geosubmesh      =       &geoMesh->mSubMesh[submesh]; 
    552561                 
     562                //      For each three vertices. 
    553563                for (i = 0; i < (geosubmesh->mIndexCount / 3); i++) 
    554564                { 
    555565                        vmi_mesh->triangles[index].id                           = index; 
    556566                        vmi_mesh->triangles[index].submesh              = submesh; 
    557                         vmi_mesh->triangles[index].indices[0] = newindices[geosubmesh->mIndex[(3 * i)]]; 
    558                         vmi_mesh->triangles[index].indices[1] = newindices[geosubmesh->mIndex[(3 * i) + 1]]; 
    559                         vmi_mesh->triangles[index].indices[2] = newindices[geosubmesh->mIndex[(3 * i) + 2]]; 
     567                        vmi_mesh->triangles[index].indices[0] = geosubmesh->mIndex[(3 * i)]; 
     568                        vmi_mesh->triangles[index].indices[1] = geosubmesh->mIndex[(3 * i) + 1]; 
     569                        vmi_mesh->triangles[index].indices[2] = geosubmesh->mIndex[(3 * i) + 2]; 
    560570 
    561571                        vmi_mesh->triangles[index].area = computeTriangleArea(vmi_mesh->vertices,  
    562572                                        &vmi_mesh->triangles[index]); 
    563                         //printf("\n%d a: %f",index , vmi_mesh->triangles[index].area); 
    564573                        computeTriangleNormal(vmi_mesh->vertices, &vmi_mesh->triangles[index]); 
    565574 
     
    575584                                // Reallocate memory for the new adjacent triangle. 
    576585                                vmi_mesh->vertices[v1].triangles = (GLuint *)realloc(vmi_mesh->vertices[v1].triangles, (vmi_mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); 
    577                                 VMI::addItem((int *)vmi_mesh->vertices[v1].triangles, (int *)&vmi_mesh->vertices[v1].numTriangles, index); 
     586 
     587                                VMI::addItem(   (int *)vmi_mesh->vertices[v1].triangles, 
     588                                                                                        (int *)&vmi_mesh->vertices[v1].numTriangles, 
     589                                                                                        index); 
    578590                        } 
    579591 
     
    601613        } 
    602614 
    603         // Init edges 
    604         for (i=0; i<vmi_mesh->numTriangles * 3; i++) 
    605         { 
    606                 vmi_mesh->edges[i].triangles    = NULL; 
    607                 vmi_mesh->edges[i].numTriangles = 0; 
    608         } 
    609  
    610615        printf("Adding edges..."); 
    611616        n = 0; 
    612617 
    613         for (i=0; i<vmi_mesh->numTriangles; i++) 
     618        //      For each triangle adds three edges. 
     619        for (i  =       0; i < vmi_mesh->numTriangles; i++) 
    614620        { 
    615621                t               =       0; 
     
    620626                if ((e = findEdge(vmi_mesh->edges, n, v1, v2)) == -1) 
    621627                { 
    622                         vmi_mesh->edges[n].u                    =       v1; 
    623                         vmi_mesh->edges[n].v                    =       v2; 
    624                         vmi_mesh->edges[n].enable =     GL_TRUE; 
     628                        vmi_mesh->edges[n].u                                            =       v1; 
     629                        vmi_mesh->edges[n].v                                            =       v2; 
     630                        vmi_mesh->edges[n].triangles    = NULL; 
     631                        vmi_mesh->edges[n].numTriangles = 0; 
     632                        vmi_mesh->edges[n].enable                       =       GL_TRUE; 
    625633 
    626634                        // Reallocate memory for the new adjacent triangle 
    627635                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
     636 
    628637                        // Adding triangle i adjacent to edge n 
    629                         VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); 
    630                         //printf("n:%d i:%d\n", n, i); 
     638                        VMI::addItem(   (int *)vmi_mesh->edges[n].triangles, 
     639                                                                                (int *)&vmi_mesh->edges[n].numTriangles, 
     640                                                                                i); 
    631641 
    632642                        // Adding edge n adjacent to triangle i 
     
    638648                        // Reallocate memory for the new adjacent triangle. 
    639649                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
     650 
    640651                        // Adding triangle i adjacent to edge e 
    641                         VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); 
    642                         //printf("n:%d i:%d\n", e, i); 
     652                        VMI::addItem(   (int *)vmi_mesh->edges[e].triangles, 
     653                                                                                (int *)&vmi_mesh->edges[e].numTriangles, 
     654                                                                                i); 
    643655 
    644656                        // Adding edge e adjacent to triangle i 
    645657                        VMI::addItem((int *)vmi_mesh->triangles[i].edges, (int *)&t, e); 
    646658                } 
     659 
    647660                if ((e = findEdge(vmi_mesh->edges, n, v2, v3)) == -1) 
    648661                { 
    649                         vmi_mesh->edges[n].u = v2; 
    650                         vmi_mesh->edges[n].v = v3; 
    651                         vmi_mesh->edges[n].enable = GL_TRUE; 
     662                        vmi_mesh->edges[n].u                                            = v2; 
     663                        vmi_mesh->edges[n].v                                            = v3; 
     664                        vmi_mesh->edges[n].triangles    = NULL; 
     665                        vmi_mesh->edges[n].numTriangles = 0; 
     666                        vmi_mesh->edges[n].enable                       = GL_TRUE; 
    652667 
    653668                        // Reallocate memory for the new adjacent triangle 
    654669                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
     670 
    655671                        // Adding triangle i adjacent to edge n 
    656672                        VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); 
    657                         //printf("n:%d i:%d\n", n, i); 
     673                        vmi_mesh->edges[n].triangles    = NULL; 
     674                        vmi_mesh->edges[n].numTriangles = 0; 
    658675 
    659676                        // Adding edge n adjacent to triangle i 
     
    665682                        // Reallocate memory for the new adjacent triangle 
    666683                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
     684 
    667685                        // Adding triangle i adjacent to edge e 
    668                         VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); 
    669                         //printf("n:%d i:%d\n", e, i); 
     686                        VMI::addItem(   (int *)vmi_mesh->edges[e].triangles, 
     687                                                                                (int *)&vmi_mesh->edges[e].numTriangles, 
     688                                                                                i); 
    670689 
    671690                        // Adding edge e adjacent to triangle i 
     
    675694                if ((e = findEdge(vmi_mesh->edges, n, v3, v1)) == -1) 
    676695                { 
    677                         vmi_mesh->edges[n].u = v3; 
    678                         vmi_mesh->edges[n].v = v1; 
    679                         vmi_mesh->edges[n].enable = GL_TRUE; 
    680  
    681                         // Reallocate memory for the new adjacent triangle 
     696                        vmi_mesh->edges[n].u                                            = v3; 
     697                        vmi_mesh->edges[n].v                                            = v1; 
     698                        vmi_mesh->edges[n].triangles    = NULL; 
     699                        vmi_mesh->edges[n].numTriangles = 0; 
     700                        vmi_mesh->edges[n].enable                       = GL_TRUE; 
     701 
     702                        // Reallocate memory for the new adjacent triangle. 
    682703                        vmi_mesh->edges[n].triangles = (GLuint *)realloc(vmi_mesh->edges[n].triangles, (vmi_mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
     704 
    683705                        // Adding triangle i adjacent to edge n 
    684                         VMI::addItem((int *)vmi_mesh->edges[n].triangles, (int *)&vmi_mesh->edges[n].numTriangles, i); 
    685                         //printf("n:%d i:%d\n", n, i); 
     706                        VMI::addItem(   (int *)vmi_mesh->edges[n].triangles, 
     707                                                                                (int *)&vmi_mesh->edges[n].numTriangles, 
     708                                                                                i); 
    686709 
    687710                        // Adding edge n adjacent to triangle i 
     
    693716                        // Reallocate memory for the new adjacent triangle 
    694717                        vmi_mesh->edges[e].triangles = (GLuint *)realloc(vmi_mesh->edges[e].triangles, (vmi_mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
     718 
    695719                        // Adding triangle i adjacent to edge e 
    696720                        VMI::addItem((int *)vmi_mesh->edges[e].triangles, (int *)&vmi_mesh->edges[e].numTriangles, i); 
    697                         //printf("n:%d i:%d\n", e, i); 
    698721 
    699722                        // Adding edge e adjacent to triangle i 
     
    705728        vmi_mesh->numEdges = n; 
    706729 
     730        //      Creates vertex multimap. 
     731        initVertexMultimap(vmi_mesh,mTexConserver->mVertices); 
     732 
    707733        return vmi_mesh; 
    708734} 
    709735 
    710 //--------------------------------------------------------------------------- 
     736//------------------------------------------------------------------------- 
    711737//      Gets the geometry mesh of a  the vmi mesh given. 
    712 //--------------------------------------------------------------------------- 
     738//------------------------------------------------------------------------- 
    713739void    ViewPointDrivenSimplifier::loadMesh() 
    714740{ 
     
    730756 
    731757        //      For each submesh. 
    732         for (unsigned int submesh       =       0; submesh < mGeoMesh->mSubMeshCount; submesh++) 
     758        for (size_t     submesh =       0; submesh < mGeoMesh->mSubMeshCount; submesh++) 
    733759        { 
    734760                geosubmesh      =       &mGeoMesh->mSubMesh[submesh]; 
     
    740766 
    741767                //      For each triangle. 
    742                 for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++) 
     768                for (size_t     i = 0; i < VMI::mesh->numTriangles; i++) 
    743769                { 
    744770                        //      If is enable and of the current submesh. 
     
    754780                geosubmesh->mIndexCount =       num_indices; 
    755781 
    756                 geosubmesh->mIndex      =       new Index[geosubmesh->mIndexCount]; 
     782                geosubmesh->mIndex      =       new Geometry::Index[geosubmesh->mIndexCount]; 
    757783 
    758784                //      Initialize number of indices. 
     
    760786 
    761787                //      Fill up indices. 
    762                 for (unsigned int i = 0; i < VMI::mesh->numTriangles; i++) 
     788                for (size_t     i = 0; i < VMI::mesh->numTriangles; i++) 
    763789                { 
    764790                        if ((VMI::mesh->triangles[i].enable) 
     
    766792                                        (VMI::mesh->triangles[i].submesh == submesh)) 
    767793                        { 
    768                                 geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[0]; 
    769                                 geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[1]; 
    770                                 geosubmesh->mIndex[num_indices++]       =       VMI::mesh->triangles[i].indices[2]; 
     794                                geosubmesh->mIndex[num_indices++]       = 
     795                                                                                                                                        VMI::mesh->triangles[i].indices[0]; 
     796 
     797                                geosubmesh->mIndex[num_indices++]       = 
     798                                                                                                                                        VMI::mesh->triangles[i].indices[1]; 
     799 
     800                                geosubmesh->mIndex[num_indices++]       = 
     801                                                                                                                                        VMI::mesh->triangles[i].indices[2]; 
    771802                        } 
    772803                } 
    773  
     804         
    774805                //      For each index. 
    775                 for (unsigned int i = 0; i < geosubmesh->mIndexCount; i++) 
     806                for (size_t     i = 0; i < geosubmesh->mIndexCount; i++) 
    776807                { 
    777808                        findVertex(submesh,i); 
     
    784815         
    785816        mGeoMesh->mVertexBuffer =       mVB; 
    786  
    787 } 
    788  
    789 //--------------------------------------------------------------------------- 
     817} 
     818 
     819//------------------------------------------------------------------------- 
    790820//      Find vertex in auxiliar vertex buffer. 
    791 //--------------------------------------------------------------------------- 
     821//------------------------------------------------------------------------- 
    792822void    ViewPointDrivenSimplifier::findVertex(size_t    submesh, size_t elem) 
    793823{ 
    794         bool                                    found; 
    795         int                                             index; 
    796         unsigned int                    i; 
    797         int                                             new_elem; 
    798         VertexBuffer    *vertex_buffer; 
     824        bool                                                                            found; 
     825        int                                                                                     index; 
     826        unsigned int                                            i; 
     827        int                                                                                     new_elem; 
     828        VertexBuffer                                            *vertex_buffer; 
     829        map<int,int>::iterator  it; 
    799830 
    800831        found   =       false; 
    801832 
    802833        //      Shared vertex buffer. 
    803         vertex_buffer   =       mGeoMesh->mVertexBuffer; 
    804          
     834        vertex_buffer   =       mInitialMesh->mVertexBuffer; 
     835 
    805836        index   =       mGeoMesh->mSubMesh[submesh].mIndex[elem]; 
    806          
     837 
    807838        i       =       0; 
    808          
    809         while (!found && (i < mVB->mVertexCount)) 
    810         { 
    811                 if ((VMI::mesh->vertices[index].x == mVB->mPosition[i].x) 
    812                                 && 
    813                                 (VMI::mesh->vertices[index].y == mVB->mPosition[i].y) 
    814                                 && 
    815                                 (VMI::mesh->vertices[index].z == mVB->mPosition[i].z)) 
    816                 { 
    817                         found   =       true; 
    818  
    819                         //      Update index. 
    820                         mGeoMesh->mSubMesh[submesh].mIndex[elem]        =        i; 
    821                 } 
    822  
    823                 //      Increments index. 
    824                 i++; 
    825         } 
    826  
    827         if (!found) 
    828         { 
     839 
     840        if ((it =       mIndexMap.find(index)) != mIndexMap.end()) 
     841        { 
     842                mGeoMesh->mSubMesh[submesh].mIndex[elem]        =       (*it).second; 
     843        } 
     844        else 
     845        { 
     846                mIndexMap[index]        =       int(mVB->mVertexCount); 
     847 
    829848                //      Last element. 
    830849                new_elem        =       int(mVB->mVertexCount); 
    831                  
    832                 //      Add to last. 
    833                 mVB->mPosition[new_elem].x      =       VMI::mesh->vertices[index].x; 
    834                 mVB->mPosition[new_elem].y      =       VMI::mesh->vertices[index].y; 
    835                 mVB->mPosition[new_elem].z      =       VMI::mesh->vertices[index].z; 
    836  
    837                 mVB->mNormal[new_elem].x        =       vertex_buffer->mNormal[index].x; 
    838                 mVB->mNormal[new_elem].y        =       vertex_buffer->mNormal[index].y; 
    839                 mVB->mNormal[new_elem].z        =       vertex_buffer->mNormal[index].z; 
    840  
    841                 mVB->mTexCoords[new_elem].x     =       vertex_buffer->mTexCoords[index].x; 
    842                 mVB->mTexCoords[new_elem].y     =       vertex_buffer->mTexCoords[index].y; 
     850 
     851                mVB->mPosition[new_elem]        =       VMI::vPositions[index]; 
     852                mVB->mNormal[new_elem]          =       VMI::vNormals[index]; 
     853                mVB->mTexCoords[new_elem]       =       VMI::vTexCoords[index]; 
    843854 
    844855                //      Update index. 
     
    850861} 
    851862 
     863//------------------------------------------------------------------------- 
     864//      Reassigns bones. 
     865//------------------------------------------------------------------------- 
     866void ViewPointDrivenSimplifier::bonesReassignament() 
     867{ 
     868        size_t                                                                  vertex_id; 
     869        size_t                                                                  bones_count; 
     870        bool                                                                            vertex_found; 
     871        map<int,int>::iterator  im; 
     872        VertexBoneAssignment            bone; 
     873 
     874        vector<VertexBoneAssignment>::iterator  ib; 
     875 
     876        //      Copy new vertices. 
     877        for (unsigned   int     i       =       0;      i < msimpsequence->mNewVertices.size(); i++) 
     878        { 
     879                vertex_id       =       msimpsequence->mNewVertices[i].id; 
     880 
     881                //      Initialize number of bones. 
     882                bones_count     =       mInitialMesh->mBones.size(); 
     883 
     884                // check if my twin-vertex-bone has a bone assignment 
     885                // we check only the GeoMesh bones because the lodstrips 
     886                // only works for sharedvertex bones. 
     887                for (int  j = 0; j < bones_count; j++) 
     888                { 
     889                        ib      =       mInitialMesh->mBones.begin() + j; 
     890 
     891                        if (ib->vertexIndex == msimpsequence->mNewVertices[i].bonefrom) 
     892                        { 
     893                                bone.vertexIndex        = vertex_id; 
     894                                bone.boneIndex          = ib->boneIndex; 
     895                                bone.weight                             = ib->weight; 
     896 
     897                                mInitialMesh->mBones.push_back(bone); 
     898                                bones_count++; 
     899                        } 
     900                } 
     901        } 
     902 
     903        //      Clears bones. 
     904        mGeoMesh->mBones.clear(); 
     905 
     906        //      For each bone assignment. 
     907        for (unsigned int i = 0; i < mInitialMesh->mBones.size(); i++) 
     908        { 
     909                bone.vertexIndex        =       mInitialMesh->mBones[i].vertexIndex; 
     910                bone.boneIndex          =       mInitialMesh->mBones[i].boneIndex; 
     911                bone.weight                             =       mInitialMesh->mBones[i].weight; 
     912 
     913                vertex_found    =       false; 
     914 
     915                //      If the vertex is found in the simplification model. 
     916                if ((im = mIndexMap.find(bone.vertexIndex)) 
     917                                != 
     918                                mIndexMap.end()) 
     919                { 
     920                        bone.vertexIndex        =       (*im).second; 
     921 
     922                        mGeoMesh->mBones.push_back(bone); 
     923                        vertex_found    =       true; 
     924                } 
     925        } 
     926} 
     927 
     928//------------------------------------------------------------------------- 
     929//      Fill up position, normal and texture coordinates. 
     930//------------------------------------------------------------------------- 
     931void    ViewPointDrivenSimplifier::fillUpPosNorTC(Mesh  *geoMesh) 
     932{ 
     933        VertexBuffer    *vertex_buffer; 
     934        Vector3                         v3; 
     935        Vector2                         v2; 
     936 
     937        vertex_buffer   =       geoMesh->mVertexBuffer; 
     938 
     939        for (size_t     i = 0; i < vertex_buffer->mVertexCount; i++) 
     940        { 
     941                v3      =       vertex_buffer->mPosition[i]; 
     942 
     943                vPositions.push_back(v3); 
     944 
     945                v3      =       vertex_buffer->mNormal[i]; 
     946 
     947                vNormals.push_back(v3); 
     948 
     949                v2      =       vertex_buffer->mTexCoords[i]; 
     950 
     951                vTexCoords.push_back(v2); 
     952        } 
     953} 
     954 
     955//------------------------------------------------------------------------- 
     956//      EdgesMultimap class 
     957//------------------------------------------------------------------------- 
     958 
     959//------------------------------------------------------------------------- 
     960//      Constructor. 
     961//------------------------------------------------------------------------- 
     962EdgesMultimap::EdgesMultimap() 
     963{ 
     964        edges.clear(); 
     965} 
     966 
     967//------------------------------------------------------------------------- 
     968//      Insert and edge. 
     969//------------------------------------------------------------------------- 
     970void    EdgesMultimap::insert(int v1,int v2) 
     971{ 
     972        edges.insert(pair<int,int>(v1,v2)); 
     973        edges.insert(pair<int,int>(v2,v1)); 
     974} 
     975 
     976//------------------------------------------------------------------------- 
     977//      Remove edges with v1 and v2. 
     978//------------------------------------------------------------------------- 
     979void    EdgesMultimap::remove(int v1,int v2) 
     980{ 
     981        multimap<int,int>::iterator     lb; 
     982        multimap<int,int>::iterator     ub; 
     983 
     984        if (edges.find(v1) != edges.end()) 
     985        { 
     986                lb      =       edges.lower_bound(v1); 
     987                ub      =       edges.upper_bound(v1); 
     988 
     989                while (lb != ub) 
     990                { 
     991                        if ((*lb).second == v2) 
     992                        { 
     993                                //      Remove edge. 
     994                                edges.erase(lb); 
     995 
     996                                lb      =       ub; 
     997                        } 
     998                        else 
     999                        { 
     1000                                lb++; 
     1001                        } 
     1002                } 
     1003        } 
     1004 
     1005        if (edges.find(v2) != edges.end()) 
     1006        { 
     1007                lb      =       edges.lower_bound(v2); 
     1008                ub      =       edges.upper_bound(v2); 
     1009 
     1010                while (lb != ub) 
     1011                { 
     1012                        if ((*lb).second == v1) 
     1013                        { 
     1014                                //      Remove edge. 
     1015                                edges.erase(lb); 
     1016 
     1017                                lb      =       ub; 
     1018                        } 
     1019                        else 
     1020                        { 
     1021                                lb++; 
     1022                        } 
     1023                } 
     1024        } 
     1025 
     1026} 
     1027 
     1028//------------------------------------------------------------------------- 
     1029//      Checks if the edge (v1,v2) exists. 
     1030//------------------------------------------------------------------------- 
     1031bool    EdgesMultimap::exists(int v1,int v2) 
     1032{ 
     1033        bool                                                                                            found; 
     1034        multimap<int,int>::iterator     lb; 
     1035        multimap<int,int>::iterator     ub; 
     1036 
     1037        found   =       false; 
     1038 
     1039        //      Find range. 
     1040        lb      =       edges.lower_bound(v1); 
     1041        ub      =       edges.upper_bound(v1); 
     1042 
     1043        //      Search for all v1 edges. 
     1044        while (lb != ub) 
     1045        { 
     1046                //      If edge is found. 
     1047                if ((*lb).second = v2) 
     1048                { 
     1049                        found   =       true; 
     1050                        lb              =       ub; 
     1051                } 
     1052                else 
     1053                { 
     1054                        //      Next iteration. 
     1055                        lb++; 
     1056                } 
     1057        } 
     1058 
     1059        return  found; 
     1060} 
     1061 
     1062//------------------------------------------------------------------------- 
     1063//      Change vertex v1 to v2 in the map. 
     1064//------------------------------------------------------------------------- 
     1065void    EdgesMultimap::contract(int v1,int v2) 
     1066{ 
     1067        multimap<int,int>::iterator     it; 
     1068        int     v_aux; 
     1069 
     1070        //      Remove contracted edge. 
     1071        remove(v1,v2); 
     1072 
     1073        //      Modify all edges where appears v1 to v2. 
     1074        while ((it =    edges.find(v1)) != edges.end()) 
     1075        { 
     1076                v_aux   =       (*it).second; 
     1077 
     1078                //      Remove current edge. 
     1079                remove(v1,v_aux); 
     1080 
     1081                //      Modify edge. 
     1082                insert(v2,v_aux); 
     1083        } 
     1084} 
     1085 
     1086//------------------------------------------------------------------------- 
     1087//      TextureConserver class 
     1088//------------------------------------------------------------------------- 
     1089TextureConserver::TextureConserver() 
     1090{ 
     1091        mEdges          =       new     EdgesMultimap(); 
     1092        mGeoMesh        =       new Mesh(); 
     1093} 
     1094 
     1095TextureConserver::~TextureConserver() 
     1096{ 
     1097        delete  mGeoMesh; 
     1098} 
     1099 
     1100//------------------------------------------------------------------------- 
     1101//      Join twin vertices into a unique vertex. 
     1102//------------------------------------------------------------------------- 
     1103void    TextureConserver::JoinVertices(Mesh *mesh) 
     1104{ 
     1105        map<_coord_, int>                                                       uniquevertices; 
     1106        map<int, int>                                                                   newindices; 
     1107        multimap<int, int>::iterator    it; 
     1108        map<int, int>::iterator                         it_map; 
     1109        VertexBuffer                                                                    *vertex_buffer; 
     1110        VertexBuffer                                                                    *mGMVB; 
     1111        unsigned        int                                                                     vertex_count; 
     1112        unsigned        int                                                                     i; 
     1113        unsigned        int                                                                     aux_i; 
     1114        unsigned        int                                                                     submesh; 
     1115        SubMesh                                                                                         *geosubmesh; 
     1116        _coord_                                                                                         vertex_aux; 
     1117 
     1118        //      Copy actual mesh. 
     1119        *mGeoMesh               =       *mesh; 
     1120 
     1121        //      Gets the shared vertex buffer. 
     1122        vertex_buffer   =       mGeoMesh->mVertexBuffer; 
     1123 
     1124        //      Initialize unique vertices count. 
     1125        vertex_count    =       0; 
     1126 
     1127        //      Fill up vertices. 
     1128        for (i  =       0; i < vertex_buffer->mVertexCount; i++) 
     1129        { 
     1130                vertex_aux.x    = vertex_buffer->mPosition[i].x; 
     1131                vertex_aux.y    = vertex_buffer->mPosition[i].y; 
     1132                vertex_aux.z    = vertex_buffer->mPosition[i].z; 
     1133 
     1134                //      New index. 
     1135                if (uniquevertices.find(vertex_aux) == uniquevertices.end()) 
     1136                { 
     1137                        uniquevertices[vertex_aux]      = vertex_count; 
     1138                        newindices[i]                                                           = vertex_count; 
     1139 
     1140                        mVertices.insert(pair<int,int>(vertex_count,i)); 
     1141                         
     1142                        //      Increments unique vertices count. 
     1143                        vertex_count++; 
     1144                } 
     1145                //      The map of unique vertices already contains this vertex. 
     1146                else 
     1147                { 
     1148                        int newindex    =       uniquevertices[vertex_aux]; 
     1149                        newindices[i]   =       newindex; 
     1150 
     1151                        mVertices.insert(pair<int,int>(newindex,i)); 
     1152                } 
     1153        } 
     1154 
     1155        //      Delete vertices. 
     1156        delete  mesh->mVertexBuffer; 
     1157 
     1158        mesh->mVertexBuffer     =       new VertexBuffer(); 
     1159 
     1160        mesh->mVertexBuffer->mPosition          = new Vector3[vertex_count]; 
     1161        mesh->mVertexBuffer->mNormal                    = new Vector3[vertex_count]; 
     1162        mesh->mVertexBuffer->mTexCoords         = new Vector2[vertex_count]; 
     1163 
     1164        mesh->mVertexBuffer->mVertexInfo        = vertex_buffer->mVertexInfo; 
     1165        mesh->mVertexBuffer->mVertexCount       = vertex_count; 
     1166 
     1167        mGMVB   =       mesh->mVertexBuffer; 
     1168 
     1169        //      Fill up unique vertices. 
     1170        for (i  =        0; i < vertex_count; i++) 
     1171        { 
     1172                //      Find first ocurrence of unique vertex. 
     1173                it      =       mVertices.find(i); 
     1174 
     1175                aux_i   =       (*it).second; 
     1176 
     1177                mGMVB->mPosition[i].x   =       vertex_buffer->mPosition[aux_i].x; 
     1178                mGMVB->mPosition[i].y   =       vertex_buffer->mPosition[aux_i].y; 
     1179                mGMVB->mPosition[i].z   =       vertex_buffer->mPosition[aux_i].z; 
     1180 
     1181                mGMVB->mNormal[i].x     =       vertex_buffer->mNormal[aux_i].x; 
     1182                mGMVB->mNormal[i].y     =       vertex_buffer->mNormal[aux_i].y; 
     1183                mGMVB->mNormal[i].z     =       vertex_buffer->mNormal[aux_i].z; 
     1184 
     1185                mGMVB->mTexCoords[i].x  =       vertex_buffer->mTexCoords[aux_i].x; 
     1186                mGMVB->mTexCoords[i].y  =       vertex_buffer->mTexCoords[aux_i].y; 
     1187        } 
     1188 
     1189        //      Fill up indices to unique vertices. 
     1190        //      For each submesh. 
     1191        for (submesh    =       0; submesh < mesh->mSubMeshCount; submesh++) 
     1192        { 
     1193                geosubmesh      =       &mesh->mSubMesh[submesh]; 
     1194 
     1195                //      Change indices. 
     1196                for (i  =       0; i < geosubmesh->mIndexCount; i++) 
     1197                { 
     1198                        //      Unique index asociated to the given one. 
     1199                        it_map  =       newindices.find(geosubmesh->mIndex[i]); 
     1200 
     1201                        geosubmesh->mIndex[i]   =       (*it_map).second; 
     1202                } 
     1203        } 
     1204} 
     1205 
     1206//------------------------------------------------------------------------- 
     1207//      Modify the joined vertices sequento to an split. 
     1208//------------------------------------------------------------------------- 
     1209MeshSimplificationSequence * 
     1210TextureConserver::WriteRealSimpSeq(MeshSimplificationSequence *simpseq) 
     1211{ 
     1212        bool    edge_found; 
     1213 
     1214        size_t  v0; 
     1215        size_t  v1; 
     1216        size_t  vertex_count; 
     1217        size_t  i,j; 
     1218 
     1219        VertexBuffer    *mVB; 
     1220        GeoVertex                       new_vertex; 
     1221 
     1222        multimap<int, int>::iterator    lb0; 
     1223        multimap<int, int>::iterator    ub0; 
     1224        multimap<int, int>::iterator    lb1; 
     1225        multimap<int, int>::iterator    ub1; 
     1226        multimap<int, int>::iterator    it0; 
     1227 
     1228        MeshSimplificationSequence                              *real_seq; 
     1229        MeshSimplificationSequence::Step        current_step; 
     1230 
     1231        //      Initialize vertex count. 
     1232        vertex_count    =       mGeoMesh->mVertexBuffer->mVertexCount; 
     1233 
     1234        //      Creates new simplification sequence. 
     1235        real_seq        =       new     MeshSimplificationSequence(); 
     1236 
     1237        //      For each simplification step. 
     1238        for (i = 0; i < simpseq->mSteps.size(); i++) 
     1239        { 
     1240                lb0     =       mVertices.lower_bound(simpseq->mSteps[i].mV0); 
     1241                ub0     =       mVertices.upper_bound(simpseq->mSteps[i].mV0); 
     1242                lb1     =       mVertices.lower_bound(simpseq->mSteps[i].mV1); 
     1243                ub1     =       mVertices.upper_bound(simpseq->mSteps[i].mV1); 
     1244 
     1245                //      Removed vertex. 
     1246                while (lb1 != ub1) 
     1247                { 
     1248                        //      Real index. 
     1249                        v1      =       (*lb1).second; 
     1250 
     1251                        //      Begin of iteration v0. 
     1252                        it0     =       lb0; 
     1253 
     1254                        edge_found      =       false; 
     1255 
     1256                        //      Remaining vertex. 
     1257                        while (it0 != ub0) 
     1258                        { 
     1259                                //      Real index. 
     1260                                v0      =       (*it0).second; 
     1261 
     1262                                //      If edge exists. 
     1263                                if (mEdges->exists(v0,v1)) 
     1264                                { 
     1265                                        mEdges->contract(v1,v0); 
     1266 
     1267                                        //      Simplification sequence. 
     1268                                        current_step.mV0        =       v0; 
     1269                                        current_step.mV1        =       v1; 
     1270 
     1271                                        real_seq->mSteps.push_back(current_step); 
     1272 
     1273                                        edge_found      =       true; 
     1274                                } 
     1275 
     1276                                it0++; 
     1277                        } 
     1278 
     1279                        //      If not found a valid edge. 
     1280                        if (!edge_found) 
     1281                        { 
     1282                                //      Id new vertex. 
     1283                                new_vertex.id                                   =       vertex_count; 
     1284                                new_vertex.bonefrom             =       v0; 
     1285 
     1286                                //      Simplification sequence. 
     1287                                current_step.mV0        =       new_vertex.id; 
     1288                                current_step.mV1        =       v1; 
     1289 
     1290                                real_seq->mSteps.push_back(current_step); 
     1291 
     1292                                //      Contracts lonely vertex. 
     1293                                mEdges->contract(v1,new_vertex.id); 
     1294 
     1295                                //      New vertex adquires position of v0. 
     1296                                new_vertex.position.x   =       mGeoMesh->mVertexBuffer->mPosition[v0].x; 
     1297                                new_vertex.position.y   =       mGeoMesh->mVertexBuffer->mPosition[v0].y; 
     1298                                new_vertex.position.z   =       mGeoMesh->mVertexBuffer->mPosition[v0].z; 
     1299 
     1300                                //      New vertex adquires normal of lonely one. 
     1301                                new_vertex.normal.x     =       mGeoMesh->mVertexBuffer->mNormal[v1].x; 
     1302                                new_vertex.normal.y     =       mGeoMesh->mVertexBuffer->mNormal[v1].y; 
     1303                                new_vertex.normal.z     =       mGeoMesh->mVertexBuffer->mNormal[v1].z; 
     1304 
     1305                                //      New vertex adquires the texture cood of the lonely one. 
     1306                                new_vertex.texcoord.x   =       mGeoMesh->mVertexBuffer->mTexCoords[v1].x; 
     1307                                new_vertex.texcoord.y   =       mGeoMesh->mVertexBuffer->mTexCoords[v1].y; 
     1308 
     1309                                //      New vertex stored. 
     1310                                real_seq->mNewVertices.push_back(new_vertex); 
     1311 
     1312                                //      New vertex added. 
     1313                                vertex_count++; 
     1314                        } 
     1315 
     1316                        lb1++; 
     1317                } 
     1318        } 
     1319 
     1320        //      Assigns new simplification sequence. 
     1321        delete  simpseq; 
     1322 
     1323        return  real_seq; 
     1324        /* 
     1325        //      New vertex buffer to add new vertices. 
     1326        mVB     =       new VertexBuffer(); 
     1327 
     1328        mVB->mPosition  =       new Vector3[vertex_count]; 
     1329        mVB->mNormal            =       new Vector3[vertex_count]; 
     1330        mVB->mTexCoords =       new Vector2[vertex_count]; 
     1331 
     1332        mVB->mVertexInfo        =       mGeoMesh->mVertexBuffer->mVertexInfo; 
     1333 
     1334        //      Copy original vertices. 
     1335        for (i = 0; i < mGeoMesh->mVertexBuffer->mVertexCount; i++) 
     1336        { 
     1337                mVB->mPosition[i].x     =       mGeoMesh->mVertexBuffer->mPosition[i].x; 
     1338                mVB->mPosition[i].y     =       mGeoMesh->mVertexBuffer->mPosition[i].y; 
     1339                mVB->mPosition[i].z     =       mGeoMesh->mVertexBuffer->mPosition[i].z; 
     1340 
     1341                mVB->mNormal[i].x       =       mGeoMesh->mVertexBuffer->mNormal[i].x; 
     1342                mVB->mNormal[i].y       =       mGeoMesh->mVertexBuffer->mNormal[i].y; 
     1343                mVB->mNormal[i].z       =       mGeoMesh->mVertexBuffer->mNormal[i].z; 
     1344 
     1345                mVB->mTexCoords[i].x    =       mGeoMesh->mVertexBuffer->mTexCoords[i].x; 
     1346                mVB->mTexCoords[i].y    =       mGeoMesh->mVertexBuffer->mTexCoords[i].y; 
     1347        } 
     1348 
     1349        //      Add new vertices. 
     1350        j       =       mGeoMesh->mVertexBuffer->mVertexCount; 
     1351 
     1352        for (i = 0; i < new_vertices.size(); i++) 
     1353        { 
     1354                mVB->mPosition[j].x     =       new_vertices[i].position.x; 
     1355                mVB->mPosition[j].y     =       new_vertices[i].position.y; 
     1356                mVB->mPosition[j].z     =       new_vertices[i].position.z; 
     1357 
     1358                mVB->mNormal[j].x       =       new_vertices[i].normal.x; 
     1359                mVB->mNormal[j].y       =       new_vertices[i].normal.y; 
     1360                mVB->mNormal[j].z       =       new_vertices[i].normal.z; 
     1361 
     1362                mVB->mTexCoords[j].x    =       new_vertices[i].texcoord.x; 
     1363                mVB->mTexCoords[j].y    =       new_vertices[i].texcoord.y; 
     1364        } 
     1365 
     1366        //      Free memory. 
     1367        delete  []mGeoMesh->mVertexBuffer; 
     1368 
     1369        //      Reassigns main vertex buffer with new vertices. 
     1370        mGeoMesh->mVertexBuffer = mVB; 
     1371 
     1372        //      Reassign submesh vertex buffers. 
     1373        for (i = 0; i < mGeoMesh->mSubMeshCount; i++) 
     1374        { 
     1375                mGeoMesh->mSubMesh[i].mVertexBuffer     =       mVB; 
     1376        } 
     1377        */ 
     1378} 
     1379 
     1380//------------------------------------------------------------------------- 
     1381//      Return  mesh that may have repeated vertices for texture aparence. 
     1382//------------------------------------------------------------------------- 
     1383Mesh    *TextureConserver::GetMesh() 
     1384{ 
     1385        return  mGeoMesh; 
     1386} 
     1387 
Note: See TracChangeset for help on using the changeset viewer.