ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.27 by greg, Sat Apr 29 10:51:28 1995 UTC vs.
Revision 2.46 by gwlarson, Sun Jan 10 12:45:19 1999 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1995 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "random.h"
20  
21 < #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
21 > #ifndef  OCTSCALE
22 > #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23 > #endif
24  
25   typedef struct ambtree {
26          AMBVAL  *alist;         /* ambient value list */
# Line 48 | Line 50 | static int  nunflshed = 0;     /* number of unflushed ambi
50   #endif
51   #endif
52   #ifndef SORT_INTVL
53 < #define SORT_INTVL      (SORT_THRESH*8)
53 > #define SORT_INTVL      (SORT_THRESH<<1)
54   #endif
55 + #ifndef MAX_SORT_INTVL
56 + #define MAX_SORT_INTVL  (SORT_INTVL<<6)
57 + #endif
58  
59 + static double  avsum = 0.;              /* computed ambient value sum (log) */
60 + static unsigned int  navsum = 0;        /* number of values in avsum */
61 + static unsigned int  nambvals = 0;      /* total number of indirect values */
62 + static unsigned int  nambshare = 0;     /* number of values from file */
63   static unsigned long  ambclock = 0;     /* ambient access clock */
55 static unsigned int  nambvals = 0;      /* number of stored ambient values */
64   static unsigned long  lastsort = 0;     /* time of last value sort */
65   static long  sortintvl = SORT_INTVL;    /* time until next sort */
66  
# Line 60 | Line 68 | static long  sortintvl = SORT_INTVL;   /* time until nex
68          /*
69           * Track access times unless we are sharing ambient values
70           * through memory on a multiprocessor, when we want to avoid
71 <         * claiming our own memory (copy on write).
71 >         * claiming our own memory (copy on write).  Go ahead anyway
72 >         * if more than two thirds of our values are unshared.
73 >         * Compile with -Dtracktime=0 to turn this code off.
74           */
75 < #define tracktime       (shm_boundary == NULL || ambfp == NULL)
76 < #define checksort()     if (ambclock > lastsort+sortintvl && \
77 <                        nambvals > SORT_THRESH) sortambvals(0)
75 > #ifndef tracktime
76 > #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
77 > #endif
78  
79   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
80  
81   #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
82  
83 < extern long  ftell(), lseek();
74 < static int  initambfile(), avsave(), avinsert(), sortambvals();
83 > static int  initambfile(), avsave(), avinsert(), sortambvals(), avlmemi();
84   static AMBVAL  *avstore();
85   #ifdef  F_SETLKW
86   static  aflock();
# Line 88 | Line 97 | int  ar;
97                  maxarad = thescene.cusize / 2.0;
98          } else {
99                  minarad = thescene.cusize / ar;
100 <                maxarad = 16 * minarad;                 /* heuristic */
100 >                maxarad = 64 * minarad;                 /* heuristic */
101                  if (maxarad > thescene.cusize / 2.0)
102                          maxarad = thescene.cusize / 2.0;
103          }
# Line 102 | Line 111 | int  ar;
111   setambacc(newa)                         /* set ambient accuracy */
112   double  newa;
113   {
114 <        static double  oldambacc = -1.0;
114 >        double  ambdiff;
115  
116 <        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
117 <        if (oldambacc < -FTINY)
118 <                oldambacc = ambacc;     /* do nothing first call */
119 <        if (fabs(newa - oldambacc) < 0.01)
120 <                return;                 /* insignificant -- don't bother */
112 <        if (ambacc <= FTINY)
113 <                return;                 /* cannot build new tree */
114 <                                        /* else need to rebuild tree */
115 <        sortambvals(1);
116 <        oldambacc = ambacc;             /* remeber setting for next call */
116 >        if (newa < 0.0)
117 >                newa = 0.0;
118 >        ambdiff = fabs(newa - ambacc);
119 >        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
120 >                sortambvals(1);                 /* rebuild tree */
121   }
122  
123  
124   setambient(afile)                       /* initialize calculation */
125   char  *afile;
126   {
127 <        long  headlen;
127 >        int     readonly = 0;
128 >        long  pos, flen;
129          AMBVAL  amb;
130                                                  /* init ambient limits */
131          setambres(ambres);
# Line 134 | Line 139 | char  *afile;
139                  return;
140          }
141                                                  /* open ambient file */
142 <        if ((ambfp = fopen(afile, "r+")) != NULL) {
143 <                initambfile(0);
144 <                headlen = ftell(ambfp);
142 >        if ((ambfp = fopen(afile, "r+")) == NULL)
143 >                readonly = (ambfp = fopen(afile, "r")) != NULL;
144 >        if (ambfp != NULL) {
145 >                initambfile(0);                 /* file exists */
146 >                pos = ftell(ambfp);
147                  while (readambval(&amb, ambfp))
148                          avinsert(avstore(&amb));
149 <                                                /* align */
150 <                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
151 <        } else if ((ambfp = fopen(afile, "w+")) != NULL)
152 <                initambfile(1);
153 <        else {
149 >                nambshare = nambvals;           /* share loaded values */
150 >                if (readonly) {
151 >                        sprintf(errmsg,
152 >                                "loaded %u values from read-only ambient file",
153 >                                        nambvals);
154 >                        error(WARNING, errmsg);
155 >                        fclose(ambfp);          /* close file so no writes */
156 >                        ambfp = NULL;
157 >                        return;                 /* avoid ambsync() */
158 >                }
159 >                                                /* align file pointer */
160 >                pos += (long)nambvals*AMBVALSIZ;
161 >                flen = lseek(fileno(ambfp), 0L, 2);
162 >                if (flen != pos) {
163 >                        sprintf(errmsg,
164 >                        "ignoring last %ld values in ambient file (corrupted)",
165 >                                        (flen - pos)/AMBVALSIZ);
166 >                        error(WARNING, errmsg);
167 >                        fseek(ambfp, pos, 0);
168 >                        ftruncate(fileno(ambfp), pos);
169 >                }
170 >        } else if ((ambfp = fopen(afile, "w+")) != NULL) {
171 >                initambfile(1);                 /* else create new file */
172 >        } else {
173                  sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
174                  error(SYSTEM, errmsg);
175          }
# Line 174 | Line 200 | OBJECT obj;
200   }
201  
202  
203 < ambient(acol, r)                /* compute ambient component for ray */
203 > ambient(acol, r, nrm)           /* compute ambient component for ray */
204   COLOR  acol;
205   register RAY  *r;
206 + FVECT  nrm;
207   {
208          static int  rdepth = 0;                 /* ambient recursion */
209 <        double  d;
209 >        double  d, l;
210  
211          if (ambdiv <= 0)                        /* no ambient calculation */
212                  goto dumbamb;
# Line 195 | Line 222 | register RAY  *r;
222                  rdepth++;
223                  d = doambient(acol, r, r->rweight, NULL, NULL);
224                  rdepth--;
225 <                if (d == 0.0)
225 >                if (d <= FTINY)
226                          goto dumbamb;
227                  return;
228          }
229 <                                                /* resort memory? */
230 <        checksort();
229 >
230 >        if (tracktime)                          /* sort to minimize thrashing */
231 >                sortambvals(0);
232                                                  /* get ambient value */
233          setcolor(acol, 0.0, 0.0, 0.0);
234 <        d = sumambient(acol, r, rdepth,
234 >        d = sumambient(acol, r, nrm, rdepth,
235                          &atrunk, thescene.cuorg, thescene.cusize);
236 <        if (d > FTINY)
236 >        if (d > FTINY) {
237                  scalecolor(acol, 1.0/d);
238 <        else {
211 <                d = makeambient(acol, r, rdepth++);
212 <                rdepth--;
238 >                return;
239          }
240 +        rdepth++;                               /* need to cache new value */
241 +        d = makeambient(acol, r, nrm, rdepth-1);
242 +        rdepth--;
243          if (d > FTINY)
244                  return;
245   dumbamb:                                        /* return global value */
246          copycolor(acol, ambval);
247 +        if (ambvwt <= 0 | navsum == 0)
248 +                return;
249 +        l = bright(ambval);                     /* average in computations */
250 +        if (l > FTINY) {
251 +                d = (log(l)*(double)ambvwt + avsum) /
252 +                                (double)(ambvwt + navsum);
253 +                d = exp(d) / l;
254 +                scalecolor(acol, d);            /* apply color of ambval */
255 +        } else {
256 +                d = exp( avsum / (double)navsum );
257 +                setcolor(acol, d, d, d);        /* neutral color */
258 +        }
259   }
260  
261  
262   double
263 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
263 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
264   COLOR  acol;
265   register RAY  *r;
266 + FVECT  rn;
267   int  al;
268   AMBTREE  *at;
269   FVECT  c0;
# Line 233 | Line 275 | double s;
275          int  i;
276          register int  j;
277          register AMBVAL  *av;
278 <                                        /* do this node */
278 >
279          wsum = 0.0;
280 +                                        /* do this node */
281          for (av = at->alist; av != NULL; av = av->next) {
282                  if (tracktime)
283 <                        av->latick = ambclock++;
283 >                        av->latick = ambclock;
284                  /*
285                   *  Ambient level test.
286                   */
# Line 248 | Line 291 | double s;
291                  /*
292                   *  Ambient radius test.
293                   */
294 <                e1 = 0.0;
295 <                for (j = 0; j < 3; j++) {
296 <                        d = av->pos[j] - r->rop[j];
297 <                        e1 += d * d;
298 <                }
294 >                d = av->pos[0] - r->rop[0];
295 >                e1 = d * d;
296 >                d = av->pos[1] - r->rop[1];
297 >                e1 += d * d;
298 >                d = av->pos[2] - r->rop[2];
299 >                e1 += d * d;
300                  e1 /= av->rad * av->rad;
301                  if (e1 > ambacc*ambacc*1.21)
302                          continue;
# Line 276 | Line 320 | double s;
320                   *  Jittering final test reduces image artifacts.
321                   */
322                  wt = sqrt(e1) + sqrt(e2);
323 <                wt *= .9 + .2*urand(9015+samplendx);
280 <                if (wt > ambacc)
323 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
324                          continue;
325                  if (wt <= 1e-3)
326                          wt = 1e3;
327                  else
328                          wt = 1.0 / wt;
329                  wsum += wt;
330 <                extambient(ct, av, r->rop, r->ron);
330 >                extambient(ct, av, r->rop, rn);
331                  scalecolor(ct, wt);
332                  addcolor(acol, ct);
333          }
# Line 303 | Line 346 | double s;
346                                  break;
347                  }
348                  if (j == 3)
349 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
349 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
350          }
351          return(wsum);
352   }
353  
354  
355   double
356 < makeambient(acol, r, al)        /* make a new ambient value */
356 > makeambient(acol, r, rn, al)    /* make a new ambient value */
357   COLOR  acol;
358   register RAY  *r;
359 + FVECT  rn;
360   int  al;
361   {
362          AMBVAL  amb;
363          FVECT   gp, gd;
364                                                  /* compute weight */
365          amb.weight = pow(AVGREFL, (double)al);
366 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
366 >        if (r->rweight < 0.1*amb.weight)        /* heuristic */
367                  amb.weight = r->rweight;
368                                                  /* compute ambient */
369          amb.rad = doambient(acol, r, amb.weight, gp, gd);
370 <        if (amb.rad == 0.0)
370 >        if (amb.rad <= FTINY)
371                  return(0.0);
372                                                  /* store it */
373          VCOPY(amb.pos, r->rop);
# Line 334 | Line 378 | int  al;
378          VCOPY(amb.gdir, gd);
379                                                  /* insert into tree */
380          avsave(&amb);                           /* and save to file */
381 +        if (rn != r->ron)
382 +                extambient(acol, &amb, r->rop, rn);     /* texture */
383          return(amb.rad);
384   }
385  
# Line 343 | Line 389 | COLOR  cr;
389   register AMBVAL  *ap;
390   FVECT  pv, nv;
391   {
392 <        FVECT  v1, v2;
392 >        FVECT  v1;
393          register int  i;
394          double  d;
395  
# Line 352 | Line 398 | FVECT  pv, nv;
398          for (i = 0; i < 3; i++)
399                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
400                                          /* gradient due to rotation */
401 <        VCOPY(v1, ap->dir);
402 <        fcross(v2, v1, nv);
357 <        d += DOT(ap->gdir, v2);
401 >        VCROSS(v1, ap->dir, nv);
402 >        d += DOT(ap->gdir, v1);
403          if (d <= 0.0) {
404                  setcolor(cr, 0.0, 0.0, 0.0);
405                  return;
# Line 379 | Line 424 | int  creat;
424          setbuf(ambfp, bmalloc(BUFSIZ+8));
425          if (creat) {                    /* new file */
426                  newheader("RADIANCE", ambfp);
427 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
427 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
428                                  progname, colval(ambval,RED),
429                                  colval(ambval,GRN), colval(ambval,BLU),
430 <                                ambounce, ambacc);
430 >                                ambvwt, ambounce, ambacc);
431                  fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
432                                  ambdiv, ambssamp, ambres,
433                                  octname==NULL ? "" : octname);
# Line 418 | Line 463 | avstore(aval)                          /* allocate memory and store aval */
463   register AMBVAL  *aval;
464   {
465          register AMBVAL  *av;
466 +        double  d;
467  
468          if ((av = newambval()) == NULL)
469                  error(SYSTEM, "out of memory in avstore");
# Line 425 | Line 471 | register AMBVAL  *aval;
471          av->latick = ambclock;
472          av->next = NULL;
473          nambvals++;
474 +        d = bright(av->val);
475 +        if (d > FTINY) {                /* add to log sum for averaging */
476 +                avsum += log(d);
477 +                navsum++;
478 +        }
479          return(av);
480   }
481  
# Line 527 | Line 578 | int    (*f)();
578   }
579  
580  
581 < static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
581 > static struct avl {
582 >        AMBVAL  *p;
583 >        unsigned long   t;
584 > }       *avlist1;                       /* ambient value list with ticks */
585 > static AMBVAL   **avlist2;              /* memory positions for sorting */
586   static int      i_avlist;               /* index for lists */
587  
588  
589   static
590   av2list(av)
591 < AMBVAL  *av;
591 > register AMBVAL *av;
592   {
593   #ifdef DEBUG
594          if (i_avlist >= nambvals)
595                  error(CONSISTENCY, "too many ambient values in av2list1");
596   #endif
597 <        avlist1[i_avlist] = avlist2[i_avlist] = av;
598 <        i_avlist++;
597 >        avlist1[i_avlist].p = avlist2[i_avlist] = av;
598 >        avlist1[i_avlist++].t = av->latick;
599   }
600  
601  
602   static int
603 < alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
604 < AMBVAL  **avp1, **avp2;
603 > alatcmp(av1, av2)                       /* compare ambient values for MRA */
604 > struct avl      *av1, *av2;
605   {
606 <        return((**avp2).latick - (**avp1).latick);
606 >        register long  lc = av2->t - av1->t;
607 >        return(lc<0 ? -1 : lc>0 ? 1 : 0);
608   }
609  
610  
# Line 560 | Line 616 | AMBVAL **avp1, **avp2;
616   }
617  
618  
619 < #ifdef DEBUG
619 > #if 1
620   static int
621   avlmemi(avaddr)                         /* find list position from address */
622   AMBVAL  *avaddr;
# Line 586 | Line 642 | int    always;
642          AMBTREE  oldatrunk;
643          AMBVAL  tav, *tap, *pnext;
644          register int    i, j;
645 +                                        /* see if it's time yet */
646 +        if (!always && (ambclock++ < lastsort+sortintvl ||
647 +                        nambvals < SORT_THRESH))
648 +                return;
649          /*
650           * The idea here is to minimize memory thrashing
651           * in VM systems by improving reference locality.
# Line 602 | Line 662 | int    always;
662           * tree will be rebuilt with the new accuracy parameter.
663           */
664          if (tracktime) {                /* allocate pointer arrays to sort */
605                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
665                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
666 <        } else
667 <                avlist1 = avlist2 = NULL;
668 <        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
669 <                if (avlist1 != NULL)
670 <                        free((char *)avlist1);
666 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
667 >        } else {
668 >                avlist2 = NULL;
669 >                avlist1 = NULL;
670 >        }
671 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
672 >                if (avlist2 != NULL)
673 >                        free((char *)avlist2);
674                  if (always) {           /* rebuild without sorting */
675                          copystruct(&oldatrunk, &atrunk);
676                          atrunk.alist = NULL;
# Line 625 | Line 687 | int    always;
687                   * when we're thrashing, which is when we need to do it.
688                   */
689   #ifdef DEBUG
690 <                sprintf(errmsg, "sorting %u ambient values...", nambvals);
690 >                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
691 >                                nambvals, ambclock);
692                  eputs(errmsg);
693   #endif
694                  i_avlist = 0;
# Line 634 | Line 697 | int    always;
697                  if (i_avlist < nambvals)
698                          error(CONSISTENCY, "missing ambient values in sortambvals");
699   #endif
700 <                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
700 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
701                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
702                  for (i = 0; i < nambvals; i++) {
703 <                        if (avlist1[i] == NULL)
703 >                        if (avlist1[i].p == NULL)
704                                  continue;
705                          tap = avlist2[i];
706                          copystruct(&tav, tap);
707 <                        for (j = i; (pnext = avlist1[j]) != tap;
707 >                        for (j = i; (pnext = avlist1[j].p) != tap;
708                                          j = avlmemi(pnext)) {
709                                  copystruct(avlist2[j], pnext);
710                                  avinsert(avlist2[j]);
711 <                                avlist1[j] = NULL;
711 >                                avlist1[j].p = NULL;
712                          }
713                          copystruct(avlist2[j], &tav);
714                          avinsert(avlist2[j]);
715 <                        avlist1[j] = NULL;
715 >                        avlist1[j].p = NULL;
716                  }
717                  free((char *)avlist1);
718                  free((char *)avlist2);
719 <                if (sortintvl < SORT_INTVL<<6)
719 >                                                /* compute new sort interval */
720 >                sortintvl = ambclock - lastsort;
721 >                if (sortintvl >= MAX_SORT_INTVL/2)
722 >                        sortintvl = MAX_SORT_INTVL;
723 >                else
724                          sortintvl <<= 1;        /* wait twice as long next */
725   #ifdef DEBUG
726                  eputs("done\n");
# Line 706 | Line 773 | ambsync()                      /* synchronize ambient file */
773                  if (fseek(ambinp, lastpos, 0) < 0)
774                          goto seekerr;
775                  while (n >= AMBVALSIZ) {        /* load contributed values */
776 <                        readambval(&avs, ambinp);
776 >                        if (!readambval(&avs, ambinp)) {
777 >                                sprintf(errmsg,
778 >                                "ambient file corrupted near character %ld",
779 >                                                flen - n);
780 >                                error(WARNING, errmsg);
781 >                                break;
782 >                        }
783                          avinsert(avstore(&avs));
784                          n -= AMBVALSIZ;
785                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines