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.47 by greg, Mon Feb 12 23:20:31 2018 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 106 | Line 106 | compute_through(BSDFDAT *ndp)
106                                          {0, -1.6},
107                                          {1.6, 0},
108                                  };
109 <        const double    peak_over = 2.0;
109 >        const double    peak_over = 1.5;
110          SDSpectralDF    *dfp;
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);
145 <                ++nsum;
149 <                if (bright(vcol) > bright(vpeak)) {
145 >                if (sv.cieY > 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 >        tomega /= fabs(pdir[2]);                /* remove cosine factor */
158 >        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
159 >                return;                         /* < 0.1% transmission */
160          for (i = 3; i--; )                      /* remove peak from average */
161                  colval(vsum,i) -= colval(vpeak,i);
162 <        --nsum;
164 <        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
162 >        if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
163                  return;                         /* not peaky enough */
164          copycolor(ndp->cthru, vpeak);           /* else use it */
165          scalecolor(ndp->cthru, tomega);
# Line 190 | Line 188 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
188   static int
189   direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
190   {
191 <        int     nsamp, ok = 0;
191 >        int     nsamp;
192 >        double  wtot = 0;
193          FVECT   vsrc, vsmp, vjit;
194          double  tomega, tomega2;
195          double  sf, tsr, sd[2];
# Line 200 | Line 199 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
199          SDError ec;
200          int     i;
201                                          /* in case we fail */
202 <        setcolor(cval, .0, .0, .0);
202 >        setcolor(cval,  0, 0, 0);
203                                          /* transform source direction */
204          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
205                  return(0);
# Line 226 | Line 225 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
225                  diffY = sv.cieY *= 1./PI;
226                  cvt_sdcolor(cdiff, &sv);
227          } else {
228 <                diffY = .0;
229 <                setcolor(cdiff, .0, .0, .0);
228 >                diffY = 0;
229 >                setcolor(cdiff,  0, 0, 0);
230          }
231                                          /* need projected solid angles */
232          omega *= fabs(vsrc[2]);
# Line 235 | Line 234 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
234          if (ec)
235                  goto baderror;
236                                          /* check indirect over-counting */
237 <        if (ndp->pr->crtype & (SPECULAR|AMBIENT)
239 <                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)
240 <                                && bright(ndp->cthru) > FTINY) {
237 >        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
238                  double  dx = vsrc[0] + ndp->vray[0];
239                  double  dy = vsrc[1] + ndp->vray[1];
240 <                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
240 >                if (dx*dx + dy*dy <= (1.5*4./PI)*(omega + tomega +
241                                                  2.*sqrt(omega*tomega)))
242                          return(0);
243          }
244                                          /* assign number of samples */
245          sf = specjitter * ndp->pr->rweight;
246 <        if (tomega <= .0)
246 >        if (tomega <= 0)
247                  nsamp = 1;
248          else if (25.*tomega <= omega)
249                  nsamp = 100.*sf + .5;
# Line 277 | Line 274 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
274                  if (tomega2 < .12*tomega)
275                          continue;       /* not safe to include */
276                  cvt_sdcolor(csmp, &sv);
277 <                addcolor(cval, csmp);   /* else average it in */
278 <                ++ok;
277 >
278 >                if (sf < 2.5*tsr) {     /* weight by Y for small sources */
279 >                        scalecolor(csmp, sv.cieY);
280 >                        wtot += sv.cieY;
281 >                } else
282 >                        wtot += 1.;
283 >                addcolor(cval, csmp);
284          }
285 <        if (!ok)                        /* no valid specular samples? */
285 >        if (wtot <= FTINY)              /* no valid specular samples? */
286                  return(0);
287  
288 <        sf = 1./(double)ok;             /* compute average BSDF */
288 >        sf = 1./wtot;                   /* weighted average BSDF */
289          scalecolor(cval, sf);
290                                          /* subtract diffuse contribution */
291          for (i = 3*(diffY > FTINY); i--; )
292 <                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
293 <                        colval(cval,i) = .0;
292 >                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
293 >                        colval(cval,i) = 0;
294          return(1);
295   baderror:
296          objerror(ndp->mp, USER, transSDError(ec));
# Line 309 | Line 311 | dir_bsdf(
311          double          dtmp;
312          COLOR           ctmp;
313  
314 <        setcolor(cval, .0, .0, .0);
314 >        setcolor(cval,  0, 0, 0);
315  
316          ldot = DOT(np->pnorm, ldir);
317          if ((-FTINY <= ldot) & (ldot <= FTINY))
# Line 363 | Line 365 | dir_brdf(
365          double          dtmp;
366          COLOR           ctmp, ctmp1, ctmp2;
367  
368 <        setcolor(cval, .0, .0, .0);
368 >        setcolor(cval,  0, 0, 0);
369  
370          ldot = DOT(np->pnorm, ldir);
371          
# Line 405 | Line 407 | dir_btdf(
407          double          dtmp;
408          COLOR           ctmp;
409  
410 <        setcolor(cval, .0, .0, .0);
410 >        setcolor(cval,  0, 0, 0);
411  
412          ldot = DOT(np->pnorm, ldir);
413  
# Line 437 | Line 439 | dir_btdf(
439  
440   /* Sample separate BSDF component */
441   static int
442 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
442 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
443   {
444 <        int     nstarget = 1;
445 <        int     nsent;
446 <        SDError ec;
447 <        SDValue bsv;
448 <        double  xrand;
449 <        FVECT   vsmp;
450 <        RAY     sr;
444 >        const int       hasthru = (xmit &&
445 >                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
446 >                                        && bright(ndp->cthru) > FTINY);
447 >        int             nstarget = 1;
448 >        int             nsent = 0;
449 >        int             n;
450 >        SDError         ec;
451 >        SDValue         bsv;
452 >        double          xrand;
453 >        FVECT           vsmp, vinc;
454 >        RAY             sr;
455                                                  /* multiple samples? */
456          if (specjitter > 1.5) {
457                  nstarget = specjitter*ndp->pr->rweight + .5;
458                  nstarget += !nstarget;
459          }
460                                                  /* run through our samples */
461 <        for (nsent = 0; nsent < nstarget; nsent++) {
461 >        for (n = 0; n < nstarget; n++) {
462                  if (nstarget == 1) {            /* stratify random variable */
463                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
464                          if (specjitter < 1.)
465                                  xrand = .5 + specjitter*(xrand-.5);
466                  } else {
467 <                        xrand = (nsent + frandom())/(double)nstarget;
467 >                        xrand = (n + frandom())/(double)nstarget;
468                  }
469                  SDerrorDetail[0] = '\0';        /* sample direction & coef. */
470                  bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
471 +                VCOPY(vinc, vsmp);              /* to compare after */
472                  ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
473                  if (ec)
474                          objerror(ndp->mp, USER, transSDError(ec));
475                  if (bsv.cieY <= FTINY)          /* zero component? */
476                          break;
477 <                                                /* map vector to world */
477 >                if (hasthru) {                  /* check for view ray */
478 >                        double  dx = vinc[0] + vsmp[0];
479 >                        double  dy = vinc[1] + vsmp[1];
480 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
481 >                                continue;       /* exclude view sample */
482 >                }
483 >                                                /* map non-view sample->world */
484                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
485                          break;
486                                                  /* spawn a specular ray */
487                  if (nstarget > 1)
488                          bsv.cieY /= (double)nstarget;
489                  cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
490 <                if (usepat)                     /* apply pattern? */
490 >                if (xmit)                       /* apply pattern on transmit */
491                          multcolor(sr.rcoef, ndp->pr->pcol);
492                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
493                          if (maxdepth > 0)
494                                  break;
495                          continue;               /* Russian roulette victim */
496                  }
497 <                                                /* need to offset origin? */
485 <                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
497 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
498                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
499                  rayvalue(&sr);                  /* send & evaluate sample */
500                  multcolor(sr.rcol, sr.rcoef);
501                  addcolor(ndp->pr->rcol, sr.rcol);
502 +                ++nsent;
503          }
504          return(nsent);
505   }
# Line 495 | Line 508 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
508   static int
509   sample_sdf(BSDFDAT *ndp, int sflags)
510   {
511 +        int             hasthru = (sflags == SDsampSpT &&
512 +                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
513 +                                        && bright(ndp->cthru) > FTINY);
514          int             n, ntotal = 0;
515 +        double          b = 0;
516          SDSpectralDF    *dfp;
517          COLORV          *unsc;
518  
# Line 512 | Line 529 | sample_sdf(BSDFDAT *ndp, int sflags)
529                  else
530                          dfp = ndp->sd->rb;
531          }
532 <        setcolor(unsc, 0., 0., 0.);
532 >        setcolor(unsc,  0, 0, 0);
533          if (dfp == NULL)                        /* no specular component? */
534                  return(0);
535 <                                                /* below sampling threshold? */
536 <        if (dfp->maxHemi <= specthresh+FTINY) {
537 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
538 <                        FVECT   vjit;
539 <                        double  d;
540 <                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
541 <                        d = SDdirectHemi(vjit, sflags, ndp->sd);
535 >
536 >        if (hasthru) {                          /* separate view sample? */
537 >                RAY     tr;
538 >                if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
539 >                        VCOPY(tr.rdir, ndp->pr->rdir);
540 >                        rayvalue(&tr);
541 >                        multcolor(tr.rcol, tr.rcoef);
542 >                        addcolor(ndp->pr->rcol, tr.rcol);
543 >                        ++ntotal;
544 >                        b = bright(ndp->cthru);
545 >                } else
546 >                        hasthru = 0;
547 >        }
548 >        if (dfp->maxHemi - b <= FTINY) {        /* have specular to sample? */
549 >                b = 0;
550 >        } else {
551 >                FVECT   vjit;
552 >                bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
553 >                b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
554 >                if (b < 0) b = 0;
555 >        }
556 >        if (b <= specthresh+FTINY) {            /* below sampling threshold? */
557 >                if (b > FTINY) {                /* XXX no color from BSDF */
558                          if (sflags == SDsampSpT) {
559                                  copycolor(unsc, ndp->pr->pcol);
560 <                                scalecolor(unsc, d);
560 >                                scalecolor(unsc, b);
561                          } else                  /* no pattern on reflection */
562 <                                setcolor(unsc, d, d, d);
562 >                                setcolor(unsc, b, b, b);
563                  }
564 <                return(0);
564 >                return(ntotal);
565          }
566 <                                                /* else need to sample */
567 <        dimlist[ndims++] = (int)(size_t)ndp->mp;
535 <        ndims++;
566 >        dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */
567 >        ndims += 2;
568          for (n = dfp->ncomp; n--; ) {           /* loop over components */
569                  dimlist[ndims-1] = n + 9438;
570                  ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
# Line 563 | Line 595 | m_bsdf(OBJREC *m, RAY *r)
595                                                  /* get thickness */
596          nd.thick = evalue(mf->ep[0]);
597          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
598 <                nd.thick = .0;
598 >                nd.thick = 0;
599                                                  /* check backface visibility */
600          if (!hitfront & !backvis) {
601                  raytrans(r);
# Line 644 | Line 676 | m_bsdf(OBJREC *m, RAY *r)
676          if (r->crtype & SHADOW) {
677                  RAY     tr;                     /* attempt to pass shadow ray */
678                  if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
679 <                        return(1);              /* blocked */
679 >                        return(1);              /* no through component */
680                  VCOPY(tr.rdir, r->rdir);
681                  rayvalue(&tr);                  /* transmit with scaling */
682                  multcolor(tr.rcol, tr.rcoef);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines