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.34 by greg, Mon Nov 6 12:03:13 1995 UTC vs.
Revision 2.42 by greg, Thu Jan 2 09:37:13 1997 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 >                        sprintf(errmsg,
148 >                        "ignoring last %ld values in ambient file (corrupted)",
149 >                                        (flen - pos)/AMBVALSIZ);
150 >                        error(WARNING, errmsg);
151 >                        fseek(ambfp, pos, 0);
152 >                        ftruncate(fileno(ambfp), pos);
153 >                }
154 >                nambshare = nambvals;
155          } else if ((ambfp = fopen(afile, "w+")) != NULL)
156                  initambfile(1);
157          else {
# Line 202 | Line 211 | FVECT  nrm;
211                          goto dumbamb;
212                  return;
213          }
214 <                                                /* resort memory? */
215 <        sortambvals(0);
214 >
215 >        if (tracktime)                          /* sort to minimize thrashing */
216 >                sortambvals(0);
217                                                  /* get ambient value */
218          setcolor(acol, 0.0, 0.0, 0.0);
219          d = sumambient(acol, r, nrm, rdepth,
# Line 219 | Line 229 | FVECT  nrm;
229                  return;
230   dumbamb:                                        /* return global value */
231          copycolor(acol, ambval);
232 < #if  AMBVWT
223 <        if (nambvals == 0)
232 >        if (ambvwt <= 0 | nambvals == 0)
233                  return;
234 <        scalecolor(acol, (double)AMBVWT);
234 >        scalecolor(acol, (double)ambvwt);
235          addcolor(acol, avsum);                  /* average in computations */
236 <        d = 1.0/(AMBVWT+nambvals);
236 >        d = 1.0/(ambvwt+nambvals);
237          scalecolor(acol, d);
229 #endif
238   }
239  
240  
# Line 251 | Line 259 | double s;
259                                          /* do this node */
260          for (av = at->alist; av != NULL; av = av->next) {
261                  if (tracktime)
262 <                        av->latick = ambclock++;
262 >                        av->latick = ambclock;
263                  /*
264                   *  Ambient level test.
265                   */
# Line 396 | Line 404 | int  creat;
404          setbuf(ambfp, bmalloc(BUFSIZ+8));
405          if (creat) {                    /* new file */
406                  newheader("RADIANCE", ambfp);
407 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
407 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
408                                  progname, colval(ambval,RED),
409                                  colval(ambval,GRN), colval(ambval,BLU),
410 <                                ambounce, ambacc);
410 >                                ambvwt, ambounce, ambacc);
411                  fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
412                                  ambdiv, ambssamp, ambres,
413                                  octname==NULL ? "" : octname);
# Line 545 | Line 553 | int    (*f)();
553   }
554  
555  
556 < static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
556 > static struct avl {
557 >        AMBVAL  *p;
558 >        unsigned long   t;
559 > }       *avlist1;                       /* ambient value list with ticks */
560 > static AMBVAL   **avlist2;              /* memory positions for sorting */
561   static int      i_avlist;               /* index for lists */
562  
563  
564   static
565   av2list(av)
566 < AMBVAL  *av;
566 > register AMBVAL *av;
567   {
568   #ifdef DEBUG
569          if (i_avlist >= nambvals)
570                  error(CONSISTENCY, "too many ambient values in av2list1");
571   #endif
572 <        avlist1[i_avlist] = avlist2[i_avlist] = av;
573 <        i_avlist++;
572 >        avlist1[i_avlist].p = avlist2[i_avlist] = av;
573 >        avlist1[i_avlist++].t = av->latick;
574   }
575  
576  
577   static int
578 < alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
579 < AMBVAL  **avp1, **avp2;
578 > alatcmp(av1, av2)                       /* compare ambient values for MRA */
579 > struct avl      *av1, *av2;
580   {
581 <        return((**avp2).latick - (**avp1).latick);
581 >        register long  lc = av2->t - av1->t;
582 >        return(lc<0 ? -1 : lc>0 ? 1 : 0);
583   }
584  
585  
# Line 578 | Line 591 | AMBVAL **avp1, **avp2;
591   }
592  
593  
594 < #ifdef DEBUG
594 > #if 1
595   static int
596   avlmemi(avaddr)                         /* find list position from address */
597   AMBVAL  *avaddr;
# Line 605 | Line 618 | int    always;
618          AMBVAL  tav, *tap, *pnext;
619          register int    i, j;
620                                          /* see if it's time yet */
621 <        if (!always && (ambclock < lastsort+sortintvl ||
621 >        if (!always && (ambclock++ < lastsort+sortintvl ||
622                          nambvals < SORT_THRESH))
623                  return;
624          /*
# Line 624 | Line 637 | int    always;
637           * tree will be rebuilt with the new accuracy parameter.
638           */
639          if (tracktime) {                /* allocate pointer arrays to sort */
627                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
640                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
641 <        } else
642 <                avlist1 = avlist2 = NULL;
643 <        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
644 <                if (avlist1 != NULL)
645 <                        free((char *)avlist1);
641 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
642 >        } else {
643 >                avlist2 = NULL;
644 >                avlist1 = NULL;
645 >        }
646 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
647 >                if (avlist2 != NULL)
648 >                        free((char *)avlist2);
649                  if (always) {           /* rebuild without sorting */
650                          copystruct(&oldatrunk, &atrunk);
651                          atrunk.alist = NULL;
# Line 657 | Line 672 | int    always;
672                  if (i_avlist < nambvals)
673                          error(CONSISTENCY, "missing ambient values in sortambvals");
674   #endif
675 <                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
675 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
676                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
677                  for (i = 0; i < nambvals; i++) {
678 <                        if (avlist1[i] == NULL)
678 >                        if (avlist1[i].p == NULL)
679                                  continue;
680                          tap = avlist2[i];
681                          copystruct(&tav, tap);
682 <                        for (j = i; (pnext = avlist1[j]) != tap;
682 >                        for (j = i; (pnext = avlist1[j].p) != tap;
683                                          j = avlmemi(pnext)) {
684                                  copystruct(avlist2[j], pnext);
685                                  avinsert(avlist2[j]);
686 <                                avlist1[j] = NULL;
686 >                                avlist1[j].p = NULL;
687                          }
688                          copystruct(avlist2[j], &tav);
689                          avinsert(avlist2[j]);
690 <                        avlist1[j] = NULL;
690 >                        avlist1[j].p = NULL;
691                  }
692                  free((char *)avlist1);
693                  free((char *)avlist2);
# Line 733 | Line 748 | ambsync()                      /* synchronize ambient file */
748                  if (fseek(ambinp, lastpos, 0) < 0)
749                          goto seekerr;
750                  while (n >= AMBVALSIZ) {        /* load contributed values */
751 <                        readambval(&avs, ambinp);
751 >                        if (!readambval(&avs, ambinp)) {
752 >                                sprintf(errmsg,
753 >                                "ambient file corrupted near character %ld",
754 >                                                flen - n);
755 >                                error(WARNING, errmsg);
756 >                                break;
757 >                        }
758                          avinsert(avstore(&avs));
759                          n -= AMBVALSIZ;
760                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines