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.35 by greg, Tue May 16 02:52:15 2017 UTC vs.
Revision 2.45 by greg, Fri Jan 5 02:47:46 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->rod > 0)
120                  dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
# Line 124 | 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 134 | Line 136 | compute_through(BSDFDAT *ndp)
136                  tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
137                  tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
138                  tdir[2] = -ndp->vray[2];
139 <                if (normalize(tdir) == 0)
138 <                        continue;
139 >                normalize(tdir);
140                  ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
141                  if (ec)
142                          goto baderror;
143                  cvt_sdcolor(vcol, &sv);
144                  addcolor(vsum, vcol);
145 <                ++nsum;
145 <                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;
160 <        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 186 | 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;
194 >        double  tomega, tomega2;
195          double  sf, tsr, sd[2];
196          COLOR   csmp, cdiff;
197          double  diffY;
198          SDValue sv;
199          SDError ec;
200          int     i;
201 +                                        /* in case we fail */
202 +        setcolor(cval,  0, 0, 0);
203                                          /* transform source direction */
204          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
205                  return(0);
# Line 220 | 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 <                                        /* assign number of samples */
231 >                                        /* need projected solid angles */
232 >        omega *= fabs(vsrc[2]);
233          ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
234          if (ec)
235                  goto baderror;
236                                          /* check indirect over-counting */
237 <        if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY)
232 <                                && ndp->pr->crtype & (SPECULAR|AMBIENT)
233 <                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) {
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 <= omega+tomega)
240 >                if (dx*dx + dy*dy <= (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;
250          else
251                  nsamp = 4.*sf*omega/tomega + .5;
252          nsamp += !nsamp;
253 <        setcolor(cval, .0, .0, .0);     /* sample our source area */
248 <        sf = sqrt(omega);
253 >        sf = sqrt(omega);               /* sample our source area */
254          tsr = sqrt(tomega);
255          for (i = nsamp; i--; ) {
256                  VCOPY(vsmp, vsrc);      /* jitter query directions */
# Line 253 | Line 258 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
258                          multisamp(sd, 2, (i + frandom())/(double)nsamp);
259                          vsmp[0] += (sd[0] - .5)*sf;
260                          vsmp[1] += (sd[1] - .5)*sf;
261 <                        if (normalize(vsmp) == 0) {
257 <                                --nsamp;
258 <                                continue;
259 <                        }
261 >                        normalize(vsmp);
262                  }
263                  bsdf_jitter(vjit, ndp, tsr);
264                                          /* compute BSDF */
265                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
266                  if (ec)
267                          goto baderror;
268 <                if (sv.cieY - diffY <= FTINY) {
267 <                        addcolor(cval, cdiff);
268 >                if (sv.cieY - diffY <= FTINY)
269                          continue;       /* no specular part */
270 <                }
270 >                                        /* check for variable resolution */
271 >                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
272 >                if (ec)
273 >                        goto baderror;
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) {
286 <                setcolor(cval, .0, .0, .0);
287 <                return(0);              /* no valid specular samples */
288 <        }
278 <        sf = 1./(double)nsamp;
285 >        if (wtot <= FTINY)              /* no valid specular samples? */
286 >                return(0);
287 >
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 301 | 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 309 | Line 319 | dir_bsdf(
319  
320          if (ldot > 0 && bright(np->rdiff) > FTINY) {
321                  /*
322 <                 *  Compute added diffuse reflected component.
322 >                 *  Compute diffuse reflected component
323                   */
324                  copycolor(ctmp, np->rdiff);
325                  dtmp = ldot * omega * (1./PI);
# Line 318 | Line 328 | dir_bsdf(
328          }
329          if (ldot < 0 && bright(np->tdiff) > FTINY) {
330                  /*
331 <                 *  Compute added diffuse transmission.
331 >                 *  Compute diffuse transmission
332                   */
333                  copycolor(ctmp, np->tdiff);
334                  dtmp = -ldot * omega * (1.0/PI);
# Line 328 | Line 338 | dir_bsdf(
338          if (ambRayInPmap(np->pr))
339                  return;         /* specular already in photon map */
340          /*
341 <         *  Compute specular scattering coefficient using BSDF.
341 >         *  Compute specular scattering coefficient using BSDF
342           */
343          if (!direct_specular_OK(ctmp, ldir, omega, np))
344                  return;
# Line 355 | 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 364 | Line 374 | dir_brdf(
374  
375          if (bright(np->rdiff) > FTINY) {
376                  /*
377 <                 *  Compute added diffuse reflected component.
377 >                 *  Compute diffuse reflected component
378                   */
379                  copycolor(ctmp, np->rdiff);
380                  dtmp = ldot * omega * (1./PI);
# Line 374 | Line 384 | dir_brdf(
384          if (ambRayInPmap(np->pr))
385                  return;         /* specular already in photon map */
386          /*
387 <         *  Compute specular reflection coefficient using BSDF.
387 >         *  Compute specular reflection coefficient using BSDF
388           */
389          if (!direct_specular_OK(ctmp, ldir, omega, np))
390                  return;
# Line 397 | 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 406 | Line 416 | dir_btdf(
416  
417          if (bright(np->tdiff) > FTINY) {
418                  /*
419 <                 *  Compute added diffuse transmission.
419 >                 *  Compute diffuse transmission
420                   */
421                  copycolor(ctmp, np->tdiff);
422                  dtmp = -ldot * omega * (1.0/PI);
# Line 416 | Line 426 | dir_btdf(
426          if (ambRayInPmap(np->pr))
427                  return;         /* specular already in photon map */
428          /*
429 <         *  Compute specular scattering coefficient using BSDF.
429 >         *  Compute specular scattering coefficient using BSDF
430           */
431          if (!direct_specular_OK(ctmp, ldir, omega, np))
432                  return;
# Line 429 | 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? */
477 <                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 487 | 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  
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 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