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.14 by greg, Tue Apr 19 01:15:06 2005 UTC vs.
Revision 2.16 by greg, Tue May 31 18:01:09 2005 UTC

# Line 16 | Line 16 | static const char      RCSid[] = "$Id$";
16   #include  "random.h"
17  
18  
19 < int
19 > void
20   inithemi(                       /* initialize sampling hemisphere */
21          register AMBHEMI  *hp,
22 +        COLOR ac,
23          RAY  *r,
23        COLOR ac,
24          double  wt
25   )
26   {
27        int     ns;
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 <        ns = ambssamp * wt + 0.5;
39 >        hp->ns = ambssamp * wt + 0.5;
40                                          /* assign coefficient */
39        d = 1.0/(hp->nt*hp->np + ns);   /* XXX weight not uniform if ns > 0 */
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);
# Line 51 | Line 53 | inithemi(                      /* initialize sampling hemisphere */
53          fcross(hp->ux, hp->uy, hp->uz);
54          normalize(hp->ux);
55          fcross(hp->uy, hp->uz, hp->ux);
54        return(ns);
56   }
57  
58  
# Line 69 | Line 70 | divsample(                             /* sample a division */
70          double  b2;
71          double  phi;
72          register int  i;
73 <                                        /* assign coefficient */
74 <        if (ambacc <= FTINY)            /* no storage, so report accurately */
74 <                copycolor(ar.rcoef, h->acoef);
75 <        else                            /* else lie for sake of cache */
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 <        copycolor(ar.rcoef, h->acoef);  /* correct coefficient rtrace output */
80 >        if (ambacc > FTINY)
81 >                copycolor(ar.rcoef, h->acoef);
82          hlist[0] = r->rno;
83          hlist[1] = dp->t;
84          hlist[2] = dp->p;
# Line 93 | Line 95 | divsample(                             /* sample a division */
95          dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
96          rayvalue(&ar);
97          ndims--;
98 +        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
99          addcolor(dp->v, ar.rcol);
100                                          /* use rt to improve gradient calc */
101          if (ar.rt > FTINY && ar.rt < FHUGE)
# Line 145 | Line 148 | double
148   doambient(                              /* compute ambient component */
149          COLOR  acol,
150          RAY  *r,
148        COLOR  ac,
151          double  wt,
152          FVECT  pg,
153          FVECT  dg
# Line 157 | Line 159 | doambient(                             /* compute ambient component */
159          AMBSAMP  dnew;
160          register AMBSAMP  *dp;
161          double  arad;
162 <        int  ndivs, ns;
162 >        int  ndivs;
163          register int  i, j;
162                                        /* initialize color */
163        setcolor(acol, 0.0, 0.0, 0.0);
164                                          /* initialize hemisphere */
165 <        ns = inithemi(&hemi, r, ac, wt);
165 >        inithemi(&hemi, acol, r, wt);
166          ndivs = hemi.nt * hemi.np;
167 +                                        /* initialize sum */
168 +        setcolor(acol, 0.0, 0.0, 0.0);
169          if (ndivs == 0)
170                  return(0.0);
171                                          /* allocate super-samples */
172 <        if (ns > 0 || pg != NULL || dg != NULL) {
172 >        if (hemi.ns > 0 || pg != NULL || dg != NULL) {
173                  div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
174                  if (div == NULL)
175                          error(SYSTEM, "out of memory in doambient");
# Line 183 | Line 185 | doambient(                             /* compute ambient component */
185                          setcolor(dp->v, 0.0, 0.0, 0.0);
186                          dp->r = 0.0;
187                          dp->n = 0;
188 <                        if (divsample(dp, &hemi, r) < 0)
189 <                                goto oopsy;
188 >                        if (divsample(dp, &hemi, r) < 0) {
189 >                                if (div != NULL) dp++;
190 >                                continue;
191 >                        }
192                          arad += dp->r;
193                          if (div != NULL)
194                                  dp++;
195                          else
196                                  addcolor(acol, dp->v);
197                  }
198 <        if (ns > 0 && arad > FTINY && ndivs/arad < minarad)
199 <                ns = 0;                 /* close enough */
200 <        else if (ns > 0) {              /* else perform super-sampling */
198 >        if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad)
199 >                hemi.ns = 0;            /* close enough */
200 >        else if (hemi.ns > 0) {         /* else perform super-sampling */
201                  comperrs(div, &hemi);                   /* compute errors */
202                  qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
203                                                  /* super-sample */
204 <                for (i = ns; i > 0; i--) {
204 >                for (i = hemi.ns; i > 0; i--) {
205                          dnew = *div;
206 <                        if (divsample(&dnew, &hemi, r) < 0)
207 <                                goto oopsy;
208 <                                                        /* reinsert */
209 <                        dp = div;
206 >                        if (divsample(&dnew, &hemi, r) < 0) {
207 >                                dp++;
208 >                                continue;
209 >                        }
210 >                        dp = div;               /* reinsert */
211                          j = ndivs < i ? ndivs : i;
212                          while (--j > 0 && dnew.k < dp[1].k) {
213                                  *dp = *(dp+1);
# Line 228 | Line 233 | doambient(                             /* compute ambient component */
233                  }
234                  b = bright(acol);
235                  if (b > FTINY) {
236 <                        b = ndivs/b;
236 >                        b = 1.0/b;      /* normalize gradient(s) */
237                          if (pg != NULL) {
238                                  posgradient(pg, div, &hemi);
239                                  for (i = 0; i < 3; i++)
# Line 249 | Line 254 | doambient(                             /* compute ambient component */
254                  }
255                  free((void *)div);
256          }
252        b = 1.0/ndivs;
253        scalecolor(acol, b);
257          if (arad <= FTINY)
258                  arad = maxarad;
259          else
260 <                arad = (ndivs+ns)/arad;
260 >                arad = (ndivs+hemi.ns)/arad;
261          if (pg != NULL) {               /* reduce radius if gradient large */
262                  d = DOT(pg,pg);
263                  if (d*arad*arad > 1.0)
# Line 271 | Line 274 | doambient(                             /* compute ambient component */
274          if ((arad /= sqrt(wt)) > maxarad)
275                  arad = maxarad;
276          return(arad);
274 oopsy:
275        if (div != NULL)
276                free((void *)div);
277        return(0.0);
277   }
278  
279  
# Line 384 | Line 383 | posgradient(                                   /* compute position gradient */
383                  yd += mag0*sinp + mag1*cosp;
384          }
385          for (i = 0; i < 3; i++)
386 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI;
386 >                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
387   }
388  
389  
# Line 419 | Line 418 | dirgradient(                                   /* compute direction gradient */
418                  yd += mag * tsin(phi);
419          }
420          for (i = 0; i < 3; i++)
421 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);
421 >                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
422   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines