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.38 by greg, Tue Jul 9 13:32:35 1996 UTC vs.
Revision 2.46 by gwlarson, Sun Jan 10 12:45:19 1999 UTC

# 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 +        int     readonly = 0;
128          long  pos, flen;
129          AMBVAL  amb;
130                                                  /* init ambient limits */
# Line 135 | Line 139 | char  *afile;
139                  return;
140          }
141                                                  /* open ambient file */
142 <        if ((ambfp = fopen(afile, "r+")) != NULL) {
143 <                initambfile(0);
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 */
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 <                        error(WARNING,
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 <                nambshare = nambvals;
171 <        } else if ((ambfp = fopen(afile, "w+")) != NULL)
172 <                initambfile(1);
156 <        else {
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 190 | Line 206 | 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 210 | Line 226 | FVECT  nrm;
226                          goto dumbamb;
227                  return;
228          }
229 <                                                /* resort memory? */
230 <        sortambvals(0);
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, nrm, rdepth,
# Line 227 | Line 244 | FVECT  nrm;
244                  return;
245   dumbamb:                                        /* return global value */
246          copycolor(acol, ambval);
247 <        if (ambvwt <= 0 | nambvals == 0)
247 >        if (ambvwt <= 0 | navsum == 0)
248                  return;
249 <        scalecolor(acol, (double)ambvwt);
250 <        addcolor(acol, avsum);                  /* average in computations */
251 <        d = 1.0/(ambvwt+nambvals);
252 <        scalecolor(acol, d);
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  
# Line 257 | Line 280 | double s;
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 366 | 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 375 | 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);
380 <        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 402 | 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 441 | 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");
470          copystruct(av, aval);
471          av->latick = ambclock;
472          av->next = NULL;
450        addcolor(avsum, av->val);       /* add to sum for averaging */
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 551 | 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 <        register long  lc = (**avp2).latick - (**avp1).latick;
606 >        register long  lc = av2->t - av1->t;
607          return(lc<0 ? -1 : lc>0 ? 1 : 0);
608   }
609  
# Line 585 | Line 616 | AMBVAL **avp1, **avp2;
616   }
617  
618  
619 < #if  1
619 > #if 1
620   static int
621   avlmemi(avaddr)                         /* find list position from address */
622   AMBVAL  *avaddr;
# Line 612 | Line 643 | int    always;
643          AMBVAL  tav, *tap, *pnext;
644          register int    i, j;
645                                          /* see if it's time yet */
646 <        if (!always && (ambclock < lastsort+sortintvl ||
646 >        if (!always && (ambclock++ < lastsort+sortintvl ||
647                          nambvals < SORT_THRESH))
648                  return;
649          /*
# Line 631 | Line 662 | int    always;
662           * tree will be rebuilt with the new accuracy parameter.
663           */
664          if (tracktime) {                /* allocate pointer arrays to sort */
634                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 664 | 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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines