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.57 by greg, Fri May 9 22:53:11 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 33 | Line 36 | typedef struct {
36  
37   typedef struct {
38          RAY     *rp;            /* originating ray sample */
36        FVECT   ux, uy;         /* tangent axis unit vectors */
39          int     ns;             /* number of samples per axis */
40 +        int     sampOK;         /* acquired full sample set? */
41          COLOR   acoef;          /* division contribution coefficient */
42 +        double  acol[3];        /* accumulated color */
43 +        FVECT   ux, uy;         /* tangent axis unit vectors */
44          AMBSAMP sa[1];          /* sample array (extends struct) */
45   }  AMBHEMI;             /* ambient sample hemisphere */
46  
# Line 48 | Line 53 | typedef struct {
53   } FFTRI;                /* vectors and coefficients for Hessian calculation */
54  
55  
56 < static AMBHEMI *
57 < inithemi(                       /* initialize sampling hemisphere */
58 <        COLOR   ac,
59 <        RAY     *r,
60 <        double  wt
56 > static int
57 > ambcollision(                           /* proposed direciton collides? */
58 >        AMBHEMI *hp,
59 >        int     i,
60 >        int     j,
61 >        FVECT   dv
62   )
63   {
64 <        AMBHEMI *hp;
65 <        double  d;
66 <        int     n, i;
67 <                                        /* set number of divisions */
68 <        if (ambacc <= FTINY &&
69 <                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
70 <                wt = d;                 /* avoid ray termination */
71 <        n = sqrt(ambdiv * wt) + 0.5;
72 <        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
73 <        if (n < i)
74 <                n = i;
75 <                                        /* allocate sampling array */
76 <        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
77 <        if (hp == NULL)
78 <                return(NULL);
79 <        hp->rp = r;
80 <        hp->ns = n;
81 <                                        /* assign coefficient */
82 <        copycolor(hp->acoef, ac);
83 <        d = 1.0/(n*n);
84 <        scalecolor(hp->acoef, d);
85 <                                        /* make tangent plane axes */
86 <        hp->uy[0] = 0.5 - frandom();
87 <        hp->uy[1] = 0.5 - frandom();
88 <        hp->uy[2] = 0.5 - frandom();
89 <        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);
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  
97 /* Sample ambient division and apply weighting coefficient */
93   static int
94 < getambsamp(RAY *arp, AMBHEMI *hp, int i, int j, int n)
94 > ambsample(                              /* initial ambient division sample */
95 >        AMBHEMI *hp,
96 >        int     i,
97 >        int     j,
98 >        int     n
99 > )
100   {
101 +        AMBSAMP *ap = &ambsam(hp,i,j);
102 +        RAY     ar;
103          int     hlist[3], ii;
104          double  spt[2], zd;
105 +                                        /* generate hemispherical sample */
106                                          /* ambient coefficient for weight */
107          if (ambacc > FTINY)
108 <                setcolor(arp->rcoef, AVGREFL, AVGREFL, AVGREFL);
108 >                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
109          else
110 <                copycolor(arp->rcoef, hp->acoef);
111 <        if (rayorigin(arp, AMBIENT, hp->rp, arp->rcoef) < 0)
110 >                copycolor(ar.rcoef, hp->acoef);
111 >        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0)
112                  return(0);
113          if (ambacc > FTINY) {
114 <                multcolor(arp->rcoef, hp->acoef);
115 <                scalecolor(arp->rcoef, 1./AVGREFL);
114 >                multcolor(ar.rcoef, hp->acoef);
115 >                scalecolor(ar.rcoef, 1./AVGREFL);
116          }
117          hlist[0] = hp->rp->rno;
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 */
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 <        }
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--; )
125 <                arp->rdir[ii] = spt[0]*hp->ux[ii] +
125 >                ar.rdir[ii] =   spt[0]*hp->ux[ii] +
126                                  spt[1]*hp->uy[ii] +
127                                  zd*hp->rp->ron[ii];
128 <        checknorm(arp->rdir);
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(arp);                  /* evaluate ray */
136 <        ndims--;                        /* apply coefficient */
137 <        multcolor(arp->rcol, arp->rcoef);
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 (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 + 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 */
148 >                hp->acol[RED] -= colval(ap->v,RED);
149 >                hp->acol[GRN] -= colval(ap->v,GRN);
150 >                hp->acol[BLU] -= colval(ap->v,BLU);
151 >                zd = 1.0/(double)(n+1);
152 >                scalecolor(ar.rcol, zd);
153 >                zd *= (double)n;
154 >                scalecolor(ap->v, zd);
155 >                addcolor(ap->v, ar.rcol);
156 >        }
157 >        addcolor(hp->acol, ap->v);      /* add to our sum */
158          return(1);
159   }
160  
161  
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
162   /* Estimate errors based on ambient division differences */
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 176 | 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 213 | Line 214 | getambdiffs(AMBHEMI *hp)
214  
215   /* Perform super-sampling on hemisphere (introduces bias) */
216   static void
217 < ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
217 > ambsupersamp(AMBHEMI *hp, int cnt)
218   {
219          float   *earr = getambdiffs(hp);
220          double  e2rem = 0;
221          AMBSAMP *ap;
221        RAY     ar;
222        double  asum[3];
222          float   *ep;
223          int     i, j, n, nss;
224  
# Line 234 | Line 233 | ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
233                  if (e2rem <= FTINY)
234                          goto done;      /* nothing left to do */
235                  nss = *ep/e2rem*cnt + frandom();
236 <                asum[0] = asum[1] = asum[2] = 0.0;
237 <                for (n = 1; n <= nss; n++) {
238 <                        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;
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:
241          free(earr);
242   }
243  
244  
245 + static AMBHEMI *
246 + samp_hemi(                              /* sample indirect hemisphere */
247 +        COLOR   rcol,
248 +        RAY     *r,
249 +        double  wt
250 + )
251 + {
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 + 5*(ambacc > FTINY);     /* minimum number of samples */
264 +        if (n < i)
265 +                n = i;
266 +                                        /* allocate sampling array */
267 +        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
268 +        if (hp == NULL)
269 +                error(SYSTEM, "out of memory in samp_hemi");
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 +        if (!getperpendicular(hp->ux, r->ron, 1))
281 +                error(CONSISTENCY, "bad ray direction in samp_hemi");
282 +        VCROSS(hp->uy, r->ron, hp->ux);
283 +                                        /* sample divisions */
284 +        for (i = hp->ns; i--; )
285 +            for (j = hp->ns; j--; )
286 +                hp->sampOK += ambsample(hp, i, j, 0);
287 +        copycolor(rcol, hp->acol);
288 +        if (!hp->sampOK) {              /* utter failure? */
289 +                free(hp);
290 +                return(NULL);
291 +        }
292 +        if (hp->sampOK < hp->ns*hp->ns) {
293 +                hp->sampOK *= -1;       /* soft failure */
294 +                return(hp);
295 +        }
296 +        n = ambssamp*wt + 0.5;
297 +        if (n > 8) {                    /* perform super-sampling? */
298 +                ambsupersamp(hp, n);
299 +                copycolor(rcol, hp->acol);
300 +        }
301 +        return(hp);                     /* all is well */
302 + }
303 +
304 +
305   /* Return brightness of farthest ambient sample */
306   static double
307   back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3)
# Line 596 | 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          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 617 | Line 667 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
667          for (i = 0; i < hp->ns; i++)
668              for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) {
669                  AMBSAMP *ap = &ambsam(hp,i,j);
620                FVECT   vec;
621                double  u, v;
622                double  ang, a1;
623                int     abp;
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 <                u = DOT(vec, uv[0]) * ap->d;
674 <                v = DOT(vec, uv[1]) * ap->d;
675 <                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 && 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) {
688 +                        ang = atan2a(v, u);
689 +                        ang += 2.*PI*(ang < 0);
690 +                        ang *= 16/PI;
691 +                        if ((ang < .5) | (ang >= 31.5))
692 +                                flgs |= 0x80000001;
693 +                        else
694 +                                flgs |= 3L<<(int)(ang-.5);
695 +                }
696 +        }
697          return(flgs);
698   }
699  
# Line 648 | Line 710 | doambient(                             /* compute ambient component */
710          uint32  *crlp                   /* returned (optional) */
711   )
712   {
713 <        AMBHEMI *hp = inithemi(rcol, r, wt);
652 <        int     cnt;
713 >        AMBHEMI *hp = samp_hemi(rcol, r, wt);
714          FVECT   my_uv[2];
715 <        double  d, K, acol[3];
715 >        double  d, K;
716          AMBSAMP *ap;
717 <        int     i, j;
718 <                                        /* check/initialize */
658 <        if (hp == NULL)
659 <                return(0);
717 >        int     i;
718 >                                        /* clear return values */
719          if (uv != NULL)
720                  memset(uv, 0, sizeof(FVECT)*2);
721          if (ra != NULL)
# Line 667 | Line 726 | doambient(                             /* compute ambient component */
726                  dg[0] = dg[1] = 0.0;
727          if (crlp != NULL)
728                  *crlp = 0;
729 <                                        /* sample the hemisphere */
730 <        acol[0] = acol[1] = acol[2] = 0.0;
731 <        cnt = 0;
732 <        for (i = hp->ns; i--; )
733 <                for (j = hp->ns; j--; )
734 <                        if ((ap = ambsample(hp, i, j)) != NULL) {
735 <                                addcolor(acol, ap->v);
677 <                                ++cnt;
678 <                        }
679 <        if (!cnt) {
680 <                setcolor(rcol, 0.0, 0.0, 0.0);
681 <                free(hp);
682 <                return(0);              /* no valid samples */
729 >        if (hp == NULL)                 /* sampling falure? */
730 >                return(0);
731 >
732 >        if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
733 >                        (hp->sampOK < 0) | (hp->ns < 6)) {
734 >                free(hp);               /* Hessian not requested/possible */
735 >                return(-1);             /* value-only return value */
736          }
737 <        if (cnt < hp->ns*hp->ns) {      /* incomplete sampling? */
685 <                copycolor(rcol, acol);
686 <                free(hp);
687 <                return(-1);             /* return value w/o Hessian */
688 <        }
689 <        cnt = ambssamp*wt + 0.5;        /* perform super-sampling? */
690 <        if (cnt > 8)
691 <                ambsupersamp(acol, hp, cnt);
692 <        copycolor(rcol, acol);          /* final indirect irradiance/PI */
693 <        if ((ra == NULL) & (pg == NULL) & (dg == NULL)) {
694 <                free(hp);
695 <                return(-1);             /* no radius or gradient calc. */
696 <        }
697 <        if ((d = bright(acol)) > FTINY) {       /* normalize Y values */
737 >        if ((d = bright(rcol)) > FTINY) {       /* normalize Y values */
738                  d = 0.99*(hp->ns*hp->ns)/d;
739                  K = 0.01;
740          } else {                        /* or fall back on geometric Hessian */
# Line 729 | Line 769 | doambient(                             /* compute ambient component */
769                          if (ra[1] < minarad)
770                                  ra[1] = minarad;
771                  }
772 <                ra[0] *= d = 1.0/sqrt(sqrt(wt));
772 >                ra[0] *= d = 1.0/sqrt(wt);
773                  if ((ra[1] *= d) > 2.0*ra[0])
774                          ra[1] = 2.0*ra[0];
775                  if (ra[1] > maxarad) {
# Line 738 | 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