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.23 by greg, Tue Feb 5 05:40:06 2013 UTC

# Line 18 | Line 18 | static const char      RCSid[] = "$Id$";
18  
19   void
20   inithemi(                       /* initialize sampling hemisphere */
21 <        register AMBHEMI  *hp,
21 >        AMBHEMI  *hp,
22 >        COLOR ac,
23          RAY  *r,
23        COLOR ac,
24          double  wt
25   )
26   {
27 <        register int  i;
27 >        double  d;
28 >        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 56 | Line 58 | inithemi(                      /* initialize sampling hemisphere */
58  
59   int
60   divsample(                              /* sample a division */
61 <        register AMBSAMP  *dp,
61 >        AMBSAMP  *dp,
62          AMBHEMI  *h,
63          RAY  *r
64   )
# Line 67 | Line 69 | divsample(                             /* sample a division */
69          double  xd, yd, zd;
70          double  b2;
71          double  phi;
72 <        register int  i;
72 >        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 131 | Line 139 | ambnorm(                               /* standard order */
139   {
140          const AMBSAMP   *d1 = (const AMBSAMP *)p1;
141          const AMBSAMP   *d2 = (const AMBSAMP *)p2;
142 <        register int    c;
142 >        int     c;
143  
144          if ( (c = d1->t - d2->t) )
145                  return(c);
# Line 141 | Line 149 | ambnorm(                               /* standard order */
149  
150   double
151   doambient(                              /* compute ambient component */
152 <        COLOR  acol,
152 >        COLOR  rcol,
153          RAY  *r,
146        COLOR  ac,
154          double  wt,
155          FVECT  pg,
156          FVECT  dg
# Line 153 | Line 160 | doambient(                             /* compute ambient component */
160          AMBHEMI  hemi;
161          AMBSAMP  *div;
162          AMBSAMP  dnew;
163 <        register AMBSAMP  *dp;
163 >        double  acol[3];
164 >        AMBSAMP  *dp;
165          double  arad;
166 <        int  ndivs;
167 <        register int  i, j;
160 <                                        /* initialize color */
161 <        setcolor(acol, 0.0, 0.0, 0.0);
166 >        int  divcnt;
167 >        int  i, j;
168                                          /* initialize hemisphere */
169 <        inithemi(&hemi, r, ac, wt);
170 <        ndivs = hemi.nt * hemi.np;
171 <        if (ndivs == 0)
169 >        inithemi(&hemi, rcol, r, wt);
170 >        divcnt = hemi.nt * hemi.np;
171 >                                        /* initialize */
172 >        if (pg != NULL)
173 >                pg[0] = pg[1] = pg[2] = 0.0;
174 >        if (dg != NULL)
175 >                dg[0] = dg[1] = dg[2] = 0.0;
176 >        setcolor(rcol, 0.0, 0.0, 0.0);
177 >        if (divcnt == 0)
178                  return(0.0);
179                                          /* allocate super-samples */
180          if (hemi.ns > 0 || pg != NULL || dg != NULL) {
181 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
181 >                div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
182                  if (div == NULL)
183                          error(SYSTEM, "out of memory in doambient");
184          } else
185                  div = NULL;
186                                          /* sample the divisions */
187          arad = 0.0;
188 +        acol[0] = acol[1] = acol[2] = 0.0;
189          if ((dp = div) == NULL)
190                  dp = &dnew;
191 +        divcnt = 0;
192          for (i = 0; i < hemi.nt; i++)
193                  for (j = 0; j < hemi.np; j++) {
194                          dp->t = i; dp->p = j;
195                          setcolor(dp->v, 0.0, 0.0, 0.0);
196                          dp->r = 0.0;
197                          dp->n = 0;
198 <                        if (divsample(dp, &hemi, r) < 0)
199 <                                goto oopsy;
198 >                        if (divsample(dp, &hemi, r) < 0) {
199 >                                if (div != NULL)
200 >                                        dp++;
201 >                                continue;
202 >                        }
203                          arad += dp->r;
204 +                        divcnt++;
205                          if (div != NULL)
206                                  dp++;
207                          else
208                                  addcolor(acol, dp->v);
209                  }
210 <        if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad)
210 >        if (!divcnt) {
211 >                if (div != NULL)
212 >                        free((void *)div);
213 >                return(0.0);            /* no samples taken */
214 >        }
215 >        if (divcnt < hemi.nt*hemi.np) {
216 >                pg = dg = NULL;         /* incomplete sampling */
217 >                hemi.ns = 0;
218 >        } else if (arad > FTINY && divcnt/arad < minarad) {
219                  hemi.ns = 0;            /* close enough */
220 <        else if (hemi.ns > 0) {         /* else perform super-sampling */
220 >        } else if (hemi.ns > 0) {       /* else perform super-sampling? */
221                  comperrs(div, &hemi);                   /* compute errors */
222 <                qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
222 >                qsort(div, divcnt, sizeof(AMBSAMP), ambcmp);    /* sort divs */
223                                                  /* super-sample */
224                  for (i = hemi.ns; i > 0; i--) {
225                          dnew = *div;
226 <                        if (divsample(&dnew, &hemi, r) < 0)
227 <                                goto oopsy;
228 <                                                        /* reinsert */
229 <                        dp = div;
230 <                        j = ndivs < i ? ndivs : i;
226 >                        if (divsample(&dnew, &hemi, r) < 0) {
227 >                                dp++;
228 >                                continue;
229 >                        }
230 >                        dp = div;               /* reinsert */
231 >                        j = divcnt < i ? divcnt : i;
232                          while (--j > 0 && dnew.k < dp[1].k) {
233                                  *dp = *(dp+1);
234                                  dp++;
# Line 209 | Line 236 | doambient(                             /* compute ambient component */
236                          *dp = dnew;
237                  }
238                  if (pg != NULL || dg != NULL)   /* restore order */
239 <                        qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
239 >                        qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
240          }
241                                          /* compute returned values */
242          if (div != NULL) {
243 <                arad = 0.0;
244 <                for (i = ndivs, dp = div; i-- > 0; dp++) {
243 >                arad = 0.0;             /* note: divcnt may be < nt*np */
244 >                for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
245                          arad += dp->r;
246                          if (dp->n > 1) {
247                                  b = 1.0/dp->n;
# Line 226 | Line 253 | doambient(                             /* compute ambient component */
253                  }
254                  b = bright(acol);
255                  if (b > FTINY) {
256 <                        b = ndivs/b;
256 >                        b = 1.0/b;      /* compute & normalize gradient(s) */
257                          if (pg != NULL) {
258                                  posgradient(pg, div, &hemi);
259                                  for (i = 0; i < 3; i++)
# Line 237 | Line 264 | doambient(                             /* compute ambient component */
264                                  for (i = 0; i < 3; i++)
265                                          dg[i] *= b;
266                          }
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;
267                  }
268                  free((void *)div);
269          }
270 <        b = 1.0/ndivs;
251 <        scalecolor(acol, b);
270 >        copycolor(rcol, acol);
271          if (arad <= FTINY)
272                  arad = maxarad;
273          else
274 <                arad = (ndivs+hemi.ns)/arad;
274 >                arad = (divcnt+hemi.ns)/arad;
275          if (pg != NULL) {               /* reduce radius if gradient large */
276                  d = DOT(pg,pg);
277                  if (d*arad*arad > 1.0)
# Line 269 | Line 288 | doambient(                             /* compute ambient component */
288          if ((arad /= sqrt(wt)) > maxarad)
289                  arad = maxarad;
290          return(arad);
272 oopsy:
273        if (div != NULL)
274                free((void *)div);
275        return(0.0);
291   }
292  
293  
294   void
295   comperrs(                       /* compute initial error estimates */
296          AMBSAMP  *da,   /* assumes standard ordering */
297 <        register AMBHEMI  *hp
297 >        AMBHEMI  *hp
298   )
299   {
300          double  b, b2;
301          int  i, j;
302 <        register AMBSAMP  *dp;
302 >        AMBSAMP  *dp;
303                                  /* sum differences from neighbors */
304          dp = da;
305          for (i = 0; i < hp->nt; i++)
# Line 332 | Line 347 | void
347   posgradient(                                    /* compute position gradient */
348          FVECT  gv,
349          AMBSAMP  *da,                   /* assumes standard ordering */
350 <        register AMBHEMI  *hp
350 >        AMBHEMI  *hp
351   )
352   {
353 <        register int  i, j;
353 >        int  i, j;
354          double  nextsine, lastsine, b, d;
355          double  mag0, mag1;
356          double  phi, cosp, sinp, xd, yd;
357 <        register AMBSAMP  *dp;
357 >        AMBSAMP  *dp;
358  
359          xd = yd = 0.0;
360          for (j = 0; j < hp->np; j++) {
# Line 382 | Line 397 | posgradient(                                   /* compute position gradient */
397                  yd += mag0*sinp + mag1*cosp;
398          }
399          for (i = 0; i < 3; i++)
400 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI;
400 >                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
401   }
402  
403  
# Line 390 | Line 405 | void
405   dirgradient(                                    /* compute direction gradient */
406          FVECT  gv,
407          AMBSAMP  *da,                   /* assumes standard ordering */
408 <        register AMBHEMI  *hp
408 >        AMBHEMI  *hp
409   )
410   {
411 <        register int  i, j;
411 >        int  i, j;
412          double  mag;
413          double  phi, xd, yd;
414 <        register AMBSAMP  *dp;
414 >        AMBSAMP  *dp;
415  
416          xd = yd = 0.0;
417          for (j = 0; j < hp->np; j++) {
# Line 417 | Line 432 | dirgradient(                                   /* compute direction gradient */
432                  yd += mag * tsin(phi);
433          }
434          for (i = 0; i < 3; i++)
435 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);
435 >                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
436   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines