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.10 by greg, Sun Nov 18 03:56:39 2012 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 52 | Line 56 | new_input_direction(double new_theta, double new_phi)
56                  new_theta = -new_theta;
57                  new_phi += 180.;
58          }
59 +        if ((theta_in_deg = new_theta) < 1.0)
60 +                return(1);              /* don't rely on phi near normal */
61          while (new_phi < 0)
62                  new_phi += 360.;
63          while (new_phi >= 360.)
# Line 60 | Line 66 | new_input_direction(double new_theta, double new_phi)
66                  single_plane_incident = (round(new_phi) == round(phi_in_deg));
67          else if (single_plane_incident < 0)
68                  single_plane_incident = 1;
63        theta_in_deg = new_theta;       /* assume it's OK */
69          phi_in_deg = new_phi;
70          if ((1. < new_phi) & (new_phi < 89.))
71                  inp_coverage |= INP_QUAD1;
# Line 167 | Line 172 | rev_rbf_symmetry(RBFNODE *rbf, int sym)
172          rev_symmetry(rbf->invec, sym);
173          if (sym & MIRROR_X)
174                  for (n = rbf->nrbf; n-- > 0; )
175 <                        rbf->rbfa[n].gx = GRIDRES-1 - rbf->rbfa[n].gx;
175 >                        rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx;
176          if (sym & MIRROR_Y)
177                  for (n = rbf->nrbf; n-- > 0; )
178 <                        rbf->rbfa[n].gy = GRIDRES-1 - rbf->rbfa[n].gy;
178 >                        rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy;
179   }
180  
181 + /* Rotate RBF to correspond to given incident vector */
182 + void
183 + rotate_rbf(RBFNODE *rbf, const FVECT invec)
184 + {
185 +        static const FVECT      vnorm = {.0, .0, 1.};
186 +        const double            phi = atan2(invec[1],invec[0]) -
187 +                                        atan2(rbf->invec[1],rbf->invec[0]);
188 +        FVECT                   outvec;
189 +        int                     pos[2];
190 +        int                     n;
191 +
192 +        for (n = ((-.01 > phi) | (phi > .01))*rbf->nrbf; n-- > 0; ) {
193 +                ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy);
194 +                spinvector(outvec, outvec, vnorm, phi);
195 +                pos_from_vec(pos, outvec);
196 +                rbf->rbfa[n].gx = pos[0];
197 +                rbf->rbfa[n].gy = pos[1];
198 +        }
199 +        VCOPY(rbf->invec, invec);
200 + }
201 +
202   /* Compute volume associated with Gaussian lobe */
203   double
204   rbf_volume(const RBFVAL *rbfp)
# 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   /* Evaluate RBF for DSF at the given normalized outgoing direction */
241   double
242   eval_rbfrep(const RBFNODE *rp, const FVECT outvec)
243   {
244 <        double          res = .0;
244 >        double          res = 0;
245          const RBFVAL    *rbfp;
246          FVECT           odir;
247          double          sig2;
# Line 280 | Line 306 | get_dsf(int ord)
306          RBFNODE         *rbf;
307  
308          for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
309 <                if (rbf->ord == ord);
309 >                if (rbf->ord == ord)
310                          return(rbf);
311          return(NULL);
312   }
# Line 311 | Line 337 | is_rev_tri(const FVECT v1, const FVECT v2, const FVECT
337   int
338   get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
339   {
340 <        const MIGRATION *ej, *ej2;
340 >        const MIGRATION *ej1, *ej2;
341          RBFNODE         *tv;
342  
343          rbfv[0] = rbfv[1] = NULL;
344          if (mig == NULL)
345                  return(0);
346 <        for (ej = mig->rbfv[0]->ejl; ej != NULL;
347 <                                ej = nextedge(mig->rbfv[0],ej)) {
348 <                if (ej == mig)
346 >        for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
347 >                                ej1 = nextedge(mig->rbfv[0],ej1)) {
348 >                if (ej1 == mig)
349                          continue;
350 <                tv = opp_rbf(mig->rbfv[0],ej);
350 >                tv = opp_rbf(mig->rbfv[0],ej1);
351                  for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
352                          if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
353                                  rbfv[is_rev_tri(mig->rbfv[0]->invec,
# Line 333 | Line 359 | get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
359          return((rbfv[0] != NULL) + (rbfv[1] != NULL));
360   }
361  
362 + /* Clear our BSDF representation and free memory */
363 + void
364 + clear_bsdf_rep(void)
365 + {
366 +        while (mig_list != NULL) {
367 +                MIGRATION       *mig = mig_list;
368 +                mig_list = mig->next;
369 +                free(mig);
370 +        }
371 +        while (dsf_list != NULL) {
372 +                RBFNODE         *rbf = dsf_list;
373 +                dsf_list = rbf->next;
374 +                free(rbf);
375 +        }
376 +        inp_coverage = 0;
377 +        single_plane_incident = -1;
378 +        input_orient = output_orient = 0;
379 +        grid_res = GRIDRES;
380 + }
381 +
382   /* Write our BSDF mesh interpolant out to the given binary stream */
383   void
384   save_bsdf_rep(FILE *ofp)
# Line 341 | Line 387 | save_bsdf_rep(FILE *ofp)
387          MIGRATION       *mig;
388          int             i, n;
389                                          /* finish header */
390 +        fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage);
391 +        fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient);
392 +        fprintf(ofp, "GRIDRES=%d\n", grid_res);
393          fputformat(BSDFREP_FMT, ofp);
394          fputc('\n', ofp);
395                                          /* write each DSF */
# Line 360 | Line 409 | save_bsdf_rep(FILE *ofp)
409          }
410          putint(-1, 4, ofp);             /* terminator */
411                                          /* write each migration matrix */
412 <        for (mig = mig_list; mig != NULL; mig = mig_list->next) {
412 >        for (mig = mig_list; mig != NULL; mig = mig->next) {
413 >                int     zerocnt = 0;
414                  putint(mig->rbfv[0]->ord, 4, ofp);
415                  putint(mig->rbfv[1]->ord, 4, ofp);
416 +                                        /* write out as sparse data */
417                  n = mtx_nrows(mig) * mtx_ncols(mig);
418 <                for (i = 0; i < n; i++)
419 <                        putflt(mig->mtx[i], ofp);
418 >                for (i = 0; i < n; i++) {
419 >                        if (zerocnt == 0xff) {
420 >                                putint(0xff, 1, ofp); zerocnt = 0;
421 >                        }
422 >                        if (mig->mtx[i] != 0) {
423 >                                putint(zerocnt, 1, ofp); zerocnt = 0;
424 >                                putflt(mig->mtx[i], ofp);
425 >                        } else
426 >                                ++zerocnt;
427 >                }
428 >                putint(zerocnt, 1, ofp);
429          }
430          putint(-1, 4, ofp);             /* terminator */
431          putint(-1, 4, ofp);
# Line 376 | Line 436 | save_bsdf_rep(FILE *ofp)
436          }
437   }
438  
439 + /* Check header line for critical information */
440 + static int
441 + headline(char *s, void *p)
442 + {
443 +        char    fmt[32];
444 +
445 +        if (!strncmp(s, "SYMMETRY=", 9)) {
446 +                inp_coverage = atoi(s+9);
447 +                single_plane_incident = !inp_coverage;
448 +                return(0);
449 +        }
450 +        if (!strncmp(s, "IO_SIDES=", 9)) {
451 +                sscanf(s+9, "%d %d", &input_orient, &output_orient);
452 +                return(0);
453 +        }
454 +        if (!strncmp(s, "GRIDRES=", 8)) {
455 +                sscanf(s+8, "%d", &grid_res);
456 +                return(0);
457 +        }
458 +        if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
459 +                return(-1);
460 +        return(0);
461 + }
462 +
463   /* Read a BSDF mesh interpolant from the given binary stream */
464   int
465   load_bsdf_rep(FILE *ifp)
# Line 383 | Line 467 | load_bsdf_rep(FILE *ifp)
467          RBFNODE         rbfh;
468          int             from_ord, to_ord;
469          int             i;
470 < #ifdef DEBUG
471 <        if ((dsf_list != NULL) | (mig_list != NULL)) {
472 <                fprintf(stderr,
389 <                "%s: attempt to load BSDF interpolant over existing\n",
390 <                                progname);
470 >
471 >        clear_bsdf_rep();
472 >        if (ifp == NULL)
473                  return(0);
474 <        }
475 < #endif
394 <        if (checkheader(ifp, BSDFREP_FMT, NULL) <= 0) {
474 >        if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 |
475 >                        !input_orient | !output_orient) {
476                  fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
477                                  progname);
478                  return(0);
# Line 404 | Line 485 | load_bsdf_rep(FILE *ifp)
485                  rbfh.invec[0] = getflt(ifp);
486                  rbfh.invec[1] = getflt(ifp);
487                  rbfh.invec[2] = getflt(ifp);
488 <                rbfh.nrbf = getint(4, ifp);
489 <                if (!new_input_vector(rbfh.invec))
488 >                if (normalize(rbfh.invec) == 0) {
489 >                        fprintf(stderr, "%s: zero incident vector\n", progname);
490                          return(0);
491 +                }
492 +                rbfh.vtotal = getflt(ifp);
493 +                rbfh.nrbf = getint(4, ifp);
494                  newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
495                                          sizeof(RBFVAL)*(rbfh.nrbf-1));
496                  if (newrbf == NULL)
497                          goto memerr;
498 <                memcpy(newrbf, &rbfh, sizeof(RBFNODE));
498 >                memcpy(newrbf, &rbfh, sizeof(RBFNODE)-sizeof(RBFVAL));
499                  for (i = 0; i < rbfh.nrbf; i++) {
500                          newrbf->rbfa[i].peak = getflt(ifp);
501                          newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;
# Line 447 | Line 531 | load_bsdf_rep(FILE *ifp)
531                          goto memerr;
532                  newmig->rbfv[0] = from_rbf;
533                  newmig->rbfv[1] = to_rbf;
534 <                                        /* read matrix coefficients */
535 <                for (i = 0; i < n; i++)
536 <                        newmig->mtx[i] = getflt(ifp);
534 >                memset(newmig->mtx, 0, sizeof(float)*n);
535 >                for (i = 0; ; ) {       /* read sparse data */
536 >                        int     zc = getint(1, ifp) & 0xff;
537 >                        if ((i += zc) >= n)
538 >                                break;
539 >                        if (zc == 0xff)
540 >                                continue;
541 >                        newmig->mtx[i++] = getflt(ifp);
542 >                }
543                  if (feof(ifp))
544                          goto badEOF;
545                                          /* insert in edge lists */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines