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.49 by greg, Wed May 7 01:16:02 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) */
# Line 56 | Line 36 | typedef struct {
36  
37   typedef struct {
38          RAY     *rp;            /* originating ray sample */
59        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;
71        int     valid;
53   } FFTRI;                /* vectors and coefficients for Hessian calculation */
54  
55  
75 /* 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  
96 /* 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);
103 <        case VDB_X:     return(db2==VDB_Xy ? VDB_y : VDB_xY);
104 <        case VDB_Y:     return(db2==VDB_xY ? VDB_x : VDB_Xy);
105 <        case VDB_xY:    return(db2==VDB_x ? VDB_y : VDB_X);
106 <        case VDB_Xy:    return(db2==VDB_y ? VDB_x : VDB_Y);
107 <        }
108 <        error(INTERNAL, "forbidden diagonal in vdb_edge()");
109 <        return(-1);
110 < }
111 <
112 <
113 < static AMBHEMI *
114 < inithemi(                       /* initialize sampling hemisphere */
115 <        COLOR   ac,
116 <        RAY     *r,
117 <        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;
122 <        int     n, i;
123 <                                        /* set number of divisions */
124 <        if (ambacc <= FTINY &&
125 <                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
126 <                wt = d;                 /* avoid ray termination */
127 <        n = sqrt(ambdiv * wt) + 0.5;
128 <        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
129 <        if (n < i)
130 <                n = i;
131 <                                        /* allocate sampling array */
132 <        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
133 <        if (hp == NULL)
134 <                return(NULL);
135 <        hp->rp = r;
136 <        hp->ns = n;
137 <                                        /* assign coefficient */
138 <        copycolor(hp->acoef, ac);
139 <        d = 1.0/(n*n);
140 <        scalecolor(hp->acoef, d);
141 <                                        /* make tangent plane axes */
142 <        hp->uy[0] = 0.5 - frandom();
143 <        hp->uy[1] = 0.5 - frandom();
144 <        hp->uy[2] = 0.5 - frandom();
145 <        for (i = 3; i--; )
146 <                if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
147 <                        break;
148 <        if (i < 0)
149 <                error(CONSISTENCY, "bad ray direction in inithemi");
150 <        hp->uy[i] = 1.0;
151 <        VCROSS(hp->ux, hp->uy, r->ron);
152 <        normalize(hp->ux);
153 <        VCROSS(hp->uy, r->ron, hp->ux);
154 <                                        /* we're ready to sample */
155 <        return(hp);
156 < }
157 <
158 <
159 < /* Sample ambient division and apply weighting coefficient */
160 < static int
161 < getambsamp(RAY *arp, AMBHEMI *hp, int i, int j, int n)
162 < {
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 */
181 <                if ((spt[0] < 0.1) | (spt[0] >= 0.9))
182 <                        spt[0] = 0.1 + 0.8*frandom();
183 <                if ((spt[1] < 0.1) | (spt[1] >= 0.9))
184 <                        spt[1] = 0.1 + 0.8*frandom();
185 <        }
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  
201 static AMBSAMP *
202 ambsample(                              /* initial ambient division sample */
203        AMBHEMI *hp,
204        int     i,
205        int     j
206 )
207 {
208        AMBSAMP *ap = &ambsam(hp,i,j);
209        RAY     ar;
210                                        /* generate hemispherical sample */
211        if (!getambsamp(&ar, hp, i, j, 0) || ar.rt <= FTINY) {
212                memset(ap, 0, sizeof(AMBSAMP));
213                return(NULL);
214        }
215        ap->d = 1.0/ar.rt;              /* limit vertex distance */
216        if (ar.rt > 10.0*thescene.cusize)
217                ar.rt = 10.0*thescene.cusize;
218        VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
219        copycolor(ap->v, ar.rcol);
220        return(ap);
221 }
222
223
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 238 | 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 269 | 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;
277        RAY     ar;
278        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;
296 <                        }
297 <                        addcolor(asum, ar.rcol);
298 <                }
299 <                if (nss) {              /* update returned ambient value */
300 <                        const double    ssf = 1./(nss + 1);
301 <                        for (n = 3; n--; )
302 <                                acol[n] += ssf*colval(asum,n) +
303 <                                                (ssf - 1.)*colval(ap->v,n);
304 <                }
305 <                e2sum -= *ep++;         /* update remainders */
306 <                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 <        uby8    *vf;
254 <        AMBSAMP *ap;
255 <        int     i, j;
256 <
257 <        if (vflags == NULL)
258 <                error(SYSTEM, "out of memory in vertex_flags()");
259 <        vf = vflags;
260 <        ap = hp->sa;            /* compute farthest along first row */
261 <        for (j = 0; j < hp->ns-1; j++, vf++, ap++)
262 <                if (ap[0].d <= ap[1].d)
263 <                        vf[0] |= 1<<VDB_X;
264 <                else
265 <                        vf[1] |= 1<<VDB_x;
266 <        ++vf; ++ap;
267 <                                /* flag subsequent rows */
268 <        for (i = 1; i < hp->ns; i++) {
269 <            for (j = 0; j < hp->ns-1; j++, vf++, ap++) {
270 <                if (ap[0].d <= ap[-hp->ns].d)   /* row before */
271 <                        vf[0] |= 1<<VDB_y;
272 <                else
273 <                        vf[-hp->ns] |= 1<<VDB_Y;
274 <                if (ap[0].d <= ap[1-hp->ns].d)  /* diagonal we care about */
275 <                        vf[0] |= 1<<VDB_Xy;
276 <                else
277 <                        vf[1-hp->ns] |= 1<<VDB_xY;
278 <                if (ap[0].d <= ap[1].d)         /* column after */
279 <                        vf[0] |= 1<<VDB_X;
280 <                else
281 <                        vf[1] |= 1<<VDB_x;
282 <            }
283 <            if (ap[0].d <= ap[-hp->ns].d)       /* final column edge */
284 <                vf[0] |= 1<<VDB_y;
285 <            else
286 <                vf[-hp->ns] |= 1<<VDB_Y;
287 <            ++vf; ++ap;
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 <        return(vflags);
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, 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 */
369 <                return(colval(hp->sa[v1].v,CIEY));
370 <        v2 = adjacent_verti(hp, i, j, dbit2);
371 <        if (vflags[v0] & 1<<dbit1)              /* v2 farthest if v0>v1 */
372 <                return(colval(hp->sa[v2].v,CIEY));
373 <                                                /* else check if v1>v2 */
374 <        if (vflags[v1] & 1<<vdb_edge(dbit1,dbit2))
375 <                return(colval(hp->sa[v1].v,CIEY));
376 <        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;
386 <        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 */
391 <                return;
392 <        i1 = adjacent_verti(hp, i, j, dbit);
393 <        ii = adjacent_trifl[VDB_OPP(dbit)];
394 <        if ((vflags[i1] & ii) == ii)    /* on either end (for both triangles) */
395 <                return;
396 <                                        /* else go ahead with calculation */
397 <        VSUB(ftp->r_i, hp->sa[i0].p, hp->rp->rop);
398 <        VSUB(ftp->r_i1, hp->sa[i1].p, hp->rp->rop);
399 <        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 410 | 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];
413        ftp->valid++;
343   }
344  
345  
# Line 436 | 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;
439
440        if (!ftp->valid) {              /* preemptive test */
441                memset(hess, 0, sizeof(FVECT)*3);
442                return;
443        }
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 504 | Line 428 | comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm)
428          double  f1;
429          int     i;
430  
507        if (!ftp->valid) {              /* preemptive test */
508                memset(grad, 0, sizeof(FVECT));
509                return;
510        }
431          f1 = 2.0*DOT(nrm, ftp->rcp);
432          VCROSS(ncp, nrm, ftp->e_i);
433          for (i = 3; i--; )
# Line 537 | 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 559 | 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 596 | 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;
599        uby8            *vflags;
520          FVECT           hessian[3];
521          FVECT           gradient;
522          FFTRI           fftr;
# Line 618 | Line 538 | ambHessian(                            /* anisotropic radii & pos. gradient */
538                          error(SYSTEM, memerrmsg);
539                  memset(gradient, 0, sizeof(gradient));
540          }
621                                        /* get vertex position flags */
622        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 632 | 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 641 | 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 655 | 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 682 | Line 602 | ambHessian(                            /* anisotropic radii & pos. gradient */
602                                          /* release row buffers */
603          if (hessrow != NULL) free(hessrow);
604          if (gradrow != NULL) free(gradrow);
685        free(vflags);
605          
606          if (ra != NULL)                 /* extract eigenvectors & radii */
607                  eigenvectors(uv, ra, hessian);
# Line 722 | Line 641 | ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
641   static uint32
642   ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
643   {
644 <        uint32  flgs = 0;
645 <        int     i, j;
646 <                                        /* circle around perimeter */
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 <                FVECT   vec;
671 <                double  u, v;
733 <                double  ang;
734 <                int     abp;
735 <                if (ap->d <= FTINY)
736 <                        continue;
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 <                ang += 2.0*PI*(ang < 0);
679 <                ang *= 16./PI;
745 <                if ((ang < .5) | (ang >= 31.5))
746 <                        flgs |= 0x80000001;
747 <                else
748 <                        flgs |= 3L<<(int)(ang-.5);
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 763 | Line 710 | doambient(                             /* compute ambient component */
710          uint32  *crlp                   /* returned (optional) */
711   )
712   {
713 <        AMBHEMI *hp = inithemi(rcol, r, wt);
767 <        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 */
773 <        if (hp == NULL)
774 <                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 782 | 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);
792 <                                ++cnt;
793 <                        }
794 <        if (!cnt) {
795 <                setcolor(rcol, 0.0, 0.0, 0.0);
796 <                free(hp);
797 <                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? */
800 <                copycolor(rcol, acol);
801 <                free(hp);
802 <                return(-1);             /* return value w/o Hessian */
803 <        }
804 <        cnt = ambssamp*wt + 0.5;        /* perform super-sampling? */
805 <        if (cnt > 0)
806 <                ambsupersamp(acol, hp, cnt);
807 <        copycolor(rcol, acol);          /* final indirect irradiance/PI */
808 <        if ((ra == NULL) & (pg == NULL) & (dg == NULL)) {
809 <                free(hp);
810 <                return(-1);             /* no radius or gradient calc. */
811 <        }
812 <        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 843 | 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 851 | Line 777 | doambient(                             /* compute ambient component */
777                          if (ra[0] > maxarad)
778                                  ra[0] = maxarad;
779                  }
780 <                if (crlp != NULL)       /* flag encroached directions */
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];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines