ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambcomp.c
Revision: 1.6
Committed: Thu Jun 13 10:54:00 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +23 -12 lines
Log Message:
made computations of gradients a little more robust

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Routines to compute "ambient" values using Monte Carlo
9 */
10
11 #include "ray.h"
12
13 #include "ambient.h"
14
15 #include "random.h"
16
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 */
24
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
33 static int
34 ambcmp(d1, d2) /* decreasing order */
35 AMBSAMP *d1, *d2;
36 {
37 if (d1->k < d2->k)
38 return(1);
39 if (d1->k > d2->k)
40 return(-1);
41 return(0);
42 }
43
44
45 static int
46 ambnorm(d1, d2) /* standard order */
47 AMBSAMP *d1, *d2;
48 {
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 {
62 RAY ar;
63 int hlist[4];
64 double xd, yd, zd;
65 double b2;
66 double phi;
67 register int i;
68
69 if (rayorigin(&ar, r, AMBIENT, 0.5) < 0)
70 return(-1);
71 hlist[0] = r->rno;
72 hlist[1] = dp->t;
73 hlist[2] = dp->p;
74 hlist[3] = 0;
75 zd = sqrt((dp->t+urand(ilhash(hlist,4)+dp->n))/h->nt);
76 hlist[3] = 1;
77 phi = 2.0*PI * (dp->p+urand(ilhash(hlist,4)+dp->n))/h->np;
78 xd = cos(phi) * zd;
79 yd = sin(phi) * zd;
80 zd = sqrt(1.0 - zd*zd);
81 for (i = 0; i < 3; i++)
82 ar.rdir[i] = xd*h->ux[i] +
83 yd*h->uy[i] +
84 zd*h->uz[i];
85 dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
86 rayvalue(&ar);
87 ndims--;
88 addcolor(dp->v, ar.rcol);
89 if (ar.rt < FHUGE)
90 dp->r += 1.0/ar.rt;
91 /* (re)initialize error */
92 if (dp->n++) {
93 b2 = bright(dp->v)/dp->n - bright(ar.rcol);
94 b2 = b2*b2 + dp->k*((dp->n-1)*(dp->n-1));
95 dp->k = b2/(dp->n*dp->n);
96 } else
97 dp->k = 0.0;
98 return(0);
99 }
100
101
102 double
103 doambient(acol, r, pg, dg) /* compute ambient component */
104 COLOR acol;
105 RAY *r;
106 FVECT pg, dg;
107 {
108 double b, d;
109 AMBHEMI hemi;
110 AMBSAMP *div;
111 AMBSAMP dnew;
112 register AMBSAMP *dp;
113 double arad;
114 int ndivs, ns;
115 register int i, j;
116 /* initialize color */
117 setcolor(acol, 0.0, 0.0, 0.0);
118 /* initialize hemisphere */
119 inithemi(&hemi, r);
120 ndivs = hemi.nt * hemi.np;
121 if (ndivs == 0)
122 return(0.0);
123 /* set number of super-samples */
124 ns = ambssamp * r->rweight + 0.5;
125 if (ns > 0 || pg != NULL || dg != NULL) {
126 div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
127 if (div == NULL)
128 error(SYSTEM, "out of memory in doambient");
129 } else
130 div = NULL;
131 /* sample the divisions */
132 arad = 0.0;
133 if ((dp = div) == NULL)
134 dp = &dnew;
135 for (i = 0; i < hemi.nt; i++)
136 for (j = 0; j < hemi.np; j++) {
137 dp->t = i; dp->p = j;
138 setcolor(dp->v, 0.0, 0.0, 0.0);
139 dp->r = 0.0;
140 dp->n = 0;
141 if (divsample(dp, &hemi, r) < 0)
142 goto oopsy;
143 if (div != NULL)
144 dp++;
145 else {
146 addcolor(acol, dp->v);
147 arad += dp->r;
148 }
149 }
150 if (ns > 0) { /* perform super-sampling */
151 comperrs(div, &hemi); /* compute errors */
152 qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */
153 /* super-sample */
154 for (i = ns; i > 0; i--) {
155 copystruct(&dnew, div);
156 if (divsample(&dnew, &hemi, r) < 0)
157 goto oopsy;
158 /* reinsert */
159 dp = div;
160 j = ndivs < i ? ndivs : i;
161 while (--j > 0 && dnew.k < dp[1].k) {
162 copystruct(dp, dp+1);
163 dp++;
164 }
165 copystruct(dp, &dnew);
166 }
167 if (pg != NULL || dg != NULL) /* restore order */
168 qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
169 }
170 /* compute returned values */
171 if (div != NULL) {
172 for (i = ndivs, dp = div; i-- > 0; dp++) {
173 arad += dp->r;
174 if (dp->n > 1) {
175 b = 1.0/dp->n;
176 scalecolor(dp->v, b);
177 dp->r *= b;
178 dp->n = 1;
179 }
180 addcolor(acol, dp->v);
181 }
182 b = bright(acol);
183 if (b > FTINY) {
184 b = ndivs/b;
185 if (pg != NULL) {
186 posgradient(pg, div, &hemi);
187 for (i = 0; i < 3; i++)
188 pg[i] *= b;
189 }
190 if (dg != NULL) {
191 dirgradient(dg, div, &hemi);
192 for (i = 0; i < 3; i++)
193 dg[i] *= b;
194 }
195 } else {
196 if (pg != NULL)
197 for (i = 0; i < 3; i++)
198 pg[i] = 0.0;
199 if (dg != NULL)
200 for (i = 0; i < 3; i++)
201 dg[i] = 0.0;
202 }
203 free((char *)div);
204 }
205 b = 1.0/ndivs;
206 scalecolor(acol, b);
207 if (arad <= FTINY)
208 arad = FHUGE;
209 else
210 arad = (ndivs+ns)/arad;
211 if (arad > maxarad)
212 arad = maxarad;
213 else if (arad < minarad)
214 arad = minarad;
215 arad /= sqrt(r->rweight);
216 return(arad);
217 oopsy:
218 if (div != NULL)
219 free((char *)div);
220 return(0.0);
221 }
222
223
224 inithemi(hp, r) /* initialize sampling hemisphere */
225 register AMBHEMI *hp;
226 RAY *r;
227 {
228 register int i;
229 /* set number of divisions */
230 hp->nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5;
231 hp->np = 2 * hp->nt;
232 /* make axes */
233 VCOPY(hp->uz, r->ron);
234 hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
235 for (i = 0; i < 3; i++)
236 if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
237 break;
238 if (i >= 3)
239 error(CONSISTENCY, "bad ray direction in inithemi");
240 hp->uy[i] = 1.0;
241 fcross(hp->ux, hp->uy, hp->uz);
242 normalize(hp->ux);
243 fcross(hp->uy, hp->uz, hp->ux);
244 }
245
246
247 comperrs(da, hp) /* compute initial error estimates */
248 AMBSAMP *da; /* assumes standard ordering */
249 register AMBHEMI *hp;
250 {
251 double b, b2;
252 int i, j;
253 register AMBSAMP *dp;
254 /* sum differences from neighbors */
255 dp = da;
256 for (i = 0; i < hp->nt; i++)
257 for (j = 0; j < hp->np; j++) {
258 #ifdef DEBUG
259 if (dp->t != i || dp->p != j)
260 error(CONSISTENCY,
261 "division order in comperrs");
262 #endif
263 b = bright(dp[0].v);
264 if (i > 0) { /* from above */
265 b2 = bright(dp[-hp->np].v) - b;
266 b2 *= b2 * 0.25;
267 dp[0].k += b2;
268 dp[-hp->np].k += b2;
269 }
270 if (j > 0) { /* from behind */
271 b2 = bright(dp[-1].v) - b;
272 b2 *= b2 * 0.25;
273 dp[0].k += b2;
274 dp[-1].k += b2;
275 } else { /* around */
276 b2 = bright(dp[hp->np-1].v) - b;
277 b2 *= b2 * 0.25;
278 dp[0].k += b2;
279 dp[hp->np-1].k += b2;
280 }
281 dp++;
282 }
283 /* divide by number of neighbors */
284 dp = da;
285 for (j = 0; j < hp->np; j++) /* top row */
286 (dp++)->k *= 1.0/3.0;
287 if (hp->nt < 2)
288 return;
289 for (i = 1; i < hp->nt-1; i++) /* central region */
290 for (j = 0; j < hp->np; j++)
291 (dp++)->k *= 0.25;
292 for (j = 0; j < hp->np; j++) /* bottom row */
293 (dp++)->k *= 1.0/3.0;
294 }
295
296
297 posgradient(gv, da, hp) /* compute position gradient */
298 FVECT gv;
299 AMBSAMP *da; /* assumes standard ordering */
300 AMBHEMI *hp;
301 {
302 register int i, j;
303 double b, d;
304 double mag0, mag1;
305 double phi, cosp, sinp, xd, yd;
306 register AMBSAMP *dp;
307
308 xd = yd = 0.0;
309 for (j = 0; j < hp->np; j++) {
310 dp = da + j;
311 mag0 = mag1 = 0.0;
312 for (i = 0; i < hp->nt; i++) {
313 #ifdef DEBUG
314 if (dp->t != i || dp->p != j)
315 error(CONSISTENCY,
316 "division order in posgradient");
317 #endif
318 b = bright(dp->v);
319 if (i > 0) {
320 d = dp[-hp->np].r;
321 if (dp[0].r > d) d = dp[0].r;
322 d *= 1.0 - sqrt((double)i/hp->nt);
323 mag0 += d*(b - bright(dp[-hp->np].v));
324 }
325 if (j > 0) {
326 d = dp[-1].r;
327 if (dp[0].r > d) d = dp[0].r;
328 mag1 += d*(b - bright(dp[-1].v));
329 } else {
330 d = dp[hp->np-1].r;
331 if (dp[0].r > d) d = dp[0].r;
332 mag1 += d*(b - bright(dp[hp->np-1].v));
333 }
334 dp += hp->np;
335 }
336 if (hp->nt > 1) {
337 mag0 /= (double)hp->np;
338 mag1 /= (double)hp->nt;
339 }
340 phi = 2.0*PI * (double)j/hp->np;
341 cosp = cos(phi); sinp = sin(phi);
342 xd += mag0*cosp - mag1*sinp;
343 yd += mag0*sinp + mag1*cosp;
344 }
345 for (i = 0; i < 3; i++)
346 gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI;
347 }
348
349
350 dirgradient(gv, da, hp) /* compute direction gradient */
351 FVECT gv;
352 AMBSAMP *da; /* assumes standard ordering */
353 AMBHEMI *hp;
354 {
355 register int i, j;
356 double mag;
357 double phi, xd, yd;
358 register AMBSAMP *dp;
359
360 xd = yd = 0.0;
361 for (j = 0; j < hp->np; j++) {
362 dp = da + j;
363 mag = 0.0;
364 for (i = 0; i < hp->nt; i++) {
365 #ifdef DEBUG
366 if (dp->t != i || dp->p != j)
367 error(CONSISTENCY,
368 "division order in dirgradient");
369 #endif
370 mag += sqrt((i+.5)/hp->nt)*bright(dp->v);
371 dp += hp->np;
372 }
373 phi = 2.0*PI * (j+.5)/hp->np + PI/2.0;
374 xd += mag * cos(phi);
375 yd += mag * sin(phi);
376 }
377 for (i = 0; i < 3; i++)
378 gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*PI/(hp->nt*hp->np);
379 }