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.14 by greg, Tue Apr 19 01:15:06 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 > int
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 >        int     ns;
28 >        double  d;
29 >        register int  i;
30 >                                        /* set number of divisions */
31 >        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
32 >        i = ambacc > FTINY ? 3 : 1;     /* minimum number of samples */
33 >        if (hp->nt < i)
34 >                hp->nt = i;
35 >        hp->np = PI * hp->nt + 0.5;
36 >                                        /* set number of super-samples */
37 >        ns = ambssamp * wt + 0.5;
38 >                                        /* assign coefficient */
39 >        d = 1.0/(hp->nt*hp->np + ns);   /* XXX weight not uniform if ns > 0 */
40 >        copycolor(hp->acoef, ac);
41 >        scalecolor(hp->acoef, d);
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 >        return(ns);
55   }
56  
57  
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
58   int
59 < divsample(dp, h, r)                     /* sample a division */
60 < register AMBSAMP  *dp;
61 < AMBHEMI  *h;
62 < RAY  *r;
59 > divsample(                              /* sample a division */
60 >        register AMBSAMP  *dp,
61 >        AMBHEMI  *h,
62 >        RAY  *r
63 > )
64   {
65          RAY  ar;
66          int  hlist[3];
# Line 108 | Line 69 | RAY  *r;
69          double  b2;
70          double  phi;
71          register int  i;
72 <
73 <        if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0)
72 >                                        /* assign coefficient */
73 >        if (ambacc <= FTINY)            /* no storage, so report accurately */
74 >                copycolor(ar.rcoef, h->acoef);
75 >        else                            /* else lie for sake of cache */
76 >                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
77 >        if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0)
78                  return(-1);
79 +        copycolor(ar.rcoef, h->acoef);  /* correct coefficient rtrace output */
80          hlist[0] = r->rno;
81          hlist[1] = dp->t;
82          hlist[2] = dp->p;
# Line 142 | Line 108 | RAY  *r;
108   }
109  
110  
111 + static int
112 + ambcmp(                                 /* decreasing order */
113 +        const void *p1,
114 +        const void *p2
115 + )
116 + {
117 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
118 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
119 +
120 +        if (d1->k < d2->k)
121 +                return(1);
122 +        if (d1->k > d2->k)
123 +                return(-1);
124 +        return(0);
125 + }
126 +
127 +
128 + static int
129 + ambnorm(                                /* standard order */
130 +        const void *p1,
131 +        const void *p2
132 + )
133 + {
134 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
135 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
136 +        register int    c;
137 +
138 +        if ( (c = d1->t - d2->t) )
139 +                return(c);
140 +        return(d1->p - d2->p);
141 + }
142 +
143 +
144   double
145 < doambient(acol, r, wt, pg, dg)          /* compute ambient component */
146 < COLOR  acol;
147 < RAY  *r;
148 < double  wt;
149 < FVECT  pg, dg;
145 > doambient(                              /* compute ambient component */
146 >        COLOR  acol,
147 >        RAY  *r,
148 >        COLOR  ac,
149 >        double  wt,
150 >        FVECT  pg,
151 >        FVECT  dg
152 > )
153   {
154          double  b, d;
155          AMBHEMI  hemi;
# Line 160 | Line 162 | FVECT  pg, dg;
162                                          /* initialize color */
163          setcolor(acol, 0.0, 0.0, 0.0);
164                                          /* initialize hemisphere */
165 <        inithemi(&hemi, r, wt);
165 >        ns = inithemi(&hemi, r, ac, wt);
166          ndivs = hemi.nt * hemi.np;
167          if (ndivs == 0)
168                  return(0.0);
169 <                                        /* set number of super-samples */
168 <        ns = ambssamp * wt + 0.5;
169 >                                        /* allocate super-samples */
170          if (ns > 0 || pg != NULL || dg != NULL) {
171                  div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
172                  if (div == NULL)
# Line 197 | Line 198 | FVECT  pg, dg;
198                  qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
199                                                  /* super-sample */
200                  for (i = ns; i > 0; i--) {
201 <                        copystruct(&dnew, div);
201 >                        dnew = *div;
202                          if (divsample(&dnew, &hemi, r) < 0)
203                                  goto oopsy;
204                                                          /* reinsert */
205                          dp = div;
206                          j = ndivs < i ? ndivs : i;
207                          while (--j > 0 && dnew.k < dp[1].k) {
208 <                                copystruct(dp, dp+1);
208 >                                *dp = *(dp+1);
209                                  dp++;
210                          }
211 <                        copystruct(dp, &dnew);
211 >                        *dp = dnew;
212                  }
213                  if (pg != NULL || dg != NULL)   /* restore order */
214                          qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
# Line 278 | Line 279 | oopsy:
279  
280  
281   void
282 < inithemi(hp, r, wt)             /* initialize sampling hemisphere */
283 < register AMBHEMI  *hp;
284 < RAY  *r;
285 < double  wt;
282 > comperrs(                       /* compute initial error estimates */
283 >        AMBSAMP  *da,   /* assumes standard ordering */
284 >        register AMBHEMI  *hp
285 > )
286   {
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 {
287          double  b, b2;
288          int  i, j;
289          register AMBSAMP  *dp;
# Line 358 | Line 331 | register AMBHEMI  *hp;
331  
332  
333   void
334 < posgradient(gv, da, hp)                         /* compute position gradient */
335 < FVECT  gv;
336 < AMBSAMP  *da;                   /* assumes standard ordering */
337 < register AMBHEMI  *hp;
334 > posgradient(                                    /* compute position gradient */
335 >        FVECT  gv,
336 >        AMBSAMP  *da,                   /* assumes standard ordering */
337 >        register AMBHEMI  *hp
338 > )
339   {
340          register int  i, j;
341          double  nextsine, lastsine, b, d;
# Line 415 | Line 389 | register AMBHEMI  *hp;
389  
390  
391   void
392 < dirgradient(gv, da, hp)                         /* compute direction gradient */
393 < FVECT  gv;
394 < AMBSAMP  *da;                   /* assumes standard ordering */
395 < register AMBHEMI  *hp;
392 > dirgradient(                                    /* compute direction gradient */
393 >        FVECT  gv,
394 >        AMBSAMP  *da,                   /* assumes standard ordering */
395 >        register AMBHEMI  *hp
396 > )
397   {
398          register int  i, j;
399          double  mag;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines