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.8 by greg, Thu Feb 24 20:14:26 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   }
89  
90 < /* returns the name of the given tag */
90 > /* Returns the name of the given tag */
91   #ifdef ezxml_name
92   #undef ezxml_name
93   static char *
# Line 98 | Line 99 | ezxml_name(ezxml_t xml)
99   }
100   #endif
101  
102 < /* returns the given tag's character content or empty string if none */
102 > /* Returns the given tag's character content or empty string if none */
103   #ifdef ezxml_txt
104   #undef ezxml_txt
105   static char *
# Line 159 | Line 160 | SDnewMatrix(int ni, int no)
160  
161   /* get vector for this angle basis index (front exiting) */
162   static int
163 < fo_getvec(FVECT v, int ndx, double randX, void *p)
163 > fo_getvec(FVECT v, double ndxr, void *p)
164   {
165 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
165 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
166 >        int             ndx = (int)ndxr;
167 >        double          randX = ndxr - ndx;
168          double          rx[2];
169          int             li;
170          double          pol, azi, d;
171          
172 <        if ((ndx < 0) | (ndx >= ab->nangles))
172 >        if ((ndxr < 0) | (ndx >= ab->nangles))
173                  return RC_FAIL;
174          for (li = 0; ndx >= ab->lat[li].nphis; li++)
175                  ndx -= ab->lat[li].nphis;
# Line 185 | Line 188 | fo_getvec(FVECT v, int ndx, double randX, void *p)
188   static int
189   fo_getndx(const FVECT v, void *p)
190   {
191 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
191 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
192          int     li, ndx;
193          double  pol, azi, d;
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 237 | Line 240 | io_getohm(int ndx, void *p)
240  
241   /* get vector for this angle basis index (back incident) */
242   static int
243 < bi_getvec(FVECT v, int ndx, double randX, void *p)
243 > bi_getvec(FVECT v, double ndxr, void *p)
244   {
245 <        if (!fo_getvec(v, ndx, randX, p))
245 >        if (!fo_getvec(v, ndxr, p))
246                  return RC_FAIL;
247  
248          v[0] = -v[0];
# Line 264 | Line 267 | bi_getndx(const FVECT v, void *p)
267  
268   /* get vector for this angle basis index (back exiting) */
269   static int
270 < bo_getvec(FVECT v, int ndx, double randX, void *p)
270 > bo_getvec(FVECT v, double ndxr, void *p)
271   {
272 <        if (!fo_getvec(v, ndx, randX, p))
272 >        if (!fo_getvec(v, ndxr, p))
273                  return RC_FAIL;
274  
275          v[2] = -v[2];
# Line 289 | Line 292 | bo_getndx(const FVECT v, void *p)
292  
293   /* get vector for this angle basis index (front incident) */
294   static int
295 < fi_getvec(FVECT v, int ndx, double randX, void *p)
295 > fi_getvec(FVECT v, double ndxr, void *p)
296   {
297 <        if (!fo_getvec(v, ndx, randX, p))
297 >        if (!fo_getvec(v, ndxr, p))
298                  return RC_FAIL;
299  
300          v[0] = -v[0];
# Line 350 | 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 360 | 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 416 | 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
427           */
428 <        if ((tfront = !strcasecmp(sdata, "Transmission Back")) ||
429 <                        (sd->tf == NULL &&
430 <                                !strcasecmp(sdata, "Transmission Front"))) {
428 >        tfront = !strcasecmp(sdata, "Transmission Back");
429 >        if (!strcasecmp(sdata, "Transmission Front") ||
430 >                        tfront & (sd->tf == NULL)) {
431                  if (sd->tf != NULL)
432                          SDfreeSpectralDF(sd->tf);
433                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
# Line 504 | 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 518 | 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 543 | 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 568 | 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 577 | 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;
585 <        
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 601 | 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 617 | 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 646 | 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 */
663        case SDqueryVal:
664                psa[0] = .0;
665                /* fall through */
684          case SDqueryMax:
685                  if (inc_psa > psa[0])
686                          psa[0] = inc_psa;
# Line 670 | 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 721 | 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 745 | 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;
754                cdlast = cd;
755        }
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 775 | 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 795 | 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