Ignore:
Timestamp:
02/05/07 13:29:55 (17 years ago)
Author:
gumbau
Message:
 
Location:
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs
Files:
24 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Buffer.h

    r1526 r2090  
    3232        { 
    3333                if( fill == len ) 
    34                 resize( len*2 ); 
     34                { 
     35                        resize( len*2 ); 
     36                } 
    3537 
    3638                data[fill] = t; 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/bheap.h

    r983 r2090  
    1515 */ 
    1616typedef struct { 
    17     int item;  /* vertex number is used for the item. */ 
     17    int dirty;  /* dirty flag if used for the lazy evaluation */ 
     18    int item;  /* edge number is used for the item. */ 
    1819    double key;  /* distance is used as the key. */ 
    1920} bheap_item_t; 
    2021 
    2122/* Binary heap structure for frontier set in Dijkstra's algorithm. 
    22  * a[] - stores (distance, vertex) pairs of the binary heap. 
     23 * a[] - stores (distance, edge) pairs of the binary heap. 
    2324 * p[] - stores the positions of vertices in the binary heap a[]. 
    2425 * n - is the size of the binary heap. 
     
    5859 */ 
    5960void bh_delete(bheap_t *h, int item); 
     61/* bh_mark() marks an item from the binary heap pointed to by h. 
     62 */ 
     63void bh_mark(bheap_t *h, int item, int flag); 
     64 
     65/* bh_get_key() returns the key value of an item from the binary heap pointed  
     66 * to by h. 
     67 */ 
     68double bh_get_key(bheap_t *h, int item); 
     69 
     70/* bh_is_dirty() returns the dirty value of an item from the binary heap pointed 
     71 * to by h. 
     72 */ 
     73int bh_is_dirty(bheap_t *h, int item); 
    6074 
    6175/* bh_decrease_key() decreases the value of 'item's key and then performs 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/camera.h

    r983 r2090  
    55 
    66#define OCTAHEDRON     6  // number of vertices 
     7#define CUBE           8 
    78#define ICOSAHEDRON   12 
    89#define DODECAHEDRON  20 
     
    2627extern void drawSphere(Camera *cameras, GLdouble radius, int slices, int stacks); 
    2728extern void drawOctahedron(Camera *cameras, GLdouble r); 
     29extern void drawCube(Camera *cameras, GLdouble r); 
    2830extern void drawIcosahedron(Camera *cameras, GLdouble r); 
    2931extern void drawDodecahedron(Camera *cameras, GLdouble r); 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/change.h

    r1024 r2090  
    55#include        <vector> 
    66#include        <map> 
     7#include        <GeoMeshSimpSequence.h> 
    78 
    89namespace       VMI 
     
    2021        } Change; 
    2122 
     23        /* 
    2224        // Represents a simplification step in the sequence. 
    2325        struct vmiStep 
     
    4143        extern  std::vector<vmiStep> mVMISteps; 
    4244 
     45        //      Vertex info. 
     46        struct  GeoVertex 
     47        { 
     48                Index           id; 
     49                Index           bonefrom; 
     50                Vector3 position; 
     51                Vector2 texcoord; 
     52                Vector3 normal; 
     53        }; 
     54 
     55        //      Vertices added in simplification. 
     56        std::vector<GeoVertex>  mNewVertices; 
     57        */ 
     58 
     59        // Stores all the simplification steps. 
     60        extern  Geometry::MeshSimplificationSequence *mSequence; 
     61 
    4362        extern Change *createChange (Mesh *mesh, int e); 
     63        extern Change *newChange (Mesh *mesh, int u, int v); 
    4464        extern void writeChange(FILE* file, Change *c); 
    4565        extern void deleteChange(Change *c); 
     
    4767 
    4868        extern void modifyTriangle(Triangle *t, int c, int p); 
    49         extern int isATriangleToModify(Triangle *t, int c, int p); 
    50         extern int getTrianglesToModify(Mesh *mesh, int c, int p, Triangle *modified); 
     69        extern int isATriangleToModify(Triangle *t, int c); 
     70        extern Triangle *getTrianglesToModify(Mesh *mesh, Change *c, GLuint *numMod); 
    5171        extern int isATriangleToDelete(Triangle *t, int c, int p); 
    52         extern int getTrianglesToDelete(Mesh *mesh, int numMod, Triangle *modified, Triangle *deleted, int c, int p); 
     72        extern Triangle *getTrianglesToDelete(Mesh *mesh, Change *c, GLuint *numDel); 
    5373 
    5474        extern void modifyTriangles(Mesh *mesh, Change *c); 
     
    6282        extern void doChange(Mesh *mesh, Change *c); 
    6383        extern void undoChange(Mesh *mesh, Change *c); 
    64         extern void computeChanges(Mesh *mesh, Change *c); 
     84        extern void computeChange(Mesh *mesh, Change *c); 
     85 
     86        extern void modifyEdges(Mesh *mesh, Change *c); 
     87  extern void deleteEdges(Mesh *mesh, Change *c); 
    6588 
    6689        //      Save simplification sequence in Geometry Game Tools format. 
    67         extern void     saveSimplificationSequence(Change       *c); 
     90        extern void     saveSimplificationSequence(Change       *c, int obligatory); 
    6891 
    69         extern std::map<int, INTVECTOR> inversemap; 
     92        //extern std::map<int, INTVECTOR> inversemap; 
     93        extern void swap(int *i, int *j); 
     94        extern int hasEdge(Triangle *t, int e); 
     95        extern void getEdges(Triangle *t, Change *c, int *d, int *a); 
    7096} 
    71  
    7297#endif 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/color.h

    r983 r2090  
    1414} Color; 
    1515 
    16 extern Color *initColors(GLuint numTriangles); 
    17 extern void fillAllColors(Color *colors, GLuint numTriangles, GLuint begin, GLuint end, GLubyte color); 
     16extern Color *initColors(GLuint numColors); 
     17extern void fillAllColors(Color *colors, GLuint numColors, GLuint begin, GLuint end, GLubyte color); 
    1818extern void setColors(Color *colors, GLuint numPasses, GLuint begin, GLuint end); 
    19 extern void setColors2(Color *colors, GLuint numTriangles, GLuint begin, GLuint end); 
    20 extern void setColors3(Color *colors, GLuint numTriangles, GLubyte color); 
    21 extern void setColors4(Color *colors, GLuint numTriangles); 
    22 extern void viewColors(Color *colors, GLuint numTriangles); 
     19extern void setColors2(Color *colors, GLuint numColors, GLuint begin, GLuint end); 
     20extern void setColors3(Color *colors, GLuint numColors, GLubyte color); 
     21extern void setColors4(Color *colors, GLuint numColors); 
     22extern void viewColors(Color *colors, GLuint numColors); 
    2323 
    2424} 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/global.h

    r983 r2090  
    88#include "mesh.h" 
    99#include "color.h" 
     10#include "change.h" 
    1011 
    1112//      For progress update. 
     
    5859 
    5960//#define SALIENCY 
     61 
     62// Extension added into the filename 
     63#ifdef KL // Kullback-Leibler 
     64#define EXT "VKL" 
     65#endif 
     66#ifdef MI // Mutual Information 
     67#define EXT "VMI" 
     68#endif 
     69#ifdef HE // Hellinger 
     70#define EXT "VHE" 
     71#endif 
     72#ifdef CS // Chi-Square 
     73#define EXT "VCS" 
     74#endif 
     75 
     76//#define USE_EDGE_ADJACENCY 
    6077 
    6178#define CHECK_OPENGL_ERROR( cmd ) \ 
     
    110127                 bEnableOffScreen, 
    111128                 bSaveLog, 
    112                  bLoadCamerasFromFile; 
     129                 bLoadCamerasFromFile, 
     130                                                                 bRemoveRedundantVertices; 
    113131 
    114132extern GLsizei width, height; 
     
    121139 
    122140extern void renderScene(GLuint **histogram, GLuint numCameras); 
     141extern void renderSceneWin(GLuint **histogram, GLuint numCameras, Change *c); 
    123142 
    124143extern void renderGeometry(void); 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/histogram.h

    r983 r2090  
    2020extern void printFullHistogram(GLuint **histogram, GLuint numCameras, GLuint numTriangles); 
    2121extern void getSWHistogram(GLuint *histogram, GLubyte *pixels); 
     22extern void getSWHistogramWin(GLuint *histogram, GLubyte *pixels, GLfloat min[3], GLfloat max[3], Change *c); 
    2223extern void getSWHistoByOcclusionQuery(Mesh *mesh, Color *colors, GLuint *histogram); 
    2324 
    2425extern void resetSWHistogram(GLuint *histogram, GLuint numTriangles); 
    25  
     26extern void getBoundingBox(Change *c, GLfloat min[3], GLfloat max[3]); 
     27extern void getWindow(GLfloat min[3], GLfloat max[3], int minw[2], int maxw[2]); 
    2628} 
    2729 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/interleave.h

    r983 r2090  
    2020    GLfloat x, y, z;    // position 
    2121} Vertex_; 
     22extern GLuint vertex_buf, color_buf; 
    2223 
    2324extern void setupInterleave(Mesh *mesh, Color *colors); 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/mesh.h

    r983 r2090  
    1111    GLuint *triangles;   // List of triangles 
    1212    int enable; 
     13                int     movable;        //      If vertex is part of a border. 
    1314} Vertex; 
    1415 
     
    2223typedef struct _Triangle { 
    2324    GLuint              id;         // Triangle id 
     25    GLuint  group;        // Triangle group 
    2426                GLuint          submesh;                // Triangle submesh      
    2527    GLuint              indices[3]; // Triangle vertices 
     
    4547extern void computeTriangleNormal(Vertex *vertices, Triangle *t); 
    4648 
     49extern Vertex *addVertex(Vertex *list, int *n, float x, float y, float z, int *pos); 
    4750extern int findEdge(Edge *e, GLuint num, GLuint _u, GLuint _v); 
     51extern Edge *addEdge(Edge *list, int *n, int u, int v, int *pos); 
    4852extern GLboolean findVertex(GLfloat *vertices, GLuint num, GLfloat x, GLfloat y, GLfloat z, int *pos); 
    4953 
     
    5559 
    5660extern int *trianglesAdjToEdge(Mesh *mesh, int e, int *n); 
    57 extern int *trianglesAdjToVertex(Mesh *mesh, int v, int *n); 
    5861extern int *verticesAdjToVertex(Mesh *mesh, int v, int *n); 
    5962extern int *edgesAdjToVertex(Mesh *mesh, int v, int *n); 
    6063extern int *edgesAdjToVertices(Mesh *mesh, int *vertices, int numVertices, int *n); 
    6164 
     65// list of integers 
    6266extern void printItemList(int *list, int n); 
    6367extern int findItem(int *list, int n, int item); 
    6468extern void addItem(int *list, int *n, int item); 
     69extern void delItem(int *list, int *n, int item); 
    6570 
    6671 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/metrics.h

    r983 r2090  
    88namespace       VMI 
    99{ 
    10 extern GLuint computeBackgroundArea(Mesh *mesh, GLuint *histogram); 
    11 extern GLdouble computeMeanProjArea(GLuint **histogram, GLuint numCameras, int t); 
    1210extern GLdouble computeMI(Mesh *mesh, GLuint **histogram, GLuint numCameras, GLuint cam); 
     11extern GLdouble decMI(GLdouble I, GLuint **histogram, GLuint numCameras, GLuint cam, Change *c); 
     12extern GLdouble incMI(GLdouble I, GLuint **histogram, GLuint numCameras, GLuint cam, Change *c); 
     13 
    1314extern GLdouble computeHE(Mesh *mesh, GLuint *histogram); 
    1415extern GLdouble computeKL(Mesh *mesh, GLuint *histogram); 
    1516extern GLdouble computeCS(Mesh *mesh, GLuint *histogram); 
    1617 
     18extern GLdouble computeMeanProjArea(GLuint **histogram, GLuint numCameras, int t); 
    1719extern GLdouble computeJS(GLuint **histogram, GLuint numCameras, GLuint j, GLuint k); 
    1820extern GLdouble computeEntropy(GLuint **histogram, GLuint numCameras, GLuint k); 
     
    2123 
    2224extern void getProjectedAreas(GLuint **histogram, GLuint numCameras); 
     25extern void getProjectedAreasWin(GLuint **histogram, GLuint numCameras, Change *c); 
    2326extern void resetProjectedAreas(GLuint **histogram, GLuint numCameras); 
    2427 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/saliency.h

    r983 r2090  
    1414 
    1515extern void computeSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras); 
     16extern void updateTriangleSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras, int v); 
    1617 
    1718extern double computeTriangleSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras, GLuint k); 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/simplify.h

    r1007 r2090  
    77#include "bheap.h" 
    88 
     9#include        <map> 
     10 
     11using   namespace       std; 
     12 
    913namespace       VMI 
    1014{ 
    11 extern GLdouble computeEdgeCost(Mesh *mesh, int e); 
     15        extern GLdouble computeEdgeCost(Mesh *mesh, int e); 
    1216 
    13 extern bheap_t *initHeap(Mesh *mesh); 
     17        extern bheap_t *initHeap(Mesh *mesh); 
    1418 
    15 extern bheap_t *updateHeap(bheap_t *h, Mesh *mesh, Change *c); 
     19        extern bheap_t *updateHeap(bheap_t *h, Mesh *mesh, Change *c); 
    1620 
    17 extern void simplifyModel(Mesh *mesh, GLuint numDemandedTri); 
     21        extern void simplifyModel(Mesh *mesh, GLuint numDemandedTri); 
    1822 
    1923 
    20 extern void bh_mydump(Mesh *mesh, bheap_t *h); 
    21 extern GLdouble computeEdgeLength(Vertex *vertices, int u, int v); 
     24        extern void bh_mydump(Mesh *mesh, bheap_t *h); 
     25        extern GLdouble computeEdgeLength(Vertex *vertices, int u, int v); 
    2226 
    23 extern void swap(unsigned int *i, unsigned int *j); 
    24 extern void chooseBestEndPoints(Mesh *mesh, int e); 
     27        extern void chooseBestEndPoints(Mesh *mesh, int e); 
    2528 
     29        extern void     initVertexMultimap(     Mesh *mesh, 
     30                                                                                                                                        multimap<int,int> &vertexMultimap); 
     31        ///////////////////////////////////////////////////////////////////////////// 
     32        //      Coordinates class. 
     33        ///////////////////////////////////////////////////////////////////////////// 
     34        class _float3_ 
     35        { 
     36                public: 
     37 
     38                        float x,y,z; 
     39 
     40                        _float3_(float x=0.0f, float y=0.0f, float z=0.0f) 
     41                        { 
     42                                this->x = x; this->y = y; this->z = z; 
     43                        } 
     44 
     45                        _float3_(const _float3_ &f) 
     46                        { 
     47                                x=f.x; y=f.y; z=f.z; 
     48                        } 
     49 
     50                        _float3_ & operator=(const _float3_ &f) 
     51                        { 
     52                                x=f.x; y=f.y; z=f.z; return *this; 
     53                        } 
     54 
     55                        bool operator<(const _float3_ &f) const 
     56                        { 
     57                                if (x<f.x) return true; 
     58                                if (x>f.x) return false; 
     59                                if (y<f.y) return true; 
     60                                if (y>f.y) return false; 
     61                                if (z<f.z) return true; 
     62                                if (z>f.z) return false; 
     63                                return false; 
     64                        } 
     65        }; 
     66 
     67        extern int extractValidEdge(Mesh *mesh, bheap_t *h); 
     68 
     69        extern int      isValidEdge(Mesh        *mesh,  int edge); 
     70 
     71        void    deleteVertexOfMap(Mesh  *mesh, int u); 
     72        bool    compareVertices(Vertex *vertices,int    u,      int v); 
     73        void    contractInitialMesh(Mesh        *mesh); 
     74        void    contractTwinVertices(Mesh       *mesh,  int     u,      int     v); 
     75 
     76        //      Vectors of positions, normals and texture coordinates. 
     77        extern  vector<Geometry::Vector3>       vPositions; 
     78        extern  vector<Geometry::Vector3>       vNormals; 
     79        extern  vector<Geometry::Vector2>       vTexCoords; 
    2680} 
    2781 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/bheap.cpp

    r983 r2090  
    154154        } 
    155155    } 
     156} 
     157/* bh_mark() marks an item from the binary heap pointed to by h. 
     158 */ 
     159void bh_mark(bheap_t *h, int item, int flag) 
     160{ 
     161    h->a[h->p[item]].dirty = flag; 
     162} 
     163/* bh_get_key() returns the key value of an item from the binary heap h. 
     164 */ 
     165double bh_get_key(bheap_t *h, int item) 
     166{ 
     167    return h->a[h->p[item]].key; 
     168} 
     169/* bh_is_dirty() returns the dirty value of an item from the binary heap h. 
     170 */ 
     171int bh_is_dirty(bheap_t *h, int item) 
     172{ 
     173    return h->a[h->p[item]].dirty; 
    156174} 
    157175 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/camera.cpp

    r983 r2090  
    174174 
    175175    switch (type) { 
    176     case OCTAHEDRON: 
    177         cameras = (Camera *)malloc(sizeof(Camera) * type); 
     176    case 0: 
     177        cameras = (Camera *)malloc(sizeof(Camera) * OCTAHEDRON); 
    178178        if (cameras == NULL) { 
    179179            fprintf(stderr, "Error allocating memory\n"); 
    180180            exit(1); 
    181181        } 
    182         *numCameras = type; 
     182        *numCameras = OCTAHEDRON; 
    183183        drawOctahedron(cameras, radius); 
    184184        break; 
    185     case ICOSAHEDRON: 
    186         cameras = (Camera *)malloc(sizeof(Camera) * type); 
     185    case 1: 
     186        cameras = (Camera *)malloc(sizeof(Camera) * CUBE); 
    187187        if (cameras == NULL) { 
    188188            fprintf(stderr, "Error allocating memory\n"); 
    189189            exit(1); 
    190190        } 
    191         *numCameras = type; 
    192         drawIcosahedron(cameras, radius); 
     191        *numCameras = CUBE; 
     192        drawCube(cameras, radius); 
    193193        break; 
    194     case DODECAHEDRON: 
    195         cameras = (Camera *)malloc(sizeof(Camera) * type); 
     194    case 2: 
     195        cameras = (Camera *)malloc(sizeof(Camera) * ICOSAHEDRON); 
    196196        if (cameras == NULL) { 
    197197            fprintf(stderr, "Error allocating memory\n"); 
    198198            exit(1); 
    199199        } 
    200         *numCameras = type; 
     200        *numCameras = ICOSAHEDRON; 
     201        drawIcosahedron(cameras, radius); 
     202        break; 
     203    case 3: 
     204        cameras = (Camera *)malloc(sizeof(Camera) * DODECAHEDRON); 
     205        if (cameras == NULL) { 
     206            fprintf(stderr, "Error allocating memory\n"); 
     207            exit(1); 
     208        } 
     209        *numCameras = DODECAHEDRON; 
    201210        drawDodecahedron(cameras, radius); 
    202211        break; 
    203212    default: 
    204         printf("Error cameras not defined\n"); 
     213        printf("Error, cameras not defined\n"); 
    205214        exit(1); 
    206215        break; 
    207216    } 
     217 
     218    printf("Number of cameras: %d\n", *numCameras); 
    208219 
    209220    return cameras; 
     
    506517    copyToCameras(cameras, 12, vertices); 
    507518} 
     519 
     520void VMI::drawCube(Camera *cameras, GLdouble r) // any radius in which the polyhedron is inscribed 
     521{ 
     522    GLdouble vertices[8][3] = { 
     523        {-1, -1, 1},   // vertex v0 
     524        {1,  -1, 1},   // vertex v1 
     525        {1,  -1, -1},  // vertex v2 
     526        {-1, -1, -1},  // vertex v3 
     527        {-1, 1,  1},   // vertex v4 
     528        {1,  1,  1},   // vertex v5 
     529        {1,  1,  -1},  // vertex v6  
     530        {-1, 1,  -1},  // vertex v7 
     531    }; // 8 vertices with x, y, z coordinates 
     532    int i; 
     533#ifdef DRAW_DEBUG 
     534    int tindex[12][3] = { 
     535         
     536        {0, 1, 4}, //polygon v0,v1,v4 
     537        {1, 5, 4}, //polygon v1,v5,v4 
     538        {1, 2, 5}, //polygon v1,v2,v5 
     539        {2, 6, 5}, //polygon v2,v6,v5 
     540        {2, 3, 6}, //polygon v2,v3,v6 
     541        {3, 7, 6}, //polygon v3,v7,v6 
     542        {3, 0, 7}, //polygon v3,v0,v7 
     543        {0, 4, 7}, //polygon v0,v4,v7 
     544        {4, 5, 7}, //polygon v4,v5,v7 
     545        {5, 6, 7}, //polygon v5,v6,v7 
     546        {3, 2, 0}, //polygon v3,v2,v0 
     547        {2, 1, 0}, //polygon v2,v1,v0 
     548    }; 
     549#endif 
     550     
     551    for(i=0; i<8; i++) 
     552    { 
     553        vertices[i][0]*=r; 
     554        vertices[i][1]*=r; 
     555        vertices[i][2]*=r; 
     556    } 
     557 
     558    // map vertices to 12 faces  
     559#ifdef DRAW_DEBUG 
     560     
     561    for (i=0; i<12; i++) {  
     562         
     563        glBegin(GL_POINTS ); 
     564        glVertex3fv(&vertices[tindex[i][0]][0]);  
     565        glVertex3fv(&vertices[tindex[i][1]][0]);  
     566        glVertex3fv(&vertices[tindex[i][2]][0]);     
     567        glEnd();  
     568    } 
     569 
     570#endif 
     571     
     572    copyToCameras(cameras, 8, vertices); 
     573} 
     574 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/change.cpp

    r1024 r2090  
    66#include "../include/change.h" 
    77 
    8 #define MAX_NUM_TRI 250 
    9  
    108using namespace VMI; 
    11  
    12 std::vector<VMI::vmiStep> VMI::mVMISteps; 
    13  
    14 Change *VMI::createChange (Mesh *mesh, int e) { 
    15     Change *c; 
    16      
    17     c = (Change*) malloc (sizeof(Change)); 
    18     if (NULL == c) { 
    19         fprintf (stderr, "no more memory for mesh changes"); 
    20         exit(1); 
    21     } 
    22     c->e = e; 
    23     c->u = mesh->edges[e].u; 
    24     c->v = mesh->edges[e].v; 
    25     c->modified = NULL; 
    26     c->deleted = NULL; 
    27     c->numDel = 0; 
    28     c->numMod = 0; 
    29      
    30     return c; 
     9using namespace std; 
     10 
     11Geometry::MeshSimplificationSequence *VMI::mSequence    =       NULL; 
     12 
     13Change *VMI::createChange(Mesh *mesh, int e) { 
     14        Change *c; 
     15 
     16        c = (Change*) malloc (sizeof(Change)); 
     17        if (NULL == c) { 
     18                fprintf (stderr, "no more memory for a mesh change\n"); 
     19                exit(1); 
     20        } 
     21        c->e = e; 
     22        c->u = mesh->edges[e].u; 
     23        c->v = mesh->edges[e].v; 
     24        c->modified = NULL; 
     25        c->deleted = NULL; 
     26        c->numDel = 0; 
     27        c->numMod = 0; 
     28 
     29        computeChange(mesh, c); 
     30 
     31        return c; 
     32} 
     33 
     34Change *VMI::newChange(Mesh *mesh, int u, int v) { 
     35        Change *c; 
     36 
     37        c = (Change*) malloc (sizeof(Change)); 
     38        if (NULL == c) { 
     39                fprintf (stderr, "no more memory for a mesh change\n"); 
     40                exit(1); 
     41        } 
     42        c->e = -1; // Default edge, not used 
     43        c->u = u; 
     44        c->v = v; 
     45        c->modified = NULL; 
     46        c->deleted = NULL; 
     47        c->numDel = 0; 
     48        c->numMod = 0; 
     49 
     50        computeChange(mesh, c); 
     51 
     52        return c; 
    3153} 
    3254 
     
    91113void VMI::modifyTriangle(Triangle *t, int c, int p) 
    92114{ 
    93  
    94115        if ((int)t->indices[0] == c) 
    95116                t->indices[0]= p; 
     
    100121} 
    101122 
    102 int VMI::isATriangleToModify(Triangle *t, int c, int p) 
     123int VMI::isATriangleToModify(Triangle *t, int c) 
    103124{ 
    104125        int u = t->indices[0], 
     
    109130 
    110131        return FALSE; 
    111 } 
    112  
    113 void VMI::modifyTriangles(Mesh *mesh, Change *c) 
    114 { 
    115         int i, t; 
    116  
    117         for (i=0; i<c->numMod; i++) 
    118         { 
    119                 t = c->modified[i].id; 
    120                 modifyTriangle(&mesh->triangles[t], c->u, c->v); 
    121  
    122                 //printf("New area of triangle %d:\n", t); 
    123                 mesh->triangles[t].area = computeTriangleArea(mesh->vertices, &mesh->triangles[t]); 
    124  
    125                 computeTriangleNormal(mesh->vertices, &mesh->triangles[t]); 
    126         } 
    127 } 
    128  
    129 void VMI::unmodifyTriangles(Mesh *mesh, Change *c) 
    130 { 
    131         int i, t; 
    132  
    133         for (i=0; i<c->numMod; i++) { 
    134                 t = c->modified[i].id; 
    135  
    136                 memcpy(&mesh->triangles[t], &c->modified[i], sizeof(Triangle)); 
    137                 /*printf("t(%d %d %d)\n", mesh->triangles[t].indices[0],  
    138                         mesh->triangles[t].indices[1], 
    139                         mesh->triangles[t].indices[2]);*/ 
    140         } 
    141 } 
    142  
    143 void VMI::deleteTriangles(Mesh *mesh, Change *c) 
    144 { 
    145         int i, t; 
    146  
    147         for (i=0; i<c->numDel; i++) 
    148         { 
    149                 t = c->deleted[i].id; 
    150  
    151                 //printf("Deleting triangle %d\n",t); 
    152                 mesh->triangles[t].enable = FALSE; 
    153                 mesh->currentNumTriangles--; 
    154         } 
    155 } 
    156  
    157 void VMI::undeleteTriangles(Mesh *mesh, Change *c) 
    158 { 
    159         int i, t; 
    160  
    161         for (i=0; i<c->numDel; i++) 
    162         { 
    163                 t = c->deleted[i].id; 
    164  
    165                 memcpy(&mesh->triangles[t], &c->deleted[i], sizeof(Triangle)); 
    166                 /*printf("t(%d %d %d)\n", mesh->triangles[t].indices[0],  
    167                         mesh->triangles[t].indices[1],  
    168                         mesh->triangles[t].indices[2]);*/ 
    169  
    170                 mesh->triangles[t].enable = TRUE; 
    171                 mesh->currentNumTriangles++; 
    172         } 
    173 } 
    174  
    175 int VMI::getTrianglesToModify(Mesh *mesh, int c, int p, Triangle *modified) 
    176 { 
    177         GLuint i, num = 0; 
    178  
    179         for (i=0; i<mesh->numTriangles; i++) 
    180         { 
    181                 if ((mesh->triangles[i].enable == TRUE) &&  
    182                                 isATriangleToModify(&mesh->triangles[i], c, p)) 
    183                 { 
    184  
    185                         //printf("Triangle to modify %d\n", i); 
    186                         // Save the original values of the triangle 
    187                         memcpy(&modified[num], &mesh->triangles[i], sizeof(Triangle)); 
    188                         num++; 
    189                 } 
    190         } 
    191  
    192         return num; 
    193132} 
    194133 
     
    211150} 
    212151 
    213 int VMI::getTrianglesToDelete(Mesh *mesh, int numMod, Triangle *modified, Triangle *deleted, int c, int p) 
    214 { 
    215         int i, num = 0; 
    216         GLuint t; 
    217  
    218         for (i=0; i<numMod; i++) 
    219         { 
    220                 t = modified[i].id; 
    221  
    222                 if (isATriangleToDelete(&mesh->triangles[t], c, p)) 
    223                 {                                              
    224                         //printf("Triangle to delete %d\n",t);                     
    225                         memcpy(&deleted[num], &modified[i], sizeof(Triangle)); 
    226                         num++; 
    227                 } 
    228         } 
    229         return num; 
    230 } 
    231  
    232 void VMI::printList(Triangle *list, int n) 
    233 { 
    234         int i; 
    235  
    236         for (i=0; i<n; i++) 
    237                 printf("%d ",list[i].id); 
    238  
    239         printf("\n"); 
    240 } 
    241  
    242 void VMI::deleteItem(Triangle *list, int *n, int item) 
    243 { 
    244         int i, j; 
    245  
    246         for (i=0; i<*n; i++) 
    247         { 
    248                 if (list[i].id == (GLuint)item) 
    249                 { 
    250                         // delete it 
    251                         for (j =i + 1 ; j<*n; j++) 
    252                                 list[j - 1] = list[j]; 
    253                         (*n)--; 
    254                         i--; // Delete all ocurrencies of an item 
    255                 } 
    256         } 
     152void VMI::modifyTriangles(Mesh *mesh, Change *c) 
     153{ 
     154        int i, t; 
     155 
     156        // Reallocate memory for new adjacent triangles from vertex v 
     157  if (c->numMod > 0) { 
     158      mesh->vertices[c->v].triangles =  
     159          (GLuint *)realloc(mesh->vertices[c->v].triangles,  
     160          (mesh->vertices[c->v].numTriangles + mesh->vertices[c->u].numTriangles) * sizeof(GLuint)); 
     161  } 
     162 
     163        for (i=0; i<c->numMod; i++) { 
     164                t = c->modified[i].id; 
     165                modifyTriangle(&mesh->triangles[t], c->u, c->v); 
     166 
     167                //printf("New area of triangle %d:\n", t); 
     168                mesh->triangles[t].area = computeTriangleArea(mesh->vertices, &mesh->triangles[t]); 
     169 
     170                computeTriangleNormal(mesh->vertices, &mesh->triangles[t]); 
     171 
     172                // Update vertex adjacency c->v 
     173                addItem((int *)mesh->vertices[c->v].triangles, (int *)&mesh->vertices[c->v].numTriangles, t); 
     174        } 
     175} 
     176 
     177void VMI::unmodifyTriangles(Mesh *mesh, Change *c) 
     178{ 
     179        int i, t; 
     180 
     181        for (i=0; i<c->numMod; i++) { 
     182                t = c->modified[i].id; 
     183 
     184                memcpy(&mesh->triangles[t], &c->modified[i], sizeof(Triangle)); 
     185                /*printf("t(%d %d %d)\n", mesh->triangles[t].indices[0],  
     186                        mesh->triangles[t].indices[1], 
     187                        mesh->triangles[t].indices[2]);*/ 
     188 
     189                // Update vertex adjacency c->v 
     190                delItem((int *)mesh->vertices[c->v].triangles, (int *)&mesh->vertices[c->v].numTriangles, t); 
     191        } 
     192 
     193} 
     194 
     195int VMI::hasEdge(Triangle *t, int e) { 
     196    int e0 = t->edges[0], 
     197        e1 = t->edges[1], 
     198        e2 = t->edges[2]; 
     199     
     200    if ((e == e0) || (e == e1) || (e == e2)) return TRUE; 
     201 
     202    return FALSE; 
     203} 
     204 
     205void VMI::getEdges(Triangle *t, Change *c, int *l, int *r) { 
     206 
     207    // Default edges 
     208    *l = -1; 
     209    *r = -1; 
     210 
     211    // Set edge l and r 
     212    if ((int)t->edges[0] == c->e) { 
     213        *l = t->edges[1]; 
     214        *r = t->edges[2]; 
     215        if ((int)mesh->edges[*l].u == c->v || (int)mesh->edges[*l].v == c->v) 
     216            swap(l, r); 
     217    } else if ((int)t->edges[1] == c->e) { 
     218        *l = t->edges[0]; 
     219        *r = t->edges[2]; 
     220        if ((int)mesh->edges[*l].u == c->v || (int)mesh->edges[*l].v == c->v) 
     221            swap(l, r); 
     222    } else if ((int)t->edges[2] == c->e) { 
     223        *l = t->edges[0]; 
     224        *r = t->edges[1]; 
     225        if ((int)mesh->edges[*l].u == c->v || (int)mesh->edges[*l].v == c->v) 
     226            swap(l, r); 
     227    } 
     228} 
     229 
     230void VMI::swap(int *i, int *j) { 
     231    int t; 
     232 
     233    t = *i; 
     234    *i = *j; 
     235    *j = t; 
     236} 
     237 
     238void VMI::deleteTriangles(Mesh *mesh, Change *c) 
     239{ 
     240            int i, j, t, v; 
     241#ifdef USE_EDGE_ADJACENCY 
     242    int t1, l, r; 
     243#endif 
     244     
     245    for (i=0; i<c->numDel; i++) { 
     246        t = c->deleted[i].id; 
     247 
     248        //printf("%d(%d,%d) Deleting triangle %d\n",c->e, c->u, c->v, t); 
     249 
     250        // Update vertex adjacency 
     251        for (j=0; j<3; j++) { 
     252            v = c->deleted[i].indices[j]; 
     253             
     254            delItem((int *)mesh->vertices[v].triangles,(int *)&mesh->vertices[v].numTriangles,t); 
     255        } 
     256 
     257#ifdef USE_EDGE_ADJACENCY 
     258        // Update triangle edge adjancency 
     259        // The modified triangles have to be calculated before 
     260        // Set edge l and r 
     261        getEdges(&c->deleted[i], c, &l, &r); 
     262        //printf("e%d(%d,%d): l:%d r:%d\n", c->e, c->u, c->v, l, r); 
     263        // Find triangle that contains edge l 
     264        for (j=0; j<c->numMod; j++) 
     265            if (hasEdge(&c->modified[j], l)) { 
     266                // Change edge l by r in the Mesh 
     267                t1 = c->modified[j].id; 
     268                //printf("t:%d %d %d %d\n", t1, mesh->triangles[t1].edges[0], mesh->triangles[t1].edges[1] ,mesh->triangles[t1].edges[2]); 
     269                if (mesh->triangles[t1].edges[0] == (GLuint)l) mesh->triangles[t1].edges[0] = r; 
     270                if (mesh->triangles[t1].edges[1] == (GLuint)l) mesh->triangles[t1].edges[1] = r; 
     271                if (mesh->triangles[t1].edges[2] == (GLuint)l) mesh->triangles[t1].edges[2] = r; 
     272                //printf("t:%d %d %d %d\n", t1, mesh->triangles[t1].edges[0], mesh->triangles[t1].edges[1] ,mesh->triangles[t1].edges[2]); 
     273                break; 
     274            } 
     275 
     276        // Delete edge l 
     277        if (l != -1) mesh->edges[l].enable = FALSE; 
     278#endif 
     279        mesh->triangles[t].enable = FALSE; 
     280        mesh->currentNumTriangles--; 
     281    } 
     282} 
     283 
     284void VMI::undeleteTriangles(Mesh *mesh, Change *c) { 
     285    int i, j, t, v; 
     286     
     287    for (i=0; i<c->numDel; i++) { 
     288        t = c->deleted[i].id; 
     289         
     290        //printf("Undeleting triangle %d\n",t); 
     291 
     292        memcpy(&mesh->triangles[t], &c->deleted[i], sizeof(Triangle)); 
     293        /*printf("t(%d %d %d)\n", mesh->triangles[t].indices[0],  
     294                                  mesh->triangles[t].indices[1],  
     295                                  mesh->triangles[t].indices[2]);*/ 
     296 
     297        // Update vertex adjacency 
     298        for (j=0; j<3; j++) { 
     299            v = c->deleted[i].indices[j]; 
     300             
     301            addItem((int *)mesh->vertices[v].triangles,(int *)&mesh->vertices[v].numTriangles,t); 
     302        } 
     303#ifdef USE_EDGE_ADJACENCY 
     304        // Enable adjacent edges 
     305        for (j=0; j<3; j++) { 
     306            mesh->edges[mesh->triangles[t].edges[j]].enable = TRUE; 
     307        } 
     308#endif 
     309        mesh->triangles[t].enable = TRUE; 
     310        mesh->currentNumTriangles++; 
     311    } 
     312} 
     313 
     314Triangle *VMI::getTrianglesToModify(Mesh *mesh, Change *c, GLuint *numMod) { 
     315  int i, t; 
     316  Triangle *modified = NULL; 
     317 
     318  // Allocating memory 
     319  modified = (Triangle *)malloc(mesh->vertices[c->u].numTriangles * sizeof(Triangle)); 
     320  *numMod = 0; 
     321   
     322  for (i=0; i<(int)mesh->vertices[c->u].numTriangles; i++) { 
     323       
     324      t = mesh->vertices[c->u].triangles[i]; 
     325       
     326      if ((mesh->triangles[t].enable == TRUE) &&  
     327          !isATriangleToDelete(&mesh->triangles[t], c->u, c->v)) { 
     328           
     329          //printf("Triangle to modify %d\n", t); 
     330          // Save the original values of the triangle 
     331          memcpy(&modified[*numMod], &mesh->triangles[t], sizeof(Triangle)); 
     332          (*numMod)++; 
     333      } 
     334  } 
     335  return modified; 
     336} 
     337 
     338Triangle *VMI::getTrianglesToDelete(Mesh *mesh, Change *c, GLuint *numDel) { 
     339  int i, t; 
     340  Triangle *deleted = NULL; 
     341   
     342  // Allocating memory 
     343  deleted = (Triangle *)malloc(mesh->vertices[c->u].numTriangles * sizeof(Triangle)); 
     344  *numDel = 0; 
     345 
     346  for (i=0; i<(int)mesh->vertices[c->u].numTriangles; i++) { 
     347       
     348      t = mesh->vertices[c->u].triangles[i]; 
     349 
     350      if ((mesh->triangles[t].enable == TRUE) && 
     351          isATriangleToDelete(&mesh->triangles[t], c->u, c->v)) { 
     352                                                     
     353          //printf("Triangle to delete %d\n",t);                     
     354          memcpy(&deleted[*numDel], &mesh->triangles[t], sizeof(Triangle)); 
     355          (*numDel)++; 
     356      } 
     357  } 
     358  return deleted; 
     359} 
     360 
     361void VMI::deleteEdges(Mesh *mesh, Change *c) { 
     362    int i, e; 
     363#ifndef USE_EDGE_ADJACENCY 
     364    int n0, n1, n2; 
     365#else 
     366    int j, u, v; 
     367#endif 
     368 
     369#ifdef USE_EDGE_ADJACENCY 
     370    for (i=0; i<c->numDel; i++) { 
     371         
     372        for (j=0; j<3; j++) { 
     373            e = c->deleted[i].edges[j]; 
     374            if (e != c->e) { 
     375                u = mesh->edges[e].u; 
     376                v = mesh->edges[e].v; 
     377                if (u == c->u || v == c->u) { 
     378                    //printf("%d\n",j); 
     379                    mesh->edges[e].enable = FALSE; 
     380                } 
     381            } 
     382        } 
     383        //getchar(); 
     384    } 
     385#else 
     386    for (i=0; i<c->numDel; i++) { 
     387 
     388        n0 = c->deleted[i].indices[0]; 
     389        n1 = c->deleted[i].indices[1]; 
     390        n2 = c->deleted[i].indices[2]; 
     391 
     392        if ((n0 == c->u && n1 != c->v) || (n1 == c->u && n0 != c->v)) { 
     393            e = findEdge(mesh->edges, mesh->numEdges, n0, n1); 
     394 
     395            //printf("a\n"); 
     396            if (e != -1) 
     397                mesh->edges[e].enable = FALSE; 
     398        } 
     399 
     400        if ((n1 == c->u && n2 != c->v) || (n2 == c->u && n1 != c->v)) { 
     401            e = findEdge(mesh->edges, mesh->numEdges, n1, n2); 
     402             
     403            //printf("b\n"); 
     404            if (e != -1) 
     405                mesh->edges[e].enable = FALSE; 
     406        } 
     407 
     408        if ((n0 == c->u && n2 != c->v) || (n2 == c->u && n0 != c->v)) { 
     409            e = findEdge(mesh->edges, mesh->numEdges, n0, n2); 
     410             
     411            //printf("c\n"); 
     412            if (e != -1) 
     413                mesh->edges[e].enable = FALSE; 
     414        } 
     415        //getchar(); 
     416    } 
     417#endif 
     418} 
     419 
     420void VMI::modifyEdges(Mesh *mesh, Change *c) { 
     421    int e, u, v; 
     422     
     423    // Update mesh and heap 
     424    for(e = 0; e < (int)mesh->numEdges; e++) { 
     425         
     426        if (mesh->edges[e].enable == TRUE) { 
     427             
     428            // Modify edge 
     429            if ((int)mesh->edges[e].u == c->u) 
     430                mesh->edges[e].u = c->v; 
     431             
     432            if ((int)mesh->edges[e].v == c->u) 
     433                mesh->edges[e].v = c->v; 
     434             
     435            // Check edge 
     436            u = mesh->edges[e].u; 
     437            v = mesh->edges[e].v; 
     438             
     439            // if the edge is not valid, we simply delete it 
     440            if ((u == v) ||  
     441                (u == c->u) || (v == c->u))  
     442                 
     443                mesh->edges[e].enable = FALSE; 
     444        } 
     445    } 
    257446} 
    258447 
     
    260449 
    261450// Compute the triangle mesh changes due to a heap node simplification 
    262 void VMI::computeChanges(Mesh *mesh, Change *c) 
    263 { 
    264         Triangle m[MAX_NUM_TRI], d[MAX_NUM_TRI/2]; 
    265         int numMod = getTrianglesToModify(mesh, c->u, c->v, m); 
    266         int numDel = getTrianglesToDelete(mesh, numMod, m, d, c->u, c->v); 
    267         int i; 
    268  
    269         //printf("d %d\n",numDel); 
    270         //printList(d, numDel); 
    271  
    272         for (i=0; i<numDel; i++) 
    273                 deleteItem(m, &numMod, d[i].id); 
    274  
    275         //printf("m %d\n",numMod); 
    276         //printList(m, numMod); 
    277         //getchar(); 
    278  
    279         c->numDel = numDel; 
    280         // Free memory 
    281         if (c->deleted != NULL) free(c->deleted); 
    282         // Allocate memory 
    283         c->deleted = (Triangle *)malloc(sizeof(Triangle) * numDel); 
    284          
    285         if (c->deleted == NULL) 
    286         { 
    287                 fprintf(stderr, "Error allocating memory\n"); 
    288                 exit(1); 
    289         } 
    290          
    291         memcpy(c->deleted, d, sizeof(Triangle) * numDel); 
    292  
    293         c->numMod = numMod; 
    294          
    295         // Free memory 
    296         if (c->modified != NULL) free(c->modified); 
    297          
    298         // Allocate memory 
    299         c->modified = (Triangle *)malloc(sizeof(Triangle) * numMod); 
    300          
    301         if (c->modified == NULL) 
    302         { 
    303                 fprintf(stderr, "Error allocating memory\n"); 
    304                 exit(1); 
    305         } 
    306          
    307         memcpy(c->modified, m, sizeof(Triangle) * numMod); 
    308  
    309         //printChange(c); 
    310         //getchar(); 
     451void VMI::computeChange(Mesh *mesh, Change *c) { 
     452  GLuint numMod, numDel; 
     453  Triangle *m = getTrianglesToModify(mesh, c, &numMod), 
     454            *d = getTrianglesToDelete(mesh, c, &numDel); 
     455   
     456  c->numDel   = numDel; 
     457  c->deleted  = d; 
     458   
     459  c->numMod   = numMod; 
     460  c->modified = m; 
     461 
     462  //printChange(c); 
     463  //getchar(); 
    311464} 
    312465 
     
    316469        modifyTriangles(mesh, c); 
    317470 
     471        // This function must be called after modifyTriangles() 
    318472        deleteTriangles(mesh, c); 
    319473 
     474        // Delete vertex u 
    320475        mesh->vertices[c->u].enable = FALSE; 
    321476        mesh->currentNumVertices--; 
     477 
     478        //printMesh(mesh); 
     479        //getchar(); 
    322480} 
    323481 
    324482void VMI::undoChange(Mesh *mesh, Change *c) 
    325 {     
     483{ 
    326484        unmodifyTriangles(mesh, c); 
    327485 
     
    330488        mesh->vertices[c->u].enable = TRUE; 
    331489        mesh->currentNumVertices++; 
    332 } 
    333  
     490 
     491        //printMesh(mesh); 
     492        //getchar(); 
     493} 
     494 
     495/* 
    334496namespace VMI{ 
    335497std::map<int, INTVECTOR> inversemap; 
    336498} 
     499*/ 
    337500 
    338501//      Save simplification sequence in Geometry Game Tools format. 
    339 extern void     VMI::saveSimplificationSequence(Change  *c) 
    340 { 
    341  
    342         int stepnum= 1; 
    343         for(INTVECTOR::iterator it= inversemap[c->u].begin(); it!=inversemap[c->u].end(); it++) 
     502extern void     VMI::saveSimplificationSequence(Change  *c,int obligatory) 
     503{ 
     504        Geometry::MeshSimplificationSequence::Step      step; 
     505 
     506        if (mSequence == NULL) 
    344507        { 
    345                 vmiStep step; 
    346                 step.mV0        =       *it; 
    347                 step.mV1        =       c->v; 
    348  
    349                 //      If only one triangle has been deleted. 
    350                 if (c->numDel == 1) 
    351                 { 
    352                         step.mT0        =       c->deleted[0].id; 
    353                         step.mT1        =       c->deleted[0].id; 
    354                 } 
    355                 //      If two triangles have been deleted. 
    356                 else 
    357                 { 
    358                         step.mT0        =       c->deleted[0].id; 
    359                         step.mT1        =       c->deleted[1].id; 
    360                 } 
    361  
    362                 step.x  =       0.0; 
    363                 step.y  =       0.0; 
    364                 step.z  =       0.0; 
    365  
    366                 //      Write obligatory field. 
    367                 if (stepnum==inversemap[c->u].size()) 
    368                         step.obligatory =       0; 
    369                 else 
    370                         step.obligatory =       1; 
    371                 stepnum++; 
    372  
    373                 //      List of new triangles. 
    374                 //      For each modified triangle. 
    375                 for (int        i = 0;  i < c->numMod;  i++) 
    376                 { 
    377                         step.mModfaces.push_back(c->modified[i].id); 
    378                 } 
    379  
    380                 //      Add step to list of changes. 
    381                 mVMISteps.push_back(step); 
    382         } 
    383 } 
     508                mSequence       =       new Geometry::MeshSimplificationSequence(); 
     509        } 
     510 
     511        step.mV0        =       c->v; 
     512        step.mV1        =       c->u; 
     513 
     514        //      Debug. 
     515        cout    <<      "----> V0: " 
     516                                <<      step.mV0 
     517                                <<      " V1: " 
     518                                <<      step.mV1 
     519                                <<      endl; 
     520 
     521        //      If only one triangle has been deleted. 
     522        if (c->numDel == 1) 
     523        { 
     524                step.mT0        =       c->deleted[0].id; 
     525                step.mT1        =       c->deleted[0].id; 
     526        } 
     527        //      If two triangles have been deleted. 
     528        else 
     529        { 
     530                step.mT0        =       c->deleted[0].id; 
     531                step.mT1        =       c->deleted[1].id; 
     532        } 
     533 
     534        step.x  =       0.0; 
     535        step.y  =       0.0; 
     536        step.z  =       0.0; 
     537 
     538        //      Write obligatory field. 
     539        step.obligatory =       obligatory; 
     540 
     541        //      List of new triangles. 
     542        //      For each modified triangle. 
     543        for (int        i = 0;  i < c->numMod;  i++) 
     544        { 
     545                step.mModfaces.push_back(c->modified[i].id); 
     546        } 
     547 
     548        //      Add step to list of changes. 
     549        mSequence->mSteps.push_back(step); 
     550} 
     551 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/color.cpp

    r983 r2090  
    77using namespace VMI; 
    88 
    9 Color *VMI::initColors(GLuint numTriangles) { 
     9Color *VMI::initColors(GLuint numColors) { 
    1010    Color *colors; 
    1111 
    12     colors = (Color *)malloc(sizeof(Color) * (numTriangles + 1)); // Triangles start at 1 
     12    colors = (Color *)malloc(sizeof(Color) * (numColors + 1)); 
     13                /* The 0 position is reserved for the BACKGROUND, thus color of triangle 0 
     14       is at 1 position and so on */ 
    1315 
    1416    if (colors == NULL) { 
     
    1719    } 
    1820 
    19     // Fill the color buffer with BACKGROUND 
    20     memset(colors, BACKGROUND, sizeof(Color) * (numTriangles + 1)); 
     21    /* Fill the color buffer with the BACKGROUND color */ 
     22    memset(colors, BACKGROUND, sizeof(Color) * (numColors + 1)); 
    2123 
    2224    return colors; 
    2325} 
    2426 
    25 void VMI::fillAllColors(Color *colors, GLuint numTriangles, GLuint begin, GLuint end, GLubyte color) { 
     27void VMI::fillAllColors(Color *colors, GLuint numColors, GLuint begin, GLuint end, GLubyte color) { 
    2628    GLuint i; 
    2729 
    28     if (end > numTriangles) return; 
     30    if (end > numColors) return; 
    2931 
    3032    for (i=begin; i<end; i++) { 
     
    8486} 
    8587 
    86 void VMI::setColors2(Color *colors, GLuint numTriangles, GLuint begin, GLuint end) { 
     88void VMI::setColors2(Color *colors, GLuint numColors, GLuint begin, GLuint end) { 
    8789    GLuint i,  
    8890        channel = 0, 
    89         step = (numTriangles < MAX_NUM_COLORS) ? (MAX_NUM_COLORS / numTriangles) : 1; 
     91        step = (numColors < MAX_NUM_COLORS) ? (MAX_NUM_COLORS / numColors) : 1; 
    9092    GLubyte r = 1; 
    9193 
    92     if (end > numTriangles) return; 
     94    if (end > numColors) return; 
    9395 
    9496    for (i = begin; i < end; i++) { 
     
    126128            channel = 0; 
    127129        } 
    128         //printf("c:%d (%d,%d,%d,%d)\n",i, colors[i].r, colors[i].g, colors[i].b,  colors[i].a); 
     130        //printf("c:%d (%d,%d,%d,%d)\n", i, colors[i].r, colors[i].g, colors[i].b,  colors[i].a); 
    129131        //getchar(); 
    130132    } 
    131133} 
    132134 
    133 void VMI::setColors3(Color *colors, GLuint numTriangles, GLubyte color) { 
     135void VMI::setColors3(Color *colors, GLuint numColors, GLubyte color) { 
    134136    GLuint i; 
    135137     
    136     for (i = 0; i < numTriangles; i++) { 
     138    for (i = 0; i < numColors; i++) { 
    137139 
    138140        colors[i].r = color; 
     
    141143        colors[i].a = 0; 
    142144 
    143         //printf("c:%d (%d,%d,%d,%d)\n",i, colors[i].r, colors[i].g, colors[i].b,  colors[i].a); 
     145        //printf("c:%d (%d,%d,%d,%d)\n", i, colors[i].r, colors[i].g, colors[i].b,  colors[i].a); 
    144146        //getchar(); 
    145147    } 
    146148} 
    147149 
    148 void VMI::setColors4(Color *colors, GLuint numTriangles) { 
     150void VMI::setColors4(Color *colors, GLuint numColors) { 
    149151    GLuint i, c = 1; 
    150152     
    151     for (i = 0; i < numTriangles; i++) { 
     153    for (i = 0; i < numColors; i++) { 
    152154 
    153155        colors[i].r = c & 0xFF; 
     
    156158        colors[i].a = (c & 0xFF000000) >> 24; 
    157159 
    158         //printf("c:%d (%d,%d,%d,%d)\n",i, colors[i].r, colors[i].g, colors[i].b,  colors[i].a); 
     160        //printf("c:%d (%d,%d,%d,%d)\n", i, colors[i].r, colors[i].g, colors[i].b,  colors[i].a); 
    159161        c++;  
    160162    } 
     
    162164} 
    163165 
    164 void VMI::viewColors(Color *colors, GLuint numTriangles) { 
     166void VMI::viewColors(Color *colors, GLuint numColors) { 
    165167    GLuint i; 
    166168 
    167169    printf("\n"); 
    168     for (i=0; i<numTriangles; i++)  
    169         printf("c:%d (%d,%d,%d,%d)\n",i, colors[i].r, colors[i].g, colors[i].b,  colors[i].a); 
     170    for (i=0; i<numColors; i++)  
     171        printf("c:%d (%d,%d,%d,%d)\n", i, colors[i].r, colors[i].g, colors[i].b,  colors[i].a); 
    170172} 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/glm.c

    r983 r2090  
    135135    copied = 1; 
    136136    for (i = 1; i <= *numvectors; i++) { 
    137         for (j = 1; j <= copied; j++) { 
     137        for (j = 1; j </*=*/ copied; j++) { 
    138138            if (glmEqual(&vectors[3 * i], &copies[3 * j], epsilon)) { 
    139139                goto duplicate; 
     
    17231723    vectors  = model->vertices; 
    17241724    copies = glmWeldVectors(vectors, &numvectors, epsilon); 
    1725      
    1726 #if 0 
     1725 
     1726//#if 0 
    17271727    printf("glmWeld(): %d redundant vertices.\n",  
    1728         model->numvertices - numvectors - 1); 
    1729 #endif 
     1728        model->numvertices - numvectors /* - 1*/); 
     1729//#endif 
    17301730     
    17311731    for (i = 0; i < model->numtriangles; i++) { 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/histogram.cpp

    r983 r2090  
    99#include "../include/histogram.h" 
    1010#include "../include/metrics.h" 
     11 
     12#define WIN_MARGIN 2 
    1113 
    1214using namespace VMI; 
     
    185187} 
    186188 
     189void VMI::getBoundingBox(Change *c, GLfloat min[3], GLfloat max[3]) { 
     190    GLuint  i, j, t, v; 
     191    GLfloat maxx = 0.0f, minx = 0.0f,  
     192            maxy = 0.0f, miny = 0.0f, 
     193            maxz = 0.0f, minz = 0.0f; 
     194     
     195    /* get the max/mins */ 
     196    for (i=0; i<(GLuint)c->numMod; i++) { 
     197        t = c->modified[i].id; 
     198         
     199        for (j=0; j<3; j++) { 
     200            v = mesh->triangles[t].indices[j]; 
     201            // v 
     202            if (maxx < mesh->vertices[v].x) 
     203                maxx = mesh->vertices[v].x; 
     204            if (minx > mesh->vertices[v].x) 
     205                minx = mesh->vertices[v].x; 
     206             
     207            if (maxy < mesh->vertices[v].y) 
     208                maxy = mesh->vertices[v].y; 
     209            if (miny > mesh->vertices[v].y) 
     210                miny = mesh->vertices[v].y; 
     211             
     212            if (maxz < mesh->vertices[v].z) 
     213                maxz = mesh->vertices[v].z; 
     214            if (minz > mesh->vertices[v].z) 
     215                minz = mesh->vertices[v].z; 
     216        } 
     217    } 
     218 
     219    for (i=0; i<(GLuint)c->numDel; i++) { 
     220        t = c->deleted[i].id; 
     221         
     222        for (j=0; j<3; j++) { 
     223            v = mesh->triangles[t].indices[j]; 
     224            // v 
     225            if (maxx < mesh->vertices[v].x) 
     226                maxx = mesh->vertices[v].x; 
     227            if (minx > mesh->vertices[v].x) 
     228                minx = mesh->vertices[v].x; 
     229             
     230            if (maxy < mesh->vertices[v].y) 
     231                maxy = mesh->vertices[v].y; 
     232            if (miny > mesh->vertices[v].y) 
     233                miny = mesh->vertices[v].y; 
     234             
     235            if (maxz < mesh->vertices[v].z) 
     236                maxz = mesh->vertices[v].z; 
     237            if (minz > mesh->vertices[v].z) 
     238                minz = mesh->vertices[v].z; 
     239        } 
     240    } 
     241 
     242    min[0] = minx; 
     243    min[1] = miny; 
     244    min[2] = minz; 
     245 
     246    max[0] = maxx; 
     247    max[1] = maxy; 
     248    max[2] = maxz; 
     249} 
     250 
     251void VMI::getWindow(GLfloat min[3], GLfloat max[3], int minw[2], int maxw[2]) { 
     252    GLdouble winx, winy, winz, 
     253             mv[16], pm[16]; 
     254    GLint vp[4], minx, miny, maxx, maxy; 
     255 
     256    glGetDoublev(GL_MODELVIEW_MATRIX, mv); 
     257    glGetDoublev(GL_PROJECTION_MATRIX, pm); 
     258    glGetIntegerv(GL_VIEWPORT, vp); 
     259     
     260    /* The eight points of the bounding box */ 
     261    /* Point 1 (min,min,min) */ 
     262    gluProject((GLdouble)min[0], (GLdouble)min[1], (GLdouble)min[2], mv, pm, vp, &winx, &winy, &winz); 
     263    minx = winx; 
     264    maxx = winx; 
     265    miny = winy; 
     266    maxy = winy; 
     267 
     268    /* Point 2 (max,min,min) */ 
     269    gluProject((GLdouble)max[0], (GLdouble)min[1], (GLdouble)min[2], mv, pm, vp, &winx, &winy, &winz); 
     270    if (winx < minx) minx =winx; 
     271    if (winx > maxx) maxx =winx; 
     272    if (winy < miny) miny =winy; 
     273    if (winy > maxy) maxy =winy; 
     274 
     275    /* Point 3 (min,max,min) */ 
     276    gluProject((GLdouble)min[0], (GLdouble)max[1], (GLdouble)min[2], mv, pm, vp, &winx, &winy, &winz); 
     277    if (winx < minx) minx =winx; 
     278    if (winx > maxx) maxx =winx; 
     279    if (winy < miny) miny =winy; 
     280    if (winy > maxy) maxy =winy; 
     281 
     282    /* Point 4 (max,max,min) */ 
     283    gluProject((GLdouble)max[0], (GLdouble)max[1], (GLdouble)min[2], mv, pm, vp, &winx, &winy, &winz); 
     284    if (winx < minx) minx =winx; 
     285    if (winx > maxx) maxx =winx; 
     286    if (winy < miny) miny =winy; 
     287    if (winy > maxy) maxy =winy; 
     288 
     289    /* Point 5 (min,min,max) */ 
     290    gluProject((GLdouble)min[0], (GLdouble)min[1], (GLdouble)max[2], mv, pm, vp, &winx, &winy, &winz); 
     291    if (winx < minx) minx =winx; 
     292    if (winx > maxx) maxx =winx; 
     293    if (winy < miny) miny =winy; 
     294    if (winy > maxy) maxy =winy; 
     295 
     296    /* Point 6 (max,min,max) */ 
     297    gluProject((GLdouble)max[0], (GLdouble)min[1], (GLdouble)max[2], mv, pm, vp, &winx, &winy, &winz); 
     298    if (winx < minx) minx =winx; 
     299    if (winx > maxx) maxx =winx; 
     300    if (winy < miny) miny =winy; 
     301    if (winy > maxy) maxy =winy; 
     302 
     303    /* Point 7 (min,max,max) */ 
     304    gluProject((GLdouble)min[0], (GLdouble)max[1], (GLdouble)max[2], mv, pm, vp, &winx, &winy, &winz); 
     305    if (winx < minx) minx =winx; 
     306    if (winx > maxx) maxx =winx; 
     307    if (winy < miny) miny =winy; 
     308    if (winy > maxy) maxy =winy; 
     309 
     310    /* Point 8 (max,max,max) */ 
     311    gluProject((GLdouble)max[0], (GLdouble)max[1], (GLdouble)max[2], mv, pm, vp, &winx, &winy, &winz); 
     312    if (winx < minx) minx =winx; 
     313    if (winx > maxx) maxx =winx; 
     314    if (winy < miny) miny =winy; 
     315    if (winy > maxy) maxy =winy; 
     316 
     317    minw[0] = minx; 
     318    minw[1] = miny; 
     319 
     320    maxw[0] = maxx; 
     321    maxw[1] = maxy; 
     322} 
     323 
     324void VMI::getSWHistogramWin(GLuint *histogram, GLubyte *pixels, GLfloat min[3], GLfloat max[3], Change *c) { 
     325    GLubyte r, g, b, a; 
     326    GLuint i, t, p, numPixels, 
     327          *histoAux = (GLuint *)calloc(mesh->numTriangles + 1, sizeof(GLuint)); 
     328    GLint minw[2], maxw[2], ox, oy, h, w; 
     329 
     330    getWindow(min, max, minw, maxw); 
     331 
     332    w = ABS(maxw[0] - minw[0]); 
     333    h = ABS(maxw[1] - minw[1]); 
     334    //printf("w:%d h:%d\n", w, h); 
     335 
     336    ox = minw[0]; 
     337    oy = minw[1]; 
     338 
     339    ox -= WIN_MARGIN; 
     340    if (ox < 0)  ox = 0; 
     341    oy -= WIN_MARGIN; 
     342    if (oy < 0)  oy = 0; 
     343 
     344    w += (WIN_MARGIN * 2); 
     345    if (w > width)  w = width; 
     346    h += (WIN_MARGIN * 2); 
     347    if (h > height) h = height; 
     348 
     349    //printf("(%d,%d)\n", ox, oy); 
     350    //printf("w:%d h:%d\n", w, h); 
     351    //getchar(); 
     352 
     353    numPixels = w * h; 
     354    //printf("NumPixels: %d\n", numPixels); 
     355 
     356    glReadPixels(ox, oy, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 
     357 
     358    for (i=0; i<numPixels; i++) { // pixel i 
     359        p = i << 2; // 4 * i 
     360        r = pixels[p]; 
     361        g = pixels[p + 1]; 
     362        b = pixels[p + 2]; 
     363        a = pixels[p + 3]; 
     364 
     365        t = r + (g << 8) + (b << 16) + (a << 24); // triangle color 
     366        //printf("pixel:%d (%d,%d,%d,%d) t %d\n", i, r, g, b, a, t); 
     367 
     368        histoAux[t]++;    
     369    } 
     370 
     371    for (i=0; i<(GLuint)c->numMod; i++) { 
     372        t = (GLuint)c->modified[i].id; 
     373 
     374        histogram[t + 1] = histoAux[t + 1];  
     375    } 
     376 
     377    free(histoAux); 
     378    //getchar(); 
     379} 
     380 
    187381void VMI::getSWHistoByOcclusionQuery(Mesh *mesh, Color *colors, GLuint *histogram) { 
    188382    GLuint i, v1, v2, v3; 
    189     GLint area; 
     383    GLint area, totalArea = 0; 
    190384 
    191385    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
     
    196390            glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queries[i]); 
    197391             
    198             // Render triangle i 
     392            /* Render triangle i */ 
    199393            glBegin(GL_TRIANGLES); 
    200394            v1= mesh->triangles[i].indices[0]; 
     
    226420            } else { 
    227421                histogram[i + 1] = area; 
     422 
     423                totalArea += area; 
    228424            } 
    229425        } 
    230426    } 
    231     histogram[0] = computeBackgroundArea(mesh, histogram); // Compute area of the background 
    232  
     427 
     428    /* Compute the background area */ 
     429    histogram[0] = (width * height) - totalArea; 
     430 
     431    //printf("background area: %d\n", histogram[0]); 
    233432} 
    234433 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/interleave.cpp

    r983 r2090  
    1717static Color   *buf_colors = NULL; 
    1818 
    19 static GLuint vertex_buf = 0, 
     19GLuint vertex_buf = 0, 
    2020       color_buf = 0; 
    2121} 
     
    100100    setupVertexArray(mesh, colors); 
    101101     
    102     // Disable the vertex array functionality: 
    103     glDisableClientState(GL_VERTEX_ARRAY); 
    104     glDisableClientState(GL_COLOR_ARRAY); 
    105      
    106     glDeleteBuffersARB(1, &vertex_buf); 
    107      
    108     glGenBuffersARB(1, &vertex_buf); 
    109102    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); 
    110103    glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data 
    111104     
    112     glDeleteBuffersARB(1, &color_buf); 
    113      
    114     glGenBuffersARB(1, &color_buf); 
    115105    glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); 
    116106    glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * sizeof(Color), buf_colors, GL_STATIC_DRAW_ARB); //upload data 
     
    182172    updateVertexArray(mesh); 
    183173     
    184     // Disable the vertex array functionality: 
    185     glDisableClientState(GL_VERTEX_ARRAY); 
    186      
    187     glDeleteBuffersARB(1, &vertex_buf); 
    188      
    189     glGenBuffersARB(1, &vertex_buf); 
    190174    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); 
    191175    glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/main.cpp

    r983 r2090  
    2525          bBeQuiet         = GL_FALSE, 
    2626          bSaveLog         = GL_FALSE, 
    27           bLoadCamerasFromFile = GL_FALSE; 
     27          bLoadCamerasFromFile = GL_FALSE, 
     28          bRemoveRedundantVertices = GL_TRUE; 
    2829 
    2930GLuint cameraType = 0, 
     
    5556    gluPerspective(fov, (GLdouble)width / height, 0.1, 40.0); 
    5657     
    57     glPixelStorei(GL_PACK_ALIGNMENT, 1); 
    58     glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
     58    //glPixelStorei(GL_PACK_ALIGNMENT, 1); 
     59    //glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
    5960     
    6061    glShadeModel(GL_FLAT); 
     
    6263    glDisable(GL_BLEND); 
    6364 
    64     //glEnable(GL_CULL_FACE); // important 
     65    glEnable(GL_CULL_FACE); // important 
    6566 
    6667    glEnable(GL_DEPTH_TEST); 
    67     glDepthFunc(GL_LEQUAL); 
    6868 
    6969    if (GL_TRUE != glewIsSupported((const char*) "GL_EXT_framebuffer_object")) 
     
    117117    { 
    118118        fprintf(stderr,"GL_ARB_vertex_buffer_object extension is not available!\n"); 
     119    } else { 
     120#ifdef VERTEX_BUFFER_OBJECTS 
     121         
     122        glGenBuffersARB(1, &vertex_buf); 
     123         
     124        glGenBuffersARB(1, &color_buf); 
     125#endif 
    119126    } 
    120127    if (GL_TRUE != glewIsSupported((const char*) "GL_ARB_occlusion_query")) 
     
    238245    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    239246 
    240     // Switch to projection mode 
    241     glMatrixMode(GL_PROJECTION); 
    242     // Save previous matrix which contains the  
    243     // settings for the perspective projection 
    244     glPushMatrix(); 
    245     // Reset matrix 
    246     glLoadIdentity(); 
    247     glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, 1.0, 1.0); 
    248     glMatrixMode(GL_MODELVIEW); 
    249     glLoadIdentity(); 
     247    setOrthographicProjection(); 
    250248 
    251249    glEnable(GL_TEXTURE_2D); 
    252250    glBindTexture(GL_TEXTURE_2D, color_tex); 
    253251    glBegin(GL_QUADS); 
    254     glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); 
    255     glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0); 
    256     glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 0.0); 
    257     glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 0.0); 
     252     
     253    glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f); 
     254    glTexCoord2f(1.0f, 0.0f); glVertex2f(width, 0.0f);  
     255    glTexCoord2f(1.0f, 1.0f); glVertex2f(width, height); 
     256    glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, height); 
     257 
    258258    glEnd(); 
    259259    glFlush(); 
     
    312312    /////////////////////////////////////////////////////////////////////////// 
    313313} 
     314void VMI::renderSceneWin(GLuint **histogram, GLuint numCameras, Change *c) 
     315{ 
     316    GLuint i, j, t, background; 
     317    int del_area, mod_area; 
     318    GLfloat min[3], max[3]; 
     319 
     320    // draw to the frame buffer 
     321    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb); 
     322 
     323    //printChange(c); 
     324    getBoundingBox(c, min, max); 
     325    //printf("\n(%f,%f,%f)-(%f,%f,%f)\n", min[0], min[1], min[2], max[0], max[1], max[2]); 
     326     
     327    // Apply HW acceleration OpenGL Technique 
     328    applyHWAcceleration();  
     329 
     330    glDrawBuffer(GL_BACK); 
     331    glReadBuffer(GL_BACK); 
     332 
     333    // Get the projected areas for all cameras 
     334    for (i=0; i<numCameras; i++) { 
     335         
     336        // Clear color and depth buffers 
     337        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     338         
     339        glMatrixMode(GL_MODELVIEW); 
     340        glLoadIdentity(); 
     341         
     342        // Camera  i 
     343        gluLookAt(cameras[i].eyeX, cameras[i].eyeY, cameras[i].eyeZ, 
     344                  cameras[i].centerX, cameras[i].centerY, cameras[i].centerZ, 
     345                  cameras[i].upX, cameras[i].upY, cameras[i].upZ); 
     346         
     347        renderGeometry(); 
     348        glFlush(); 
     349         
     350        /////////////////////////////////////////////////////////////////////////// 
     351        background = histogram[i][0]; 
     352        //printf("b: %d\n", histogram[i][0]); 
     353 
     354        del_area = 0; 
     355        mod_area = 0; 
     356 
     357        for (j=0; j<( GLuint)c->numDel; j++) { 
     358            t = c->deleted[j].id; 
     359            del_area += histogram[i][t + 1]; 
     360 
     361            histogram[i][t + 1] = 0; 
     362        } 
     363 
     364        for (j=0; j<( GLuint)c->numMod; j++) { 
     365            t = c->modified[j].id; 
     366            del_area += histogram[i][t + 1]; 
     367 
     368            histogram[i][t + 1] = 0; 
     369        } 
     370         
     371        getSWHistogramWin(histogram[i], pixels, min, max, c);  
     372 
     373        for (j=0; j<( GLuint)c->numMod; j++) { 
     374            t = c->modified[j].id; 
     375            mod_area += histogram[i][t + 1]; 
     376 
     377            //printf("t%d: %d\n",t, histogram[i][t + 1]); 
     378        } 
     379        histogram[i][0] = background + (del_area - mod_area); 
     380        //printf("b: %d\n", histogram[i][0]); 
     381    } 
     382    //getchar(); 
     383 
     384    // draw to the window, reading from the color texture 
     385    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
     386 
     387    if (!bEnableOffScreen) drawTexture(); 
     388     
     389    /////////////////////////////////////////////////////////////////////////// 
     390} 
    314391 
    315392void VMI::freeMemory(void) { 
     
    339416#endif 
    340417 
    341                 glutDestroyWindow(vmiWin); 
     418                if (vmiWin != 0) 
     419                { 
     420                        glutDestroyWindow(vmiWin); 
     421                } 
     422 
     423#ifdef VERTEX_BUFFER_OBJECTS 
     424    glDeleteBuffersARB(1, &vertex_buf); 
     425     
     426    glDeleteBuffersARB(1, &color_buf); 
     427#endif 
     428 
     429        pmodel = NULL; // The .obj model 
     430 
     431        pixels = NULL; 
     432 
     433        cameras = NULL; 
     434        numCameras = 0; 
     435 
     436        colors = NULL; 
     437 
     438        histogram = NULL; 
     439  queries = NULL; 
     440 
     441        mesh = NULL; 
     442 
     443        initialIs = NULL; 
     444 
     445        vmiWin = 0; 
    342446} 
    343447 
     
    391495 
    392496#ifdef SALIENCY 
    393 #ifdef KL // Kullback-Leibler 
    394         sprintf(s,"%s_%d_%d_VKL_S.obj", filename, numCameras, mesh->currentNumTriangles); 
    395 #endif 
    396 #ifdef MI // Mutual Information 
    397         sprintf(s,"%s_%d_%d_VMI_S.obj", filename, numCameras, mesh->currentNumTriangles); 
    398 #endif 
    399 #ifdef HE // Hellinger 
    400         sprintf(s,"%s_%d_%d_VHE_S.obj", filename, numCameras, mesh->currentNumTriangles); 
    401 #endif 
    402 #ifdef CS // Chi-Square 
    403         sprintf(s,"%s_%d_%d_VCS_S.obj", filename, numCameras, mesh->currentNumTriangles); 
    404 #endif 
     497    sprintf(s,"%s_%d_%d_%s_S.obj", filename, numCameras, mesh->currentNumTriangles, EXT); 
    405498#else 
    406 #ifdef KL // Kullback-Leibler 
    407         sprintf(s,"%s_%d_%d_VKL.obj", filename, numCameras, mesh->currentNumTriangles); 
    408 #endif 
    409 #ifdef MI // Mutual Information 
    410         sprintf(s,"%s_%d_%d_VMI.obj", filename, numCameras, mesh->currentNumTriangles); 
    411 #endif 
    412 #ifdef HE // Hellinger 
    413         sprintf(s,"%s_%d_%d_VHE.obj", filename, numCameras, mesh->currentNumTriangles); 
    414 #endif 
    415 #ifdef CS // Chi-Square 
    416         sprintf(s,"%s_%d_%d_VCS.obj", filename, numCameras, mesh->currentNumTriangles); 
    417 #endif 
     499    sprintf(s,"%s_%d_%d_%s.obj", filename, numCameras, mesh->currentNumTriangles, EXT); 
    418500#endif 
    419501 
     
    454536 *  RGBA display mode, and handle input events. 
    455537 */ 
    456 //int main(int argc, char** argv) 
    457 //{ 
    458 //    char s[MAX_CHAR]; 
    459 // 
    460 //    process_cmdline(argc, argv); 
    461 //     
    462 //    // Load a .obj model 
    463 //    pmodel = glmReadOBJ(argv[argc-1]); 
    464 //    if (!pmodel) exit(0); 
    465 //    glmUnitize(pmodel); 
    466 //    //glmFacetNormals(pmodel); 
    467 //    //glmVertexNormals(pmodel, 90.0); 
    468 // 
    469 //    mesh = initMesh(pmodel); 
    470 //    //printMesh(mesh); 
    471 //    //getchar(); 
    472 // 
    473 //    if ((numDemandedTriangles == 0) || (numDemandedTriangles >= mesh->currentNumTriangles)) 
    474 //        usage_error("Illegal number of triangles."); 
    475 // 
    476 //    printf("w: %d h: %d\n", width, height); 
    477 //    printf("t: %d c: %d o: %d r: %f\n", numDemandedTriangles, cameraType, bEnableOffScreen, radius); 
    478 //    //getchar(); 
    479 // 
    480 //    // Get a filename without extension 
    481 //    strncpy(filename, argv[argc - 1], strlen(argv[argc - 1]) - 4); 
    482 //     
    483 //    glutInit(&argc, argv); 
    484 //    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA); /* RGB and Alpha */ 
    485 //    glutInitWindowSize(width, height); 
    486 //    glutInitWindowPosition(100, 100); 
    487 // 
    488 //#ifdef KL // Kullback-Leibler 
    489 //    sprintf(s, "VKL - [%s]", argv[argc-1]);  
    490 //#endif 
    491 //#ifdef MI // Mutual Information 
    492 //    sprintf(s, "VMI - [%s]", argv[argc-1]);  
    493 //#endif 
    494 //#ifdef HE // Hellinger 
    495 //    sprintf(s, "VHE - [%s]", argv[argc-1]);  
    496 //#endif 
    497 //#ifdef CS // Chi-Square 
    498 //    sprintf(s, "VCS - [%s]", argv[argc-1]);  
    499 //#endif 
    500 //     
    501 //    vmiWin = glutCreateWindow(s); 
    502 // 
    503 //    glewInit(); 
    504 // 
    505 //    init(); 
    506 // 
    507 //    if (bLoadCamerasFromFile == GL_FALSE) { 
    508 //        switch (cameraType) { 
    509 //        case 0: 
    510 //            cameras = setCameras(radius, OCTAHEDRON, &numCameras); 
    511 //            printf("Number of cameras: %d\n", OCTAHEDRON); 
    512 //            break; 
    513 //        case 1: 
    514 //            cameras = setCameras(radius, ICOSAHEDRON, &numCameras); 
    515 //            printf("Number of cameras: %d\n", ICOSAHEDRON); 
    516 //            break; 
    517 //        case 2: 
    518 //            cameras = setCameras(radius, DODECAHEDRON, &numCameras); 
    519 //            printf("Number of cameras: %d\n", DODECAHEDRON); 
    520 //            break; 
    521 //        default: 
    522 //            break; 
    523 //        } 
    524 //    } else { 
    525 //        sprintf(s,"%s.cam", filename); 
    526 //         
    527 //        cameras = loadCameras(radius, s, &numCameras); 
    528 //        //getchar(); 
    529 //    } 
    530 // 
    531 //              histogram = initHistogram(mesh->currentNumTriangles, numCameras); 
    532 // 
    533 //    initialIs = initIs(numCameras); 
    534 //     
    535 //              if (!bEnableOffScreen) { 
    536 //                        glutReshapeFunc(reshape); 
    537 //        glutKeyboardFunc(keyboard); 
    538 //                        glutDisplayFunc(display); 
    539 //              } 
    540 //              else { 
    541 //        display(); 
    542 //              freeMemory();            
    543 //    } 
    544 //    exit(0); 
    545 // 
    546 //    glutMainLoop(); 
    547 //    return 0; 
    548 //} 
     538/*int main(int argc, char** argv) 
     539{ 
     540    char s[MAX_CHAR]; 
     541 
     542    process_cmdline(argc, argv); 
     543     
     544    // Load a .obj model 
     545    pmodel = glmReadOBJ(argv[argc-1]); 
     546    if (!pmodel) exit(0); 
     547    glmUnitize(pmodel); 
     548    if (bRemoveRedundantVertices == GL_TRUE) 
     549        glmWeld(pmodel, 0.00001); 
     550    //glmFacetNormals(pmodel); 
     551    //glmVertexNormals(pmodel, 90.0); 
     552 
     553    mesh = initMesh(pmodel); 
     554    //printMesh(mesh); 
     555    //getchar(); 
     556 
     557    if ((numDemandedTriangles == 0) || (numDemandedTriangles >= mesh->numTriangles)) 
     558        usage_error("Illegal number of triangles."); 
     559 
     560    printf("w: %d h: %d\n", width, height); 
     561    printf("t: %d c: %d o: %d r: %f\n", numDemandedTriangles, cameraType, bEnableOffScreen, radius); 
     562    //getchar(); 
     563 
     564    // Get a filename without extension 
     565    strncpy(filename, argv[argc - 1], strlen(argv[argc - 1]) - 4); 
     566     
     567    glutInit(&argc, argv); 
     568    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA); // RGB and Alpha 
     569    glutInitWindowSize(width, height); 
     570    glutInitWindowPosition(100, 100); 
     571 
     572    sprintf(s, "%sW - [%s]", EXT, argv[argc-1]); 
     573     
     574    glutCreateWindow(s); 
     575 
     576    glewInit(); 
     577 
     578    init(); 
     579 
     580    if (bLoadCamerasFromFile == GL_FALSE) 
     581        cameras = setCameras(radius, cameraType, &numCameras); 
     582    else { 
     583        sprintf(s,"%s.cam", filename); 
     584         
     585        cameras = loadCameras(radius, s, &numCameras); 
     586        //getchar(); 
     587    } 
     588 
     589    histogram = initHistogram(mesh->numTriangles, numCameras); 
     590 
     591    initialIs = initIs(numCameras); 
     592     
     593    // Allocate memory for colors 
     594    colors = initColors(mesh->numTriangles); 
     595 
     596    // Set a different color for every triangle  
     597    setColors4(colors, mesh->numTriangles); 
     598 
     599    if (!bEnableOffScreen){ 
     600        glutReshapeFunc(reshape); 
     601        glutKeyboardFunc(keyboard); 
     602        glutDisplayFunc(display); 
     603 
     604        glutMainLoop(); 
     605    } else display(); 
     606 
     607    return 0; 
     608}*/ 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/mesh.cpp

    r983 r2090  
    1111using namespace VMI; 
    1212 
    13 #define MAX_NUM_TRI 450 
    14  
     13#define OUTPUT_SEVERAL_GROUPS 
     14 
     15/////////////////////////////////////////////////////////////////////////////// 
     16// list of integers 
    1517void VMI::printItemList(int *list, int n) { 
    1618    int i; 
     
    3537        list[(*n)++] = item; 
    3638} 
     39 
     40void VMI::delItem(int *list, int *n, int item) { 
     41    int i, j; 
     42     
     43    for (i=0; i<*n; i++) { 
     44        if (list[i] == item) { 
     45            // delete it 
     46            for (j =i + 1 ; j<*n; j++) 
     47                list[j - 1] = list[j]; 
     48            (*n)--; 
     49            i--; // Delete all ocurrencies of an item 
     50        } 
     51    } 
     52} 
     53/////////////////////////////////////////////////////////////////////////////// 
    3754 
    3855int VMI::findEdge(Edge *e, GLuint num, GLuint _u, GLuint _v) { 
     
    5673 
    5774    return found; 
     75} 
     76 
     77// Adds a new edge at the end of the mesh edge list 
     78Edge *VMI::addEdge(Edge *list, int *n, int u, int v, int *pos) { 
     79          Edge *newList; 
     80 
     81                // Reallocate memory for the new edge 
     82    newList = (Edge *)realloc(list, ((*n) + 1) * sizeof(Edge)); 
     83 
     84                // New vertex indices 
     85                newList[*n].u = u; 
     86    newList[*n].v = v; 
     87 
     88                // This edge has no triangle adjancency 
     89                newList[*n].numTriangles = 0; 
     90                newList[*n].triangles = NULL; 
     91                 
     92                // Enable the new edge 
     93                newList[*n].enable = TRUE; 
     94 
     95                // New edge position 
     96                *pos = *n; 
     97 
     98                // New number of edges 
     99          (*n)++; 
     100 
     101                return newList; 
    58102} 
    59103 
     
    131175Mesh *VMI::initMesh(GLMmodel* pmodel) { 
    132176 
    133     GLuint i, j, v1, v2, v3, n, e, t; 
     177    GLuint i, j, curGroup, v1, v2, v3, n, m, t; 
     178    int e; 
    134179    Mesh *mesh; 
     180    GLMgroup *group; 
    135181 
    136182    mesh = (Mesh *)malloc (sizeof(Mesh)); 
     
    157203    printf("Adding vertices..."); 
    158204    for (i=1; i<=pmodel->numvertices; i++) { // Vertices start at 1 
    159  
    160205        mesh->vertices[i - 1].x = pmodel->vertices[3 * i + 0]; 
    161206        mesh->vertices[i - 1].y = pmodel->vertices[3 * i + 1]; 
     
    170215 
    171216    printf("Adding triangles..."); 
     217 
     218#ifdef OUTPUT_SEVERAL_GROUPS 
     219    group = pmodel->groups; 
     220    curGroup=0; // current group 
     221    while(group) { 
     222         
     223        for (i=0; i<group->numtriangles; i++) { 
     224             
     225            t = group->triangles[i]; 
     226 
     227            mesh->triangles[t].id = t; 
     228            mesh->triangles[t].group = curGroup; // set group 
     229            mesh->triangles[t].indices[0] = pmodel->triangles[t].vindices[0] - 1; 
     230            mesh->triangles[t].indices[1] = pmodel->triangles[t].vindices[1] - 1; 
     231            mesh->triangles[t].indices[2] = pmodel->triangles[t].vindices[2] - 1; 
     232            mesh->triangles[t].area = computeTriangleArea(mesh->vertices, &mesh->triangles[t]); 
     233            //printf("\n%d a: %f",i , mesh->triangles[i].area); 
     234            computeTriangleNormal(mesh->vertices, &mesh->triangles[t]); 
     235             
     236            mesh->triangles[t].saliency = 0.0; 
     237             
     238            mesh->triangles[t].enable = GL_TRUE; 
     239             
     240            for (j=0; j<3; j++) { 
     241                // Adding triangle i adjacent to 3 vertices 
     242                v1 = mesh->triangles[t].indices[j]; 
     243                 
     244                // Reallocate memory for the new adjacent triangle 
     245                mesh->vertices[v1].triangles =  
     246                                        (GLuint *)realloc(mesh->vertices[v1].triangles, (mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); 
     247                addItem((int *)mesh->vertices[v1].triangles, (int *)&mesh->vertices[v1].numTriangles, t); 
     248            } 
     249        } 
     250        curGroup++; 
     251        group = group->next; 
     252    } 
     253#else // One single group 
    172254    for (i=0; i<pmodel->numtriangles; i++) { 
    173255        mesh->triangles[i].id = i; 
     
    188270    
    189271            // Reallocate memory for the new adjacent triangle 
    190             mesh->vertices[v1].triangles = (GLuint *)realloc(mesh->vertices[v1].triangles, (mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); 
     272            mesh->vertices[v1].triangles =  
     273                            (GLuint *)realloc(mesh->vertices[v1].triangles, (mesh->vertices[v1].numTriangles + 1) * sizeof(GLuint)); 
    191274            addItem((int *)mesh->vertices[v1].triangles, (int *)&mesh->vertices[v1].numTriangles, i); 
    192275        } 
    193276    } 
    194     //printf("\n"); 
     277    printf("\n"); 
     278#endif 
    195279    printf("Ok\n"); 
    196280 
    197281    mesh->numTriangles = pmodel->numtriangles; 
    198282    mesh->currentNumTriangles = pmodel->numtriangles; 
     283    printf("Number of triangles: %d\n", mesh->numTriangles); 
    199284 
    200285    mesh->edges = (Edge *)malloc (sizeof(Edge) * mesh->numTriangles * 3);  // E = 3 T / 2 
     
    205290    } 
    206291     
    207     // Init edges 
    208     for (i=0; i<mesh->numTriangles * 3; i++) { 
    209          
    210         mesh->edges[i].triangles    = NULL; 
    211         mesh->edges[i].numTriangles = 0; 
    212     } 
    213  
    214292    printf("Adding edges..."); 
    215293    n = 0; 
     
    221299        v3 = mesh->triangles[i].indices[2]; 
    222300         
     301        ///////////////////////////////////////////////////////////////////////////////// 
     302                // edge (v1,v2) 
    223303        if ((e = findEdge(mesh->edges, n, v1, v2)) == -1) { 
    224304            mesh->edges[n].u = v1; 
    225305            mesh->edges[n].v = v2; 
     306                                                mesh->edges[n].triangles    = NULL; 
     307                        mesh->edges[n].numTriangles = 0; 
    226308            mesh->edges[n].enable = GL_TRUE; 
    227              
    228             // Reallocate memory for the new adjacent triangle 
    229             mesh->edges[n].triangles = (GLuint *)realloc(mesh->edges[n].triangles, (mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
    230             // Adding triangle i adjacent to edge n 
    231             addItem((int *)mesh->edges[n].triangles, (int *)&mesh->edges[n].numTriangles, i); 
    232             //printf("n:%d i:%d\n", n, i); 
    233              
    234             // Adding edge n adjacent to triangle i 
    235             addItem((int *)mesh->triangles[i].edges, (int *)&t, n); 
     309            m = n; 
    236310            n++; 
    237         } else { 
    238             // Reallocate memory for the new adjacent triangle 
    239             mesh->edges[e].triangles = (GLuint *)realloc(mesh->edges[e].triangles, (mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
    240             // Adding triangle i adjacent to edge e 
    241             addItem((int *)mesh->edges[e].triangles, (int *)&mesh->edges[e].numTriangles, i); 
    242             //printf("n:%d i:%d\n", e, i); 
    243              
    244             // Adding edge e adjacent to triangle i 
    245             addItem((int *)mesh->triangles[i].edges, (int *)&t, e); 
    246         } 
     311        } else m = e; 
     312                // Reallocate memory for the new adjacent triangle 
     313        mesh->edges[m].triangles =  
     314                        (GLuint *)realloc(mesh->edges[m].triangles, (mesh->edges[m].numTriangles + 1) * sizeof(GLuint)); 
     315        // Adding triangle i adjacent to edge m 
     316        addItem((int *)mesh->edges[m].triangles, (int *)&mesh->edges[m].numTriangles, i); 
     317        //printf("n:%d i:%d\n", m, i); 
     318         
     319        // Adding edge m adjacent to triangle i 
     320        addItem((int *)mesh->triangles[i].edges, (int *)&t, m); 
     321 
     322        ///////////////////////////////////////////////////////////////////////////////// 
     323        // edge (v2,v3) 
    247324        if (( e = findEdge(mesh->edges, n, v2, v3)) == -1) { 
    248325            mesh->edges[n].u = v2; 
    249326            mesh->edges[n].v = v3; 
     327                                                mesh->edges[n].triangles    = NULL; 
     328                        mesh->edges[n].numTriangles = 0; 
    250329            mesh->edges[n].enable = GL_TRUE; 
    251              
    252             // Reallocate memory for the new adjacent triangle 
    253             mesh->edges[n].triangles = (GLuint *)realloc(mesh->edges[n].triangles, (mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
    254             // Adding triangle i adjacent to edge n 
    255             addItem((int *)mesh->edges[n].triangles, (int *)&mesh->edges[n].numTriangles, i); 
    256             //printf("n:%d i:%d\n", n, i); 
    257              
    258             // Adding edge n adjacent to triangle i 
    259             addItem((int *)mesh->triangles[i].edges, (int *)&t, n); 
     330            m = n; 
    260331            n++; 
    261         } else { 
    262             // Reallocate memory for the new adjacent triangle 
    263             mesh->edges[e].triangles = (GLuint *)realloc(mesh->edges[e].triangles, (mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
    264             // Adding triangle i adjacent to edge e 
    265             addItem((int *)mesh->edges[e].triangles, (int *)&mesh->edges[e].numTriangles, i); 
    266             //printf("n:%d i:%d\n", e, i); 
    267              
    268             // Adding edge e adjacent to triangle i 
    269             addItem((int *)mesh->triangles[i].edges, (int *)&t, e); 
    270         } 
     332        } else m = e; 
     333                // Reallocate memory for the new adjacent triangle 
     334        mesh->edges[m].triangles =  
     335                        (GLuint *)realloc(mesh->edges[m].triangles, (mesh->edges[m].numTriangles + 1) * sizeof(GLuint)); 
     336        // Adding triangle i adjacent to edge m 
     337        addItem((int *)mesh->edges[m].triangles, (int *)&mesh->edges[m].numTriangles, i); 
     338        //printf("n:%d i:%d\n", m, i); 
     339         
     340        // Adding edge m adjacent to triangle i 
     341        addItem((int *)mesh->triangles[i].edges, (int *)&t, m); 
     342 
     343        ///////////////////////////////////////////////////////////////////////////////// 
     344        // edge (v3,v1) 
    271345        if ((e = findEdge(mesh->edges, n, v3, v1)) == -1) { 
    272346            mesh->edges[n].u = v3; 
    273347            mesh->edges[n].v = v1; 
     348                                                mesh->edges[n].triangles    = NULL; 
     349                        mesh->edges[n].numTriangles = 0; 
    274350            mesh->edges[n].enable = GL_TRUE; 
    275              
    276             // Reallocate memory for the new adjacent triangle 
    277             mesh->edges[n].triangles = (GLuint *)realloc(mesh->edges[n].triangles, (mesh->edges[n].numTriangles + 1) * sizeof(GLuint)); 
    278             // Adding triangle i adjacent to edge n 
    279             addItem((int *)mesh->edges[n].triangles, (int *)&mesh->edges[n].numTriangles, i); 
    280             //printf("n:%d i:%d\n", n, i); 
    281              
    282             // Adding edge n adjacent to triangle i 
    283             addItem((int *)mesh->triangles[i].edges, (int *)&t, n); 
     351            m = n; 
    284352            n++; 
    285         } else  { 
    286             // Reallocate memory for the new adjacent triangle 
    287             mesh->edges[e].triangles = (GLuint *)realloc(mesh->edges[e].triangles, (mesh->edges[e].numTriangles + 1) * sizeof(GLuint)); 
    288             // Adding triangle i adjacent to edge e 
    289             addItem((int *)mesh->edges[e].triangles, (int *)&mesh->edges[e].numTriangles, i); 
    290             //printf("n:%d i:%d\n", e, i); 
    291              
    292             // Adding edge e adjacent to triangle i 
    293             addItem((int *)mesh->triangles[i].edges, (int *)&t, e); 
    294         } 
     353        } else m = e; 
     354                // Reallocate memory for the new adjacent triangle 
     355        mesh->edges[m].triangles =  
     356                        (GLuint *)realloc(mesh->edges[m].triangles, (mesh->edges[m].numTriangles + 1) * sizeof(GLuint)); 
     357        // Adding triangle i adjacent to edge m 
     358        addItem((int *)mesh->edges[m].triangles, (int *)&mesh->edges[m].numTriangles, i); 
     359        //printf("n:%d i:%d\n", m, i); 
     360         
     361        // Adding edge m adjacent to triangle i 
     362        addItem((int *)mesh->triangles[i].edges, (int *)&t, m); 
    295363    } 
    296364    printf("Ok\n"); 
     
    322390    printf("------------------------\n"); 
    323391    for (i=0; i<mesh->numEdges; i++) { 
    324         printf("e%d (%d,%d)\n", i, mesh->edges[i].u, mesh->edges[i].v); 
     392        printf("e%d (%d,%d) %d\n", i, mesh->edges[i].u, mesh->edges[i].v, mesh->edges[i].enable); 
    325393        printf("  t(%d)[", mesh->edges[i].numTriangles); 
    326394        if (mesh->edges[i].triangles != NULL) { 
     
    329397                printf(",%d", mesh->edges[i].triangles[j]); 
    330398            } 
    331         } 
     399                                } 
    332400        printf("]\n"); 
    333401    } 
     
    336404    printf("------------------------\n"); 
    337405    for (i=0; i<mesh->numTriangles; i++) { 
    338         printf("t%d (%d,%d,%d)\n", i, mesh->triangles[i].indices[0], mesh->triangles[i].indices[1], 
    339                                      mesh->triangles[i].indices[2]); 
     406        printf("t%d (%d,%d,%d) %d\n", i, mesh->triangles[i].indices[0], mesh->triangles[i].indices[1], 
     407                                     mesh->triangles[i].indices[2],  mesh->triangles[i].enable); 
    340408        printf("  e(3)["); 
    341409        printf("%d", mesh->triangles[i].edges[0]); 
     
    385453 
    386454int *VMI::trianglesAdjToEdge(Mesh *mesh, int e, int *n) { 
    387     int triangles[MAX_NUM_TRI]; 
    388     int i, n0, n1, n2,  
     455    int i, n0, n1, n2, num = 0, t,  
    389456        u = mesh->edges[e].u,  
    390457        v = mesh->edges[e].v; 
    391     int *list, num = 0; 
    392  
    393     for (i=0; i<(int)mesh->numTriangles; i++) { 
    394          
    395         if (mesh->triangles[i].enable == TRUE) { 
    396              
    397             n0 = mesh->triangles[i].indices[0]; 
    398             n1 = mesh->triangles[i].indices[1]; 
    399             n2 = mesh->triangles[i].indices[2]; 
     458    int *triangles = NULL; 
     459 
     460        if ((mesh->vertices[u].numTriangles +  
     461         mesh->vertices[v].numTriangles) > 0) 
     462                 triangles = (int *)malloc((mesh->vertices[u].numTriangles +  
     463                                    mesh->vertices[v].numTriangles) * sizeof(int)); 
     464 
     465    for (i=0; i<(int)mesh->vertices[u].numTriangles; i++) { 
     466 
     467        t = mesh->vertices[u].triangles[i]; 
     468         
     469        if (mesh->triangles[t].enable == TRUE) { 
     470             
     471            n0 = mesh->triangles[t].indices[0]; 
     472            n1 = mesh->triangles[t].indices[1]; 
     473            n2 = mesh->triangles[t].indices[2]; 
    400474             
    401475            if ((n0 == v) || (n1 == v) || (n2 == v) || 
    402476                (n0 == u) || (n1 == u) || (n2 == u)) 
    403477                 
    404                 triangles[num++] = i; 
    405         } 
    406     } 
    407  
    408     list = (int*) malloc (sizeof(int) * num); 
    409     if (NULL == list) { 
    410         fprintf (stderr, "no more memory for list"); 
    411         exit(1); 
    412     } 
    413  
    414     memcpy(list, triangles, sizeof(int) * num); 
    415  
     478                addItem(triangles, &num, t); 
     479        } 
     480    } 
     481 
     482    for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) { 
     483 
     484        t = mesh->vertices[v].triangles[i]; 
     485         
     486        if (mesh->triangles[t].enable == TRUE) { 
     487             
     488            n0 = mesh->triangles[t].indices[0]; 
     489            n1 = mesh->triangles[t].indices[1]; 
     490            n2 = mesh->triangles[t].indices[2]; 
     491             
     492            if ((n0 == v) || (n1 == v) || (n2 == v) || 
     493                (n0 == u) || (n1 == u) || (n2 == u)) 
     494                 
     495                addItem(triangles, &num, t); 
     496        } 
     497    } 
    416498    *n = num; 
    417499 
    418     return list; 
    419 } 
    420  
    421 int *VMI::trianglesAdjToVertex(Mesh *mesh, int v, int *n) { 
    422     int triangles[MAX_NUM_TRI]; 
    423     int i, n0, n1, n2; 
    424     int *list, num = 0; 
    425  
    426     for (i=0; i<(int)mesh->numTriangles; i++) { 
    427          
    428         if (mesh->triangles[i].enable == TRUE) { 
    429              
    430             n0 = mesh->triangles[i].indices[0]; 
    431             n1 = mesh->triangles[i].indices[1]; 
    432             n2 = mesh->triangles[i].indices[2]; 
    433              
    434             if ((n0 == v) || (n1 == v) || (n2 == v)) 
    435                  
    436                 triangles[num++] = i; 
    437         } 
    438     } 
    439  
    440     list = (int*) malloc (sizeof(int) * num); 
    441     if (NULL == list) { 
    442         fprintf (stderr, "no more memory for list"); 
    443         exit(1); 
    444     } 
    445  
    446     memcpy(list, triangles, sizeof(int) * num); 
    447  
    448     *n = num; 
    449  
    450     return list; 
     500    return triangles; 
    451501} 
    452502 
    453503int *VMI::verticesAdjToVertex(Mesh *mesh, int v, int *n) { 
    454     int numTr; 
    455     int *triangles = trianglesAdjToVertex(mesh, v, &numTr); 
    456     int *vertices = (int*) malloc (sizeof(int) * numTr * 2); 
     504    int *vertices = NULL; 
    457505    int i, t, n0, n1, n2; 
    458506    int num = 0; 
    459507 
    460     for (i=0; i<numTr; i++) { 
    461         t = triangles[i]; 
     508        if (mesh->vertices[v].numTriangles > 0) 
     509                vertices = (int*)malloc(sizeof(int) * mesh->vertices[v].numTriangles * 2); 
     510 
     511    for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) { 
     512        t = mesh->vertices[v].triangles[i]; 
    462513 
    463514        n0 = mesh->triangles[t].indices[0]; 
     
    469520        if (n2 != v) addItem(vertices, &num, n2); 
    470521    } 
    471  
    472522    *n = num; 
    473523 
    474     free(triangles); 
    475  
    476524    return vertices; 
    477525} 
    478526 
    479527int *VMI::edgesAdjToVertex(Mesh *mesh, int v, int *n) { 
    480     int numTr; 
    481     int *triangles = trianglesAdjToVertex(mesh, v, &numTr); 
    482     int *edges = (int*) malloc (sizeof(int) * numTr * 2); 
    483     int i, t, n0, n1, n2, e; 
     528    int *edges = NULL; 
     529    int i, t, e; 
     530#ifndef USE_EDGE_ADJACENCY 
     531    int n0, n1, n2; 
     532#else 
     533    int j; 
     534#endif 
    484535    int num = 0; 
    485536 
    486     for (i=0; i<numTr; i++) { 
    487         t = triangles[i]; 
    488  
     537        if (mesh->vertices[v].numTriangles > 0)  
     538                edges = (int*)malloc(sizeof(int) * mesh->vertices[v].numTriangles * 2); 
     539 
     540    for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) { 
     541 
     542        t = mesh->vertices[v].triangles[i]; 
     543 
     544#ifdef USE_EDGE_ADJACENCY 
     545        for (j=0; j<3; j++) { 
     546            e = mesh->triangles[t].edges[j]; 
     547             
     548            if ((int)mesh->edges[e].u == v || (int)mesh->edges[e].v == v) 
     549                 
     550                addItem(edges, &num, e); 
     551        } 
     552#else 
    489553        n0 = mesh->triangles[t].indices[0]; 
    490554        n1 = mesh->triangles[t].indices[1]; 
     
    498562         
    499563        if (((n0 == v) || (n2 == v)) && 
    500             ((e = findEdge(mesh->edges, mesh->numEdges, n0, n2)) != -1)) addItem(edges, &num, e);   
    501     } 
    502  
     564            ((e = findEdge(mesh->edges, mesh->numEdges, n0, n2)) != -1)) addItem(edges, &num, e); 
     565 
     566#endif 
     567    } 
    503568    *n = num; 
    504569 
    505     free(triangles); 
    506  
    507570    return edges; 
    508571} 
    509572 
    510573int *VMI::edgesAdjToVertices(Mesh *mesh, int *vertices, int numVertices, int *n) { 
    511     int edges[MAX_NUM_TRI*2]; 
    512     int i, j, v, num = 0, e; 
    513     int *list, numEdges; 
    514  
    515     for (i=0; i<numVertices; i++) { 
    516         v = vertices[i]; 
    517  
    518         list = edgesAdjToVertex(mesh, v, &numEdges); 
    519  
    520         for (j=0; j<numEdges; j++) { 
    521             e = list[j]; 
    522              
    523             addItem(edges, &num, e); 
    524         } 
     574    int *edges = NULL; 
     575    int i, j, num = 0; 
     576    int *list = NULL, numEdges; 
     577 
     578    if (numVertices > 0) { 
     579        // Add the first 
     580        list = edgesAdjToVertex(mesh, vertices[0], &numEdges); 
     581 
     582        // Allocate memory 
     583        edges = (int *)malloc(numEdges * sizeof(int)); 
     584 
     585        memcpy(edges, list, sizeof(int) * numEdges); 
    525586 
    526587        free(list); 
    527     } 
    528  
    529     list = (int*) malloc (sizeof(int) * num); 
    530     if (NULL == list) { 
    531         fprintf (stderr, "no more memory for list"); 
    532         exit(1); 
    533     } 
    534  
    535     memcpy(list, edges, sizeof(int) * num); 
    536  
     588 
     589        num = numEdges; 
     590 
     591        // Add the rest 
     592        for (i=1; i<numVertices; i++) { 
     593 
     594            list = edgesAdjToVertex(mesh, vertices[i], &numEdges); 
     595 
     596            // Reallocate memory 
     597                        if (numEdges > 0) { 
     598 
     599                edges = (int *)realloc(edges, (num + numEdges + 1) * sizeof(int)); 
     600                 
     601                for (j=0; j<numEdges; j++) 
     602                    addItem(edges, &num, list[j]); 
     603                        } 
     604            free(list); 
     605        } 
     606    } 
    537607    *n = num; 
    538608 
    539     return list; 
     609    return edges; 
    540610} 
    541611 
     
    563633} 
    564634 
     635// Adds a new vertex at the end of the mesh vertex list 
     636Vertex *VMI::addVertex(Vertex *list, int *n, float x, float y, float z, int *pos) { 
     637          Vertex *newList; 
     638 
     639                // Reallocate memory for the new vertex 
     640    newList = (Vertex *)realloc(list, ((*n) + 1) * sizeof(Vertex)); 
     641 
     642                // New Vertex coordinates 
     643                newList[*n].x = x; 
     644    newList[*n].y = y; 
     645    newList[*n].z = z; 
     646 
     647                // This vertex has no triangle adjancency 
     648                newList[*n].numTriangles = 0; 
     649                newList[*n].triangles = NULL; 
     650         
     651    // Enable the new vertex 
     652                newList[*n].enable = TRUE; 
     653 
     654                // New vertex position 
     655                *pos = *n; 
     656 
     657                // New number of vertices 
     658          (*n)++; 
     659                 
     660                return newList; 
     661} 
     662 
     663 
     664 
    565665void VMI::updateModel(GLMmodel* pmodel, Mesh *mesh, GLuint numVertices, GLuint numTriangles) { 
    566666    GLuint i , v1, v2 ,v3, numV = 1, numT = 0; 
     
    569669     
    570670    if (pmodel->vertices != NULL) free(pmodel->vertices); 
    571  
    572671    pmodel->vertices = (GLfloat *)calloc ((numVertices + 1) * 3,  sizeof(GLfloat)); 
    573      
    574672 
    575673    if (pmodel->triangles != NULL) free(pmodel->triangles); 
    576  
    577674    pmodel->triangles = (GLMtriangle *)malloc (numTriangles * sizeof(GLMtriangle)); 
    578675 
    579      
    580676    for (i=0; i<mesh->numTriangles; i++) { 
    581677        if (mesh->triangles[i].enable) { 
     
    630726 
    631727void VMI::saveModel(char *filename, GLMmodel* pmodel, Mesh *mesh) { 
    632     GLuint i; 
    633  
     728    GLuint i, j, g, numT = 0; 
     729    GLMgroup *group; 
     730 
     731#ifdef OUTPUT_SEVERAL_GROUPS 
     732    // The output model maintains groups 
     733 
     734    // Empty all groups 
     735    group = pmodel->groups; 
     736    while(group) { 
     737        // Free memory for triangles 
     738        free(group->triangles); 
     739 
     740        // Reset group 
     741        group->triangles = NULL; 
     742        group->numtriangles = 0; 
     743 
     744        group = group->next; 
     745    } 
     746    // Fill all groups 
     747    for (i=0; i<mesh->numTriangles; i++) { 
     748 
     749        if (mesh->triangles[i].enable) { 
     750            g = mesh->triangles[i].group; 
     751 
     752            // Get group 
     753            group = pmodel->groups; 
     754            for (j=0; j <g; j++) 
     755                group = group->next; 
     756 
     757            // Reallocate memory for triangles 
     758            group->triangles = (GLuint *)realloc(group->triangles, (group->numtriangles + 1) * sizeof(GLuint)); 
     759            // Add triangle into group 
     760            addItem((int *)group->triangles, (int *)&group->numtriangles, numT); 
     761 
     762            numT++; 
     763        } 
     764    } 
     765#else 
    634766    // The output model contains only one group 
    635767    pmodel->numgroups = 1; 
     
    646778    for (i=0; i<mesh->currentNumTriangles; i++) 
    647779        pmodel->groups->triangles[i] = i; 
     780#endif 
    648781 
    649782    updateModel(pmodel, mesh, mesh->currentNumTriangles * 3, mesh->currentNumTriangles); 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/metrics.cpp

    r983 r2090  
    1313using namespace VMI; 
    1414 
    15 GLuint VMI::computeBackgroundArea(Mesh *mesh, GLuint *histogram) { 
    16     GLuint i, area = 0; 
    17  
    18     for (i=0; i<mesh->numTriangles; i++) { // Triangles start at 1 
    19         if (mesh->triangles[i].enable == TRUE) { 
    20             area += histogram[i + 1]; 
    21         } 
    22     } 
    23  
    24     return ((GLuint)(width * height) - area); 
    25 } 
    26  
    2715GLdouble VMI::computeMeanProjAreaNoBG(GLuint **histogram, GLuint numCameras, int t) { 
    2816    GLuint i; 
     
    3725 
    3826GLdouble VMI::computeMeanProjArea(GLuint **histogram, GLuint numCameras, int t) { 
    39     GLuint i; 
    40     GLdouble mean_proj_area = 0.0, total_proj_area, resolution = width * height; 
    41  
    42     for (i=0; i<numCameras; i++) { 
     27        GLuint i; 
     28        GLdouble mean_proj_area = 0.0, total_proj_area, resolution = width * height; 
     29 
     30        for (i=0; i<numCameras; i++) { 
    4331                total_proj_area = resolution - histogram[i][0]; 
    4432 
    45         mean_proj_area += histogram[i][t] / total_proj_area; 
    46     } 
    47  
    48     return (mean_proj_area / (GLdouble)numCameras); 
    49 } 
     33                mean_proj_area += histogram[i][t] / total_proj_area; 
     34        } 
     35 
     36        return (mean_proj_area / (GLdouble)numCameras); 
     37} 
     38/////////////////////////////////////////////////////////////////////////////// 
    5039// Mutual Information 
    5140GLdouble VMI::computeMI(Mesh *mesh, GLuint **histogram, GLuint numCameras, GLuint cam) { 
     
    7665    return (I / numCameras); 
    7766} 
     67 
     68GLdouble VMI::decMI(GLdouble I, GLuint **histogram, GLuint numCameras, GLuint cam, Change *c) { 
     69    GLdouble newI = I * numCameras,  
     70             pov = 0.0, po; 
     71    GLsizei total_proj_area = width * height; 
     72    GLint i, t; 
     73 
     74    // decrease entropy of deleted triangles 
     75    for (i=0; i<c->numDel; i++) {  
     76         
     77        t = c->deleted[i].id; 
     78        // projected pixels of triangle t is at t + 1 position 
     79        pov = (GLdouble)histogram[cam][t + 1] / total_proj_area; 
     80         
     81        po = computeMeanProjAreaNoBG(histogram, numCameras, t + 1) / total_proj_area; 
     82         
     83        if ((pov > 0.0) && (po > 0.0)) 
     84            newI -= (pov * log2(pov / po)); 
     85    } 
     86 
     87    // decrease entropy of modified triangles 
     88    for (i=0; i<c->numMod; i++) {  
     89         
     90        t = c->modified[i].id; 
     91        // projected pixels of triangle t is at t + 1 position 
     92        pov = (GLdouble)histogram[cam][t + 1] / total_proj_area; 
     93         
     94        po = computeMeanProjAreaNoBG(histogram, numCameras, t + 1) / total_proj_area; 
     95         
     96        if ((pov > 0.0) && (po > 0.0)) 
     97            newI -= (pov * log2(pov / po)); 
     98    } 
     99 
     100    // take into account the background 
     101    pov = (GLdouble)histogram[cam][0] / total_proj_area; 
     102     
     103    po = computeMeanProjAreaNoBG(histogram, numCameras, 0) / total_proj_area; 
     104     
     105    if ((pov > 0.0) && (po > 0.0)) 
     106        newI -= (pov * log2(pov / po)); 
     107 
     108    return (newI / numCameras); 
     109} 
     110 
     111GLdouble VMI::incMI(GLdouble I, GLuint **histogram, GLuint numCameras, GLuint cam, Change *c) { 
     112    GLdouble newI = I * numCameras,  
     113             pov = 0.0, po; 
     114    GLsizei total_proj_area = width * height; 
     115    GLuint i, t; 
     116 
     117    // increase entropy of modified triangles 
     118    for (i=0; i<(GLuint)c->numMod; i++) {  
     119         
     120        t = c->modified[i].id; 
     121        // projected pixels of triangle t is at t + 1 position 
     122        pov = (GLdouble)histogram[cam][t + 1] / total_proj_area; 
     123         
     124        po = computeMeanProjAreaNoBG(histogram, numCameras, t + 1) / total_proj_area; 
     125         
     126        if ((pov > 0.0) && (po > 0.0)) 
     127            newI += (pov * log2(pov / po)); 
     128    } 
     129 
     130    // take into account the background 
     131    pov = (GLdouble)histogram[cam][0] / total_proj_area; 
     132     
     133    po = computeMeanProjAreaNoBG(histogram, numCameras, 0) / total_proj_area; 
     134     
     135    if ((pov > 0.0) && (po > 0.0)) 
     136        newI += (pov * log2(pov / po)); 
     137 
     138    return (newI / numCameras); 
     139} 
     140/////////////////////////////////////////////////////////////////////////////// 
    78141// Kullback-Leibler divergence 
    79142GLdouble VMI::computeKL(Mesh *mesh, GLuint *histogram) { 
     
    107170// Hellinger divergence (square root) 
    108171GLdouble VMI::computeHE(Mesh *mesh, GLuint *histogram) { 
    109     GLdouble I = 0.0, t,  
     172    GLdouble I = 0.0, temp,  
    110173             total_real_area = 0.0, pi, qi; 
    111174    GLsizei  total_proj_area = (width * height) - histogram[0]; 
     
    128191            if ((pi > 0.0) && (qi > 0.0)) { 
    129192                 
    130                 t = sqrt(pi) - sqrt(qi); 
     193                temp = sqrt(pi) - sqrt(qi); 
    131194                 
    132                 I += (t * t); 
     195                I += (temp * temp); 
    133196            } 
    134197        } 
    135198    } 
    136199 
    137     return (sqrt(I / 2.0)); 
     200    return sqrt(I / 2.0); 
     201 
    138202} 
    139203 
     
    168232/////////////////////////////////////////////////////////////////////////////// 
    169233GLdouble VMI::computeEntropy(GLuint **histogram, GLuint numCameras, GLuint k) { 
    170     GLdouble H = 0.0, POk, pi, 
    171                      total_proj_area, resolution = width * height, pv = 1.0 / (GLdouble)numCameras; 
     234        GLdouble H = 0.0, POk, pi, 
     235                                         total_proj_area, resolution = width * height, pv = 1.0 / (GLdouble)numCameras; 
     236        GLuint i; 
     237 
     238        for (i=0; i<numCameras; i++) { 
     239 
     240                total_proj_area = resolution - histogram[i][0]; 
     241 
     242                POk = computeMeanProjArea(histogram, numCameras, k + 1); 
     243 
     244                if (POk > 0.0) 
     245                        pi = (pv * ((double)histogram[i][k + 1]) / total_proj_area ) / POk; 
     246                else pi = 0.0; 
     247 
     248                if (pi > 0.0) 
     249                        H += (pi * log2(pi)); 
     250        } 
     251 
     252        return -H; 
     253} 
     254 
     255GLdouble VMI::computeMixedEntropy(GLdouble *mixed, GLuint numCameras) { 
     256    GLdouble H = 0.0, pi; 
    172257    GLuint i; 
    173258     
    174259    for (i=0; i<numCameras; i++) { 
    175  
    176                 total_proj_area = resolution - histogram[i][0]; 
    177          
    178         POk = computeMeanProjArea(histogram, numCameras, k + 1); 
    179          
    180         if (POk > 0.0) 
    181             pi = (pv * ((double)histogram[i][k + 1]) / total_proj_area ) / POk; 
    182         else pi = 0.0; 
     260         
     261        pi = mixed[i]; 
    183262         
    184263        if (pi > 0.0) 
     
    188267    return -H; 
    189268} 
    190  
    191 GLdouble VMI::computeMixedEntropy(GLdouble *mixed, GLuint numCameras) { 
    192     GLdouble H = 0.0, pi; 
    193     GLuint i; 
    194      
    195     for (i=0; i<numCameras; i++) { 
    196          
    197         pi = mixed[i]; 
    198          
    199         if (pi > 0.0) 
    200             H += (pi * log2(pi)); 
    201     } 
    202      
    203     return -H; 
    204 } 
    205269/////////////////////////////////////////////////////////////////////////////// 
    206270// Jensen-Shannon divergence 
    207271GLdouble VMI::computeJS(GLuint **histogram, GLuint numCameras, GLuint j, GLuint k) { 
    208     GLdouble js, Wj = 0.0, Wk = 0.0, POj, POk, pj, pk, total_proj_area, 
    209                     resolution = width * height, pv = 1.0 / (GLdouble)numCameras, 
    210                         *mixing = (GLdouble *)malloc(numCameras * sizeof(GLdouble)); 
    211     GLuint i; 
    212      
    213     POj = computeMeanProjArea(histogram, numCameras, j + 1); 
    214     POk = computeMeanProjArea(histogram, numCameras, k + 1); 
    215  
    216     //printf("%f %f\n", POj, POk); 
    217      
    218     // weights 
    219     if ((POj + POk) > 0.0) { 
    220         Wj = POj / (POj + POk); 
    221         Wk = POk / (POj + POk); 
    222     } 
    223  
    224     //printf("%f %f\n", Wj, Wk); 
    225      
    226     // Compute mixing distribution 
    227     for (i=0; i<numCameras; i++) { 
    228          
     272        GLdouble js, Wj = 0.0, Wk = 0.0, POj, POk, pj, pk, total_proj_area, 
     273                                        resolution = width * height, pv = 1.0 / (GLdouble)numCameras, 
     274                                        *mixing = (GLdouble *)malloc(numCameras * sizeof(GLdouble)); 
     275        GLuint i; 
     276 
     277        POj = computeMeanProjArea(histogram, numCameras, j + 1); 
     278        POk = computeMeanProjArea(histogram, numCameras, k + 1); 
     279 
     280        //printf("%f %f\n", POj, POk); 
     281 
     282        // weights 
     283        if ((POj + POk) > 0.0) { 
     284                Wj = POj / (POj + POk); 
     285                Wk = POk / (POj + POk); 
     286        } 
     287 
     288        //printf("%f %f\n", Wj, Wk); 
     289 
     290        // Compute mixing distribution 
     291        for (i=0; i<numCameras; i++) { 
     292 
    229293                total_proj_area = resolution - histogram[i][0]; 
    230294 
    231         if (POj > 0.0) pj = (pv * ((double)histogram[i][j + 1] / total_proj_area)) / POj; 
    232         else pj = 0.0; 
    233              
    234         if (POk > 0.0) pk = (pv * ((double)histogram[i][k + 1] / total_proj_area)) / POk; 
    235         else pk = 0.0; 
    236              
    237         mixing[i] = (Wj * pj) + (Wk * pk); 
    238     } 
    239      
    240     js = /*(POj + POk) * */(computeMixedEntropy(mixing, numCameras)  
    241                 - (Wj * computeEntropy(histogram, numCameras, j))  
    242                 - (Wk * computeEntropy(histogram, numCameras, k))); 
     295                if (POj > 0.0) pj = (pv * ((double)histogram[i][j + 1] / total_proj_area)) / POj; 
     296                else pj = 0.0; 
     297 
     298                if (POk > 0.0) pk = (pv * ((double)histogram[i][k + 1] / total_proj_area)) / POk; 
     299                else pk = 0.0; 
     300 
     301                mixing[i] = (Wj * pj) + (Wk * pk); 
     302        } 
     303 
     304        js = /*(POj + POk) * */(computeMixedEntropy(mixing, numCameras)  
     305                        - (Wj * computeEntropy(histogram, numCameras, j))  
     306                        - (Wk * computeEntropy(histogram, numCameras, k))); 
    243307 
    244308        free(mixing); 
    245309 
    246     return js; 
     310        return js; 
    247311} 
    248312/////////////////////////////////////////////////////////////////////////////// 
     
    250314 
    251315    renderScene(histogram, numCameras); 
     316} 
     317 
     318void VMI::getProjectedAreasWin(GLuint **histogram, GLuint numCameras, Change *c) { 
     319 
     320    renderSceneWin(histogram, numCameras, c); 
    252321} 
    253322 
     
    301370    meanI /= numCameras; 
    302371#endif 
    303     printIs(mis, numCameras); 
     372    //printIs(mis, numCameras); 
    304373    printf("I0= %f\n", meanI);  
    305374} 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/saliency.cpp

    r983 r2090  
    2323 
    2424void VMI::computeSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras) { 
    25     GLuint i = 0; 
    26     double sal; 
    27      
    28     maxSal = minSal = computeTriangleSaliency(mesh, histogram, numCameras, 0); 
    29  
    30     mesh->triangles[0].saliency = maxSal; 
    31  
    32     for (i=1; i<mesh->numTriangles; i++) { 
    33  
    34         sal = computeTriangleSaliency(mesh, histogram, numCameras, i); 
    35  
    36         if (sal > maxSal) maxSal = sal; 
    37         if (sal < minSal) minSal = sal; 
    38          
    39         mesh->triangles[i].saliency = sal; 
    40     } 
    41  
    42     printf("\nMax Sal: %f  Min Sal: %f\n", maxSal, minSal); 
    43  
    44     percentile = computePercentile(alpha, mesh->triangles, mesh->numTriangles); 
    45     printf("\n%dth percentile: %f\n", alpha, percentile); 
     25        GLuint i = 0; 
     26        double sal; 
     27 
     28        maxSal = minSal = computeTriangleSaliency(mesh, histogram, numCameras, 0); 
     29 
     30        mesh->triangles[0].saliency = maxSal; 
     31 
     32        for (i=1; i<mesh->numTriangles; i++) { 
     33 
     34                sal = computeTriangleSaliency(mesh, histogram, numCameras, i); 
     35 
     36                if (sal > maxSal) maxSal = sal; 
     37                if (sal < minSal) minSal = sal; 
     38 
     39                mesh->triangles[i].saliency = sal; 
     40        } 
     41 
     42        printf("\nMax Sal: %f  Min Sal: %f\n", maxSal, minSal); 
     43 
     44        percentile = computePercentile(alpha, mesh->triangles, mesh->numTriangles); 
     45        printf("\n%dth percentile: %f\n", alpha, percentile); 
     46} 
     47 
     48void updateTriangleSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras, int v) { 
     49        int i, t; 
     50 
     51        for(i=0; i<(int)mesh->vertices[v].numTriangles; i++) { 
     52 
     53                t = mesh->vertices[v].triangles[i]; 
     54 
     55                mesh->triangles[t].saliency = computeTriangleSaliency(mesh, histogram, numCameras, t); 
     56        } 
    4657} 
    4758 
    4859double VMI::computeTriangleSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras, GLuint k) { 
    49     GLuint i, l, v0, v1, v2; 
    50     int triangles[MAX_NUM_TRI], n = 0; 
     60     GLuint i, l, v0, v1, v2; 
     61    int *triangles = NULL, n = 0; 
    5162    double sal = 0.0; 
    5263     
     
    5566    v2 = mesh->triangles[k].indices[2]; 
    5667     
     68    /* Allocating memory */ 
     69    triangles = (int *)malloc((mesh->vertices[v0].numTriangles + 
     70                               mesh->vertices[v1].numTriangles + 
     71                               mesh->vertices[v2].numTriangles) * sizeof(int)); 
     72 
    5773    for(i=0; i<mesh->vertices[v0].numTriangles; i++) { 
    5874        l = mesh->vertices[v0].triangles[i]; 
     
    86102    //getchar(); 
    87103     
     104    free(triangles); 
     105 
    88106    return (sal /*/ n*/); 
    89107} 
     
    150168    if((fp= fopen(filename, "rt"))== NULL) { 
    151169        printf("Can't open file %s\n", filename); 
    152         exit(1); 
     170                                getchar(); 
     171                                exit(1); 
    153172    } 
    154173 
     
    219238void VMI::computeRGB(double min, double max,double value,float *r,float *g,float *b) 
    220239{ 
    221     //Dados el valor máximo, el mínimo y el valor que le quieres calcular el color te devuelve el respectivo RGB. 
    222     if(value>max) value=max; 
    223     if(value<min) value=min; 
    224      
    225     if(max==min) value=1; 
    226     else value=(float)(value-min)/(float)(max-min); 
    227      
    228     if (value<=0.25) 
    229     { 
    230         (*r)=0; 
    231         (*g)=4*value; 
    232         (*b)=1; 
    233     } 
    234     else if (value<=0.50) 
    235     { 
    236         (*r)=0; 
    237         (*g)=1; 
    238         (*b)=2-4*value; 
    239     } 
    240     else if (value<=0.75) 
    241     { 
    242         (*r)=4*value-2; 
    243         (*g)=1; 
    244         (*b)=0; 
    245     } 
    246     else 
    247     { 
    248         (*r)=1; 
    249         (*g)=4-4*value; 
    250         (*b)=0; 
    251     } 
     240        /* Given the maximum, the minimum value and the demanded color value returns 
     241         * the respective RGB color 
     242         */ 
     243        if(value>max) value=max; 
     244        if(value<min) value=min; 
     245 
     246        if(max==min) value=1; 
     247        else value=(float)(value-min)/(float)(max-min); 
     248 
     249        if (value<=0.25) 
     250        { 
     251                (*r)=0; 
     252                (*g)=4*value; 
     253                (*b)=1; 
     254        } 
     255        else if (value<=0.50) 
     256        { 
     257                (*r)=0; 
     258                (*g)=1; 
     259                (*b)=2-4*value; 
     260        } 
     261        else if (value<=0.75) 
     262        { 
     263                (*r)=4*value-2; 
     264                (*g)=1; 
     265                (*b)=0; 
     266        } 
     267        else 
     268        { 
     269                (*r)=1; 
     270                (*g)=4-4*value; 
     271                (*b)=0; 
     272        } 
    252273} 
    253274 
  • GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/simplify.cpp

    r1007 r2090  
    99#include "../include/simplify.h" 
    1010#include "../include/saliency.h" 
     11#include "../include/histogram.h" 
    1112 
    1213//#define RECOMPUTE_THE_WHOLE_HEAP 
    13 //#define MAKE_FULL_COPY 
    1414 
    1515using namespace VMI; 
     16using   namespace       std; 
     17 
     18//      Multimap to store current vertex in a simplification state. 
     19typedef multimap<_float3_,      int>    t_map; 
     20typedef t_map::iterator                                         t_map_str; 
     21typedef t_map::value_type                                       t_pair; 
     22t_map                                                                                                                   mVertexMap; 
     23multimap<int,int>                                                                       mVertices; 
     24 
     25vector<Geometry::Vector3>       VMI::vPositions; 
     26vector<Geometry::Vector3>       VMI::vNormals; 
     27vector<Geometry::Vector2>       VMI::vTexCoords; 
    1628 
    1729/////////////////////////////////////////////////////////////////////////////// 
    1830 
    1931void VMI::bh_mydump(Mesh *mesh, bheap_t *h) { 
    20     int i, d; 
    21  
    22     printf("Heap status.\n"); 
    23     for(i = 1; i <= h->n; i++) { 
    24         d = h->a[i].item; 
    25         printf ("Heap [%d] = pri %f, e%d(%d, %d)\n", i, 
    26                   h->a[i].key,d, 
    27                   mesh->edges[d].u, 
    28                   mesh->edges[d].v); 
    29     } 
    30  
    31     printf("\n"); 
    32      
    33     for(i = 2; i <= h->n; i++) { 
    34         if(h->a[i].key < h->a[i/2].key) { 
    35             printf("key error at entry %d, value %f\n", i, h->a[i].key); 
    36             exit(1); 
    37         } 
    38     } 
    39     for(i = 1; i <= h->n; i++) { 
    40         if(h->p[h->a[i].item] != i) { 
    41             printf("indexing error at entry %d", i); exit(1); 
    42         } 
    43     }     
     32        int i, d; 
     33 
     34        printf("Heap status.\n"); 
     35        for(i = 1; i <= h->n; i++) { 
     36                d = h->a[i].item; 
     37                printf ("Heap [%d] = pri %f, e%d(%d, %d) d:%d\n", 
     38                                i, 
     39                                h->a[i].key,d, 
     40                                mesh->edges[d].u, 
     41                                mesh->edges[d].v,  
     42                                h->a[i].dirty); 
     43        } 
     44        printf("\n"); 
     45 
     46        for(i = 2; i <= h->n; i++) { 
     47                if(h->a[i].key < h->a[i/2].key) { 
     48                        printf("key error at entry %d, value %f\n", i, h->a[i].key); 
     49                        exit(1); 
     50                } 
     51        } 
     52        for(i = 1; i <= h->n; i++) { 
     53                if(h->p[h->a[i].item] != i) { 
     54                        printf("indexing error at entry %d", i); 
     55                        exit(1); 
     56                } 
     57        }     
    4458} 
    4559 
    4660bheap_t *VMI::initHeap(Mesh *mesh) { 
    47     GLuint i; 
    48     double cost; 
    49     bheap_t *h = NULL; 
    50 #ifdef MAKE_FULL_COPY 
    51     Triangle *triangleCopy = NULL; 
    52     GLuint lastNumTriangles = mesh->numTriangles; 
    53  
    54     // Allocate memory 
    55     triangleCopy = (Triangle *)malloc(lastNumTriangles * sizeof(Triangle));   
    56     if (triangleCopy == NULL) { 
    57         fprintf(stderr, "Error allocating memory\n"); 
    58         exit(1); 
    59     } 
    60     // Make a copy of current triangle list 
    61     memcpy(triangleCopy, mesh->triangles, lastNumTriangles * sizeof(Triangle)); 
    62 #endif 
    63      
    64     printf("Creating a heap for simplification..."); 
    65      
    66     h = bh_alloc(mesh->numEdges); 
    67  
    68                 //      Init percent update count. 
    69                 float   percent =       60.0 / mesh->numEdges; 
    70  
    71     for (i = 0; i <mesh->numEdges; i++) { 
    72  
    73                                 //      Update progress bar. 
    74                                 mUPB(percent); 
    75  
    76         if (mesh->edges[i].enable == TRUE) { 
    77  
    78             cost = computeEdgeCost(mesh, i); 
    79          
    80             // Add only valid edges 
    81             if (cost != FLT_MAX) 
    82                 bh_insert(h, i, cost); 
    83  
    84 #ifdef MAKE_FULL_COPY 
    85             // Restore initial triangle list 
    86             memcpy(mesh->triangles, triangleCopy, lastNumTriangles * sizeof(Triangle)); 
    87             mesh->numTriangles = lastNumTriangles; 
    88 #endif 
    89         } 
    90     } 
    91      
    92 #ifdef MAKE_FULL_COPY 
    93     // Free memory 
    94     if (triangleCopy != NULL) free(triangleCopy); 
    95 #endif 
    96     printf("Ok\n"); 
     61        GLuint i; 
     62        double cost; 
     63        bheap_t *h = NULL; 
     64 
     65        printf("Creating a heap for simplification..."); 
     66 
     67        h = bh_alloc(mesh->numEdges); 
     68 
     69        //      Init percent update count. 
     70        float   percent =       60.0 / mesh->numEdges; 
     71 
     72        for (i = 0; i <mesh->numEdges; i++) { 
     73 
     74                //      Update progress bar. 
     75                mUPB(percent); 
     76 
     77                if (mesh->edges[i].enable == TRUE) { 
     78 
     79                        cost = computeEdgeCost(mesh, i); 
     80 
     81                        // Add only valid edges 
     82                        if (cost != FLT_MAX) { 
     83                                bh_insert(h, i, cost); 
     84                                /* set dirty flag to FALSE */ 
     85                                bh_mark(h, i, FALSE); 
     86                        } 
     87                } 
     88        } 
     89 
     90        printf("Ok\n"); 
    9791 
    9892#ifdef DEBUG  
    99     bh_mydump(mesh, h); 
    100     //getchar(); 
    101 #endif 
    102  
    103     return h; 
     93        bh_mydump(mesh, h); 
     94        //getchar(); 
     95#endif 
     96 
     97        return h; 
    10498} 
    10599 
    106100bheap_t *VMI::updateHeap(bheap_t *h, Mesh *mesh, Change *c) { 
    107     int e, i, n0, n1, n2, u, v; 
     101        int e, i; 
    108102#ifdef RECOMPUTE_THE_WHOLE_HEAP 
    109     bheap_t *nh; 
     103        bheap_t *nh; 
     104        double cost; 
    110105#else 
    111     int *lv, *le, numV, numE; 
    112 #endif 
    113     double cost; 
    114 #ifdef MAKE_FULL_COPY 
    115     GLuint lastNumTriangles = mesh->numTriangles; 
    116     Triangle *triangleCopy = NULL; 
    117      
    118     // Allocate memory 
    119     triangleCopy = (Triangle *)malloc(lastNumTriangles * sizeof(Triangle));   
    120     if (triangleCopy == NULL) { 
    121         fprintf(stderr, "Error allocating memory\n"); 
    122         exit(1); 
    123     } 
    124     // Make a copy of current triangle list 
    125     memcpy(triangleCopy, mesh->triangles, lastNumTriangles * sizeof(Triangle)); 
    126 #endif 
    127  
    128     for (i=0; i<c->numDel; i++) { 
    129  
    130         n0 = c->deleted[i].indices[0]; 
    131         n1 = c->deleted[i].indices[1]; 
    132         n2 = c->deleted[i].indices[2]; 
    133  
    134         if (((n0 == c->u) || (n1 == c->u)) && 
    135             ((e = findEdge(mesh->edges, mesh->numEdges, n0, n1)) != -1)) { 
    136  
    137             mesh->edges[e].enable = FALSE; 
    138             bh_delete(h, e); 
    139         } 
    140  
    141         if (((n1 == c->u) || (n2 == c->u)) && 
    142             ((e = findEdge(mesh->edges, mesh->numEdges, n1, n2)) != -1)) { 
    143               
    144             mesh->edges[e].enable = FALSE; 
    145             bh_delete(h, e); 
    146         } 
    147  
    148         if (((n0 == c->u) || (n2 == c->u)) && 
    149             ((e = findEdge(mesh->edges, mesh->numEdges, n0, n2)) != -1)) { 
    150                
    151             mesh->edges[e].enable = FALSE; 
    152             bh_delete(h, e); 
    153         } 
    154     } 
    155  
    156     // Update mesh and heap 
    157     for(i = 1; i <= h->n; i++) { 
    158         e = h->a[i].item; 
    159  
    160         if ((int)mesh->edges[e].u == c->u) 
    161             mesh->edges[e].u = c->v; 
    162  
    163         if ((int)mesh->edges[e].v == c->u) 
    164             mesh->edges[e].v = c->v; 
    165          
    166         // Check edge 
    167         u = mesh->edges[e].u; 
    168         v = mesh->edges[e].v; 
    169      
    170         // if it is not a valid edge we simply delete it 
    171         if ((u == v) ||  
    172             (u == c->u) || (v == c->u)) { 
    173  
    174             mesh->edges[e].enable = FALSE; 
    175             bh_delete(h, e); 
    176         } 
    177     } 
     106        int *lv, *le, numV, numE; 
     107#endif 
    178108 
    179109#ifdef RECOMPUTE_THE_WHOLE_HEAP 
    180110 
    181     nh = bh_alloc(mesh->numEdges); 
    182  
    183     // Recompute heap 
    184     for(i = 1; i <= h->n; i++) { 
    185         e = h->a[i].item; 
    186          
    187         cost = computeEdgeCost(mesh, e); 
    188          
    189         // Add only valid edges 
    190         if (cost != FLT_MAX) 
    191             bh_insert(nh, e, cost); 
    192          
    193 #ifdef MAKE_FULL_COPY 
    194         // Restore initial triangle list 
    195         memcpy(mesh->triangles, triangleCopy, lastNumTriangles * sizeof(Triangle)); 
    196         mesh->numTriangles = lastNumTriangles; 
    197 #endif 
    198     } 
    199  
    200 #ifdef MAKE_FULL_COPY 
    201     // Free memory 
    202     if (triangleCopy != NULL) free(triangleCopy); 
    203 #endif 
    204     bh_free(h); 
    205  
    206     h = nh; 
     111        nh = bh_alloc(mesh->numEdges); 
     112 
     113        // Recompute heap 
     114        for(i = 1; i <= h->n; i++) { 
     115                e = h->a[i].item; 
     116 
     117                cost = computeEdgeCost(mesh, e); 
     118 
     119                // Add only valid edges 
     120                if (cost != FLT_MAX) 
     121                        bh_insert(nh, e, cost); 
     122        } 
     123 
     124        bh_free(h); 
     125 
     126        h = nh; 
    207127 
    208128#else 
    209129 
    210     // Compute vertices adjacent to a vertex 
    211     lv = verticesAdjToVertex(mesh, c->v, &numV); 
    212  
    213     // Compute edges adjacent to vertices 
    214     le = edgesAdjToVertices(mesh, lv, numV, &numE); 
    215  
    216     free(lv); 
    217  
    218     // Delete adjacent edges from the heap 
    219     for(i = 0; i <numE; i++) { 
    220         e = le[i]; 
    221  
    222         mesh->edges[e].enable = FALSE; 
    223         bh_delete(h, e); 
    224     } 
    225  
    226     // Recompute cost only for the adjacent edges 
    227     for(i = 0; i <numE; i++) { 
    228         e = le[i]; 
    229          
    230         cost = computeEdgeCost(mesh, e); 
    231  
    232         // Add only valid edges 
    233         if (cost != FLT_MAX) { 
    234             bh_insert(h, e, cost); 
    235             mesh->edges[e].enable = TRUE; 
    236         } 
    237          
    238 #ifdef MAKE_FULL_COPY 
    239         // Restore initial triangle list 
    240         memcpy(mesh->triangles, triangleCopy, lastNumTriangles * sizeof(Triangle)); 
    241         mesh->numTriangles = lastNumTriangles; 
    242 #endif 
    243  
    244     } 
    245  
    246 #ifdef MAKE_FULL_COPY 
    247     // Free memory 
    248     if (triangleCopy != NULL) free(triangleCopy); 
    249 #endif 
    250  
    251     free(le); 
     130        // Compute vertices adjacent to a vertex 
     131        lv = verticesAdjToVertex(mesh, c->v, &numV); 
     132 
     133        // Compute edges adjacent to vertices 
     134        le = edgesAdjToVertices(mesh, lv, numV, &numE); 
     135 
     136        free(lv); 
     137 
     138        // Set a dirty flag to adjacent edges from the heap 
     139        for(i = 0; i <numE; i++) { 
     140                e = le[i]; 
     141 
     142                bh_mark(h, e, TRUE); 
     143        } 
     144 
     145        free(le); 
    252146#endif 
    253147 
    254148#ifdef DEBUG  
    255     bh_mydump(mesh, h); 
    256     //getchar(); 
    257 #endif 
    258  
    259     return h; 
     149        bh_mydump(mesh, h); 
     150        //getchar(); 
     151#endif 
     152 
     153        return h; 
    260154} 
    261155 
    262156GLdouble VMI::computeEdgeLength(Vertex *vertices, int u, int v) { 
    263     GLfloat ux, uy, uz, vx, vy ,vz, rx, ry, rz; 
    264      
    265     ux = vertices[u].x; 
    266     uy = vertices[u].y; 
    267     uz = vertices[u].z; 
    268  
    269     vx = vertices[v].x; 
    270     vy = vertices[v].y; 
    271     vz = vertices[v].z; 
    272  
    273     rx = ux - vx; 
    274     ry = uy - vy; 
    275     rz = uz - vz; 
    276  
    277     return sqrt((rx * rx) + (ry * ry) + (rz * rz)); 
     157        GLfloat ux, uy, uz, vx, vy ,vz, rx, ry, rz; 
     158 
     159        ux = vertices[u].x; 
     160        uy = vertices[u].y; 
     161        uz = vertices[u].z; 
     162 
     163        vx = vertices[v].x; 
     164        vy = vertices[v].y; 
     165        vz = vertices[v].z; 
     166 
     167        rx = ux - vx; 
     168        ry = uy - vy; 
     169        rz = uz - vz; 
     170 
     171        return sqrt((rx * rx) + (ry * ry) + (rz * rz)); 
    278172} 
    279173 
    280174/////////////////////////////////////////////////////////////////////////////// 
    281175 
    282 void VMI::swap(unsigned int *i, unsigned int *j) { 
    283     unsigned int t; 
    284  
    285     t = *i; 
    286     *i = *j; 
    287     *j = t; 
    288 } 
    289  
    290176void VMI::chooseBestEndPoints(Mesh *mesh, int e) { 
    291     int v = mesh->edges[e].v; 
    292     int u = mesh->edges[e].u; 
    293     int numTu; 
    294     int *Tu  = trianglesAdjToVertex(mesh, u, &numTu); 
    295     int numTuv = 0; 
    296     int *Tuv = (int *)malloc(numTu * sizeof(int)); 
    297     int numTv; 
    298     int *Tv  = trianglesAdjToVertex(mesh, v, &numTv); 
    299     int i, j, f, n, n0, n1, n2; 
    300     float cost_u_v, cost_v_u, mincurve, curve, dot; 
    301      
    302     //printItemList(Tv, numTv); 
    303     //printItemList(Tu, numTu); 
    304      
    305     // Compute Tuv 
    306     for (i=0; i<(int)numTu; i++) { 
    307          
    308         n = Tu[i]; 
    309          
    310         n0 = mesh->triangles[n].indices[0]; 
    311         n1 = mesh->triangles[n].indices[1]; 
    312         n2 = mesh->triangles[n].indices[2]; 
    313          
    314         if ((n0 == v) || (n1 == v) || (n2 == v)) 
    315              
    316             Tuv[numTuv++] = n; 
    317     } 
    318     //printItemList(Tuv, numTuv); 
    319      
    320     curve = 0.0f; 
    321     for (i=0; i<numTu; i++) { 
    322         n = Tu[i]; 
    323         mincurve = 1.0f; 
    324         for (j=0; j<numTuv; j++) { 
    325             f = Tuv[j]; 
    326             dot = glmDot(mesh->triangles[f].normal, mesh->triangles[n].normal); 
    327             //printf("dot: %f \n", dot); 
    328             mincurve = MIN(mincurve, (1.0f - dot) / 2.0f); 
    329         } 
    330         //printf("mincurve: %f \n", mincurve); 
    331         curve = MAX(curve, mincurve); 
    332     } 
    333     cost_u_v = curve; 
    334      
    335     curve = 0.0f; 
    336     for (i=0; i<numTv; i++) { 
    337         n = Tv[i]; 
    338         mincurve = 1.0f; 
    339         for (j=0; j<numTuv; j++) { 
    340             f = Tuv[j]; 
    341             dot = glmDot(mesh->triangles[f].normal, mesh->triangles[n].normal); 
    342             //printf("%f \n", dot); 
    343             mincurve = MIN(mincurve, (1.0f - dot) / 2.0f); 
    344         } 
    345         curve = MAX(curve, mincurve); 
    346     } 
    347     cost_v_u = curve; 
    348      
    349     //printf("e%d(%d,%d) cost: %f \n", e, u, v, cost_u_v); 
    350     //printf("e%d(%d,%d) cost: %f \n", e, v, u, cost_v_u); 
    351      
    352     if ( (cost_v_u + 0.000001) < cost_u_v) { 
    353         //printf("Swap edge\n"); 
    354         //printf("e%d(%d,%d) cost: %f \n", e, u, v, cost_u_v); 
    355         //printf("e%d(%d,%d) cost: %f \n", e, v, u, cost_v_u); 
    356         swap(&mesh->edges[e].u, &mesh->edges[e].v); 
    357     } 
    358  
    359     free(Tuv); 
    360     free(Tu); 
    361     free(Tv); 
     177        int v = mesh->edges[e].v; 
     178        int u = mesh->edges[e].u; 
     179        int numTu = mesh->vertices[u].numTriangles; 
     180        int numTuv = 0; 
     181        int *Tuv = (int *)malloc(numTu * sizeof(int)); 
     182        int i, j, f, n, n0, n1, n2; 
     183        float cost_u_v, cost_v_u, mincurve, curve, dot; 
     184 
     185        // Compute Tuv 
     186        for (i=0; i<(int)numTu; i++) { 
     187 
     188                n = mesh->vertices[u].triangles[i]; 
     189 
     190                n0 = mesh->triangles[n].indices[0]; 
     191                n1 = mesh->triangles[n].indices[1]; 
     192                n2 = mesh->triangles[n].indices[2]; 
     193 
     194                if ((n0 == v) || (n1 == v) || (n2 == v)) 
     195 
     196                        Tuv[numTuv++] = n; 
     197        } 
     198        //printItemList(Tuv, numTuv); 
     199 
     200        curve = 0.0f; 
     201        for (i=0; i<numTu; i++) { 
     202                n = mesh->vertices[u].triangles[i]; 
     203                mincurve = 1.0f; 
     204                for (j=0; j<numTuv; j++) { 
     205                        f = Tuv[j]; 
     206                        dot = glmDot(mesh->triangles[f].normal, mesh->triangles[n].normal); 
     207                        //printf("dot: %f \n", dot); 
     208                        mincurve = MIN(mincurve, (1.0f - dot) / 2.0f); 
     209                } 
     210                //printf("mincurve: %f \n", mincurve); 
     211                curve = MAX(curve, mincurve); 
     212        } 
     213        cost_u_v = curve; 
     214 
     215        curve = 0.0f; 
     216        for (i=0; i<(int)mesh->vertices[v].numTriangles; i++) { 
     217                n = mesh->vertices[v].triangles[i]; 
     218                mincurve = 1.0f; 
     219                for (j=0; j<numTuv; j++) { 
     220                        f = Tuv[j]; 
     221                        dot = glmDot(mesh->triangles[f].normal, mesh->triangles[n].normal); 
     222                        //printf("%f \n", dot); 
     223                        mincurve = MIN(mincurve, (1.0f - dot) / 2.0f); 
     224                } 
     225                curve = MAX(curve, mincurve); 
     226        } 
     227 
     228        cost_v_u = curve; 
     229 
     230        //printf("e%d(%d,%d) cost: %f \n", e, u, v, cost_u_v); 
     231        //printf("e%d(%d,%d) cost: %f \n", e, v, u, cost_v_u); 
     232 
     233        if ( (cost_v_u + 0.000001) < cost_u_v) { 
     234                //printf("Swap edge\n"); 
     235                //printf("e%d(%d,%d) cost: %f \n", e, u, v, cost_u_v); 
     236                //printf("e%d(%d,%d) cost: %f \n", e, v, u, cost_v_u); 
     237                 
     238                swap((int *)&mesh->edges[e].u, (int *)&mesh->edges[e].v); 
     239        } 
     240 
     241        free(Tuv); 
    362242} 
    363243/////////////////////////////////////////////////////////////////////////////// 
    364244 
     245void saveProjectedAreas(GLuint **histogram, GLuint numCameras, Change *c, GLuint *dest) { 
     246        GLuint i, j, t, n = 0; 
     247 
     248        for (j=0; j<(GLuint)c->numDel; j++) { 
     249 
     250                t = c->deleted[j].id; 
     251 
     252                for (i=0; i<numCameras; i++) { 
     253 
     254                        dest[n] = histogram[i][t + 1]; 
     255                        n++; 
     256                } 
     257        } 
     258 
     259        for (j=0; j<(GLuint)c->numMod; j++) {  
     260 
     261                t = c->modified[j].id; 
     262 
     263                for (i=0; i<numCameras; i++) { 
     264 
     265                        dest[n] = histogram[i][t + 1]; 
     266                        n++; 
     267                }    
     268        } 
     269 
     270        // Save background area 
     271        for (i=0; i<numCameras; i++) { 
     272 
     273                dest[n] = histogram[i][0]; 
     274                n++; 
     275        } 
     276} 
     277 
     278void loadProjectedAreas(GLuint *src, GLuint numCameras, Change *c, GLuint **histogram) { 
     279        GLuint i, j, t, n = 0; 
     280 
     281        for (j=0; j<(GLuint)c->numDel; j++) {  
     282 
     283                t = c->deleted[j].id; 
     284 
     285                for (i=0; i<numCameras; i++) { 
     286 
     287                        histogram[i][t + 1] = src[n]; 
     288                        n++; 
     289                } 
     290        } 
     291 
     292        for (j=0; j<(GLuint)c->numMod; j++) {  
     293 
     294                t = c->modified[j].id; 
     295 
     296                for (i=0; i<numCameras; i++) { 
     297 
     298                        histogram[i][t + 1] = src[n]; 
     299                        n++; 
     300                } 
     301        } 
     302 
     303        // Load background area 
     304        for (i=0; i<numCameras; i++) { 
     305 
     306                histogram[i][0] = src[n]; 
     307                n++; 
     308        } 
     309} 
     310 
     311/////////////////////////////////////////////////////////////////////////////// 
     312 
    365313GLdouble VMI::computeEdgeCost(Mesh *mesh, int e) { 
    366     GLdouble cost = 0.0, newI, sal = 1.0, /*length,*/ error; 
    367     GLuint i; 
    368     Change *c; 
    369  
    370     chooseBestEndPoints(mesh, e); 
    371     //getchar(); 
    372  
    373     c = createChange(mesh, e); 
    374      
     314        GLdouble cost = 0.0, sal = 1.0; 
     315#ifdef MI 
     316        GLdouble *auxIs = NULL; 
     317#else 
     318        GLdouble newI; 
     319#endif 
     320        GLuint i, *histoAux = NULL; 
     321        Change *c; 
     322 
     323        chooseBestEndPoints(mesh, e); 
     324        //getchar(); 
     325 
     326        c = createChange(mesh, e); 
     327 
     328        // Compute cost only for boundary or manifold edges 
     329        if (c->numDel == 2 
     330                        /*(c->numDel <3) && (c->numDel > 0)*/) { 
     331                // Allocate memory 
     332                histoAux = (GLuint *)malloc((c->numDel + c->numMod + 1) * numCameras * sizeof(GLuint)); 
     333 
     334#ifdef MI // Mutual Information 
     335                /////////////////////////////////////////////////////////////////////////////// 
     336                // Allocate memory 
     337                auxIs = (GLdouble *)malloc(numCameras * sizeof(GLdouble)); 
     338 
     339                //printFullHistogram(histogram, numCameras, mesh->numTriangles); 
     340 
     341                for (i=0; i<numCameras; i++) { 
     342                        // Copy old informations 
     343                        auxIs[i] = initialIs[i]; 
     344 
     345                        initialIs[i] = decMI(initialIs[i], histogram, numCameras, i, c); 
     346                } 
     347 
     348                saveProjectedAreas(histogram, numCameras, c, histoAux); 
    375349    // Apply the edge collapse 
    376     computeChanges(mesh, c); 
    377      
    378     // Compute cost only for boundary or manifold edges 
    379     if ((c->numDel <3) && (c->numDel > 0)) { 
    380  
    381         //length = computeEdgeLength(mesh->vertices, c->u, c->v); 
    382  
    383         doChange(mesh, c); 
    384          
    385         // Compute the cost of an edge collapse 
    386         getProjectedAreas(histogram, numCameras); 
    387         //printFullHistogram(histogram, numCameras, mesh->numTriangles); 
    388         //getchar(); 
     350                doChange(mesh, c); 
     351 
     352                // Compute the cost of an edge collapse 
     353                getProjectedAreasWin(histogram, numCameras, c); 
     354                //printFullHistogram(histogram, numCameras, mesh->numTriangles); 
     355                //getchar(); 
     356 
    389357#ifdef SALIENCY 
    390         sal = computeEdgeSaliency(mesh, c, percentile); 
    391         //printf("  e:%d s: %f\n", c->e, sal); 
    392 #endif 
    393         for (i=0;i <numCameras; i++) { 
     358                sal = computeEdgeSaliency(mesh, c, percentile); 
     359                //printf("  e:%d s: %f\n", c->e, sal); 
     360#endif 
     361 
     362                for (i=0; i<numCameras; i++) { 
     363 
     364                        initialIs[i] = incMI(initialIs[i], histogram, numCameras, i, c); 
     365                        //printf("  I0:%f Is: %f\n", auxE[i], initialIs[i]); 
     366 
     367                        cost += (ABS(auxIs[i] - initialIs[i]) * cameras[i].weight * sal); 
     368 
     369                        // Restore old informations 
     370                        initialIs[i] = auxIs[i]; 
     371                } 
     372 
     373                undoChange(mesh, c); 
     374 
     375                loadProjectedAreas(histoAux, numCameras, c, histogram); 
     376                //printFullHistogram(histogram, numCameras, mesh->numTriangles); 
     377                //exit(1); 
     378 
     379                // Free memory 
     380                free(auxIs); 
     381#else 
     382                /////////////////////////////////////////////////////////////////////////////// 
     383 
     384                saveProjectedAreas(histogram, numCameras, c, histoAux); 
     385 
     386                doChange(mesh, c); 
     387 
     388                // Compute the cost of an edge collapse 
     389                getProjectedAreasWin(histogram, numCameras, c); 
     390                //printFullHistogram(histogram, numCameras, mesh->numTriangles); 
     391 
     392#ifdef SALIENCY 
     393                sal = computeEdgeSaliency(mesh, c, percentile); 
     394                //printf("  e:%d s: %f\n", c->e, sal); 
     395#endif 
     396                for (i=0;i <numCameras; i++) { 
    394397 
    395398#ifdef KL // Kullback-Leibler 
    396             newI = computeKL(mesh, histogram[i]); 
    397 #endif 
    398 #ifdef MI // Mutual Information 
    399             newI = computeMI(mesh, histogram, numCameras, i); 
     399                        newI = computeKL(mesh, histogram[i]); 
    400400#endif 
    401401#ifdef HE // Hellinger 
    402             newI = computeHE(mesh, histogram[i]); 
     402                        newI = computeHE(mesh, histogram[i]); 
    403403#endif 
    404404#ifdef CS // Chi-Square 
    405             newI = computeCS(mesh, histogram[i]); 
    406 #endif 
    407  
    408             error = ABS(initialIs[i] - newI); 
    409              
    410             if (error > 0.0)  
    411                 cost += ((error * cameras[i].weight * sal) /*+ (length * 0.05)*/); 
    412         } 
    413  
    414         undoChange(mesh, c); 
    415  
    416     } else cost = FLT_MAX; 
    417  
    418  
    419     deleteChange(c); 
    420  
    421     return cost; 
    422 } 
    423  
    424 /////////////////////////////////////////////////////////////////////////////// 
    425  
     405                        newI = computeCS(mesh, histogram[i]); 
     406#endif 
     407                        cost += (ABS(initialIs[i] - newI) * cameras[i].weight * sal); 
     408                } 
     409                undoChange(mesh, c); 
     410 
     411                loadProjectedAreas(histoAux, numCameras, c, histogram); 
     412                //printFullHistogram(histogram, numCameras, mesh->numTriangles); 
     413                //exit(1); 
     414 
     415                /////////////////////////////////////////////////////////////////////////////// 
     416#endif 
     417                // Free memory 
     418                free(histoAux); 
     419 
     420        } else cost = FLT_MAX; 
     421 
     422        deleteChange(c); 
     423 
     424        return cost; 
     425} 
     426/* Get a valid edge from the heap */ 
     427int getMinValidEdge(Mesh *mesh, bheap_t *h) { 
     428        int e; 
     429 
     430        /* pick the minimum-cost valid edge from heap */ 
     431        e = bh_min(h); 
     432 
     433        while (mesh->edges[e].enable == FALSE) { 
     434                /* delete invalid edge */ 
     435                bh_delete(h, e); 
     436                /* pick again*/ 
     437                e = bh_min(h); 
     438        } 
     439 
     440        return e; 
     441} 
     442 
     443/* Get the mininum edge cost from the heap using lazy evaluation */ 
     444int getMinEdge(Mesh *mesh, bheap_t *h) { 
     445        double cost; 
     446        int e; 
     447 
     448        /* pick the minimum-cost edge from heap */ 
     449        e = getMinValidEdge(mesh, h); 
     450 
     451        while (bh_is_dirty(h, e)) { 
     452                /* delete edge from heap */ 
     453                bh_delete(h, e); 
     454                mesh->edges[e].enable = FALSE; 
     455                /* recompute cost*/ 
     456                cost = computeEdgeCost(mesh, e); 
     457 
     458                // Add only valid edges 
     459                if (cost != FLT_MAX) { 
     460                        /* reinsert into the heap */ 
     461                        bh_insert(h, e, cost); 
     462                        mesh->edges[e].enable = TRUE; 
     463                        /* set dirty flag to FALSE */ 
     464                        bh_mark(h, e, FALSE); 
     465                } 
     466 
     467                /* pick again */ 
     468                e = getMinValidEdge(mesh, h); 
     469        } 
     470 
     471        return e; 
     472} 
     473 
     474/////////////////////////////////////////////////////////////////////////// 
    426475void VMI::simplifyModel(Mesh *mesh, GLuint numDemandedTri) 
    427476{ 
     
    444493        { 
    445494#ifdef SALIENCY        
    446 #ifdef KL // Kullback-Leibler 
    447                 sprintf(s,"%s_VKL_S.log", filename); 
    448 #endif 
    449 #ifdef MI // Mutual Information 
    450                 sprintf(s,"%s_VMI_S.log", filename); 
    451 #endif 
    452 #ifdef HE // Hellinger 
    453                 sprintf(s,"%s_VHE_S.log", filename); 
    454 #endif 
    455 #ifdef CS // Chi-Square 
    456                 sprintf(s,"%s_VCS_S.log", filename); 
    457 #endif 
     495                sprintf(s,"%s_%s_S.log", filename, EXT); 
    458496#else 
    459 #ifdef KL // Kullback-Leibler 
    460                 sprintf(s,"%s_VKL.log", filename); 
    461 #endif 
    462 #ifdef MI // Mutual Information 
    463                 sprintf(s,"%s_VMI.log", filename); 
    464 #endif 
    465 #ifdef HE // Hellinger 
    466                 sprintf(s,"%s_VHE.log", filename); 
    467 #endif 
    468 #ifdef CS // Chi-Square 
    469                 sprintf(s,"%s_VCS.log", filename); 
    470 #endif 
     497                sprintf(s,"%s_%s.log", filename, EXT); 
    471498#endif 
    472499 
     
    475502                /* open the file */ 
    476503                file = fopen(s, "a+"); 
    477                  
     504 
    478505                if (!file) 
    479506                { 
     
    485512        //      Open file of simplification sequence. 
    486513        file_simp_seq   =       fopen("SimplifSequence.txt", "w"); 
    487          
     514 
    488515        if (!file_simp_seq) 
    489516        { 
    490517                fprintf(stderr, 
    491                                                 "simplifyModel() failed: ", 
    492                                                 "can't open file \"SimplifSequence\" to write.\n"); 
    493         } 
    494          
     518                                "simplifyModel() failed: ", 
     519                                "can't open file \"SimplifSequence\" to write.\n"); 
     520        } 
     521 
    495522        h = initHeap(mesh); 
    496523 
     
    500527        { 
    501528                // Get the edge that has the minimum cost 
    502                 e = bh_min(h); 
     529                //e = getMinEdge(mesh, h); 
     530                e = extractValidEdge(mesh, h); 
    503531 
    504532                c = createChange(mesh, e); 
    505533 
    506534                // Apply the edge collapse 
    507                 computeChanges(mesh, c); 
    508535                doChange(mesh, c); 
     536                //deleteVertexOfMap(mesh, c->u); 
     537 
     538                // Write Simplification sequence. 
     539                saveSimplificationSequence(c,0); 
     540 
     541                //contractTwinVertices(mesh, c->u, c->v); // vertex u is removed 
    509542 
    510543                if (bSaveLog == TRUE) writeChange(file, c); 
    511544 
    512                 //      Write Simplification sequence. 
    513                 saveSimplificationSequence(c); 
    514                  
    515545                cost = h->a[h->p[e]].key; 
    516                  
     546 
     547                /* 
    517548                printf( "Edge collapse e%d(%d,%d) %f MIN d %d m %d\n", 
    518                                                 c->e, 
    519                                                 c->u, 
    520                                                 c->v, 
    521                                                 cost, 
    522                                                 c->numDel, 
    523                                                 c->numMod); 
     549                                c->e, 
     550                                c->u, 
     551                                c->v, 
     552                                cost, 
     553                                c->numDel, 
     554                                c->numMod); 
     555                */ 
    524556 
    525557                mesh->edges[e].enable = FALSE; 
    526                  
    527558                bh_delete(h, e); 
    528559 
     
    532563                computeCameraIs(histogram, numCameras, initialIs); 
    533564 
     565                deleteEdges(mesh, c); 
     566                modifyEdges(mesh, c); 
     567 
    534568                // Update the heap according to the edge collapse 
    535569                h = updateHeap(h, mesh, c); 
     
    539573                deleteChange(c); 
    540574 
    541                 printf("t %d\n", mesh->currentNumTriangles); 
    542         } 
     575                //printf("t %d\n", mesh->currentNumTriangles); 
     576        } 
     577 
     578        //      Debug. 
     579        cout    <<      "Number of vertices: " 
     580                <<      mesh->currentNumVertices 
     581                <<      endl; 
    543582 
    544583        if (bSaveLog == TRUE) 
     
    554593} 
    555594 
     595/////////////////////////////////////////////////////////////////////////////// 
     596//      Extract a correct edge of the heap. 
     597/////////////////////////////////////////////////////////////////////////////// 
     598int VMI::extractValidEdge(Mesh *mesh,   bheap_t *h) 
     599{ 
     600        int     e; 
     601 
     602        while((e        =       isValidEdge(mesh,getMinEdge(mesh, h))) == -1); 
     603 
     604        return  e; 
     605} 
     606 
     607/////////////////////////////////////////////////////////////////////////////// 
     608//      Indicates if an edge is valid. 
     609/////////////////////////////////////////////////////////////////////////////// 
     610int     VMI::isValidEdge(Mesh   *mesh,  int edge) 
     611{ 
     612        int     result; 
     613        Vertex  *u; 
     614        Vertex  *v; 
     615        _float3_        fu; 
     616        _float3_        fv; 
     617 
     618        u       =       &mesh->vertices[mesh->edges[edge].u]; 
     619        v       =       &mesh->vertices[mesh->edges[edge].v]; 
     620 
     621        fu      =       _float3_(u->x,u->y,u->z); 
     622        fv      =       _float3_(v->x,v->y,v->z); 
     623 
     624        if ((mVertexMap.find(fu) != mVertexMap.end()) 
     625                        || 
     626                        (mVertexMap.find(fv) != mVertexMap.end())) 
     627        { 
     628                result  = edge; 
     629        } 
     630        //      Edge is not valid. 
     631        else 
     632        { 
     633                result  =       -1; 
     634        } 
     635 
     636        return  result; 
     637} 
     638 
     639//------------------------------------------------------------------------- 
     640//      Inits the multimap of vertices. 
     641//------------------------------------------------------------------------- 
     642void    VMI::initVertexMultimap(Mesh *mesh,multimap<int,int> &vertexMultimap) 
     643{ 
     644        Vertex  *vertex; 
     645        float           x,y,z; 
     646 
     647        //      Clears multimap of vertices. 
     648        mVertexMap.clear(); 
     649        mVertices.clear(); 
     650 
     651        mVertices       =       vertexMultimap; 
     652 
     653        //      Debug. 
     654        cout    <<      "Vertex Map Elements: " 
     655                                <<      mVertices.size() 
     656                                <<      endl; 
     657 
     658        //      For each vertex. 
     659        for (int        i       =       0;      i < mesh->numVertices;  i++) 
     660        { 
     661                vertex  =       &mesh->vertices[i]; 
     662 
     663                x       =       vertex->x; 
     664                y       =       vertex->y; 
     665                z       =       vertex->z; 
     666 
     667                mVertexMap.insert(t_pair(_float3_(x,y,z),i)); 
     668        } 
     669} 
     670 
     671//------------------------------------------------------------------------- 
     672//      Deletes a vertex in the multimap. 
     673//------------------------------------------------------------------------- 
     674void    VMI::deleteVertexOfMap(Mesh     *mesh, int u) 
     675{ 
     676        _float3_        removed_vert; 
     677        t_map_str       lb; 
     678        t_map_str       ub; 
     679 
     680        //      Position of the vertex removed. 
     681        removed_vert    =       _float3_(       mesh->vertices[u].x, 
     682                                                                                                                mesh->vertices[u].y, 
     683                                                                                                                mesh->vertices[u].z); 
     684 
     685        //      If position of the removed vertex is found. 
     686        if (mVertexMap.end() != mVertexMap.find(removed_vert)) 
     687        { 
     688                lb      =       mVertexMap.lower_bound(removed_vert); 
     689                ub      =       mVertexMap.upper_bound(removed_vert); 
     690 
     691                //      For each vertex. 
     692                while   (lb != ub) 
     693                { 
     694                        //      If removed vertex is found. 
     695                        if ((*lb).second == u) 
     696                        { 
     697                                //      Debug. 
     698                                cout    <<      "Vertex erased: " 
     699                                                        <<      (*lb).second 
     700                                                        <<      endl; 
     701 
     702                                //      Delete vertex that disappears. 
     703                                mVertexMap.erase(lb); 
     704                                 
     705                                //      Break while. 
     706                                lb      =       ub; 
     707                        } 
     708                        else 
     709                        { 
     710                                //      Next iteration. 
     711                                lb++; 
     712                        } 
     713                } 
     714        } 
     715} 
     716 
     717//------------------------------------------------------------------------- 
     718//      Compare the coordinates of two vertices. 
     719//------------------------------------------------------------------------- 
     720bool    VMI::compareVertices(Vertex *vertices,int       u,      int v) 
     721{ 
     722        if ((vertices[u].x == vertices[v].x) 
     723                        && 
     724                        (vertices[u].y == vertices[v].y) 
     725                        && 
     726                        (vertices[u].z == vertices[v].z)) 
     727        { 
     728                return  true; 
     729        } 
     730        else 
     731        { 
     732                return  false; 
     733        } 
     734} 
     735 
     736//------------------------------------------------------------------------- 
     737//      Find edge of the simplification sequence. 
     738//------------------------------------------------------------------------- 
     739void    VMI::contractInitialMesh(Mesh   *mesh) 
     740{ 
     741        bool            edge_found; 
     742        Geometry::MeshSimplificationSequence::Step      step; 
     743 
     744        multimap<int,int>::iterator     it0; 
     745        multimap<int,int>::iterator     lb0; 
     746        multimap<int,int>::iterator     ub0; 
     747        multimap<int,int>::iterator     lb1; 
     748        multimap<int,int>::iterator     ub1; 
     749 
     750        std::vector<Geometry::MeshSimplificationSequence::Step> steps; 
     751 
     752        Edge            *econ; 
     753        float           x,y,z; 
     754        int                     *lv1; 
     755        int                     v0; 
     756        int                     v1; 
     757        int                     edge; 
     758        int                     num_edges; 
     759        Change  *c; 
     760 
     761        //      Copy simplification steps of the joined mesh. 
     762        steps   =       mSequence->mSteps; 
     763 
     764        mSequence->mSteps.clear(); 
     765 
     766        for     (size_t i       =       0;      i < steps.size(); i++) 
     767        { 
     768                step    =       steps[i]; 
     769 
     770                lb0     =       mVertices.lower_bound(steps[i].mV0); 
     771                ub0     =       mVertices.upper_bound(steps[i].mV0); 
     772                lb1     =       mVertices.lower_bound(steps[i].mV1); 
     773                ub1     =       mVertices.upper_bound(steps[i].mV1); 
     774 
     775                edge_found      =       false; 
     776 
     777                //      Debug. 
     778                cout    <<      "step " 
     779                                        <<      i 
     780                                        <<      " V0: " 
     781                                        <<      steps[i].mV0 
     782                                        <<      " V1: " 
     783                                        <<      steps[i].mV1 
     784                                        <<      endl; 
     785 
     786                //      Removed vertex. 
     787                while ((lb1 != ub1) && !edge_found) 
     788                { 
     789                        //      Real index. 
     790                        v1      =       (*lb1).second; 
     791 
     792                        lv1     =       edgesAdjToVertex(mesh,v1,&num_edges); 
     793 
     794                        //      Edje iterator. 
     795                        edge    =       0; 
     796 
     797                        while   ((edge < num_edges) &&  !edge_found) 
     798                        { 
     799                                econ    =       &mesh->edges[lv1[edge]]; 
     800 
     801                                //      Begin of iteration v0. 
     802                                it0     =       lb0; 
     803 
     804                                //      Remaining vertex. 
     805                                while ((it0 != ub0) &&  !edge_found) 
     806                                { 
     807                                        //      Real index. 
     808                                        v0      =       (*it0).second; 
     809                                         
     810                                        if (compareVertices(mesh->vertices,econ->v,v0)) 
     811                                        { 
     812                                                c = newChange(mesh, econ->u, econ->v); 
     813 
     814                                                edge_found      =       true; 
     815                                        } 
     816                                        else if (compareVertices(mesh->vertices,econ->u,v0)) 
     817                                        { 
     818                                                c = newChange(mesh, econ->v, econ->u); 
     819 
     820                                                edge_found      =       true; 
     821                                        } 
     822                                        else 
     823                                        { 
     824                                                it0++; 
     825                                        } 
     826                                } 
     827 
     828                                edge++; 
     829                        } 
     830 
     831                        lb1++; 
     832                } 
     833 
     834                if (edge_found) 
     835                { 
     836                        //      Debug. 
     837                        cout    <<      "Contracting edge of the initial mesh..."        
     838                                                <<      endl 
     839                                                <<      "u: " 
     840                                                <<      c->u 
     841                                                <<      " v: " 
     842                                                <<      c->v 
     843                                                <<      endl; 
     844         
     845                        // Collapse new edge. 
     846                        doChange(mesh, c); // the mesh has been updated. 
     847 
     848                        // Write Simplification sequence. 
     849                        saveSimplificationSequence(c,0); 
     850 
     851                        deleteVertexOfMap(mesh, c->u); 
     852                        deleteEdges(mesh, c); 
     853                        modifyEdges(mesh, c); 
     854                        deleteChange(c); 
     855 
     856                        //      Contract twin vertices. 
     857                        contractTwinVertices(mesh,v1,v0); 
     858                } 
     859        } 
     860} 
     861 
     862//------------------------------------------------------------------------- 
     863//      Find twin vertices and contract them. 
     864//------------------------------------------------------------------------- 
     865void VMI::contractTwinVertices( Mesh    *mesh, 
     866                                                                                                                                int             u, 
     867                                                                                                                                int             v) 
     868{ 
     869        bool                                    twin_found; 
     870        int                                             edge; 
     871        int                                             lonely_vert; 
     872        int                                             new_vert; 
     873        t_map_str                       lb; 
     874        t_map_str                       ub; 
     875        t_map_str                       it; 
     876        _float3_                        fu; 
     877        _float3_                        fv; 
     878        Edge                                    *econ; 
     879        float                                   x,y,z; 
     880        int                                             *le; 
     881        int                                             num_edges; 
     882        Change                          *c; 
     883 
     884        Geometry::GeoVertex     vertex_added; 
     885 
     886        if (!compareVertices(mesh->vertices,u,v)) 
     887        { 
     888                //take_bone_from_vert = v; 
     889 
     890                fu      =       _float3_(       mesh->vertices[u].x, 
     891                                                                                mesh->vertices[u].y, 
     892                                                                                mesh->vertices[u].z); 
     893 
     894                fv      =       _float3_(       mesh->vertices[v].x, 
     895                                                                                mesh->vertices[v].y, 
     896                                                                                mesh->vertices[v].z); 
     897 
     898                //      Find vertices width u coordinates. 
     899                while ((it = mVertexMap.find(fu)) != mVertexMap.end()) 
     900                { 
     901                        twin_found      =       false; 
     902 
     903                        lonely_vert     =       (*it).second; 
     904 
     905                        le      =       edgesAdjToVertex(mesh,lonely_vert,&num_edges); 
     906 
     907                        //      Find an edge width v coordinates. 
     908                        int     i       =       0; 
     909 
     910                        while((i < num_edges) && !twin_found) 
     911                        { 
     912                                econ    =       &mesh->edges[le[i]]; 
     913 
     914                                if (compareVertices(mesh->vertices,econ->u,v) 
     915                                                || 
     916                                                compareVertices(mesh->vertices,econ->v,v)) 
     917                                { 
     918                                        lb      =       mVertexMap.lower_bound(fv); 
     919                                        ub      =       mVertexMap.upper_bound(fv); 
     920 
     921                                        //      For each vertex. 
     922                                        while   (lb != ub) 
     923                                        { 
     924                                                //      If removed vertex is found. 
     925                                                if (((*lb).second == econ->u) 
     926                                                                || 
     927                                                                ((*lb).second == econ->v)) 
     928                                                { 
     929                                                        twin_found      =       true; 
     930 
     931                                                        //      Break while. 
     932                                                        lb      =       ub; 
     933                                                } 
     934                                                else 
     935                                                { 
     936                                                        //      Next iteration. 
     937                                                        lb++; 
     938                                                } 
     939                                        } 
     940                                } 
     941                                i++; 
     942                        } 
     943 
     944                        //      If a twin edge has been found. 
     945                        if (twin_found) 
     946                        { 
     947                                //      Debug. 
     948                                cout    <<      "Twin Collapsed..."     <<      endl; 
     949 
     950                                //      Compare vertices coordinates. 
     951                                if (compareVertices(mesh->vertices,econ->u,v)) 
     952                                { 
     953                                        // New edge for the change 
     954                                        c = newChange(mesh, econ->v, econ->u); 
     955 
     956                                        //      Debug. 
     957                                        cout    <<      "--Reverse--"   <<      endl; 
     958                                } 
     959                                else 
     960                                { 
     961                                        // New edge for the change 
     962                                        c = newChange (mesh, econ->u, econ->v); 
     963                                } 
     964 
     965                                // Collapse new edge. 
     966                                doChange(mesh, c); // the mesh has been updated. 
     967 
     968                                // Write Simplification sequence. 
     969                                saveSimplificationSequence(c,1); 
     970 
     971                                deleteVertexOfMap(mesh, c->u); 
     972                                deleteEdges(mesh, c); 
     973                                modifyEdges(mesh, c); 
     974                                deleteChange(c); 
     975                        } 
     976                        else 
     977                        { 
     978                                //      Debug. 
     979                                cout    <<      "Collapsing new edge..."        <<      endl; 
     980 
     981                                x       =       mesh->vertices[v].x; 
     982                                y       =       mesh->vertices[v].y; 
     983                                z       =       mesh->vertices[v].z; 
     984 
     985                                mesh->vertices = addVertex(     mesh->vertices, 
     986                                                                                                                                                (int *)&mesh->numVertices, 
     987                                                                                                                                                x, 
     988                                                                                                                                                y, 
     989                                                                                                                                                z, 
     990                                                                                                                                                &new_vert); 
     991                                 
     992                                // When a new vertex is added to the mesh, not only the 
     993                                // total number of vertices is increased but also current number 
     994                                mesh->currentNumVertices++; 
     995 
     996                                //      Adds new vertex to multimap. 
     997                                mVertexMap.insert(t_pair(_float3_(x,y,z),new_vert)); 
     998 
     999                                //      Creates new edge. 
     1000                                mesh->edges     =       addEdge(mesh->edges, 
     1001                                                                                                                        (int *)&mesh->numEdges, 
     1002                                                                                                                        lonely_vert, 
     1003                                                                                                                        new_vert, 
     1004                                                                                                                        &edge); 
     1005 
     1006                                /* 
     1007                                //      Debug. 
     1008                                cout    <<      "lonely_vert" 
     1009                                        <<      lonely_vert 
     1010                                        <<      "(" 
     1011                                        <<      mesh->vertices[lonely_vert].x 
     1012                                        <<      "," 
     1013                                        <<      mesh->vertices[lonely_vert].y 
     1014                                        <<      "," 
     1015                                        <<      mesh->vertices[lonely_vert].z 
     1016                                        <<      ")" 
     1017                                        <<      endl; 
     1018                                //      Debug. 
     1019                                cout    <<      "new_vert" 
     1020                                        <<      new_vert 
     1021                                        <<      "(" 
     1022                                        <<      mesh->vertices[new_vert].x 
     1023                                        <<      "," 
     1024                                        <<      mesh->vertices[new_vert].y 
     1025                                        <<      "," 
     1026                                        <<      mesh->vertices[new_vert].z 
     1027                                        <<      ")" 
     1028                                        <<      endl; 
     1029                                */ 
     1030 
     1031                                //      We assume here there are the same number of vertices 
     1032                                //      and texture coordinates and normals. 
     1033 
     1034                                //      Assigns the position of the vertex. 
     1035                                vPositions.push_back(Geometry::Vector3(x,y,z)); 
     1036 
     1037                                //      Assigns a texture coordinate for the vertex. 
     1038                                vTexCoords.push_back(vTexCoords[lonely_vert]); 
     1039 
     1040                                //      Assigns a normal coordinate for the vertex. 
     1041                                vNormals.push_back(vNormals[lonely_vert]); 
     1042 
     1043                                //      Adds new vertex information to simplification sequence. 
     1044                                vertex_added.id                         =       new_vert; 
     1045                                vertex_added.bonefrom   =       v; 
     1046 
     1047                                vertex_added.position   =       vPositions[new_vert]; 
     1048 
     1049                                vertex_added.texcoord   =       vTexCoords[new_vert]; 
     1050 
     1051                                vertex_added.normal     =       vNormals[new_vert]; 
     1052 
     1053                                mSequence->mNewVertices.push_back(vertex_added); 
     1054 
     1055                                // Collapse new edge 
     1056                                c = newChange(mesh, lonely_vert, new_vert); 
     1057 
     1058                                doChange(mesh, c); // the mesh has been updated 
     1059 
     1060                                // Write Simplification sequence. 
     1061                                saveSimplificationSequence(c,1); 
     1062 
     1063                                deleteVertexOfMap(mesh, c->u); 
     1064                                deleteEdges(mesh, c); 
     1065                                modifyEdges(mesh, c); 
     1066                                deleteChange(c); 
     1067                        } 
     1068                } 
     1069        } 
     1070} 
     1071 
Note: See TracChangeset for help on using the changeset viewer.