--- ray/src/rt/ambcomp.c 1991/08/13 12:16:41 1.11 +++ ray/src/rt/ambcomp.c 2003/02/22 02:07:28 2.9 @@ -1,35 +1,76 @@ -/* 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.9 2003/02/22 02:07:28 greg Exp $"; #endif - /* * Routines to compute "ambient" values using Monte Carlo + * + * Declarations of external symbols in ambient.h */ +/* ==================================================================== + * The Radiance Software License, Version 1.0 + * + * Copyright (c) 1990 - 2002 The Regents of the University of California, + * through Lawrence Berkeley National Laboratory. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes Radiance software + * (http://radsite.lbl.gov/) + * developed by the Lawrence Berkeley National Laboratory + * (http://www.lbl.gov/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory" + * and "The Regents of the University of California" must + * not be used to endorse or promote products derived from this + * software without prior written permission. For written + * permission, please contact radiance@radsite.lbl.gov. + * + * 5. Products derived from this software may not be called "Radiance", + * nor may "Radiance" appear in their name, without prior written + * permission of Lawrence Berkeley National Laboratory. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of Lawrence Berkeley National Laboratory. For more + * information on Lawrence Berkeley National Laboratory, please see + * . + */ + #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; @@ -54,6 +95,7 @@ AMBSAMP *d1, *d2; } +int divsample(dp, h, r) /* sample a division */ register AMBSAMP *dp; AMBHEMI *h; @@ -67,16 +109,16 @@ RAY *r; double phi; register int i; - if (rayorigin(&ar, r, AMBIENT, 0.5) < 0) + if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0) return(-1); hlist[0] = r->rno; hlist[1] = dp->t; hlist[2] = dp->p; - peano(spt, 2, urand(ilhash(hlist,3)+dp->n), .01); + 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 +128,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 */ @@ -100,9 +143,10 @@ RAY *r; double -doambient(acol, r, pg, dg) /* compute ambient component */ +doambient(acol, r, wt, pg, dg) /* compute ambient component */ COLOR acol; RAY *r; +double wt; FVECT pg, dg; { double b, d; @@ -116,12 +160,12 @@ FVECT pg, dg; /* initialize color */ setcolor(acol, 0.0, 0.0, 0.0); /* initialize hemisphere */ - inithemi(&hemi, r); + inithemi(&hemi, r, wt); ndivs = hemi.nt * hemi.np; if (ndivs == 0) return(0.0); /* set number of super-samples */ - ns = ambssamp * r->rweight + 0.5; + ns = ambssamp * wt + 0.5; if (ns > 0 || pg != NULL || dg != NULL) { div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); if (div == NULL) @@ -140,14 +184,15 @@ 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 */ @@ -169,6 +214,7 @@ FVECT pg, dg; } /* compute returned values */ if (div != NULL) { + arad = 0.0; for (i = ndivs, dp = div; i-- > 0; dp++) { arad += dp->r; if (dp->n > 1) { @@ -200,43 +246,51 @@ 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); if (arad <= FTINY) - arad = FHUGE; + arad = maxarad; else arad = (ndivs+ns)/arad; - if (arad > maxarad) - arad = maxarad; - else if (arad < minarad) + if (pg != NULL) { /* reduce radius if gradient large */ + d = DOT(pg,pg); + if (d*arad*arad > 1.0) + arad = 1.0/sqrt(d); + } + if (arad < minarad) { arad = minarad; - arad /= sqrt(r->rweight); - if (pg != NULL) { /* clip pos. gradient if too large */ - d = 4.0*DOT(pg,pg)*arad*arad; - if (d > 1.0) { - d = 1.0/sqrt(d); + if (pg != NULL && d*arad*arad > 1.0) { /* cap gradient */ + d = 1.0/arad/sqrt(d); for (i = 0; i < 3; i++) pg[i] *= d; } } + if ((arad /= sqrt(wt)) > maxarad) + arad = maxarad; return(arad); oopsy: if (div != NULL) - free((char *)div); + free((void *)div); return(0.0); } -inithemi(hp, r) /* initialize sampling hemisphere */ +void +inithemi(hp, r, wt) /* initialize sampling hemisphere */ register AMBHEMI *hp; RAY *r; +double wt; { register int i; /* set number of divisions */ - hp->nt = sqrt(ambdiv * r->rweight / PI) + 0.5; - hp->np = PI * hp->nt; + 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; @@ -252,6 +306,7 @@ RAY *r; } +void comperrs(da, hp) /* compute initial error estimates */ AMBSAMP *da; /* assumes standard ordering */ register AMBHEMI *hp; @@ -302,13 +357,14 @@ register AMBHEMI *hp; } +void posgradient(gv, da, hp) /* compute position gradient */ FVECT gv; AMBSAMP *da; /* assumes standard ordering */ -AMBHEMI *hp; +register AMBHEMI *hp; { register int i, j; - double b, d; + double nextsine, lastsine, b, d; double mag0, mag1; double phi, cosp, sinp, xd, yd; register AMBSAMP *dp; @@ -317,6 +373,7 @@ AMBHEMI *hp; for (j = 0; j < hp->np; j++) { dp = da + j; mag0 = mag1 = 0.0; + lastsine = 0.0; for (i = 0; i < hp->nt; i++) { #ifdef DEBUG if (dp->t != i || dp->p != j) @@ -327,26 +384,28 @@ AMBHEMI *hp; if (i > 0) { d = dp[-hp->np].r; if (dp[0].r > d) d = dp[0].r; - d *= 1.0 - sqrt((double)i/hp->nt); + /* sin(t)*cos(t)^2 */ + d *= lastsine * (1.0 - (double)i/hp->nt); mag0 += d*(b - bright(dp[-hp->np].v)); } + nextsine = sqrt((double)(i+1)/hp->nt); if (j > 0) { d = dp[-1].r; if (dp[0].r > d) d = dp[0].r; - mag1 += d*(b - bright(dp[-1].v)); + mag1 += d * (nextsine - lastsine) * + (b - bright(dp[-1].v)); } else { d = dp[hp->np-1].r; if (dp[0].r > d) d = dp[0].r; - mag1 += d*(b - bright(dp[hp->np-1].v)); + mag1 += d * (nextsine - lastsine) * + (b - bright(dp[hp->np-1].v)); } dp += hp->np; + lastsine = nextsine; } - if (hp->nt > 1) { - mag0 /= (double)hp->np; - mag1 /= (double)hp->nt; - } + 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; } @@ -355,10 +414,11 @@ AMBHEMI *hp; } +void dirgradient(gv, da, hp) /* compute direction gradient */ FVECT gv; AMBSAMP *da; /* assumes standard ordering */ -AMBHEMI *hp; +register AMBHEMI *hp; { register int i, j; double mag; @@ -375,13 +435,14 @@ AMBHEMI *hp; error(CONSISTENCY, "division order in dirgradient"); #endif - mag += sqrt((i+.5)/hp->nt)*bright(dp->v); + /* tan(t) */ + mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0); 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])*PI/(hp->nt*hp->np); + gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np); }