source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/saliency.cpp @ 2323

Revision 2323, 6.8 KB checked in by gumbau, 17 years ago (diff)
Line 
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
14namespace VMI
15{
16double maxSal = 0.0, minSal = 0.0;
17int alpha = 30; // 30th percentile
18int lambda = /*100*/1;
19double percentile = 0.0; // percentile
20}
21
22using namespace VMI;
23
24void VMI::computeSaliency(Mesh *mesh, int **histogram, int numCameras) {
25        int i = 0;
26        double sal;
27
28        maxSal = minSal = computeTriangleSaliency(mesh, histogram, numCameras, 0);
29
30        mesh->triangles[0].saliency = maxSal;
31
32        for (i=1; i<mesh->numTriangles; i++) {
33
34                sal = computeTriangleSaliency(mesh, histogram, numCameras, i);
35
36                if (sal > maxSal) maxSal = sal;
37                if (sal < minSal) minSal = sal;
38
39                mesh->triangles[i].saliency = sal;
40        }
41
42        printf("\nMax Sal: %f  Min Sal: %f\n", maxSal, minSal);
43
44        percentile = computePercentile(alpha, mesh->triangles, mesh->numTriangles);
45        printf("\n%dth percentile: %f\n", alpha, percentile);
46}
47
48void updateTriangleSaliency(Mesh *mesh, int **histogram, int 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        }
57}
58
59double VMI::computeTriangleSaliency(Mesh *mesh, int **histogram, int numCameras, int k) {
60    int i, j, t, v0;
61    int n = 0;
62    double sal = 0.0;
63   
64    for (j=0; j<3; j++) {
65       
66        v0 = mesh->triangles[k].indices[j];
67       
68        for(i=0; i<mesh->vertices[v0].numTriangles; i++) {
69            t = mesh->vertices[v0].triangles[i];
70           
71            if (t != k) {
72                //printf("\n%d %d", k, triangles[i]);
73                //sal += computeJS(histogram, numCameras, k, t);
74                sal = MAX(sal, computeJS(histogram, numCameras, k, t));
75               
76                n++;
77            }
78        }
79    }
80    //printf("\nT%d Sal: %f\n", k, (sal / n));
81    //getchar();
82   
83    return (sal /*/ n*/);
84}
85
86double VMI::computeEdgeSaliency(Mesh *mesh, Change *c, double p) {
87    int i, t;
88    double sal, edgeSal = 0.0;
89   
90    // edge saliency
91    for(i=0; i<c->numDel; i++) {
92
93        t = c->deleted[i].id;
94
95        sal = mesh->triangles[t].saliency;
96
97        //printf("\nt%d: osal: %f\n",t, mesh->triangles[t].saliency);
98
99        // weight map derived from saliency map
100        if (sal>= p) sal *= lambda;
101
102        //printf("\nt%d: nsal: %f\n",t, sal);
103
104        edgeSal += sal;
105    }
106   
107    //printf("\ne:%d sal: %f\n",c->e, edgeSal);
108
109    //getchar();
110
111    return edgeSal;
112}
113
114void VMI::saveSaliencyMap(Mesh *mesh, char* filename) {
115    FILE *fp;
116    int i;
117
118
119    if((fp= fopen(filename, "wt"))== NULL) {
120        printf("Can't open file %s\n", filename);
121        exit(1);
122    }
123
124    printf("Writing file %s\n", filename);
125
126    fprintf (fp, "%d\n", mesh->numTriangles);
127    //printf("%d\n", num);
128
129    for (i=0; i<mesh->numTriangles; i++) {
130
131        fprintf(fp, "t%d %f\n",  i, mesh->triangles[i].saliency);
132
133    }
134
135    fclose(fp);
136    printf("File writen.\n");
137}
138
139void VMI::loadSaliencyMap(Mesh *mesh, char* filename) {
140    FILE *fp;
141    int i, num;
142    float s;
143
144
145    if((fp= fopen(filename, "rt"))== NULL) {
146        printf("Can't open file %s\n", filename);
147                                getchar();
148                                exit(1);
149    }
150
151    printf("Reading file %s\n", filename);
152
153    maxSal = minSal = 0.0;
154
155    // Reading number of triangles
156    fscanf (fp, "%d\n", &num);
157    //printf("%d\n", num);
158
159    while (!feof(fp)) {
160
161        fscanf(fp, "t%d %f\n",  &i, &s);
162
163        if (s > maxSal) maxSal = s;
164        if (s < minSal) minSal = s;
165
166        // Save the saliency into the triangle i
167        mesh->triangles[i].saliency = s;
168
169    }
170
171    fclose(fp);
172    printf("File read.\n");
173
174    printf("\nMax Sal: %f  Min Sal: %f\n", maxSal, minSal);
175
176    percentile = computePercentile(alpha, mesh->triangles, mesh->numTriangles);
177    printf("\n%dth percentile: %f\n", alpha, percentile);
178}
179
180void VMI::viewSaliency(Mesh *mesh, Camera *cameras, int cam) {
181    int i, v1, v2, v3;
182    float r, g, b;
183   
184    // Clear color and depth buffers
185    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
186   
187    glMatrixMode(GL_MODELVIEW);
188    glLoadIdentity();
189   
190    // Camera  i
191    gluLookAt(cameras[cam].eyeX, cameras[cam].eyeY, cameras[cam].eyeZ,
192              cameras[cam].centerX, cameras[cam].centerY, cameras[cam].centerZ,
193              cameras[cam].upX, cameras[cam].upY, cameras[cam].upZ);
194   
195    // Immediate mode
196    glBegin (GL_TRIANGLES);
197    for (i=0; i<mesh->numTriangles; i++) {
198        if (mesh->triangles[i].enable == TRUE) {
199           
200            v1= mesh->triangles[i].indices[0];
201            v2= mesh->triangles[i].indices[1];
202            v3= mesh->triangles[i].indices[2];
203           
204            computeRGB(minSal, maxSal, mesh->triangles[i].saliency, &r, &g, &b);
205           
206            glColor3f(r, g, b);
207            glVertex3f(mesh->vertices[v1].x, mesh->vertices[v1].y, mesh->vertices[v1].z);
208            glVertex3f(mesh->vertices[v2].x, mesh->vertices[v2].y, mesh->vertices[v2].z);
209            glVertex3f(mesh->vertices[v3].x, mesh->vertices[v3].y, mesh->vertices[v3].z);
210        }
211    }
212    glEnd();
213}
214
215void VMI::computeRGB(double min, double max,double value,float *r,float *g,float *b)
216{
217        /* Given the maximum, the minimum value and the demanded color value returns
218         * the respective RGB color
219         */
220        if(value>max) value=max;
221        if(value<min) value=min;
222
223        if(max==min) value=1;
224        else value=(float)(value-min)/(float)(max-min);
225
226        if (value<=0.25)
227        {
228                (*r)=0;
229                (*g)=4*value;
230                (*b)=1;
231        }
232        else if (value<=0.50)
233        {
234                (*r)=0;
235                (*g)=1;
236                (*b)=2-4*value;
237        }
238        else if (value<=0.75)
239        {
240                (*r)=4*value-2;
241                (*g)=1;
242                (*b)=0;
243        }
244        else
245        {
246                (*r)=1;
247                (*g)=4-4*value;
248                (*b)=0;
249        }
250}
251
252int VMI::compare(const void *arg1, const void *arg2)
253{
254    if(*(double *)arg1 < *(double *)arg2) return -1;
255    else if(*(double *)arg1 > *(double *)arg2) return 1;
256    else return 0;
257}
258
259int VMI::isDecimal(double value) {
260    long r = round(value);
261
262    //printf ("%d\n",r);
263
264    if ((double)r != value) return TRUE;
265    else return FALSE;
266}
267
268double VMI::computePercentile(int q, Triangle *triangles, int numTriangles) {
269    double p = (numTriangles * q) / 100.0;
270    double *list = (double *)malloc(numTriangles * sizeof(double)), l, per = 0.0;
271    int i;
272   
273    //printf("%f\n",p);
274
275    // filling the list
276    for(i = 0; i < numTriangles; i++)
277        list[i] = triangles[i].saliency;
278   
279    /* Quick-Sort */
280    qsort(list, numTriangles, sizeof(list[0]), compare);
281   
282    /* showing the sorted list */
283    /*for(i = 0; i < numTriangles; i++)
284        printf("%f\n", list[i]);
285    printf("\n");*/
286
287    if (isDecimal(p) == TRUE) per = list[(int)p + 1];
288    else {
289        l = (list[(int)p + 1] - list[(int)p]) * (q /100.0);
290
291        per = list[(int)p] + l;
292    }
293
294    free(list);
295   
296    return per;
297}
Note: See TracBrowser for help on using the repository browser.