ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfrep.c
(Generate patch)

Comparing ray/src/cv/bsdfrep.c (file contents):
Revision 2.1 by greg, Fri Oct 19 04:14:29 2012 UTC vs.
Revision 2.24 by greg, Tue Mar 25 14:55:35 2014 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9  
10   #define _USE_MATH_DEFINES
11   #include <stdlib.h>
12 + #include <string.h>
13   #include <math.h>
14   #include "rtio.h"
15   #include "resolu.h"
16   #include "bsdfrep.h"
17 <                                /* which quadrants are represented */
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 >
23 >                                /* coverage/symmetry using INP_QUAD? flags */
24   int                     inp_coverage = 0;
25                                  /* all incident angles in-plane so far? */
26   int                     single_plane_incident = -1;
# Line 22 | Line 29 | int                    single_plane_incident = -1;
29   int                     input_orient = 0;
30   int                     output_orient = 0;
31  
32 +                                /* BSDF histogram */
33 + unsigned long           bsdf_hist[HISTLEN];
34 +
35 +                                /* BSDF value for boundary regions */
36 + double                  bsdf_min = 0;
37 +
38                                  /* processed incident DSF measurements */
39   RBFNODE                 *dsf_list = NULL;
40  
# Line 52 | Line 65 | new_input_direction(double new_theta, double new_phi)
65                  new_theta = -new_theta;
66                  new_phi += 180.;
67          }
68 +        if ((theta_in_deg = new_theta) < 1.0)
69 +                return(1);              /* don't rely on phi near normal */
70          while (new_phi < 0)
71                  new_phi += 360.;
72          while (new_phi >= 360.)
# Line 60 | Line 75 | new_input_direction(double new_theta, double new_phi)
75                  single_plane_incident = (round(new_phi) == round(phi_in_deg));
76          else if (single_plane_incident < 0)
77                  single_plane_incident = 1;
63        theta_in_deg = new_theta;       /* assume it's OK */
78          phi_in_deg = new_phi;
79          if ((1. < new_phi) & (new_phi < 89.))
80                  inp_coverage |= INP_QUAD1;
# Line 77 | Line 91 | new_input_direction(double new_theta, double new_phi)
91   int
92   use_symmetry(FVECT vec)
93   {
94 <        double  phi = get_phi360(vec);
94 >        const double    phi = get_phi360(vec);
95  
96          switch (inp_coverage) {
97          case INP_QUAD1|INP_QUAD2|INP_QUAD3|INP_QUAD4:
# Line 167 | Line 181 | rev_rbf_symmetry(RBFNODE *rbf, int sym)
181          rev_symmetry(rbf->invec, sym);
182          if (sym & MIRROR_X)
183                  for (n = rbf->nrbf; n-- > 0; )
184 <                        rbf->rbfa[n].gx = GRIDRES-1 - rbf->rbfa[n].gx;
184 >                        rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx;
185          if (sym & MIRROR_Y)
186                  for (n = rbf->nrbf; n-- > 0; )
187 <                        rbf->rbfa[n].gy = GRIDRES-1 - rbf->rbfa[n].gy;
187 >                        rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy;
188   }
189  
190 < /* Compute volume associated with Gaussian lobe */
191 < double
192 < rbf_volume(const RBFVAL *rbfp)
190 > /* Rotate RBF to correspond to given incident vector */
191 > void
192 > rotate_rbf(RBFNODE *rbf, const FVECT invec)
193   {
194 <        double  rad = R2ANG(rbfp->crad);
194 >        static const FVECT      vnorm = {.0, .0, 1.};
195 >        const double            phi = atan2(invec[1],invec[0]) -
196 >                                        atan2(rbf->invec[1],rbf->invec[0]);
197 >        FVECT                   outvec;
198 >        int                     pos[2];
199 >        int                     n;
200  
201 <        return((2.*M_PI) * rbfp->peak * rad*rad);
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);
205 >                rbf->rbfa[n].gx = pos[0];
206 >                rbf->rbfa[n].gy = pos[1];
207 >        }
208 >        VCOPY(rbf->invec, invec);
209   }
210  
211   /* Compute outgoing vector from grid position */
# Line 189 | Line 215 | ovec_from_pos(FVECT vec, int xpos, int ypos)
215          double  uv[2];
216          double  r2;
217          
218 <        SDsquare2disk(uv, (1./GRIDRES)*(xpos+.5), (1./GRIDRES)*(ypos+.5));
218 >        SDsquare2disk(uv, (xpos+.5)/grid_res, (ypos+.5)/grid_res);
219                                  /* uniform hemispherical projection */
220          r2 = uv[0]*uv[0] + uv[1]*uv[1];
221          vec[0] = vec[1] = sqrt(2. - r2);
# Line 207 | Line 233 | pos_from_vec(int pos[2], const FVECT vec)
233  
234          SDdisk2square(sq, vec[0]*norm, vec[1]*norm);
235  
236 <        pos[0] = (int)(sq[0]*GRIDRES);
237 <        pos[1] = (int)(sq[1]*GRIDRES);
236 >        pos[0] = (int)(sq[0]*grid_res);
237 >        pos[1] = (int)(sq[1]*grid_res);
238   }
239  
240 + /* Compute volume associated with Gaussian lobe */
241 + double
242 + rbf_volume(const RBFVAL *rbfp)
243 + {
244 +        double  rad = R2ANG(rbfp->crad);
245 +        FVECT   odir;
246 +        double  elev, integ;
247 +                                /* infinite integral approximation */
248 +        integ = (2.*M_PI) * rbfp->peak * rad*rad;
249 +                                /* check if we're near horizon */
250 +        ovec_from_pos(odir, rbfp->gx, rbfp->gy);
251 +        elev = output_orient*odir[2];
252 +                                /* apply cut-off correction if > 1% */
253 +        if (elev < 2.8*rad) {
254 +                /* elev = asin(elev);   /* this is so crude, anyway... */
255 +                integ *= 1. - .5*exp(-.5*elev*elev/(rad*rad));
256 +        }
257 +        return(integ);
258 + }
259 +
260   /* Evaluate RBF for DSF at the given normalized outgoing direction */
261   double
262   eval_rbfrep(const RBFNODE *rp, const FVECT outvec)
263   {
264 <        double          res = .0;
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 <
273 <        if (rp == NULL)
272 >                                /* check for wrong side */
273 >        if (outvec[2] > 0 ^ output_orient > 0)
274                  return(.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);
230 <                sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2);
231 <                if (sig2 > -19.)
232 <                        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);
294          return(res);
295   }
296  
# Line 244 | Line 304 | insert_dsf(RBFNODE *newrbf)
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                  }
# Line 280 | Line 341 | get_dsf(int ord)
341          RBFNODE         *rbf;
342  
343          for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
344 <                if (rbf->ord == ord);
344 >                if (rbf->ord == ord)
345                          return(rbf);
346          return(NULL);
347   }
# Line 311 | Line 372 | is_rev_tri(const FVECT v1, const FVECT v2, const FVECT
372   int
373   get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
374   {
375 <        const MIGRATION *ej, *ej2;
375 >        const MIGRATION *ej1, *ej2;
376          RBFNODE         *tv;
377  
378          rbfv[0] = rbfv[1] = NULL;
379          if (mig == NULL)
380                  return(0);
381 <        for (ej = mig->rbfv[0]->ejl; ej != NULL;
382 <                                ej = nextedge(mig->rbfv[0],ej)) {
383 <                if (ej == mig)
381 >        for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
382 >                                ej1 = nextedge(mig->rbfv[0],ej1)) {
383 >                if (ej1 == mig)
384                          continue;
385 <                tv = opp_rbf(mig->rbfv[0],ej);
385 >                tv = opp_rbf(mig->rbfv[0],ej1);
386                  for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
387                          if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
388                                  rbfv[is_rev_tri(mig->rbfv[0]->invec,
# Line 333 | Line 394 | get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
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)
486 + {
487 +        while (mig_list != NULL) {
488 +                MIGRATION       *mig = mig_list;
489 +                mig_list = mig->next;
490 +                free(mig);
491 +        }
492 +        while (dsf_list != NULL) {
493 +                RBFNODE         *rbf = dsf_list;
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;
502 +        grid_res = GRIDRES;
503 + }
504 +
505   /* Write our BSDF mesh interpolant out to the given binary stream */
506   void
507   save_bsdf_rep(FILE *ofp)
# Line 341 | Line 510 | save_bsdf_rep(FILE *ofp)
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);
520 +        fprintf(ofp, "BSDFMIN=%g\n", bsdf_min);
521          fputformat(BSDFREP_FMT, ofp);
522          fputc('\n', ofp);
523                                          /* write each DSF */
# Line 360 | Line 537 | save_bsdf_rep(FILE *ofp)
537          }
538          putint(-1, 4, ofp);             /* terminator */
539                                          /* write each migration matrix */
540 <        for (mig = mig_list; mig != NULL; mig = mig_list->next) {
540 >        for (mig = mig_list; mig != NULL; mig = mig->next) {
541 >                int     zerocnt = 0;
542                  putint(mig->rbfv[0]->ord, 4, ofp);
543                  putint(mig->rbfv[1]->ord, 4, ofp);
544 +                                        /* write out as sparse data */
545                  n = mtx_nrows(mig) * mtx_ncols(mig);
546 <                for (i = 0; i < n; i++)
547 <                        putflt(mig->mtx[i], ofp);
546 >                for (i = 0; i < n; i++) {
547 >                        if (zerocnt == 0xff) {
548 >                                putint(0xff, 1, ofp); zerocnt = 0;
549 >                        }
550 >                        if (mig->mtx[i] != 0) {
551 >                                putint(zerocnt, 1, ofp); zerocnt = 0;
552 >                                putflt(mig->mtx[i], ofp);
553 >                        } else
554 >                                ++zerocnt;
555 >                }
556 >                putint(zerocnt, 1, ofp);
557          }
558          putint(-1, 4, ofp);             /* terminator */
559          putint(-1, 4, ofp);
# Line 376 | Line 564 | save_bsdf_rep(FILE *ofp)
564          }
565   }
566  
567 + /* Check header line for critical information */
568 + static int
569 + headline(char *s, void *p)
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;
584 +                return(0);
585 +        }
586 +        if (!strncmp(s, "IO_SIDES=", 9)) {
587 +                sscanf(s+9, "%d %d", &input_orient, &output_orient);
588 +                return(0);
589 +        }
590 +        if (!strncmp(s, "GRIDRES=", 8)) {
591 +                sscanf(s+8, "%d", &grid_res);
592 +                return(0);
593 +        }
594 +        if (!strncmp(s, "BSDFMIN=", 8)) {
595 +                sscanf(s+8, "%lf", &bsdf_min);
596 +                return(0);
597 +        }
598 +        if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
599 +                return(-1);
600 +        return(0);
601 + }
602 +
603   /* Read a BSDF mesh interpolant from the given binary stream */
604   int
605   load_bsdf_rep(FILE *ifp)
# Line 383 | Line 607 | load_bsdf_rep(FILE *ifp)
607          RBFNODE         rbfh;
608          int             from_ord, to_ord;
609          int             i;
610 < #ifdef DEBUG
611 <        if ((dsf_list != NULL) | (mig_list != NULL)) {
612 <                fprintf(stderr,
389 <                "%s: attempt to load BSDF interpolant over existing\n",
390 <                                progname);
610 >
611 >        clear_bsdf_rep();
612 >        if (ifp == NULL)
613                  return(0);
614 <        }
615 < #endif
616 <        if (checkheader(ifp, BSDFREP_FMT, NULL) <= 0) {
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 */
400 <        rbfh.ejl = NULL;
621 >        memset(&rbfh, 0, sizeof(rbfh)); /* read each DSF */
622          while ((rbfh.ord = getint(4, ifp)) >= 0) {
623                  RBFNODE         *newrbf;
624  
625                  rbfh.invec[0] = getflt(ifp);
626                  rbfh.invec[1] = getflt(ifp);
627                  rbfh.invec[2] = getflt(ifp);
628 <                rbfh.nrbf = getint(4, ifp);
629 <                if (!new_input_vector(rbfh.invec))
628 >                if (normalize(rbfh.invec) == 0) {
629 >                        fprintf(stderr, "%s: zero incident vector\n", progname);
630                          return(0);
631 +                }
632 +                rbfh.vtotal = getflt(ifp);
633 +                rbfh.nrbf = getint(4, ifp);
634                  newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
635                                          sizeof(RBFVAL)*(rbfh.nrbf-1));
636                  if (newrbf == NULL)
637                          goto memerr;
638 <                memcpy(newrbf, &rbfh, sizeof(RBFNODE));
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;
# Line 447 | Line 671 | load_bsdf_rep(FILE *ifp)
671                          goto memerr;
672                  newmig->rbfv[0] = from_rbf;
673                  newmig->rbfv[1] = to_rbf;
674 <                                        /* read matrix coefficients */
675 <                for (i = 0; i < n; i++)
676 <                        newmig->mtx[i] = getflt(ifp);
674 >                memset(newmig->mtx, 0, sizeof(float)*n);
675 >                for (i = 0; ; ) {       /* read sparse data */
676 >                        int     zc = getint(1, ifp) & 0xff;
677 >                        if ((i += zc) >= n)
678 >                                break;
679 >                        if (zc == 0xff)
680 >                                continue;
681 >                        newmig->mtx[i++] = getflt(ifp);
682 >                }
683                  if (feof(ifp))
684                          goto badEOF;
685                                          /* insert in edge lists */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines