| 14 | 
  | 
#include "rtio.h" | 
| 15 | 
  | 
#include "resolu.h" | 
| 16 | 
  | 
#include "bsdfrep.h" | 
| 17 | 
+ | 
                                /* active grid resolution */ | 
| 18 | 
+ | 
int                     grid_res = GRIDRES; | 
| 19 | 
+ | 
 | 
| 20 | 
  | 
                                /* coverage/symmetry using INP_QUAD? flags */ | 
| 21 | 
  | 
int                     inp_coverage = 0; | 
| 22 | 
  | 
                                /* all incident angles in-plane so far? */ | 
| 26 | 
  | 
int                     input_orient = 0; | 
| 27 | 
  | 
int                     output_orient = 0; | 
| 28 | 
  | 
 | 
| 29 | 
+ | 
                                /* BSDF histogram */ | 
| 30 | 
+ | 
unsigned long           bsdf_hist[HISTLEN]; | 
| 31 | 
+ | 
 | 
| 32 | 
+ | 
                                /* BSDF value for boundary regions */ | 
| 33 | 
+ | 
double                  bsdf_min = 0; | 
| 34 | 
+ | 
 | 
| 35 | 
  | 
                                /* processed incident DSF measurements */ | 
| 36 | 
  | 
RBFNODE                 *dsf_list = NULL; | 
| 37 | 
  | 
 | 
| 62 | 
  | 
                new_theta = -new_theta; | 
| 63 | 
  | 
                new_phi += 180.; | 
| 64 | 
  | 
        } | 
| 65 | 
+ | 
        if ((theta_in_deg = new_theta) < 1.0) | 
| 66 | 
+ | 
                return(1);              /* don't rely on phi near normal */ | 
| 67 | 
  | 
        while (new_phi < 0) | 
| 68 | 
  | 
                new_phi += 360.; | 
| 69 | 
  | 
        while (new_phi >= 360.) | 
| 72 | 
  | 
                single_plane_incident = (round(new_phi) == round(phi_in_deg)); | 
| 73 | 
  | 
        else if (single_plane_incident < 0) | 
| 74 | 
  | 
                single_plane_incident = 1; | 
| 64 | 
– | 
        theta_in_deg = new_theta;       /* assume it's OK */ | 
| 75 | 
  | 
        phi_in_deg = new_phi; | 
| 76 | 
  | 
        if ((1. < new_phi) & (new_phi < 89.)) | 
| 77 | 
  | 
                inp_coverage |= INP_QUAD1; | 
| 88 | 
  | 
int | 
| 89 | 
  | 
use_symmetry(FVECT vec) | 
| 90 | 
  | 
{ | 
| 91 | 
< | 
        double  phi = get_phi360(vec); | 
| 91 | 
> | 
        const double    phi = get_phi360(vec); | 
| 92 | 
  | 
 | 
| 93 | 
  | 
        switch (inp_coverage) { | 
| 94 | 
  | 
        case INP_QUAD1|INP_QUAD2|INP_QUAD3|INP_QUAD4: | 
| 178 | 
  | 
        rev_symmetry(rbf->invec, sym); | 
| 179 | 
  | 
        if (sym & MIRROR_X) | 
| 180 | 
  | 
                for (n = rbf->nrbf; n-- > 0; ) | 
| 181 | 
< | 
                        rbf->rbfa[n].gx = GRIDRES-1 - rbf->rbfa[n].gx; | 
| 181 | 
> | 
                        rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx; | 
| 182 | 
  | 
        if (sym & MIRROR_Y) | 
| 183 | 
  | 
                for (n = rbf->nrbf; n-- > 0; ) | 
| 184 | 
< | 
                        rbf->rbfa[n].gy = GRIDRES-1 - rbf->rbfa[n].gy; | 
| 184 | 
> | 
                        rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy; | 
| 185 | 
  | 
} | 
| 186 | 
  | 
 | 
| 187 | 
< | 
/* Compute volume associated with Gaussian lobe */ | 
| 188 | 
< | 
double | 
| 189 | 
< | 
rbf_volume(const RBFVAL *rbfp) | 
| 187 | 
> | 
/* Rotate RBF to correspond to given incident vector */ | 
| 188 | 
> | 
void | 
| 189 | 
> | 
rotate_rbf(RBFNODE *rbf, const FVECT invec) | 
| 190 | 
  | 
{ | 
| 191 | 
< | 
        double  rad = R2ANG(rbfp->crad); | 
| 191 | 
> | 
        static const FVECT      vnorm = {.0, .0, 1.}; | 
| 192 | 
> | 
        const double            phi = atan2(invec[1],invec[0]) - | 
| 193 | 
> | 
                                        atan2(rbf->invec[1],rbf->invec[0]); | 
| 194 | 
> | 
        FVECT                   outvec; | 
| 195 | 
> | 
        int                     pos[2]; | 
| 196 | 
> | 
        int                     n; | 
| 197 | 
  | 
 | 
| 198 | 
< | 
        return((2.*M_PI) * rbfp->peak * rad*rad); | 
| 198 | 
> | 
        for (n = ((-.01 > phi) | (phi > .01))*rbf->nrbf; n-- > 0; ) { | 
| 199 | 
> | 
                ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy); | 
| 200 | 
> | 
                spinvector(outvec, outvec, vnorm, phi); | 
| 201 | 
> | 
                pos_from_vec(pos, outvec); | 
| 202 | 
> | 
                rbf->rbfa[n].gx = pos[0]; | 
| 203 | 
> | 
                rbf->rbfa[n].gy = pos[1]; | 
| 204 | 
> | 
        } | 
| 205 | 
> | 
        VCOPY(rbf->invec, invec); | 
| 206 | 
  | 
} | 
| 207 | 
  | 
 | 
| 208 | 
  | 
/* Compute outgoing vector from grid position */ | 
| 212 | 
  | 
        double  uv[2]; | 
| 213 | 
  | 
        double  r2; | 
| 214 | 
  | 
         | 
| 215 | 
< | 
        SDsquare2disk(uv, (1./GRIDRES)*(xpos+.5), (1./GRIDRES)*(ypos+.5)); | 
| 215 | 
> | 
        SDsquare2disk(uv, (xpos+.5)/grid_res, (ypos+.5)/grid_res); | 
| 216 | 
  | 
                                /* uniform hemispherical projection */ | 
| 217 | 
  | 
        r2 = uv[0]*uv[0] + uv[1]*uv[1]; | 
| 218 | 
  | 
        vec[0] = vec[1] = sqrt(2. - r2); | 
| 230 | 
  | 
 | 
| 231 | 
  | 
        SDdisk2square(sq, vec[0]*norm, vec[1]*norm); | 
| 232 | 
  | 
 | 
| 233 | 
< | 
        pos[0] = (int)(sq[0]*GRIDRES); | 
| 234 | 
< | 
        pos[1] = (int)(sq[1]*GRIDRES); | 
| 233 | 
> | 
        pos[0] = (int)(sq[0]*grid_res); | 
| 234 | 
> | 
        pos[1] = (int)(sq[1]*grid_res); | 
| 235 | 
  | 
} | 
| 236 | 
  | 
 | 
| 237 | 
+ | 
/* Compute volume associated with Gaussian lobe */ | 
| 238 | 
+ | 
double | 
| 239 | 
+ | 
rbf_volume(const RBFVAL *rbfp) | 
| 240 | 
+ | 
{ | 
| 241 | 
+ | 
        double  rad = R2ANG(rbfp->crad); | 
| 242 | 
+ | 
        FVECT   odir; | 
| 243 | 
+ | 
        double  elev, integ; | 
| 244 | 
+ | 
                                /* infinite integral approximation */ | 
| 245 | 
+ | 
        integ = (2.*M_PI) * rbfp->peak * rad*rad; | 
| 246 | 
+ | 
                                /* check if we're near horizon */ | 
| 247 | 
+ | 
        ovec_from_pos(odir, rbfp->gx, rbfp->gy); | 
| 248 | 
+ | 
        elev = output_orient*odir[2]; | 
| 249 | 
+ | 
                                /* apply cut-off correction if > 1% */ | 
| 250 | 
+ | 
        if (elev < 2.8*rad) { | 
| 251 | 
+ | 
                /* elev = asin(elev);   /* this is so crude, anyway... */ | 
| 252 | 
+ | 
                integ *= 1. - .5*exp(-.5*elev*elev/(rad*rad)); | 
| 253 | 
+ | 
        } | 
| 254 | 
+ | 
        return(integ); | 
| 255 | 
+ | 
} | 
| 256 | 
+ | 
 | 
| 257 | 
  | 
/* Evaluate RBF for DSF at the given normalized outgoing direction */ | 
| 258 | 
  | 
double | 
| 259 | 
  | 
eval_rbfrep(const RBFNODE *rp, const FVECT outvec) | 
| 260 | 
  | 
{ | 
| 261 | 
< | 
        double          res = .0; | 
| 261 | 
> | 
        const double    rfact2 = (38./M_PI/M_PI)*(grid_res*grid_res); | 
| 262 | 
> | 
        double          minval = bsdf_min*output_orient*outvec[2]; | 
| 263 | 
> | 
        int             pos[2]; | 
| 264 | 
> | 
        double          res = 0; | 
| 265 | 
  | 
        const RBFVAL    *rbfp; | 
| 266 | 
  | 
        FVECT           odir; | 
| 267 | 
< | 
        double          sig2; | 
| 267 | 
> | 
        double          rad2; | 
| 268 | 
  | 
        int             n; | 
| 269 | 
< | 
 | 
| 270 | 
< | 
        if (rp == NULL) | 
| 269 | 
> | 
                                /* check for wrong side */ | 
| 270 | 
> | 
        if (outvec[2] > 0 ^ output_orient > 0) | 
| 271 | 
  | 
                return(.0); | 
| 272 | 
+ | 
                                /* use minimum if no information avail. */ | 
| 273 | 
+ | 
        if (rp == NULL) | 
| 274 | 
+ | 
                return(minval); | 
| 275 | 
+ | 
                                /* optimization for fast lobe culling */ | 
| 276 | 
+ | 
        pos_from_vec(pos, outvec); | 
| 277 | 
+ | 
                                /* sum radial basis function */ | 
| 278 | 
  | 
        rbfp = rp->rbfa; | 
| 279 | 
  | 
        for (n = rp->nrbf; n--; rbfp++) { | 
| 280 | 
+ | 
                int     d2 = (pos[0]-rbfp->gx)*(pos[0]-rbfp->gx) + | 
| 281 | 
+ | 
                                (pos[1]-rbfp->gy)*(pos[1]-rbfp->gy); | 
| 282 | 
+ | 
                rad2 = R2ANG(rbfp->crad); | 
| 283 | 
+ | 
                rad2 *= rad2; | 
| 284 | 
+ | 
                if (d2 > rad2*rfact2) | 
| 285 | 
+ | 
                        continue; | 
| 286 | 
  | 
                ovec_from_pos(odir, rbfp->gx, rbfp->gy); | 
| 287 | 
< | 
                sig2 = R2ANG(rbfp->crad); | 
| 231 | 
< | 
                sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2); | 
| 232 | 
< | 
                if (sig2 > -19.) | 
| 233 | 
< | 
                        res += rbfp->peak * exp(sig2); | 
| 287 | 
> | 
                res += rbfp->peak * exp((DOT(odir,outvec) - 1.) / rad2); | 
| 288 | 
  | 
        } | 
| 289 | 
+ | 
        if (res < minval)       /* never return less than minval */ | 
| 290 | 
+ | 
                return(minval); | 
| 291 | 
  | 
        return(res); | 
