ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
(Generate patch)

Comparing ray/src/rt/m_bsdf.c (file contents):
Revision 2.39 by greg, Fri Jun 2 18:10:11 2017 UTC vs.
Revision 2.42 by greg, Tue Nov 28 22:17:00 2017 UTC

# Line 37 | Line 37 | static const char RCSid[] = "$Id$";
37   *  Reflection is ignored on the hidden side, as those rays pass through.
38   *      When thickness is set to zero, shadow rays will be blocked unless
39   *  a BTDF has a strong "through" component in the source direction.
40 < *  A separate test prevents over-counting by dropping specular & ambient
41 < *  samples that are too close to this "through" direction.  The same
42 < *  restriction applies for the proxy case (thickness != 0).
40 > *  A separate test prevents over-counting by dropping samples that are
41 > *  too close to this "through" direction.  BSDFs with such a through direction
42 > *  will also have a view component, meaning they are somewhat see-through.
43   *      The "up" vector for the BSDF is given by three variables, defined
44   *  (along with the thickness) by the named function file, or '.' if none.
45   *  Together with the surface normal, this defines the local coordinate
# Line 111 | Line 111 | compute_through(BSDFDAT *ndp)
111          FVECT           pdir;
112          double          tomega, srchrad;
113          COLOR           vpeak, vsum;
114 <        int             nsum, i;
114 >        int             i;
115          SDError         ec;
116  
117 <        setcolor(ndp->cthru, .0, .0, .0);       /* starting assumption */
117 >        setcolor(ndp->cthru, 0, 0, 0);          /* starting assumption */
118  
119        if (!(ndp->pr->crtype & (SPECULAR|AMBIENT|SHADOW)))
120                return;                         /* simply don't need to know */
121
119          if (ndp->pr->rod > 0)
120                  dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
121          else
# Line 129 | Line 126 | compute_through(BSDFDAT *ndp)
126          if (bright(ndp->pr->pcol) <= FTINY)
127                  return;                         /* pattern is black, here */
128          srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
129 <        setcolor(vpeak, .0, .0, .0);
130 <        setcolor(vsum, .0, .0, .0);
131 <        nsum = 0;
129 >        setcolor(vpeak, 0, 0, 0);
130 >        setcolor(vsum, 0, 0, 0);
131 >        pdir[2] = 0.0;
132          for (i = 0; i < NDIR2CHECK; i++) {
133                  FVECT   tdir;
134                  SDValue sv;
# Line 145 | Line 142 | compute_through(BSDFDAT *ndp)
142                          goto baderror;
143                  cvt_sdcolor(vcol, &sv);
144                  addcolor(vsum, vcol);
148                ++nsum;
145                  if (bright(vcol) > bright(vpeak)) {
146                          copycolor(vpeak, vcol);
147                          VCOPY(pdir, tdir);
148                  }
149          }
150 +        if (pdir[2] == 0.0)
151 +                return;                         /* zero neighborhood */
152          ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
153          if (ec)
154                  goto baderror;
155          if (tomega > 1.5*dfp->minProjSA)
156                  return;                         /* not really a peak? */
157 <        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007)
158 <                return;                         /* < 0.7% transmission */
157 >        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
158 >                return;                         /* < 0.1% transmission */
159          for (i = 3; i--; )                      /* remove peak from average */
160                  colval(vsum,i) -= colval(vpeak,i);
161 <        --nsum;
164 <        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
161 >        if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
162                  return;                         /* not peaky enough */
163          copycolor(ndp->cthru, vpeak);           /* else use it */
164          scalecolor(ndp->cthru, tomega);
# Line 200 | Line 197 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
197          SDError ec;
198          int     i;
199                                          /* in case we fail */
200 <        setcolor(cval, .0, .0, .0);
200 >        setcolor(cval,  0, 0, 0);
201                                          /* transform source direction */
202          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
203                  return(0);
# Line 226 | Line 223 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
223                  diffY = sv.cieY *= 1./PI;
224                  cvt_sdcolor(cdiff, &sv);
225          } else {
226 <                diffY = .0;
227 <                setcolor(cdiff, .0, .0, .0);
226 >                diffY = 0;
227 >                setcolor(cdiff,  0, 0, 0);
228          }
229                                          /* need projected solid angles */
230          omega *= fabs(vsrc[2]);
# Line 235 | Line 232 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
232          if (ec)
233                  goto baderror;
234                                          /* check indirect over-counting */
235 <        if (ndp->pr->crtype & (SPECULAR|AMBIENT)
239 <                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)
240 <                                && bright(ndp->cthru) > FTINY) {
235 >        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
236                  double  dx = vsrc[0] + ndp->vray[0];
237                  double  dy = vsrc[1] + ndp->vray[1];
238                  if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
# Line 246 | Line 241 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
241          }
242                                          /* assign number of samples */
243          sf = specjitter * ndp->pr->rweight;
244 <        if (tomega <= .0)
244 >        if (tomega <= 0)
245                  nsamp = 1;
246          else if (25.*tomega <= omega)
247                  nsamp = 100.*sf + .5;
# Line 287 | Line 282 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
282          scalecolor(cval, sf);
283                                          /* subtract diffuse contribution */
284          for (i = 3*(diffY > FTINY); i--; )
285 <                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
286 <                        colval(cval,i) = .0;
285 >                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
286 >                        colval(cval,i) = 0;
287          return(1);
288   baderror:
289          objerror(ndp->mp, USER, transSDError(ec));
# Line 309 | Line 304 | dir_bsdf(
304          double          dtmp;
305          COLOR           ctmp;
306  
307 <        setcolor(cval, .0, .0, .0);
307 >        setcolor(cval,  0, 0, 0);
308  
309          ldot = DOT(np->pnorm, ldir);
310          if ((-FTINY <= ldot) & (ldot <= FTINY))
# Line 363 | Line 358 | dir_brdf(
358          double          dtmp;
359          COLOR           ctmp, ctmp1, ctmp2;
360  
361 <        setcolor(cval, .0, .0, .0);
361 >        setcolor(cval,  0, 0, 0);
362  
363          ldot = DOT(np->pnorm, ldir);
364          
# Line 405 | Line 400 | dir_btdf(
400          double          dtmp;
401          COLOR           ctmp;
402  
403 <        setcolor(cval, .0, .0, .0);
403 >        setcolor(cval,  0, 0, 0);
404  
405          ldot = DOT(np->pnorm, ldir);
406  
# Line 437 | Line 432 | dir_btdf(
432  
433   /* Sample separate BSDF component */
434   static int
435 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
435 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
436   {
437 <        int     nstarget = 1;
438 <        int     nsent;
439 <        SDError ec;
440 <        SDValue bsv;
441 <        double  xrand;
442 <        FVECT   vsmp;
443 <        RAY     sr;
437 >        const int       hasthru = (xmit &&
438 >                                !(ndp->pr->crtype & (SPECULAR|AMBIENT)) &&
439 >                                bright(ndp->cthru) > FTINY);
440 >        int             nstarget = 1;
441 >        int             nsent = 0;
442 >        int             n;
443 >        SDError         ec;
444 >        SDValue         bsv;
445 >        double          xrand;
446 >        FVECT           vsmp, vinc;
447 >        RAY             sr;
448                                                  /* multiple samples? */
449          if (specjitter > 1.5) {
450                  nstarget = specjitter*ndp->pr->rweight + .5;
451                  nstarget += !nstarget;
452          }
453                                                  /* run through our samples */
454 <        for (nsent = 0; nsent < nstarget; nsent++) {
454 >        for (n = 0; n < nstarget; n++) {
455                  if (nstarget == 1) {            /* stratify random variable */
456                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
457                          if (specjitter < 1.)
458                                  xrand = .5 + specjitter*(xrand-.5);
459                  } else {
460 <                        xrand = (nsent + frandom())/(double)nstarget;
460 >                        xrand = (n + frandom())/(double)nstarget;
461                  }
462                  SDerrorDetail[0] = '\0';        /* sample direction & coef. */
463                  bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
464 +                VCOPY(vinc, vsmp);              /* to compare after */
465                  ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
466                  if (ec)
467                          objerror(ndp->mp, USER, transSDError(ec));
468                  if (bsv.cieY <= FTINY)          /* zero component? */
469                          break;
470 <                                                /* map vector to world */
470 >                if (hasthru) {                  /* check for view ray */
471 >                        double  dx = vinc[0] + vsmp[0];
472 >                        double  dy = vinc[1] + vsmp[1];
473 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
474 >                                continue;       /* exclude view sample */
475 >                }
476 >                                                /* map non-view sample->world */
477                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
478                          break;
479                                                  /* spawn a specular ray */
480                  if (nstarget > 1)
481                          bsv.cieY /= (double)nstarget;
482                  cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
483 <                if (usepat)                     /* apply pattern? */
483 >                if (xmit)                       /* apply pattern on transmit */
484                          multcolor(sr.rcoef, ndp->pr->pcol);
485                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
486                          if (maxdepth > 0)
487                                  break;
488                          continue;               /* Russian roulette victim */
489                  }
490 <                                                /* need to offset origin? */
485 <                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
490 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
491                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
492                  rayvalue(&sr);                  /* send & evaluate sample */
493                  multcolor(sr.rcol, sr.rcoef);
494                  addcolor(ndp->pr->rcol, sr.rcol);
495 +                ++nsent;
496          }
497          return(nsent);
498   }
# Line 495 | Line 501 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
501   static int
502   sample_sdf(BSDFDAT *ndp, int sflags)
503   {
504 +        int             hasthru = (sflags == SDsampSpT
505 +                                    && !(ndp->pr->crtype & (SPECULAR|AMBIENT))
506 +                                    && bright(ndp->cthru) > FTINY);
507          int             n, ntotal = 0;
508 +        double          b = 0;
509          SDSpectralDF    *dfp;
510          COLORV          *unsc;
511  
# Line 512 | Line 522 | sample_sdf(BSDFDAT *ndp, int sflags)
522                  else
523                          dfp = ndp->sd->rb;
524          }
525 <        setcolor(unsc, 0., 0., 0.);
525 >        setcolor(unsc,  0, 0, 0);
526          if (dfp == NULL)                        /* no specular component? */
527                  return(0);
528 <                                                /* below sampling threshold? */
529 <        if (dfp->maxHemi <= specthresh+FTINY) {
530 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
531 <                        FVECT   vjit;
532 <                        double  d;
533 <                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
534 <                        d = SDdirectHemi(vjit, sflags, ndp->sd);
528 >
529 >        if (hasthru) {                          /* separate view sample? */
530 >                RAY     tr;
531 >                if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
532 >                        VCOPY(tr.rdir, ndp->pr->rdir);
533 >                        rayvalue(&tr);
534 >                        multcolor(tr.rcol, tr.rcoef);
535 >                        addcolor(ndp->pr->rcol, tr.rcol);
536 >                        ++ntotal;
537 >                        b = bright(ndp->cthru);
538 >                } else
539 >                        hasthru = 0;
540 >        }
541 >        if (dfp->maxHemi - b <= FTINY) {        /* how specular to sample? */
542 >                b = 0;
543 >        } else {
544 >                FVECT   vjit;
545 >                bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
546 >                b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
547 >                if (b < 0) b = 0;
548 >        }
549 >        if (b <= specthresh+FTINY) {            /* below sampling threshold? */
550 >                if (b > FTINY) {                /* XXX no color from BSDF */
551                          if (sflags == SDsampSpT) {
552                                  copycolor(unsc, ndp->pr->pcol);
553 <                                scalecolor(unsc, d);
553 >                                scalecolor(unsc, b);
554                          } else                  /* no pattern on reflection */
555 <                                setcolor(unsc, d, d, d);
555 >                                setcolor(unsc, b, b, b);
556                  }
557 <                return(0);
557 >                return(ntotal);
558          }
559 <                                                /* else need to sample */
560 <        dimlist[ndims++] = (int)(size_t)ndp->mp;
535 <        ndims++;
559 >        dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */
560 >        ndims += 2;
561          for (n = dfp->ncomp; n--; ) {           /* loop over components */
562                  dimlist[ndims-1] = n + 9438;
563                  ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
# Line 563 | Line 588 | m_bsdf(OBJREC *m, RAY *r)
588                                                  /* get thickness */
589          nd.thick = evalue(mf->ep[0]);
590          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
591 <                nd.thick = .0;
591 >                nd.thick = 0;
592                                                  /* check backface visibility */
593          if (!hitfront & !backvis) {
594                  raytrans(r);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines