--- ray/src/rt/ambient.c 2005/08/22 16:07:26 2.62 +++ ray/src/rt/ambient.c 2011/09/22 02:15:56 2.70 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambient.c,v 2.62 2005/08/22 16:07:26 greg Exp $"; +static const char RCSid[] = "$Id: ambient.c,v 2.70 2011/09/22 02:15:56 greg Exp $"; #endif /* * ambient.c - routines dealing with ambient (inter-reflected) component. @@ -25,7 +25,7 @@ static const char RCSid[] = "$Id: ambient.c,v 2.62 200 extern char *shm_boundary; /* memory sharing boundary */ #ifndef MAXASET -#define MAXASET 2047 /* maximum number of elements in ambient set */ +#define MAXASET 4095 /* maximum number of elements in ambient set */ #endif OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ @@ -140,7 +140,7 @@ extern void setambient(void) /* initialize calculation */ { int readonly = 0; - long pos, flen; + long flen; AMBVAL amb; /* make sure we're fresh */ ambdone(); @@ -160,7 +160,7 @@ setambient(void) /* initialize calculation */ readonly = (ambfp = fopen(ambfile, "r")) != NULL; if (ambfp != NULL) { initambfile(0); /* file exists */ - pos = ftell(ambfp); + lastpos = ftell(ambfp); while (readambval(&amb, ambfp)) avinsert(avstore(&amb)); nambshare = nambvals; /* share loaded values */ @@ -174,26 +174,29 @@ setambient(void) /* initialize calculation */ return; /* avoid ambsync() */ } /* align file pointer */ - pos += (long)nambvals*AMBVALSIZ; + lastpos += (long)nambvals*AMBVALSIZ; flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); - if (flen != pos) { + if (flen != lastpos) { sprintf(errmsg, "ignoring last %ld values in ambient file (corrupted)", - (flen - pos)/AMBVALSIZ); + (flen - lastpos)/AMBVALSIZ); error(WARNING, errmsg); - fseek(ambfp, pos, 0); + fseek(ambfp, lastpos, SEEK_SET); #ifndef _WIN32 /* XXX we need a replacement for that one */ - ftruncate(fileno(ambfp), (off_t)pos); + ftruncate(fileno(ambfp), (off_t)lastpos); #endif } } else if ((ambfp = fopen(ambfile, "w+")) != NULL) { initambfile(1); /* else create new file */ + fflush(ambfp); + lastpos = ftell(ambfp); } else { sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); error(SYSTEM, errmsg); } - nunflshed++; /* lie */ - ambsync(); +#ifdef F_SETLKW + aflock(F_UNLCK); /* release file */ +#endif } @@ -229,8 +232,8 @@ ambnotify( /* record new modifier */ ) { static int hitlimit = 0; - register OBJREC *o; - register char **amblp; + OBJREC *o; + char **amblp; if (obj == OVOID) { /* starting over */ ambset[0] = 0; @@ -256,7 +259,7 @@ ambnotify( /* record new modifier */ extern void multambient( /* compute ambient component & multiply by coef. */ COLOR aval, - register RAY *r, + RAY *r, FVECT nrm ) { @@ -326,7 +329,7 @@ dumbamb: /* return global value */ extern double sumambient( /* get interpolated ambient value */ COLOR acol, - register RAY *r, + RAY *r, FVECT rn, int al, AMBTREE *at, @@ -338,8 +341,8 @@ sumambient( /* get interpolated ambient value */ COLOR ct; FVECT ck0; int i; - register int j; - register AMBVAL *av; + int j; + AMBVAL *av; wsum = 0.0; /* do this node */ @@ -357,13 +360,8 @@ sumambient( /* get interpolated ambient value */ /* * Ambient radius test. */ - d = av->pos[0] - r->rop[0]; - e1 = d * d; - d = av->pos[1] - r->rop[1]; - e1 += d * d; - d = av->pos[2] - r->rop[2]; - e1 += d * d; - e1 /= av->rad * av->rad; + VSUB(ck0, av->pos, r->rop); + e1 = DOT(ck0, ck0) / (av->rad * av->rad); if (e1 > ambacc*ambacc*1.21) continue; /* @@ -380,8 +378,9 @@ sumambient( /* get interpolated ambient value */ } } e2 = (1.0 - d) * r->rweight; - if (e2 < 0.0) e2 = 0.0; - if (e1 + e2 > ambacc*ambacc*1.21) + if (e2 < 0.0) + e2 = 0.0; + else if (e1 + e2 > ambacc*ambacc*1.21) continue; /* * Ray behind test. @@ -481,13 +480,13 @@ makeambient( /* make a new ambient value for storage extern void extambient( /* extrapolate value at pv, nv */ COLOR cr, - register AMBVAL *ap, + AMBVAL *ap, FVECT pv, FVECT nv ) { FVECT v1; - register int i; + int i; double d; d = 1.0; /* zeroeth order */ @@ -508,20 +507,20 @@ extambient( /* extrapolate value at pv, nv */ static void initambfile( /* initialize ambient file */ - int creat + int cre8 ) { extern char *progname, *octname; static char *mybuf = NULL; #ifdef F_SETLKW - aflock(creat ? F_WRLCK : F_RDLCK); + aflock(cre8 ? F_WRLCK : F_RDLCK); #endif SET_FILE_BINARY(ambfp); if (mybuf == NULL) mybuf = (char *)bmalloc(BUFSIZ+8); setbuf(ambfp, mybuf); - if (creat) { /* new file */ + if (cre8) { /* new file */ newheader("RADIANCE", ambfp); fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", progname, colval(ambval,RED), @@ -530,13 +529,12 @@ initambfile( /* initialize ambient file */ fprintf(ambfp, "-ad %d -as %d -ar %d ", ambdiv, ambssamp, ambres); if (octname != NULL) - printargs(1, &octname, ambfp); - else - fputc('\n', ambfp); + fputs(octname, ambfp); + fputc('\n', ambfp); fprintf(ambfp, "SOFTWARE= %s\n", VersionID); fputnow(ambfp); fputformat(AMBFMT, ambfp); - putc('\n', ambfp); + fputc('\n', ambfp); putambmagic(ambfp); } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) error(USER, "bad ambient file"); @@ -564,10 +562,10 @@ writerr: static AMBVAL * avstore( /* allocate memory and store aval */ - register AMBVAL *aval + AMBVAL *aval ) { - register AMBVAL *av; + AMBVAL *av; double d; if ((av = newambval()) == NULL) @@ -593,7 +591,7 @@ static AMBTREE *atfreelist = NULL; /* free ambient tr static AMBTREE * newambtree(void) /* allocate 8 ambient tree structs */ { - register AMBTREE *atp, *upperlim; + AMBTREE *atp, *upperlim; if (atfreelist == NULL) { /* get more nodes */ atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); @@ -627,13 +625,13 @@ avinsert( /* insert ambient value in our tree */ void *av ) { - register AMBTREE *at; - register AMBVAL *ap; + AMBTREE *at; + AMBVAL *ap; AMBVAL avh; FVECT ck0; double s; int branch; - register int i; + int i; if (((AMBVAL*)av)->rad <= FTINY) error(CONSISTENCY, "zero ambient radius in avinsert"); @@ -665,12 +663,12 @@ avinsert( /* insert ambient value in our tree */ static void unloadatree( /* unload an ambient value tree */ - register AMBTREE *at, + AMBTREE *at, unloadtf_t *f ) { - register AMBVAL *av; - register int i; + AMBVAL *av; + int i; /* transfer values at this node */ for (av = at->alist; av != NULL; av = at->alist) { at->alist = av->next; @@ -714,7 +712,7 @@ alatcmp( /* compare ambient values for MRA */ const void *av2 ) { - register long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; + long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; return(lc<0 ? -1 : lc>0 ? 1 : 0); } @@ -731,7 +729,7 @@ aposcmp( /* compare ambient value positions */ const void *avp2 ) { - register long diff = *(char * const *)avp1 - *(char * const *)avp2; + long diff = *(char * const *)avp1 - *(char * const *)avp2; if (diff < 0) return(-1); return(diff > 0); @@ -743,7 +741,7 @@ avlmemi( /* find list position from address */ AMBVAL *avaddr ) { - register AMBVAL **avlpp; + AMBVAL **avlpp; avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); @@ -764,7 +762,7 @@ sortambvals( /* resort ambient values */ { AMBTREE oldatrunk; AMBVAL tav, *tap, *pnext; - register int i, j; + int i, j; /* see if it's time yet */ if (!always && (ambclock++ < lastsort+sortintvl || nambvals < SORT_THRESH)) @@ -864,6 +862,8 @@ aflock( /* lock/unlock ambient file */ { static struct flock fls; /* static so initialized to zeroes */ + if (typ == fls.l_type) /* already called? */ + return; fls.l_type = typ; if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) error(SYSTEM, "cannot (un)lock ambient file"); @@ -875,24 +875,22 @@ ambsync(void) /* synchronize ambient file */ { long flen; AMBVAL avs; - register int n; + int n; - if (nunflshed == 0) + if (ambfp == NULL) /* no ambient file? */ return(0); - if (lastpos < 0) /* initializing (locked in initambfile) */ - goto syncend; - /* gain exclusive access */ - aflock(F_WRLCK); + /* gain appropriate access */ + aflock(nunflshed ? F_WRLCK : F_RDLCK); /* see if file has grown */ if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) goto seekerr; - if ( (n = flen - lastpos) ) { /* file has grown */ + if ((n = flen - lastpos) > 0) { /* file has grown */ if (ambinp == NULL) { /* use duplicate filedes */ ambinp = fdopen(dup(fileno(ambfp)), "r"); if (ambinp == NULL) error(SYSTEM, "fdopen failed in ambsync"); } - if (fseek(ambinp, lastpos, 0) < 0) + if (fseek(ambinp, lastpos, SEEK_SET) < 0) goto seekerr; while (n >= AMBVALSIZ) { /* load contributed values */ if (!readambval(&avs, ambinp)) { @@ -905,23 +903,18 @@ ambsync(void) /* synchronize ambient file */ avinsert(avstore(&avs)); n -= AMBVALSIZ; } + lastpos = flen - n; /*** seek always as safety measure if (n) ***/ /* alignment */ - if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) + if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 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 */ - if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) + if (n != EOF) + lastpos += (long)nunflshed*AMBVALSIZ; + else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) goto seekerr; + aflock(F_UNLCK); /* release file */ nunflshed = 0; return(n); @@ -935,7 +928,7 @@ seekerr: extern int ambsync(void) /* flush ambient file */ { - if (nunflshed == 0) + if (ambfp == NULL) return(0); nunflshed = 0; return(fflush(ambfp));