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.8 by gwlarson, Wed Dec 16 18:14:57 1998 UTC vs.
Revision 2.14 by greg, Tue Apr 19 01:15:06 2005 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
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;
# Line 84 | Line 94 | RAY  *r;
94          rayvalue(&ar);
95          ndims--;
96          addcolor(dp->v, ar.rcol);
97 <                                        /* be conservative and use rot */
98 <        if (ar.rot > FTINY && ar.rot < FHUGE)
99 <                dp->r += 1.0/ar.rot;
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 */
101          if (dp->n++) {
102                  b2 = bright(dp->v)/dp->n - bright(ar.rcol);
# Line 98 | 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 116 | 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 */
124 <        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 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);
# Line 202 | 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 228 | 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   {
241        register int  i;
242                                        /* set number of divisions */
243        if (wt < (.25*PI)/ambdiv+FTINY) {
244                hp->nt = hp->np = 0;
245                return;                 /* zero samples */
246        }
247        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
248        hp->np = PI * hp->nt + 0.5;
249                                        /* make axes */
250        VCOPY(hp->uz, r->ron);
251        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
252        for (i = 0; i < 3; i++)
253                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
254                        break;
255        if (i >= 3)
256                error(CONSISTENCY, "bad ray direction in inithemi");
257        hp->uy[i] = 1.0;
258        fcross(hp->ux, hp->uy, hp->uz);
259        normalize(hp->ux);
260        fcross(hp->uy, hp->uz, hp->ux);
261 }
262
263
264 comperrs(da, hp)                /* compute initial error estimates */
265 AMBSAMP  *da;           /* assumes standard ordering */
266 register AMBHEMI  *hp;
267 {
287          double  b, b2;
288          int  i, j;
289          register AMBSAMP  *dp;
# Line 311 | 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 367 | 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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines