Changeset 2090 for GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs
- Timestamp:
- 02/05/07 13:29:55 (18 years ago)
- 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 32 32 { 33 33 if( fill == len ) 34 resize( len*2 ); 34 { 35 resize( len*2 ); 36 } 35 37 36 38 data[fill] = t; -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/bheap.h
r983 r2090 15 15 */ 16 16 typedef 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. */ 18 19 double key; /* distance is used as the key. */ 19 20 } bheap_item_t; 20 21 21 22 /* 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. 23 24 * p[] - stores the positions of vertices in the binary heap a[]. 24 25 * n - is the size of the binary heap. … … 58 59 */ 59 60 void bh_delete(bheap_t *h, int item); 61 /* bh_mark() marks an item from the binary heap pointed to by h. 62 */ 63 void 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 */ 68 double 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 */ 73 int bh_is_dirty(bheap_t *h, int item); 60 74 61 75 /* 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 5 5 6 6 #define OCTAHEDRON 6 // number of vertices 7 #define CUBE 8 7 8 #define ICOSAHEDRON 12 8 9 #define DODECAHEDRON 20 … … 26 27 extern void drawSphere(Camera *cameras, GLdouble radius, int slices, int stacks); 27 28 extern void drawOctahedron(Camera *cameras, GLdouble r); 29 extern void drawCube(Camera *cameras, GLdouble r); 28 30 extern void drawIcosahedron(Camera *cameras, GLdouble r); 29 31 extern void drawDodecahedron(Camera *cameras, GLdouble r); -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/change.h
r1024 r2090 5 5 #include <vector> 6 6 #include <map> 7 #include <GeoMeshSimpSequence.h> 7 8 8 9 namespace VMI … … 20 21 } Change; 21 22 23 /* 22 24 // Represents a simplification step in the sequence. 23 25 struct vmiStep … … 41 43 extern std::vector<vmiStep> mVMISteps; 42 44 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 43 62 extern Change *createChange (Mesh *mesh, int e); 63 extern Change *newChange (Mesh *mesh, int u, int v); 44 64 extern void writeChange(FILE* file, Change *c); 45 65 extern void deleteChange(Change *c); … … 47 67 48 68 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); 51 71 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); 53 73 54 74 extern void modifyTriangles(Mesh *mesh, Change *c); … … 62 82 extern void doChange(Mesh *mesh, Change *c); 63 83 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); 65 88 66 89 // Save simplification sequence in Geometry Game Tools format. 67 extern void saveSimplificationSequence(Change *c );90 extern void saveSimplificationSequence(Change *c, int obligatory); 68 91 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); 70 96 } 71 72 97 #endif -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/color.h
r983 r2090 14 14 } Color; 15 15 16 extern Color *initColors(GLuint num Triangles);17 extern void fillAllColors(Color *colors, GLuint num Triangles, GLuint begin, GLuint end, GLubyte color);16 extern Color *initColors(GLuint numColors); 17 extern void fillAllColors(Color *colors, GLuint numColors, GLuint begin, GLuint end, GLubyte color); 18 18 extern void setColors(Color *colors, GLuint numPasses, GLuint begin, GLuint end); 19 extern void setColors2(Color *colors, GLuint num Triangles, GLuint begin, GLuint end);20 extern void setColors3(Color *colors, GLuint num Triangles, GLubyte color);21 extern void setColors4(Color *colors, GLuint num Triangles);22 extern void viewColors(Color *colors, GLuint num Triangles);19 extern void setColors2(Color *colors, GLuint numColors, GLuint begin, GLuint end); 20 extern void setColors3(Color *colors, GLuint numColors, GLubyte color); 21 extern void setColors4(Color *colors, GLuint numColors); 22 extern void viewColors(Color *colors, GLuint numColors); 23 23 24 24 } -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/global.h
r983 r2090 8 8 #include "mesh.h" 9 9 #include "color.h" 10 #include "change.h" 10 11 11 12 // For progress update. … … 58 59 59 60 //#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 60 77 61 78 #define CHECK_OPENGL_ERROR( cmd ) \ … … 110 127 bEnableOffScreen, 111 128 bSaveLog, 112 bLoadCamerasFromFile; 129 bLoadCamerasFromFile, 130 bRemoveRedundantVertices; 113 131 114 132 extern GLsizei width, height; … … 121 139 122 140 extern void renderScene(GLuint **histogram, GLuint numCameras); 141 extern void renderSceneWin(GLuint **histogram, GLuint numCameras, Change *c); 123 142 124 143 extern void renderGeometry(void); -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/histogram.h
r983 r2090 20 20 extern void printFullHistogram(GLuint **histogram, GLuint numCameras, GLuint numTriangles); 21 21 extern void getSWHistogram(GLuint *histogram, GLubyte *pixels); 22 extern void getSWHistogramWin(GLuint *histogram, GLubyte *pixels, GLfloat min[3], GLfloat max[3], Change *c); 22 23 extern void getSWHistoByOcclusionQuery(Mesh *mesh, Color *colors, GLuint *histogram); 23 24 24 25 extern void resetSWHistogram(GLuint *histogram, GLuint numTriangles); 25 26 extern void getBoundingBox(Change *c, GLfloat min[3], GLfloat max[3]); 27 extern void getWindow(GLfloat min[3], GLfloat max[3], int minw[2], int maxw[2]); 26 28 } 27 29 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/interleave.h
r983 r2090 20 20 GLfloat x, y, z; // position 21 21 } Vertex_; 22 extern GLuint vertex_buf, color_buf; 22 23 23 24 extern void setupInterleave(Mesh *mesh, Color *colors); -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/mesh.h
r983 r2090 11 11 GLuint *triangles; // List of triangles 12 12 int enable; 13 int movable; // If vertex is part of a border. 13 14 } Vertex; 14 15 … … 22 23 typedef struct _Triangle { 23 24 GLuint id; // Triangle id 25 GLuint group; // Triangle group 24 26 GLuint submesh; // Triangle submesh 25 27 GLuint indices[3]; // Triangle vertices … … 45 47 extern void computeTriangleNormal(Vertex *vertices, Triangle *t); 46 48 49 extern Vertex *addVertex(Vertex *list, int *n, float x, float y, float z, int *pos); 47 50 extern int findEdge(Edge *e, GLuint num, GLuint _u, GLuint _v); 51 extern Edge *addEdge(Edge *list, int *n, int u, int v, int *pos); 48 52 extern GLboolean findVertex(GLfloat *vertices, GLuint num, GLfloat x, GLfloat y, GLfloat z, int *pos); 49 53 … … 55 59 56 60 extern int *trianglesAdjToEdge(Mesh *mesh, int e, int *n); 57 extern int *trianglesAdjToVertex(Mesh *mesh, int v, int *n);58 61 extern int *verticesAdjToVertex(Mesh *mesh, int v, int *n); 59 62 extern int *edgesAdjToVertex(Mesh *mesh, int v, int *n); 60 63 extern int *edgesAdjToVertices(Mesh *mesh, int *vertices, int numVertices, int *n); 61 64 65 // list of integers 62 66 extern void printItemList(int *list, int n); 63 67 extern int findItem(int *list, int n, int item); 64 68 extern void addItem(int *list, int *n, int item); 69 extern void delItem(int *list, int *n, int item); 65 70 66 71 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/metrics.h
r983 r2090 8 8 namespace VMI 9 9 { 10 extern GLuint computeBackgroundArea(Mesh *mesh, GLuint *histogram);11 extern GLdouble computeMeanProjArea(GLuint **histogram, GLuint numCameras, int t);12 10 extern GLdouble computeMI(Mesh *mesh, GLuint **histogram, GLuint numCameras, GLuint cam); 11 extern GLdouble decMI(GLdouble I, GLuint **histogram, GLuint numCameras, GLuint cam, Change *c); 12 extern GLdouble incMI(GLdouble I, GLuint **histogram, GLuint numCameras, GLuint cam, Change *c); 13 13 14 extern GLdouble computeHE(Mesh *mesh, GLuint *histogram); 14 15 extern GLdouble computeKL(Mesh *mesh, GLuint *histogram); 15 16 extern GLdouble computeCS(Mesh *mesh, GLuint *histogram); 16 17 18 extern GLdouble computeMeanProjArea(GLuint **histogram, GLuint numCameras, int t); 17 19 extern GLdouble computeJS(GLuint **histogram, GLuint numCameras, GLuint j, GLuint k); 18 20 extern GLdouble computeEntropy(GLuint **histogram, GLuint numCameras, GLuint k); … … 21 23 22 24 extern void getProjectedAreas(GLuint **histogram, GLuint numCameras); 25 extern void getProjectedAreasWin(GLuint **histogram, GLuint numCameras, Change *c); 23 26 extern void resetProjectedAreas(GLuint **histogram, GLuint numCameras); 24 27 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/saliency.h
r983 r2090 14 14 15 15 extern void computeSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras); 16 extern void updateTriangleSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras, int v); 16 17 17 18 extern double computeTriangleSaliency(Mesh *mesh, GLuint **histogram, GLuint numCameras, GLuint k); -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/simplify.h
r1007 r2090 7 7 #include "bheap.h" 8 8 9 #include <map> 10 11 using namespace std; 12 9 13 namespace VMI 10 14 { 11 extern GLdouble computeEdgeCost(Mesh *mesh, int e);15 extern GLdouble computeEdgeCost(Mesh *mesh, int e); 12 16 13 extern bheap_t *initHeap(Mesh *mesh);17 extern bheap_t *initHeap(Mesh *mesh); 14 18 15 extern bheap_t *updateHeap(bheap_t *h, Mesh *mesh, Change *c);19 extern bheap_t *updateHeap(bheap_t *h, Mesh *mesh, Change *c); 16 20 17 extern void simplifyModel(Mesh *mesh, GLuint numDemandedTri);21 extern void simplifyModel(Mesh *mesh, GLuint numDemandedTri); 18 22 19 23 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); 22 26 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); 25 28 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; 26 80 } 27 81 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/bheap.cpp
r983 r2090 154 154 } 155 155 } 156 } 157 /* bh_mark() marks an item from the binary heap pointed to by h. 158 */ 159 void 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 */ 165 double 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 */ 171 int bh_is_dirty(bheap_t *h, int item) 172 { 173 return h->a[h->p[item]].dirty; 156 174 } 157 175 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/camera.cpp
r983 r2090 174 174 175 175 switch (type) { 176 case OCTAHEDRON:177 cameras = (Camera *)malloc(sizeof(Camera) * type);176 case 0: 177 cameras = (Camera *)malloc(sizeof(Camera) * OCTAHEDRON); 178 178 if (cameras == NULL) { 179 179 fprintf(stderr, "Error allocating memory\n"); 180 180 exit(1); 181 181 } 182 *numCameras = type;182 *numCameras = OCTAHEDRON; 183 183 drawOctahedron(cameras, radius); 184 184 break; 185 case ICOSAHEDRON:186 cameras = (Camera *)malloc(sizeof(Camera) * type);185 case 1: 186 cameras = (Camera *)malloc(sizeof(Camera) * CUBE); 187 187 if (cameras == NULL) { 188 188 fprintf(stderr, "Error allocating memory\n"); 189 189 exit(1); 190 190 } 191 *numCameras = type;192 draw Icosahedron(cameras, radius);191 *numCameras = CUBE; 192 drawCube(cameras, radius); 193 193 break; 194 case DODECAHEDRON:195 cameras = (Camera *)malloc(sizeof(Camera) * type);194 case 2: 195 cameras = (Camera *)malloc(sizeof(Camera) * ICOSAHEDRON); 196 196 if (cameras == NULL) { 197 197 fprintf(stderr, "Error allocating memory\n"); 198 198 exit(1); 199 199 } 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; 201 210 drawDodecahedron(cameras, radius); 202 211 break; 203 212 default: 204 printf("Error cameras not defined\n");213 printf("Error, cameras not defined\n"); 205 214 exit(1); 206 215 break; 207 216 } 217 218 printf("Number of cameras: %d\n", *numCameras); 208 219 209 220 return cameras; … … 506 517 copyToCameras(cameras, 12, vertices); 507 518 } 519 520 void 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 6 6 #include "../include/change.h" 7 7 8 #define MAX_NUM_TRI 2509 10 8 using 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; 9 using namespace std; 10 11 Geometry::MeshSimplificationSequence *VMI::mSequence = NULL; 12 13 Change *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 34 Change *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; 31 53 } 32 54 … … 91 113 void VMI::modifyTriangle(Triangle *t, int c, int p) 92 114 { 93 94 115 if ((int)t->indices[0] == c) 95 116 t->indices[0]= p; … … 100 121 } 101 122 102 int VMI::isATriangleToModify(Triangle *t, int c , int p)123 int VMI::isATriangleToModify(Triangle *t, int c) 103 124 { 104 125 int u = t->indices[0], … … 109 130 110 131 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 triangle187 memcpy(&modified[num], &mesh->triangles[i], sizeof(Triangle));188 num++;189 }190 }191 192 return num;193 132 } 194 133 … … 211 150 } 212 151 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 } 152 void 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 177 void 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 195 int 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 205 void 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 230 void VMI::swap(int *i, int *j) { 231 int t; 232 233 t = *i; 234 *i = *j; 235 *j = t; 236 } 237 238 void 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 284 void 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 314 Triangle *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 338 Triangle *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 361 void 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 420 void 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 } 257 446 } 258 447 … … 260 449 261 450 // 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(); 451 void 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(); 311 464 } 312 465 … … 316 469 modifyTriangles(mesh, c); 317 470 471 // This function must be called after modifyTriangles() 318 472 deleteTriangles(mesh, c); 319 473 474 // Delete vertex u 320 475 mesh->vertices[c->u].enable = FALSE; 321 476 mesh->currentNumVertices--; 477 478 //printMesh(mesh); 479 //getchar(); 322 480 } 323 481 324 482 void VMI::undoChange(Mesh *mesh, Change *c) 325 { 483 { 326 484 unmodifyTriangles(mesh, c); 327 485 … … 330 488 mesh->vertices[c->u].enable = TRUE; 331 489 mesh->currentNumVertices++; 332 } 333 490 491 //printMesh(mesh); 492 //getchar(); 493 } 494 495 /* 334 496 namespace VMI{ 335 497 std::map<int, INTVECTOR> inversemap; 336 498 } 499 */ 337 500 338 501 // 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++)502 extern void VMI::saveSimplificationSequence(Change *c,int obligatory) 503 { 504 Geometry::MeshSimplificationSequence::Step step; 505 506 if (mSequence == NULL) 344 507 { 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 7 7 using namespace VMI; 8 8 9 Color *VMI::initColors(GLuint num Triangles) {9 Color *VMI::initColors(GLuint numColors) { 10 10 Color *colors; 11 11 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 */ 13 15 14 16 if (colors == NULL) { … … 17 19 } 18 20 19 / / Fill the color buffer with BACKGROUND20 memset(colors, BACKGROUND, sizeof(Color) * (num Triangles + 1));21 /* Fill the color buffer with the BACKGROUND color */ 22 memset(colors, BACKGROUND, sizeof(Color) * (numColors + 1)); 21 23 22 24 return colors; 23 25 } 24 26 25 void VMI::fillAllColors(Color *colors, GLuint num Triangles, GLuint begin, GLuint end, GLubyte color) {27 void VMI::fillAllColors(Color *colors, GLuint numColors, GLuint begin, GLuint end, GLubyte color) { 26 28 GLuint i; 27 29 28 if (end > num Triangles) return;30 if (end > numColors) return; 29 31 30 32 for (i=begin; i<end; i++) { … … 84 86 } 85 87 86 void VMI::setColors2(Color *colors, GLuint num Triangles, GLuint begin, GLuint end) {88 void VMI::setColors2(Color *colors, GLuint numColors, GLuint begin, GLuint end) { 87 89 GLuint i, 88 90 channel = 0, 89 step = (num Triangles < MAX_NUM_COLORS) ? (MAX_NUM_COLORS / numTriangles) : 1;91 step = (numColors < MAX_NUM_COLORS) ? (MAX_NUM_COLORS / numColors) : 1; 90 92 GLubyte r = 1; 91 93 92 if (end > num Triangles) return;94 if (end > numColors) return; 93 95 94 96 for (i = begin; i < end; i++) { … … 126 128 channel = 0; 127 129 } 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); 129 131 //getchar(); 130 132 } 131 133 } 132 134 133 void VMI::setColors3(Color *colors, GLuint num Triangles, GLubyte color) {135 void VMI::setColors3(Color *colors, GLuint numColors, GLubyte color) { 134 136 GLuint i; 135 137 136 for (i = 0; i < num Triangles; i++) {138 for (i = 0; i < numColors; i++) { 137 139 138 140 colors[i].r = color; … … 141 143 colors[i].a = 0; 142 144 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); 144 146 //getchar(); 145 147 } 146 148 } 147 149 148 void VMI::setColors4(Color *colors, GLuint num Triangles) {150 void VMI::setColors4(Color *colors, GLuint numColors) { 149 151 GLuint i, c = 1; 150 152 151 for (i = 0; i < num Triangles; i++) {153 for (i = 0; i < numColors; i++) { 152 154 153 155 colors[i].r = c & 0xFF; … … 156 158 colors[i].a = (c & 0xFF000000) >> 24; 157 159 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); 159 161 c++; 160 162 } … … 162 164 } 163 165 164 void VMI::viewColors(Color *colors, GLuint num Triangles) {166 void VMI::viewColors(Color *colors, GLuint numColors) { 165 167 GLuint i; 166 168 167 169 printf("\n"); 168 for (i=0; i<num Triangles; 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); 170 172 } -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/glm.c
r983 r2090 135 135 copied = 1; 136 136 for (i = 1; i <= *numvectors; i++) { 137 for (j = 1; j < =copied; j++) {137 for (j = 1; j </*=*/ copied; j++) { 138 138 if (glmEqual(&vectors[3 * i], &copies[3 * j], epsilon)) { 139 139 goto duplicate; … … 1723 1723 vectors = model->vertices; 1724 1724 copies = glmWeldVectors(vectors, &numvectors, epsilon); 1725 1726 #if 01725 1726 //#if 0 1727 1727 printf("glmWeld(): %d redundant vertices.\n", 1728 model->numvertices - numvectors - 1);1729 #endif1728 model->numvertices - numvectors /* - 1*/); 1729 //#endif 1730 1730 1731 1731 for (i = 0; i < model->numtriangles; i++) { -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/histogram.cpp
r983 r2090 9 9 #include "../include/histogram.h" 10 10 #include "../include/metrics.h" 11 12 #define WIN_MARGIN 2 11 13 12 14 using namespace VMI; … … 185 187 } 186 188 189 void 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 251 void 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 324 void 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 187 381 void VMI::getSWHistoByOcclusionQuery(Mesh *mesh, Color *colors, GLuint *histogram) { 188 382 GLuint i, v1, v2, v3; 189 GLint area ;383 GLint area, totalArea = 0; 190 384 191 385 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); … … 196 390 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queries[i]); 197 391 198 / / Render triangle i392 /* Render triangle i */ 199 393 glBegin(GL_TRIANGLES); 200 394 v1= mesh->triangles[i].indices[0]; … … 226 420 } else { 227 421 histogram[i + 1] = area; 422 423 totalArea += area; 228 424 } 229 425 } 230 426 } 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]); 233 432 } 234 433 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/interleave.cpp
r983 r2090 17 17 static Color *buf_colors = NULL; 18 18 19 staticGLuint vertex_buf = 0,19 GLuint vertex_buf = 0, 20 20 color_buf = 0; 21 21 } … … 100 100 setupVertexArray(mesh, colors); 101 101 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);109 102 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); 110 103 glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data 111 104 112 glDeleteBuffersARB(1, &color_buf);113 114 glGenBuffersARB(1, &color_buf);115 105 glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); 116 106 glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->currentNumTriangles * 3 * sizeof(Color), buf_colors, GL_STATIC_DRAW_ARB); //upload data … … 182 172 updateVertexArray(mesh); 183 173 184 // Disable the vertex array functionality:185 glDisableClientState(GL_VERTEX_ARRAY);186 187 glDeleteBuffersARB(1, &vertex_buf);188 189 glGenBuffersARB(1, &vertex_buf);190 174 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); 191 175 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 25 25 bBeQuiet = GL_FALSE, 26 26 bSaveLog = GL_FALSE, 27 bLoadCamerasFromFile = GL_FALSE; 27 bLoadCamerasFromFile = GL_FALSE, 28 bRemoveRedundantVertices = GL_TRUE; 28 29 29 30 GLuint cameraType = 0, … … 55 56 gluPerspective(fov, (GLdouble)width / height, 0.1, 40.0); 56 57 57 glPixelStorei(GL_PACK_ALIGNMENT, 1);58 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);58 //glPixelStorei(GL_PACK_ALIGNMENT, 1); 59 //glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 59 60 60 61 glShadeModel(GL_FLAT); … … 62 63 glDisable(GL_BLEND); 63 64 64 //glEnable(GL_CULL_FACE); // important65 glEnable(GL_CULL_FACE); // important 65 66 66 67 glEnable(GL_DEPTH_TEST); 67 glDepthFunc(GL_LEQUAL);68 68 69 69 if (GL_TRUE != glewIsSupported((const char*) "GL_EXT_framebuffer_object")) … … 117 117 { 118 118 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 119 126 } 120 127 if (GL_TRUE != glewIsSupported((const char*) "GL_ARB_occlusion_query")) … … 238 245 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 239 246 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(); 250 248 251 249 glEnable(GL_TEXTURE_2D); 252 250 glBindTexture(GL_TEXTURE_2D, color_tex); 253 251 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 258 258 glEnd(); 259 259 glFlush(); … … 312 312 /////////////////////////////////////////////////////////////////////////// 313 313 } 314 void 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 } 314 391 315 392 void VMI::freeMemory(void) { … … 339 416 #endif 340 417 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; 342 446 } 343 447 … … 391 495 392 496 #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); 405 498 #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); 418 500 #endif 419 501 … … 454 536 * RGBA display mode, and handle input events. 455 537 */ 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 11 11 using namespace VMI; 12 12 13 #define MAX_NUM_TRI 450 14 13 #define OUTPUT_SEVERAL_GROUPS 14 15 /////////////////////////////////////////////////////////////////////////////// 16 // list of integers 15 17 void VMI::printItemList(int *list, int n) { 16 18 int i; … … 35 37 list[(*n)++] = item; 36 38 } 39 40 void 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 /////////////////////////////////////////////////////////////////////////////// 37 54 38 55 int VMI::findEdge(Edge *e, GLuint num, GLuint _u, GLuint _v) { … … 56 73 57 74 return found; 75 } 76 77 // Adds a new edge at the end of the mesh edge list 78 Edge *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; 58 102 } 59 103 … … 131 175 Mesh *VMI::initMesh(GLMmodel* pmodel) { 132 176 133 GLuint i, j, v1, v2, v3, n, e, t; 177 GLuint i, j, curGroup, v1, v2, v3, n, m, t; 178 int e; 134 179 Mesh *mesh; 180 GLMgroup *group; 135 181 136 182 mesh = (Mesh *)malloc (sizeof(Mesh)); … … 157 203 printf("Adding vertices..."); 158 204 for (i=1; i<=pmodel->numvertices; i++) { // Vertices start at 1 159 160 205 mesh->vertices[i - 1].x = pmodel->vertices[3 * i + 0]; 161 206 mesh->vertices[i - 1].y = pmodel->vertices[3 * i + 1]; … … 170 215 171 216 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 172 254 for (i=0; i<pmodel->numtriangles; i++) { 173 255 mesh->triangles[i].id = i; … … 188 270 189 271 // 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)); 191 274 addItem((int *)mesh->vertices[v1].triangles, (int *)&mesh->vertices[v1].numTriangles, i); 192 275 } 193 276 } 194 //printf("\n"); 277 printf("\n"); 278 #endif 195 279 printf("Ok\n"); 196 280 197 281 mesh->numTriangles = pmodel->numtriangles; 198 282 mesh->currentNumTriangles = pmodel->numtriangles; 283 printf("Number of triangles: %d\n", mesh->numTriangles); 199 284 200 285 mesh->edges = (Edge *)malloc (sizeof(Edge) * mesh->numTriangles * 3); // E = 3 T / 2 … … 205 290 } 206 291 207 // Init edges208 for (i=0; i<mesh->numTriangles * 3; i++) {209 210 mesh->edges[i].triangles = NULL;211 mesh->edges[i].numTriangles = 0;212 }213 214 292 printf("Adding edges..."); 215 293 n = 0; … … 221 299 v3 = mesh->triangles[i].indices[2]; 222 300 301 ///////////////////////////////////////////////////////////////////////////////// 302 // edge (v1,v2) 223 303 if ((e = findEdge(mesh->edges, n, v1, v2)) == -1) { 224 304 mesh->edges[n].u = v1; 225 305 mesh->edges[n].v = v2; 306 mesh->edges[n].triangles = NULL; 307 mesh->edges[n].numTriangles = 0; 226 308 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; 236 310 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) 247 324 if (( e = findEdge(mesh->edges, n, v2, v3)) == -1) { 248 325 mesh->edges[n].u = v2; 249 326 mesh->edges[n].v = v3; 327 mesh->edges[n].triangles = NULL; 328 mesh->edges[n].numTriangles = 0; 250 329 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; 260 331 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) 271 345 if ((e = findEdge(mesh->edges, n, v3, v1)) == -1) { 272 346 mesh->edges[n].u = v3; 273 347 mesh->edges[n].v = v1; 348 mesh->edges[n].triangles = NULL; 349 mesh->edges[n].numTriangles = 0; 274 350 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; 284 352 n++; 285 } else {286 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 i293 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); 295 363 } 296 364 printf("Ok\n"); … … 322 390 printf("------------------------\n"); 323 391 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); 325 393 printf(" t(%d)[", mesh->edges[i].numTriangles); 326 394 if (mesh->edges[i].triangles != NULL) { … … 329 397 printf(",%d", mesh->edges[i].triangles[j]); 330 398 } 331 399 } 332 400 printf("]\n"); 333 401 } … … 336 404 printf("------------------------\n"); 337 405 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); 340 408 printf(" e(3)["); 341 409 printf("%d", mesh->triangles[i].edges[0]); … … 385 453 386 454 int *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, 389 456 u = mesh->edges[e].u, 390 457 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]; 400 474 401 475 if ((n0 == v) || (n1 == v) || (n2 == v) || 402 476 (n0 == u) || (n1 == u) || (n2 == u)) 403 477 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 } 416 498 *n = num; 417 499 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; 451 501 } 452 502 453 503 int *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; 457 505 int i, t, n0, n1, n2; 458 506 int num = 0; 459 507 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]; 462 513 463 514 n0 = mesh->triangles[t].indices[0]; … … 469 520 if (n2 != v) addItem(vertices, &num, n2); 470 521 } 471 472 522 *n = num; 473 523 474 free(triangles);475 476 524 return vertices; 477 525 } 478 526 479 527 int *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 484 535 int num = 0; 485 536 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 489 553 n0 = mesh->triangles[t].indices[0]; 490 554 n1 = mesh->triangles[t].indices[1]; … … 498 562 499 563 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 } 503 568 *n = num; 504 569 505 free(triangles);506 507 570 return edges; 508 571 } 509 572 510 573 int *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); 525 586 526 587 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 } 537 607 *n = num; 538 608 539 return list;609 return edges; 540 610 } 541 611 … … 563 633 } 564 634 635 // Adds a new vertex at the end of the mesh vertex list 636 Vertex *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 565 665 void VMI::updateModel(GLMmodel* pmodel, Mesh *mesh, GLuint numVertices, GLuint numTriangles) { 566 666 GLuint i , v1, v2 ,v3, numV = 1, numT = 0; … … 569 669 570 670 if (pmodel->vertices != NULL) free(pmodel->vertices); 571 572 671 pmodel->vertices = (GLfloat *)calloc ((numVertices + 1) * 3, sizeof(GLfloat)); 573 574 672 575 673 if (pmodel->triangles != NULL) free(pmodel->triangles); 576 577 674 pmodel->triangles = (GLMtriangle *)malloc (numTriangles * sizeof(GLMtriangle)); 578 675 579 580 676 for (i=0; i<mesh->numTriangles; i++) { 581 677 if (mesh->triangles[i].enable) { … … 630 726 631 727 void 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 634 766 // The output model contains only one group 635 767 pmodel->numgroups = 1; … … 646 778 for (i=0; i<mesh->currentNumTriangles; i++) 647 779 pmodel->groups->triangles[i] = i; 780 #endif 648 781 649 782 updateModel(pmodel, mesh, mesh->currentNumTriangles * 3, mesh->currentNumTriangles); -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/metrics.cpp
r983 r2090 13 13 using namespace VMI; 14 14 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 119 if (mesh->triangles[i].enable == TRUE) {20 area += histogram[i + 1];21 }22 }23 24 return ((GLuint)(width * height) - area);25 }26 27 15 GLdouble VMI::computeMeanProjAreaNoBG(GLuint **histogram, GLuint numCameras, int t) { 28 16 GLuint i; … … 37 25 38 26 GLdouble VMI::computeMeanProjArea(GLuint **histogram, GLuint numCameras, int t) { 39 40 41 42 27 GLuint i; 28 GLdouble mean_proj_area = 0.0, total_proj_area, resolution = width * height; 29 30 for (i=0; i<numCameras; i++) { 43 31 total_proj_area = resolution - histogram[i][0]; 44 32 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 /////////////////////////////////////////////////////////////////////////////// 50 39 // Mutual Information 51 40 GLdouble VMI::computeMI(Mesh *mesh, GLuint **histogram, GLuint numCameras, GLuint cam) { … … 76 65 return (I / numCameras); 77 66 } 67 68 GLdouble 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 111 GLdouble 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 /////////////////////////////////////////////////////////////////////////////// 78 141 // Kullback-Leibler divergence 79 142 GLdouble VMI::computeKL(Mesh *mesh, GLuint *histogram) { … … 107 170 // Hellinger divergence (square root) 108 171 GLdouble VMI::computeHE(Mesh *mesh, GLuint *histogram) { 109 GLdouble I = 0.0, t ,172 GLdouble I = 0.0, temp, 110 173 total_real_area = 0.0, pi, qi; 111 174 GLsizei total_proj_area = (width * height) - histogram[0]; … … 128 191 if ((pi > 0.0) && (qi > 0.0)) { 129 192 130 t = sqrt(pi) - sqrt(qi);193 temp = sqrt(pi) - sqrt(qi); 131 194 132 I += (t * t);195 I += (temp * temp); 133 196 } 134 197 } 135 198 } 136 199 137 return (sqrt(I / 2.0)); 200 return sqrt(I / 2.0); 201 138 202 } 139 203 … … 168 232 /////////////////////////////////////////////////////////////////////////////// 169 233 GLdouble 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 255 GLdouble VMI::computeMixedEntropy(GLdouble *mixed, GLuint numCameras) { 256 GLdouble H = 0.0, pi; 172 257 GLuint i; 173 258 174 259 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]; 183 262 184 263 if (pi > 0.0) … … 188 267 return -H; 189 268 } 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 }205 269 /////////////////////////////////////////////////////////////////////////////// 206 270 // Jensen-Shannon divergence 207 271 GLdouble VMI::computeJS(GLuint **histogram, GLuint numCameras, GLuint j, GLuint k) { 208 209 210 *mixing = (GLdouble *)malloc(numCameras * sizeof(GLdouble));211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 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 229 293 total_proj_area = resolution - histogram[i][0]; 230 294 231 232 233 234 235 236 237 238 239 240 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))); 243 307 244 308 free(mixing); 245 309 246 310 return js; 247 311 } 248 312 /////////////////////////////////////////////////////////////////////////////// … … 250 314 251 315 renderScene(histogram, numCameras); 316 } 317 318 void VMI::getProjectedAreasWin(GLuint **histogram, GLuint numCameras, Change *c) { 319 320 renderSceneWin(histogram, numCameras, c); 252 321 } 253 322 … … 301 370 meanI /= numCameras; 302 371 #endif 303 printIs(mis, numCameras);372 //printIs(mis, numCameras); 304 373 printf("I0= %f\n", meanI); 305 374 } -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/saliency.cpp
r983 r2090 23 23 24 24 void 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 48 void 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 } 46 57 } 47 58 48 59 double 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; 51 62 double sal = 0.0; 52 63 … … 55 66 v2 = mesh->triangles[k].indices[2]; 56 67 68 /* Allocating memory */ 69 triangles = (int *)malloc((mesh->vertices[v0].numTriangles + 70 mesh->vertices[v1].numTriangles + 71 mesh->vertices[v2].numTriangles) * sizeof(int)); 72 57 73 for(i=0; i<mesh->vertices[v0].numTriangles; i++) { 58 74 l = mesh->vertices[v0].triangles[i]; … … 86 102 //getchar(); 87 103 104 free(triangles); 105 88 106 return (sal /*/ n*/); 89 107 } … … 150 168 if((fp= fopen(filename, "rt"))== NULL) { 151 169 printf("Can't open file %s\n", filename); 152 exit(1); 170 getchar(); 171 exit(1); 153 172 } 154 173 … … 219 238 void VMI::computeRGB(double min, double max,double value,float *r,float *g,float *b) 220 239 { 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 } 252 273 } 253 274 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/simplify.cpp
r1007 r2090 9 9 #include "../include/simplify.h" 10 10 #include "../include/saliency.h" 11 #include "../include/histogram.h" 11 12 12 13 //#define RECOMPUTE_THE_WHOLE_HEAP 13 //#define MAKE_FULL_COPY14 14 15 15 using namespace VMI; 16 using namespace std; 17 18 // Multimap to store current vertex in a simplification state. 19 typedef multimap<_float3_, int> t_map; 20 typedef t_map::iterator t_map_str; 21 typedef t_map::value_type t_pair; 22 t_map mVertexMap; 23 multimap<int,int> mVertices; 24 25 vector<Geometry::Vector3> VMI::vPositions; 26 vector<Geometry::Vector3> VMI::vNormals; 27 vector<Geometry::Vector2> VMI::vTexCoords; 16 28 17 29 /////////////////////////////////////////////////////////////////////////////// 18 30 19 31 void 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 } 44 58 } 45 59 46 60 bheap_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"); 97 91 98 92 #ifdef DEBUG 99 100 101 #endif 102 103 93 bh_mydump(mesh, h); 94 //getchar(); 95 #endif 96 97 return h; 104 98 } 105 99 106 100 bheap_t *VMI::updateHeap(bheap_t *h, Mesh *mesh, Change *c) { 107 int e, i, n0, n1, n2, u, v;101 int e, i; 108 102 #ifdef RECOMPUTE_THE_WHOLE_HEAP 109 bheap_t *nh; 103 bheap_t *nh; 104 double cost; 110 105 #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 178 108 179 109 #ifdef RECOMPUTE_THE_WHOLE_HEAP 180 110 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; 207 127 208 128 #else 209 129 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); 252 146 #endif 253 147 254 148 #ifdef DEBUG 255 256 257 #endif 258 259 149 bh_mydump(mesh, h); 150 //getchar(); 151 #endif 152 153 return h; 260 154 } 261 155 262 156 GLdouble VMI::computeEdgeLength(Vertex *vertices, int u, int v) { 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 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)); 278 172 } 279 173 280 174 /////////////////////////////////////////////////////////////////////////////// 281 175 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 290 176 void 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); 362 242 } 363 243 /////////////////////////////////////////////////////////////////////////////// 364 244 245 void 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 278 void 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 365 313 GLdouble 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); 375 349 // 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 389 357 #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++) { 394 397 395 398 #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]); 400 400 #endif 401 401 #ifdef HE // Hellinger 402 402 newI = computeHE(mesh, histogram[i]); 403 403 #endif 404 404 #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 */ 427 int 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 */ 444 int 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 /////////////////////////////////////////////////////////////////////////// 426 475 void VMI::simplifyModel(Mesh *mesh, GLuint numDemandedTri) 427 476 { … … 444 493 { 445 494 #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); 458 496 #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); 471 498 #endif 472 499 … … 475 502 /* open the file */ 476 503 file = fopen(s, "a+"); 477 504 478 505 if (!file) 479 506 { … … 485 512 // Open file of simplification sequence. 486 513 file_simp_seq = fopen("SimplifSequence.txt", "w"); 487 514 488 515 if (!file_simp_seq) 489 516 { 490 517 fprintf(stderr, 491 492 493 } 494 518 "simplifyModel() failed: ", 519 "can't open file \"SimplifSequence\" to write.\n"); 520 } 521 495 522 h = initHeap(mesh); 496 523 … … 500 527 { 501 528 // Get the edge that has the minimum cost 502 e = bh_min(h); 529 //e = getMinEdge(mesh, h); 530 e = extractValidEdge(mesh, h); 503 531 504 532 c = createChange(mesh, e); 505 533 506 534 // Apply the edge collapse 507 computeChanges(mesh, c);508 535 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 509 542 510 543 if (bSaveLog == TRUE) writeChange(file, c); 511 544 512 // Write Simplification sequence.513 saveSimplificationSequence(c);514 515 545 cost = h->a[h->p[e]].key; 516 546 547 /* 517 548 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 */ 524 556 525 557 mesh->edges[e].enable = FALSE; 526 527 558 bh_delete(h, e); 528 559 … … 532 563 computeCameraIs(histogram, numCameras, initialIs); 533 564 565 deleteEdges(mesh, c); 566 modifyEdges(mesh, c); 567 534 568 // Update the heap according to the edge collapse 535 569 h = updateHeap(h, mesh, c); … … 539 573 deleteChange(c); 540 574 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; 543 582 544 583 if (bSaveLog == TRUE) … … 554 593 } 555 594 595 /////////////////////////////////////////////////////////////////////////////// 596 // Extract a correct edge of the heap. 597 /////////////////////////////////////////////////////////////////////////////// 598 int 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 /////////////////////////////////////////////////////////////////////////////// 610 int 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 //------------------------------------------------------------------------- 642 void 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 //------------------------------------------------------------------------- 674 void 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 //------------------------------------------------------------------------- 720 bool 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 //------------------------------------------------------------------------- 739 void 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 //------------------------------------------------------------------------- 865 void 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.