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.15 by greg, Sat May 28 22:27:54 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 >        RAY  *r,
23 >        COLOR ac,
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 >        register int  i;
28 >                                        /* set number of divisions */
29 >        hp->nt = sqrt(ambdiv * wt * (1./PI/AVGREFL)) + 0.5;
30 >        i = ambacc > FTINY ? 3 : 1;     /* minimum number of samples */
31 >        if (hp->nt < i)
32 >                hp->nt = i;
33 >        hp->np = PI * hp->nt + 0.5;
34 >                                        /* set number of super-samples */
35 >        hp->ns = ambssamp * wt + 0.5;
36 >                                        /* assign coefficients */
37 >        copycolor(hp->acoef, ac);
38 >        if (wt >= r->rweight)
39 >                hp->drc = 1.;
40 >        else
41 >                hp->drc = wt / r->rweight;
42 >                                        /* make axes */
43 >        VCOPY(hp->uz, r->ron);
44 >        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
45 >        for (i = 0; i < 3; i++)
46 >                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
47 >                        break;
48 >        if (i >= 3)
49 >                error(CONSISTENCY, "bad ray direction in inithemi");
50 >        hp->uy[i] = 1.0;
51 >        fcross(hp->ux, hp->uy, hp->uz);
52 >        normalize(hp->ux);
53 >        fcross(hp->uy, hp->uz, hp->ux);
54   }
55  
56  
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
57   int
58 < divsample(dp, h, r)                     /* sample a division */
59 < register AMBSAMP  *dp;
60 < AMBHEMI  *h;
61 < RAY  *r;
58 > divsample(                              /* sample a division */
59 >        register AMBSAMP  *dp,
60 >        AMBHEMI  *h,
61 >        RAY  *r
62 > )
63   {
64          RAY  ar;
65          int  hlist[3];
# Line 108 | Line 68 | RAY  *r;
68          double  b2;
69          double  phi;
70          register int  i;
71 <
72 <        if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0)
71 >                                        /* ambient coefficient for weight */
72 >        setcolor(ar.rcoef, h->drc, h->drc, h->drc);
73 >        if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0)
74                  return(-1);
75 +        copycolor(ar.rcoef, h->acoef);  /* correct coefficient for trace */
76 +        b2 = 1.0/(h->nt*h->np + h->ns); /* XXX not uniform if ns > 0 */
77 +        scalecolor(ar.rcoef, b2);
78          hlist[0] = r->rno;
79          hlist[1] = dp->t;
80          hlist[2] = dp->p;
# Line 142 | Line 106 | RAY  *r;
106   }
107  
108  
109 + static int
110 + ambcmp(                                 /* decreasing order */
111 +        const void *p1,
112 +        const void *p2
113 + )
114 + {
115 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
116 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
117 +
118 +        if (d1->k < d2->k)
119 +                return(1);
120 +        if (d1->k > d2->k)
121 +                return(-1);
122 +        return(0);
123 + }
124 +
125 +
126 + static int
127 + ambnorm(                                /* standard order */
128 +        const void *p1,
129 +        const void *p2
130 + )
131 + {
132 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
133 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
134 +        register int    c;
135 +
136 +        if ( (c = d1->t - d2->t) )
137 +                return(c);
138 +        return(d1->p - d2->p);
139 + }
140 +
141 +
142   double
143 < doambient(acol, r, wt, pg, dg)          /* compute ambient component */
144 < COLOR  acol;
145 < RAY  *r;
146 < double  wt;
147 < FVECT  pg, dg;
143 > doambient(                              /* compute ambient component */
144 >        COLOR  acol,
145 >        RAY  *r,
146 >        COLOR  ac,
147 >        double  wt,
148 >        FVECT  pg,
149 >        FVECT  dg
150 > )
151   {
152          double  b, d;
153          AMBHEMI  hemi;
# Line 155 | Line 155 | FVECT  pg, dg;
155          AMBSAMP  dnew;
156          register AMBSAMP  *dp;
157          double  arad;
158 <        int  ndivs, ns;
158 >        int  ndivs;
159          register int  i, j;
160                                          /* initialize color */
161          setcolor(acol, 0.0, 0.0, 0.0);
162                                          /* initialize hemisphere */
163 <        inithemi(&hemi, r, wt);
163 >        inithemi(&hemi, r, ac, wt);
164          ndivs = hemi.nt * hemi.np;
165          if (ndivs == 0)
166                  return(0.0);
167 <                                        /* set number of super-samples */
168 <        ns = ambssamp * wt + 0.5;
169 <        if (ns > 0 || pg != NULL || dg != NULL) {
167 >                                        /* allocate super-samples */
168 >        if (hemi.ns > 0 || pg != NULL || dg != NULL) {
169                  div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
170                  if (div == NULL)
171                          error(SYSTEM, "out of memory in doambient");
# Line 190 | Line 189 | FVECT  pg, dg;
189                          else
190                                  addcolor(acol, dp->v);
191                  }
192 <        if (ns > 0 && arad > FTINY && ndivs/arad < minarad)
193 <                ns = 0;                 /* close enough */
194 <        else if (ns > 0) {              /* else perform super-sampling */
192 >        if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad)
193 >                hemi.ns = 0;            /* close enough */
194 >        else if (hemi.ns > 0) {         /* else perform super-sampling */
195                  comperrs(div, &hemi);                   /* compute errors */
196                  qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
197                                                  /* super-sample */
198 <                for (i = ns; i > 0; i--) {
199 <                        copystruct(&dnew, div);
198 >                for (i = hemi.ns; i > 0; i--) {
199 >                        dnew = *div;
200                          if (divsample(&dnew, &hemi, r) < 0)
201                                  goto oopsy;
202                                                          /* reinsert */
203                          dp = div;
204                          j = ndivs < i ? ndivs : i;
205                          while (--j > 0 && dnew.k < dp[1].k) {
206 <                                copystruct(dp, dp+1);
206 >                                *dp = *(dp+1);
207                                  dp++;
208                          }
209 <                        copystruct(dp, &dnew);
209 >                        *dp = dnew;
210                  }
211                  if (pg != NULL || dg != NULL)   /* restore order */
212                          qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
# Line 253 | Line 252 | FVECT  pg, dg;
252          if (arad <= FTINY)
253                  arad = maxarad;
254          else
255 <                arad = (ndivs+ns)/arad;
255 >                arad = (ndivs+hemi.ns)/arad;
256          if (pg != NULL) {               /* reduce radius if gradient large */
257                  d = DOT(pg,pg);
258                  if (d*arad*arad > 1.0)
# Line 278 | Line 277 | oopsy:
277  
278  
279   void
280 < inithemi(hp, r, wt)             /* initialize sampling hemisphere */
281 < register AMBHEMI  *hp;
282 < RAY  *r;
283 < double  wt;
280 > comperrs(                       /* compute initial error estimates */
281 >        AMBSAMP  *da,   /* assumes standard ordering */
282 >        register AMBHEMI  *hp
283 > )
284   {
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 {
285          double  b, b2;
286          int  i, j;
287          register AMBSAMP  *dp;
# Line 358 | Line 329 | register AMBHEMI  *hp;
329  
330  
331   void
332 < posgradient(gv, da, hp)                         /* compute position gradient */
333 < FVECT  gv;
334 < AMBSAMP  *da;                   /* assumes standard ordering */
335 < register AMBHEMI  *hp;
332 > posgradient(                                    /* compute position gradient */
333 >        FVECT  gv,
334 >        AMBSAMP  *da,                   /* assumes standard ordering */
335 >        register AMBHEMI  *hp
336 > )
337   {
338          register int  i, j;
339          double  nextsine, lastsine, b, d;
# Line 415 | Line 387 | register AMBHEMI  *hp;
387  
388  
389   void
390 < dirgradient(gv, da, hp)                         /* compute direction gradient */
391 < FVECT  gv;
392 < AMBSAMP  *da;                   /* assumes standard ordering */
393 < register AMBHEMI  *hp;
390 > dirgradient(                                    /* compute direction gradient */
391 >        FVECT  gv,
392 >        AMBSAMP  *da,                   /* assumes standard ordering */
393 >        register AMBHEMI  *hp
394 > )
395   {
396          register int  i, j;
397          double  mag;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines