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.40 by greg, Mon Jul 17 00:14:28 2017 UTC vs.
Revision 2.53 by greg, Thu Aug 2 22:44:35 2018 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   #include "copyright.h"
9  
10   #include  "ray.h"
11 + #include  "otypes.h"
12   #include  "ambient.h"
13   #include  "source.h"
14   #include  "func.h"
# Line 16 | Line 17 | static const char RCSid[] = "$Id$";
17   #include  "pmapmat.h"
18  
19   /*
20 < *      Arguments to this material include optional diffuse colors.
20 >  *     Arguments to this material include optional diffuse colors.
21   *  String arguments include the BSDF and function files.
22 < *      A non-zero thickness causes the strange but useful behavior
22 > *      For the MAT_BSDF type, a non-zero thickness causes the useful behavior
23   *  of translating transmitted rays this distance beneath the surface
24   *  (opposite the surface normal) to bypass any intervening geometry.
25   *  Translation only affects scattered, non-source-directed samples.
# Line 35 | Line 36 | static const char RCSid[] = "$Id$";
36   *  hides geometry in front of the surface when rays hit from behind,
37   *  and applies only the transmission and backside reflectance properties.
38   *  Reflection is ignored on the hidden side, as those rays pass through.
39 < *      When thickness is set to zero, shadow rays will be blocked unless
40 < *  a BTDF has a strong "through" component in the source direction.
39 > *      For the MAT_ABSDF type, we check for a strong "through" component.
40 > *  Such a component will cause direct rays to pass through unscattered.
41   *  A separate test prevents over-counting by dropping samples that are
42   *  too close to this "through" direction.  BSDFs with such a through direction
43   *  will also have a view component, meaning they are somewhat see-through.
44 + *  A MAT_BSDF type with zero thickness behaves the same as a MAT_ABSDF
45 + *  type with no strong through component.
46   *      The "up" vector for the BSDF is given by three variables, defined
47   *  (along with the thickness) by the named function file, or '.' if none.
48   *  Together with the surface normal, this defines the local coordinate
# Line 52 | Line 55 | static const char RCSid[] = "$Id$";
55   *  not multiplied.  However, patterns affect this material as a multiplier
56   *  on everything except non-diffuse reflection.
57   *
58 + *  Arguments for MAT_ABSDF are:
59 + *      5+      BSDFfile        ux uy uz        funcfile        transform
60 + *      0
61 + *      0|3|6|9 rdf     gdf     bdf
62 + *              rdb     gdb     bdb
63 + *              rdt     gdt     bdt
64 + *
65   *  Arguments for MAT_BSDF are:
66   *      6+      thick   BSDFfile        ux uy uz        funcfile        transform
67   *      0
# Line 76 | Line 86 | typedef struct {
86          RREAL   toloc[3][3];    /* world to local BSDF coords */
87          RREAL   fromloc[3][3];  /* local BSDF coords to world */
88          double  thick;          /* surface thickness */
89 <        COLOR   cthru;          /* "through" component multiplier */
89 >        COLOR   cthru;          /* "through" component for MAT_ABSDF */
90          SDData  *sd;            /* loaded BSDF data */
91          COLOR   rdiff;          /* diffuse reflection */
92          COLOR   runsamp;        /* BSDF hemispherical reflection */
# Line 86 | Line 96 | typedef struct {
96  
97   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
98  
99 < /* Compute "through" component color */
99 > /* Compute "through" component color for MAT_ABSDF */
100   static void
101   compute_through(BSDFDAT *ndp)
102   {
# Line 106 | Line 116 | compute_through(BSDFDAT *ndp)
116                                          {0, -1.6},
117                                          {1.6, 0},
118                                  };
119 <        const double    peak_over = 2.0;
119 >        const double    peak_over = 1.5;
120          SDSpectralDF    *dfp;
121          FVECT           pdir;
122          double          tomega, srchrad;
# Line 114 | Line 124 | compute_through(BSDFDAT *ndp)
124          int             i;
125          SDError         ec;
126  
117        setcolor(ndp->cthru, 0, 0, 0);          /* starting assumption */
118
127          if (ndp->pr->rod > 0)
128                  dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
129          else
# Line 128 | Line 136 | compute_through(BSDFDAT *ndp)
136          srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
137          setcolor(vpeak, 0, 0, 0);
138          setcolor(vsum, 0, 0, 0);
139 +        pdir[2] = 0.0;
140          for (i = 0; i < NDIR2CHECK; i++) {
141                  FVECT   tdir;
142                  SDValue sv;
# Line 141 | Line 150 | compute_through(BSDFDAT *ndp)
150                          goto baderror;
151                  cvt_sdcolor(vcol, &sv);
152                  addcolor(vsum, vcol);
153 <                if (bright(vcol) > bright(vpeak)) {
153 >                if (sv.cieY > bright(vpeak)) {
154                          copycolor(vpeak, vcol);
155                          VCOPY(pdir, tdir);
156                  }
157          }
158 +        if (pdir[2] == 0.0)
159 +                return;                         /* zero neighborhood */
160          ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
161          if (ec)
162                  goto baderror;
163          if (tomega > 1.5*dfp->minProjSA)
164                  return;                         /* not really a peak? */
165 +        tomega /= fabs(pdir[2]);                /* remove cosine factor */
166          if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
167                  return;                         /* < 0.1% transmission */
168          for (i = 3; i--; )                      /* remove peak from average */
# Line 184 | Line 196 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
196   static int
197   direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
198   {
199 <        int     nsamp, ok = 0;
199 >        int     nsamp;
200 >        double  wtot = 0;
201          FVECT   vsrc, vsmp, vjit;
202          double  tomega, tomega2;
203          double  sf, tsr, sd[2];
# Line 223 | Line 236 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
236                  diffY = 0;
237                  setcolor(cdiff,  0, 0, 0);
238          }
239 <                                        /* need projected solid angles */
239 >                                        /* need projected solid angle */
240          omega *= fabs(vsrc[2]);
228        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
229        if (ec)
230                goto baderror;
241                                          /* check indirect over-counting */
242          if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
243 <                double  dx = vsrc[0] + ndp->vray[0];
244 <                double  dy = vsrc[1] + ndp->vray[1];
245 <                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
246 <                                                2.*sqrt(omega*tomega)))
243 >                double          dx = vsrc[0] + ndp->vray[0];
244 >                double          dy = vsrc[1] + ndp->vray[1];
245 >                SDSpectralDF    *dfp = (ndp->pr->rod > 0) ?
246 >                        ((ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb) :
247 >                        ((ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf) ;
248 >
249 >                if (dx*dx + dy*dy <= (2.5*4./PI)*(omega + dfp->minProjSA +
250 >                                                2.*sqrt(omega*dfp->minProjSA)))
251                          return(0);
252          }
253 +        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
254 +        if (ec)
255 +                goto baderror;
256                                          /* assign number of samples */
257          sf = specjitter * ndp->pr->rweight;
258          if (tomega <= 0)
# Line 269 | Line 286 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
286                  if (tomega2 < .12*tomega)
287                          continue;       /* not safe to include */
288                  cvt_sdcolor(csmp, &sv);
289 <                addcolor(cval, csmp);   /* else average it in */
290 <                ++ok;
289 >
290 >                if (sf < 2.5*tsr) {     /* weight by Y for small sources */
291 >                        scalecolor(csmp, sv.cieY);
292 >                        wtot += sv.cieY;
293 >                } else
294 >                        wtot += 1.;
295 >                addcolor(cval, csmp);
296          }
297 <        if (!ok)                        /* no valid specular samples? */
297 >        if (wtot <= FTINY)              /* no valid specular samples? */
298                  return(0);
299  
300 <        sf = 1./(double)ok;             /* compute average BSDF */
300 >        sf = 1./wtot;                   /* weighted average BSDF */
301          scalecolor(cval, sf);
302                                          /* subtract diffuse contribution */
303          for (i = 3*(diffY > FTINY); i--; )
# Line 431 | Line 453 | dir_btdf(
453   static int
454   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
455   {
456 <        int     hasthru = (xmit && !(ndp->pr->crtype & (SPECULAR|AMBIENT))
457 <                                && bright(ndp->cthru) > FTINY);
458 <        int     nstarget = 1;
459 <        int     nsent = 0;
460 <        int     n;
461 <        SDError ec;
462 <        SDValue bsv;
463 <        double  xrand;
464 <        FVECT   vsmp, vinc;
465 <        RAY     sr;
456 >        const int       hasthru = (xmit &&
457 >                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
458 >                                        && bright(ndp->cthru) > FTINY);
459 >        int             nstarget = 1;
460 >        int             nsent = 0;
461 >        int             n;
462 >        SDError         ec;
463 >        SDValue         bsv;
464 >        double          xrand;
465 >        FVECT           vsmp, vinc;
466 >        RAY             sr;
467                                                  /* multiple samples? */
468          if (specjitter > 1.5) {
469                  nstarget = specjitter*ndp->pr->rweight + .5;
# Line 479 | Line 502 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit
502                  if (xmit)                       /* apply pattern on transmit */
503                          multcolor(sr.rcoef, ndp->pr->pcol);
504                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
505 <                        if (maxdepth > 0)
506 <                                break;
507 <                        continue;               /* Russian roulette victim */
505 >                        if (!n & (nstarget > 1)) {
506 >                                n = nstarget;   /* avoid infinitue loop */
507 >                                nstarget = nstarget*sr.rweight/minweight;
508 >                                if (n == nstarget) break;
509 >                                n = -1;         /* moved target */
510 >                        }
511 >                        continue;               /* try again */
512                  }
513                  if (xmit && ndp->thick != 0)    /* need to offset origin? */
514                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
# Line 497 | Line 524 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit
524   static int
525   sample_sdf(BSDFDAT *ndp, int sflags)
526   {
527 <        int             hasthru = (sflags == SDsampSpT
528 <                                    && !(ndp->pr->crtype & (SPECULAR|AMBIENT))
529 <                                    && bright(ndp->cthru) > FTINY);
527 >        int             hasthru = (sflags == SDsampSpT &&
528 >                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
529 >                                        && bright(ndp->cthru) > FTINY);
530          int             n, ntotal = 0;
531          double          b = 0;
532          SDSpectralDF    *dfp;
# Line 522 | Line 549 | sample_sdf(BSDFDAT *ndp, int sflags)
549          if (dfp == NULL)                        /* no specular component? */
550                  return(0);
551  
525        dimlist[ndims++] = (int)(size_t)ndp->mp;
552          if (hasthru) {                          /* separate view sample? */
553                  RAY     tr;
554                  if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
# Line 535 | Line 561 | sample_sdf(BSDFDAT *ndp, int sflags)
561                  } else
562                          hasthru = 0;
563          }
564 <        ndims--;
539 <        if (dfp->maxHemi - b <= FTINY) {        /* how specular to sample? */
564 >        if (dfp->maxHemi - b <= FTINY) {        /* have specular to sample? */
565                  b = 0;
566          } else {
567                  FVECT   vjit;
# Line 554 | Line 579 | sample_sdf(BSDFDAT *ndp, int sflags)
579                  }
580                  return(ntotal);
581          }
582 <        ndims += 2;                             /* else sample specular */
582 >        dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */
583 >        ndims += 2;
584          for (n = dfp->ncomp; n--; ) {           /* loop over components */
585                  dimlist[ndims-1] = n + 9438;
586                  ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
# Line 567 | Line 593 | sample_sdf(BSDFDAT *ndp, int sflags)
593   int
594   m_bsdf(OBJREC *m, RAY *r)
595   {
596 +        int     hasthick = (m->otype == MAT_BSDF);
597          int     hitfront;
598          COLOR   ctmp;
599          SDError ec;
# Line 574 | Line 601 | m_bsdf(OBJREC *m, RAY *r)
601          MFUNC   *mf;
602          BSDFDAT nd;
603                                                  /* check arguments */
604 <        if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
604 >        if ((m->oargs.nsargs < hasthick+5) | (m->oargs.nfargs > 9) |
605                                  (m->oargs.nfargs % 3))
606                  objerror(m, USER, "bad # arguments");
607                                                  /* record surface struck */
608          hitfront = (r->rod > 0);
609                                                  /* load cal file */
610 <        mf = getfunc(m, 5, 0x1d, 1);
610 >        mf = hasthick   ? getfunc(m, 5, 0x1d, 1)
611 >                        : getfunc(m, 4, 0xe, 1) ;
612          setfunc(m, r);
613 <                                                /* get thickness */
614 <        nd.thick = evalue(mf->ep[0]);
615 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
616 <                nd.thick = 0;
613 >        nd.thick = 0;                           /* set thickness */
614 >        if (hasthick) {
615 >                nd.thick = evalue(mf->ep[0]);
616 >                if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
617 >                        nd.thick = 0;
618 >        }
619                                                  /* check backface visibility */
620          if (!hitfront & !backvis) {
621                  raytrans(r);
# Line 598 | Line 628 | m_bsdf(OBJREC *m, RAY *r)
628                  raytrans(r);                    /* hide our proxy */
629                  return(1);
630          }
631 +        if (hasthick && r->crtype & SHADOW)     /* early shadow check #1 */
632 +                return(1);
633          nd.mp = m;
634          nd.pr = r;
635                                                  /* get BSDF data */
636 <        nd.sd = loadBSDF(m->oargs.sarg[1]);
637 <                                                /* early shadow check */
636 >        nd.sd = loadBSDF(m->oargs.sarg[hasthick]);
637 >                                                /* early shadow check #2 */
638          if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
639                  return(1);
640                                                  /* diffuse reflectance */
# Line 637 | Line 669 | m_bsdf(OBJREC *m, RAY *r)
669          multcolor(nd.rdiff, r->pcol);
670          multcolor(nd.tdiff, r->pcol);
671                                                  /* get up vector */
672 <        upvec[0] = evalue(mf->ep[1]);
673 <        upvec[1] = evalue(mf->ep[2]);
674 <        upvec[2] = evalue(mf->ep[3]);
672 >        upvec[0] = evalue(mf->ep[hasthick+0]);
673 >        upvec[1] = evalue(mf->ep[hasthick+1]);
674 >        upvec[2] = evalue(mf->ep[hasthick+2]);
675                                                  /* return to world coords */
676          if (mf->fxp != &unitxf) {
677                  multv3(upvec, upvec, mf->fxp->xfm);
# Line 662 | Line 694 | m_bsdf(OBJREC *m, RAY *r)
694                  objerror(m, WARNING, "Illegal orientation vector");
695                  return(1);
696          }
697 <        compute_through(&nd);                   /* compute through component */
698 <        if (r->crtype & SHADOW) {
699 <                RAY     tr;                     /* attempt to pass shadow ray */
700 <                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
701 <                        return(1);              /* blocked */
702 <                VCOPY(tr.rdir, r->rdir);
703 <                rayvalue(&tr);                  /* transmit with scaling */
704 <                multcolor(tr.rcol, tr.rcoef);
705 <                copycolor(r->rcol, tr.rcol);
706 <                return(1);                      /* we're done */
697 >        setcolor(nd.cthru, 0, 0, 0);            /* consider through component */
698 >        if (m->otype == MAT_ABSDF) {
699 >                compute_through(&nd);
700 >                if (r->crtype & SHADOW) {
701 >                        RAY     tr;             /* attempt to pass shadow ray */
702 >                        if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
703 >                                return(1);      /* no through component */
704 >                        VCOPY(tr.rdir, r->rdir);
705 >                        rayvalue(&tr);          /* transmit with scaling */
706 >                        multcolor(tr.rcol, tr.rcoef);
707 >                        copycolor(r->rcol, tr.rcol);
708 >                        return(1);              /* we're done */
709 >                }
710          }
711          ec = SDinvXform(nd.fromloc, nd.toloc);
712          if (!ec)                                /* determine BSDF resolution */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines