Ignore:
Timestamp:
09/28/06 17:49:37 (18 years ago)
Author:
gumbau
Message:

Updated modules to the new interface and the new simplification algorithm improvements.

Location:
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs
Files:
15 added
18 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/AdjModel.cxx

    r1025 r1526  
    1717int Model::in_Vertex(const Vec3& p) 
    1818{ 
    19     Vertex *v = newVertex(p[X], p[Y], p[Z]); 
    20     bounds.addPoint(p); 
    21     return vertCount() - 1; 
     19        Vertex *v = newVertex(p[X], p[Y], p[Z]); 
     20        bounds.addPoint(p); 
     21        return vertCount() - 1; 
    2222} 
    2323 
     
    3636int Model::in_Face(int a, int b, int c, int n1, int n2, int n3, int t1, int t2, int t3) 
    3737{ 
    38     Vertex *v1 = vertices(a-1); 
    39     Vertex *v2 = vertices(b-1); 
    40     Vertex *v3 = vertices(c-1); 
    41          
    42     Face *t = newFace(v1, v2, v3); 
     38        Vertex *v1 = vertices(a-1); 
     39        Vertex *v2 = vertices(b-1); 
     40        Vertex *v3 = vertices(c-1); 
     41 
     42        Face *t = newFace(v1, v2, v3); 
    4343 
    4444        t->normals[0] = n1; 
     
    5555int Model::miin_Face(int a, int b, int c) 
    5656{ 
    57     Vertex *v1 = vertices(a-1); 
    58     Vertex *v2 = vertices(b-1); 
    59     Vertex *v3 = vertices(c-1); 
    60          
    61     Face *t = newFace(v1, v2, v3); 
     57        Vertex *v1 = vertices(a); 
     58        Vertex *v2 = vertices(b); 
     59        Vertex *v3 = vertices(c); 
     60 
     61        Face *t = newFace(v1, v2, v3); 
    6262 
    6363        t->normals[0] = a; 
     
    7575int Model::in_FColor(const Vec3& c) 
    7676{ 
    77     Face *f = faces(faces.length()-1); 
    78  
    79     if( !f->props ) 
    80         f->props = new FProp; 
    81  
    82     f->props->color = c; 
    83     return 0; 
     77        Face *f = faces(faces.length()-1); 
     78 
     79        if( !f->props ) 
     80        { 
     81                f->props = new FProp; 
     82        } 
     83 
     84        f->props->color = c; 
     85 
     86        return 0; 
    8487} 
    8588#endif 
     
    8891int Model::in_VColor(const Vec3& c) 
    8992{ 
    90     Vertex *v = vertices(vertices.length()-1); 
    91  
    92     if( !v->props ) 
    93         v->props = new VProp; 
    94  
    95     v->props->color = c; 
    96     return 0; 
     93        Vertex *v = vertices(vertices.length()-1); 
     94 
     95        if( !v->props ) 
     96        { 
     97                v->props = new VProp; 
     98        } 
     99 
     100        v->props->color = c; 
     101        return 0; 
    97102} 
    98103#endif 
     
    100105Vec3 Model::synthesizeNormal(Vertex *v) 
    101106{ 
    102     Vec3 n(0,0,0); 
    103     int n_count = 0; 
    104  
    105     edge_buffer& edges = v->edgeUses(); 
    106     for(int i=0; i<edges.length(); i++) 
    107     { 
    108         face_buffer& faces = edges(i)->faceUses(); 
    109  
    110         for(int j=0; j<faces.length(); j++) 
    111         { 
    112             n += faces(j)->plane().normal(); 
    113             n_count++; 
    114         } 
    115     } 
    116  
    117     if( n_count ) 
    118         n /= (real)n_count; 
    119     else 
    120     { 
     107        Vec3 n(0,0,0); 
     108        int n_count = 0; 
     109 
     110        edge_buffer& edges = v->edgeUses(); 
     111        for(int i=0; i<edges.length(); i++) 
     112        { 
     113                face_buffer& faces = edges(i)->faceUses(); 
     114 
     115                for(int j=0; j<faces.length(); j++) 
     116                { 
     117                        n += faces(j)->plane().normal(); 
     118                        n_count++; 
     119                } 
     120        } 
     121 
     122        if( n_count ) 
     123        { 
     124                n /= (real)n_count; 
     125        } 
     126        else 
     127        { 
    121128                std::cerr << "Vertex with no normals!!: " << v->uniqID; 
    122129                std::cerr << " / " << v->tempID << std::endl; 
    123     } 
    124     return n; 
     130        } 
     131        return n; 
    125132} 
    126133 
     
    138145Vertex *Model::newVertex(real x, real y, real z) 
    139146{ 
    140     Vertex *v = new Vertex(x, y, z); 
    141  
    142     v->vID = v->uniqID = vertices.add(v); 
    143  
    144 /*    if( logfile && selected_output&OUTPUT_MODEL_DEFN ) 
    145                 *logfile << "v " << x << " " << y << " " << z << std::endl; 
    146 */ 
    147     validVertCount++; 
    148  
    149     return v; 
     147        Vertex *v = new Vertex(x, y, z); 
     148 
     149        v->vID = v->uniqID = vertices.add(v); 
     150 
     151        /*    if( logfile && selected_output&OUTPUT_MODEL_DEFN ) 
     152         *logfile << "v " << x << " " << y << " " << z << std::endl; 
     153         */ 
     154        validVertCount++; 
     155 
     156        return v; 
    150157} 
    151158 
    152159Edge *Model::newEdge(Vertex *a, Vertex *b) 
    153160{ 
    154     Edge *e = new Edge(a, b); 
    155  
    156     e->uniqID = edges.add(e); 
    157     e->sym()->uniqID = e->uniqID; 
    158  
    159     validEdgeCount++; 
    160  
    161     return e; 
     161        Edge *e = new Edge(a, b); 
     162 
     163        e->uniqID = edges.add(e); 
     164        e->sym()->uniqID = e->uniqID; 
     165 
     166        validEdgeCount++; 
     167 
     168        return e; 
    162169} 
    163170 
    164171static Edge *get_edge(Model *m, Vertex *org, Vertex *v) 
    165172{ 
    166     edge_buffer& edge_uses = org->edgeUses(); 
    167  
    168     for(int i=0; i<edge_uses.length(); i++) 
    169         if( edge_uses(i)->dest() == v ) 
    170             return edge_uses(i); 
    171  
    172     Edge *e = m->newEdge(org, v); 
    173  
    174     return e; 
    175 } 
    176  
     173        edge_buffer& edge_uses = org->edgeUses(); 
     174 
     175        for (int i=0; i<edge_uses.length(); i++) 
     176        { 
     177                if ( edge_uses(i)->dest() == v ) 
     178                { 
     179                        return edge_uses(i); 
     180                } 
     181        } 
     182 
     183        Edge *e = m->newEdge(org, v); 
     184 
     185        return e; 
     186} 
    177187 
    178188Face *Model::newFace(Vertex *v1, Vertex *v2, Vertex *v3) 
    179189{ 
    180     Edge *e0 = get_edge(this, v1, v2);   // v1->edgeTo(m, v2); 
    181     Edge *e1 = get_edge(this, v2, v3);   // v2->edgeTo(m, v3); 
    182     Edge *e2 = get_edge(this, v3, v1);   // v3->edgeTo(m, v1); 
    183    
    184     Face *t = new Face(e0, e1, e2); 
    185  
    186     t->uniqID = faces.add(t); 
    187  
    188 /*    if( logfile && selected_output&OUTPUT_MODEL_DEFN ) 
    189         *logfile << "f " << v1->uniqID+1 << " " 
    190         << v2->uniqID+1 << " " << v3->uniqID+1 << std::endl; 
    191 */ 
    192     validFaceCount++; 
    193  
    194     return t; 
     190        Edge *e0 = get_edge(this, v1, v2);   // v1->edgeTo(m, v2); 
     191        Edge *e1 = get_edge(this, v2, v3);   // v2->edgeTo(m, v3); 
     192        Edge *e2 = get_edge(this, v3, v1);   // v3->edgeTo(m, v1); 
     193 
     194        Face *t = new Face(e0, e1, e2); 
     195 
     196        t->uniqID = faces.add(t); 
     197 
     198        /*    if( logfile && selected_output&OUTPUT_MODEL_DEFN ) 
     199         *logfile << "f " << v1->uniqID+1 << " " 
     200         << v2->uniqID+1 << " " << v3->uniqID+1 << std::endl; 
     201         */ 
     202        validFaceCount++; 
     203 
     204        return t; 
    195205} 
    196206 
     
    198208void Model::killVertex(Vertex *v) 
    199209{ 
    200     if( v->isValid() ) 
    201     { 
    202 /*      if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 
    203                 *logfile << "v- " << v->uniqID+1 << std::endl; 
    204 */ 
    205         v->kill(); 
    206         validVertCount--; 
    207     } 
     210        if( v->isValid() ) 
     211        { 
     212                /*      if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 
     213                 *logfile << "v- " << v->uniqID+1 << std::endl; 
     214                 */ 
     215                v->kill(); 
     216                validVertCount--; 
     217        } 
    208218} 
    209219 
    210220void Model::killEdge(Edge *e) 
    211221{ 
    212     if( e->isValid() ) 
    213     { 
    214         e->kill(); 
    215         validEdgeCount--; 
    216     } 
     222        if( e->isValid() ) 
     223        { 
     224                e->kill(); 
     225                validEdgeCount--; 
     226        } 
    217227} 
    218228 
    219229void Model::killFace(Face *f) 
    220230{ 
    221     if( f->isValid() ) 
    222     { 
    223 /*      if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 
    224                 *logfile << "f- " << f->uniqID+1 << std::endl; 
    225 */ 
    226         f->kill(); 
    227         validFaceCount--; 
    228     } 
     231        if( f->isValid() ) 
     232        { 
     233                /*      if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 
     234                 *logfile << "f- " << f->uniqID+1 << std::endl; 
     235                 */ 
     236                f->kill(); 
     237                validFaceCount--; 
     238        } 
    229239} 
    230240 
    231241void Model::reshapeVertex(Vertex *v, real x, real y, real z) 
    232242{ 
    233     v->set(x, y, z); 
     243        v->set(x, y, z); 
    234244 
    235245#ifdef LOG_LOWLEVEL_OPS 
    236     if( logfile ) 
    237         *logfile << "v! " << v->uniqID+1 << " " 
    238                  << x << " " << y << " " << z << endl; 
     246        if( logfile ) 
     247        { 
     248                *logfile << "v! " << v->uniqID+1 << " " 
     249                        << x << " " << y << " " << z << endl; 
     250        } 
    239251#endif 
    240252} 
     
    242254void Model::remapVertex(Vertex *from, Vertex *to) 
    243255{ 
    244     from->remapTo(to); 
     256        from->remapTo(to); 
    245257 
    246258#ifdef LOG_LOWLEVEL_OPS 
    247     if( logfile ) 
    248         *logfile << "v> " << from->uniqID+1 << " " << to->uniqID+1 << endl; 
    249 #endif 
    250 } 
    251  
    252  
     259        if( logfile ) 
     260        { 
     261                *logfile << "v> " << from->uniqID+1 << " " << to->uniqID+1 << endl; 
     262        } 
     263#endif 
     264} 
    253265 
    254266void Model::contract(Vertex *v1, 
     
    257269                     face_buffer& changed) 
    258270{ 
    259     int i; 
    260  
    261 /*    if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 
    262     { 
    263         *logfile << "v% (" << v1->uniqID+1; 
     271        int i; 
     272 
     273        /*    if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 
     274                                { 
     275         *logfile << "v% (" << v1->uniqID+1; 
     276         for(i=0; i<rest.length(); i++) 
     277         *logfile << " " << rest(i)->uniqID+1; 
     278 
     279         *logfile << ") " << to[X] << " " << to[Y] << " " << to[Z] << std::endl; 
     280         } 
     281         */ 
     282 
     283        // 
     284        // Collect all the faces that are going to be changed 
     285        // 
     286        contractionRegion(v1, rest, changed); 
     287 
     288        reshapeVertex(v1, to[X], to[Y], to[Z]); 
     289 
    264290        for(i=0; i<rest.length(); i++) 
    265             *logfile << " " << rest(i)->uniqID+1; 
    266          
    267         *logfile << ") " << to[X] << " " << to[Y] << " " << to[Z] << std::endl; 
    268     } 
    269 */ 
    270  
    271     // 
    272     // Collect all the faces that are going to be changed 
    273     // 
    274     contractionRegion(v1, rest, changed); 
    275  
    276     reshapeVertex(v1, to[X], to[Y], to[Z]); 
    277  
    278     for(i=0; i<rest.length(); i++) 
     291        { 
    279292                rest(i)->remapTo(v1); 
    280  
    281     removeDegeneracy(changed); 
     293        } 
     294 
     295        removeDegeneracy(changed); 
    282296} 
    283297 
    284298void Model::maybeFixFace(Face *F) 
    285299{ 
    286     // 
    287     // Invalid faces do not need to be fixed. 
     300        // 
     301        // Invalid faces do not need to be fixed. 
    288302#ifdef SAFETY 
    289     assert( F->isValid() ); 
    290 #endif 
    291  
    292     Vertex *v0=F->vertex(0); Vertex *v1=F->vertex(1); Vertex *v2=F->vertex(2); 
    293     Edge *e0 = F->edge(0); Edge *e1 = F->edge(1); Edge *e2 = F->edge(2); 
    294  
    295     bool a=(v0 == v1),  b=(v0 == v2),  c=(v1 == v2); 
    296  
    297     if( a && c ) 
    298     { 
    299         // This triangle has been reduced to a point 
    300         killEdge(e0); 
    301         killEdge(e1); 
    302         killEdge(e2); 
    303  
    304         killFace(F); 
    305     } 
    306     // 
    307     // In the following 3 cases, the triangle has become an edge 
    308     else if( a ) 
    309     { 
    310         killEdge(e0); 
    311         e1->remapTo(e2->sym()); 
    312         killFace(F); 
    313     } 
    314     else if( b ) 
    315     { 
    316         killEdge(e2); 
    317         e0->remapTo(e1->sym()); 
    318         killFace(F); 
    319     } 
    320     else if( c ) 
    321     { 
    322         killEdge(e1); 
    323         e0->remapTo(e2->sym()); 
    324         killFace(F); 
    325     } 
    326     else 
    327     { 
    328         // This triangle remains non-degenerate 
    329 /*              // SUS: recalcular la normal en los triangulos cambiados no degenerados 
    330  
    331                 simplif::Vertex * auxv0 = F->vertex(0); 
    332                 simplif::Vertex * auxv1 = F->vertex(1); 
    333                 simplif::Vertex * auxv2 = F->vertex(2); 
    334                 simplif::Vec3 vd1(auxv1->operator[](simplif::X) - auxv0->operator[](simplif::X),  
    335                                                 auxv1->operator[](simplif::Y) - auxv0->operator[](simplif::Y), 
    336                                                 auxv1->operator[](simplif::Z) - auxv0->operator[](simplif::Z)); 
    337                 simplif::Vec3 vd2(auxv2->operator[](simplif::X) - auxv0->operator[](simplif::X),  
    338                                                 auxv2->operator[](simplif::Y) - auxv0->operator[](simplif::Y), 
    339                                                 auxv2->operator[](simplif::Z) - auxv0->operator[](simplif::Z)); 
    340  
    341                 simplif::unitize(vd1); 
    342                 simplif::unitize(vd2); 
    343  
    344                 simplif::Vec3 vn = vd1 ^ vd2; 
    345                 simplif::unitize(vn); 
    346                  
     303        assert( F->isValid() ); 
     304#endif 
     305 
     306        Vertex *v0=F->vertex(0); Vertex *v1=F->vertex(1); Vertex *v2=F->vertex(2); 
     307        Edge *e0 = F->edge(0); Edge *e1 = F->edge(1); Edge *e2 = F->edge(2); 
     308 
     309        bool a=(v0 == v1),  b=(v0 == v2),  c=(v1 == v2); 
     310 
     311        if( a && c ) 
     312        { 
     313                // This triangle has been reduced to a point 
     314                killEdge(e0); 
     315                killEdge(e1); 
     316                killEdge(e2); 
     317 
     318                killFace(F); 
     319        } 
     320        // 
     321        // In the following 3 cases, the triangle has become an edge 
     322        else if( a ) 
     323        { 
     324                killEdge(e0); 
     325                e1->remapTo(e2->sym()); 
     326                killFace(F); 
     327        } 
     328        else if( b ) 
     329        { 
     330                killEdge(e2); 
     331                e0->remapTo(e1->sym()); 
     332                killFace(F); 
     333        } 
     334        else if( c ) 
     335        { 
     336                killEdge(e1); 
     337                e0->remapTo(e2->sym()); 
     338                killFace(F); 
     339        } 
     340        else 
     341        { 
     342                // This triangle remains non-degenerate 
     343                /*              // SUS: recalcular la normal en los triangulos cambiados no degenerados 
     344 
     345                                        simplif::Vertex * auxv0 = F->vertex(0); 
     346                                        simplif::Vertex * auxv1 = F->vertex(1); 
     347                                        simplif::Vertex * auxv2 = F->vertex(2); 
     348                                        simplif::Vec3 vd1(auxv1->operator[](simplif::X) - auxv0->operator[](simplif::X),  
     349                                        auxv1->operator[](simplif::Y) - auxv0->operator[](simplif::Y), 
     350                                        auxv1->operator[](simplif::Z) - auxv0->operator[](simplif::Z)); 
     351                                        simplif::Vec3 vd2(auxv2->operator[](simplif::X) - auxv0->operator[](simplif::X),  
     352                                        auxv2->operator[](simplif::Y) - auxv0->operator[](simplif::Y), 
     353                                        auxv2->operator[](simplif::Z) - auxv0->operator[](simplif::Z)); 
     354 
     355                                        simplif::unitize(vd1); 
     356                                        simplif::unitize(vd2); 
     357 
     358                                        simplif::Vec3 vn = vd1 ^ vd2; 
     359                                        simplif::unitize(vn); 
     360 
    347361                //auxf->vertex_normals[0] = vn; 
    348362                //auxf->vertex_normals[1] = vn; 
     
    358372                F->vertex_normals[2][simplif::Z] = vn[simplif::Z];*/ 
    359373 
    360     } 
     374        } 
    361375} 
    362376 
    363377void Model::removeDegeneracy(face_buffer& changed) 
    364378{ 
    365     for(int i=0; i<changed.length(); i++) 
     379        for(int i=0; i<changed.length(); i++) 
     380        { 
    366381                maybeFixFace(changed(i)); 
     382        } 
    367383} 
    368384 
    369385void Model::contractionRegion(Vertex *v1, 
    370                               const vert_buffer& vertices, 
    371                               face_buffer& changed) 
    372 { 
    373     changed.reset(); 
    374  
    375     // 
    376     // First, reset the marks on all reachable vertices 
    377     int i; 
    378  
    379     untagFaceLoop(v1); 
    380     for(i=0; i<vertices.length(); i++) 
     386                                                                                                const vert_buffer& vertices, 
     387                                                                                                face_buffer& changed) 
     388{ 
     389        changed.reset(); 
     390 
     391        // First, reset the marks on all reachable vertices 
     392        int i; 
     393 
     394        untagFaceLoop(v1); 
     395 
     396        for(i   =       0; i < vertices.length(); i++) 
     397        { 
    381398                untagFaceLoop(vertices(i)); 
    382  
    383     // 
    384     // Now, pick out all the unique reachable faces 
    385     collectFaceLoop(v1, changed); 
    386     for(i=0; i<vertices.length(); i++) 
     399        } 
     400 
     401        // Now, pick out all the unique reachable faces 
     402        collectFaceLoop(v1, changed); 
     403 
     404        for(i   =       0; i < vertices.length(); i++) 
     405        { 
    387406                collectFaceLoop(vertices(i), changed); 
     407        } 
    388408} 
    389409 
     
    391411void Model::contractionRegion(Vertex *v1, Vertex *v2, face_buffer& changed) 
    392412{ 
    393     changed.reset(); 
    394  
    395     // 
    396     // Clear marks on all reachable faces 
    397     untagFaceLoop(v1); 
    398     untagFaceLoop(v2); 
    399  
    400     // 
    401     // Collect all the unique reachable faces 
    402     collectFaceLoop(v1, changed); 
    403     collectFaceLoop(v2, changed); 
     413        changed.reset(); 
     414 
     415        // 
     416        // Clear marks on all reachable faces 
     417        untagFaceLoop(v1); 
     418        untagFaceLoop(v2); 
     419 
     420        // 
     421        // Collect all the unique reachable faces 
     422        collectFaceLoop(v1, changed); 
     423        collectFaceLoop(v2, changed); 
    404424} 
    405425 
     
    408428{ 
    409429#ifdef SAFETY 
    410     assert( v1 != v2); 
    411 #endif 
    412  
    413 /*    if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 
    414         *logfile << "v% (" << v1->uniqID+1 << " " << v2->uniqID+1 << ") " 
    415                  << to[X] << " " 
    416                  << to[Y] << " " 
    417                  << to[Z] << std::endl; 
    418 */ 
    419     // 
    420     // Collect all the faces that are going to be changed 
    421     // 
    422     contractionRegion(v1, v2, changed); 
    423  
    424     reshapeVertex(v1, to[X], to[Y], to[Z]); 
    425  
    426     // 
    427     // Map v2 ---> v1.  This accomplishes the topological change that we want. 
    428     v2->remapTo(v1); 
    429  
    430     removeDegeneracy(changed); 
    431 } 
    432  
     430        assert( v1 != v2); 
     431#endif 
     432        // 
     433        // Collect all the faces that are going to be changed 
     434        // 
     435        contractionRegion(v1, v2, changed); 
     436 
     437        reshapeVertex(v1, to[X], to[Y], to[Z]); 
     438 
     439        // Map v2 ---> v1.  This accomplishes the topological change that we want. 
     440        v2->remapTo(v1); 
     441 
     442//      std::cout << "Contract(QslimInds): " << v2->validID() << " --> " << v1->validID() << std::endl; 
     443 
     444        removeDegeneracy(changed); 
     445} 
    433446 
    434447/* 
     
    494507} 
    495508*/ 
     509 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/AdjModel.h

    r1025 r1526  
    2222        class Model : public SMF_Model 
    2323        { 
    24         protected: 
    25                 vert_buffer vertices; 
    26                 edge_buffer edges; 
    27                 face_buffer faces; 
    28                 vec3_buffer normals; // SUS 
    29                 vec2_buffer texcoords; 
     24                protected: 
     25                         
     26                        vert_buffer vertices; 
     27                        edge_buffer edges; 
     28                        face_buffer faces; 
     29                        vec3_buffer normals; // SUS 
     30                        vec2_buffer texcoords; 
    3031 
    31         private: 
     32                private: 
    3233 
    33                 void maybeFixFace(Face *); 
     34                        void maybeFixFace(Face *); 
    3435 
    35         public: 
    36                 Model() { 
    37                         validVertCount = 0; 
    38                         validEdgeCount = 0; 
    39                         validFaceCount = 0; 
    40                 } 
     36                public: 
     37                         
     38                        Model() 
     39                        { 
     40                                validVertCount = 0; 
     41                                validEdgeCount = 0; 
     42                                validFaceCount = 0; 
     43                        } 
    4144 
    42                 ~Model(){ 
    43                         for (int i=0; i<vertices.length(); i++) 
    44                                 delete vertices[i]; 
    45                         for (int i=0; i<edges.length(); i++) 
    46                                 delete edges[i]; 
    47                         for (int i=0; i<faces.length(); i++) 
    48                                 delete faces[i]; 
    49                 } 
     45                        ~Model() 
     46                        { 
     47                                for (int i=0; i<vertices.length(); i++) 
     48                                { 
     49                                        delete vertices[i]; 
     50                                } 
    5051 
    51                 Bounds bounds; 
     52                                for (int i=0; i<edges.length(); i++) 
     53                                { 
     54                                        delete edges[i]; 
     55                                } 
    5256 
    53                 int validVertCount; 
    54                 int validEdgeCount; 
    55                 int validFaceCount; 
     57                                for (int i=0; i<faces.length(); i++) 
     58                                { 
     59                                        delete faces[i]; 
     60                                } 
     61                        } 
    5662 
    57                 // 
    58                 // Basic model accessor functions 
    59                 // 
    60                 simplif::Vertex *vertex(int i) { return vertices(i); } 
    61                 simplif::Vec3 & normal(int i){ return normals(i); } 
    62                 simplif::Vec2 & texcoord(int i){ return texcoords(i); } 
    63                 simplif::Edge *edge(int i) { return edges(i); } 
    64                 simplif::Face *face(int i) { return faces(i); } 
     63                        Bounds bounds; 
    6564 
    66                 int vertCount() { return vertices.length(); } 
    67                 int normCount() { return normals.length();  } 
    68                 int texcoordCount() { return texcoords.length();  } 
    69                 int edgeCount() { return edges.length();    } 
    70                 int faceCount() { return faces.length();    } 
     65                        int validVertCount; 
     66                        int validEdgeCount; 
     67                        int validFaceCount; 
    7168 
    72                 vert_buffer& allVertices() { return vertices; } 
    73                 edge_buffer& allEdges()    { return edges;    } 
    74                 face_buffer& allFaces()    { return faces;    } 
     69                        // 
     70                        // Basic model accessor functions 
     71                        // 
     72                        simplif::Vertex *vertex(int i) { return vertices(i); } 
     73                        simplif::Vec3 & normal(int i){ return normals(i); } 
     74                        simplif::Vec2 & texcoord(int i){ return texcoords(i); } 
     75                        simplif::Edge *edge(int i) { return edges(i); } 
     76                        simplif::Face *face(int i) { return faces(i); } 
    7577 
    76                 // 
    77                 // Simplification primitives 
    78                 // 
    79                 Vertex   *newVertex(real x=0.0, real y=0.0, real z=0.0); 
    80                 Edge     *newEdge(Vertex *,Vertex *); 
    81                 Face *newFace(Vertex *, Vertex *, Vertex *); 
     78                        int vertCount() { return vertices.length(); } 
     79                        int normCount() { return normals.length();  } 
     80                        int texcoordCount() { return texcoords.length();  } 
     81                        int edgeCount() { return edges.length();    } 
     82                        int faceCount() { return faces.length();    } 
    8283 
    83                 void killVertex(Vertex *); 
    84                 void killEdge(Edge *); 
    85                 void killFace(Face *); 
     84                        vert_buffer& allVertices() { return vertices; } 
     85                        edge_buffer& allEdges()    { return edges;    } 
     86                        face_buffer& allFaces()    { return faces;    } 
    8687 
    87                 void reshapeVertex(Vertex *, real, real, real); 
    88                 void remapVertex(Vertex *from, Vertex *to); 
     88                        // 
     89                        // Simplification primitives 
     90                        // 
     91                        Vertex   *newVertex(real x=0.0, real y=0.0, real z=0.0); 
     92                        Edge     *newEdge(Vertex *,Vertex *); 
     93                        Face *newFace(Vertex *, Vertex *, Vertex *); 
    8994 
    90                 void contract(Vertex *v1, Vertex *v2, const Vec3& to, 
    91                           face_buffer& changed); 
     95                        void killVertex(Vertex *); 
     96                        void killEdge(Edge *); 
     97                        void killFace(Face *); 
    9298 
    93                 void contract(Vertex *v1,  
    94                           const vert_buffer& others, 
    95                           const Vec3& to, 
    96                           face_buffer& changed); 
     99                        void reshapeVertex(Vertex *, real, real, real); 
     100                        void remapVertex(Vertex *from, Vertex *to); 
    97101 
     102                        void contract(Vertex *v1, Vertex *v2, const Vec3& to, 
     103                                        face_buffer& changed); 
    98104 
    99                 // 
    100                 // Simplification convenience procedures 
    101                 // 
    102                 void removeDegeneracy(face_buffer& changed); 
    103                 void contractionRegion(Vertex *v1, Vertex *v2, face_buffer& changed); 
    104                 void contractionRegion(Vertex *v1, 
    105                                    const vert_buffer& vertices, 
    106                                    face_buffer& changed); 
     105                        void contract(Vertex *v1,  
     106                                        const vert_buffer& others, 
     107                                        const Vec3& to, 
     108                                        face_buffer& changed); 
    107109 
    108                 // 
    109                 // SMF reader functions 
    110                 // 
    111                 int in_Vertex(const Vec3&); 
    112                 int in_Normal(const Vec3&); 
    113                 int in_TexCoord(const Vec2&); 
    114                 int in_Face(int v1, int v2, int v3, int n1, int n2, int n3, int t1, int t2, int t3); 
    115                 int miin_Face(int v1, int v2, int v3); 
     110                        // 
     111                        // Simplification convenience procedures 
     112                        // 
     113                        void removeDegeneracy(face_buffer& changed); 
     114                        void contractionRegion(Vertex *v1, Vertex *v2, face_buffer& changed); 
     115                        void contractionRegion(Vertex *v1, 
     116                                        const vert_buffer& vertices, 
     117                                        face_buffer& changed); 
    116118 
    117 /*      #ifdef SUPPORT_VCOLOR 
    118                 int in_VColor(const Vec3&); 
    119         #endif 
    120         #ifdef SUPPORT_FCOLOR 
    121                 int in_FColor(const Vec3&); 
    122         #endif 
     119                        // 
     120                        // SMF reader functions 
     121                        // 
     122                        int in_Vertex(const Vec3&); 
     123                        int in_Normal(const Vec3&); 
     124                        int in_TexCoord(const Vec2&); 
     125 
     126                        int in_Face(int v1, int v2, int v3,  
     127                                                                        int n1, int n2, int n3, 
     128                                                                        int t1, int t2, int t3); 
     129 
     130                        int miin_Face(int v1, int v2, int v3); 
     131 
     132                        /*      #ifdef SUPPORT_VCOLOR 
     133                                        int in_VColor(const Vec3&); 
     134#endif 
     135#ifdef SUPPORT_FCOLOR 
     136int in_FColor(const Vec3&); 
     137#endif 
    123138*/ 
    124                 // 
    125                 // Some random functions that are mostly temporary 
    126                 // 
     139                        // 
     140                        // Some random functions that are mostly temporary 
     141                        // 
    127142 
    128                 Vec3 synthesizeNormal(Vertex *); 
     143                        Vec3 synthesizeNormal(Vertex *); 
    129144        }; 
    130145} 
    131146 
    132  
    133147// NAUTILUS_ADJMODEL_INCLUDED 
    134148#endif 
     149 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/AdjPrims.cxx

    r1025 r1526  
    1818{ 
    1919#ifdef SAFETY 
    20     assert( edge_uses.length() == 0 ); 
    21 #endif 
    22     markInvalid(); 
    23     edge_uses.reset(); 
     20        assert( edge_uses.length() == 0 ); 
     21#endif 
     22        markInvalid(); 
     23        edge_uses.reset(); 
    2424} 
    2525 
    2626void Vertex::linkEdge(Edge *e) 
    2727{ 
    28     edge_uses.add(e); 
     28        edge_uses.add(e); 
    2929} 
    3030 
    3131void Vertex::unlinkEdge(Edge *e) 
    3232{ 
    33     int index = edge_uses.find(e); 
    34  
    35 #ifdef SAFETY 
    36     assert( index >= 0 ); 
    37 #endif 
    38     edge_uses.remove(index); 
    39     if( edge_uses.length() <= 0 ) 
    40         kill(); 
     33        int index = edge_uses.find(e); 
     34 
     35#ifdef SAFETY 
     36        assert( index >= 0 ); 
     37#endif 
     38        edge_uses.remove(index); 
     39        if( edge_uses.length() <= 0 ) 
     40                kill(); 
    4141} 
    4242 
     
    4545void Vertex::remapTo(Vertex *v) 
    4646{ 
    47     if( v != this ) 
    48     { 
    49                 for(int i=0; i<edge_uses.length(); i++) 
     47        if ( v != this ) 
     48        { 
     49                for (int i=0; i<edge_uses.length(); i++) 
    5050                { 
    5151                        assert( edge_uses(i)->org() == this ); 
     
    5454 
    5555                kill(); 
    56     } 
    57 } 
    58  
     56        } 
     57} 
    5958 
    6059Edge::Edge(Vertex *a, Vertex *b) 
    6160{ 
    62     v1 = a; 
    63     v1->linkEdge(this); 
    64  
    65     face_uses = new buffer<Face *>(2); 
    66  
    67     twin = new Edge(this, b); 
     61        v1 = a; 
     62        v1->linkEdge(this); 
     63 
     64        face_uses = new buffer<Face *>(2); 
     65 
     66        twin = new Edge(this, b); 
    6867} 
    6968 
    7069Edge::Edge(Edge *sibling, Vertex *org) 
    7170{ 
    72     v1 = org; 
    73     v1->linkEdge(this); 
    74  
    75     face_uses = sibling->face_uses; 
    76     twin = sibling; 
     71        v1 = org; 
     72        v1->linkEdge(this); 
     73 
     74        face_uses = sibling->face_uses; 
     75        twin = sibling; 
    7776} 
    7877 
    7978Edge::~Edge() 
    8079{ 
    81     if( twin ) 
    82     { 
    83         face_uses->f_r_e_e(); 
    84         delete face_uses; 
    85  
    86         twin->twin = NULL; 
    87         delete twin; 
    88     } 
     80        if ( twin ) 
     81        { 
     82                face_uses->f_r_e_e(); 
     83                delete face_uses; 
     84 
     85                twin->twin = NULL; 
     86                delete twin; 
     87        } 
    8988} 
    9089 
     
    9291{ 
    9392#ifdef SAFETY 
    94     assert( face_uses->length() == 0 ); 
    95 #endif 
    96     if( isValid() ) 
    97     { 
    98         org()->unlinkEdge(this); 
    99         dest()->unlinkEdge(sym()); 
    100         markInvalid(); 
    101         twin->markInvalid(); 
    102         face_uses->reset(); 
    103     } 
     93        assert( face_uses->length() == 0 ); 
     94#endif 
     95        if ( isValid() ) 
     96        { 
     97                org()->unlinkEdge(this); 
     98                dest()->unlinkEdge(sym()); 
     99                markInvalid(); 
     100                twin->markInvalid(); 
     101                face_uses->reset(); 
     102        } 
    104103} 
    105104 
    106105void Edge::linkFace(Face *face) 
    107106{ 
    108     face_uses->add(face); 
     107        face_uses->add(face); 
    109108} 
    110109 
    111110void Edge::unlinkFace(Face *face) 
    112111{ 
    113     int index = face_uses->find(face); 
    114  
    115 #ifdef SAFETY 
    116     assert( index>=0 ); 
    117 #endif 
    118     face_uses->remove(index); 
    119     if( face_uses->length() == 0 ) 
    120         kill(); 
     112        int index = face_uses->find(face); 
     113 
     114#ifdef SAFETY 
     115        assert( index>=0 ); 
     116#endif 
     117        face_uses->remove(index); 
     118        if( face_uses->length() == 0 ) 
     119                kill(); 
    121120} 
    122121 
    123122void Edge::remapTo(Edge *e) 
    124123{ 
    125     if( e != this ) 
    126     { 
    127         for(int i=0; i<face_uses->length(); i++) 
    128         { 
    129             (*face_uses)(i)->remapEdge(this, e); 
    130         } 
    131      
    132         // Disconnect from all faces and vertices 
    133         kill(); 
    134     } 
     124        if ( e != this ) 
     125        { 
     126                for (int i=0; i<face_uses->length(); i++) 
     127                { 
     128                        (*face_uses)(i)->remapEdge(this, e); 
     129                } 
     130 
     131                // Disconnect from all faces and vertices 
     132                kill(); 
     133        } 
    135134} 
    136135 
    137136void Edge::remapEndpoint(Vertex *from, Vertex *to) 
    138137{ 
    139     if( org()==from ) 
    140     { 
     138        if ( org() == from ) 
     139        { 
    141140                v1 = to; 
    142141                to->linkEdge(this); 
    143     } 
    144     else if( dest()==from ) 
    145     { 
     142        } 
     143        else if ( dest() == from ) 
     144        { 
    146145                twin->v1 = to; 
    147146                to->linkEdge(twin); 
    148     } 
    149     else 
    150     { 
     147        } 
     148        else 
     149        { 
    151150                std::cerr << "WARNING remapEndpoint: Illegal endpoint." << std::endl; 
    152     } 
    153  
    154     // 
    155     // The cached Plane equations for the faces attached to us may 
    156     // no longer be valid (in general, chances are pretty slim that they're OK) 
    157     for(int i=0; i<face_uses->length(); i++) 
    158     { 
     151        } 
     152 
     153        // 
     154        // The cached Plane equations for the faces attached to us may 
     155        // no longer be valid (in general, chances are pretty slim that they're OK) 
     156        for (int i      =       0; i < face_uses->length(); i++) 
     157        { 
    159158                face_uses->ref(i)->invalidatePlane(); 
    160     } 
    161 } 
    162  
     159        } 
     160} 
    163161 
    164162Face::Face(Edge *e0, Edge *e1, Edge *e2) 
    165163    : Face3(*e0->org(), *e1->org(), *e2->org()) 
    166164{ 
    167     edges[0] = e0; 
    168     edges[1] = e1; 
    169     edges[2] = e2; 
    170  
    171     edges[0]->linkFace(this); 
    172     edges[1]->linkFace(this); 
    173     edges[2]->linkFace(this); 
     165        edges[0] = e0; 
     166        edges[1] = e1; 
     167        edges[2] = e2; 
     168 
     169        edges[0]->linkFace(this); 
     170        edges[1]->linkFace(this); 
     171        edges[2]->linkFace(this); 
    174172 
    175173#ifdef SUPPORT_FCOLOR 
    176     props = NULL; 
     174        props = NULL; 
    177175#endif 
    178176} 
     
    180178void Face::kill() 
    181179{ 
    182     if( isValid() ) 
    183     { 
    184         if( edge(0)->isValid() ) 
    185             edge(0)->unlinkFace(this); 
    186  
    187         if( edge(1)->isValid() ) 
    188             edge(1)->unlinkFace(this); 
    189  
    190         if( edge(2)->isValid() ) 
    191             edge(2)->unlinkFace(this); 
    192  
    193         markInvalid(); 
    194     } 
     180        if( isValid() ) 
     181        { 
     182                if( edge(0)->isValid() ) 
     183                        edge(0)->unlinkFace(this); 
     184 
     185                if( edge(1)->isValid() ) 
     186                        edge(1)->unlinkFace(this); 
     187 
     188                if( edge(2)->isValid() ) 
     189                        edge(2)->unlinkFace(this); 
     190 
     191                markInvalid(); 
     192        } 
    195193} 
    196194 
    197195void Face::remapEdge(Edge *from, Edge *to) 
    198196{ 
    199     for(int i=0; i<3; i++) 
    200     { 
    201         if( edges[i] == from ) 
    202         { 
    203             edges[i] = to; 
    204             to->linkFace(this); 
    205         } 
    206         else if( edges[i] == from->sym() ) 
    207         { 
    208             edges[i] = to->sym(); 
    209             to->sym()->linkFace(this); 
    210         } 
    211     } 
    212  
    213     invalidatePlane(); 
    214 } 
    215  
     197        for (int i=0; i<3; i++) 
     198        { 
     199                if ( edges[i] == from ) 
     200                { 
     201                        edges[i] = to; 
     202                        to->linkFace(this); 
     203                } 
     204                else if ( edges[i] == from->sym() ) 
     205                { 
     206                        edges[i] = to->sym(); 
     207                        to->sym()->linkFace(this); 
     208                } 
     209        } 
     210 
     211        invalidatePlane(); 
     212} 
    216213 
    217214void simplif::untagFaceLoop(Vertex *v) 
    218215{ 
    219     edge_buffer& edges = v->edgeUses(); 
    220  
    221     for(int j=0; j<edges.length(); j++) 
    222     {        
    223         face_buffer& faces = edges(j)->faceUses(); 
    224         for(int k=0; k<faces.length(); k++) 
    225             faces(k)->untag(); 
    226     } 
     216        edge_buffer& edges = v->edgeUses(); 
     217 
     218        for (int j      =       0; j < edges.length(); j++) 
     219        {            
     220                face_buffer& faces = edges(j)->faceUses(); 
     221 
     222                for (int k      =       0; k < faces.length(); k++) 
     223                { 
     224                        faces(k)->untag(); 
     225                } 
     226        } 
    227227} 
    228228 
    229229void simplif::collectFaceLoop(Vertex *v, face_buffer& loop) 
    230230{ 
    231     edge_buffer& edges = v->edgeUses(); 
    232  
    233     for(int j=0; j<edges.length(); j++) 
    234     {        
    235         face_buffer& faces = edges(j)->faceUses(); 
    236         for(int k=0; k<faces.length(); k++) 
    237             if( !faces(k)->isTagged() ) 
    238             { 
    239                 loop.add(faces(k)); 
    240                 faces(k)->tag(); 
    241             } 
    242     } 
     231        edge_buffer& edges = v->edgeUses(); 
     232 
     233        for(int j=0; j<edges.length(); j++) 
     234        {            
     235                face_buffer& faces = edges(j)->faceUses(); 
     236                for(int k=0; k<faces.length(); k++) 
     237                        if( !faces(k)->isTagged() ) 
     238                        { 
     239                                loop.add(faces(k)); 
     240                                faces(k)->tag(); 
     241                        } 
     242        } 
    243243} 
    244244 
    245245int simplif::classifyEdge(Edge *e) 
    246246{ 
    247     int cls = e->faceUses().length(); 
    248  
    249     if( cls>3 ) cls=3; 
    250  
    251     return cls; 
     247        int cls = e->faceUses().length(); 
     248 
     249        if( cls>3 ) cls=3; 
     250 
     251        return cls; 
    252252} 
    253253 
    254254int simplif::classifyVertex(Vertex *v) 
    255255{ 
    256     int border_count = 0; 
    257     const edge_buffer& edges = v->edgeUses(); 
    258  
    259     for(int i=0; i<edges.length(); i++) 
    260         if( classifyEdge(edges(i)) == 1 ) 
    261             border_count++; 
    262  
    263     if( border_count == edges.length() ) 
    264         return VERTEX_BORDER_ONLY; 
    265     else  
    266         return (border_count > 0); 
    267 } 
     256        int border_count = 0; 
     257        const edge_buffer& edges = v->edgeUses(); 
     258 
     259        for(int i=0; i<edges.length(); i++) 
     260                if( classifyEdge(edges(i)) == 1 ) 
     261                        border_count++; 
     262 
     263        if( border_count == edges.length() ) 
     264                return VERTEX_BORDER_ONLY; 
     265        else  
     266                return (border_count > 0); 
     267} 
     268 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/AdjPrims.h

    r1025 r1526  
    2727        class Face; 
    2828 
    29  
    3029        typedef buffer<Vertex *> vert_buffer; 
    3130        typedef buffer<Edge *> edge_buffer; 
     
    3736        extern void collectFaceLoop(Vertex *v, face_buffer& faces); 
    3837 
    39         #define EDGE_BOGUS 0 
    40         #define EDGE_BORDER 1 
    41         #define EDGE_MANIFOLD 2 
    42         #define EDGE_NONMANIFOLD 3 
     38#define EDGE_BOGUS 0 
     39#define EDGE_BORDER 1 
     40#define EDGE_MANIFOLD 2 
     41#define EDGE_NONMANIFOLD 3 
    4342        extern int classifyEdge(Edge *); 
    4443 
    45         #define VERTEX_INTERIOR 0 
    46         #define VERTEX_BORDER 1 
    47         #define VERTEX_BORDER_ONLY 2 
     44#define VERTEX_INTERIOR 0 
     45#define VERTEX_BORDER 1 
     46#define VERTEX_BORDER_ONLY 2 
    4847        extern int classifyVertex(Vertex *); 
    4948 
     
    5251        // The actual class definitions 
    5352        // 
     53        //////////////////////////////////////////////////////////////////////// 
    5454 
    5555        class VProp 
    5656        { 
    57         public: 
    58                 Vec3 color; 
     57                public: 
     58                        Vec3 color; 
    5959        }; 
    6060 
    6161        class FProp 
    6262        { 
    63         public: 
    64                 Vec3 color; 
     63                public: 
     64                        Vec3 color; 
    6565        }; 
    6666 
     
    6868        { 
    6969                edge_buffer edge_uses; 
    70      
    71         public: 
    72         #ifdef SUPPORT_VCOLOR 
     70 
     71                public: 
     72#ifdef SUPPORT_VCOLOR 
    7373                VProp *props; 
    74         #endif 
     74#endif 
    7575 
    7676                Vertex(real x, real y, real z) : Vec3(x, y, z), edge_uses(6) { 
    77         #ifdef SUPPORT_VCOLOR 
    78                 props = NULL; 
    79         #endif 
     77#ifdef SUPPORT_VCOLOR 
     78                        props = NULL; 
     79#endif                   
    8080                } 
    8181                // 
     
    9292                void remapTo(Vertex *v); 
    9393 
    94                 unsigned int vID; 
     94                // SUS 
     95                int vID; // this can be -1 when the vertex becomes non-valid 
    9596        }; 
    96  
    9797 
    9898        class Edge : public NPrim 
    9999        { 
    100         private: 
     100                private: 
    101101 
    102                 Vertex *v1; 
     102                        Vertex *v1; 
    103103 
    104                 face_buffer *face_uses; 
    105                 Edge *twin; 
     104                        face_buffer *face_uses; 
     105                        Edge *twin; 
    106106 
    107                 Edge(Edge *twin, Vertex *org); // the twin constructor 
     107                        Edge(Edge *twin, Vertex *org); // the twin constructor 
    108108 
    109         public: 
    110                 Edge(Vertex *, Vertex *); 
    111                 ~Edge(); 
     109                public: 
    112110 
    113                 // 
    114                 // Fundamental Edge accessors 
    115                 // 
    116                 Vertex *org()  { return v1;       } 
    117                 Vertex *dest() { return twin->v1; } 
    118                 Edge *sym()    { return twin;     } 
     111                        Edge(Vertex *, Vertex *); 
     112                        ~Edge(); 
    119113 
    120                 // 
    121                 // Standard methods for all objects 
    122                 // 
    123                 void kill(); 
    124                 face_buffer& faceUses() { return *face_uses; } 
     114                        // 
     115                        // Fundamental Edge accessors 
     116                        // 
     117                        Vertex *org()  { return v1;       } 
     118                        Vertex *dest() { return twin->v1; } 
     119                        Edge *sym()    { return twin;     } 
    125120 
    126                 // 
    127                 // Basic primitives for manipulating model topology 
    128                 // 
    129                 void linkFace(Face *); 
    130                 void unlinkFace(Face *); 
    131                 void remapEndpoint(Vertex *from, Vertex *to); 
    132                 void remapTo(Edge *); 
     121                        // 
     122                        // Standard methods for all objects 
     123                        // 
     124                        void kill(); 
     125                        face_buffer& faceUses() { return *face_uses; } 
     126 
     127                        // 
     128                        // Basic primitives for manipulating model topology 
     129                        // 
     130                        void linkFace(Face *); 
     131                        void unlinkFace(Face *); 
     132                        void remapEndpoint(Vertex *from, Vertex *to); 
     133                        void remapTo(Edge *); 
    133134        }; 
    134135 
     
    137138                Edge *edges[3]; 
    138139 
    139  
    140         public: 
    141         #ifdef SUPPORT_FCOLOR 
     140                public: 
     141#ifdef SUPPORT_FCOLOR 
    142142                FProp *props; 
    143         #endif 
     143#endif 
    144144 
    145145                Face(Edge *, Edge *, Edge *); 
     
    148148                // Basic Face accessors 
    149149                // 
    150                 const Vec3& vertexPos(int i) const { return *edges[i]->org(); } 
    151                 void vertexPos(int, const Vec3&) { 
     150                const Vec3& vertexPos(int i) const{return *edges[i]->org();} 
     151 
     152                void vertexPos(int, const Vec3&) 
     153                { 
    152154                        fatal_error("Face: can't directly set vertex position."); 
    153155                } 
    154156 
    155                 Vertex *vertex(int i) { return edges[i]->org(); } 
    156                 const Vertex *vertex(int i) const { return edges[i]->org(); } 
    157                 Edge *edge(int i)               { return edges[i];        } 
    158  
     157                Vertex *vertex(int i){return edges[i]->org();} 
     158                const Vertex *vertex(int i) const{return edges[i]->org();} 
     159                Edge *edge(int i){return edges[i];} 
    159160 
    160161                // 
     
    173174} 
    174175 
    175  
    176176// NAUTILUS_ADJPRIMS_INCLUDED 
    177177#endif 
     178 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/SimplificationMethod.cpp

    r1025 r1526  
    66using namespace std; 
    77using namespace Geometry; 
     8using   namespace       simplif; 
    89 
    910#include <gfx/geom/ProxGrid.h> 
     
    1314//      Constructor 
    1415//--------------------------------------------------------------------------- 
    15 SimplificationMethod::SimplificationMethod(const Mesh *m)  
     16SimplificationMethod::SimplificationMethod(const Mesh *m) 
    1617{ 
    1718        objmesh                                                 =       m; 
     
    1920        first_index_submesh     =       new unsigned int[objmesh->mSubMeshCount]; 
    2021        indexMeshLeaves                 =       -1; 
     22 
     23        //      Create simplification sequence. 
     24        msimpseq        =       new MeshSimplificationSequence(); 
    2125} 
    2226 
     
    4145        simplif::vert_info& v1_info = vertex_info(v1); 
    4246 
    43         simplif::Mat4 Q = v0_info.Q + v1_info.Q; 
    44         simplif::real norm = v0_info.norm + v1_info.norm; 
     47        simplif::Mat4 Q                 =       v0_info.Q + v1_info.Q; 
     48        simplif::real norm      =       v0_info.norm + v1_info.norm; 
    4549 
    4650        auxpair->cost = simplif::quadrix_pair_target(Q, v0, v1, auxpair->candidate); 
    4751 
    48         if( simplif::will_weight_by_area ) 
     52        if ( simplif::will_weight_by_area ) 
     53        { 
    4954                auxpair->cost /= norm; 
    50  
    51         if( simplif::will_preserve_mesh_quality ) 
     55        } 
     56 
     57        if ( simplif::will_preserve_mesh_quality ) 
     58        { 
    5259                auxpair->cost += pair_mesh_penalty(M0, v0, v1, auxpair->candidate); 
    53  
     60        } 
    5461 
    5562        // 
     
    5764        //          the heap is implemented as a MAX heap. 
    5865        // 
    59         if( auxpair->isInHeap() ) 
     66        if ( auxpair->isInHeap() ) 
    6067        { 
    6168                heap->update(auxpair, (float)-auxpair->cost); 
     
    7077//      Reasign the new vertex to the adequate face 
    7178//--------------------------------------------------------------------------- 
    72 int SimplificationMethod::predict_face(simplif::Face& F, simplif::Vertex *v1, simplif::Vertex *v2, simplif::Vec3& vnew, simplif::Vec3& f1, simplif::Vec3& f2, simplif::Vec3& f3) 
     79int SimplificationMethod::predict_face( simplif::Face& F, 
     80                                                                                                                                                                simplif::Vertex *v1, 
     81                                                                                                                                                                simplif::Vertex *v2, 
     82                                                                                                                                                                simplif::Vec3& vnew, 
     83                                                                                                                                                                simplif::Vec3& f1, 
     84                                                                                                                                                                simplif::Vec3& f2, 
     85                                                                                                                                                                simplif::Vec3& f3) 
    7386{ 
    7487        int nmapped = 0; 
    7588 
    7689        if( F.vertex(0) == v1 || F.vertex(0) == v2 ) 
    77         { f1 = vnew;  nmapped++; } 
    78         else f1 = *F.vertex(0); 
     90        { 
     91                f1 = vnew;  nmapped++; 
     92        } 
     93        else 
     94        { 
     95                f1 = *F.vertex(0); 
     96        } 
    7997 
    8098        if( F.vertex(1) == v1 || F.vertex(1) == v2 ) 
    81         { f2 = vnew;  nmapped++; } 
    82         else f2 = *F.vertex(1); 
     99        { 
     100                f2 = vnew;  nmapped++; 
     101        } 
     102        else 
     103        { 
     104                f2 = *F.vertex(1); 
     105        } 
    83106 
    84107        if( F.vertex(2) == v1 || F.vertex(2) == v2 ) 
    85         { f3 = vnew;  nmapped++; } 
    86         else f3 = *F.vertex(2); 
     108        { 
     109                f3 = vnew;  nmapped++; 
     110        } 
     111        else 
     112        { 
     113                f3 = *F.vertex(2); 
     114        } 
    87115 
    88116        return nmapped; 
     
    93121//--------------------------------------------------------------------------- 
    94122//--------------------------------------------------------------------------- 
    95 simplif::real SimplificationMethod::pair_mesh_penalty(simplif::Model& M, simplif::Vertex *v1, simplif::Vertex *v2, simplif::Vec3& vnew) 
     123simplif::real SimplificationMethod::pair_mesh_penalty(simplif::Model& M, simplif::Vertex *v1, 
     124                                                                                                                                                 simplif::Vertex *v2, 
     125                                                                                                                                                 simplif::Vec3& vnew) 
    96126{ 
    97127        static simplif::face_buffer changed; 
     128 
    98129        changed.reset(); 
    99130 
     
    103134        simplif::real Nmin = 0; 
    104135 
    105         for(int i=0; i<changed.length(); i++) 
     136        for (int i      =       0; i < changed.length(); i++) 
    106137        { 
    107138                simplif::Face& F = *changed(i); 
     
    114145                { 
    115146                        simplif::Plane Pnew(f1, f2, f3); 
     147 
    116148                        simplif::real delta =  Pnew.normal() * F.plane().normal(); 
    117149 
    118                         if( Nmin > delta ) Nmin = delta; 
     150                        if( Nmin > delta ) 
     151                        { 
     152                                Nmin = delta; 
     153                        } 
    119154                } 
    120155        } 
     
    122157        //return (-Nmin) * MESH_INVERSION_PENALTY; 
    123158        if( Nmin < 0.0 ) 
     159        { 
    124160                return MESH_INVERSION_PENALTY; 
     161        } 
    125162        else 
     163        { 
    126164                return 0.0; 
     165        } 
    127166} 
    128167 
     
    130169//      Returns true if the givens vertices are a valid pair 
    131170//--------------------------------------------------------------------------- 
    132 bool SimplificationMethod::check_for_pair(simplif::Vertex *v0, simplif::Vertex *v1) 
     171bool SimplificationMethod::check_for_pair(simplif::Vertex *v0, 
     172                                                                                                                                                                        simplif::Vertex *v1) 
    133173{ 
    134174        const simplif::pair_buffer& pairs = vertex_info(v0).pairs; 
     
    137177        { 
    138178                if( pairs(i)->v0==v1 || pairs(i)->v1==v1 ) 
     179                { 
    139180                        return true; 
     181                } 
    140182        } 
    141183 
     
    146188//      Create a new pair with two given vertices 
    147189//--------------------------------------------------------------------------- 
    148 simplif::pair_info *SimplificationMethod::new_pair(simplif::Vertex *v0, simplif::Vertex *v1) 
     190simplif::pair_info *SimplificationMethod::new_pair(     simplif::Vertex *v0, 
     191                                                                                                                                                                                                                simplif::Vertex *v1) 
    149192{ 
    150193        simplif::vert_info& v0_info = vertex_info(v0); 
     
    152195 
    153196        simplif::pair_info *pair = new simplif::pair_info(v0,v1); 
     197 
    154198        v0_info.pairs.add(pair); 
    155199        v1_info.pairs.add(pair); 
     
    169213        v1_info.pairs.remove(v1_info.pairs.find(auxpair)); 
    170214 
    171         if( auxpair->isInHeap() ) 
     215        if ( auxpair->isInHeap() ) 
     216        { 
    172217                heap->kill(auxpair->getHeapPos()); 
     218        } 
    173219 
    174220        delete auxpair; 
     
    178224//      Contract a given pair 
    179225//--------------------------------------------------------------------------- 
    180 void SimplificationMethod::do_contract(simplif::Model& m, simplif::pair_info *auxpair) 
    181 { 
    182         simplif::Vertex *v0 = auxpair->v0;  simplif::Vertex *v1 = auxpair->v1; 
     226void SimplificationMethod::do_contract( simplif::Model& m, 
     227                                                                                                                                                                simplif::pair_info *auxpair) 
     228{ 
     229        simplif::Vertex *v0                                     = auxpair->v0; 
     230        simplif::Vertex *v1                                     = auxpair->v1; 
    183231        simplif::vert_info& v0_info = vertex_info(v0); 
    184232        simplif::vert_info& v1_info = vertex_info(v1); 
    185         simplif::Vec3 vnew = auxpair->candidate; 
     233        simplif::Vec3 vnew                                      = auxpair->candidate; 
     234 
    186235        int i; 
     236         
     237        // V0 must be always the alive vertex 
     238        // V1 must be always the removed vertex 
     239        if (vnew == (*v0)) 
     240        { 
     241                //      Simplification step. 
     242                simplifstep.mV0 = v0->vID; 
     243                simplifstep.mV1 = v1->vID; 
     244        } 
     245        else 
     246        { 
     247                //      Simplification step. 
     248                simplifstep.mV0 = v1->vID; 
     249                simplifstep.mV1 = v0->vID; 
     250                v0->vID = v1->vID; 
     251        } 
    187252 
    188253        // Make v0 be the new vertex 
    189         v0_info.Q += v1_info.Q; 
    190         v0_info.norm += v1_info.norm; 
     254        v0_info.Q               += v1_info.Q; 
     255        v0_info.norm    += v1_info.norm; 
    191256 
    192257        simplif::Vec3 p(vnew); 
    193         simplif::Vec3 v0antes(v0->operator[](simplif::X),v0->operator[](simplif::Y),v0->operator[](simplif::Z)); 
    194  
    195         // Find the edge to remove (before remove it (contract())) 
    196         simplif::Edge *econ=NULL; 
    197         bool finded = false; 
    198         for (i=0; i<v0->edgeUses().length(); i++) 
    199         {                
    200                 econ = v0->edgeUses()(i); 
    201                 if (v1->vID == econ->org()->vID || v1->vID == econ->dest()->vID) 
    202                 { 
    203                         finded = true; 
    204                         break; 
    205                 } 
    206         } 
    207  
    208         assert(finded); 
    209  
    210  
    211         // Perform the actual contraction 
     258/*      simplif::Vec3 v0antes(v0->operator[](simplif::X), 
     259                                                  v0->operator[](simplif::Y), 
     260                                                  v0->operator[](simplif::Z));*/ 
     261 
     262        //      Perform the actual contraction. 
    212263        static simplif::face_buffer changed; 
     264 
    213265        changed.reset(); 
    214266        m.contract(v0, v1, vnew, changed); 
    215267 
    216         // Stores the simplification step 
    217         MeshSimplificationSequence::Step simplifstep; 
    218         simplif::Vec3 vdesp = p - v0antes; 
    219  
    220         // Stores the displacement in the simplification step 
    221         simplifstep.x=(float)vdesp[simplif::X]; 
    222         simplifstep.y = (float)vdesp[simplif::Y]; 
    223         simplifstep.z = (float)vdesp[simplif::Z]; 
    224          
    225         //      Submeshes which pertains each vertex 
    226         simplifstep.mV1 = v0->validID();  
    227  
    228         //      The simplification method returns as v1 the dead vertex. 
    229         simplifstep.mV0 = v1->validID(); 
     268        std::cout << "Simp seq: " << simplifstep.mV1 << " --> " << simplifstep.mV0 << std::endl; 
     269 
     270/*      simplif::Vec3 vdesp = p - v0antes; 
     271 
     272        //      Stores the displacement in the simplification step. 
     273        simplifstep.x   =       (float)vdesp[X]; 
     274        simplifstep.y = (float)vdesp[Y]; 
     275        simplifstep.z = (float)vdesp[Z];*/ 
     276 
     277        //      Stores the displacement in the simplification step. 
     278        simplifstep.x = (float)0.0f; 
     279        simplifstep.y = (float)0.0f; 
     280        simplifstep.z = (float)0.0f; 
    230281 
    231282        //      Number of triangles that are removed in this simplification step. 
     
    242293        int del_index   =       0; 
    243294 
    244         // mModfaces y mT0, mT1 of simplifstep stores the triangles id's -> geomesh has not triangles id's 
    245         for (i=0; i<changed.length(); i++) 
    246         { 
    247                 simplif::Face *auxface = changed(i); 
     295        //      Clears faces list. 
     296        simplifstep.mModfaces.clear(); 
     297 
     298        //      mModfaces y mT0, mT1 of simplifstep stores  
     299        //      the triangles id's -> geomesh has not triangles id's. 
     300        for (i  =       0;      i < changed.length();   i++) 
     301        { 
     302                simplif::Face *auxface  =       changed(i); 
    248303 
    249304                if (auxface->isValid()) 
    250305                { 
    251                         // Modified triangles 
     306                        //      Modified triangles. 
    252307                        simplifstep.mModfaces.push_back(auxface->validID()); 
    253308                } 
    254309                else 
    255310                { 
    256                         // Removed triangles 
    257                         if (_numDelTris==1)  
    258                         { 
    259                                 simplifstep.mT0=auxface->validID(); 
    260                                 simplifstep.mT1=auxface->validID(); 
     311                        //      Removed triangles. 
     312                        if (_numDelTris == 1) 
     313                        { 
     314                                simplifstep.mT0 =       auxface->validID(); 
     315                                simplifstep.mT1 =       auxface->validID(); 
    261316                        } 
    262317                        else 
    263318                        { 
    264                                 if(del_index==0) 
     319                                if(del_index == 0) 
    265320                                { 
    266                                         simplifstep.mT0=auxface->validID(); 
     321                                        simplifstep.mT0 =       auxface->validID(); 
    267322                                        del_index++; 
    268323                                } 
    269324                                else 
    270325                                { 
    271                                         simplifstep.mT1=auxface->validID(); 
     326                                        simplifstep.mT1 =       auxface->validID(); 
    272327                                } 
    273328                        } 
     
    275330        } 
    276331 
    277         decim_data.push_back(simplifstep); // Simplification sequence that make reference to the simplification method sequence 
     332        //      Simplification sequence that make reference to the  
     333        //      simplification method sequence. 
     334        //decim_data.push_back(simplifstep); 
     335 
     336        //      Adds new simplification step. 
     337        msimpseq->mSteps.push_back(simplifstep); 
    278338 
    279339#ifdef SUPPORT_VCOLOR 
    280         // 
    281         // If the vertices are colored, color the new vertex 
    282         // using the average of the old colors. 
     340        //      If the vertices are colored, color the new vertex 
     341        //      using the average of the old colors. 
    283342        v0->props->color += v1->props->color; 
    284343        v0->props->color /= 2; 
    285344#endif 
    286345 
    287         // Remove the pair that we just contracted 
     346        //      Remove the pair that we just contracted. 
    288347        delete_pair(auxpair); 
    289348 
    290         // Recalculate pairs associated with v0 
    291         for(i=0; i<v0_info.pairs.length(); i++) 
     349        //      Recalculate pairs associated with v0. 
     350        for (i  =       0;      i < v0_info.pairs.length();     i++) 
    292351        { 
    293352                simplif::pair_info *p = v0_info.pairs(i); 
     
    295354        } 
    296355 
    297         // Process pairs associated with now dead vertex 
    298         static simplif::pair_buffer condemned(6); // collect condemned pairs for execution 
     356        //      Process pairs associated with now dead vertex. 
     357        //      Collect condemned pairs for execution. 
     358        static simplif::pair_buffer condemned(6); 
     359 
    299360        condemned.reset(); 
    300361 
    301         for(i=0; i<v1_info.pairs.length(); i++) 
     362        for (i  =       0;      i < v1_info.pairs.length(); i++) 
    302363        { 
    303364                simplif::pair_info *p = v1_info.pairs(i); 
    304365 
    305366                simplif::Vertex *u; 
    306                 if( p->v0 == v1 )       
     367 
     368                if( p->v0 == v1 ) 
     369                { 
    307370                        u = p->v1; 
    308                 else  
    309                         if( p->v1 == v1)   
     371                } 
     372                else 
     373                { 
     374                        if( p->v1 == v1) 
     375                        { 
    310376                                u = p->v0; 
     377                        } 
    311378                        else 
     379                        { 
    312380                                std::cerr << "YOW!  This is a bogus pair." << endl; 
    313  
    314                 if( !check_for_pair(u, v0) ) 
    315                 { 
    316                         p->v0 = v0; 
    317                         p->v1 = u; 
     381                        } 
     382                } 
     383 
     384                if ( !check_for_pair(u, v0) ) 
     385                { 
     386                        p->v0   =       v0; 
     387                        p->v1   =       u; 
     388 
    318389                        v0_info.pairs.add(p); 
    319390                        compute_pair_info(p); 
    320391                } 
    321392                else 
     393                { 
    322394                        condemned.add(p); 
    323         } 
    324  
    325         for(i=0; i<condemned.length(); i++) 
     395                } 
     396        } 
     397 
     398        for (i  =       0;      i < condemned.length(); i++) 
     399        { 
    326400                // Do you have any last requests? 
    327401                delete_pair(condemned(i)); 
    328         v1_info.pairs.reset(); // safety precaution 
    329  
    330 } 
    331  
    332 //--------------------------------------------------------------------------- 
    333 //--------------------------------------------------------------------------- 
    334 bool SimplificationMethod::decimate_quadric(simplif::Vertex *v, simplif::Mat4& Q) 
     402        } 
     403 
     404        //      Safety precaution. 
     405        v1_info.pairs.reset(); 
     406} 
     407 
     408//--------------------------------------------------------------------------- 
     409//--------------------------------------------------------------------------- 
     410bool SimplificationMethod::decimate_quadric(simplif::Vertex *v, 
     411                                                                                                                                                                                simplif::Mat4& Q) 
    335412{ 
    336413        if( vinfo.length() > 0 ) 
     
    340417        } 
    341418        else 
     419        { 
    342420                return false; 
     421        } 
    343422} 
    344423 
     
    347426//--------------------------------------------------------------------------- 
    348427void SimplificationMethod::decimate_contract(simplif::Model& m) 
     428{ 
     429        simplif::heap_node      *top; 
     430        simplif::pair_info      *pair; 
     431        simplif::Vertex         *v0; 
     432        simplif::Vertex         *v1; 
     433        simplif::Vec3           candidate; 
     434 
     435        for (;;) 
     436        { 
     437                top     =       heap->extract(); 
     438                 
     439                if (!top) 
     440                { 
     441                        return; 
     442                } 
     443                 
     444                pair    =       (simplif::pair_info *)top->obj; 
     445 
     446                //      Remove all the vertices of the removed edges. 
     447                bool    sharededge      =       false; 
     448                 
     449                for (int        i       =       0;      i < pair->v0->edgeUses().length();      i++) 
     450                { 
     451                        simplif::Edge   *econ   =       pair->v0->edgeUses()(i); 
     452                         
     453                        if (pair->v1->vID == econ->org()->vID 
     454                                        || 
     455                                        pair->v1->vID == econ->dest()->vID) 
     456                        { 
     457                                sharededge      =       true; 
     458                                break; 
     459                        } 
     460                } 
     461 
     462                if (pair->isValid() && sharededge) 
     463                { 
     464                        break; 
     465                } 
     466 
     467                //      Deletes repetitions of pair in heap. 
     468                delete_pair(pair); 
     469        } 
     470 
     471        //      Copy pair. 
     472        v0      =       new Vertex(     pair->v0->operator[](0), 
     473                                                                                pair->v0->operator[](1), 
     474                                                                                pair->v0->operator[](2)); 
     475        v0->uniqID = pair->v0->uniqID; 
     476         
     477        v1      =       new Vertex(     pair->v1->operator[](0), 
     478                                                                                pair->v1->operator[](1), 
     479                                                                                pair->v1->operator[](2)); 
     480 
     481        v1->uniqID = pair->v1->uniqID; 
     482 
     483        candidate       =       pair->candidate; 
     484 
     485        //      First contract. 
     486        simplifstep.obligatory  =       0; 
     487 
     488        //      Debug. 
     489        cout    <<      "Contracting..."        <<      endl; 
     490 
     491        //      Contract main pair. 
     492        do_contract(m, pair); 
     493 
     494        //      Twin contract and lonely vertex contract. 
     495        simplifstep.obligatory  =       1; 
     496 
     497        //      Attempt to maintain valid vertex information. 
     498        M0.validVertCount--; 
     499 
     500        //      Find an edge of the same coordinates and different vertex identifiers. 
     501        removeTwinEdges(m,v0,v1,candidate); 
     502 
     503        //      Move vertex of non twin edges. 
     504        contractLonelyVertices(m,v0,v1,candidate); 
     505 
     506        //      Free Memory. 
     507        delete  v0; 
     508        delete  v1; 
     509} 
     510 
     511//--------------------------------------------------------------------------- 
     512//      Contract lonely vertex that has the same coords that the contracted one. 
     513//--------------------------------------------------------------------------- 
     514void    SimplificationMethod::contractLonelyVertices(   simplif::Model  &m,     simplif::Vertex *v0, 
     515                                                                                                                                                        simplif::Vertex *v1, 
     516                                                                                                                                                        simplif::Vec3   candidate) 
     517{ 
     518        bool                            lonely_vertex_found; 
     519        simplif::Vertex         *vert; 
     520        simplif::Vertex         *new_vert; 
     521        simplif::Vertex         *lonely_vert; 
     522        simplif::Edge           *edge; 
     523        simplif::heap_node      *top; 
     524        simplif::pair_info      *pair; 
     525        simplif::vert_info      new_vert_info; 
     526        Geometry::GeoVertex     vertex_added; 
     527        simplif::Vertex         *take_bone_from_vert; 
     528 
     529        //      Gets the vertex that is not the candidate. 
     530        if ((*v0) == candidate) 
     531        { 
     532                vert = v1; 
     533                take_bone_from_vert = v0; 
     534        } 
     535        else if ((*v1) == candidate) 
     536        { 
     537                vert = v0; 
     538                take_bone_from_vert = v1; 
     539        } 
     540 
     541        //      For all vertex. 
     542        for (int        i = 0; i < heap->getSize();     i++) 
     543        { 
     544                //      Initialize lonely vertex flag. 
     545                lonely_vertex_found     =       false; 
     546 
     547                //      Gets the current pair. 
     548                top             =       heap->getElement(i); 
     549                pair    =       (simplif::pair_info *)top->obj; 
     550 
     551                if (pair->v0->isValid() && (*vert) == (*pair->v0)) 
     552                { 
     553                        lonely_vert     =       pair->v0; 
     554                        lonely_vertex_found     =       true; 
     555                } 
     556                else if (pair->v1->isValid() && (*vert) == (*pair->v1)) 
     557                { 
     558                        lonely_vert     =       pair->v1; 
     559                        lonely_vertex_found     =       true; 
     560                } 
     561 
     562                //      If a lonely vertex is found. Creates new vertex and contract. 
     563                if (lonely_vertex_found) 
     564                { 
     565                        new_vert        =       m.newVertex(candidate[X],candidate[Y],candidate[Z]);     
     566                        edge            =       m.newEdge(new_vert,lonely_vert); 
     567 
     568            // We assume here the there are the same number of vertices and texture coordinates and normals 
     569 
     570                        // assign a texture coordinate for the vbertex 
     571                        simplif::Vec2 newtexcoord = m.texcoord(lonely_vert->validID()); 
     572                        m.in_TexCoord(newtexcoord); 
     573 
     574                        // assign a normal coordinate for the vbertex 
     575                        simplif::Vec3 newnormal = m.normal(lonely_vert->validID()); 
     576                        m.in_Normal(newnormal); 
     577 
     578                        //      Adds new vertex information to simplification sequence. 
     579                        vertex_added.id                 =       new_vert->vID; 
     580                        vertex_added.bonefrom   =       take_bone_from_vert->vID; 
     581                        vertex_added.position   =       Vector3(candidate[X],candidate[Y],candidate[Z]); 
     582 
     583                        simplif::Vec2 tc = m.texcoord(new_vert->validID()); 
     584                        vertex_added.texcoord   =       Vector2(tc[X],tc[Y]); 
     585 
     586                        simplif::Vec3 vn = m.normal(new_vert->validID()); 
     587                        vertex_added.normal             =       Vector3(vn[X],vn[Y],vn[Z]); 
     588 
     589                        msimpseq->mNewVertices.push_back(vertex_added); 
     590 
     591                        pair                    =       new_pair(new_vert,lonely_vert); 
     592                        pair->candidate =       candidate; 
     593 
     594                        pair->notInHeap(); 
     595/*                      vinfo(new_vert->validID()).Q            =       vinfo(lonely_vert->validID()).Q; 
     596                        vinfo(new_vert->validID()).norm =       vinfo(lonely_vert->validID()).norm;*/ 
     597 
     598                        //      Debug. 
     599                        cout    <<      "Contracting new pair..."       <<      endl; 
     600 
     601                        //      Contract twin pair. 
     602                        do_contract(m, pair); 
     603                        M0.validVertCount--; 
     604                } 
     605        } 
     606} 
     607 
     608//--------------------------------------------------------------------------- 
     609//      Find a twin edge of the given one. 
     610//--------------------------------------------------------------------------- 
     611void SimplificationMethod::removeTwinEdges(     simplif::Model  &m, 
     612                                                                                                                                                                                simplif::Vertex *v0, 
     613                                                                                                                                                                                simplif::Vertex *v1, 
     614                                                                                                                                                                                simplif::Vec3           candidate) 
     615{ 
     616        bool                                                            twin_found; 
     617        simplif::heap_node      *top; 
     618        simplif::pair_info      *pair; 
     619        simplif::Vertex                 *aux_vert; 
     620 
     621        //      Find a twin edge in heap. 
     622        for (int        i       =       0;      i < heap->getSize(); i++) 
     623        { 
     624                //      Initialize twin flag. 
     625                twin_found      =       false; 
     626 
     627                //      Gets the current pair. 
     628                top             =       heap->getElement(i); 
     629                pair    =       (simplif::pair_info *)top->obj; 
     630 
     631                if (pair->v0->isValid()==false || pair->v1->isValid()==false) 
     632                        continue;  
     633 
     634                if (v0->operator[](0) == pair->v0->operator[](0) 
     635                                && 
     636                                v0->operator[](1) == pair->v0->operator[](1) 
     637                                && 
     638                                v0->operator[](2) == pair->v0->operator[](2)) 
     639                { 
     640                        if (v1->operator[](0) == pair->v1->operator[](0) 
     641                                        && 
     642                                        v1->operator[](1) == pair->v1->operator[](1) 
     643                                        && 
     644                                        v1->operator[](2) == pair->v1->operator[](2)) 
     645                        { 
     646                                //      Debug. 
     647                                cout    <<      "Twin contracted (NO Swap)..."  <<      endl; 
     648 
     649                                //      Active twin flag. 
     650                                twin_found      = true; 
     651                        } 
     652                } 
     653                else 
     654                { 
     655                        //      If there is a twin edge in reverse order. 
     656                        if (v0->operator[](0) == pair->v1->operator[](0) 
     657                                        && 
     658                                        v0->operator[](1) == pair->v1->operator[](1) 
     659                                        && 
     660                                        v0->operator[](2) == pair->v1->operator[](2)) 
     661                        { 
     662                                if (v1->operator[](0) == pair->v0->operator[](0) 
     663                                                && 
     664                                                v1->operator[](1) == pair->v0->operator[](1) 
     665                                                && 
     666                                                v1->operator[](2) == pair->v0->operator[](2)) 
     667                                { 
     668                                        //      Debug. 
     669                                        cout    <<      "Twin contracted (Swap)..."     <<      endl; 
     670 
     671                                        //      Swap. 
     672                                        aux_vert        =       pair->v0; 
     673                                        pair->v0        =       pair->v1; 
     674                                        pair->v1        =       aux_vert; 
     675 
     676                                        //      Active twin flag. 
     677                                        twin_found      =       true; 
     678                                } 
     679                        } 
     680                } 
     681 
     682                //      If a twin edge has been found. 
     683                if (twin_found) 
     684                { 
     685                        //      Extract twin edge from heap. 
     686                        top             =       heap->extract(i); 
     687                        pair    =       (simplif::pair_info *)top->obj; 
     688 
     689                        //      Copy candidate. 
     690                        pair->candidate =       candidate; 
     691 
     692                        //      Contract twin pair. 
     693                        do_contract(m, pair); 
     694                        i--; 
     695                        M0.validVertCount--; 
     696                } 
     697        } 
     698} 
     699 
     700//--------------------------------------------------------------------------- 
     701//--------------------------------------------------------------------------- 
     702simplif::real SimplificationMethod::decimate_error(simplif::Vertex *v) 
     703{ 
     704        simplif::vert_info& info = vertex_info(v); 
     705 
     706        simplif::real err = simplif::quadrix_evaluate_vertex(*v, info.Q); 
     707 
     708    if( simplif::will_weight_by_area ) 
     709                { 
     710                        err /= info.norm; 
     711                } 
     712 
     713    return err; 
     714} 
     715 
     716//--------------------------------------------------------------------------- 
     717//      Extract the minimum cost of the the valid nodes (not simplified) 
     718//--------------------------------------------------------------------------- 
     719simplif::real SimplificationMethod::decimate_min_error() 
    349720{ 
    350721        simplif::heap_node *top; 
     
    353724        for(;;) 
    354725        { 
    355                 top = heap->extract(); 
    356                 if( !top ) return; 
     726                top = heap->top(); 
     727                 
     728                if( !top ) 
     729                { 
     730                        return -1.0; 
     731                } 
     732 
    357733                pair = (simplif::pair_info *)top->obj; 
    358734 
    359                 //Remove all the vertices of the removed edges 
    360                 bool sharededge= false; 
    361                 for (int i=0; i<pair->v0->edgeUses().length(); i++) 
    362                 {                
    363                         simplif::Edge *econ = pair->v0->edgeUses()(i); 
    364                         if (pair->v1->vID == econ->org()->vID || pair->v1->vID == econ->dest()->vID) 
    365                         { 
    366                                 sharededge= true; 
    367                                 break; 
    368                         } 
    369  
    370                 } 
    371  
    372                 if( pair->isValid() && sharededge) 
     735                if( pair->isValid() ) 
     736                { 
    373737                        break; 
    374  
    375                 delete_pair(pair); 
    376         } 
    377  
    378         do_contract(m, pair); 
    379  
    380         M0.validVertCount--;  // Attempt to maintain valid vertex information 
    381 } 
    382  
    383 //--------------------------------------------------------------------------- 
    384 //--------------------------------------------------------------------------- 
    385 simplif::real SimplificationMethod::decimate_error(simplif::Vertex *v) 
    386 { 
    387         simplif::vert_info& info = vertex_info(v); 
    388  
    389         simplif::real err = simplif::quadrix_evaluate_vertex(*v, info.Q); 
    390  
    391     if( simplif::will_weight_by_area ) 
    392                 err /= info.norm; 
    393  
    394     return err; 
    395 } 
    396  
    397 //--------------------------------------------------------------------------- 
    398 //      Extract the minimum cost of the the valid nodes (not simplified) 
    399 //--------------------------------------------------------------------------- 
    400 simplif::real SimplificationMethod::decimate_min_error() 
    401 { 
    402         simplif::heap_node *top; 
    403         simplif::pair_info *pair; 
    404  
    405         for(;;) 
    406         { 
    407                 top = heap->top(); 
    408                 if( !top ) return -1.0; 
    409                 pair = (simplif::pair_info *)top->obj; 
    410  
    411                 if( pair->isValid() ) 
    412                         break; 
     738                } 
    413739 
    414740                top = heap->extract(); 
     
    442768        int i,j; 
    443769 
    444         vinfo.init(m.vertCount()); 
    445  
    446         if( simplif::will_use_vertex_constraint ) 
    447                 for(i=0; i<m.vertCount(); i++) 
    448                 { 
    449                         simplif::Vertex *v = m.vertex(i); 
    450                         if( v->isValid() ) 
     770        //      Reserve memory for vert_info array. 
     771        vinfo.init(m.vertCount()*10); 
     772 
     773        if (simplif::will_use_vertex_constraint) 
     774        { 
     775                //      For each vertex, calculate contraints. 
     776                for     (i      =       0;      i < m.vertCount();      i++) 
     777                { 
     778                        simplif::Vertex *v      =       m.vertex(i); 
     779                         
     780                        if (v->isValid()) 
     781                        { 
    451782                                vertex_info(v).Q = simplif::quadrix_vertex_constraint(*v); 
    452                 } 
    453  
    454         for(i=0; i<m.faceCount(); i++) 
    455                 if( m.face(i)->isValid() ) 
    456                 { 
    457                         if( simplif::will_use_plane_constraint ) 
     783                        } 
     784                } 
     785        } 
     786 
     787        //      Sets the fill value of the vinfo buffer. 
     788        //vinfo.setFill(m.vertCount()); 
     789 
     790        //      For each face, recalculate constraints. 
     791        for (i  =       0;      i < m.faceCount();      i++) 
     792        { 
     793                if (m.face(i)->isValid()) 
     794                { 
     795                        if (simplif::will_use_plane_constraint) 
    458796                        { 
    459797                                simplif::Mat4 Q = simplif::quadrix_plane_constraint(*m.face(i)); 
    460798                                simplif::real norm = 0.0; 
    461799 
    462                                 if( simplif::will_weight_by_area ) 
     800                                if (simplif::will_weight_by_area) 
    463801                                { 
    464                                         norm = m.face(i)->area(); 
    465                                         Q *= norm; 
     802                                        norm    =               m.face(i)->area(); 
     803                                        Q                       *=      norm; 
    466804                                } 
    467805 
    468                                 for(j=0; j<3; j++) 
     806                                for (j  =       0;      j < 3;  j++) 
    469807                                { 
    470                                         vertex_info(m.face(i)->vertex(j)).Q += Q; 
    471                                         vertex_info(m.face(i)->vertex(j)).norm += norm; 
    472  
     808                                        vertex_info(m.face(i)->vertex(j)).Q                     +=      Q; 
     809                                        vertex_info(m.face(i)->vertex(j)).norm  +=      norm; 
    473810                                } 
    474811                        } 
    475812                } 
    476  
    477         if( simplif::will_constrain_boundaries ) 
    478         { 
    479                 for(i=0; i<m.edgeCount(); i++) 
    480                         if( m.edge(i)->isValid() && simplif::check_for_discontinuity(m.edge(i)) ) 
     813        } 
     814 
     815        if (simplif::will_constrain_boundaries) 
     816        { 
     817                //      For each edge, recalculate constraints. 
     818                for (i  =       0;      i < m.edgeCount();      i++) 
     819                { 
     820                        if (m.edge(i)->isValid() && simplif::check_for_discontinuity(m.edge(i))) 
    481821                        { 
    482822                                simplif::Mat4 B = simplif::quadrix_discontinuity_constraint(m.edge(i)); 
    483823                                simplif::real norm = 0.0; 
    484824 
    485                                 if( simplif::will_weight_by_area ) 
     825                                if (simplif::will_weight_by_area) 
    486826                                { 
    487                                         norm = simplif::norm2(*m.edge(i)->org() - *m.edge(i)->dest()); 
    488                                         B *= norm; 
     827                                        norm    =               simplif::norm2(*m.edge(i)->org() - *m.edge(i)->dest()); 
     828                                        B                       *=      norm; 
    489829                                } 
    490830 
     
    495835                                vertex_info(m.edge(i)->dest()).norm += norm; 
    496836                        } 
    497         } 
    498  
     837                } 
     838        } 
     839 
     840        //      Create heap. 
    499841        heap = new simplif::Heap(m.validEdgeCount); 
    500842 
    501843        int pair_count = 0; 
    502844 
    503         for(i=0; i<m.edgeCount(); i++) 
    504                 if( m.edge(i)->isValid() ) 
     845        //      For each edge, add pairs to heap. 
     846        for (i  =       0;      i < m.edgeCount();      i++) 
     847        { 
     848                if (m.edge(i)->isValid()) 
    505849                { 
    506850                        simplif::pair_info *pair = new_pair(m.edge(i)->org(), m.edge(i)->dest()); 
     
    509853                        pair_count++; 
    510854                } 
    511  
    512         if( limit<0 ) 
    513         { 
    514                 limit = m.bounds.radius * 0.05; 
    515         } 
    516         proximity_limit = limit * limit; 
    517         if( proximity_limit > 0 ) 
     855        } 
     856 
     857        if (limit < 0) 
     858        { 
     859                limit   =       m.bounds.radius * 0.05; 
     860        } 
     861         
     862        proximity_limit =       limit * limit; 
     863         
     864        if (proximity_limit > 0) 
    518865        { 
    519866                simplif::ProxGrid grid(m.bounds.min, m.bounds.max, limit); 
    520                 for(i=0; i<m.vertCount(); i++) 
     867                 
     868                //      Add points to grid. 
     869                for (i  =       0;      i < m.vertCount();      i++) 
     870                { 
    521871                        grid.addPoint(m.vertex(i)); 
     872                } 
    522873 
    523874                simplif::buffer<simplif::Vec3 *> nearby(32); 
    524                 for(i=0; i<m.vertCount(); i++) 
     875                 
     876                //      For each vertex. 
     877                for (i  =       0;      i < m.vertCount();      i++) 
    525878                { 
    526879                        nearby.reset(); 
    527880                        grid.proximalPoints(m.vertex(i), nearby); 
    528881 
    529                         for(j=0; j<nearby.length(); j++) 
     882                        for (j  =       0;      j < nearby.length();    j++) 
    530883                        { 
    531884                                simplif::Vertex *v1 = m.vertex(i); 
    532885                                simplif::Vertex *v2 = (simplif::Vertex *)nearby(j); 
    533886 
    534                                 if( v1->isValid() && v2->isValid() ) 
     887                                if (v1->isValid() && v2->isValid()) 
    535888                                { 
    536889#ifdef SAFETY 
    537890                                        assert(pair_is_valid(v1,v2)); 
    538891#endif 
    539                                         if( !check_for_pair(v1,v2) ) 
     892                                        if (!check_for_pair(v1,v2)) 
    540893                                        { 
    541894                                                simplif::pair_info *pair = new_pair(v1,v2); 
     
    544897                                        } 
    545898                                } 
    546  
    547899                        } 
    548900                } 
     
    555907void SimplificationMethod::simplifmethod_init(void) 
    556908{ 
    557         int i; 
    558  
    559909        // Change mesh structure. 
    560         geomesh2simplifModel(); 
    561  
     910        generateSimplifModel(); 
     911                 
    562912        M0.bounds.complete(); 
    563913 
     
    566916        initialFaceCount = M0.faceCount(); 
    567917 
    568         // Get rid of degenerate faces 
    569         for(i=0; i<M0.faceCount(); i++) 
    570                 if( !M0.face(i)->plane().isValid() ) 
     918        // Get rid of degenerate faces. 
     919        for (int        i       =       0;      i < M0.faceCount();     i++) 
     920        { 
     921                if (!M0.face(i)->plane().isValid()) 
     922                { 
    571923                        M0.killFace(M0.face(i)); 
     924                } 
     925        } 
    572926 
    573927        M0.removeDegeneracy(M0.allFaces()); 
    574928 
    575         // Get rid of unused vertices 
    576         for(i=0; i<M0.vertCount(); i++) 
    577         { 
    578                 if( M0.vertex(i)->edgeUses().length() == 0 ) 
     929        // Get rid of unused vertices. 
     930        for (int        i       =       0;      i < M0.vertCount();     i++) 
     931        { 
     932                if (M0.vertex(i)->edgeUses().length() == 0) 
     933                { 
    579934                        M0.vertex(i)->kill(); 
     935                } 
    580936        } 
    581937} 
     
    598954        } 
    599955 
    600         for (std::vector<MeshSimplificationSequence::Step>::iterator it= decim_data.begin(); it!=decim_data.end(); it++) 
     956        /* 
     957        for (   std::vector<MeshSimplificationSequence::Step>::iterator 
     958                                it      =       decim_data.begin(); 
     959                                it != decim_data.end(); 
     960                                it++) 
    601961        { 
    602962                it->mModfaces.clear(); 
    603963        } 
     964         
    604965        decim_data.clear(); 
    605  
    606         while( M0.validFaceCount > finalfaces/*simplif::face_target*/ 
    607                         && M0.validFaceCount > 1 
    608                         && decimate_min_error() < simplif::error_tolerance )  
     966        */ 
     967 
     968        //      Debug. 
     969        cout    <<      "M0.validFaceCount: "   <<      M0.validFaceCount       <<      endl; 
     970        cout    <<      "finalfaces: "                          <<      finalfaces                              <<      endl; 
     971         
     972        while ( M0.validFaceCount > finalfaces/*simplif::face_target*/ 
     973                                        && M0.validFaceCount > 1 
     974                                        && decimate_min_error() < simplif::error_tolerance ) 
    609975        { 
    610976                decimate_contract(M0); 
     
    6361002        previousValidVertCount  =       0; 
    6371003 
    638         percent =       (float)(2.0 * 100.0) /(float)((M0.validFaceCount - numvertices / 3.0)); 
     1004        percent =       (float)(2.0 * 100.0) 
     1005                                                / 
     1006                                                (float)((M0.validFaceCount - numvertices / 3.0)); 
    6391007 
    6401008        //      Update progress bar. 
     
    6441012        } 
    6451013 
    646         for (std::vector<MeshSimplificationSequence::Step>::iterator it= decim_data.begin(); it!=decim_data.end(); it++) 
     1014        /* 
     1015        for (   std::vector<MeshSimplificationSequence::Step>::iterator 
     1016                                it      =       decim_data.begin(); 
     1017                                it != decim_data.end(); 
     1018                                it++) 
    6471019        { 
    6481020                it->mModfaces.clear(); 
    6491021        } 
     1022         
    6501023        decim_data.clear(); 
     1024        */ 
     1025 
     1026        //      Debug. 
     1027        cout    <<      "M0.validVertCount:     "       <<      M0.validVertCount       <<      endl; 
     1028        cout    <<      "numvertices:   "       <<      numvertices     <<      endl; 
    6511029 
    6521030        while(  M0.validVertCount > numvertices /*simplif::face_target*/ 
     
    6801058{ 
    6811059        public: 
     1060 
    6821061                float x,y,z; 
    683                 _float3_(float x=0.0f, float y=0.0f, float z=0.0f){ this->x = x; this->y = y; this->z = z; } 
    684                 _float3_(const _float3_ &f){ x=f.x; y=f.y; z=f.z; } 
    685                 _float3_ & operator=(const _float3_ &f){ x=f.x; y=f.y; z=f.z; return *this; } 
     1062 
     1063                _float3_(float x=0.0f, float y=0.0f, float z=0.0f) 
     1064                { 
     1065                        this->x = x; this->y = y; this->z = z; 
     1066                } 
     1067 
     1068                _float3_(const _float3_ &f) 
     1069                { 
     1070                        x=f.x; y=f.y; z=f.z; 
     1071                } 
     1072 
     1073                _float3_ & operator=(const _float3_ &f) 
     1074                { 
     1075                        x=f.x; y=f.y; z=f.z; return *this; 
     1076                } 
     1077 
    6861078                bool operator<(const _float3_ &f) const  
    6871079                {  
     
    6961088}; 
    6971089 
    698  
    699 //--------------------------------------------------------------------------- 
    700 //Unify the model to work with it 
    701 //The vertices that have the same space coordinates are stored as an unique vertex 
    702 //in order to avoid holes in the simplification 
    703 //The necessary information to store correctly the decimation data is stored 
    704 //--------------------------------------------------------------------------- 
    705 void SimplificationMethod::geomesh2simplifModel(void) 
    706 { 
    707         std::map<_float3_,int> vertices_map; //Unique vertices (without repetitions) from all the submeshes 
    708         unsigned int num_final_verts = 0; //Number of unique vertices from all the submeshes 
    709  
    710         int num_final_verts2=0; 
    711         int num_repeated_verts=0; 
    712  
    713         int simplifindex; 
    714         for (size_t i=0; i<objmesh->mSubMeshCount; i++) //For all the submeshes 
    715         { 
    716                 if (i   !=      indexMeshLeaves) 
    717                 { 
    718                         //If the model has texture information 
    719                         if (objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS)  
    720                         { 
    721                                 //For all the vertices of each submesh 
    722                                 for (size_t j=0; j<objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; j++)  
    723                                 { 
    724                                         _float3_ auxvert(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x,  
     1090//--------------------------------------------------------------------------- 
     1091//      Generate simplification model. 
     1092//--------------------------------------------------------------------------- 
     1093void    SimplificationMethod::generateSimplifModel(void) 
     1094{ 
     1095        int     face_index      =       0; 
     1096 
     1097        //      For each submesh. 
     1098        bool added_vertices = false; 
     1099        for (size_t     i =     0;      i       <       objmesh->mSubMeshCount; i++) 
     1100        { 
     1101                //      If is not a tree leaves submesh. 
     1102                if (i != indexMeshLeaves) 
     1103                { 
     1104                        //      For all the vertices of each submesh. 
     1105                        for (   size_t  j       =       0; 
     1106                                        j < objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; 
     1107                                        j++) 
     1108                        { 
     1109                                //      If the vertex is not in vertices_map, then is added. 
     1110                                M0.in_Vertex(simplif::Vec3( 
     1111                                                        objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, 
    7251112                                                        objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 
    726                                                         objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z); 
    727  
    728                                         std::map<_float3_,int>::iterator mit = vertices_map.find(auxvert); 
    729                                         if (mit==vertices_map.end()) 
    730                                         { 
    731                                                 //If the vertex is not in vertices_map, then is added 
    732                                                 simplifindex=M0.in_Vertex(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x,  
    733                                                                         objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 
    734                                                                         objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); 
    735                                                 M0.in_Normal(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x,  
    736                                                                         objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, 
    737                                                                         objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); 
    738  
    739                                                 M0.in_TexCoord(simplif::Vec2(objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x,  
    740                                                                         objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y)); 
    741                                                 vertices_map[auxvert] = num_final_verts; 
    742                                                 num_final_verts ++; 
    743  
    744                                                 std::vector<int> v; 
    745                                                 submeshmap[simplifindex]=v; 
    746                                                 submeshmap[simplifindex].push_back((int)i); //Add the index of the mesh that contains the vertex 
    747                                                 std::vector<int> v2; 
    748                                                 vertexbuffermap[simplifindex]=v2; 
    749                                                 vertexbuffermap[simplifindex].push_back((int)j); //Add the index of vertex in the submesh 
    750  
    751                                                 // Debug 
    752                                                 num_final_verts2++; 
    753                                         } 
    754                                         else 
    755                                         { 
    756                                                 //the vertex is already in vertices_map, so store the reference of the first one with the same space coodinates 
    757                                                 simplifindex = mit->second; 
    758                                                 submeshmap[simplifindex].push_back((int)i); //Submeshes that contains the vertex 
    759                                                 vertexbuffermap[simplifindex].push_back((int)j); //Indices of the vertex buffers that contains the vertex 
    760  
    761                                                 // Debug 
    762                                                 num_repeated_verts++; 
    763                                         }        
    764                                 } 
    765                         } 
    766                         else //the model has not texture information 
    767                         {  
    768                                 //For all the vertices of each submesh 
    769                                 for (size_t j=0; j<objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; j++)  
    770                                 { 
    771                                         _float3_ auxvert(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x,  
    772                                                         objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 
    773                                                         objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z); 
    774  
    775                                         std::map<_float3_,int>::iterator mit = vertices_map.find(auxvert); 
    776                                         if (mit==vertices_map.end()) 
    777                                         { 
    778                                                 //If the vertex is not in vertices_map, then is added 
    779                                                 simplifindex=M0.in_Vertex(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x,  
    780                                                                         objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 
    781                                                                         objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); 
    782                                                 M0.in_Normal(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x,  
    783                                                                         objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, 
    784                                                                         objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); 
    785                                                 M0.in_TexCoord(simplif::Vec2(0.0,0.0)); 
    786                                                 vertices_map[auxvert] = num_final_verts; 
    787                                                 num_final_verts ++; 
    788  
    789                                                 std::vector<int> v; 
    790                                                 submeshmap[simplifindex]=v; 
    791                                                 submeshmap[simplifindex].push_back((int)i); //Add the index of the mesh that contains the vertex 
    792                                                 std::vector<int> v2; 
    793                                                 vertexbuffermap[simplifindex]=v2; 
    794                                                 vertexbuffermap[simplifindex].push_back((int)j); //Add the index of vertex in the submesh 
    795                                         } 
    796                                         else 
    797                                         { 
    798                                                 //the vertex is already in vertices_map, so store the reference of the first one with the same space coodinates 
    799                                                 simplifindex = mit->second; 
    800                                                 submeshmap[simplifindex].push_back((int)i); //Submeshes that contains the vertex 
    801                                                 vertexbuffermap[simplifindex].push_back((int)j); //Indices of the vertex buffers that contains the vertex 
    802                                         }        
    803                                 } 
    804                         } 
    805                         if (objmesh->mSubMesh[i].mSharedVertexBuffer) 
    806                         { 
    807                                 printf("Shared break"); 
    808                                 break; 
    809                         } 
    810                 } 
    811         } 
    812  
    813         //Create the faces 
    814         unsigned int base_index=0; 
    815  
    816         number_of_triangles=0; //Number of triangles of the model 
    817         int face_index=0; 
    818  
    819         int index_in_vertices_map0,index_in_vertices_map1,index_in_vertices_map2; 
    820         for (size_t i=0; i<objmesh->mSubMeshCount; i++) //For all the submeshes 
    821         {  
    822                 if (i!=indexMeshLeaves) 
    823                 { 
    824                         for (Index j=0; j<objmesh->mSubMesh[i].mIndexCount; j+=3)  
    825                         { 
    826                                 //+1 is required because the first index in the simplification method is 1 
    827                                 //Index of the vertex in vertices_map 
    828                                 Index indice0=objmesh->mSubMesh[i].mIndex[j]; 
    829                                 _float3_ auxvert0(objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice0].x,  
    830                                                 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice0].y, 
    831                                                 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice0].z); 
    832                                 index_in_vertices_map0=vertices_map[auxvert0] + 1; 
    833  
    834                                 Index indice1=objmesh->mSubMesh[i].mIndex[j+1]; 
    835                                 _float3_ auxvert1(objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice1].x,  
    836                                                 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice1].y, 
    837                                                 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice1].z); 
    838                                 index_in_vertices_map1=vertices_map[auxvert1] + 1; 
    839  
    840                                 Index indice2=objmesh->mSubMesh[i].mIndex[j+2]; 
    841                                 _float3_ auxvert2(objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice2].x,  
    842                                                 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice2].y, 
    843                                                 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice2].z); 
    844                                 index_in_vertices_map2=vertices_map[auxvert2] + 1; 
    845  
    846                                 //Create a triangle with its indices in vertices_map 
    847                                 face_index=M0.miin_Face(index_in_vertices_map0,index_in_vertices_map1,index_in_vertices_map2); 
    848  
    849                                 //igeo allows to identify the submesh that contains the triangle 
    850                                 M0.face(face_index)->igeo=(int)i; 
    851                         } 
    852                 } 
    853         } 
    854         number_of_triangles=face_index; 
    855 } 
    856  
    857  
    858 //--------------------------------------------------------------------------- 
    859 //Wrties a file with the vertices, triangles and the simplification sequence 
    860 //--------------------------------------------------------------------------- 
    861 void SimplificationMethod::WriteOBJ(void) 
    862 { 
    863         MeshSimplificationSequence::Step stepaux; 
    864         vector<int> v0,v1,submesh0,submesh1; 
    865         std::ofstream obj("salida.lod"); 
    866         obj << "begin" << std::endl; 
    867         //vertices 
    868         for (size_t i=0; i<objmesh->mSubMeshCount; i++)  
    869         { 
    870                 for (size_t j=0; j<objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; j++)  
    871                 {        
    872                         obj << "v " <<  objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x << " " << 
    873                                 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y << " " << 
    874                                 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z << " " << std::endl; 
    875                 } 
    876         } 
    877  
    878  
    879         //faces 
    880         for (size_t i=0; i<objmesh->mSubMeshCount; i++)  
    881         { 
    882                 for (size_t j=0; j<objmesh->mSubMesh[i].mIndexCount; j=j+3)  
    883                 {        
    884                         obj << "f " <<  objmesh->mSubMesh[i].mIndex[j]+1 << " " <<  
    885                                 objmesh->mSubMesh[i].mIndex[j+1]+1 << " " <<  
    886                                 objmesh->mSubMesh[i].mIndex[j+2]+1 << std::endl; 
    887                 } 
    888         } 
    889  
    890         obj << "end" << std::endl; 
    891  
    892         for (unsigned i=0; i<decim_data.size(); i++)  
    893         { 
    894                 stepaux = decim_data.operator [](i); 
    895                 v0=vertexbuffermap[stepaux.mV0];  //vertexbuffer index 
    896                 submesh0=submeshmap[stepaux.mV0]; //displacement for each submesh 
    897                 v1=vertexbuffermap[stepaux.mV1]; 
    898                 submesh1=submeshmap[stepaux.mV1]; 
    899                 for (unsigned j=0; j<v0.size(); j++) 
    900                 { 
    901                         for (unsigned k=0; k<v1.size(); k++)  
    902                         { 
    903                                 //obtain the index of the VertexBuffer of each submesh and the displacemente by submesh 
    904                                 obj << "v% " << v1.operator [](k)+first_index_submesh[submesh1.operator [](k)] <<  
    905                                         " " << v0.operator [](j)+first_index_submesh[submesh0.operator [](j)] << " " <<  
    906                                         stepaux.x << " " << stepaux.y << " " << stepaux.z << " " << stepaux.mT0 << " " << 
    907                                         stepaux.mT1 << " & "; 
    908                                 if (stepaux.mModfaces.size()>0)  
    909                                 { 
    910                                         unsigned int ii=0; 
    911                                         for (ii=0; ii<stepaux.mModfaces.size()-1; ii++)  
    912                                         { 
    913                                                 obj << stepaux.mModfaces.operator [](ii) << " "; 
    914                                         } 
    915                                         obj << stepaux.mModfaces.operator [](ii) << std::endl; 
    916                                 } 
    917                                 else  
    918                                 {  
    919                                         obj << std::endl;  
    920                                 }                                        
    921                         } 
    922                 } 
    923         } 
    924         obj.close(); 
    925 } 
    926  
    927 //--------------------------------------------------------------------------- 
    928 //simplifmethod_init is executed to do some initalizations 
    929 //simplifmethod_run is executed to do the decimation 
    930 //Then, the simpified model is created and the decimation information is returned 
     1113                                                        objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); 
     1114 
     1115                                M0.in_Normal(simplif::Vec3( 
     1116                                                        objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x, 
     1117                                                        objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, 
     1118                                                        objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); 
     1119 
     1120                                M0.in_TexCoord(simplif::Vec2( 
     1121                                                        objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x, 
     1122                                                        objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y)); 
     1123 
     1124                                added_vertices = true; 
     1125                        } 
     1126                }        
     1127                 
     1128                //      If is shared vertex buffer object. 
     1129                if (objmesh->mSubMesh[i].mSharedVertexBuffer && added_vertices) 
     1130                { 
     1131                        printf("Shared break"); 
     1132                        break; 
     1133                } 
     1134        } 
     1135 
     1136        //      For each submesh. 
     1137        for (size_t     i = 0;  i < objmesh->mSubMeshCount;     i++) 
     1138        { 
     1139                if (i != indexMeshLeaves) 
     1140                { 
     1141                        for (Index      j       =       0;      j < objmesh->mSubMesh[i].mIndexCount;   j       +=      3) 
     1142                        { 
     1143                                //      +1 is required because the first index in the 
     1144                                //      simplification method is 1 
     1145                                //      Index of the vertex in vertices_map. 
     1146                                Index index0    =               objmesh->mSubMesh[i].mIndex[j]; 
     1147                                 
     1148                                Index index1    =               objmesh->mSubMesh[i].mIndex[j + 1]; 
     1149                                 
     1150                                Index index2    =               objmesh->mSubMesh[i].mIndex[j + 2]; 
     1151                                 
     1152                                //      Create a triangle with its indices in vertices_map. 
     1153                                face_index      =       M0.miin_Face( 
     1154                                                index0, 
     1155                                                index1, 
     1156                                                index2); 
     1157 
     1158                                //      igeo allows to identify the submesh that contains the triangle. 
     1159                                M0.face(face_index)->igeo       =       (int)i; 
     1160                        } 
     1161                } 
     1162        } 
     1163        number_of_triangles     =       face_index; 
     1164} 
     1165 
     1166//--------------------------------------------------------------------------- 
     1167//      simplifmethod_init is executed to do some initalizations 
     1168//      simplifmethod_run is executed to do the decimation 
     1169//      Then, the simpified model is created and the decimation  
     1170//      information is returned. 
    9311171//--------------------------------------------------------------------------- 
    9321172MeshSimplificationSequence *SimplificationMethod::Decimate(     float lod, 
     
    9381178        if (simpliftype == 0) 
    9391179        { 
    940                 //Percentage option 
     1180                //      Percentage option. 
    9411181                simplifmethod_run((int)(number_of_triangles*lod),upb); 
    9421182        } 
    9431183        else 
    9441184        { 
    945                 //Number of vertices option 
     1185                //      Number of vertices option. 
    9461186                simplifmethod_runv((int)lod,upb); 
    9471187        } 
     
    9531193        typedef std::vector<int> REPINDLIST; 
    9541194 
    955         MAPAINDIND *unique_verts_inds_by_geo = new MAPAINDIND[objmesh->mSubMeshCount];  
    956         REPINDLIST *ver_inds_rep_by_geo         = new REPINDLIST[objmesh->mSubMeshCount];  
     1195        MAPAINDIND *unique_verts_inds_by_geo = 
     1196                                                                                                                new MAPAINDIND[objmesh->mSubMeshCount]; 
     1197 
     1198        REPINDLIST *ver_inds_rep_by_geo = new REPINDLIST[objmesh->mSubMeshCount]; 
    9571199 
    9581200        //      Index counter by submesh. 
     
    9641206        } 
    9651207 
    966  
    9671208        //      Construct the model. 
     1209        std::cout << "M0.faceCount(): " << M0.faceCount() << std::endl; 
    9681210        for (int        i = 0;  i < M0.faceCount();     i++) 
    9691211        { 
     
    9761218 
    9771219                        //      Insert to each submesh its triangles. 
    978                         std::map<int,int>::iterator findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(0)->validID()); 
     1220                        std::map<int,int>::iterator findit = 
     1221                                unique_verts_inds_by_geo[igeo].find(auxface->vertex(0)->validID()); 
     1222                         
    9791223                        if (findit == unique_verts_inds_by_geo[igeo].end()) 
    9801224                        { 
    9811225                                //      If it is not added... add and map it. 
    982                                 unique_verts_inds_by_geo[igeo][auxface->vertex(0)->validID()] = inextvert[igeo]; 
     1226                                unique_verts_inds_by_geo[igeo][auxface->vertex(0)->validID()] = 
     1227                                                                                                                                                                                                                                                inextvert[igeo]; 
     1228                                 
    9831229                                inextvert[igeo]++; 
    984                         }                        
    985                         findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(1)->validID()); 
     1230                        } 
     1231 
     1232                        findit = 
     1233                                unique_verts_inds_by_geo[igeo].find(auxface->vertex(1)->validID()); 
     1234                         
    9861235                        if (findit == unique_verts_inds_by_geo[igeo].end()) 
    9871236                        { 
    9881237                                //      If it is not added... add and map it. 
    989                                 unique_verts_inds_by_geo[igeo][auxface->vertex(1)->validID()] = inextvert[igeo]; 
     1238                                unique_verts_inds_by_geo[igeo][auxface->vertex(1)->validID()] = 
     1239                                                                                                                                                                                                                                                inextvert[igeo]; 
    9901240                                inextvert[igeo]++; 
    991                         }                        
    992                         findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(2)->validID()); 
     1241                        } 
     1242                         
     1243                        findit =  
     1244                                unique_verts_inds_by_geo[igeo].find(auxface->vertex(2)->validID()); 
     1245                         
    9931246                        if (findit == unique_verts_inds_by_geo[igeo].end()) 
    9941247                        { 
    9951248                                //      If it is not added... add and map it. 
    996                                 unique_verts_inds_by_geo[igeo][auxface->vertex(2)->validID()] = inextvert[igeo]; 
     1249                                unique_verts_inds_by_geo[igeo][auxface->vertex(2)->validID()] =  
     1250                                                                                                                                                                                                                                                inextvert[igeo]; 
    9971251                                inextvert[igeo]++; 
    998                         }                        
     1252                        } 
    9991253 
    10001254                        //      Total number of indices by submesh. 
     
    10431297        bool copiedShared = false; 
    10441298 
     1299        //      For each submesh. 
    10451300        for (size_t     i = 0;  i < objmesh->mSubMeshCount;     i++) 
    10461301        { 
    1047                 mGeoMesh->mSubMesh[i].mStripCount                                       =       0; 
    1048                 mGeoMesh->mSubMesh[i].mStrip                                                    =       NULL; 
     1302                mGeoMesh->mSubMesh[i].mStripCount       =       0; 
     1303                mGeoMesh->mSubMesh[i].mStrip                    =       NULL; 
    10491304 
    10501305                mGeoMesh-> 
     
    10601315                        { 
    10611316                                mGeoMesh->mSubMesh[i].mBones.push_back(objmesh-> 
    1062                                                 mSubMesh[i].mBones[j]); 
     1317                                                                                                                                                                mSubMesh[i].mBones[j]); 
    10631318                        } 
    10641319                } 
     
    10681323                        //      Indices vectors. 
    10691324                        mGeoMesh->mSubMesh[i].mIndexCount       =       ver_inds_rep_by_geo[i].size(); 
     1325                         
    10701326                        mGeoMesh->mSubMesh[i].mIndex                    =       new Index[mGeoMesh-> 
    1071                                 mSubMesh[i].mIndexCount]; 
     1327                                                                                                                                                                                mSubMesh[i].mIndexCount]; 
    10721328 
    10731329                        //      Store the indices. 
     
    10751331                        { 
    10761332                                //      Obtain the indices that point at VertexBuffer. 
    1077                                 mGeoMesh->mSubMesh[i].mIndex[j] =       unique_verts_inds_by_geo[i].operator [](ver_inds_rep_by_geo[i].operator [](j)); 
     1333                                mGeoMesh->mSubMesh[i].mIndex[j] = 
     1334                                        unique_verts_inds_by_geo[i]. 
     1335                                                                operator [](ver_inds_rep_by_geo[i]. 
     1336                                                                operator [](j)); 
    10781337                        } 
    10791338 
     
    10961355                        } 
    10971356 
    1098                         mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount       =       unique_verts_inds_by_geo[i].size(); 
    1099                         mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo        =       objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 
     1357                        mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount       = 
     1358                                                                                                                                        unique_verts_inds_by_geo[i].size(); 
     1359                         
     1360                        mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo        = 
     1361                                                                                        objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 
    11001362 
    11011363                        //      Allocate memory for position, normal and texutre coordinates. 
    1102                         mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition  =       new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
    1103                         mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal            =       new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
    1104                         mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords =       new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
     1364                        mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition  = 
     1365                                                new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
     1366                         
     1367                        mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal            = 
     1368                                                new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
     1369                         
     1370                        mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords = 
     1371                                                new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
    11051372 
    11061373                        for (   MAPAINDIND::iterator mapit = unique_verts_inds_by_geo[i].begin(); 
     
    11141381                                //      Vertex coordinate. 
    11151382                                //      Construction of the submeshes. 
    1116                                 Vector3 v3((Real)M0.vertex(isrc)->operator [](0), 
    1117                                                 (Real)M0.vertex(isrc)->operator [](1), 
    1118                                                 (Real)M0.vertex(isrc)->operator [](2)); 
    1119                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[idst]=v3; 
     1383                                Vector3 v3(     (Real)M0.vertex(isrc)->operator [](0), 
     1384                                                                                (Real)M0.vertex(isrc)->operator [](1), 
     1385                                                                                (Real)M0.vertex(isrc)->operator [](2)); 
     1386                                 
     1387                                mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[idst]    =       v3; 
    11201388 
    11211389                                //      Normal coordinates. 
    1122                                 Vector3 v3n((Real)M0.normal(isrc)(0), (Real)M0.normal(isrc)(1), (Real)M0.normal(isrc)(2)); 
    1123                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[idst]=v3n; 
     1390                                Vector3 v3n(    (Real)M0.normal(isrc)(0), 
     1391                                                                                        (Real)M0.normal(isrc)(1), 
     1392                                                                                        (Real)M0.normal(isrc)(2)); 
     1393                                 
     1394                                mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[idst]      =       v3n; 
    11241395 
    11251396                                //      Texture coordinates. 
    1126                                 Vector2 v2((Real)M0.texcoord(isrc)(0), (Real)M0.texcoord(isrc)(1)); 
    1127                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[idst]=v2; 
     1397                                Vector2 v2(     (Real)M0.texcoord(isrc)(0), 
     1398                                                                                (Real)M0.texcoord(isrc)(1)); 
     1399                                 
     1400                                mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[idst]   =       v2; 
    11281401                        } 
    11291402                } 
     
    11311404                { 
    11321405                        //      Leaves mesh. 
    1133                         mGeoMesh->mSubMesh[i].mIndexCount=objmesh->mSubMesh[i].mIndexCount; 
    1134                         mGeoMesh->mSubMesh[i].mIndex=new Index[mGeoMesh->mSubMesh[i].mIndexCount]; 
     1406                        mGeoMesh->mSubMesh[i].mIndexCount       =       objmesh->mSubMesh[i].mIndexCount; 
     1407                         
     1408                        mGeoMesh->mSubMesh[i].mIndex    =               new Index[mGeoMesh->mSubMesh[i].mIndexCount]; 
    11351409 
    11361410                        //      Copy the leaves submesh indexes. 
    1137                         for (unsigned int j=0; j<mGeoMesh->mSubMesh[i].mIndexCount; j++) 
     1411                        for (unsigned int       j       =       0;      j < mGeoMesh->mSubMesh[i].mIndexCount;  j++) 
    11381412                        {        
    1139                                 mGeoMesh->mSubMesh[i].mIndex[j]=objmesh->mSubMesh[i].mIndex[j]; 
     1413                                mGeoMesh->mSubMesh[i].mIndex[j] =       objmesh->mSubMesh[i].mIndex[j]; 
    11401414                        } 
    11411415 
    11421416                        //      Copy the leaves submesh vertices. 
    1143                         mGeoMesh->mSubMesh[i].mVertexBuffer=new VertexBuffer(); 
    1144                         mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount=objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; 
    1145                         mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo=objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 
     1417                        mGeoMesh->mSubMesh[i].mVertexBuffer     =       new VertexBuffer(); 
     1418                         
     1419                        mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount       = 
     1420                                                                                                                objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; 
     1421                         
     1422                        mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo        = 
     1423                                                                                                                objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 
    11461424 
    11471425                        //      Allocate memory for position, normal and texture coordinates. 
    1148                         mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition=new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
    1149                         mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal=new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
    1150                         mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords=new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
     1426                        mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition  = 
     1427                                                        new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
     1428                         
     1429                        mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal    = 
     1430                                                        new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
     1431                         
     1432                        mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords = 
     1433                                                        new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 
    11511434 
    11521435                        for (   unsigned int j = 0; 
     
    11551438                        { 
    11561439                                //      Position. 
    1157                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].x=objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x; 
    1158                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].y=objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y; 
    1159                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].z=objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z; 
     1440                                mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].x     = 
     1441                                                                        objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x; 
     1442                                 
     1443                                mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].y     = 
     1444                                                                        objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y; 
     1445                                 
     1446                                mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].z     = 
     1447                                                                        objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z; 
    11601448 
    11611449                                //      Normals. 
    1162                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].x=objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x; 
    1163                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].y=objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y; 
    1164                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].z=objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z; 
     1450                                mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].x       = 
     1451                                                                        objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x; 
     1452                                 
     1453                                mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].y       = 
     1454                                                                        objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y; 
     1455                                 
     1456                                mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].z       = 
     1457                                                                        objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z; 
    11651458 
    11661459                                //      Textures. 
    1167                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x=objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x; 
    1168                                 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y=objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y; 
     1460                                mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x    = 
     1461                                                                        objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x; 
     1462                                 
     1463                                mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y    = 
     1464                                                                        objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y; 
    11691465                        } 
    11701466                } 
     
    11761472 
    11771473        //      Store the simplification steps in MeshSimplificationSequence. 
    1178         int acum        =       0; 
     1474        int     acum    =       0; 
    11791475 
    11801476        for (size_t i   =       0; i < objmesh->mSubMeshCount; i++) 
     
    11921488        } 
    11931489 
    1194         MeshSimplificationSequence::Step stepaux, newstep; 
    1195         vector<int> v0,v1,submesh0,submesh1; 
    1196         MeshSimplificationSequence *msimpseq; 
    1197  
    1198         msimpseq        =       new MeshSimplificationSequence(); 
    1199  
    1200         for (unsigned i = 0;    i < decim_data.size();  i++)  
    1201         { 
    1202                 stepaux         =       decim_data.operator [](i); 
    1203                 v0                              =       vertexbuffermap[stepaux.mV0];  // vertexbuffer index 
    1204                 submesh0        =       submeshmap[stepaux.mV0]; // displacement by submesh 
    1205                 v1                              =       vertexbuffermap[stepaux.mV1]; 
    1206                 submesh1        =       submeshmap[stepaux.mV1]; 
    1207  
    1208                 for (unsigned j = 0;    j < v0.size();  j++) 
    1209                 { 
    1210                         for (unsigned k = 0;    k < v1.size(); k++) 
    1211                         { 
    1212                                 //      Obtain the submesh index in VertexBuffer 
    1213                                 //      and the displacement by submesh 
    1214                                 newstep.mV0     =       v0.operator [](j)+first_index_submesh[submesh0.operator [](j)]; 
    1215                                 newstep.mV1     =       v1.operator [](k)+first_index_submesh[submesh1.operator [](k)]; 
    1216                                 newstep.x               =       stepaux.x; 
    1217                                 newstep.y               =       stepaux.y; 
    1218                                 newstep.z               =       stepaux.z; 
    1219  
    1220                                 // mT0 y mT1 are unique triangles identifiers 
    1221                                 // returned by the simplification method 
    1222                                 newstep.mT0     =       stepaux.mT0; 
    1223                                 newstep.mT1     =       stepaux.mT1; 
    1224  
    1225                                 if (j == v0.size() - 1 && k == v1.size() - 1) 
    1226                                 { 
    1227                                         newstep.obligatorio     =       0; 
    1228                                 } 
    1229                                 else 
    1230                                 { 
    1231                                         newstep.obligatorio     =       1; 
    1232                                 } 
    1233  
    1234                                 if (j == 0 && k == 0) 
    1235                                 { 
    1236                                         //      First simplification step. 
    1237                                         //      unique triangle identifiers  
    1238                                         //      returned by the simplification method. 
    1239                                         newstep.mModfaces       =       stepaux.mModfaces; 
    1240                                 } 
    1241                                 else  
    1242                                 { 
    1243                                         std::vector<Index>      vvacio; 
    1244  
    1245                                         newstep.mModfaces       =       vvacio; 
    1246                                 } 
    1247  
    1248                                 msimpseq->mSteps.push_back(newstep); 
    1249                         } 
    1250                 } 
    1251         } 
    1252  
    1253         // WriteOBJ(); 
     1490        vector<int>     v0; 
     1491        vector<int>     v1; 
     1492        vector<int>     submesh0; 
     1493        vector<int>     submesh1; 
     1494 
    12541495        return msimpseq; 
    12551496} 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/SimplificationMethod.h

    r1025 r1526  
    99#include <GeoMeshSimplifier.h> 
    1010 
     11using   namespace       Geometry; 
     12 
    1113class SimplificationMethod 
    1214{ 
    13 private: 
    14         Geometry::Mesh  *mGeoMesh; 
    15         void compute_pair_info(simplif::pair_info*); 
    16         simplif::Model M0; 
    17         simplif::array<simplif::vert_info> vinfo; 
    18         simplif::Heap *heap; 
    19         simplif::real proximity_limit;    // distance threshold squared 
    20         int initialVertCount; 
    21         int initialEdgeCount; 
    22         int initialFaceCount; 
     15        private: 
    2316 
    24         inline simplif::vert_info& vertex_info(simplif::Vertex *v) 
    25         { 
    26                 return vinfo(v->validID()); 
    27         } 
    28          
    29         simplif::real pair_mesh_penalty(simplif::Model& M, 
    30                                                                                                                                 simplif::Vertex *v1, 
    31                                                                                                                                 simplif::Vertex *v2, 
    32                                                                                                                                 simplif::Vec3& vnew); 
    33          
    34         int predict_face(simplif::Face& F, 
    35                                                                         simplif::Vertex *v1, 
    36                                                                         simplif::Vertex *v2, 
    37                                                                         simplif::Vec3& vnew, 
    38                                                                         simplif::Vec3& f1, 
    39                                                                         simplif::Vec3& f2, 
    40                                                                         simplif::Vec3& f3); 
    41          
    42         bool check_for_pair(simplif::Vertex *v0, simplif::Vertex *v1); 
    43          
    44         simplif::pair_info *new_pair(simplif::Vertex *v0, simplif::Vertex *v1); 
    45          
    46         void delete_pair(simplif::pair_info *pair); 
    47          
    48         void do_contract(simplif::Model& m, simplif::pair_info *pair); 
    49          
    50         bool decimate_quadric(simplif::Vertex *v, simplif::Mat4& Q); 
    51          
    52         void decimate_contract(simplif::Model& m); 
    53          
    54         simplif::real decimate_error(simplif::Vertex *v); 
    55          
    56         simplif::real decimate_min_error(); 
    57          
    58         simplif::real decimate_max_error(simplif::Model& m); 
    59          
    60         void decimate_init(simplif::Model& m, simplif::real limit); 
    61          
    62         bool pair_is_valid(simplif::Vertex *u, simplif::Vertex *v); 
    63          
    64         void simplifmethod_run(int,     Geometry::TIPOFUNC      upb=0); 
    65          
    66         void simplifmethod_runv(int,    Geometry::TIPOFUNC      upb=0); 
    67          
    68         void simplifmethod_init(void); 
     17                Geometry::Mesh  *mGeoMesh; 
    6918 
    70         //      To map the mesh with de simplification method structure. 
    71         //      Submeshes which pertains each vertex. 
    72         std::map<int,std::vector<int> > submeshmap; 
    73          
    74         //      Vertices of the VertexBuffer that point  
    75         //      at this vertex of the simplification method. 
    76         std::map<int,std::vector<int> > vertexbuffermap;  
     19                void compute_pair_info(simplif::pair_info*); 
    7720 
    78         //      Simplification sequence of the simplification method. 
    79         std::vector<Geometry::MeshSimplificationSequence::Step> decim_data; 
    80          
    81         std::string meshName; 
    82          
    83         void WriteOBJ(void); 
    84          
    85         unsigned int *first_index_submesh; 
    86          
    87         //std::vector<simplif::pair_info *> pointers_to_remove; 
     21                simplif::Model M0; 
    8822 
    89         int indexMeshLeaves; 
    90          
    91 public: 
    92          
    93         const Geometry::Mesh *objmesh; 
    94          
    95         //      Total number of triangles of all the submeshes. 
    96         int number_of_triangles; 
    97          
    98         SimplificationMethod(const Geometry::Mesh *m); 
    99          
    100         void geomesh2simplifModel(void); 
    101          
    102         Geometry::MeshSimplificationSequence *Decimate( float lod, 
    103                                                                                                                                                                                                         int simpliftype, 
    104                                                                                                                                                                                                         Geometry::TIPOFUNC      upb=0); 
    105          
    106         void setMeshLeaves(int meshLeaves); 
     23                simplif::array<simplif::vert_info> vinfo; 
    10724 
    108         ///     Gets mesh simplified. 
    109         Geometry::Mesh  *       GetMesh(); 
    110          
    111         ///Destructor 
    112         ~SimplificationMethod(); 
     25                simplif::Heap *heap; 
     26                simplif::real proximity_limit;    // distance threshold squared 
     27 
     28                int initialVertCount; 
     29                int initialEdgeCount; 
     30                int initialFaceCount; 
     31 
     32                //      Simplification sequence. 
     33                MeshSimplificationSequence      *msimpseq; 
     34 
     35                //      Stores the simplification step. 
     36                MeshSimplificationSequence::Step simplifstep; 
     37 
     38                inline simplif::vert_info& vertex_info(simplif::Vertex *v) 
     39                { 
     40                        return vinfo(v->validID()); 
     41                } 
     42 
     43                simplif::real pair_mesh_penalty(simplif::Model& M, 
     44                                simplif::Vertex *v1, 
     45                                simplif::Vertex *v2, 
     46                                simplif::Vec3& vnew); 
     47 
     48                int predict_face(simplif::Face& F, 
     49                                simplif::Vertex *v1, 
     50                                simplif::Vertex *v2, 
     51                                simplif::Vec3& vnew, 
     52                                simplif::Vec3& f1, 
     53                                simplif::Vec3& f2, 
     54                                simplif::Vec3& f3); 
     55 
     56                bool check_for_pair(simplif::Vertex *v0, simplif::Vertex *v1); 
     57 
     58                simplif::pair_info *new_pair(simplif::Vertex *v0, simplif::Vertex *v1); 
     59 
     60                void delete_pair(simplif::pair_info *pair); 
     61 
     62                void do_contract(simplif::Model& m, simplif::pair_info *pair); 
     63 
     64                bool decimate_quadric(simplif::Vertex *v, simplif::Mat4& Q); 
     65 
     66                void decimate_contract(simplif::Model& m); 
     67 
     68                simplif::real decimate_error(simplif::Vertex *v); 
     69 
     70                simplif::real decimate_min_error(); 
     71 
     72                simplif::real decimate_max_error(simplif::Model& m); 
     73 
     74                void decimate_init(simplif::Model& m, simplif::real limit); 
     75 
     76                bool pair_is_valid(simplif::Vertex *u, simplif::Vertex *v); 
     77 
     78                void simplifmethod_run(int,     Geometry::TIPOFUNC      upb=0); 
     79 
     80                void simplifmethod_runv(int,    Geometry::TIPOFUNC      upb=0); 
     81 
     82                void simplifmethod_init(void); 
     83 
     84                //      To map the mesh with de simplification method structure. 
     85                //      Submeshes which pertains each vertex. 
     86                std::map< int,  std::vector<int> > submeshmap; 
     87 
     88                //      Vertices of the VertexBuffer that point 
     89                //      at this vertex of the simplification method. 
     90                std::map< int,  std::vector<int> > vertexbuffermap; 
     91 
     92                //      Simplification sequence of the simplification method. 
     93                std::vector<Geometry::MeshSimplificationSequence::Step> decim_data; 
     94 
     95                std::string meshName; 
     96 
     97                unsigned int *first_index_submesh; 
     98 
     99                //std::vector<simplif::pair_info *> pointers_to_remove; 
     100 
     101                int indexMeshLeaves; 
     102 
     103                //      Contract lonely vertices that have same coords than the contracted one. 
     104                void    contractLonelyVertices( simplif::Model  &m, 
     105                                                                                                                                        simplif::Vertex *v0, 
     106                                                                                                                                        simplif::Vertex *v1, 
     107                                                                                                                                        simplif::Vec3           candidate); 
     108 
     109                //      Remove twin edges. 
     110                void    removeTwinEdges(simplif::Model  &m, 
     111                                                                                                        simplif::Vertex *v0, 
     112                                                                                                        simplif::Vertex *v1, 
     113                                                                                                        simplif::Vec3           candidate); 
     114 
     115        public: 
     116 
     117                const Geometry::Mesh *objmesh; 
     118 
     119                //      Total number of triangles of all the submeshes. 
     120                int number_of_triangles; 
     121 
     122                SimplificationMethod(const Geometry::Mesh *m); 
     123 
     124                void    generateSimplifModel(void); 
     125 
     126                Geometry::MeshSimplificationSequence *Decimate( float lod, 
     127                                int simpliftype, 
     128                                Geometry::TIPOFUNC      upb=0); 
     129 
     130                void setMeshLeaves(int meshLeaves); 
     131 
     132                ///     Gets mesh simplified. 
     133                Geometry::Mesh  *       GetMesh(); 
     134 
     135                ///     Destructor. 
     136                ~SimplificationMethod(); 
    113137}; 
    114138 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/avars.cxx

    r1025 r1526  
    1919bool simplif::will_preserve_mesh_quality = true; 
    2020bool simplif::will_constrain_boundaries = true; 
    21 simplif::real simplif::boundary_constraint_weight = 1.0; 
     21simplif::real simplif::boundary_constraint_weight = 10.0; 
    2222 
    2323bool simplif::will_weight_by_area = false; 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/geom/3D.cxx

    r1025 r1526  
    8585} 
    8686 
    87  
    88  
    89  
    9087//////////////////////////////////////////////////////////////////////// 
    9188// 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/geom/3D.h

    r1025 r1526  
    6363        }; 
    6464 
    65  
    6665        // 
    6766        // A triangular face in 3D (ie. a 2-simplex in E3) 
     
    9493} 
    9594 
    96  
    97  
    9895// GFXGEOM_3D_INCLUDED 
    9996#endif 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Array.h

    r1025 r1526  
    88{ 
    99        template<class T> 
    10         class array { 
    11         protected: 
    12                 T *data; 
    13                 int len; 
    14         public: 
    15                 array() { data=NULL; len=0; } 
    16                 array(int l) { init(l); } 
    17                 ~array() { f_r_e_e(); } 
     10                class array 
     11                { 
     12                        protected: 
     13                                T *data; 
     14                                int len; 
     15                        public: 
     16                                array() { data=NULL; len=0; } 
     17                                array(int l) { init(l); } 
     18                                ~array() { f_r_e_e(); } 
    1819 
     20                                inline void init(int l); 
     21                                inline void f_r_e_e(); 
     22                                inline void resize(int l); 
    1923 
    20                 inline void init(int l); 
    21                 inline void f_r_e_e(); 
    22                 inline void resize(int l); 
     24                                inline T& ref(int i); 
     25                                inline T& operator[](int i) { return data[i]; } 
     26                                inline T& operator()(int i) { return ref(i); } 
    2327 
    24                 inline T& ref(int i); 
    25                 inline T& operator[](int i) { return data[i]; } 
    26                 inline T& operator()(int i) { return ref(i); } 
     28                                inline const T& ref(int i) const; 
     29                                inline const T& operator[](int i) const { return data[i]; } 
     30                                inline const T& operator()(int i) const { return ref(i); } 
    2731 
    28                 inline const T& ref(int i) const; 
    29                 inline const T& operator[](int i) const { return data[i]; } 
    30                 inline const T& operator()(int i) const { return ref(i); } 
    31  
    32  
    33                 inline int length() const { return len; } 
    34                 inline int maxLength() const { return len; } 
    35         }; 
     32                                inline int length() const { return len; } 
     33                                inline int maxLength() const { return len; } 
     34                }; 
    3635 
    3736        template<class T> 
    38         inline void array<T>::init(int l) 
    39         { 
    40                 data = new T[l]; 
    41                 len = l; 
    42         } 
     37                inline void array<T>::init(int l) 
     38                { 
     39                        data = new T[l]; 
     40                        len = l; 
     41                } 
    4342 
    4443        template<class T> 
    45         inline void array<T>::f_r_e_e() 
    46         { 
    47                 if( data ) 
     44                inline void array<T>::f_r_e_e() 
    4845                { 
    49                 delete[] data; 
    50                 data = NULL; 
     46                        if( data ) 
     47                        { 
     48                                delete[] data; 
     49                                data = NULL; 
     50                        } 
    5151                } 
    52         } 
    5352 
    5453        template<class T> 
    55         inline T& array<T>::ref(int i) 
    56         { 
    57         #ifdef SAFETY 
    58                 assert( data ); 
    59                 assert( i>=0 && i<len ); 
    60         #endif 
    61                 return data[i]; 
    62         } 
     54                inline T& array<T>::ref(int i) 
     55                { 
     56#ifdef SAFETY 
     57                        assert( data ); 
     58                        assert( i>=0 && i<len ); 
     59#endif 
     60                        return data[i]; 
     61                } 
    6362 
    6463        template<class T> 
    65         inline const T& array<T>::ref(int i) const 
    66         { 
    67         #ifdef SAFETY 
    68                 assert( data ); 
    69                 assert( i>=0 && i<len ); 
    70         #endif 
    71                 return data[i]; 
    72         } 
     64                inline const T& array<T>::ref(int i) const 
     65                { 
     66#ifdef SAFETY 
     67                        assert( data ); 
     68                        assert( i>=0 && i<len ); 
     69#endif 
     70                        return data[i]; 
     71                } 
    7372 
    7473        template<class T> 
    75         inline void array<T>::resize(int l) 
    76         { 
    77                 T *old = data; 
    78                 data = new T[l]; 
    79                 data = (T *)memcpy(data,old,MIN(len,l)*sizeof(T)); 
    80                 len = l; 
    81                 delete[] old; 
    82         } 
     74                inline void array<T>::resize(int l) 
     75                { 
     76                        T *old = data; 
     77                        data = new T[l]; 
     78                        data = (T *)memcpy(data,old,MIN(len,l)*sizeof(T)); 
     79                        len = l; 
     80                        delete[] old; 
     81                } 
    8382} 
    8483 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Buffer.h

    r1025 r1526  
    2525                inline int length() const { return fill; } 
    2626                inline int maxLength() const { return len; } 
     27                inline void setFill(int l){fill =       l;} 
    2728        }; 
    28  
    2929 
    3030        template<class T> 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Heap.h

    r1025 r1526  
    1616        class Heapable 
    1717        { 
    18         private: 
    19                 int token; 
     18                private: 
    2019 
    21         public: 
    22                 Heapable() { notInHeap(); } 
     20                        int token; 
    2321 
    24                 inline int isInHeap() { return token!=NOT_IN_HEAP; } 
    25                 inline void notInHeap() { token = NOT_IN_HEAP; } 
    26                 inline int getHeapPos() { return token; } 
    27                 inline void setHeapPos(int t) { token=t; } 
     22                public: 
     23 
     24                        Heapable() { notInHeap(); } 
     25 
     26                        inline int isInHeap() { return token!=NOT_IN_HEAP; } 
     27                        inline void notInHeap() { token = NOT_IN_HEAP; } 
     28                        inline int getHeapPos() { return token; } 
     29                        inline void setHeapPos(int t) { token=t; } 
    2830        }; 
    2931 
     32        class heap_node 
     33        { 
     34                public: 
     35                        float import; 
     36                        Heapable *obj; 
    3037 
    31         class heap_node { 
    32         public: 
    33                 float import; 
    34                 Heapable *obj; 
    35  
    36                 heap_node() { obj=NULL; import=0.0; } 
    37                 heap_node(Heapable *t, float i=0.0) { obj=t; import=i; } 
    38                 heap_node(const heap_node& h) { import=h.import; obj=h.obj; } 
     38                        heap_node() { obj=NULL; import=0.0; } 
     39                        heap_node(Heapable *t, float i=0.0) { obj=t; import=i; } 
     40                        heap_node(const heap_node& h) { import=h.import; obj=h.obj; } 
    3941        }; 
    4042 
    41  
    42  
    43         class Heap : public array<heap_node> { 
    44  
     43        class Heap : public array<heap_node> 
     44        { 
    4545                // 
    4646                // The actual size of the heap.  array::length() 
     
    5757                void downheap(int i); 
    5858 
    59         public: 
     59                public: 
    6060 
    6161                Heap() { size=0; } 
    6262                Heap(int s) : array<heap_node>(s) { size=0; } 
    63  
    6463 
    6564                void insert(Heapable *, float); 
     
    6766 
    6867                heap_node *extract(); 
     68 
     69                //      Overhead method. 
     70                heap_node *extract(int i); 
     71 
    6972                heap_node *top() { return size<1 ? (heap_node *)NULL : &ref(0); } 
    7073                heap_node *kill(int i); 
     74 
     75                //      Gets heap size. 
     76                int     getSize(){return        size;} 
     77 
     78                //      Gets element at position i. 
     79                heap_node       *getElement(int i){return       &ref(i);} 
     80 
    7181        }; 
    7282 
    7383} 
    7484 
    75  
    7685// GFXTOOLS_HEAP_INCLUDED 
    7786#endif 
     87 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/heap.cxx

    r1025 r1526  
    77void Heap::swap(int i,int j) 
    88{ 
    9     heap_node tmp = ref(i); 
     9        heap_node tmp = ref(i); 
    1010 
    11     ref(i) = ref(j); 
    12     ref(j) = tmp; 
     11        ref(i) = ref(j); 
     12        ref(j) = tmp; 
    1313 
    14     ref(i).obj->setHeapPos(i); 
    15     ref(j).obj->setHeapPos(j); 
     14        ref(i).obj->setHeapPos(i); 
     15        ref(j).obj->setHeapPos(j); 
    1616} 
    1717 
    1818void Heap::upheap(int i) 
    1919{ 
    20     if( i==0 ) return; 
     20        if( i==0 ) return; 
    2121 
    22     if( ref(i).import > ref(parent(i)).import ) { 
    23         swap(i,parent(i)); 
    24         upheap(parent(i)); 
    25     } 
     22        if( ref(i).import > ref(parent(i)).import ) 
     23        { 
     24                swap(i,parent(i)); 
     25                upheap(parent(i)); 
     26        } 
    2627} 
    2728 
    2829void Heap::downheap(int i) 
    2930{ 
    30     if (i>=size) return;        // perhaps just extracted the last 
     31        if (i>=size) return;        // perhaps just extracted the last 
    3132 
    32     int largest = i, 
    33         l = left(i), 
    34         r = right(i); 
     33        int largest = i, 
     34                        l = left(i), 
     35                        r = right(i); 
    3536 
    36     if( l<size && ref(l).import > ref(largest).import ) largest = l; 
    37     if( r<size && ref(r).import > ref(largest).import ) largest = r; 
     37        if( l<size && ref(l).import > ref(largest).import ) largest = l; 
     38        if( r<size && ref(r).import > ref(largest).import ) largest = r; 
    3839 
    39     if( largest != i ) { 
    40         swap(i,largest); 
    41         downheap(largest); 
    42     } 
     40        if( largest != i ) { 
     41                swap(i,largest); 
     42                downheap(largest); 
     43        } 
    4344} 
    44  
    45  
    4645 
    4746void Heap::insert(Heapable *t,float v) 
    4847{ 
    49     if( size == maxLength() ) 
    50     { 
    51         std::cerr << "NOTE: Growing heap from " << size << " to " << 2*size << std::endl; 
    52         resize(2*size); 
    53     } 
     48        if( size == maxLength() ) 
     49        { 
     50                std::cerr << "NOTE: Growing heap from " << size << " to " << 2*size << std::endl; 
     51                resize(2*size); 
     52        } 
    5453 
    55     int i = size++; 
     54        int i = size++; 
    5655 
    57     ref(i).obj = t; 
    58     ref(i).import = v; 
     56        ref(i).obj = t; 
     57        ref(i).import = v; 
    5958 
    60     ref(i).obj->setHeapPos(i); 
     59        ref(i).obj->setHeapPos(i); 
    6160 
    62     upheap(i); 
     61        upheap(i); 
    6362} 
    6463 
    6564void Heap::update(Heapable *t,float v) 
    6665{ 
    67     int i = t->getHeapPos(); 
     66        int i = t->getHeapPos(); 
    6867 
    69     if( i >= size ) 
    70     { 
     68        if( i >= size ) 
     69        { 
    7170                std::cerr << "WARNING: Attempting to update past end of heap!" << std::endl; 
    7271                return; 
    73     } 
    74     else if( i == NOT_IN_HEAP ) 
    75     { 
     72        } 
     73        else if( i == NOT_IN_HEAP ) 
     74        { 
    7675                std::cerr << "WARNING: Attempting to update object not in heap!" << std::endl; 
    7776                return; 
    78     } 
     77        } 
    7978 
    80     float old=ref(i).import; 
    81     ref(i).import = v; 
     79        float old=ref(i).import; 
     80        ref(i).import = v; 
    8281 
    83     if( v<old ) 
    84         downheap(i); 
    85     else 
    86         upheap(i); 
     82        if( v<old ) 
     83                downheap(i); 
     84        else 
     85                upheap(i); 
    8786} 
    88  
    89  
    9087 
    9188heap_node *Heap::extract() 
    9289{ 
    93     if( size<1 ) return 0; 
     90        if( size<1 ) return 0; 
    9491 
    95     swap(0,size-1); 
    96     size--; 
     92        swap(0,size-1); 
     93        size--; 
    9794 
    98     downheap(0); 
     95        downheap(0); 
    9996 
    100     ref(size).obj->notInHeap(); 
     97        ref(size).obj->notInHeap(); 
    10198 
    102     return &ref(size); 
     99        return &ref(size); 
     100} 
     101 
     102//      Overhead method. 
     103heap_node *Heap::extract(int i) 
     104{ 
     105        if( size<1 ) return 0; 
     106 
     107        swap(i,size-1); 
     108        size--; 
     109 
     110        downheap(i); 
     111 
     112        ref(size).obj->notInHeap(); 
     113 
     114        return &ref(size); 
    103115} 
    104116 
    105117heap_node *Heap::kill(int i) 
    106118{ 
    107     if( i>=size ) 
     119        if( i>=size ) 
    108120                std::cerr << "WARNING: Attempt to delete invalid heap node." << std::endl; 
    109121 
    110     swap(i, size-1); 
    111     size--; 
    112     ref(size).obj->notInHeap(); 
     122        swap(i, size-1); 
     123        size--; 
     124        ref(size).obj->notInHeap(); 
    113125 
    114     if( ref(i).import < ref(size).import ) 
     126        if( ref(i).import < ref(size).import ) 
    115127                downheap(i); 
    116     else 
     128        else 
    117129                upheap(i); 
    118130 
     131        return &ref(size); 
     132} 
    119133 
    120     return &ref(size); 
    121 } 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/Leaf.cpp

    r1026 r1526  
    1010        vertsLeaf[0] = vertsLeaf[1] = vertsLeaf[2] = vertsLeaf[3] =0;  
    1111        center[0] = center[1] = center[2] = 0; 
    12         normal[0] = normal[1] = normal[2] = 0; 
     12//      normal[0] = normal[1] = normal[2] = 0; 
    1313        leafNear=-1; 
    1414        parentLeafCount = 1; 
     
    4040        for ( int i=0;i<3;i++){ 
    4141                center[i] = aLeaf.center[i]; 
    42                 normal[i] = aLeaf.normal[i]; 
     42//              normal[i] = aLeaf.normal[i]; 
    4343        } 
    4444        for (i = 0L; i < 4; i++) 
     
    5353 
    5454 
     55RuntimeLeaf::RuntimeLeaf(void)  
     56{        
     57        vertsLeaf[0] = vertsLeaf[1] = vertsLeaf[2] = vertsLeaf[3] = 0; 
     58        parent = root = childLeft = childRight = -1; 
     59} 
    5560 
    5661//-------------------------------------------------------------------------------------------------------------------------------- 
    57 // Destructor. We must deallocate the memory allocated for pointers to vertices and edges 
     62// Copy constructor 
    5863//-------------------------------------------------------------------------------------------------------------------------------- 
    59 Leaf::~Leaf (void) 
     64RuntimeLeaf::RuntimeLeaf (const RuntimeLeaf& aLeaf) 
    6065{ 
     66        for (int i = 0L; i < 4; i++) 
     67                vertsLeaf[i] = aLeaf.vertsLeaf[i];  
     68        parent = aLeaf.parent; 
     69        childLeft = aLeaf.childLeft; 
     70        childRight = aLeaf.childRight; 
     71        root = aLeaf.root; 
    6172} 
    6273 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/VertexData.cpp

    r1105 r1526  
    11#include "VertexData.h" 
    22 
    3 Geometry::DefaultVertexData::DefaultVertexData(unsigned int numv):Geometry::VertexData(numv) 
     3/* 
     4void Geometry::DefaultIndexData::Init(unsigned int numi) 
    45{ 
    5         v = new float[numv*3]; 
    6         n = new float[numv*3]; 
    7         t = new float[numv*2]; 
    8 } 
    9 Geometry::DefaultVertexData::~DefaultVertexData(void) 
    10 { 
    11 /*      delete[] v; 
    12         delete[] n; 
    13         delete[] t;*/ 
    14 } 
    15  
    16 Geometry::VertexData *Geometry::DefaultVertexDataCreator(unsigned int numv) 
    17 {  
    18         return new DefaultVertexData(numv);  
    19 } 
    20  
    21  
    22 Geometry::DefaultIndexData::DefaultIndexData(unsigned int numi):Geometry::IndexData(numi) 
    23 { 
    24         indices = new unsigned int[numi];        
     6        indices = new unsigned int[numi]; 
    257} 
    268Geometry::DefaultIndexData::~DefaultIndexData(void) 
    279{ 
    28 /*      delete[] indices;*/ 
    2910} 
    3011 
    3112Geometry::IndexData *Geometry::DefaultIndexDataCreator(unsigned int numi) 
    3213{  
    33         return new DefaultIndexData(numi);  
     14        IndexData *ret = new DefaultIndexData(); 
     15//      ret->Init(numi); 
     16        return ret; 
    3417} 
    3518 
    36  
    37 /*Geometry::DefaultMultiIndexData::DefaultMultiIndexData(unsigned int numprims, unsigned int *numi):Geometry::MultiIndexData(numprims,numi) 
    38 { 
    39         indices = new unsigned int*[numprims];   
    40         for (unsigned int i=0; i<numprims; i++) 
    41                 indices[i]=new unsigned int[numi[i]]; 
    42 } 
    43 Geometry::DefaultMultiIndexData::~DefaultMultiIndexData(void) 
    44 { 
    45         for (unsigned int i=0; i<GetNumPrims(); i++) 
    46                 delete[] indices[i]; 
    47         delete[] indices; 
    48 } 
    49  
    50 Geometry::MultiIndexData *Geometry::DefaultMultiIndexDataCreator(unsigned int numprims, unsigned int *numi) 
    51 {  
    52         return new DefaultMultiIndexData(numprims,numi);  
    53 } 
    5419*/ 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/foliage.cpp

    r1083 r1526  
    1010// Parameters --> None 
    1111//-------------------------------------------------------------------------------------------------------------------------------- 
    12 Foliage::Foliage(const Geometry::SubMesh *leavesSubMesh, const Geometry::TreeSimplificationSequence * simpSeq, Geometry::CREATEVERTEXDATAFUNC vdfun, Geometry::CREATEINDEXDATAFUNC idfun):  
     12Foliage::Foliage(int leavessubmeshID,  
     13                                 const Geometry::SubMesh *leavesSubMesh,  
     14                                 const Geometry::TreeSimplificationSequence * simpSeq/*, 
     15                                 Geometry::CREATEVERTEXDATAFUNC vdfun*/):  
    1316Acth(NULL), 
    14 create_vertex_data_func(vdfun==NULL?Geometry::DefaultVertexDataCreator:vdfun), 
    15 create_index_data_func(idfun==NULL?Geometry::DefaultIndexDataCreator:idfun), 
    16 vertexdata(NULL), Leaves(NULL), MinDet(NULL) 
     17//create_vertex_data_func(vdfun==NULL?Geometry::DefaultVertexDataCreator:vdfun), 
     18//create_index_data_func(idfun==NULL?Geometry::DefaultIndexDataCreator:idfun), 
     19/*vertexdata(NULL),*/ Leaves(NULL), MinDet(NULL) 
    1720{ 
    1821        begin = final = -1; 
    19  
    20         ReadVertices(leavesSubMesh); 
     22//      indexdata=NULL; 
     23 
     24//      ReadVertices(leavesSubMesh); 
     25        int countv= int(leavesSubMesh->mVertexBuffer->mVertexCount); 
     26        Leaves = new RuntimeLeaf[countv*2]; 
     27        TotalVerts = countv; 
     28 
    2129        ReadLeafs(leavesSubMesh); 
    2230        if (!ReadSimpSeq(simpSeq)) exit(1); 
    2331        FillRoot(); 
    24         CalculateTexCoordsAndNorms(); 
    25  
    26         indexdata->SetNumValidIndices(0); 
     32//      CalculateTexCoordsAndNorms(); 
     33 
     34//      indexdata->SetNumValidIndices(0); 
    2735 
    2836        int h=0; 
     
    4452        final = leafCount-1; 
    4553        active_leaf_count = leafCount; 
     54        leavesSubMeshID=leavessubmeshID; 
    4655} 
    4756 
     
    5160Foliage::~Foliage (void) 
    5261{ 
    53         if (vertexdata) delete vertexdata; 
    54         if (indexdata) delete indexdata; 
     62//      if (vertexdata) delete vertexdata; 
     63//      if (indexdata) delete indexdata; 
    5564        delete[] Leaves; 
    5665        delete MinDet; 
     
    222231 
    223232 } 
    224  
     233/* 
    225234void Foliage::ReadVertices(const Geometry::SubMesh *submesh) 
    226235{ 
     
    228237        vertexdata = create_vertex_data_func(2*countv); 
    229238        Leaves = new Leaf[countv*2]; 
    230         indexdata = create_index_data_func(countv*2*3); // 3 indices x 2 triangulos x hoja 
     239        //indexdata = create_index_data_func(countv*2*3); // 3 indices x 2 triangulos x hoja 
    231240         
    232241        vertexdata->Begin(); 
     
    249258        float twox, twoy, twoz; 
    250259        float threex, threey, threez; 
    251  
    252 /*      Vertices[aHoja.vertsLeaf[0]].GetCoordinates (onex, oney, onez); 
    253         Vertices[aHoja.vertsLeaf[1]].GetCoordinates(twox, twoy, twoz); 
    254         Vertices[aHoja.vertsLeaf[2]].GetCoordinates (threex, threey, threez);*/ 
    255260 
    256261        vertexdata->GetVertexCoord(aleaf.vertsLeaf[0],onex,oney,onez); 
     
    286291        res[2]=v[2]/module; 
    287292} 
    288  
     293*/ 
    289294void Foliage::ReadLeafs(const Geometry::SubMesh *submesh) 
    290295{ 
     
    297302                Leaves[h].vertsLeaf[2] = submesh->mIndex[h*6+2]; 
    298303                Leaves[h].vertsLeaf[3] = submesh->mIndex[h*6+5]; 
    299                 Leaves[h].visible = 0; 
    300  
    301                 GetNormalH ( Leaves[h]); 
     304//              Leaves[h].visible = 0; 
     305 
     306//              GetNormalH ( Leaves[h]); 
    302307        } 
    303308} 
     
    316321                Leaves[tn].vertsLeaf[3] = it->mNewQuad[3]; 
    317322 
    318                 Leaves[tn].visible = 0; 
    319  
    320                 GetNormalH  (Leaves[tn]); 
     323//              Leaves[tn].visible = 0; 
     324 
     325//              GetNormalH  (Leaves[tn]); 
    321326 
    322327                tv1 = it->mV0/2; 
     
    433438} 
    434439 
    435 void Foliage::CalculateTexCoordsAndNorms(void) 
     440/*void Foliage::CalculateTexCoordsAndNorms(void) 
    436441{        
    437442        vertexdata->Begin(); 
     
    453458        vertexdata->End(); 
    454459} 
    455  
     460*/ 
    456461Foliage::Foliage(const Foliage *ar) 
    457462{ 
     
    465470        TotalVerts=ar->TotalVerts; 
    466471 
    467         create_vertex_data_func=ar->create_vertex_data_func; 
    468         create_index_data_func=ar->create_index_data_func; 
     472/*      create_vertex_data_func=ar->create_vertex_data_func; 
     473//      create_index_data_func=ar->create_index_data_func; 
    469474        vertexdata=create_vertex_data_func(ar->vertexdata->GetNumVertices()); 
    470475        vertexdata->Begin();  
     
    477482                vertexdata->SetVertexNormal(i,va,vb,vc); 
    478483        } 
    479         vertexdata->End(); 
    480         indexdata=create_index_data_func(ar->indexdata->GetNumMaxIndices()); 
    481         indexdata->Begin();  
     484        vertexdata->End();*/ 
     485/*      indexdata=create_index_data_func(ar->indexdata->GetNumMaxIndices()); 
     486        indexdata->Begin(ar->leavesSubMeshID,indexdata->GetNumMaxIndices()); 
    482487        for (unsigned int i=0; i<indexdata->GetNumMaxIndices(); i++)  
    483488                indexdata->SetIndex(i,ar->indexdata->GetIndex(i)); 
    484         indexdata->End(); 
    485  
    486         Leaves=new Leaf[vertexdata->GetNumVertices()]; 
     489        indexdata->End();*/ 
     490 
     491        Leaves=new RuntimeLeaf[TotalVerts]; 
    487492//      for (unsigned int i=0; i<vertexdata->GetNumVertices(); i++)  
    488493//              Leaves[i]=ar->Leaves[i]; 
    489         memcpy(Leaves,ar->Leaves,sizeof(Leaf)*vertexdata->GetNumVertices()); 
     494        memcpy(Leaves,ar->Leaves,sizeof(Leaf)*TotalVerts); 
    490495 
    491496        // esto no sé si devería haber akí 
    492         indexdata->SetNumValidIndices(0); 
     497//      indexdata->SetNumValidIndices(0); 
    493498 
    494499        int h=0; 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/foliage.h

    r1083 r1526  
    2626                int begin, final; 
    2727                int active_leaf_count; 
     28                int leavesSubMeshID; 
    2829 
    29                 Foliage (const Geometry::SubMesh *, const Geometry::TreeSimplificationSequence *, Geometry::CREATEVERTEXDATAFUNC vdfun=NULL, Geometry::CREATEINDEXDATAFUNC idfun=NULL); 
     30                Foliage (       int leavesSubMeshID,  
     31                                        const Geometry::SubMesh *,  
     32                                        const Geometry::TreeSimplificationSequence *    ); 
     33 
    3034                Foliage (const Foliage *); 
    3135                virtual ~Foliage (void); // Destructor 
     
    3337                void CalculateLOD(int nhojas); 
    3438 
    35                 Geometry::VertexData *vertexdata; 
    36                 Geometry::IndexData *indexdata; 
    37  
    38                 Leaf   *Leaves; 
     39                RuntimeLeaf   *Leaves; 
    3940                ActiveLeafNode  *MinDet; // first active leaf 
    4041                int leafCount; 
     
    4546 
    4647        private: 
    47                 Geometry::CREATEVERTEXDATAFUNC create_vertex_data_func;          
    48                 Geometry::CREATEINDEXDATAFUNC create_index_data_func; 
    4948 
    5049                bool IsActive( int num) const; 
     
    5958                bool ReadSimpSeq(const Geometry::TreeSimplificationSequence *); /// returns true when successful 
    6059                void FillRoot(void); 
    61  
    62                 void GetNormalH (Leaf&); 
    63  
    64                 void CrossProduct(const float *v1, const float *v2, float *res); 
    65                 void Normalize(const float *v, float *res); 
    66 //              void CalculaNormalesVertice(void); 
    67                 void CalculateTexCoordsAndNorms(void); 
    68  
    6960}; 
    7061 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/simplif.h

    r1025 r1526  
    1212        class pair_info : public Heapable 
    1313        { 
    14         public: 
    15                 Vertex *v0, *v1; 
     14                public: 
     15                        Vertex *v0, *v1; 
    1616 
    17                 Vec3 candidate; 
    18                 real cost; 
     17                        Vec3 candidate; 
     18                        real cost; 
    1919 
    20                 pair_info(Vertex *a,Vertex *b) { v0=a; v1=b; cost=HUGE; } 
     20                        pair_info(Vertex *a,Vertex *b) { v0=a; v1=b; cost=HUGE; } 
    2121 
    22                 bool isValid() { return v0->isValid() && v1->isValid(); } 
     22                        bool isValid() { return v0->isValid() && v1->isValid(); } 
    2323        }; 
    2424 
     
    2727        class vert_info 
    2828        { 
    29         public: 
     29                public: 
    3030 
    31                 pair_buffer pairs; 
     31                        pair_buffer pairs; 
    3232 
    33                 Mat4 Q; 
    34                 real norm; 
     33                        Mat4 Q; 
     34                        real norm; 
    3535 
    36                 vert_info() : Q(Mat4::zero) { pairs.init(2); norm=0.0; } 
     36                        vert_info() : Q(Mat4::zero) { pairs.init(2); norm=0.0; } 
    3737        }; 
    3838} 
     39 
Note: See TracChangeset for help on using the changeset viewer.