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.19 by greg, Sat Nov 9 05:47:49 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 >                                /* 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 = ((-.01 > phi) | (phi > .01))*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 280 | Line 340 | get_dsf(int ord)
340          RBFNODE         *rbf;
341  
342          for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
343 <                if (rbf->ord == ord);
343 >                if (rbf->ord == ord)
344                          return(rbf);
345          return(NULL);
346   }
# Line 311 | Line 371 | is_rev_tri(const FVECT v1, const FVECT v2, const FVECT
371   int
372   get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
373   {
374 <        const MIGRATION *ej, *ej2;
374 >        const MIGRATION *ej1, *ej2;
375          RBFNODE         *tv;
376  
377          rbfv[0] = rbfv[1] = NULL;
378          if (mig == NULL)
379                  return(0);
380 <        for (ej = mig->rbfv[0]->ejl; ej != NULL;
381 <                                ej = nextedge(mig->rbfv[0],ej)) {
382 <                if (ej == mig)
380 >        for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
381 >                                ej1 = nextedge(mig->rbfv[0],ej1)) {
382 >                if (ej1 == mig)
383                          continue;
384 <                tv = opp_rbf(mig->rbfv[0],ej);
384 >                tv = opp_rbf(mig->rbfv[0],ej1);
385                  for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
386                          if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
387                                  rbfv[is_rev_tri(mig->rbfv[0]->invec,
# Line 333 | Line 393 | get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
393          return((rbfv[0] != NULL) + (rbfv[1] != NULL));
394   }
395  
396 + /* Clear our BSDF representation and free memory */
397 + void
398 + clear_bsdf_rep(void)
399 + {
400 +        while (mig_list != NULL) {
401 +                MIGRATION       *mig = mig_list;
402 +                mig_list = mig->next;
403 +                free(mig);
404 +        }
405 +        while (dsf_list != NULL) {
406 +                RBFNODE         *rbf = dsf_list;
407 +                dsf_list = rbf->next;
408 +                free(rbf);
409 +        }
410 +        bsdf_name[0] = '\0';
411 +        bsdf_manuf[0] = '\0';
412 +        inp_coverage = 0;
413 +        single_plane_incident = -1;
414 +        input_orient = output_orient = 0;
415 +        grid_res = GRIDRES;
416 + }
417 +
418   /* Write our BSDF mesh interpolant out to the given binary stream */
419   void
420   save_bsdf_rep(FILE *ofp)
# Line 341 | Line 423 | save_bsdf_rep(FILE *ofp)
423          MIGRATION       *mig;
424          int             i, n;
425                                          /* finish header */
426 +        if (bsdf_name[0])
427 +                fprintf(ofp, "NAME=%s\n", bsdf_name);
428 +        if (bsdf_manuf[0])
429 +                fprintf(ofp, "MANUFACT=%s\n", bsdf_manuf);
430 +        fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage);
431 +        fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient);
432 +        fprintf(ofp, "GRIDRES=%d\n", grid_res);
433 +        fprintf(ofp, "BSDFMIN=%g\n", bsdf_min);
434          fputformat(BSDFREP_FMT, ofp);
435          fputc('\n', ofp);
436                                          /* write each DSF */
# Line 360 | Line 450 | save_bsdf_rep(FILE *ofp)
450          }
451          putint(-1, 4, ofp);             /* terminator */
452                                          /* write each migration matrix */
453 <        for (mig = mig_list; mig != NULL; mig = mig_list->next) {
453 >        for (mig = mig_list; mig != NULL; mig = mig->next) {
454 >                int     zerocnt = 0;
455                  putint(mig->rbfv[0]->ord, 4, ofp);
456                  putint(mig->rbfv[1]->ord, 4, ofp);
457 +                                        /* write out as sparse data */
458                  n = mtx_nrows(mig) * mtx_ncols(mig);
459 <                for (i = 0; i < n; i++)
460 <                        putflt(mig->mtx[i], ofp);
459 >                for (i = 0; i < n; i++) {
460 >                        if (zerocnt == 0xff) {
461 >                                putint(0xff, 1, ofp); zerocnt = 0;
462 >                        }
463 >                        if (mig->mtx[i] != 0) {
464 >                                putint(zerocnt, 1, ofp); zerocnt = 0;
465 >                                putflt(mig->mtx[i], ofp);
466 >                        } else
467 >                                ++zerocnt;
468 >                }
469 >                putint(zerocnt, 1, ofp);
470          }
471          putint(-1, 4, ofp);             /* terminator */
472          putint(-1, 4, ofp);
# Line 376 | Line 477 | save_bsdf_rep(FILE *ofp)
477          }
478   }
479  
480 + /* Check header line for critical information */
481 + static int
482 + headline(char *s, void *p)
483 + {
484 +        char    fmt[32];
485 +
486 +        if (!strncmp(s, "NAME=", 5)) {
487 +                strcpy(bsdf_name, s+5);
488 +                bsdf_name[strlen(bsdf_name)-1] = '\0';
489 +        }
490 +        if (!strncmp(s, "MANUFACT=", 9)) {
491 +                strcpy(bsdf_manuf, s+9);
492 +                bsdf_manuf[strlen(bsdf_manuf)-1] = '\0';
493 +        }
494 +        if (!strncmp(s, "SYMMETRY=", 9)) {
495 +                inp_coverage = atoi(s+9);
496 +                single_plane_incident = !inp_coverage;
497 +                return(0);
498 +        }
499 +        if (!strncmp(s, "IO_SIDES=", 9)) {
500 +                sscanf(s+9, "%d %d", &input_orient, &output_orient);
501 +                return(0);
502 +        }
503 +        if (!strncmp(s, "GRIDRES=", 8)) {
504 +                sscanf(s+8, "%d", &grid_res);
505 +                return(0);
506 +        }
507 +        if (!strncmp(s, "BSDFMIN=", 8)) {
508 +                sscanf(s+8, "%lf", &bsdf_min);
509 +                return(0);
510 +        }
511 +        if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
512 +                return(-1);
513 +        return(0);
514 + }
515 +
516   /* Read a BSDF mesh interpolant from the given binary stream */
517   int
518   load_bsdf_rep(FILE *ifp)
# Line 383 | Line 520 | load_bsdf_rep(FILE *ifp)
520          RBFNODE         rbfh;
521          int             from_ord, to_ord;
522          int             i;
523 < #ifdef DEBUG
524 <        if ((dsf_list != NULL) | (mig_list != NULL)) {
525 <                fprintf(stderr,
389 <                "%s: attempt to load BSDF interpolant over existing\n",
390 <                                progname);
523 >
524 >        clear_bsdf_rep();
525 >        if (ifp == NULL)
526                  return(0);
527 <        }
528 < #endif
394 <        if (checkheader(ifp, BSDFREP_FMT, NULL) <= 0) {
527 >        if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 |
528 >                        !input_orient | !output_orient) {
529                  fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
530                                  progname);
531                  return(0);
532          }
533 <        rbfh.next = NULL;               /* read each DSF */
400 <        rbfh.ejl = NULL;
533 >        memset(&rbfh, 0, sizeof(rbfh)); /* read each DSF */
534          while ((rbfh.ord = getint(4, ifp)) >= 0) {
535                  RBFNODE         *newrbf;
536  
537                  rbfh.invec[0] = getflt(ifp);
538                  rbfh.invec[1] = getflt(ifp);
539                  rbfh.invec[2] = getflt(ifp);
540 <                rbfh.nrbf = getint(4, ifp);
541 <                if (!new_input_vector(rbfh.invec))
540 >                if (normalize(rbfh.invec) == 0) {
541 >                        fprintf(stderr, "%s: zero incident vector\n", progname);
542                          return(0);
543 +                }
544 +                rbfh.vtotal = getflt(ifp);
545 +                rbfh.nrbf = getint(4, ifp);
546                  newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
547                                          sizeof(RBFVAL)*(rbfh.nrbf-1));
548                  if (newrbf == NULL)
549                          goto memerr;
550 <                memcpy(newrbf, &rbfh, sizeof(RBFNODE));
550 >                *newrbf = rbfh;
551                  for (i = 0; i < rbfh.nrbf; i++) {
552                          newrbf->rbfa[i].peak = getflt(ifp);
553                          newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;
# Line 447 | Line 583 | load_bsdf_rep(FILE *ifp)
583                          goto memerr;
584                  newmig->rbfv[0] = from_rbf;
585                  newmig->rbfv[1] = to_rbf;
586 <                                        /* read matrix coefficients */
587 <                for (i = 0; i < n; i++)
588 <                        newmig->mtx[i] = getflt(ifp);
586 >                memset(newmig->mtx, 0, sizeof(float)*n);
587 >                for (i = 0; ; ) {       /* read sparse data */
588 >                        int     zc = getint(1, ifp) & 0xff;
589 >                        if ((i += zc) >= n)
590 >                                break;
591 >                        if (zc == 0xff)
592 >                                continue;
593 >                        newmig->mtx[i++] = getflt(ifp);
594 >                }
595                  if (feof(ifp))
596                          goto badEOF;
597                                          /* insert in edge lists */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines