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.3 by greg, Wed Mar 4 16:28:25 1992 UTC vs.
Revision 2.17 by greg, Sat Jun 4 06:10:12 2005 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  
19 < typedef struct {
20 <        FVECT  ux, uy, uz;      /* x, y and z axis directions */
21 <        short  nt, np;          /* number of theta and phi directions */
22 < }  AMBHEMI;             /* ambient sample hemisphere */
23 <
24 < extern double  sin(), cos(), sqrt();
25 <
32 <
33 < static int
34 < ambcmp(d1, d2)                          /* decreasing order */
35 < AMBSAMP  *d1, *d2;
19 > void
20 > inithemi(                       /* initialize sampling hemisphere */
21 >        register AMBHEMI  *hp,
22 >        COLOR ac,
23 >        RAY  *r,
24 >        double  wt
25 > )
26   {
27 <        if (d1->k < d2->k)
28 <                return(1);
29 <        if (d1->k > d2->k)
30 <                return(-1);
31 <        return(0);
27 >        double  d;
28 >        register int  i;
29 >                                        /* set number of divisions */
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 coefficient */
41 >        copycolor(hp->acoef, ac);
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;
47 >        for (i = 0; i < 3; i++)
48 >                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
49 >                        break;
50 >        if (i >= 3)
51 >                error(CONSISTENCY, "bad ray direction in inithemi");
52 >        hp->uy[i] = 1.0;
53 >        fcross(hp->ux, hp->uy, hp->uz);
54 >        normalize(hp->ux);
55 >        fcross(hp->uy, hp->uz, hp->ux);
56   }
57  
58  
59 < static int
60 < ambnorm(d1, d2)                         /* standard order */
61 < AMBSAMP  *d1, *d2;
59 > int
60 > divsample(                              /* sample a division */
61 >        register AMBSAMP  *dp,
62 >        AMBHEMI  *h,
63 >        RAY  *r
64 > )
65   {
49        register int  c;
50
51        if (c = d1->t - d2->t)
52                return(c);
53        return(d1->p - d2->p);
54 }
55
56
57 divsample(dp, h, r)                     /* sample a division */
58 register AMBSAMP  *dp;
59 AMBHEMI  *h;
60 RAY  *r;
61 {
66          RAY  ar;
67          int  hlist[3];
68          double  spt[2];
# Line 66 | Line 70 | RAY  *r;
70          double  b2;
71          double  phi;
72          register int  i;
73 <
74 <        if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0)
73 >                                        /* ambient coefficient for weight */
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 +        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;
87          multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n));
88          zd = sqrt((dp->t + spt[0])/h->nt);
89          phi = 2.0*PI * (dp->p + spt[1])/h->np;
90 <        xd = cos(phi) * zd;
91 <        yd = sin(phi) * zd;
90 >        xd = tcos(phi) * zd;
91 >        yd = tsin(phi) * zd;
92          zd = sqrt(1.0 - zd*zd);
93          for (i = 0; i < 3; i++)
94                  ar.rdir[i] =    xd*h->ux[i] +
# Line 85 | Line 97 | RAY  *r;
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)
104                  dp->r += 1.0/ar.rt;
105                                          /* (re)initialize error */
# Line 99 | Line 113 | RAY  *r;
113   }
114  
115  
116 + static int
117 + ambcmp(                                 /* decreasing order */
118 +        const void *p1,
119 +        const void *p2
120 + )
121 + {
122 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
123 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
124 +
125 +        if (d1->k < d2->k)
126 +                return(1);
127 +        if (d1->k > d2->k)
128 +                return(-1);
129 +        return(0);
130 + }
131 +
132 +
133 + static int
134 + ambnorm(                                /* standard order */
135 +        const void *p1,
136 +        const void *p2
137 + )
138 + {
139 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
140 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
141 +        register int    c;
142 +
143 +        if ( (c = d1->t - d2->t) )
144 +                return(c);
145 +        return(d1->p - d2->p);
146 + }
147 +
148 +
149   double
150 < doambient(acol, r, wt, pg, dg)          /* compute ambient component */
151 < COLOR  acol;
152 < RAY  *r;
153 < double  wt;
154 < FVECT  pg, dg;
150 > doambient(                              /* compute ambient component */
151 >        COLOR  acol,
152 >        RAY  *r,
153 >        double  wt,
154 >        FVECT  pg,
155 >        FVECT  dg
156 > )
157   {
158          double  b, d;
159          AMBHEMI  hemi;
# Line 112 | Line 161 | FVECT  pg, dg;
161          AMBSAMP  dnew;
162          register AMBSAMP  *dp;
163          double  arad;
164 <        int  ndivs, ns;
164 >        int  ndivs;
165          register int  i, j;
117                                        /* initialize color */
118        setcolor(acol, 0.0, 0.0, 0.0);
166                                          /* initialize hemisphere */
167 <        inithemi(&hemi, r, wt);
167 >        inithemi(&hemi, acol, r, wt);
168          ndivs = 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)
176                  return(0.0);
177 <                                        /* set number of super-samples */
178 <        ns = ambssamp * wt + 0.5;
126 <        if (ns > 0 || pg != NULL || dg != NULL) {
177 >                                        /* allocate super-samples */
178 >        if (hemi.ns > 0 || pg != NULL || dg != NULL) {
179                  div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
180                  if (div == NULL)
181                          error(SYSTEM, "out of memory in doambient");
# Line 139 | Line 191 | FVECT  pg, dg;
191                          setcolor(dp->v, 0.0, 0.0, 0.0);
192                          dp->r = 0.0;
193                          dp->n = 0;
194 <                        if (divsample(dp, &hemi, r) < 0)
195 <                                goto oopsy;
194 >                        if (divsample(dp, &hemi, r) < 0) {
195 >                                if (div != NULL) dp++;
196 >                                hemi.ns = 0;    /* incomplete sampling */
197 >                                pg = dg = NULL;
198 >                                continue;
199 >                        }
200 >                        arad += dp->r;
201                          if (div != NULL)
202                                  dp++;
203 <                        else {
203 >                        else
204                                  addcolor(acol, dp->v);
148                                arad += dp->r;
149                        }
205                  }
206 <        if (ns > 0) {                   /* perform super-sampling */
206 >        if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad)
207 >                hemi.ns = 0;            /* close enough */
208 >        else if (hemi.ns > 0) {         /* else perform super-sampling */
209                  comperrs(div, &hemi);                   /* compute errors */
210                  qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
211                                                  /* super-sample */
212 <                for (i = ns; i > 0; i--) {
213 <                        copystruct(&dnew, div);
214 <                        if (divsample(&dnew, &hemi, r) < 0)
215 <                                goto oopsy;
216 <                                                        /* reinsert */
217 <                        dp = div;
212 >                for (i = hemi.ns; i > 0; i--) {
213 >                        dnew = *div;
214 >                        if (divsample(&dnew, &hemi, r) < 0) {
215 >                                dp++;
216 >                                continue;
217 >                        }
218 >                        dp = div;               /* reinsert */
219                          j = ndivs < i ? ndivs : i;
220                          while (--j > 0 && dnew.k < dp[1].k) {
221 <                                copystruct(dp, dp+1);
221 >                                *dp = *(dp+1);
222                                  dp++;
223                          }
224 <                        copystruct(dp, &dnew);
224 >                        *dp = dnew;
225                  }
226                  if (pg != NULL || dg != NULL)   /* restore order */
227                          qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
228          }
229                                          /* compute returned values */
230          if (div != NULL) {
231 +                arad = 0.0;
232                  for (i = ndivs, dp = div; i-- > 0; dp++) {
233                          arad += dp->r;
234                          if (dp->n > 1) {
# Line 182 | Line 241 | FVECT  pg, dg;
241                  }
242                  b = bright(acol);
243                  if (b > FTINY) {
244 <                        b = ndivs/b;
244 >                        b = 1.0/b;      /* compute & normalize gradient(s) */
245                          if (pg != NULL) {
246                                  posgradient(pg, div, &hemi);
247                                  for (i = 0; i < 3; i++)
# Line 193 | Line 252 | FVECT  pg, dg;
252                                  for (i = 0; i < 3; i++)
253                                          dg[i] *= b;
254                          }
196                } else {
197                        if (pg != NULL)
198                                for (i = 0; i < 3; i++)
199                                        pg[i] = 0.0;
200                        if (dg != NULL)
201                                for (i = 0; i < 3; i++)
202                                        dg[i] = 0.0;
255                  }
256 <                free((char *)div);
256 >                free((void *)div);
257          }
206        b = 1.0/ndivs;
207        scalecolor(acol, b);
258          if (arad <= FTINY)
259                  arad = maxarad;
260          else
261 <                arad = (ndivs+ns)/arad;
261 >                arad = (ndivs+hemi.ns)/arad;
262          if (pg != NULL) {               /* reduce radius if gradient large */
263                  d = DOT(pg,pg);
264                  if (d*arad*arad > 1.0)
# Line 225 | Line 275 | FVECT  pg, dg;
275          if ((arad /= sqrt(wt)) > maxarad)
276                  arad = maxarad;
277          return(arad);
228 oopsy:
229        if (div != NULL)
230                free((char *)div);
231        return(0.0);
278   }
279  
280  
281 < inithemi(hp, r, wt)             /* initialize sampling hemisphere */
282 < register AMBHEMI  *hp;
283 < RAY  *r;
284 < double  wt;
281 > void
282 > comperrs(                       /* compute initial error estimates */
283 >        AMBSAMP  *da,   /* assumes standard ordering */
284 >        register AMBHEMI  *hp
285 > )
286   {
240        register int  i;
241                                        /* set number of divisions */
242        if (wt < (.25*PI)/ambdiv+FTINY) {
243                hp->nt = hp->np = 0;
244                return;                 /* zero samples */
245        }
246        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
247        hp->np = PI * hp->nt + 0.5;
248                                        /* make axes */
249        VCOPY(hp->uz, r->ron);
250        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
251        for (i = 0; i < 3; i++)
252                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
253                        break;
254        if (i >= 3)
255                error(CONSISTENCY, "bad ray direction in inithemi");
256        hp->uy[i] = 1.0;
257        fcross(hp->ux, hp->uy, hp->uz);
258        normalize(hp->ux);
259        fcross(hp->uy, hp->uz, hp->ux);
260 }
261
262
263 comperrs(da, hp)                /* compute initial error estimates */
264 AMBSAMP  *da;           /* assumes standard ordering */
265 register AMBHEMI  *hp;
266 {
287          double  b, b2;
288          int  i, j;
289          register AMBSAMP  *dp;
# Line 310 | Line 330 | register AMBHEMI  *hp;
330   }
331  
332  
333 < posgradient(gv, da, hp)                         /* compute position gradient */
334 < FVECT  gv;
335 < AMBSAMP  *da;                   /* assumes standard ordering */
336 < register AMBHEMI  *hp;
333 > void
334 > posgradient(                                    /* compute position gradient */
335 >        FVECT  gv,
336 >        AMBSAMP  *da,                   /* assumes standard ordering */
337 >        register AMBHEMI  *hp
338 > )
339   {
340          register int  i, j;
341          double  nextsine, lastsine, b, d;
# Line 357 | Line 379 | register AMBHEMI  *hp;
379                  }
380                  mag0 *= 2.0*PI / hp->np;
381                  phi = 2.0*PI * (double)j/hp->np;
382 <                cosp = cos(phi); sinp = sin(phi);
382 >                cosp = tcos(phi); sinp = tsin(phi);
383                  xd += mag0*cosp - mag1*sinp;
384                  yd += mag0*sinp + mag1*cosp;
385          }
386          for (i = 0; i < 3; i++)
387 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI;
387 >                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
388   }
389  
390  
391 < dirgradient(gv, da, hp)                         /* compute direction gradient */
392 < FVECT  gv;
393 < AMBSAMP  *da;                   /* assumes standard ordering */
394 < register AMBHEMI  *hp;
391 > void
392 > dirgradient(                                    /* compute direction gradient */
393 >        FVECT  gv,
394 >        AMBSAMP  *da,                   /* assumes standard ordering */
395 >        register AMBHEMI  *hp
396 > )
397   {
398          register int  i, j;
399          double  mag;
# Line 391 | Line 415 | register AMBHEMI  *hp;
415                          dp += hp->np;
416                  }
417                  phi = 2.0*PI * (j+.5)/hp->np + PI/2.0;
418 <                xd += mag * cos(phi);
419 <                yd += mag * sin(phi);
418 >                xd += mag * tcos(phi);
419 >                yd += mag * tsin(phi);
420          }
421          for (i = 0; i < 3; i++)
422 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);
422 >                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
423   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines