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.5 by greg, Sat Feb 19 23:42:09 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 157 | Line 158 | SDnewMatrix(int ni, int no)
158   /* Free a BSDF matrix */
159   #define SDfreeMatrix            free
160  
161 < /* get vector for this angle basis index */
161 > /* get vector for this angle basis index (front exiting) */
162   static int
163 < ab_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 181 | Line 184 | ab_getvec(FVECT v, int ndx, double randX, void *p)
184          return RC_GOOD;
185   }
186  
187 < /* get index corresponding to the given vector */
187 > /* get index corresponding to the given vector (front exiting) */
188   static int
189 < ab_getndx(const FVECT v, void *p)
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 210 | Line 213 | ab_getndx(const FVECT v, void *p)
213   /* compute square of real value */
214   static double sq(double x) { return x*x; }
215  
216 < /* get projected solid angle for this angle basis index */
216 > /* get projected solid angle for this angle basis index (universal) */
217   static double
218 < ab_getohm(int ndx, void *p)
218 > io_getohm(int ndx, void *p)
219   {
220          static int      last_li = -1;
221          static double   last_ohm;
# Line 235 | Line 238 | ab_getohm(int ndx, void *p)
238                                  (double)ab->lat[li].nphis;
239   }
240  
241 < /* get reverse vector for this angle basis index */
241 > /* get vector for this angle basis index (back incident) */
242   static int
243 < ab_getvecR(FVECT v, int ndx, double randX, void *p)
243 > bi_getvec(FVECT v, double ndxr, void *p)
244   {
245 <        int     na = (*(ANGLE_BASIS *)p).nangles;
243 <
244 <        if (!ab_getvec(v, ndx, randX, p))
245 >        if (!fo_getvec(v, ndxr, p))
246                  return RC_FAIL;
247  
248          v[0] = -v[0];
# Line 251 | Line 252 | ab_getvecR(FVECT v, int ndx, double randX, void *p)
252          return RC_GOOD;
253   }
254  
255 < /* get index corresponding to the reverse vector */
255 > /* get index corresponding to the vector (back incident) */
256   static int
257 < ab_getndxR(const FVECT v, void *p)
257 > bi_getndx(const FVECT v, void *p)
258   {
259          FVECT  v2;
260          
# Line 261 | Line 262 | ab_getndxR(const FVECT v, void *p)
262          v2[1] = -v[1];
263          v2[2] = -v[2];
264  
265 <        return ab_getndx(v2, p);
265 >        return fo_getndx(v2, p);
266   }
267  
268 + /* get vector for this angle basis index (back exiting) */
269 + static int
270 + bo_getvec(FVECT v, double ndxr, void *p)
271 + {
272 +        if (!fo_getvec(v, ndxr, p))
273 +                return RC_FAIL;
274 +
275 +        v[2] = -v[2];
276 +
277 +        return RC_GOOD;
278 + }
279 +
280 + /* get index corresponding to the vector (back exiting) */
281 + static int
282 + bo_getndx(const FVECT v, void *p)
283 + {
284 +        FVECT  v2;
285 +        
286 +        v2[0] = v[0];
287 +        v2[1] = v[1];
288 +        v2[2] = -v[2];
289 +
290 +        return fo_getndx(v2, p);
291 + }
292 +
293 + /* get vector for this angle basis index (front incident) */
294 + static int
295 + fi_getvec(FVECT v, double ndxr, void *p)
296 + {
297 +        if (!fo_getvec(v, ndxr, p))
298 +                return RC_FAIL;
299 +
300 +        v[0] = -v[0];
301 +        v[1] = -v[1];
302 +
303 +        return RC_GOOD;
304 + }
305 +
306 + /* get index corresponding to the vector (front incident) */
307 + static int
308 + fi_getndx(const FVECT v, void *p)
309 + {
310 +        FVECT  v2;
311 +        
312 +        v2[0] = -v[0];
313 +        v2[1] = -v[1];
314 +        v2[2] = v[2];
315 +
316 +        return fo_getndx(v2, p);
317 + }
318 +
319   /* load custom BSDF angle basis */
320   static int
321   load_angle_basis(ezxml_t wab)
# Line 301 | 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 311 | 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 362 | Line 414 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
414          SDSpectralDF    *df;
415          SDMat           *dp;
416          char            *sdata;
417 +        int             tfront;
418          int             inbi, outbi;
419          int             i;
420                                          /* allocate BSDF component */
421          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
422 <        if (!strcasecmp(sdata, "Transmission Front")) {
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 >        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)
434                          return RC_MEMERR;
435                  df = sd->tf;
436          } else if (!strcasecmp(sdata, "Reflection Front")) {
437 <                if (sd->rf != NULL)
377 <                        SDfreeSpectralDF(sd->rf);
378 <                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
379 <                        return RC_MEMERR;
380 <                df = sd->rf;
381 <        } else if (!strcasecmp(sdata, "Reflection Back")) {
382 <                if (sd->rb != NULL)
437 >                if (sd->rb != NULL)     /* note back-front reversal */
438                          SDfreeSpectralDF(sd->rb);
439                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
440                          return RC_MEMERR;
441                  df = sd->rb;
442 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
443 +                if (sd->rf != NULL)     /* note front-back reversal */
444 +                        SDfreeSpectralDF(sd->rf);
445 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
446 +                        return RC_MEMERR;
447 +                df = sd->rf;
448          } else
449                  return RC_FAIL;
450          /* XXX should also check "ScatteringDataType" for consistency? */
# Line 421 | Line 482 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
482          dp->ib_priv = &abase_list[inbi];
483          dp->ob_priv = &abase_list[outbi];
484          if (df == sd->tf) {
485 <                dp->ib_vec = &ab_getvecR;
486 <                dp->ib_ndx = &ab_getndxR;
487 <                dp->ob_vec = &ab_getvec;
488 <                dp->ob_ndx = &ab_getndx;
485 >                if (tfront) {
486 >                        dp->ib_vec = &fi_getvec;
487 >                        dp->ib_ndx = &fi_getndx;
488 >                        dp->ob_vec = &bo_getvec;
489 >                        dp->ob_ndx = &bo_getndx;
490 >                } else {
491 >                        dp->ib_vec = &bi_getvec;
492 >                        dp->ib_ndx = &bi_getndx;
493 >                        dp->ob_vec = &fo_getvec;
494 >                        dp->ob_ndx = &fo_getndx;
495 >                }
496          } else if (df == sd->rf) {
497 <                dp->ib_vec = &ab_getvec;
498 <                dp->ib_ndx = &ab_getndx;
499 <                dp->ob_vec = &ab_getvec;
500 <                dp->ob_ndx = &ab_getndx;
497 >                dp->ib_vec = &fi_getvec;
498 >                dp->ib_ndx = &fi_getndx;
499 >                dp->ob_vec = &fo_getvec;
500 >                dp->ob_ndx = &fo_getndx;
501          } else /* df == sd->rb */ {
502 <                dp->ib_vec = &ab_getvecR;
503 <                dp->ib_ndx = &ab_getndxR;
504 <                dp->ob_vec = &ab_getvecR;
505 <                dp->ob_ndx = &ab_getndxR;
502 >                dp->ib_vec = &bi_getvec;
503 >                dp->ib_ndx = &bi_getndx;
504 >                dp->ob_vec = &bo_getvec;
505 >                dp->ob_ndx = &bo_getndx;
506          }
507 <        dp->ib_ohm = &ab_getohm;
508 <        dp->ob_ohm = &ab_getohm;
507 >        dp->ib_ohm = &io_getohm;
508 >        dp->ob_ohm = &io_getohm;
509          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
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 456 | 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 481 | 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 506 | 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 515 | 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;
523 <        
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 539 | 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 555 | 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 584 | 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 */
601        case SDqueryVal:
602                psa[0] = .0;
603                /* fall through */
684          case SDqueryMax:
685                  if (inc_psa > psa[0])
686                          psa[0] = inc_psa;
# Line 608 | 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 659 | 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 683 | 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;
692                cdlast = cd;
693        }
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 713 | 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 733 | 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