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 |
> |
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 |
|
{ |
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 |
– |
{ |
65 |
|
RAY ar; |
66 |
< |
int hlist[4]; |
66 |
> |
int hlist[3]; |
67 |
> |
double spt[2]; |
68 |
|
double xd, yd, zd; |
69 |
|
double b2; |
70 |
|
double phi; |
71 |
|
register int i; |
72 |
< |
|
73 |
< |
if (rayorigin(&ar, r, AMBIENT, 0.5) < 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; |
83 |
< |
hlist[3] = 0; |
84 |
< |
zd = sqrt((dp->t+urand(ilhash(hlist,4)+dp->n))/h->nt); |
85 |
< |
hlist[3] = 1; |
86 |
< |
phi = 2.0*PI * (dp->p+urand(ilhash(hlist,4)+dp->n))/h->np; |
87 |
< |
xd = cos(phi) * zd; |
79 |
< |
yd = sin(phi) * zd; |
83 |
> |
multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); |
84 |
> |
zd = sqrt((dp->t + spt[0])/h->nt); |
85 |
> |
phi = 2.0*PI * (dp->p + spt[1])/h->np; |
86 |
> |
xd = tcos(phi) * zd; |
87 |
> |
yd = tsin(phi) * zd; |
88 |
|
zd = sqrt(1.0 - zd*zd); |
89 |
|
for (i = 0; i < 3; i++) |
90 |
|
ar.rdir[i] = xd*h->ux[i] + |
94 |
|
rayvalue(&ar); |
95 |
|
ndims--; |
96 |
|
addcolor(dp->v, ar.rcol); |
97 |
< |
if (ar.rt < FHUGE) |
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++) { |
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, pg, dg) /* compute ambient component */ |
146 |
< |
COLOR acol; |
147 |
< |
RAY *r; |
148 |
< |
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; |
162 |
|
/* initialize color */ |
163 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
164 |
|
/* initialize hemisphere */ |
165 |
< |
inithemi(&hemi, r); |
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 * r->rweight + 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) |
185 |
|
dp->n = 0; |
186 |
|
if (divsample(dp, &hemi, r) < 0) |
187 |
|
goto oopsy; |
188 |
+ |
arad += dp->r; |
189 |
|
if (div != NULL) |
190 |
|
dp++; |
191 |
< |
else { |
191 |
> |
else |
192 |
|
addcolor(acol, dp->v); |
147 |
– |
arad += dp->r; |
148 |
– |
} |
193 |
|
} |
194 |
< |
if (ns > 0) { /* perform super-sampling */ |
194 |
> |
if (ns > 0 && arad > FTINY && ndivs/arad < minarad) |
195 |
> |
ns = 0; /* close enough */ |
196 |
> |
else if (ns > 0) { /* else perform super-sampling */ |
197 |
|
comperrs(div, &hemi); /* compute errors */ |
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); |
215 |
|
} |
216 |
|
/* compute returned values */ |
217 |
|
if (div != NULL) { |
218 |
+ |
arad = 0.0; |
219 |
|
for (i = ndivs, dp = div; i-- > 0; dp++) { |
220 |
|
arad += dp->r; |
221 |
|
if (dp->n > 1) { |
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); |
254 |
|
if (arad <= FTINY) |
255 |
< |
arad = FHUGE; |
255 |
> |
arad = maxarad; |
256 |
|
else |
257 |
|
arad = (ndivs+ns)/arad; |
258 |
< |
if (arad > maxarad) |
259 |
< |
arad = maxarad; |
260 |
< |
else if (arad < minarad) |
258 |
> |
if (pg != NULL) { /* reduce radius if gradient large */ |
259 |
> |
d = DOT(pg,pg); |
260 |
> |
if (d*arad*arad > 1.0) |
261 |
> |
arad = 1.0/sqrt(d); |
262 |
> |
} |
263 |
> |
if (arad < minarad) { |
264 |
|
arad = minarad; |
265 |
< |
arad /= sqrt(r->rweight); |
266 |
< |
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); |
265 |
> |
if (pg != NULL && d*arad*arad > 1.0) { /* cap gradient */ |
266 |
> |
d = 1.0/arad/sqrt(d); |
267 |
|
for (i = 0; i < 3; i++) |
268 |
|
pg[i] *= d; |
269 |
|
} |
270 |
|
} |
271 |
+ |
if ((arad /= sqrt(wt)) > maxarad) |
272 |
+ |
arad = maxarad; |
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) /* initialize sampling hemisphere */ |
282 |
< |
register AMBHEMI *hp; |
283 |
< |
RAY *r; |
281 |
> |
void |
282 |
> |
comperrs( /* compute initial error estimates */ |
283 |
> |
AMBSAMP *da, /* assumes standard ordering */ |
284 |
> |
register AMBHEMI *hp |
285 |
> |
) |
286 |
|
{ |
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 |
– |
{ |
287 |
|
double b, b2; |
288 |
|
int i, j; |
289 |
|
register AMBSAMP *dp; |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
< |
posgradient(gv, da, hp) /* compute position gradient */ |
334 |
< |
FVECT gv; |
335 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
336 |
< |
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 b, d; |
341 |
> |
double nextsine, lastsine, b, d; |
342 |
|
double mag0, mag1; |
343 |
|
double phi, cosp, sinp, xd, yd; |
344 |
|
register AMBSAMP *dp; |
347 |
|
for (j = 0; j < hp->np; j++) { |
348 |
|
dp = da + j; |
349 |
|
mag0 = mag1 = 0.0; |
350 |
+ |
lastsine = 0.0; |
351 |
|
for (i = 0; i < hp->nt; i++) { |
352 |
|
#ifdef DEBUG |
353 |
|
if (dp->t != i || dp->p != j) |
358 |
|
if (i > 0) { |
359 |
|
d = dp[-hp->np].r; |
360 |
|
if (dp[0].r > d) d = dp[0].r; |
361 |
< |
d *= 1.0 - sqrt((double)i/hp->nt); |
361 |
> |
/* sin(t)*cos(t)^2 */ |
362 |
> |
d *= lastsine * (1.0 - (double)i/hp->nt); |
363 |
|
mag0 += d*(b - bright(dp[-hp->np].v)); |
364 |
|
} |
365 |
+ |
nextsine = sqrt((double)(i+1)/hp->nt); |
366 |
|
if (j > 0) { |
367 |
|
d = dp[-1].r; |
368 |
|
if (dp[0].r > d) d = dp[0].r; |
369 |
< |
mag1 += d*(b - bright(dp[-1].v)); |
369 |
> |
mag1 += d * (nextsine - lastsine) * |
370 |
> |
(b - bright(dp[-1].v)); |
371 |
|
} else { |
372 |
|
d = dp[hp->np-1].r; |
373 |
|
if (dp[0].r > d) d = dp[0].r; |
374 |
< |
mag1 += d*(b - bright(dp[hp->np-1].v)); |
374 |
> |
mag1 += d * (nextsine - lastsine) * |
375 |
> |
(b - bright(dp[hp->np-1].v)); |
376 |
|
} |
377 |
|
dp += hp->np; |
378 |
+ |
lastsine = nextsine; |
379 |
|
} |
380 |
< |
if (hp->nt > 1) { |
345 |
< |
mag0 /= (double)hp->np; |
346 |
< |
mag1 /= (double)hp->nt; |
347 |
< |
} |
380 |
> |
mag0 *= 2.0*PI / hp->np; |
381 |
|
phi = 2.0*PI * (double)j/hp->np; |
382 |
< |
cosp = cos(phi); sinp = sin(phi); |
382 |
> |
cosp = tcos(phi); sinp = tsin(phi); |
383 |
|
xd += mag0*cosp - mag1*sinp; |
384 |
|
yd += mag0*sinp + mag1*cosp; |
385 |
|
} |
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
< |
dirgradient(gv, da, hp) /* compute direction gradient */ |
392 |
< |
FVECT gv; |
393 |
< |
AMBSAMP *da; /* assumes standard ordering */ |
394 |
< |
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; |
410 |
|
error(CONSISTENCY, |
411 |
|
"division order in dirgradient"); |
412 |
|
#endif |
413 |
< |
mag += sqrt((i+.5)/hp->nt)*bright(dp->v); |
413 |
> |
/* tan(t) */ |
414 |
> |
mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0); |
415 |
|
dp += hp->np; |
416 |
|
} |
417 |
|
phi = 2.0*PI * (j+.5)/hp->np + PI/2.0; |
418 |
< |
xd += mag * cos(phi); |
419 |
< |
yd += mag * sin(phi); |
418 |
> |
xd += mag * tcos(phi); |
419 |
> |
yd += mag * tsin(phi); |
420 |
|
} |
421 |
|
for (i = 0; i < 3; i++) |
422 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*PI/(hp->nt*hp->np); |
422 |
> |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np); |
423 |
|
} |