--- ray/src/rt/ambient.c 1996/02/14 15:43:22 2.36 +++ ray/src/rt/ambient.c 1998/06/17 13:29:55 2.44 @@ -1,4 +1,4 @@ -/* Copyright (c) 1995 Regents of the University of California */ +/* Copyright (c) 1996 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -50,13 +50,14 @@ static int nunflshed = 0; /* number of unflushed ambi #endif #endif #ifndef SORT_INTVL -#define SORT_INTVL (SORT_THRESH*256) +#define SORT_INTVL (SORT_THRESH<<1) #endif #ifndef MAX_SORT_INTVL -#define MAX_SORT_INTVL (SORT_INTVL<<4) +#define MAX_SORT_INTVL (SORT_INTVL<<6) #endif -static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ +static double avsum = 0.; /* computed ambient value sum (log) */ +static unsigned int navsum = 0; /* number of values in avsum */ static unsigned int nambvals = 0; /* total number of indirect values */ static unsigned int nambshare = 0; /* number of values from file */ static unsigned long ambclock = 0; /* ambient access clock */ @@ -69,15 +70,17 @@ static long sortintvl = SORT_INTVL; /* time until nex * through memory on a multiprocessor, when we want to avoid * claiming our own memory (copy on write). Go ahead anyway * if more than two thirds of our values are unshared. + * Compile with -Dtracktime=0 to turn this code off. */ +#ifndef tracktime #define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) +#endif #define AMBFLUSH (BUFSIZ/AMBVALSIZ) #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) -extern long ftell(), lseek(); -static int initambfile(), avsave(), avinsert(), sortambvals(); +static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); static AMBVAL *avstore(); #ifdef F_SETLKW static aflock(); @@ -121,7 +124,7 @@ double newa; setambient(afile) /* initialize calculation */ char *afile; { - long headlen; + long pos, flen; AMBVAL amb; /* init ambient limits */ setambres(ambres); @@ -137,11 +140,20 @@ char *afile; /* open ambient file */ if ((ambfp = fopen(afile, "r+")) != NULL) { initambfile(0); - headlen = ftell(ambfp); + pos = ftell(ambfp); while (readambval(&amb, ambfp)) avinsert(avstore(&amb)); /* align */ - fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); + pos += (long)nambvals*AMBVALSIZ; + flen = lseek(fileno(ambfp), 0L, 2); + if (flen != pos) { + sprintf(errmsg, + "ignoring last %ld values in ambient file (corrupted)", + (flen - pos)/AMBVALSIZ); + error(WARNING, errmsg); + fseek(ambfp, pos, 0); + ftruncate(fileno(ambfp), pos); + } nambshare = nambvals; } else if ((ambfp = fopen(afile, "w+")) != NULL) initambfile(1); @@ -182,7 +194,7 @@ register RAY *r; FVECT nrm; { static int rdepth = 0; /* ambient recursion */ - double d; + double d, l; if (ambdiv <= 0) /* no ambient calculation */ goto dumbamb; @@ -202,8 +214,9 @@ FVECT nrm; goto dumbamb; return; } - /* resort memory? */ - sortambvals(0); + + if (tracktime) /* sort to minimize thrashing */ + sortambvals(0); /* get ambient value */ setcolor(acol, 0.0, 0.0, 0.0); d = sumambient(acol, r, nrm, rdepth, @@ -219,12 +232,18 @@ FVECT nrm; return; dumbamb: /* return global value */ copycolor(acol, ambval); - if (ambvwt <= 0 | nambvals == 0) + if (ambvwt <= 0 | navsum == 0) return; - scalecolor(acol, (double)ambvwt); - addcolor(acol, avsum); /* average in computations */ - d = 1.0/(ambvwt+nambvals); - scalecolor(acol, d); + l = bright(ambval); /* average in computations */ + if (l > FTINY) { + d = (log(l)*(double)ambvwt + avsum) / + (double)(ambvwt + navsum); + d = exp(d) / l; + scalecolor(acol, d); /* apply color of ambval */ + } else { + d = exp( avsum / (double)navsum ); + setcolor(acol, d, d, d); /* neutral color */ + } } @@ -249,7 +268,7 @@ double s; /* do this node */ for (av = at->alist; av != NULL; av = av->next) { if (tracktime) - av->latick = ambclock++; + av->latick = ambclock; /* * Ambient level test. */ @@ -394,10 +413,10 @@ int creat; setbuf(ambfp, bmalloc(BUFSIZ+8)); if (creat) { /* new file */ newheader("RADIANCE", ambfp); - fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", + fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", progname, colval(ambval,RED), colval(ambval,GRN), colval(ambval,BLU), - ambounce, ambacc); + ambvwt, ambounce, ambacc); fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", ambdiv, ambssamp, ambres, octname==NULL ? "" : octname); @@ -433,14 +452,19 @@ avstore(aval) /* allocate memory and store aval */ register AMBVAL *aval; { register AMBVAL *av; + double d; if ((av = newambval()) == NULL) error(SYSTEM, "out of memory in avstore"); copystruct(av, aval); av->latick = ambclock; av->next = NULL; - addcolor(avsum, av->val); /* add to sum for averaging */ nambvals++; + d = bright(av->val); + if (d > FTINY) { /* add to log sum for averaging */ + avsum += log(d); + navsum++; + } return(av); } @@ -543,28 +567,33 @@ int (*f)(); } -static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ +static struct avl { + AMBVAL *p; + unsigned long t; +} *avlist1; /* ambient value list with ticks */ +static AMBVAL **avlist2; /* memory positions for sorting */ static int i_avlist; /* index for lists */ static av2list(av) -AMBVAL *av; +register AMBVAL *av; { #ifdef DEBUG if (i_avlist >= nambvals) error(CONSISTENCY, "too many ambient values in av2list1"); #endif - avlist1[i_avlist] = avlist2[i_avlist] = av; - i_avlist++; + avlist1[i_avlist].p = avlist2[i_avlist] = av; + avlist1[i_avlist++].t = av->latick; } static int -alatcmp(avp1, avp2) /* compare ambient values for MRA */ -AMBVAL **avp1, **avp2; +alatcmp(av1, av2) /* compare ambient values for MRA */ +struct avl *av1, *av2; { - return((**avp2).latick - (**avp1).latick); + register long lc = av2->t - av1->t; + return(lc<0 ? -1 : lc>0 ? 1 : 0); } @@ -576,7 +605,7 @@ AMBVAL **avp1, **avp2; } -#ifdef DEBUG +#if 1 static int avlmemi(avaddr) /* find list position from address */ AMBVAL *avaddr; @@ -603,7 +632,7 @@ int always; AMBVAL tav, *tap, *pnext; register int i, j; /* see if it's time yet */ - if (!always && (ambclock < lastsort+sortintvl || + if (!always && (ambclock++ < lastsort+sortintvl || nambvals < SORT_THRESH)) return; /* @@ -622,13 +651,15 @@ int always; * tree will be rebuilt with the new accuracy parameter. */ if (tracktime) { /* allocate pointer arrays to sort */ - avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); - } else - avlist1 = avlist2 = NULL; - if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ - if (avlist1 != NULL) - free((char *)avlist1); + avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); + } else { + avlist2 = NULL; + avlist1 = NULL; + } + if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ + if (avlist2 != NULL) + free((char *)avlist2); if (always) { /* rebuild without sorting */ copystruct(&oldatrunk, &atrunk); atrunk.alist = NULL; @@ -655,22 +686,22 @@ int always; if (i_avlist < nambvals) error(CONSISTENCY, "missing ambient values in sortambvals"); #endif - qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); + qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); for (i = 0; i < nambvals; i++) { - if (avlist1[i] == NULL) + if (avlist1[i].p == NULL) continue; tap = avlist2[i]; copystruct(&tav, tap); - for (j = i; (pnext = avlist1[j]) != tap; + for (j = i; (pnext = avlist1[j].p) != tap; j = avlmemi(pnext)) { copystruct(avlist2[j], pnext); avinsert(avlist2[j]); - avlist1[j] = NULL; + avlist1[j].p = NULL; } copystruct(avlist2[j], &tav); avinsert(avlist2[j]); - avlist1[j] = NULL; + avlist1[j].p = NULL; } free((char *)avlist1); free((char *)avlist2); @@ -731,7 +762,13 @@ ambsync() /* synchronize ambient file */ if (fseek(ambinp, lastpos, 0) < 0) goto seekerr; while (n >= AMBVALSIZ) { /* load contributed values */ - readambval(&avs, ambinp); + if (!readambval(&avs, ambinp)) { + sprintf(errmsg, + "ambient file corrupted near character %ld", + flen - n); + error(WARNING, errmsg); + break; + } avinsert(avstore(&avs)); n -= AMBVALSIZ; }