| 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 | 
  | 
 | 
| 261 | 
  | 
double | 
| 262 | 
  | 
eval_rbfrep(const RBFNODE *rp, const FVECT outvec) | 
| 263 | 
  | 
{ | 
| 264 | 
+ | 
        const double    rfact2 = (38./M_PI/M_PI)*(grid_res*grid_res); | 
| 265 | 
  | 
        double          minval = bsdf_min*output_orient*outvec[2]; | 
| 266 | 
+ | 
        int             pos[2]; | 
| 267 | 
  | 
        double          res = 0; | 
| 268 | 
  | 
        const RBFVAL    *rbfp; | 
| 269 | 
  | 
        FVECT           odir; | 
| 270 | 
< | 
        double          sig2; | 
| 270 | 
> | 
        double          rad2; | 
| 271 | 
  | 
        int             n; | 
| 272 | 
  | 
                                /* check for wrong side */ | 
| 273 | 
  | 
        if (outvec[2] > 0 ^ output_orient > 0) | 
| 275 | 
  | 
                                /* use minimum if no information avail. */ | 
| 276 | 
  | 
        if (rp == NULL) | 
| 277 | 
  | 
                return(minval); | 
| 278 | 
+ | 
                                /* optimization for fast lobe culling */ | 
| 279 | 
+ | 
        pos_from_vec(pos, outvec); | 
| 280 | 
  | 
                                /* sum radial basis function */ | 
| 281 | 
  | 
        rbfp = rp->rbfa; | 
| 282 | 
  | 
        for (n = rp->nrbf; n--; rbfp++) { | 
| 283 | 
+ | 
                int     d2 = (pos[0]-rbfp->gx)*(pos[0]-rbfp->gx) + | 
| 284 | 
+ | 
                                (pos[1]-rbfp->gy)*(pos[1]-rbfp->gy); | 
| 285 | 
+ | 
                rad2 = R2ANG(rbfp->crad); | 
| 286 | 
+ | 
                rad2 *= rad2; | 
| 287 | 
+ | 
                if (d2 > rad2*rfact2) | 
| 288 | 
+ | 
                        continue; | 
| 289 | 
  | 
                ovec_from_pos(odir, rbfp->gx, rbfp->gy); | 
| 290 | 
< | 
                sig2 = R2ANG(rbfp->crad); | 
| 278 | 
< | 
                sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2); | 
| 279 | 
< | 
                if (sig2 > -19.) | 
| 280 | 
< | 
                        res += rbfp->peak * exp(sig2); | 
| 290 | 
> | 
                res += rbfp->peak * exp((DOT(odir,outvec) - 1.) / rad2); | 
| 291 | 
  | 
        } | 
| 292 | 
  | 
        if (res < minval)       /* never return less than minval */ | 
| 293 | 
  | 
                return(minval); | 
| 393 | 
  | 
        return((rbfv[0] != NULL) + (rbfv[1] != NULL)); | 
| 394 | 
  | 
} | 
| 395 | 
  | 
 | 
| 396 | 
+ | 
/* Advect and allocate new RBF along edge (internal call) */ | 
| 397 | 
+ | 
RBFNODE * | 
| 398 | 
+ | 
e_advect_rbf(const MIGRATION *mig, const FVECT invec, int lobe_lim) | 
| 399 | 
+ | 
{ | 
| 400 | 
+ | 
        double          cthresh = FTINY; | 
| 401 | 
+ | 
        RBFNODE         *rbf; | 
| 402 | 
+ | 
        int             n, i, j; | 
| 403 | 
+ | 
        double          t, full_dist; | 
| 404 | 
+ | 
                                                /* get relative position */ | 
| 405 | 
+ | 
        t = Acos(DOT(invec, mig->rbfv[0]->invec)); | 
| 406 | 
+ | 
        if (t < M_PI/grid_res) {                /* near first DSF */ | 
| 407 | 
+ | 
                n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1); | 
| 408 | 
+ | 
                rbf = (RBFNODE *)malloc(n); | 
| 409 | 
+ | 
                if (rbf == NULL) | 
| 410 | 
+ | 
                        goto memerr; | 
| 411 | 
+ | 
                memcpy(rbf, mig->rbfv[0], n);   /* just duplicate */ | 
| 412 | 
+ | 
                rbf->next = NULL; rbf->ejl = NULL; | 
| 413 | 
+ | 
                return(rbf); | 
| 414 | 
+ | 
        } | 
| 415 | 
+ | 
        full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec)); | 
| 416 | 
+ | 
        if (t > full_dist-M_PI/grid_res) {      /* near second DSF */ | 
| 417 | 
+ | 
                n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1); | 
| 418 | 
+ | 
                rbf = (RBFNODE *)malloc(n); | 
| 419 | 
+ | 
                if (rbf == NULL) | 
| 420 | 
+ | 
                        goto memerr; | 
| 421 | 
+ | 
                memcpy(rbf, mig->rbfv[1], n);   /* just duplicate */ | 
| 422 | 
+ | 
                rbf->next = NULL; rbf->ejl = NULL; | 
| 423 | 
+ | 
                return(rbf); | 
| 424 | 
+ | 
        } | 
| 425 | 
+ | 
        t /= full_dist; | 
| 426 | 
+ | 
tryagain: | 
| 427 | 
+ | 
        n = 0;                                  /* count migrating particles */ | 
| 428 | 
+ | 
        for (i = 0; i < mtx_nrows(mig); i++) | 
| 429 | 
+ | 
            for (j = 0; j < mtx_ncols(mig); j++) | 
| 430 | 
+ | 
                n += (mtx_coef(mig,i,j) > cthresh); | 
| 431 | 
+ | 
                                                /* are we over our limit? */ | 
| 432 | 
+ | 
        if ((lobe_lim > 0) & (n > lobe_lim)) { | 
| 433 | 
+ | 
                cthresh = cthresh*2. + 10.*FTINY; | 
| 434 | 
+ | 
                goto tryagain; | 
| 435 | 
+ | 
        } | 
| 436 | 
+ | 
#ifdef DEBUG | 
| 437 | 
+ | 
        fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n", | 
| 438 | 
+ | 
                        mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n); | 
| 439 | 
+ | 
#endif | 
| 440 | 
+ | 
        rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1)); | 
| 441 | 
+ | 
        if (rbf == NULL) | 
| 442 | 
+ | 
                goto memerr; | 
| 443 | 
+ | 
        rbf->next = NULL; rbf->ejl = NULL; | 
| 444 | 
+ | 
        VCOPY(rbf->invec, invec); | 
| 445 | 
+ | 
        rbf->nrbf = n; | 
| 446 | 
+ | 
        rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal; | 
| 447 | 
+ | 
        n = 0;                                  /* advect RBF lobes */ | 
| 448 | 
+ | 
        for (i = 0; i < mtx_nrows(mig); i++) { | 
| 449 | 
+ | 
            const RBFVAL        *rbf0i = &mig->rbfv[0]->rbfa[i]; | 
| 450 | 
+ | 
            const float         peak0 = rbf0i->peak; | 
| 451 | 
+ | 
            const double        rad0 = R2ANG(rbf0i->crad); | 
| 452 | 
+ | 
            FVECT               v0; | 
| 453 | 
+ | 
            float               mv; | 
| 454 | 
+ | 
            ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); | 
| 455 | 
+ | 
            for (j = 0; j < mtx_ncols(mig); j++) | 
| 456 | 
+ | 
                if ((mv = mtx_coef(mig,i,j)) > cthresh) { | 
| 457 | 
+ | 
                        const RBFVAL    *rbf1j = &mig->rbfv[1]->rbfa[j]; | 
| 458 | 
+ | 
                        double          rad2; | 
| 459 | 
+ | 
                        FVECT           v; | 
| 460 | 
+ | 
                        int             pos[2]; | 
| 461 | 
+ | 
                        rad2 = R2ANG(rbf1j->crad); | 
| 462 | 
+ | 
                        rad2 = rad0*rad0*(1.-t) + rad2*rad2*t; | 
| 463 | 
+ | 
                        rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal * | 
| 464 | 
+ | 
                                                rad0*rad0/rad2; | 
| 465 | 
+ | 
                        rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); | 
| 466 | 
+ | 
                        ovec_from_pos(v, rbf1j->gx, rbf1j->gy); | 
| 467 | 
+ | 
                        geodesic(v, v0, v, t, GEOD_REL); | 
| 468 | 
+ | 
                        pos_from_vec(pos, v); | 
| 469 | 
+ | 
                        rbf->rbfa[n].gx = pos[0]; | 
| 470 | 
+ | 
                        rbf->rbfa[n].gy = pos[1]; | 
| 471 | 
+ | 
                        ++n; | 
| 472 | 
+ | 
                } | 
| 473 | 
+ | 
        } | 
| 474 | 
+ | 
        rbf->vtotal *= mig->rbfv[0]->vtotal;    /* turn ratio into actual */ | 
| 475 | 
+ | 
        return(rbf); | 
| 476 | 
+ | 
memerr: | 
| 477 | 
+ | 
        fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname); | 
| 478 | 
+ | 
        exit(1); | 
| 479 | 
+ | 
        return(NULL);   /* pro forma return */ | 
| 480 | 
+ | 
} | 
| 481 | 
+ | 
 | 
| 482 | 
  | 
/* Clear our BSDF representation and free memory */ | 
| 483 | 
  | 
void | 
| 484 | 
  | 
clear_bsdf_rep(void) | 
| 493 | 
  | 
                dsf_list = rbf->next; | 
| 494 | 
  | 
                free(rbf); | 
| 495 | 
  | 
        } | 
| 496 | 
+ | 
        bsdf_name[0] = '\0'; | 
| 497 | 
+ | 
        bsdf_manuf[0] = '\0'; | 
| 498 | 
  | 
        inp_coverage = 0; | 
| 499 | 
  | 
        single_plane_incident = -1; | 
| 500 | 
  | 
        input_orient = output_orient = 0; | 
| 509 | 
  | 
        MIGRATION       *mig; | 
| 510 | 
  | 
        int             i, n; | 
| 511 | 
  | 
                                        /* finish header */ | 
| 512 | 
+ | 
        if (bsdf_name[0]) | 
| 513 | 
+ | 
                fprintf(ofp, "NAME=%s\n", bsdf_name); | 
| 514 | 
+ | 
        if (bsdf_manuf[0]) | 
| 515 | 
+ | 
                fprintf(ofp, "MANUFACT=%s\n", bsdf_manuf); | 
| 516 | 
  | 
        fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); | 
| 517 | 
  | 
        fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); | 
| 518 | 
  | 
        fprintf(ofp, "GRIDRES=%d\n", grid_res); | 
| 569 | 
  | 
{ | 
| 570 | 
  | 
        char    fmt[32]; | 
| 571 | 
  | 
 | 
| 572 | 
+ | 
        if (!strncmp(s, "NAME=", 5)) { | 
| 573 | 
+ | 
                strcpy(bsdf_name, s+5); | 
| 574 | 
+ | 
                bsdf_name[strlen(bsdf_name)-1] = '\0'; | 
| 575 | 
+ | 
        } | 
| 576 | 
+ | 
        if (!strncmp(s, "MANUFACT=", 9)) { | 
| 577 | 
+ | 
                strcpy(bsdf_manuf, s+9); | 
| 578 | 
+ | 
                bsdf_manuf[strlen(bsdf_manuf)-1] = '\0'; | 
| 579 | 
+ | 
        } | 
| 580 | 
  | 
        if (!strncmp(s, "SYMMETRY=", 9)) { | 
| 581 | 
  | 
                inp_coverage = atoi(s+9); | 
| 582 | 
  | 
                single_plane_incident = !inp_coverage; | 
| 610 | 
  | 
        clear_bsdf_rep(); | 
| 611 | 
  | 
        if (ifp == NULL) | 
| 612 | 
  | 
                return(0); | 
| 613 | 
< | 
        if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | | 
| 613 | 
> | 
        if (getheader(ifp, headline, NULL) < 0 || (single_plane_incident < 0) | | 
| 614 | 
  | 
                        !input_orient | !output_orient) { | 
| 615 | 
  | 
                fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", | 
| 616 | 
  | 
                                progname); | 
| 617 | 
  | 
                return(0); | 
| 618 | 
  | 
        } | 
| 619 | 
< | 
        rbfh.next = NULL;               /* read each DSF */ | 
| 510 | 
< | 
        rbfh.ejl = NULL; | 
| 619 | 
> | 
        memset(&rbfh, 0, sizeof(rbfh)); /* read each DSF */ | 
| 620 | 
  | 
        while ((rbfh.ord = getint(4, ifp)) >= 0) { | 
| 621 | 
  | 
                RBFNODE         *newrbf; | 
| 622 | 
  | 
 | 
| 633 | 
  | 
                                        sizeof(RBFVAL)*(rbfh.nrbf-1)); | 
| 634 | 
  | 
                if (newrbf == NULL) | 
| 635 | 
  | 
                        goto memerr; | 
| 636 | 
< | 
                memcpy(newrbf, &rbfh, sizeof(RBFNODE)-sizeof(RBFVAL)); | 
| 636 | 
> | 
                *newrbf = rbfh; | 
| 637 | 
  | 
                for (i = 0; i < rbfh.nrbf; i++) { | 
| 638 | 
  | 
                        newrbf->rbfa[i].peak = getflt(ifp); | 
| 639 | 
  | 
                        newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff; |