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.15 by greg, Fri Feb 18 00:40:25 2011 UTC vs.
Revision 2.41 by greg, Sat Jun 23 16:47:39 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 45 | Line 48 | int                    SDretainSet = SDretainNone;
48   SDError
49   SDreportEnglish(SDError ec, FILE *fp)
50   {
48        if (fp == NULL)
49                return ec;
51          if (!ec)
52                  return SDEnone;
53 +        if ((ec < SDEnone) | (ec > SDEunknown)) {
54 +                SDerrorDetail[0] = '\0';
55 +                ec = SDEunknown;
56 +        }
57 +        if (fp == NULL)
58 +                return ec;
59          fputs(SDerrorEnglish[ec], fp);
60          if (SDerrorDetail[0]) {
61                  fputs(": ", fp);
# Line 77 | Line 84 | to_meters(             /* return factor to convert given unit to
84  
85   /* Load geometric dimensions and description (if any) */
86   static SDError
87 < SDloadGeometry(SDData *dp, ezxml_t wdb)
87 > SDloadGeometry(SDData *sd, ezxml_t wdb)
88   {
89          ezxml_t         geom;
90          double          cfact;
91          const char      *fmt, *mgfstr;
92  
93 <        sprintf(SDerrorDetail, "Negative size in \"%s\"", dp->name);
94 <        dp->dim[0] = dp->dim[1] = dp->dim[2] = .0;
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 <                dp->dim[0] = atof(ezxml_txt(geom)) *
108 >                sd->dim[0] = atof(ezxml_txt(geom)) *
109                                  to_meters(ezxml_attr(geom, "unit"));
110          if ((geom = ezxml_child(wdb, "Height")) != NULL)
111 <                dp->dim[1] = atof(ezxml_txt(geom)) *
111 >                sd->dim[1] = atof(ezxml_txt(geom)) *
112                                  to_meters(ezxml_attr(geom, "unit"));
113          if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
114 <                dp->dim[2] = atof(ezxml_txt(geom)) *
114 >                sd->dim[2] = atof(ezxml_txt(geom)) *
115                                  to_meters(ezxml_attr(geom, "unit"));
116 <        if ((dp->dim[0] < .0) | (dp->dim[1] < .0) | (dp->dim[2] < .0))
116 >        if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) {
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,
132                          "Unrecognized geometry format '%s' in \"%s\"",
133 <                                        fmt, dp->name);
133 >                                        fmt, sd->name);
134                  return SDEsupport;
135          }
136          cfact = to_meters(ezxml_attr(geom, "unit"));
137 <        dp->mgf = (char *)malloc(strlen(mgfstr)+32);
138 <        if (dp->mgf == NULL) {
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");
142                  return SDEmemory;
143          }
144          if (cfact < 0.99 || cfact > 1.01)
145 <                sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
145 >                sprintf(sd->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
146          else
147 <                strcpy(dp->mgf, mgfstr);
147 >                strcpy(sd->mgf, mgfstr);
148          return SDEnone;
149   }
150  
122
151   /* Load a BSDF struct from the given file (free first and keep name) */
152   SDError
153   SDloadFile(SDData *sd, const char *fname)
154   {
155          SDError         lastErr;
156 <        ezxml_t         fl;
156 >        ezxml_t         fl, wtl;
157  
158          if ((sd == NULL) | (fname == NULL || !*fname))
159                  return SDEargument;
# Line 142 | Line 170 | SDloadFile(SDData *sd, const char *fname)
170                  ezxml_free(fl);
171                  return SDEformat;
172          }
173 +        if (strcmp(ezxml_name(fl), "WindowElement")) {
174 +                sprintf(SDerrorDetail,
175 +                        "BSDF \"%s\": top level node not 'WindowElement'",
176 +                                sd->name);
177 +                ezxml_free(fl);
178 +                return SDEformat;
179 +        }
180 +        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
181 +        if (wtl == NULL) {
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 <        if ((lastErr = SDloadGeometry(sd, fl)))
188 >        lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material"));
189 >        if (lastErr) {
190 >                ezxml_free(fl);
191                  return lastErr;
192 +        }
193                                  /* try loading variable resolution data */
194 <        lastErr = SDloadTre(sd, fl);
194 >        lastErr = SDloadTre(sd, wtl);
195                                  /* check our result */
196 <        switch (lastErr) {
197 <        case SDEformat:
198 <        case SDEdata:
154 <        case SDEsupport:        /* possibly we just tried the wrong format */
155 <                lastErr = SDloadMtx(sd, fl);
156 <                break;
157 <        default:                /* variable res. OK else serious error */
158 <                break;
159 <        }
196 >        if (lastErr == SDEsupport)      /* try matrix BSDF if not tree data */
197 >                lastErr = SDloadMtx(sd, wtl);
198 >                
199                                  /* done with XML file */
200          ezxml_free(fl);
201 <                                /* return success or failure */
202 <        return lastErr;
201 >        
202 >        if (lastErr) {          /* was there a load error? */
203 >                SDfreeBSDF(sd);
204 >                return lastErr;
205 >        }
206 >                                /* remove any insignificant components */
207 >        if (sd->rf != NULL && sd->rf->maxHemi <= .001) {
208 >                SDfreeSpectralDF(sd->rf); sd->rf = NULL;
209 >        }
210 >        if (sd->rb != NULL && sd->rb->maxHemi <= .001) {
211 >                SDfreeSpectralDF(sd->rb); sd->rb = NULL;
212 >        }
213 >        if (sd->tf != NULL && sd->tf->maxHemi <= .001) {
214 >                SDfreeSpectralDF(sd->tf); sd->tf = NULL;
215 >        }
216 >                                /* return success */
217 >        return SDEnone;
218   }
219  
220   /* Allocate new spectral distribution function */
# Line 187 | 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 213 | 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  
298   /* Shorten file path to useable BSDF name, removing suffix */
299   void
300 < SDclipName(char res[SDnameLn], const char *fname)
300 > SDclipName(char *res, const char *fname)
301   {
302          const char      *cp, *dot = NULL;
303          
# Line 237 | Line 315 | SDclipName(char res[SDnameLn], const char *fname)
315  
316   /* Initialize an unused BSDF struct (simply clears to zeroes) */
317   void
318 < SDclearBSDF(SDData *sd)
318 > SDclearBSDF(SDData *sd, const char *fname)
319   {
320 <        if (sd != NULL)
321 <                memset(sd, 0, sizeof(SDData));
320 >        if (sd == NULL)
321 >                return;
322 >        memset(sd, 0, sizeof(SDData));
323 >        if (fname == NULL)
324 >                return;
325 >        SDclipName(sd->name, fname);
326   }
327  
328   /* Free data associated with BSDF struct */
# Line 266 | Line 348 | SDfreeBSDF(SDData *sd)
348                  sd->tf = NULL;
349          }
350          sd->rLambFront.cieY = .0;
351 <        sd->rLambFront.spec.clock = 0;
351 >        sd->rLambFront.spec.flags = 0;
352          sd->rLambBack.cieY = .0;
353 <        sd->rLambBack.spec.clock = 0;
353 >        sd->rLambBack.spec.flags = 0;
354          sd->tLamb.cieY = .0;
355 <        sd->tLamb.spec.clock = 0;
355 >        sd->tLamb.spec.flags = 0;
356   }
357  
358   /* Find writeable BSDF by name, or allocate new cache entry if absent */
# Line 298 | Line 380 | SDgetCache(const char *bname)
380          sdl->next = SDcacheList;
381          SDcacheList = sdl;
382  
383 <        sdl->refcnt++;
383 >        sdl->refcnt = 1;
384          return &sdl->bsdf;
385   }
386  
# Line 342 | Line 424 | SDfreeCache(const SDData *sd)
424          for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next)
425                  if (&sdl->bsdf == sd)
426                          break;
427 <        if (sdl == NULL || --sdl->refcnt)
427 >        if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0)))
428                  return;                 /* missing or still in use */
429                                          /* keep unreferenced data? */
430          if (SDisLoaded(sd) && SDretainSet) {
# Line 365 | Line 447 | SDfreeCache(const SDData *sd)
447  
448   /* Sample an individual BSDF component */
449   SDError
450 < SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
369 <                        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 417 | Line 500 | SDmultiSamp(double t[], int n, double randX)
500          unsigned        nBits;
501          double          scale;
502          bitmask_t       ndx, coord[MS_MAXDIM];
503 <        
503 >
504 >        if (n <= 0)                     /* check corner cases */
505 >                return;
506 >        if (randX < 0) randX = 0;
507 >        else if (randX >= 1.) randX = 0.999999999999999;
508 >        if (n == 1) {
509 >                t[0] = randX;
510 >                return;
511 >        }
512          while (n > MS_MAXDIM)           /* punt for higher dimensions */
513 <                t[--n] = drand48();
513 >                t[--n] = rand()*(1./(RAND_MAX+.5));
514          nBits = (8*sizeof(bitmask_t) - 1) / n;
515          ndx = randX * (double)((bitmask_t)1 << (nBits*n));
516                                          /* get coordinate on Hilbert curve */
# Line 427 | Line 518 | SDmultiSamp(double t[], int n, double randX)
518                                          /* convert back to [0,1) range */
519          scale = 1. / (double)((bitmask_t)1 << nBits);
520          while (n--)
521 <                t[n] = scale * ((double)coord[n] + drand48());
521 >                t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5)));
522   }
523  
524   #undef MS_MAXDIM
# Line 440 | Line 531 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
531          SDmultiSamp(outVec, 2, randX);
532          SDsquare2disk(outVec, outVec[0], outVec[1]);
533          outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1];
534 <        if (outVec[2] > .0)             /* a bit of paranoia */
534 >        if (outVec[2] > 0)              /* a bit of paranoia */
535                  outVec[2] = sqrt(outVec[2]);
536          if (!outFront)                  /* going out back? */
537                  outVec[2] = -outVec[2];
# Line 448 | Line 539 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
539  
540   /* Query projected solid angle coverage for non-diffuse BSDF direction */
541   SDError
542 < SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd)
542 > SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2,
543 >                                int qflags, const SDData *sd)
544   {
545 <        SDSpectralDF    *rdf;
545 >        SDSpectralDF    *rdf, *tdf;
546          SDError         ec;
547          int             i;
548                                          /* check arguments */
549 <        if ((projSA == NULL) | (vec == NULL) | (sd == NULL))
549 >        if ((projSA == NULL) | (v1 == NULL) | (sd == NULL))
550                  return SDEargument;
551                                          /* initialize extrema */
552 <        switch (qflags & SDqueryMin+SDqueryMax) {
552 >        switch (qflags) {
553          case SDqueryMax:
554                  projSA[0] = .0;
555                  break;
# Line 470 | Line 562 | SDsizeBSDF(double *projSA, const FVECT vec, int qflags
562          case 0:
563                  return SDEargument;
564          }
565 <        if (vec[2] > .0)                /* front surface query? */
565 >        if (v1[2] > 0)                  /* front surface query? */
566                  rdf = sd->rf;
567          else
568                  rdf = sd->rb;
569 +        tdf = sd->tf;
570 +        if (v2 != NULL)                 /* bidirectional? */
571 +                if (v1[2] > 0 ^ v2[2] > 0)
572 +                        rdf = NULL;
573 +                else
574 +                        tdf = NULL;
575          ec = SDEdata;                   /* run through components */
576          for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
577 <                ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags,
578 <                                                        rdf->comp[i].dist);
577 >                ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
578 >                                                qflags, &rdf->comp[i]);
579                  if (ec)
580                          return ec;
581          }
582 <        for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) {
583 <                ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags,
584 <                                                        sd->tf->comp[i].dist);
582 >        for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
583 >                ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
584 >                                                qflags, &tdf->comp[i]);
585                  if (ec)
586                          return ec;
587          }
588 <        return ec;
588 >        if (ec) {                       /* all diffuse? */
589 >                projSA[0] = M_PI;
590 >                if (qflags == SDqueryMin+SDqueryMax)
591 >                        projSA[1] = M_PI;
592 >        }
593 >        return SDEnone;
594   }
595  
596   /* Return BSDF for the given incident and scattered ray vectors */
# Line 502 | Line 605 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
605          if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL))
606                  return SDEargument;
607                                          /* whose side are we on? */
608 <        inFront = (inVec[2] > .0);
609 <        outFront = (outVec[2] > .0);
608 >        inFront = (inVec[2] > 0);
609 >        outFront = (outVec[2] > 0);
610                                          /* start with diffuse portion */
611          if (inFront & outFront) {
612                  *sv = sd->rLambFront;
# Line 520 | Line 623 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
623          i = (sdf != NULL) ? sdf->ncomp : 0;
624          while (i-- > 0) {
625                  nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
626 <                                                        sdf->comp[i].dist);
626 >                                                        &sdf->comp[i]);
627                  while (nch-- > 0) {
628                          c_cmix(&sv->spec, sv->cieY, &sv->spec,
629                                          coef[nch], &sdf->comp[i].cspec[nch]);
# Line 544 | Line 647 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
647          if ((inVec == NULL) | (sd == NULL))
648                  return .0;
649                                          /* gather diffuse components */
650 <        if (inVec[2] > .0) {
650 >        if (inVec[2] > 0) {
651                  hsum = sd->rLambFront.cieY;
652                  rdf = sd->rf;
653          } else /* !inFront */ {
# Line 575 | Line 678 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
678  
679   /* Sample BSDF direction based on the given random variable */
680   SDError
681 < SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
579 <                        double randX, int sflags, const SDData *sd)
681 > SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
682   {
683          SDError         ec;
684 +        FVECT           inVec;
685          int             inFront;
686          SDSpectralDF    *rdf;
687          double          rdiff;
# Line 587 | Line 690 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
690          SDComponent     *sdc;
691          const SDCDst    **cdarr = NULL;
692                                          /* check arguments */
693 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
694 <                        (randX < .0) | (randX >= 1.))
693 >        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
694 >                        (randX < 0) | (randX >= 1.))
695                  return SDEargument;
696                                          /* whose side are we on? */
697 <        inFront = (inVec[2] > .0);
697 >        VCOPY(inVec, ioVec);
698 >        inFront = (inVec[2] > 0);
699                                          /* remember diffuse portions */
700          if (inFront) {
701                  *sv = sd->rLambFront;
# Line 629 | Line 733 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
733                  }
734                  sv->cieY += cdarr[i]->cTotal;
735          }
736 <        if (sv->cieY <= 1e-7) {         /* anything to sample? */
736 >        if (sv->cieY <= 1e-6) {         /* anything to sample? */
737                  sv->cieY = .0;
738 <                memset(outVec, 0, 3*sizeof(double));
738 >                memset(ioVec, 0, 3*sizeof(double));
739                  return SDEnone;
740          }
741                                          /* scale random variable */
742          randX *= sv->cieY;
743                                          /* diffuse reflection? */
744          if (randX < rdiff) {
745 <                SDdiffuseSamp(outVec, inFront, randX/rdiff);
745 >                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
746                  goto done;
747          }
748          randX -= rdiff;
# Line 646 | Line 750 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
750          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
751                  if (randX < sd->tLamb.cieY) {
752                          sv->spec = sd->tLamb.spec;
753 <                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
753 >                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
754                          goto done;
755                  }
756                  randX -= sd->tLamb.cieY;
# Line 658 | Line 762 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
762                  return SDEinternal;
763                                          /* compute sample direction */
764          sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
765 <        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
765 >        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
766          if (ec)
767                  return ec;
768                                          /* compute color */
769 <        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
769 >        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
770          if (j <= 0) {
771                  sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
772                                  sd->name);
# Line 689 | Line 793 | SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const
793          if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL))
794                  return SDEargument;
795          VCOPY(vMtx[2], sNrm);
796 <        if (normalize(vMtx[2]) == .0)
796 >        if (normalize(vMtx[2]) == 0)
797                  return SDEargument;
798          fcross(vMtx[0], uVec, vMtx[2]);
799 <        if (normalize(vMtx[0]) == .0)
799 >        if (normalize(vMtx[0]) == 0)
800                  return SDEargument;
801          fcross(vMtx[1], vMtx[2], vMtx[0]);
802          return SDEnone;
# Line 712 | Line 816 | SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3])
816          mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1];
817          mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2];
818          d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2];
819 <        if (d == .0) {
819 >        if (d == 0) {
820                  strcpy(SDerrorDetail, "Zero determinant in matrix inversion");
821                  return SDEargument;
822          }
# Line 739 | Line 843 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
843          if (vMtx == NULL) {             /* assume they just want to normalize */
844                  if (resVec != inpVec)
845                          VCOPY(resVec, inpVec);
846 <                return (normalize(resVec) > .0) ? SDEnone : SDEargument;
846 >                return (normalize(resVec) > 0) ? SDEnone : SDEargument;
847          }
848          vTmp[0] = DOT(vMtx[0], inpVec);
849          vTmp[1] = DOT(vMtx[1], inpVec);
850          vTmp[2] = DOT(vMtx[2], inpVec);
851 <        if (normalize(vTmp) == .0)
851 >        if (normalize(vTmp) == 0)
852                  return SDEargument;
853          VCOPY(resVec, vTmp);
854          return SDEnone;
# Line 759 | Line 863 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
863  
864   #include "standard.h"
865   #include "paths.h"
762 #include <ctype.h>
866  
867   #define MAXLATS         46              /* maximum number of latitudes */
868  
# Line 816 | Line 919 | static int     nabases = 3;    /* current number of defined b
919   static int
920   fequal(double a, double b)
921   {
922 <        if (b != .0)
922 >        if (b != 0)
923                  a = a/b - 1.;
924          return((a <= 1e-6) & (a >= -1e-6));
925   }
# Line 879 | Line 982 | ab_getndx(             /* get index corresponding to the given ve
982   {
983          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
984          int     li, ndx;
985 <        double  pol, azi, d;
985 >        double  pol, azi;
986  
987          if ((v[2] < -1.0) | (v[2] > 1.0))
988                  return(-1);
# Line 1077 | Line 1180 | load_bsdf_data(                /* load BSDF distribution for this wa
1180                          break;
1181                  }
1182          if (i < 0) {
1183 <                sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis);
1183 >                sprintf(errmsg, "undefined RowAngleBasis '%s'", rbasis);
1184                  error(WARNING, errmsg);
1185                  return;
1186          }
# Line 1119 | Line 1222 | check_bsdf_data(       /* check that BSDF data is sane */
1222   )
1223   {
1224          double          *omega_iarr, *omega_oarr;
1225 <        double          dom, contrib, hemi_total, full_total;
1225 >        double          dom, hemi_total, full_total;
1226          int             nneg;
1227          FVECT           v;
1228          int             i, o;
# Line 1134 | Line 1237 | check_bsdf_data(       /* check that BSDF data is sane */
1237          hemi_total = .0;
1238          for (i = dp->ninc; i--; ) {
1239                  dom = getBSDF_incohm(dp,i);
1240 <                if (dom <= .0) {
1240 >                if (dom <= 0) {
1241                          error(WARNING, "zero/negative incoming solid angle");
1242                          continue;
1243                  }
# Line 1157 | Line 1260 | check_bsdf_data(       /* check that BSDF data is sane */
1260          hemi_total = .0;
1261          for (o = dp->nout; o--; ) {
1262                  dom = getBSDF_outohm(dp,o);
1263 <                if (dom <= .0) {
1263 >                if (dom <= 0) {
1264                          error(WARNING, "zero/negative outgoing solid angle");
1265                          continue;
1266                  }
# Line 1181 | Line 1284 | check_bsdf_data(       /* check that BSDF data is sane */
1284                  hemi_total = .0;
1285                  for (o = dp->nout; o--; ) {
1286                          double  f = BSDF_value(dp,i,o);
1287 <                        if (f >= .0)
1287 >                        if (f >= 0)
1288                                  hemi_total += f*omega_oarr[o];
1289                          else {
1290                                  nneg += (f < -FTINY);
# Line 1269 | Line 1372 | load_BSDF(             /* load BSDF data from file */
1372                  error(WARNING, errmsg);
1373                  ezxml_free(fl);
1374                  return(NULL);
1375 <        }              
1376 <        load_angle_basis(ezxml_child(ezxml_child(wtl,
1377 <                                "DataDefinition"), "AngleBasis"));
1375 >        }
1376 >        for (wld = ezxml_child(ezxml_child(wtl,
1377 >                                "DataDefinition"), "AngleBasis");
1378 >                        wld != NULL; wld = wld->next)
1379 >                load_angle_basis(wld);
1380          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
1381          load_geometry(dp, ezxml_child(wtl, "Material"));
1382          for (wld = ezxml_child(wtl, "WavelengthData");
# Line 1332 | Line 1437 | r_BSDF_incvec(         /* compute random input vector at give
1437          if (!getBSDF_incvec(v, b, i))
1438                  return(0);
1439          rad = sqrt(getBSDF_incohm(b, i) / PI);
1440 <        multisamp(pert, 3, rv);
1440 >        SDmultiSamp(pert, 3, rv);
1441          for (j = 0; j < 3; j++)
1442                  v[j] += rad*(2.*pert[j] - 1.);
1443          if (xm != NULL)
# Line 1357 | Line 1462 | r_BSDF_outvec(         /* compute random output vector at giv
1462          if (!getBSDF_outvec(v, b, o))
1463                  return(0);
1464          rad = sqrt(getBSDF_outohm(b, o) / PI);
1465 <        multisamp(pert, 3, rv);
1465 >        SDmultiSamp(pert, 3, rv);
1466          for (j = 0; j < 3; j++)
1467                  v[j] += rad*(2.*pert[j] - 1.);
1468          if (xm != NULL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines