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.15 by greg, Sat Oct 19 00:11:50 2013 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 >                                /* 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? */
23   int                     single_plane_incident = -1;
# Line 22 | Line 26 | int                    single_plane_incident = -1;
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  
# Line 52 | Line 62 | new_input_direction(double new_theta, double new_phi)
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.)
# Line 60 | Line 72 | new_input_direction(double new_theta, double new_phi)
72                  single_plane_incident = (round(new_phi) == round(phi_in_deg));
73          else if (single_plane_incident < 0)
74                  single_plane_incident = 1;
63        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;
# Line 77 | Line 88 | new_input_direction(double new_theta, double new_phi)
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:
# Line 167 | Line 178 | rev_rbf_symmetry(RBFNODE *rbf, int sym)
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 */
# Line 189 | Line 212 | ovec_from_pos(FVECT vec, int xpos, int ypos)
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);
# Line 207 | Line 230 | pos_from_vec(int pos[2], const FVECT vec)
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 >        double          minval = bsdf_min*output_orient*outvec[2];
262 >        double          res = 0;
263          const RBFVAL    *rbfp;
264          FVECT           odir;
265          double          sig2;
266          int             n;
267 <
268 <        if (rp == NULL)
267 >                                /* check for wrong side */
268 >        if (outvec[2] > 0 ^ output_orient > 0)
269                  return(.0);
270 +                                /* use minimum if no information avail. */
271 +        if (rp == NULL)
272 +                return(minval);
273 +                                /* sum radial basis function */
274          rbfp = rp->rbfa;
275          for (n = rp->nrbf; n--; rbfp++) {
276                  ovec_from_pos(odir, rbfp->gx, rbfp->gy);
# Line 231 | Line 279 | eval_rbfrep(const RBFNODE *rp, const FVECT outvec)
279                  if (sig2 > -19.)
280                          res += rbfp->peak * exp(sig2);
281          }
282 +        if (res < minval)       /* never return less than minval */
283 +                return(minval);
284          return(res);
285   }
286  
# Line 280 | Line 330 | get_dsf(int ord)
330          RBFNODE         *rbf;
331  
332          for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
333 <                if (rbf->ord == ord);
333 >                if (rbf->ord == ord)
334                          return(rbf);
335          return(NULL);
336   }
# Line 311 | Line 361 | is_rev_tri(const FVECT v1, const FVECT v2, const FVECT
361   int
362   get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
363   {
364 <        const MIGRATION *ej, *ej2;
364 >        const MIGRATION *ej1, *ej2;
365          RBFNODE         *tv;
366  
367          rbfv[0] = rbfv[1] = NULL;
368          if (mig == NULL)
369                  return(0);
370 <        for (ej = mig->rbfv[0]->ejl; ej != NULL;
371 <                                ej = nextedge(mig->rbfv[0],ej)) {
372 <                if (ej == mig)
370 >        for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
371 >                                ej1 = nextedge(mig->rbfv[0],ej1)) {
372 >                if (ej1 == mig)
373                          continue;
374 <                tv = opp_rbf(mig->rbfv[0],ej);
374 >                tv = opp_rbf(mig->rbfv[0],ej1);
375                  for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
376                          if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
377                                  rbfv[is_rev_tri(mig->rbfv[0]->invec,
# Line 333 | Line 383 | get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
383          return((rbfv[0] != NULL) + (rbfv[1] != NULL));
384   }
385  
386 + /* Clear our BSDF representation and free memory */
387 + void
388 + clear_bsdf_rep(void)
389 + {
390 +        while (mig_list != NULL) {
391 +                MIGRATION       *mig = mig_list;
392 +                mig_list = mig->next;
393 +                free(mig);
394 +        }
395 +        while (dsf_list != NULL) {
396 +                RBFNODE         *rbf = dsf_list;
397 +                dsf_list = rbf->next;
398 +                free(rbf);
399 +        }
400 +        inp_coverage = 0;
401 +        single_plane_incident = -1;
402 +        input_orient = output_orient = 0;
403 +        grid_res = GRIDRES;
404 + }
405 +
406   /* Write our BSDF mesh interpolant out to the given binary stream */
407   void
408   save_bsdf_rep(FILE *ofp)
# Line 341 | Line 411 | save_bsdf_rep(FILE *ofp)
411          MIGRATION       *mig;
412          int             i, n;
413                                          /* finish header */
414 +        fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage);
415 +        fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient);
416 +        fprintf(ofp, "GRIDRES=%d\n", grid_res);
417 +        fprintf(ofp, "BSDFMIN=%g\n", bsdf_min);
418          fputformat(BSDFREP_FMT, ofp);
419          fputc('\n', ofp);
420                                          /* write each DSF */
# Line 360 | Line 434 | save_bsdf_rep(FILE *ofp)
434          }
435          putint(-1, 4, ofp);             /* terminator */
436                                          /* write each migration matrix */
437 <        for (mig = mig_list; mig != NULL; mig = mig_list->next) {
437 >        for (mig = mig_list; mig != NULL; mig = mig->next) {
438 >                int     zerocnt = 0;
439                  putint(mig->rbfv[0]->ord, 4, ofp);
440                  putint(mig->rbfv[1]->ord, 4, ofp);
441 +                                        /* write out as sparse data */
442                  n = mtx_nrows(mig) * mtx_ncols(mig);
443 <                for (i = 0; i < n; i++)
444 <                        putflt(mig->mtx[i], ofp);
443 >                for (i = 0; i < n; i++) {
444 >                        if (zerocnt == 0xff) {
445 >                                putint(0xff, 1, ofp); zerocnt = 0;
446 >                        }
447 >                        if (mig->mtx[i] != 0) {
448 >                                putint(zerocnt, 1, ofp); zerocnt = 0;
449 >                                putflt(mig->mtx[i], ofp);
450 >                        } else
451 >                                ++zerocnt;
452 >                }
453 >                putint(zerocnt, 1, ofp);
454          }
455          putint(-1, 4, ofp);             /* terminator */
456          putint(-1, 4, ofp);
# Line 376 | Line 461 | save_bsdf_rep(FILE *ofp)
461          }
462   }
463  
464 + /* Check header line for critical information */
465 + static int
466 + headline(char *s, void *p)
467 + {
468 +        char    fmt[32];
469 +
470 +        if (!strncmp(s, "SYMMETRY=", 9)) {
471 +                inp_coverage = atoi(s+9);
472 +                single_plane_incident = !inp_coverage;
473 +                return(0);
474 +        }
475 +        if (!strncmp(s, "IO_SIDES=", 9)) {
476 +                sscanf(s+9, "%d %d", &input_orient, &output_orient);
477 +                return(0);
478 +        }
479 +        if (!strncmp(s, "GRIDRES=", 8)) {
480 +                sscanf(s+8, "%d", &grid_res);
481 +                return(0);
482 +        }
483 +        if (!strncmp(s, "BSDFMIN=", 8)) {
484 +                sscanf(s+8, "%lf", &bsdf_min);
485 +                return(0);
486 +        }
487 +        if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
488 +                return(-1);
489 +        return(0);
490 + }
491 +
492   /* Read a BSDF mesh interpolant from the given binary stream */
493   int
494   load_bsdf_rep(FILE *ifp)
# Line 383 | Line 496 | load_bsdf_rep(FILE *ifp)
496          RBFNODE         rbfh;
497          int             from_ord, to_ord;
498          int             i;
499 < #ifdef DEBUG
500 <        if ((dsf_list != NULL) | (mig_list != NULL)) {
501 <                fprintf(stderr,
389 <                "%s: attempt to load BSDF interpolant over existing\n",
390 <                                progname);
499 >
500 >        clear_bsdf_rep();
501 >        if (ifp == NULL)
502                  return(0);
503 <        }
504 < #endif
394 <        if (checkheader(ifp, BSDFREP_FMT, NULL) <= 0) {
503 >        if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 |
504 >                        !input_orient | !output_orient) {
505                  fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
506                                  progname);
507                  return(0);
# Line 404 | Line 514 | load_bsdf_rep(FILE *ifp)
514                  rbfh.invec[0] = getflt(ifp);
515                  rbfh.invec[1] = getflt(ifp);
516                  rbfh.invec[2] = getflt(ifp);
517 <                rbfh.nrbf = getint(4, ifp);
518 <                if (!new_input_vector(rbfh.invec))
517 >                if (normalize(rbfh.invec) == 0) {
518 >                        fprintf(stderr, "%s: zero incident vector\n", progname);
519                          return(0);
520 +                }
521 +                rbfh.vtotal = getflt(ifp);
522 +                rbfh.nrbf = getint(4, ifp);
523                  newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
524                                          sizeof(RBFVAL)*(rbfh.nrbf-1));
525                  if (newrbf == NULL)
526                          goto memerr;
527 <                memcpy(newrbf, &rbfh, sizeof(RBFNODE));
527 >                memcpy(newrbf, &rbfh, sizeof(RBFNODE)-sizeof(RBFVAL));
528                  for (i = 0; i < rbfh.nrbf; i++) {
529                          newrbf->rbfa[i].peak = getflt(ifp);
530                          newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;
# Line 447 | Line 560 | load_bsdf_rep(FILE *ifp)
560                          goto memerr;
561                  newmig->rbfv[0] = from_rbf;
562                  newmig->rbfv[1] = to_rbf;
563 <                                        /* read matrix coefficients */
564 <                for (i = 0; i < n; i++)
565 <                        newmig->mtx[i] = getflt(ifp);
563 >                memset(newmig->mtx, 0, sizeof(float)*n);
564 >                for (i = 0; ; ) {       /* read sparse data */
565 >                        int     zc = getint(1, ifp) & 0xff;
566 >                        if ((i += zc) >= n)
567 >                                break;
568 >                        if (zc == 0xff)
569 >                                continue;
570 >                        newmig->mtx[i++] = getflt(ifp);
571 >                }
572                  if (feof(ifp))
573                          goto badEOF;
574                                          /* insert in edge lists */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines