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.61 by greg, Sun May 18 18:59:55 2014 UTC vs.
Revision 2.74 by greg, Fri Oct 14 19:15:34 2016 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/10th division */
64 +        cos_thresh = (PI/10.)/(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 68 | Line 105 | ambsample(                             /* initial ambient division sample */
105                  setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
106          else
107                  copycolor(ar.rcoef, hp->acoef);
108 <        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) {
72 <                if (!n) memset(ap, 0, sizeof(AMBSAMP));
108 >        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0)
109                  return(0);
74        }
110          if (ambacc > FTINY) {
111                  multcolor(ar.rcoef, hp->acoef);
112                  scalecolor(ar.rcoef, 1./AVGREFL);
# Line 80 | 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 */
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 <        }
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 93 | 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;
130 +        }
131          dimlist[ndims++] = AI(hp,i,j) + 90171;
132          rayvalue(&ar);                  /* evaluate ray */
133          ndims--;
134          if (ar.rt <= FTINY)
135                  return(0);              /* should never happen */
136          multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
137 <        if (!n || ar.rt*ap->d < 1.0)    /* new/closer distance? */
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)
# Line 180 | Line 215 | ambsupersamp(AMBHEMI *hp, int cnt)
215          float   *earr = getambdiffs(hp);
216          double  e2rem = 0;
217          AMBSAMP *ap;
183        RAY     ar;
218          float   *ep;
219          int     i, j, n, nss;
220  
# Line 195 | Line 229 | ambsupersamp(AMBHEMI *hp, int cnt)
229                  if (e2rem <= FTINY)
230                          goto done;      /* nothing left to do */
231                  nss = *ep/e2rem*cnt + frandom();
232 <                for (n = 1; n <= nss; n++)
233 <                        cnt -= ambsample(hp, i, j, n);
232 >                for (n = 1; n <= nss && ambsample(hp,i,j,n); n++)
233 >                        --cnt;
234                  e2rem -= *ep++;         /* update remainder */
235          }
236   done:
# Line 229 | Line 263 | samp_hemi(                             /* sample indirect hemisphere */
263          hp->rp = r;
264          hp->ns = n;
265          hp->acol[RED] = hp->acol[GRN] = hp->acol[BLU] = 0.0;
266 +        memset(hp->sa, 0, sizeof(AMBSAMP)*n*n);
267          hp->sampOK = 0;
268                                          /* assign coefficient */
269          copycolor(hp->acoef, rcol);
270          d = 1.0/(n*n);
271          scalecolor(hp->acoef, d);
272                                          /* make tangent plane axes */
273 <        hp->uy[0] = 0.5 - frandom();
239 <        hp->uy[1] = 0.5 - frandom();
240 <        hp->uy[2] = 0.5 - frandom();
241 <        for (i = 3; i--; )
242 <                if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
243 <                        break;
244 <        if (i < 0)
273 >        if (!getperpendicular(hp->ux, r->ron, 1))
274                  error(CONSISTENCY, "bad ray direction in samp_hemi");
246        hp->uy[i] = 1.0;
247        VCROSS(hp->ux, hp->uy, r->ron);
248        normalize(hp->ux);
275          VCROSS(hp->uy, r->ron, hp->ux);
276                                          /* sample divisions */
277          for (i = hp->ns; i--; )
# Line 609 | Line 635 | static uint32
635   ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
636   {
637          const double    max_d = 1.0/(minarad*ambacc + 0.001);
638 <        const double    ang_res = 0.5*PI/(hp->ns-1);
639 <        const double    ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY));
638 >        const double    ang_res = 0.5*PI/hp->ns;
639 >        const double    ang_step = ang_res/((int)(16/PI*ang_res) + 1.01);
640          double          avg_d = 0;
641          uint32          flgs = 0;
642          FVECT           vec;
643 <        double          d, u, v;
643 >        double          u, v;
644          double          ang, a1;
645          int             i, j;
646                                          /* don't bother for a few samples */
647 <        if (hp->ns < 12)
647 >        if (hp->ns < 8)
648                  return(0);
649                                          /* check distances overhead */
650          for (i = hp->ns*3/4; i-- > hp->ns>>2; )
# Line 636 | Line 662 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
662                  if ((ap->d <= FTINY) | (ap->d >= max_d))
663                          continue;       /* too far or too near */
664                  VSUB(vec, ap->p, hp->rp->rop);
665 <                d = DOT(vec, hp->rp->ron);
666 <                d = 1.0/sqrt(DOT(vec,vec) - d*d);
667 <                u = DOT(vec, uv[0]) * d;
642 <                v = DOT(vec, uv[1]) * d;
643 <                if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0)
665 >                u = DOT(vec, uv[0]);
666 >                v = DOT(vec, uv[1]);
667 >                if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= u*u + v*v)
668                          continue;       /* occluder outside ellipse */
669                  ang = atan2a(v, u);     /* else set direction flags */
670 <                for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step)
670 >                for (a1 = ang-ang_res; a1 <= ang+ang_res; a1 += ang_step)
671                          flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0)));
672              }
673                                          /* add low-angle incident (< 20deg) */
# Line 696 | Line 720 | doambient(                             /* compute ambient component */
720                  return(0);
721  
722          if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
723 <                        (hp->sampOK < 0) | (hp->ns < 4)) {
723 >                        (hp->sampOK < 0) | (hp->ns < 6)) {
724                  free(hp);               /* Hessian not requested/possible */
725                  return(-1);             /* value-only return value */
726          }
# Line 744 | Line 768 | doambient(                             /* compute ambient component */
768                                  ra[0] = maxarad;
769                  }
770                                          /* flag encroached directions */
771 <                if ((wt >= 0.89*AVGREFL) & (crlp != NULL))
771 >                if (crlp != NULL)
772                          *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc);
773                  if (pg != NULL) {       /* cap gradient if necessary */
774                          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