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.41 by greg, Wed Jul 10 13:33:56 1996 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 */
60 > static unsigned int  nambvals = 0;      /* total number of indirect values */
61 > static unsigned int  nambshare = 0;     /* number of values from file */
62   static unsigned long  ambclock = 0;     /* ambient access clock */
63   static unsigned long  lastsort = 0;     /* time of last value sort */
64   static long  sortintvl = SORT_INTVL;    /* time until next sort */
# Line 69 | Line 67 | static long  sortintvl = SORT_INTVL;   /* time until nex
67          /*
68           * Track access times unless we are sharing ambient values
69           * through memory on a multiprocessor, when we want to avoid
70 <         * claiming our own memory (copy on write).
70 >         * claiming our own memory (copy on write).  Go ahead anyway
71 >         * if more than two thirds of our values are unshared.
72           */
73 < #define tracktime       (shm_boundary == NULL || ambfp == NULL)
73 > #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
74  
75   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
76  
77   #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
78  
79   extern long  ftell(), lseek();
80 < static int  initambfile(), avsave(), avinsert(), sortambvals();
80 > static int  initambfile(), avsave(), avinsert(), sortambvals(), avlmemi();
81   static AMBVAL  *avstore();
82   #ifdef  F_SETLKW
83   static  aflock();
# Line 122 | Line 121 | double  newa;
121   setambient(afile)                       /* initialize calculation */
122   char  *afile;
123   {
124 <        long  headlen;
124 >        long  pos, flen;
125          AMBVAL  amb;
126                                                  /* init ambient limits */
127          setambres(ambres);
# Line 138 | Line 137 | char  *afile;
137                                                  /* open ambient file */
138          if ((ambfp = fopen(afile, "r+")) != NULL) {
139                  initambfile(0);
140 <                headlen = ftell(ambfp);
140 >                pos = ftell(ambfp);
141                  while (readambval(&amb, ambfp))
142                          avinsert(avstore(&amb));
143                                                  /* align */
144 <                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
144 >                pos += (long)nambvals*AMBVALSIZ;
145 >                flen = lseek(fileno(ambfp), 0L, 2);
146 >                if (flen != pos) {
147 >                        error(WARNING,
148 >                        "ignoring last %ld values in ambient file (corrupted)",
149 >                                        (flen - pos)/AMBVALSIZ);
150 >                        fseek(ambfp, pos, 0);
151 >                        ftruncate(fileno(ambfp), pos);
152 >                }
153 >                nambshare = nambvals;
154          } else if ((ambfp = fopen(afile, "w+")) != NULL)
155                  initambfile(1);
156          else {
# Line 176 | Line 184 | OBJECT obj;
184   }
185  
186  
187 < ambient(acol, r)                /* compute ambient component for ray */
187 > ambient(acol, r, nrm)           /* compute ambient component for ray */
188   COLOR  acol;
189   register RAY  *r;
190 + FVECT  nrm;
191   {
192          static int  rdepth = 0;                 /* ambient recursion */
193          double  d;
# Line 201 | Line 210 | register RAY  *r;
210                          goto dumbamb;
211                  return;
212          }
213 <                                                /* resort memory? */
214 <        sortambvals(0);
213 >
214 >        if (tracktime)                          /* sort to minimize thrashing */
215 >                sortambvals(0);
216                                                  /* get ambient value */
217          setcolor(acol, 0.0, 0.0, 0.0);
218 <        d = sumambient(acol, r, rdepth,
218 >        d = sumambient(acol, r, nrm, rdepth,
219                          &atrunk, thescene.cuorg, thescene.cusize);
220          if (d > FTINY) {
221                  scalecolor(acol, 1.0/d);
222                  return;
223          }
224          rdepth++;                               /* need to cache new value */
225 <        d = makeambient(acol, r, rdepth-1);
225 >        d = makeambient(acol, r, nrm, rdepth-1);
226          rdepth--;
227          if (d > FTINY)
228                  return;
229   dumbamb:                                        /* return global value */
230          copycolor(acol, ambval);
231 < #if  AMBVWT
222 <        if (nambvals == 0)
231 >        if (ambvwt <= 0 | nambvals == 0)
232                  return;
233 <        scalecolor(acol, (double)AMBVWT);
233 >        scalecolor(acol, (double)ambvwt);
234          addcolor(acol, avsum);                  /* average in computations */
235 <        d = 1.0/(AMBVWT+nambvals);
235 >        d = 1.0/(ambvwt+nambvals);
236          scalecolor(acol, d);
228 #endif
237   }
238  
239  
240   double
241 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
241 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
242   COLOR  acol;
243   register RAY  *r;
244 + FVECT  rn;
245   int  al;
246   AMBTREE  *at;
247   FVECT  c0;
# Line 249 | Line 258 | double s;
258                                          /* do this node */
259          for (av = at->alist; av != NULL; av = av->next) {
260                  if (tracktime)
261 <                        av->latick = ambclock++;
261 >                        av->latick = ambclock;
262                  /*
263                   *  Ambient level test.
264                   */
# Line 296 | Line 305 | double s;
305                  else
306                          wt = 1.0 / wt;
307                  wsum += wt;
308 <                extambient(ct, av, r->rop, r->ron);
308 >                extambient(ct, av, r->rop, rn);
309                  scalecolor(ct, wt);
310                  addcolor(acol, ct);
311          }
# Line 315 | Line 324 | double s;
324                                  break;
325                  }
326                  if (j == 3)
327 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
327 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
328          }
329          return(wsum);
330   }
331  
332  
333   double
334 < makeambient(acol, r, al)        /* make a new ambient value */
334 > makeambient(acol, r, rn, al)    /* make a new ambient value */
335   COLOR  acol;
336   register RAY  *r;
337 + FVECT  rn;
338   int  al;
339   {
340          AMBVAL  amb;
# Line 346 | Line 356 | int  al;
356          VCOPY(amb.gdir, gd);
357                                                  /* insert into tree */
358          avsave(&amb);                           /* and save to file */
359 +        if (rn != r->ron)
360 +                extambient(acol, &amb, r->rop, rn);     /* texture */
361          return(amb.rad);
362   }
363  
# Line 391 | Line 403 | int  creat;
403          setbuf(ambfp, bmalloc(BUFSIZ+8));
404          if (creat) {                    /* new file */
405                  newheader("RADIANCE", ambfp);
406 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
406 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
407                                  progname, colval(ambval,RED),
408                                  colval(ambval,GRN), colval(ambval,BLU),
409 <                                ambounce, ambacc);
409 >                                ambvwt, ambounce, ambacc);
410                  fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
411                                  ambdiv, ambssamp, ambres,
412                                  octname==NULL ? "" : octname);
# Line 540 | Line 552 | int    (*f)();
552   }
553  
554  
555 < static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
555 > static struct avl {
556 >        AMBVAL  *p;
557 >        unsigned long   t;
558 > }       *avlist1;                       /* ambient value list with ticks */
559 > static AMBVAL   **avlist2;              /* memory positions for sorting */
560   static int      i_avlist;               /* index for lists */
561  
562  
563   static
564   av2list(av)
565 < AMBVAL  *av;
565 > register AMBVAL *av;
566   {
567   #ifdef DEBUG
568          if (i_avlist >= nambvals)
569                  error(CONSISTENCY, "too many ambient values in av2list1");
570   #endif
571 <        avlist1[i_avlist] = avlist2[i_avlist] = av;
572 <        i_avlist++;
571 >        avlist1[i_avlist].p = avlist2[i_avlist] = av;
572 >        avlist1[i_avlist++].t = av->latick;
573   }
574  
575  
576   static int
577 < alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
578 < AMBVAL  **avp1, **avp2;
577 > alatcmp(av1, av2)                       /* compare ambient values for MRA */
578 > struct avl      *av1, *av2;
579   {
580 <        return((**avp2).latick - (**avp1).latick);
580 >        register long  lc = av2->t - av1->t;
581 >        return(lc<0 ? -1 : lc>0 ? 1 : 0);
582   }
583  
584  
# Line 573 | Line 590 | AMBVAL **avp1, **avp2;
590   }
591  
592  
593 < #ifdef DEBUG
593 > #if 1
594   static int
595   avlmemi(avaddr)                         /* find list position from address */
596   AMBVAL  *avaddr;
# Line 600 | Line 617 | int    always;
617          AMBVAL  tav, *tap, *pnext;
618          register int    i, j;
619                                          /* see if it's time yet */
620 <        if (!always && (ambclock < lastsort+sortintvl ||
620 >        if (!always && (ambclock++ < lastsort+sortintvl ||
621                          nambvals < SORT_THRESH))
622                  return;
623          /*
# Line 619 | Line 636 | int    always;
636           * tree will be rebuilt with the new accuracy parameter.
637           */
638          if (tracktime) {                /* allocate pointer arrays to sort */
622                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
639                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
640 <        } else
641 <                avlist1 = avlist2 = NULL;
642 <        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
643 <                if (avlist1 != NULL)
644 <                        free((char *)avlist1);
640 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
641 >        } else {
642 >                avlist2 = NULL;
643 >                avlist1 = NULL;
644 >        }
645 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
646 >                if (avlist2 != NULL)
647 >                        free((char *)avlist2);
648                  if (always) {           /* rebuild without sorting */
649                          copystruct(&oldatrunk, &atrunk);
650                          atrunk.alist = NULL;
# Line 652 | Line 671 | int    always;
671                  if (i_avlist < nambvals)
672                          error(CONSISTENCY, "missing ambient values in sortambvals");
673   #endif
674 <                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
674 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
675                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
676                  for (i = 0; i < nambvals; i++) {
677 <                        if (avlist1[i] == NULL)
677 >                        if (avlist1[i].p == NULL)
678                                  continue;
679                          tap = avlist2[i];
680                          copystruct(&tav, tap);
681 <                        for (j = i; (pnext = avlist1[j]) != tap;
681 >                        for (j = i; (pnext = avlist1[j].p) != tap;
682                                          j = avlmemi(pnext)) {
683                                  copystruct(avlist2[j], pnext);
684                                  avinsert(avlist2[j]);
685 <                                avlist1[j] = NULL;
685 >                                avlist1[j].p = NULL;
686                          }
687                          copystruct(avlist2[j], &tav);
688                          avinsert(avlist2[j]);
689 <                        avlist1[j] = NULL;
689 >                        avlist1[j].p = NULL;
690                  }
691                  free((char *)avlist1);
692                  free((char *)avlist2);
# Line 728 | Line 747 | ambsync()                      /* synchronize ambient file */
747                  if (fseek(ambinp, lastpos, 0) < 0)
748                          goto seekerr;
749                  while (n >= AMBVALSIZ) {        /* load contributed values */
750 <                        readambval(&avs, ambinp);
750 >                        if (!readambval(&avs, ambinp)) {
751 >                                sprintf(errmsg,
752 >                                "ambient file corrupted near character %ld",
753 >                                                flen - n);
754 >                                error(WARNING, errmsg);
755 >                                break;
756 >                        }
757                          avinsert(avstore(&avs));
758                          n -= AMBVALSIZ;
759                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines