35 |
|
RAY *rp; /* originating ray sample */ |
36 |
|
int ns; /* number of samples per axis */ |
37 |
|
int sampOK; /* acquired full sample set? */ |
38 |
+ |
int atyp; /* RAMBIENT or TAMBIENT */ |
39 |
|
SCOLOR acoef; /* division contribution coefficient */ |
40 |
|
SCOLOR acol; /* accumulated color */ |
41 |
+ |
FVECT onrm; /* oriented unperturbed surface normal */ |
42 |
|
FVECT ux, uy; /* tangent axis unit vectors */ |
43 |
|
AMBSAMP sa[1]; /* sample array (extends struct) */ |
44 |
|
} AMBHEMI; /* ambient sample hemisphere */ |
64 |
|
int ii, jj; |
65 |
|
/* min. spacing = 1/4th division */ |
66 |
|
cos_thresh = (PI/4.)/(double)hp->ns; |
67 |
+ |
if (cos_thresh > 7.*PI/180.) /* 7 degrees is enough in any case */ |
68 |
+ |
cos_thresh = 7.*PI/180.; |
69 |
|
cos_thresh = 1. - .5*cos_thresh*cos_thresh; |
70 |
|
/* check existing neighbors */ |
71 |
|
for (ii = i-1; ii <= i+1; ii++) { |
102 |
|
AMBSAMP *ap = &ambsam(hp,i,j); |
103 |
|
RAY ar; |
104 |
|
int hlist[3], ii; |
105 |
+ |
double ss[2]; |
106 |
|
RREAL spt[2]; |
107 |
|
double zd; |
108 |
|
/* generate hemispherical sample */ |
111 |
|
setscolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
112 |
|
else |
113 |
|
copyscolor(ar.rcoef, hp->acoef); |
114 |
< |
if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) |
114 |
> |
if (rayorigin(&ar, hp->atyp, hp->rp, ar.rcoef) < 0) |
115 |
|
return(0); |
116 |
|
if (ambacc > FTINY) { |
117 |
|
smultscolor(ar.rcoef, hp->acoef); |
118 |
|
scalescolor(ar.rcoef, 1./AVGREFL); |
119 |
|
} |
120 |
|
hlist[0] = hp->rp->rno; |
121 |
< |
hlist[1] = j; |
122 |
< |
hlist[2] = i; |
123 |
< |
multisamp(spt, 2, urand(ilhash(hlist,3)+n)); |
121 |
> |
hlist[1] = AI(hp,i,j); |
122 |
> |
hlist[2] = samplendx; |
123 |
> |
multisamp(ss, 2, urand(ilhash(hlist,3)+n)); |
124 |
|
resample: |
125 |
< |
square2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns); |
125 |
> |
square2disk(spt, (j+ss[1])/hp->ns, (i+ss[0])/hp->ns); |
126 |
|
zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]); |
127 |
|
for (ii = 3; ii--; ) |
128 |
|
ar.rdir[ii] = spt[0]*hp->ux[ii] + |
129 |
|
spt[1]*hp->uy[ii] + |
130 |
< |
zd*hp->rp->ron[ii]; |
130 |
> |
zd*hp->onrm[ii]; |
131 |
|
checknorm(ar.rdir); |
132 |
< |
/* avoid coincident samples */ |
133 |
< |
if (!n && ambcollision(hp, i, j, ar.rdir)) { |
134 |
< |
spt[0] = frandom(); spt[1] = frandom(); |
132 |
> |
/* avoid coincident samples? */ |
133 |
> |
if (!n & (ambacc > FTINY) & (hp->ns >= 4) && |
134 |
> |
ambcollision(hp, i, j, ar.rdir)) { |
135 |
> |
ss[0] = frandom(); ss[1] = frandom(); |
136 |
|
goto resample; /* reject this sample */ |
137 |
|
} |
138 |
|
dimlist[ndims++] = AI(hp,i,j) + 90171; |
166 |
|
static float * |
167 |
|
getambdiffs(AMBHEMI *hp) |
168 |
|
{ |
169 |
< |
const double normf = 1./bright(hp->acoef); |
170 |
< |
float *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float)); |
169 |
> |
const double normf = 1./(pbright(hp->acoef) + FTINY); |
170 |
> |
float *earr = (float *)calloc(2*hp->ns*hp->ns, sizeof(float)); |
171 |
|
float *ep; |
172 |
|
AMBSAMP *ap; |
173 |
|
double b, b1, d2; |
176 |
|
if (earr == NULL) /* out of memory? */ |
177 |
|
return(NULL); |
178 |
|
/* sum squared neighbor diffs */ |
179 |
< |
for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++) |
179 |
> |
ap = hp->sa; |
180 |
> |
ep = earr + hp->ns*hp->ns; /* original estimates to scratch */ |
181 |
> |
for (i = 0; i < hp->ns; i++) |
182 |
|
for (j = 0; j < hp->ns; j++, ap++, ep++) { |
183 |
|
b = pbright(ap[0].v); |
184 |
|
if (i) { /* from above */ |
204 |
|
ep[-hp->ns-1] += d2; |
205 |
|
} |
206 |
|
/* correct for number of neighbors */ |
207 |
< |
earr[0] *= 8./3.; |
208 |
< |
earr[hp->ns-1] *= 8./3.; |
209 |
< |
earr[(hp->ns-1)*hp->ns] *= 8./3.; |
210 |
< |
earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.; |
207 |
> |
ep = earr + hp->ns*hp->ns; |
208 |
> |
ep[0] *= 6./3.; |
209 |
> |
ep[hp->ns-1] *= 6./3.; |
210 |
> |
ep[(hp->ns-1)*hp->ns] *= 6./3.; |
211 |
> |
ep[(hp->ns-1)*hp->ns + hp->ns-1] *= 6./3.; |
212 |
|
for (i = 1; i < hp->ns-1; i++) { |
213 |
< |
earr[i*hp->ns] *= 8./5.; |
214 |
< |
earr[i*hp->ns + hp->ns-1] *= 8./5.; |
213 |
> |
ep[i*hp->ns] *= 6./5.; |
214 |
> |
ep[i*hp->ns + hp->ns-1] *= 6./5.; |
215 |
|
} |
216 |
|
for (j = 1; j < hp->ns-1; j++) { |
217 |
< |
earr[j] *= 8./5.; |
218 |
< |
earr[(hp->ns-1)*hp->ns + j] *= 8./5.; |
217 |
> |
ep[j] *= 6./5.; |
218 |
> |
ep[(hp->ns-1)*hp->ns + j] *= 6./5.; |
219 |
|
} |
220 |
+ |
/* blur final map to reduce bias */ |
221 |
+ |
for (i = 0; i < hp->ns-1; i++) { |
222 |
+ |
float *ep2; |
223 |
+ |
ep = earr + i*hp->ns; |
224 |
+ |
ep2 = ep + hp->ns*hp->ns; |
225 |
+ |
for (j = 0; j < hp->ns-1; j++, ep++, ep2++) { |
226 |
+ |
ep[0] += .5*ep2[0] + .125*(ep2[1] + ep2[hp->ns]); |
227 |
+ |
ep[1] += .125*ep2[0]; |
228 |
+ |
ep[hp->ns] += .125*ep2[0]; |
229 |
+ |
} |
230 |
+ |
} |
231 |
|
return(earr); |
232 |
|
} |
233 |
|
|
268 |
|
double wt |
269 |
|
) |
270 |
|
{ |
271 |
+ |
int backside = (wt < 0); |
272 |
|
AMBHEMI *hp; |
273 |
|
double d; |
274 |
|
int n, i, j; |
277 |
|
if (d <= FTINY) |
278 |
|
return(NULL); |
279 |
|
/* set number of divisions */ |
280 |
+ |
if (backside) wt = -wt; |
281 |
|
if (ambacc <= FTINY && |
282 |
< |
wt > (d *= 0.8*r->rweight/(ambdiv*minweight))) |
282 |
> |
wt > (d *= 0.8*r->rweight/(ambdiv*minweight + 1e-20))) |
283 |
|
wt = d; /* avoid ray termination */ |
284 |
|
n = sqrt(ambdiv * wt) + 0.5; |
285 |
|
i = 1 + (MINADIV-1)*(ambacc > FTINY); |
289 |
|
hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1)); |
290 |
|
if (hp == NULL) |
291 |
|
error(SYSTEM, "out of memory in samp_hemi"); |
292 |
+ |
|
293 |
+ |
if (backside) { |
294 |
+ |
hp->atyp = TAMBIENT; |
295 |
+ |
hp->onrm[0] = -r->ron[0]; |
296 |
+ |
hp->onrm[1] = -r->ron[1]; |
297 |
+ |
hp->onrm[2] = -r->ron[2]; |
298 |
+ |
} else { |
299 |
+ |
hp->atyp = RAMBIENT; |
300 |
+ |
VCOPY(hp->onrm, r->ron); |
301 |
+ |
} |
302 |
|
hp->rp = r; |
303 |
|
hp->ns = n; |
304 |
|
scolorblack(hp->acol); |
309 |
|
d = 1.0/(n*n); |
310 |
|
scalescolor(hp->acoef, d); |
311 |
|
/* make tangent plane axes */ |
312 |
< |
if (!getperpendicular(hp->ux, r->ron, 1)) |
312 |
> |
if (!getperpendicular(hp->ux, hp->onrm, 1)) |
313 |
|
error(CONSISTENCY, "bad ray direction in samp_hemi"); |
314 |
< |
VCROSS(hp->uy, r->ron, hp->ux); |
314 |
> |
VCROSS(hp->uy, hp->onrm, hp->ux); |
315 |
|
/* sample divisions */ |
316 |
|
for (i = hp->ns; i--; ) |
317 |
|
for (j = hp->ns; j--; ) |
328 |
|
if (hp->sampOK <= MINADIV*MINADIV) |
329 |
|
return(hp); /* don't bother super-sampling */ |
330 |
|
n = ambssamp*wt + 0.5; |
331 |
< |
if (n > 8) { /* perform super-sampling? */ |
331 |
> |
if (n >= 4*hp->ns) { /* perform super-sampling? */ |
332 |
|
ambsupersamp(hp, n); |
333 |
|
copyscolor(rcol, hp->acol); |
334 |
|
} |
576 |
|
for (j = 0; j < hp->ns-1; j++) { |
577 |
|
comp_fftri(&fftr, hp, AI(hp,0,j), AI(hp,0,j+1)); |
578 |
|
if (hessrow != NULL) |
579 |
< |
comp_hessian(hessrow[j], &fftr, hp->rp->ron); |
579 |
> |
comp_hessian(hessrow[j], &fftr, hp->onrm); |
580 |
|
if (gradrow != NULL) |
581 |
< |
comp_gradient(gradrow[j], &fftr, hp->rp->ron); |
581 |
> |
comp_gradient(gradrow[j], &fftr, hp->onrm); |
582 |
|
} |
583 |
|
/* sum each row of triangles */ |
584 |
|
for (i = 0; i < hp->ns-1; i++) { |
586 |
|
FVECT gradcol; |
587 |
|
comp_fftri(&fftr, hp, AI(hp,i,0), AI(hp,i+1,0)); |
588 |
|
if (hessrow != NULL) |
589 |
< |
comp_hessian(hesscol, &fftr, hp->rp->ron); |
589 |
> |
comp_hessian(hesscol, &fftr, hp->onrm); |
590 |
|
if (gradrow != NULL) |
591 |
< |
comp_gradient(gradcol, &fftr, hp->rp->ron); |
591 |
> |
comp_gradient(gradcol, &fftr, hp->onrm); |
592 |
|
for (j = 0; j < hp->ns-1; j++) { |
593 |
|
FVECT hessdia[3]; /* compute triangle contributions */ |
594 |
|
FVECT graddia; |
598 |
|
/* diagonal (inner) edge */ |
599 |
|
comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j)); |
600 |
|
if (hessrow != NULL) { |
601 |
< |
comp_hessian(hessdia, &fftr, hp->rp->ron); |
601 |
> |
comp_hessian(hessdia, &fftr, hp->onrm); |
602 |
|
rev_hessian(hesscol); |
603 |
|
add2hessian(hessian, hessrow[j], hessdia, hesscol, backg); |
604 |
|
} |
605 |
|
if (gradrow != NULL) { |
606 |
< |
comp_gradient(graddia, &fftr, hp->rp->ron); |
606 |
> |
comp_gradient(graddia, &fftr, hp->onrm); |
607 |
|
rev_gradient(gradcol); |
608 |
|
add2gradient(gradient, gradrow[j], graddia, gradcol, backg); |
609 |
|
} |
610 |
|
/* initialize edge in next row */ |
611 |
|
comp_fftri(&fftr, hp, AI(hp,i+1,j+1), AI(hp,i+1,j)); |
612 |
|
if (hessrow != NULL) |
613 |
< |
comp_hessian(hessrow[j], &fftr, hp->rp->ron); |
613 |
> |
comp_hessian(hessrow[j], &fftr, hp->onrm); |
614 |
|
if (gradrow != NULL) |
615 |
< |
comp_gradient(gradrow[j], &fftr, hp->rp->ron); |
615 |
> |
comp_gradient(gradrow[j], &fftr, hp->onrm); |
616 |
|
/* new column edge & paired triangle */ |
617 |
|
backg = back_ambval(hp, AI(hp,i+1,j+1), |
618 |
|
AI(hp,i+1,j), AI(hp,i,j+1)); |
619 |
|
comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j+1)); |
620 |
|
if (hessrow != NULL) { |
621 |
< |
comp_hessian(hesscol, &fftr, hp->rp->ron); |
621 |
> |
comp_hessian(hesscol, &fftr, hp->onrm); |
622 |
|
rev_hessian(hessdia); |
623 |
|
add2hessian(hessian, hessrow[j], hessdia, hesscol, backg); |
624 |
|
if (i < hp->ns-2) |
625 |
|
rev_hessian(hessrow[j]); |
626 |
|
} |
627 |
|
if (gradrow != NULL) { |
628 |
< |
comp_gradient(gradcol, &fftr, hp->rp->ron); |
628 |
> |
comp_gradient(gradcol, &fftr, hp->onrm); |
629 |
|
rev_gradient(graddia); |
630 |
|
add2gradient(gradient, gradrow[j], graddia, gradcol, backg); |
631 |
|
if (i < hp->ns-2) |
661 |
|
/* use vector for azimuth + 90deg */ |
662 |
|
VSUB(vd, ap->p, hp->rp->rop); |
663 |
|
/* brightness over cosine factor */ |
664 |
< |
gfact = ap->v[0] / DOT(hp->rp->ron, vd); |
664 |
> |
gfact = ap->v[0] / DOT(hp->onrm, vd); |
665 |
|
/* sine = proj_radius/vd_length */ |
666 |
|
dgsum[0] -= DOT(uv[1], vd) * gfact; |
667 |
|
dgsum[1] += DOT(uv[0], vd) * gfact; |
719 |
|
doambient( /* compute ambient component */ |
720 |
|
SCOLOR rcol, /* input/output color */ |
721 |
|
RAY *r, |
722 |
< |
double wt, |
722 |
> |
double wt, /* negative for back side */ |
723 |
|
FVECT uv[2], /* returned (optional) */ |
724 |
|
float ra[2], /* returned (optional) */ |
725 |
|
float pg[2], /* returned (optional) */ |
786 |
|
if (ra[1] < minarad) |
787 |
|
ra[1] = minarad; |
788 |
|
} |
789 |
< |
ra[0] *= d = 1.0/sqrt(wt); |
789 |
> |
ra[0] *= d = 1.0/sqrt(fabs(wt)); |
790 |
|
if ((ra[1] *= d) > 2.0*ra[0]) |
791 |
|
ra[1] = 2.0*ra[0]; |
792 |
|
if (ra[1] > maxarad) { |