--- ray/src/rt/ambcomp.c 2003/02/22 02:07:28 2.9 +++ ray/src/rt/ambcomp.c 2005/04/19 01:15:06 2.14 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.9 2003/02/22 02:07:28 greg Exp $"; +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 @@ -7,62 +7,7 @@ static const char RCSid[] = "$Id: ambcomp.c,v 2.9 2003 * 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 "copyright.h" #include "ray.h" @@ -71,35 +16,51 @@ static const char RCSid[] = "$Id: ambcomp.c,v 2.9 2003 #include "random.h" -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; -{ - register int c; - - if (c = d1->t - d2->t) - return(c); - return(d1->p - d2->p); -} - - int -divsample(dp, h, r) /* sample a division */ -register AMBSAMP *dp; -AMBHEMI *h; -RAY *r; +divsample( /* sample a division */ + register AMBSAMP *dp, + AMBHEMI *h, + RAY *r +) { RAY ar; int hlist[3]; @@ -108,9 +69,14 @@ 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; @@ -142,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; @@ -160,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) @@ -197,17 +198,17 @@ FVECT pg, dg; 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); @@ -278,39 +279,11 @@ oopsy: void -inithemi(hp, r, wt) /* initialize sampling hemisphere */ -register AMBHEMI *hp; -RAY *r; -double wt; +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); -} - - -void -comperrs(da, hp) /* compute initial error estimates */ -AMBSAMP *da; /* assumes standard ordering */ -register AMBHEMI *hp; -{ double b, b2; int i, j; register AMBSAMP *dp; @@ -358,10 +331,11 @@ register AMBHEMI *hp; void -posgradient(gv, da, hp) /* compute position gradient */ -FVECT gv; -AMBSAMP *da; /* assumes standard ordering */ -register AMBHEMI *hp; +posgradient( /* compute position gradient */ + FVECT gv, + AMBSAMP *da, /* assumes standard ordering */ + register AMBHEMI *hp +) { register int i, j; double nextsine, lastsine, b, d; @@ -415,10 +389,11 @@ register AMBHEMI *hp; void -dirgradient(gv, da, hp) /* compute direction gradient */ -FVECT gv; -AMBSAMP *da; /* assumes standard ordering */ -register AMBHEMI *hp; +dirgradient( /* compute direction gradient */ + FVECT gv, + AMBSAMP *da, /* assumes standard ordering */ + register AMBHEMI *hp +) { register int i, j; double mag;