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.6 by greg, Tue Apr 23 11:06:54 1996 UTC vs.
Revision 2.22 by greg, Sun Sep 26 15:51:15 2010 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 <
25 < static int
32 < ambcmp(d1, d2)                          /* decreasing order */
33 < 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*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 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   {
47        register int  c;
48
49        if (c = d1->t - d2->t)
50                return(c);
51        return(d1->p - d2->p);
52 }
53
54
55 divsample(dp, h, r)                     /* sample a division */
56 register AMBSAMP  *dp;
57 AMBHEMI  *h;
58 RAY  *r;
59 {
66          RAY  ar;
67          int  hlist[3];
68          double  spt[2];
# Line 64 | 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] +
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)
105                  dp->r += 1.0/ar.rt;
106                                          /* (re)initialize error */
# Line 97 | Line 114 | RAY  *r;
114   }
115  
116  
117 + static int
118 + ambcmp(                                 /* decreasing order */
119 +        const void *p1,
120 +        const void *p2
121 + )
122 + {
123 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
124 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
125 +
126 +        if (d1->k < d2->k)
127 +                return(1);
128 +        if (d1->k > d2->k)
129 +                return(-1);
130 +        return(0);
131 + }
132 +
133 +
134 + static int
135 + ambnorm(                                /* standard order */
136 +        const void *p1,
137 +        const void *p2
138 + )
139 + {
140 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
141 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
142 +        register int    c;
143 +
144 +        if ( (c = d1->t - d2->t) )
145 +                return(c);
146 +        return(d1->p - d2->p);
147 + }
148 +
149 +
150   double
151 < doambient(acol, r, wt, pg, dg)          /* compute ambient component */
152 < COLOR  acol;
153 < RAY  *r;
154 < double  wt;
155 < FVECT  pg, dg;
151 > doambient(                              /* compute ambient component */
152 >        COLOR  acol,
153 >        RAY  *r,
154 >        double  wt,
155 >        FVECT  pg,
156 >        FVECT  dg
157 > )
158   {
159          double  b, d;
160          AMBHEMI  hemi;
# Line 110 | Line 162 | FVECT  pg, dg;
162          AMBSAMP  dnew;
163          register AMBSAMP  *dp;
164          double  arad;
165 <        int  ndivs, ns;
165 >        int  divcnt;
166          register int  i, j;
115                                        /* initialize color */
116        setcolor(acol, 0.0, 0.0, 0.0);
167                                          /* initialize hemisphere */
168 <        inithemi(&hemi, r, 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 <                                        /* set number of super-samples */
179 <        ns = ambssamp * wt + 0.5;
180 <        if (ns > 0 || pg != NULL || dg != NULL) {
125 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
178 >                                        /* allocate super-samples */
179 >        if (hemi.ns > 0 || pg != NULL || dg != NULL) {
180 >                div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
181                  if (div == NULL)
182                          error(SYSTEM, "out of memory in doambient");
183          } else
# Line 131 | Line 186 | FVECT  pg, dg;
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 (ns > 0 && arad > FTINY && ndivs/arad < minarad)
209 <                ns = 0;                 /* close enough */
210 <        else if (ns > 0) {              /* else perform super-sampling */
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? */
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 = ns; i > 0; i--) {
223 <                        copystruct(&dnew, div);
224 <                        if (divsample(&dnew, &hemi, r) < 0)
225 <                                goto oopsy;
226 <                                                        /* reinsert */
227 <                        dp = div;
228 <                        j = ndivs < i ? ndivs : i;
222 >                for (i = hemi.ns; i > 0; i--) {
223 >                        dnew = *div;
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 <                                copystruct(dp, dp+1);
231 >                                *dp = *(dp+1);
232                                  dp++;
233                          }
234 <                        copystruct(dp, &dnew);
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 182 | Line 251 | FVECT  pg, dg;
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 193 | Line 262 | FVECT  pg, dg;
262                                  for (i = 0; i < 3; i++)
263                                          dg[i] *= b;
264                          }
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;
265                  }
266 <                free((char *)div);
266 >                free((void *)div);
267          }
206        b = 1.0/ndivs;
207        scalecolor(acol, b);
268          if (arad <= FTINY)
269                  arad = maxarad;
270          else
271 <                arad = (ndivs+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 225 | Line 285 | FVECT  pg, dg;
285          if ((arad /= sqrt(wt)) > maxarad)
286                  arad = maxarad;
287          return(arad);
228 oopsy:
229        if (div != NULL)
230                free((char *)div);
231        return(0.0);
288   }
289  
290  
291 < inithemi(hp, r, wt)             /* initialize sampling hemisphere */
292 < register AMBHEMI  *hp;
293 < RAY  *r;
294 < double  wt;
291 > void
292 > comperrs(                       /* compute initial error estimates */
293 >        AMBSAMP  *da,   /* assumes standard ordering */
294 >        register AMBHEMI  *hp
295 > )
296   {
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 {
297          double  b, b2;
298          int  i, j;
299          register AMBSAMP  *dp;
# Line 310 | Line 340 | register AMBHEMI  *hp;
340   }
341  
342  
343 < posgradient(gv, da, hp)                         /* compute position gradient */
344 < FVECT  gv;
345 < AMBSAMP  *da;                   /* assumes standard ordering */
346 < register AMBHEMI  *hp;
343 > void
344 > posgradient(                                    /* compute position gradient */
345 >        FVECT  gv,
346 >        AMBSAMP  *da,                   /* assumes standard ordering */
347 >        register AMBHEMI  *hp
348 > )
349   {
350          register int  i, j;
351          double  nextsine, lastsine, b, d;
# Line 357 | Line 389 | register AMBHEMI  *hp;
389                  }
390                  mag0 *= 2.0*PI / hp->np;
391                  phi = 2.0*PI * (double)j/hp->np;
392 <                cosp = cos(phi); sinp = sin(phi);
392 >                cosp = tcos(phi); sinp = tsin(phi);
393                  xd += mag0*cosp - mag1*sinp;
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  
401 < dirgradient(gv, da, hp)                         /* compute direction gradient */
402 < FVECT  gv;
403 < AMBSAMP  *da;                   /* assumes standard ordering */
404 < register AMBHEMI  *hp;
401 > void
402 > dirgradient(                                    /* compute direction gradient */
403 >        FVECT  gv,
404 >        AMBSAMP  *da,                   /* assumes standard ordering */
405 >        register AMBHEMI  *hp
406 > )
407   {
408          register int  i, j;
409          double  mag;
# Line 391 | Line 425 | register AMBHEMI  *hp;
425                          dp += hp->np;
426                  }
427                  phi = 2.0*PI * (j+.5)/hp->np + PI/2.0;
428 <                xd += mag * cos(phi);
429 <                yd += mag * sin(phi);
428 >                xd += mag * tcos(phi);
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