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 */ |
129 |
|
return(sgn2 == sgn3); |
130 |
|
} |
131 |
|
|
132 |
< |
/* Test and set for edge */ |
132 |
> |
/* Test (and set) bitmap for edge */ |
133 |
|
static int |
134 |
|
check_edge(unsigned char *emap, int nedges, const MIGRATION *mig, int mark) |
135 |
|
{ |
154 |
|
in_mesh(MIGRATION *miga[3], unsigned char *emap, int nedges, |
155 |
|
const FVECT ivec, MIGRATION *mig) |
156 |
|
{ |
157 |
< |
MIGRATION *ej1, *ej2; |
158 |
< |
RBFNODE *tv; |
157 |
> |
RBFNODE *tv[2]; |
158 |
> |
MIGRATION *sej[2], *dej[2]; |
159 |
> |
int i; |
160 |
|
/* check visitation record */ |
161 |
|
if (!check_edge(emap, nedges, mig, 1)) |
162 |
|
return(0); |
164 |
|
miga[0] = mig; /* close enough to edge */ |
165 |
|
return(1); |
166 |
|
} |
167 |
< |
/* do triangles either side */ |
168 |
< |
for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL; |
169 |
< |
ej1 = nextedge(mig->rbfv[0],ej1)) { |
170 |
< |
if (ej1 == mig) |
171 |
< |
continue; |
172 |
< |
tv = opp_rbf(mig->rbfv[0],ej1); |
173 |
< |
for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2)) |
174 |
< |
if (opp_rbf(tv,ej2) == mig->rbfv[1]) { |
175 |
< |
int do_ej1 = check_edge(emap, nedges, ej1, 0); |
176 |
< |
int do_ej2 = check_edge(emap, nedges, ej2, 0); |
177 |
< |
if (do_ej1 && in_mesh(miga, emap, nedges, ivec, ej1)) |
178 |
< |
return(1); |
179 |
< |
if (do_ej2 && in_mesh(miga, emap, nedges, ivec, ej2)) |
180 |
< |
return(1); |
181 |
< |
/* 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); |
167 |
> |
if (!get_triangles(tv, mig)) /* do triangles either side? */ |
168 |
> |
return(0); |
169 |
> |
for (i = 2; i--; ) { /* identify edges to check */ |
170 |
> |
MIGRATION *ej; |
171 |
> |
sej[i] = dej[i] = NULL; |
172 |
> |
if (tv[i] == NULL) |
173 |
> |
continue; |
174 |
> |
for (ej = tv[i]->ejl; ej != NULL; ej = nextedge(tv[i],ej)) { |
175 |
> |
RBFNODE *rbfop = opp_rbf(tv[i],ej); |
176 |
> |
if (rbfop == mig->rbfv[0]) { |
177 |
> |
if (check_edge(emap, nedges, ej, 0)) |
178 |
> |
sej[i] = ej; |
179 |
> |
} else if (rbfop == mig->rbfv[1]) { |
180 |
> |
if (check_edge(emap, nedges, ej, 0)) |
181 |
> |
dej[i] = ej; |
182 |
|
} |
183 |
|
} |
184 |
|
} |
185 |
+ |
for (i = 2; i--; ) { /* check triangles just once */ |
186 |
+ |
if (sej[i] != NULL && in_mesh(miga, emap, nedges, ivec, sej[i])) |
187 |
+ |
return(1); |
188 |
+ |
if (dej[i] != NULL && in_mesh(miga, emap, nedges, ivec, dej[i])) |
189 |
+ |
return(1); |
190 |
+ |
if ((sej[i] == NULL) | (dej[i] == NULL)) |
191 |
+ |
continue; |
192 |
+ |
if (in_tri(mig->rbfv[0], mig->rbfv[1], tv[i], ivec)) { |
193 |
+ |
miga[0] = mig; |
194 |
+ |
miga[1] = sej[i]; |
195 |
+ |
miga[2] = dej[i]; |
196 |
+ |
return(1); |
197 |
+ |
} |
198 |
+ |
} |
199 |
|
return(0); /* not near this edge */ |
200 |
|
} |
201 |
|
|
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 |
< |
return(0); |
219 |
< |
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); |
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); |
262 |
|
} |
263 |
|
} |
264 |
|
|
265 |
< |
/* Advect and allocate new RBF along edge */ |
242 |
< |
static RBFNODE * |
243 |
< |
e_advect_rbf(const MIGRATION *mig, const FVECT invec) |
244 |
< |
{ |
245 |
< |
RBFNODE *rbf; |
246 |
< |
int n, i, j; |
247 |
< |
double t, full_dist; |
248 |
< |
/* get relative position */ |
249 |
< |
t = acos(DOT(invec, mig->rbfv[0]->invec)); |
250 |
< |
if (t < M_PI/GRIDRES) { /* near first DSF */ |
251 |
< |
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1); |
252 |
< |
rbf = (RBFNODE *)malloc(n); |
253 |
< |
if (rbf == NULL) |
254 |
< |
goto memerr; |
255 |
< |
memcpy(rbf, mig->rbfv[0], n); /* just duplicate */ |
256 |
< |
return(rbf); |
257 |
< |
} |
258 |
< |
full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec)); |
259 |
< |
if (t > full_dist-M_PI/GRIDRES) { /* near second DSF */ |
260 |
< |
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1); |
261 |
< |
rbf = (RBFNODE *)malloc(n); |
262 |
< |
if (rbf == NULL) |
263 |
< |
goto memerr; |
264 |
< |
memcpy(rbf, mig->rbfv[1], n); /* just duplicate */ |
265 |
< |
return(rbf); |
266 |
< |
} |
267 |
< |
t /= full_dist; |
268 |
< |
n = 0; /* count migrating particles */ |
269 |
< |
for (i = 0; i < mtx_nrows(mig); i++) |
270 |
< |
for (j = 0; j < mtx_ncols(mig); j++) |
271 |
< |
n += (mtx_coef(mig,i,j) > FTINY); |
272 |
< |
#ifdef DEBUG |
273 |
< |
fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n", |
274 |
< |
mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n); |
275 |
< |
#endif |
276 |
< |
rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1)); |
277 |
< |
if (rbf == NULL) |
278 |
< |
goto memerr; |
279 |
< |
rbf->next = NULL; rbf->ejl = NULL; |
280 |
< |
VCOPY(rbf->invec, invec); |
281 |
< |
rbf->nrbf = n; |
282 |
< |
rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal; |
283 |
< |
n = 0; /* advect RBF lobes */ |
284 |
< |
for (i = 0; i < mtx_nrows(mig); i++) { |
285 |
< |
const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i]; |
286 |
< |
const float peak0 = rbf0i->peak; |
287 |
< |
const double rad0 = R2ANG(rbf0i->crad); |
288 |
< |
FVECT v0; |
289 |
< |
float mv; |
290 |
< |
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); |
291 |
< |
for (j = 0; j < mtx_ncols(mig); j++) |
292 |
< |
if ((mv = mtx_coef(mig,i,j)) > FTINY) { |
293 |
< |
const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j]; |
294 |
< |
double rad1 = R2ANG(rbf1j->crad); |
295 |
< |
FVECT v; |
296 |
< |
int pos[2]; |
297 |
< |
rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal; |
298 |
< |
rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) + |
299 |
< |
rad1*rad1*t)); |
300 |
< |
ovec_from_pos(v, rbf1j->gx, rbf1j->gy); |
301 |
< |
geodesic(v, v0, v, t, GEOD_REL); |
302 |
< |
pos_from_vec(pos, v); |
303 |
< |
rbf->rbfa[n].gx = pos[0]; |
304 |
< |
rbf->rbfa[n].gy = pos[1]; |
305 |
< |
++n; |
306 |
< |
} |
307 |
< |
} |
308 |
< |
rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */ |
309 |
< |
return(rbf); |
310 |
< |
memerr: |
311 |
< |
fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname); |
312 |
< |
exit(1); |
313 |
< |
return(NULL); /* pro forma return */ |
314 |
< |
} |
315 |
< |
|
316 |
< |
/* 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); |
285 |
< |
rev_rbf_symmetry(rbf, sym); |
284 |
> |
rbf = e_advect_rbf(miga[0], sivec, lobe_lim); |
285 |
> |
if (single_plane_incident) |
286 |
> |
rotate_rbf(rbf, invec); |
287 |
> |
else |
288 |
> |
rev_rbf_symmetry(rbf, sym); |
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; |
403 |
< |
FVECT vout; |
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(vout, v1, v2, t, GEOD_REL); |
385 |
< |
pos_from_vec(pos, vout); |
384 |
> |
geodesic(v2, v1, v2, t, GEOD_REL); |
385 |
> |
pos_from_vec(pos, v2); |
386 |
|
rbf->rbfa[n].gx = pos[0]; |
387 |
|
rbf->rbfa[n].gy = pos[1]; |
388 |
|
++n; |