ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambcomp.c
(Generate patch)

Comparing ray/src/rt/ambcomp.c (file contents):
Revision 2.9 by greg, Sat Feb 22 02:07:28 2003 UTC vs.
Revision 2.18 by greg, Mon Jun 6 19:14:28 2005 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7   *  Declarations of external symbols in ambient.h
8   */
9  
10 < /* ====================================================================
11 < * The Radiance Software License, Version 1.0
12 < *
13 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 < *
16 < * Redistribution and use in source and binary forms, with or without
17 < * modification, are permitted provided that the following conditions
18 < * are met:
19 < *
20 < * 1. Redistributions of source code must retain the above copyright
21 < *         notice, this list of conditions and the following disclaimer.
22 < *
23 < * 2. Redistributions in binary form must reproduce the above copyright
24 < *       notice, this list of conditions and the following disclaimer in
25 < *       the documentation and/or other materials provided with the
26 < *       distribution.
27 < *
28 < * 3. The end-user documentation included with the redistribution,
29 < *           if any, must include the following acknowledgment:
30 < *             "This product includes Radiance software
31 < *                 (http://radsite.lbl.gov/)
32 < *                 developed by the Lawrence Berkeley National Laboratory
33 < *               (http://www.lbl.gov/)."
34 < *       Alternately, this acknowledgment may appear in the software itself,
35 < *       if and wherever such third-party acknowledgments normally appear.
36 < *
37 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 < *       and "The Regents of the University of California" must
39 < *       not be used to endorse or promote products derived from this
40 < *       software without prior written permission. For written
41 < *       permission, please contact [email protected].
42 < *
43 < * 5. Products derived from this software may not be called "Radiance",
44 < *       nor may "Radiance" appear in their name, without prior written
45 < *       permission of Lawrence Berkeley National Laboratory.
46 < *
47 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 < * SUCH DAMAGE.
59 < * ====================================================================
60 < *
61 < * This software consists of voluntary contributions made by many
62 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 < * information on Lawrence Berkeley National Laboratory, please see
64 < * <http://www.lbl.gov/>.
65 < */
10 > #include "copyright.h"
11  
12   #include  "ray.h"
13  
# Line 71 | Line 16 | static const char      RCSid[] = "$Id$";
16   #include  "random.h"
17  
18  
19 < static int
20 < ambcmp(d1, d2)                          /* decreasing order */
21 < AMBSAMP  *d1, *d2;
19 > void
20 > inithemi(                       /* initialize sampling hemisphere */
21 >        register AMBHEMI  *hp,
22 >        COLOR ac,
23 >        RAY  *r,
24 >        double  wt
25 > )
26   {
27 <        if (d1->k < d2->k)
28 <                return(1);
29 <        if (d1->k > d2->k)
30 <                return(-1);
31 <        return(0);
27 >        double  d;
28 >        register int  i;
29 >                                        /* set number of divisions */
30 >        if (ambacc <= FTINY &&
31 >                        wt > (d = 0.8*bright(ac)*r->rweight/(ambdiv*minweight)))
32 >                wt = d;                 /* avoid ray termination */
33 >        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
34 >        i = ambacc > FTINY ? 3 : 1;     /* minimum number of samples */
35 >        if (hp->nt < i)
36 >                hp->nt = i;
37 >        hp->np = PI * hp->nt + 0.5;
38 >                                        /* set number of super-samples */
39 >        hp->ns = ambssamp * wt + 0.5;
40 >                                        /* assign coefficient */
41 >        copycolor(hp->acoef, ac);
42 >        d = 1.0/(hp->nt*hp->np);
43 >        scalecolor(hp->acoef, d);
44 >                                        /* make axes */
45 >        VCOPY(hp->uz, r->ron);
46 >        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
47 >        for (i = 0; i < 3; i++)
48 >                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
49 >                        break;
50 >        if (i >= 3)
51 >                error(CONSISTENCY, "bad ray direction in inithemi");
52 >        hp->uy[i] = 1.0;
53 >        fcross(hp->ux, hp->uy, hp->uz);
54 >        normalize(hp->ux);
55 >        fcross(hp->uy, hp->uz, hp->ux);
56   }
57  
58  
86 static int
87 ambnorm(d1, d2)                         /* standard order */
88 AMBSAMP  *d1, *d2;
89 {
90        register int  c;
91
92        if (c = d1->t - d2->t)
93                return(c);
94        return(d1->p - d2->p);
95 }
96
97
59   int
60 < divsample(dp, h, r)                     /* sample a division */
61 < register AMBSAMP  *dp;
62 < AMBHEMI  *h;
63 < RAY  *r;
60 > divsample(                              /* sample a division */
61 >        register AMBSAMP  *dp,
62 >        AMBHEMI  *h,
63 >        RAY  *r
64 > )
65   {
66          RAY  ar;
67          int  hlist[3];
# Line 108 | Line 70 | RAY  *r;
70          double  b2;
71          double  phi;
72          register int  i;
73 <
74 <        if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0)
73 >                                        /* ambient coefficient for weight */
74 >        if (ambacc > FTINY)
75 >                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
76 >        else
77 >                copycolor(ar.rcoef, h->acoef);
78 >        if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0)
79                  return(-1);
80 +        if (ambacc > FTINY) {
81 +                multcolor(ar.rcoef, h->acoef);
82 +                scalecolor(ar.rcoef, 1./AVGREFL);
83 +        }
84          hlist[0] = r->rno;
85          hlist[1] = dp->t;
86          hlist[2] = dp->p;
# Line 127 | Line 97 | RAY  *r;
97          dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
98          rayvalue(&ar);
99          ndims--;
100 +        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
101          addcolor(dp->v, ar.rcol);
102                                          /* use rt to improve gradient calc */
103          if (ar.rt > FTINY && ar.rt < FHUGE)
# Line 142 | Line 113 | RAY  *r;
113   }
114  
115  
116 + static int
117 + ambcmp(                                 /* decreasing order */
118 +        const void *p1,
119 +        const void *p2
120 + )
121 + {
122 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
123 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
124 +
125 +        if (d1->k < d2->k)
126 +                return(1);
127 +        if (d1->k > d2->k)
128 +                return(-1);
129 +        return(0);
130 + }
131 +
132 +
133 + static int
134 + ambnorm(                                /* standard order */
135 +        const void *p1,
136 +        const void *p2
137 + )
138 + {
139 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
140 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
141 +        register int    c;
142 +
143 +        if ( (c = d1->t - d2->t) )
144 +                return(c);
145 +        return(d1->p - d2->p);
146 + }
147 +
148 +
149   double
150 < doambient(acol, r, wt, pg, dg)          /* compute ambient component */
151 < COLOR  acol;
152 < RAY  *r;
153 < double  wt;
154 < FVECT  pg, dg;
150 > doambient(                              /* compute ambient component */
151 >        COLOR  acol,
152 >        RAY  *r,
153 >        double  wt,
154 >        FVECT  pg,
155 >        FVECT  dg
156 > )
157   {
158          double  b, d;
159          AMBHEMI  hemi;
# Line 155 | Line 161 | FVECT  pg, dg;
161          AMBSAMP  dnew;
162          register AMBSAMP  *dp;
163          double  arad;
164 <        int  ndivs, ns;
164 >        int  ndivs;
165          register int  i, j;
160                                        /* initialize color */
161        setcolor(acol, 0.0, 0.0, 0.0);
166                                          /* initialize hemisphere */
167 <        inithemi(&hemi, r, wt);
167 >        inithemi(&hemi, acol, r, wt);
168          ndivs = hemi.nt * hemi.np;
169 +                                        /* initialize */
170 +        if (pg != NULL)
171 +                pg[0] = pg[1] = pg[2] = 0.0;
172 +        if (dg != NULL)
173 +                dg[0] = dg[1] = dg[2] = 0.0;
174 +        setcolor(acol, 0.0, 0.0, 0.0);
175          if (ndivs == 0)
176                  return(0.0);
177 <                                        /* set number of super-samples */
178 <        ns = ambssamp * wt + 0.5;
169 <        if (ns > 0 || pg != NULL || dg != NULL) {
177 >                                        /* allocate super-samples */
178 >        if (hemi.ns > 0 || pg != NULL || dg != NULL) {
179                  div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
180                  if (div == NULL)
181                          error(SYSTEM, "out of memory in doambient");
# Line 182 | Line 191 | FVECT  pg, dg;
191                          setcolor(dp->v, 0.0, 0.0, 0.0);
192                          dp->r = 0.0;
193                          dp->n = 0;
194 <                        if (divsample(dp, &hemi, r) < 0)
195 <                                goto oopsy;
194 >                        if (divsample(dp, &hemi, r) < 0) {
195 >                                if (div == NULL) continue;
196 >                                dp++;
197 >                                hemi.ns = 0;    /* incomplete sampling */
198 >                                pg = dg = NULL;
199 >                                continue;
200 >                        }
201                          arad += dp->r;
202                          if (div != NULL)
203                                  dp++;
204                          else
205                                  addcolor(acol, dp->v);
206                  }
207 <        if (ns > 0 && arad > FTINY && ndivs/arad < minarad)
208 <                ns = 0;                 /* close enough */
209 <        else if (ns > 0) {              /* else perform super-sampling */
207 >        if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad)
208 >                hemi.ns = 0;            /* close enough */
209 >        else if (hemi.ns > 0) {         /* else perform super-sampling */
210                  comperrs(div, &hemi);                   /* compute errors */
211                  qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
212                                                  /* super-sample */
213 <                for (i = ns; i > 0; i--) {
214 <                        copystruct(&dnew, div);
215 <                        if (divsample(&dnew, &hemi, r) < 0)
216 <                                goto oopsy;
217 <                                                        /* reinsert */
218 <                        dp = div;
213 >                for (i = hemi.ns; i > 0; i--) {
214 >                        dnew = *div;
215 >                        if (divsample(&dnew, &hemi, r) < 0) {
216 >                                dp++;
217 >                                continue;
218 >                        }
219 >                        dp = div;               /* reinsert */
220                          j = ndivs < i ? ndivs : i;
221                          while (--j > 0 && dnew.k < dp[1].k) {
222 <                                copystruct(dp, dp+1);
222 >                                *dp = *(dp+1);
223                                  dp++;
224                          }
225 <                        copystruct(dp, &dnew);
225 >                        *dp = dnew;
226                  }
227                  if (pg != NULL || dg != NULL)   /* restore order */
228                          qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
# Line 227 | Line 242 | FVECT  pg, dg;
242                  }
243                  b = bright(acol);
244                  if (b > FTINY) {
245 <                        b = ndivs/b;
245 >                        b = 1.0/b;      /* compute & normalize gradient(s) */
246                          if (pg != NULL) {
247                                  posgradient(pg, div, &hemi);
248                                  for (i = 0; i < 3; i++)
# Line 238 | Line 253 | FVECT  pg, dg;
253                                  for (i = 0; i < 3; i++)
254                                          dg[i] *= b;
255                          }
241                } else {
242                        if (pg != NULL)
243                                for (i = 0; i < 3; i++)
244                                        pg[i] = 0.0;
245                        if (dg != NULL)
246                                for (i = 0; i < 3; i++)
247                                        dg[i] = 0.0;
256                  }
257                  free((void *)div);
258          }
251        b = 1.0/ndivs;
252        scalecolor(acol, b);
259          if (arad <= FTINY)
260                  arad = maxarad;
261          else
262 <                arad = (ndivs+ns)/arad;
262 >                arad = (ndivs+hemi.ns)/arad;
263          if (pg != NULL) {               /* reduce radius if gradient large */
264                  d = DOT(pg,pg);
265                  if (d*arad*arad > 1.0)
# Line 270 | Line 276 | FVECT  pg, dg;
276          if ((arad /= sqrt(wt)) > maxarad)
277                  arad = maxarad;
278          return(arad);
273 oopsy:
274        if (div != NULL)
275                free((void *)div);
276        return(0.0);
279   }
280  
281  
282   void
283 < inithemi(hp, r, wt)             /* initialize sampling hemisphere */
284 < register AMBHEMI  *hp;
285 < RAY  *r;
286 < double  wt;
283 > comperrs(                       /* compute initial error estimates */
284 >        AMBSAMP  *da,   /* assumes standard ordering */
285 >        register AMBHEMI  *hp
286 > )
287   {
286        register int  i;
287                                        /* set number of divisions */
288        if (wt < (.25*PI)/ambdiv+FTINY) {
289                hp->nt = hp->np = 0;
290                return;                 /* zero samples */
291        }
292        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
293        hp->np = PI * hp->nt + 0.5;
294                                        /* make axes */
295        VCOPY(hp->uz, r->ron);
296        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
297        for (i = 0; i < 3; i++)
298                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
299                        break;
300        if (i >= 3)
301                error(CONSISTENCY, "bad ray direction in inithemi");
302        hp->uy[i] = 1.0;
303        fcross(hp->ux, hp->uy, hp->uz);
304        normalize(hp->ux);
305        fcross(hp->uy, hp->uz, hp->ux);
306 }
307
308
309 void
310 comperrs(da, hp)                /* compute initial error estimates */
311 AMBSAMP  *da;           /* assumes standard ordering */
312 register AMBHEMI  *hp;
313 {
288          double  b, b2;
289          int  i, j;
290          register AMBSAMP  *dp;
# Line 358 | Line 332 | register AMBHEMI  *hp;
332  
333  
334   void
335 < posgradient(gv, da, hp)                         /* compute position gradient */
336 < FVECT  gv;
337 < AMBSAMP  *da;                   /* assumes standard ordering */
338 < register AMBHEMI  *hp;
335 > posgradient(                                    /* compute position gradient */
336 >        FVECT  gv,
337 >        AMBSAMP  *da,                   /* assumes standard ordering */
338 >        register AMBHEMI  *hp
339 > )
340   {
341          register int  i, j;
342          double  nextsine, lastsine, b, d;
# Line 410 | Line 385 | register AMBHEMI  *hp;
385                  yd += mag0*sinp + mag1*cosp;
386          }
387          for (i = 0; i < 3; i++)
388 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI;
388 >                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
389   }
390  
391  
392   void
393 < dirgradient(gv, da, hp)                         /* compute direction gradient */
394 < FVECT  gv;
395 < AMBSAMP  *da;                   /* assumes standard ordering */
396 < register AMBHEMI  *hp;
393 > dirgradient(                                    /* compute direction gradient */
394 >        FVECT  gv,
395 >        AMBSAMP  *da,                   /* assumes standard ordering */
396 >        register AMBHEMI  *hp
397 > )
398   {
399          register int  i, j;
400          double  mag;
# Line 444 | Line 420 | register AMBHEMI  *hp;
420                  yd += mag * tsin(phi);
421          }
422          for (i = 0; i < 3; i++)
423 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);
423 >                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
424   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines