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.32 by greg, Thu Jun 23 16:00:37 2011 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 85 | Line 92 | SDloadGeometry(SDData *sd, ezxml_t wdb)
92  
93          if (wdb == NULL)                /* no geometry section? */
94                  return SDEnone;
88        sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name);
95          sd->dim[0] = sd->dim[1] = sd->dim[2] = .0;
96          if ((geom = ezxml_child(wdb, "Width")) != NULL)
97                  sd->dim[0] = atof(ezxml_txt(geom)) *
# Line 96 | Line 102 | SDloadGeometry(SDData *sd, ezxml_t wdb)
102          if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
103                  sd->dim[2] = atof(ezxml_txt(geom)) *
104                                  to_meters(ezxml_attr(geom, "unit"));
105 <        if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0))
105 >        if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) {
106 >                sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name);
107                  return SDEdata;
108 +        }
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 176 | SDloadFile(SDData *sd, const char *fname)
176                                  /* try loading variable resolution data */
177          lastErr = SDloadTre(sd, wtl);
178                                  /* check our result */
179 <        switch (lastErr) {
168 <        case SDEformat:
169 <        case SDEdata:
170 <        case SDEsupport:        /* possibly we just tried the wrong format */
179 >        if (lastErr == SDEsupport)      /* try matrix BSDF if not tree data */
180                  lastErr = SDloadMtx(sd, wtl);
181 <                break;
173 <        default:                /* variable res. OK else serious error */
174 <                break;
175 <        }
181 >                
182                                  /* done with XML file */
183          ezxml_free(fl);
184          
# Line 268 | Line 274 | SDclipName(char *res, const char *fname)
274  
275   /* Initialize an unused BSDF struct (simply clears to zeroes) */
276   void
277 < SDclearBSDF(SDData *sd)
277 > SDclearBSDF(SDData *sd, const char *fname)
278   {
279 <        if (sd != NULL)
280 <                memset(sd, 0, sizeof(SDData));
279 >        if (sd == NULL)
280 >                return;
281 >        memset(sd, 0, sizeof(SDData));
282 >        if (fname == NULL)
283 >                return;
284 >        SDclipName(sd->name, fname);
285   }
286  
287   /* Free data associated with BSDF struct */
# Line 329 | Line 339 | SDgetCache(const char *bname)
339          sdl->next = SDcacheList;
340          SDcacheList = sdl;
341  
342 <        sdl->refcnt++;
342 >        sdl->refcnt = 1;
343          return &sdl->bsdf;
344   }
345  
# Line 373 | Line 383 | SDfreeCache(const SDData *sd)
383          for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next)
384                  if (&sdl->bsdf == sd)
385                          break;
386 <        if (sdl == NULL || --sdl->refcnt)
386 >        if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0)))
387                  return;                 /* missing or still in use */
388                                          /* keep unreferenced data? */
389          if (SDisLoaded(sd) && SDretainSet) {
# Line 396 | Line 406 | SDfreeCache(const SDData *sd)
406  
407   /* Sample an individual BSDF component */
408   SDError
409 < SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
400 <                        double randX, SDComponent *sdc)
409 > SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc)
410   {
411          float           coef[SDmaxCh];
412          SDError         ec;
413 +        FVECT           inVec;
414          const SDCDst    *cd;
415          double          d;
416          int             n;
417                                          /* check arguments */
418 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL))
418 >        if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL))
419                  return SDEargument;
420                                          /* get cumulative distribution */
421 +        VCOPY(inVec, ioVec);
422          cd = (*sdc->func->getCDist)(inVec, sdc);
423          if (cd == NULL)
424                  return SDEmemory;
425          if (cd->cTotal <= 1e-7) {       /* anything to sample? */
426                  sv->spec = c_dfcolor;
427                  sv->cieY = .0;
428 <                memset(outVec, 0, 3*sizeof(double));
428 >                memset(ioVec, 0, 3*sizeof(double));
429                  return SDEnone;
430          }
431          sv->cieY = cd->cTotal;
432                                          /* compute sample direction */
433 <        ec = (*sdc->func->sampCDist)(outVec, randX, cd);
433 >        ec = (*sdc->func->sampCDist)(ioVec, randX, cd);
434          if (ec)
435                  return ec;
436                                          /* get BSDF color */
437 <        n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
437 >        n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
438          if (n <= 0) {
439                  strcpy(SDerrorDetail, "BSDF sample value error");
440                  return SDEinternal;
# Line 450 | Line 461 | SDmultiSamp(double t[], int n, double randX)
461          bitmask_t       ndx, coord[MS_MAXDIM];
462          
463          while (n > MS_MAXDIM)           /* punt for higher dimensions */
464 <                t[--n] = drand48();
464 >                t[--n] = rand()*(1./(RAND_MAX+.5));
465          nBits = (8*sizeof(bitmask_t) - 1) / n;
466          ndx = randX * (double)((bitmask_t)1 << (nBits*n));
467                                          /* get coordinate on Hilbert curve */
# Line 458 | Line 469 | SDmultiSamp(double t[], int n, double randX)
469                                          /* convert back to [0,1) range */
470          scale = 1. / (double)((bitmask_t)1 << nBits);
471          while (n--)
472 <                t[n] = scale * ((double)coord[n] + drand48());
472 >                t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5)));
473   }
474  
475   #undef MS_MAXDIM
# Line 471 | Line 482 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
482          SDmultiSamp(outVec, 2, randX);
483          SDsquare2disk(outVec, outVec[0], outVec[1]);
484          outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1];
485 <        if (outVec[2] > .0)             /* a bit of paranoia */
485 >        if (outVec[2] > 0)              /* a bit of paranoia */
486                  outVec[2] = sqrt(outVec[2]);
487          if (!outFront)                  /* going out back? */
488                  outVec[2] = -outVec[2];
# Line 479 | Line 490 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
490  
491   /* Query projected solid angle coverage for non-diffuse BSDF direction */
492   SDError
493 < SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd)
493 > SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2,
494 >                                int qflags, const SDData *sd)
495   {
496 <        SDSpectralDF    *rdf;
496 >        SDSpectralDF    *rdf, *tdf;
497          SDError         ec;
498          int             i;
499                                          /* check arguments */
500 <        if ((projSA == NULL) | (vec == NULL) | (sd == NULL))
500 >        if ((projSA == NULL) | (v1 == NULL) | (sd == NULL))
501                  return SDEargument;
502                                          /* initialize extrema */
503 <        switch (qflags & SDqueryMin+SDqueryMax) {
503 >        switch (qflags) {
504          case SDqueryMax:
505                  projSA[0] = .0;
506                  break;
# Line 501 | Line 513 | SDsizeBSDF(double *projSA, const FVECT vec, int qflags
513          case 0:
514                  return SDEargument;
515          }
516 <        if (vec[2] > .0)                /* front surface query? */
516 >        if (v1[2] > 0)                  /* front surface query? */
517                  rdf = sd->rf;
518          else
519                  rdf = sd->rb;
520 +        tdf = sd->tf;
521 +        if (v2 != NULL)                 /* bidirectional? */
522 +                if (v1[2] > 0 ^ v2[2] > 0)
523 +                        rdf = NULL;
524 +                else
525 +                        tdf = NULL;
526          ec = SDEdata;                   /* run through components */
527          for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
528 <                ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags,
529 <                                                        rdf->comp[i].dist);
528 >                ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
529 >                                                qflags, &rdf->comp[i]);
530                  if (ec)
531                          return ec;
532          }
533 <        for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) {
534 <                ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags,
535 <                                                        sd->tf->comp[i].dist);
533 >        for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
534 >                ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
535 >                                                qflags, &tdf->comp[i]);
536                  if (ec)
537                          return ec;
538          }
539 <        return ec;
539 >        if (ec) {                       /* all diffuse? */
540 >                projSA[0] = M_PI;
541 >                if (qflags == SDqueryMin+SDqueryMax)
542 >                        projSA[1] = M_PI;
543 >        }
544 >        return SDEnone;
545   }
546  
547   /* Return BSDF for the given incident and scattered ray vectors */
# Line 533 | Line 556 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
556          if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL))
557                  return SDEargument;
558                                          /* whose side are we on? */
559 <        inFront = (inVec[2] > .0);
560 <        outFront = (outVec[2] > .0);
559 >        inFront = (inVec[2] > 0);
560 >        outFront = (outVec[2] > 0);
561                                          /* start with diffuse portion */
562          if (inFront & outFront) {
563                  *sv = sd->rLambFront;
# Line 551 | Line 574 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
574          i = (sdf != NULL) ? sdf->ncomp : 0;
575          while (i-- > 0) {
576                  nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
577 <                                                        sdf->comp[i].dist);
577 >                                                        &sdf->comp[i]);
578                  while (nch-- > 0) {
579                          c_cmix(&sv->spec, sv->cieY, &sv->spec,
580                                          coef[nch], &sdf->comp[i].cspec[nch]);
# Line 575 | Line 598 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
598          if ((inVec == NULL) | (sd == NULL))
599                  return .0;
600                                          /* gather diffuse components */
601 <        if (inVec[2] > .0) {
601 >        if (inVec[2] > 0) {
602                  hsum = sd->rLambFront.cieY;
603                  rdf = sd->rf;
604          } else /* !inFront */ {
# Line 606 | Line 629 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
629  
630   /* Sample BSDF direction based on the given random variable */
631   SDError
632 < SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
610 <                        double randX, int sflags, const SDData *sd)
632 > SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
633   {
634          SDError         ec;
635 +        FVECT           inVec;
636          int             inFront;
637          SDSpectralDF    *rdf;
638          double          rdiff;
# Line 618 | Line 641 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
641          SDComponent     *sdc;
642          const SDCDst    **cdarr = NULL;
643                                          /* check arguments */
644 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
645 <                        (randX < .0) | (randX >= 1.))
644 >        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
645 >                        (randX < 0) | (randX >= 1.))
646                  return SDEargument;
647                                          /* whose side are we on? */
648 <        inFront = (inVec[2] > .0);
648 >        VCOPY(inVec, ioVec);
649 >        inFront = (inVec[2] > 0);
650                                          /* remember diffuse portions */
651          if (inFront) {
652                  *sv = sd->rLambFront;
# Line 662 | Line 686 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
686          }
687          if (sv->cieY <= 1e-7) {         /* anything to sample? */
688                  sv->cieY = .0;
689 <                memset(outVec, 0, 3*sizeof(double));
689 >                memset(ioVec, 0, 3*sizeof(double));
690                  return SDEnone;
691          }
692                                          /* scale random variable */
693          randX *= sv->cieY;
694                                          /* diffuse reflection? */
695          if (randX < rdiff) {
696 <                SDdiffuseSamp(outVec, inFront, randX/rdiff);
696 >                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
697                  goto done;
698          }
699          randX -= rdiff;
# Line 677 | Line 701 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
701          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
702                  if (randX < sd->tLamb.cieY) {
703                          sv->spec = sd->tLamb.spec;
704 <                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
704 >                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
705                          goto done;
706                  }
707                  randX -= sd->tLamb.cieY;
# Line 689 | Line 713 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
713                  return SDEinternal;
714                                          /* compute sample direction */
715          sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
716 <        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
716 >        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
717          if (ec)
718                  return ec;
719                                          /* compute color */
720 <        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
720 >        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
721          if (j <= 0) {
722                  sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
723                                  sd->name);
# Line 720 | Line 744 | SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const
744          if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL))
745                  return SDEargument;
746          VCOPY(vMtx[2], sNrm);
747 <        if (normalize(vMtx[2]) == .0)
747 >        if (normalize(vMtx[2]) == 0)
748                  return SDEargument;
749          fcross(vMtx[0], uVec, vMtx[2]);
750 <        if (normalize(vMtx[0]) == .0)
750 >        if (normalize(vMtx[0]) == 0)
751                  return SDEargument;
752          fcross(vMtx[1], vMtx[2], vMtx[0]);
753          return SDEnone;
# Line 743 | Line 767 | SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3])
767          mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1];
768          mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2];
769          d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2];
770 <        if (d == .0) {
770 >        if (d == 0) {
771                  strcpy(SDerrorDetail, "Zero determinant in matrix inversion");
772                  return SDEargument;
773          }
# Line 770 | Line 794 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
794          if (vMtx == NULL) {             /* assume they just want to normalize */
795                  if (resVec != inpVec)
796                          VCOPY(resVec, inpVec);
797 <                return (normalize(resVec) > .0) ? SDEnone : SDEargument;
797 >                return (normalize(resVec) > 0) ? SDEnone : SDEargument;
798          }
799          vTmp[0] = DOT(vMtx[0], inpVec);
800          vTmp[1] = DOT(vMtx[1], inpVec);
801          vTmp[2] = DOT(vMtx[2], inpVec);
802 <        if (normalize(vTmp) == .0)
802 >        if (normalize(vTmp) == 0)
803                  return SDEargument;
804          VCOPY(resVec, vTmp);
805          return SDEnone;
# Line 790 | Line 814 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
814  
815   #include "standard.h"
816   #include "paths.h"
793 #include <ctype.h>
817  
818   #define MAXLATS         46              /* maximum number of latitudes */
819  
# Line 847 | Line 870 | static int     nabases = 3;    /* current number of defined b
870   static int
871   fequal(double a, double b)
872   {
873 <        if (b != .0)
873 >        if (b != 0)
874                  a = a/b - 1.;
875          return((a <= 1e-6) & (a >= -1e-6));
876   }
# Line 1165 | Line 1188 | check_bsdf_data(       /* check that BSDF data is sane */
1188          hemi_total = .0;
1189          for (i = dp->ninc; i--; ) {
1190                  dom = getBSDF_incohm(dp,i);
1191 <                if (dom <= .0) {
1191 >                if (dom <= 0) {
1192                          error(WARNING, "zero/negative incoming solid angle");
1193                          continue;
1194                  }
# Line 1188 | Line 1211 | check_bsdf_data(       /* check that BSDF data is sane */
1211          hemi_total = .0;
1212          for (o = dp->nout; o--; ) {
1213                  dom = getBSDF_outohm(dp,o);
1214 <                if (dom <= .0) {
1214 >                if (dom <= 0) {
1215                          error(WARNING, "zero/negative outgoing solid angle");
1216                          continue;
1217                  }
# Line 1212 | Line 1235 | check_bsdf_data(       /* check that BSDF data is sane */
1235                  hemi_total = .0;
1236                  for (o = dp->nout; o--; ) {
1237                          double  f = BSDF_value(dp,i,o);
1238 <                        if (f >= .0)
1238 >                        if (f >= 0)
1239                                  hemi_total += f*omega_oarr[o];
1240                          else {
1241                                  nneg += (f < -FTINY);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines