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.2 by greg, Wed Feb 19 12:00:04 1992 UTC vs.
Revision 2.10 by greg, Tue Feb 25 02:47:22 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines to compute "ambient" values using Monte Carlo
6 + *
7 + *  Declarations of external symbols in ambient.h
8   */
9  
10 + #include "copyright.h"
11 +
12   #include  "ray.h"
13  
14   #include  "ambient.h"
15  
16   #include  "random.h"
17  
17 typedef struct {
18        short  t, p;            /* theta, phi indices */
19        COLOR  v;               /* value sum */
20        float  r;               /* 1/distance sum */
21        float  k;               /* variance for this division */
22        int  n;                 /* number of subsamples */
23 }  AMBSAMP;             /* ambient sample division */
18  
25 typedef struct {
26        FVECT  ux, uy, uz;      /* x, y and z axis directions */
27        short  nt, np;          /* number of theta and phi directions */
28 }  AMBHEMI;             /* ambient sample hemisphere */
29
30 extern double  sin(), cos(), sqrt();
31
32
19   static int
20   ambcmp(d1, d2)                          /* decreasing order */
21   AMBSAMP  *d1, *d2;
# Line 54 | Line 40 | AMBSAMP  *d1, *d2;
40   }
41  
42  
43 + int
44   divsample(dp, h, r)                     /* sample a division */
45   register AMBSAMP  *dp;
46   AMBHEMI  *h;
# Line 75 | Line 62 | RAY  *r;
62          multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n));
63          zd = sqrt((dp->t + spt[0])/h->nt);
64          phi = 2.0*PI * (dp->p + spt[1])/h->np;
65 <        xd = cos(phi) * zd;
66 <        yd = sin(phi) * zd;
65 >        xd = tcos(phi) * zd;
66 >        yd = tsin(phi) * zd;
67          zd = sqrt(1.0 - zd*zd);
68          for (i = 0; i < 3; i++)
69                  ar.rdir[i] =    xd*h->ux[i] +
# Line 86 | Line 73 | RAY  *r;
73          rayvalue(&ar);
74          ndims--;
75          addcolor(dp->v, ar.rcol);
76 +                                        /* use rt to improve gradient calc */
77          if (ar.rt > FTINY && ar.rt < FHUGE)
78                  dp->r += 1.0/ar.rt;
79                                          /* (re)initialize error */
# Line 141 | Line 129 | FVECT  pg, dg;
129                          dp->n = 0;
130                          if (divsample(dp, &hemi, r) < 0)
131                                  goto oopsy;
132 +                        arad += dp->r;
133                          if (div != NULL)
134                                  dp++;
135 <                        else {
135 >                        else
136                                  addcolor(acol, dp->v);
148                                arad += dp->r;
149                        }
137                  }
138 <        if (ns > 0) {                   /* perform super-sampling */
138 >        if (ns > 0 && arad > FTINY && ndivs/arad < minarad)
139 >                ns = 0;                 /* close enough */
140 >        else if (ns > 0) {              /* else perform super-sampling */
141                  comperrs(div, &hemi);                   /* compute errors */
142                  qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
143                                                  /* super-sample */
# Line 170 | Line 159 | FVECT  pg, dg;
159          }
160                                          /* compute returned values */
161          if (div != NULL) {
162 +                arad = 0.0;
163                  for (i = ndivs, dp = div; i-- > 0; dp++) {
164                          arad += dp->r;
165                          if (dp->n > 1) {
# Line 201 | Line 191 | FVECT  pg, dg;
191                                  for (i = 0; i < 3; i++)
192                                          dg[i] = 0.0;
193                  }
194 <                free((char *)div);
194 >                free((void *)div);
195          }
196          b = 1.0/ndivs;
197          scalecolor(acol, b);
198          if (arad <= FTINY)
199                  arad = maxarad;
200 <        else {
200 >        else
201                  arad = (ndivs+ns)/arad;
212                if (arad > maxarad)
213                        arad = maxarad;
214        }
202          if (pg != NULL) {               /* reduce radius if gradient large */
203                  d = DOT(pg,pg);
204                  if (d*arad*arad > 1.0)
# Line 225 | Line 212 | FVECT  pg, dg;
212                                  pg[i] *= d;
213                  }
214          }
215 <        return(arad/sqrt(wt));
215 >        if ((arad /= sqrt(wt)) > maxarad)
216 >                arad = maxarad;
217 >        return(arad);
218   oopsy:
219          if (div != NULL)
220 <                free((char *)div);
220 >                free((void *)div);
221          return(0.0);
222   }
223  
224  
225 + void
226   inithemi(hp, r, wt)             /* initialize sampling hemisphere */
227   register AMBHEMI  *hp;
228   RAY  *r;
# Line 261 | Line 251 | double  wt;
251   }
252  
253  
254 + void
255   comperrs(da, hp)                /* compute initial error estimates */
256   AMBSAMP  *da;           /* assumes standard ordering */
257   register AMBHEMI  *hp;
# Line 311 | Line 302 | register AMBHEMI  *hp;
302   }
303  
304  
305 + void
306   posgradient(gv, da, hp)                         /* compute position gradient */
307   FVECT  gv;
308   AMBSAMP  *da;                   /* assumes standard ordering */
# Line 358 | Line 350 | register AMBHEMI  *hp;
350                  }
351                  mag0 *= 2.0*PI / hp->np;
352                  phi = 2.0*PI * (double)j/hp->np;
353 <                cosp = cos(phi); sinp = sin(phi);
353 >                cosp = tcos(phi); sinp = tsin(phi);
354                  xd += mag0*cosp - mag1*sinp;
355                  yd += mag0*sinp + mag1*cosp;
356          }
# Line 367 | Line 359 | register AMBHEMI  *hp;
359   }
360  
361  
362 + void
363   dirgradient(gv, da, hp)                         /* compute direction gradient */
364   FVECT  gv;
365   AMBSAMP  *da;                   /* assumes standard ordering */
# Line 392 | Line 385 | register AMBHEMI  *hp;
385                          dp += hp->np;
386                  }
387                  phi = 2.0*PI * (j+.5)/hp->np + PI/2.0;
388 <                xd += mag * cos(phi);
389 <                yd += mag * sin(phi);
388 >                xd += mag * tcos(phi);
389 >                yd += mag * tsin(phi);
390          }
391          for (i = 0; i < 3; i++)
392                  gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines