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.36 by greg, Wed Feb 14 15:43:22 1996 UTC vs.
Revision 2.44 by gwlarson, Wed Jun 17 13:29:55 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 50 | 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 */
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 */
# Line 69 | Line 70 | static long  sortintvl = SORT_INTVL;   /* time until nex
70           * through memory on a multiprocessor, when we want to avoid
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 + #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();
80 < 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 121 | 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 137 | 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);
# Line 182 | Line 194 | 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 202 | Line 214 | FVECT  nrm;
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, nrm, rdepth,
# Line 219 | Line 232 | FVECT  nrm;
232                  return;
233   dumbamb:                                        /* return global value */
234          copycolor(acol, ambval);
235 <        if (ambvwt <= 0 | 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);
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  
# 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 394 | Line 413 | int  creat;
413          setbuf(ambfp, bmalloc(BUFSIZ+8));
414          if (creat) {                    /* new file */
415                  newheader("RADIANCE", ambfp);
416 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
416 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
417                                  progname, colval(ambval,RED),
418                                  colval(ambval,GRN), colval(ambval,BLU),
419 <                                ambounce, ambacc);
419 >                                ambvwt, ambounce, ambacc);
420                  fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
421                                  ambdiv, ambssamp, ambres,
422                                  octname==NULL ? "" : octname);
# Line 433 | Line 452 | avstore(aval)                          /* allocate memory and store aval */
452   register AMBVAL  *aval;
453   {
454          register AMBVAL  *av;
455 +        double  d;
456  
457          if ((av = newambval()) == NULL)
458                  error(SYSTEM, "out of memory in avstore");
459          copystruct(av, aval);
460          av->latick = ambclock;
461          av->next = NULL;
442        addcolor(avsum, av->val);       /* add to sum for averaging */
462          nambvals++;
463 +        d = bright(av->val);
464 +        if (d > FTINY) {                /* add to log sum for averaging */
465 +                avsum += log(d);
466 +                navsum++;
467 +        }
468          return(av);
469   }
470  
# Line 543 | Line 567 | int    (*f)();
567   }
568  
569  
570 < static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
570 > static struct avl {
571 >        AMBVAL  *p;
572 >        unsigned long   t;
573 > }       *avlist1;                       /* ambient value list with ticks */
574 > static AMBVAL   **avlist2;              /* memory positions for sorting */
575   static int      i_avlist;               /* index for lists */
576  
577  
578   static
579   av2list(av)
580 < AMBVAL  *av;
580 > register AMBVAL *av;
581   {
582   #ifdef DEBUG
583          if (i_avlist >= nambvals)
584                  error(CONSISTENCY, "too many ambient values in av2list1");
585   #endif
586 <        avlist1[i_avlist] = avlist2[i_avlist] = av;
587 <        i_avlist++;
586 >        avlist1[i_avlist].p = avlist2[i_avlist] = av;
587 >        avlist1[i_avlist++].t = av->latick;
588   }
589  
590  
591   static int
592 < alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
593 < AMBVAL  **avp1, **avp2;
592 > alatcmp(av1, av2)                       /* compare ambient values for MRA */
593 > struct avl      *av1, *av2;
594   {
595 <        return((**avp2).latick - (**avp1).latick);
595 >        register long  lc = av2->t - av1->t;
596 >        return(lc<0 ? -1 : lc>0 ? 1 : 0);
597   }
598  
599  
# Line 576 | Line 605 | AMBVAL **avp1, **avp2;
605   }
606  
607  
608 < #ifdef DEBUG
608 > #if 1
609   static int
610   avlmemi(avaddr)                         /* find list position from address */
611   AMBVAL  *avaddr;
# Line 603 | Line 632 | int    always;
632          AMBVAL  tav, *tap, *pnext;
633          register int    i, j;
634                                          /* see if it's time yet */
635 <        if (!always && (ambclock < lastsort+sortintvl ||
635 >        if (!always && (ambclock++ < lastsort+sortintvl ||
636                          nambvals < SORT_THRESH))
637                  return;
638          /*
# Line 622 | Line 651 | int    always;
651           * tree will be rebuilt with the new accuracy parameter.
652           */
653          if (tracktime) {                /* allocate pointer arrays to sort */
625                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
654                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
655 <        } else
656 <                avlist1 = avlist2 = NULL;
657 <        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
658 <                if (avlist1 != NULL)
659 <                        free((char *)avlist1);
655 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
656 >        } else {
657 >                avlist2 = NULL;
658 >                avlist1 = NULL;
659 >        }
660 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
661 >                if (avlist2 != NULL)
662 >                        free((char *)avlist2);
663                  if (always) {           /* rebuild without sorting */
664                          copystruct(&oldatrunk, &atrunk);
665                          atrunk.alist = NULL;
# Line 655 | Line 686 | int    always;
686                  if (i_avlist < nambvals)
687                          error(CONSISTENCY, "missing ambient values in sortambvals");
688   #endif
689 <                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
689 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
690                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
691                  for (i = 0; i < nambvals; i++) {
692 <                        if (avlist1[i] == NULL)
692 >                        if (avlist1[i].p == NULL)
693                                  continue;
694                          tap = avlist2[i];
695                          copystruct(&tav, tap);
696 <                        for (j = i; (pnext = avlist1[j]) != tap;
696 >                        for (j = i; (pnext = avlist1[j].p) != tap;
697                                          j = avlmemi(pnext)) {
698                                  copystruct(avlist2[j], pnext);
699                                  avinsert(avlist2[j]);
700 <                                avlist1[j] = NULL;
700 >                                avlist1[j].p = NULL;
701                          }
702                          copystruct(avlist2[j], &tav);
703                          avinsert(avlist2[j]);
704 <                        avlist1[j] = NULL;
704 >                        avlist1[j].p = NULL;
705                  }
706                  free((char *)avlist1);
707                  free((char *)avlist2);
# Line 731 | Line 762 | ambsync()                      /* synchronize ambient file */
762                  if (fseek(ambinp, lastpos, 0) < 0)
763                          goto seekerr;
764                  while (n >= AMBVALSIZ) {        /* load contributed values */
765 <                        readambval(&avs, ambinp);
765 >                        if (!readambval(&avs, ambinp)) {
766 >                                sprintf(errmsg,
767 >                                "ambient file corrupted near character %ld",
768 >                                                flen - n);
769 >                                error(WARNING, errmsg);
770 >                                break;
771 >                        }
772                          avinsert(avstore(&avs));
773                          n -= AMBVALSIZ;
774                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines