ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bsdf.c
(Generate patch)

Comparing ray/src/common/bsdf.c (file contents):
Revision 2.23 by greg, Wed Apr 20 14:44:05 2011 UTC vs.
Revision 2.37 by greg, Sun Mar 4 20:11:10 2012 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   *
11   */
12  
13 + #define _USE_MATH_DEFINES
14   #include <stdio.h>
15   #include <stdlib.h>
16 + #include <string.h>
17   #include <math.h>
18 + #include <ctype.h>
19   #include "ezxml.h"
20   #include "hilbert.h"
21   #include "bsdf.h"
# Line 106 | Line 109 | SDloadGeometry(SDData *sd, ezxml_t wdb)
109          if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
110                          (mgfstr = ezxml_txt(geom)) == NULL)
111                  return SDEnone;
112 +        while (isspace(*mgfstr))
113 +                ++mgfstr;
114 +        if (!*mgfstr)
115 +                return SDEnone;
116          if ((fmt = ezxml_attr(geom, "format")) != NULL &&
117                          strcasecmp(fmt, "MGF")) {
118                  sprintf(SDerrorDetail,
# Line 164 | Line 171 | SDloadFile(SDData *sd, const char *fname)
171          }
172                                  /* load geometry if present */
173          lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material"));
174 <        if (lastErr)
174 >        if (lastErr) {
175 >                ezxml_free(fl);
176                  return lastErr;
177 +        }
178                                  /* try loading variable resolution data */
179          lastErr = SDloadTre(sd, wtl);
180                                  /* check our result */
181 <        switch (lastErr) {
173 <        case SDEformat:
174 <        case SDEdata:
175 <        case SDEsupport:        /* possibly we just tried the wrong format */
181 >        if (lastErr == SDEsupport)      /* try matrix BSDF if not tree data */
182                  lastErr = SDloadMtx(sd, wtl);
183 <                break;
178 <        default:                /* variable res. OK else serious error */
179 <                break;
180 <        }
183 >                
184                                  /* done with XML file */
185          ezxml_free(fl);
186          
# Line 223 | Line 226 | SDnewSpectralDF(int nc)
226          return df;
227   }
228  
229 + /* Add component(s) to spectral distribution function */
230 + SDSpectralDF *
231 + SDaddComponent(SDSpectralDF *odf, int nadd)
232 + {
233 +        SDSpectralDF    *df;
234 +
235 +        if (odf == NULL)
236 +                return SDnewSpectralDF(nadd);
237 +        if (nadd <= 0)
238 +                return odf;
239 +        df = (SDSpectralDF *)realloc(odf, sizeof(SDSpectralDF) +
240 +                                (odf->ncomp+nadd-1)*sizeof(SDComponent));
241 +        if (df == NULL) {
242 +                sprintf(SDerrorDetail,
243 +                        "Cannot add %d component(s) to spectral DF", nadd);
244 +                SDfreeSpectralDF(odf);
245 +                return NULL;
246 +        }
247 +        memset(df->comp+df->ncomp, 0, nadd*sizeof(SDComponent));
248 +        df->ncomp += nadd;
249 +        return df;
250 + }
251 +
252   /* Free cached cumulative distributions for BSDF component */
253   void
254   SDfreeCumulativeCache(SDSpectralDF *df)
# Line 249 | Line 275 | SDfreeSpectralDF(SDSpectralDF *df)
275                  return;
276          SDfreeCumulativeCache(df);
277          for (n = df->ncomp; n-- > 0; )
278 <                (*df->comp[n].func->freeSC)(df->comp[n].dist);
278 >                if (df->comp[n].dist != NULL)
279 >                        (*df->comp[n].func->freeSC)(df->comp[n].dist);
280          free(df);
281   }
282  
# Line 405 | Line 432 | SDfreeCache(const SDData *sd)
432  
433   /* Sample an individual BSDF component */
434   SDError
435 < SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
409 <                        double randX, SDComponent *sdc)
435 > SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc)
436   {
437          float           coef[SDmaxCh];
438          SDError         ec;
439 +        FVECT           inVec;
440          const SDCDst    *cd;
441          double          d;
442          int             n;
443                                          /* check arguments */
444 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL))
444 >        if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL))
445                  return SDEargument;
446                                          /* get cumulative distribution */
447 +        VCOPY(inVec, ioVec);
448          cd = (*sdc->func->getCDist)(inVec, sdc);
449          if (cd == NULL)
450                  return SDEmemory;
451 <        if (cd->cTotal <= 1e-7) {       /* anything to sample? */
451 >        if (cd->cTotal <= 1e-6) {       /* anything to sample? */
452                  sv->spec = c_dfcolor;
453                  sv->cieY = .0;
454 <                memset(outVec, 0, 3*sizeof(double));
454 >                memset(ioVec, 0, 3*sizeof(double));
455                  return SDEnone;
456          }
457          sv->cieY = cd->cTotal;
458                                          /* compute sample direction */
459 <        ec = (*sdc->func->sampCDist)(outVec, randX, cd);
459 >        ec = (*sdc->func->sampCDist)(ioVec, randX, cd);
460          if (ec)
461                  return ec;
462                                          /* get BSDF color */
463 <        n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
463 >        n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
464          if (n <= 0) {
465                  strcpy(SDerrorDetail, "BSDF sample value error");
466                  return SDEinternal;
# Line 495 | Line 523 | SDsizeBSDF(double *projSA, const FVECT v1, const RREAL
523          SDError         ec;
524          int             i;
525                                          /* check arguments */
526 <        if ((projSA == NULL) | (v1 == NULL))
526 >        if ((projSA == NULL) | (v1 == NULL) | (sd == NULL))
527                  return SDEargument;
528                                          /* initialize extrema */
529          switch (qflags) {
# Line 515 | Line 543 | SDsizeBSDF(double *projSA, const FVECT v1, const RREAL
543                  rdf = sd->rf;
544          else
545                  rdf = sd->rb;
546 <        tdf = NULL;                     /* transmitted component? */
547 <        if (v2 != NULL && v1[2] > 0 ^ v2[2] > 0) {
548 <                rdf = NULL;
549 <                tdf = sd->tf;
550 <        }
546 >        tdf = sd->tf;
547 >        if (v2 != NULL)                 /* bidirectional? */
548 >                if (v1[2] > 0 ^ v2[2] > 0)
549 >                        rdf = NULL;
550 >                else
551 >                        tdf = NULL;
552          ec = SDEdata;                   /* run through components */
553          for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
554                  ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
555 <                                                qflags, rdf->comp[i].dist);
555 >                                                qflags, &rdf->comp[i]);
556                  if (ec)
557                          return ec;
558          }
559          for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
560                  ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
561 <                                                qflags, tdf->comp[i].dist);
561 >                                                qflags, &tdf->comp[i]);
562                  if (ec)
563                          return ec;
564          }
# Line 571 | Line 600 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
600          i = (sdf != NULL) ? sdf->ncomp : 0;
601          while (i-- > 0) {
602                  nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
603 <                                                        sdf->comp[i].dist);
603 >                                                        &sdf->comp[i]);
604                  while (nch-- > 0) {
605                          c_cmix(&sv->spec, sv->cieY, &sv->spec,
606                                          coef[nch], &sdf->comp[i].cspec[nch]);
# Line 626 | Line 655 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
655  
656   /* Sample BSDF direction based on the given random variable */
657   SDError
658 < SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
630 <                        double randX, int sflags, const SDData *sd)
658 > SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
659   {
660          SDError         ec;
661 +        FVECT           inVec;
662          int             inFront;
663          SDSpectralDF    *rdf;
664          double          rdiff;
# Line 638 | Line 667 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
667          SDComponent     *sdc;
668          const SDCDst    **cdarr = NULL;
669                                          /* check arguments */
670 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
670 >        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
671                          (randX < 0) | (randX >= 1.))
672                  return SDEargument;
673                                          /* whose side are we on? */
674 +        VCOPY(inVec, ioVec);
675          inFront = (inVec[2] > 0);
676                                          /* remember diffuse portions */
677          if (inFront) {
# Line 680 | Line 710 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
710                  }
711                  sv->cieY += cdarr[i]->cTotal;
712          }
713 <        if (sv->cieY <= 1e-7) {         /* anything to sample? */
713 >        if (sv->cieY <= 1e-6) {         /* anything to sample? */
714                  sv->cieY = .0;
715 <                memset(outVec, 0, 3*sizeof(double));
715 >                memset(ioVec, 0, 3*sizeof(double));
716                  return SDEnone;
717          }
718                                          /* scale random variable */
719          randX *= sv->cieY;
720                                          /* diffuse reflection? */
721          if (randX < rdiff) {
722 <                SDdiffuseSamp(outVec, inFront, randX/rdiff);
722 >                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
723                  goto done;
724          }
725          randX -= rdiff;
# Line 697 | Line 727 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
727          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
728                  if (randX < sd->tLamb.cieY) {
729                          sv->spec = sd->tLamb.spec;
730 <                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
730 >                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
731                          goto done;
732                  }
733                  randX -= sd->tLamb.cieY;
# Line 709 | Line 739 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
739                  return SDEinternal;
740                                          /* compute sample direction */
741          sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
742 <        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
742 >        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
743          if (ec)
744                  return ec;
745                                          /* compute color */
746 <        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
746 >        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
747          if (j <= 0) {
748                  sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
749                                  sd->name);
# Line 810 | Line 840 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
840  
841   #include "standard.h"
842   #include "paths.h"
813 #include <ctype.h>
843  
844   #define MAXLATS         46              /* maximum number of latitudes */
845  
# Line 930 | Line 959 | ab_getndx(             /* get index corresponding to the given ve
959   {
960          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
961          int     li, ndx;
962 <        double  pol, azi, d;
962 >        double  pol, azi;
963  
964          if ((v[2] < -1.0) | (v[2] > 1.0))
965                  return(-1);
# Line 1170 | Line 1199 | check_bsdf_data(       /* check that BSDF data is sane */
1199   )
1200   {
1201          double          *omega_iarr, *omega_oarr;
1202 <        double          dom, contrib, hemi_total, full_total;
1202 >        double          dom, hemi_total, full_total;
1203          int             nneg;
1204          FVECT           v;
1205          int             i, o;
# Line 1320 | Line 1349 | load_BSDF(             /* load BSDF data from file */
1349                  error(WARNING, errmsg);
1350                  ezxml_free(fl);
1351                  return(NULL);
1352 <        }              
1353 <        load_angle_basis(ezxml_child(ezxml_child(wtl,
1354 <                                "DataDefinition"), "AngleBasis"));
1352 >        }
1353 >        for (wld = ezxml_child(ezxml_child(wtl,
1354 >                                "DataDefinition"), "AngleBasis");
1355 >                        wld != NULL; wld = wld->next)
1356 >                load_angle_basis(wld);
1357          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
1358          load_geometry(dp, ezxml_child(wtl, "Material"));
1359          for (wld = ezxml_child(wtl, "WavelengthData");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines