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.36 by greg, Tue May 16 20:06:40 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 79 | Line 79 | typedef struct {
79          COLOR   cthru;          /* "through" component multiplier */
80          SDData  *sd;            /* loaded BSDF data */
81          COLOR   rdiff;          /* diffuse reflection */
82 +        COLOR   runsamp;        /* BSDF hemispherical reflection */
83          COLOR   tdiff;          /* diffuse transmission */
84 +        COLOR   tunsamp;        /* BSDF hemispherical transmission */
85   }  BSDFDAT;             /* BSDF material data */
86  
87   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
# Line 104 | 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) && !(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;
124          else
# Line 124 | 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 140 | Line 145 | compute_through(BSDFDAT *ndp)
145                          goto baderror;
146                  cvt_sdcolor(vcol, &sv);
147                  addcolor(vsum, vcol);
148 <                ++nsum;
144 <                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;
159 <        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 185 | 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 194 | Line 201 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
201          SDValue sv;
202          SDError ec;
203          int     i;
204 +                                        /* in case we fail */
205 +        setcolor(cval,  0, 0, 0);
206                                          /* transform source direction */
207          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
208                  return(0);
# Line 219 | 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 <                                        /* assign number of samples */
234 >                                        /* need projected solid angles */
235 >        omega *= fabs(vsrc[2]);
236          ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
237          if (ec)
238                  goto baderror;
239                                          /* check indirect over-counting */
240 <        if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY)
231 <                                && ndp->pr->crtype & (SPECULAR|AMBIENT)
232 <                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) {
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 <= omega+tomega)
243 >                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
244 >                                                2.*sqrt(omega*tomega)))
245                          return(0);
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;
253          else
254                  nsamp = 4.*sf*omega/tomega + .5;
255          nsamp += !nsamp;
256 <        setcolor(cval, .0, .0, .0);     /* sample our source area */
247 <        sf = sqrt(omega);
256 >        sf = sqrt(omega);               /* sample our source area */
257          tsr = sqrt(tomega);
258          for (i = nsamp; i--; ) {
259                  VCOPY(vsmp, vsrc);      /* jitter query directions */
# Line 255 | Line 264 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
264                          normalize(vsmp);
265                  }
266                  bsdf_jitter(vjit, ndp, tsr);
267 +                                        /* compute BSDF */
268 +                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
269 +                if (ec)
270 +                        goto baderror;
271 +                if (sv.cieY - diffY <= FTINY)
272 +                        continue;       /* no specular part */
273                                          /* check for variable resolution */
274                  ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
275                  if (ec)
276                          goto baderror;
277                  if (tomega2 < .12*tomega)
278                          continue;       /* not safe to include */
264                                        /* else compute BSDF */
265                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
266                if (ec)
267                        goto baderror;
268                if (sv.cieY - diffY <= FTINY)
269                        continue;       /* no specular part */
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) {
289 <                setcolor(cval, .0, .0, .0);
290 <                return(0);              /* no valid specular samples */
291 <        }
278 <        sf = 1./(double)ok;
288 >        if (wtot <= FTINY)              /* no valid specular samples? */
289 >                return(0);
290 >
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 301 | 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 309 | Line 322 | dir_bsdf(
322  
323          if (ldot > 0 && bright(np->rdiff) > FTINY) {
324                  /*
325 <                 *  Compute added diffuse reflected component.
325 >                 *  Compute diffuse reflected component
326                   */
327                  copycolor(ctmp, np->rdiff);
328                  dtmp = ldot * omega * (1./PI);
# Line 318 | Line 331 | dir_bsdf(
331          }
332          if (ldot < 0 && bright(np->tdiff) > FTINY) {
333                  /*
334 <                 *  Compute added diffuse transmission.
334 >                 *  Compute diffuse transmission
335                   */
336                  copycolor(ctmp, np->tdiff);
337                  dtmp = -ldot * omega * (1.0/PI);
# Line 328 | Line 341 | dir_bsdf(
341          if (ambRayInPmap(np->pr))
342                  return;         /* specular already in photon map */
343          /*
344 <         *  Compute specular scattering coefficient using BSDF.
344 >         *  Compute specular scattering coefficient using BSDF
345           */
346          if (!direct_specular_OK(ctmp, ldir, omega, np))
347                  return;
# Line 355 | 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 364 | Line 377 | dir_brdf(
377  
378          if (bright(np->rdiff) > FTINY) {
379                  /*
380 <                 *  Compute added diffuse reflected component.
380 >                 *  Compute diffuse reflected component
381                   */
382                  copycolor(ctmp, np->rdiff);
383                  dtmp = ldot * omega * (1./PI);
# Line 374 | Line 387 | dir_brdf(
387          if (ambRayInPmap(np->pr))
388                  return;         /* specular already in photon map */
389          /*
390 <         *  Compute specular reflection coefficient using BSDF.
390 >         *  Compute specular reflection coefficient using BSDF
391           */
392          if (!direct_specular_OK(ctmp, ldir, omega, np))
393                  return;
# Line 397 | 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 406 | Line 419 | dir_btdf(
419  
420          if (bright(np->tdiff) > FTINY) {
421                  /*
422 <                 *  Compute added diffuse transmission.
422 >                 *  Compute diffuse transmission
423                   */
424                  copycolor(ctmp, np->tdiff);
425                  dtmp = -ldot * omega * (1.0/PI);
# Line 416 | Line 429 | dir_btdf(
429          if (ambRayInPmap(np->pr))
430                  return;         /* specular already in photon map */
431          /*
432 <         *  Compute specular scattering coefficient using BSDF.
432 >         *  Compute specular scattering coefficient using BSDF
433           */
434          if (!direct_specular_OK(ctmp, ldir, omega, np))
435                  return;
# Line 429 | 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? */
477 <                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 487 | 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  
519          if (sflags == SDsampSpT) {
520 <                unsc = ndp->tdiff;
520 >                unsc = ndp->tunsamp;
521                  if (ndp->pr->rod > 0)
522                          dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
523                  else
524                          dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
525          } else /* sflags == SDsampSpR */ {
526 <                unsc = ndp->rdiff;
526 >                unsc = ndp->runsamp;
527                  if (ndp->pr->rod > 0)
528                          dfp = ndp->sd->rf;
529                  else
530                          dfp = ndp->sd->rb;
531          }
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 <                        COLOR   ctmp;
541 <                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
542 <                        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(ctmp, ndp->pr->pcol);
560 <                                scalecolor(ctmp, d);
559 >                                copycolor(unsc, ndp->pr->pcol);
560 >                                scalecolor(unsc, b);
561                          } else                  /* no pattern on reflection */
562 <                                setcolor(ctmp, d, d, d);
522 <                        addcolor(unsc, ctmp);
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;
528 <        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 556 | 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 637 | 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);
# Line 663 | Line 702 | m_bsdf(OBJREC *m, RAY *r)
702                                                  /* sample transmission */
703          sample_sdf(&nd, SDsampSpT);
704                                                  /* compute indirect diffuse */
705 <        if (bright(nd.rdiff) > FTINY) {         /* ambient from reflection */
705 >        copycolor(ctmp, nd.rdiff);
706 >        addcolor(ctmp, nd.runsamp);
707 >        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
708                  if (!hitfront)
709                          flipsurface(r);
669                copycolor(ctmp, nd.rdiff);
710                  multambient(ctmp, r, nd.pnorm);
711                  addcolor(r->rcol, ctmp);
712                  if (!hitfront)
713                          flipsurface(r);
714          }
715 <        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
715 >        copycolor(ctmp, nd.tdiff);
716 >        addcolor(ctmp, nd.tunsamp);
717 >        if (bright(ctmp) > FTINY) {             /* ambient from other side */
718                  FVECT  bnorm;
719                  if (hitfront)
720                          flipsurface(r);
721                  bnorm[0] = -nd.pnorm[0];
722                  bnorm[1] = -nd.pnorm[1];
723                  bnorm[2] = -nd.pnorm[2];
682                copycolor(ctmp, nd.tdiff);
724                  if (nd.thick != 0) {            /* proxy with offset? */
725                          VCOPY(vtmp, r->rop);
726                          VSUM(r->rop, vtmp, r->ron, nd.thick);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines