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.40 by greg, Mon Mar 5 15:27:08 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 89 | Line 92 | SDloadGeometry(SDData *sd, ezxml_t wdb)
92  
93          if (wdb == NULL)                /* no geometry section? */
94                  return SDEnone;
95 +        if ((geom = ezxml_child(wdb, "Name")) != NULL) {
96 +                strncpy(sd->matn, ezxml_txt(geom), SDnameLn);
97 +                if (sd->matn[SDnameLn-1])
98 +                        strcpy(sd->matn+(SDnameLn-4), "...");
99 +        }
100 +        if ((geom = ezxml_child(wdb, "Manufacturer")) != NULL) {
101 +                strncpy(sd->makr, ezxml_txt(geom), SDnameLn);
102 +                if (sd->makr[SDnameLn-1])
103 +                        strcpy(sd->makr+(SDnameLn-4), "...");
104 +        }
105          sd->dim[0] = sd->dim[1] = sd->dim[2] = .0;
106 +        SDerrorDetail[0] = '\0';
107          if ((geom = ezxml_child(wdb, "Width")) != NULL)
108                  sd->dim[0] = atof(ezxml_txt(geom)) *
109                                  to_meters(ezxml_attr(geom, "unit"));
# Line 100 | Line 114 | SDloadGeometry(SDData *sd, ezxml_t wdb)
114                  sd->dim[2] = atof(ezxml_txt(geom)) *
115                                  to_meters(ezxml_attr(geom, "unit"));
116          if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) {
117 <                sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name);
117 >                if (!SDerrorDetail[0])
118 >                        sprintf(SDerrorDetail, "Negative dimension in \"%s\"",
119 >                                                sd->name);
120                  return SDEdata;
121          }
122          if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
123                          (mgfstr = ezxml_txt(geom)) == NULL)
124                  return SDEnone;
125 +        while (isspace(*mgfstr))
126 +                ++mgfstr;
127 +        if (!*mgfstr)
128 +                return SDEnone;
129          if ((fmt = ezxml_attr(geom, "format")) != NULL &&
130                          strcasecmp(fmt, "MGF")) {
131                  sprintf(SDerrorDetail,
# Line 114 | Line 134 | SDloadGeometry(SDData *sd, ezxml_t wdb)
134                  return SDEsupport;
135          }
136          cfact = to_meters(ezxml_attr(geom, "unit"));
137 +        if (cfact <= 0)
138 +                return SDEformat;
139          sd->mgf = (char *)malloc(strlen(mgfstr)+32);
140          if (sd->mgf == NULL) {
141                  strcpy(SDerrorDetail, "Out of memory in SDloadGeometry");
# Line 157 | Line 179 | SDloadFile(SDData *sd, const char *fname)
179          }
180          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
181          if (wtl == NULL) {
182 <                sprintf(SDerrorDetail, "BSDF \"%s\": no optical layer'",
182 >                sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'",
183                                  sd->name);
184                  ezxml_free(fl);
185                  return SDEformat;
186          }
187                                  /* load geometry if present */
188          lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material"));
189 <        if (lastErr)
189 >        if (lastErr) {
190 >                ezxml_free(fl);
191                  return lastErr;
192 +        }
193                                  /* try loading variable resolution data */
194          lastErr = SDloadTre(sd, wtl);
195                                  /* check our result */
196 <        switch (lastErr) {
173 <        case SDEformat:
174 <        case SDEdata:
175 <        case SDEsupport:        /* possibly we just tried the wrong format */
196 >        if (lastErr == SDEsupport)      /* try matrix BSDF if not tree data */
197                  lastErr = SDloadMtx(sd, wtl);
198 <                break;
178 <        default:                /* variable res. OK else serious error */
179 <                break;
180 <        }
198 >                
199                                  /* done with XML file */
200          ezxml_free(fl);
201          
# Line 223 | Line 241 | SDnewSpectralDF(int nc)
241          return df;
242   }
243  
244 + /* Add component(s) to spectral distribution function */
245 + SDSpectralDF *
246 + SDaddComponent(SDSpectralDF *odf, int nadd)
247 + {
248 +        SDSpectralDF    *df;
249 +
250 +        if (odf == NULL)
251 +                return SDnewSpectralDF(nadd);
252 +        if (nadd <= 0)
253 +                return odf;
254 +        df = (SDSpectralDF *)realloc(odf, sizeof(SDSpectralDF) +
255 +                                (odf->ncomp+nadd-1)*sizeof(SDComponent));
256 +        if (df == NULL) {
257 +                sprintf(SDerrorDetail,
258 +                        "Cannot add %d component(s) to spectral DF", nadd);
259 +                SDfreeSpectralDF(odf);
260 +                return NULL;
261 +        }
262 +        memset(df->comp+df->ncomp, 0, nadd*sizeof(SDComponent));
263 +        df->ncomp += nadd;
264 +        return df;
265 + }
266 +
267   /* Free cached cumulative distributions for BSDF component */
268   void
269   SDfreeCumulativeCache(SDSpectralDF *df)
# Line 249 | Line 290 | SDfreeSpectralDF(SDSpectralDF *df)
290                  return;
291          SDfreeCumulativeCache(df);
292          for (n = df->ncomp; n-- > 0; )
293 <                (*df->comp[n].func->freeSC)(df->comp[n].dist);
293 >                if (df->comp[n].dist != NULL)
294 >                        (*df->comp[n].func->freeSC)(df->comp[n].dist);
295          free(df);
296   }
297  
# Line 405 | Line 447 | SDfreeCache(const SDData *sd)
447  
448   /* Sample an individual BSDF component */
449   SDError
450 < SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
409 <                        double randX, SDComponent *sdc)
450 > SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc)
451   {
452          float           coef[SDmaxCh];
453          SDError         ec;
454 +        FVECT           inVec;
455          const SDCDst    *cd;
456          double          d;
457          int             n;
458                                          /* check arguments */
459 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL))
459 >        if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL))
460                  return SDEargument;
461                                          /* get cumulative distribution */
462 +        VCOPY(inVec, ioVec);
463          cd = (*sdc->func->getCDist)(inVec, sdc);
464          if (cd == NULL)
465                  return SDEmemory;
466 <        if (cd->cTotal <= 1e-7) {       /* anything to sample? */
466 >        if (cd->cTotal <= 1e-6) {       /* anything to sample? */
467                  sv->spec = c_dfcolor;
468                  sv->cieY = .0;
469 <                memset(outVec, 0, 3*sizeof(double));
469 >                memset(ioVec, 0, 3*sizeof(double));
470                  return SDEnone;
471          }
472          sv->cieY = cd->cTotal;
473                                          /* compute sample direction */
474 <        ec = (*sdc->func->sampCDist)(outVec, randX, cd);
474 >        ec = (*sdc->func->sampCDist)(ioVec, randX, cd);
475          if (ec)
476                  return ec;
477                                          /* get BSDF color */
478 <        n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
478 >        n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
479          if (n <= 0) {
480                  strcpy(SDerrorDetail, "BSDF sample value error");
481                  return SDEinternal;
# Line 495 | Line 538 | SDsizeBSDF(double *projSA, const FVECT v1, const RREAL
538          SDError         ec;
539          int             i;
540                                          /* check arguments */
541 <        if ((projSA == NULL) | (v1 == NULL))
541 >        if ((projSA == NULL) | (v1 == NULL) | (sd == NULL))
542                  return SDEargument;
543                                          /* initialize extrema */
544          switch (qflags) {
# Line 515 | Line 558 | SDsizeBSDF(double *projSA, const FVECT v1, const RREAL
558                  rdf = sd->rf;
559          else
560                  rdf = sd->rb;
561 <        tdf = NULL;                     /* transmitted component? */
562 <        if (v2 != NULL && v1[2] > 0 ^ v2[2] > 0) {
563 <                rdf = NULL;
564 <                tdf = sd->tf;
565 <        }
561 >        tdf = sd->tf;
562 >        if (v2 != NULL)                 /* bidirectional? */
563 >                if (v1[2] > 0 ^ v2[2] > 0)
564 >                        rdf = NULL;
565 >                else
566 >                        tdf = NULL;
567          ec = SDEdata;                   /* run through components */
568          for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
569                  ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
570 <                                                qflags, rdf->comp[i].dist);
570 >                                                qflags, &rdf->comp[i]);
571                  if (ec)
572                          return ec;
573          }
574          for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
575                  ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
576 <                                                qflags, tdf->comp[i].dist);
576 >                                                qflags, &tdf->comp[i]);
577                  if (ec)
578                          return ec;
579          }
# Line 571 | Line 615 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
615          i = (sdf != NULL) ? sdf->ncomp : 0;
616          while (i-- > 0) {
617                  nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
618 <                                                        sdf->comp[i].dist);
618 >                                                        &sdf->comp[i]);
619                  while (nch-- > 0) {
620                          c_cmix(&sv->spec, sv->cieY, &sv->spec,
621                                          coef[nch], &sdf->comp[i].cspec[nch]);
# Line 626 | Line 670 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
670  
671   /* Sample BSDF direction based on the given random variable */
672   SDError
673 < SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
630 <                        double randX, int sflags, const SDData *sd)
673 > SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
674   {
675          SDError         ec;
676 +        FVECT           inVec;
677          int             inFront;
678          SDSpectralDF    *rdf;
679          double          rdiff;
# Line 638 | Line 682 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
682          SDComponent     *sdc;
683          const SDCDst    **cdarr = NULL;
684                                          /* check arguments */
685 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
685 >        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
686                          (randX < 0) | (randX >= 1.))
687                  return SDEargument;
688                                          /* whose side are we on? */
689 +        VCOPY(inVec, ioVec);
690          inFront = (inVec[2] > 0);
691                                          /* remember diffuse portions */
692          if (inFront) {
# Line 680 | Line 725 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
725                  }
726                  sv->cieY += cdarr[i]->cTotal;
727          }
728 <        if (sv->cieY <= 1e-7) {         /* anything to sample? */
728 >        if (sv->cieY <= 1e-6) {         /* anything to sample? */
729                  sv->cieY = .0;
730 <                memset(outVec, 0, 3*sizeof(double));
730 >                memset(ioVec, 0, 3*sizeof(double));
731                  return SDEnone;
732          }
733                                          /* scale random variable */
734          randX *= sv->cieY;
735                                          /* diffuse reflection? */
736          if (randX < rdiff) {
737 <                SDdiffuseSamp(outVec, inFront, randX/rdiff);
737 >                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
738                  goto done;
739          }
740          randX -= rdiff;
# Line 697 | Line 742 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
742          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
743                  if (randX < sd->tLamb.cieY) {
744                          sv->spec = sd->tLamb.spec;
745 <                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
745 >                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
746                          goto done;
747                  }
748                  randX -= sd->tLamb.cieY;
# Line 709 | Line 754 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
754                  return SDEinternal;
755                                          /* compute sample direction */
756          sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
757 <        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
757 >        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
758          if (ec)
759                  return ec;
760                                          /* compute color */
761 <        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
761 >        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
762          if (j <= 0) {
763                  sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
764                                  sd->name);
# Line 810 | Line 855 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
855  
856   #include "standard.h"
857   #include "paths.h"
813 #include <ctype.h>
858  
859   #define MAXLATS         46              /* maximum number of latitudes */
860  
# Line 930 | Line 974 | ab_getndx(             /* get index corresponding to the given ve
974   {
975          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
976          int     li, ndx;
977 <        double  pol, azi, d;
977 >        double  pol, azi;
978  
979          if ((v[2] < -1.0) | (v[2] > 1.0))
980                  return(-1);
# Line 1170 | Line 1214 | check_bsdf_data(       /* check that BSDF data is sane */
1214   )
1215   {
1216          double          *omega_iarr, *omega_oarr;
1217 <        double          dom, contrib, hemi_total, full_total;
1217 >        double          dom, hemi_total, full_total;
1218          int             nneg;
1219          FVECT           v;
1220          int             i, o;
# Line 1320 | Line 1364 | load_BSDF(             /* load BSDF data from file */
1364                  error(WARNING, errmsg);
1365                  ezxml_free(fl);
1366                  return(NULL);
1367 <        }              
1368 <        load_angle_basis(ezxml_child(ezxml_child(wtl,
1369 <                                "DataDefinition"), "AngleBasis"));
1367 >        }
1368 >        for (wld = ezxml_child(ezxml_child(wtl,
1369 >                                "DataDefinition"), "AngleBasis");
1370 >                        wld != NULL; wld = wld->next)
1371 >                load_angle_basis(wld);
1372          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
1373          load_geometry(dp, ezxml_child(wtl, "Material"));
1374          for (wld = ezxml_child(wtl, "WavelengthData");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines