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.1 by greg, Fri Feb 18 00:40:25 2011 UTC vs.
Revision 3.8 by greg, Thu Feb 24 20:14:26 2011 UTC

# Line 1 | Line 1
1 + #ifndef lint
2 + static const char RCSid[] = "$Id$";
3 + #endif
4   /*
5   *  bsdf_m.c
6   *  
# Line 8 | Line 11
11   *
12   */
13  
14 < #include <stdio.h>
14 > #include "rtio.h"
15   #include <stdlib.h>
16   #include <math.h>
14 #include <strings.h>
17   #include <ctype.h>
18   #include "ezxml.h"
19   #include "bsdf.h"
20   #include "bsdf_m.h"
21  
20 #ifndef FTINY
21 #define FTINY           1e-6
22 #endif
23
22   /* Function return codes */
23   #define RC_GOOD         1
24   #define RC_FAIL         0
# Line 159 | Line 157 | SDnewMatrix(int ni, int no)
157   /* Free a BSDF matrix */
158   #define SDfreeMatrix            free
159  
160 < /* get vector for this angle basis index */
160 > /* get vector for this angle basis index (front exiting) */
161   static int
162 < ab_getvec(FVECT v, int ndx, double randX, void *p)
162 > fo_getvec(FVECT v, int ndx, double randX, void *p)
163   {
164          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
165          double          rx[2];
# Line 183 | Line 181 | ab_getvec(FVECT v, int ndx, double randX, void *p)
181          return RC_GOOD;
182   }
183  
184 < /* get index corresponding to the given vector */
184 > /* get index corresponding to the given vector (front exiting) */
185   static int
186 < ab_getndx(const FVECT v, void *p)
186 > fo_getndx(const FVECT v, void *p)
187   {
188          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
189          int     li, ndx;
# Line 212 | Line 210 | ab_getndx(const FVECT v, void *p)
210   /* compute square of real value */
211   static double sq(double x) { return x*x; }
212  
213 < /* get projected solid angle for this angle basis index */
213 > /* get projected solid angle for this angle basis index (universal) */
214   static double
215 < ab_getohm(int ndx, void *p)
215 > io_getohm(int ndx, void *p)
216   {
217          static int      last_li = -1;
218          static double   last_ohm;
# Line 237 | Line 235 | ab_getohm(int ndx, void *p)
235                                  (double)ab->lat[li].nphis;
236   }
237  
238 < /* get reverse vector for this angle basis index */
238 > /* get vector for this angle basis index (back incident) */
239   static int
240 < ab_getvecR(FVECT v, int ndx, double randX, void *p)
240 > bi_getvec(FVECT v, int ndx, double randX, void *p)
241   {
242 <        int     na = (*(ANGLE_BASIS *)p).nangles;
245 <
246 <        if (!ab_getvec(v, ndx, randX, p))
242 >        if (!fo_getvec(v, ndx, randX, p))
243                  return RC_FAIL;
244  
245          v[0] = -v[0];
# Line 253 | Line 249 | ab_getvecR(FVECT v, int ndx, double randX, void *p)
249          return RC_GOOD;
250   }
251  
252 < /* get index corresponding to the reverse vector */
252 > /* get index corresponding to the vector (back incident) */
253   static int
254 < ab_getndxR(const FVECT v, void *p)
254 > bi_getndx(const FVECT v, void *p)
255   {
256          FVECT  v2;
257          
# Line 263 | Line 259 | ab_getndxR(const FVECT v, void *p)
259          v2[1] = -v[1];
260          v2[2] = -v[2];
261  
262 <        return ab_getndx(v2, p);
262 >        return fo_getndx(v2, p);
263   }
264  
265 + /* get vector for this angle basis index (back exiting) */
266 + static int
267 + bo_getvec(FVECT v, int ndx, double randX, void *p)
268 + {
269 +        if (!fo_getvec(v, ndx, randX, p))
270 +                return RC_FAIL;
271 +
272 +        v[2] = -v[2];
273 +
274 +        return RC_GOOD;
275 + }
276 +
277 + /* get index corresponding to the vector (back exiting) */
278 + static int
279 + bo_getndx(const FVECT v, void *p)
280 + {
281 +        FVECT  v2;
282 +        
283 +        v2[0] = v[0];
284 +        v2[1] = v[1];
285 +        v2[2] = -v[2];
286 +
287 +        return fo_getndx(v2, p);
288 + }
289 +
290 + /* get vector for this angle basis index (front incident) */
291 + static int
292 + fi_getvec(FVECT v, int ndx, double randX, void *p)
293 + {
294 +        if (!fo_getvec(v, ndx, randX, p))
295 +                return RC_FAIL;
296 +
297 +        v[0] = -v[0];
298 +        v[1] = -v[1];
299 +
300 +        return RC_GOOD;
301 + }
302 +
303 + /* get index corresponding to the vector (front incident) */
304 + static int
305 + fi_getndx(const FVECT v, void *p)
306 + {
307 +        FVECT  v2;
308 +        
309 +        v2[0] = -v[0];
310 +        v2[1] = -v[1];
311 +        v2[2] = v[2];
312 +
313 +        return fo_getndx(v2, p);
314 + }
315 +
316   /* load custom BSDF angle basis */
317   static int
318   load_angle_basis(ezxml_t wab)
# Line 348 | Line 395 | get_extrema(SDSpectralDF *df)
395          }
396          free(ohma);
397                                          /* need incoming solid angles, too? */
398 <        if (dp->ninc < dp->nout || dp->ib_ohm != dp->ob_ohm ||
352 <                                dp->ib_priv != dp->ob_priv) {
398 >        if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) {
399                  double  ohm;
400                  for (i = dp->ninc; i--; )
401                          if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA)
# Line 358 | Line 404 | get_extrema(SDSpectralDF *df)
404          return (df->maxHemi <= 1.01);
405   }
406  
361 /* skip integer in string */
362 static char *
363 i_skip(char  *s)
364 {
365        while (isspace(*s))
366                s++;
367        if (*s == '-' || *s == '+')
368                s++;
369        if (!isdigit(*s))
370                return(NULL);
371        do
372                s++;
373        while (isdigit(*s));
374        return(s);
375 }
376
377 /* skip float in string */
378 static char *
379 f_skip(char  *s)
380 {
381        register char  *cp;
382
383        while (isspace(*s))
384                s++;
385        if (*s == '-' || *s == '+')
386                s++;
387        cp = s;
388        while (isdigit(*cp))
389                cp++;
390        if (*cp == '.') {
391                cp++; s++;
392                while (isdigit(*cp))
393                        cp++;
394        }
395        if (cp == s)
396                return(NULL);
397        if (*cp == 'e' || *cp == 'E')
398                return(i_skip(cp+1));
399        return(cp);
400 }
401
407   /* load BSDF distribution for this wavelength */
408   static int
409   load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
410   {
406        char            *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
407        char            *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
411          SDSpectralDF    *df;
412          SDMat           *dp;
413          char            *sdata;
414 +        int             tfront;
415          int             inbi, outbi;
416          int             i;
413
414        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
415                sprintf(SDerrorDetail, "Missing column/row basis for BSDF '%s'",
416                                sd->name);
417                return RC_FORMERR;
418        }
417                                          /* allocate BSDF component */
418          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
419 <        if (!strcasecmp(sdata, "Transmission Front")) {
419 >        /*
420 >         * Remember that front and back are reversed from WINDOW 6 orientations
421 >         */
422 >        if ((tfront = !strcasecmp(sdata, "Transmission Back")) ||
423 >                        (sd->tf == NULL &&
424 >                                !strcasecmp(sdata, "Transmission Front"))) {
425                  if (sd->tf != NULL)
426                          SDfreeSpectralDF(sd->tf);
427                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
428                          return RC_MEMERR;
429                  df = sd->tf;
430          } else if (!strcasecmp(sdata, "Reflection Front")) {
431 <                if (sd->rf != NULL)
429 <                        SDfreeSpectralDF(sd->rf);
430 <                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
431 <                        return RC_MEMERR;
432 <                df = sd->rf;
433 <        } else if (!strcasecmp(sdata, "Reflection Back")) {
434 <                if (sd->rb != NULL)
431 >                if (sd->rb != NULL)     /* note back-front reversal */
432                          SDfreeSpectralDF(sd->rb);
433                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
434                          return RC_MEMERR;
435                  df = sd->rb;
436 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
437 +                if (sd->rf != NULL)     /* note front-back reversal */
438 +                        SDfreeSpectralDF(sd->rf);
439 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
440 +                        return RC_MEMERR;
441 +                df = sd->rf;
442          } else
443                  return RC_FAIL;
444 +        /* XXX should also check "ScatteringDataType" for consistency? */
445                                          /* get angle bases */
446          sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
447          if (!sdata || !*sdata) {
# Line 446 | Line 450 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
450                  return RC_FORMERR;
451          }
452          for (inbi = nabases; inbi--; )
453 <                if (!strcasecmp(cbasis, abase_list[inbi].name))
453 >                if (!strcasecmp(sdata, abase_list[inbi].name))
454                          break;
455          if (inbi < 0) {
456 <                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'",
453 <                                cbasis);
456 >                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata);
457                  return RC_FORMERR;
458          }
459          sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
# Line 460 | Line 463 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
463                  return RC_FORMERR;
464          }
465          for (outbi = nabases; outbi--; )
466 <                if (!strcasecmp(rbasis, abase_list[outbi].name))
466 >                if (!strcasecmp(sdata, abase_list[outbi].name))
467                          break;
468          if (outbi < 0) {
469 <                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", cbasis);
469 >                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata);
470                  return RC_FORMERR;
471          }
472                                          /* allocate BSDF matrix */
473          dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles);
474          if (dp == NULL)
475                  return RC_MEMERR;
476 <        dp->ib_priv = (void *)&abase_list[inbi];
477 <        dp->ob_priv = (void *)&abase_list[outbi];
476 >        dp->ib_priv = &abase_list[inbi];
477 >        dp->ob_priv = &abase_list[outbi];
478          if (df == sd->tf) {
479 <                dp->ib_vec = ab_getvecR;
480 <                dp->ib_ndx = ab_getndxR;
481 <                dp->ob_vec = ab_getvec;
482 <                dp->ob_ndx = ab_getndx;
479 >                if (tfront) {
480 >                        dp->ib_vec = &fi_getvec;
481 >                        dp->ib_ndx = &fi_getndx;
482 >                        dp->ob_vec = &bo_getvec;
483 >                        dp->ob_ndx = &bo_getndx;
484 >                } else {
485 >                        dp->ib_vec = &bi_getvec;
486 >                        dp->ib_ndx = &bi_getndx;
487 >                        dp->ob_vec = &fo_getvec;
488 >                        dp->ob_ndx = &fo_getndx;
489 >                }
490          } else if (df == sd->rf) {
491 <                dp->ib_vec = ab_getvec;
492 <                dp->ib_ndx = ab_getndx;
493 <                dp->ob_vec = ab_getvec;
494 <                dp->ob_ndx = ab_getndx;
491 >                dp->ib_vec = &fi_getvec;
492 >                dp->ib_ndx = &fi_getndx;
493 >                dp->ob_vec = &fo_getvec;
494 >                dp->ob_ndx = &fo_getndx;
495          } else /* df == sd->rb */ {
496 <                dp->ib_vec = ab_getvecR;
497 <                dp->ib_ndx = ab_getndxR;
498 <                dp->ob_vec = ab_getvecR;
499 <                dp->ob_ndx = ab_getndxR;
496 >                dp->ib_vec = &bi_getvec;
497 >                dp->ib_ndx = &bi_getndx;
498 >                dp->ob_vec = &bo_getvec;
499 >                dp->ob_ndx = &bo_getndx;
500          }
501 <        dp->ib_ohm = ab_getohm;
502 <        dp->ob_ohm = ab_getohm;
501 >        dp->ib_ohm = &io_getohm;
502 >        dp->ob_ohm = &io_getohm;
503          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
504          df->comp[0].dist = dp;
505          df->comp[0].func = &SDhandleMtx;
# Line 501 | Line 511 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
511                  return RC_FORMERR;
512          }
513          for (i = 0; i < dp->ninc*dp->nout; i++) {
514 <                char  *sdnext = f_skip(sdata);
514 >                char  *sdnext = fskip(sdata);
515                  if (sdnext == NULL) {
516                          sprintf(SDerrorDetail,
517                                  "Bad/missing BSDF ScatteringData in '%s'",
# Line 558 | Line 568 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
568                  c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
569                  dv->cieY += ymin;
570          }
571 <        df->maxHemi -= dv->cieY;        /* correct minimum hemispherical */
572 <        dv->spec.clock++;               /* make sure everything is set */
571 >        df->maxHemi -= dv->cieY;        /* adjust minimum hemispherical */
572 >                                        /* make sure everything is set */
573          c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
574   }
575  
576   /* Load a BSDF matrix from an open XML file */
577   SDError
578 < SDloadMtx(SDData *sd, ezxml_t fl)
578 > SDloadMtx(SDData *sd, ezxml_t wtl)
579   {
580 <        ezxml_t                 wtl, wld, wdb;
580 >        ezxml_t                 wld, wdb;
581          int                     rowIn;
582          struct BSDF_data        *dp;
583          char                    *txt;
584          int                     rval;
585          
586 <        if (strcmp(ezxml_name(fl), "WindowElement")) {
586 >        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
587 >                        "DataDefinition"), "IncidentDataStructure"));
588 >        if (txt == NULL || !*txt) {
589                  sprintf(SDerrorDetail,
590 <                        "BSDF \"%s\": top level node not 'WindowElement'",
590 >                        "BSDF \"%s\": missing IncidentDataStructure",
591                                  sd->name);
592                  return SDEformat;
593          }
582        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
583        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
584                        "DataDefinition"), "IncidentDataStructure"));
594          if (!strcasecmp(txt, "Rows"))
595                  rowIn = 1;
596          else if (!strcasecmp(txt, "Columns"))
# Line 596 | Line 605 | SDloadMtx(SDData *sd, ezxml_t fl)
605          rval = load_angle_basis(ezxml_child(ezxml_child(wtl,
606                                  "DataDefinition"), "AngleBasis"));
607          if (rval < 0)
608 <                goto err_return;
608 >                return convert_errcode(rval);
609                                  /* load BSDF components */
610          for (wld = ezxml_child(wtl, "WavelengthData");
611                                  wld != NULL; wld = wld->next) {
# Line 606 | Line 615 | SDloadMtx(SDData *sd, ezxml_t fl)
615                  for (wdb = ezxml_child(wld, "WavelengthDataBlock");
616                                          wdb != NULL; wdb = wdb->next)
617                          if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0)
618 <                                goto err_return;
618 >                                return convert_errcode(rval);
619          }
620                                  /* separate diffuse components */
621          extract_diffuse(&sd->rLambFront, sd->rf);
# Line 614 | Line 623 | SDloadMtx(SDData *sd, ezxml_t fl)
623          extract_diffuse(&sd->tLamb, sd->tf);
624                                  /* return success */
625          return SDEnone;
617 err_return:                     /* jump here on failure */
618        if (sd->rf != NULL) {
619                SDfreeSpectralDF(sd->rf);
620                sd->rf = NULL;
621        }
622        if (sd->rb != NULL) {
623                SDfreeSpectralDF(sd->rb);
624                sd->rb = NULL;
625        }
626        if (sd->tf != NULL) {
627                SDfreeSpectralDF(sd->tf);
628                sd->tf = NULL;
629        }
630        return convert_errcode(rval);
626   }
627  
628   /* Get Matrix BSDF value */
# Line 656 | Line 651 | static SDError
651   SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist)
652   {
653          const SDMat     *dp = (const SDMat *)dist;
654 <
655 <        if (!(qflags & SDqueryInc+SDqueryOut))
654 >        double          inc_psa, out_psa;
655 >                                        /* check arguments */
656 >        if ((psa == NULL) | (vec == NULL) | (dp == NULL))
657                  return SDEargument;
658 <        if (qflags & SDqueryInc) {
659 <                double  inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
660 <                if (inc_psa < .0)
661 <                        return SDEinternal;
662 <                switch (qflags & SDqueryMin+SDqueryMax) {
663 <                case SDqueryMax:
664 <                        if (inc_psa > psa[0])
665 <                                psa[0] = inc_psa;
666 <                        break;
667 <                case SDqueryMin+SDqueryMax:
672 <                        if (inc_psa > psa[1])
673 <                                psa[1] = inc_psa;
674 <                        /* fall through */
675 <                case SDqueryMin:
676 <                        if (inc_psa < psa[0])
677 <                                psa[0] = inc_psa;
678 <                        break;
679 <                case 0:
658 >                                        /* get projected solid angles */
659 >        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
660 >        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec));
661 >
662 >        switch (qflags) {               /* record based on flag settings */
663 >        case SDqueryVal:
664 >                psa[0] = .0;
665 >                /* fall through */
666 >        case SDqueryMax:
667 >                if (inc_psa > psa[0])
668                          psa[0] = inc_psa;
669 <                        break;
670 <                }
669 >                if (out_psa > psa[0])
670 >                        psa[0] = out_psa;
671 >                break;
672 >        case SDqueryMin+SDqueryMax:
673 >                if (inc_psa > psa[0])
674 >                        psa[1] = inc_psa;
675 >                if (out_psa > psa[0])
676 >                        psa[1] = out_psa;
677 >                /* fall through */
678 >        case SDqueryMin:
679 >                if ((inc_psa > .0) & (inc_psa < psa[0]))
680 >                        psa[0] = inc_psa;
681 >                if ((out_psa > .0) & (out_psa < psa[0]))
682 >                        psa[0] = out_psa;
683 >                break;
684          }
685 <        if (qflags & SDqueryOut) {
686 <                double  out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec));
686 <                if (out_psa < .0)
687 <                        return SDEinternal;
688 <                switch (qflags & SDqueryMin+SDqueryMax) {
689 <                case SDqueryMax:
690 <                        if (out_psa > psa[0])
691 <                                psa[0] = out_psa;
692 <                        break;
693 <                case SDqueryMin+SDqueryMax:
694 <                        if (out_psa > psa[1])
695 <                                psa[1] = out_psa;
696 <                        /* fall through */
697 <                case SDqueryMin:
698 <                        if (out_psa < psa[0])
699 <                                psa[0] = out_psa;
700 <                        break;
701 <                case 0:
702 <                        psa[(qflags&SDqueryInc)!=0] = out_psa;
703 <                        break;
704 <                }
705 <        }
706 <        return SDEnone;
685 >                                        /* make sure it's legal */
686 >        return (psa[0] <= .0) ? SDEinternal : SDEnone;
687   }
688  
689   /* Compute new cumulative distribution from BSDF */
# Line 745 | Line 725 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
725          int             reverse;
726          SDMatCDst       myCD;
727          SDMatCDst       *cd, *cdlast;
728 <
729 <        if (dp == NULL)
728 >                                        /* check arguments */
729 >        if ((inVec == NULL) | (dp == NULL))
730                  return NULL;
731          memset(&myCD, 0, sizeof(myCD));
732          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 801 | Line 781 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
781          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
782          const unsigned  target = randX*maxval;
783          int             i, iupper, ilower;
784 +                                        /* check arguments */
785 +        if ((outVec == NULL) | (mcd == NULL))
786 +                return SDEargument;
787                                          /* binary search to find index */
788          ilower = 0; iupper = mcd->calen;
789          while ((i = (iupper + ilower) >> 1) != ilower)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines