ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bsdf_m.c
(Generate patch)

Comparing ray/src/common/bsdf_m.c (file contents):
Revision 3.4 by greg, Sat Feb 19 01:48:59 2011 UTC vs.
Revision 3.10 by greg, Tue Apr 19 21:31:22 2011 UTC

# Line 81 | Line 81 | static int     nabases = 3;    /* current number of defined b
81   static int
82   fequal(double a, double b)
83   {
84 <        if (b != .0)
84 >        if (b != 0)
85                  a = a/b - 1.;
86          return (a <= 1e-6) & (a >= -1e-6);
87   }
88  
89 < /* returns the name of the given tag */
89 > /* Returns the name of the given tag */
90   #ifdef ezxml_name
91   #undef ezxml_name
92   static char *
# Line 98 | Line 98 | ezxml_name(ezxml_t xml)
98   }
99   #endif
100  
101 < /* returns the given tag's character content or empty string if none */
101 > /* Returns the given tag's character content or empty string if none */
102   #ifdef ezxml_txt
103   #undef ezxml_txt
104   static char *
# Line 157 | Line 157 | SDnewMatrix(int ni, int no)
157   /* Free a BSDF matrix */
158   #define SDfreeMatrix            free
159  
160 < /* get vector for this angle basis index */
160 > /* get vector for this angle basis index (front exiting) */
161   static int
162 < ab_getvec(FVECT v, int ndx, double randX, void *p)
162 > fo_getvec(FVECT v, double ndxr, void *p)
163   {
164 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
164 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
165 >        int             ndx = (int)ndxr;
166 >        double          randX = ndxr - ndx;
167          double          rx[2];
168          int             li;
169          double          pol, azi, d;
170          
171 <        if ((ndx < 0) | (ndx >= ab->nangles))
171 >        if ((ndxr < 0) | (ndx >= ab->nangles))
172                  return RC_FAIL;
173          for (li = 0; ndx >= ab->lat[li].nphis; li++)
174                  ndx -= ab->lat[li].nphis;
# Line 181 | Line 183 | ab_getvec(FVECT v, int ndx, double randX, void *p)
183          return RC_GOOD;
184   }
185  
186 < /* get index corresponding to the given vector */
186 > /* get index corresponding to the given vector (front exiting) */
187   static int
188 < ab_getndx(const FVECT v, void *p)
188 > fo_getndx(const FVECT v, void *p)
189   {
190 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
190 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
191          int     li, ndx;
192          double  pol, azi, d;
193  
194          if (v == NULL)
195                  return -1;
196 <        if ((v[2] < .0) | (v[2] > 1.0))
196 >        if ((v[2] < 0) | (v[2] > 1.))
197                  return -1;
198          pol = 180.0/M_PI*acos(v[2]);
199          azi = 180.0/M_PI*atan2(v[1], v[0]);
# Line 210 | Line 212 | ab_getndx(const FVECT v, void *p)
212   /* compute square of real value */
213   static double sq(double x) { return x*x; }
214  
215 < /* get projected solid angle for this angle basis index */
215 > /* get projected solid angle for this angle basis index (universal) */
216   static double
217 < ab_getohm(int ndx, void *p)
217 > io_getohm(int ndx, void *p)
218   {
219          static int      last_li = -1;
220          static double   last_ohm;
# Line 235 | Line 237 | ab_getohm(int ndx, void *p)
237                                  (double)ab->lat[li].nphis;
238   }
239  
240 < /* get reverse vector for this angle basis index */
240 > /* get vector for this angle basis index (back incident) */
241   static int
242 < ab_getvecR(FVECT v, int ndx, double randX, void *p)
242 > bi_getvec(FVECT v, double ndxr, void *p)
243   {
244 <        int     na = (*(ANGLE_BASIS *)p).nangles;
243 <
244 <        if (!ab_getvec(v, ndx, randX, p))
244 >        if (!fo_getvec(v, ndxr, p))
245                  return RC_FAIL;
246  
247          v[0] = -v[0];
# Line 251 | Line 251 | ab_getvecR(FVECT v, int ndx, double randX, void *p)
251          return RC_GOOD;
252   }
253  
254 < /* get index corresponding to the reverse vector */
254 > /* get index corresponding to the vector (back incident) */
255   static int
256 < ab_getndxR(const FVECT v, void *p)
256 > bi_getndx(const FVECT v, void *p)
257   {
258          FVECT  v2;
259          
# Line 261 | Line 261 | ab_getndxR(const FVECT v, void *p)
261          v2[1] = -v[1];
262          v2[2] = -v[2];
263  
264 <        return ab_getndx(v2, p);
264 >        return fo_getndx(v2, p);
265   }
266  
267 + /* get vector for this angle basis index (back exiting) */
268 + static int
269 + bo_getvec(FVECT v, double ndxr, void *p)
270 + {
271 +        if (!fo_getvec(v, ndxr, p))
272 +                return RC_FAIL;
273 +
274 +        v[2] = -v[2];
275 +
276 +        return RC_GOOD;
277 + }
278 +
279 + /* get index corresponding to the vector (back exiting) */
280 + static int
281 + bo_getndx(const FVECT v, void *p)
282 + {
283 +        FVECT  v2;
284 +        
285 +        v2[0] = v[0];
286 +        v2[1] = v[1];
287 +        v2[2] = -v[2];
288 +
289 +        return fo_getndx(v2, p);
290 + }
291 +
292 + /* get vector for this angle basis index (front incident) */
293 + static int
294 + fi_getvec(FVECT v, double ndxr, void *p)
295 + {
296 +        if (!fo_getvec(v, ndxr, p))
297 +                return RC_FAIL;
298 +
299 +        v[0] = -v[0];
300 +        v[1] = -v[1];
301 +
302 +        return RC_GOOD;
303 + }
304 +
305 + /* get index corresponding to the vector (front incident) */
306 + static int
307 + fi_getndx(const FVECT v, void *p)
308 + {
309 +        FVECT  v2;
310 +        
311 +        v2[0] = -v[0];
312 +        v2[1] = -v[1];
313 +        v2[2] = v[2];
314 +
315 +        return fo_getndx(v2, p);
316 + }
317 +
318   /* load custom BSDF angle basis */
319   static int
320   load_angle_basis(ezxml_t wab)
# Line 346 | Line 397 | get_extrema(SDSpectralDF *df)
397          }
398          free(ohma);
399                                          /* need incoming solid angles, too? */
400 <        if (dp->ninc < dp->nout || dp->ib_ohm != dp->ob_ohm ||
350 <                                dp->ib_priv != dp->ob_priv) {
400 >        if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) {
401                  double  ohm;
402                  for (i = dp->ninc; i--; )
403                          if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA)
# Line 363 | Line 413 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
413          SDSpectralDF    *df;
414          SDMat           *dp;
415          char            *sdata;
416 +        int             tfront;
417          int             inbi, outbi;
418          int             i;
419                                          /* allocate BSDF component */
420          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
421 <        if (!strcasecmp(sdata, "Transmission Front")) {
421 >        /*
422 >         * Remember that front and back are reversed from WINDOW 6 orientations
423 >         * Favor their "Front" (incoming light) since that's more often valid
424 >         */
425 >        tfront = !strcasecmp(sdata, "Transmission Back");
426 >        if (!strcasecmp(sdata, "Transmission Front") ||
427 >                        tfront & (sd->tf == NULL)) {
428                  if (sd->tf != NULL)
429                          SDfreeSpectralDF(sd->tf);
430                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
431                          return RC_MEMERR;
432                  df = sd->tf;
433          } else if (!strcasecmp(sdata, "Reflection Front")) {
434 <                if (sd->rf != NULL)
378 <                        SDfreeSpectralDF(sd->rf);
379 <                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
380 <                        return RC_MEMERR;
381 <                df = sd->rf;
382 <        } else if (!strcasecmp(sdata, "Reflection Back")) {
383 <                if (sd->rb != NULL)
434 >                if (sd->rb != NULL)     /* note back-front reversal */
435                          SDfreeSpectralDF(sd->rb);
436                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
437                          return RC_MEMERR;
438                  df = sd->rb;
439 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
440 +                if (sd->rf != NULL)     /* note front-back reversal */
441 +                        SDfreeSpectralDF(sd->rf);
442 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
443 +                        return RC_MEMERR;
444 +                df = sd->rf;
445          } else
446                  return RC_FAIL;
447          /* XXX should also check "ScatteringDataType" for consistency? */
# Line 419 | Line 476 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
476          dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles);
477          if (dp == NULL)
478                  return RC_MEMERR;
479 <        dp->ib_priv = (void *)&abase_list[inbi];
480 <        dp->ob_priv = (void *)&abase_list[outbi];
479 >        dp->ib_priv = &abase_list[inbi];
480 >        dp->ob_priv = &abase_list[outbi];
481          if (df == sd->tf) {
482 <                dp->ib_vec = ab_getvecR;
483 <                dp->ib_ndx = ab_getndxR;
484 <                dp->ob_vec = ab_getvec;
485 <                dp->ob_ndx = ab_getndx;
482 >                if (tfront) {
483 >                        dp->ib_vec = &fi_getvec;
484 >                        dp->ib_ndx = &fi_getndx;
485 >                        dp->ob_vec = &bo_getvec;
486 >                        dp->ob_ndx = &bo_getndx;
487 >                } else {
488 >                        dp->ib_vec = &bi_getvec;
489 >                        dp->ib_ndx = &bi_getndx;
490 >                        dp->ob_vec = &fo_getvec;
491 >                        dp->ob_ndx = &fo_getndx;
492 >                }
493          } else if (df == sd->rf) {
494 <                dp->ib_vec = ab_getvec;
495 <                dp->ib_ndx = ab_getndx;
496 <                dp->ob_vec = ab_getvec;
497 <                dp->ob_ndx = ab_getndx;
494 >                dp->ib_vec = &fi_getvec;
495 >                dp->ib_ndx = &fi_getndx;
496 >                dp->ob_vec = &fo_getvec;
497 >                dp->ob_ndx = &fo_getndx;
498          } else /* df == sd->rb */ {
499 <                dp->ib_vec = ab_getvecR;
500 <                dp->ib_ndx = ab_getndxR;
501 <                dp->ob_vec = ab_getvecR;
502 <                dp->ob_ndx = ab_getndxR;
499 >                dp->ib_vec = &bi_getvec;
500 >                dp->ib_ndx = &bi_getndx;
501 >                dp->ob_vec = &bo_getvec;
502 >                dp->ob_ndx = &bo_getndx;
503          }
504 <        dp->ib_ohm = ab_getohm;
505 <        dp->ob_ohm = ab_getohm;
504 >        dp->ib_ohm = &io_getohm;
505 >        dp->ob_ohm = &io_getohm;
506          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
507          df->comp[0].dist = dp;
508          df->comp[0].func = &SDhandleMtx;
509                                          /* read BSDF data */
510 <        sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
510 >        sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
511          if (!sdata || !*sdata) {
512                  sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
513                                  sd->name);
# Line 587 | Line 651 | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
651  
652   /* Query solid angle for vector */
653   static SDError
654 < SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist)
654 > SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2,
655 >                                        int qflags, const void *dist)
656   {
657          const SDMat     *dp = (const SDMat *)dist;
658 <
659 <        if (!(qflags & SDqueryInc+SDqueryOut))
658 >        double          inc_psa, out_psa;
659 >                                        /* check arguments */
660 >        if ((psa == NULL) | (v1 == NULL) | (dp == NULL))
661                  return SDEargument;
662 <        if (qflags & SDqueryInc) {
663 <                double  inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
664 <                if (inc_psa < .0)
665 <                        return SDEinternal;
666 <                switch (qflags & SDqueryMin+SDqueryMax) {
667 <                case SDqueryMax:
668 <                        if (inc_psa > psa[0])
669 <                                psa[0] = inc_psa;
670 <                        break;
671 <                case SDqueryMin+SDqueryMax:
672 <                        if (inc_psa > psa[1])
673 <                                psa[1] = inc_psa;
608 <                        /* fall through */
609 <                case SDqueryMin:
610 <                        if (inc_psa < psa[0])
611 <                                psa[0] = inc_psa;
612 <                        break;
613 <                case 0:
662 >        if (v2 == NULL)
663 >                v2 = v1;
664 >                                        /* get projected solid angles */
665 >        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1));
666 >        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2));
667 >
668 >        switch (qflags) {               /* record based on flag settings */
669 >        case SDqueryVal:
670 >                psa[0] = .0;
671 >                /* fall through */
672 >        case SDqueryMax:
673 >                if (inc_psa > psa[0])
674                          psa[0] = inc_psa;
675 <                        break;
676 <                }
675 >                if (out_psa > psa[0])
676 >                        psa[0] = out_psa;
677 >                break;
678 >        case SDqueryMin+SDqueryMax:
679 >                if (inc_psa > psa[0])
680 >                        psa[1] = inc_psa;
681 >                if (out_psa > psa[0])
682 >                        psa[1] = out_psa;
683 >                /* fall through */
684 >        case SDqueryMin:
685 >                if ((inc_psa > 0) & (inc_psa < psa[0]))
686 >                        psa[0] = inc_psa;
687 >                if ((out_psa > 0) & (out_psa < psa[0]))
688 >                        psa[0] = out_psa;
689 >                break;
690          }
691 <        if (qflags & SDqueryOut) {
692 <                double  out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec));
620 <                if (out_psa < .0)
621 <                        return SDEinternal;
622 <                switch (qflags & SDqueryMin+SDqueryMax) {
623 <                case SDqueryMax:
624 <                        if (out_psa > psa[0])
625 <                                psa[0] = out_psa;
626 <                        break;
627 <                case SDqueryMin+SDqueryMax:
628 <                        if (out_psa > psa[1])
629 <                                psa[1] = out_psa;
630 <                        /* fall through */
631 <                case SDqueryMin:
632 <                        if (out_psa < psa[0])
633 <                                psa[0] = out_psa;
634 <                        break;
635 <                case 0:
636 <                        psa[(qflags&SDqueryInc)!=0] = out_psa;
637 <                        break;
638 <                }
639 <        }
640 <        return SDEnone;
691 >                                        /* make sure it's legal */
692 >        return (psa[0] <= 0) ? SDEinternal : SDEnone;
693   }
694  
695   /* Compute new cumulative distribution from BSDF */
# Line 679 | Line 731 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
731          int             reverse;
732          SDMatCDst       myCD;
733          SDMatCDst       *cd, *cdlast;
734 <
735 <        if (dp == NULL)
734 >                                        /* check arguments */
735 >        if ((inVec == NULL) | (dp == NULL))
736                  return NULL;
737          memset(&myCD, 0, sizeof(myCD));
738          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 735 | Line 787 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
787          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
788          const unsigned  target = randX*maxval;
789          int             i, iupper, ilower;
790 +                                        /* check arguments */
791 +        if ((outVec == NULL) | (mcd == NULL))
792 +                return SDEargument;
793                                          /* binary search to find index */
794          ilower = 0; iupper = mcd->calen;
795          while ((i = (iupper + ilower) >> 1) != ilower)
# Line 746 | Line 801 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
801          randX = (randX*maxval - mcd->carr[ilower]) /
802                          (double)(mcd->carr[iupper] - mcd->carr[ilower]);
803                                          /* convert index to vector */
804 <        if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv))
804 >        if ((*mcd->ob_vec)(outVec, i+randX, mcd->ob_priv))
805                  return SDEnone;
806          strcpy(SDerrorDetail, "BSDF sampling fault");
807          return SDEinternal;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines