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.16 by greg, Sat Feb 19 01:48:59 2011 UTC vs.
Revision 2.42 by greg, Sun Sep 2 15:33:15 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 35 | Line 38 | const char             *SDerrorEnglish[] = {
38   /* Additional information on last error (ASCII English) */
39   char                    SDerrorDetail[256];
40  
41 + /* Empty distribution for getCDist() calls that fail for some reason */
42 + const SDCDst            SDemptyCD;
43 +
44   /* Cache of loaded BSDFs */
45   struct SDCache_s        *SDcacheList = NULL;
46  
# Line 45 | Line 51 | int                    SDretainSet = SDretainNone;
51   SDError
52   SDreportEnglish(SDError ec, FILE *fp)
53   {
48        if (fp == NULL)
49                return ec;
54          if (!ec)
55                  return SDEnone;
56 +        if ((ec < SDEnone) | (ec > SDEunknown)) {
57 +                SDerrorDetail[0] = '\0';
58 +                ec = SDEunknown;
59 +        }
60 +        if (fp == NULL)
61 +                return ec;
62          fputs(SDerrorEnglish[ec], fp);
63          if (SDerrorDetail[0]) {
64                  fputs(": ", fp);
# Line 85 | Line 95 | SDloadGeometry(SDData *sd, ezxml_t wdb)
95  
96          if (wdb == NULL)                /* no geometry section? */
97                  return SDEnone;
98 <        sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name);
98 >        if ((geom = ezxml_child(wdb, "Name")) != NULL) {
99 >                strncpy(sd->matn, ezxml_txt(geom), SDnameLn);
100 >                if (sd->matn[SDnameLn-1])
101 >                        strcpy(sd->matn+(SDnameLn-4), "...");
102 >        }
103 >        if ((geom = ezxml_child(wdb, "Manufacturer")) != NULL) {
104 >                strncpy(sd->makr, ezxml_txt(geom), SDnameLn);
105 >                if (sd->makr[SDnameLn-1])
106 >                        strcpy(sd->makr+(SDnameLn-4), "...");
107 >        }
108          sd->dim[0] = sd->dim[1] = sd->dim[2] = .0;
109 +        SDerrorDetail[0] = '\0';
110          if ((geom = ezxml_child(wdb, "Width")) != NULL)
111                  sd->dim[0] = atof(ezxml_txt(geom)) *
112                                  to_meters(ezxml_attr(geom, "unit"));
# Line 96 | Line 116 | SDloadGeometry(SDData *sd, ezxml_t wdb)
116          if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
117                  sd->dim[2] = atof(ezxml_txt(geom)) *
118                                  to_meters(ezxml_attr(geom, "unit"));
119 <        if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0))
119 >        if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) {
120 >                if (!SDerrorDetail[0])
121 >                        sprintf(SDerrorDetail, "Negative dimension in \"%s\"",
122 >                                                sd->name);
123                  return SDEdata;
124 +        }
125          if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
126                          (mgfstr = ezxml_txt(geom)) == NULL)
127                  return SDEnone;
128 +        while (isspace(*mgfstr))
129 +                ++mgfstr;
130 +        if (!*mgfstr)
131 +                return SDEnone;
132          if ((fmt = ezxml_attr(geom, "format")) != NULL &&
133                          strcasecmp(fmt, "MGF")) {
134                  sprintf(SDerrorDetail,
# Line 109 | Line 137 | SDloadGeometry(SDData *sd, ezxml_t wdb)
137                  return SDEsupport;
138          }
139          cfact = to_meters(ezxml_attr(geom, "unit"));
140 +        if (cfact <= 0)
141 +                return SDEformat;
142          sd->mgf = (char *)malloc(strlen(mgfstr)+32);
143          if (sd->mgf == NULL) {
144                  strcpy(SDerrorDetail, "Out of memory in SDloadGeometry");
# Line 152 | Line 182 | SDloadFile(SDData *sd, const char *fname)
182          }
183          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
184          if (wtl == NULL) {
185 <                sprintf(SDerrorDetail, "BSDF \"%s\": no optical layer'",
185 >                sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'",
186                                  sd->name);
187                  ezxml_free(fl);
188                  return SDEformat;
189          }
190                                  /* load geometry if present */
191          lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material"));
192 <        if (lastErr)
192 >        if (lastErr) {
193 >                ezxml_free(fl);
194                  return lastErr;
195 +        }
196                                  /* try loading variable resolution data */
197          lastErr = SDloadTre(sd, wtl);
198                                  /* check our result */
199 <        switch (lastErr) {
168 <        case SDEformat:
169 <        case SDEdata:
170 <        case SDEsupport:        /* possibly we just tried the wrong format */
199 >        if (lastErr == SDEsupport)      /* try matrix BSDF if not tree data */
200                  lastErr = SDloadMtx(sd, wtl);
201 <                break;
173 <        default:                /* variable res. OK else serious error */
174 <                break;
175 <        }
201 >                
202                                  /* done with XML file */
203          ezxml_free(fl);
204          
# Line 190 | Line 216 | SDloadFile(SDData *sd, const char *fname)
216          if (sd->tf != NULL && sd->tf->maxHemi <= .001) {
217                  SDfreeSpectralDF(sd->tf); sd->tf = NULL;
218          }
219 +        if (sd->tb != NULL && sd->tb->maxHemi <= .001) {
220 +                SDfreeSpectralDF(sd->tb); sd->tb = NULL;
221 +        }
222                                  /* return success */
223          return SDEnone;
224   }
# Line 218 | Line 247 | SDnewSpectralDF(int nc)
247          return df;
248   }
249  
250 + /* Add component(s) to spectral distribution function */
251 + SDSpectralDF *
252 + SDaddComponent(SDSpectralDF *odf, int nadd)
253 + {
254 +        SDSpectralDF    *df;
255 +
256 +        if (odf == NULL)
257 +                return SDnewSpectralDF(nadd);
258 +        if (nadd <= 0)
259 +                return odf;
260 +        df = (SDSpectralDF *)realloc(odf, sizeof(SDSpectralDF) +
261 +                                (odf->ncomp+nadd-1)*sizeof(SDComponent));
262 +        if (df == NULL) {
263 +                sprintf(SDerrorDetail,
264 +                        "Cannot add %d component(s) to spectral DF", nadd);
265 +                SDfreeSpectralDF(odf);
266 +                return NULL;
267 +        }
268 +        memset(df->comp+df->ncomp, 0, nadd*sizeof(SDComponent));
269 +        df->ncomp += nadd;
270 +        return df;
271 + }
272 +
273   /* Free cached cumulative distributions for BSDF component */
274   void
275   SDfreeCumulativeCache(SDSpectralDF *df)
# Line 244 | Line 296 | SDfreeSpectralDF(SDSpectralDF *df)
296                  return;
297          SDfreeCumulativeCache(df);
298          for (n = df->ncomp; n-- > 0; )
299 <                (*df->comp[n].func->freeSC)(df->comp[n].dist);
299 >                if (df->comp[n].dist != NULL)
300 >                        (*df->comp[n].func->freeSC)(df->comp[n].dist);
301          free(df);
302   }
303  
# Line 268 | Line 321 | SDclipName(char *res, const char *fname)
321  
322   /* Initialize an unused BSDF struct (simply clears to zeroes) */
323   void
324 < SDclearBSDF(SDData *sd)
324 > SDclearBSDF(SDData *sd, const char *fname)
325   {
326 <        if (sd != NULL)
327 <                memset(sd, 0, sizeof(SDData));
326 >        if (sd == NULL)
327 >                return;
328 >        memset(sd, 0, sizeof(SDData));
329 >        if (fname == NULL)
330 >                return;
331 >        SDclipName(sd->name, fname);
332   }
333  
334   /* Free data associated with BSDF struct */
# Line 296 | Line 353 | SDfreeBSDF(SDData *sd)
353                  SDfreeSpectralDF(sd->tf);
354                  sd->tf = NULL;
355          }
356 +        if (sd->tb != NULL) {
357 +                SDfreeSpectralDF(sd->tb);
358 +                sd->tb = NULL;
359 +        }
360          sd->rLambFront.cieY = .0;
361          sd->rLambFront.spec.flags = 0;
362          sd->rLambBack.cieY = .0;
# Line 329 | Line 390 | SDgetCache(const char *bname)
390          sdl->next = SDcacheList;
391          SDcacheList = sdl;
392  
393 <        sdl->refcnt++;
393 >        sdl->refcnt = 1;
394          return &sdl->bsdf;
395   }
396  
# Line 373 | Line 434 | SDfreeCache(const SDData *sd)
434          for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next)
435                  if (&sdl->bsdf == sd)
436                          break;
437 <        if (sdl == NULL || --sdl->refcnt)
437 >        if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0)))
438                  return;                 /* missing or still in use */
439                                          /* keep unreferenced data? */
440          if (SDisLoaded(sd) && SDretainSet) {
# Line 383 | Line 444 | SDfreeCache(const SDData *sd)
444                  SDfreeCumulativeCache(sd->rf);
445                  SDfreeCumulativeCache(sd->rb);
446                  SDfreeCumulativeCache(sd->tf);
447 +                SDfreeCumulativeCache(sd->tb);
448                  return;
449          }
450                                          /* remove from list and free */
# Line 396 | Line 458 | SDfreeCache(const SDData *sd)
458  
459   /* Sample an individual BSDF component */
460   SDError
461 < SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
400 <                        double randX, SDComponent *sdc)
461 > SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc)
462   {
463          float           coef[SDmaxCh];
464          SDError         ec;
465 +        FVECT           inVec;
466          const SDCDst    *cd;
467          double          d;
468          int             n;
469                                          /* check arguments */
470 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL))
470 >        if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL))
471                  return SDEargument;
472                                          /* get cumulative distribution */
473 +        VCOPY(inVec, ioVec);
474          cd = (*sdc->func->getCDist)(inVec, sdc);
475          if (cd == NULL)
476                  return SDEmemory;
477 <        if (cd->cTotal <= 1e-7) {       /* anything to sample? */
477 >        if (cd->cTotal <= 1e-6) {       /* anything to sample? */
478                  sv->spec = c_dfcolor;
479                  sv->cieY = .0;
480 <                memset(outVec, 0, 3*sizeof(double));
480 >                memset(ioVec, 0, 3*sizeof(double));
481                  return SDEnone;
482          }
483          sv->cieY = cd->cTotal;
484                                          /* compute sample direction */
485 <        ec = (*sdc->func->sampCDist)(outVec, randX, cd);
485 >        ec = (*sdc->func->sampCDist)(ioVec, randX, cd);
486          if (ec)
487                  return ec;
488                                          /* get BSDF color */
489 <        n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
489 >        n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
490          if (n <= 0) {
491                  strcpy(SDerrorDetail, "BSDF sample value error");
492                  return SDEinternal;
# Line 448 | Line 511 | SDmultiSamp(double t[], int n, double randX)
511          unsigned        nBits;
512          double          scale;
513          bitmask_t       ndx, coord[MS_MAXDIM];
514 <        
514 >
515 >        if (n <= 0)                     /* check corner cases */
516 >                return;
517 >        if (randX < 0) randX = 0;
518 >        else if (randX >= 1.) randX = 0.999999999999999;
519 >        if (n == 1) {
520 >                t[0] = randX;
521 >                return;
522 >        }
523          while (n > MS_MAXDIM)           /* punt for higher dimensions */
524 <                t[--n] = drand48();
524 >                t[--n] = rand()*(1./(RAND_MAX+.5));
525          nBits = (8*sizeof(bitmask_t) - 1) / n;
526          ndx = randX * (double)((bitmask_t)1 << (nBits*n));
527                                          /* get coordinate on Hilbert curve */
# Line 458 | Line 529 | SDmultiSamp(double t[], int n, double randX)
529                                          /* convert back to [0,1) range */
530          scale = 1. / (double)((bitmask_t)1 << nBits);
531          while (n--)
532 <                t[n] = scale * ((double)coord[n] + drand48());
532 >                t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5)));
533   }
534  
535   #undef MS_MAXDIM
# Line 471 | Line 542 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
542          SDmultiSamp(outVec, 2, randX);
543          SDsquare2disk(outVec, outVec[0], outVec[1]);
544          outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1];
545 <        if (outVec[2] > .0)             /* a bit of paranoia */
545 >        if (outVec[2] > 0)              /* a bit of paranoia */
546                  outVec[2] = sqrt(outVec[2]);
547          if (!outFront)                  /* going out back? */
548                  outVec[2] = -outVec[2];
# Line 479 | Line 550 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
550  
551   /* Query projected solid angle coverage for non-diffuse BSDF direction */
552   SDError
553 < SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd)
553 > SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2,
554 >                                int qflags, const SDData *sd)
555   {
556 <        SDSpectralDF    *rdf;
556 >        SDSpectralDF    *rdf, *tdf;
557          SDError         ec;
558          int             i;
559                                          /* check arguments */
560 <        if ((projSA == NULL) | (vec == NULL) | (sd == NULL))
560 >        if ((projSA == NULL) | (v1 == NULL) | (sd == NULL))
561                  return SDEargument;
562                                          /* initialize extrema */
563 <        switch (qflags & SDqueryMin+SDqueryMax) {
563 >        switch (qflags) {
564          case SDqueryMax:
565                  projSA[0] = .0;
566                  break;
# Line 501 | Line 573 | SDsizeBSDF(double *projSA, const FVECT vec, int qflags
573          case 0:
574                  return SDEargument;
575          }
576 <        if (vec[2] > .0)                /* front surface query? */
576 >        if (v1[2] > 0) {                /* front surface query? */
577                  rdf = sd->rf;
578 <        else
578 >                tdf = (sd->tf != NULL) ? sd->tf : sd->tb;
579 >        } else {
580                  rdf = sd->rb;
581 +                tdf = (sd->tb != NULL) ? sd->tb : sd->tf;
582 +        }
583 +        if (v2 != NULL)                 /* bidirectional? */
584 +                if (v1[2] > 0 ^ v2[2] > 0)
585 +                        rdf = NULL;
586 +                else
587 +                        tdf = NULL;
588          ec = SDEdata;                   /* run through components */
589          for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
590 <                ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags,
591 <                                                        rdf->comp[i].dist);
590 >                ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
591 >                                                qflags, &rdf->comp[i]);
592                  if (ec)
593                          return ec;
594          }
595 <        for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) {
596 <                ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags,
597 <                                                        sd->tf->comp[i].dist);
595 >        for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
596 >                ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
597 >                                                qflags, &tdf->comp[i]);
598                  if (ec)
599                          return ec;
600          }
601 <        return ec;
601 >        if (ec) {                       /* all diffuse? */
602 >                projSA[0] = M_PI;
603 >                if (qflags == SDqueryMin+SDqueryMax)
604 >                        projSA[1] = M_PI;
605 >        }
606 >        return SDEnone;
607   }
608  
609   /* Return BSDF for the given incident and scattered ray vectors */
# Line 533 | Line 618 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
618          if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL))
619                  return SDEargument;
620                                          /* whose side are we on? */
621 <        inFront = (inVec[2] > .0);
622 <        outFront = (outVec[2] > .0);
621 >        inFront = (inVec[2] > 0);
622 >        outFront = (outVec[2] > 0);
623                                          /* start with diffuse portion */
624          if (inFront & outFront) {
625                  *sv = sd->rLambFront;
# Line 542 | Line 627 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
627          } else if (!(inFront | outFront)) {
628                  *sv = sd->rLambBack;
629                  sdf = sd->rb;
630 <        } else /* inFront ^ outFront */ {
630 >        } else if (inFront) {
631                  *sv = sd->tLamb;
632 <                sdf = sd->tf;
632 >                sdf = (sd->tf != NULL) ? sd->tf : sd->tb;
633 >        } else /* inBack */ {
634 >                *sv = sd->tLamb;
635 >                sdf = (sd->tb != NULL) ? sd->tb : sd->tf;
636          }
637          sv->cieY *= 1./M_PI;
638                                          /* add non-diffuse components */
639          i = (sdf != NULL) ? sdf->ncomp : 0;
640          while (i-- > 0) {
641                  nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
642 <                                                        sdf->comp[i].dist);
642 >                                                        &sdf->comp[i]);
643                  while (nch-- > 0) {
644                          c_cmix(&sv->spec, sv->cieY, &sv->spec,
645                                          coef[nch], &sdf->comp[i].cspec[nch]);
# Line 568 | Line 656 | double
656   SDdirectHemi(const FVECT inVec, int sflags, const SDData *sd)
657   {
658          double          hsum;
659 <        SDSpectralDF    *rdf;
659 >        SDSpectralDF    *rdf, *tdf;
660          const SDCDst    *cd;
661          int             i;
662                                          /* check arguments */
663          if ((inVec == NULL) | (sd == NULL))
664                  return .0;
665                                          /* gather diffuse components */
666 <        if (inVec[2] > .0) {
666 >        if (inVec[2] > 0) {
667                  hsum = sd->rLambFront.cieY;
668                  rdf = sd->rf;
669 +                tdf = (sd->tf != NULL) ? sd->tf : sd->tb;
670          } else /* !inFront */ {
671                  hsum = sd->rLambBack.cieY;
672                  rdf = sd->rb;
673 +                tdf = (sd->tb != NULL) ? sd->tb : sd->tf;
674          }
675          if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR)
676                  hsum = .0;
677          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT)
678                  hsum += sd->tLamb.cieY;
679                                          /* gather non-diffuse components */
680 <        i = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR &&
681 <                        rdf != NULL) ? rdf->ncomp : 0;
680 >        i = (((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR) &
681 >                        (rdf != NULL)) ? rdf->ncomp : 0;
682          while (i-- > 0) {               /* non-diffuse reflection */
683                  cd = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]);
684                  if (cd != NULL)
685                          hsum += cd->cTotal;
686          }
687 <        i = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT &&
688 <                        sd->tf != NULL) ? sd->tf->ncomp : 0;
687 >        i = (((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT) &
688 >                        (tdf != NULL)) ? tdf->ncomp : 0;
689          while (i-- > 0) {               /* non-diffuse transmission */
690 <                cd = (*sd->tf->comp[i].func->getCDist)(inVec, &sd->tf->comp[i]);
690 >                cd = (*tdf->comp[i].func->getCDist)(inVec, &tdf->comp[i]);
691                  if (cd != NULL)
692                          hsum += cd->cTotal;
693          }
# Line 606 | Line 696 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
696  
697   /* Sample BSDF direction based on the given random variable */
698   SDError
699 < SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
610 <                        double randX, int sflags, const SDData *sd)
699 > SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
700   {
701          SDError         ec;
702 +        FVECT           inVec;
703          int             inFront;
704 <        SDSpectralDF    *rdf;
704 >        SDSpectralDF    *rdf, *tdf;
705          double          rdiff;
706          float           coef[SDmaxCh];
707          int             i, j, n, nr;
708          SDComponent     *sdc;
709          const SDCDst    **cdarr = NULL;
710                                          /* check arguments */
711 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
712 <                        (randX < .0) | (randX >= 1.))
711 >        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
712 >                        (randX < 0) | (randX >= 1.))
713                  return SDEargument;
714                                          /* whose side are we on? */
715 <        inFront = (inVec[2] > .0);
715 >        VCOPY(inVec, ioVec);
716 >        inFront = (inVec[2] > 0);
717                                          /* remember diffuse portions */
718          if (inFront) {
719                  *sv = sd->rLambFront;
720                  rdf = sd->rf;
721 +                tdf = (sd->tf != NULL) ? sd->tf : sd->tb;
722          } else /* !inFront */ {
723                  *sv = sd->rLambBack;
724                  rdf = sd->rb;
725 +                tdf = (sd->tb != NULL) ? sd->tb : sd->tf;
726          }
727          if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR)
728                  sv->cieY = .0;
# Line 637 | Line 730 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
730          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT)
731                  sv->cieY += sd->tLamb.cieY;
732                                          /* gather non-diffuse components */
733 <        i = nr = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR &&
734 <                        rdf != NULL) ? rdf->ncomp : 0;
735 <        j = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT &&
736 <                        sd->tf != NULL) ? sd->tf->ncomp : 0;
733 >        i = nr = (((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR) &
734 >                        (rdf != NULL)) ? rdf->ncomp : 0;
735 >        j = (((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT) &
736 >                        (tdf != NULL)) ? tdf->ncomp : 0;
737          n = i + j;
738          if (n > 0 && (cdarr = (const SDCDst **)malloc(n*sizeof(SDCDst *))) == NULL)
739                  return SDEmemory;
740          while (j-- > 0) {               /* non-diffuse transmission */
741 <                cdarr[i+j] = (*sd->tf->comp[j].func->getCDist)(inVec, &sd->tf->comp[j]);
741 >                cdarr[i+j] = (*tdf->comp[j].func->getCDist)(inVec, &tdf->comp[j]);
742                  if (cdarr[i+j] == NULL) {
743                          free(cdarr);
744                          return SDEmemory;
# Line 660 | Line 753 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
753                  }
754                  sv->cieY += cdarr[i]->cTotal;
755          }
756 <        if (sv->cieY <= 1e-7) {         /* anything to sample? */
756 >        if (sv->cieY <= 1e-6) {         /* anything to sample? */
757                  sv->cieY = .0;
758 <                memset(outVec, 0, 3*sizeof(double));
758 >                memset(ioVec, 0, 3*sizeof(double));
759                  return SDEnone;
760          }
761                                          /* scale random variable */
762          randX *= sv->cieY;
763                                          /* diffuse reflection? */
764          if (randX < rdiff) {
765 <                SDdiffuseSamp(outVec, inFront, randX/rdiff);
765 >                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
766                  goto done;
767          }
768          randX -= rdiff;
# Line 677 | Line 770 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
770          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
771                  if (randX < sd->tLamb.cieY) {
772                          sv->spec = sd->tLamb.spec;
773 <                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
773 >                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
774                          goto done;
775                  }
776                  randX -= sd->tLamb.cieY;
# Line 688 | Line 781 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
781          if (i >= n)
782                  return SDEinternal;
783                                          /* compute sample direction */
784 <        sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
785 <        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
784 >        sdc = (i < nr) ? &rdf->comp[i] : &tdf->comp[i-nr];
785 >        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
786          if (ec)
787                  return ec;
788                                          /* compute color */
789 <        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
789 >        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
790          if (j <= 0) {
791                  sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
792                                  sd->name);
# Line 720 | Line 813 | SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const
813          if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL))
814                  return SDEargument;
815          VCOPY(vMtx[2], sNrm);
816 <        if (normalize(vMtx[2]) == .0)
816 >        if (normalize(vMtx[2]) == 0)
817                  return SDEargument;
818          fcross(vMtx[0], uVec, vMtx[2]);
819 <        if (normalize(vMtx[0]) == .0)
819 >        if (normalize(vMtx[0]) == 0)
820                  return SDEargument;
821          fcross(vMtx[1], vMtx[2], vMtx[0]);
822          return SDEnone;
# Line 743 | Line 836 | SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3])
836          mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1];
837          mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2];
838          d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2];
839 <        if (d == .0) {
839 >        if (d == 0) {
840                  strcpy(SDerrorDetail, "Zero determinant in matrix inversion");
841                  return SDEargument;
842          }
# Line 770 | Line 863 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
863          if (vMtx == NULL) {             /* assume they just want to normalize */
864                  if (resVec != inpVec)
865                          VCOPY(resVec, inpVec);
866 <                return (normalize(resVec) > .0) ? SDEnone : SDEargument;
866 >                return (normalize(resVec) > 0) ? SDEnone : SDEargument;
867          }
868          vTmp[0] = DOT(vMtx[0], inpVec);
869          vTmp[1] = DOT(vMtx[1], inpVec);
870          vTmp[2] = DOT(vMtx[2], inpVec);
871 <        if (normalize(vTmp) == .0)
871 >        if (normalize(vTmp) == 0)
872                  return SDEargument;
873          VCOPY(resVec, vTmp);
874          return SDEnone;
# Line 790 | Line 883 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
883  
884   #include "standard.h"
885   #include "paths.h"
793 #include <ctype.h>
886  
887   #define MAXLATS         46              /* maximum number of latitudes */
888  
# Line 847 | Line 939 | static int     nabases = 3;    /* current number of defined b
939   static int
940   fequal(double a, double b)
941   {
942 <        if (b != .0)
942 >        if (b != 0)
943                  a = a/b - 1.;
944          return((a <= 1e-6) & (a >= -1e-6));
945   }
# Line 910 | Line 1002 | ab_getndx(             /* get index corresponding to the given ve
1002   {
1003          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
1004          int     li, ndx;
1005 <        double  pol, azi, d;
1005 >        double  pol, azi;
1006  
1007          if ((v[2] < -1.0) | (v[2] > 1.0))
1008                  return(-1);
# Line 1150 | Line 1242 | check_bsdf_data(       /* check that BSDF data is sane */
1242   )
1243   {
1244          double          *omega_iarr, *omega_oarr;
1245 <        double          dom, contrib, hemi_total, full_total;
1245 >        double          dom, hemi_total, full_total;
1246          int             nneg;
1247          FVECT           v;
1248          int             i, o;
# Line 1165 | Line 1257 | check_bsdf_data(       /* check that BSDF data is sane */
1257          hemi_total = .0;
1258          for (i = dp->ninc; i--; ) {
1259                  dom = getBSDF_incohm(dp,i);
1260 <                if (dom <= .0) {
1260 >                if (dom <= 0) {
1261                          error(WARNING, "zero/negative incoming solid angle");
1262                          continue;
1263                  }
# Line 1188 | Line 1280 | check_bsdf_data(       /* check that BSDF data is sane */
1280          hemi_total = .0;
1281          for (o = dp->nout; o--; ) {
1282                  dom = getBSDF_outohm(dp,o);
1283 <                if (dom <= .0) {
1283 >                if (dom <= 0) {
1284                          error(WARNING, "zero/negative outgoing solid angle");
1285                          continue;
1286                  }
# Line 1212 | Line 1304 | check_bsdf_data(       /* check that BSDF data is sane */
1304                  hemi_total = .0;
1305                  for (o = dp->nout; o--; ) {
1306                          double  f = BSDF_value(dp,i,o);
1307 <                        if (f >= .0)
1307 >                        if (f >= 0)
1308                                  hemi_total += f*omega_oarr[o];
1309                          else {
1310                                  nneg += (f < -FTINY);
# Line 1300 | Line 1392 | load_BSDF(             /* load BSDF data from file */
1392                  error(WARNING, errmsg);
1393                  ezxml_free(fl);
1394                  return(NULL);
1395 <        }              
1396 <        load_angle_basis(ezxml_child(ezxml_child(wtl,
1397 <                                "DataDefinition"), "AngleBasis"));
1395 >        }
1396 >        for (wld = ezxml_child(ezxml_child(wtl,
1397 >                                "DataDefinition"), "AngleBasis");
1398 >                        wld != NULL; wld = wld->next)
1399 >                load_angle_basis(wld);
1400          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
1401          load_geometry(dp, ezxml_child(wtl, "Material"));
1402          for (wld = ezxml_child(wtl, "WavelengthData");
# Line 1363 | Line 1457 | r_BSDF_incvec(         /* compute random input vector at give
1457          if (!getBSDF_incvec(v, b, i))
1458                  return(0);
1459          rad = sqrt(getBSDF_incohm(b, i) / PI);
1460 <        multisamp(pert, 3, rv);
1460 >        SDmultiSamp(pert, 3, rv);
1461          for (j = 0; j < 3; j++)
1462                  v[j] += rad*(2.*pert[j] - 1.);
1463          if (xm != NULL)
# Line 1388 | Line 1482 | r_BSDF_outvec(         /* compute random output vector at giv
1482          if (!getBSDF_outvec(v, b, o))
1483                  return(0);
1484          rad = sqrt(getBSDF_outohm(b, o) / PI);
1485 <        multisamp(pert, 3, rv);
1485 >        SDmultiSamp(pert, 3, rv);
1486          for (j = 0; j < 3; j++)
1487                  v[j] += rad*(2.*pert[j] - 1.);
1488          if (xm != NULL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines