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.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 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 >        int     readonly = 0;
128 >        long  pos, flen;
129          AMBVAL  amb;
130                                                  /* init ambient limits */
131          setambres(ambres);
# Line 136 | 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 176 | 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 201 | Line 226 | register RAY  *r;
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, rdepth,
234 >        d = sumambient(acol, r, nrm, rdepth,
235                          &atrunk, thescene.cuorg, thescene.cusize);
236          if (d > FTINY) {
237                  scalecolor(acol, 1.0/d);
238                  return;
239          }
240          rdepth++;                               /* need to cache new value */
241 <        d = makeambient(acol, r, rdepth-1);
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
222 <        if (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);
253 < #endif
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 249 | 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 296 | Line 327 | double s;
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 315 | 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;
# Line 346 | 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 355 | 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 364 | 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);
369 <        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 391 | 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 430 | 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;
439        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 540 | 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 573 | 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 600 | 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 619 | Line 662 | int    always;
662           * tree will be rebuilt with the new accuracy parameter.
663           */
664          if (tracktime) {                /* allocate pointer arrays to sort */
622                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 652 | 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);
# Line 728 | 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