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.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 < #ifdef NEWAMB
27 > #ifndef OLDAMB
28  
29   extern void             SDsquare2disk(double ds[2], double seedx, double seedy);
30  
# 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 68 | Line 108 | ambsample(                             /* initial ambient division sample */
108                  setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
109          else
110                  copycolor(ar.rcoef, hp->acoef);
111 <        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) {
72 <                if (!n) memset(ap, 0, sizeof(AMBSAMP));
111 >        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0)
112                  return(0);
74        }
113          if (ambacc > FTINY) {
114                  multcolor(ar.rcoef, hp->acoef);
115                  scalecolor(ar.rcoef, 1./AVGREFL);
# Line 80 | 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 <        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 <        }
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--;
137          if (ar.rt <= FTINY)
138                  return(0);              /* should never happen */
139          multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
140 <        if (!n || ar.rt*ap->d < 1.0)    /* new/closer distance? */
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 195 | Line 233 | ambsupersamp(AMBHEMI *hp, int cnt)
233                  if (e2rem <= FTINY)
234                          goto done;      /* nothing left to do */
235                  nss = *ep/e2rem*cnt + frandom();
236 <                for (n = 1; n <= nss; n++)
237 <                        cnt -= ambsample(hp, i, j, n);
236 >                for (n = 1; n <= nss && ambsample(hp,i,j,n); n++)
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)))
# Line 229 | Line 270 | samp_hemi(                             /* sample indirect hemisphere */
270          hp->rp = r;
271          hp->ns = n;
272          hp->acol[RED] = hp->acol[GRN] = hp->acol[BLU] = 0.0;
273 +        memset(hp->sa, 0, sizeof(AMBSAMP)*n*n);
274          hp->sampOK = 0;
275                                          /* assign coefficient */
276          copycolor(hp->acoef, rcol);
277          d = 1.0/(n*n);
278          scalecolor(hp->acoef, d);
279                                          /* make tangent plane axes */
280 <        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)
280 >        if (!getperpendicular(hp->ux, r->ron, 1))
281                  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);
282          VCROSS(hp->uy, r->ron, hp->ux);
283                                          /* sample divisions */
284          for (i = hp->ns; i--; )
# Line 609 | Line 642 | static uint32
642   ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
643   {
644          const double    max_d = 1.0/(minarad*ambacc + 0.001);
645 <        const double    ang_res = 0.5*PI/(hp->ns-1);
646 <        const double    ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY));
645 >        const double    ang_res = 0.5*PI/hp->ns;
646 >        const double    ang_step = ang_res/((int)(16/PI*ang_res) + 1.01);
647          double          avg_d = 0;
648          uint32          flgs = 0;
649          FVECT           vec;
650 <        double          d, u, v;
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 636 | Line 670 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
670                  if ((ap->d <= FTINY) | (ap->d >= max_d))
671                          continue;       /* too far or too near */
672                  VSUB(vec, ap->p, hp->rp->rop);
673 <                d = DOT(vec, hp->rp->ron);
674 <                d = 1.0/sqrt(DOT(vec,vec) - d*d);
675 <                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)
673 >                u = DOT(vec, uv[0]);
674 >                v = DOT(vec, uv[1]);
675 >                if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= u*u + v*v)
676                          continue;       /* occluder outside ellipse */
677                  ang = atan2a(v, u);     /* else set direction flags */
678 <                for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step)
678 >                for (a1 = ang-ang_res; a1 <= ang+ang_res; a1 += ang_step)
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 696 | Line 730 | doambient(                             /* compute ambient component */
730                  return(0);
731  
732          if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
733 <                        (hp->sampOK < 0) | (hp->ns < 4)) {
733 >                        (hp->sampOK < 0) | (hp->ns < 6)) {
734                  free(hp);               /* Hessian not requested/possible */
735                  return(-1);             /* value-only return value */
736          }
# Line 744 | 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