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 |
< |
extern double sin(), cos(), sqrt(); |
25 |
< |
|
32 |
< |
|
33 |
< |
static int |
34 |
< |
ambcmp(d1, d2) /* decreasing order */ |
35 |
< |
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*bright(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 |
|
{ |
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 |
– |
{ |
66 |
|
RAY ar; |
67 |
< |
int hlist[4]; |
67 |
> |
int hlist[3]; |
68 |
> |
double spt[2]; |
69 |
|
double xd, yd, zd; |
70 |
|
double b2; |
71 |
|
double phi; |
72 |
|
register int i; |
73 |
< |
|
74 |
< |
if (rayorigin(&ar, r, AMBIENT, 0.5) < 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 |
+ |
copycolor(ar.rcoef, h->acoef); |
82 |
|
hlist[0] = r->rno; |
83 |
|
hlist[1] = dp->t; |
84 |
|
hlist[2] = dp->p; |
85 |
< |
hlist[3] = 0; |
86 |
< |
zd = sqrt((dp->t+urand(urind(ilhash(hlist,4),dp->n)))/h->nt); |
87 |
< |
hlist[3] = 1; |
88 |
< |
phi = 2.0*PI * (dp->p+urand(urind(ilhash(hlist,4),dp->n)))/h->np; |
89 |
< |
xd = cos(phi) * zd; |
79 |
< |
yd = sin(phi) * zd; |
85 |
> |
multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); |
86 |
> |
zd = sqrt((dp->t + spt[0])/h->nt); |
87 |
> |
phi = 2.0*PI * (dp->p + spt[1])/h->np; |
88 |
> |
xd = tcos(phi) * zd; |
89 |
> |
yd = tsin(phi) * zd; |
90 |
|
zd = sqrt(1.0 - zd*zd); |
91 |
|
for (i = 0; i < 3; i++) |
92 |
|
ar.rdir[i] = xd*h->ux[i] + |
95 |
|
dimlist[ndims++] = dp->t*h->np + dp->p + 90171; |
96 |
|
rayvalue(&ar); |
97 |
|
ndims--; |
98 |
+ |
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
99 |
|
addcolor(dp->v, ar.rcol); |
100 |
+ |
/* use rt to improve gradient calc */ |
101 |
|
if (ar.rt > FTINY && ar.rt < FHUGE) |
102 |
|
dp->r += 1.0/ar.rt; |
103 |
|
/* (re)initialize error */ |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
+ |
static int |
115 |
+ |
ambcmp( /* decreasing order */ |
116 |
+ |
const void *p1, |
117 |
+ |
const void *p2 |
118 |
+ |
) |
119 |
+ |
{ |
120 |
+ |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
121 |
+ |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
122 |
+ |
|
123 |
+ |
if (d1->k < d2->k) |
124 |
+ |
return(1); |
125 |
+ |
if (d1->k > d2->k) |
126 |
+ |
return(-1); |
127 |
+ |
return(0); |
128 |
+ |
} |
129 |
+ |
|
130 |
+ |
|
131 |
+ |
static int |
132 |
+ |
ambnorm( /* standard order */ |
133 |
+ |
const void *p1, |
134 |
+ |
const void *p2 |
135 |
+ |
) |
136 |
+ |
{ |
137 |
+ |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
138 |
+ |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
139 |
+ |
register int c; |
140 |
+ |
|
141 |
+ |
if ( (c = d1->t - d2->t) ) |
142 |
+ |
return(c); |
143 |
+ |
return(d1->p - d2->p); |
144 |
+ |
} |
145 |
+ |
|
146 |
+ |
|
147 |
|
double |
148 |
< |
doambient(acol, r, pg, dg) /* compute ambient component */ |
149 |
< |
COLOR acol; |
150 |
< |
RAY *r; |
151 |
< |
FVECT pg, dg; |
148 |
> |
doambient( /* compute ambient component */ |
149 |
> |
COLOR acol, |
150 |
> |
RAY *r, |
151 |
> |
double wt, |
152 |
> |
FVECT pg, |
153 |
> |
FVECT dg |
154 |
> |
) |
155 |
|
{ |
156 |
|
double b, d; |
157 |
|
AMBHEMI hemi; |
159 |
|
AMBSAMP dnew; |
160 |
|
register AMBSAMP *dp; |
161 |
|
double arad; |
162 |
< |
int ndivs, ns; |
162 |
> |
int ndivs; |
163 |
|
register int i, j; |
116 |
– |
/* initialize color */ |
117 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
164 |
|
/* initialize hemisphere */ |
165 |
< |
inithemi(&hemi, r); |
165 |
> |
inithemi(&hemi, acol, r, wt); |
166 |
|
ndivs = hemi.nt * hemi.np; |
167 |
+ |
/* initialize sum */ |
168 |
+ |
setcolor(acol, 0.0, 0.0, 0.0); |
169 |
|
if (ndivs == 0) |
170 |
|
return(0.0); |
171 |
< |
/* set number of super-samples */ |
172 |
< |
ns = ambssamp * r->rweight + 0.5; |
125 |
< |
if (ns > 0 || pg != NULL || dg != NULL) { |
171 |
> |
/* allocate super-samples */ |
172 |
> |
if (hemi.ns > 0 || pg != NULL || dg != NULL) { |
173 |
|
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
174 |
|
if (div == NULL) |
175 |
|
error(SYSTEM, "out of memory in doambient"); |
185 |
|
setcolor(dp->v, 0.0, 0.0, 0.0); |
186 |
|
dp->r = 0.0; |
187 |
|
dp->n = 0; |
188 |
< |
if (divsample(dp, &hemi, r) < 0) |
189 |
< |
goto oopsy; |
188 |
> |
if (divsample(dp, &hemi, r) < 0) { |
189 |
> |
if (div != NULL) dp++; |
190 |
> |
continue; |
191 |
> |
} |
192 |
> |
arad += dp->r; |
193 |
|
if (div != NULL) |
194 |
|
dp++; |
195 |
< |
else { |
195 |
> |
else |
196 |
|
addcolor(acol, dp->v); |
147 |
– |
arad += dp->r; |
148 |
– |
} |
197 |
|
} |
198 |
< |
if (ns > 0) { /* perform super-sampling */ |
198 |
> |
if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad) |
199 |
> |
hemi.ns = 0; /* close enough */ |
200 |
> |
else if (hemi.ns > 0) { /* else perform super-sampling */ |
201 |
|
comperrs(div, &hemi); /* compute errors */ |
202 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
203 |
|
/* super-sample */ |
204 |
< |
for (i = ns; i > 0; i--) { |
205 |
< |
copystruct(&dnew, div); |
206 |
< |
if (divsample(&dnew, &hemi, r) < 0) |
207 |
< |
goto oopsy; |
208 |
< |
/* reinsert */ |
209 |
< |
dp = div; |
204 |
> |
for (i = hemi.ns; i > 0; i--) { |
205 |
> |
dnew = *div; |
206 |
> |
if (divsample(&dnew, &hemi, r) < 0) { |
207 |
> |
dp++; |
208 |
> |
continue; |
209 |
> |
} |
210 |
> |
dp = div; /* reinsert */ |
211 |
|
j = ndivs < i ? ndivs : i; |
212 |
|
while (--j > 0 && dnew.k < dp[1].k) { |
213 |
< |
copystruct(dp, dp+1); |
213 |
> |
*dp = *(dp+1); |
214 |
|
dp++; |
215 |
|
} |
216 |
< |
copystruct(dp, &dnew); |
216 |
> |
*dp = dnew; |
217 |
|
} |
218 |
|
if (pg != NULL || dg != NULL) /* restore order */ |
219 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); |
220 |
|
} |
221 |
|
/* compute returned values */ |
222 |
|
if (div != NULL) { |
223 |
+ |
arad = 0.0; |
224 |
|
for (i = ndivs, dp = div; i-- > 0; dp++) { |
225 |
|
arad += dp->r; |
226 |
|
if (dp->n > 1) { |
233 |
|
} |
234 |
|
b = bright(acol); |
235 |
|
if (b > FTINY) { |
236 |
< |
b = ndivs/b; |
236 |
> |
b = 1.0/b; /* normalize gradient(s) */ |
237 |
|
if (pg != NULL) { |
238 |
|
posgradient(pg, div, &hemi); |
239 |
|
for (i = 0; i < 3; i++) |
252 |
|
for (i = 0; i < 3; i++) |
253 |
|
dg[i] = 0.0; |
254 |
|
} |
255 |
< |
free((char *)div); |
255 |
> |
free((void *)div); |
256 |
|
} |
205 |
– |
b = 1.0/ndivs; |
206 |
– |
scalecolor(acol, b); |
257 |
|
if (arad <= FTINY) |
208 |
– |
arad = FHUGE; |
209 |
– |
else |
210 |
– |
arad = (ndivs+ns)/arad; |
211 |
– |
if (arad > maxarad) |
258 |
|
arad = maxarad; |
259 |
< |
else if (arad < minarad) |
259 |
> |
else |
260 |
> |
arad = (ndivs+hemi.ns)/arad; |
261 |
> |
if (pg != NULL) { /* reduce radius if gradient large */ |
262 |
> |
d = DOT(pg,pg); |
263 |
> |
if (d*arad*arad > 1.0) |
264 |
> |
arad = 1.0/sqrt(d); |
265 |
> |
} |
266 |
> |
if (arad < minarad) { |
267 |
|
arad = minarad; |
268 |
< |
arad /= sqrt(r->rweight); |
269 |
< |
if (pg != NULL) { /* clip pos. gradient if too large */ |
217 |
< |
d = 4.0*DOT(pg,pg)*arad*arad; |
218 |
< |
if (d > 1.0) { |
219 |
< |
d = 1.0/sqrt(d); |
268 |
> |
if (pg != NULL && d*arad*arad > 1.0) { /* cap gradient */ |
269 |
> |
d = 1.0/arad/sqrt(d); |
270 |
|
for (i = 0; i < 3; i++) |
271 |
|
pg[i] *= d; |
272 |
|
} |
273 |
|
} |
274 |
+ |
if ((arad /= sqrt(wt)) > maxarad) |
275 |
+ |
arad = maxarad; |
276 |
|
return(arad); |
225 |
– |
oopsy: |
226 |
– |
if (div != NULL) |
227 |
– |
free((char *)div); |
228 |
– |
return(0.0); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
< |
inithemi(hp, r) /* initialize sampling hemisphere */ |
281 |
< |
register AMBHEMI *hp; |
282 |
< |
RAY *r; |
280 |
> |
void |
281 |
> |
comperrs( /* compute initial error estimates */ |
282 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
283 |
> |
register AMBHEMI *hp |
284 |
> |
) |
285 |
|
{ |
236 |
– |
register int i; |
237 |
– |
/* set number of divisions */ |
238 |
– |
hp->nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5; |
239 |
– |
hp->np = 2 * hp->nt; |
240 |
– |
/* make axes */ |
241 |
– |
VCOPY(hp->uz, r->ron); |
242 |
– |
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
243 |
– |
for (i = 0; i < 3; i++) |
244 |
– |
if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) |
245 |
– |
break; |
246 |
– |
if (i >= 3) |
247 |
– |
error(CONSISTENCY, "bad ray direction in inithemi"); |
248 |
– |
hp->uy[i] = 1.0; |
249 |
– |
fcross(hp->ux, hp->uy, hp->uz); |
250 |
– |
normalize(hp->ux); |
251 |
– |
fcross(hp->uy, hp->uz, hp->ux); |
252 |
– |
} |
253 |
– |
|
254 |
– |
|
255 |
– |
comperrs(da, hp) /* compute initial error estimates */ |
256 |
– |
AMBSAMP *da; /* assumes standard ordering */ |
257 |
– |
register AMBHEMI *hp; |
258 |
– |
{ |
286 |
|
double b, b2; |
287 |
|
int i, j; |
288 |
|
register AMBSAMP *dp; |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
< |
posgradient(gv, da, hp) /* compute position gradient */ |
333 |
< |
FVECT gv; |
334 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
335 |
< |
AMBHEMI *hp; |
332 |
> |
void |
333 |
> |
posgradient( /* compute position gradient */ |
334 |
> |
FVECT gv, |
335 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
336 |
> |
register AMBHEMI *hp |
337 |
> |
) |
338 |
|
{ |
339 |
|
register int i, j; |
340 |
< |
double b, d; |
340 |
> |
double nextsine, lastsine, b, d; |
341 |
|
double mag0, mag1; |
342 |
|
double phi, cosp, sinp, xd, yd; |
343 |
|
register AMBSAMP *dp; |
346 |
|
for (j = 0; j < hp->np; j++) { |
347 |
|
dp = da + j; |
348 |
|
mag0 = mag1 = 0.0; |
349 |
+ |
lastsine = 0.0; |
350 |
|
for (i = 0; i < hp->nt; i++) { |
351 |
|
#ifdef DEBUG |
352 |
|
if (dp->t != i || dp->p != j) |
357 |
|
if (i > 0) { |
358 |
|
d = dp[-hp->np].r; |
359 |
|
if (dp[0].r > d) d = dp[0].r; |
360 |
< |
d *= 1.0 - sqrt((double)i/hp->nt); |
360 |
> |
/* sin(t)*cos(t)^2 */ |
361 |
> |
d *= lastsine * (1.0 - (double)i/hp->nt); |
362 |
|
mag0 += d*(b - bright(dp[-hp->np].v)); |
363 |
|
} |
364 |
+ |
nextsine = sqrt((double)(i+1)/hp->nt); |
365 |
|
if (j > 0) { |
366 |
|
d = dp[-1].r; |
367 |
|
if (dp[0].r > d) d = dp[0].r; |
368 |
< |
mag1 += d*(b - bright(dp[-1].v)); |
368 |
> |
mag1 += d * (nextsine - lastsine) * |
369 |
> |
(b - bright(dp[-1].v)); |
370 |
|
} else { |
371 |
|
d = dp[hp->np-1].r; |
372 |
|
if (dp[0].r > d) d = dp[0].r; |
373 |
< |
mag1 += d*(b - bright(dp[hp->np-1].v)); |
373 |
> |
mag1 += d * (nextsine - lastsine) * |
374 |
> |
(b - bright(dp[hp->np-1].v)); |
375 |
|
} |
376 |
|
dp += hp->np; |
377 |
+ |
lastsine = nextsine; |
378 |
|
} |
379 |
< |
if (hp->nt > 1) { |
345 |
< |
mag0 /= (double)hp->np; |
346 |
< |
mag1 /= (double)hp->nt; |
347 |
< |
} |
379 |
> |
mag0 *= 2.0*PI / hp->np; |
380 |
|
phi = 2.0*PI * (double)j/hp->np; |
381 |
< |
cosp = cos(phi); sinp = sin(phi); |
381 |
> |
cosp = tcos(phi); sinp = tsin(phi); |
382 |
|
xd += mag0*cosp - mag1*sinp; |
383 |
|
yd += mag0*sinp + mag1*cosp; |
384 |
|
} |
385 |
|
for (i = 0; i < 3; i++) |
386 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI; |
386 |
> |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI; |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
< |
dirgradient(gv, da, hp) /* compute direction gradient */ |
391 |
< |
FVECT gv; |
392 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
393 |
< |
AMBHEMI *hp; |
390 |
> |
void |
391 |
> |
dirgradient( /* compute direction gradient */ |
392 |
> |
FVECT gv, |
393 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
394 |
> |
register AMBHEMI *hp |
395 |
> |
) |
396 |
|
{ |
397 |
|
register int i, j; |
398 |
|
double mag; |
409 |
|
error(CONSISTENCY, |
410 |
|
"division order in dirgradient"); |
411 |
|
#endif |
412 |
< |
mag += sqrt((i+.5)/hp->nt)*bright(dp->v); |
412 |
> |
/* tan(t) */ |
413 |
> |
mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0); |
414 |
|
dp += hp->np; |
415 |
|
} |
416 |
|
phi = 2.0*PI * (j+.5)/hp->np + PI/2.0; |
417 |
< |
xd += mag * cos(phi); |
418 |
< |
yd += mag * sin(phi); |
417 |
> |
xd += mag * tcos(phi); |
418 |
> |
yd += mag * tsin(phi); |
419 |
|
} |
420 |
|
for (i = 0; i < 3; i++) |
421 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*PI/(hp->nt*hp->np); |
421 |
> |
gv[i] = xd*hp->ux[i] + yd*hp->uy[i]; |
422 |
|
} |