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.66 by greg, Thu Sep 4 09:09:08 2014 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 > ambsample(                              /* initial ambient division sample */
55 >        AMBHEMI *hp,
56 >        int     i,
57 >        int     j,
58 >        int     n
59   )
60   {
61 <        AMBHEMI *hp;
62 <        double  d;
60 <        int     n, i;
61 <                                        /* set number of divisions */
62 <        if (ambacc <= FTINY &&
63 <                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
64 <                wt = d;                 /* avoid ray termination */
65 <        n = sqrt(ambdiv * wt) + 0.5;
66 <        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
67 <        if (n < i)
68 <                n = i;
69 <                                        /* allocate sampling array */
70 <        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
71 <        if (hp == NULL)
72 <                return(NULL);
73 <        hp->rp = r;
74 <        hp->ns = n;
75 <                                        /* assign coefficient */
76 <        copycolor(hp->acoef, ac);
77 <        d = 1.0/(n*n);
78 <        scalecolor(hp->acoef, d);
79 <                                        /* make tangent plane axes */
80 <        hp->uy[0] = 0.5 - frandom();
81 <        hp->uy[1] = 0.5 - frandom();
82 <        hp->uy[2] = 0.5 - frandom();
83 <        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);
94 < }
95 <
96 <
97 < /* Sample ambient division and apply weighting coefficient */
98 < static int
99 < getambsamp(RAY *arp, AMBHEMI *hp, int i, int j, int n)
100 < {
61 >        AMBSAMP *ap = &ambsam(hp,i,j);
62 >        RAY     ar;
63          int     hlist[3], ii;
64          double  spt[2], zd;
65 +                                        /* generate hemispherical sample */
66                                          /* ambient coefficient for weight */
67          if (ambacc > FTINY)
68 <                setcolor(arp->rcoef, AVGREFL, AVGREFL, AVGREFL);
68 >                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
69          else
70 <                copycolor(arp->rcoef, hp->acoef);
71 <        if (rayorigin(arp, AMBIENT, hp->rp, arp->rcoef) < 0)
70 >                copycolor(ar.rcoef, hp->acoef);
71 >        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0)
72                  return(0);
73          if (ambacc > FTINY) {
74 <                multcolor(arp->rcoef, hp->acoef);
75 <                scalecolor(arp->rcoef, 1./AVGREFL);
74 >                multcolor(ar.rcoef, hp->acoef);
75 >                scalecolor(ar.rcoef, 1./AVGREFL);
76          }
77          hlist[0] = hp->rp->rno;
78          hlist[1] = j;
79          hlist[2] = i;
80          multisamp(spt, 2, urand(ilhash(hlist,3)+n));
81 <        if (!n) {                       /* avoid border samples for n==0 */
81 >                                        /* avoid coincident samples */
82 >        if (!n && (0 < i) & (i < hp->ns-1) &&
83 >                        (0 < j) & (j < hp->ns-1)) {
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))
# Line 124 | Line 89 | getambsamp(RAY *arp, AMBHEMI *hp, int i, int j, int n)
89          SDsquare2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns);
90          zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
91          for (ii = 3; ii--; )
92 <                arp->rdir[ii] = spt[0]*hp->ux[ii] +
92 >                ar.rdir[ii] =   spt[0]*hp->ux[ii] +
93                                  spt[1]*hp->uy[ii] +
94                                  zd*hp->rp->ron[ii];
95 <        checknorm(arp->rdir);
95 >        checknorm(ar.rdir);
96          dimlist[ndims++] = AI(hp,i,j) + 90171;
97 <        rayvalue(arp);                  /* evaluate ray */
98 <        ndims--;                        /* apply coefficient */
99 <        multcolor(arp->rcol, arp->rcoef);
97 >        rayvalue(&ar);                  /* evaluate ray */
98 >        ndims--;
99 >        if (ar.rt <= FTINY)
100 >                return(0);              /* should never happen */
101 >        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
102 >        if (ar.rt*ap->d < 1.0)          /* new/closer distance? */
103 >                ap->d = 1.0/ar.rt;
104 >        if (!n) {                       /* record first vertex & value */
105 >                if (ar.rt > 10.0*thescene.cusize)
106 >                        ar.rt = 10.0*thescene.cusize;
107 >                VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
108 >                copycolor(ap->v, ar.rcol);
109 >        } else {                        /* else update recorded value */
110 >                hp->acol[RED] -= colval(ap->v,RED);
111 >                hp->acol[GRN] -= colval(ap->v,GRN);
112 >                hp->acol[BLU] -= colval(ap->v,BLU);
113 >                zd = 1.0/(double)(n+1);
114 >                scalecolor(ar.rcol, zd);
115 >                zd *= (double)n;
116 >                scalecolor(ap->v, zd);
117 >                addcolor(ap->v, ar.rcol);
118 >        }
119 >        addcolor(hp->acol, ap->v);      /* add to our sum */
120          return(1);
121   }
122  
123  
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
124   /* Estimate errors based on ambient division differences */
125   static float *
126   getambdiffs(AMBHEMI *hp)
# Line 213 | Line 175 | getambdiffs(AMBHEMI *hp)
175  
176   /* Perform super-sampling on hemisphere (introduces bias) */
177   static void
178 < ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
178 > ambsupersamp(AMBHEMI *hp, int cnt)
179   {
180          float   *earr = getambdiffs(hp);
181          double  e2rem = 0;
182          AMBSAMP *ap;
183          RAY     ar;
222        double  asum[3];
184          float   *ep;
185          int     i, j, n, nss;
186  
# Line 234 | Line 195 | ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
195                  if (e2rem <= FTINY)
196                          goto done;      /* nothing left to do */
197                  nss = *ep/e2rem*cnt + frandom();
198 <                asum[0] = asum[1] = asum[2] = 0.0;
199 <                for (n = 1; n <= nss; n++) {
200 <                        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;
198 >                for (n = 1; n <= nss && ambsample(hp,i,j,n); n++)
199 >                        --cnt;
200 >                e2rem -= *ep++;         /* update remainder */
201          }
202   done:
203          free(earr);
204   }
205  
206  
207 + static AMBHEMI *
208 + samp_hemi(                              /* sample indirect hemisphere */
209 +        COLOR   rcol,
210 +        RAY     *r,
211 +        double  wt
212 + )
213 + {
214 +        AMBHEMI *hp;
215 +        double  d;
216 +        int     n, i, j;
217 +                                        /* set number of divisions */
218 +        if (ambacc <= FTINY &&
219 +                        wt > (d = 0.8*intens(rcol)*r->rweight/(ambdiv*minweight)))
220 +                wt = d;                 /* avoid ray termination */
221 +        n = sqrt(ambdiv * wt) + 0.5;
222 +        i = 1 + 8*(ambacc > FTINY);     /* minimum number of samples */
223 +        if (n < i)
224 +                n = i;
225 +                                        /* allocate sampling array */
226 +        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
227 +        if (hp == NULL)
228 +                error(SYSTEM, "out of memory in samp_hemi");
229 +        hp->rp = r;
230 +        hp->ns = n;
231 +        hp->acol[RED] = hp->acol[GRN] = hp->acol[BLU] = 0.0;
232 +        memset(hp->sa, 0, sizeof(AMBSAMP)*n*n);
233 +        hp->sampOK = 0;
234 +                                        /* assign coefficient */
235 +        copycolor(hp->acoef, rcol);
236 +        d = 1.0/(n*n);
237 +        scalecolor(hp->acoef, d);
238 +                                        /* make tangent plane axes */
239 +        hp->uy[0] = 0.5 - frandom();
240 +        hp->uy[1] = 0.5 - frandom();
241 +        hp->uy[2] = 0.5 - frandom();
242 +        for (i = 3; i--; )
243 +                if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
244 +                        break;
245 +        if (i < 0)
246 +                error(CONSISTENCY, "bad ray direction in samp_hemi");
247 +        hp->uy[i] = 1.0;
248 +        VCROSS(hp->ux, hp->uy, r->ron);
249 +        normalize(hp->ux);
250 +        VCROSS(hp->uy, r->ron, hp->ux);
251 +                                        /* sample divisions */
252 +        for (i = hp->ns; i--; )
253 +            for (j = hp->ns; j--; )
254 +                hp->sampOK += ambsample(hp, i, j, 0);
255 +        copycolor(rcol, hp->acol);
256 +        if (!hp->sampOK) {              /* utter failure? */
257 +                free(hp);
258 +                return(NULL);
259 +        }
260 +        if (hp->sampOK < hp->ns*hp->ns) {
261 +                hp->sampOK *= -1;       /* soft failure */
262 +                return(hp);
263 +        }
264 +        n = ambssamp*wt + 0.5;
265 +        if (n > 8) {                    /* perform super-sampling? */
266 +                ambsupersamp(hp, n);
267 +                copycolor(rcol, hp->acol);
268 +        }
269 +        return(hp);                     /* all is well */
270 + }
271 +
272 +
273   /* Return brightness of farthest ambient sample */
274   static double
275   back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3)
# Line 596 | Line 610 | static uint32
610   ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
611   {
612          const double    max_d = 1.0/(minarad*ambacc + 0.001);
613 <        const double    ang_res = 0.5*PI/(hp->ns-1);
614 <        const double    ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY));
613 >        const double    ang_res = 0.5*PI/hp->ns;
614 >        const double    ang_step = ang_res/((int)(16/PI*ang_res) + 1.01);
615          double          avg_d = 0;
616          uint32          flgs = 0;
617 +        FVECT           vec;
618 +        double          u, v;
619 +        double          ang, a1;
620          int             i, j;
621                                          /* don't bother for a few samples */
622          if (hp->ns < 12)
# Line 617 | Line 634 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
634          for (i = 0; i < hp->ns; i++)
635              for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) {
636                  AMBSAMP *ap = &ambsam(hp,i,j);
620                FVECT   vec;
621                double  u, v;
622                double  ang, a1;
623                int     abp;
637                  if ((ap->d <= FTINY) | (ap->d >= max_d))
638                          continue;       /* too far or too near */
639                  VSUB(vec, ap->p, hp->rp->rop);
640 <                u = DOT(vec, uv[0]) * ap->d;
641 <                v = DOT(vec, uv[1]) * ap->d;
642 <                if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0)
640 >                u = DOT(vec, uv[0]);
641 >                v = DOT(vec, uv[1]);
642 >                if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= u*u + v*v)
643                          continue;       /* occluder outside ellipse */
644                  ang = atan2a(v, u);     /* else set direction flags */
645 <                for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step)
645 >                for (a1 = ang-ang_res; a1 <= ang+ang_res; a1 += ang_step)
646                          flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0)));
647              }
648 +                                        /* add low-angle incident (< 20deg) */
649 +        if (fabs(hp->rp->rod) <= 0.342) {
650 +                u = -DOT(hp->rp->rdir, uv[0]);
651 +                v = -DOT(hp->rp->rdir, uv[1]);
652 +                if ((r0*r0*u*u + r1*r1*v*v) > hp->rp->rot*hp->rp->rot) {
653 +                        ang = atan2a(v, u);
654 +                        ang += 2.*PI*(ang < 0);
655 +                        ang *= 16/PI;
656 +                        if ((ang < .5) | (ang >= 31.5))
657 +                                flgs |= 0x80000001;
658 +                        else
659 +                                flgs |= 3L<<(int)(ang-.5);
660 +                }
661 +        }
662          return(flgs);
663   }
664  
# Line 648 | Line 675 | doambient(                             /* compute ambient component */
675          uint32  *crlp                   /* returned (optional) */
676   )
677   {
678 <        AMBHEMI *hp = inithemi(rcol, r, wt);
652 <        int     cnt;
678 >        AMBHEMI *hp = samp_hemi(rcol, r, wt);
679          FVECT   my_uv[2];
680 <        double  d, K, acol[3];
680 >        double  d, K;
681          AMBSAMP *ap;
682 <        int     i, j;
683 <                                        /* check/initialize */
658 <        if (hp == NULL)
659 <                return(0);
682 >        int     i;
683 >                                        /* clear return values */
684          if (uv != NULL)
685                  memset(uv, 0, sizeof(FVECT)*2);
686          if (ra != NULL)
# Line 667 | Line 691 | doambient(                             /* compute ambient component */
691                  dg[0] = dg[1] = 0.0;
692          if (crlp != NULL)
693                  *crlp = 0;
694 <                                        /* sample the hemisphere */
695 <        acol[0] = acol[1] = acol[2] = 0.0;
696 <        cnt = 0;
697 <        for (i = hp->ns; i--; )
698 <                for (j = hp->ns; j--; )
699 <                        if ((ap = ambsample(hp, i, j)) != NULL) {
700 <                                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 */
694 >        if (hp == NULL)                 /* sampling falure? */
695 >                return(0);
696 >
697 >        if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
698 >                        (hp->sampOK < 0) | (hp->ns < 9)) {
699 >                free(hp);               /* Hessian not requested/possible */
700 >                return(-1);             /* value-only return value */
701          }
702 <        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 */
702 >        if ((d = bright(rcol)) > FTINY) {       /* normalize Y values */
703                  d = 0.99*(hp->ns*hp->ns)/d;
704                  K = 0.01;
705          } else {                        /* or fall back on geometric Hessian */
# Line 729 | Line 734 | doambient(                             /* compute ambient component */
734                          if (ra[1] < minarad)
735                                  ra[1] = minarad;
736                  }
737 <                ra[0] *= d = 1.0/sqrt(sqrt(wt));
737 >                ra[0] *= d = 1.0/sqrt(wt);
738                  if ((ra[1] *= d) > 2.0*ra[0])
739                          ra[1] = 2.0*ra[0];
740                  if (ra[1] > maxarad) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines