--- ray/src/rt/ambcomp.c 2014/05/07 20:20:24 2.51 +++ ray/src/rt/ambcomp.c 2014/05/09 16:05:09 2.55 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.51 2014/05/07 20:20:24 greg Exp $"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.55 2014/05/09 16:05:09 greg Exp $"; #endif /* * Routines to compute "ambient" values using Monte Carlo @@ -105,7 +105,7 @@ vdb_edge(int db1, int db2) case VDB_xY: return(db2==VDB_x ? VDB_y : VDB_X); case VDB_Xy: return(db2==VDB_y ? VDB_x : VDB_Y); } - error(INTERNAL, "forbidden diagonal in vdb_edge()"); + error(CONSISTENCY, "forbidden diagonal in vdb_edge()"); return(-1); } @@ -243,25 +243,31 @@ getambdiffs(AMBHEMI *hp) ep[0] += d2; ep[-hp->ns] += d2; } - if (j) { /* from behind */ - d2 = b - bright(ap[-1].v); - d2 *= d2; - ep[0] += d2; - ep[-1] += d2; - } + if (!j) continue; + /* from behind */ + d2 = b - bright(ap[-1].v); + d2 *= d2; + ep[0] += d2; + ep[-1] += d2; + if (!i) continue; + /* diagonal */ + d2 = b - bright(ap[-hp->ns-1].v); + d2 *= d2; + ep[0] += d2; + ep[-hp->ns-1] += d2; } /* correct for number of neighbors */ - earr[0] *= 2.f; - earr[hp->ns-1] *= 2.f; - earr[(hp->ns-1)*hp->ns] *= 2.f; - earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 2.f; + earr[0] *= 8./3.; + earr[hp->ns-1] *= 8./3.; + earr[(hp->ns-1)*hp->ns] *= 8./3.; + earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.; for (i = 1; i < hp->ns-1; i++) { - earr[i*hp->ns] *= 4./3.; - earr[i*hp->ns + hp->ns-1] *= 4./3.; + earr[i*hp->ns] *= 8./5.; + earr[i*hp->ns + hp->ns-1] *= 8./5.; } for (j = 1; j < hp->ns-1; j++) { - earr[j] *= 4./3.; - earr[(hp->ns-1)*hp->ns + j] *= 4./3.; + earr[j] *= 8./5.; + earr[(hp->ns-1)*hp->ns + j] *= 8./5.; } return(earr); } @@ -272,22 +278,24 @@ static void ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) { float *earr = getambdiffs(hp); - double e2sum = 0.0; + double e2rem = 0; AMBSAMP *ap; RAY ar; double asum[3]; float *ep; - int i, j, n; + int i, j, n, nss; if (earr == NULL) /* just skip calc. if no memory */ return; - /* add up estimated variances */ - for (ep = earr + hp->ns*hp->ns; ep-- > earr; ) - e2sum += *ep; + /* accumulate estimated variances */ + for (ep = earr + hp->ns*hp->ns; ep > earr; ) + e2rem += *--ep; ep = earr; /* perform super-sampling */ for (ap = hp->sa, i = 0; i < hp->ns; i++) for (j = 0; j < hp->ns; j++, ap++) { - int nss = *ep/e2sum*cnt + frandom(); + if (e2rem <= FTINY) + goto done; /* nothing left to do */ + nss = *ep/e2rem*cnt + frandom(); asum[0] = asum[1] = asum[2] = 0.0; for (n = 1; n <= nss; n++) { if (!getambsamp(&ar, hp, i, j, n)) { @@ -297,14 +305,15 @@ ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) addcolor(asum, ar.rcol); } if (nss) { /* update returned ambient value */ - const double ssf = 1./(nss + 1); + const double ssf = 1./(nss + 1.); for (n = 3; n--; ) acol[n] += ssf*asum[n] + (ssf - 1.)*colval(ap->v,n); } - e2sum -= *ep++; /* update remainders */ + e2rem -= *ep++; /* update remainders */ cnt -= nss; } +done: free(earr); } @@ -537,7 +546,7 @@ add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, F /* Compute anisotropic radii and eigenvector directions */ -static int +static void eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3]) { double hess2[2][2]; @@ -559,9 +568,10 @@ eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3 if (i == 1) /* double-root (circle) */ evalue[1] = evalue[0]; if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) | - ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) - error(INTERNAL, "bad eigenvalue calculation"); - + ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) { + ra[0] = ra[1] = maxarad; + return; + } if (evalue[0] > evalue[1]) { ra[0] = sqrt(sqrt(4.0/evalue[0])); ra[1] = sqrt(sqrt(4.0/evalue[1])); @@ -728,13 +738,18 @@ ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c double avg_d = 0; uint32 flgs = 0; int i, j; - /* check distances above us */ + /* don't bother for a few samples */ + if (hp->ns < 12) + return(0); + /* check distances overhead */ for (i = hp->ns*3/4; i-- > hp->ns>>2; ) for (j = hp->ns*3/4; j-- > hp->ns>>2; ) avg_d += ambsam(hp,i,j).d; avg_d *= 4.0/(hp->ns*hp->ns); - if (avg_d >= max_d) /* too close to corral? */ + if (avg_d*r0 >= 1.0) /* ceiling too low for corral? */ return(0); + if (avg_d >= max_d) /* insurance */ + return(0); /* else circle around perimeter */ for (i = 0; i < hp->ns; i++) for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) { @@ -823,6 +838,7 @@ doambient( /* compute ambient component */ K = 1.0; pg = NULL; dg = NULL; + crlp = NULL; } ap = hp->sa; /* relative Y channel from here on... */ for (i = hp->ns*hp->ns; i--; ap++) @@ -858,7 +874,8 @@ doambient( /* compute ambient component */ if (ra[0] > maxarad) ra[0] = maxarad; } - if (crlp != NULL) /* flag encroached directions */ + /* flag encroached directions */ + if ((wt >= 0.5-FTINY) & (crlp != NULL)) *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc); if (pg != NULL) { /* cap gradient if necessary */ d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1];