94 |
|
cos_a = DOT(ej->rbfv[0]->invec, ivec); |
95 |
|
if (cos_a <= 0) |
96 |
|
return(0); |
97 |
+ |
if (cos_a >= 1.) /* handles rounding error */ |
98 |
+ |
return(1); |
99 |
|
|
100 |
|
cos_b = DOT(ej->rbfv[1]->invec, ivec); |
101 |
|
if (cos_b <= 0) |
102 |
|
return(0); |
103 |
+ |
if (cos_b >= 1.) |
104 |
+ |
return(1); |
105 |
|
|
106 |
|
cos_aplusb = cos_a*cos_b - sqrt((1.-cos_a*cos_a)*(1.-cos_b*cos_b)); |
107 |
|
if (cos_aplusb <= 0) |
208 |
|
if (single_plane_incident) { /* isotropic BSDF? */ |
209 |
|
RBFNODE *rbf; /* find edge we're on */ |
210 |
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { |
211 |
< |
if (input_orient*rbf->invec[2] < input_orient*invec[2]) |
211 |
> |
if (input_orient*rbf->invec[2] < input_orient*invec[2]-FTINY) |
212 |
|
break; |
213 |
|
if (rbf->next != NULL && input_orient*rbf->next->invec[2] < |
214 |
< |
input_orient*invec[2]) { |
214 |
> |
input_orient*invec[2]+FTINY) { |
215 |
|
for (miga[0] = rbf->ejl; miga[0] != NULL; |
216 |
|
miga[0] = nextedge(rbf,miga[0])) |
217 |
|
if (opp_rbf(rbf,miga[0]) == rbf->next) { |
218 |
< |
double nf = 1. - rbf->invec[2]*rbf->invec[2]; |
218 |
> |
double nf = 1. - |
219 |
> |
rbf->next->invec[2]*rbf->next->invec[2]; |
220 |
|
if (nf > FTINY) { /* rotate to match */ |
221 |
|
nf = sqrt((1.-invec[2]*invec[2])/nf); |
222 |
< |
invec[0] = nf*rbf->invec[0]; |
223 |
< |
invec[1] = nf*rbf->invec[1]; |
222 |
> |
invec[0] = nf*rbf->next->invec[0]; |
223 |
> |
invec[1] = nf*rbf->next->invec[1]; |
224 |
|
} |
225 |
< |
return(0); |
225 |
> |
return(0); /* rotational symmetry */ |
226 |
|
} |
227 |
|
break; |
228 |
|
} |
263 |
|
} |
264 |
|
} |
265 |
|
|
266 |
< |
/* Advect and allocate new RBF along edge */ |
262 |
< |
static RBFNODE * |
263 |
< |
e_advect_rbf(const MIGRATION *mig, const FVECT invec, int lobe_lim) |
264 |
< |
{ |
265 |
< |
double cthresh = FTINY; |
266 |
< |
RBFNODE *rbf; |
267 |
< |
int n, i, j; |
268 |
< |
double t, full_dist; |
269 |
< |
/* get relative position */ |
270 |
< |
t = Acos(DOT(invec, mig->rbfv[0]->invec)); |
271 |
< |
if (t < M_PI/grid_res) { /* near first DSF */ |
272 |
< |
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1); |
273 |
< |
rbf = (RBFNODE *)malloc(n); |
274 |
< |
if (rbf == NULL) |
275 |
< |
goto memerr; |
276 |
< |
memcpy(rbf, mig->rbfv[0], n); /* just duplicate */ |
277 |
< |
rbf->next = NULL; rbf->ejl = NULL; |
278 |
< |
return(rbf); |
279 |
< |
} |
280 |
< |
full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec)); |
281 |
< |
if (t > full_dist-M_PI/grid_res) { /* near second DSF */ |
282 |
< |
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1); |
283 |
< |
rbf = (RBFNODE *)malloc(n); |
284 |
< |
if (rbf == NULL) |
285 |
< |
goto memerr; |
286 |
< |
memcpy(rbf, mig->rbfv[1], n); /* just duplicate */ |
287 |
< |
rbf->next = NULL; rbf->ejl = NULL; |
288 |
< |
return(rbf); |
289 |
< |
} |
290 |
< |
t /= full_dist; |
291 |
< |
tryagain: |
292 |
< |
n = 0; /* count migrating particles */ |
293 |
< |
for (i = 0; i < mtx_nrows(mig); i++) |
294 |
< |
for (j = 0; j < mtx_ncols(mig); j++) |
295 |
< |
n += (mtx_coef(mig,i,j) > cthresh); |
296 |
< |
/* are we over our limit? */ |
297 |
< |
if ((lobe_lim > 0) & (n > lobe_lim)) { |
298 |
< |
cthresh = cthresh*2. + 10.*FTINY; |
299 |
< |
goto tryagain; |
300 |
< |
} |
301 |
< |
#ifdef DEBUG |
302 |
< |
fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n", |
303 |
< |
mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n); |
304 |
< |
#endif |
305 |
< |
rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1)); |
306 |
< |
if (rbf == NULL) |
307 |
< |
goto memerr; |
308 |
< |
rbf->next = NULL; rbf->ejl = NULL; |
309 |
< |
VCOPY(rbf->invec, invec); |
310 |
< |
rbf->nrbf = n; |
311 |
< |
rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal; |
312 |
< |
n = 0; /* advect RBF lobes */ |
313 |
< |
for (i = 0; i < mtx_nrows(mig); i++) { |
314 |
< |
const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i]; |
315 |
< |
const float peak0 = rbf0i->peak; |
316 |
< |
const double rad0 = R2ANG(rbf0i->crad); |
317 |
< |
FVECT v0; |
318 |
< |
float mv; |
319 |
< |
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); |
320 |
< |
for (j = 0; j < mtx_ncols(mig); j++) |
321 |
< |
if ((mv = mtx_coef(mig,i,j)) > cthresh) { |
322 |
< |
const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j]; |
323 |
< |
double rad1 = R2ANG(rbf1j->crad); |
324 |
< |
FVECT v; |
325 |
< |
int pos[2]; |
326 |
< |
rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal; |
327 |
< |
rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) + |
328 |
< |
rad1*rad1*t)); |
329 |
< |
ovec_from_pos(v, rbf1j->gx, rbf1j->gy); |
330 |
< |
geodesic(v, v0, v, t, GEOD_REL); |
331 |
< |
pos_from_vec(pos, v); |
332 |
< |
rbf->rbfa[n].gx = pos[0]; |
333 |
< |
rbf->rbfa[n].gy = pos[1]; |
334 |
< |
++n; |
335 |
< |
} |
336 |
< |
} |
337 |
< |
rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */ |
338 |
< |
return(rbf); |
339 |
< |
memerr: |
340 |
< |
fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname); |
341 |
< |
exit(1); |
342 |
< |
return(NULL); /* pro forma return */ |
343 |
< |
} |
344 |
< |
|
345 |
< |
/* Partially advect between recorded incident angles and allocate new RBF */ |
266 |
> |
/* Advect between recorded incident angles and allocate new RBF */ |
267 |
|
RBFNODE * |
268 |
|
advect_rbf(const FVECT invec, int lobe_lim) |
269 |
|
{ |
280 |
|
VCOPY(sivec, invec); /* find triangle/edge */ |
281 |
|
sym = get_interp(miga, sivec); |
282 |
|
if (sym < 0) /* can't interpolate? */ |
283 |
< |
return(NULL); |
283 |
> |
return(def_rbf_spec(invec)); |
284 |
|
if (miga[1] == NULL) { /* advect along edge? */ |
285 |
|
rbf = e_advect_rbf(miga[0], sivec, lobe_lim); |
286 |
|
if (single_plane_incident) |
290 |
|
return(rbf); |
291 |
|
} |
292 |
|
#ifdef DEBUG |
293 |
< |
if (miga[0]->rbfv[0] != miga[2]->rbfv[0] | |
294 |
< |
miga[0]->rbfv[1] != miga[1]->rbfv[0] | |
295 |
< |
miga[1]->rbfv[1] != miga[2]->rbfv[1]) { |
293 |
> |
if ((miga[0]->rbfv[0] != miga[2]->rbfv[0]) | |
294 |
> |
(miga[0]->rbfv[1] != miga[1]->rbfv[0]) | |
295 |
> |
(miga[1]->rbfv[1] != miga[2]->rbfv[1])) { |
296 |
|
fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname); |
297 |
|
exit(1); |
298 |
|
} |
343 |
|
const RBFVAL *rbf0i = &miga[0]->rbfv[0]->rbfa[i]; |
344 |
|
const float w0i = rbf0i->peak; |
345 |
|
const double rad0i = R2ANG(rbf0i->crad); |
346 |
+ |
C_COLOR cc0; |
347 |
|
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); |
348 |
+ |
c_decodeChroma(&cc0, rbf0i->chroma); |
349 |
|
for (j = 0; j < mtx_ncols(miga[0]); j++) { |
350 |
|
const float ma = mtx_coef(miga[0],i,j); |
351 |
|
const RBFVAL *rbf1j; |
352 |
< |
double rad1j, srad2; |
352 |
> |
C_COLOR ccs; |
353 |
> |
double srad2; |
354 |
|
if (ma <= cthresh) |
355 |
|
continue; |
356 |
|
rbf1j = &miga[0]->rbfv[1]->rbfa[j]; |
357 |
< |
rad1j = R2ANG(rbf1j->crad); |
358 |
< |
srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j; |
357 |
> |
c_decodeChroma(&ccs, rbf1j->chroma); |
358 |
> |
c_cmix(&ccs, 1.-s, &cc0, s, &ccs); |
359 |
> |
srad2 = R2ANG(rbf1j->crad); |
360 |
> |
srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*srad2*srad2; |
361 |
|
ovec_from_pos(v1, rbf1j->gx, rbf1j->gy); |
362 |
|
geodesic(v1, v0, v1, s, GEOD_REL); |
363 |
|
for (k = 0; k < mtx_ncols(miga[2]); k++) { |
364 |
|
float mb = mtx_coef(miga[1],j,k); |
365 |
|
float mc = mtx_coef(miga[2],i,k); |
366 |
|
const RBFVAL *rbf2k; |
367 |
< |
double rad2k; |
367 |
> |
double rad2; |
368 |
|
int pos[2]; |
369 |
|
if ((mb <= cthresh) & (mc <= cthresh)) |
370 |
|
continue; |
371 |
|
rbf2k = &miga[2]->rbfv[1]->rbfa[k]; |
372 |
< |
rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact); |
373 |
< |
rad2k = R2ANG(rbf2k->crad); |
374 |
< |
rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k)); |
372 |
> |
rad2 = R2ANG(rbf2k->crad); |
373 |
> |
rad2 = srad2 + t*rad2*rad2; |
374 |
> |
rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact) * |
375 |
> |
rad0i*rad0i/rad2; |
376 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
377 |
> |
C_COLOR cres; |
378 |
> |
c_decodeChroma(&cres, rbf2k->chroma); |
379 |
> |
c_cmix(&cres, 1.-t, &ccs, t, &cres); |
380 |
> |
rbf->rbfa[n].chroma = c_encodeChroma(&cres); |
381 |
> |
} else |
382 |
> |
rbf->rbfa[n].chroma = c_dfchroma; |
383 |
> |
rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); |
384 |
|
ovec_from_pos(v2, rbf2k->gx, rbf2k->gy); |
385 |
|
geodesic(v2, v1, v2, t, GEOD_REL); |
386 |
|
pos_from_vec(pos, v2); |