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.18 by greg, Mon Jun 6 19:14:28 2005 UTC vs.
Revision 2.21 by greg, Mon Dec 31 18:19:42 2007 UTC

# Line 28 | Line 28 | inithemi(                      /* initialize sampling hemisphere */
28          register int  i;
29                                          /* set number of divisions */
30          if (ambacc <= FTINY &&
31 <                        wt > (d = 0.8*bright(ac)*r->rweight/(ambdiv*minweight)))
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 */
# Line 161 | 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;
166                                          /* initialize hemisphere */
167          inithemi(&hemi, acol, r, wt);
168 <        ndivs = hemi.nt * hemi.np;
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 (ndivs == 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 185 | 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;
# Line 192 | Line 193 | doambient(                             /* compute ambient component */
193                          dp->r = 0.0;
194                          dp->n = 0;
195                          if (divsample(dp, &hemi, r) < 0) {
196 <                                if (div == NULL) continue;
197 <                                dp++;
197 <                                hemi.ns = 0;    /* incomplete sampling */
198 <                                pg = dg = NULL;
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 >                if (div != NULL)
209 >                        free((void *)div);
210 >                return(0.0);            /* no samples taken */
211 >        }
212 >        if (divcnt < hemi.nt*hemi.np) {
213 >                pg = dg = NULL;         /* incomplete sampling */
214 >                hemi.ns = 0;
215 >        } else if (arad > FTINY && divcnt/arad < minarad) {
216                  hemi.ns = 0;            /* close enough */
217 <        else if (hemi.ns > 0) {         /* else perform super-sampling */
217 >        } else if (hemi.ns > 0) {       /* else perform super-sampling? */
218                  comperrs(div, &hemi);                   /* compute errors */
219 <                qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
219 >                qsort(div, divcnt, sizeof(AMBSAMP), ambcmp);    /* sort divs */
220                                                  /* super-sample */
221                  for (i = hemi.ns; i > 0; i--) {
222                          dnew = *div;
# Line 217 | Line 225 | doambient(                             /* compute ambient component */
225                                  continue;
226                          }
227                          dp = div;               /* reinsert */
228 <                        j = ndivs < i ? ndivs : i;
228 >                        j = divcnt < i ? divcnt : i;
229                          while (--j > 0 && dnew.k < dp[1].k) {
230                                  *dp = *(dp+1);
231                                  dp++;
# Line 225 | Line 233 | doambient(                             /* compute ambient component */
233                          *dp = dnew;
234                  }
235                  if (pg != NULL || dg != NULL)   /* restore order */
236 <                        qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
236 >                        qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
237          }
238                                          /* compute returned values */
239          if (div != NULL) {
240 <                arad = 0.0;
241 <                for (i = ndivs, dp = div; i-- > 0; dp++) {
240 >                arad = 0.0;             /* note: divcnt may be < nt*np */
241 >                for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
242                          arad += dp->r;
243                          if (dp->n > 1) {
244                                  b = 1.0/dp->n;
# Line 259 | Line 267 | doambient(                             /* compute ambient component */
267          if (arad <= FTINY)
268                  arad = maxarad;
269          else
270 <                arad = (ndivs+hemi.ns)/arad;
270 >                arad = (divcnt+hemi.ns)/arad;
271          if (pg != NULL) {               /* reduce radius if gradient large */
272                  d = DOT(pg,pg);
273                  if (d*arad*arad > 1.0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines