ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimove2.c
Revision: 3.6
Committed: Fri Mar 26 21:36:20 2004 UTC (20 years ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.5: +51 -28 lines
Log Message:
Continued ANSIfication.

File Contents

# User Rev Content
1 greg 3.1 #ifndef lint
2 schorsch 3.6 static const char RCSid[] = "$Id: ranimove2.c,v 3.5 2003/07/27 22:12:04 schorsch Exp $";
3 greg 3.1 #endif
4     /*
5     * ranimove2.c
6     *
7     * Frame refinement routines for ranimate(1).
8     *
9     * Created by Gregory Ward on Wed Jan 08 2003.
10     */
11    
12 greg 3.2 #include "copyright.h"
13 greg 3.1
14 schorsch 3.3 #include <string.h>
15    
16 greg 3.1 #include "ranimove.h"
17     #include "random.h"
18    
19    
20     #define HL_ERR 0.32 /* highlight error threshold */
21    
22     int cerrzero; /* is cerrmap all zeroes? */
23    
24 schorsch 3.6 static int ppri_cmp(const void *pp1, const void *pp2);
25     static int ray_refine(int n);
26     static long refine_rays(long nrays);
27 greg 3.1
28 schorsch 3.6
29     extern int
30     refine_first(void) /* initial refinement pass */
31 greg 3.1 {
32     int *esamp = (int *)zprev; /* OK to reuse */
33     int hl_erri = errori(HL_ERR);
34     int nextra = 0;
35     int x, y, xp, yp;
36     int neigh;
37     register int n, np;
38    
39     if (sizeof(int) < sizeof(*zprev))
40     error(CONSISTENCY, "code error in refine_first");
41     if (!silent) {
42     printf("\tFirst refinement pass...");
43     fflush(stdout);
44     }
45 schorsch 3.3 memset((void *)esamp, '\0', sizeof(int)*hres*vres);
46 greg 3.1 /*
47     * In our initial pass, we look for lower error pixels from
48     * the same objects in the previous frame, and copy them here.
49     */
50     for (y = vres; y--; )
51     for (x = hres; x--; ) {
52     n = fndx(x, y);
53     if (obuffer[n] == OVOID)
54     continue;
55     if (xmbuffer[n] == MO_UNK)
56     continue;
57     xp = x + xmbuffer[n];
58 schorsch 3.5 if ((xp < 0) | (xp >= hres))
59 greg 3.1 continue;
60     yp = y + ymbuffer[n];
61 schorsch 3.5 if ((yp < 0) | (yp >= vres))
62 greg 3.1 continue;
63     np = fndx(xp, yp);
64     /* make sure we hit same object */
65     if (oprev[np] != obuffer[n])
66     continue;
67     /* is previous frame error lower? */
68     if (aprev[np] < AMIN + ATIDIFF)
69     continue;
70     if (aprev[np] <= abuffer[n] + ATIDIFF)
71     continue;
72     /* shadow & highlight detection */
73     if (abuffer[n] > hl_erri &&
74     getclosest(&neigh, 1, x, y) &&
75     bigdiff(cbuffer[neigh], cprev[np],
76     HL_ERR*(.9+.2*frandom())))
77     continue;
78     abuffer[n] = aprev[np] - ATIDIFF;
79     copycolor(cbuffer[n], cprev[np]);
80     esamp[n] = 1; /* record extrapolated sample */
81     nextra++;
82     }
83     for (n = hres*vres; n--; ) /* update sample counts */
84     if (esamp[n])
85     sbuffer[n] = 1;
86     if (!silent)
87     printf("extrapolated %d pixels\n", nextra);
88     return(1);
89     }
90    
91    
92     /*
93     * We use a recursive computation of the conspicuity
94     * map to avoid associated memory costs and simplify
95     * coding. We create a virtual image pyramid, pooling
96     * variance calculations, etc. The top of the pyramid
97     * corresponds to the foveal resolution, as there should
98     * not be any interesting mechanisms above this level.
99     */
100    
101     #define CSF_C0 1.14
102     #define CSF_C1 0.67
103     #define CSF_C2 1.7
104     #define CSF_S1 6.1
105     #define CSF_S2 7.3
106     #define CSF_P1 45.9
107     #define CSF_PC (30./45.9*CSF_P1)
108     #define CSF_VR0 0.15
109     #define CSF_VRC 80.
110    
111     struct ConspSum {
112     COLOR vsum; /* value sum */
113     COLOR v2sum; /* value^2 sum */
114     long nsamp; /* number of samples */
115     long xmsum; /* x-motion sum */
116     long ymsum; /* y-motion sum */
117     int npix; /* number of pixels */
118     double hls; /* high-level saliency */
119     };
120    
121     static double pixel_deg; /* base pixel frequency */
122     static int fhsiz, fvsiz; /* foveal subimage size */
123    
124 schorsch 3.6 static void clr_consp(struct ConspSum *cs);
125     static void sum_consp(struct ConspSum *cdest, struct ConspSum *cs);
126     static void est_consp(int x0, int y0, int x1, int y1, struct ConspSum *cs);
127     static void subconspicuity(int x0, int y0, int x1, int y1, struct ConspSum *cs);
128    
129 greg 3.1 static void
130 schorsch 3.6 clr_consp( /* initialize a conspicuity sum */
131     register struct ConspSum *cs
132     )
133 greg 3.1 {
134     if (cs == NULL)
135     return;
136     setcolor(cs->vsum, 0., 0., 0.);
137     setcolor(cs->v2sum, 0., 0., 0.);
138     cs->nsamp = 0;
139     cs->xmsum = cs->ymsum = 0;
140     cs->npix = 0;
141     cs->hls = 0;
142     }
143    
144     static void
145 schorsch 3.6 sum_consp( /* sum in conspicuity result */
146     register struct ConspSum *cdest,
147     register struct ConspSum *cs
148     )
149 greg 3.1 {
150 schorsch 3.5 if ((cdest == NULL) | (cs == NULL))
151 greg 3.1 return;
152     addcolor(cdest->vsum, cs->vsum);
153     addcolor(cdest->v2sum, cs->v2sum);
154     cdest->nsamp += cs->nsamp;
155     cdest->xmsum += cs->xmsum;
156     cdest->ymsum += cs->ymsum;
157     cdest->npix += cs->npix;
158     if (cs->hls > cdest->hls)
159     cdest->hls = cs->hls;
160     }
161    
162     static void
163 schorsch 3.6 est_consp( /* estimate error conspicuity & update */
164     int x0,
165     int y0,
166     int x1,
167     int y1,
168     register struct ConspSum *cs
169     )
170 greg 3.1 {
171     double rad2, mtn2, cpd, vm, vr, csf, eest;
172     /* do we care? */
173     if (cs->hls <= FTINY)
174     return;
175     /* get relative error */
176     if (cs->nsamp < NSAMPOK) {
177     int neigh[NSAMPOK]; /* gather neighbors */
178     eest = comperr(neigh,
179     getclosest(neigh, NSAMPOK, (x0+x1)>>1, (y0+y1)>>1),
180     cs->nsamp);
181     } else
182     eest = estimaterr(cs->vsum, cs->v2sum, cs->nsamp, cs->nsamp);
183    
184 schorsch 3.5 if ((x0 == x1-1) & (y0 == y1-1)) { /* update pixel error */
185 greg 3.1 int n = fndx(x0, y0);
186     int ai;
187     int ne;
188     if (sbuffer[n] >= 255) {
189     abuffer[n] = ADISTANT;
190     } else {
191     ai = errori(eest);
192     if (ai < AMIN) ai = AMIN;
193     else if (ai >= ADISTANT) ai = ADISTANT-1;
194     abuffer[n] = ai;
195     /* can't improve on closest */
196     if (!cs->nsamp && getclosest(&ne, 1, x0, y0) &&
197     abuffer[ne] < ai &&
198     abuffer[ne] >= AMIN)
199     abuffer[n] = abuffer[ne];
200     }
201     }
202     /* compute radius^2 */
203     rad2 = 0.125*((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
204    
205     /* average motion^2 */
206     mtn2 = (double)cs->xmsum*cs->xmsum + (double)cs->ymsum*cs->ymsum;
207     mtn2 /= (double)(cs->npix*cs->npix);
208     /* motion blur hides us? */
209     if (mblur*mblur*mtn2 >= 4.*rad2)
210     return;
211     /* too small to see? */
212     cpd = pixel_deg * pixel_deg / rad2;
213     if (cpd > CSF_PC*CSF_PC)
214     return;
215     cpd = sqrt(cpd);
216     /* compute CSF [Daley98] */
217     vm = rate * sqrt(mtn2) / pixel_deg;
218     vr = cs->hls/hlsmax*vm + CSF_VR0; /* use hls tracking eff. */
219     if (vr > CSF_VRC) vr = CSF_VRC;
220     vr = vm - vr;
221     if (vr < 0) vr = -vr;
222     csf = log(CSF_C2*(1./3.)*vr);
223     if (csf < 0) csf = -csf;
224     csf = CSF_S1 + CSF_S2*csf*csf*csf;
225     csf *= CSF_C0*CSF_C2*4.*PI*PI*CSF_C1*CSF_C1*cpd*cpd;
226     csf *= exp(-CSF_C1*4.*PI/CSF_P1*(CSF_C2*vr + 2.)*cpd);
227     /* compute visible error */
228     eest = eest*csf/ndthresh - 1.;
229     if (eest <= FTINY)
230     return;
231     /* scale by saleincy */
232     eest *= cs->hls;
233     /* worth the bother? */
234     if (eest <= .01)
235     return;
236     /* sum into map */
237     for ( ; y0 < y1; y0++) {
238     float *em0 = cerrmap + fndx(x0, y0);
239     register float *emp = em0 + (x1-x0);
240     while (emp-- > em0)
241     *emp += eest;
242     }
243     cerrzero = 0;
244     }
245    
246     static void
247 schorsch 3.6 subconspicuity( /* compute subportion of conspicuity */
248     int x0,
249     int y0,
250     int x1,
251     int y1,
252     struct ConspSum *cs
253     )
254 greg 3.1 {
255     struct ConspSum mysum;
256     int i;
257    
258 schorsch 3.5 if ((x0 >= x1) | (y0 >= y1))
259 greg 3.1 error(CONSISTENCY, "bad call to subconspicuity");
260    
261     clr_consp(&mysum); /* prepare sum */
262    
263 schorsch 3.5 if ((x0 == x1-1) & (y0 == y1-1)) { /* single pixel */
264 greg 3.1 double hls;
265     register int n = fndx(x0, y0);
266     if (sbuffer[n]) {
267     copycolor(mysum.vsum, cbuffer[n]);
268     copycolor(mysum.v2sum, val2map[n]);
269     mysum.nsamp = sbuffer[n];
270     }
271     if ((mysum.xmsum = xmbuffer[n]) == MO_UNK)
272     mysum.xmsum = 0;
273     else
274     mysum.ymsum = ymbuffer[n];
275     mysum.npix = 1;
276     /* max. hls in fovea */
277     mysum.hls = obj_prio(obuffer[n]);
278     if (x0 >= fhsiz) {
279     hls = obj_prio(obuffer[fndx(x0-fhsiz,y0)]);
280     if (hls > mysum.hls) mysum.hls = hls;
281     }
282     if (x0 < hres-fhsiz) {
283     hls = obj_prio(obuffer[fndx(x0+fhsiz,y0)]);
284     if (hls > mysum.hls) mysum.hls = hls;
285     }
286     if (y0 >= fvsiz) {
287     hls = obj_prio(obuffer[fndx(x0,y0-fvsiz)]);
288     if (hls > mysum.hls) mysum.hls = hls;
289     }
290     if (y0 < vres-fvsiz) {
291     hls = obj_prio(obuffer[fndx(x0,y0+fvsiz)]);
292     if (hls > mysum.hls) mysum.hls = hls;
293     }
294     } else if (x0 == x1-1) { /* vertical pair */
295     for (i = y0 ; i < y1; i++)
296     subconspicuity(x0, i, x1, i+1, &mysum);
297     } else if (y0 == y1-1) { /* horizontal pair */
298     for (i = x0 ; i < x1; i++)
299     subconspicuity(i, y0, i+1, y1, &mysum);
300     } else { /* rectangle */
301     subconspicuity(x0, y0, (x0+x1)>>1, (y0+y1)>>1, &mysum);
302     subconspicuity((x0+x1)>>1, y0, x1, (y0+y1)>>1, &mysum);
303     subconspicuity(x0, (y0+y1)>>1, (x0+x1)>>1, y1, &mysum);
304     subconspicuity((x0+x1)>>1, (y0+y1)>>1, x1, y1, &mysum);
305     }
306     /* update conspicuity */
307     est_consp(x0, y0, x1, y1, &mysum);
308     /* sum into return value */
309     sum_consp(cs, &mysum);
310     }
311    
312 schorsch 3.6 extern void
313     conspicuity(void) /* compute conspicuous error map */
314 greg 3.1 {
315     int fhres, fvres;
316     int fx, fy;
317     /* reuse previous z-buffer */
318     cerrmap = (float *)zprev;
319 schorsch 3.3 memset((void *)cerrmap, '\0', sizeof(float)*hres*vres);
320 greg 3.1 cerrzero = 1;
321     /* compute base pixel frequency */
322     pixel_deg = .5*(hres/vw.horiz + vres/vw.vert);
323     /* compute foveal resolution */
324     fhres = vw.horiz/FOV_DEG + 0.5;
325     if (fhres <= 0) fhres = 1;
326     else if (fhres > hres) fhres = hres;
327     fvres = vw.vert/FOV_DEG + 0.5;
328     if (fvres <= 0) fvres = 1;
329     else if (fvres > vres) fvres = vres;
330     fhsiz = hres/fhres;
331     fvsiz = vres/fvres;
332     /* call our foveal subroutine */
333     for (fy = fvres; fy--; )
334     for (fx = fhres; fx--; )
335     subconspicuity(hres*fx/fhres, vres*fy/fvres,
336     hres*(fx+1)/fhres, vres*(fy+1)/fvres,
337     NULL);
338     }
339    
340    
341     /*
342     * The following structure is used to collect data on the
343     * initial error in the ambient value estimate, in order
344     * to correct for it in the subsequent frames.
345     */
346     static struct AmbSum {
347     double diffsum[3]; /* sum of (correct - ambval) */
348     long nsamps; /* number of values in sum */
349     } *asump = NULL;
350    
351    
352     static int
353 schorsch 3.6 ppri_cmp( /* pixel priority comparison */
354     const void *pp1,
355     const void *pp2
356     )
357 greg 3.1 {
358     double se1 = cerrmap[*(const int *)pp1];
359     double se2 = cerrmap[*(const int *)pp2];
360     int adiff;
361     /* higher conspicuity to front */
362     if (se1 < se2) return(1);
363     if (se1 > se2) return(-1);
364     /* else higher error to front */
365     adiff = (int)abuffer[*(const int *)pp1] -
366     (int)abuffer[*(const int *)pp2];
367     if (adiff)
368     return(adiff);
369     /* else fewer samples to front */
370     return((int)sbuffer[*(const int *)pp1] -
371     (int)sbuffer[*(const int *)pp2]);
372     }
373    
374    
375     static int
376 schorsch 3.6 ray_refine( /* refine the given pixel by tracing a ray */
377     register int n
378     )
379 greg 3.1 {
380     RAY ir;
381     COLOR ctmp;
382     int i;
383    
384     if (n < 0) { /* fetching stragglers */
385     if (nprocs <= 1 || !ray_presult(&ir, 0))
386     return(-1);
387     n = ir.rno;
388     } else { /* else tracing a new ray */
389     double hv[2];
390     if (sbuffer[n] >= 255) /* reached limit? */
391     return(-1);
392     sample_pos(hv, n%hres, n/hres, sbuffer[n]);
393     ir.rmax = viewray(ir.rorg, ir.rdir, &vw, hv[0], hv[1]);
394     if (ir.rmax < -FTINY)
395     return(-1);
396     if (nprocs > 1) {
397     int rval;
398     rayorigin(&ir, NULL, PRIMARY, 1.0);
399     ir.rno = n;
400     rval = ray_pqueue(&ir);
401     if (!rval)
402     return(-1);
403     if (rval < 0)
404     quit(1);
405     n = ir.rno;
406     } else
407     ray_trace(&ir);
408     }
409     if (abuffer[n] == ALOWQ && asump != NULL) {
410     if (sbuffer[n] != 1)
411     error(CONSISTENCY, "bad code in ray_refine");
412     if (getambcolor(ctmp, obuffer[n]) &&
413 schorsch 3.5 (colval(ctmp,RED) > 0.01) &
414     (colval(ctmp,GRN) > 0.01) &
415     (colval(ctmp,BLU) > 0.01)) {
416 greg 3.1 for (i = 0; i < 3; i++)
417     asump->diffsum[i] +=
418     (colval(ir.rcol,i) - colval(cbuffer[n],i))
419     / colval(ctmp,i);
420     asump->nsamps++;
421     }
422     sbuffer[n] = 0;
423     }
424     setcolor(ctmp,
425     colval(ir.rcol,RED)*colval(ir.rcol,RED),
426     colval(ir.rcol,GRN)*colval(ir.rcol,GRN),
427     colval(ir.rcol,BLU)*colval(ir.rcol,BLU));
428     if (!sbuffer[n]) { /* first sample */
429     copycolor(cbuffer[n], ir.rcol);
430     copycolor(val2map[n], ctmp);
431     abuffer[n] = AHIGHQ;
432     sbuffer[n] = 1;
433     } else { /* else sum in sample */
434     addcolor(cbuffer[n], ir.rcol);
435     addcolor(val2map[n], ctmp);
436     sbuffer[n]++;
437     }
438     return(n);
439     }
440    
441    
442     static long
443 schorsch 3.6 refine_rays( /* compute refinement rays */
444     long nrays
445     )
446 greg 3.1 {
447     int *pord;
448     int ntodo;
449     long rdone;
450     int i;
451     /* skip if nothing significant */
452     if (ndtset && cerrzero)
453 greg 3.4 return(0);
454 greg 3.1 /* initialize priority list */
455     pord = (int *)malloc(sizeof(int)*hres*vres);
456     for (i = hres*vres; i--; )
457     pord[i] = i;
458     /* sort our priorities */
459     ntodo = hres*vres;
460     if (nrays < ntodo)
461     qsort((void *)pord, hres*vres, sizeof(int), ppri_cmp);
462     i = 0;
463     /* trace rays in list */
464     for (rdone = 0; rdone < nrays; rdone++) {
465     if (ndtset && i >= 1000 && cerrmap[pord[i]] <= FTINY)
466     ntodo = i;
467     if (i >= ntodo) { /* redo conspicuity & priority */
468     while (ray_refine(-1) >= 0)
469     ;
470     conspicuity();
471     if (ndtset && cerrzero)
472     break;
473     qsort((void *)pord, hres*vres, sizeof(int), ppri_cmp);
474     ntodo = hres*vres/8;
475     i = 0;
476     }
477     /* sample next pixel */
478     ray_refine(pord[i++]);
479     }
480     /* clean up and return */
481     while (ray_refine(-1) >= 0)
482     ;
483     free((void *)pord);
484     return(rdone);
485     }
486    
487    
488 schorsch 3.6 extern int
489     refine_frame( /* refine current frame */
490     int pass
491     )
492 greg 3.1 {
493     static double rtime_used = 0;
494     static long ray_cnt = 0;
495     static double ctime_used = 0;
496     static int csp_cnt = 0;
497 schorsch 3.5 int timed = (fcur > fbeg) | (pass > 0) | (quickstart);
498 greg 3.1 double time_start, rtime_start, time_done;
499     struct AmbSum myAmbSum;
500     long rays_todo, nr;
501     register int n;
502     /* IBR refinement? */
503 schorsch 3.5 if ((pass == 0) & (fcur > fbeg))
504 greg 3.1 return(refine_first());
505     /* any time left? */
506     time_start = getTime();
507     if (timed) {
508     if (time_start >= frm_stop)
509     goto nomore;
510     if (csp_cnt > 0 && time_start + ctime_used/csp_cnt >= frm_stop)
511     goto nomore;
512     }
513     asump = NULL; /* use resampling to update ambval? */
514     if (!curparams->ambounce && hirendparams.ambounce) {
515     myAmbSum.diffsum[RED] =
516     myAmbSum.diffsum[GRN] =
517     myAmbSum.diffsum[BLU] = 0;
518     myAmbSum.nsamps = 0;
519     asump = &myAmbSum;
520     }
521     /* initialize value-squared map */
522     if (val2map == NULL) {
523     val2map = cprev; /* OK to reuse at this point */
524     n = (asump == NULL) ? hres*vres : 0;
525     while (n--)
526     if (sbuffer[n])
527     setcolor(val2map[n],
528     colval(cbuffer[n],RED)*colval(cbuffer[n],RED),
529     colval(cbuffer[n],GRN)*colval(cbuffer[n],GRN),
530     colval(cbuffer[n],BLU)*colval(cbuffer[n],BLU));
531     else
532     setcolor(val2map[n], 0., 0., 0.);
533     }
534     /* compute conspicuity */
535     if (!silent) {
536     printf("\tComputing conspicuity map\n");
537     fflush(stdout);
538     }
539     conspicuity();
540     csp_cnt++;
541     #if 0
542     if (pass == 1) {
543     char fnm[256];
544     sprintf(fnm, vval(BASENAME), fcur);
545     strcat(fnm, "_incmap.pic");
546     write_map(cerrmap, fnm);
547     }
548     #endif
549     /* get ray start time */
550     rtime_start = getTime();
551     ctime_used += rtime_start - time_start;
552     if (timed && rtime_start >= frm_stop)
553     return(0); /* error done but out of time */
554     if (rtime_used <= FTINY) {
555     if (quickstart)
556     rays_todo = 1000;
557     else
558     rays_todo = hres*vres;
559     } else {
560     rays_todo = (long)((frm_stop - rtime_start) *
561     ray_cnt / rtime_used);
562     if (rays_todo < 1000)
563     return(0); /* let's call it a frame */
564     }
565     /* set higher rendering quality */
566     if (twolevels && curparams != &hirendparams) {
567     ray_restore(curparams = &hirendparams);
568     if (nprocs > 1) { /* need to update children */
569     if (!silent) {
570     printf("\tRestarting %d processes\n", nprocs);
571     fflush(stdout);
572     }
573     ray_pclose(0);
574     ray_popen(nprocs);
575     }
576     }
577     /* compute refinement rays */
578     if (!silent) {
579     printf("\tRefinement pass %d...",
580 schorsch 3.6 pass+1); /*, rays_todo); */
581 greg 3.1 fflush(stdout);
582     }
583     if (asump != NULL) /* flag low-quality samples */
584     for (n = hres*vres; n--; )
585     if (sbuffer[n])
586     abuffer[n] = ALOWQ;
587     /* trace those rays */
588     nr = refine_rays(rays_todo);
589     if (!silent)
590 schorsch 3.6 printf("traced %ld HQ rays\n", nr);
591 greg 3.1 if (nr <= 0)
592     return(0);
593     /* update timing stats */
594     while (ray_cnt >= 1L<<20) {
595     ray_cnt >>= 1;
596     rtime_used *= .5;
597     }
598     ray_cnt += nr;
599     time_done = getTime();
600     rtime_used += time_done - rtime_start;
601     if (!timed && time_done > frm_stop)
602     frm_stop = time_done;
603     /* update ambient value */
604     if (asump != NULL && asump->nsamps >= 1000) {
605     double sf = 1./(double)asump->nsamps;
606     for (n = 3; n--; ) {
607     asump->diffsum[n] *= sf;
608     asump->diffsum[n] += colval(lorendparams.ambval,n);
609     if (asump->diffsum[n] < 0) asump->diffsum[n] = 0;
610     }
611     setcolor(lorendparams.ambval,
612     asump->diffsum[RED],
613     asump->diffsum[GRN],
614     asump->diffsum[BLU]);
615     if (!silent)
616     printf("\tUpdated parameter: -av %f %f %f\n",
617     asump->diffsum[RED],
618     asump->diffsum[GRN],
619     asump->diffsum[BLU]);
620     asump = NULL;
621     }
622     return(1);
623     nomore:
624     /* make sure error map is updated */
625 schorsch 3.5 if ((fcur == fbeg) | (pass > 1))
626 greg 3.1 comp_frame_error();
627     return(0);
628     }