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) |
130 |
|
return(sgn2 == sgn3); |
131 |
|
} |
132 |
|
|
133 |
< |
/* Test and set for edge */ |
133 |
> |
/* Test (and set) bitmap for edge */ |
134 |
|
static int |
135 |
|
check_edge(unsigned char *emap, int nedges, const MIGRATION *mig, int mark) |
136 |
|
{ |
155 |
|
in_mesh(MIGRATION *miga[3], unsigned char *emap, int nedges, |
156 |
|
const FVECT ivec, MIGRATION *mig) |
157 |
|
{ |
158 |
< |
MIGRATION *ej1, *ej2; |
159 |
< |
RBFNODE *tv; |
158 |
> |
RBFNODE *tv[2]; |
159 |
> |
MIGRATION *sej[2], *dej[2]; |
160 |
> |
int i; |
161 |
|
/* check visitation record */ |
162 |
|
if (!check_edge(emap, nedges, mig, 1)) |
163 |
|
return(0); |
165 |
|
miga[0] = mig; /* close enough to edge */ |
166 |
|
return(1); |
167 |
|
} |
168 |
< |
/* do triangles either side */ |
169 |
< |
for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL; |
170 |
< |
ej1 = nextedge(mig->rbfv[0],ej1)) { |
171 |
< |
if (ej1 == mig) |
172 |
< |
continue; |
173 |
< |
tv = opp_rbf(mig->rbfv[0],ej1); |
174 |
< |
for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2)) |
175 |
< |
if (opp_rbf(tv,ej2) == mig->rbfv[1]) { |
176 |
< |
int do_ej1 = check_edge(emap, nedges, ej1, 0); |
177 |
< |
int do_ej2 = check_edge(emap, nedges, ej2, 0); |
178 |
< |
if (do_ej1 && in_mesh(miga, emap, nedges, ivec, ej1)) |
179 |
< |
return(1); |
180 |
< |
if (do_ej2 && in_mesh(miga, emap, nedges, ivec, ej2)) |
181 |
< |
return(1); |
182 |
< |
/* check just once */ |
178 |
< |
if (do_ej1 & do_ej2 && in_tri(mig->rbfv[0], |
179 |
< |
mig->rbfv[1], tv, ivec)) { |
180 |
< |
miga[0] = mig; |
181 |
< |
miga[1] = ej1; |
182 |
< |
miga[2] = ej2; |
183 |
< |
return(1); |
168 |
> |
if (!get_triangles(tv, mig)) /* do triangles either side? */ |
169 |
> |
return(0); |
170 |
> |
for (i = 2; i--; ) { /* identify edges to check */ |
171 |
> |
MIGRATION *ej; |
172 |
> |
sej[i] = dej[i] = NULL; |
173 |
> |
if (tv[i] == NULL) |
174 |
> |
continue; |
175 |
> |
for (ej = tv[i]->ejl; ej != NULL; ej = nextedge(tv[i],ej)) { |
176 |
> |
RBFNODE *rbfop = opp_rbf(tv[i],ej); |
177 |
> |
if (rbfop == mig->rbfv[0]) { |
178 |
> |
if (check_edge(emap, nedges, ej, 0)) |
179 |
> |
sej[i] = ej; |
180 |
> |
} else if (rbfop == mig->rbfv[1]) { |
181 |
> |
if (check_edge(emap, nedges, ej, 0)) |
182 |
> |
dej[i] = ej; |
183 |
|
} |
184 |
|
} |
185 |
|
} |
186 |
+ |
for (i = 2; i--; ) { /* check triangles just once */ |
187 |
+ |
if (sej[i] != NULL && in_mesh(miga, emap, nedges, ivec, sej[i])) |
188 |
+ |
return(1); |
189 |
+ |
if (dej[i] != NULL && in_mesh(miga, emap, nedges, ivec, dej[i])) |
190 |
+ |
return(1); |
191 |
+ |
if ((sej[i] == NULL) | (dej[i] == NULL)) |
192 |
+ |
continue; |
193 |
+ |
if (in_tri(mig->rbfv[0], mig->rbfv[1], tv[i], ivec)) { |
194 |
+ |
miga[0] = mig; |
195 |
+ |
miga[1] = sej[i]; |
196 |
+ |
miga[2] = dej[i]; |
197 |
+ |
return(1); |
198 |
+ |
} |
199 |
+ |
} |
200 |
|
return(0); /* not near this edge */ |
201 |
|
} |
202 |
|
|
206 |
|
{ |
207 |
|
miga[0] = miga[1] = miga[2] = NULL; |
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]) |
212 |
< |
break; |
213 |
< |
if (rbf->next != NULL && |
214 |
< |
input_orient*rbf->next->invec[2] < |
215 |
< |
input_orient*invec[2]) { |
216 |
< |
for (miga[0] = rbf->ejl; miga[0] != NULL; |
217 |
< |
miga[0] = nextedge(rbf,miga[0])) |
218 |
< |
if (opp_rbf(rbf,miga[0]) == rbf->next) { |
219 |
< |
double nf = 1.-rbf->invec[2]*rbf->invec[2]; |
220 |
< |
if (nf > FTINY) { |
221 |
< |
nf = sqrt((1.-invec[2]*invec[2])/nf); |
222 |
< |
invec[0] = nf*rbf->invec[0]; |
223 |
< |
invec[1] = nf*rbf->invec[1]; |
224 |
< |
} |
212 |
< |
return(0); |
213 |
< |
} |
214 |
< |
break; |
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]-FTINY) |
212 |
> |
break; |
213 |
> |
if (rbf->next != NULL && input_orient*rbf->next->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]; |
219 |
> |
if (nf > FTINY) { /* rotate to match */ |
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); |
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); |
243 |
|
exit(1); |
244 |
|
} |
245 |
|
/* identify intersection */ |
246 |
< |
if (!in_mesh(miga, emap, nedges, invec, mig_list)) |
246 |
> |
if (!in_mesh(miga, emap, nedges, invec, mig_list)) { |
247 |
> |
#ifdef DEBUG |
248 |
> |
fprintf(stderr, |
249 |
> |
"Incident angle (%.1f,%.1f) deg. outside mesh\n", |
250 |
> |
get_theta180(invec), get_phi360(invec)); |
251 |
> |
#endif |
252 |
|
sym = -1; /* outside mesh */ |
253 |
< |
else if (miga[1] != NULL && |
253 |
> |
} else if (miga[1] != NULL && |
254 |
|
(miga[2] == NULL || !order_triangle(miga))) { |
255 |
|
#ifdef DEBUG |
256 |
|
fputs("Munged triangle in get_interp()\n", stderr); |
264 |
|
|
265 |
|
/* Advect and allocate new RBF along edge */ |
266 |
|
static RBFNODE * |
267 |
< |
e_advect_rbf(const MIGRATION *mig, const FVECT invec) |
267 |
> |
e_advect_rbf(const MIGRATION *mig, const FVECT invec, int lobe_lim) |
268 |
|
{ |
269 |
+ |
double cthresh = FTINY; |
270 |
|
RBFNODE *rbf; |
271 |
|
int n, i, j; |
272 |
|
double t, full_dist; |
273 |
|
/* get relative position */ |
274 |
< |
t = acos(DOT(invec, mig->rbfv[0]->invec)); |
274 |
> |
t = Acos(DOT(invec, mig->rbfv[0]->invec)); |
275 |
|
if (t < M_PI/grid_res) { /* near first DSF */ |
276 |
|
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1); |
277 |
|
rbf = (RBFNODE *)malloc(n); |
278 |
|
if (rbf == NULL) |
279 |
|
goto memerr; |
280 |
|
memcpy(rbf, mig->rbfv[0], n); /* just duplicate */ |
281 |
+ |
rbf->next = NULL; rbf->ejl = NULL; |
282 |
|
return(rbf); |
283 |
|
} |
284 |
|
full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec)); |
288 |
|
if (rbf == NULL) |
289 |
|
goto memerr; |
290 |
|
memcpy(rbf, mig->rbfv[1], n); /* just duplicate */ |
291 |
+ |
rbf->next = NULL; rbf->ejl = NULL; |
292 |
|
return(rbf); |
293 |
|
} |
294 |
< |
t /= full_dist; |
294 |
> |
t /= full_dist; |
295 |
> |
tryagain: |
296 |
|
n = 0; /* count migrating particles */ |
297 |
|
for (i = 0; i < mtx_nrows(mig); i++) |
298 |
|
for (j = 0; j < mtx_ncols(mig); j++) |
299 |
< |
n += (mtx_coef(mig,i,j) > FTINY); |
299 |
> |
n += (mtx_coef(mig,i,j) > cthresh); |
300 |
> |
/* are we over our limit? */ |
301 |
> |
if ((lobe_lim > 0) & (n > lobe_lim)) { |
302 |
> |
cthresh = cthresh*2. + 10.*FTINY; |
303 |
> |
goto tryagain; |
304 |
> |
} |
305 |
|
#ifdef DEBUG |
306 |
|
fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n", |
307 |
|
mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n); |
322 |
|
float mv; |
323 |
|
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); |
324 |
|
for (j = 0; j < mtx_ncols(mig); j++) |
325 |
< |
if ((mv = mtx_coef(mig,i,j)) > FTINY) { |
325 |
> |
if ((mv = mtx_coef(mig,i,j)) > cthresh) { |
326 |
|
const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j]; |
327 |
< |
double rad1 = R2ANG(rbf1j->crad); |
327 |
> |
double rad2; |
328 |
|
FVECT v; |
329 |
|
int pos[2]; |
330 |
< |
rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal; |
331 |
< |
rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) + |
332 |
< |
rad1*rad1*t)); |
330 |
> |
rad2 = R2ANG(rbf1j->crad); |
331 |
> |
rad2 = rad0*rad0*(1.-t) + rad2*rad2*t; |
332 |
> |
rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal * |
333 |
> |
rad0*rad0/rad2; |
334 |
> |
rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); |
335 |
|
ovec_from_pos(v, rbf1j->gx, rbf1j->gy); |
336 |
|
geodesic(v, v0, v, t, GEOD_REL); |
337 |
|
pos_from_vec(pos, v); |
348 |
|
return(NULL); /* pro forma return */ |
349 |
|
} |
350 |
|
|
351 |
< |
/* Partially advect between recorded incident angles and allocate new RBF */ |
351 |
> |
/* Advect between recorded incident angles and allocate new RBF */ |
352 |
|
RBFNODE * |
353 |
< |
advect_rbf(const FVECT invec) |
353 |
> |
advect_rbf(const FVECT invec, int lobe_lim) |
354 |
|
{ |
355 |
+ |
double cthresh = FTINY; |
356 |
|
FVECT sivec; |
357 |
|
MIGRATION *miga[3]; |
358 |
|
RBFNODE *rbf; |
367 |
|
if (sym < 0) /* can't interpolate? */ |
368 |
|
return(NULL); |
369 |
|
if (miga[1] == NULL) { /* advect along edge? */ |
370 |
< |
rbf = e_advect_rbf(miga[0], sivec); |
370 |
> |
rbf = e_advect_rbf(miga[0], sivec, lobe_lim); |
371 |
|
if (single_plane_incident) |
372 |
|
rotate_rbf(rbf, invec); |
373 |
|
else |
375 |
|
return(rbf); |
376 |
|
} |
377 |
|
#ifdef DEBUG |
378 |
< |
if (miga[0]->rbfv[0] != miga[2]->rbfv[0] | |
379 |
< |
miga[0]->rbfv[1] != miga[1]->rbfv[0] | |
380 |
< |
miga[1]->rbfv[1] != miga[2]->rbfv[1]) { |
378 |
> |
if ((miga[0]->rbfv[0] != miga[2]->rbfv[0]) | |
379 |
> |
(miga[0]->rbfv[1] != miga[1]->rbfv[0]) | |
380 |
> |
(miga[1]->rbfv[1] != miga[2]->rbfv[1])) { |
381 |
|
fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname); |
382 |
|
exit(1); |
383 |
|
} |
393 |
|
geodesic(v1, miga[0]->rbfv[0]->invec, miga[0]->rbfv[1]->invec, |
394 |
|
s, GEOD_REL); |
395 |
|
t = acos(DOT(v1,sivec)) / acos(DOT(v1,miga[1]->rbfv[1]->invec)); |
396 |
+ |
tryagain: |
397 |
|
n = 0; /* count migrating particles */ |
398 |
|
for (i = 0; i < mtx_nrows(miga[0]); i++) |
399 |
|
for (j = 0; j < mtx_ncols(miga[0]); j++) |
400 |
< |
for (k = (mtx_coef(miga[0],i,j) > FTINY) * |
400 |
> |
for (k = (mtx_coef(miga[0],i,j) > cthresh) * |
401 |
|
mtx_ncols(miga[2]); k--; ) |
402 |
< |
n += (mtx_coef(miga[2],i,k) > FTINY || |
403 |
< |
mtx_coef(miga[1],j,k) > FTINY); |
402 |
> |
n += (mtx_coef(miga[2],i,k) > cthresh || |
403 |
> |
mtx_coef(miga[1],j,k) > cthresh); |
404 |
> |
/* are we over our limit? */ |
405 |
> |
if ((lobe_lim > 0) & (n > lobe_lim)) { |
406 |
> |
cthresh = cthresh*2. + 10.*FTINY; |
407 |
> |
goto tryagain; |
408 |
> |
} |
409 |
|
#ifdef DEBUG |
410 |
|
fprintf(stderr, "Input RBFs have %d, %d, %d nodes -> output has %d\n", |
411 |
|
miga[0]->rbfv[0]->nrbf, miga[0]->rbfv[1]->nrbf, |
432 |
|
for (j = 0; j < mtx_ncols(miga[0]); j++) { |
433 |
|
const float ma = mtx_coef(miga[0],i,j); |
434 |
|
const RBFVAL *rbf1j; |
435 |
< |
double rad1j, srad2; |
436 |
< |
if (ma <= FTINY) |
435 |
> |
double srad2; |
436 |
> |
if (ma <= cthresh) |
437 |
|
continue; |
438 |
|
rbf1j = &miga[0]->rbfv[1]->rbfa[j]; |
439 |
< |
rad1j = R2ANG(rbf1j->crad); |
440 |
< |
srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j; |
439 |
> |
srad2 = R2ANG(rbf1j->crad); |
440 |
> |
srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*srad2*srad2; |
441 |
|
ovec_from_pos(v1, rbf1j->gx, rbf1j->gy); |
442 |
|
geodesic(v1, v0, v1, s, GEOD_REL); |
443 |
|
for (k = 0; k < mtx_ncols(miga[2]); k++) { |
444 |
|
float mb = mtx_coef(miga[1],j,k); |
445 |
|
float mc = mtx_coef(miga[2],i,k); |
446 |
|
const RBFVAL *rbf2k; |
447 |
< |
double rad2k; |
413 |
< |
FVECT vout; |
447 |
> |
double rad2; |
448 |
|
int pos[2]; |
449 |
< |
if ((mb <= FTINY) & (mc <= FTINY)) |
449 |
> |
if ((mb <= cthresh) & (mc <= cthresh)) |
450 |
|
continue; |
451 |
|
rbf2k = &miga[2]->rbfv[1]->rbfa[k]; |
452 |
< |
rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact); |
453 |
< |
rad2k = R2ANG(rbf2k->crad); |
454 |
< |
rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k)); |
452 |
> |
rad2 = R2ANG(rbf2k->crad); |
453 |
> |
rad2 = srad2 + t*rad2*rad2; |
454 |
> |
rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact) * |
455 |
> |
rad0i*rad0i/rad2; |
456 |
> |
rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); |
457 |
|
ovec_from_pos(v2, rbf2k->gx, rbf2k->gy); |
458 |
< |
geodesic(vout, v1, v2, t, GEOD_REL); |
459 |
< |
pos_from_vec(pos, vout); |
458 |
> |
geodesic(v2, v1, v2, t, GEOD_REL); |
459 |
> |
pos_from_vec(pos, v2); |
460 |
|
rbf->rbfa[n].gx = pos[0]; |
461 |
|
rbf->rbfa[n].gy = pos[1]; |
462 |
|
++n; |