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.7 by greg, Thu Nov 8 22:05:04 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 + #ifdef DEBUG
192 +        double  tdiff = 180./M_PI*fabs(acos(invec[2])-acos(rbf->invec[2]));
193 +        if (tdiff >= 1.5)
194 +                fprintf(stderr,
195 +                        "%s: Warning - rotated theta differs by %.1f degrees\n",
196 +                                progname, tdiff);
197 + #endif
198 +        for (n = 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 volume associated with Gaussian lobe */
209   double
210   rbf_volume(const RBFVAL *rbfp)
# Line 189 | Line 221 | ovec_from_pos(FVECT vec, int xpos, int ypos)
221          double  uv[2];
222          double  r2;
223          
224 <        SDsquare2disk(uv, (1./GRIDRES)*(xpos+.5), (1./GRIDRES)*(ypos+.5));
224 >        SDsquare2disk(uv, (1./grid_res)*(xpos+.5), (1./grid_res)*(ypos+.5));
225                                  /* uniform hemispherical projection */
226          r2 = uv[0]*uv[0] + uv[1]*uv[1];
227          vec[0] = vec[1] = sqrt(2. - r2);
# Line 207 | Line 239 | pos_from_vec(int pos[2], const FVECT vec)
239  
240          SDdisk2square(sq, vec[0]*norm, vec[1]*norm);
241  
242 <        pos[0] = (int)(sq[0]*GRIDRES);
243 <        pos[1] = (int)(sq[1]*GRIDRES);
242 >        pos[0] = (int)(sq[0]*grid_res);
243 >        pos[1] = (int)(sq[1]*grid_res);
244   }
245  
246   /* Evaluate RBF for DSF at the given normalized outgoing direction */
# Line 280 | Line 312 | get_dsf(int ord)
312          RBFNODE         *rbf;
313  
314          for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
315 <                if (rbf->ord == ord);
315 >                if (rbf->ord == ord)
316                          return(rbf);
317          return(NULL);
318   }
# Line 311 | Line 343 | is_rev_tri(const FVECT v1, const FVECT v2, const FVECT
343   int
344   get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
345   {
346 <        const MIGRATION *ej, *ej2;
346 >        const MIGRATION *ej1, *ej2;
347          RBFNODE         *tv;
348  
349          rbfv[0] = rbfv[1] = NULL;
350          if (mig == NULL)
351                  return(0);
352 <        for (ej = mig->rbfv[0]->ejl; ej != NULL;
353 <                                ej = nextedge(mig->rbfv[0],ej)) {
354 <                if (ej == mig)
352 >        for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
353 >                                ej1 = nextedge(mig->rbfv[0],ej1)) {
354 >                if (ej1 == mig)
355                          continue;
356 <                tv = opp_rbf(mig->rbfv[0],ej);
356 >                tv = opp_rbf(mig->rbfv[0],ej1);
357                  for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
358                          if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
359                                  rbfv[is_rev_tri(mig->rbfv[0]->invec,
# Line 333 | Line 365 | get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
365          return((rbfv[0] != NULL) + (rbfv[1] != NULL));
366   }
367  
368 + /* Clear our BSDF representation and free memory */
369 + void
370 + clear_bsdf_rep(void)
371 + {
372 +        while (mig_list != NULL) {
373 +                MIGRATION       *mig = mig_list;
374 +                mig_list = mig->next;
375 +                free(mig);
376 +        }
377 +        while (dsf_list != NULL) {
378 +                RBFNODE         *rbf = dsf_list;
379 +                dsf_list = rbf->next;
380 +                free(rbf);
381 +        }
382 +        inp_coverage = 0;
383 +        single_plane_incident = -1;
384 +        input_orient = output_orient = 0;
385 +        grid_res = GRIDRES;
386 + }
387 +
388   /* Write our BSDF mesh interpolant out to the given binary stream */
389   void
390   save_bsdf_rep(FILE *ofp)
# Line 341 | Line 393 | save_bsdf_rep(FILE *ofp)
393          MIGRATION       *mig;
394          int             i, n;
395                                          /* finish header */
396 +        fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage);
397 +        fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient);
398 +        fprintf(ofp, "GRIDRES=%d\n", grid_res);
399          fputformat(BSDFREP_FMT, ofp);
400          fputc('\n', ofp);
401                                          /* write each DSF */
# Line 360 | Line 415 | save_bsdf_rep(FILE *ofp)
415          }
416          putint(-1, 4, ofp);             /* terminator */
417                                          /* write each migration matrix */
418 <        for (mig = mig_list; mig != NULL; mig = mig_list->next) {
418 >        for (mig = mig_list; mig != NULL; mig = mig->next) {
419 >                int     zerocnt = 0;
420                  putint(mig->rbfv[0]->ord, 4, ofp);
421                  putint(mig->rbfv[1]->ord, 4, ofp);
422 +                                        /* write out as sparse data */
423                  n = mtx_nrows(mig) * mtx_ncols(mig);
424 <                for (i = 0; i < n; i++)
425 <                        putflt(mig->mtx[i], ofp);
424 >                for (i = 0; i < n; i++) {
425 >                        if (zerocnt == 0xff) {
426 >                                putint(0xff, 1, ofp); zerocnt = 0;
427 >                        }
428 >                        if (mig->mtx[i] != 0) {
429 >                                putint(zerocnt, 1, ofp); zerocnt = 0;
430 >                                putflt(mig->mtx[i], ofp);
431 >                        } else
432 >                                ++zerocnt;
433 >                }
434 >                putint(zerocnt, 1, ofp);
435          }
436          putint(-1, 4, ofp);             /* terminator */
437          putint(-1, 4, ofp);
# Line 376 | Line 442 | save_bsdf_rep(FILE *ofp)
442          }
443   }
444  
445 + /* Check header line for critical information */
446 + static int
447 + headline(char *s, void *p)
448 + {
449 +        char    fmt[32];
450 +
451 +        if (!strncmp(s, "SYMMETRY=", 9)) {
452 +                inp_coverage = atoi(s+9);
453 +                single_plane_incident = !inp_coverage;
454 +                return(0);
455 +        }
456 +        if (!strncmp(s, "IO_SIDES=", 9)) {
457 +                sscanf(s+9, "%d %d", &input_orient, &output_orient);
458 +                return(0);
459 +        }
460 +        if (!strncmp(s, "GRIDRES=", 8)) {
461 +                sscanf(s+8, "%d", &grid_res);
462 +                return(0);
463 +        }
464 +        if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
465 +                return(-1);
466 +        return(0);
467 + }
468 +
469   /* Read a BSDF mesh interpolant from the given binary stream */
470   int
471   load_bsdf_rep(FILE *ifp)
# Line 383 | Line 473 | load_bsdf_rep(FILE *ifp)
473          RBFNODE         rbfh;
474          int             from_ord, to_ord;
475          int             i;
476 < #ifdef DEBUG
477 <        if ((dsf_list != NULL) | (mig_list != NULL)) {
478 <                fprintf(stderr,
389 <                "%s: attempt to load BSDF interpolant over existing\n",
390 <                                progname);
476 >
477 >        clear_bsdf_rep();
478 >        if (ifp == NULL)
479                  return(0);
480 <        }
481 < #endif
394 <        if (checkheader(ifp, BSDFREP_FMT, NULL) <= 0) {
480 >        if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 |
481 >                        !input_orient | !output_orient) {
482                  fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
483                                  progname);
484                  return(0);
# Line 404 | Line 491 | load_bsdf_rep(FILE *ifp)
491                  rbfh.invec[0] = getflt(ifp);
492                  rbfh.invec[1] = getflt(ifp);
493                  rbfh.invec[2] = getflt(ifp);
494 +                rbfh.vtotal = getflt(ifp);
495                  rbfh.nrbf = getint(4, ifp);
408                if (!new_input_vector(rbfh.invec))
409                        return(0);
496                  newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
497                                          sizeof(RBFVAL)*(rbfh.nrbf-1));
498                  if (newrbf == NULL)
# Line 447 | Line 533 | load_bsdf_rep(FILE *ifp)
533                          goto memerr;
534                  newmig->rbfv[0] = from_rbf;
535                  newmig->rbfv[1] = to_rbf;
536 <                                        /* read matrix coefficients */
537 <                for (i = 0; i < n; i++)
538 <                        newmig->mtx[i] = getflt(ifp);
536 >                memset(newmig->mtx, 0, sizeof(float)*n);
537 >                for (i = 0; ; ) {       /* read sparse data */
538 >                        int     zc = getint(1, ifp) & 0xff;
539 >                        if ((i += zc) >= n)
540 >                                break;
541 >                        if (zc == 0xff)
542 >                                continue;
543 >                        newmig->mtx[i++] = getflt(ifp);
544 >                }
545                  if (feof(ifp))
546                          goto badEOF;
547                                          /* insert in edge lists */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines