--- ray/src/rt/ambient.c 1995/11/21 14:28:22 2.35 +++ ray/src/rt/ambient.c 1996/07/09 13:32:35 2.38 @@ -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 */ @@ -124,7 +121,7 @@ double newa; setambient(afile) /* initialize calculation */ char *afile; { - long headlen; + long pos, flen; AMBVAL amb; /* init ambient limits */ setambres(ambres); @@ -140,11 +137,19 @@ 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) { + error(WARNING, + "ignoring last %ld values in ambient file (corrupted)", + (flen - pos)/AMBVALSIZ); + fseek(ambfp, pos, 0); + ftruncate(fileno(ambfp), pos); + } nambshare = nambvals; } else if ((ambfp = fopen(afile, "w+")) != NULL) initambfile(1); @@ -222,14 +227,12 @@ FVECT nrm; return; dumbamb: /* return global value */ copycolor(acol, ambval); -#if AMBVWT - if (nambvals == 0) + if (ambvwt <= 0 | nambvals == 0) return; - scalecolor(acol, (double)AMBVWT); + scalecolor(acol, (double)ambvwt); addcolor(acol, avsum); /* average in computations */ - d = 1.0/(AMBVWT+nambvals); + d = 1.0/(ambvwt+nambvals); scalecolor(acol, d); -#endif } @@ -569,7 +572,8 @@ static int alatcmp(avp1, avp2) /* compare ambient values for MRA */ AMBVAL **avp1, **avp2; { - return((**avp2).latick - (**avp1).latick); + register long lc = (**avp2).latick - (**avp1).latick; + return(lc<0 ? -1 : lc>0 ? 1 : 0); } @@ -581,7 +585,7 @@ AMBVAL **avp1, **avp2; } -#ifdef DEBUG +#if 1 static int avlmemi(avaddr) /* find list position from address */ AMBVAL *avaddr; @@ -736,7 +740,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; }