1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ambcomp.c,v 2.26 2014/04/16 20:32:00 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Routines to compute "ambient" values using Monte Carlo |
6 |
* |
7 |
* Hessian calculations based on "Practical Hessian-Based Error Control |
8 |
* for Irradiance Caching" by Schwarzhaupt, Wann Jensen, & Jarosz |
9 |
* from ACM SIGGRAPH Asia 2012 conference proceedings. |
10 |
* |
11 |
* Declarations of external symbols in ambient.h |
12 |
*/ |
13 |
|
14 |
#include "copyright.h" |
15 |
|
16 |
#include "ray.h" |
17 |
#include "ambient.h" |
18 |
#include "random.h" |
19 |
|
20 |
#ifdef NEWAMB |
21 |
|
22 |
extern void SDsquare2disk(double ds[2], double seedx, double seedy); |
23 |
|
24 |
typedef struct { |
25 |
RAY *rp; /* originating ray sample */ |
26 |
FVECT ux, uy; /* tangent axis unit vectors */ |
27 |
int ns; /* number of samples per axis */ |
28 |
COLOR acoef; /* division contribution coefficient */ |
29 |
struct s_ambsamp { |
30 |
COLOR v; /* hemisphere sample value */ |
31 |
float p[3]; /* intersection point */ |
32 |
} sa[1]; /* sample array (extends struct) */ |
33 |
} AMBHEMI; /* ambient sample hemisphere */ |
34 |
|
35 |
#define ambsamp(h,i,j) (h)->sa[(i)*(h)->ns + (j)] |
36 |
|
37 |
typedef struct { |
38 |
FVECT r_i, r_i1, e_i; |
39 |
double nf, I1, I2, J2; |
40 |
} FFTRI; /* vectors and coefficients for Hessian calculation */ |
41 |
|
42 |
|
43 |
static AMBHEMI * |
44 |
inithemi( /* initialize sampling hemisphere */ |
45 |
COLOR ac, |
46 |
RAY *r, |
47 |
double wt |
48 |
) |
49 |
{ |
50 |
AMBHEMI *hp; |
51 |
double d; |
52 |
int n, i; |
53 |
/* set number of divisions */ |
54 |
if (ambacc <= FTINY && |
55 |
wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight))) |
56 |
wt = d; /* avoid ray termination */ |
57 |
n = sqrt(ambdiv * wt) + 0.5; |
58 |
i = 1 + 5*(ambacc > FTINY); /* minimum number of samples */ |
59 |
if (n < i) |
60 |
n = i; |
61 |
/* allocate sampling array */ |
62 |
hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + |
63 |
sizeof(struct s_ambsamp)*(n*n - 1)); |
64 |
if (hp == NULL) |
65 |
return(NULL); |
66 |
hp->rp = r; |
67 |
hp->ns = n; |
68 |
/* assign coefficient */ |
69 |
copycolor(hp->acoef, ac); |
70 |
d = 1.0/(n*n); |
71 |
scalecolor(hp->acoef, d); |
72 |
/* make tangent axes */ |
73 |
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
74 |
for (i = 0; i < 3; i++) |
75 |
if (r->ron[i] < 0.6 && r->ron[i] > -0.6) |
76 |
break; |
77 |
if (i >= 3) |
78 |
error(CONSISTENCY, "bad ray direction in inithemi()"); |
79 |
hp->uy[i] = 1.0; |
80 |
VCROSS(hp->ux, hp->uy, r->ron); |
81 |
normalize(hp->ux); |
82 |
VCROSS(hp->uy, r->ron, hp->ux); |
83 |
/* we're ready to sample */ |
84 |
return(hp); |
85 |
} |
86 |
|
87 |
|
88 |
static int |
89 |
ambsample( /* sample an ambient direction */ |
90 |
AMBHEMI *hp, |
91 |
int i, |
92 |
int j |
93 |
) |
94 |
{ |
95 |
struct s_ambsamp *ap = &ambsamp(hp,i,j); |
96 |
RAY ar; |
97 |
int hlist[3]; |
98 |
double spt[2], zd; |
99 |
int ii; |
100 |
/* ambient coefficient for weight */ |
101 |
if (ambacc > FTINY) |
102 |
setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
103 |
else |
104 |
copycolor(ar.rcoef, hp->acoef); |
105 |
if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) { |
106 |
setcolor(ap->v, 0., 0., 0.); |
107 |
VCOPY(ap->p, hp->rp->rop); |
108 |
return(0); /* no sample taken */ |
109 |
} |
110 |
if (ambacc > FTINY) { |
111 |
multcolor(ar.rcoef, hp->acoef); |
112 |
scalecolor(ar.rcoef, 1./AVGREFL); |
113 |
} |
114 |
/* generate hemispherical sample */ |
115 |
SDsquare2disk(spt, (i+.1+.8*frandom())/hp->ns, |
116 |
(j+.1+.8*frandom())/hp->ns); |
117 |
zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]); |
118 |
for (ii = 3; ii--; ) |
119 |
ar.rdir[ii] = spt[0]*hp->ux[ii] + |
120 |
spt[1]*hp->uy[ii] + |
121 |
zd*hp->rp->ron[ii]; |
122 |
checknorm(ar.rdir); |
123 |
dimlist[ndims++] = i*hp->ns + j + 90171; |
124 |
rayvalue(&ar); /* evaluate ray */ |
125 |
ndims--; |
126 |
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
127 |
copycolor(ap->v, ar.rcol); |
128 |
if (ar.rt > 20.0*maxarad) /* limit vertex distance */ |
129 |
ar.rt = 20.0*maxarad; |
130 |
VSUM(ap->p, ar.rorg, ar.rdir, ar.rt); |
131 |
return(1); |
132 |
} |
133 |
|
134 |
|
135 |
/* Compute vectors and coefficients for Hessian/gradient calcs */ |
136 |
static void |
137 |
comp_fftri(FFTRI *ftp, float ap0[3], float ap1[3], FVECT rop) |
138 |
{ |
139 |
FVECT v1; |
140 |
double dot_e, dot_er, dot_r, dot_r1; |
141 |
|
142 |
VSUB(ftp->r_i, ap0, rop); |
143 |
VSUB(ftp->r_i1, ap1, rop); |
144 |
VSUB(ftp->e_i, ap1, ap0); |
145 |
VCROSS(v1, ftp->e_i, ftp->r_i); |
146 |
ftp->nf = 1.0/DOT(v1,v1); |
147 |
VCROSS(v1, ftp->r_i, ftp->r_i1); |
148 |
ftp->I1 = sqrt(DOT(v1,v1)*ftp->nf); |
149 |
dot_e = DOT(ftp->e_i,ftp->e_i); |
150 |
dot_er = DOT(ftp->e_i, ftp->r_i); |
151 |
dot_r = DOT(ftp->r_i,ftp->r_i); |
152 |
dot_r1 = DOT(ftp->r_i1,ftp->r_i1); |
153 |
ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)/dot_r1 - dot_er/dot_r + |
154 |
dot_e*ftp->I1 )*0.5*ftp->nf; |
155 |
ftp->J2 = 0.25*ftp->nf*( 1.0/dot_r - 1.0/dot_r1 ) - |
156 |
dot_er/dot_e*ftp->I2; |
157 |
} |
158 |
|
159 |
|
160 |
/* Compose matrix from two vectors */ |
161 |
static void |
162 |
compose_matrix(FVECT mat[3], FVECT va, FVECT vb) |
163 |
{ |
164 |
mat[0][0] = 2.0*va[0]*vb[0]; |
165 |
mat[1][1] = 2.0*va[1]*vb[1]; |
166 |
mat[2][2] = 2.0*va[2]*vb[2]; |
167 |
mat[0][1] = mat[1][0] = va[0]*vb[1] + va[1]*vb[0]; |
168 |
mat[0][2] = mat[2][0] = va[0]*vb[2] + va[2]*vb[0]; |
169 |
mat[1][2] = mat[2][1] = va[1]*vb[2] + va[2]*vb[1]; |
170 |
} |
171 |
|
172 |
|
173 |
/* Compute partial 3x3 Hessian matrix for edge */ |
174 |
static void |
175 |
comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm) |
176 |
{ |
177 |
FVECT v1, v2; |
178 |
FVECT m1[3], m2[3], m3[3], m4[3]; |
179 |
double d1, d2, d3, d4; |
180 |
double I3, J3, K3; |
181 |
int i, j; |
182 |
/* compute intermediate coefficients */ |
183 |
d1 = 1.0/DOT(ftp->r_i,ftp->r_i); |
184 |
d2 = 1.0/DOT(ftp->r_i1,ftp->r_i1); |
185 |
d3 = 1.0/DOT(ftp->e_i,ftp->e_i); |
186 |
d4 = DOT(ftp->e_i, ftp->r_i); |
187 |
I3 = 0.25*ftp->nf*( DOT(ftp->e_i, ftp->r_i1)*d2*d2 - d4*d1*d1 + |
188 |
3.0*ftp->I2*d3 ); |
189 |
J3 = 0.25*d3*(d1*d1 - d2*d2) - d4*d3*I3; |
190 |
K3 = d3*(ftp->I2 - I3/d1 - 2.0*d4*J3); |
191 |
/* intermediate matrices */ |
192 |
VCROSS(v1, nrm, ftp->e_i); |
193 |
for (j = 3; j--; ) |
194 |
v2[i] = ftp->I2*ftp->r_i[j] + ftp->J2*ftp->e_i[j]; |
195 |
compose_matrix(m1, v1, v2); |
196 |
compose_matrix(m2, ftp->r_i, ftp->r_i); |
197 |
compose_matrix(m3, ftp->e_i, ftp->e_i); |
198 |
compose_matrix(m4, ftp->r_i, ftp->e_i); |
199 |
VCROSS(v1, ftp->r_i, ftp->e_i); |
200 |
d1 = DOT(nrm, v1); |
201 |
d2 = -d1*ftp->I2; |
202 |
d1 *= 2.0; |
203 |
for (i = 3; i--; ) /* final matrix sum */ |
204 |
for (j = 3; j--; ) { |
205 |
hess[i][j] = m1[i][j] + d1*( I3*m2[i][j] + K3*m3[i][j] + |
206 |
2.0*J3*m4[i][j] ); |
207 |
hess[i][j] += d2*(i==j); |
208 |
hess[i][j] *= -1.0/PI; |
209 |
} |
210 |
} |
211 |
|
212 |
|
213 |
/* Reverse hessian calculation result for edge in other direction */ |
214 |
static void |
215 |
rev_hessian(FVECT hess[3]) |
216 |
{ |
217 |
int i; |
218 |
|
219 |
for (i = 3; i--; ) { |
220 |
hess[i][0] = -hess[i][0]; |
221 |
hess[i][1] = -hess[i][1]; |
222 |
hess[i][2] = -hess[i][2]; |
223 |
} |
224 |
} |
225 |
|
226 |
|
227 |
/* Add to radiometric Hessian from the given triangle */ |
228 |
static void |
229 |
add2hessian(FVECT hess[3], FVECT ehess1[3], |
230 |
FVECT ehess2[3], FVECT ehess3[3], COLORV v) |
231 |
{ |
232 |
int i, j; |
233 |
|
234 |
for (i = 3; i--; ) |
235 |
for (j = 3; j--; ) |
236 |
hess[i][j] += v*( ehess1[i][j] + ehess2[i][j] + ehess3[i][j] ); |
237 |
} |
238 |
|
239 |
|
240 |
/* Compute partial displacement form factor gradient for edge */ |
241 |
static void |
242 |
comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm) |
243 |
{ |
244 |
FVECT vcp; |
245 |
double f1; |
246 |
int i; |
247 |
|
248 |
VCROSS(vcp, ftp->r_i, ftp->r_i1); |
249 |
f1 = 2.0*DOT(nrm, vcp); |
250 |
VCROSS(vcp, nrm, ftp->e_i); |
251 |
for (i = 3; i--; ) |
252 |
grad[i] = (0.5/PI)*( ftp->I1*vcp[i] + |
253 |
f1*(ftp->I2*ftp->r_i[i] + ftp->J2*ftp->e_i[i]) ); |
254 |
} |
255 |
|
256 |
|
257 |
/* Reverse gradient calculation result for edge in other direction */ |
258 |
static void |
259 |
rev_gradient(FVECT grad) |
260 |
{ |
261 |
grad[0] = -grad[0]; |
262 |
grad[1] = -grad[1]; |
263 |
grad[2] = -grad[2]; |
264 |
} |
265 |
|
266 |
|
267 |
/* Add to displacement gradient from the given triangle */ |
268 |
static void |
269 |
add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, COLORV v) |
270 |
{ |
271 |
int i; |
272 |
|
273 |
for (i = 3; i--; ) |
274 |
grad[i] += v*( egrad1[i] + egrad2[i] + egrad3[i] ); |
275 |
} |
276 |
|
277 |
|
278 |
/* Return brightness of furthest ambient sample */ |
279 |
static COLORV |
280 |
back_ambval(struct s_ambsamp *ap1, struct s_ambsamp *ap2, |
281 |
struct s_ambsamp *ap3, FVECT orig) |
282 |
{ |
283 |
COLORV vback; |
284 |
FVECT vec; |
285 |
double d2, d2best; |
286 |
|
287 |
VSUB(vec, ap1->p, orig); |
288 |
d2best = DOT(vec,vec); |
289 |
vback = ap1->v[CIEY]; |
290 |
VSUB(vec, ap2->p, orig); |
291 |
d2 = DOT(vec,vec); |
292 |
if (d2 > d2best) { |
293 |
d2best = d2; |
294 |
vback = ap2->v[CIEY]; |
295 |
} |
296 |
VSUB(vec, ap3->p, orig); |
297 |
d2 = DOT(vec,vec); |
298 |
if (d2 > d2best) |
299 |
return(ap3->v[CIEY]); |
300 |
return(vback); |
301 |
} |
302 |
|
303 |
|
304 |
/* Compute anisotropic radii and eigenvector directions */ |
305 |
static int |
306 |
eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3]) |
307 |
{ |
308 |
double hess2[2][2]; |
309 |
FVECT a, b; |
310 |
double evalue[2], slope1, xmag1; |
311 |
int i; |
312 |
/* project Hessian to sample plane */ |
313 |
for (i = 3; i--; ) { |
314 |
a[i] = DOT(hessian[i], uv[0]); |
315 |
b[i] = DOT(hessian[i], uv[1]); |
316 |
} |
317 |
hess2[0][0] = DOT(uv[0], a); |
318 |
hess2[0][1] = DOT(uv[0], b); |
319 |
hess2[1][0] = DOT(uv[1], a); |
320 |
hess2[1][1] = DOT(uv[1], b); |
321 |
/* compute eigenvalues */ |
322 |
if (quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1], |
323 |
hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]) != 2 || |
324 |
(evalue[0] = fabs(evalue[0])) <= FTINY*FTINY*FTINY || |
325 |
(evalue[1] = fabs(evalue[1])) <= FTINY*FTINY*FTINY) |
326 |
error(INTERNAL, "bad eigenvalue calculation"); |
327 |
|
328 |
if (evalue[0] > evalue[1]) { |
329 |
ra[0] = 1.0/sqrt(sqrt(evalue[0])); |
330 |
ra[1] = 1.0/sqrt(sqrt(evalue[1])); |
331 |
slope1 = evalue[1]; |
332 |
} else { |
333 |
ra[0] = 1.0/sqrt(sqrt(evalue[1])); |
334 |
ra[1] = 1.0/sqrt(sqrt(evalue[0])); |
335 |
slope1 = evalue[0]; |
336 |
} |
337 |
/* compute unit eigenvectors */ |
338 |
if (fabs(hess2[0][1]) <= FTINY) |
339 |
return; /* uv OK as is */ |
340 |
slope1 = (slope1 - hess2[0][0]) / hess2[0][1]; |
341 |
xmag1 = sqrt(1.0/(1.0 + slope1*slope1)); |
342 |
for (i = 3; i--; ) { |
343 |
b[i] = xmag1*uv[0][i] + slope1*xmag1*uv[1][i]; |
344 |
a[i] = slope1*xmag1*uv[0][i] - xmag1*uv[1][i]; |
345 |
} |
346 |
VCOPY(uv[0], a); |
347 |
VCOPY(uv[1], b); |
348 |
} |
349 |
|
350 |
|
351 |
static void |
352 |
ambHessian( /* anisotropic radii & pos. gradient */ |
353 |
AMBHEMI *hp, |
354 |
FVECT uv[2], /* returned */ |
355 |
float ra[2], /* returned */ |
356 |
float pg[2] /* returned */ |
357 |
) |
358 |
{ |
359 |
static char memerrmsg[] = "out of memory in ambHessian()"; |
360 |
FVECT (*hessrow)[3] = NULL; |
361 |
FVECT *gradrow = NULL; |
362 |
FVECT hessian[3]; |
363 |
FVECT gradient; |
364 |
FFTRI fftr; |
365 |
int i, j; |
366 |
/* be sure to assign unit vectors */ |
367 |
VCOPY(uv[0], hp->ux); |
368 |
VCOPY(uv[1], hp->uy); |
369 |
/* clock-wise vertex traversal from sample POV */ |
370 |
if (ra != NULL) { /* initialize Hessian row buffer */ |
371 |
hessrow = (FVECT (*)[3])malloc(sizeof(FVECT)*3*hp->ns); |
372 |
if (hessrow == NULL) |
373 |
error(SYSTEM, memerrmsg); |
374 |
memset(hessian, 0, sizeof(hessian)); |
375 |
} else if (pg == NULL) /* bogus call? */ |
376 |
return; |
377 |
if (pg != NULL) { /* initialize form factor row buffer */ |
378 |
gradrow = (FVECT *)malloc(sizeof(FVECT)*hp->ns); |
379 |
if (gradrow == NULL) |
380 |
error(SYSTEM, memerrmsg); |
381 |
memset(gradient, 0, sizeof(gradient)); |
382 |
} |
383 |
/* compute first row of edges */ |
384 |
for (j = 0; j < hp->ns-1; j++) { |
385 |
comp_fftri(&fftr, ambsamp(hp,0,j).p, |
386 |
ambsamp(hp,0,j+1).p, hp->rp->rop); |
387 |
if (hessrow != NULL) |
388 |
comp_hessian(hessrow[j], &fftr, hp->rp->ron); |
389 |
if (gradrow != NULL) |
390 |
comp_gradient(gradrow[j], &fftr, hp->rp->ron); |
391 |
} |
392 |
/* sum each row of triangles */ |
393 |
for (i = 0; i < hp->ns-1; i++) { |
394 |
FVECT hesscol[3]; /* compute first vertical edge */ |
395 |
FVECT gradcol; |
396 |
comp_fftri(&fftr, ambsamp(hp,i,0).p, |
397 |
ambsamp(hp,i+1,0).p, hp->rp->rop); |
398 |
if (hessrow != NULL) |
399 |
comp_hessian(hesscol, &fftr, hp->rp->ron); |
400 |
if (gradrow != NULL) |
401 |
comp_gradient(gradcol, &fftr, hp->rp->ron); |
402 |
for (j = 0; j < hp->ns-1; j++) { |
403 |
FVECT hessdia[3]; /* compute triangle contributions */ |
404 |
FVECT graddia; |
405 |
COLORV backg; |
406 |
backg = back_ambval(&ambsamp(hp,i,j), &ambsamp(hp,i,j+1), |
407 |
&ambsamp(hp,i+1,j), hp->rp->rop); |
408 |
/* diagonal (inner) edge */ |
409 |
comp_fftri(&fftr, ambsamp(hp,i,j+1).p, |
410 |
ambsamp(hp,i+1,j).p, hp->rp->rop); |
411 |
if (hessrow != NULL) { |
412 |
comp_hessian(hessdia, &fftr, hp->rp->ron); |
413 |
rev_hessian(hesscol); |
414 |
add2hessian(hessian, hessrow[j], hessdia, hesscol, backg); |
415 |
} |
416 |
if (gradient != NULL) { |
417 |
comp_gradient(graddia, &fftr, hp->rp->ron); |
418 |
rev_gradient(gradcol); |
419 |
add2gradient(gradient, gradrow[j], graddia, gradcol, backg); |
420 |
} |
421 |
/* initialize edge in next row */ |
422 |
comp_fftri(&fftr, ambsamp(hp,i+1,j+1).p, |
423 |
ambsamp(hp,i+1,j).p, hp->rp->rop); |
424 |
if (hessrow != NULL) |
425 |
comp_hessian(hessrow[j], &fftr, hp->rp->ron); |
426 |
if (gradrow != NULL) |
427 |
comp_gradient(gradrow[j], &fftr, hp->rp->ron); |
428 |
/* new column edge & paired triangle */ |
429 |
backg = back_ambval(&ambsamp(hp,i,j+1), &ambsamp(hp,i+1,j+1), |
430 |
&ambsamp(hp,i+1,j), hp->rp->rop); |
431 |
comp_fftri(&fftr, ambsamp(hp,i,j+1).p, ambsamp(hp,i+1,j+1).p, |
432 |
hp->rp->rop); |
433 |
if (hessrow != NULL) { |
434 |
comp_hessian(hesscol, &fftr, hp->rp->ron); |
435 |
rev_hessian(hessdia); |
436 |
add2hessian(hessian, hessrow[j], hessdia, hesscol, backg); |
437 |
if (i < hp->ns-2) |
438 |
rev_hessian(hessrow[j]); |
439 |
} |
440 |
if (gradrow != NULL) { |
441 |
comp_gradient(gradcol, &fftr, hp->rp->ron); |
442 |
rev_gradient(graddia); |
443 |
add2gradient(gradient, gradrow[j], graddia, gradcol, backg); |
444 |
if (i < hp->ns-2) |
445 |
rev_gradient(gradrow[j]); |
446 |
} |
447 |
} |
448 |
} |
449 |
/* release row buffers */ |
450 |
if (hessrow != NULL) free(hessrow); |
451 |
if (gradrow != NULL) free(gradrow); |
452 |
|
453 |
if (ra != NULL) /* extract eigenvectors & radii */ |
454 |
eigenvectors(uv, ra, hessian); |
455 |
if (pg != NULL) { /* project position gradient */ |
456 |
pg[0] = DOT(gradient, uv[0]); |
457 |
pg[1] = DOT(gradient, uv[1]); |
458 |
} |
459 |
} |
460 |
|
461 |
|
462 |
/* Compute direction gradient from a hemispherical sampling */ |
463 |
static void |
464 |
ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2]) |
465 |
{ |
466 |
struct s_ambsamp *ap; |
467 |
int n; |
468 |
|
469 |
dg[0] = dg[1] = 0; |
470 |
for (ap = hp->sa, n = hp->ns*hp->ns; n--; ap++) { |
471 |
FVECT vd; |
472 |
double gfact; |
473 |
/* use vector for azimuth + 90deg */ |
474 |
VSUB(vd, ap->p, hp->rp->rop); |
475 |
/* brightness with tangent factor */ |
476 |
gfact = ap->v[CIEY] / DOT(hp->rp->ron, vd); |
477 |
/* sine = proj_radius/vd_length */ |
478 |
dg[0] -= DOT(uv[1], vd) * gfact ; |
479 |
dg[1] += DOT(uv[0], vd) * gfact; |
480 |
} |
481 |
} |
482 |
|
483 |
|
484 |
int |
485 |
doambient( /* compute ambient component */ |
486 |
COLOR rcol, /* input/output color */ |
487 |
RAY *r, |
488 |
double wt, |
489 |
FVECT uv[2], /* returned (optional) */ |
490 |
float ra[2], /* returned (optional) */ |
491 |
float pg[2], /* returned (optional) */ |
492 |
float dg[2] /* returned (optional) */ |
493 |
) |
494 |
{ |
495 |
int cnt = 0; |
496 |
FVECT my_uv[2]; |
497 |
AMBHEMI *hp; |
498 |
double d, acol[3]; |
499 |
struct s_ambsamp *ap; |
500 |
int i, j; |
501 |
/* initialize */ |
502 |
if ((hp = inithemi(rcol, r, wt)) == NULL) |
503 |
return(0); |
504 |
if (uv != NULL) |
505 |
memset(uv, 0, sizeof(FVECT)*2); |
506 |
if (ra != NULL) |
507 |
ra[0] = ra[1] = 0.0; |
508 |
if (pg != NULL) |
509 |
pg[0] = pg[1] = 0.0; |
510 |
if (dg != NULL) |
511 |
dg[0] = dg[1] = 0.0; |
512 |
/* sample the hemisphere */ |
513 |
acol[0] = acol[1] = acol[2] = 0.0; |
514 |
for (i = hp->ns; i--; ) |
515 |
for (j = hp->ns; j--; ) |
516 |
if (ambsample(hp, i, j)) { |
517 |
ap = &ambsamp(hp,i,j); |
518 |
addcolor(acol, ap->v); |
519 |
++cnt; |
520 |
} |
521 |
if (!cnt) { |
522 |
setcolor(rcol, 0.0, 0.0, 0.0); |
523 |
free(hp); |
524 |
return(0); /* no valid samples */ |
525 |
} |
526 |
d = 1.0 / cnt; /* final indirect irradiance/PI */ |
527 |
acol[0] *= d; acol[1] *= d; acol[2] *= d; |
528 |
copycolor(rcol, acol); |
529 |
if (cnt < hp->ns*hp->ns || /* incomplete sampling? */ |
530 |
(ra == NULL) & (pg == NULL) & (dg == NULL)) { |
531 |
free(hp); |
532 |
return(-1); /* no radius or gradient calc. */ |
533 |
} |
534 |
d = 0.01 * bright(rcol); /* add in 1% before Hessian comp. */ |
535 |
if (d < FTINY) d = FTINY; |
536 |
ap = hp->sa; /* using Y channel from here on... */ |
537 |
for (i = hp->ns*hp->ns; i--; ap++) |
538 |
colval(ap->v,CIEY) = bright(ap->v) + d; |
539 |
|
540 |
if (uv == NULL) /* make sure we have axis pointers */ |
541 |
uv = my_uv; |
542 |
/* compute radii & pos. gradient */ |
543 |
ambHessian(hp, uv, ra, pg); |
544 |
if (dg != NULL) /* compute direction gradient */ |
545 |
ambdirgrad(hp, uv, dg); |
546 |
if (ra != NULL) { /* adjust/clamp radii */ |
547 |
d = sqrt(sqrt((4.0/PI)*bright(rcol)/wt)); |
548 |
if ((ra[0] *= d) > maxarad) |
549 |
ra[0] = maxarad; |
550 |
if ((ra[1] *= d) > 2.0*ra[0]) |
551 |
ra[1] = 2.0*ra[0]; |
552 |
} |
553 |
free(hp); /* clean up and return */ |
554 |
return(1); |
555 |
} |
556 |
|
557 |
|
558 |
#else /* ! NEWAMB */ |
559 |
|
560 |
|
561 |
void |
562 |
inithemi( /* initialize sampling hemisphere */ |
563 |
AMBHEMI *hp, |
564 |
COLOR ac, |
565 |
RAY *r, |
566 |
double wt |
567 |
) |
568 |
{ |
569 |
double d; |
570 |
int i; |
571 |
/* set number of divisions */ |
572 |
if (ambacc <= FTINY && |
573 |
wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight))) |
574 |
wt = d; /* avoid ray termination */ |
575 |
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
576 |
i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ |
577 |
if (hp->nt < i) |
578 |
hp->nt = i; |
579 |
hp->np = PI * hp->nt + 0.5; |
580 |
/* set number of super-samples */ |
581 |
hp->ns = ambssamp * wt + 0.5; |
582 |
/* assign coefficient */ |
583 |
copycolor(hp->acoef, ac); |
584 |
d = 1.0/(hp->nt*hp->np); |
585 |
scalecolor(hp->acoef, d); |
586 |
/* make axes */ |
587 |
VCOPY(hp->uz, r->ron); |
588 |
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
589 |
for (i = 0; i < 3; i++) |
590 |
if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) |
591 |
break; |
592 |
if (i >= 3) |
593 |
error(CONSISTENCY, "bad ray direction in inithemi"); |
594 |
hp->uy[i] = 1.0; |
595 |
fcross(hp->ux, hp->uy, hp->uz); |
596 |
normalize(hp->ux); |
597 |
fcross(hp->uy, hp->uz, hp->ux); |
598 |
} |
599 |
|
600 |
|
601 |
int |
602 |
divsample( /* sample a division */ |
603 |
AMBSAMP *dp, |
604 |
AMBHEMI *h, |
605 |
RAY *r |
606 |
) |
607 |
{ |
608 |
RAY ar; |
609 |
int hlist[3]; |
610 |
double spt[2]; |
611 |
double xd, yd, zd; |
612 |
double b2; |
613 |
double phi; |
614 |
int i; |
615 |
/* ambient coefficient for weight */ |
616 |
if (ambacc > FTINY) |
617 |
setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
618 |
else |
619 |
copycolor(ar.rcoef, h->acoef); |
620 |
if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0) |
621 |
return(-1); |
622 |
if (ambacc > FTINY) { |
623 |
multcolor(ar.rcoef, h->acoef); |
624 |
scalecolor(ar.rcoef, 1./AVGREFL); |
625 |
} |
626 |
hlist[0] = r->rno; |
627 |
hlist[1] = dp->t; |
628 |
hlist[2] = dp->p; |
629 |
multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); |
630 |
zd = sqrt((dp->t + spt[0])/h->nt); |
631 |
phi = 2.0*PI * (dp->p + spt[1])/h->np; |
632 |
xd = tcos(phi) * zd; |
633 |
yd = tsin(phi) * zd; |
634 |
zd = sqrt(1.0 - zd*zd); |
635 |
for (i = 0; i < 3; i++) |
636 |
ar.rdir[i] = xd*h->ux[i] + |
637 |
yd*h->uy[i] + |
638 |
zd*h->uz[i]; |
639 |
checknorm(ar.rdir); |
640 |
dimlist[ndims++] = dp->t*h->np + dp->p + 90171; |
641 |
rayvalue(&ar); |
642 |
ndims--; |
643 |
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
644 |
addcolor(dp->v, ar.rcol); |
645 |
/* use rt to improve gradient calc */ |
646 |
if (ar.rt > FTINY && ar.rt < FHUGE) |
647 |
dp->r += 1.0/ar.rt; |
648 |
/* (re)initialize error */ |
649 |
if (dp->n++) { |
650 |
b2 = bright(dp->v)/dp->n - bright(ar.rcol); |
651 |
b2 = b2*b2 + dp->k*((dp->n-1)*(dp->n-1)); |
652 |
dp->k = b2/(dp->n*dp->n); |
653 |
} else |
654 |
dp->k = 0.0; |
655 |
return(0); |
656 |
} |
657 |
|
658 |
|
659 |
static int |
660 |
ambcmp( /* decreasing order */ |
661 |
const void *p1, |
662 |
const void *p2 |
663 |
) |
664 |
{ |
665 |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
666 |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
667 |
|
668 |
if (d1->k < d2->k) |
669 |
return(1); |
670 |
if (d1->k > d2->k) |
671 |
return(-1); |
672 |
return(0); |
673 |
} |
674 |
|
675 |
|
676 |
static int |
677 |
ambnorm( /* standard order */ |
678 |
const void *p1, |
679 |
const void *p2 |
680 |
) |
681 |
{ |
682 |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
683 |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
684 |
int c; |
685 |
|
686 |
if ( (c = d1->t - d2->t) ) |
687 |
return(c); |
688 |
return(d1->p - d2->p); |
689 |
} |
690 |
|
691 |
|
692 |
double |
693 |
doambient( /* compute ambient component */ |
694 |
COLOR rcol, |
695 |
RAY *r, |
696 |
double wt, |
697 |
FVECT pg, |
698 |
FVECT dg |
699 |
) |
700 |
{ |
701 |
double b, d=0; |
702 |
AMBHEMI hemi; |
703 |
AMBSAMP *div; |
704 |
AMBSAMP dnew; |
705 |
double acol[3]; |
706 |
AMBSAMP *dp; |
707 |
double arad; |
708 |
int divcnt; |
709 |
int i, j; |
710 |
/* initialize hemisphere */ |
711 |
inithemi(&hemi, rcol, r, wt); |
712 |
divcnt = hemi.nt * hemi.np; |
713 |
/* initialize */ |
714 |
if (pg != NULL) |
715 |
pg[0] = pg[1] = pg[2] = 0.0; |
716 |
if (dg != NULL) |
717 |
dg[0] = dg[1] = dg[2] = 0.0; |
718 |
setcolor(rcol, 0.0, 0.0, 0.0); |
719 |
if (divcnt == 0) |
720 |
return(0.0); |
721 |
/* allocate super-samples */ |
722 |
if (hemi.ns > 0 || pg != NULL || dg != NULL) { |
723 |
div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP)); |
724 |
if (div == NULL) |
725 |
error(SYSTEM, "out of memory in doambient"); |
726 |
} else |
727 |
div = NULL; |
728 |
/* sample the divisions */ |
729 |
arad = 0.0; |
730 |
acol[0] = acol[1] = acol[2] = 0.0; |
731 |
if ((dp = div) == NULL) |
732 |
dp = &dnew; |
733 |
divcnt = 0; |
734 |
for (i = 0; i < hemi.nt; i++) |
735 |
for (j = 0; j < hemi.np; j++) { |
736 |
dp->t = i; dp->p = j; |
737 |
setcolor(dp->v, 0.0, 0.0, 0.0); |
738 |
dp->r = 0.0; |
739 |
dp->n = 0; |
740 |
if (divsample(dp, &hemi, r) < 0) { |
741 |
if (div != NULL) |
742 |
dp++; |
743 |
continue; |
744 |
} |
745 |
arad += dp->r; |
746 |
divcnt++; |
747 |
if (div != NULL) |
748 |
dp++; |
749 |
else |
750 |
addcolor(acol, dp->v); |
751 |
} |
752 |
if (!divcnt) { |
753 |
if (div != NULL) |
754 |
free((void *)div); |
755 |
return(0.0); /* no samples taken */ |
756 |
} |
757 |
if (divcnt < hemi.nt*hemi.np) { |
758 |
pg = dg = NULL; /* incomplete sampling */ |
759 |
hemi.ns = 0; |
760 |
} else if (arad > FTINY && divcnt/arad < minarad) { |
761 |
hemi.ns = 0; /* close enough */ |
762 |
} else if (hemi.ns > 0) { /* else perform super-sampling? */ |
763 |
comperrs(div, &hemi); /* compute errors */ |
764 |
qsort(div, divcnt, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
765 |
/* super-sample */ |
766 |
for (i = hemi.ns; i > 0; i--) { |
767 |
dnew = *div; |
768 |
if (divsample(&dnew, &hemi, r) < 0) { |
769 |
dp++; |
770 |
continue; |
771 |
} |
772 |
dp = div; /* reinsert */ |
773 |
j = divcnt < i ? divcnt : i; |
774 |
while (--j > 0 && dnew.k < dp[1].k) { |
775 |
*dp = *(dp+1); |
776 |
dp++; |
777 |
} |
778 |
*dp = dnew; |
779 |
} |
780 |
if (pg != NULL || dg != NULL) /* restore order */ |
781 |
qsort(div, divcnt, sizeof(AMBSAMP), ambnorm); |
782 |
} |
783 |
/* compute returned values */ |
784 |
if (div != NULL) { |
785 |
arad = 0.0; /* note: divcnt may be < nt*np */ |
786 |
for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) { |
787 |
arad += dp->r; |
788 |
if (dp->n > 1) { |
789 |
b = 1.0/dp->n; |
790 |
scalecolor(dp->v, b); |
791 |
dp->r *= b; |
792 |
dp->n = 1; |
793 |
} |
794 |
addcolor(acol, dp->v); |
795 |
} |
796 |
b = bright(acol); |
797 |
if (b > FTINY) { |
798 |
b = 1.0/b; /* compute & normalize gradient(s) */ |
799 |
if (pg != NULL) { |
800 |
posgradient(pg, div, &hemi); |
801 |
for (i = 0; i < 3; i++) |
802 |
pg[i] *= b; |
803 |
} |
804 |
if (dg != NULL) { |
805 |
dirgradient(dg, div, &hemi); |
806 |
for (i = 0; i < 3; i++) |
807 |
dg[i] *= b; |
808 |
} |
809 |
} |
810 |
free((void *)div); |
811 |
} |
812 |
copycolor(rcol, acol); |
813 |
if (arad <= FTINY) |
814 |
arad = maxarad; |
815 |
else |
816 |
arad = (divcnt+hemi.ns)/arad; |
817 |
if (pg != NULL) { /* reduce radius if gradient large */ |
818 |
d = DOT(pg,pg); |
819 |
if (d*arad*arad > 1.0) |
820 |
arad = 1.0/sqrt(d); |
821 |
} |
822 |
if (arad < minarad) { |
823 |
arad = minarad; |
824 |
if (pg != NULL && d*arad*arad > 1.0) { /* cap gradient */ |
825 |
d = 1.0/arad/sqrt(d); |
826 |
for (i = 0; i < 3; i++) |
827 |
pg[i] *= d; |
828 |
} |
829 |
} |
830 |
if ((arad /= sqrt(wt)) > maxarad) |
831 |
arad = maxarad; |
832 |
return(arad); |
833 |
} |
834 |
|
835 |
|
836 |
void |
837 |
comperrs( /* compute initial error estimates */ |
838 |
AMBSAMP *da, /* assumes standard ordering */ |
839 |
AMBHEMI *hp |
840 |
) |
841 |
{ |
842 |
double b, b2; |
843 |
int i, j; |
844 |
AMBSAMP *dp; |
845 |
/* sum differences from neighbors */ |
846 |
dp = da; |
847 |
for (i = 0; i < hp->nt; i++) |
848 |
for (j = 0; j < hp->np; j++) { |
849 |
#ifdef DEBUG |
850 |
if (dp->t != i || dp->p != j) |
851 |
error(CONSISTENCY, |
852 |
"division order in comperrs"); |
853 |
#endif |
854 |
b = bright(dp[0].v); |
855 |
if (i > 0) { /* from above */ |
856 |
b2 = bright(dp[-hp->np].v) - b; |
857 |
b2 *= b2 * 0.25; |
858 |
dp[0].k += b2; |
859 |
dp[-hp->np].k += b2; |
860 |
} |
861 |
if (j > 0) { /* from behind */ |
862 |
b2 = bright(dp[-1].v) - b; |
863 |
b2 *= b2 * 0.25; |
864 |
dp[0].k += b2; |
865 |
dp[-1].k += b2; |
866 |
} else { /* around */ |
867 |
b2 = bright(dp[hp->np-1].v) - b; |
868 |
b2 *= b2 * 0.25; |
869 |
dp[0].k += b2; |
870 |
dp[hp->np-1].k += b2; |
871 |
} |
872 |
dp++; |
873 |
} |
874 |
/* divide by number of neighbors */ |
875 |
dp = da; |
876 |
for (j = 0; j < hp->np; j++) /* top row */ |
877 |
(dp++)->k *= 1.0/3.0; |
878 |
if (hp->nt < 2) |
879 |
return; |
880 |
for (i = 1; i < hp->nt-1; i++) /* central region */ |
881 |
for (j = 0; j < hp->np; j++) |
882 |
(dp++)->k *= 0.25; |
883 |
for (j = 0; j < hp->np; j++) /* bottom row */ |
884 |
(dp++)->k *= 1.0/3.0; |
885 |
} |
886 |
|
887 |
|
888 |
void |
889 |
posgradient( /* compute position gradient */ |
890 |
FVECT gv, |
891 |
AMBSAMP *da, /* assumes standard ordering */ |
892 |
AMBHEMI *hp |
893 |
) |
894 |
{ |
895 |
int i, j; |
896 |
double nextsine, lastsine, b, d; |
897 |
double mag0, mag1; |
898 |
double phi, cosp, sinp, xd, yd; |
899 |
AMBSAMP *dp; |
900 |
|
901 |
xd = yd = 0.0; |
902 |
for (j = 0; j < hp->np; j++) { |
903 |
dp = da + j; |
904 |
mag0 = mag1 = 0.0; |
905 |
lastsine = 0.0; |
906 |
for (i = 0; i < hp->nt; i++) { |
907 |
#ifdef DEBUG |
908 |
if (dp->t != i || dp->p != j) |
909 |
error(CONSISTENCY, |
910 |
"division order in posgradient"); |
911 |
#endif |
912 |
b = bright(dp->v); |
913 |
if (i > 0) { |
914 |
d = dp[-hp->np].r; |
915 |
if (dp[0].r > d) d = dp[0].r; |
916 |
/* sin(t)*cos(t)^2 */ |
917 |
d *= lastsine * (1.0 - (double)i/hp->nt); |
918 |
mag0 += d*(b - bright(dp[-hp->np].v)); |
919 |
} |
920 |
nextsine = sqrt((double)(i+1)/hp->nt); |
921 |
if (j > 0) { |
922 |
d = dp[-1].r; |
923 |
if (dp[0].r > d) d = dp[0].r; |
924 |
mag1 += d * (nextsine - lastsine) * |
925 |
(b - bright(dp[-1].v)); |
926 |
} else { |
927 |
d = dp[hp->np-1].r; |
928 |
if (dp[0].r > d) d = dp[0].r; |
929 |
mag1 += d * (nextsine - lastsine) * |
930 |
(b - bright(dp[hp->np-1].v)); |
931 |
} |
932 |
dp += hp->np; |
933 |
lastsine = nextsine; |
934 |
} |
935 |
mag0 *= 2.0*PI / hp->np; |
936 |
phi = 2.0*PI * (double)j/hp->np; |
937 |
cosp = tcos(phi); sinp = tsin(phi); |
938 |
xd += mag0*cosp - mag1*sinp; |
939 |
yd += mag0*sinp + mag1*cosp; |
940 |
} |
941 |
for (i = 0; i < 3; i++) |
942 |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI; |
943 |
} |
944 |
|
945 |
|
946 |
void |
947 |
dirgradient( /* compute direction gradient */ |
948 |
FVECT gv, |
949 |
AMBSAMP *da, /* assumes standard ordering */ |
950 |
AMBHEMI *hp |
951 |
) |
952 |
{ |
953 |
int i, j; |
954 |
double mag; |
955 |
double phi, xd, yd; |
956 |
AMBSAMP *dp; |
957 |
|
958 |
xd = yd = 0.0; |
959 |
for (j = 0; j < hp->np; j++) { |
960 |
dp = da + j; |
961 |
mag = 0.0; |
962 |
for (i = 0; i < hp->nt; i++) { |
963 |
#ifdef DEBUG |
964 |
if (dp->t != i || dp->p != j) |
965 |
error(CONSISTENCY, |
966 |
"division order in dirgradient"); |
967 |
#endif |
968 |
/* tan(t) */ |
969 |
mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0); |
970 |
dp += hp->np; |
971 |
} |
972 |
phi = 2.0*PI * (j+.5)/hp->np + PI/2.0; |
973 |
xd += mag * tcos(phi); |
974 |
yd += mag * tsin(phi); |
975 |
} |
976 |
for (i = 0; i < 3; i++) |
977 |
gv[i] = xd*hp->ux[i] + yd*hp->uy[i]; |
978 |
} |
979 |
|
980 |
#endif /* ! NEWAMB */ |