--- ray/src/rt/ambcomp.c 1991/11/12 17:10:29 2.1 +++ 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; @@ -75,8 +117,8 @@ RAY *r; 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 */ @@ -141,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 */ @@ -170,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) { @@ -201,17 +246,14 @@ 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 = maxarad; - else { + else arad = (ndivs+ns)/arad; - if (arad > maxarad) - arad = maxarad; - } if (pg != NULL) { /* reduce radius if gradient large */ d = DOT(pg,pg); if (d*arad*arad > 1.0) @@ -225,14 +267,17 @@ FVECT pg, dg; pg[i] *= d; } } - return(arad/sqrt(wt)); + if ((arad /= sqrt(wt)) > maxarad) + arad = maxarad; + return(arad); oopsy: if (div != NULL) - free((char *)div); + free((void *)div); return(0.0); } +void inithemi(hp, r, wt) /* initialize sampling hemisphere */ register AMBHEMI *hp; RAY *r; @@ -261,6 +306,7 @@ double wt; } +void comperrs(da, hp) /* compute initial error estimates */ AMBSAMP *da; /* assumes standard ordering */ register AMBHEMI *hp; @@ -311,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; @@ -326,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) @@ -336,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 - (double)i/hp->nt; /* cos(t)^2 */ + /* 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; } @@ -364,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; @@ -384,13 +435,14 @@ AMBHEMI *hp; error(CONSISTENCY, "division order in dirgradient"); #endif - mag += sqrt((i+.5)/hp->nt)*bright(dp->v); /* sin(t) */ + /* 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); }