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.7 by greg, Tue Feb 22 05:41:02 2011 UTC vs.
Revision 2.13 by greg, Sun Aug 21 21:24:30 2011 UTC

# Line 50 | Line 50 | static const char RCSid[] = "$Id$";
50   *  Arguments for MAT_BSDF are:
51   *      6+      thick   BSDFfile        ux uy uz        funcfile        transform
52   *      0
53 < *      0|3|9   rdf     gdf     bdf
53 > *      0|3|6|9 rdf     gdf     bdf
54   *              rdb     gdb     bdb
55   *              rdt     gdt     bdt
56   */
# Line 67 | Line 67 | typedef struct {
67          RAY     *pr;            /* intersected ray */
68          FVECT   pnorm;          /* perturbed surface normal */
69          FVECT   vray;           /* local outgoing (return) vector */
70 <        double  sr_vpsa;        /* sqrt of BSDF projected solid angle */
70 >        double  sr_vpsa[2];     /* sqrt of BSDF projected solid angle extrema */
71          RREAL   toloc[3][3];    /* world to local BSDF coords */
72          RREAL   fromloc[3][3];  /* local BSDF coords to world */
73          double  thick;          /* surface thickness */
# Line 82 | Line 82 | typedef struct {
82  
83   /* Jitter ray sample according to projected solid angle and specjitter */
84   static void
85 < bsdf_jitter(FVECT vres, BSDFDAT *ndp)
85 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, int domax)
86   {
87 <        double  sr_psa = ndp->sr_vpsa;
87 >        double  sr_psa = ndp->sr_vpsa[domax];
88  
89          VCOPY(vres, ndp->vray);
90          if (specjitter < 1.)
# Line 98 | Line 98 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
98  
99   /* Evaluate BSDF for direct component, returning true if OK to proceed */
100   static int
101 < direct_bsdf_OK(COLOR cval, FVECT ldir, BSDFDAT *ndp)
101 > direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
102   {
103 <        FVECT   vsrc, vjit;
103 >        int     nsamp, ok = 0;
104 >        FVECT   vsrc, vsmp, vjit;
105 >        double  tomega;
106 >        double  sf, sd[2];
107 >        COLOR   csmp;
108          SDValue sv;
109          SDError ec;
110 +        int     i;
111                                          /* transform source direction */
112          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
113                  return(0);
114 <                                        /* jitter query direction */
115 <        bsdf_jitter(vjit, ndp);
116 <                                        /* avoid indirect over-counting */
117 <        if (ndp->thick != .0 && ndp->pr->crtype & (SPECULAR|AMBIENT) &&
118 <                                vsrc[2] > .0 ^ vjit[2] > .0) {
119 <                double  dx = vsrc[0] + vjit[0];
115 <                double  dy = vsrc[1] + vjit[1];
116 <                if (dx*dx + dy*dy <= ndp->sr_vpsa*ndp->sr_vpsa)
114 >                                        /* check indirect over-counting */
115 >        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
116 >                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
117 >                double  dx = vsrc[0] + ndp->vray[0];
118 >                double  dy = vsrc[1] + ndp->vray[1];
119 >                if (dx*dx + dy*dy <= omega*(1./PI))
120                          return(0);
121          }
122 <        ec = SDevalBSDF(&sv, vjit, vsrc, ndp->sd);
122 >                                        /* get local BSDF resolution */
123 >        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
124          if (ec)
125 <                objerror(ndp->mp, USER, transSDError(ec));
126 <
127 <        if (sv.cieY <= FTINY)           /* not worth using? */
128 <                return(0);
129 <                                        /* else we're good to go */
130 <        cvt_sdcolor(cval, &sv);
131 <        return(1);
125 >                goto baderror;
126 >                                        /* assign number of samples */
127 >        if (tomega <= omega*.02)
128 >                nsamp = 50;
129 >        else
130 >                nsamp = 2.*omega/tomega + 1.;
131 >        sf = sqrt(omega);
132 >        setcolor(cval, .0, .0, .0);     /* sample our source area */
133 >        for (i = nsamp; i--; ) {
134 >                VCOPY(vsmp, vsrc);      /* jitter query directions */
135 >                if (nsamp > 1) {
136 >                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
137 >                        vsmp[0] += (sd[0] - .5)*sf;
138 >                        vsmp[1] += (sd[1] - .5)*sf;
139 >                        if (normalize(vsmp) == 0) {
140 >                                --nsamp;
141 >                                continue;
142 >                        }
143 >                }
144 >                bsdf_jitter(vjit, ndp, 0);
145 >                                        /* compute BSDF */
146 >                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
147 >                if (ec)
148 >                        goto baderror;
149 >                if (sv.cieY <= FTINY)   /* worth using? */
150 >                        continue;
151 >                cvt_sdcolor(csmp, &sv);
152 >                addcolor(cval, csmp);   /* average it in */
153 >                ++ok;
154 >        }
155 >        sf = 1./(double)nsamp;
156 >        scalecolor(cval, sf);
157 >        return(ok);
158 > baderror:
159 >        objerror(ndp->mp, USER, transSDError(ec));
160   }
161  
162   /* Compute source contribution for BSDF (reflected & transmitted) */
# Line 147 | Line 179 | dir_bsdf(
179          if ((-FTINY <= ldot) & (ldot <= FTINY))
180                  return;
181  
182 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
182 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
183                  /*
184                   *  Compute added diffuse reflected component.
185                   */
# Line 156 | Line 188 | dir_bsdf(
188                  scalecolor(ctmp, dtmp);
189                  addcolor(cval, ctmp);
190          }
191 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
191 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
192                  /*
193                   *  Compute added diffuse transmission.
194                   */
# Line 168 | Line 200 | dir_bsdf(
200          /*
201           *  Compute scattering coefficient using BSDF.
202           */
203 <        if (!direct_bsdf_OK(ctmp, ldir, np))
203 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
204                  return;
205 <        if (ldot > .0) {                /* pattern only diffuse reflection */
205 >        if (ldot > 0) {         /* pattern only diffuse reflection */
206                  COLOR   ctmp1, ctmp2;
207 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
207 >                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
208                                          : np->sd->rLambBack.cieY;
209                                          /* diffuse fraction */
210                  dtmp /= PI * bright(ctmp);
# Line 223 | Line 255 | dir_brdf(
255          /*
256           *  Compute reflection coefficient using BSDF.
257           */
258 <        if (!direct_bsdf_OK(ctmp, ldir, np))
258 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
259                  return;
260                                          /* pattern only diffuse reflection */
261 <        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
261 >        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
262                                  : np->sd->rLambBack.cieY;
263          dtmp /= PI * bright(ctmp);      /* diffuse fraction */
264          copycolor(ctmp2, np->pr->pcol);
# Line 272 | Line 304 | dir_btdf(
304          /*
305           *  Compute scattering coefficient using BSDF.
306           */
307 <        if (!direct_bsdf_OK(ctmp, ldir, np))
307 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
308                  return;
309                                          /* full pattern on transmission */
310          multcolor(ctmp, np->pr->pcol);
# Line 286 | Line 318 | static int
318   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
319   {
320          int     nstarget = 1;
321 <        int     nsent = 0;
321 >        int     nsent;
322          SDError ec;
323          SDValue bsv;
324 <        double  sthick;
325 <        FVECT   vjit, vsmp;
324 >        double  xrand;
325 >        FVECT   vsmp;
326          RAY     sr;
295        int     ntrials;
327                                                  /* multiple samples? */
328          if (specjitter > 1.5) {
329                  nstarget = specjitter*ndp->pr->rweight + .5;
330                  if (nstarget < 1)
331                          nstarget = 1;
332          }
333 <                                                /* run through our trials */
334 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
335 <                SDerrorDetail[0] = '\0';
336 <                                                /* sample direction & coef. */
337 <                bsdf_jitter(vjit, ndp);
338 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
339 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
333 >                                                /* run through our samples */
334 >        for (nsent = 0; nsent < nstarget; nsent++) {
335 >                if (nstarget == 1)              /* stratify random variable */
336 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
337 >                else
338 >                        xrand = (nsent + frandom())/(double)nstarget;
339 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
340 >                bsdf_jitter(vsmp, ndp, 0);
341 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
342                  if (ec)
343                          objerror(ndp->mp, USER, transSDError(ec));
344 <                                                /* zero component? */
312 <                if (bsv.cieY <= FTINY)
344 >                if (bsv.cieY <= FTINY)          /* zero component? */
345                          break;
346                                                  /* map vector to world */
347                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
348                          break;
317                                                /* unintentional penetration? */
318                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
319                        continue;
349                                                  /* spawn a specular ray */
350                  if (nstarget > 1)
351                          bsv.cieY /= (double)nstarget;
352 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
353 <                if (usepat)                     /* pattern on transmission */
352 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
353 >                if (usepat)                     /* apply pattern? */
354                          multcolor(sr.rcoef, ndp->pr->pcol);
355                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
356 <                        if (maxdepth  > 0)
356 >                        if (maxdepth > 0)
357                                  break;
358 <                        ++nsent;                /* Russian roulette victim */
330 <                        continue;
358 >                        continue;               /* Russian roulette victim */
359                  }
360                                                  /* need to offset origin? */
361 <                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
361 >                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
362                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
363                  rayvalue(&sr);                  /* send & evaluate sample */
364                  multcolor(sr.rcol, sr.rcoef);
365                  addcolor(ndp->pr->rcol, sr.rcol);
338                ++nsent;
366          }
367          return(nsent);
368   }
# Line 354 | Line 381 | sample_sdf(BSDFDAT *ndp, int sflags)
381                  cvt_sdcolor(unsc, &ndp->sd->tLamb);
382          } else /* sflags == SDsampSpR */ {
383                  unsc = ndp->runsamp;
384 <                if (ndp->pr->rod > .0) {
384 >                if (ndp->pr->rod > 0) {
385                          dfp = ndp->sd->rf;
386                          cvt_sdcolor(unsc, &ndp->sd->rLambFront);
387                  } else {
# Line 371 | Line 398 | sample_sdf(BSDFDAT *ndp, int sflags)
398                          FVECT   vjit;
399                          double  d;
400                          COLOR   ctmp;
401 <                        bsdf_jitter(vjit, ndp);
401 >                        bsdf_jitter(vjit, ndp, 1);
402                          d = SDdirectHemi(vjit, sflags, ndp->sd);
403                          if (sflags == SDsampSpT) {
404                                  copycolor(ctmp, ndp->pr->pcol);
# Line 408 | Line 435 | m_bsdf(OBJREC *m, RAY *r)
435                                  (m->oargs.nfargs % 3))
436                  objerror(m, USER, "bad # arguments");
437                                                  /* record surface struck */
438 <        hitfront = (r->rod > .0);
438 >        hitfront = (r->rod > 0);
439                                                  /* load cal file */
440          mf = getfunc(m, 5, 0x1d, 1);
441                                                  /* get thickness */
# Line 417 | Line 444 | m_bsdf(OBJREC *m, RAY *r)
444                  nd.thick = .0;
445                                                  /* check shadow */
446          if (r->crtype & SHADOW) {
447 <                if (nd.thick != .0)
447 >                if (nd.thick != 0)
448                          raytrans(r);            /* pass-through */
449                  return(1);                      /* or shadow */
450          }
451                                                  /* check other rays to pass */
452 <        if (nd.thick != .0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
453 <                                nd.thick > .0 ^ hitfront)) {
452 >        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
453 >                                nd.thick > 0 ^ hitfront)) {
454                  raytrans(r);                    /* hide our proxy */
455                  return(1);
456          }
# Line 487 | Line 514 | m_bsdf(OBJREC *m, RAY *r)
514                  ec = SDinvXform(nd.fromloc, nd.toloc);
515                                                  /* determine BSDF resolution */
516          if (!ec)
517 <                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
518 <        if (!ec)
519 <                nd.sr_vpsa = sqrt(nd.sr_vpsa);
493 <        else {
517 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
518 >                                                SDqueryMin+SDqueryMax, nd.sd);
519 >        if (ec) {
520                  objerror(m, WARNING, transSDError(ec));
521                  SDfreeCache(nd.sd);
522                  return(1);
523          }
524 +        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
525 +        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
526          if (!hitfront) {                        /* perturb normal towards hit */
527                  nd.pnorm[0] = -nd.pnorm[0];
528                  nd.pnorm[1] = -nd.pnorm[1];
# Line 524 | Line 552 | m_bsdf(OBJREC *m, RAY *r)
552                  bnorm[0] = -nd.pnorm[0];
553                  bnorm[1] = -nd.pnorm[1];
554                  bnorm[2] = -nd.pnorm[2];
555 <                if (nd.thick != .0) {           /* proxy with offset? */
555 >                if (nd.thick != 0) {            /* proxy with offset? */
556                          VCOPY(vtmp, r->rop);
557                          VSUM(r->rop, vtmp, r->ron, -nd.thick);
558                          multambient(ctmp, r, bnorm);
# Line 538 | Line 566 | m_bsdf(OBJREC *m, RAY *r)
566                                                  /* add direct component */
567          if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
568                  direct(r, dir_brdf, &nd);       /* reflection only */
569 <        } else if (nd.thick == .0) {
569 >        } else if (nd.thick == 0) {
570                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
571          } else {
572                  direct(r, dir_brdf, &nd);       /* reflection first */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines