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