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.21 by greg, Sun Apr 17 17:45:13 2011 UTC vs.
Revision 2.24 by greg, Sun Apr 24 19:39:21 2011 UTC

# Line 99 | 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 405 | Line 405 | SDfreeCache(const SDData *sd)
405  
406   /* Sample an individual BSDF component */
407   SDError
408 < SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
409 <                        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->dist);
437          if (n <= 0) {
438                  strcpy(SDerrorDetail, "BSDF sample value error");
439                  return SDEinternal;
# Line 480 | 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 488 | 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))
500                  return SDEargument;
501                                          /* initialize extrema */
502          switch (qflags) {
# Line 510 | 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 = NULL;                     /* transmitted component? */
520 +        if (v2 != NULL && v1[2] > 0 ^ v2[2] > 0) {
521 +                rdf = NULL;
522 +                tdf = sd->tf;
523 +        }
524          ec = SDEdata;                   /* run through components */
525          for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
526 <                ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags,
527 <                                                        rdf->comp[i].dist);
526 >                ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
527 >                                                qflags, rdf->comp[i].dist);
528                  if (ec)
529                          return ec;
530          }
531 <        for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) {
532 <                ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags,
533 <                                                        sd->tf->comp[i].dist);
531 >        for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
532 >                ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
533 >                                                qflags, tdf->comp[i].dist);
534                  if (ec)
535                          return ec;
536          }
# Line 547 | Line 554 | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVEC
554          if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL))
555                  return SDEargument;
556                                          /* whose side are we on? */
557 <        inFront = (inVec[2] > .0);
558 <        outFront = (outVec[2] > .0);
557 >        inFront = (inVec[2] > 0);
558 >        outFront = (outVec[2] > 0);
559                                          /* start with diffuse portion */
560          if (inFront & outFront) {
561                  *sv = sd->rLambFront;
# Line 589 | Line 596 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
596          if ((inVec == NULL) | (sd == NULL))
597                  return .0;
598                                          /* gather diffuse components */
599 <        if (inVec[2] > .0) {
599 >        if (inVec[2] > 0) {
600                  hsum = sd->rLambFront.cieY;
601                  rdf = sd->rf;
602          } else /* !inFront */ {
# Line 620 | Line 627 | SDdirectHemi(const FVECT inVec, int sflags, const SDDa
627  
628   /* Sample BSDF direction based on the given random variable */
629   SDError
630 < SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
624 <                        double randX, int sflags, const SDData *sd)
630 > SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
631   {
632          SDError         ec;
633 +        FVECT           inVec;
634          int             inFront;
635          SDSpectralDF    *rdf;
636          double          rdiff;
# Line 632 | Line 639 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
639          SDComponent     *sdc;
640          const SDCDst    **cdarr = NULL;
641                                          /* check arguments */
642 <        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
643 <                        (randX < .0) | (randX >= 1.))
642 >        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
643 >                        (randX < 0) | (randX >= 1.))
644                  return SDEargument;
645                                          /* whose side are we on? */
646 <        inFront = (inVec[2] > .0);
646 >        VCOPY(inVec, ioVec);
647 >        inFront = (inVec[2] > 0);
648                                          /* remember diffuse portions */
649          if (inFront) {
650                  *sv = sd->rLambFront;
# Line 676 | Line 684 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
684          }
685          if (sv->cieY <= 1e-7) {         /* anything to sample? */
686                  sv->cieY = .0;
687 <                memset(outVec, 0, 3*sizeof(double));
687 >                memset(ioVec, 0, 3*sizeof(double));
688                  return SDEnone;
689          }
690                                          /* scale random variable */
691          randX *= sv->cieY;
692                                          /* diffuse reflection? */
693          if (randX < rdiff) {
694 <                SDdiffuseSamp(outVec, inFront, randX/rdiff);
694 >                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
695                  goto done;
696          }
697          randX -= rdiff;
# Line 691 | Line 699 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
699          if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
700                  if (randX < sd->tLamb.cieY) {
701                          sv->spec = sd->tLamb.spec;
702 <                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
702 >                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
703                          goto done;
704                  }
705                  randX -= sd->tLamb.cieY;
# Line 703 | Line 711 | SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVe
711                  return SDEinternal;
712                                          /* compute sample direction */
713          sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
714 <        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
714 >        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
715          if (ec)
716                  return ec;
717                                          /* compute color */
718 <        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
718 >        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc->dist);
719          if (j <= 0) {
720                  sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
721                                  sd->name);
# Line 734 | Line 742 | SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const
742          if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL))
743                  return SDEargument;
744          VCOPY(vMtx[2], sNrm);
745 <        if (normalize(vMtx[2]) == .0)
745 >        if (normalize(vMtx[2]) == 0)
746                  return SDEargument;
747          fcross(vMtx[0], uVec, vMtx[2]);
748 <        if (normalize(vMtx[0]) == .0)
748 >        if (normalize(vMtx[0]) == 0)
749                  return SDEargument;
750          fcross(vMtx[1], vMtx[2], vMtx[0]);
751          return SDEnone;
# Line 757 | Line 765 | SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3])
765          mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1];
766          mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2];
767          d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2];
768 <        if (d == .0) {
768 >        if (d == 0) {
769                  strcpy(SDerrorDetail, "Zero determinant in matrix inversion");
770                  return SDEargument;
771          }
# Line 784 | Line 792 | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT i
792          if (vMtx == NULL) {             /* assume they just want to normalize */
793                  if (resVec != inpVec)
794                          VCOPY(resVec, inpVec);
795 <                return (normalize(resVec) > .0) ? SDEnone : SDEargument;
795 >                return (normalize(resVec) > 0) ? SDEnone : SDEargument;
796          }
797          vTmp[0] = DOT(vMtx[0], inpVec);
798          vTmp[1] = DOT(vMtx[1], inpVec);
799          vTmp[2] = DOT(vMtx[2], inpVec);
800 <        if (normalize(vTmp) == .0)
800 >        if (normalize(vTmp) == 0)
801                  return SDEargument;
802          VCOPY(resVec, vTmp);
803          return SDEnone;
# Line 861 | Line 869 | static int     nabases = 3;    /* current number of defined b
869   static int
870   fequal(double a, double b)
871   {
872 <        if (b != .0)
872 >        if (b != 0)
873                  a = a/b - 1.;
874          return((a <= 1e-6) & (a >= -1e-6));
875   }
# Line 1179 | Line 1187 | check_bsdf_data(       /* check that BSDF data is sane */
1187          hemi_total = .0;
1188          for (i = dp->ninc; i--; ) {
1189                  dom = getBSDF_incohm(dp,i);
1190 <                if (dom <= .0) {
1190 >                if (dom <= 0) {
1191                          error(WARNING, "zero/negative incoming solid angle");
1192                          continue;
1193                  }
# Line 1202 | Line 1210 | check_bsdf_data(       /* check that BSDF data is sane */
1210          hemi_total = .0;
1211          for (o = dp->nout; o--; ) {
1212                  dom = getBSDF_outohm(dp,o);
1213 <                if (dom <= .0) {
1213 >                if (dom <= 0) {
1214                          error(WARNING, "zero/negative outgoing solid angle");
1215                          continue;
1216                  }
# Line 1226 | Line 1234 | check_bsdf_data(       /* check that BSDF data is sane */
1234                  hemi_total = .0;
1235                  for (o = dp->nout; o--; ) {
1236                          double  f = BSDF_value(dp,i,o);
1237 <                        if (f >= .0)
1237 >                        if (f >= 0)
1238                                  hemi_total += f*omega_oarr[o];
1239                          else {
1240                                  nneg += (f < -FTINY);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines