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.46 by greg, Mon Feb 12 18:46:29 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 */
119 >        if (ndp->pr->crtype & (SPECULAR|AMBIENT) && !(ndp->pr->crtype & SHADOW))
120 >                return;                         /* no need for through comp. */
121  
122          if (ndp->pr->rod > 0)
123                  dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
# Line 129 | Line 129 | compute_through(BSDFDAT *ndp)
129          if (bright(ndp->pr->pcol) <= FTINY)
130                  return;                         /* pattern is black, here */
131          srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
132 <        setcolor(vpeak, .0, .0, .0);
133 <        setcolor(vsum, .0, .0, .0);
134 <        nsum = 0;
132 >        setcolor(vpeak, 0, 0, 0);
133 >        setcolor(vsum, 0, 0, 0);
134 >        pdir[2] = 0.0;
135          for (i = 0; i < NDIR2CHECK; i++) {
136                  FVECT   tdir;
137                  SDValue sv;
# Line 145 | Line 145 | compute_through(BSDFDAT *ndp)
145                          goto baderror;
146                  cvt_sdcolor(vcol, &sv);
147                  addcolor(vsum, vcol);
148 <                ++nsum;
149 <                if (bright(vcol) > bright(vpeak)) {
148 >                if (sv.cieY > bright(vpeak)) {
149                          copycolor(vpeak, vcol);
150                          VCOPY(pdir, tdir);
151                  }
152          }
153 +        if (pdir[2] == 0.0)
154 +                return;                         /* zero neighborhood */
155          ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
156          if (ec)
157                  goto baderror;
158          if (tomega > 1.5*dfp->minProjSA)
159                  return;                         /* not really a peak? */
160 <        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007)
161 <                return;                         /* < 0.7% transmission */
160 >        tomega /= fabs(pdir[2]);                /* remove cosine factor */
161 >        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
162 >                return;                         /* < 0.1% transmission */
163          for (i = 3; i--; )                      /* remove peak from average */
164                  colval(vsum,i) -= colval(vpeak,i);
165 <        --nsum;
164 <        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
165 >        if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
166                  return;                         /* not peaky enough */
167          copycolor(ndp->cthru, vpeak);           /* else use it */
168          scalecolor(ndp->cthru, tomega);
# Line 190 | Line 191 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
191   static int
192   direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
193   {
194 <        int     nsamp, ok = 0;
194 >        int     nsamp;
195 >        double  wtot = 0;
196          FVECT   vsrc, vsmp, vjit;
197          double  tomega, tomega2;
198          double  sf, tsr, sd[2];
# Line 200 | Line 202 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
202          SDError ec;
203          int     i;
204                                          /* in case we fail */
205 <        setcolor(cval, .0, .0, .0);
205 >        setcolor(cval,  0, 0, 0);
206                                          /* transform source direction */
207          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
208                  return(0);
# Line 226 | Line 228 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
228                  diffY = sv.cieY *= 1./PI;
229                  cvt_sdcolor(cdiff, &sv);
230          } else {
231 <                diffY = .0;
232 <                setcolor(cdiff, .0, .0, .0);
231 >                diffY = 0;
232 >                setcolor(cdiff,  0, 0, 0);
233          }
234                                          /* need projected solid angles */
235          omega *= fabs(vsrc[2]);
# Line 235 | Line 237 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
237          if (ec)
238                  goto baderror;
239                                          /* check indirect over-counting */
240 <        if (ndp->pr->crtype & (SPECULAR|AMBIENT)
239 <                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)
240 <                                && bright(ndp->cthru) > FTINY) {
240 >        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
241                  double  dx = vsrc[0] + ndp->vray[0];
242                  double  dy = vsrc[1] + ndp->vray[1];
243                  if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
# Line 246 | Line 246 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
246          }
247                                          /* assign number of samples */
248          sf = specjitter * ndp->pr->rweight;
249 <        if (tomega <= .0)
249 >        if (tomega <= 0)
250                  nsamp = 1;
251          else if (25.*tomega <= omega)
252                  nsamp = 100.*sf + .5;
# Line 277 | Line 277 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
277                  if (tomega2 < .12*tomega)
278                          continue;       /* not safe to include */
279                  cvt_sdcolor(csmp, &sv);
280 <                addcolor(cval, csmp);   /* else average it in */
281 <                ++ok;
280 >
281 >                if (sf < 2.5*tsr) {     /* weight by Y for small sources */
282 >                        scalecolor(csmp, sv.cieY);
283 >                        wtot += sv.cieY;
284 >                } else
285 >                        wtot += 1.;
286 >                addcolor(cval, csmp);
287          }
288 <        if (!ok)                        /* no valid specular samples? */
288 >        if (wtot <= FTINY)              /* no valid specular samples? */
289                  return(0);
290  
291 <        sf = 1./(double)ok;             /* compute average BSDF */
291 >        sf = 1./wtot;                   /* weighted average BSDF */
292          scalecolor(cval, sf);
293                                          /* subtract diffuse contribution */
294          for (i = 3*(diffY > FTINY); i--; )
295 <                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
296 <                        colval(cval,i) = .0;
295 >                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
296 >                        colval(cval,i) = 0;
297          return(1);
298   baderror:
299          objerror(ndp->mp, USER, transSDError(ec));
# Line 309 | Line 314 | dir_bsdf(
314          double          dtmp;
315          COLOR           ctmp;
316  
317 <        setcolor(cval, .0, .0, .0);
317 >        setcolor(cval,  0, 0, 0);
318  
319          ldot = DOT(np->pnorm, ldir);
320          if ((-FTINY <= ldot) & (ldot <= FTINY))
# Line 363 | Line 368 | dir_brdf(
368          double          dtmp;
369          COLOR           ctmp, ctmp1, ctmp2;
370  
371 <        setcolor(cval, .0, .0, .0);
371 >        setcolor(cval,  0, 0, 0);
372  
373          ldot = DOT(np->pnorm, ldir);
374          
# Line 405 | Line 410 | dir_btdf(
410          double          dtmp;
411          COLOR           ctmp;
412  
413 <        setcolor(cval, .0, .0, .0);
413 >        setcolor(cval,  0, 0, 0);
414  
415          ldot = DOT(np->pnorm, ldir);
416  
# Line 437 | Line 442 | dir_btdf(
442  
443   /* Sample separate BSDF component */
444   static int
445 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
445 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
446   {
447 <        int     nstarget = 1;
448 <        int     nsent;
449 <        SDError ec;
450 <        SDValue bsv;
451 <        double  xrand;
452 <        FVECT   vsmp;
453 <        RAY     sr;
447 >        const int       hasthru = (xmit && bright(ndp->cthru) > FTINY);
448 >        int             nstarget = 1;
449 >        int             nsent = 0;
450 >        int             n;
451 >        SDError         ec;
452 >        SDValue         bsv;
453 >        double          xrand;
454 >        FVECT           vsmp, vinc;
455 >        RAY             sr;
456                                                  /* multiple samples? */
457          if (specjitter > 1.5) {
458                  nstarget = specjitter*ndp->pr->rweight + .5;
459                  nstarget += !nstarget;
460          }
461                                                  /* run through our samples */
462 <        for (nsent = 0; nsent < nstarget; nsent++) {
462 >        for (n = 0; n < nstarget; n++) {
463                  if (nstarget == 1) {            /* stratify random variable */
464                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
465                          if (specjitter < 1.)
466                                  xrand = .5 + specjitter*(xrand-.5);
467                  } else {
468 <                        xrand = (nsent + frandom())/(double)nstarget;
468 >                        xrand = (n + frandom())/(double)nstarget;
469                  }
470                  SDerrorDetail[0] = '\0';        /* sample direction & coef. */
471                  bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
472 +                VCOPY(vinc, vsmp);              /* to compare after */
473                  ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
474                  if (ec)
475                          objerror(ndp->mp, USER, transSDError(ec));
476                  if (bsv.cieY <= FTINY)          /* zero component? */
477                          break;
478 <                                                /* map vector to world */
478 >                if (hasthru) {                  /* check for view ray */
479 >                        double  dx = vinc[0] + vsmp[0];
480 >                        double  dy = vinc[1] + vsmp[1];
481 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
482 >                                continue;       /* exclude view sample */
483 >                }
484 >                                                /* map non-view sample->world */
485                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
486                          break;
487                                                  /* spawn a specular ray */
488                  if (nstarget > 1)
489                          bsv.cieY /= (double)nstarget;
490                  cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
491 <                if (usepat)                     /* apply pattern? */
491 >                if (xmit)                       /* apply pattern on transmit */
492                          multcolor(sr.rcoef, ndp->pr->pcol);
493                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
494                          if (maxdepth > 0)
495                                  break;
496                          continue;               /* Russian roulette victim */
497                  }
498 <                                                /* need to offset origin? */
485 <                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
498 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
499                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
500                  rayvalue(&sr);                  /* send & evaluate sample */
501                  multcolor(sr.rcol, sr.rcoef);
502                  addcolor(ndp->pr->rcol, sr.rcol);
503 +                ++nsent;
504          }
505          return(nsent);
506   }
# Line 495 | Line 509 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
509   static int
510   sample_sdf(BSDFDAT *ndp, int sflags)
511   {
512 +        int             hasthru = (sflags == SDsampSpT &&
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