--- ray/src/rt/ambient.c 1993/01/22 09:51:13 2.16 +++ ray/src/rt/ambient.c 1994/03/24 13:41:04 2.24 @@ -35,7 +35,6 @@ double minarad; /* minimum ambient radius */ static AMBTREE atrunk; /* our ambient trunk node */ -static char *ambfname = NULL; /* ambient file name */ static FILE *ambfp = NULL; /* ambient file pointer */ static int nunflshed = 0; /* number of unflushed ambient values */ @@ -44,14 +43,20 @@ 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 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; @@ -67,6 +72,28 @@ int ar; } +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; { @@ -74,26 +101,31 @@ 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 ((ambfname = afile) != NULL) { - if ((ambfp = fopen(afile, "r+")) != NULL) { - initambfile(0); - headlen = ftell(ambfp); - while (readambval(&amb, ambfp)) - avinsert(&amb, &atrunk, thescene.cuorg, - thescene.cusize); - /* align */ - fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); - } else if ((ambfp = fopen(afile, "w+")) != NULL) - initambfile(1); - else { - sprintf(errmsg, "cannot open ambient file \"%s\"", - afile); - error(SYSTEM, errmsg); - } - nunflshed++; /* lie */ - ambsync(); + if ((ambfp = fopen(afile, "r+")) != NULL) { + initambfile(0); + headlen = ftell(ambfp); + while (readambval(&amb, ambfp)) + avinsert(avstore(&amb)); + /* align */ + fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); + } else if ((ambfp = fopen(afile, "w+")) != NULL) + initambfile(1); + else { + sprintf(errmsg, "cannot open ambient file \"%s\"", afile); + error(SYSTEM, errmsg); } + nunflshed++; /* lie */ + ambsync(); } @@ -182,7 +214,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. @@ -309,11 +343,15 @@ int creat; { extern char *progname, *octname, VersionID[]; +#ifdef F_SETLKW + aflock(creat ? F_WRLCK : F_RDLCK); +#endif #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), @@ -325,10 +363,8 @@ int creat; fputformat(AMBFMT, ambfp); putc('\n', ambfp); putambmagic(ambfp); - } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) { - sprintf(errmsg, "bad ambient file \"%s\"", ambfname); - error(USER, errmsg); - } + } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) + error(USER, "bad ambient file"); } @@ -336,7 +372,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) @@ -346,31 +382,44 @@ AMBVAL *av; goto writerr; return; writerr: - sprintf(errmsg, "error writing ambient file \"%s\"", ambfname); - error(SYSTEM, errmsg); + error(SYSTEM, "error writing ambient file"); } +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++) @@ -380,74 +429,113 @@ 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; } -#ifdef NIX +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); +} -int -ambsync() /* flush ambient file */ + +#ifdef F_SETLKW + +static +aflock(typ) /* lock/unlock ambient file */ +int typ; { - if (nunflshed == 0) - return(0); - nunflshed = 0; - return(fflush(ambfp)); + static struct flock fls; /* static so initialized to zeroes */ + + fls.l_type = typ; + if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) + error(SYSTEM, "cannot (un)lock ambient file"); } -#else int ambsync() /* synchronize ambient file */ { static FILE *ambinp = NULL; static long lastpos = -1; - struct flock fls; long flen; AMBVAL avs; register int n; if (nunflshed == 0) return(0); - /* gain exclusive access */ - fls.l_type = F_WRLCK; - fls.l_whence = 0; - fls.l_start = 0L; - fls.l_len = 0L; - if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) - error(SYSTEM, "cannot lock ambient file"); - if (lastpos < 0) /* initializing */ + if (lastpos < 0) /* initializing (locked in initambfile) */ goto syncend; + /* gain exclusive access */ + 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) { - ambinp = fopen(ambfname, "r"); + if (ambinp == NULL) { /* use duplicate filedes */ + ambinp = fdopen(dup(fileno(ambfp)), "r"); if (ambinp == NULL) - error(SYSTEM, "fopen failed in ambsync"); + 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); - fls.l_type = F_UNLCK; /* release file */ - fcntl(fileno(ambfp), F_SETLKW, &fls); + 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 + +int +ambsync() /* flush ambient file */ +{ + if (nunflshed == 0) + return(0); + nunflshed = 0; + return(fflush(ambfp)); } #endif