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.6 by greg, Mon Feb 21 22:50:37 2011 UTC vs.
Revision 3.17 by greg, Tue Jun 28 21:13:46 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 < #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 <
101 < /* returns the given tag's character content or empty string if none */
90 > /* Returns the given tag's character content or empty string if none */
91   #ifdef ezxml_txt
92   #undef ezxml_txt
93   static char *
# Line 157 | Line 146 | SDnewMatrix(int ni, int no)
146   /* Free a BSDF matrix */
147   #define SDfreeMatrix            free
148  
149 < /* get vector for this angle basis index */
149 > /* get vector for this angle basis index (front exiting) */
150   static int
151 < ab_getvec(FVECT v, int ndx, double randX, void *p)
151 > fo_getvec(FVECT v, double ndxr, void *p)
152   {
153 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
153 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
154 >        int             ndx = (int)ndxr;
155 >        double          randX = ndxr - ndx;
156          double          rx[2];
157          int             li;
158          double          pol, azi, d;
159          
160 <        if ((ndx < 0) | (ndx >= ab->nangles))
160 >        if ((ndxr < 0) | (ndx >= ab->nangles))
161                  return RC_FAIL;
162          for (li = 0; ndx >= ab->lat[li].nphis; li++)
163                  ndx -= ab->lat[li].nphis;
# Line 181 | Line 172 | ab_getvec(FVECT v, int ndx, double randX, void *p)
172          return RC_GOOD;
173   }
174  
175 < /* get index corresponding to the given vector */
175 > /* get index corresponding to the given vector (front exiting) */
176   static int
177 < ab_getndx(const FVECT v, void *p)
177 > fo_getndx(const FVECT v, void *p)
178   {
179 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
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;
185 <        if ((v[2] < .0) | (v[2] > 1.0))
185 >        if ((v[2] < 0) | (v[2] > 1.))
186                  return -1;
187          pol = 180.0/M_PI*acos(v[2]);
188          azi = 180.0/M_PI*atan2(v[1], v[0]);
# Line 210 | Line 201 | ab_getndx(const FVECT v, void *p)
201   /* compute square of real value */
202   static double sq(double x) { return x*x; }
203  
204 < /* get projected solid angle for this angle basis index */
204 > /* get projected solid angle for this angle basis index (universal) */
205   static double
206 < ab_getohm(int ndx, void *p)
206 > io_getohm(int ndx, void *p)
207   {
208          static int      last_li = -1;
209          static double   last_ohm;
# Line 235 | Line 226 | ab_getohm(int ndx, void *p)
226                                  (double)ab->lat[li].nphis;
227   }
228  
229 < /* get reverse vector for this angle basis index */
229 > /* get vector for this angle basis index (back incident) */
230   static int
231 < ab_getvecR(FVECT v, int ndx, double randX, void *p)
231 > bi_getvec(FVECT v, double ndxr, void *p)
232   {
233 <        int     na = (*(ANGLE_BASIS *)p).nangles;
243 <
244 <        if (!ab_getvec(v, ndx, randX, p))
233 >        if (!fo_getvec(v, ndxr, p))
234                  return RC_FAIL;
235  
236          v[0] = -v[0];
# Line 251 | Line 240 | ab_getvecR(FVECT v, int ndx, double randX, void *p)
240          return RC_GOOD;
241   }
242  
243 < /* get index corresponding to the reverse vector */
243 > /* get index corresponding to the vector (back incident) */
244   static int
245 < ab_getndxR(const FVECT v, void *p)
245 > bi_getndx(const FVECT v, void *p)
246   {
247          FVECT  v2;
248          
# Line 261 | Line 250 | ab_getndxR(const FVECT v, void *p)
250          v2[1] = -v[1];
251          v2[2] = -v[2];
252  
253 <        return ab_getndx(v2, p);
253 >        return fo_getndx(v2, p);
254   }
255  
256 + /* get vector for this angle basis index (back exiting) */
257 + static int
258 + bo_getvec(FVECT v, double ndxr, void *p)
259 + {
260 +        if (!fo_getvec(v, ndxr, p))
261 +                return RC_FAIL;
262 +
263 +        v[2] = -v[2];
264 +
265 +        return RC_GOOD;
266 + }
267 +
268 + /* get index corresponding to the vector (back exiting) */
269 + static int
270 + bo_getndx(const FVECT v, void *p)
271 + {
272 +        FVECT  v2;
273 +        
274 +        v2[0] = v[0];
275 +        v2[1] = v[1];
276 +        v2[2] = -v[2];
277 +
278 +        return fo_getndx(v2, p);
279 + }
280 +
281 + /* get vector for this angle basis index (front incident) */
282 + static int
283 + fi_getvec(FVECT v, double ndxr, void *p)
284 + {
285 +        if (!fo_getvec(v, ndxr, p))
286 +                return RC_FAIL;
287 +
288 +        v[0] = -v[0];
289 +        v[1] = -v[1];
290 +
291 +        return RC_GOOD;
292 + }
293 +
294 + /* get index corresponding to the vector (front incident) */
295 + static int
296 + fi_getndx(const FVECT v, void *p)
297 + {
298 +        FVECT  v2;
299 +        
300 +        v2[0] = -v[0];
301 +        v2[1] = -v[1];
302 +        v2[2] = v[2];
303 +
304 +        return fo_getndx(v2, p);
305 + }
306 +
307   /* load custom BSDF angle basis */
308   static int
309   load_angle_basis(ezxml_t wab)
# Line 301 | 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 311 | 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 362 | Line 402 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
402          SDSpectralDF    *df;
403          SDMat           *dp;
404          char            *sdata;
405 +        int             tfront;
406          int             inbi, outbi;
407          int             i;
408                                          /* allocate BSDF component */
409          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
410 <        if (!strcasecmp(sdata, "Transmission Back") || (sd->tf == NULL &&
411 <                        !strcasecmp(sdata, "Transmission Front"))) {
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
415 >         */
416 >        tfront = !strcasecmp(sdata, "Transmission Back");
417 >        if (!strcasecmp(sdata, "Transmission Front") ||
418 >                        tfront & (sd->tf == NULL)) {
419                  if (sd->tf != NULL)
420                          SDfreeSpectralDF(sd->tf);
421                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
# Line 422 | Line 470 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
470          dp->ib_priv = &abase_list[inbi];
471          dp->ob_priv = &abase_list[outbi];
472          if (df == sd->tf) {
473 <                dp->ib_vec = &ab_getvecR;
474 <                dp->ib_ndx = &ab_getndxR;
475 <                dp->ob_vec = &ab_getvec;
476 <                dp->ob_ndx = &ab_getndx;
473 >                if (tfront) {
474 >                        dp->ib_vec = &fi_getvec;
475 >                        dp->ib_ndx = &fi_getndx;
476 >                        dp->ob_vec = &bo_getvec;
477 >                        dp->ob_ndx = &bo_getndx;
478 >                } else {
479 >                        dp->ib_vec = &bi_getvec;
480 >                        dp->ib_ndx = &bi_getndx;
481 >                        dp->ob_vec = &fo_getvec;
482 >                        dp->ob_ndx = &fo_getndx;
483 >                }
484          } else if (df == sd->rf) {
485 <                dp->ib_vec = &ab_getvec;
486 <                dp->ib_ndx = &ab_getndx;
487 <                dp->ob_vec = &ab_getvec;
488 <                dp->ob_ndx = &ab_getndx;
485 >                dp->ib_vec = &fi_getvec;
486 >                dp->ib_ndx = &fi_getndx;
487 >                dp->ob_vec = &fo_getvec;
488 >                dp->ob_ndx = &fo_getndx;
489          } else /* df == sd->rb */ {
490 <                dp->ib_vec = &ab_getvecR;
491 <                dp->ib_ndx = &ab_getndxR;
492 <                dp->ob_vec = &ab_getvecR;
493 <                dp->ob_ndx = &ab_getndxR;
490 >                dp->ib_vec = &bi_getvec;
491 >                dp->ib_ndx = &bi_getndx;
492 >                dp->ob_vec = &bo_getvec;
493 >                dp->ob_ndx = &bo_getndx;
494          }
495 <        dp->ib_ohm = &ab_getohm;
496 <        dp->ob_ohm = &ab_getohm;
495 >        dp->ib_ohm = &io_getohm;
496 >        dp->ob_ohm = &io_getohm;
497          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
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 457 | 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 482 | 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 507 | 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 516 | 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;
524 <        
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 540 | Line 598 | SDloadMtx(SDData *sd, ezxml_t wtl)
598                                  sd->name);
599                  return SDEsupport;
600          }
601 <                                /* get angle basis */
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 */
606 >                                        /* load BSDF components */
607          for (wld = ezxml_child(wtl, "WavelengthData");
608                                  wld != NULL; wld = wld->next) {
609                  if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
# Line 556 | Line 614 | SDloadMtx(SDData *sd, ezxml_t wtl)
614                          if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0)
615                                  return convert_errcode(rval);
616          }
617 <                                /* separate diffuse components */
617 >                                        /* separate diffuse components */
618          extract_diffuse(&sd->rLambFront, sd->rf);
619          extract_diffuse(&sd->rLambBack, sd->rb);
620          extract_diffuse(&sd->tLamb, sd->tf);
621 <                                /* return success */
621 >                                        /* return success */
622          return SDEnone;
623   }
624  
625   /* Get Matrix BSDF value */
626   static int
627   SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
628 <                                const FVECT inVec, const void *dist)
628 >                                const FVECT inVec, SDComponent *sdc)
629   {
630 <        const SDMat     *dp = (const SDMat *)dist;
630 >        const SDMat     *dp;
631          int             i_ndx, o_ndx;
632 +                                        /* check arguments */
633 +        if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)
634 +                        || (dp = (SDMat *)sdc->dist) == NULL)
635 +                return 0;
636                                          /* get angle indices */
637          i_ndx = mBSDF_incndx(dp, inVec);
638          o_ndx = mBSDF_outndx(dp, outVec);
# Line 585 | Line 647 | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
647          return 1;                       /* XXX monochrome for now */
648   }
649  
650 < /* Query solid angle for vector */
650 > /* Query solid angle for vector(s) */
651   static SDError
652 < SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist)
652 > SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2,
653 >                                        int qflags, SDComponent *sdc)
654   {
655 <        const SDMat     *dp = (const SDMat *)dist;
655 >        const SDMat     *dp;
656          double          inc_psa, out_psa;
657                                          /* check arguments */
658 <        if ((psa == NULL) | (vec == NULL) | (dp == NULL))
658 >        if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) ||
659 >                        (dp = (SDMat *)sdc->dist) == NULL)
660                  return SDEargument;
661 +        if (v2 == NULL)
662 +                v2 = v1;
663                                          /* get projected solid angles */
664 <        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
665 <        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec));
664 >        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1));
665 >        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2));
666 >        if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) {
667 >                inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2));
668 >                out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1));
669 >        }
670  
671          switch (qflags) {               /* record based on flag settings */
602        case SDqueryVal:
603                psa[0] = .0;
604                /* fall through */
672          case SDqueryMax:
673                  if (inc_psa > psa[0])
674                          psa[0] = inc_psa;
# Line 609 | Line 676 | SDqueryMtxProjSA(double *psa, const FVECT vec, int qfl
676                          psa[0] = out_psa;
677                  break;
678          case SDqueryMin+SDqueryMax:
679 <                if (inc_psa > psa[0])
679 >                if (inc_psa > psa[1])
680                          psa[1] = inc_psa;
681 <                if (out_psa > psa[0])
681 >                if (out_psa > psa[1])
682                          psa[1] = out_psa;
683                  /* fall through */
684 +        case SDqueryVal:
685 +                if (qflags == SDqueryVal)
686 +                        psa[0] = M_PI;
687 +                /* fall through */
688          case SDqueryMin:
689 <                if ((inc_psa > .0) & (inc_psa < psa[0]))
689 >                if ((inc_psa > 0) & (inc_psa < psa[0]))
690                          psa[0] = inc_psa;
691 <                if ((out_psa > .0) & (out_psa < psa[0]))
691 >                if ((out_psa > 0) & (out_psa < psa[0]))
692                          psa[0] = out_psa;
693                  break;
694          }
695                                          /* make sure it's legal */
696 <        return (psa[0] <= .0) ? SDEinternal : SDEnone;
696 >        return (psa[0] <= 0) ? SDEinternal : SDEnone;
697   }
698  
699   /* Compute new cumulative distribution from BSDF */
# Line 660 | Line 731 | make_cdist(SDMatCDst *cd, const FVECT inVec, SDMat *dp
731   static const SDCDst *
732   SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
733   {
734 <        SDMat           *dp = (SDMat *)sdc->dist;
734 >        SDMat           *dp;
735          int             reverse;
736          SDMatCDst       myCD;
737          SDMatCDst       *cd, *cdlast;
738                                          /* check arguments */
739 <        if ((inVec == NULL) | (dp == NULL))
739 >        if ((inVec == NULL) | (sdc == NULL) ||
740 >                        (dp = (SDMat *)sdc->dist) == NULL)
741                  return NULL;
742          memset(&myCD, 0, sizeof(myCD));
743          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 684 | Line 756 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
756                  reverse = 1;
757          }
758          cdlast = NULL;                  /* check for it in cache list */
759 <        for (cd = (SDMatCDst *)sdc->cdList;
760 <                                cd != NULL; cd = (SDMatCDst *)cd->next) {
759 >        for (cd = (SDMatCDst *)sdc->cdList; cd != NULL;
760 >                                cdlast = cd, cd = (SDMatCDst *)cd->next)
761                  if (cd->indx == myCD.indx && (cd->calen == myCD.calen) &
762                                          (cd->ob_priv == myCD.ob_priv) &
763                                          (cd->ob_vec == myCD.ob_vec))
764                          break;
693                cdlast = cd;
694        }
765          if (cd == NULL) {               /* need to allocate new entry */
766                  cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) +
767 <                                        myCD.calen*sizeof(myCD.carr[0]));
767 >                                        sizeof(myCD.carr[0])*myCD.calen);
768                  if (cd == NULL)
769                          return NULL;
770                  *cd = myCD;             /* compute cumulative distribution */
# Line 714 | Line 784 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
784  
785   /* Sample cumulative distribution */
786   static SDError
787 < SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp)
787 > SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp)
788   {
789          const unsigned  maxval = ~0;
790          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
791          const unsigned  target = randX*maxval;
792          int             i, iupper, ilower;
793                                          /* check arguments */
794 <        if ((outVec == NULL) | (mcd == NULL))
794 >        if ((ioVec == NULL) | (mcd == NULL))
795                  return SDEargument;
796                                          /* binary search to find index */
797          ilower = 0; iupper = mcd->calen;
# Line 734 | Line 804 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
804          randX = (randX*maxval - mcd->carr[ilower]) /
805                          (double)(mcd->carr[iupper] - mcd->carr[ilower]);
806                                          /* convert index to vector */
807 <        if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv))
807 >        if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv))
808                  return SDEnone;
809 <        strcpy(SDerrorDetail, "BSDF sampling fault");
809 >        strcpy(SDerrorDetail, "Matrix BSDF sampling fault");
810          return SDEinternal;
811   }
812  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines