ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambcomp.c
Revision: 1.15
Committed: Tue Oct 22 12:15:41 1991 UTC (32 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +8 -12 lines
Log Message:
made it so ambient radius reduce when large gradient detected
fixed incorrect position gradient calculation

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