ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambcomp.c
Revision: 2.56
Committed: Fri May 9 20:05:00 2014 UTC (9 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.55: +29 -166 lines
Log Message:
Tore out complicated vertex comparisons that were no faster (-DNEWAMB)

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.56 static const char RCSid[] = "$Id: ambcomp.c,v 2.55 2014/05/09 16:05:09 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Routines to compute "ambient" values using Monte Carlo
6 greg 2.9 *
7 greg 2.27 * Hessian calculations based on "Practical Hessian-Based Error Control
8     * for Irradiance Caching" by Schwarzhaupt, Wann Jensen, & Jarosz
9     * from ACM SIGGRAPH Asia 2012 conference proceedings.
10     *
11 greg 2.46 * 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 greg 2.9 * Declarations of external symbols in ambient.h
16     */
17    
18 greg 2.10 #include "copyright.h"
19 greg 1.1
20     #include "ray.h"
21 greg 2.25 #include "ambient.h"
22     #include "random.h"
23 greg 1.1
24 greg 2.25 #ifdef NEWAMB
25 greg 1.1
26 greg 2.26 extern void SDsquare2disk(double ds[2], double seedx, double seedy);
27    
28     typedef struct {
29 greg 2.44 COLOR v; /* hemisphere sample value */
30 greg 2.47 float d; /* reciprocal distance (1/rt) */
31 greg 2.44 FVECT p; /* intersection point */
32     } AMBSAMP; /* sample value */
33    
34     typedef struct {
35 greg 2.26 RAY *rp; /* originating ray sample */
36 greg 2.27 FVECT ux, uy; /* tangent axis unit vectors */
37 greg 2.26 int ns; /* number of samples per axis */
38     COLOR acoef; /* division contribution coefficient */
39 greg 2.44 AMBSAMP sa[1]; /* sample array (extends struct) */
40 greg 2.26 } AMBHEMI; /* ambient sample hemisphere */
41    
42 greg 2.56 #define AI(h,i,j) ((i)*(h)->ns + (j))
43     #define ambsam(h,i,j) (h)->sa[AI(h,i,j)]
44 greg 2.26
45 greg 2.27 typedef struct {
46 greg 2.35 FVECT r_i, r_i1, e_i, rcp, rI2_eJ2;
47     double I1, I2;
48 greg 2.27 } FFTRI; /* vectors and coefficients for Hessian calculation */
49    
50 greg 2.26
51     static AMBHEMI *
52     inithemi( /* initialize sampling hemisphere */
53     COLOR ac,
54     RAY *r,
55     double wt
56     )
57     {
58     AMBHEMI *hp;
59     double d;
60     int n, i;
61     /* set number of divisions */
62     if (ambacc <= FTINY &&
63     wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
64     wt = d; /* avoid ray termination */
65     n = sqrt(ambdiv * wt) + 0.5;
66 greg 2.27 i = 1 + 5*(ambacc > FTINY); /* minimum number of samples */
67 greg 2.26 if (n < i)
68     n = i;
69     /* allocate sampling array */
70 greg 2.41 hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
71 greg 2.26 if (hp == NULL)
72     return(NULL);
73     hp->rp = r;
74     hp->ns = n;
75     /* assign coefficient */
76     copycolor(hp->acoef, ac);
77     d = 1.0/(n*n);
78     scalecolor(hp->acoef, d);
79 greg 2.28 /* make tangent plane axes */
80 greg 2.38 hp->uy[0] = 0.5 - frandom();
81     hp->uy[1] = 0.5 - frandom();
82     hp->uy[2] = 0.5 - frandom();
83 greg 2.36 for (i = 3; i--; )
84 greg 2.37 if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6))
85     break;
86     if (i < 0)
87     error(CONSISTENCY, "bad ray direction in inithemi");
88     hp->uy[i] = 1.0;
89 greg 2.27 VCROSS(hp->ux, hp->uy, r->ron);
90 greg 2.26 normalize(hp->ux);
91 greg 2.27 VCROSS(hp->uy, r->ron, hp->ux);
92 greg 2.26 /* we're ready to sample */
93     return(hp);
94     }
95    
96    
97 greg 2.43 /* Sample ambient division and apply weighting coefficient */
98 greg 2.41 static int
99 greg 2.43 getambsamp(RAY *arp, AMBHEMI *hp, int i, int j, int n)
100 greg 2.26 {
101 greg 2.41 int hlist[3], ii;
102     double spt[2], zd;
103 greg 2.26 /* ambient coefficient for weight */
104     if (ambacc > FTINY)
105 greg 2.41 setcolor(arp->rcoef, AVGREFL, AVGREFL, AVGREFL);
106 greg 2.26 else
107 greg 2.41 copycolor(arp->rcoef, hp->acoef);
108     if (rayorigin(arp, AMBIENT, hp->rp, arp->rcoef) < 0)
109     return(0);
110 greg 2.26 if (ambacc > FTINY) {
111 greg 2.41 multcolor(arp->rcoef, hp->acoef);
112     scalecolor(arp->rcoef, 1./AVGREFL);
113     }
114     hlist[0] = hp->rp->rno;
115 greg 2.46 hlist[1] = j;
116     hlist[2] = i;
117 greg 2.41 multisamp(spt, 2, urand(ilhash(hlist,3)+n));
118     if (!n) { /* avoid border samples for n==0 */
119 greg 2.46 if ((spt[0] < 0.1) | (spt[0] >= 0.9))
120 greg 2.41 spt[0] = 0.1 + 0.8*frandom();
121 greg 2.46 if ((spt[1] < 0.1) | (spt[1] >= 0.9))
122 greg 2.41 spt[1] = 0.1 + 0.8*frandom();
123 greg 2.26 }
124 greg 2.46 SDsquare2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns);
125 greg 2.26 zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
126     for (ii = 3; ii--; )
127 greg 2.41 arp->rdir[ii] = spt[0]*hp->ux[ii] +
128 greg 2.26 spt[1]*hp->uy[ii] +
129     zd*hp->rp->ron[ii];
130 greg 2.41 checknorm(arp->rdir);
131 greg 2.56 dimlist[ndims++] = AI(hp,i,j) + 90171;
132 greg 2.43 rayvalue(arp); /* evaluate ray */
133     ndims--; /* apply coefficient */
134     multcolor(arp->rcol, arp->rcoef);
135 greg 2.41 return(1);
136     }
137    
138    
139     static AMBSAMP *
140 greg 2.43 ambsample( /* initial ambient division sample */
141 greg 2.41 AMBHEMI *hp,
142     int i,
143     int j
144     )
145     {
146 greg 2.43 AMBSAMP *ap = &ambsam(hp,i,j);
147 greg 2.41 RAY ar;
148     /* generate hemispherical sample */
149 greg 2.47 if (!getambsamp(&ar, hp, i, j, 0) || ar.rt <= FTINY) {
150     memset(ap, 0, sizeof(AMBSAMP));
151     return(NULL);
152     }
153     ap->d = 1.0/ar.rt; /* limit vertex distance */
154 greg 2.34 if (ar.rt > 10.0*thescene.cusize)
155     ar.rt = 10.0*thescene.cusize;
156 greg 2.31 VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
157 greg 2.26 copycolor(ap->v, ar.rcol);
158 greg 2.28 return(ap);
159 greg 2.26 }
160    
161    
162 greg 2.41 /* Estimate errors based on ambient division differences */
163     static float *
164     getambdiffs(AMBHEMI *hp)
165     {
166 greg 2.55 float *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float));
167 greg 2.41 float *ep;
168 greg 2.42 AMBSAMP *ap;
169 greg 2.41 double b, d2;
170     int i, j;
171    
172     if (earr == NULL) /* out of memory? */
173     return(NULL);
174     /* compute squared neighbor diffs */
175 greg 2.42 for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++)
176     for (j = 0; j < hp->ns; j++, ap++, ep++) {
177     b = bright(ap[0].v);
178 greg 2.41 if (i) { /* from above */
179 greg 2.42 d2 = b - bright(ap[-hp->ns].v);
180 greg 2.41 d2 *= d2;
181     ep[0] += d2;
182     ep[-hp->ns] += d2;
183     }
184 greg 2.55 if (!j) continue;
185     /* from behind */
186     d2 = b - bright(ap[-1].v);
187     d2 *= d2;
188     ep[0] += d2;
189     ep[-1] += d2;
190     if (!i) continue;
191     /* diagonal */
192     d2 = b - bright(ap[-hp->ns-1].v);
193     d2 *= d2;
194     ep[0] += d2;
195     ep[-hp->ns-1] += d2;
196 greg 2.41 }
197     /* correct for number of neighbors */
198 greg 2.55 earr[0] *= 8./3.;
199     earr[hp->ns-1] *= 8./3.;
200     earr[(hp->ns-1)*hp->ns] *= 8./3.;
201     earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.;
202 greg 2.41 for (i = 1; i < hp->ns-1; i++) {
203 greg 2.55 earr[i*hp->ns] *= 8./5.;
204     earr[i*hp->ns + hp->ns-1] *= 8./5.;
205 greg 2.41 }
206     for (j = 1; j < hp->ns-1; j++) {
207 greg 2.55 earr[j] *= 8./5.;
208     earr[(hp->ns-1)*hp->ns + j] *= 8./5.;
209 greg 2.41 }
210     return(earr);
211     }
212    
213    
214 greg 2.43 /* Perform super-sampling on hemisphere (introduces bias) */
215 greg 2.41 static void
216     ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
217     {
218     float *earr = getambdiffs(hp);
219 greg 2.54 double e2rem = 0;
220 greg 2.41 AMBSAMP *ap;
221     RAY ar;
222 greg 2.50 double asum[3];
223 greg 2.41 float *ep;
224 greg 2.55 int i, j, n, nss;
225 greg 2.41
226     if (earr == NULL) /* just skip calc. if no memory */
227     return;
228 greg 2.54 /* accumulate estimated variances */
229 greg 2.55 for (ep = earr + hp->ns*hp->ns; ep > earr; )
230     e2rem += *--ep;
231 greg 2.41 ep = earr; /* perform super-sampling */
232     for (ap = hp->sa, i = 0; i < hp->ns; i++)
233     for (j = 0; j < hp->ns; j++, ap++) {
234 greg 2.55 if (e2rem <= FTINY)
235     goto done; /* nothing left to do */
236     nss = *ep/e2rem*cnt + frandom();
237 greg 2.50 asum[0] = asum[1] = asum[2] = 0.0;
238 greg 2.41 for (n = 1; n <= nss; n++) {
239 greg 2.43 if (!getambsamp(&ar, hp, i, j, n)) {
240 greg 2.41 nss = n-1;
241     break;
242     }
243     addcolor(asum, ar.rcol);
244     }
245     if (nss) { /* update returned ambient value */
246 greg 2.54 const double ssf = 1./(nss + 1.);
247 greg 2.41 for (n = 3; n--; )
248 greg 2.50 acol[n] += ssf*asum[n] +
249 greg 2.41 (ssf - 1.)*colval(ap->v,n);
250     }
251 greg 2.54 e2rem -= *ep++; /* update remainders */
252 greg 2.41 cnt -= nss;
253     }
254 greg 2.55 done:
255 greg 2.41 free(earr);
256     }
257    
258    
259 greg 2.46 /* Return brightness of farthest ambient sample */
260     static double
261 greg 2.56 back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3)
262 greg 2.46 {
263 greg 2.56 if (hp->sa[n1].d <= hp->sa[n2].d) {
264     if (hp->sa[n1].d <= hp->sa[n3].d)
265     return(colval(hp->sa[n1].v,CIEY));
266     return(colval(hp->sa[n3].v,CIEY));
267     }
268     if (hp->sa[n2].d <= hp->sa[n3].d)
269     return(colval(hp->sa[n2].v,CIEY));
270     return(colval(hp->sa[n3].v,CIEY));
271 greg 2.46 }
272    
273    
274 greg 2.27 /* Compute vectors and coefficients for Hessian/gradient calcs */
275     static void
276 greg 2.56 comp_fftri(FFTRI *ftp, AMBHEMI *hp, const int n0, const int n1)
277 greg 2.27 {
278 greg 2.56 double rdot_cp, dot_e, dot_er, rdot_r, rdot_r1, J2;
279     int ii;
280    
281     VSUB(ftp->r_i, hp->sa[n0].p, hp->rp->rop);
282     VSUB(ftp->r_i1, hp->sa[n1].p, hp->rp->rop);
283     VSUB(ftp->e_i, hp->sa[n1].p, hp->sa[n0].p);
284 greg 2.35 VCROSS(ftp->rcp, ftp->r_i, ftp->r_i1);
285     rdot_cp = 1.0/DOT(ftp->rcp,ftp->rcp);
286 greg 2.27 dot_e = DOT(ftp->e_i,ftp->e_i);
287     dot_er = DOT(ftp->e_i, ftp->r_i);
288 greg 2.32 rdot_r = 1.0/DOT(ftp->r_i,ftp->r_i);
289     rdot_r1 = 1.0/DOT(ftp->r_i1,ftp->r_i1);
290     ftp->I1 = acos( DOT(ftp->r_i, ftp->r_i1) * sqrt(rdot_r*rdot_r1) ) *
291 greg 2.35 sqrt( rdot_cp );
292 greg 2.32 ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)*rdot_r1 - dot_er*rdot_r +
293 greg 2.35 dot_e*ftp->I1 )*0.5*rdot_cp;
294 greg 2.32 J2 = ( 0.5*(rdot_r - rdot_r1) - dot_er*ftp->I2 ) / dot_e;
295 greg 2.46 for (ii = 3; ii--; )
296     ftp->rI2_eJ2[ii] = ftp->I2*ftp->r_i[ii] + J2*ftp->e_i[ii];
297 greg 2.27 }
298    
299    
300 greg 2.28 /* Compose 3x3 matrix from two vectors */
301 greg 2.27 static void
302     compose_matrix(FVECT mat[3], FVECT va, FVECT vb)
303     {
304     mat[0][0] = 2.0*va[0]*vb[0];
305     mat[1][1] = 2.0*va[1]*vb[1];
306     mat[2][2] = 2.0*va[2]*vb[2];
307     mat[0][1] = mat[1][0] = va[0]*vb[1] + va[1]*vb[0];
308     mat[0][2] = mat[2][0] = va[0]*vb[2] + va[2]*vb[0];
309     mat[1][2] = mat[2][1] = va[1]*vb[2] + va[2]*vb[1];
310     }
311    
312    
313     /* Compute partial 3x3 Hessian matrix for edge */
314     static void
315     comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
316     {
317 greg 2.35 FVECT ncp;
318 greg 2.27 FVECT m1[3], m2[3], m3[3], m4[3];
319     double d1, d2, d3, d4;
320     double I3, J3, K3;
321     int i, j;
322     /* compute intermediate coefficients */
323     d1 = 1.0/DOT(ftp->r_i,ftp->r_i);
324     d2 = 1.0/DOT(ftp->r_i1,ftp->r_i1);
325     d3 = 1.0/DOT(ftp->e_i,ftp->e_i);
326     d4 = DOT(ftp->e_i, ftp->r_i);
327 greg 2.35 I3 = ( DOT(ftp->e_i, ftp->r_i1)*d2*d2 - d4*d1*d1 + 3.0/d3*ftp->I2 )
328     / ( 4.0*DOT(ftp->rcp,ftp->rcp) );
329 greg 2.27 J3 = 0.25*d3*(d1*d1 - d2*d2) - d4*d3*I3;
330     K3 = d3*(ftp->I2 - I3/d1 - 2.0*d4*J3);
331     /* intermediate matrices */
332 greg 2.35 VCROSS(ncp, nrm, ftp->e_i);
333     compose_matrix(m1, ncp, ftp->rI2_eJ2);
334 greg 2.27 compose_matrix(m2, ftp->r_i, ftp->r_i);
335     compose_matrix(m3, ftp->e_i, ftp->e_i);
336     compose_matrix(m4, ftp->r_i, ftp->e_i);
337 greg 2.35 d1 = DOT(nrm, ftp->rcp);
338 greg 2.27 d2 = -d1*ftp->I2;
339     d1 *= 2.0;
340     for (i = 3; i--; ) /* final matrix sum */
341     for (j = 3; j--; ) {
342     hess[i][j] = m1[i][j] + d1*( I3*m2[i][j] + K3*m3[i][j] +
343     2.0*J3*m4[i][j] );
344     hess[i][j] += d2*(i==j);
345 greg 2.46 hess[i][j] *= -1.0/PI;
346 greg 2.27 }
347     }
348    
349    
350     /* Reverse hessian calculation result for edge in other direction */
351     static void
352     rev_hessian(FVECT hess[3])
353     {
354     int i;
355    
356     for (i = 3; i--; ) {
357     hess[i][0] = -hess[i][0];
358     hess[i][1] = -hess[i][1];
359     hess[i][2] = -hess[i][2];
360     }
361     }
362    
363    
364     /* Add to radiometric Hessian from the given triangle */
365     static void
366     add2hessian(FVECT hess[3], FVECT ehess1[3],
367 greg 2.46 FVECT ehess2[3], FVECT ehess3[3], double v)
368 greg 2.27 {
369     int i, j;
370    
371     for (i = 3; i--; )
372     for (j = 3; j--; )
373     hess[i][j] += v*( ehess1[i][j] + ehess2[i][j] + ehess3[i][j] );
374     }
375    
376    
377     /* Compute partial displacement form factor gradient for edge */
378     static void
379     comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm)
380     {
381 greg 2.35 FVECT ncp;
382 greg 2.27 double f1;
383     int i;
384    
385 greg 2.35 f1 = 2.0*DOT(nrm, ftp->rcp);
386     VCROSS(ncp, nrm, ftp->e_i);
387 greg 2.27 for (i = 3; i--; )
388 greg 2.46 grad[i] = (0.5/PI)*( ftp->I1*ncp[i] + f1*ftp->rI2_eJ2[i] );
389 greg 2.27 }
390    
391    
392     /* Reverse gradient calculation result for edge in other direction */
393     static void
394     rev_gradient(FVECT grad)
395     {
396     grad[0] = -grad[0];
397     grad[1] = -grad[1];
398     grad[2] = -grad[2];
399     }
400    
401    
402     /* Add to displacement gradient from the given triangle */
403     static void
404 greg 2.46 add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, double v)
405 greg 2.27 {
406     int i;
407    
408     for (i = 3; i--; )
409     grad[i] += v*( egrad1[i] + egrad2[i] + egrad3[i] );
410     }
411    
412    
413     /* Compute anisotropic radii and eigenvector directions */
414 greg 2.53 static void
415 greg 2.27 eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3])
416     {
417     double hess2[2][2];
418     FVECT a, b;
419     double evalue[2], slope1, xmag1;
420     int i;
421     /* project Hessian to sample plane */
422     for (i = 3; i--; ) {
423     a[i] = DOT(hessian[i], uv[0]);
424     b[i] = DOT(hessian[i], uv[1]);
425     }
426     hess2[0][0] = DOT(uv[0], a);
427     hess2[0][1] = DOT(uv[0], b);
428     hess2[1][0] = DOT(uv[1], a);
429     hess2[1][1] = DOT(uv[1], b);
430 greg 2.38 /* compute eigenvalue(s) */
431     i = quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1],
432     hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]);
433     if (i == 1) /* double-root (circle) */
434     evalue[1] = evalue[0];
435     if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) |
436 greg 2.53 ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) {
437     ra[0] = ra[1] = maxarad;
438     return;
439     }
440 greg 2.27 if (evalue[0] > evalue[1]) {
441 greg 2.29 ra[0] = sqrt(sqrt(4.0/evalue[0]));
442     ra[1] = sqrt(sqrt(4.0/evalue[1]));
443 greg 2.27 slope1 = evalue[1];
444     } else {
445 greg 2.29 ra[0] = sqrt(sqrt(4.0/evalue[1]));
446     ra[1] = sqrt(sqrt(4.0/evalue[0]));
447 greg 2.27 slope1 = evalue[0];
448     }
449     /* compute unit eigenvectors */
450     if (fabs(hess2[0][1]) <= FTINY)
451     return; /* uv OK as is */
452     slope1 = (slope1 - hess2[0][0]) / hess2[0][1];
453     xmag1 = sqrt(1.0/(1.0 + slope1*slope1));
454     for (i = 3; i--; ) {
455     b[i] = xmag1*uv[0][i] + slope1*xmag1*uv[1][i];
456     a[i] = slope1*xmag1*uv[0][i] - xmag1*uv[1][i];
457     }
458     VCOPY(uv[0], a);
459     VCOPY(uv[1], b);
460     }
461    
462    
463 greg 2.26 static void
464     ambHessian( /* anisotropic radii & pos. gradient */
465     AMBHEMI *hp,
466     FVECT uv[2], /* returned */
467 greg 2.28 float ra[2], /* returned (optional) */
468     float pg[2] /* returned (optional) */
469 greg 2.26 )
470     {
471 greg 2.27 static char memerrmsg[] = "out of memory in ambHessian()";
472     FVECT (*hessrow)[3] = NULL;
473     FVECT *gradrow = NULL;
474     FVECT hessian[3];
475     FVECT gradient;
476     FFTRI fftr;
477     int i, j;
478     /* be sure to assign unit vectors */
479     VCOPY(uv[0], hp->ux);
480     VCOPY(uv[1], hp->uy);
481     /* clock-wise vertex traversal from sample POV */
482     if (ra != NULL) { /* initialize Hessian row buffer */
483 greg 2.28 hessrow = (FVECT (*)[3])malloc(sizeof(FVECT)*3*(hp->ns-1));
484 greg 2.27 if (hessrow == NULL)
485     error(SYSTEM, memerrmsg);
486     memset(hessian, 0, sizeof(hessian));
487     } else if (pg == NULL) /* bogus call? */
488     return;
489     if (pg != NULL) { /* initialize form factor row buffer */
490 greg 2.28 gradrow = (FVECT *)malloc(sizeof(FVECT)*(hp->ns-1));
491 greg 2.27 if (gradrow == NULL)
492     error(SYSTEM, memerrmsg);
493     memset(gradient, 0, sizeof(gradient));
494     }
495     /* compute first row of edges */
496     for (j = 0; j < hp->ns-1; j++) {
497 greg 2.56 comp_fftri(&fftr, hp, AI(hp,0,j), AI(hp,0,j+1));
498 greg 2.27 if (hessrow != NULL)
499     comp_hessian(hessrow[j], &fftr, hp->rp->ron);
500     if (gradrow != NULL)
501     comp_gradient(gradrow[j], &fftr, hp->rp->ron);
502     }
503     /* sum each row of triangles */
504     for (i = 0; i < hp->ns-1; i++) {
505     FVECT hesscol[3]; /* compute first vertical edge */
506     FVECT gradcol;
507 greg 2.56 comp_fftri(&fftr, hp, AI(hp,i,0), AI(hp,i+1,0));
508 greg 2.27 if (hessrow != NULL)
509     comp_hessian(hesscol, &fftr, hp->rp->ron);
510     if (gradrow != NULL)
511     comp_gradient(gradcol, &fftr, hp->rp->ron);
512     for (j = 0; j < hp->ns-1; j++) {
513     FVECT hessdia[3]; /* compute triangle contributions */
514     FVECT graddia;
515 greg 2.46 double backg;
516 greg 2.56 backg = back_ambval(hp, AI(hp,i,j),
517     AI(hp,i,j+1), AI(hp,i+1,j));
518 greg 2.27 /* diagonal (inner) edge */
519 greg 2.56 comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j));
520 greg 2.27 if (hessrow != NULL) {
521     comp_hessian(hessdia, &fftr, hp->rp->ron);
522     rev_hessian(hesscol);
523     add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
524     }
525 greg 2.39 if (gradrow != NULL) {
526 greg 2.27 comp_gradient(graddia, &fftr, hp->rp->ron);
527     rev_gradient(gradcol);
528     add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
529     }
530     /* initialize edge in next row */
531 greg 2.56 comp_fftri(&fftr, hp, AI(hp,i+1,j+1), AI(hp,i+1,j));
532 greg 2.27 if (hessrow != NULL)
533     comp_hessian(hessrow[j], &fftr, hp->rp->ron);
534     if (gradrow != NULL)
535     comp_gradient(gradrow[j], &fftr, hp->rp->ron);
536     /* new column edge & paired triangle */
537 greg 2.56 backg = back_ambval(hp, AI(hp,i+1,j+1),
538     AI(hp,i+1,j), AI(hp,i,j+1));
539     comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j+1));
540 greg 2.27 if (hessrow != NULL) {
541     comp_hessian(hesscol, &fftr, hp->rp->ron);
542     rev_hessian(hessdia);
543     add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
544     if (i < hp->ns-2)
545     rev_hessian(hessrow[j]);
546     }
547     if (gradrow != NULL) {
548     comp_gradient(gradcol, &fftr, hp->rp->ron);
549     rev_gradient(graddia);
550     add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
551     if (i < hp->ns-2)
552     rev_gradient(gradrow[j]);
553     }
554     }
555     }
556     /* release row buffers */
557     if (hessrow != NULL) free(hessrow);
558     if (gradrow != NULL) free(gradrow);
559    
560     if (ra != NULL) /* extract eigenvectors & radii */
561     eigenvectors(uv, ra, hessian);
562 greg 2.32 if (pg != NULL) { /* tangential position gradient */
563     pg[0] = DOT(gradient, uv[0]);
564     pg[1] = DOT(gradient, uv[1]);
565 greg 2.27 }
566     }
567    
568    
569     /* Compute direction gradient from a hemispherical sampling */
570     static void
571     ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
572     {
573 greg 2.41 AMBSAMP *ap;
574     double dgsum[2];
575     int n;
576     FVECT vd;
577     double gfact;
578 greg 2.27
579 greg 2.29 dgsum[0] = dgsum[1] = 0.0; /* sum values times -tan(theta) */
580 greg 2.27 for (ap = hp->sa, n = hp->ns*hp->ns; n--; ap++) {
581     /* use vector for azimuth + 90deg */
582     VSUB(vd, ap->p, hp->rp->rop);
583 greg 2.29 /* brightness over cosine factor */
584     gfact = colval(ap->v,CIEY) / DOT(hp->rp->ron, vd);
585 greg 2.40 /* sine = proj_radius/vd_length */
586     dgsum[0] -= DOT(uv[1], vd) * gfact;
587     dgsum[1] += DOT(uv[0], vd) * gfact;
588 greg 2.26 }
589 greg 2.29 dg[0] = dgsum[0] / (hp->ns*hp->ns);
590     dg[1] = dgsum[1] / (hp->ns*hp->ns);
591 greg 2.26 }
592    
593 greg 2.27
594 greg 2.49 /* Compute potential light leak direction flags for cache value */
595     static uint32
596     ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
597 greg 2.47 {
598 greg 2.50 const double max_d = 1.0/(minarad*ambacc + 0.001);
599     const double ang_res = 0.5*PI/(hp->ns-1);
600     const double ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY));
601 greg 2.51 double avg_d = 0;
602 greg 2.50 uint32 flgs = 0;
603     int i, j;
604 greg 2.52 /* don't bother for a few samples */
605     if (hp->ns < 12)
606     return(0);
607     /* check distances overhead */
608 greg 2.51 for (i = hp->ns*3/4; i-- > hp->ns>>2; )
609     for (j = hp->ns*3/4; j-- > hp->ns>>2; )
610     avg_d += ambsam(hp,i,j).d;
611     avg_d *= 4.0/(hp->ns*hp->ns);
612 greg 2.52 if (avg_d*r0 >= 1.0) /* ceiling too low for corral? */
613     return(0);
614     if (avg_d >= max_d) /* insurance */
615 greg 2.51 return(0);
616     /* else circle around perimeter */
617 greg 2.47 for (i = 0; i < hp->ns; i++)
618     for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) {
619     AMBSAMP *ap = &ambsam(hp,i,j);
620     FVECT vec;
621 greg 2.49 double u, v;
622 greg 2.50 double ang, a1;
623 greg 2.49 int abp;
624 greg 2.50 if ((ap->d <= FTINY) | (ap->d >= max_d))
625     continue; /* too far or too near */
626 greg 2.47 VSUB(vec, ap->p, hp->rp->rop);
627 greg 2.49 u = DOT(vec, uv[0]) * ap->d;
628     v = DOT(vec, uv[1]) * ap->d;
629     if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0)
630     continue; /* occluder outside ellipse */
631     ang = atan2a(v, u); /* else set direction flags */
632 greg 2.50 for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step)
633     flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0)));
634 greg 2.47 }
635 greg 2.49 return(flgs);
636 greg 2.47 }
637    
638    
639 greg 2.26 int
640     doambient( /* compute ambient component */
641     COLOR rcol, /* input/output color */
642     RAY *r,
643     double wt,
644 greg 2.27 FVECT uv[2], /* returned (optional) */
645     float ra[2], /* returned (optional) */
646     float pg[2], /* returned (optional) */
647 greg 2.49 float dg[2], /* returned (optional) */
648     uint32 *crlp /* returned (optional) */
649 greg 2.26 )
650     {
651 greg 2.41 AMBHEMI *hp = inithemi(rcol, r, wt);
652 greg 2.45 int cnt;
653 greg 2.41 FVECT my_uv[2];
654     double d, K, acol[3];
655     AMBSAMP *ap;
656     int i, j;
657 greg 2.28 /* check/initialize */
658     if (hp == NULL)
659 greg 2.26 return(0);
660     if (uv != NULL)
661     memset(uv, 0, sizeof(FVECT)*2);
662     if (ra != NULL)
663     ra[0] = ra[1] = 0.0;
664     if (pg != NULL)
665     pg[0] = pg[1] = 0.0;
666     if (dg != NULL)
667     dg[0] = dg[1] = 0.0;
668 greg 2.49 if (crlp != NULL)
669     *crlp = 0;
670 greg 2.26 /* sample the hemisphere */
671     acol[0] = acol[1] = acol[2] = 0.0;
672 greg 2.45 cnt = 0;
673 greg 2.27 for (i = hp->ns; i--; )
674     for (j = hp->ns; j--; )
675 greg 2.28 if ((ap = ambsample(hp, i, j)) != NULL) {
676 greg 2.26 addcolor(acol, ap->v);
677     ++cnt;
678     }
679     if (!cnt) {
680     setcolor(rcol, 0.0, 0.0, 0.0);
681     free(hp);
682     return(0); /* no valid samples */
683     }
684 greg 2.41 if (cnt < hp->ns*hp->ns) { /* incomplete sampling? */
685     copycolor(rcol, acol);
686     free(hp);
687     return(-1); /* return value w/o Hessian */
688     }
689     cnt = ambssamp*wt + 0.5; /* perform super-sampling? */
690 greg 2.50 if (cnt > 8)
691 greg 2.41 ambsupersamp(acol, hp, cnt);
692 greg 2.29 copycolor(rcol, acol); /* final indirect irradiance/PI */
693 greg 2.41 if ((ra == NULL) & (pg == NULL) & (dg == NULL)) {
694 greg 2.26 free(hp);
695     return(-1); /* no radius or gradient calc. */
696     }
697 greg 2.45 if ((d = bright(acol)) > FTINY) { /* normalize Y values */
698     d = 0.99*(hp->ns*hp->ns)/d;
699 greg 2.38 K = 0.01;
700 greg 2.45 } else { /* or fall back on geometric Hessian */
701 greg 2.38 K = 1.0;
702     pg = NULL;
703     dg = NULL;
704 greg 2.53 crlp = NULL;
705 greg 2.38 }
706 greg 2.29 ap = hp->sa; /* relative Y channel from here on... */
707 greg 2.26 for (i = hp->ns*hp->ns; i--; ap++)
708 greg 2.38 colval(ap->v,CIEY) = bright(ap->v)*d + K;
709 greg 2.26
710     if (uv == NULL) /* make sure we have axis pointers */
711     uv = my_uv;
712     /* compute radii & pos. gradient */
713     ambHessian(hp, uv, ra, pg);
714 greg 2.29
715 greg 2.26 if (dg != NULL) /* compute direction gradient */
716     ambdirgrad(hp, uv, dg);
717 greg 2.29
718 greg 2.28 if (ra != NULL) { /* scale/clamp radii */
719 greg 2.35 if (pg != NULL) {
720     if (ra[0]*(d = fabs(pg[0])) > 1.0)
721     ra[0] = 1.0/d;
722     if (ra[1]*(d = fabs(pg[1])) > 1.0)
723     ra[1] = 1.0/d;
724 greg 2.48 if (ra[0] > ra[1])
725     ra[0] = ra[1];
726 greg 2.35 }
727 greg 2.29 if (ra[0] < minarad) {
728     ra[0] = minarad;
729     if (ra[1] < minarad)
730     ra[1] = minarad;
731     }
732     ra[0] *= d = 1.0/sqrt(sqrt(wt));
733 greg 2.26 if ((ra[1] *= d) > 2.0*ra[0])
734     ra[1] = 2.0*ra[0];
735 greg 2.28 if (ra[1] > maxarad) {
736     ra[1] = maxarad;
737     if (ra[0] > maxarad)
738     ra[0] = maxarad;
739     }
740 greg 2.53 /* flag encroached directions */
741     if ((wt >= 0.5-FTINY) & (crlp != NULL))
742 greg 2.49 *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc);
743 greg 2.35 if (pg != NULL) { /* cap gradient if necessary */
744     d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1];
745     if (d > 1.0) {
746     d = 1.0/sqrt(d);
747     pg[0] *= d;
748     pg[1] *= d;
749     }
750     }
751 greg 2.26 }
752     free(hp); /* clean up and return */
753     return(1);
754     }
755    
756    
757 greg 2.25 #else /* ! NEWAMB */
758 greg 1.1
759    
760 greg 2.15 void
761 greg 2.14 inithemi( /* initialize sampling hemisphere */
762 greg 2.23 AMBHEMI *hp,
763 greg 2.16 COLOR ac,
764 greg 2.14 RAY *r,
765     double wt
766     )
767 greg 1.1 {
768 greg 2.16 double d;
769 greg 2.23 int i;
770 greg 2.14 /* set number of divisions */
771 greg 2.16 if (ambacc <= FTINY &&
772 greg 2.20 wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
773 greg 2.16 wt = d; /* avoid ray termination */
774     hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
775 greg 2.14 i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */
776     if (hp->nt < i)
777     hp->nt = i;
778     hp->np = PI * hp->nt + 0.5;
779     /* set number of super-samples */
780 greg 2.15 hp->ns = ambssamp * wt + 0.5;
781 greg 2.16 /* assign coefficient */
782 greg 2.14 copycolor(hp->acoef, ac);
783 greg 2.16 d = 1.0/(hp->nt*hp->np);
784     scalecolor(hp->acoef, d);
785 greg 2.14 /* make axes */
786     VCOPY(hp->uz, r->ron);
787     hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
788     for (i = 0; i < 3; i++)
789     if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
790     break;
791     if (i >= 3)
792     error(CONSISTENCY, "bad ray direction in inithemi");
793     hp->uy[i] = 1.0;
794     fcross(hp->ux, hp->uy, hp->uz);
795     normalize(hp->ux);
796     fcross(hp->uy, hp->uz, hp->ux);
797 greg 1.1 }
798    
799    
800 greg 2.9 int
801 greg 2.14 divsample( /* sample a division */
802 greg 2.23 AMBSAMP *dp,
803 greg 2.14 AMBHEMI *h,
804     RAY *r
805     )
806 greg 1.1 {
807     RAY ar;
808 greg 1.11 int hlist[3];
809     double spt[2];
810 greg 1.1 double xd, yd, zd;
811     double b2;
812     double phi;
813 greg 2.23 int i;
814 greg 2.15 /* ambient coefficient for weight */
815 greg 2.16 if (ambacc > FTINY)
816     setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
817     else
818     copycolor(ar.rcoef, h->acoef);
819 greg 2.14 if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0)
820 greg 1.4 return(-1);
821 greg 2.17 if (ambacc > FTINY) {
822     multcolor(ar.rcoef, h->acoef);
823     scalecolor(ar.rcoef, 1./AVGREFL);
824     }
825 greg 1.1 hlist[0] = r->rno;
826     hlist[1] = dp->t;
827     hlist[2] = dp->p;
828 greg 1.13 multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n));
829 greg 1.11 zd = sqrt((dp->t + spt[0])/h->nt);
830     phi = 2.0*PI * (dp->p + spt[1])/h->np;
831 gwlarson 2.8 xd = tcos(phi) * zd;
832     yd = tsin(phi) * zd;
833 greg 1.1 zd = sqrt(1.0 - zd*zd);
834 greg 1.2 for (i = 0; i < 3; i++)
835     ar.rdir[i] = xd*h->ux[i] +
836     yd*h->uy[i] +
837     zd*h->uz[i];
838 greg 2.22 checknorm(ar.rdir);
839 greg 1.2 dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
840 greg 1.1 rayvalue(&ar);
841     ndims--;
842 greg 2.16 multcolor(ar.rcol, ar.rcoef); /* apply coefficient */
843 greg 1.1 addcolor(dp->v, ar.rcol);
844 greg 2.9 /* use rt to improve gradient calc */
845     if (ar.rt > FTINY && ar.rt < FHUGE)
846     dp->r += 1.0/ar.rt;
847 greg 1.1 /* (re)initialize error */
848     if (dp->n++) {
849     b2 = bright(dp->v)/dp->n - bright(ar.rcol);
850     b2 = b2*b2 + dp->k*((dp->n-1)*(dp->n-1));
851     dp->k = b2/(dp->n*dp->n);
852     } else
853     dp->k = 0.0;
854 greg 1.4 return(0);
855 greg 1.1 }
856    
857    
858 greg 2.14 static int
859     ambcmp( /* decreasing order */
860     const void *p1,
861     const void *p2
862     )
863     {
864     const AMBSAMP *d1 = (const AMBSAMP *)p1;
865     const AMBSAMP *d2 = (const AMBSAMP *)p2;
866    
867     if (d1->k < d2->k)
868     return(1);
869     if (d1->k > d2->k)
870     return(-1);
871     return(0);
872     }
873    
874    
875     static int
876     ambnorm( /* standard order */
877     const void *p1,
878     const void *p2
879     )
880     {
881     const AMBSAMP *d1 = (const AMBSAMP *)p1;
882     const AMBSAMP *d2 = (const AMBSAMP *)p2;
883 greg 2.23 int c;
884 greg 2.14
885     if ( (c = d1->t - d2->t) )
886     return(c);
887     return(d1->p - d2->p);
888     }
889    
890    
891 greg 1.1 double
892 greg 2.14 doambient( /* compute ambient component */
893 greg 2.23 COLOR rcol,
894 greg 2.14 RAY *r,
895     double wt,
896     FVECT pg,
897     FVECT dg
898     )
899 greg 1.1 {
900 greg 2.24 double b, d=0;
901 greg 1.1 AMBHEMI hemi;
902     AMBSAMP *div;
903     AMBSAMP dnew;
904 greg 2.23 double acol[3];
905     AMBSAMP *dp;
906 greg 1.1 double arad;
907 greg 2.19 int divcnt;
908 greg 2.23 int i, j;
909 greg 1.1 /* initialize hemisphere */
910 greg 2.23 inithemi(&hemi, rcol, r, wt);
911 greg 2.19 divcnt = hemi.nt * hemi.np;
912 greg 2.17 /* initialize */
913     if (pg != NULL)
914     pg[0] = pg[1] = pg[2] = 0.0;
915     if (dg != NULL)
916     dg[0] = dg[1] = dg[2] = 0.0;
917 greg 2.23 setcolor(rcol, 0.0, 0.0, 0.0);
918 greg 2.19 if (divcnt == 0)
919 greg 1.1 return(0.0);
920 greg 2.14 /* allocate super-samples */
921 greg 2.15 if (hemi.ns > 0 || pg != NULL || dg != NULL) {
922 greg 2.19 div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
923 greg 1.1 if (div == NULL)
924     error(SYSTEM, "out of memory in doambient");
925     } else
926     div = NULL;
927     /* sample the divisions */
928     arad = 0.0;
929 greg 2.23 acol[0] = acol[1] = acol[2] = 0.0;
930 greg 1.1 if ((dp = div) == NULL)
931     dp = &dnew;
932 greg 2.19 divcnt = 0;
933 greg 1.1 for (i = 0; i < hemi.nt; i++)
934     for (j = 0; j < hemi.np; j++) {
935     dp->t = i; dp->p = j;
936     setcolor(dp->v, 0.0, 0.0, 0.0);
937 greg 1.2 dp->r = 0.0;
938 greg 1.1 dp->n = 0;
939 greg 2.16 if (divsample(dp, &hemi, r) < 0) {
940 greg 2.19 if (div != NULL)
941     dp++;
942 greg 2.16 continue;
943     }
944 greg 2.6 arad += dp->r;
945 greg 2.19 divcnt++;
946 greg 1.1 if (div != NULL)
947     dp++;
948 greg 2.6 else
949 greg 1.1 addcolor(acol, dp->v);
950     }
951 greg 2.21 if (!divcnt) {
952     if (div != NULL)
953     free((void *)div);
954 greg 2.19 return(0.0); /* no samples taken */
955 greg 2.21 }
956 greg 2.19 if (divcnt < hemi.nt*hemi.np) {
957     pg = dg = NULL; /* incomplete sampling */
958     hemi.ns = 0;
959     } else if (arad > FTINY && divcnt/arad < minarad) {
960 greg 2.15 hemi.ns = 0; /* close enough */
961 greg 2.19 } else if (hemi.ns > 0) { /* else perform super-sampling? */
962 greg 1.4 comperrs(div, &hemi); /* compute errors */
963 greg 2.19 qsort(div, divcnt, sizeof(AMBSAMP), ambcmp); /* sort divs */
964 greg 1.1 /* super-sample */
965 greg 2.15 for (i = hemi.ns; i > 0; i--) {
966 schorsch 2.11 dnew = *div;
967 greg 2.16 if (divsample(&dnew, &hemi, r) < 0) {
968     dp++;
969     continue;
970     }
971     dp = div; /* reinsert */
972 greg 2.19 j = divcnt < i ? divcnt : i;
973 greg 1.1 while (--j > 0 && dnew.k < dp[1].k) {
974 schorsch 2.11 *dp = *(dp+1);
975 greg 1.1 dp++;
976     }
977 schorsch 2.11 *dp = dnew;
978 greg 1.1 }
979 greg 1.2 if (pg != NULL || dg != NULL) /* restore order */
980 greg 2.19 qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
981 greg 1.1 }
982     /* compute returned values */
983 greg 1.3 if (div != NULL) {
984 greg 2.19 arad = 0.0; /* note: divcnt may be < nt*np */
985     for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
986 greg 1.3 arad += dp->r;
987     if (dp->n > 1) {
988     b = 1.0/dp->n;
989     scalecolor(dp->v, b);
990     dp->r *= b;
991     dp->n = 1;
992     }
993     addcolor(acol, dp->v);
994     }
995 greg 1.5 b = bright(acol);
996 greg 1.6 if (b > FTINY) {
997 greg 2.17 b = 1.0/b; /* compute & normalize gradient(s) */
998 greg 1.6 if (pg != NULL) {
999     posgradient(pg, div, &hemi);
1000     for (i = 0; i < 3; i++)
1001     pg[i] *= b;
1002     }
1003     if (dg != NULL) {
1004     dirgradient(dg, div, &hemi);
1005     for (i = 0; i < 3; i++)
1006     dg[i] *= b;
1007     }
1008 greg 1.5 }
1009 greg 2.9 free((void *)div);
1010 greg 1.3 }
1011 greg 2.23 copycolor(rcol, acol);
1012 greg 1.1 if (arad <= FTINY)
1013 greg 1.16 arad = maxarad;
1014 greg 2.3 else
1015 greg 2.19 arad = (divcnt+hemi.ns)/arad;
1016 greg 1.15 if (pg != NULL) { /* reduce radius if gradient large */
1017     d = DOT(pg,pg);
1018     if (d*arad*arad > 1.0)
1019     arad = 1.0/sqrt(d);
1020     }
1021 greg 1.16 if (arad < minarad) {
1022 greg 1.1 arad = minarad;
1023 greg 1.16 if (pg != NULL && d*arad*arad > 1.0) { /* cap gradient */
1024     d = 1.0/arad/sqrt(d);
1025     for (i = 0; i < 3; i++)
1026     pg[i] *= d;
1027     }
1028     }
1029 greg 2.3 if ((arad /= sqrt(wt)) > maxarad)
1030     arad = maxarad;
1031     return(arad);
1032 greg 1.1 }
1033    
1034    
1035 greg 2.9 void
1036 greg 2.14 comperrs( /* compute initial error estimates */
1037     AMBSAMP *da, /* assumes standard ordering */
1038 greg 2.23 AMBHEMI *hp
1039 greg 2.14 )
1040 greg 1.1 {
1041     double b, b2;
1042     int i, j;
1043 greg 2.23 AMBSAMP *dp;
1044 greg 1.1 /* sum differences from neighbors */
1045     dp = da;
1046     for (i = 0; i < hp->nt; i++)
1047     for (j = 0; j < hp->np; j++) {
1048 greg 1.6 #ifdef DEBUG
1049     if (dp->t != i || dp->p != j)
1050     error(CONSISTENCY,
1051     "division order in comperrs");
1052     #endif
1053 greg 1.1 b = bright(dp[0].v);
1054     if (i > 0) { /* from above */
1055     b2 = bright(dp[-hp->np].v) - b;
1056     b2 *= b2 * 0.25;
1057     dp[0].k += b2;
1058     dp[-hp->np].k += b2;
1059     }
1060     if (j > 0) { /* from behind */
1061     b2 = bright(dp[-1].v) - b;
1062     b2 *= b2 * 0.25;
1063     dp[0].k += b2;
1064     dp[-1].k += b2;
1065 greg 1.4 } else { /* around */
1066     b2 = bright(dp[hp->np-1].v) - b;
1067 greg 1.1 b2 *= b2 * 0.25;
1068     dp[0].k += b2;
1069 greg 1.4 dp[hp->np-1].k += b2;
1070 greg 1.1 }
1071     dp++;
1072     }
1073     /* divide by number of neighbors */
1074     dp = da;
1075     for (j = 0; j < hp->np; j++) /* top row */
1076     (dp++)->k *= 1.0/3.0;
1077     if (hp->nt < 2)
1078     return;
1079     for (i = 1; i < hp->nt-1; i++) /* central region */
1080     for (j = 0; j < hp->np; j++)
1081     (dp++)->k *= 0.25;
1082     for (j = 0; j < hp->np; j++) /* bottom row */
1083     (dp++)->k *= 1.0/3.0;
1084     }
1085    
1086    
1087 greg 2.9 void
1088 greg 2.14 posgradient( /* compute position gradient */
1089     FVECT gv,
1090     AMBSAMP *da, /* assumes standard ordering */
1091 greg 2.23 AMBHEMI *hp
1092 greg 2.14 )
1093 greg 1.1 {
1094 greg 2.23 int i, j;
1095 greg 2.2 double nextsine, lastsine, b, d;
1096 greg 1.2 double mag0, mag1;
1097     double phi, cosp, sinp, xd, yd;
1098 greg 2.23 AMBSAMP *dp;
1099 greg 1.2
1100     xd = yd = 0.0;
1101     for (j = 0; j < hp->np; j++) {
1102     dp = da + j;
1103     mag0 = mag1 = 0.0;
1104 greg 2.2 lastsine = 0.0;
1105 greg 1.2 for (i = 0; i < hp->nt; i++) {
1106     #ifdef DEBUG
1107     if (dp->t != i || dp->p != j)
1108     error(CONSISTENCY,
1109     "division order in posgradient");
1110     #endif
1111     b = bright(dp->v);
1112     if (i > 0) {
1113     d = dp[-hp->np].r;
1114     if (dp[0].r > d) d = dp[0].r;
1115 greg 2.2 /* sin(t)*cos(t)^2 */
1116     d *= lastsine * (1.0 - (double)i/hp->nt);
1117 greg 1.2 mag0 += d*(b - bright(dp[-hp->np].v));
1118     }
1119 greg 2.2 nextsine = sqrt((double)(i+1)/hp->nt);
1120 greg 1.2 if (j > 0) {
1121     d = dp[-1].r;
1122     if (dp[0].r > d) d = dp[0].r;
1123 greg 2.2 mag1 += d * (nextsine - lastsine) *
1124     (b - bright(dp[-1].v));
1125 greg 1.2 } else {
1126     d = dp[hp->np-1].r;
1127     if (dp[0].r > d) d = dp[0].r;
1128 greg 2.2 mag1 += d * (nextsine - lastsine) *
1129     (b - bright(dp[hp->np-1].v));
1130 greg 1.2 }
1131     dp += hp->np;
1132 greg 2.2 lastsine = nextsine;
1133 greg 1.2 }
1134 greg 2.2 mag0 *= 2.0*PI / hp->np;
1135 greg 1.2 phi = 2.0*PI * (double)j/hp->np;
1136 gwlarson 2.8 cosp = tcos(phi); sinp = tsin(phi);
1137 greg 1.2 xd += mag0*cosp - mag1*sinp;
1138     yd += mag0*sinp + mag1*cosp;
1139     }
1140     for (i = 0; i < 3; i++)
1141 greg 2.16 gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
1142 greg 1.1 }
1143    
1144    
1145 greg 2.9 void
1146 greg 2.14 dirgradient( /* compute direction gradient */
1147     FVECT gv,
1148     AMBSAMP *da, /* assumes standard ordering */
1149 greg 2.23 AMBHEMI *hp
1150 greg 2.14 )
1151 greg 1.1 {
1152 greg 2.23 int i, j;
1153 greg 1.2 double mag;
1154     double phi, xd, yd;
1155 greg 2.23 AMBSAMP *dp;
1156 greg 1.2
1157     xd = yd = 0.0;
1158     for (j = 0; j < hp->np; j++) {
1159     dp = da + j;
1160     mag = 0.0;
1161     for (i = 0; i < hp->nt; i++) {
1162     #ifdef DEBUG
1163     if (dp->t != i || dp->p != j)
1164     error(CONSISTENCY,
1165     "division order in dirgradient");
1166     #endif
1167 greg 2.2 /* tan(t) */
1168     mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0);
1169 greg 1.2 dp += hp->np;
1170     }
1171     phi = 2.0*PI * (j+.5)/hp->np + PI/2.0;
1172 gwlarson 2.8 xd += mag * tcos(phi);
1173     yd += mag * tsin(phi);
1174 greg 1.2 }
1175     for (i = 0; i < 3; i++)
1176 greg 2.16 gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
1177 greg 1.1 }
1178 greg 2.25
1179     #endif /* ! NEWAMB */