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.39 by greg, Tue Jul 9 19:37:09 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 60 | Line 57 | static int  nunflshed = 0;     /* number of unflushed ambi
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  
# 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 219 | Line 227 | FVECT  nrm;
227                  return;
228   dumbamb:                                        /* return global value */
229          copycolor(acol, ambval);
230 < #if  AMBVWT
223 <        if (nambvals == 0)
230 >        if (ambvwt <= 0 | nambvals == 0)
231                  return;
232 <        scalecolor(acol, (double)AMBVWT);
232 >        scalecolor(acol, (double)ambvwt);
233          addcolor(acol, avsum);                  /* average in computations */
234 <        d = 1.0/(AMBVWT+nambvals);
234 >        d = 1.0/(ambvwt+nambvals);
235          scalecolor(acol, d);
229 #endif
236   }
237  
238  
# Line 545 | Line 551 | int    (*f)();
551   }
552  
553  
554 < static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
554 > static struct avl {
555 >        AMBVAL  *p;
556 >        unsigned long   t;
557 > }       *avlist1;                       /* ambient value list with ticks */
558 > static AMBVAL   **avlist2;              /* memory positions for sorting */
559   static int      i_avlist;               /* index for lists */
560  
561  
562   static
563   av2list(av)
564 < AMBVAL  *av;
564 > register AMBVAL *av;
565   {
566   #ifdef DEBUG
567          if (i_avlist >= nambvals)
568                  error(CONSISTENCY, "too many ambient values in av2list1");
569   #endif
570 <        avlist1[i_avlist] = avlist2[i_avlist] = av;
571 <        i_avlist++;
570 >        avlist1[i_avlist].p = avlist2[i_avlist] = av;
571 >        avlist1[i_avlist++].t = av->latick;
572   }
573  
574  
575   static int
576 < alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
577 < AMBVAL  **avp1, **avp2;
576 > alatcmp(av1, av2)                       /* compare ambient values for MRA */
577 > struct avl      *av1, *av2;
578   {
579 <        return((**avp2).latick - (**avp1).latick);
579 >        register long  lc = av2->t - av1->t;
580 >        return(lc<0 ? -1 : lc>0 ? 1 : 0);
581   }
582  
583  
# Line 578 | Line 589 | AMBVAL **avp1, **avp2;
589   }
590  
591  
592 < #ifdef DEBUG
592 > #if 1
593   static int
594   avlmemi(avaddr)                         /* find list position from address */
595   AMBVAL  *avaddr;
# Line 624 | Line 635 | int    always;
635           * tree will be rebuilt with the new accuracy parameter.
636           */
637          if (tracktime) {                /* allocate pointer arrays to sort */
627                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
638                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
639 <        } else
640 <                avlist1 = avlist2 = NULL;
641 <        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
642 <                if (avlist1 != NULL)
643 <                        free((char *)avlist1);
639 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
640 >        } else {
641 >                avlist2 = NULL;
642 >                avlist1 = NULL;
643 >        }
644 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
645 >                if (avlist2 != NULL)
646 >                        free((char *)avlist2);
647                  if (always) {           /* rebuild without sorting */
648                          copystruct(&oldatrunk, &atrunk);
649                          atrunk.alist = NULL;
# Line 657 | Line 670 | int    always;
670                  if (i_avlist < nambvals)
671                          error(CONSISTENCY, "missing ambient values in sortambvals");
672   #endif
673 <                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
673 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
674                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
675                  for (i = 0; i < nambvals; i++) {
676 <                        if (avlist1[i] == NULL)
676 >                        if (avlist1[i].p == NULL)
677                                  continue;
678                          tap = avlist2[i];
679                          copystruct(&tav, tap);
680 <                        for (j = i; (pnext = avlist1[j]) != tap;
680 >                        for (j = i; (pnext = avlist1[j].p) != tap;
681                                          j = avlmemi(pnext)) {
682                                  copystruct(avlist2[j], pnext);
683                                  avinsert(avlist2[j]);
684 <                                avlist1[j] = NULL;
684 >                                avlist1[j].p = NULL;
685                          }
686                          copystruct(avlist2[j], &tav);
687                          avinsert(avlist2[j]);
688 <                        avlist1[j] = NULL;
688 >                        avlist1[j].p = NULL;
689                  }
690                  free((char *)avlist1);
691                  free((char *)avlist2);
# Line 733 | Line 746 | ambsync()                      /* synchronize ambient file */
746                  if (fseek(ambinp, lastpos, 0) < 0)
747                          goto seekerr;
748                  while (n >= AMBVALSIZ) {        /* load contributed values */
749 <                        readambval(&avs, ambinp);
749 >                        if (!readambval(&avs, ambinp)) {
750 >                                sprintf(errmsg,
751 >                                "ambient file corrupted near character %ld",
752 >                                                flen - n);
753 >                                error(WARNING, errmsg);
754 >                                break;
755 >                        }
756                          avinsert(avstore(&avs));
757                          n -= AMBVALSIZ;
758                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines