| 14 |
|
#include "rtio.h" |
| 15 |
|
#include "resolu.h" |
| 16 |
|
#include "bsdfrep.h" |
| 17 |
+ |
/* name and manufacturer if known */ |
| 18 |
+ |
char bsdf_name[256]; |
| 19 |
+ |
char bsdf_manuf[256]; |
| 20 |
|
/* active grid resolution */ |
| 21 |
|
int grid_res = GRIDRES; |
| 22 |
|
|
| 198 |
|
int pos[2]; |
| 199 |
|
int n; |
| 200 |
|
|
| 201 |
< |
for (n = ((-.01 > phi) | (phi > .01))*rbf->nrbf; n-- > 0; ) { |
| 201 |
> |
for (n = (cos(phi) < 1.-FTINY)*rbf->nrbf; n-- > 0; ) { |
| 202 |
|
ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy); |
| 203 |
|
spinvector(outvec, outvec, vnorm, phi); |
| 204 |
|
pos_from_vec(pos, outvec); |
| 304 |
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
| 305 |
|
if (DOT(rbf->invec, newrbf->invec) >= 1.-FTINY) { |
| 306 |
|
fprintf(stderr, |
| 307 |
< |
"%s: Duplicate incident measurement (ignored)\n", |
| 308 |
< |
progname); |
| 307 |
> |
"%s: Duplicate incident measurement ignored at (%.1f,%.1f)\n", |
| 308 |
> |
progname, get_theta180(newrbf->invec), |
| 309 |
> |
get_phi360(newrbf->invec)); |
| 310 |
|
free(newrbf); |
| 311 |
|
return(-1); |
| 312 |
|
} |
| 394 |
|
return((rbfv[0] != NULL) + (rbfv[1] != NULL)); |
| 395 |
|
} |
| 396 |
|
|
| 397 |
+ |
/* Advect and allocate new RBF along edge (internal call) */ |
| 398 |
+ |
RBFNODE * |
| 399 |
+ |
e_advect_rbf(const MIGRATION *mig, const FVECT invec, int lobe_lim) |
| 400 |
+ |
{ |
| 401 |
+ |
double cthresh = FTINY; |
| 402 |
+ |
RBFNODE *rbf; |
| 403 |
+ |
int n, i, j; |
| 404 |
+ |
double t, full_dist; |
| 405 |
+ |
/* get relative position */ |
| 406 |
+ |
t = Acos(DOT(invec, mig->rbfv[0]->invec)); |
| 407 |
+ |
if (t < M_PI/grid_res) { /* near first DSF */ |
| 408 |
+ |
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1); |
| 409 |
+ |
rbf = (RBFNODE *)malloc(n); |
| 410 |
+ |
if (rbf == NULL) |
| 411 |
+ |
goto memerr; |
| 412 |
+ |
memcpy(rbf, mig->rbfv[0], n); /* just duplicate */ |
| 413 |
+ |
rbf->next = NULL; rbf->ejl = NULL; |
| 414 |
+ |
return(rbf); |
| 415 |
+ |
} |
| 416 |
+ |
full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec)); |
| 417 |
+ |
if (t > full_dist-M_PI/grid_res) { /* near second DSF */ |
| 418 |
+ |
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1); |
| 419 |
+ |
rbf = (RBFNODE *)malloc(n); |
| 420 |
+ |
if (rbf == NULL) |
| 421 |
+ |
goto memerr; |
| 422 |
+ |
memcpy(rbf, mig->rbfv[1], n); /* just duplicate */ |
| 423 |
+ |
rbf->next = NULL; rbf->ejl = NULL; |
| 424 |
+ |
return(rbf); |
| 425 |
+ |
} |
| 426 |
+ |
t /= full_dist; |
| 427 |
+ |
tryagain: |
| 428 |
+ |
n = 0; /* count migrating particles */ |
| 429 |
+ |
for (i = 0; i < mtx_nrows(mig); i++) |
| 430 |
+ |
for (j = 0; j < mtx_ncols(mig); j++) |
| 431 |
+ |
n += (mtx_coef(mig,i,j) > cthresh); |
| 432 |
+ |
/* are we over our limit? */ |
| 433 |
+ |
if ((lobe_lim > 0) & (n > lobe_lim)) { |
| 434 |
+ |
cthresh = cthresh*2. + 10.*FTINY; |
| 435 |
+ |
goto tryagain; |
| 436 |
+ |
} |
| 437 |
+ |
#ifdef DEBUG |
| 438 |
+ |
fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n", |
| 439 |
+ |
mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n); |
| 440 |
+ |
#endif |
| 441 |
+ |
rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1)); |
| 442 |
+ |
if (rbf == NULL) |
| 443 |
+ |
goto memerr; |
| 444 |
+ |
rbf->next = NULL; rbf->ejl = NULL; |
| 445 |
+ |
VCOPY(rbf->invec, invec); |
| 446 |
+ |
rbf->nrbf = n; |
| 447 |
+ |
rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal; |
| 448 |
+ |
n = 0; /* advect RBF lobes */ |
| 449 |
+ |
for (i = 0; i < mtx_nrows(mig); i++) { |
| 450 |
+ |
const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i]; |
| 451 |
+ |
const float peak0 = rbf0i->peak; |
| 452 |
+ |
const double rad0 = R2ANG(rbf0i->crad); |
| 453 |
+ |
FVECT v0; |
| 454 |
+ |
float mv; |
| 455 |
+ |
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); |
| 456 |
+ |
for (j = 0; j < mtx_ncols(mig); j++) |
| 457 |
+ |
if ((mv = mtx_coef(mig,i,j)) > cthresh) { |
| 458 |
+ |
const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j]; |
| 459 |
+ |
double rad2; |
| 460 |
+ |
FVECT v; |
| 461 |
+ |
int pos[2]; |
| 462 |
+ |
rad2 = R2ANG(rbf1j->crad); |
| 463 |
+ |
rad2 = rad0*rad0*(1.-t) + rad2*rad2*t; |
| 464 |
+ |
rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal * |
| 465 |
+ |
rad0*rad0/rad2; |
| 466 |
+ |
rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); |
| 467 |
+ |
ovec_from_pos(v, rbf1j->gx, rbf1j->gy); |
| 468 |
+ |
geodesic(v, v0, v, t, GEOD_REL); |
| 469 |
+ |
pos_from_vec(pos, v); |
| 470 |
+ |
rbf->rbfa[n].gx = pos[0]; |
| 471 |
+ |
rbf->rbfa[n].gy = pos[1]; |
| 472 |
+ |
++n; |
| 473 |
+ |
} |
| 474 |
+ |
} |
| 475 |
+ |
rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */ |
| 476 |
+ |
return(rbf); |
| 477 |
+ |
memerr: |
| 478 |
+ |
fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname); |
| 479 |
+ |
exit(1); |
| 480 |
+ |
return(NULL); /* pro forma return */ |
| 481 |
+ |
} |
| 482 |
+ |
|
| 483 |
|
/* Clear our BSDF representation and free memory */ |
| 484 |
|
void |
| 485 |
|
clear_bsdf_rep(void) |
| 494 |
|
dsf_list = rbf->next; |
| 495 |
|
free(rbf); |
| 496 |
|
} |
| 497 |
+ |
bsdf_name[0] = '\0'; |
| 498 |
+ |
bsdf_manuf[0] = '\0'; |
| 499 |
|
inp_coverage = 0; |
| 500 |
|
single_plane_incident = -1; |
| 501 |
|
input_orient = output_orient = 0; |
| 510 |
|
MIGRATION *mig; |
| 511 |
|
int i, n; |
| 512 |
|
/* finish header */ |
| 513 |
+ |
if (bsdf_name[0]) |
| 514 |
+ |
fprintf(ofp, "NAME=%s\n", bsdf_name); |
| 515 |
+ |
if (bsdf_manuf[0]) |
| 516 |
+ |
fprintf(ofp, "MANUFACT=%s\n", bsdf_manuf); |
| 517 |
|
fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); |
| 518 |
|
fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); |
| 519 |
|
fprintf(ofp, "GRIDRES=%d\n", grid_res); |
| 570 |
|
{ |
| 571 |
|
char fmt[32]; |
| 572 |
|
|
| 573 |
+ |
if (!strncmp(s, "NAME=", 5)) { |
| 574 |
+ |
strcpy(bsdf_name, s+5); |
| 575 |
+ |
bsdf_name[strlen(bsdf_name)-1] = '\0'; |
| 576 |
+ |
} |
| 577 |
+ |
if (!strncmp(s, "MANUFACT=", 9)) { |
| 578 |
+ |
strcpy(bsdf_manuf, s+9); |
| 579 |
+ |
bsdf_manuf[strlen(bsdf_manuf)-1] = '\0'; |
| 580 |
+ |
} |
| 581 |
|
if (!strncmp(s, "SYMMETRY=", 9)) { |
| 582 |
|
inp_coverage = atoi(s+9); |
| 583 |
|
single_plane_incident = !inp_coverage; |
| 611 |
|
clear_bsdf_rep(); |
| 612 |
|
if (ifp == NULL) |
| 613 |
|
return(0); |
| 614 |
< |
if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | |
| 615 |
< |
!input_orient | !output_orient) { |
| 614 |
> |
if (getheader(ifp, headline, NULL) < 0 || (single_plane_incident < 0) | |
| 615 |
> |
!input_orient | !output_orient | |
| 616 |
> |
(grid_res < 16) | (grid_res > 256)) { |
| 617 |
|
fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", |
| 618 |
|
progname); |
| 619 |
|
return(0); |
| 620 |
|
} |
| 621 |
< |
rbfh.next = NULL; /* read each DSF */ |
| 517 |
< |
rbfh.ejl = NULL; |
| 621 |
> |
memset(&rbfh, 0, sizeof(rbfh)); /* read each DSF */ |
| 622 |
|
while ((rbfh.ord = getint(4, ifp)) >= 0) { |
| 623 |
|
RBFNODE *newrbf; |
| 624 |
|
|
| 635 |
|
sizeof(RBFVAL)*(rbfh.nrbf-1)); |
| 636 |
|
if (newrbf == NULL) |
| 637 |
|
goto memerr; |
| 638 |
< |
memcpy(newrbf, &rbfh, sizeof(RBFNODE)-sizeof(RBFVAL)); |
| 638 |
> |
*newrbf = rbfh; |
| 639 |
|
for (i = 0; i < rbfh.nrbf; i++) { |
| 640 |
|
newrbf->rbfa[i].peak = getflt(ifp); |
| 641 |
|
newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff; |