--- ray/src/rt/ambcomp.c 1992/03/04 16:28:25 2.3 +++ ray/src/rt/ambcomp.c 2005/04/19 01:15:06 2.14 @@ -1,64 +1,67 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.14 2005/04/19 01:15:06 greg Exp $"; #endif - /* * Routines to compute "ambient" values using Monte Carlo + * + * Declarations of external symbols in ambient.h */ +#include "copyright.h" + #include "ray.h" #include "ambient.h" #include "random.h" -typedef struct { - short t, p; /* theta, phi indices */ - COLOR v; /* value sum */ - float r; /* 1/distance sum */ - float k; /* variance for this division */ - int n; /* number of subsamples */ -} AMBSAMP; /* ambient sample division */ -typedef struct { - FVECT ux, uy, uz; /* x, y and z axis directions */ - short nt, np; /* number of theta and phi directions */ -} AMBHEMI; /* ambient sample hemisphere */ - -extern double sin(), cos(), sqrt(); - - -static int -ambcmp(d1, d2) /* decreasing order */ -AMBSAMP *d1, *d2; +int +inithemi( /* initialize sampling hemisphere */ + register AMBHEMI *hp, + RAY *r, + COLOR ac, + double wt +) { - if (d1->k < d2->k) - return(1); - if (d1->k > d2->k) - return(-1); - return(0); + int ns; + double d; + register int i; + /* set number of divisions */ + hp->nt = sqrt(ambdiv * wt / PI) + 0.5; + i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ + if (hp->nt < i) + hp->nt = i; + hp->np = PI * hp->nt + 0.5; + /* set number of super-samples */ + ns = ambssamp * wt + 0.5; + /* assign coefficient */ + d = 1.0/(hp->nt*hp->np + ns); /* XXX weight not uniform if ns > 0 */ + copycolor(hp->acoef, ac); + scalecolor(hp->acoef, d); + /* make axes */ + VCOPY(hp->uz, r->ron); + hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; + for (i = 0; i < 3; i++) + if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) + break; + if (i >= 3) + error(CONSISTENCY, "bad ray direction in inithemi"); + hp->uy[i] = 1.0; + fcross(hp->ux, hp->uy, hp->uz); + normalize(hp->ux); + fcross(hp->uy, hp->uz, hp->ux); + return(ns); } -static int -ambnorm(d1, d2) /* standard order */ -AMBSAMP *d1, *d2; +int +divsample( /* sample a division */ + register AMBSAMP *dp, + AMBHEMI *h, + RAY *r +) { - register int c; - - if (c = d1->t - d2->t) - return(c); - return(d1->p - d2->p); -} - - -divsample(dp, h, r) /* sample a division */ -register AMBSAMP *dp; -AMBHEMI *h; -RAY *r; -{ RAY ar; int hlist[3]; double spt[2]; @@ -66,17 +69,22 @@ RAY *r; double b2; double phi; register int i; - - if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0) + /* assign coefficient */ + if (ambacc <= FTINY) /* no storage, so report accurately */ + copycolor(ar.rcoef, h->acoef); + else /* else lie for sake of cache */ + setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); + if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0) return(-1); + copycolor(ar.rcoef, h->acoef); /* correct coefficient rtrace output */ hlist[0] = r->rno; hlist[1] = dp->t; hlist[2] = dp->p; multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); zd = sqrt((dp->t + spt[0])/h->nt); phi = 2.0*PI * (dp->p + spt[1])/h->np; - xd = cos(phi) * zd; - yd = sin(phi) * zd; + xd = tcos(phi) * zd; + yd = tsin(phi) * zd; zd = sqrt(1.0 - zd*zd); for (i = 0; i < 3; i++) ar.rdir[i] = xd*h->ux[i] + @@ -86,6 +94,7 @@ RAY *r; rayvalue(&ar); ndims--; addcolor(dp->v, ar.rcol); + /* use rt to improve gradient calc */ if (ar.rt > FTINY && ar.rt < FHUGE) dp->r += 1.0/ar.rt; /* (re)initialize error */ @@ -99,12 +108,48 @@ RAY *r; } +static int +ambcmp( /* decreasing order */ + const void *p1, + const void *p2 +) +{ + const AMBSAMP *d1 = (const AMBSAMP *)p1; + const AMBSAMP *d2 = (const AMBSAMP *)p2; + + if (d1->k < d2->k) + return(1); + if (d1->k > d2->k) + return(-1); + return(0); +} + + +static int +ambnorm( /* standard order */ + const void *p1, + const void *p2 +) +{ + const AMBSAMP *d1 = (const AMBSAMP *)p1; + const AMBSAMP *d2 = (const AMBSAMP *)p2; + register int c; + + if ( (c = d1->t - d2->t) ) + return(c); + return(d1->p - d2->p); +} + + double -doambient(acol, r, wt, pg, dg) /* compute ambient component */ -COLOR acol; -RAY *r; -double wt; -FVECT pg, dg; +doambient( /* compute ambient component */ + COLOR acol, + RAY *r, + COLOR ac, + double wt, + FVECT pg, + FVECT dg +) { double b, d; AMBHEMI hemi; @@ -117,12 +162,11 @@ FVECT pg, dg; /* initialize color */ setcolor(acol, 0.0, 0.0, 0.0); /* initialize hemisphere */ - inithemi(&hemi, r, wt); + ns = inithemi(&hemi, r, ac, wt); ndivs = hemi.nt * hemi.np; if (ndivs == 0) return(0.0); - /* set number of super-samples */ - ns = ambssamp * wt + 0.5; + /* allocate super-samples */ if (ns > 0 || pg != NULL || dg != NULL) { div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); if (div == NULL) @@ -141,35 +185,37 @@ FVECT pg, dg; dp->n = 0; if (divsample(dp, &hemi, r) < 0) goto oopsy; + arad += dp->r; if (div != NULL) dp++; - else { + else addcolor(acol, dp->v); - arad += dp->r; - } } - if (ns > 0) { /* perform super-sampling */ + if (ns > 0 && arad > FTINY && ndivs/arad < minarad) + ns = 0; /* close enough */ + else if (ns > 0) { /* else perform super-sampling */ comperrs(div, &hemi); /* compute errors */ qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ /* super-sample */ for (i = ns; i > 0; i--) { - copystruct(&dnew, div); + dnew = *div; if (divsample(&dnew, &hemi, r) < 0) goto oopsy; /* reinsert */ dp = div; j = ndivs < i ? ndivs : i; while (--j > 0 && dnew.k < dp[1].k) { - copystruct(dp, dp+1); + *dp = *(dp+1); dp++; } - copystruct(dp, &dnew); + *dp = dnew; } if (pg != NULL || dg != NULL) /* restore order */ qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); } /* compute returned values */ if (div != NULL) { + arad = 0.0; for (i = ndivs, dp = div; i-- > 0; dp++) { arad += dp->r; if (dp->n > 1) { @@ -201,7 +247,7 @@ FVECT pg, dg; for (i = 0; i < 3; i++) dg[i] = 0.0; } - free((char *)div); + free((void *)div); } b = 1.0/ndivs; scalecolor(acol, b); @@ -227,43 +273,17 @@ FVECT pg, dg; return(arad); oopsy: if (div != NULL) - free((char *)div); + free((void *)div); return(0.0); } -inithemi(hp, r, wt) /* initialize sampling hemisphere */ -register AMBHEMI *hp; -RAY *r; -double wt; +void +comperrs( /* compute initial error estimates */ + AMBSAMP *da, /* assumes standard ordering */ + register AMBHEMI *hp +) { - register int i; - /* set number of divisions */ - if (wt < (.25*PI)/ambdiv+FTINY) { - hp->nt = hp->np = 0; - return; /* zero samples */ - } - hp->nt = sqrt(ambdiv * wt / PI) + 0.5; - hp->np = PI * hp->nt + 0.5; - /* make axes */ - VCOPY(hp->uz, r->ron); - hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; - for (i = 0; i < 3; i++) - if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) - break; - if (i >= 3) - error(CONSISTENCY, "bad ray direction in inithemi"); - hp->uy[i] = 1.0; - fcross(hp->ux, hp->uy, hp->uz); - normalize(hp->ux); - fcross(hp->uy, hp->uz, hp->ux); -} - - -comperrs(da, hp) /* compute initial error estimates */ -AMBSAMP *da; /* assumes standard ordering */ -register AMBHEMI *hp; -{ double b, b2; int i, j; register AMBSAMP *dp; @@ -310,10 +330,12 @@ register AMBHEMI *hp; } -posgradient(gv, da, hp) /* compute position gradient */ -FVECT gv; -AMBSAMP *da; /* assumes standard ordering */ -register AMBHEMI *hp; +void +posgradient( /* compute position gradient */ + FVECT gv, + AMBSAMP *da, /* assumes standard ordering */ + register AMBHEMI *hp +) { register int i, j; double nextsine, lastsine, b, d; @@ -357,7 +379,7 @@ register AMBHEMI *hp; } mag0 *= 2.0*PI / hp->np; phi = 2.0*PI * (double)j/hp->np; - cosp = cos(phi); sinp = sin(phi); + cosp = tcos(phi); sinp = tsin(phi); xd += mag0*cosp - mag1*sinp; yd += mag0*sinp + mag1*cosp; } @@ -366,10 +388,12 @@ register AMBHEMI *hp; } -dirgradient(gv, da, hp) /* compute direction gradient */ -FVECT gv; -AMBSAMP *da; /* assumes standard ordering */ -register AMBHEMI *hp; +void +dirgradient( /* compute direction gradient */ + FVECT gv, + AMBSAMP *da, /* assumes standard ordering */ + register AMBHEMI *hp +) { register int i, j; double mag; @@ -391,8 +415,8 @@ register AMBHEMI *hp; dp += hp->np; } phi = 2.0*PI * (j+.5)/hp->np + PI/2.0; - xd += mag * cos(phi); - yd += mag * sin(phi); + xd += mag * tcos(phi); + yd += mag * tsin(phi); } for (i = 0; i < 3; i++) gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);