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.37 by greg, Sat Apr 26 05:09:54 2014 UTC vs.
Revision 2.92 by greg, Fri Apr 5 01:10:26 2024 UTC

# Line 8 | Line 8 | static const char      RCSid[] = "$Id$";
8   *      for Irradiance Caching" by Schwarzhaupt, Wann Jensen, & Jarosz
9   *      from ACM SIGGRAPH Asia 2012 conference proceedings.
10   *
11 + *  Added book-keeping optimization to avoid calculations that would
12 + *      cancel due to traversal both directions on edges that are adjacent
13 + *      to same-valued triangles.  This cuts about half of Hessian math.
14 + *
15   *  Declarations of external symbols in ambient.h
16   */
17  
# Line 17 | Line 21 | static const char      RCSid[] = "$Id$";
21   #include  "ambient.h"
22   #include  "random.h"
23  
24 < #ifdef NEWAMB
24 > #ifndef MINADIV
25 > #define MINADIV         7       /* minimum # divisions in each dimension */
26 > #endif
27  
28 < extern void             SDsquare2disk(double ds[2], double seedx, double seedy);
28 > typedef struct {
29 >        FVECT   p;              /* intersection point */
30 >        float   d;              /* reciprocal distance */
31 >        SCOLOR  v;              /* hemisphere sample value */
32 > } AMBSAMP;              /* sample value */
33  
34   typedef struct {
35          RAY     *rp;            /* originating ray sample */
26        FVECT   ux, uy;         /* tangent axis unit vectors */
36          int     ns;             /* number of samples per axis */
37 <        COLOR   acoef;          /* division contribution coefficient */
38 <        struct s_ambsamp {
39 <                COLOR   v;              /* hemisphere sample value */
40 <                FVECT   p;              /* intersection point */
41 <        } sa[1];                /* sample array (extends struct) */
37 >        int     sampOK;         /* acquired full sample set? */
38 >        int     atyp;           /* RAMBIENT or TAMBIENT */
39 >        SCOLOR  acoef;          /* division contribution coefficient */
40 >        SCOLOR  acol;           /* accumulated color */
41 >        FVECT   onrm;           /* oriented unperturbed surface normal */
42 >        FVECT   ux, uy;         /* tangent axis unit vectors */
43 >        AMBSAMP sa[1];          /* sample array (extends struct) */
44   }  AMBHEMI;             /* ambient sample hemisphere */
45  
46 < #define ambsamp(h,i,j)  (h)->sa[(i)*(h)->ns + (j)]
46 > #define AI(h,i,j)       ((i)*(h)->ns + (j))
47 > #define ambsam(h,i,j)   (h)->sa[AI(h,i,j)]
48  
49   typedef struct {
50          FVECT   r_i, r_i1, e_i, rcp, rI2_eJ2;
# Line 40 | Line 52 | typedef struct {
52   } FFTRI;                /* vectors and coefficients for Hessian calculation */
53  
54  
55 + static int
56 + ambcollision(                           /* proposed direciton collides? */
57 +        AMBHEMI *hp,
58 +        int     i,
59 +        int     j,
60 +        FVECT   dv
61 + )
62 + {
63 +        double  cos_thresh;
64 +        int     ii, jj;
65 +                                        /* min. spacing = 1/4th division */
66 +        cos_thresh = (PI/4.)/(double)hp->ns;
67 +        cos_thresh = 1. - .5*cos_thresh*cos_thresh;
68 +                                        /* check existing neighbors */
69 +        for (ii = i-1; ii <= i+1; ii++) {
70 +                if (ii < 0) continue;
71 +                if (ii >= hp->ns) break;
72 +                for (jj = j-1; jj <= j+1; jj++) {
73 +                        AMBSAMP *ap;
74 +                        FVECT   avec;
75 +                        double  dprod;
76 +                        if (jj < 0) continue;
77 +                        if (jj >= hp->ns) break;
78 +                        if ((ii==i) & (jj==j)) continue;
79 +                        ap = &ambsam(hp,ii,jj);
80 +                        if (ap->d <= .5/FHUGE)
81 +                                continue;       /* no one home */
82 +                        VSUB(avec, ap->p, hp->rp->rop);
83 +                        dprod = DOT(avec, dv);
84 +                        if (dprod >= cos_thresh*VLEN(avec))
85 +                                return(1);      /* collision */
86 +                }
87 +        }
88 +        return(0);                      /* nothing to worry about */
89 + }
90 +
91 +
92 + static int
93 + ambsample(                              /* initial ambient division sample */
94 +        AMBHEMI *hp,
95 +        int     i,
96 +        int     j,
97 +        int     n
98 + )
99 + {
100 +        AMBSAMP *ap = &ambsam(hp,i,j);
101 +        RAY     ar;
102 +        int     hlist[3], ii;
103 +        RREAL   spt[2];
104 +        double  zd;
105 +                                        /* generate hemispherical sample */
106 +                                        /* ambient coefficient for weight */
107 +        if (ambacc > FTINY)
108 +                setscolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
109 +        else
110 +                copyscolor(ar.rcoef, hp->acoef);
111 +        if (rayorigin(&ar, hp->atyp, hp->rp, ar.rcoef) < 0)
112 +                return(0);
113 +        if (ambacc > FTINY) {
114 +                smultscolor(ar.rcoef, hp->acoef);
115 +                scalescolor(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 + resample:
122 +        square2disk(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 +                ar.rdir[ii] =   spt[0]*hp->ux[ii] +
126 +                                spt[1]*hp->uy[ii] +
127 +                                zd*hp->onrm[ii];
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 +        zd = raydistance(&ar);
138 +        if (zd <= FTINY)
139 +                return(0);              /* should never happen */
140 +        smultscolor(ar.rcol, ar.rcoef); /* apply coefficient */
141 +        if (zd*ap->d < 1.0)             /* new/closer distance? */
142 +                ap->d = 1.0/zd;
143 +        if (!n) {                       /* record first vertex & value */
144 +                if (zd > 10.0*thescene.cusize + 1000.)
145 +                        zd = 10.0*thescene.cusize + 1000.;
146 +                VSUM(ap->p, ar.rorg, ar.rdir, zd);
147 +                copyscolor(ap->v, ar.rcol);
148 +        } else {                        /* else update recorded value */
149 +                sopscolor(hp->acol, -=, ap->v);
150 +                zd = 1.0/(double)(n+1);
151 +                scalescolor(ar.rcol, zd);
152 +                zd *= (double)n;
153 +                scalescolor(ap->v, zd);
154 +                saddscolor(ap->v, ar.rcol);
155 +        }
156 +        saddscolor(hp->acol, ap->v);    /* add to our sum */
157 +        return(1);
158 + }
159 +
160 +
161 + /* Estimate variance based on ambient division differences */
162 + static float *
163 + getambdiffs(AMBHEMI *hp)
164 + {
165 +        const double    normf = 1./bright(hp->acoef);
166 +        float   *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float));
167 +        float   *ep;
168 +        AMBSAMP *ap;
169 +        double  b, b1, d2;
170 +        int     i, j;
171 +
172 +        if (earr == NULL)               /* out of memory? */
173 +                return(NULL);
174 +                                        /* sum squared neighbor diffs */
175 +        for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++)
176 +            for (j = 0; j < hp->ns; j++, ap++, ep++) {
177 +                b = pbright(ap[0].v);
178 +                if (i) {                /* from above */
179 +                        b1 = pbright(ap[-hp->ns].v);
180 +                        d2 = b - b1;
181 +                        d2 *= d2*normf/(b + b1 + FTINY);
182 +                        ep[0] += d2;
183 +                        ep[-hp->ns] += d2;
184 +                }
185 +                if (!j) continue;
186 +                                        /* from behind */
187 +                b1 = pbright(ap[-1].v);
188 +                d2 = b - b1;
189 +                d2 *= d2*normf/(b + b1 + FTINY);
190 +                ep[0] += d2;
191 +                ep[-1] += d2;
192 +                if (!i) continue;
193 +                                        /* diagonal */
194 +                b1 = pbright(ap[-hp->ns-1].v);
195 +                d2 = b - b1;
196 +                d2 *= d2*normf/(b + b1 + FTINY);
197 +                ep[0] += d2;
198 +                ep[-hp->ns-1] += d2;
199 +            }
200 +                                        /* correct for number of neighbors */
201 +        earr[0] *= 8./3.;
202 +        earr[hp->ns-1] *= 8./3.;
203 +        earr[(hp->ns-1)*hp->ns] *= 8./3.;
204 +        earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.;
205 +        for (i = 1; i < hp->ns-1; i++) {
206 +                earr[i*hp->ns] *= 8./5.;
207 +                earr[i*hp->ns + hp->ns-1] *= 8./5.;
208 +        }
209 +        for (j = 1; j < hp->ns-1; j++) {
210 +                earr[j] *= 8./5.;
211 +                earr[(hp->ns-1)*hp->ns + j] *= 8./5.;
212 +        }
213 +        return(earr);
214 + }
215 +
216 +
217 + /* Perform super-sampling on hemisphere (introduces bias) */
218 + static void
219 + ambsupersamp(AMBHEMI *hp, int cnt)
220 + {
221 +        float   *earr = getambdiffs(hp);
222 +        double  e2rem = 0;
223 +        float   *ep;
224 +        int     i, j, n, nss;
225 +
226 +        if (earr == NULL)               /* just skip calc. if no memory */
227 +                return;
228 +                                        /* accumulate estimated variances */
229 +        for (ep = earr + hp->ns*hp->ns; ep > earr; )
230 +                e2rem += *--ep;
231 +        ep = earr;                      /* perform super-sampling */
232 +        for (i = 0; i < hp->ns; i++)
233 +            for (j = 0; j < hp->ns; j++) {
234 +                if (e2rem <= FTINY)
235 +                        goto done;      /* nothing left to do */
236 +                nss = *ep/e2rem*cnt + frandom();
237 +                for (n = 1; n <= nss && ambsample(hp,i,j,n); n++)
238 +                        if (!--cnt) goto done;
239 +                e2rem -= *ep++;         /* update remainder */
240 +        }
241 + done:
242 +        free(earr);
243 + }
244 +
245 +
246   static AMBHEMI *
247 < inithemi(                       /* initialize sampling hemisphere */
248 <        COLOR   ac,
247 > samp_hemi(                              /* sample indirect hemisphere */
248 >        SCOLOR  rcol,
249          RAY     *r,
250          double  wt
251   )
252   {
253 +        int     backside = (wt < 0);
254          AMBHEMI *hp;
255          double  d;
256 <        int     n, i;
256 >        int     n, i, j;
257 >                                        /* insignificance check */
258 >        d = sintens(rcol);
259 >        if (d <= FTINY)
260 >                return(NULL);
261                                          /* set number of divisions */
262 +        if (backside) wt = -wt;
263          if (ambacc <= FTINY &&
264 <                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
264 >                        wt > (d *= 0.8*r->rweight/(ambdiv*minweight)))
265                  wt = d;                 /* avoid ray termination */
266          n = sqrt(ambdiv * wt) + 0.5;
267 <        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
268 <        if (n < i)
267 >        i = 1 + (MINADIV-1)*(ambacc > FTINY);
268 >        if (n < i)                      /* use minimum number of samples? */
269                  n = i;
270                                          /* allocate sampling array */
271 <        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) +
63 <                                sizeof(struct s_ambsamp)*(n*n - 1));
271 >        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
272          if (hp == NULL)
273 <                return(NULL);
273 >                error(SYSTEM, "out of memory in samp_hemi");
274 >
275 >        if (backside) {
276 >                hp->atyp = TAMBIENT;
277 >                hp->onrm[0] = -r->ron[0];
278 >                hp->onrm[1] = -r->ron[1];
279 >                hp->onrm[2] = -r->ron[2];
280 >        } else {
281 >                hp->atyp = RAMBIENT;
282 >                VCOPY(hp->onrm, r->ron);
283 >        }
284          hp->rp = r;
285          hp->ns = n;
286 +        scolorblack(hp->acol);
287 +        memset(hp->sa, 0, sizeof(AMBSAMP)*n*n);
288 +        hp->sampOK = 0;
289                                          /* assign coefficient */
290 <        copycolor(hp->acoef, ac);
290 >        copyscolor(hp->acoef, rcol);
291          d = 1.0/(n*n);
292 <        scalecolor(hp->acoef, d);
292 >        scalescolor(hp->acoef, d);
293                                          /* make tangent plane axes */
294 <        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
295 <        for (i = 3; i--; )
296 <                if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
297 <                        break;
298 <        if (i < 0)
299 <                error(CONSISTENCY, "bad ray direction in inithemi");
300 <        hp->uy[i] = 1.0;
301 <        VCROSS(hp->ux, hp->uy, r->ron);
302 <        normalize(hp->ux);
303 <        VCROSS(hp->uy, r->ron, hp->ux);
304 <                                        /* we're ready to sample */
305 <        return(hp);
294 >        if (!getperpendicular(hp->ux, hp->onrm, 1))
295 >                error(CONSISTENCY, "bad ray direction in samp_hemi");
296 >        VCROSS(hp->uy, hp->onrm, hp->ux);
297 >                                        /* sample divisions */
298 >        for (i = hp->ns; i--; )
299 >            for (j = hp->ns; j--; )
300 >                hp->sampOK += ambsample(hp, i, j, 0);
301 >        copyscolor(rcol, hp->acol);
302 >        if (!hp->sampOK) {              /* utter failure? */
303 >                free(hp);
304 >                return(NULL);
305 >        }
306 >        if (hp->sampOK < hp->ns*hp->ns) {
307 >                hp->sampOK *= -1;       /* soft failure */
308 >                return(hp);
309 >        }
310 >        if (hp->sampOK <= MINADIV*MINADIV)
311 >                return(hp);             /* don't bother super-sampling */
312 >        n = ambssamp*wt + 0.5;
313 >        if (n > 8) {                    /* perform super-sampling? */
314 >                ambsupersamp(hp, n);
315 >                copyscolor(rcol, hp->acol);
316 >        }
317 >        return(hp);                     /* all is well */
318   }
319  
320  
321 < static struct s_ambsamp *
322 < ambsample(                              /* sample an ambient direction */
323 <        AMBHEMI *hp,
91 <        int     i,
92 <        int     j
93 < )
321 > /* Return brightness of farthest ambient sample */
322 > static double
323 > back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3)
324   {
325 <        struct s_ambsamp        *ap = &ambsamp(hp,i,j);
326 <        RAY                     ar;
327 <        double                  spt[2], zd;
328 <        int                     ii;
99 <                                        /* ambient coefficient for weight */
100 <        if (ambacc > FTINY)
101 <                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
102 <        else
103 <                copycolor(ar.rcoef, hp->acoef);
104 <        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0)
105 <                goto badsample;
106 <        if (ambacc > FTINY) {
107 <                multcolor(ar.rcoef, hp->acoef);
108 <                scalecolor(ar.rcoef, 1./AVGREFL);
325 >        if (hp->sa[n1].d <= hp->sa[n2].d) {
326 >                if (hp->sa[n1].d <= hp->sa[n3].d)
327 >                        return(hp->sa[n1].v[0]);
328 >                return(hp->sa[n3].v[0]);
329          }
330 <                                        /* generate hemispherical sample */
331 <        SDsquare2disk(spt,      (i+.1+.8*frandom())/hp->ns,
332 <                                (j+.1+.8*frandom())/hp->ns );
113 <        zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
114 <        for (ii = 3; ii--; )
115 <                ar.rdir[ii] =   spt[0]*hp->ux[ii] +
116 <                                spt[1]*hp->uy[ii] +
117 <                                zd*hp->rp->ron[ii];
118 <        checknorm(ar.rdir);
119 <        dimlist[ndims++] = i*hp->ns + j + 90171;
120 <        rayvalue(&ar);                  /* evaluate ray */
121 <        ndims--;
122 <                                        /* limit vertex distance */
123 <        if (ar.rt > 10.0*thescene.cusize)
124 <                ar.rt = 10.0*thescene.cusize;
125 <        else if (ar.rt <= FTINY)        /* should never happen! */
126 <                goto badsample;
127 <        VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
128 <        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
129 <        copycolor(ap->v, ar.rcol);
130 <        return(ap);
131 < badsample:
132 <        setcolor(ap->v, 0., 0., 0.);
133 <        VCOPY(ap->p, hp->rp->rop);
134 <        return(NULL);
330 >        if (hp->sa[n2].d <= hp->sa[n3].d)
331 >                return(hp->sa[n2].v[0]);
332 >        return(hp->sa[n3].v[0]);
333   }
334  
335  
336   /* Compute vectors and coefficients for Hessian/gradient calcs */
337   static void
338 < comp_fftri(FFTRI *ftp, FVECT ap0, FVECT ap1, FVECT rop)
338 > comp_fftri(FFTRI *ftp, AMBHEMI *hp, const int n0, const int n1)
339   {
340          double  rdot_cp, dot_e, dot_er, rdot_r, rdot_r1, J2;
341 <        int     i;
341 >        int     ii;
342  
343 <        VSUB(ftp->r_i, ap0, rop);
344 <        VSUB(ftp->r_i1, ap1, rop);
345 <        VSUB(ftp->e_i, ap1, ap0);
343 >        VSUB(ftp->r_i, hp->sa[n0].p, hp->rp->rop);
344 >        VSUB(ftp->r_i1, hp->sa[n1].p, hp->rp->rop);
345 >        VSUB(ftp->e_i, hp->sa[n1].p, hp->sa[n0].p);
346          VCROSS(ftp->rcp, ftp->r_i, ftp->r_i1);
347          rdot_cp = 1.0/DOT(ftp->rcp,ftp->rcp);
348          dot_e = DOT(ftp->e_i,ftp->e_i);
# Line 156 | Line 354 | comp_fftri(FFTRI *ftp, FVECT ap0, FVECT ap1, FVECT rop
354          ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)*rdot_r1 - dot_er*rdot_r +
355                          dot_e*ftp->I1 )*0.5*rdot_cp;
356          J2 =  ( 0.5*(rdot_r - rdot_r1) - dot_er*ftp->I2 ) / dot_e;
357 <        for (i = 3; i--; )
358 <                ftp->rI2_eJ2[i] = ftp->I2*ftp->r_i[i] + J2*ftp->e_i[i];
357 >        for (ii = 3; ii--; )
358 >                ftp->rI2_eJ2[ii] = ftp->I2*ftp->r_i[ii] + J2*ftp->e_i[ii];
359   }
360  
361  
# Line 206 | Line 404 | comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
404                  hess[i][j] = m1[i][j] + d1*( I3*m2[i][j] + K3*m3[i][j] +
405                                                  2.0*J3*m4[i][j] );
406                  hess[i][j] += d2*(i==j);
407 <                hess[i][j] *= 1.0/PI;
407 >                hess[i][j] *= -1.0/PI;
408              }
409   }
410  
# Line 228 | Line 426 | rev_hessian(FVECT hess[3])
426   /* Add to radiometric Hessian from the given triangle */
427   static void
428   add2hessian(FVECT hess[3], FVECT ehess1[3],
429 <                FVECT ehess2[3], FVECT ehess3[3], COLORV v)
429 >                FVECT ehess2[3], FVECT ehess3[3], double v)
430   {
431          int     i, j;
432  
# Line 249 | Line 447 | comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm)
447          f1 = 2.0*DOT(nrm, ftp->rcp);
448          VCROSS(ncp, nrm, ftp->e_i);
449          for (i = 3; i--; )
450 <                grad[i] = (-0.5/PI)*( ftp->I1*ncp[i] + f1*ftp->rI2_eJ2[i] );
450 >                grad[i] = (0.5/PI)*( ftp->I1*ncp[i] + f1*ftp->rI2_eJ2[i] );
451   }
452  
453  
# Line 265 | Line 463 | rev_gradient(FVECT grad)
463  
464   /* Add to displacement gradient from the given triangle */
465   static void
466 < add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, COLORV v)
466 > add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, double v)
467   {
468          int     i;
469  
# Line 274 | Line 472 | add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, F
472   }
473  
474  
277 /* Return brightness of furthest ambient sample */
278 static COLORV
279 back_ambval(struct s_ambsamp *ap1, struct s_ambsamp *ap2,
280                struct s_ambsamp *ap3, FVECT orig)
281 {
282        COLORV  vback;
283        FVECT   vec;
284        double  d2, d2best;
285
286        VSUB(vec, ap1->p, orig);
287        d2best = DOT(vec,vec);
288        vback = colval(ap1->v,CIEY);
289        VSUB(vec, ap2->p, orig);
290        d2 = DOT(vec,vec);
291        if (d2 > d2best) {
292                d2best = d2;
293                vback = colval(ap2->v,CIEY);
294        }
295        VSUB(vec, ap3->p, orig);
296        d2 = DOT(vec,vec);
297        if (d2 > d2best)
298                return(colval(ap3->v,CIEY));
299        return(vback);
300 }
301
302
475   /* Compute anisotropic radii and eigenvector directions */
476 < static int
476 > static void
477   eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3])
478   {
479          double  hess2[2][2];
# Line 317 | Line 489 | eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3
489          hess2[0][1] = DOT(uv[0], b);
490          hess2[1][0] = DOT(uv[1], a);
491          hess2[1][1] = DOT(uv[1], b);
492 <                                        /* compute eigenvalues */
493 <        if ( quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1],
494 <                        hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]) != 2 ||
495 <                        ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) |
496 <                        ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) )
497 <                error(INTERNAL, "bad eigenvalue calculation");
498 <
492 >                                        /* compute eigenvalue(s) */
493 >        i = quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1],
494 >                        hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]);
495 >        if (i == 1)                     /* double-root (circle) */
496 >                evalue[1] = evalue[0];
497 >        if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) |
498 >                        ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) {
499 >                ra[0] = ra[1] = maxarad;
500 >                return;
501 >        }
502          if (evalue[0] > evalue[1]) {
503                  ra[0] = sqrt(sqrt(4.0/evalue[0]));
504                  ra[1] = sqrt(sqrt(4.0/evalue[1]));
# Line 381 | Line 556 | ambHessian(                            /* anisotropic radii & pos. gradient */
556          }
557                                          /* compute first row of edges */
558          for (j = 0; j < hp->ns-1; j++) {
559 <                comp_fftri(&fftr, ambsamp(hp,0,j).p,
385 <                                ambsamp(hp,0,j+1).p, hp->rp->rop);
559 >                comp_fftri(&fftr, hp, AI(hp,0,j), AI(hp,0,j+1));
560                  if (hessrow != NULL)
561 <                        comp_hessian(hessrow[j], &fftr, hp->rp->ron);
561 >                        comp_hessian(hessrow[j], &fftr, hp->onrm);
562                  if (gradrow != NULL)
563 <                        comp_gradient(gradrow[j], &fftr, hp->rp->ron);
563 >                        comp_gradient(gradrow[j], &fftr, hp->onrm);
564          }
565                                          /* sum each row of triangles */
566          for (i = 0; i < hp->ns-1; i++) {
567              FVECT       hesscol[3];     /* compute first vertical edge */
568              FVECT       gradcol;
569 <            comp_fftri(&fftr, ambsamp(hp,i,0).p,
396 <                        ambsamp(hp,i+1,0).p, hp->rp->rop);
569 >            comp_fftri(&fftr, hp, AI(hp,i,0), AI(hp,i+1,0));
570              if (hessrow != NULL)
571 <                comp_hessian(hesscol, &fftr, hp->rp->ron);
571 >                comp_hessian(hesscol, &fftr, hp->onrm);
572              if (gradrow != NULL)
573 <                comp_gradient(gradcol, &fftr, hp->rp->ron);
573 >                comp_gradient(gradcol, &fftr, hp->onrm);
574              for (j = 0; j < hp->ns-1; j++) {
575                  FVECT   hessdia[3];     /* compute triangle contributions */
576                  FVECT   graddia;
577 <                COLORV  backg;
578 <                backg = back_ambval(&ambsamp(hp,i,j), &ambsamp(hp,i,j+1),
579 <                                        &ambsamp(hp,i+1,j), hp->rp->rop);
577 >                double  backg;
578 >                backg = back_ambval(hp, AI(hp,i,j),
579 >                                        AI(hp,i,j+1), AI(hp,i+1,j));
580                                          /* diagonal (inner) edge */
581 <                comp_fftri(&fftr, ambsamp(hp,i,j+1).p,
409 <                                ambsamp(hp,i+1,j).p, hp->rp->rop);
581 >                comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j));
582                  if (hessrow != NULL) {
583 <                    comp_hessian(hessdia, &fftr, hp->rp->ron);
583 >                    comp_hessian(hessdia, &fftr, hp->onrm);
584                      rev_hessian(hesscol);
585                      add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
586                  }
587 <                if (gradient != NULL) {
588 <                    comp_gradient(graddia, &fftr, hp->rp->ron);
587 >                if (gradrow != NULL) {
588 >                    comp_gradient(graddia, &fftr, hp->onrm);
589                      rev_gradient(gradcol);
590                      add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
591                  }
592                                          /* initialize edge in next row */
593 <                comp_fftri(&fftr, ambsamp(hp,i+1,j+1).p,
422 <                                ambsamp(hp,i+1,j).p, hp->rp->rop);
593 >                comp_fftri(&fftr, hp, AI(hp,i+1,j+1), AI(hp,i+1,j));
594                  if (hessrow != NULL)
595 <                    comp_hessian(hessrow[j], &fftr, hp->rp->ron);
595 >                    comp_hessian(hessrow[j], &fftr, hp->onrm);
596                  if (gradrow != NULL)
597 <                    comp_gradient(gradrow[j], &fftr, hp->rp->ron);
597 >                    comp_gradient(gradrow[j], &fftr, hp->onrm);
598                                          /* new column edge & paired triangle */
599 <                backg = back_ambval(&ambsamp(hp,i,j+1), &ambsamp(hp,i+1,j+1),
600 <                                        &ambsamp(hp,i+1,j), hp->rp->rop);
601 <                comp_fftri(&fftr, ambsamp(hp,i,j+1).p, ambsamp(hp,i+1,j+1).p,
431 <                                hp->rp->rop);
599 >                backg = back_ambval(hp, AI(hp,i+1,j+1),
600 >                                        AI(hp,i+1,j), AI(hp,i,j+1));
601 >                comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j+1));
602                  if (hessrow != NULL) {
603 <                    comp_hessian(hesscol, &fftr, hp->rp->ron);
603 >                    comp_hessian(hesscol, &fftr, hp->onrm);
604                      rev_hessian(hessdia);
605                      add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
606                      if (i < hp->ns-2)
607                          rev_hessian(hessrow[j]);
608                  }
609                  if (gradrow != NULL) {
610 <                    comp_gradient(gradcol, &fftr, hp->rp->ron);
610 >                    comp_gradient(gradcol, &fftr, hp->onrm);
611                      rev_gradient(graddia);
612                      add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
613                      if (i < hp->ns-2)
# Line 462 | Line 632 | ambHessian(                            /* anisotropic radii & pos. gradient */
632   static void
633   ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
634   {
635 <        struct s_ambsamp        *ap;
636 <        double                  dgsum[2];
637 <        int                     n;
638 <        FVECT                   vd;
639 <        double                  gfact;
635 >        AMBSAMP *ap;
636 >        double  dgsum[2];
637 >        int     n;
638 >        FVECT   vd;
639 >        double  gfact;
640  
641          dgsum[0] = dgsum[1] = 0.0;      /* sum values times -tan(theta) */
642          for (ap = hp->sa, n = hp->ns*hp->ns; n--; ap++) {
643                                          /* use vector for azimuth + 90deg */
644                  VSUB(vd, ap->p, hp->rp->rop);
645                                          /* brightness over cosine factor */
646 <                gfact = colval(ap->v,CIEY) / DOT(hp->rp->ron, vd);
647 <                                        /* -sine = -proj_radius/vd_length */
648 <                dgsum[0] += DOT(uv[1], vd) * gfact;
649 <                dgsum[1] -= DOT(uv[0], vd) * gfact;
646 >                gfact = ap->v[0] / DOT(hp->onrm, vd);
647 >                                        /* sine = proj_radius/vd_length */
648 >                dgsum[0] -= DOT(uv[1], vd) * gfact;
649 >                dgsum[1] += DOT(uv[0], vd) * gfact;
650          }
651          dg[0] = dgsum[0] / (hp->ns*hp->ns);
652          dg[1] = dgsum[1] / (hp->ns*hp->ns);
653   }
654  
655  
656 + /* Compute potential light leak direction flags for cache value */
657 + static uint32
658 + ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
659 + {
660 +        const double    max_d = 1.0/(minarad*ambacc + 0.001);
661 +        const double    ang_res = 0.5*PI/hp->ns;
662 +        const double    ang_step = ang_res/((int)(16/PI*ang_res) + 1.01);
663 +        double          avg_d = 0;
664 +        uint32          flgs = 0;
665 +        FVECT           vec;
666 +        double          u, v;
667 +        double          ang, a1;
668 +        int             i, j;
669 +                                        /* don't bother for a few samples */
670 +        if (hp->ns < 8)
671 +                return(0);
672 +                                        /* check distances overhead */
673 +        for (i = hp->ns*3/4; i-- > hp->ns>>2; )
674 +            for (j = hp->ns*3/4; j-- > hp->ns>>2; )
675 +                avg_d += ambsam(hp,i,j).d;
676 +        avg_d *= 4.0/(hp->ns*hp->ns);
677 +        if (avg_d*r0 >= 1.0)            /* ceiling too low for corral? */
678 +                return(0);
679 +        if (avg_d >= max_d)             /* insurance */
680 +                return(0);
681 +                                        /* else circle around perimeter */
682 +        for (i = 0; i < hp->ns; i++)
683 +            for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) {
684 +                AMBSAMP *ap = &ambsam(hp,i,j);
685 +                if ((ap->d <= FTINY) | (ap->d >= max_d))
686 +                        continue;       /* too far or too near */
687 +                VSUB(vec, ap->p, hp->rp->rop);
688 +                u = DOT(vec, uv[0]);
689 +                v = DOT(vec, uv[1]);
690 +                if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= u*u + v*v)
691 +                        continue;       /* occluder outside ellipse */
692 +                ang = atan2a(v, u);     /* else set direction flags */
693 +                for (a1 = ang-ang_res; a1 <= ang+ang_res; a1 += ang_step)
694 +                        flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0)));
695 +            }
696 +        return(flgs);
697 + }
698 +
699 +
700   int
701   doambient(                              /* compute ambient component */
702 <        COLOR   rcol,                   /* input/output color */
702 >        SCOLOR  rcol,                   /* input/output color */
703          RAY     *r,
704 <        double  wt,
704 >        double  wt,                     /* negative for back side */
705          FVECT   uv[2],                  /* returned (optional) */
706          float   ra[2],                  /* returned (optional) */
707          float   pg[2],                  /* returned (optional) */
708 <        float   dg[2]                   /* returned (optional) */
708 >        float   dg[2],                  /* returned (optional) */
709 >        uint32  *crlp                   /* returned (optional) */
710   )
711   {
712 <        AMBHEMI                 *hp = inithemi(rcol, r, wt);
713 <        int                     cnt = 0;
714 <        FVECT                   my_uv[2];
715 <        double                  d, acol[3];
716 <        struct s_ambsamp        *ap;
717 <        int                     i, j;
503 <                                        /* check/initialize */
504 <        if (hp == NULL)
505 <                return(0);
712 >        AMBHEMI *hp = samp_hemi(rcol, r, wt);
713 >        FVECT   my_uv[2];
714 >        double  d, K;
715 >        AMBSAMP *ap;
716 >        int     i;
717 >                                        /* clear return values */
718          if (uv != NULL)
719                  memset(uv, 0, sizeof(FVECT)*2);
720          if (ra != NULL)
# Line 511 | Line 723 | doambient(                             /* compute ambient component */
723                  pg[0] = pg[1] = 0.0;
724          if (dg != NULL)
725                  dg[0] = dg[1] = 0.0;
726 <                                        /* sample the hemisphere */
727 <        acol[0] = acol[1] = acol[2] = 0.0;
728 <        for (i = hp->ns; i--; )
729 <                for (j = hp->ns; j--; )
730 <                        if ((ap = ambsample(hp, i, j)) != NULL) {
731 <                                addcolor(acol, ap->v);
732 <                                ++cnt;
733 <                        }
734 <        if (!cnt) {
523 <                setcolor(rcol, 0.0, 0.0, 0.0);
524 <                free(hp);
525 <                return(0);              /* no valid samples */
726 >        if (crlp != NULL)
727 >                *crlp = 0;
728 >        if (hp == NULL)                 /* sampling falure? */
729 >                return(0);
730 >
731 >        if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
732 >                        (hp->sampOK < 0) | (hp->ns < MINADIV)) {
733 >                free(hp);               /* Hessian not requested/possible */
734 >                return(-1);             /* value-only return value */
735          }
736 <        copycolor(rcol, acol);          /* final indirect irradiance/PI */
737 <        if (cnt < hp->ns*hp->ns ||      /* incomplete sampling? */
738 <                        (ra == NULL) & (pg == NULL) & (dg == NULL)) {
739 <                free(hp);
740 <                return(-1);             /* no radius or gradient calc. */
736 >        if ((d = scolor_mean(rcol)) > FTINY) {
737 >                d = 0.99*(hp->ns*hp->ns)/d;     /* normalize avg. values */
738 >                K = 0.01;
739 >        } else {                        /* or fall back on geometric Hessian */
740 >                K = 1.0;
741 >                pg = NULL;
742 >                dg = NULL;
743 >                crlp = NULL;
744          }
745 <        if (bright(acol) > FTINY)       /* normalize Y values */
534 <                d = cnt/bright(acol);
535 <        else
536 <                d = 0.0;
537 <        ap = hp->sa;                    /* relative Y channel from here on... */
745 >        ap = hp->sa;                    /* single channel from here on... */
746          for (i = hp->ns*hp->ns; i--; ap++)
747 <                colval(ap->v,CIEY) = bright(ap->v)*d + 0.01;
747 >                ap->v[0] = scolor_mean(ap->v)*d + K;
748  
749          if (uv == NULL)                 /* make sure we have axis pointers */
750                  uv = my_uv;
# Line 560 | Line 768 | doambient(                             /* compute ambient component */
768                          if (ra[1] < minarad)
769                                  ra[1] = minarad;
770                  }
771 <                ra[0] *= d = 1.0/sqrt(sqrt(wt));
771 >                ra[0] *= d = 1.0/sqrt(fabs(wt));
772                  if ((ra[1] *= d) > 2.0*ra[0])
773                          ra[1] = 2.0*ra[0];
774                  if (ra[1] > maxarad) {
# Line 568 | Line 776 | doambient(                             /* compute ambient component */
776                          if (ra[0] > maxarad)
777                                  ra[0] = maxarad;
778                  }
779 +                                        /* flag encroached directions */
780 +                if (crlp != NULL)       /* XXX doesn't update with changes to ambacc */
781 +                        *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc);
782                  if (pg != NULL) {       /* cap gradient if necessary */
783                          d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1];
784                          if (d > 1.0) {
# Line 580 | Line 791 | doambient(                             /* compute ambient component */
791          free(hp);                       /* clean up and return */
792          return(1);
793   }
583
584
585 #else /* ! NEWAMB */
586
587
588 void
589 inithemi(                       /* initialize sampling hemisphere */
590        AMBHEMI  *hp,
591        COLOR ac,
592        RAY  *r,
593        double  wt
594 )
595 {
596        double  d;
597        int  i;
598                                        /* set number of divisions */
599        if (ambacc <= FTINY &&
600                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
601                wt = d;                 /* avoid ray termination */
602        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
603        i = ambacc > FTINY ? 3 : 1;     /* minimum number of samples */
604        if (hp->nt < i)
605                hp->nt = i;
606        hp->np = PI * hp->nt + 0.5;
607                                        /* set number of super-samples */
608        hp->ns = ambssamp * wt + 0.5;
609                                        /* assign coefficient */
610        copycolor(hp->acoef, ac);
611        d = 1.0/(hp->nt*hp->np);
612        scalecolor(hp->acoef, d);
613                                        /* make axes */
614        VCOPY(hp->uz, r->ron);
615        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
616        for (i = 0; i < 3; i++)
617                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
618                        break;
619        if (i >= 3)
620                error(CONSISTENCY, "bad ray direction in inithemi");
621        hp->uy[i] = 1.0;
622        fcross(hp->ux, hp->uy, hp->uz);
623        normalize(hp->ux);
624        fcross(hp->uy, hp->uz, hp->ux);
625 }
626
627
628 int
629 divsample(                              /* sample a division */
630        AMBSAMP  *dp,
631        AMBHEMI  *h,
632        RAY  *r
633 )
634 {
635        RAY  ar;
636        int  hlist[3];
637        double  spt[2];
638        double  xd, yd, zd;
639        double  b2;
640        double  phi;
641        int  i;
642                                        /* ambient coefficient for weight */
643        if (ambacc > FTINY)
644                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
645        else
646                copycolor(ar.rcoef, h->acoef);
647        if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0)
648                return(-1);
649        if (ambacc > FTINY) {
650                multcolor(ar.rcoef, h->acoef);
651                scalecolor(ar.rcoef, 1./AVGREFL);
652        }
653        hlist[0] = r->rno;
654        hlist[1] = dp->t;
655        hlist[2] = dp->p;
656        multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n));
657        zd = sqrt((dp->t + spt[0])/h->nt);
658        phi = 2.0*PI * (dp->p + spt[1])/h->np;
659        xd = tcos(phi) * zd;
660        yd = tsin(phi) * zd;
661        zd = sqrt(1.0 - zd*zd);
662        for (i = 0; i < 3; i++)
663                ar.rdir[i] =    xd*h->ux[i] +
664                                yd*h->uy[i] +
665                                zd*h->uz[i];
666        checknorm(ar.rdir);
667        dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
668        rayvalue(&ar);
669        ndims--;
670        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
671        addcolor(dp->v, ar.rcol);
672                                        /* use rt to improve gradient calc */
673        if (ar.rt > FTINY && ar.rt < FHUGE)
674                dp->r += 1.0/ar.rt;
675                                        /* (re)initialize error */
676        if (dp->n++) {
677                b2 = bright(dp->v)/dp->n - bright(ar.rcol);
678                b2 = b2*b2 + dp->k*((dp->n-1)*(dp->n-1));
679                dp->k = b2/(dp->n*dp->n);
680        } else
681                dp->k = 0.0;
682        return(0);
683 }
684
685
686 static int
687 ambcmp(                                 /* decreasing order */
688        const void *p1,
689        const void *p2
690 )
691 {
692        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
693        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
694
695        if (d1->k < d2->k)
696                return(1);
697        if (d1->k > d2->k)
698                return(-1);
699        return(0);
700 }
701
702
703 static int
704 ambnorm(                                /* standard order */
705        const void *p1,
706        const void *p2
707 )
708 {
709        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
710        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
711        int     c;
712
713        if ( (c = d1->t - d2->t) )
714                return(c);
715        return(d1->p - d2->p);
716 }
717
718
719 double
720 doambient(                              /* compute ambient component */
721        COLOR  rcol,
722        RAY  *r,
723        double  wt,
724        FVECT  pg,
725        FVECT  dg
726 )
727 {
728        double  b, d=0;
729        AMBHEMI  hemi;
730        AMBSAMP  *div;
731        AMBSAMP  dnew;
732        double  acol[3];
733        AMBSAMP  *dp;
734        double  arad;
735        int  divcnt;
736        int  i, j;
737                                        /* initialize hemisphere */
738        inithemi(&hemi, rcol, r, wt);
739        divcnt = hemi.nt * hemi.np;
740                                        /* initialize */
741        if (pg != NULL)
742                pg[0] = pg[1] = pg[2] = 0.0;
743        if (dg != NULL)
744                dg[0] = dg[1] = dg[2] = 0.0;
745        setcolor(rcol, 0.0, 0.0, 0.0);
746        if (divcnt == 0)
747                return(0.0);
748                                        /* allocate super-samples */
749        if (hemi.ns > 0 || pg != NULL || dg != NULL) {
750                div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
751                if (div == NULL)
752                        error(SYSTEM, "out of memory in doambient");
753        } else
754                div = NULL;
755                                        /* sample the divisions */
756        arad = 0.0;
757        acol[0] = acol[1] = acol[2] = 0.0;
758        if ((dp = div) == NULL)
759                dp = &dnew;
760        divcnt = 0;
761        for (i = 0; i < hemi.nt; i++)
762                for (j = 0; j < hemi.np; j++) {
763                        dp->t = i; dp->p = j;
764                        setcolor(dp->v, 0.0, 0.0, 0.0);
765                        dp->r = 0.0;
766                        dp->n = 0;
767                        if (divsample(dp, &hemi, r) < 0) {
768                                if (div != NULL)
769                                        dp++;
770                                continue;
771                        }
772                        arad += dp->r;
773                        divcnt++;
774                        if (div != NULL)
775                                dp++;
776                        else
777                                addcolor(acol, dp->v);
778                }
779        if (!divcnt) {
780                if (div != NULL)
781                        free((void *)div);
782                return(0.0);            /* no samples taken */
783        }
784        if (divcnt < hemi.nt*hemi.np) {
785                pg = dg = NULL;         /* incomplete sampling */
786                hemi.ns = 0;
787        } else if (arad > FTINY && divcnt/arad < minarad) {
788                hemi.ns = 0;            /* close enough */
789        } else if (hemi.ns > 0) {       /* else perform super-sampling? */
790                comperrs(div, &hemi);                   /* compute errors */
791                qsort(div, divcnt, sizeof(AMBSAMP), ambcmp);    /* sort divs */
792                                                /* super-sample */
793                for (i = hemi.ns; i > 0; i--) {
794                        dnew = *div;
795                        if (divsample(&dnew, &hemi, r) < 0) {
796                                dp++;
797                                continue;
798                        }
799                        dp = div;               /* reinsert */
800                        j = divcnt < i ? divcnt : i;
801                        while (--j > 0 && dnew.k < dp[1].k) {
802                                *dp = *(dp+1);
803                                dp++;
804                        }
805                        *dp = dnew;
806                }
807                if (pg != NULL || dg != NULL)   /* restore order */
808                        qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
809        }
810                                        /* compute returned values */
811        if (div != NULL) {
812                arad = 0.0;             /* note: divcnt may be < nt*np */
813                for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
814                        arad += dp->r;
815                        if (dp->n > 1) {
816                                b = 1.0/dp->n;
817                                scalecolor(dp->v, b);
818                                dp->r *= b;
819                                dp->n = 1;
820                        }
821                        addcolor(acol, dp->v);
822                }
823                b = bright(acol);
824                if (b > FTINY) {
825                        b = 1.0/b;      /* compute & normalize gradient(s) */
826                        if (pg != NULL) {
827                                posgradient(pg, div, &hemi);
828                                for (i = 0; i < 3; i++)
829                                        pg[i] *= b;
830                        }
831                        if (dg != NULL) {
832                                dirgradient(dg, div, &hemi);
833                                for (i = 0; i < 3; i++)
834                                        dg[i] *= b;
835                        }
836                }
837                free((void *)div);
838        }
839        copycolor(rcol, acol);
840        if (arad <= FTINY)
841                arad = maxarad;
842        else
843                arad = (divcnt+hemi.ns)/arad;
844        if (pg != NULL) {               /* reduce radius if gradient large */
845                d = DOT(pg,pg);
846                if (d*arad*arad > 1.0)
847                        arad = 1.0/sqrt(d);
848        }
849        if (arad < minarad) {
850                arad = minarad;
851                if (pg != NULL && d*arad*arad > 1.0) {  /* cap gradient */
852                        d = 1.0/arad/sqrt(d);
853                        for (i = 0; i < 3; i++)
854                                pg[i] *= d;
855                }
856        }
857        if ((arad /= sqrt(wt)) > maxarad)
858                arad = maxarad;
859        return(arad);
860 }
861
862
863 void
864 comperrs(                       /* compute initial error estimates */
865        AMBSAMP  *da,   /* assumes standard ordering */
866        AMBHEMI  *hp
867 )
868 {
869        double  b, b2;
870        int  i, j;
871        AMBSAMP  *dp;
872                                /* sum differences from neighbors */
873        dp = da;
874        for (i = 0; i < hp->nt; i++)
875                for (j = 0; j < hp->np; j++) {
876 #ifdef  DEBUG
877                        if (dp->t != i || dp->p != j)
878                                error(CONSISTENCY,
879                                        "division order in comperrs");
880 #endif
881                        b = bright(dp[0].v);
882                        if (i > 0) {            /* from above */
883                                b2 = bright(dp[-hp->np].v) - b;
884                                b2 *= b2 * 0.25;
885                                dp[0].k += b2;
886                                dp[-hp->np].k += b2;
887                        }
888                        if (j > 0) {            /* from behind */
889                                b2 = bright(dp[-1].v) - b;
890                                b2 *= b2 * 0.25;
891                                dp[0].k += b2;
892                                dp[-1].k += b2;
893                        } else {                /* around */
894                                b2 = bright(dp[hp->np-1].v) - b;
895                                b2 *= b2 * 0.25;
896                                dp[0].k += b2;
897                                dp[hp->np-1].k += b2;
898                        }
899                        dp++;
900                }
901                                /* divide by number of neighbors */
902        dp = da;
903        for (j = 0; j < hp->np; j++)            /* top row */
904                (dp++)->k *= 1.0/3.0;
905        if (hp->nt < 2)
906                return;
907        for (i = 1; i < hp->nt-1; i++)          /* central region */
908                for (j = 0; j < hp->np; j++)
909                        (dp++)->k *= 0.25;
910        for (j = 0; j < hp->np; j++)            /* bottom row */
911                (dp++)->k *= 1.0/3.0;
912 }
913
914
915 void
916 posgradient(                                    /* compute position gradient */
917        FVECT  gv,
918        AMBSAMP  *da,                   /* assumes standard ordering */
919        AMBHEMI  *hp
920 )
921 {
922        int  i, j;
923        double  nextsine, lastsine, b, d;
924        double  mag0, mag1;
925        double  phi, cosp, sinp, xd, yd;
926        AMBSAMP  *dp;
927
928        xd = yd = 0.0;
929        for (j = 0; j < hp->np; j++) {
930                dp = da + j;
931                mag0 = mag1 = 0.0;
932                lastsine = 0.0;
933                for (i = 0; i < hp->nt; i++) {
934 #ifdef  DEBUG
935                        if (dp->t != i || dp->p != j)
936                                error(CONSISTENCY,
937                                        "division order in posgradient");
938 #endif
939                        b = bright(dp->v);
940                        if (i > 0) {
941                                d = dp[-hp->np].r;
942                                if (dp[0].r > d) d = dp[0].r;
943                                                        /* sin(t)*cos(t)^2 */
944                                d *= lastsine * (1.0 - (double)i/hp->nt);
945                                mag0 += d*(b - bright(dp[-hp->np].v));
946                        }
947                        nextsine = sqrt((double)(i+1)/hp->nt);
948                        if (j > 0) {
949                                d = dp[-1].r;
950                                if (dp[0].r > d) d = dp[0].r;
951                                mag1 += d * (nextsine - lastsine) *
952                                                (b - bright(dp[-1].v));
953                        } else {
954                                d = dp[hp->np-1].r;
955                                if (dp[0].r > d) d = dp[0].r;
956                                mag1 += d * (nextsine - lastsine) *
957                                                (b - bright(dp[hp->np-1].v));
958                        }
959                        dp += hp->np;
960                        lastsine = nextsine;
961                }
962                mag0 *= 2.0*PI / hp->np;
963                phi = 2.0*PI * (double)j/hp->np;
964                cosp = tcos(phi); sinp = tsin(phi);
965                xd += mag0*cosp - mag1*sinp;
966                yd += mag0*sinp + mag1*cosp;
967        }
968        for (i = 0; i < 3; i++)
969                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
970 }
971
972
973 void
974 dirgradient(                                    /* compute direction gradient */
975        FVECT  gv,
976        AMBSAMP  *da,                   /* assumes standard ordering */
977        AMBHEMI  *hp
978 )
979 {
980        int  i, j;
981        double  mag;
982        double  phi, xd, yd;
983        AMBSAMP  *dp;
984
985        xd = yd = 0.0;
986        for (j = 0; j < hp->np; j++) {
987                dp = da + j;
988                mag = 0.0;
989                for (i = 0; i < hp->nt; i++) {
990 #ifdef  DEBUG
991                        if (dp->t != i || dp->p != j)
992                                error(CONSISTENCY,
993                                        "division order in dirgradient");
994 #endif
995                                                        /* tan(t) */
996                        mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0);
997                        dp += hp->np;
998                }
999                phi = 2.0*PI * (j+.5)/hp->np + PI/2.0;
1000                xd += mag * tcos(phi);
1001                yd += mag * tsin(phi);
1002        }
1003        for (i = 0; i < 3; i++)
1004                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
1005 }
1006
1007 #endif  /* ! NEWAMB */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines