--- ray/src/rt/ambient.c 1993/08/04 14:22:11 2.19 +++ ray/src/rt/ambient.c 1994/06/02 11:45:27 2.25 @@ -43,9 +43,11 @@ static int nunflshed = 0; /* number of unflushed ambi #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) #define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) +#define freeambtree(t) free((char *)(t)) extern long ftell(), lseek(); -static int initambfile(), avsave(), avinsert(); +static int initambfile(), avsave(), avinsert(), loadatree(); +static AMBVAL *avstore(); #ifdef F_SETLKW static aflock(); #endif @@ -54,21 +56,46 @@ static aflock(); setambres(ar) /* set ambient resolution */ int ar; { + ambres = ar < 0 ? 0 : ar; /* may be done already */ /* set min & max radii */ if (ar <= 0) { - minarad = 0.0; + minarad = 0; maxarad = thescene.cusize / 2.0; } else { minarad = thescene.cusize / ar; - maxarad = 16.0 * minarad; /* heuristic */ + maxarad = 16 * minarad; /* heuristic */ if (maxarad > thescene.cusize / 2.0) maxarad = thescene.cusize / 2.0; } - if (maxarad <= FTINY) - maxarad = .001; + if (minarad <= FTINY) + minarad = 10*FTINY; + if (maxarad <= minarad) + maxarad = 64 * minarad; } +setambacc(newa) /* set ambient accuracy */ +double newa; +{ + static double oldambacc = -1.0; + AMBTREE oldatrunk; + + ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ + if (oldambacc < -FTINY) + oldambacc = ambacc; /* do nothing first call */ + if (fabs(newa - oldambacc) < 0.01) + return; /* insignificant -- don't bother */ + if (ambacc <= FTINY) + return; /* cannot build new tree */ + /* else need to rebuild tree */ + copystruct(&oldatrunk, &atrunk); + atrunk.alist = NULL; + atrunk.kid = NULL; + loadatree(&oldatrunk); + oldambacc = ambacc; /* remeber setting for next call */ +} + + setambient(afile) /* initialize calculation */ char *afile; { @@ -76,15 +103,21 @@ char *afile; AMBVAL amb; /* init ambient limits */ setambres(ambres); + setambacc(ambacc); if (afile == NULL) return; + if (ambacc <= FTINY) { + sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", + afile); + error(WARNING, errmsg); + return; + } /* open ambient file */ if ((ambfp = fopen(afile, "r+")) != NULL) { initambfile(0); headlen = ftell(ambfp); while (readambval(&amb, ambfp)) - avinsert(&amb, &atrunk, thescene.cuorg, - thescene.cusize); + avinsert(avstore(&amb)); /* align */ fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); } else if ((ambfp = fopen(afile, "w+")) != NULL) @@ -183,7 +216,9 @@ double s; /* * Ambient level test. */ - if (av->lvl > al || av->weight < r->rweight-FTINY) + if (av->lvl > al) /* list sorted, so this works */ + break; + if (av->weight < r->rweight-FTINY) continue; /* * Ambient radius test. @@ -316,8 +351,9 @@ int creat; #ifdef MSDOS setmode(fileno(ambfp), O_BINARY); #endif - setbuf(ambfp, bmalloc(BUFSIZ)); + setbuf(ambfp, bmalloc(BUFSIZ+8)); if (creat) { /* new file */ + newheader("RADIANCE", ambfp); fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", progname, colval(ambval,RED), colval(ambval,GRN), colval(ambval,BLU), @@ -338,7 +374,7 @@ static avsave(av) /* insert and save an ambient value */ AMBVAL *av; { - avinsert(av, &atrunk, thescene.cuorg, thescene.cusize); + avinsert(avstore(av)); if (ambfp == NULL) return; if (writambval(av, ambfp) < 0) @@ -352,26 +388,40 @@ writerr: } +static AMBVAL * +avstore(aval) /* allocate memory and store aval */ +register AMBVAL *aval; +{ + register AMBVAL *av; + + if ((av = newambval()) == NULL) + error(SYSTEM, "out of memory in avstore"); + copystruct(av, aval); + return(av); +} + + static -avinsert(aval, at, c0, s) /* insert ambient value in a tree */ -AMBVAL *aval; -register AMBTREE *at; -FVECT c0; -double s; +avinsert(av) /* insert ambient value in our tree */ +register AMBVAL *av; { + register AMBTREE *at; + register AMBVAL *ap; + AMBVAL avh; FVECT ck0; + double s; int branch; - register AMBVAL *av; register int i; - if ((av = newambval()) == NULL) - goto memerr; - copystruct(av, aval); - VCOPY(ck0, c0); + if (av->rad <= FTINY) + error(CONSISTENCY, "zero ambient radius in avinsert"); + at = &atrunk; + VCOPY(ck0, thescene.cuorg); + s = thescene.cusize; while (s*(OCTSCALE/2) > av->rad*ambacc) { if (at->kid == NULL) if ((at->kid = newambtree()) == NULL) - goto memerr; + error(SYSTEM, "out of memory in avinsert"); s *= 0.5; branch = 0; for (i = 0; i < 3; i++) @@ -381,14 +431,35 @@ double s; } at = at->kid + branch; } - av->next = at->alist; - at->alist = av; - return; -memerr: - error(SYSTEM, "out of memory in avinsert"); + avh.next = at->alist; /* order by increasing level */ + for (ap = &avh; ap->next != NULL; ap = ap->next) + if (ap->next->lvl >= av->lvl) + break; + av->next = ap->next; + ap->next = av; + at->alist = avh.next; } +static +loadatree(at) /* move tree to main store */ +register AMBTREE *at; +{ + register AMBVAL *av; + register int i; + /* transfer values at this node */ + for (av = at->alist; av != NULL; av = at->alist) { + at->alist = av->next; + avinsert(av); + } + if (at->kid == NULL) + return; + for (i = 0; i < 8; i++) /* transfer and free children */ + loadatree(at->kid+i); + freeambtree(at->kid); +} + + #ifdef F_SETLKW static @@ -420,7 +491,7 @@ ambsync() /* synchronize ambient file */ aflock(F_WRLCK); /* see if file has grown */ if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) - error(SYSTEM, "cannot seek on ambient file"); + goto seekerr; if (n = flen - lastpos) { /* file has grown */ if (ambinp == NULL) { /* use duplicate filedes */ ambinp = fdopen(dup(fileno(ambfp)), "r"); @@ -428,21 +499,34 @@ ambsync() /* synchronize ambient file */ error(SYSTEM, "fdopen failed in ambsync"); } if (fseek(ambinp, lastpos, 0) < 0) - error(SYSTEM, "fseek failed in ambsync"); + goto seekerr; while (n >= AMBVALSIZ) { /* load contributed values */ readambval(&avs, ambinp); - avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize); + avinsert(avstore(&avs)); n -= AMBVALSIZ; } - if (n) /* alignment */ - lseek(fileno(ambfp), flen-n, 0); + /*** seek always as safety measure + if (n) ***/ /* alignment */ + if (lseek(fileno(ambfp), flen-n, 0) < 0) + goto seekerr; } +#ifdef DEBUG + if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { + sprintf(errmsg, "ambient file buffer at %d rather than %d", + ambfp->_ptr - ambfp->_base, + nunflshed*AMBVALSIZ); + error(CONSISTENCY, errmsg); + } +#endif syncend: n = fflush(ambfp); /* calls write() at last */ - lastpos = lseek(fileno(ambfp), 0L, 1); + if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) + goto seekerr; aflock(F_UNLCK); /* release file */ nunflshed = 0; return(n); +seekerr: + error(SYSTEM, "seek failed in ambsync"); } #else