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.11 by greg, Wed Apr 20 14:44:05 2011 UTC vs.
Revision 3.18 by greg, Thu Jul 7 15:25:09 2011 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   *
12   */
13  
14 + #define _USE_MATH_DEFINES
15   #include "rtio.h"
16   #include <stdlib.h>
17   #include <math.h>
# Line 86 | Line 87 | fequal(double a, double b)
87          return (a <= 1e-6) & (a >= -1e-6);
88   }
89  
89 /* Returns the name of the given tag */
90 #ifdef ezxml_name
91 #undef ezxml_name
92 static char *
93 ezxml_name(ezxml_t xml)
94 {
95        if (xml == NULL)
96                return NULL;
97        return xml->name;
98 }
99 #endif
100
90   /* Returns the given tag's character content or empty string if none */
91   #ifdef ezxml_txt
92   #undef ezxml_txt
# Line 189 | Line 178 | fo_getndx(const FVECT v, void *p)
178   {
179          ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
180          int     li, ndx;
181 <        double  pol, azi, d;
181 >        double  pol, azi;
182  
183          if (v == NULL)
184                  return -1;
# Line 352 | Line 341 | load_angle_basis(ezxml_t wab)
341                                          "ThetaBounds"), "LowerTheta"))),
342                                  abase_list[nabases].lat[i].tmin)) {
343                          sprintf(SDerrorDetail, "Theta values disagree in '%s'",
344 <                                abname);
344 >                                        abname);
345                          return RC_DATERR;
346                  }
347                  abase_list[nabases].nangles +=
# Line 362 | Line 351 | load_angle_basis(ezxml_t wab)
351                                  (abase_list[nabases].lat[i].nphis == 1 &&
352                                  abase_list[nabases].lat[i].tmin > FTINY)) {
353                          sprintf(SDerrorDetail, "Illegal phi count in '%s'",
354 <                                abname);
354 >                                        abname);
355                          return RC_DATERR;
356                  }
357          }
# Line 418 | Line 407 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
407          int             i;
408                                          /* allocate BSDF component */
409          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
410 +        if (!sdata)
411 +                return RC_FAIL;
412          /*
413           * Remember that front and back are reversed from WINDOW 6 orientations
414           * Favor their "Front" (incoming light) since that's more often valid
# Line 507 | Line 498 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
498          df->comp[0].dist = dp;
499          df->comp[0].func = &SDhandleMtx;
500                                          /* read BSDF data */
501 <        sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
501 >        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
502          if (!sdata || !*sdata) {
503                  sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
504                                  sd->name);
# Line 521 | Line 512 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
512                                          sd->name);
513                          return RC_FORMERR;
514                  }
515 <                while (*sdnext && isspace(*sdnext))
515 >                while (isspace(*sdnext))
516                          sdnext++;
517                  if (*sdnext == ',') sdnext++;
518                  if (rowinc) {
519                          int     r = i/dp->nout;
520 <                        int     c = i - c*dp->nout;
520 >                        int     c = i - r*dp->nout;
521                          mBSDF_value(dp,r,c) = atof(sdata);
522                  } else
523                          dp->bsdf[i] = atof(sdata);
# Line 546 | Line 537 | subtract_min(SDMat *sm)
537          for (i = n; --i; )
538                  if (sm->bsdf[i] < minv)
539                          minv = sm->bsdf[i];
540 +        
541 +        if (minv <= FTINY)
542 +                return .0;
543 +
544          for (i = n; i--; )
545                  sm->bsdf[i] -= minv;
546  
# Line 571 | Line 566 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
566                  c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
567                  dv->cieY += ymin;
568          }
569 <        df->maxHemi -= dv->cieY;        /* adjust minimum hemispherical */
569 >        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
570                                          /* make sure everything is set */
571          c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
572   }
# Line 580 | Line 575 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
575   SDError
576   SDloadMtx(SDData *sd, ezxml_t wtl)
577   {
578 <        ezxml_t                 wld, wdb;
579 <        int                     rowIn;
580 <        struct BSDF_data        *dp;
581 <        char                    *txt;
582 <        int                     rval;
588 <        
578 >        ezxml_t         wld, wdb;
579 >        int             rowIn;
580 >        char            *txt;
581 >        int             rval;
582 >                                        /* basic checks and data ordering */
583          txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
584                          "DataDefinition"), "IncidentDataStructure"));
585          if (txt == NULL || !*txt) {
# Line 604 | Line 598 | SDloadMtx(SDData *sd, ezxml_t wtl)
598                                  sd->name);
599                  return SDEsupport;
600          }
601 <                                /* get angle basis */
602 <        rval = load_angle_basis(ezxml_child(ezxml_child(wtl,
603 <                                "DataDefinition"), "AngleBasis"));
604 <        if (rval < 0)
605 <                return convert_errcode(rval);
606 <                                /* load BSDF components */
601 >                                        /* get angle bases */
602 >        for (wld = ezxml_child(ezxml_child(wtl, "DataDefinition"), "AngleBasis");
603 >                                wld != NULL; wld = wld->next) {
604 >                rval = load_angle_basis(wld);
605 >                if (rval < 0)
606 >                        return convert_errcode(rval);
607 >        }
608 >                                        /* load BSDF components */
609          for (wld = ezxml_child(wtl, "WavelengthData");
610                                  wld != NULL; wld = wld->next) {
611                  if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
# Line 620 | Line 616 | SDloadMtx(SDData *sd, ezxml_t wtl)
616                          if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0)
617                                  return convert_errcode(rval);
618          }
619 <                                /* separate diffuse components */
619 >                                        /* separate diffuse components */
620          extract_diffuse(&sd->rLambFront, sd->rf);
621          extract_diffuse(&sd->rLambBack, sd->rb);
622          extract_diffuse(&sd->tLamb, sd->tf);
623 <                                /* return success */
623 >                                        /* return success */
624          return SDEnone;
625   }
626  
627   /* Get Matrix BSDF value */
628   static int
629   SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
630 <                                const FVECT inVec, const void *dist)
630 >                                const FVECT inVec, SDComponent *sdc)
631   {
632 <        const SDMat     *dp = (const SDMat *)dist;
632 >        const SDMat     *dp;
633          int             i_ndx, o_ndx;
634 +                                        /* check arguments */
635 +        if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)
636 +                        || (dp = (SDMat *)sdc->dist) == NULL)
637 +                return 0;
638                                          /* get angle indices */
639          i_ndx = mBSDF_incndx(dp, inVec);
640          o_ndx = mBSDF_outndx(dp, outVec);
641                                          /* try reciprocity if necessary */
642 <        if ((i_ndx < 0) & (o_ndx < 0)) {
642 >        if ((i_ndx < 0) & (o_ndx < 0) && dp->ninc == dp->nout) {
643                  i_ndx = mBSDF_incndx(dp, outVec);
644                  o_ndx = mBSDF_outndx(dp, inVec);
645          }
# Line 649 | Line 649 | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
649          return 1;                       /* XXX monochrome for now */
650   }
651  
652 < /* Query solid angle for vector */
652 > /* Query solid angle for vector(s) */
653   static SDError
654   SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2,
655 <                                        int qflags, const void *dist)
655 >                                        int qflags, SDComponent *sdc)
656   {
657 <        const SDMat     *dp = (const SDMat *)dist;
657 >        const SDMat     *dp;
658          double          inc_psa, out_psa;
659                                          /* check arguments */
660 <        if ((psa == NULL) | (v1 == NULL) | (dp == NULL))
660 >        if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) ||
661 >                        (dp = (SDMat *)sdc->dist) == NULL)
662                  return SDEargument;
663          if (v2 == NULL)
664                  v2 = v1;
665                                          /* get projected solid angles */
666          out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1));
667          inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2));
668 <        if ((out_psa <= 0) & (inc_psa <= 0)) {
668 >        if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) {
669                  inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2));
670                  out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1));
671          }
672  
673          switch (qflags) {               /* record based on flag settings */
673        case SDqueryVal:
674                psa[0] = .0;
675                /* fall through */
674          case SDqueryMax:
675                  if (inc_psa > psa[0])
676                          psa[0] = inc_psa;
# Line 680 | Line 678 | SDqueryMtxProjSA(double *psa, const FVECT v1, const RR
678                          psa[0] = out_psa;
679                  break;
680          case SDqueryMin+SDqueryMax:
681 <                if (inc_psa > psa[0])
681 >                if (inc_psa > psa[1])
682                          psa[1] = inc_psa;
683 <                if (out_psa > psa[0])
683 >                if (out_psa > psa[1])
684                          psa[1] = out_psa;
685                  /* fall through */
686 +        case SDqueryVal:
687 +                if (qflags == SDqueryVal)
688 +                        psa[0] = M_PI;
689 +                /* fall through */
690          case SDqueryMin:
691                  if ((inc_psa > 0) & (inc_psa < psa[0]))
692                          psa[0] = inc_psa;
# Line 731 | Line 733 | make_cdist(SDMatCDst *cd, const FVECT inVec, SDMat *dp
733   static const SDCDst *
734   SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
735   {
736 <        SDMat           *dp = (SDMat *)sdc->dist;
736 >        SDMat           *dp;
737          int             reverse;
738          SDMatCDst       myCD;
739          SDMatCDst       *cd, *cdlast;
740                                          /* check arguments */
741 <        if ((inVec == NULL) | (dp == NULL))
741 >        if ((inVec == NULL) | (sdc == NULL) ||
742 >                        (dp = (SDMat *)sdc->dist) == NULL)
743                  return NULL;
744          memset(&myCD, 0, sizeof(myCD));
745          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 745 | Line 748 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
748                  myCD.ob_vec = dp->ob_vec;
749                  myCD.calen = dp->nout;
750                  reverse = 0;
751 <        } else {                        /* try reciprocity */
751 >        } else if (dp->ninc == dp->nout) {      /* try reciprocity */
752                  myCD.indx = mBSDF_outndx(dp, inVec);
753                  if (myCD.indx < 0)
754                          return NULL;
# Line 755 | Line 758 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
758                  reverse = 1;
759          }
760          cdlast = NULL;                  /* check for it in cache list */
761 <        for (cd = (SDMatCDst *)sdc->cdList;
762 <                                cd != NULL; cd = (SDMatCDst *)cd->next) {
761 >        for (cd = (SDMatCDst *)sdc->cdList; cd != NULL;
762 >                                cdlast = cd, cd = (SDMatCDst *)cd->next)
763                  if (cd->indx == myCD.indx && (cd->calen == myCD.calen) &
764                                          (cd->ob_priv == myCD.ob_priv) &
765                                          (cd->ob_vec == myCD.ob_vec))
766                          break;
764                cdlast = cd;
765        }
767          if (cd == NULL) {               /* need to allocate new entry */
768                  cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) +
769 <                                        myCD.calen*sizeof(myCD.carr[0]));
769 >                                        sizeof(myCD.carr[0])*myCD.calen);
770                  if (cd == NULL)
771                          return NULL;
772                  *cd = myCD;             /* compute cumulative distribution */
# Line 785 | Line 786 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
786  
787   /* Sample cumulative distribution */
788   static SDError
789 < SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp)
789 > SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp)
790   {
791          const unsigned  maxval = ~0;
792          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
793          const unsigned  target = randX*maxval;
794          int             i, iupper, ilower;
795                                          /* check arguments */
796 <        if ((outVec == NULL) | (mcd == NULL))
796 >        if ((ioVec == NULL) | (mcd == NULL))
797                  return SDEargument;
798                                          /* binary search to find index */
799          ilower = 0; iupper = mcd->calen;
800          while ((i = (iupper + ilower) >> 1) != ilower)
801 <                if ((long)target >= (long)mcd->carr[i])
801 >                if (target >= mcd->carr[i])
802                          ilower = i;
803                  else
804                          iupper = i;
# Line 805 | Line 806 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
806          randX = (randX*maxval - mcd->carr[ilower]) /
807                          (double)(mcd->carr[iupper] - mcd->carr[ilower]);
808                                          /* convert index to vector */
809 <        if ((*mcd->ob_vec)(outVec, i+randX, mcd->ob_priv))
809 >        if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv))
810                  return SDEnone;
811 <        strcpy(SDerrorDetail, "BSDF sampling fault");
811 >        strcpy(SDerrorDetail, "Matrix BSDF sampling fault");
812          return SDEinternal;
813   }
814  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines