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