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.33 by greg, Tue Oct 24 13:33:23 1995 UTC vs.
Revision 2.45 by gwlarson, Wed Aug 12 18:16:07 1998 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 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21   #ifndef  OCTSCALE
22   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23   #endif
24 #ifndef  AMBVWT
25 #define  AMBVWT         250     /* relative ambient value weight (# calcs) */
26 #endif
24  
25   typedef struct ambtree {
26          AMBVAL  *alist;         /* ambient value list */
# Line 53 | Line 50 | static int  nunflshed = 0;     /* number of unflushed ambi
50   #endif
51   #endif
52   #ifndef SORT_INTVL
53 < #define SORT_INTVL      (SORT_THRESH*256)
53 > #define SORT_INTVL      (SORT_THRESH<<1)
54   #endif
55   #ifndef MAX_SORT_INTVL
56 < #define MAX_SORT_INTVL  (SORT_INTVL<<4)
56 > #define MAX_SORT_INTVL  (SORT_INTVL<<6)
57   #endif
58  
59 < static COLOR  avsum = BLKCOLOR;         /* computed ambient value sum */
60 < static unsigned int  nambvals = 0;      /* number of computed ambient values */
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 */
64   static unsigned long  lastsort = 0;     /* time of last value sort */
65   static long  sortintvl = SORT_INTVL;    /* time until next sort */
# Line 69 | 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)
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();
81 < 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 122 | Line 124 | double  newa;
124   setambient(afile)                       /* initialize calculation */
125   char  *afile;
126   {
127 <        long  headlen;
127 >        long  pos, flen;
128          AMBVAL  amb;
129                                                  /* init ambient limits */
130          setambres(ambres);
# Line 138 | Line 140 | char  *afile;
140                                                  /* open ambient file */
141          if ((ambfp = fopen(afile, "r+")) != NULL) {
142                  initambfile(0);
143 <                headlen = ftell(ambfp);
143 >                pos = ftell(ambfp);
144                  while (readambval(&amb, ambfp))
145                          avinsert(avstore(&amb));
146                                                  /* align */
147 <                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
147 >                pos += (long)nambvals*AMBVALSIZ;
148 >                flen = lseek(fileno(ambfp), 0L, 2);
149 >                if (flen != pos) {
150 >                        sprintf(errmsg,
151 >                        "ignoring last %ld values in ambient file (corrupted)",
152 >                                        (flen - pos)/AMBVALSIZ);
153 >                        error(WARNING, errmsg);
154 >                        fseek(ambfp, pos, 0);
155 >                        ftruncate(fileno(ambfp), pos);
156 >                }
157 >                nambshare = nambvals;
158          } else if ((ambfp = fopen(afile, "w+")) != NULL)
159                  initambfile(1);
160          else {
# Line 176 | Line 188 | OBJECT obj;
188   }
189  
190  
191 < ambient(acol, r)                /* compute ambient component for ray */
191 > ambient(acol, r, nrm)           /* compute ambient component for ray */
192   COLOR  acol;
193   register RAY  *r;
194 + FVECT  nrm;
195   {
196          static int  rdepth = 0;                 /* ambient recursion */
197 <        double  d;
197 >        double  d, l;
198  
199          if (ambdiv <= 0)                        /* no ambient calculation */
200                  goto dumbamb;
# Line 201 | Line 214 | register RAY  *r;
214                          goto dumbamb;
215                  return;
216          }
217 <                                                /* resort memory? */
218 <        sortambvals(0);
217 >
218 >        if (tracktime)                          /* sort to minimize thrashing */
219 >                sortambvals(0);
220                                                  /* get ambient value */
221          setcolor(acol, 0.0, 0.0, 0.0);
222 <        d = sumambient(acol, r, rdepth,
222 >        d = sumambient(acol, r, nrm, rdepth,
223                          &atrunk, thescene.cuorg, thescene.cusize);
224          if (d > FTINY) {
225                  scalecolor(acol, 1.0/d);
226                  return;
227          }
228          rdepth++;                               /* need to cache new value */
229 <        d = makeambient(acol, r, rdepth-1);
229 >        d = makeambient(acol, r, nrm, rdepth-1);
230          rdepth--;
231          if (d > FTINY)
232                  return;
233   dumbamb:                                        /* return global value */
234          copycolor(acol, ambval);
235 < #if  AMBVWT
222 <        if (nambvals == 0)
235 >        if (ambvwt <= 0 | navsum == 0)
236                  return;
237 <        scalecolor(acol, (double)AMBVWT);
238 <        addcolor(acol, avsum);                  /* average in computations */
239 <        d = 1.0/(AMBVWT+nambvals);
240 <        scalecolor(acol, d);
241 < #endif
237 >        l = bright(ambval);                     /* average in computations */
238 >        if (l > FTINY) {
239 >                d = (log(l)*(double)ambvwt + avsum) /
240 >                                (double)(ambvwt + navsum);
241 >                d = exp(d) / l;
242 >                scalecolor(acol, d);            /* apply color of ambval */
243 >        } else {
244 >                d = exp( avsum / (double)navsum );
245 >                setcolor(acol, d, d, d);        /* neutral color */
246 >        }
247   }
248  
249  
250   double
251 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
251 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
252   COLOR  acol;
253   register RAY  *r;
254 + FVECT  rn;
255   int  al;
256   AMBTREE  *at;
257   FVECT  c0;
# Line 249 | Line 268 | double s;
268                                          /* do this node */
269          for (av = at->alist; av != NULL; av = av->next) {
270                  if (tracktime)
271 <                        av->latick = ambclock++;
271 >                        av->latick = ambclock;
272                  /*
273                   *  Ambient level test.
274                   */
# Line 296 | Line 315 | double s;
315                  else
316                          wt = 1.0 / wt;
317                  wsum += wt;
318 <                extambient(ct, av, r->rop, r->ron);
318 >                extambient(ct, av, r->rop, rn);
319                  scalecolor(ct, wt);
320                  addcolor(acol, ct);
321          }
# Line 315 | Line 334 | double s;
334                                  break;
335                  }
336                  if (j == 3)
337 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
337 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
338          }
339          return(wsum);
340   }
341  
342  
343   double
344 < makeambient(acol, r, al)        /* make a new ambient value */
344 > makeambient(acol, r, rn, al)    /* make a new ambient value */
345   COLOR  acol;
346   register RAY  *r;
347 + FVECT  rn;
348   int  al;
349   {
350          AMBVAL  amb;
# Line 346 | Line 366 | int  al;
366          VCOPY(amb.gdir, gd);
367                                                  /* insert into tree */
368          avsave(&amb);                           /* and save to file */
369 +        if (rn != r->ron)
370 +                extambient(acol, &amb, r->rop, rn);     /* texture */
371          return(amb.rad);
372   }
373  
# Line 355 | Line 377 | COLOR  cr;
377   register AMBVAL  *ap;
378   FVECT  pv, nv;
379   {
380 <        FVECT  v1, v2;
380 >        FVECT  v1;
381          register int  i;
382          double  d;
383  
# Line 364 | Line 386 | FVECT  pv, nv;
386          for (i = 0; i < 3; i++)
387                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
388                                          /* gradient due to rotation */
389 <        VCOPY(v1, ap->dir);
390 <        fcross(v2, v1, nv);
369 <        d += DOT(ap->gdir, v2);
389 >        VCROSS(v1, ap->dir, nv);
390 >        d += DOT(ap->gdir, v1);
391          if (d <= 0.0) {
392                  setcolor(cr, 0.0, 0.0, 0.0);
393                  return;
# Line 391 | Line 412 | int  creat;
412          setbuf(ambfp, bmalloc(BUFSIZ+8));
413          if (creat) {                    /* new file */
414                  newheader("RADIANCE", ambfp);
415 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
415 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
416                                  progname, colval(ambval,RED),
417                                  colval(ambval,GRN), colval(ambval,BLU),
418 <                                ambounce, ambacc);
418 >                                ambvwt, ambounce, ambacc);
419                  fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
420                                  ambdiv, ambssamp, ambres,
421                                  octname==NULL ? "" : octname);
# Line 430 | Line 451 | avstore(aval)                          /* allocate memory and store aval */
451   register AMBVAL  *aval;
452   {
453          register AMBVAL  *av;
454 +        double  d;
455  
456          if ((av = newambval()) == NULL)
457                  error(SYSTEM, "out of memory in avstore");
458          copystruct(av, aval);
459          av->latick = ambclock;
460          av->next = NULL;
439        addcolor(avsum, av->val);       /* add to sum for averaging */
461          nambvals++;
462 +        d = bright(av->val);
463 +        if (d > FTINY) {                /* add to log sum for averaging */
464 +                avsum += log(d);
465 +                navsum++;
466 +        }
467          return(av);
468   }
469  
# Line 540 | Line 566 | int    (*f)();
566   }
567  
568  
569 < static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
569 > static struct avl {
570 >        AMBVAL  *p;
571 >        unsigned long   t;
572 > }       *avlist1;                       /* ambient value list with ticks */
573 > static AMBVAL   **avlist2;              /* memory positions for sorting */
574   static int      i_avlist;               /* index for lists */
575  
576  
577   static
578   av2list(av)
579 < AMBVAL  *av;
579 > register AMBVAL *av;
580   {
581   #ifdef DEBUG
582          if (i_avlist >= nambvals)
583                  error(CONSISTENCY, "too many ambient values in av2list1");
584   #endif
585 <        avlist1[i_avlist] = avlist2[i_avlist] = av;
586 <        i_avlist++;
585 >        avlist1[i_avlist].p = avlist2[i_avlist] = av;
586 >        avlist1[i_avlist++].t = av->latick;
587   }
588  
589  
590   static int
591 < alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
592 < AMBVAL  **avp1, **avp2;
591 > alatcmp(av1, av2)                       /* compare ambient values for MRA */
592 > struct avl      *av1, *av2;
593   {
594 <        return((**avp2).latick - (**avp1).latick);
594 >        register long  lc = av2->t - av1->t;
595 >        return(lc<0 ? -1 : lc>0 ? 1 : 0);
596   }
597  
598  
# Line 573 | Line 604 | AMBVAL **avp1, **avp2;
604   }
605  
606  
607 < #ifdef DEBUG
607 > #if 1
608   static int
609   avlmemi(avaddr)                         /* find list position from address */
610   AMBVAL  *avaddr;
# Line 600 | Line 631 | int    always;
631          AMBVAL  tav, *tap, *pnext;
632          register int    i, j;
633                                          /* see if it's time yet */
634 <        if (!always && (ambclock < lastsort+sortintvl ||
634 >        if (!always && (ambclock++ < lastsort+sortintvl ||
635                          nambvals < SORT_THRESH))
636                  return;
637          /*
# Line 619 | Line 650 | int    always;
650           * tree will be rebuilt with the new accuracy parameter.
651           */
652          if (tracktime) {                /* allocate pointer arrays to sort */
622                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
653                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
654 <        } else
655 <                avlist1 = avlist2 = NULL;
656 <        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
657 <                if (avlist1 != NULL)
658 <                        free((char *)avlist1);
654 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
655 >        } else {
656 >                avlist2 = NULL;
657 >                avlist1 = NULL;
658 >        }
659 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
660 >                if (avlist2 != NULL)
661 >                        free((char *)avlist2);
662                  if (always) {           /* rebuild without sorting */
663                          copystruct(&oldatrunk, &atrunk);
664                          atrunk.alist = NULL;
# Line 652 | Line 685 | int    always;
685                  if (i_avlist < nambvals)
686                          error(CONSISTENCY, "missing ambient values in sortambvals");
687   #endif
688 <                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
688 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
689                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
690                  for (i = 0; i < nambvals; i++) {
691 <                        if (avlist1[i] == NULL)
691 >                        if (avlist1[i].p == NULL)
692                                  continue;
693                          tap = avlist2[i];
694                          copystruct(&tav, tap);
695 <                        for (j = i; (pnext = avlist1[j]) != tap;
695 >                        for (j = i; (pnext = avlist1[j].p) != tap;
696                                          j = avlmemi(pnext)) {
697                                  copystruct(avlist2[j], pnext);
698                                  avinsert(avlist2[j]);
699 <                                avlist1[j] = NULL;
699 >                                avlist1[j].p = NULL;
700                          }
701                          copystruct(avlist2[j], &tav);
702                          avinsert(avlist2[j]);
703 <                        avlist1[j] = NULL;
703 >                        avlist1[j].p = NULL;
704                  }
705                  free((char *)avlist1);
706                  free((char *)avlist2);
# Line 728 | Line 761 | ambsync()                      /* synchronize ambient file */
761                  if (fseek(ambinp, lastpos, 0) < 0)
762                          goto seekerr;
763                  while (n >= AMBVALSIZ) {        /* load contributed values */
764 <                        readambval(&avs, ambinp);
764 >                        if (!readambval(&avs, ambinp)) {
765 >                                sprintf(errmsg,
766 >                                "ambient file corrupted near character %ld",
767 >                                                flen - n);
768 >                                error(WARNING, errmsg);
769 >                                break;
770 >                        }
771                          avinsert(avstore(&avs));
772                          n -= AMBVALSIZ;
773                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines