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 |
|
|
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 |
– |
|
19 |
|
static int |
20 |
|
ambcmp(d1, d2) /* decreasing order */ |
21 |
|
AMBSAMP *d1, *d2; |
34 |
|
{ |
35 |
|
register int c; |
36 |
|
|
37 |
< |
if (c = d1->t - d2->t) |
37 |
> |
if ( (c = d1->t - d2->t) ) |
38 |
|
return(c); |
39 |
|
return(d1->p - d2->p); |
40 |
|
} |
41 |
|
|
42 |
|
|
43 |
+ |
int |
44 |
|
divsample(dp, h, r) /* sample a division */ |
45 |
|
register AMBSAMP *dp; |
46 |
|
AMBHEMI *h; |
47 |
|
RAY *r; |
48 |
|
{ |
49 |
|
RAY ar; |
50 |
< |
int hlist[4]; |
50 |
> |
int hlist[3]; |
51 |
> |
double spt[2]; |
52 |
|
double xd, yd, zd; |
53 |
|
double b2; |
54 |
|
double phi; |
55 |
|
register int i; |
56 |
|
|
57 |
< |
if (rayorigin(&ar, r, AMBIENT, 0.5) < 0) |
57 |
> |
if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0) |
58 |
|
return(-1); |
59 |
|
hlist[0] = r->rno; |
60 |
|
hlist[1] = dp->t; |
61 |
|
hlist[2] = dp->p; |
62 |
< |
hlist[3] = 0; |
63 |
< |
zd = sqrt((dp->t+urand(ilhash(hlist,4)+dp->n))/h->nt); |
64 |
< |
hlist[3] = 1; |
65 |
< |
phi = 2.0*PI * (dp->p+urand(ilhash(hlist,4)+dp->n))/h->np; |
66 |
< |
xd = cos(phi) * zd; |
79 |
< |
yd = sin(phi) * zd; |
62 |
> |
multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); |
63 |
> |
zd = sqrt((dp->t + spt[0])/h->nt); |
64 |
> |
phi = 2.0*PI * (dp->p + spt[1])/h->np; |
65 |
> |
xd = tcos(phi) * zd; |
66 |
> |
yd = tsin(phi) * zd; |
67 |
|
zd = sqrt(1.0 - zd*zd); |
68 |
|
for (i = 0; i < 3; i++) |
69 |
|
ar.rdir[i] = xd*h->ux[i] + |
73 |
|
rayvalue(&ar); |
74 |
|
ndims--; |
75 |
|
addcolor(dp->v, ar.rcol); |
76 |
< |
if (ar.rt < FHUGE) |
76 |
> |
/* use rt to improve gradient calc */ |
77 |
> |
if (ar.rt > FTINY && ar.rt < FHUGE) |
78 |
|
dp->r += 1.0/ar.rt; |
79 |
|
/* (re)initialize error */ |
80 |
|
if (dp->n++) { |
88 |
|
|
89 |
|
|
90 |
|
double |
91 |
< |
doambient(acol, r, pg, dg) /* compute ambient component */ |
91 |
> |
doambient(acol, r, wt, pg, dg) /* compute ambient component */ |
92 |
|
COLOR acol; |
93 |
|
RAY *r; |
94 |
+ |
double wt; |
95 |
|
FVECT pg, dg; |
96 |
|
{ |
97 |
|
double b, d; |
105 |
|
/* initialize color */ |
106 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
107 |
|
/* initialize hemisphere */ |
108 |
< |
inithemi(&hemi, r); |
108 |
> |
inithemi(&hemi, r, wt); |
109 |
|
ndivs = hemi.nt * hemi.np; |
110 |
|
if (ndivs == 0) |
111 |
|
return(0.0); |
112 |
|
/* set number of super-samples */ |
113 |
< |
ns = ambssamp * r->rweight + 0.5; |
113 |
> |
ns = ambssamp * wt + 0.5; |
114 |
|
if (ns > 0 || pg != NULL || dg != NULL) { |
115 |
|
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
116 |
|
if (div == NULL) |
129 |
|
dp->n = 0; |
130 |
|
if (divsample(dp, &hemi, r) < 0) |
131 |
|
goto oopsy; |
132 |
+ |
arad += dp->r; |
133 |
|
if (div != NULL) |
134 |
|
dp++; |
135 |
< |
else { |
135 |
> |
else |
136 |
|
addcolor(acol, dp->v); |
147 |
– |
arad += dp->r; |
148 |
– |
} |
137 |
|
} |
138 |
< |
if (ns > 0) { /* perform super-sampling */ |
138 |
> |
if (ns > 0 && arad > FTINY && ndivs/arad < minarad) |
139 |
> |
ns = 0; /* close enough */ |
140 |
> |
else if (ns > 0) { /* else perform super-sampling */ |
141 |
|
comperrs(div, &hemi); /* compute errors */ |
142 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
143 |
|
/* super-sample */ |
144 |
|
for (i = ns; i > 0; i--) { |
145 |
< |
copystruct(&dnew, div); |
145 |
> |
dnew = *div; |
146 |
|
if (divsample(&dnew, &hemi, r) < 0) |
147 |
|
goto oopsy; |
148 |
|
/* reinsert */ |
149 |
|
dp = div; |
150 |
|
j = ndivs < i ? ndivs : i; |
151 |
|
while (--j > 0 && dnew.k < dp[1].k) { |
152 |
< |
copystruct(dp, dp+1); |
152 |
> |
*dp = *(dp+1); |
153 |
|
dp++; |
154 |
|
} |
155 |
< |
copystruct(dp, &dnew); |
155 |
> |
*dp = dnew; |
156 |
|
} |
157 |
|
if (pg != NULL || dg != NULL) /* restore order */ |
158 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); |
159 |
|
} |
160 |
|
/* compute returned values */ |
161 |
|
if (div != NULL) { |
162 |
+ |
arad = 0.0; |
163 |
|
for (i = ndivs, dp = div; i-- > 0; dp++) { |
164 |
|
arad += dp->r; |
165 |
|
if (dp->n > 1) { |
171 |
|
addcolor(acol, dp->v); |
172 |
|
} |
173 |
|
b = bright(acol); |
174 |
< |
if (b > FTINY) |
174 |
> |
if (b > FTINY) { |
175 |
|
b = ndivs/b; |
176 |
< |
else |
177 |
< |
b = 0.0; |
178 |
< |
if (pg != NULL) { |
179 |
< |
posgradient(pg, div, &hemi); |
180 |
< |
for (i = 0; i < 3; i++) |
181 |
< |
pg[i] *= b; |
176 |
> |
if (pg != NULL) { |
177 |
> |
posgradient(pg, div, &hemi); |
178 |
> |
for (i = 0; i < 3; i++) |
179 |
> |
pg[i] *= b; |
180 |
> |
} |
181 |
> |
if (dg != NULL) { |
182 |
> |
dirgradient(dg, div, &hemi); |
183 |
> |
for (i = 0; i < 3; i++) |
184 |
> |
dg[i] *= b; |
185 |
> |
} |
186 |
> |
} else { |
187 |
> |
if (pg != NULL) |
188 |
> |
for (i = 0; i < 3; i++) |
189 |
> |
pg[i] = 0.0; |
190 |
> |
if (dg != NULL) |
191 |
> |
for (i = 0; i < 3; i++) |
192 |
> |
dg[i] = 0.0; |
193 |
|
} |
194 |
< |
if (dg != NULL) { |
193 |
< |
dirgradient(dg, div, &hemi); |
194 |
< |
for (i = 0; i < 3; i++) |
195 |
< |
dg[i] *= b; |
196 |
< |
} |
197 |
< |
free((char *)div); |
194 |
> |
free((void *)div); |
195 |
|
} |
196 |
|
b = 1.0/ndivs; |
197 |
|
scalecolor(acol, b); |
198 |
|
if (arad <= FTINY) |
199 |
< |
arad = FHUGE; |
199 |
> |
arad = maxarad; |
200 |
|
else |
201 |
|
arad = (ndivs+ns)/arad; |
202 |
< |
if (arad > maxarad) |
203 |
< |
arad = maxarad; |
204 |
< |
else if (arad < minarad) |
202 |
> |
if (pg != NULL) { /* reduce radius if gradient large */ |
203 |
> |
d = DOT(pg,pg); |
204 |
> |
if (d*arad*arad > 1.0) |
205 |
> |
arad = 1.0/sqrt(d); |
206 |
> |
} |
207 |
> |
if (arad < minarad) { |
208 |
|
arad = minarad; |
209 |
< |
arad /= sqrt(r->rweight); |
209 |
> |
if (pg != NULL && d*arad*arad > 1.0) { /* cap gradient */ |
210 |
> |
d = 1.0/arad/sqrt(d); |
211 |
> |
for (i = 0; i < 3; i++) |
212 |
> |
pg[i] *= d; |
213 |
> |
} |
214 |
> |
} |
215 |
> |
if ((arad /= sqrt(wt)) > maxarad) |
216 |
> |
arad = maxarad; |
217 |
|
return(arad); |
218 |
|
oopsy: |
219 |
|
if (div != NULL) |
220 |
< |
free((char *)div); |
220 |
> |
free((void *)div); |
221 |
|
return(0.0); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
< |
inithemi(hp, r) /* initialize sampling hemisphere */ |
225 |
> |
void |
226 |
> |
inithemi(hp, r, wt) /* initialize sampling hemisphere */ |
227 |
|
register AMBHEMI *hp; |
228 |
|
RAY *r; |
229 |
+ |
double wt; |
230 |
|
{ |
231 |
|
register int i; |
232 |
|
/* set number of divisions */ |
233 |
< |
hp->nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5; |
234 |
< |
hp->np = 2 * hp->nt; |
233 |
> |
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
234 |
> |
i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ |
235 |
> |
if (hp->nt < i) |
236 |
> |
hp->nt = i; |
237 |
> |
hp->np = PI * hp->nt + 0.5; |
238 |
|
/* make axes */ |
239 |
|
VCOPY(hp->uz, r->ron); |
240 |
|
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
+ |
void |
254 |
|
comperrs(da, hp) /* compute initial error estimates */ |
255 |
|
AMBSAMP *da; /* assumes standard ordering */ |
256 |
|
register AMBHEMI *hp; |
262 |
|
dp = da; |
263 |
|
for (i = 0; i < hp->nt; i++) |
264 |
|
for (j = 0; j < hp->np; j++) { |
265 |
+ |
#ifdef DEBUG |
266 |
+ |
if (dp->t != i || dp->p != j) |
267 |
+ |
error(CONSISTENCY, |
268 |
+ |
"division order in comperrs"); |
269 |
+ |
#endif |
270 |
|
b = bright(dp[0].v); |
271 |
|
if (i > 0) { /* from above */ |
272 |
|
b2 = bright(dp[-hp->np].v) - b; |
301 |
|
} |
302 |
|
|
303 |
|
|
304 |
+ |
void |
305 |
|
posgradient(gv, da, hp) /* compute position gradient */ |
306 |
|
FVECT gv; |
307 |
|
AMBSAMP *da; /* assumes standard ordering */ |
308 |
< |
AMBHEMI *hp; |
308 |
> |
register AMBHEMI *hp; |
309 |
|
{ |
310 |
|
register int i, j; |
311 |
< |
double b, d; |
311 |
> |
double nextsine, lastsine, b, d; |
312 |
|
double mag0, mag1; |
313 |
|
double phi, cosp, sinp, xd, yd; |
314 |
|
register AMBSAMP *dp; |
317 |
|
for (j = 0; j < hp->np; j++) { |
318 |
|
dp = da + j; |
319 |
|
mag0 = mag1 = 0.0; |
320 |
+ |
lastsine = 0.0; |
321 |
|
for (i = 0; i < hp->nt; i++) { |
322 |
|
#ifdef DEBUG |
323 |
|
if (dp->t != i || dp->p != j) |
328 |
|
if (i > 0) { |
329 |
|
d = dp[-hp->np].r; |
330 |
|
if (dp[0].r > d) d = dp[0].r; |
331 |
< |
d *= 1.0 - sqrt((double)i/hp->nt); |
331 |
> |
/* sin(t)*cos(t)^2 */ |
332 |
> |
d *= lastsine * (1.0 - (double)i/hp->nt); |
333 |
|
mag0 += d*(b - bright(dp[-hp->np].v)); |
334 |
|
} |
335 |
+ |
nextsine = sqrt((double)(i+1)/hp->nt); |
336 |
|
if (j > 0) { |
337 |
|
d = dp[-1].r; |
338 |
|
if (dp[0].r > d) d = dp[0].r; |
339 |
< |
mag1 += d*(b - bright(dp[-1].v)); |
339 |
> |
mag1 += d * (nextsine - lastsine) * |
340 |
> |
(b - bright(dp[-1].v)); |
341 |
|
} else { |
342 |
|
d = dp[hp->np-1].r; |
343 |
|
if (dp[0].r > d) d = dp[0].r; |
344 |
< |
mag1 += d*(b - bright(dp[hp->np-1].v)); |
344 |
> |
mag1 += d * (nextsine - lastsine) * |
345 |
> |
(b - bright(dp[hp->np-1].v)); |
346 |
|
} |
347 |
|
dp += hp->np; |
348 |
+ |
lastsine = nextsine; |
349 |
|
} |
350 |
< |
if (hp->nt > 1) { |
326 |
< |
mag0 /= (double)hp->np; |
327 |
< |
mag1 /= (double)hp->nt; |
328 |
< |
} |
350 |
> |
mag0 *= 2.0*PI / hp->np; |
351 |
|
phi = 2.0*PI * (double)j/hp->np; |
352 |
< |
cosp = cos(phi); sinp = sin(phi); |
352 |
> |
cosp = tcos(phi); sinp = tsin(phi); |
353 |
|
xd += mag0*cosp - mag1*sinp; |
354 |
|
yd += mag0*sinp + mag1*cosp; |
355 |
|
} |
358 |
|
} |
359 |
|
|
360 |
|
|
361 |
+ |
void |
362 |
|
dirgradient(gv, da, hp) /* compute direction gradient */ |
363 |
|
FVECT gv; |
364 |
|
AMBSAMP *da; /* assumes standard ordering */ |
365 |
< |
AMBHEMI *hp; |
365 |
> |
register AMBHEMI *hp; |
366 |
|
{ |
367 |
|
register int i, j; |
368 |
|
double mag; |
379 |
|
error(CONSISTENCY, |
380 |
|
"division order in dirgradient"); |
381 |
|
#endif |
382 |
< |
mag += sqrt((i+.5)/hp->nt)*bright(dp->v); |
382 |
> |
/* tan(t) */ |
383 |
> |
mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0); |
384 |
|
dp += hp->np; |
385 |
|
} |
386 |
|
phi = 2.0*PI * (j+.5)/hp->np + PI/2.0; |
387 |
< |
xd += mag * cos(phi); |
388 |
< |
yd += mag * sin(phi); |
387 |
> |
xd += mag * tcos(phi); |
388 |
> |
yd += mag * tsin(phi); |
389 |
|
} |
390 |
|
for (i = 0; i < 3; i++) |
391 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*PI/(hp->nt*hp->np); |
391 |
> |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np); |
392 |
|
} |