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.5 by greg, Wed May 3 09:46:31 1995 UTC vs.
Revision 2.14 by greg, Tue Apr 19 01:15:06 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 <
25 < static int
32 < ambcmp(d1, d2)                          /* decreasing order */
33 < AMBSAMP  *d1, *d2;
19 > int
20 > inithemi(                       /* initialize sampling hemisphere */
21 >        register AMBHEMI  *hp,
22 >        RAY  *r,
23 >        COLOR ac,
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 >        int     ns;
28 >        double  d;
29 >        register int  i;
30 >                                        /* set number of divisions */
31 >        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
32 >        i = ambacc > FTINY ? 3 : 1;     /* minimum number of samples */
33 >        if (hp->nt < i)
34 >                hp->nt = i;
35 >        hp->np = PI * hp->nt + 0.5;
36 >                                        /* set number of super-samples */
37 >        ns = ambssamp * wt + 0.5;
38 >                                        /* assign coefficient */
39 >        d = 1.0/(hp->nt*hp->np + ns);   /* XXX weight not uniform if ns > 0 */
40 >        copycolor(hp->acoef, ac);
41 >        scalecolor(hp->acoef, d);
42 >                                        /* make axes */
43 >        VCOPY(hp->uz, r->ron);
44 >        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
45 >        for (i = 0; i < 3; i++)
46 >                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
47 >                        break;
48 >        if (i >= 3)
49 >                error(CONSISTENCY, "bad ray direction in inithemi");
50 >        hp->uy[i] = 1.0;
51 >        fcross(hp->ux, hp->uy, hp->uz);
52 >        normalize(hp->ux);
53 >        fcross(hp->uy, hp->uz, hp->ux);
54 >        return(ns);
55   }
56  
57  
58 < static int
59 < ambnorm(d1, d2)                         /* standard order */
60 < AMBSAMP  *d1, *d2;
58 > int
59 > divsample(                              /* sample a division */
60 >        register AMBSAMP  *dp,
61 >        AMBHEMI  *h,
62 >        RAY  *r
63 > )
64   {
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 {
65          RAY  ar;
66          int  hlist[3];
67          double  spt[2];
# Line 64 | Line 69 | RAY  *r;
69          double  b2;
70          double  phi;
71          register int  i;
72 <
73 <        if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0)
72 >                                        /* assign coefficient */
73 >        if (ambacc <= FTINY)            /* no storage, so report accurately */
74 >                copycolor(ar.rcoef, h->acoef);
75 >        else                            /* else lie for sake of cache */
76 >                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
77 >        if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0)
78                  return(-1);
79 +        copycolor(ar.rcoef, h->acoef);  /* correct coefficient rtrace output */
80          hlist[0] = r->rno;
81          hlist[1] = dp->t;
82          hlist[2] = dp->p;
83          multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n));
84          zd = sqrt((dp->t + spt[0])/h->nt);
85          phi = 2.0*PI * (dp->p + spt[1])/h->np;
86 <        xd = cos(phi) * zd;
87 <        yd = sin(phi) * zd;
86 >        xd = tcos(phi) * zd;
87 >        yd = tsin(phi) * zd;
88          zd = sqrt(1.0 - zd*zd);
89          for (i = 0; i < 3; i++)
90                  ar.rdir[i] =    xd*h->ux[i] +
# Line 84 | Line 94 | RAY  *r;
94          rayvalue(&ar);
95          ndims--;
96          addcolor(dp->v, ar.rcol);
97 +                                        /* use rt to improve gradient calc */
98          if (ar.rt > FTINY && ar.rt < FHUGE)
99                  dp->r += 1.0/ar.rt;
100                                          /* (re)initialize error */
# Line 97 | Line 108 | RAY  *r;
108   }
109  
110  
111 + static int
112 + ambcmp(                                 /* decreasing order */
113 +        const void *p1,
114 +        const void *p2
115 + )
116 + {
117 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
118 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
119 +
120 +        if (d1->k < d2->k)
121 +                return(1);
122 +        if (d1->k > d2->k)
123 +                return(-1);
124 +        return(0);
125 + }
126 +
127 +
128 + static int
129 + ambnorm(                                /* standard order */
130 +        const void *p1,
131 +        const void *p2
132 + )
133 + {
134 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
135 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
136 +        register int    c;
137 +
138 +        if ( (c = d1->t - d2->t) )
139 +                return(c);
140 +        return(d1->p - d2->p);
141 + }
142 +
143 +
144   double
145 < doambient(acol, r, wt, pg, dg)          /* compute ambient component */
146 < COLOR  acol;
147 < RAY  *r;
148 < double  wt;
149 < FVECT  pg, dg;
145 > doambient(                              /* compute ambient component */
146 >        COLOR  acol,
147 >        RAY  *r,
148 >        COLOR  ac,
149 >        double  wt,
150 >        FVECT  pg,
151 >        FVECT  dg
152 > )
153   {
154          double  b, d;
155          AMBHEMI  hemi;
# Line 115 | Line 162 | FVECT  pg, dg;
162                                          /* initialize color */
163          setcolor(acol, 0.0, 0.0, 0.0);
164                                          /* initialize hemisphere */
165 <        inithemi(&hemi, r, wt);
165 >        ns = inithemi(&hemi, r, ac, wt);
166          ndivs = hemi.nt * hemi.np;
167          if (ndivs == 0)
168                  return(0.0);
169 <                                        /* set number of super-samples */
123 <        ns = ambssamp * wt + 0.5;
169 >                                        /* allocate super-samples */
170          if (ns > 0 || pg != NULL || dg != NULL) {
171                  div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
172                  if (div == NULL)
# Line 139 | Line 185 | FVECT  pg, dg;
185                          dp->n = 0;
186                          if (divsample(dp, &hemi, r) < 0)
187                                  goto oopsy;
188 +                        arad += dp->r;
189                          if (div != NULL)
190                                  dp++;
191 <                        else {
191 >                        else
192                                  addcolor(acol, dp->v);
146                                arad += dp->r;
147                        }
193                  }
194          if (ns > 0 && arad > FTINY && ndivs/arad < minarad)
195                  ns = 0;                 /* close enough */
# Line 153 | Line 198 | FVECT  pg, dg;
198                  qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
199                                                  /* super-sample */
200                  for (i = ns; i > 0; i--) {
201 <                        copystruct(&dnew, div);
201 >                        dnew = *div;
202                          if (divsample(&dnew, &hemi, r) < 0)
203                                  goto oopsy;
204                                                          /* reinsert */
205                          dp = div;
206                          j = ndivs < i ? ndivs : i;
207                          while (--j > 0 && dnew.k < dp[1].k) {
208 <                                copystruct(dp, dp+1);
208 >                                *dp = *(dp+1);
209                                  dp++;
210                          }
211 <                        copystruct(dp, &dnew);
211 >                        *dp = dnew;
212                  }
213                  if (pg != NULL || dg != NULL)   /* restore order */
214                          qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
215          }
216                                          /* compute returned values */
217          if (div != NULL) {
218 +                arad = 0.0;
219                  for (i = ndivs, dp = div; i-- > 0; dp++) {
220                          arad += dp->r;
221                          if (dp->n > 1) {
# Line 201 | Line 247 | FVECT  pg, dg;
247                                  for (i = 0; i < 3; i++)
248                                          dg[i] = 0.0;
249                  }
250 <                free((char *)div);
250 >                free((void *)div);
251          }
252          b = 1.0/ndivs;
253          scalecolor(acol, b);
# Line 227 | Line 273 | FVECT  pg, dg;
273          return(arad);
274   oopsy:
275          if (div != NULL)
276 <                free((char *)div);
276 >                free((void *)div);
277          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          }
# Line 366 | Line 388 | register AMBHEMI  *hp;
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines