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.59 by greg, Fri May 16 23:39:24 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 33 | Line 33 | typedef struct {
33  
34   typedef struct {
35          RAY     *rp;            /* originating ray sample */
36        FVECT   ux, uy;         /* tangent axis unit vectors */
36          int     ns;             /* number of samples per axis */
37 +        int     sampOK;         /* acquired full sample set? */
38          COLOR   acoef;          /* division contribution coefficient */
39 +        double  acol[3];        /* accumulated color */
40 +        FVECT   ux, uy;         /* tangent axis unit vectors */
41          AMBSAMP sa[1];          /* sample array (extends struct) */
42   }  AMBHEMI;             /* ambient sample hemisphere */
43  
# Line 48 | Line 50 | typedef struct {
50   } FFTRI;                /* vectors and coefficients for Hessian calculation */
51  
52  
53 < static AMBHEMI *
54 < inithemi(                       /* initialize sampling hemisphere */
55 <        COLOR   ac,
56 <        RAY     *r,
57 <        double  wt
53 > static int
54 > ambcollision(                           /* proposed direciton collides? */
55 >        AMBHEMI *hp,
56 >        int     i,
57 >        int     j,
58 >        FVECT   dv
59   )
60   {
61 <        AMBHEMI *hp;
62 <        double  d;
63 <        int     n, i;
64 <                                        /* set number of divisions */
65 <        if (ambacc <= FTINY &&
66 <                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
67 <                wt = d;                 /* avoid ray termination */
68 <        n = sqrt(ambdiv * wt) + 0.5;
69 <        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
70 <        if (n < i)
71 <                n = i;
72 <                                        /* allocate sampling array */
73 <        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
74 <        if (hp == NULL)
75 <                return(NULL);
76 <        hp->rp = r;
77 <        hp->ns = n;
78 <                                        /* assign coefficient */
79 <        copycolor(hp->acoef, ac);
80 <        d = 1.0/(n*n);
81 <        scalecolor(hp->acoef, d);
82 <                                        /* make tangent plane axes */
83 <        hp->uy[0] = 0.5 - frandom();
84 <        hp->uy[1] = 0.5 - frandom();
85 <        hp->uy[2] = 0.5 - frandom();
86 <        for (i = 3; i--; )
84 <                if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
85 <                        break;
86 <        if (i < 0)
87 <                error(CONSISTENCY, "bad ray direction in inithemi");
88 <        hp->uy[i] = 1.0;
89 <        VCROSS(hp->ux, hp->uy, r->ron);
90 <        normalize(hp->ux);
91 <        VCROSS(hp->uy, r->ron, hp->ux);
92 <                                        /* we're ready to sample */
93 <        return(hp);
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  
97 /* Sample ambient division and apply weighting coefficient */
90   static int
91 < getambsamp(RAY *arp, AMBHEMI *hp, int i, int j, int n)
91 > ambsample(                              /* initial ambient division sample */
92 >        AMBHEMI *hp,
93 >        int     i,
94 >        int     j,
95 >        int     n
96 > )
97   {
98 +        AMBSAMP *ap = &ambsam(hp,i,j);
99 +        RAY     ar;
100          int     hlist[3], ii;
101          double  spt[2], zd;
102 +                                        /* generate hemispherical sample */
103                                          /* ambient coefficient for weight */
104          if (ambacc > FTINY)
105 <                setcolor(arp->rcoef, AVGREFL, AVGREFL, AVGREFL);
105 >                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
106          else
107 <                copycolor(arp->rcoef, hp->acoef);
108 <        if (rayorigin(arp, AMBIENT, hp->rp, arp->rcoef) < 0)
107 >                copycolor(ar.rcoef, hp->acoef);
108 >        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0)
109                  return(0);
110          if (ambacc > FTINY) {
111 <                multcolor(arp->rcoef, hp->acoef);
112 <                scalecolor(arp->rcoef, 1./AVGREFL);
111 >                multcolor(ar.rcoef, hp->acoef);
112 >                scalecolor(ar.rcoef, 1./AVGREFL);
113          }
114          hlist[0] = hp->rp->rno;
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 */
119 <                if ((spt[0] < 0.1) | (spt[0] >= 0.9))
120 <                        spt[0] = 0.1 + 0.8*frandom();
121 <                if ((spt[1] < 0.1) | (spt[1] >= 0.9))
122 <                        spt[1] = 0.1 + 0.8*frandom();
123 <        }
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--; )
122 <                arp->rdir[ii] = spt[0]*hp->ux[ii] +
122 >                ar.rdir[ii] =   spt[0]*hp->ux[ii] +
123                                  spt[1]*hp->uy[ii] +
124                                  zd*hp->rp->ron[ii];
125 <        checknorm(arp->rdir);
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(arp);                  /* evaluate ray */
133 <        ndims--;                        /* apply coefficient */
134 <        multcolor(arp->rcol, arp->rcoef);
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 (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 + 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 */
145 >                hp->acol[RED] -= colval(ap->v,RED);
146 >                hp->acol[GRN] -= colval(ap->v,GRN);
147 >                hp->acol[BLU] -= colval(ap->v,BLU);
148 >                zd = 1.0/(double)(n+1);
149 >                scalecolor(ar.rcol, zd);
150 >                zd *= (double)n;
151 >                scalecolor(ap->v, zd);
152 >                addcolor(ap->v, ar.rcol);
153 >        }
154 >        addcolor(hp->acol, ap->v);      /* add to our sum */
155          return(1);
156   }
157  
158  
139 static AMBSAMP *
140 ambsample(                              /* initial ambient division sample */
141        AMBHEMI *hp,
142        int     i,
143        int     j
144 )
145 {
146        AMBSAMP *ap = &ambsam(hp,i,j);
147        RAY     ar;
148                                        /* generate hemispherical sample */
149        if (!getambsamp(&ar, hp, i, j, 0) || ar.rt <= FTINY) {
150                memset(ap, 0, sizeof(AMBSAMP));
151                return(NULL);
152        }
153        ap->d = 1.0/ar.rt;              /* limit vertex distance */
154        if (ar.rt > 10.0*thescene.cusize)
155                ar.rt = 10.0*thescene.cusize;
156        VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
157        copycolor(ap->v, ar.rcol);
158        return(ap);
159 }
160
161
159   /* Estimate errors based on ambient division differences */
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 176 | 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 213 | Line 211 | getambdiffs(AMBHEMI *hp)
211  
212   /* Perform super-sampling on hemisphere (introduces bias) */
213   static void
214 < ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
214 > ambsupersamp(AMBHEMI *hp, int cnt)
215   {
216          float   *earr = getambdiffs(hp);
217          double  e2rem = 0;
218          AMBSAMP *ap;
221        RAY     ar;
222        double  asum[3];
219          float   *ep;
220          int     i, j, n, nss;
221  
# Line 234 | Line 230 | ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
230                  if (e2rem <= FTINY)
231                          goto done;      /* nothing left to do */
232                  nss = *ep/e2rem*cnt + frandom();
233 <                asum[0] = asum[1] = asum[2] = 0.0;
234 <                for (n = 1; n <= nss; n++) {
235 <                        if (!getambsamp(&ar, hp, i, j, n)) {
240 <                                nss = n-1;
241 <                                break;
242 <                        }
243 <                        addcolor(asum, ar.rcol);
244 <                }
245 <                if (nss) {              /* update returned ambient value */
246 <                        const double    ssf = 1./(nss + 1.);
247 <                        for (n = 3; n--; )
248 <                                acol[n] += ssf*asum[n] +
249 <                                                (ssf - 1.)*colval(ap->v,n);
250 <                }
251 <                e2rem -= *ep++;         /* update remainders */
252 <                cnt -= nss;
233 >                for (n = 1; n <= nss && ambsample(hp,i,j,n); n++)
234 >                        if (!--cnt) goto done;
235 >                e2rem -= *ep++;         /* update remainder */
236          }
237   done:
238          free(earr);
239   }
240  
241  
242 + static AMBHEMI *
243 + samp_hemi(                              /* sample indirect hemisphere */
244 +        COLOR   rcol,
245 +        RAY     *r,
246 +        double  wt
247 + )
248 + {
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)))
258 +                wt = d;                 /* avoid ray termination */
259 +        n = sqrt(ambdiv * wt) + 0.5;
260 +        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
261 +        if (n < i)
262 +                n = i;
263 +                                        /* allocate sampling array */
264 +        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
265 +        if (hp == NULL)
266 +                error(SYSTEM, "out of memory in samp_hemi");
267 +        hp->rp = r;
268 +        hp->ns = n;
269 +        hp->acol[RED] = hp->acol[GRN] = hp->acol[BLU] = 0.0;
270 +        memset(hp->sa, 0, sizeof(AMBSAMP)*n*n);
271 +        hp->sampOK = 0;
272 +                                        /* assign coefficient */
273 +        copycolor(hp->acoef, rcol);
274 +        d = 1.0/(n*n);
275 +        scalecolor(hp->acoef, d);
276 +                                        /* make tangent plane axes */
277 +        if (!getperpendicular(hp->ux, r->ron, 1))
278 +                error(CONSISTENCY, "bad ray direction in samp_hemi");
279 +        VCROSS(hp->uy, r->ron, hp->ux);
280 +                                        /* sample divisions */
281 +        for (i = hp->ns; i--; )
282 +            for (j = hp->ns; j--; )
283 +                hp->sampOK += ambsample(hp, i, j, 0);
284 +        copycolor(rcol, hp->acol);
285 +        if (!hp->sampOK) {              /* utter failure? */
286 +                free(hp);
287 +                return(NULL);
288 +        }
289 +        if (hp->sampOK < hp->ns*hp->ns) {
290 +                hp->sampOK *= -1;       /* soft failure */
291 +                return(hp);
292 +        }
293 +        n = ambssamp*wt + 0.5;
294 +        if (n > 8) {                    /* perform super-sampling? */
295 +                ambsupersamp(hp, n);
296 +                copycolor(rcol, hp->acol);
297 +        }
298 +        return(hp);                     /* all is well */
299 + }
300 +
301 +
302   /* Return brightness of farthest ambient sample */
303   static double
304   back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3)
# Line 596 | 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 605 | 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 623 | Line 666 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
666                  if ((ap->d <= FTINY) | (ap->d >= max_d))
667                          continue;       /* too far or too near */
668                  VSUB(vec, ap->p, hp->rp->rop);
669 <                u = DOT(vec, uv[0]) * ap->d;
670 <                v = DOT(vec, uv[1]) * ap->d;
671 <                if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0)
669 >                u = DOT(vec, uv[0]);
670 >                v = DOT(vec, uv[1]);
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 661 | Line 704 | doambient(                             /* compute ambient component */
704          uint32  *crlp                   /* returned (optional) */
705   )
706   {
707 <        AMBHEMI *hp = inithemi(rcol, r, wt);
665 <        int     cnt;
707 >        AMBHEMI *hp = samp_hemi(rcol, r, wt);
708          FVECT   my_uv[2];
709 <        double  d, K, acol[3];
709 >        double  d, K;
710          AMBSAMP *ap;
711 <        int     i, j;
712 <                                        /* check/initialize */
671 <        if (hp == NULL)
672 <                return(0);
711 >        int     i;
712 >                                        /* clear return values */
713          if (uv != NULL)
714                  memset(uv, 0, sizeof(FVECT)*2);
715          if (ra != NULL)
# Line 680 | Line 720 | doambient(                             /* compute ambient component */
720                  dg[0] = dg[1] = 0.0;
721          if (crlp != NULL)
722                  *crlp = 0;
723 <                                        /* sample the hemisphere */
724 <        acol[0] = acol[1] = acol[2] = 0.0;
725 <        cnt = 0;
726 <        for (i = hp->ns; i--; )
727 <                for (j = hp->ns; j--; )
728 <                        if ((ap = ambsample(hp, i, j)) != NULL) {
729 <                                addcolor(acol, ap->v);
690 <                                ++cnt;
691 <                        }
692 <        if ((hp->ns < 4) | (cnt < hp->ns*hp->ns)) {
693 <                free(hp);               /* inadequate sampling */
694 <                copycolor(rcol, acol);
695 <                return(-cnt);           /* value-only result */
723 >        if (hp == NULL)                 /* sampling falure? */
724 >                return(0);
725 >
726 >        if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
727 >                        (hp->sampOK < 0) | (hp->ns < 6)) {
728 >                free(hp);               /* Hessian not requested/possible */
729 >                return(-1);             /* value-only return value */
730          }
731 <        cnt = ambssamp*wt + 0.5;        /* perform super-sampling? */
698 <        if (cnt > 8)
699 <                ambsupersamp(acol, hp, cnt);
700 <        copycolor(rcol, acol);          /* final indirect irradiance/PI */
701 <        if ((ra == NULL) & (pg == NULL) & (dg == NULL)) {
702 <                free(hp);
703 <                return(-1);             /* no Hessian or gradients requested */
704 <        }
705 <        if ((d = bright(acol)) > FTINY) {       /* normalize Y values */
731 >        if ((d = bright(rcol)) > FTINY) {       /* normalize Y values */
732                  d = 0.99*(hp->ns*hp->ns)/d;
733                  K = 0.01;
734          } else {                        /* or fall back on geometric Hessian */
# Line 737 | Line 763 | doambient(                             /* compute ambient component */
763                          if (ra[1] < minarad)
764                                  ra[1] = minarad;
765                  }
766 <                ra[0] *= d = 1.0/sqrt(sqrt(wt));
766 >                ra[0] *= d = 1.0/sqrt(wt);
767                  if ((ra[1] *= d) > 2.0*ra[0])
768                          ra[1] = 2.0*ra[0];
769                  if (ra[1] > maxarad) {
# Line 746 | 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