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.20 by greg, Mon Apr 11 03:47:46 2011 UTC vs.
Revision 2.26 by greg, Mon Apr 25 15:48:05 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 334 | Line 338 | SDgetCache(const char *bname)
338          sdl->next = SDcacheList;
339          SDcacheList = sdl;
340  
341 <        sdl->refcnt++;
341 >        sdl->refcnt = 1;
342          return &sdl->bsdf;
343   }
344  
# Line 378 | Line 382 | SDfreeCache(const SDData *sd)
382          for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next)
383                  if (&sdl->bsdf == sd)
384                          break;
385 <        if (sdl == NULL || --sdl->refcnt)
385 >        if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0)))
386                  return;                 /* missing or still in use */
387                                          /* keep unreferenced data? */
388          if (SDisLoaded(sd) && SDretainSet) {
# Line 401 | Line 405 | SDfreeCache(const SDData *sd)
405  
406   /* Sample an individual BSDF component */
407   SDError
408 < SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
405 <                        double randX, SDComponent *sdc)
408 > SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc)
409   {
410          float           coef[SDmaxCh];
411          SDError         ec;
412 +        FVECT           inVec;
413          const SDCDst    *cd;
414          double          d;
415          int             n;
416                                          /* check arguments */
417 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL))
417 >        if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL))
418                  return SDEargument;
419                                          /* get cumulative distribution */
420 +        VCOPY(inVec, ioVec);
421          cd = (*sdc->func->getCDist)(inVec, sdc);
422          if (cd == NULL)
423                  return SDEmemory;
424          if (cd->cTotal <= 1e-7) {       /* anything to sample? */
425                  sv->spec = c_dfcolor;
426                  sv->cieY = .0;
427 <                memset(outVec, 0, 3*sizeof(double));
427 >                memset(ioVec, 0, 3*sizeof(double));
428                  return SDEnone;
429          }
430          sv->cieY = cd->cTotal;
431                                          /* compute sample direction */
432 <        ec = (*sdc->func->sampCDist)(outVec, randX, cd);
432 >        ec = (*sdc->func->sampCDist)(ioVec, randX, cd);
433          if (ec)
434                  return ec;
435                                          /* get BSDF color */
436 <        n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
436 >        n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
437          if (n <= 0) {
438                  strcpy(SDerrorDetail, "BSDF sample value error");
439                  return SDEinternal;
# Line 476 | Line 481 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
481          SDmultiSamp(outVec, 2, randX);
482          SDsquare2disk(outVec, outVec[0], outVec[1]);
483          outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1];
484 <        if (outVec[2] > .0)             /* a bit of paranoia */
484 >        if (outVec[2] > 0)              /* a bit of paranoia */
485                  outVec[2] = sqrt(outVec[2]);
486          if (!outFront)                  /* going out back? */
487                  outVec[2] = -outVec[2];
# Line 484 | Line 489 | SDdiffuseSamp(FVECT outVec, int outFront, double randX
489  
490   /* Query projected solid angle coverage for non-diffuse BSDF direction */
491   SDError
492 < SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd)
492 > SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2,
493 >                                int qflags, const SDData *sd)
494   {
495 <        SDSpectralDF    *rdf;
495 >        SDSpectralDF    *rdf, *tdf;
496          SDError         ec;
497          int             i;
498                                          /* check arguments */
499 <        if ((projSA == NULL) | (vec == NULL) | (sd == NULL))
499 >        if ((projSA == NULL) | (v1 == NULL) | (sd == NULL))
500                  return SDEargument;
501                                          /* initialize extrema */
502          switch (qflags) {
# Line 506 | Line 512 | SDsizeBSDF(double *projSA, const FVECT vec, int qflags
512          case 0:
513                  return SDEargument;
514          }
515 <        if (vec[2] > .0)                /* front surface query? */
515 >        if (v1[2] > 0)                  /* front surface query? */
516                  rdf = sd->rf;
517          else
518                  rdf = sd->rb;
519 +        tdf = sd->tf;
520 +        if (v2 != NULL)                 /* bidirectional? */
521 +                if (v1[2] > 0 ^ v2[2] > 0)
522 +                        rdf = NULL;
523 +                else
524 +                        tdf = NULL;
525          ec = SDEdata;                   /* run through components */
526          for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
527 <                ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags,
528 <                                                        rdf->comp[i].dist);
527 >                ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
528 >                                                qflags, &rdf->comp[i]);
529                  if (ec)
530                          return ec;
531          }
532 <        for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) {
533 <                ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags,
534 <                                                        sd->tf->comp[i].dist);
532 >        for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
533 >                ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
534 >                                                qflags, &tdf->comp[i]);
535                  if (ec)
536                          return ec;
537          }
# Line 543 | Line 555 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
555          if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL))
556                  return SDEargument;
557                                          /* whose side are we on? */
558 <        inFront = (inVec[2] > .0);
559 <        outFront = (outVec[2] > .0);
558 >        inFront = (inVec[2] > 0);
559 >        outFront = (outVec[2] > 0);
560                                          /* start with diffuse portion */
561          if (inFront & outFront) {
562                  *sv = sd->rLambFront;
# Line 561 | Line 573 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
573          i = (sdf != NULL) ? sdf->ncomp : 0;
574          while (i-- > 0) {
575                  nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
576 <                                                        sdf->comp[i].dist);
576 >                                                        &sdf->comp[i]);
577                  while (nch-- > 0) {
578                          c_cmix(&sv->spec, sv->cieY, &sv->spec,
579                                          coef[nch], &sdf->comp[i].cspec[nch]);
# Line 585 | Line 597 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
597          if ((inVec == NULL) | (sd == NULL))
598                  return .0;
599                                          /* gather diffuse components */
600 <        if (inVec[2] > .0) {
600 >        if (inVec[2] > 0) {
601                  hsum = sd->rLambFront.cieY;
602                  rdf = sd->rf;
603          } else /* !inFront */ {
# Line 616 | Line 628 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
628  
629   /* Sample BSDF direction based on the given random variable */
630   SDError
631 < SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
620 <                        double randX, int sflags, const SDData *sd)
631 > SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
632   {
633          SDError         ec;
634 +        FVECT           inVec;
635          int             inFront;
636          SDSpectralDF    *rdf;
637          double          rdiff;
# Line 628 | Line 640 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
640          SDComponent     *sdc;
641          const SDCDst    **cdarr = NULL;
642                                          /* check arguments */
643 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
644 <                        (randX < .0) | (randX >= 1.))
643 >        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
644 >                        (randX < 0) | (randX >= 1.))
645                  return SDEargument;
646                                          /* whose side are we on? */
647 <        inFront = (inVec[2] > .0);
647 >        VCOPY(inVec, ioVec);
648 >        inFront = (inVec[2] > 0);
649                                          /* remember diffuse portions */
650          if (inFront) {
651                  *sv = sd->rLambFront;
# Line 672 | Line 685 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
685          }
686          if (sv->cieY <= 1e-7) {         /* anything to sample? */
687                  sv->cieY = .0;
688 <                memset(outVec, 0, 3*sizeof(double));
688 >                memset(ioVec, 0, 3*sizeof(double));
689                  return SDEnone;
690          }
691                                          /* scale random variable */
692          randX *= sv->cieY;
693                                          /* diffuse reflection? */
694          if (randX < rdiff) {
695 <                SDdiffuseSamp(outVec, inFront, randX/rdiff);
695 >                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
696                  goto done;
697          }
698          randX -= rdiff;
# Line 687 | Line 700 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
700          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
701                  if (randX < sd->tLamb.cieY) {
702                          sv->spec = sd->tLamb.spec;
703 <                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
703 >                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
704                          goto done;
705                  }
706                  randX -= sd->tLamb.cieY;
# Line 699 | Line 712 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
712                  return SDEinternal;
713                                          /* compute sample direction */
714          sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
715 <        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
715 >        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
716          if (ec)
717                  return ec;
718                                          /* compute color */
719 <        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
719 >        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
720          if (j <= 0) {
721                  sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
722                                  sd->name);
# Line 730 | Line 743 | SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const
743          if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL))
744                  return SDEargument;
745          VCOPY(vMtx[2], sNrm);
746 <        if (normalize(vMtx[2]) == .0)
746 >        if (normalize(vMtx[2]) == 0)
747                  return SDEargument;
748          fcross(vMtx[0], uVec, vMtx[2]);
749 <        if (normalize(vMtx[0]) == .0)
749 >        if (normalize(vMtx[0]) == 0)
750                  return SDEargument;
751          fcross(vMtx[1], vMtx[2], vMtx[0]);
752          return SDEnone;
# Line 753 | Line 766 | SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3])
766          mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1];
767          mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2];
768          d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2];
769 <        if (d == .0) {
769 >        if (d == 0) {
770                  strcpy(SDerrorDetail, "Zero determinant in matrix inversion");
771                  return SDEargument;
772          }
# Line 780 | Line 793 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
793          if (vMtx == NULL) {             /* assume they just want to normalize */
794                  if (resVec != inpVec)
795                          VCOPY(resVec, inpVec);
796 <                return (normalize(resVec) > .0) ? SDEnone : SDEargument;
796 >                return (normalize(resVec) > 0) ? SDEnone : SDEargument;
797          }
798          vTmp[0] = DOT(vMtx[0], inpVec);
799          vTmp[1] = DOT(vMtx[1], inpVec);
800          vTmp[2] = DOT(vMtx[2], inpVec);
801 <        if (normalize(vTmp) == .0)
801 >        if (normalize(vTmp) == 0)
802                  return SDEargument;
803          VCOPY(resVec, vTmp);
804          return SDEnone;
# Line 857 | 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 1175 | 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 1198 | 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 1222 | 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