| 292 | 
  | 
} | 
| 293 | 
  | 
 | 
| 407 | 
  | 
        inp_coverage = 0; | 
| 408 | 
  | 
        single_plane_incident = -1; | 
| 409 | 
  | 
        input_orient = output_orient = 0; | 
| 410 | 
+ | 
        grid_res = GRIDRES; | 
| 411 | 
  | 
} | 
| 412 | 
  | 
 | 
| 413 | 
  | 
/* Write our BSDF mesh interpolant out to the given binary stream */ | 
| 420 | 
  | 
                                        /* finish header */ | 
| 421 | 
  | 
        fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); | 
| 422 | 
  | 
        fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); | 
| 423 | 
+ | 
        fprintf(ofp, "GRIDRES=%d\n", grid_res); | 
| 424 | 
+ | 
        fprintf(ofp, "BSDFMIN=%g\n", bsdf_min); | 
| 425 | 
  | 
        fputformat(BSDFREP_FMT, ofp); | 
| 426 | 
  | 
        fputc('\n', ofp); | 
| 427 | 
  | 
                                        /* write each DSF */ | 
| 483 | 
  | 
                sscanf(s+9, "%d %d", &input_orient, &output_orient); | 
| 484 | 
  | 
                return(0); | 
| 485 | 
  | 
        } | 
| 486 | 
+ | 
        if (!strncmp(s, "GRIDRES=", 8)) { | 
| 487 | 
+ | 
                sscanf(s+8, "%d", &grid_res); | 
| 488 | 
+ | 
                return(0); | 
| 489 | 
+ | 
        } | 
| 490 | 
+ | 
        if (!strncmp(s, "BSDFMIN=", 8)) { | 
| 491 | 
+ | 
                sscanf(s+8, "%lf", &bsdf_min); | 
| 492 | 
+ | 
                return(0); | 
| 493 | 
+ | 
        } | 
| 494 | 
  | 
        if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT)) | 
| 495 | 
  | 
                return(-1); | 
| 496 | 
  | 
        return(0); | 
| 505 | 
  | 
        int             i; | 
| 506 | 
  | 
 | 
| 507 | 
  | 
        clear_bsdf_rep(); | 
| 508 | 
+ | 
        if (ifp == NULL) | 
| 509 | 
+ | 
                return(0); | 
| 510 | 
  | 
        if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | | 
| 511 | 
  | 
                        !input_orient | !output_orient) { | 
| 512 | 
  | 
                fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", | 
| 521 | 
  | 
                rbfh.invec[0] = getflt(ifp); | 
| 522 | 
  | 
                rbfh.invec[1] = getflt(ifp); | 
| 523 | 
  | 
                rbfh.invec[2] = getflt(ifp); | 
| 524 | 
+ | 
                if (normalize(rbfh.invec) == 0) { | 
| 525 | 
+ | 
                        fprintf(stderr, "%s: zero incident vector\n", progname); | 
| 526 | 
+ | 
                        return(0); | 
| 527 | 
+ | 
                } | 
| 528 | 
  | 
                rbfh.vtotal = getflt(ifp); | 
| 529 | 
  | 
                rbfh.nrbf = getint(4, ifp); | 
| 530 | 
  | 
                newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) + | 
| 531 | 
  | 
                                        sizeof(RBFVAL)*(rbfh.nrbf-1)); | 
| 532 | 
  | 
                if (newrbf == NULL) | 
| 533 | 
  | 
                        goto memerr; | 
| 534 | 
< | 
                memcpy(newrbf, &rbfh, sizeof(RBFNODE)); | 
| 534 | 
> | 
                memcpy(newrbf, &rbfh, sizeof(RBFNODE)-sizeof(RBFVAL)); | 
| 535 | 
  | 
                for (i = 0; i < rbfh.nrbf; i++) { | 
| 536 | 
  | 
                        newrbf->rbfa[i].peak = getflt(ifp); | 
| 537 | 
  | 
                        newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff; |