--- ray/src/rt/ambient.c 1995/10/24 13:33:23 2.33 +++ 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"; @@ -21,9 +21,6 @@ static char SCCSid[] = "$SunId$ LBL"; #ifndef OCTSCALE #define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ #endif -#ifndef AMBVWT -#define AMBVWT 250 /* relative ambient value weight (# calcs) */ -#endif typedef struct ambtree { AMBVAL *alist; /* ambient value list */ @@ -53,14 +50,16 @@ 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 unsigned int nambvals = 0; /* number of computed ambient values */ +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 */ static unsigned long lastsort = 0; /* time of last value sort */ static long sortintvl = SORT_INTVL; /* time until next sort */ @@ -69,16 +68,19 @@ static long sortintvl = SORT_INTVL; /* time until nex /* * Track access times unless we are sharing ambient values * through memory on a multiprocessor, when we want to avoid - * claiming our own memory (copy on write). + * 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. */ -#define tracktime (shm_boundary == NULL || ambfp == NULL) +#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(); @@ -122,7 +124,7 @@ double newa; setambient(afile) /* initialize calculation */ char *afile; { - long headlen; + long pos, flen; AMBVAL amb; /* init ambient limits */ setambres(ambres); @@ -138,11 +140,21 @@ 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); else { @@ -176,12 +188,13 @@ OBJECT obj; } -ambient(acol, r) /* compute ambient component for ray */ +ambient(acol, r, nrm) /* compute ambient component for ray */ COLOR acol; register RAY *r; +FVECT nrm; { static int rdepth = 0; /* ambient recursion */ - double d; + double d, l; if (ambdiv <= 0) /* no ambient calculation */ goto dumbamb; @@ -201,38 +214,44 @@ register RAY *r; 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, rdepth, + d = sumambient(acol, r, nrm, rdepth, &atrunk, thescene.cuorg, thescene.cusize); if (d > FTINY) { scalecolor(acol, 1.0/d); return; } rdepth++; /* need to cache new value */ - d = makeambient(acol, r, rdepth-1); + d = makeambient(acol, r, nrm, rdepth-1); rdepth--; if (d > FTINY) return; dumbamb: /* return global value */ copycolor(acol, ambval); -#if AMBVWT - if (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); -#endif + 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 */ + } } double -sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ +sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ COLOR acol; register RAY *r; +FVECT rn; int al; AMBTREE *at; FVECT c0; @@ -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. */ @@ -296,7 +315,7 @@ double s; else wt = 1.0 / wt; wsum += wt; - extambient(ct, av, r->rop, r->ron); + extambient(ct, av, r->rop, rn); scalecolor(ct, wt); addcolor(acol, ct); } @@ -315,16 +334,17 @@ double s; break; } if (j == 3) - wsum += sumambient(acol, r, al, at->kid+i, ck0, s); + wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); } return(wsum); } double -makeambient(acol, r, al) /* make a new ambient value */ +makeambient(acol, r, rn, al) /* make a new ambient value */ COLOR acol; register RAY *r; +FVECT rn; int al; { AMBVAL amb; @@ -346,6 +366,8 @@ int al; VCOPY(amb.gdir, gd); /* insert into tree */ avsave(&amb); /* and save to file */ + if (rn != r->ron) + extambient(acol, &amb, r->rop, rn); /* texture */ return(amb.rad); } @@ -391,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); @@ -430,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); } @@ -540,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); } @@ -573,7 +605,7 @@ AMBVAL **avp1, **avp2; } -#ifdef DEBUG +#if 1 static int avlmemi(avaddr) /* find list position from address */ AMBVAL *avaddr; @@ -600,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; /* @@ -619,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; @@ -652,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); @@ -728,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; }