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.62 by greg, Mon May 19 20:23:48 2014 UTC vs.
Revision 2.77 by greg, Fri Apr 21 16:07:29 2017 UTC

# Line 21 | Line 21 | static const char      RCSid[] = "$Id$";
21   #include  "ambient.h"
22   #include  "random.h"
23  
24 < #ifdef NEWAMB
24 > #ifndef OLDAMB
25  
26   extern void             SDsquare2disk(double ds[2], double seedx, double seedy);
27  
# Line 51 | Line 51 | typedef struct {
51  
52  
53   static int
54 + ambcollision(                           /* proposed direciton collides? */
55 +        AMBHEMI *hp,
56 +        int     i,
57 +        int     j,
58 +        FVECT   dv
59 + )
60 + {
61 +        double  cos_thresh;
62 +        int     ii, jj;
63 +                                        /* min. spacing = 1/4th division */
64 +        cos_thresh = (PI/4.)/(double)hp->ns;
65 +        cos_thresh = 1. - .5*cos_thresh*cos_thresh;
66 +                                        /* check existing neighbors */
67 +        for (ii = i-1; ii <= i+1; ii++) {
68 +                if (ii < 0) continue;
69 +                if (ii >= hp->ns) break;
70 +                for (jj = j-1; jj <= j+1; jj++) {
71 +                        AMBSAMP *ap;
72 +                        FVECT   avec;
73 +                        double  dprod;
74 +                        if (jj < 0) continue;
75 +                        if (jj >= hp->ns) break;
76 +                        if ((ii==i) & (jj==j)) continue;
77 +                        ap = &ambsam(hp,ii,jj);
78 +                        if (ap->d <= .5/FHUGE)
79 +                                continue;       /* no one home */
80 +                        VSUB(avec, ap->p, hp->rp->rop);
81 +                        dprod = DOT(avec, dv);
82 +                        if (dprod >= cos_thresh*VLEN(avec))
83 +                                return(1);      /* collision */
84 +                }
85 +        }
86 +        return(0);                      /* nothing to worry about */
87 + }
88 +
89 +
90 + static int
91   ambsample(                              /* initial ambient division sample */
92          AMBHEMI *hp,
93          int     i,
# Line 78 | Line 115 | ambsample(                             /* initial ambient division sample */
115          hlist[1] = j;
116          hlist[2] = i;
117          multisamp(spt, 2, urand(ilhash(hlist,3)+n));
118 <        if (!n) {                       /* avoid border samples for n==0 */
82 <                if ((spt[0] < 0.1) | (spt[0] >= 0.9))
83 <                        spt[0] = 0.1 + 0.8*frandom();
84 <                if ((spt[1] < 0.1) | (spt[1] >= 0.9))
85 <                        spt[1] = 0.1 + 0.8*frandom();
86 <        }
118 > resample:
119          SDsquare2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns);
120          zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
121          for (ii = 3; ii--; )
# Line 91 | Line 123 | ambsample(                             /* initial ambient division sample */
123                                  spt[1]*hp->uy[ii] +
124                                  zd*hp->rp->ron[ii];
125          checknorm(ar.rdir);
126 +                                        /* avoid coincident samples */
127 +        if (!n && ambcollision(hp, i, j, ar.rdir)) {
128 +                spt[0] = frandom(); spt[1] = frandom();
129 +                goto resample;          /* reject this sample */
130 +        }
131          dimlist[ndims++] = AI(hp,i,j) + 90171;
132          rayvalue(&ar);                  /* evaluate ray */
133          ndims--;
# Line 100 | Line 137 | ambsample(                             /* initial ambient division sample */
137          if (ar.rt*ap->d < 1.0)          /* new/closer distance? */
138                  ap->d = 1.0/ar.rt;
139          if (!n) {                       /* record first vertex & value */
140 <                if (ar.rt > 10.0*thescene.cusize)
141 <                        ar.rt = 10.0*thescene.cusize;
140 >                if (ar.rt > 10.0*thescene.cusize + 1000.)
141 >                        ar.rt = 10.0*thescene.cusize + 1000.;
142                  VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
143                  copycolor(ap->v, ar.rcol);
144          } else {                        /* else update recorded value */
# Line 123 | Line 160 | ambsample(                             /* initial ambient division sample */
160   static float *
161   getambdiffs(AMBHEMI *hp)
162   {
163 +        const double    normf = 1./bright(hp->acoef);
164          float   *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float));
165          float   *ep;
166          AMBSAMP *ap;
# Line 136 | Line 174 | getambdiffs(AMBHEMI *hp)
174              for (j = 0; j < hp->ns; j++, ap++, ep++) {
175                  b = bright(ap[0].v);
176                  if (i) {                /* from above */
177 <                        d2 = b - bright(ap[-hp->ns].v);
177 >                        d2 = normf*(b - bright(ap[-hp->ns].v));
178                          d2 *= d2;
179                          ep[0] += d2;
180                          ep[-hp->ns] += d2;
181                  }
182                  if (!j) continue;
183                                          /* from behind */
184 <                d2 = b - bright(ap[-1].v);
184 >                d2 = normf*(b - bright(ap[-1].v));
185                  d2 *= d2;
186                  ep[0] += d2;
187                  ep[-1] += d2;
188                  if (!i) continue;
189                                          /* diagonal */
190 <                d2 = b - bright(ap[-hp->ns-1].v);
190 >                d2 = normf*(b - bright(ap[-hp->ns-1].v));
191                  d2 *= d2;
192                  ep[0] += d2;
193                  ep[-hp->ns-1] += d2;
# Line 178 | Line 216 | ambsupersamp(AMBHEMI *hp, int cnt)
216          float   *earr = getambdiffs(hp);
217          double  e2rem = 0;
218          AMBSAMP *ap;
181        RAY     ar;
219          float   *ep;
220          int     i, j, n, nss;
221  
# Line 194 | Line 231 | ambsupersamp(AMBHEMI *hp, int cnt)
231                          goto done;      /* nothing left to do */
232                  nss = *ep/e2rem*cnt + frandom();
233                  for (n = 1; n <= nss && ambsample(hp,i,j,n); n++)
234 <                        --cnt;
234 >                        if (!--cnt) goto done;
235                  e2rem -= *ep++;         /* update remainder */
236          }
237   done:
# Line 212 | Line 249 | samp_hemi(                             /* sample indirect hemisphere */
249          AMBHEMI *hp;
250          double  d;
251          int     n, i, j;
252 +                                        /* insignificance check */
253 +        if (bright(rcol) <= FTINY)
254 +                return(NULL);
255                                          /* set number of divisions */
256          if (ambacc <= FTINY &&
257                          wt > (d = 0.8*intens(rcol)*r->rweight/(ambdiv*minweight)))
# Line 234 | Line 274 | samp_hemi(                             /* sample indirect hemisphere */
274          d = 1.0/(n*n);
275          scalecolor(hp->acoef, d);
276                                          /* make tangent plane axes */
277 <        hp->uy[0] = 0.5 - frandom();
238 <        hp->uy[1] = 0.5 - frandom();
239 <        hp->uy[2] = 0.5 - frandom();
240 <        for (i = 3; i--; )
241 <                if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
242 <                        break;
243 <        if (i < 0)
277 >        if (!getperpendicular(hp->ux, r->ron, 1))
278                  error(CONSISTENCY, "bad ray direction in samp_hemi");
245        hp->uy[i] = 1.0;
246        VCROSS(hp->ux, hp->uy, r->ron);
247        normalize(hp->ux);
279          VCROSS(hp->uy, r->ron, hp->ux);
280                                          /* sample divisions */
281          for (i = hp->ns; i--; )
# Line 608 | Line 639 | static uint32
639   ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
640   {
641          const double    max_d = 1.0/(minarad*ambacc + 0.001);
642 <        const double    ang_res = 0.5*PI/(hp->ns-1);
643 <        const double    ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY));
642 >        const double    ang_res = 0.5*PI/hp->ns;
643 >        const double    ang_step = ang_res/((int)(16/PI*ang_res) + 1.01);
644          double          avg_d = 0;
645          uint32          flgs = 0;
646          FVECT           vec;
# Line 617 | Line 648 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
648          double          ang, a1;
649          int             i, j;
650                                          /* don't bother for a few samples */
651 <        if (hp->ns < 12)
651 >        if (hp->ns < 8)
652                  return(0);
653                                          /* check distances overhead */
654          for (i = hp->ns*3/4; i-- > hp->ns>>2; )
# Line 640 | Line 671 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
671                  if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= u*u + v*v)
672                          continue;       /* occluder outside ellipse */
673                  ang = atan2a(v, u);     /* else set direction flags */
674 <                for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step)
674 >                for (a1 = ang-ang_res; a1 <= ang+ang_res; a1 += ang_step)
675                          flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0)));
676              }
677                                          /* add low-angle incident (< 20deg) */
# Line 693 | Line 724 | doambient(                             /* compute ambient component */
724                  return(0);
725  
726          if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
727 <                        (hp->sampOK < 0) | (hp->ns < 4)) {
727 >                        (hp->sampOK < 0) | (hp->ns < 6)) {
728                  free(hp);               /* Hessian not requested/possible */
729                  return(-1);             /* value-only return value */
730          }
# Line 741 | Line 772 | doambient(                             /* compute ambient component */
772                                  ra[0] = maxarad;
773                  }
774                                          /* flag encroached directions */
775 <                if ((wt >= 0.89*AVGREFL) & (crlp != NULL))
775 >                if (crlp != NULL)
776                          *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc);
777                  if (pg != NULL) {       /* cap gradient if necessary */
778                          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