--- ray/src/rt/ambient.c 1995/11/21 14:28:22 2.35 +++ ray/src/rt/ambient.c 1999/01/10 12:45:19 2.46 @@ -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,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 */ @@ -72,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(); @@ -124,7 +124,8 @@ double newa; setambient(afile) /* initialize calculation */ char *afile; { - long headlen; + int readonly = 0; + long pos, flen; AMBVAL amb; /* init ambient limits */ setambres(ambres); @@ -138,17 +139,37 @@ char *afile; return; } /* open ambient file */ - if ((ambfp = fopen(afile, "r+")) != NULL) { - initambfile(0); - headlen = ftell(ambfp); + if ((ambfp = fopen(afile, "r+")) == NULL) + readonly = (ambfp = fopen(afile, "r")) != NULL; + if (ambfp != NULL) { + initambfile(0); /* file exists */ + pos = ftell(ambfp); while (readambval(&amb, ambfp)) avinsert(avstore(&amb)); - /* align */ - fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); - nambshare = nambvals; - } else if ((ambfp = fopen(afile, "w+")) != NULL) - initambfile(1); - else { + nambshare = nambvals; /* share loaded values */ + if (readonly) { + sprintf(errmsg, + "loaded %u values from read-only ambient file", + nambvals); + error(WARNING, errmsg); + fclose(ambfp); /* close file so no writes */ + ambfp = NULL; + return; /* avoid ambsync() */ + } + /* align file pointer */ + 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); + } + } else if ((ambfp = fopen(afile, "w+")) != NULL) { + initambfile(1); /* else create new file */ + } else { sprintf(errmsg, "cannot open ambient file \"%s\"", afile); error(SYSTEM, errmsg); } @@ -185,7 +206,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; @@ -205,8 +226,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, @@ -222,14 +244,18 @@ FVECT nrm; 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 */ + } } @@ -254,7 +280,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. */ @@ -363,7 +389,7 @@ COLOR cr; register AMBVAL *ap; FVECT pv, nv; { - FVECT v1, v2; + FVECT v1; register int i; double d; @@ -372,9 +398,8 @@ FVECT pv, nv; for (i = 0; i < 3; i++) d += ap->gpos[i]*(pv[i]-ap->pos[i]); /* gradient due to rotation */ - VCOPY(v1, ap->dir); - fcross(v2, v1, nv); - d += DOT(ap->gdir, v2); + VCROSS(v1, ap->dir, nv); + d += DOT(ap->gdir, v1); if (d <= 0.0) { setcolor(cr, 0.0, 0.0, 0.0); return; @@ -399,10 +424,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); @@ -438,14 +463,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); } @@ -548,28 +578,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); } @@ -581,7 +616,7 @@ AMBVAL **avp1, **avp2; } -#ifdef DEBUG +#if 1 static int avlmemi(avaddr) /* find list position from address */ AMBVAL *avaddr; @@ -608,7 +643,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; /* @@ -627,13 +662,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; @@ -660,22 +697,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); @@ -736,7 +773,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; }