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.15 by greg, Sat May 28 22:27:54 2005 UTC vs.
Revision 2.19 by greg, Mon Aug 22 16:07:26 2005 UTC

# Line 19 | Line 19 | static const char      RCSid[] = "$Id$";
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 +        double  d;
28          register int  i;
29                                          /* set number of divisions */
30 <        hp->nt = sqrt(ambdiv * wt * (1./PI/AVGREFL)) + 0.5;
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 coefficients */
40 >                                        /* assign coefficient */
41          copycolor(hp->acoef, ac);
42 <        if (wt >= r->rweight)
43 <                hp->drc = 1.;
40 <        else
41 <                hp->drc = wt / r->rweight;
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;
# Line 69 | Line 71 | divsample(                             /* sample a division */
71          double  phi;
72          register int  i;
73                                          /* ambient coefficient for weight */
74 <        setcolor(ar.rcoef, h->drc, h->drc, h->drc);
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 for trace */
81 <        b2 = 1.0/(h->nt*h->np + h->ns); /* XXX not uniform if ns > 0 */
82 <        scalecolor(ar.rcoef, b2);
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 91 | Line 97 | divsample(                             /* sample a division */
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 143 | Line 150 | double
150   doambient(                              /* compute ambient component */
151          COLOR  acol,
152          RAY  *r,
146        COLOR  ac,
153          double  wt,
154          FVECT  pg,
155          FVECT  dg
# Line 155 | Line 161 | doambient(                             /* compute ambient component */
161          AMBSAMP  dnew;
162          register AMBSAMP  *dp;
163          double  arad;
164 <        int  ndivs;
164 >        int  divcnt;
165          register int  i, j;
160                                        /* initialize color */
161        setcolor(acol, 0.0, 0.0, 0.0);
166                                          /* initialize hemisphere */
167 <        inithemi(&hemi, r, ac, wt);
168 <        ndivs = hemi.nt * hemi.np;
169 <        if (ndivs == 0)
167 >        inithemi(&hemi, acol, r, wt);
168 >        divcnt = 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 (divcnt == 0)
176                  return(0.0);
177                                          /* allocate super-samples */
178          if (hemi.ns > 0 || pg != NULL || dg != NULL) {
179 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
179 >                div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
180                  if (div == NULL)
181                          error(SYSTEM, "out of memory in doambient");
182          } else
# Line 175 | Line 185 | doambient(                             /* compute ambient component */
185          arad = 0.0;
186          if ((dp = div) == NULL)
187                  dp = &dnew;
188 +        divcnt = 0;
189          for (i = 0; i < hemi.nt; i++)
190                  for (j = 0; j < hemi.np; j++) {
191                          dp->t = i; dp->p = j;
192                          setcolor(dp->v, 0.0, 0.0, 0.0);
193                          dp->r = 0.0;
194                          dp->n = 0;
195 <                        if (divsample(dp, &hemi, r) < 0)
196 <                                goto oopsy;
195 >                        if (divsample(dp, &hemi, r) < 0) {
196 >                                if (div != NULL)
197 >                                        dp++;
198 >                                continue;
199 >                        }
200                          arad += dp->r;
201 +                        divcnt++;
202                          if (div != NULL)
203                                  dp++;
204                          else
205                                  addcolor(acol, dp->v);
206                  }
207 <        if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad)
207 >        if (!divcnt)
208 >                return(0.0);            /* no samples taken */
209 >        if (divcnt < hemi.nt*hemi.np) {
210 >                pg = dg = NULL;         /* incomplete sampling */
211 >                hemi.ns = 0;
212 >        } else if (arad > FTINY && divcnt/arad < minarad) {
213                  hemi.ns = 0;            /* close enough */
214 <        else if (hemi.ns > 0) {         /* else perform super-sampling */
214 >        } else if (hemi.ns > 0) {       /* else perform super-sampling? */
215                  comperrs(div, &hemi);                   /* compute errors */
216 <                qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
216 >                qsort(div, divcnt, sizeof(AMBSAMP), ambcmp);    /* sort divs */
217                                                  /* super-sample */
218                  for (i = hemi.ns; i > 0; i--) {
219                          dnew = *div;
220 <                        if (divsample(&dnew, &hemi, r) < 0)
221 <                                goto oopsy;
222 <                                                        /* reinsert */
223 <                        dp = div;
224 <                        j = ndivs < i ? ndivs : i;
220 >                        if (divsample(&dnew, &hemi, r) < 0) {
221 >                                dp++;
222 >                                continue;
223 >                        }
224 >                        dp = div;               /* reinsert */
225 >                        j = divcnt < i ? divcnt : i;
226                          while (--j > 0 && dnew.k < dp[1].k) {
227                                  *dp = *(dp+1);
228                                  dp++;
# Line 209 | Line 230 | doambient(                             /* compute ambient component */
230                          *dp = dnew;
231                  }
232                  if (pg != NULL || dg != NULL)   /* restore order */
233 <                        qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
233 >                        qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
234          }
235                                          /* compute returned values */
236          if (div != NULL) {
237 <                arad = 0.0;
238 <                for (i = ndivs, dp = div; i-- > 0; dp++) {
237 >                arad = 0.0;             /* note: divcnt may be < nt*np */
238 >                for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
239                          arad += dp->r;
240                          if (dp->n > 1) {
241                                  b = 1.0/dp->n;
# Line 226 | Line 247 | doambient(                             /* compute ambient component */
247                  }
248                  b = bright(acol);
249                  if (b > FTINY) {
250 <                        b = ndivs/b;
250 >                        b = 1.0/b;      /* compute & normalize gradient(s) */
251                          if (pg != NULL) {
252                                  posgradient(pg, div, &hemi);
253                                  for (i = 0; i < 3; i++)
# Line 237 | Line 258 | doambient(                             /* compute ambient component */
258                                  for (i = 0; i < 3; i++)
259                                          dg[i] *= b;
260                          }
240                } else {
241                        if (pg != NULL)
242                                for (i = 0; i < 3; i++)
243                                        pg[i] = 0.0;
244                        if (dg != NULL)
245                                for (i = 0; i < 3; i++)
246                                        dg[i] = 0.0;
261                  }
262                  free((void *)div);
263          }
250        b = 1.0/ndivs;
251        scalecolor(acol, b);
264          if (arad <= FTINY)
265                  arad = maxarad;
266          else
267 <                arad = (ndivs+hemi.ns)/arad;
267 >                arad = (divcnt+hemi.ns)/arad;
268          if (pg != NULL) {               /* reduce radius if gradient large */
269                  d = DOT(pg,pg);
270                  if (d*arad*arad > 1.0)
# Line 269 | Line 281 | doambient(                             /* compute ambient component */
281          if ((arad /= sqrt(wt)) > maxarad)
282                  arad = maxarad;
283          return(arad);
272 oopsy:
273        if (div != NULL)
274                free((void *)div);
275        return(0.0);
284   }
285  
286  
# Line 382 | Line 390 | posgradient(                                   /* compute position gradient */
390                  yd += mag0*sinp + mag1*cosp;
391          }
392          for (i = 0; i < 3; i++)
393 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI;
393 >                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
394   }
395  
396  
# Line 417 | Line 425 | dirgradient(                                   /* compute direction gradient */
425                  yd += mag * tsin(phi);
426          }
427          for (i = 0; i < 3; i++)
428 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);
428 >                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
429   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines