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.2 by greg, Sat Oct 20 07:02:00 2012 UTC vs.
Revision 2.12 by greg, Wed Mar 20 01:00:22 2013 UTC

# Line 14 | Line 14 | static const char RCSid[] = "$Id$";
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 23 | Line 26 | int                    single_plane_incident = -1;
26   int                     input_orient = 0;
27   int                     output_orient = 0;
28  
29 +                                /* BSDF histogram */
30 + int                     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 53 | 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 61 | 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;
64        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 78 | 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 168 | 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 + /* Rotate RBF to correspond to given incident vector */
188 + void
189 + rotate_rbf(RBFNODE *rbf, const FVECT invec)
190 + {
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 +        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 volume associated with Gaussian lobe */
209   double
210   rbf_volume(const RBFVAL *rbfp)
# Line 190 | 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, (xpos+.5)/grid_res, (ypos+.5)/grid_res);
225                                  /* uniform hemispherical projection */
226          r2 = uv[0]*uv[0] + uv[1]*uv[1];
227          vec[0] = vec[1] = sqrt(2. - r2);
# Line 208 | 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 */
247   double
248   eval_rbfrep(const RBFNODE *rp, const FVECT outvec)
249   {
250 <        double          res = .0;
250 >        double          res = 0;
251          const RBFVAL    *rbfp;
252          FVECT           odir;
253          double          sig2;
254          int             n;
255 <
256 <        if (rp == NULL)
257 <                return(.0);
255 >                                /* use minimum if no information avail. */
256 >        if (rp == NULL) {
257 >                if (outvec[2] > 0 ^ output_orient > 0)
258 >                        return(.0);
259 >                return(bsdf_min*output_orient*outvec[2]);
260 >        }
261          rbfp = rp->rbfa;
262          for (n = rp->nrbf; n--; rbfp++) {
263                  ovec_from_pos(odir, rbfp->gx, rbfp->gy);
# Line 281 | Line 315 | get_dsf(int ord)
315          RBFNODE         *rbf;
316  
317          for (rbf = dsf_list; rbf != NULL; rbf = rbf->next)
318 <                if (rbf->ord == ord);
318 >                if (rbf->ord == ord)
319                          return(rbf);
320          return(NULL);
321   }
# Line 312 | Line 346 | is_rev_tri(const FVECT v1, const FVECT v2, const FVECT
346   int
347   get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
348   {
349 <        const MIGRATION *ej, *ej2;
349 >        const MIGRATION *ej1, *ej2;
350          RBFNODE         *tv;
351  
352          rbfv[0] = rbfv[1] = NULL;
353          if (mig == NULL)
354                  return(0);
355 <        for (ej = mig->rbfv[0]->ejl; ej != NULL;
356 <                                ej = nextedge(mig->rbfv[0],ej)) {
357 <                if (ej == mig)
355 >        for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL;
356 >                                ej1 = nextedge(mig->rbfv[0],ej1)) {
357 >                if (ej1 == mig)
358                          continue;
359 <                tv = opp_rbf(mig->rbfv[0],ej);
359 >                tv = opp_rbf(mig->rbfv[0],ej1);
360                  for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2))
361                          if (opp_rbf(tv,ej2) == mig->rbfv[1]) {
362                                  rbfv[is_rev_tri(mig->rbfv[0]->invec,
# Line 334 | Line 368 | get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig)
368          return((rbfv[0] != NULL) + (rbfv[1] != NULL));
369   }
370  
371 + /* Clear our BSDF representation and free memory */
372 + void
373 + clear_bsdf_rep(void)
374 + {
375 +        while (mig_list != NULL) {
376 +                MIGRATION       *mig = mig_list;
377 +                mig_list = mig->next;
378 +                free(mig);
379 +        }
380 +        while (dsf_list != NULL) {
381 +                RBFNODE         *rbf = dsf_list;
382 +                dsf_list = rbf->next;
383 +                free(rbf);
384 +        }
385 +        inp_coverage = 0;
386 +        single_plane_incident = -1;
387 +        input_orient = output_orient = 0;
388 +        grid_res = GRIDRES;
389 + }
390 +
391   /* Write our BSDF mesh interpolant out to the given binary stream */
392   void
393   save_bsdf_rep(FILE *ofp)
# Line 344 | Line 398 | save_bsdf_rep(FILE *ofp)
398                                          /* finish header */
399          fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage);
400          fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient);
401 +        fprintf(ofp, "GRIDRES=%d\n", grid_res);
402 +        fprintf(ofp, "BSDFMIN=%g\n", bsdf_min);
403          fputformat(BSDFREP_FMT, ofp);
404          fputc('\n', ofp);
405                                          /* write each DSF */
# Line 370 | Line 426 | save_bsdf_rep(FILE *ofp)
426                                          /* write out as sparse data */
427                  n = mtx_nrows(mig) * mtx_ncols(mig);
428                  for (i = 0; i < n; i++) {
429 <                        if (zerocnt >= 0xff) {
430 <                                putint(zerocnt, 1, ofp); zerocnt = 0;
429 >                        if (zerocnt == 0xff) {
430 >                                putint(0xff, 1, ofp); zerocnt = 0;
431                          }
432                          if (mig->mtx[i] != 0) {
433                                  putint(zerocnt, 1, ofp); zerocnt = 0;
# Line 405 | Line 461 | headline(char *s, void *p)
461                  sscanf(s+9, "%d %d", &input_orient, &output_orient);
462                  return(0);
463          }
464 +        if (!strncmp(s, "GRIDRES=", 8)) {
465 +                sscanf(s+8, "%d", &grid_res);
466 +                return(0);
467 +        }
468 +        if (!strncmp(s, "BSDFMIN=", 8)) {
469 +                sscanf(s+8, "%lf", &bsdf_min);
470 +                return(0);
471 +        }
472          if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT))
473                  return(-1);
474          return(0);
# Line 417 | Line 481 | load_bsdf_rep(FILE *ifp)
481          RBFNODE         rbfh;
482          int             from_ord, to_ord;
483          int             i;
484 < #ifdef DEBUG
485 <        if ((dsf_list != NULL) | (mig_list != NULL)) {
486 <                fprintf(stderr,
423 <                "%s: attempt to load BSDF interpolant over existing\n",
424 <                                progname);
484 >
485 >        clear_bsdf_rep();
486 >        if (ifp == NULL)
487                  return(0);
426        }
427 #endif
428        input_orient = output_orient = 0;
429        single_plane_incident = -1;
488          if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 |
489                          !input_orient | !output_orient) {
490                  fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n",
# Line 441 | Line 499 | load_bsdf_rep(FILE *ifp)
499                  rbfh.invec[0] = getflt(ifp);
500                  rbfh.invec[1] = getflt(ifp);
501                  rbfh.invec[2] = getflt(ifp);
502 <                rbfh.nrbf = getint(4, ifp);
503 <                if (!new_input_vector(rbfh.invec))
502 >                if (normalize(rbfh.invec) == 0) {
503 >                        fprintf(stderr, "%s: zero incident vector\n", progname);
504                          return(0);
505 +                }
506 +                rbfh.vtotal = getflt(ifp);
507 +                rbfh.nrbf = getint(4, ifp);
508                  newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) +
509                                          sizeof(RBFVAL)*(rbfh.nrbf-1));
510                  if (newrbf == NULL)
511                          goto memerr;
512 <                memcpy(newrbf, &rbfh, sizeof(RBFNODE));
512 >                memcpy(newrbf, &rbfh, sizeof(RBFNODE)-sizeof(RBFVAL));
513                  for (i = 0; i < rbfh.nrbf; i++) {
514                          newrbf->rbfa[i].peak = getflt(ifp);
515                          newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;
# Line 487 | Line 548 | load_bsdf_rep(FILE *ifp)
548                  memset(newmig->mtx, 0, sizeof(float)*n);
549                  for (i = 0; ; ) {       /* read sparse data */
550                          int     zc = getint(1, ifp) & 0xff;
490                        if (zc == 0xff) {
491                                i += 0xff;
492                                continue;
493                        }
551                          if ((i += zc) >= n)
552                                  break;
553 +                        if (zc == 0xff)
554 +                                continue;
555                          newmig->mtx[i++] = getflt(ifp);
556                  }
557                  if (feof(ifp))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines