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.18 by greg, Fri Apr 8 23:23:28 2011 UTC vs.
Revision 2.29 by greg, Fri Jun 3 18:12:58 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines