[983] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | |
---|
| 4 | #include <math.h> |
---|
| 5 | |
---|
| 6 | #include "../include/saliency.h" |
---|
| 7 | #include "../include/global.h" |
---|
| 8 | #include "../include/simplify.h" |
---|
| 9 | |
---|
| 10 | #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) |
---|
| 11 | |
---|
| 12 | #define MAX_NUM_TRI 450 |
---|
| 13 | |
---|
| 14 | namespace VMI |
---|
| 15 | { |
---|
| 16 | double maxSal = 0.0, minSal = 0.0; |
---|
| 17 | int alpha = 30; // 30th percentile |
---|
| 18 | int lambda = /*100*/1; |
---|
| 19 | double percentile = 0.0; // percentile |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | using namespace VMI; |
---|
| 23 | |
---|
| 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); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | 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; |
---|
| 51 | double sal = 0.0; |
---|
| 52 | |
---|
| 53 | v0 = mesh->triangles[k].indices[0]; |
---|
| 54 | v1 = mesh->triangles[k].indices[1]; |
---|
| 55 | v2 = mesh->triangles[k].indices[2]; |
---|
| 56 | |
---|
| 57 | for(i=0; i<mesh->vertices[v0].numTriangles; i++) { |
---|
| 58 | l = mesh->vertices[v0].triangles[i]; |
---|
| 59 | |
---|
| 60 | if (l != k) |
---|
| 61 | addItem(triangles, &n, l); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | for(i=0; i<mesh->vertices[v1].numTriangles; i++) { |
---|
| 65 | l = mesh->vertices[v1].triangles[i]; |
---|
| 66 | |
---|
| 67 | if (l != k) |
---|
| 68 | addItem(triangles, &n, l); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | for(i=0; i<mesh->vertices[v2].numTriangles; i++) { |
---|
| 72 | l = mesh->vertices[v2].triangles[i]; |
---|
| 73 | |
---|
| 74 | if (l != k) |
---|
| 75 | addItem(triangles, &n, l); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | //printItemList(triangles, n); |
---|
| 79 | |
---|
| 80 | for(i=0; i<(GLuint)n; i++) { |
---|
| 81 | //printf("\n%d %d", k, triangles[i]); |
---|
| 82 | //sal += computeJS(histogram, numCameras, k, triangles[i]); |
---|
| 83 | sal = MAX ( sal, computeJS(histogram, numCameras, k, triangles[i])); |
---|
| 84 | } |
---|
| 85 | //printf("\nT%d Sal: %f\n", k, (sal / n)); |
---|
| 86 | //getchar(); |
---|
| 87 | |
---|
| 88 | return (sal /*/ n*/); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | double VMI::computeEdgeSaliency(Mesh *mesh, Change *c, double p) { |
---|
| 92 | int i, t; |
---|
| 93 | double sal, edgeSal = 0.0; |
---|
| 94 | |
---|
| 95 | // edge saliency |
---|
| 96 | for(i=0; i<c->numDel; i++) { |
---|
| 97 | |
---|
| 98 | t = c->deleted[i].id; |
---|
| 99 | |
---|
| 100 | sal = mesh->triangles[t].saliency; |
---|
| 101 | |
---|
| 102 | //printf("\nt%d: osal: %f\n",t, mesh->triangles[t].saliency); |
---|
| 103 | |
---|
| 104 | // weight map derived from saliency map |
---|
| 105 | if (sal>= p) sal *= lambda; |
---|
| 106 | |
---|
| 107 | //printf("\nt%d: nsal: %f\n",t, sal); |
---|
| 108 | |
---|
| 109 | edgeSal += sal; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | //printf("\ne:%d sal: %f\n",c->e, edgeSal); |
---|
| 113 | |
---|
| 114 | //getchar(); |
---|
| 115 | |
---|
| 116 | return edgeSal; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void VMI::saveSaliencyMap(Mesh *mesh, char* filename) { |
---|
| 120 | FILE *fp; |
---|
| 121 | GLuint i; |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | if((fp= fopen(filename, "wt"))== NULL) { |
---|
| 125 | printf("Can't open file %s\n", filename); |
---|
| 126 | exit(1); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | printf("Writing file %s\n", filename); |
---|
| 130 | |
---|
| 131 | fprintf (fp, "%d\n", mesh->numTriangles); |
---|
| 132 | //printf("%d\n", num); |
---|
| 133 | |
---|
| 134 | for (i=0; i<mesh->numTriangles; i++) { |
---|
| 135 | |
---|
| 136 | fprintf(fp, "t%d %f\n", i, mesh->triangles[i].saliency); |
---|
| 137 | |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | fclose(fp); |
---|
| 141 | printf("File writen.\n"); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | void VMI::loadSaliencyMap(Mesh *mesh, char* filename) { |
---|
| 145 | FILE *fp; |
---|
| 146 | unsigned int i, num; |
---|
| 147 | float s; |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | if((fp= fopen(filename, "rt"))== NULL) { |
---|
| 151 | printf("Can't open file %s\n", filename); |
---|
| 152 | exit(1); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | printf("Reading file %s\n", filename); |
---|
| 156 | |
---|
| 157 | maxSal = minSal = 0.0; |
---|
| 158 | |
---|
| 159 | // Reading number of triangles |
---|
| 160 | fscanf (fp, "%d\n", &num); |
---|
| 161 | //printf("%d\n", num); |
---|
| 162 | |
---|
| 163 | while (!feof(fp)) { |
---|
| 164 | |
---|
| 165 | fscanf(fp, "t%d %f\n", &i, &s); |
---|
| 166 | |
---|
| 167 | if (s > maxSal) maxSal = s; |
---|
| 168 | if (s < minSal) minSal = s; |
---|
| 169 | |
---|
| 170 | // Save the saliency into the triangle i |
---|
| 171 | mesh->triangles[i].saliency = s; |
---|
| 172 | |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | fclose(fp); |
---|
| 176 | printf("File read.\n"); |
---|
| 177 | |
---|
| 178 | printf("\nMax Sal: %f Min Sal: %f\n", maxSal, minSal); |
---|
| 179 | |
---|
| 180 | percentile = computePercentile(alpha, mesh->triangles, mesh->numTriangles); |
---|
| 181 | printf("\n%dth percentile: %f\n", alpha, percentile); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | void VMI::viewSaliency(Mesh *mesh, Camera *cameras, int cam) { |
---|
| 185 | GLuint i, v1, v2, v3; |
---|
| 186 | float r, g, b; |
---|
| 187 | |
---|
| 188 | // Clear color and depth buffers |
---|
| 189 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 190 | |
---|
| 191 | glMatrixMode(GL_MODELVIEW); |
---|
| 192 | glLoadIdentity(); |
---|
| 193 | |
---|
| 194 | // Camera i |
---|
| 195 | gluLookAt(cameras[cam].eyeX, cameras[cam].eyeY, cameras[cam].eyeZ, |
---|
| 196 | cameras[cam].centerX, cameras[cam].centerY, cameras[cam].centerZ, |
---|
| 197 | cameras[cam].upX, cameras[cam].upY, cameras[cam].upZ); |
---|
| 198 | |
---|
| 199 | // Immediate mode |
---|
| 200 | glBegin (GL_TRIANGLES); |
---|
| 201 | for (i=0; i<mesh->numTriangles; i++) { |
---|
| 202 | if (mesh->triangles[i].enable == TRUE) { |
---|
| 203 | |
---|
| 204 | v1= mesh->triangles[i].indices[0]; |
---|
| 205 | v2= mesh->triangles[i].indices[1]; |
---|
| 206 | v3= mesh->triangles[i].indices[2]; |
---|
| 207 | |
---|
| 208 | computeRGB(minSal, maxSal, mesh->triangles[i].saliency, &r, &g, &b); |
---|
| 209 | |
---|
| 210 | glColor3f(r, g, b); |
---|
| 211 | glVertex3f(mesh->vertices[v1].x, mesh->vertices[v1].y, mesh->vertices[v1].z); |
---|
| 212 | glVertex3f(mesh->vertices[v2].x, mesh->vertices[v2].y, mesh->vertices[v2].z); |
---|
| 213 | glVertex3f(mesh->vertices[v3].x, mesh->vertices[v3].y, mesh->vertices[v3].z); |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | glEnd(); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | void VMI::computeRGB(double min, double max,double value,float *r,float *g,float *b) |
---|
| 220 | { |
---|
| 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 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | int VMI::compare(const void *arg1, const void *arg2) |
---|
| 255 | { |
---|
| 256 | if(*(double *)arg1 < *(double *)arg2) return -1; |
---|
| 257 | else if(*(double *)arg1 > *(double *)arg2) return 1; |
---|
| 258 | else return 0; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | int VMI::isDecimal(double value) { |
---|
| 262 | long r = round(value); |
---|
| 263 | |
---|
| 264 | //printf ("%d\n",r); |
---|
| 265 | |
---|
| 266 | if ((double)r != value) return TRUE; |
---|
| 267 | else return FALSE; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | double VMI::computePercentile(int q, Triangle *triangles, int numTriangles) { |
---|
| 271 | double p = (numTriangles * q) / 100.0; |
---|
| 272 | double *list = (double *)malloc(numTriangles * sizeof(double)), l, per = 0.0; |
---|
| 273 | int i; |
---|
| 274 | |
---|
| 275 | //printf("%f\n",p); |
---|
| 276 | |
---|
| 277 | // filling the list |
---|
| 278 | for(i = 0; i < numTriangles; i++) |
---|
| 279 | list[i] = triangles[i].saliency; |
---|
| 280 | |
---|
| 281 | /* Quick-Sort */ |
---|
| 282 | qsort(list, numTriangles, sizeof(list[0]), compare); |
---|
| 283 | |
---|
| 284 | /* showing the sorted list */ |
---|
| 285 | /*for(i = 0; i < numTriangles; i++) |
---|
| 286 | printf("%f\n", list[i]); |
---|
| 287 | printf("\n");*/ |
---|
| 288 | |
---|
| 289 | if (isDecimal(p) == TRUE) per = list[(int)p + 1]; |
---|
| 290 | else { |
---|
| 291 | l = (list[(int)p + 1] - list[(int)p]) * (q /100.0); |
---|
| 292 | |
---|
| 293 | per = list[(int)p] + l; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | free(list); |
---|
| 297 | |
---|
| 298 | return per; |
---|
| 299 | } |
---|