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.22 by greg, Sun Sep 26 15:51:15 2010 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*intens(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 88 | Line 94 | divsample(                             /* sample a division */
94                  ar.rdir[i] =    xd*h->ux[i] +
95                                  yd*h->uy[i] +
96                                  zd*h->uz[i];
97 +        checknorm(ar.rdir);
98          dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
99          rayvalue(&ar);
100          ndims--;
101 +        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
102          addcolor(dp->v, ar.rcol);
103                                          /* use rt to improve gradient calc */
104          if (ar.rt > FTINY && ar.rt < FHUGE)
# Line 143 | Line 151 | double
151   doambient(                              /* compute ambient component */
152          COLOR  acol,
153          RAY  *r,
146        COLOR  ac,
154          double  wt,
155          FVECT  pg,
156          FVECT  dg
# Line 155 | Line 162 | doambient(                             /* compute ambient component */
162          AMBSAMP  dnew;
163          register AMBSAMP  *dp;
164          double  arad;
165 <        int  ndivs;
165 >        int  divcnt;
166          register int  i, j;
160                                        /* initialize color */
161        setcolor(acol, 0.0, 0.0, 0.0);
167                                          /* initialize hemisphere */
168 <        inithemi(&hemi, r, ac, wt);
169 <        ndivs = hemi.nt * hemi.np;
170 <        if (ndivs == 0)
168 >        inithemi(&hemi, acol, r, wt);
169 >        divcnt = hemi.nt * hemi.np;
170 >                                        /* initialize */
171 >        if (pg != NULL)
172 >                pg[0] = pg[1] = pg[2] = 0.0;
173 >        if (dg != NULL)
174 >                dg[0] = dg[1] = dg[2] = 0.0;
175 >        setcolor(acol, 0.0, 0.0, 0.0);
176 >        if (divcnt == 0)
177                  return(0.0);
178                                          /* allocate super-samples */
179          if (hemi.ns > 0 || pg != NULL || dg != NULL) {
180 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
180 >                div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
181                  if (div == NULL)
182                          error(SYSTEM, "out of memory in doambient");
183          } else
# Line 175 | Line 186 | doambient(                             /* compute ambient component */
186          arad = 0.0;
187          if ((dp = div) == NULL)
188                  dp = &dnew;
189 +        divcnt = 0;
190          for (i = 0; i < hemi.nt; i++)
191                  for (j = 0; j < hemi.np; j++) {
192                          dp->t = i; dp->p = j;
193                          setcolor(dp->v, 0.0, 0.0, 0.0);
194                          dp->r = 0.0;
195                          dp->n = 0;
196 <                        if (divsample(dp, &hemi, r) < 0)
197 <                                goto oopsy;
196 >                        if (divsample(dp, &hemi, r) < 0) {
197 >                                if (div != NULL)
198 >                                        dp++;
199 >                                continue;
200 >                        }
201                          arad += dp->r;
202 +                        divcnt++;
203                          if (div != NULL)
204                                  dp++;
205                          else
206                                  addcolor(acol, dp->v);
207                  }
208 <        if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad)
208 >        if (!divcnt) {
209 >                if (div != NULL)
210 >                        free((void *)div);
211 >                return(0.0);            /* no samples taken */
212 >        }
213 >        if (divcnt < hemi.nt*hemi.np) {
214 >                pg = dg = NULL;         /* incomplete sampling */
215 >                hemi.ns = 0;
216 >        } else if (arad > FTINY && divcnt/arad < minarad) {
217                  hemi.ns = 0;            /* close enough */
218 <        else if (hemi.ns > 0) {         /* else perform super-sampling */
218 >        } else if (hemi.ns > 0) {       /* else perform super-sampling? */
219                  comperrs(div, &hemi);                   /* compute errors */
220 <                qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
220 >                qsort(div, divcnt, sizeof(AMBSAMP), ambcmp);    /* sort divs */
221                                                  /* super-sample */
222                  for (i = hemi.ns; i > 0; i--) {
223                          dnew = *div;
224 <                        if (divsample(&dnew, &hemi, r) < 0)
225 <                                goto oopsy;
226 <                                                        /* reinsert */
227 <                        dp = div;
228 <                        j = ndivs < i ? ndivs : i;
224 >                        if (divsample(&dnew, &hemi, r) < 0) {
225 >                                dp++;
226 >                                continue;
227 >                        }
228 >                        dp = div;               /* reinsert */
229 >                        j = divcnt < i ? divcnt : i;
230                          while (--j > 0 && dnew.k < dp[1].k) {
231                                  *dp = *(dp+1);
232                                  dp++;
# Line 209 | Line 234 | doambient(                             /* compute ambient component */
234                          *dp = dnew;
235                  }
236                  if (pg != NULL || dg != NULL)   /* restore order */
237 <                        qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
237 >                        qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
238          }
239                                          /* compute returned values */
240          if (div != NULL) {
241 <                arad = 0.0;
242 <                for (i = ndivs, dp = div; i-- > 0; dp++) {
241 >                arad = 0.0;             /* note: divcnt may be < nt*np */
242 >                for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
243                          arad += dp->r;
244                          if (dp->n > 1) {
245                                  b = 1.0/dp->n;
# Line 226 | Line 251 | doambient(                             /* compute ambient component */
251                  }
252                  b = bright(acol);
253                  if (b > FTINY) {
254 <                        b = ndivs/b;
254 >                        b = 1.0/b;      /* compute & normalize gradient(s) */
255                          if (pg != NULL) {
256                                  posgradient(pg, div, &hemi);
257                                  for (i = 0; i < 3; i++)
# Line 237 | Line 262 | doambient(                             /* compute ambient component */
262                                  for (i = 0; i < 3; i++)
263                                          dg[i] *= b;
264                          }
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;
265                  }
266                  free((void *)div);
267          }
250        b = 1.0/ndivs;
251        scalecolor(acol, b);
268          if (arad <= FTINY)
269                  arad = maxarad;
270          else
271 <                arad = (ndivs+hemi.ns)/arad;
271 >                arad = (divcnt+hemi.ns)/arad;
272          if (pg != NULL) {               /* reduce radius if gradient large */
273                  d = DOT(pg,pg);
274                  if (d*arad*arad > 1.0)
# Line 269 | Line 285 | doambient(                             /* compute ambient component */
285          if ((arad /= sqrt(wt)) > maxarad)
286                  arad = maxarad;
287          return(arad);
272 oopsy:
273        if (div != NULL)
274                free((void *)div);
275        return(0.0);
288   }
289  
290  
# Line 382 | Line 394 | posgradient(                                   /* compute position gradient */
394                  yd += mag0*sinp + mag1*cosp;
395          }
396          for (i = 0; i < 3; i++)
397 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI;
397 >                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
398   }
399  
400  
# Line 417 | Line 429 | dirgradient(                                   /* compute direction gradient */
429                  yd += mag * tsin(phi);
430          }
431          for (i = 0; i < 3; i++)
432 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);
432 >                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
433   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines