--- ray/src/rt/ambcomp.c 2005/06/04 06:10:12 2.17 +++ ray/src/rt/ambcomp.c 2013/02/05 05:40:06 2.23 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.17 2005/06/04 06:10:12 greg Exp $"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.23 2013/02/05 05:40:06 greg Exp $"; #endif /* * Routines to compute "ambient" values using Monte Carlo @@ -18,17 +18,17 @@ static const char RCSid[] = "$Id: ambcomp.c,v 2.17 200 void inithemi( /* initialize sampling hemisphere */ - register AMBHEMI *hp, + AMBHEMI *hp, COLOR ac, RAY *r, double wt ) { double d; - register int i; + int i; /* set number of divisions */ if (ambacc <= FTINY && - wt > (d = 0.8*bright(ac)*r->rweight/(ambdiv*minweight))) + wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight))) wt = d; /* avoid ray termination */ hp->nt = sqrt(ambdiv * wt / PI) + 0.5; i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ @@ -58,7 +58,7 @@ inithemi( /* initialize sampling hemisphere */ int divsample( /* sample a division */ - register AMBSAMP *dp, + AMBSAMP *dp, AMBHEMI *h, RAY *r ) @@ -69,7 +69,7 @@ divsample( /* sample a division */ double xd, yd, zd; double b2; double phi; - register int i; + int i; /* ambient coefficient for weight */ if (ambacc > FTINY) setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); @@ -94,6 +94,7 @@ divsample( /* sample a division */ ar.rdir[i] = xd*h->ux[i] + yd*h->uy[i] + zd*h->uz[i]; + checknorm(ar.rdir); dimlist[ndims++] = dp->t*h->np + dp->p + 90171; rayvalue(&ar); ndims--; @@ -138,7 +139,7 @@ ambnorm( /* standard order */ { const AMBSAMP *d1 = (const AMBSAMP *)p1; const AMBSAMP *d2 = (const AMBSAMP *)p2; - register int c; + int c; if ( (c = d1->t - d2->t) ) return(c); @@ -148,7 +149,7 @@ ambnorm( /* standard order */ double doambient( /* compute ambient component */ - COLOR acol, + COLOR rcol, RAY *r, double wt, FVECT pg, @@ -159,32 +160,35 @@ doambient( /* compute ambient component */ AMBHEMI hemi; AMBSAMP *div; AMBSAMP dnew; - register AMBSAMP *dp; + double acol[3]; + AMBSAMP *dp; double arad; - int ndivs; - register int i, j; + int divcnt; + int i, j; /* initialize hemisphere */ - inithemi(&hemi, acol, r, wt); - ndivs = hemi.nt * hemi.np; + inithemi(&hemi, rcol, r, wt); + divcnt = hemi.nt * hemi.np; /* initialize */ if (pg != NULL) pg[0] = pg[1] = pg[2] = 0.0; if (dg != NULL) dg[0] = dg[1] = dg[2] = 0.0; - setcolor(acol, 0.0, 0.0, 0.0); - if (ndivs == 0) + setcolor(rcol, 0.0, 0.0, 0.0); + if (divcnt == 0) return(0.0); /* allocate super-samples */ if (hemi.ns > 0 || pg != NULL || dg != NULL) { - div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); + div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP)); if (div == NULL) error(SYSTEM, "out of memory in doambient"); } else div = NULL; /* sample the divisions */ arad = 0.0; + acol[0] = acol[1] = acol[2] = 0.0; if ((dp = div) == NULL) dp = &dnew; + divcnt = 0; for (i = 0; i < hemi.nt; i++) for (j = 0; j < hemi.np; j++) { dp->t = i; dp->p = j; @@ -192,22 +196,30 @@ doambient( /* compute ambient component */ dp->r = 0.0; dp->n = 0; if (divsample(dp, &hemi, r) < 0) { - if (div != NULL) dp++; - hemi.ns = 0; /* incomplete sampling */ - pg = dg = NULL; + if (div != NULL) + dp++; continue; } arad += dp->r; + divcnt++; if (div != NULL) dp++; else addcolor(acol, dp->v); } - if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad) + if (!divcnt) { + if (div != NULL) + free((void *)div); + return(0.0); /* no samples taken */ + } + if (divcnt < hemi.nt*hemi.np) { + pg = dg = NULL; /* incomplete sampling */ + hemi.ns = 0; + } else if (arad > FTINY && divcnt/arad < minarad) { hemi.ns = 0; /* close enough */ - else if (hemi.ns > 0) { /* else perform super-sampling */ + } else if (hemi.ns > 0) { /* else perform super-sampling? */ comperrs(div, &hemi); /* compute errors */ - qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ + qsort(div, divcnt, sizeof(AMBSAMP), ambcmp); /* sort divs */ /* super-sample */ for (i = hemi.ns; i > 0; i--) { dnew = *div; @@ -216,7 +228,7 @@ doambient( /* compute ambient component */ continue; } dp = div; /* reinsert */ - j = ndivs < i ? ndivs : i; + j = divcnt < i ? divcnt : i; while (--j > 0 && dnew.k < dp[1].k) { *dp = *(dp+1); dp++; @@ -224,12 +236,12 @@ doambient( /* compute ambient component */ *dp = dnew; } if (pg != NULL || dg != NULL) /* restore order */ - qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); + qsort(div, divcnt, sizeof(AMBSAMP), ambnorm); } /* compute returned values */ if (div != NULL) { - arad = 0.0; - for (i = ndivs, dp = div; i-- > 0; dp++) { + arad = 0.0; /* note: divcnt may be < nt*np */ + for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) { arad += dp->r; if (dp->n > 1) { b = 1.0/dp->n; @@ -255,10 +267,11 @@ doambient( /* compute ambient component */ } free((void *)div); } + copycolor(rcol, acol); if (arad <= FTINY) arad = maxarad; else - arad = (ndivs+hemi.ns)/arad; + arad = (divcnt+hemi.ns)/arad; if (pg != NULL) { /* reduce radius if gradient large */ d = DOT(pg,pg); if (d*arad*arad > 1.0) @@ -281,12 +294,12 @@ doambient( /* compute ambient component */ void comperrs( /* compute initial error estimates */ AMBSAMP *da, /* assumes standard ordering */ - register AMBHEMI *hp + AMBHEMI *hp ) { double b, b2; int i, j; - register AMBSAMP *dp; + AMBSAMP *dp; /* sum differences from neighbors */ dp = da; for (i = 0; i < hp->nt; i++) @@ -334,14 +347,14 @@ void posgradient( /* compute position gradient */ FVECT gv, AMBSAMP *da, /* assumes standard ordering */ - register AMBHEMI *hp + AMBHEMI *hp ) { - register int i, j; + int i, j; double nextsine, lastsine, b, d; double mag0, mag1; double phi, cosp, sinp, xd, yd; - register AMBSAMP *dp; + AMBSAMP *dp; xd = yd = 0.0; for (j = 0; j < hp->np; j++) { @@ -392,13 +405,13 @@ void dirgradient( /* compute direction gradient */ FVECT gv, AMBSAMP *da, /* assumes standard ordering */ - register AMBHEMI *hp + AMBHEMI *hp ) { - register int i, j; + int i, j; double mag; double phi, xd, yd; - register AMBSAMP *dp; + AMBSAMP *dp; xd = yd = 0.0; for (j = 0; j < hp->np; j++) {