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.17 by greg, Sat Feb 19 23:42:09 2011 UTC vs.
Revision 2.31 by greg, Fri Jun 10 01:11:26 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 "ezxml.h"
19   #include "hilbert.h"
# Line 45 | Line 47 | int                    SDretainSet = SDretainNone;
47   SDError
48   SDreportEnglish(SDError ec, FILE *fp)
49   {
48        if (fp == NULL)
49                return ec;
50          if (!ec)
51                  return SDEnone;
52 +        if ((ec < SDEnone) | (ec > SDEunknown)) {
53 +                SDerrorDetail[0] = '\0';
54 +                ec = SDEunknown;
55 +        }
56 +        if (fp == NULL)
57 +                return ec;
58          fputs(SDerrorEnglish[ec], fp);
59          if (SDerrorDetail[0]) {
60                  fputs(": ", fp);
# Line 95 | Line 101 | SDloadGeometry(SDData *sd, ezxml_t wdb)
101          if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
102                  sd->dim[2] = atof(ezxml_txt(geom)) *
103                                  to_meters(ezxml_attr(geom, "unit"));
104 <        if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0)) {
104 >        if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) {
105                  sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name);
106                  return SDEdata;
107          }
# Line 165 | Line 171 | SDloadFile(SDData *sd, const char *fname)
171                                  /* try loading variable resolution data */
172          lastErr = SDloadTre(sd, wtl);
173                                  /* check our result */
174 <        switch (lastErr) {
169 <        case SDEformat:
170 <        case SDEdata:
171 <        case SDEsupport:        /* possibly we just tried the wrong format */
174 >        if (lastErr == SDEsupport)      /* try matrix BSDF if not tree data */
175                  lastErr = SDloadMtx(sd, wtl);
176 <                break;
174 <        default:                /* variable res. OK else serious error */
175 <                break;
176 <        }
176 >                
177                                  /* done with XML file */
178          ezxml_free(fl);
179          
# Line 269 | Line 269 | SDclipName(char *res, const char *fname)
269  
270   /* Initialize an unused BSDF struct (simply clears to zeroes) */
271   void
272 < SDclearBSDF(SDData *sd)
272 > SDclearBSDF(SDData *sd, const char *fname)
273   {
274 <        if (sd != NULL)
275 <                memset(sd, 0, sizeof(SDData));
274 >        if (sd == NULL)
275 >                return;
276 >        memset(sd, 0, sizeof(SDData));
277 >        if (fname == NULL)
278 >                return;
279 >        SDclipName(sd->name, fname);
280   }
281  
282   /* Free data associated with BSDF struct */
# Line 330 | Line 334 | SDgetCache(const char *bname)
334          sdl->next = SDcacheList;
335          SDcacheList = sdl;
336  
337 <        sdl->refcnt++;
337 >        sdl->refcnt = 1;
338          return &sdl->bsdf;
339   }
340  
# Line 374 | Line 378 | SDfreeCache(const SDData *sd)
378          for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next)
379                  if (&sdl->bsdf == sd)
380                          break;
381 <        if (sdl == NULL || --sdl->refcnt)
381 >        if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0)))
382                  return;                 /* missing or still in use */
383                                          /* keep unreferenced data? */
384          if (SDisLoaded(sd) && SDretainSet) {
# Line 397 | Line 401 | SDfreeCache(const SDData *sd)
401  
402   /* Sample an individual BSDF component */
403   SDError
404 < SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
401 <                        double randX, SDComponent *sdc)
404 > SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc)
405   {
406          float           coef[SDmaxCh];
407          SDError         ec;
408 +        FVECT           inVec;
409          const SDCDst    *cd;
410          double          d;
411          int             n;
412                                          /* check arguments */
413 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL))
413 >        if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL))
414                  return SDEargument;
415                                          /* get cumulative distribution */
416 +        VCOPY(inVec, ioVec);
417          cd = (*sdc->func->getCDist)(inVec, sdc);
418          if (cd == NULL)
419                  return SDEmemory;
420          if (cd->cTotal <= 1e-7) {       /* anything to sample? */
421                  sv->spec = c_dfcolor;
422                  sv->cieY = .0;
423 <                memset(outVec, 0, 3*sizeof(double));
423 >                memset(ioVec, 0, 3*sizeof(double));
424                  return SDEnone;
425          }
426          sv->cieY = cd->cTotal;
427                                          /* compute sample direction */
428 <        ec = (*sdc->func->sampCDist)(outVec, randX, cd);
428 >        ec = (*sdc->func->sampCDist)(ioVec, randX, cd);
429          if (ec)
430                  return ec;
431                                          /* get BSDF color */
432 <        n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
432 >        n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
433          if (n <= 0) {
434                  strcpy(SDerrorDetail, "BSDF sample value error");
435                  return SDEinternal;
# Line 451 | Line 456 | SDmultiSamp(double t[], int n, double randX)
456          bitmask_t       ndx, coord[MS_MAXDIM];
457          
458          while (n > MS_MAXDIM)           /* punt for higher dimensions */
459 <                t[--n] = drand48();
459 >                t[--n] = rand()*(1./(RAND_MAX+.5));
460          nBits = (8*sizeof(bitmask_t) - 1) / n;
461          ndx = randX * (double)((bitmask_t)1 << (nBits*n));
462                                          /* get coordinate on Hilbert curve */
# Line 459 | Line 464 | SDmultiSamp(double t[], int n, double randX)
464                                          /* convert back to [0,1) range */
465          scale = 1. / (double)((bitmask_t)1 << nBits);
466          while (n--)
467 <                t[n] = scale * ((double)coord[n] + drand48());
467 >                t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5)));
468   }
469  
470   #undef MS_MAXDIM
# Line 472 | Line 477 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
477          SDmultiSamp(outVec, 2, randX);
478          SDsquare2disk(outVec, outVec[0], outVec[1]);
479          outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1];
480 <        if (outVec[2] > .0)             /* a bit of paranoia */
480 >        if (outVec[2] > 0)              /* a bit of paranoia */
481                  outVec[2] = sqrt(outVec[2]);
482          if (!outFront)                  /* going out back? */
483                  outVec[2] = -outVec[2];
# Line 480 | Line 485 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
485  
486   /* Query projected solid angle coverage for non-diffuse BSDF direction */
487   SDError
488 < SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd)
488 > SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2,
489 >                                int qflags, const SDData *sd)
490   {
491 <        SDSpectralDF    *rdf;
491 >        SDSpectralDF    *rdf, *tdf;
492          SDError         ec;
493          int             i;
494                                          /* check arguments */
495 <        if ((projSA == NULL) | (vec == NULL) | (sd == NULL))
495 >        if ((projSA == NULL) | (v1 == NULL) | (sd == NULL))
496                  return SDEargument;
497                                          /* initialize extrema */
498          switch (qflags) {
# Line 502 | Line 508 | SDsizeBSDF(double *projSA, const FVECT vec, int qflags
508          case 0:
509                  return SDEargument;
510          }
511 <        if (vec[2] > .0)                /* front surface query? */
511 >        if (v1[2] > 0)                  /* front surface query? */
512                  rdf = sd->rf;
513          else
514                  rdf = sd->rb;
515 +        tdf = sd->tf;
516 +        if (v2 != NULL)                 /* bidirectional? */
517 +                if (v1[2] > 0 ^ v2[2] > 0)
518 +                        rdf = NULL;
519 +                else
520 +                        tdf = NULL;
521          ec = SDEdata;                   /* run through components */
522          for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
523 <                ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags,
524 <                                                        rdf->comp[i].dist);
523 >                ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
524 >                                                qflags, &rdf->comp[i]);
525                  if (ec)
526                          return ec;
527          }
528 <        for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) {
529 <                ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags,
530 <                                                        sd->tf->comp[i].dist);
528 >        for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
529 >                ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
530 >                                                qflags, &tdf->comp[i]);
531                  if (ec)
532                          return ec;
533          }
# Line 539 | Line 551 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
551          if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL))
552                  return SDEargument;
553                                          /* whose side are we on? */
554 <        inFront = (inVec[2] > .0);
555 <        outFront = (outVec[2] > .0);
554 >        inFront = (inVec[2] > 0);
555 >        outFront = (outVec[2] > 0);
556                                          /* start with diffuse portion */
557          if (inFront & outFront) {
558                  *sv = sd->rLambFront;
# Line 557 | Line 569 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
569          i = (sdf != NULL) ? sdf->ncomp : 0;
570          while (i-- > 0) {
571                  nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
572 <                                                        sdf->comp[i].dist);
572 >                                                        &sdf->comp[i]);
573                  while (nch-- > 0) {
574                          c_cmix(&sv->spec, sv->cieY, &sv->spec,
575                                          coef[nch], &sdf->comp[i].cspec[nch]);
# Line 581 | Line 593 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
593          if ((inVec == NULL) | (sd == NULL))
594                  return .0;
595                                          /* gather diffuse components */
596 <        if (inVec[2] > .0) {
596 >        if (inVec[2] > 0) {
597                  hsum = sd->rLambFront.cieY;
598                  rdf = sd->rf;
599          } else /* !inFront */ {
# Line 612 | Line 624 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
624  
625   /* Sample BSDF direction based on the given random variable */
626   SDError
627 < SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
616 <                        double randX, int sflags, const SDData *sd)
627 > SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
628   {
629          SDError         ec;
630 +        FVECT           inVec;
631          int             inFront;
632          SDSpectralDF    *rdf;
633          double          rdiff;
# Line 624 | Line 636 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
636          SDComponent     *sdc;
637          const SDCDst    **cdarr = NULL;
638                                          /* check arguments */
639 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
640 <                        (randX < .0) | (randX >= 1.))
639 >        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
640 >                        (randX < 0) | (randX >= 1.))
641                  return SDEargument;
642                                          /* whose side are we on? */
643 <        inFront = (inVec[2] > .0);
643 >        VCOPY(inVec, ioVec);
644 >        inFront = (inVec[2] > 0);
645                                          /* remember diffuse portions */
646          if (inFront) {
647                  *sv = sd->rLambFront;
# Line 668 | Line 681 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
681          }
682          if (sv->cieY <= 1e-7) {         /* anything to sample? */
683                  sv->cieY = .0;
684 <                memset(outVec, 0, 3*sizeof(double));
684 >                memset(ioVec, 0, 3*sizeof(double));
685                  return SDEnone;
686          }
687                                          /* scale random variable */
688          randX *= sv->cieY;
689                                          /* diffuse reflection? */
690          if (randX < rdiff) {
691 <                SDdiffuseSamp(outVec, inFront, randX/rdiff);
691 >                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
692                  goto done;
693          }
694          randX -= rdiff;
# Line 683 | Line 696 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
696          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
697                  if (randX < sd->tLamb.cieY) {
698                          sv->spec = sd->tLamb.spec;
699 <                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
699 >                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
700                          goto done;
701                  }
702                  randX -= sd->tLamb.cieY;
# Line 695 | Line 708 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
708                  return SDEinternal;
709                                          /* compute sample direction */
710          sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
711 <        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
711 >        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
712          if (ec)
713                  return ec;
714                                          /* compute color */
715 <        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
715 >        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
716          if (j <= 0) {
717                  sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
718                                  sd->name);
# Line 726 | Line 739 | SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const
739          if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL))
740                  return SDEargument;
741          VCOPY(vMtx[2], sNrm);
742 <        if (normalize(vMtx[2]) == .0)
742 >        if (normalize(vMtx[2]) == 0)
743                  return SDEargument;
744          fcross(vMtx[0], uVec, vMtx[2]);
745 <        if (normalize(vMtx[0]) == .0)
745 >        if (normalize(vMtx[0]) == 0)
746                  return SDEargument;
747          fcross(vMtx[1], vMtx[2], vMtx[0]);
748          return SDEnone;
# Line 749 | Line 762 | SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3])
762          mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1];
763          mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2];
764          d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2];
765 <        if (d == .0) {
765 >        if (d == 0) {
766                  strcpy(SDerrorDetail, "Zero determinant in matrix inversion");
767                  return SDEargument;
768          }
# Line 776 | Line 789 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
789          if (vMtx == NULL) {             /* assume they just want to normalize */
790                  if (resVec != inpVec)
791                          VCOPY(resVec, inpVec);
792 <                return (normalize(resVec) > .0) ? SDEnone : SDEargument;
792 >                return (normalize(resVec) > 0) ? SDEnone : SDEargument;
793          }
794          vTmp[0] = DOT(vMtx[0], inpVec);
795          vTmp[1] = DOT(vMtx[1], inpVec);
796          vTmp[2] = DOT(vMtx[2], inpVec);
797 <        if (normalize(vTmp) == .0)
797 >        if (normalize(vTmp) == 0)
798                  return SDEargument;
799          VCOPY(resVec, vTmp);
800          return SDEnone;
# Line 853 | Line 866 | static int     nabases = 3;    /* current number of defined b
866   static int
867   fequal(double a, double b)
868   {
869 <        if (b != .0)
869 >        if (b != 0)
870                  a = a/b - 1.;
871          return((a <= 1e-6) & (a >= -1e-6));
872   }
# Line 1171 | Line 1184 | check_bsdf_data(       /* check that BSDF data is sane */
1184          hemi_total = .0;
1185          for (i = dp->ninc; i--; ) {
1186                  dom = getBSDF_incohm(dp,i);
1187 <                if (dom <= .0) {
1187 >                if (dom <= 0) {
1188                          error(WARNING, "zero/negative incoming solid angle");
1189                          continue;
1190                  }
# Line 1194 | Line 1207 | check_bsdf_data(       /* check that BSDF data is sane */
1207          hemi_total = .0;
1208          for (o = dp->nout; o--; ) {
1209                  dom = getBSDF_outohm(dp,o);
1210 <                if (dom <= .0) {
1210 >                if (dom <= 0) {
1211                          error(WARNING, "zero/negative outgoing solid angle");
1212                          continue;
1213                  }
# Line 1218 | Line 1231 | check_bsdf_data(       /* check that BSDF data is sane */
1231                  hemi_total = .0;
1232                  for (o = dp->nout; o--; ) {
1233                          double  f = BSDF_value(dp,i,o);
1234 <                        if (f >= .0)
1234 >                        if (f >= 0)
1235                                  hemi_total += f*omega_oarr[o];
1236                          else {
1237                                  nneg += (f < -FTINY);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines