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.9 by greg, Fri Apr 8 18:13:48 2011 UTC vs.
Revision 3.16 by greg, Thu Jun 9 17:09:39 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 81 | Line 82 | static int     nabases = 3;    /* current number of defined b
82   static int
83   fequal(double a, double b)
84   {
85 <        if (b != .0)
85 >        if (b != 0)
86                  a = a/b - 1.;
87          return (a <= 1e-6) & (a >= -1e-6);
88   }
# Line 193 | Line 194 | fo_getndx(const FVECT v, void *p)
194  
195          if (v == NULL)
196                  return -1;
197 <        if ((v[2] < .0) | (v[2] > 1.0))
197 >        if ((v[2] < 0) | (v[2] > 1.))
198                  return -1;
199          pol = 180.0/M_PI*acos(v[2]);
200          azi = 180.0/M_PI*atan2(v[1], v[0]);
# Line 352 | Line 353 | load_angle_basis(ezxml_t wab)
353                                          "ThetaBounds"), "LowerTheta"))),
354                                  abase_list[nabases].lat[i].tmin)) {
355                          sprintf(SDerrorDetail, "Theta values disagree in '%s'",
356 <                                abname);
356 >                                        abname);
357                          return RC_DATERR;
358                  }
359                  abase_list[nabases].nangles +=
# Line 362 | Line 363 | load_angle_basis(ezxml_t wab)
363                                  (abase_list[nabases].lat[i].nphis == 1 &&
364                                  abase_list[nabases].lat[i].tmin > FTINY)) {
365                          sprintf(SDerrorDetail, "Illegal phi count in '%s'",
366 <                                abname);
366 >                                        abname);
367                          return RC_DATERR;
368                  }
369          }
# Line 418 | Line 419 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
419          int             i;
420                                          /* allocate BSDF component */
421          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
422 +        if (!sdata)
423 +                return RC_FAIL;
424          /*
425           * Remember that front and back are reversed from WINDOW 6 orientations
426           * Favor their "Front" (incoming light) since that's more often valid
# Line 507 | Line 510 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
510          df->comp[0].dist = dp;
511          df->comp[0].func = &SDhandleMtx;
512                                          /* read BSDF data */
513 <        sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
513 >        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
514          if (!sdata || !*sdata) {
515                  sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
516                                  sd->name);
# Line 521 | Line 524 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
524                                          sd->name);
525                          return RC_FORMERR;
526                  }
527 <                while (*sdnext && isspace(*sdnext))
527 >                while (isspace(*sdnext))
528                          sdnext++;
529                  if (*sdnext == ',') sdnext++;
530                  if (rowinc) {
531                          int     r = i/dp->nout;
532 <                        int     c = i - c*dp->nout;
532 >                        int     c = i - r*dp->nout;
533                          mBSDF_value(dp,r,c) = atof(sdata);
534                  } else
535                          dp->bsdf[i] = atof(sdata);
# Line 546 | Line 549 | subtract_min(SDMat *sm)
549          for (i = n; --i; )
550                  if (sm->bsdf[i] < minv)
551                          minv = sm->bsdf[i];
552 +        
553 +        if (minv <= FTINY)
554 +                return .0;
555 +
556          for (i = n; i--; )
557                  sm->bsdf[i] -= minv;
558  
# Line 571 | Line 578 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
578                  c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
579                  dv->cieY += ymin;
580          }
581 <        df->maxHemi -= dv->cieY;        /* adjust minimum hemispherical */
581 >        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
582                                          /* make sure everything is set */
583          c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
584   }
# Line 580 | Line 587 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
587   SDError
588   SDloadMtx(SDData *sd, ezxml_t wtl)
589   {
590 <        ezxml_t                 wld, wdb;
591 <        int                     rowIn;
592 <        struct BSDF_data        *dp;
593 <        char                    *txt;
594 <        int                     rval;
588 <        
590 >        ezxml_t         wld, wdb;
591 >        int             rowIn;
592 >        char            *txt;
593 >        int             rval;
594 >                                        /* basic checks and data ordering */
595          txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
596                          "DataDefinition"), "IncidentDataStructure"));
597          if (txt == NULL || !*txt) {
# Line 604 | Line 610 | SDloadMtx(SDData *sd, ezxml_t wtl)
610                                  sd->name);
611                  return SDEsupport;
612          }
613 <                                /* get angle basis */
613 >                                        /* get angle basis */
614          rval = load_angle_basis(ezxml_child(ezxml_child(wtl,
615                                  "DataDefinition"), "AngleBasis"));
616          if (rval < 0)
617                  return convert_errcode(rval);
618 <                                /* load BSDF components */
618 >                                        /* load BSDF components */
619          for (wld = ezxml_child(wtl, "WavelengthData");
620                                  wld != NULL; wld = wld->next) {
621                  if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
# Line 620 | Line 626 | SDloadMtx(SDData *sd, ezxml_t wtl)
626                          if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0)
627                                  return convert_errcode(rval);
628          }
629 <                                /* separate diffuse components */
629 >                                        /* separate diffuse components */
630          extract_diffuse(&sd->rLambFront, sd->rf);
631          extract_diffuse(&sd->rLambBack, sd->rb);
632          extract_diffuse(&sd->tLamb, sd->tf);
633 <                                /* return success */
633 >                                        /* return success */
634          return SDEnone;
635   }
636  
637   /* Get Matrix BSDF value */
638   static int
639   SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
640 <                                const FVECT inVec, const void *dist)
640 >                                const FVECT inVec, SDComponent *sdc)
641   {
642 <        const SDMat     *dp = (const SDMat *)dist;
642 >        const SDMat     *dp;
643          int             i_ndx, o_ndx;
644 +                                        /* check arguments */
645 +        if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)
646 +                        || (dp = (SDMat *)sdc->dist) == NULL)
647 +                return 0;
648                                          /* get angle indices */
649          i_ndx = mBSDF_incndx(dp, inVec);
650          o_ndx = mBSDF_outndx(dp, outVec);
# Line 649 | Line 659 | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
659          return 1;                       /* XXX monochrome for now */
660   }
661  
662 < /* Query solid angle for vector */
662 > /* Query solid angle for vector(s) */
663   static SDError
664 < SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist)
664 > SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2,
665 >                                        int qflags, SDComponent *sdc)
666   {
667 <        const SDMat     *dp = (const SDMat *)dist;
667 >        const SDMat     *dp;
668          double          inc_psa, out_psa;
669                                          /* check arguments */
670 <        if ((psa == NULL) | (vec == NULL) | (dp == NULL))
670 >        if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) ||
671 >                        (dp = (SDMat *)sdc->dist) == NULL)
672                  return SDEargument;
673 +        if (v2 == NULL)
674 +                v2 = v1;
675                                          /* get projected solid angles */
676 <        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
677 <        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec));
676 >        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1));
677 >        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2));
678 >        if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) {
679 >                inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2));
680 >                out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1));
681 >        }
682  
683          switch (qflags) {               /* record based on flag settings */
666        case SDqueryVal:
667                psa[0] = .0;
668                /* fall through */
684          case SDqueryMax:
685                  if (inc_psa > psa[0])
686                          psa[0] = inc_psa;
# Line 673 | Line 688 | SDqueryMtxProjSA(double *psa, const FVECT vec, int qfl
688                          psa[0] = out_psa;
689                  break;
690          case SDqueryMin+SDqueryMax:
691 <                if (inc_psa > psa[0])
691 >                if (inc_psa > psa[1])
692                          psa[1] = inc_psa;
693 <                if (out_psa > psa[0])
693 >                if (out_psa > psa[1])
694                          psa[1] = out_psa;
695                  /* fall through */
696 +        case SDqueryVal:
697 +                if (qflags == SDqueryVal)
698 +                        psa[0] = M_PI;
699 +                /* fall through */
700          case SDqueryMin:
701 <                if ((inc_psa > .0) & (inc_psa < psa[0]))
701 >                if ((inc_psa > 0) & (inc_psa < psa[0]))
702                          psa[0] = inc_psa;
703 <                if ((out_psa > .0) & (out_psa < psa[0]))
703 >                if ((out_psa > 0) & (out_psa < psa[0]))
704                          psa[0] = out_psa;
705                  break;
706          }
707                                          /* make sure it's legal */
708 <        return (psa[0] <= .0) ? SDEinternal : SDEnone;
708 >        return (psa[0] <= 0) ? SDEinternal : SDEnone;
709   }
710  
711   /* Compute new cumulative distribution from BSDF */
# Line 724 | Line 743 | make_cdist(SDMatCDst *cd, const FVECT inVec, SDMat *dp
743   static const SDCDst *
744   SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
745   {
746 <        SDMat           *dp = (SDMat *)sdc->dist;
746 >        SDMat           *dp;
747          int             reverse;
748          SDMatCDst       myCD;
749          SDMatCDst       *cd, *cdlast;
750                                          /* check arguments */
751 <        if ((inVec == NULL) | (dp == NULL))
751 >        if ((inVec == NULL) | (sdc == NULL) ||
752 >                        (dp = (SDMat *)sdc->dist) == NULL)
753                  return NULL;
754          memset(&myCD, 0, sizeof(myCD));
755          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 748 | Line 768 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
768                  reverse = 1;
769          }
770          cdlast = NULL;                  /* check for it in cache list */
771 <        for (cd = (SDMatCDst *)sdc->cdList;
772 <                                cd != NULL; cd = (SDMatCDst *)cd->next) {
771 >        for (cd = (SDMatCDst *)sdc->cdList; cd != NULL;
772 >                                cdlast = cd, cd = (SDMatCDst *)cd->next)
773                  if (cd->indx == myCD.indx && (cd->calen == myCD.calen) &
774                                          (cd->ob_priv == myCD.ob_priv) &
775                                          (cd->ob_vec == myCD.ob_vec))
776                          break;
757                cdlast = cd;
758        }
777          if (cd == NULL) {               /* need to allocate new entry */
778                  cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) +
779 <                                        myCD.calen*sizeof(myCD.carr[0]));
779 >                                        sizeof(myCD.carr[0])*myCD.calen);
780                  if (cd == NULL)
781                          return NULL;
782                  *cd = myCD;             /* compute cumulative distribution */
# Line 778 | Line 796 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
796  
797   /* Sample cumulative distribution */
798   static SDError
799 < SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp)
799 > SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp)
800   {
801          const unsigned  maxval = ~0;
802          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
803          const unsigned  target = randX*maxval;
804          int             i, iupper, ilower;
805                                          /* check arguments */
806 <        if ((outVec == NULL) | (mcd == NULL))
806 >        if ((ioVec == NULL) | (mcd == NULL))
807                  return SDEargument;
808                                          /* binary search to find index */
809          ilower = 0; iupper = mcd->calen;
# Line 798 | Line 816 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
816          randX = (randX*maxval - mcd->carr[ilower]) /
817                          (double)(mcd->carr[iupper] - mcd->carr[ilower]);
818                                          /* convert index to vector */
819 <        if ((*mcd->ob_vec)(outVec, i+randX, mcd->ob_priv))
819 >        if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv))
820                  return SDEnone;
821 <        strcpy(SDerrorDetail, "BSDF sampling fault");
821 >        strcpy(SDerrorDetail, "Matrix BSDF sampling fault");
822          return SDEinternal;
823   }
824  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines