ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambcomp.c
(Generate patch)

Comparing ray/src/rt/ambcomp.c (file contents):
Revision 2.66 by greg, Thu Sep 4 09:09:08 2014 UTC vs.
Revision 2.78 by greg, Tue Jan 9 00:51:51 2018 UTC

# Line 20 | Line 20 | static const char      RCSid[] = "$Id$";
20   #include  "ray.h"
21   #include  "ambient.h"
22   #include  "random.h"
23 + #include  "source.h"
24 + #include  "otypes.h"
25 + #include  "otspecial.h"
26  
27   #ifndef OLDAMB
28  
# Line 51 | Line 54 | typedef struct {
54  
55  
56   static int
57 + ambcollision(                           /* proposed direciton collides? */
58 +        AMBHEMI *hp,
59 +        int     i,
60 +        int     j,
61 +        FVECT   dv
62 + )
63 + {
64 +        double  cos_thresh;
65 +        int     ii, jj;
66 +                                        /* min. spacing = 1/4th division */
67 +        cos_thresh = (PI/4.)/(double)hp->ns;
68 +        cos_thresh = 1. - .5*cos_thresh*cos_thresh;
69 +                                        /* check existing neighbors */
70 +        for (ii = i-1; ii <= i+1; ii++) {
71 +                if (ii < 0) continue;
72 +                if (ii >= hp->ns) break;
73 +                for (jj = j-1; jj <= j+1; jj++) {
74 +                        AMBSAMP *ap;
75 +                        FVECT   avec;
76 +                        double  dprod;
77 +                        if (jj < 0) continue;
78 +                        if (jj >= hp->ns) break;
79 +                        if ((ii==i) & (jj==j)) continue;
80 +                        ap = &ambsam(hp,ii,jj);
81 +                        if (ap->d <= .5/FHUGE)
82 +                                continue;       /* no one home */
83 +                        VSUB(avec, ap->p, hp->rp->rop);
84 +                        dprod = DOT(avec, dv);
85 +                        if (dprod >= cos_thresh*VLEN(avec))
86 +                                return(1);      /* collision */
87 +                }
88 +        }
89 +        return(0);                      /* nothing to worry about */
90 + }
91 +
92 +
93 + static int
94   ambsample(                              /* initial ambient division sample */
95          AMBHEMI *hp,
96          int     i,
# Line 78 | Line 118 | ambsample(                             /* initial ambient division sample */
118          hlist[1] = j;
119          hlist[2] = i;
120          multisamp(spt, 2, urand(ilhash(hlist,3)+n));
121 <                                        /* avoid coincident samples */
82 <        if (!n && (0 < i) & (i < hp->ns-1) &&
83 <                        (0 < j) & (j < hp->ns-1)) {
84 <                if ((spt[0] < 0.1) | (spt[0] >= 0.9))
85 <                        spt[0] = 0.1 + 0.8*frandom();
86 <                if ((spt[1] < 0.1) | (spt[1] >= 0.9))
87 <                        spt[1] = 0.1 + 0.8*frandom();
88 <        }
121 > resample:
122          SDsquare2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns);
123          zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
124          for (ii = 3; ii--; )
# Line 93 | Line 126 | ambsample(                             /* initial ambient division sample */
126                                  spt[1]*hp->uy[ii] +
127                                  zd*hp->rp->ron[ii];
128          checknorm(ar.rdir);
129 +                                        /* avoid coincident samples */
130 +        if (!n && ambcollision(hp, i, j, ar.rdir)) {
131 +                spt[0] = frandom(); spt[1] = frandom();
132 +                goto resample;          /* reject this sample */
133 +        }
134          dimlist[ndims++] = AI(hp,i,j) + 90171;
135          rayvalue(&ar);                  /* evaluate ray */
136          ndims--;
# Line 102 | Line 140 | ambsample(                             /* initial ambient division sample */
140          if (ar.rt*ap->d < 1.0)          /* new/closer distance? */
141                  ap->d = 1.0/ar.rt;
142          if (!n) {                       /* record first vertex & value */
143 <                if (ar.rt > 10.0*thescene.cusize)
144 <                        ar.rt = 10.0*thescene.cusize;
143 >                if (ar.rt > 10.0*thescene.cusize + 1000.)
144 >                        ar.rt = 10.0*thescene.cusize + 1000.;
145                  VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
146                  copycolor(ap->v, ar.rcol);
147          } else {                        /* else update recorded value */
# Line 125 | Line 163 | ambsample(                             /* initial ambient division sample */
163   static float *
164   getambdiffs(AMBHEMI *hp)
165   {
166 +        const double    normf = 1./bright(hp->acoef);
167          float   *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float));
168          float   *ep;
169          AMBSAMP *ap;
# Line 138 | Line 177 | getambdiffs(AMBHEMI *hp)
177              for (j = 0; j < hp->ns; j++, ap++, ep++) {
178                  b = bright(ap[0].v);
179                  if (i) {                /* from above */
180 <                        d2 = b - bright(ap[-hp->ns].v);
180 >                        d2 = normf*(b - bright(ap[-hp->ns].v));
181                          d2 *= d2;
182                          ep[0] += d2;
183                          ep[-hp->ns] += d2;
184                  }
185                  if (!j) continue;
186                                          /* from behind */
187 <                d2 = b - bright(ap[-1].v);
187 >                d2 = normf*(b - bright(ap[-1].v));
188                  d2 *= d2;
189                  ep[0] += d2;
190                  ep[-1] += d2;
191                  if (!i) continue;
192                                          /* diagonal */
193 <                d2 = b - bright(ap[-hp->ns-1].v);
193 >                d2 = normf*(b - bright(ap[-hp->ns-1].v));
194                  d2 *= d2;
195                  ep[0] += d2;
196                  ep[-hp->ns-1] += d2;
# Line 180 | Line 219 | ambsupersamp(AMBHEMI *hp, int cnt)
219          float   *earr = getambdiffs(hp);
220          double  e2rem = 0;
221          AMBSAMP *ap;
183        RAY     ar;
222          float   *ep;
223          int     i, j, n, nss;
224  
# Line 196 | Line 234 | ambsupersamp(AMBHEMI *hp, int cnt)
234                          goto done;      /* nothing left to do */
235                  nss = *ep/e2rem*cnt + frandom();
236                  for (n = 1; n <= nss && ambsample(hp,i,j,n); n++)
237 <                        --cnt;
237 >                        if (!--cnt) goto done;
238                  e2rem -= *ep++;         /* update remainder */
239          }
240   done:
# Line 214 | Line 252 | samp_hemi(                             /* sample indirect hemisphere */
252          AMBHEMI *hp;
253          double  d;
254          int     n, i, j;
255 +                                        /* insignificance check */
256 +        if (bright(rcol) <= FTINY)
257 +                return(NULL);
258                                          /* set number of divisions */
259          if (ambacc <= FTINY &&
260                          wt > (d = 0.8*intens(rcol)*r->rweight/(ambdiv*minweight)))
261                  wt = d;                 /* avoid ray termination */
262          n = sqrt(ambdiv * wt) + 0.5;
263 <        i = 1 + 8*(ambacc > FTINY);     /* minimum number of samples */
263 >        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
264          if (n < i)
265                  n = i;
266                                          /* allocate sampling array */
# Line 236 | Line 277 | samp_hemi(                             /* sample indirect hemisphere */
277          d = 1.0/(n*n);
278          scalecolor(hp->acoef, d);
279                                          /* make tangent plane axes */
280 <        hp->uy[0] = 0.5 - frandom();
240 <        hp->uy[1] = 0.5 - frandom();
241 <        hp->uy[2] = 0.5 - frandom();
242 <        for (i = 3; i--; )
243 <                if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
244 <                        break;
245 <        if (i < 0)
280 >        if (!getperpendicular(hp->ux, r->ron, 1))
281                  error(CONSISTENCY, "bad ray direction in samp_hemi");
247        hp->uy[i] = 1.0;
248        VCROSS(hp->ux, hp->uy, r->ron);
249        normalize(hp->ux);
282          VCROSS(hp->uy, r->ron, hp->ux);
283                                          /* sample divisions */
284          for (i = hp->ns; i--; )
# Line 617 | Line 649 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
649          FVECT           vec;
650          double          u, v;
651          double          ang, a1;
652 +        OBJREC          *m;
653          int             i, j;
654                                          /* don't bother for a few samples */
655 <        if (hp->ns < 12)
655 >        if (hp->ns < 8)
656                  return(0);
657                                          /* check distances overhead */
658          for (i = hp->ns*3/4; i-- > hp->ns>>2; )
# Line 646 | Line 679 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
679                          flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0)));
680              }
681                                          /* add low-angle incident (< 20deg) */
682 <        if (fabs(hp->rp->rod) <= 0.342) {
682 >        if (fabs(hp->rp->rod) <= 0.342 && hp->rp->parent != NULL &&
683 >                        (m = findmaterial(hp->rp->parent->ro)) != NULL &&
684 >                        isopaque(m->otype)) {
685                  u = -DOT(hp->rp->rdir, uv[0]);
686                  v = -DOT(hp->rp->rdir, uv[1]);
687                  if ((r0*r0*u*u + r1*r1*v*v) > hp->rp->rot*hp->rp->rot) {
# Line 695 | Line 730 | doambient(                             /* compute ambient component */
730                  return(0);
731  
732          if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
733 <                        (hp->sampOK < 0) | (hp->ns < 9)) {
733 >                        (hp->sampOK < 0) | (hp->ns < 6)) {
734                  free(hp);               /* Hessian not requested/possible */
735                  return(-1);             /* value-only return value */
736          }
# Line 743 | Line 778 | doambient(                             /* compute ambient component */
778                                  ra[0] = maxarad;
779                  }
780                                          /* flag encroached directions */
781 <                if ((wt >= 0.89*AVGREFL) & (crlp != NULL))
781 >                if (crlp != NULL)
782                          *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc);
783                  if (pg != NULL) {       /* cap gradient if necessary */
784                          d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines