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.46 by greg, Fri May 2 21:58:50 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  
28                                /* vertex direction bit positions */
29 #define VDB_xy  0
30 #define VDB_y   01
31 #define VDB_x   02
32 #define VDB_Xy  03
33 #define VDB_xY  04
34 #define VDB_X   05
35 #define VDB_Y   06
36 #define VDB_XY  07
37                                /* get opposite vertex direction bit */
38 #define VDB_OPP(f)      (~(f) & 07)
39                                /* adjacent triangle vertex flags */
40 static const int  adjacent_trifl[8] = {
41                        0,                      /* forbidden diagonal */
42                        1<<VDB_x|1<<VDB_y|1<<VDB_Xy,
43                        1<<VDB_y|1<<VDB_x|1<<VDB_xY,
44                        1<<VDB_y|1<<VDB_Xy|1<<VDB_X,
45                        1<<VDB_x|1<<VDB_xY|1<<VDB_Y,
46                        1<<VDB_Xy|1<<VDB_X|1<<VDB_Y,
47                        1<<VDB_xY|1<<VDB_Y|1<<VDB_X,
48                        0,                      /* forbidden diagonal */
49                };
50
31   typedef struct {
32          COLOR   v;              /* hemisphere sample value */
33 +        float   d;              /* reciprocal distance (1/rt) */
34          FVECT   p;              /* intersection point */
35   } AMBSAMP;              /* sample value */
36  
37   typedef struct {
38          RAY     *rp;            /* originating ray sample */
58        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  
47 < #define ambndx(h,i,j)   ((i)*(h)->ns + (j))
48 < #define ambsam(h,i,j)   (h)->sa[ambndx(h,i,j)]
47 > #define AI(h,i,j)       ((i)*(h)->ns + (j))
48 > #define ambsam(h,i,j)   (h)->sa[AI(h,i,j)]
49  
50   typedef struct {
51          FVECT   r_i, r_i1, e_i, rcp, rI2_eJ2;
52          double  I1, I2;
70        int     valid;
53   } FFTRI;                /* vectors and coefficients for Hessian calculation */
54  
55  
74 /* Get index for adjacent vertex */
56   static int
57 < adjacent_verti(AMBHEMI *hp, int i, int j, int dbit)
57 > ambcollision(                           /* proposed direciton collides? */
58 >        AMBHEMI *hp,
59 >        int     i,
60 >        int     j,
61 >        FVECT   dv
62 > )
63   {
64 <        int     i0 = i*hp->ns + j;
65 <
66 <        switch (dbit) {
67 <        case VDB_y:     return(i0 - hp->ns);
68 <        case VDB_x:     return(i0 - 1);
69 <        case VDB_Xy:    return(i0 - hp->ns + 1);
70 <        case VDB_xY:    return(i0 + hp->ns - 1);
71 <        case VDB_X:     return(i0 + 1);
72 <        case VDB_Y:     return(i0 + hp->ns);
73 <                                /* the following should never occur */
74 <        case VDB_xy:    return(i0 - hp->ns - 1);
75 <        case VDB_XY:    return(i0 + hp->ns + 1);
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(-1);
89 >        return(0);                      /* nothing to worry about */
90   }
91  
92  
95 /* Get vertex direction bit for the opposite edge to complete triangle */
93   static int
94 < vdb_edge(int db1, int db2)
95 < {
96 <        switch (db1) {
97 <        case VDB_x:     return(db2==VDB_y ? VDB_Xy : VDB_Y);
98 <        case VDB_y:     return(db2==VDB_x ? VDB_xY : VDB_X);
102 <        case VDB_X:     return(db2==VDB_Xy ? VDB_y : VDB_xY);
103 <        case VDB_Y:     return(db2==VDB_xY ? VDB_x : VDB_Xy);
104 <        case VDB_xY:    return(db2==VDB_x ? VDB_y : VDB_X);
105 <        case VDB_Xy:    return(db2==VDB_y ? VDB_x : VDB_Y);
106 <        }
107 <        error(INTERNAL, "forbidden diagonal in vdb_edge()");
108 <        return(-1);
109 < }
110 <
111 <
112 < static AMBHEMI *
113 < inithemi(                       /* initialize sampling hemisphere */
114 <        COLOR   ac,
115 <        RAY     *r,
116 <        double  wt
94 > ambsample(                              /* initial ambient division sample */
95 >        AMBHEMI *hp,
96 >        int     i,
97 >        int     j,
98 >        int     n
99   )
100   {
101 <        AMBHEMI *hp;
102 <        double  d;
121 <        int     n, i;
122 <                                        /* set number of divisions */
123 <        if (ambacc <= FTINY &&
124 <                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
125 <                wt = d;                 /* avoid ray termination */
126 <        n = sqrt(ambdiv * wt) + 0.5;
127 <        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
128 <        if (n < i)
129 <                n = i;
130 <                                        /* allocate sampling array */
131 <        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
132 <        if (hp == NULL)
133 <                return(NULL);
134 <        hp->rp = r;
135 <        hp->ns = n;
136 <                                        /* assign coefficient */
137 <        copycolor(hp->acoef, ac);
138 <        d = 1.0/(n*n);
139 <        scalecolor(hp->acoef, d);
140 <                                        /* make tangent plane axes */
141 <        hp->uy[0] = 0.5 - frandom();
142 <        hp->uy[1] = 0.5 - frandom();
143 <        hp->uy[2] = 0.5 - frandom();
144 <        for (i = 3; i--; )
145 <                if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
146 <                        break;
147 <        if (i < 0)
148 <                error(CONSISTENCY, "bad ray direction in inithemi");
149 <        hp->uy[i] = 1.0;
150 <        VCROSS(hp->ux, hp->uy, r->ron);
151 <        normalize(hp->ux);
152 <        VCROSS(hp->uy, r->ron, hp->ux);
153 <                                        /* we're ready to sample */
154 <        return(hp);
155 < }
156 <
157 <
158 < /* Sample ambient division and apply weighting coefficient */
159 < static int
160 < getambsamp(RAY *arp, AMBHEMI *hp, int i, int j, int n)
161 < {
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 */
180 <                if ((spt[0] < 0.1) | (spt[0] >= 0.9))
181 <                        spt[0] = 0.1 + 0.8*frandom();
182 <                if ((spt[1] < 0.1) | (spt[1] >= 0.9))
183 <                        spt[1] = 0.1 + 0.8*frandom();
184 <        }
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);
129 <        dimlist[ndims++] = ambndx(hp,i,j) + 90171;
130 <        rayvalue(arp);                  /* evaluate ray */
131 <        ndims--;                        /* apply coefficient */
132 <        multcolor(arp->rcol, arp->rcoef);
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 (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  
200 static AMBSAMP *
201 ambsample(                              /* initial ambient division sample */
202        AMBHEMI *hp,
203        int     i,
204        int     j
205 )
206 {
207        AMBSAMP *ap = &ambsam(hp,i,j);
208        RAY     ar;
209                                        /* generate hemispherical sample */
210        if (!getambsamp(&ar, hp, i, j, 0))
211                goto badsample;
212                                        /* limit vertex distance */
213        if (ar.rt > 10.0*thescene.cusize)
214                ar.rt = 10.0*thescene.cusize;
215        else if (ar.rt <= FTINY)        /* should never happen! */
216                goto badsample;
217        VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
218        copycolor(ap->v, ar.rcol);
219        return(ap);
220 badsample:
221        setcolor(ap->v, 0., 0., 0.);
222        VCOPY(ap->p, hp->rp->rop);
223        return(NULL);
224 }
225
226
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 241 | 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) {                /* from behind */
186 <                        d2 = b - bright(ap[-1].v);
187 <                        d2 *= d2;
188 <                        ep[0] += d2;
189 <                        ep[-1] += d2;
190 <                }
185 >                if (!j) continue;
186 >                                        /* from behind */
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 = normf*(b - bright(ap[-hp->ns-1].v));
194 >                d2 *= d2;
195 >                ep[0] += d2;
196 >                ep[-hp->ns-1] += d2;
197              }
198                                          /* correct for number of neighbors */
199 <        earr[0] *= 2.f;
200 <        earr[hp->ns-1] *= 2.f;
201 <        earr[(hp->ns-1)*hp->ns] *= 2.f;
202 <        earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 2.f;
199 >        earr[0] *= 8./3.;
200 >        earr[hp->ns-1] *= 8./3.;
201 >        earr[(hp->ns-1)*hp->ns] *= 8./3.;
202 >        earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.;
203          for (i = 1; i < hp->ns-1; i++) {
204 <                earr[i*hp->ns] *= 4./3.;
205 <                earr[i*hp->ns + hp->ns-1] *= 4./3.;
204 >                earr[i*hp->ns] *= 8./5.;
205 >                earr[i*hp->ns + hp->ns-1] *= 8./5.;
206          }
207          for (j = 1; j < hp->ns-1; j++) {
208 <                earr[j] *= 4./3.;
209 <                earr[(hp->ns-1)*hp->ns + j] *= 4./3.;
208 >                earr[j] *= 8./5.;
209 >                earr[(hp->ns-1)*hp->ns + j] *= 8./5.;
210          }
211          return(earr);
212   }
# Line 272 | 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  e2sum = 0;
220 >        double  e2rem = 0;
221          AMBSAMP *ap;
280        RAY     ar;
281        COLOR   asum;
222          float   *ep;
223 <        int     i, j, n;
223 >        int     i, j, n, nss;
224  
225          if (earr == NULL)               /* just skip calc. if no memory */
226                  return;
227 <                                        /* add up estimated variances */
228 <        for (ep = earr + hp->ns*hp->ns; ep-- > earr; )
229 <                e2sum += *ep;
227 >                                        /* accumulate estimated variances */
228 >        for (ep = earr + hp->ns*hp->ns; ep > earr; )
229 >                e2rem += *--ep;
230          ep = earr;                      /* perform super-sampling */
231          for (ap = hp->sa, i = 0; i < hp->ns; i++)
232              for (j = 0; j < hp->ns; j++, ap++) {
233 <                int     nss = *ep/e2sum*cnt + frandom();
234 <                setcolor(asum, 0., 0., 0.);
235 <                for (n = 1; n <= nss; n++) {
236 <                        if (!getambsamp(&ar, hp, i, j, n)) {
237 <                                nss = n-1;
238 <                                break;
299 <                        }
300 <                        addcolor(asum, ar.rcol);
301 <                }
302 <                if (nss) {              /* update returned ambient value */
303 <                        const double    ssf = 1./(nss + 1);
304 <                        for (n = 3; n--; )
305 <                                acol[n] += ssf*colval(asum,n) +
306 <                                                (ssf - 1.)*colval(ap->v,n);
307 <                }
308 <                e2sum -= *ep++;         /* update remainders */
309 <                cnt -= nss;
233 >                if (e2rem <= FTINY)
234 >                        goto done;      /* nothing left to do */
235 >                nss = *ep/e2rem*cnt + frandom();
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 < /* Compute vertex flags, indicating farthest in each direction */
246 < static uby8 *
247 < vertex_flags(AMBHEMI *hp)
245 > static AMBHEMI *
246 > samp_hemi(                              /* sample indirect hemisphere */
247 >        COLOR   rcol,
248 >        RAY     *r,
249 >        double  wt
250 > )
251   {
252 <        uby8    *vflags = (uby8 *)calloc(hp->ns*hp->ns, sizeof(uby8));
253 <        double  *dist2a = (double *)malloc(sizeof(double)*hp->ns);
254 <        uby8    *vf;
255 <        int     i, j;
256 <
257 <        if ((vflags == NULL) | (dist2a == NULL))
258 <                error(SYSTEM, "out of memory in vertex_flags()");
259 <        vf = vflags;            /* compute distances along first row */
260 <        for (j = 0; j < hp->ns; j++) {
261 <                dist2a[j] = dist2(ambsam(hp,0,j).p, hp->rp->rop);
262 <                ++vf;
263 <                if (!j) continue;
264 <                if (dist2a[j] >= dist2a[j-1])
265 <                        vf[0] |= 1<<VDB_x;
266 <                else
267 <                        vf[-1] |= 1<<VDB_X;
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 <                                /* flag subsequent rows */
293 <        for (i = 1; i < hp->ns; i++) {
294 <            double      d2n = dist2(ambsam(hp,i,0).p, hp->rp->rop);
339 <            for (j = 0; j < hp->ns-1; j++) {
340 <                double  d2 = d2n;
341 <                if (d2 >= dist2a[j])    /* row before */
342 <                        vf[0] |= 1<<VDB_y;
343 <                else
344 <                        vf[-hp->ns] |= 1<<VDB_Y;
345 <                dist2a[j] = d2n;
346 <                if (d2 >= dist2a[j+1])  /* diagonal we care about */
347 <                        vf[0] |= 1<<VDB_Xy;
348 <                else
349 <                        vf[1-hp->ns] |= 1<<VDB_xY;
350 <                d2n = dist2(ambsam(hp,i,j+1).p, hp->rp->rop);
351 <                if (d2 >= d2n)          /* column after */
352 <                        vf[0] |= 1<<VDB_X;
353 <                else
354 <                        vf[1] |= 1<<VDB_x;
355 <                ++vf;
356 <            }
357 <            if (d2n >= dist2a[j])       /* final column edge */
358 <                vf[0] |= 1<<VDB_y;
359 <            else
360 <                vf[-hp->ns] |= 1<<VDB_Y;
361 <            dist2a[j] = d2n;
362 <            ++vf;
292 >        if (hp->sampOK < hp->ns*hp->ns) {
293 >                hp->sampOK *= -1;       /* soft failure */
294 >                return(hp);
295          }
296 <        free(dist2a);
297 <        return(vflags);
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, int i, int j, int dbit1, int dbit2, const uby8 *vflags)
307 > back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3)
308   {
309 <        const int       v0 = ambndx(hp,i,j);
310 <        const int       tflags = (1<<dbit1 | 1<<dbit2);
311 <        int             v1, v2;
312 <
313 <        if ((vflags[v0] & tflags) == tflags)    /* is v0 the farthest? */
314 <                return(colval(hp->sa[v0].v,CIEY));
315 <        v1 = adjacent_verti(hp, i, j, dbit1);
316 <        if (vflags[v0] & 1<<dbit2)              /* v1 farthest if v0>v2 */
381 <                return(colval(hp->sa[v1].v,CIEY));
382 <        v2 = adjacent_verti(hp, i, j, dbit2);
383 <        if (vflags[v0] & 1<<dbit1)              /* v2 farthest if v0>v1 */
384 <                return(colval(hp->sa[v2].v,CIEY));
385 <                                                /* else check if v1>v2 */
386 <        if (vflags[v1] & 1<<vdb_edge(dbit1,dbit2))
387 <                return(colval(hp->sa[v1].v,CIEY));
388 <        return(colval(hp->sa[v2].v,CIEY));
309 >        if (hp->sa[n1].d <= hp->sa[n2].d) {
310 >                if (hp->sa[n1].d <= hp->sa[n3].d)
311 >                        return(colval(hp->sa[n1].v,CIEY));
312 >                return(colval(hp->sa[n3].v,CIEY));
313 >        }
314 >        if (hp->sa[n2].d <= hp->sa[n3].d)
315 >                return(colval(hp->sa[n2].v,CIEY));
316 >        return(colval(hp->sa[n3].v,CIEY));
317   }
318  
319  
320   /* Compute vectors and coefficients for Hessian/gradient calcs */
321   static void
322 < comp_fftri(FFTRI *ftp, AMBHEMI *hp, int i, int j, int dbit, const uby8 *vflags)
322 > comp_fftri(FFTRI *ftp, AMBHEMI *hp, const int n0, const int n1)
323   {
324 <        const int       i0 = ambndx(hp,i,j);
325 <        double          rdot_cp, dot_e, dot_er, rdot_r, rdot_r1, J2;
398 <        int             i1, ii;
324 >        double  rdot_cp, dot_e, dot_er, rdot_r, rdot_r1, J2;
325 >        int     ii;
326  
327 <        ftp->valid = 0;                 /* check if we can skip this edge */
328 <        ii = adjacent_trifl[dbit];
329 <        if ((vflags[i0] & ii) == ii)    /* cancels if vertex used as value */
403 <                return;
404 <        i1 = adjacent_verti(hp, i, j, dbit);
405 <        ii = adjacent_trifl[VDB_OPP(dbit)];
406 <        if ((vflags[i1] & ii) == ii)    /* on either end (for both triangles) */
407 <                return;
408 <                                        /* else go ahead with calculation */
409 <        VSUB(ftp->r_i, hp->sa[i0].p, hp->rp->rop);
410 <        VSUB(ftp->r_i1, hp->sa[i1].p, hp->rp->rop);
411 <        VSUB(ftp->e_i, hp->sa[i1].p, hp->sa[i0].p);
327 >        VSUB(ftp->r_i, hp->sa[n0].p, hp->rp->rop);
328 >        VSUB(ftp->r_i1, hp->sa[n1].p, hp->rp->rop);
329 >        VSUB(ftp->e_i, hp->sa[n1].p, hp->sa[n0].p);
330          VCROSS(ftp->rcp, ftp->r_i, ftp->r_i1);
331          rdot_cp = 1.0/DOT(ftp->rcp,ftp->rcp);
332          dot_e = DOT(ftp->e_i,ftp->e_i);
# Line 422 | Line 340 | comp_fftri(FFTRI *ftp, AMBHEMI *hp, int i, int j, int
340          J2 =  ( 0.5*(rdot_r - rdot_r1) - dot_er*ftp->I2 ) / dot_e;
341          for (ii = 3; ii--; )
342                  ftp->rI2_eJ2[ii] = ftp->I2*ftp->r_i[ii] + J2*ftp->e_i[ii];
425        ftp->valid++;
343   }
344  
345  
# Line 448 | Line 365 | comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
365          double  d1, d2, d3, d4;
366          double  I3, J3, K3;
367          int     i, j;
451
452        if (!ftp->valid) {              /* preemptive test */
453                memset(hess, 0, sizeof(FVECT)*3);
454                return;
455        }
368                                          /* compute intermediate coefficients */
369          d1 = 1.0/DOT(ftp->r_i,ftp->r_i);
370          d2 = 1.0/DOT(ftp->r_i1,ftp->r_i1);
# Line 516 | Line 428 | comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm)
428          double  f1;
429          int     i;
430  
519        if (!ftp->valid) {              /* preemptive test */
520                memset(grad, 0, sizeof(FVECT));
521                return;
522        }
431          f1 = 2.0*DOT(nrm, ftp->rcp);
432          VCROSS(ncp, nrm, ftp->e_i);
433          for (i = 3; i--; )
# Line 549 | Line 457 | add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, F
457  
458  
459   /* Compute anisotropic radii and eigenvector directions */
460 < static int
460 > static void
461   eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3])
462   {
463          double  hess2[2][2];
# Line 571 | Line 479 | eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3
479          if (i == 1)                     /* double-root (circle) */
480                  evalue[1] = evalue[0];
481          if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) |
482 <                        ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) )
483 <                error(INTERNAL, "bad eigenvalue calculation");
484 <
482 >                        ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) {
483 >                ra[0] = ra[1] = maxarad;
484 >                return;
485 >        }
486          if (evalue[0] > evalue[1]) {
487                  ra[0] = sqrt(sqrt(4.0/evalue[0]));
488                  ra[1] = sqrt(sqrt(4.0/evalue[1]));
# Line 608 | Line 517 | ambHessian(                            /* anisotropic radii & pos. gradient */
517          static char     memerrmsg[] = "out of memory in ambHessian()";
518          FVECT           (*hessrow)[3] = NULL;
519          FVECT           *gradrow = NULL;
611        uby8            *vflags;
520          FVECT           hessian[3];
521          FVECT           gradient;
522          FFTRI           fftr;
# Line 630 | Line 538 | ambHessian(                            /* anisotropic radii & pos. gradient */
538                          error(SYSTEM, memerrmsg);
539                  memset(gradient, 0, sizeof(gradient));
540          }
633                                        /* get vertex position flags */
634        vflags = vertex_flags(hp);
541                                          /* compute first row of edges */
542          for (j = 0; j < hp->ns-1; j++) {
543 <                comp_fftri(&fftr, hp, 0, j, VDB_X, vflags);
543 >                comp_fftri(&fftr, hp, AI(hp,0,j), AI(hp,0,j+1));
544                  if (hessrow != NULL)
545                          comp_hessian(hessrow[j], &fftr, hp->rp->ron);
546                  if (gradrow != NULL)
# Line 644 | Line 550 | ambHessian(                            /* anisotropic radii & pos. gradient */
550          for (i = 0; i < hp->ns-1; i++) {
551              FVECT       hesscol[3];     /* compute first vertical edge */
552              FVECT       gradcol;
553 <            comp_fftri(&fftr, hp, i, 0, VDB_Y, vflags);
553 >            comp_fftri(&fftr, hp, AI(hp,i,0), AI(hp,i+1,0));
554              if (hessrow != NULL)
555                  comp_hessian(hesscol, &fftr, hp->rp->ron);
556              if (gradrow != NULL)
# Line 653 | Line 559 | ambHessian(                            /* anisotropic radii & pos. gradient */
559                  FVECT   hessdia[3];     /* compute triangle contributions */
560                  FVECT   graddia;
561                  double  backg;
562 <                backg = back_ambval(hp, i, j, VDB_X, VDB_Y, vflags);
562 >                backg = back_ambval(hp, AI(hp,i,j),
563 >                                        AI(hp,i,j+1), AI(hp,i+1,j));
564                                          /* diagonal (inner) edge */
565 <                comp_fftri(&fftr, hp, i, j+1, VDB_xY, vflags);
565 >                comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j));
566                  if (hessrow != NULL) {
567                      comp_hessian(hessdia, &fftr, hp->rp->ron);
568                      rev_hessian(hesscol);
# Line 667 | Line 574 | ambHessian(                            /* anisotropic radii & pos. gradient */
574                      add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
575                  }
576                                          /* initialize edge in next row */
577 <                comp_fftri(&fftr, hp, i+1, j+1, VDB_x, vflags);
577 >                comp_fftri(&fftr, hp, AI(hp,i+1,j+1), AI(hp,i+1,j));
578                  if (hessrow != NULL)
579                      comp_hessian(hessrow[j], &fftr, hp->rp->ron);
580                  if (gradrow != NULL)
581                      comp_gradient(gradrow[j], &fftr, hp->rp->ron);
582                                          /* new column edge & paired triangle */
583 <                backg = back_ambval(hp, i+1, j+1, VDB_x, VDB_y, vflags);
584 <                comp_fftri(&fftr, hp, i, j+1, VDB_Y, vflags);
583 >                backg = back_ambval(hp, AI(hp,i+1,j+1),
584 >                                        AI(hp,i+1,j), AI(hp,i,j+1));
585 >                comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j+1));
586                  if (hessrow != NULL) {
587                      comp_hessian(hesscol, &fftr, hp->rp->ron);
588                      rev_hessian(hessdia);
# Line 694 | Line 602 | ambHessian(                            /* anisotropic radii & pos. gradient */
602                                          /* release row buffers */
603          if (hessrow != NULL) free(hessrow);
604          if (gradrow != NULL) free(gradrow);
697        free(vflags);
605          
606          if (ra != NULL)                 /* extract eigenvectors & radii */
607                  eigenvectors(uv, ra, hessian);
# Line 730 | Line 637 | ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
637   }
638  
639  
640 + /* Compute potential light leak direction flags for cache value */
641 + 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;
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 < 8)
656 +                return(0);
657 +                                        /* check distances overhead */
658 +        for (i = hp->ns*3/4; i-- > hp->ns>>2; )
659 +            for (j = hp->ns*3/4; j-- > hp->ns>>2; )
660 +                avg_d += ambsam(hp,i,j).d;
661 +        avg_d *= 4.0/(hp->ns*hp->ns);
662 +        if (avg_d*r0 >= 1.0)            /* ceiling too low for corral? */
663 +                return(0);
664 +        if (avg_d >= max_d)             /* insurance */
665 +                return(0);
666 +                                        /* else circle around perimeter */
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);
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]);
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-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 +
700 +
701   int
702   doambient(                              /* compute ambient component */
703          COLOR   rcol,                   /* input/output color */
# Line 738 | Line 706 | doambient(                             /* compute ambient component */
706          FVECT   uv[2],                  /* returned (optional) */
707          float   ra[2],                  /* returned (optional) */
708          float   pg[2],                  /* returned (optional) */
709 <        float   dg[2]                   /* returned (optional) */
709 >        float   dg[2],                  /* returned (optional) */
710 >        uint32  *crlp                   /* returned (optional) */
711   )
712   {
713 <        AMBHEMI *hp = inithemi(rcol, r, wt);
745 <        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 */
751 <        if (hp == NULL)
752 <                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 758 | Line 724 | doambient(                             /* compute ambient component */
724                  pg[0] = pg[1] = 0.0;
725          if (dg != NULL)
726                  dg[0] = dg[1] = 0.0;
727 <                                        /* sample the hemisphere */
728 <        acol[0] = acol[1] = acol[2] = 0.0;
729 <        cnt = 0;
730 <        for (i = hp->ns; i--; )
731 <                for (j = hp->ns; j--; )
732 <                        if ((ap = ambsample(hp, i, j)) != NULL) {
733 <                                addcolor(acol, ap->v);
734 <                                ++cnt;
735 <                        }
770 <        if (!cnt) {
771 <                setcolor(rcol, 0.0, 0.0, 0.0);
772 <                free(hp);
773 <                return(0);              /* no valid samples */
727 >        if (crlp != NULL)
728 >                *crlp = 0;
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? */
776 <                copycolor(rcol, acol);
777 <                free(hp);
778 <                return(-1);             /* return value w/o Hessian */
779 <        }
780 <        cnt = ambssamp*wt + 0.5;        /* perform super-sampling? */
781 <        if (cnt > 0)
782 <                ambsupersamp(acol, hp, cnt);
783 <        copycolor(rcol, acol);          /* final indirect irradiance/PI */
784 <        if ((ra == NULL) & (pg == NULL) & (dg == NULL)) {
785 <                free(hp);
786 <                return(-1);             /* no radius or gradient calc. */
787 <        }
788 <        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 */
741                  K = 1.0;
742                  pg = NULL;
743                  dg = NULL;
744 +                crlp = NULL;
745          }
746          ap = hp->sa;                    /* relative Y channel from here on... */
747          for (i = hp->ns*hp->ns; i--; ap++)
# Line 819 | 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 827 | Line 777 | doambient(                             /* compute ambient component */
777                          if (ra[0] > maxarad)
778                                  ra[0] = maxarad;
779                  }
780 +                                        /* flag encroached directions */
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];
785                          if (d > 1.0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines