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.60 by greg, Sat May 17 00:49:17 2014 UTC vs.
Revision 2.61 by greg, Sun May 18 18:59:55 2014 UTC

# 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 >                if (!n) memset(ap, 0, sizeof(AMBSAMP));
73                  return(0);
74 +        }
75          if (ambacc > FTINY) {
76 <                multcolor(arp->rcoef, hp->acoef);
77 <                scalecolor(arp->rcoef, 1./AVGREFL);
76 >                multcolor(ar.rcoef, hp->acoef);
77 >                scalecolor(ar.rcoef, 1./AVGREFL);
78          }
79          hlist[0] = hp->rp->rno;
80          hlist[1] = j;
# 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 (!n || 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; n++)
199 >                        cnt -= ambsample(hp, i, j, n);
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 + 5*(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 +        hp->sampOK = 0;
233 +                                        /* assign coefficient */
234 +        copycolor(hp->acoef, rcol);
235 +        d = 1.0/(n*n);
236 +        scalecolor(hp->acoef, d);
237 +                                        /* make tangent plane axes */
238 +        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)
245 +                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);
249 +        VCROSS(hp->uy, r->ron, hp->ux);
250 +                                        /* sample divisions */
251 +        for (i = hp->ns; i--; )
252 +            for (j = hp->ns; j--; )
253 +                hp->sampOK += ambsample(hp, i, j, 0);
254 +        copycolor(rcol, hp->acol);
255 +        if (!hp->sampOK) {              /* utter failure? */
256 +                free(hp);
257 +                return(NULL);
258 +        }
259 +        if (hp->sampOK < hp->ns*hp->ns) {
260 +                hp->sampOK *= -1;       /* soft failure */
261 +                return(hp);
262 +        }
263 +        n = ambssamp*wt + 0.5;
264 +        if (n > 8) {                    /* perform super-sampling? */
265 +                ambsupersamp(hp, n);
266 +                copycolor(rcol, hp->acol);
267 +        }
268 +        return(hp);                     /* all is well */
269 + }
270 +
271 +
272   /* Return brightness of farthest ambient sample */
273   static double
274   back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3)
# Line 601 | Line 614 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
614          double          avg_d = 0;
615          uint32          flgs = 0;
616          FVECT           vec;
617 <        double          u, v;
617 >        double          d, u, v;
618          double          ang, a1;
619          int             i, j;
620                                          /* don't bother for a few samples */
# Line 623 | Line 636 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
636                  if ((ap->d <= FTINY) | (ap->d >= max_d))
637                          continue;       /* too far or too near */
638                  VSUB(vec, ap->p, hp->rp->rop);
639 <                u = DOT(vec, uv[0]) * ap->d;
640 <                v = DOT(vec, uv[1]) * ap->d;
639 >                d = DOT(vec, hp->rp->ron);
640 >                d = 1.0/sqrt(DOT(vec,vec) - d*d);
641 >                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)
644                          continue;       /* occluder outside ellipse */
645                  ang = atan2a(v, u);     /* else set direction flags */
# Line 661 | Line 676 | doambient(                             /* compute ambient component */
676          uint32  *crlp                   /* returned (optional) */
677   )
678   {
679 <        AMBHEMI *hp = inithemi(rcol, r, wt);
665 <        int     cnt;
679 >        AMBHEMI *hp = samp_hemi(rcol, r, wt);
680          FVECT   my_uv[2];
681 <        double  d, K, acol[3];
681 >        double  d, K;
682          AMBSAMP *ap;
683 <        int     i, j;
684 <                                        /* check/initialize */
671 <        if (hp == NULL)
672 <                return(0);
683 >        int     i;
684 >                                        /* clear return values */
685          if (uv != NULL)
686                  memset(uv, 0, sizeof(FVECT)*2);
687          if (ra != NULL)
# Line 680 | Line 692 | doambient(                             /* compute ambient component */
692                  dg[0] = dg[1] = 0.0;
693          if (crlp != NULL)
694                  *crlp = 0;
695 <                                        /* sample the hemisphere */
696 <        acol[0] = acol[1] = acol[2] = 0.0;
697 <        cnt = 0;
698 <        for (i = hp->ns; i--; )
699 <                for (j = hp->ns; j--; )
700 <                        if ((ap = ambsample(hp, i, j)) != NULL) {
701 <                                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 */
695 >        if (hp == NULL)                 /* sampling falure? */
696 >                return(0);
697 >
698 >        if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
699 >                        (hp->sampOK < 0) | (hp->ns < 4)) {
700 >                free(hp);               /* Hessian not requested/possible */
701 >                return(-1);             /* value-only return value */
702          }
703 <        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 */
703 >        if ((d = bright(rcol)) > FTINY) {       /* normalize Y values */
704                  d = 0.99*(hp->ns*hp->ns)/d;
705                  K = 0.01;
706          } else {                        /* or fall back on geometric Hessian */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines