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.13 by greg, Wed Apr 13 23:00:59 2005 UTC vs.
Revision 2.27 by greg, Sat Apr 19 02:39:44 2014 UTC

# Line 4 | Line 4 | static const char      RCSid[] = "$Id$";
4   /*
5   * Routines to compute "ambient" values using Monte Carlo
6   *
7 + *  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   *  Declarations of external symbols in ambient.h
12   */
13  
14   #include "copyright.h"
15  
16   #include  "ray.h"
13
17   #include  "ambient.h"
15
18   #include  "random.h"
19  
20 + #ifdef NEWAMB
21  
22 + extern void             SDsquare2disk(double ds[2], double seedx, double seedy);
23 +
24 + typedef struct {
25 +        RAY     *rp;            /* originating ray sample */
26 +        FVECT   ux, uy;         /* tangent axis unit vectors */
27 +        int     ns;             /* number of samples per axis */
28 +        COLOR   acoef;          /* division contribution coefficient */
29 +        struct s_ambsamp {
30 +                COLOR   v;              /* hemisphere sample value */
31 +                float   p[3];           /* intersection point */
32 +        } sa[1];                /* sample array (extends struct) */
33 + }  AMBHEMI;             /* ambient sample hemisphere */
34 +
35 + #define ambsamp(h,i,j)  (h)->sa[(i)*(h)->ns + (j)]
36 +
37 + typedef struct {
38 +        FVECT   r_i, r_i1, e_i;
39 +        double  nf, I1, I2, J2;
40 + } FFTRI;                /* vectors and coefficients for Hessian calculation */
41 +
42 +
43 + static AMBHEMI *
44 + inithemi(                       /* initialize sampling hemisphere */
45 +        COLOR   ac,
46 +        RAY     *r,
47 +        double  wt
48 + )
49 + {
50 +        AMBHEMI *hp;
51 +        double  d;
52 +        int     n, i;
53 +                                        /* set number of divisions */
54 +        if (ambacc <= FTINY &&
55 +                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
56 +                wt = d;                 /* avoid ray termination */
57 +        n = sqrt(ambdiv * wt) + 0.5;
58 +        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
59 +        if (n < i)
60 +                n = i;
61 +                                        /* allocate sampling array */
62 +        hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) +
63 +                                sizeof(struct s_ambsamp)*(n*n - 1));
64 +        if (hp == NULL)
65 +                return(NULL);
66 +        hp->rp = r;
67 +        hp->ns = n;
68 +                                        /* assign coefficient */
69 +        copycolor(hp->acoef, ac);
70 +        d = 1.0/(n*n);
71 +        scalecolor(hp->acoef, d);
72 +                                        /* make tangent axes */
73 +        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
74 +        for (i = 0; i < 3; i++)
75 +                if (r->ron[i] < 0.6 && r->ron[i] > -0.6)
76 +                        break;
77 +        if (i >= 3)
78 +                error(CONSISTENCY, "bad ray direction in inithemi()");
79 +        hp->uy[i] = 1.0;
80 +        VCROSS(hp->ux, hp->uy, r->ron);
81 +        normalize(hp->ux);
82 +        VCROSS(hp->uy, r->ron, hp->ux);
83 +                                        /* we're ready to sample */
84 +        return(hp);
85 + }
86 +
87 +
88   static int
89 < ambcmp(d1, d2)                          /* decreasing order */
90 < AMBSAMP  *d1, *d2;
89 > ambsample(                              /* sample an ambient direction */
90 >        AMBHEMI *hp,
91 >        int     i,
92 >        int     j
93 > )
94   {
95 <        if (d1->k < d2->k)
96 <                return(1);
97 <        if (d1->k > d2->k)
98 <                return(-1);
99 <        return(0);
95 >        struct s_ambsamp        *ap = &ambsamp(hp,i,j);
96 >        RAY                     ar;
97 >        int                     hlist[3];
98 >        double                  spt[2], zd;
99 >        int                     ii;
100 >                                        /* ambient coefficient for weight */
101 >        if (ambacc > FTINY)
102 >                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
103 >        else
104 >                copycolor(ar.rcoef, hp->acoef);
105 >        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) {
106 >                setcolor(ap->v, 0., 0., 0.);
107 >                VCOPY(ap->p, hp->rp->rop);
108 >                return(0);              /* no sample taken */
109 >        }
110 >        if (ambacc > FTINY) {
111 >                multcolor(ar.rcoef, hp->acoef);
112 >                scalecolor(ar.rcoef, 1./AVGREFL);
113 >        }
114 >                                        /* generate hemispherical sample */
115 >        SDsquare2disk(spt,      (i+.1+.8*frandom())/hp->ns,
116 >                                (j+.1+.8*frandom())/hp->ns);
117 >        zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
118 >        for (ii = 3; ii--; )
119 >                ar.rdir[ii] =   spt[0]*hp->ux[ii] +
120 >                                spt[1]*hp->uy[ii] +
121 >                                zd*hp->rp->ron[ii];
122 >        checknorm(ar.rdir);
123 >        dimlist[ndims++] = i*hp->ns + j + 90171;
124 >        rayvalue(&ar);                  /* evaluate ray */
125 >        ndims--;
126 >        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
127 >        copycolor(ap->v, ar.rcol);
128 >        if (ar.rt > 20.0*maxarad)       /* limit vertex distance */
129 >                ar.rt = 20.0*maxarad;
130 >        VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
131 >        return(1);
132   }
133  
134  
135 + /* Compute vectors and coefficients for Hessian/gradient calcs */
136 + static void
137 + comp_fftri(FFTRI *ftp, float ap0[3], float ap1[3], FVECT rop)
138 + {
139 +        FVECT   v1;
140 +        double  dot_e, dot_er, dot_r, dot_r1;
141 +
142 +        VSUB(ftp->r_i, ap0, rop);
143 +        VSUB(ftp->r_i1, ap1, rop);
144 +        VSUB(ftp->e_i, ap1, ap0);
145 +        VCROSS(v1, ftp->e_i, ftp->r_i);
146 +        ftp->nf = 1.0/DOT(v1,v1);
147 +        VCROSS(v1, ftp->r_i, ftp->r_i1);
148 +        ftp->I1 = sqrt(DOT(v1,v1)*ftp->nf);
149 +        dot_e = DOT(ftp->e_i,ftp->e_i);
150 +        dot_er = DOT(ftp->e_i, ftp->r_i);
151 +        dot_r = DOT(ftp->r_i,ftp->r_i);
152 +        dot_r1 = DOT(ftp->r_i1,ftp->r_i1);
153 +        ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)/dot_r1 - dot_er/dot_r +
154 +                        dot_e*ftp->I1 )*0.5*ftp->nf;
155 +        ftp->J2 =  0.25*ftp->nf*( 1.0/dot_r - 1.0/dot_r1 ) -
156 +                        dot_er/dot_e*ftp->I2;
157 + }
158 +
159 +
160 + /* Compose matrix from two vectors */
161 + static void
162 + compose_matrix(FVECT mat[3], FVECT va, FVECT vb)
163 + {
164 +        mat[0][0] = 2.0*va[0]*vb[0];
165 +        mat[1][1] = 2.0*va[1]*vb[1];
166 +        mat[2][2] = 2.0*va[2]*vb[2];
167 +        mat[0][1] = mat[1][0] = va[0]*vb[1] + va[1]*vb[0];
168 +        mat[0][2] = mat[2][0] = va[0]*vb[2] + va[2]*vb[0];
169 +        mat[1][2] = mat[2][1] = va[1]*vb[2] + va[2]*vb[1];
170 + }
171 +
172 +
173 + /* Compute partial 3x3 Hessian matrix for edge */
174 + static void
175 + comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
176 + {
177 +        FVECT   v1, v2;
178 +        FVECT   m1[3], m2[3], m3[3], m4[3];
179 +        double  d1, d2, d3, d4;
180 +        double  I3, J3, K3;
181 +        int     i, j;
182 +                                        /* compute intermediate coefficients */
183 +        d1 = 1.0/DOT(ftp->r_i,ftp->r_i);
184 +        d2 = 1.0/DOT(ftp->r_i1,ftp->r_i1);
185 +        d3 = 1.0/DOT(ftp->e_i,ftp->e_i);
186 +        d4 = DOT(ftp->e_i, ftp->r_i);
187 +        I3 = 0.25*ftp->nf*( DOT(ftp->e_i, ftp->r_i1)*d2*d2 - d4*d1*d1 +
188 +                                3.0*ftp->I2*d3 );
189 +        J3 = 0.25*d3*(d1*d1 - d2*d2) - d4*d3*I3;
190 +        K3 = d3*(ftp->I2 - I3/d1 - 2.0*d4*J3);
191 +                                        /* intermediate matrices */
192 +        VCROSS(v1, nrm, ftp->e_i);
193 +        for (j = 3; j--; )
194 +                v2[i] = ftp->I2*ftp->r_i[j] + ftp->J2*ftp->e_i[j];
195 +        compose_matrix(m1, v1, v2);
196 +        compose_matrix(m2, ftp->r_i, ftp->r_i);
197 +        compose_matrix(m3, ftp->e_i, ftp->e_i);
198 +        compose_matrix(m4, ftp->r_i, ftp->e_i);
199 +        VCROSS(v1, ftp->r_i, ftp->e_i);
200 +        d1 = DOT(nrm, v1);
201 +        d2 = -d1*ftp->I2;
202 +        d1 *= 2.0;
203 +        for (i = 3; i--; )              /* final matrix sum */
204 +            for (j = 3; j--; ) {
205 +                hess[i][j] = m1[i][j] + d1*( I3*m2[i][j] + K3*m3[i][j] +
206 +                                                2.0*J3*m4[i][j] );
207 +                hess[i][j] += d2*(i==j);
208 +                hess[i][j] *= -1.0/PI;
209 +            }
210 + }
211 +
212 +
213 + /* Reverse hessian calculation result for edge in other direction */
214 + static void
215 + rev_hessian(FVECT hess[3])
216 + {
217 +        int     i;
218 +
219 +        for (i = 3; i--; ) {
220 +                hess[i][0] = -hess[i][0];
221 +                hess[i][1] = -hess[i][1];
222 +                hess[i][2] = -hess[i][2];
223 +        }
224 + }
225 +
226 +
227 + /* Add to radiometric Hessian from the given triangle */
228 + static void
229 + add2hessian(FVECT hess[3], FVECT ehess1[3],
230 +                FVECT ehess2[3], FVECT ehess3[3], COLORV v)
231 + {
232 +        int     i, j;
233 +
234 +        for (i = 3; i--; )
235 +            for (j = 3; j--; )
236 +                hess[i][j] += v*( ehess1[i][j] + ehess2[i][j] + ehess3[i][j] );
237 + }
238 +
239 +
240 + /* Compute partial displacement form factor gradient for edge */
241 + static void
242 + comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm)
243 + {
244 +        FVECT   vcp;
245 +        double  f1;
246 +        int     i;
247 +
248 +        VCROSS(vcp, ftp->r_i, ftp->r_i1);
249 +        f1 = 2.0*DOT(nrm, vcp);
250 +        VCROSS(vcp, nrm, ftp->e_i);
251 +        for (i = 3; i--; )
252 +                grad[i] = (0.5/PI)*( ftp->I1*vcp[i] +
253 +                            f1*(ftp->I2*ftp->r_i[i] + ftp->J2*ftp->e_i[i]) );
254 + }
255 +
256 +
257 + /* Reverse gradient calculation result for edge in other direction */
258 + static void
259 + rev_gradient(FVECT grad)
260 + {
261 +        grad[0] = -grad[0];
262 +        grad[1] = -grad[1];
263 +        grad[2] = -grad[2];
264 + }
265 +
266 +
267 + /* Add to displacement gradient from the given triangle */
268 + static void
269 + add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, COLORV v)
270 + {
271 +        int     i;
272 +
273 +        for (i = 3; i--; )
274 +                grad[i] += v*( egrad1[i] + egrad2[i] + egrad3[i] );
275 + }
276 +
277 +
278 + /* Return brightness of furthest ambient sample */
279 + static COLORV
280 + back_ambval(struct s_ambsamp *ap1, struct s_ambsamp *ap2,
281 +                struct s_ambsamp *ap3, FVECT orig)
282 + {
283 +        COLORV  vback;
284 +        FVECT   vec;
285 +        double  d2, d2best;
286 +
287 +        VSUB(vec, ap1->p, orig);
288 +        d2best = DOT(vec,vec);
289 +        vback = ap1->v[CIEY];
290 +        VSUB(vec, ap2->p, orig);
291 +        d2 = DOT(vec,vec);
292 +        if (d2 > d2best) {
293 +                d2best = d2;
294 +                vback = ap2->v[CIEY];
295 +        }
296 +        VSUB(vec, ap3->p, orig);
297 +        d2 = DOT(vec,vec);
298 +        if (d2 > d2best)
299 +                return(ap3->v[CIEY]);
300 +        return(vback);
301 + }
302 +
303 +
304 + /* Compute anisotropic radii and eigenvector directions */
305   static int
306 < ambnorm(d1, d2)                         /* standard order */
33 < AMBSAMP  *d1, *d2;
306 > eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3])
307   {
308 <        register int  c;
308 >        double  hess2[2][2];
309 >        FVECT   a, b;
310 >        double  evalue[2], slope1, xmag1;
311 >        int     i;
312 >                                        /* project Hessian to sample plane */
313 >        for (i = 3; i--; ) {
314 >                a[i] = DOT(hessian[i], uv[0]);
315 >                b[i] = DOT(hessian[i], uv[1]);
316 >        }
317 >        hess2[0][0] = DOT(uv[0], a);
318 >        hess2[0][1] = DOT(uv[0], b);
319 >        hess2[1][0] = DOT(uv[1], a);
320 >        hess2[1][1] = DOT(uv[1], b);
321 >                                        /* compute eigenvalues */
322 >        if (quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1],
323 >                        hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]) != 2 ||
324 >                        (evalue[0] = fabs(evalue[0])) <= FTINY*FTINY*FTINY ||
325 >                        (evalue[1] = fabs(evalue[1])) <= FTINY*FTINY*FTINY)
326 >                error(INTERNAL, "bad eigenvalue calculation");
327  
328 <        if ( (c = d1->t - d2->t) )
329 <                return(c);
330 <        return(d1->p - d2->p);
328 >        if (evalue[0] > evalue[1]) {
329 >                ra[0] = 1.0/sqrt(sqrt(evalue[0]));
330 >                ra[1] = 1.0/sqrt(sqrt(evalue[1]));
331 >                slope1 = evalue[1];
332 >        } else {
333 >                ra[0] = 1.0/sqrt(sqrt(evalue[1]));
334 >                ra[1] = 1.0/sqrt(sqrt(evalue[0]));
335 >                slope1 = evalue[0];
336 >        }
337 >                                        /* compute unit eigenvectors */
338 >        if (fabs(hess2[0][1]) <= FTINY)
339 >                return;                 /* uv OK as is */
340 >        slope1 = (slope1 - hess2[0][0]) / hess2[0][1];
341 >        xmag1 = sqrt(1.0/(1.0 + slope1*slope1));
342 >        for (i = 3; i--; ) {
343 >                b[i] = xmag1*uv[0][i] + slope1*xmag1*uv[1][i];
344 >                a[i] = slope1*xmag1*uv[0][i] - xmag1*uv[1][i];
345 >        }
346 >        VCOPY(uv[0], a);
347 >        VCOPY(uv[1], b);
348   }
349  
350  
351 + static void
352 + ambHessian(                             /* anisotropic radii & pos. gradient */
353 +        AMBHEMI *hp,
354 +        FVECT   uv[2],                  /* returned */
355 +        float   ra[2],                  /* returned */
356 +        float   pg[2]                   /* returned */
357 + )
358 + {
359 +        static char     memerrmsg[] = "out of memory in ambHessian()";
360 +        FVECT           (*hessrow)[3] = NULL;
361 +        FVECT           *gradrow = NULL;
362 +        FVECT           hessian[3];
363 +        FVECT           gradient;
364 +        FFTRI           fftr;
365 +        int             i, j;
366 +                                        /* be sure to assign unit vectors */
367 +        VCOPY(uv[0], hp->ux);
368 +        VCOPY(uv[1], hp->uy);
369 +                        /* clock-wise vertex traversal from sample POV */
370 +        if (ra != NULL) {               /* initialize Hessian row buffer */
371 +                hessrow = (FVECT (*)[3])malloc(sizeof(FVECT)*3*hp->ns);
372 +                if (hessrow == NULL)
373 +                        error(SYSTEM, memerrmsg);
374 +                memset(hessian, 0, sizeof(hessian));
375 +        } else if (pg == NULL)          /* bogus call? */
376 +                return;
377 +        if (pg != NULL) {               /* initialize form factor row buffer */
378 +                gradrow = (FVECT *)malloc(sizeof(FVECT)*hp->ns);
379 +                if (gradrow == NULL)
380 +                        error(SYSTEM, memerrmsg);
381 +                memset(gradient, 0, sizeof(gradient));
382 +        }
383 +                                        /* compute first row of edges */
384 +        for (j = 0; j < hp->ns-1; j++) {
385 +                comp_fftri(&fftr, ambsamp(hp,0,j).p,
386 +                                ambsamp(hp,0,j+1).p, hp->rp->rop);
387 +                if (hessrow != NULL)
388 +                        comp_hessian(hessrow[j], &fftr, hp->rp->ron);
389 +                if (gradrow != NULL)
390 +                        comp_gradient(gradrow[j], &fftr, hp->rp->ron);
391 +        }
392 +                                        /* sum each row of triangles */
393 +        for (i = 0; i < hp->ns-1; i++) {
394 +            FVECT       hesscol[3];     /* compute first vertical edge */
395 +            FVECT       gradcol;
396 +            comp_fftri(&fftr, ambsamp(hp,i,0).p,
397 +                        ambsamp(hp,i+1,0).p, hp->rp->rop);
398 +            if (hessrow != NULL)
399 +                comp_hessian(hesscol, &fftr, hp->rp->ron);
400 +            if (gradrow != NULL)
401 +                comp_gradient(gradcol, &fftr, hp->rp->ron);
402 +            for (j = 0; j < hp->ns-1; j++) {
403 +                FVECT   hessdia[3];     /* compute triangle contributions */
404 +                FVECT   graddia;
405 +                COLORV  backg;
406 +                backg = back_ambval(&ambsamp(hp,i,j), &ambsamp(hp,i,j+1),
407 +                                        &ambsamp(hp,i+1,j), hp->rp->rop);
408 +                                        /* diagonal (inner) edge */
409 +                comp_fftri(&fftr, ambsamp(hp,i,j+1).p,
410 +                                ambsamp(hp,i+1,j).p, hp->rp->rop);
411 +                if (hessrow != NULL) {
412 +                    comp_hessian(hessdia, &fftr, hp->rp->ron);
413 +                    rev_hessian(hesscol);
414 +                    add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
415 +                }
416 +                if (gradient != NULL) {
417 +                    comp_gradient(graddia, &fftr, hp->rp->ron);
418 +                    rev_gradient(gradcol);
419 +                    add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
420 +                }
421 +                                        /* initialize edge in next row */
422 +                comp_fftri(&fftr, ambsamp(hp,i+1,j+1).p,
423 +                                ambsamp(hp,i+1,j).p, hp->rp->rop);
424 +                if (hessrow != NULL)
425 +                    comp_hessian(hessrow[j], &fftr, hp->rp->ron);
426 +                if (gradrow != NULL)
427 +                    comp_gradient(gradrow[j], &fftr, hp->rp->ron);
428 +                                        /* new column edge & paired triangle */
429 +                backg = back_ambval(&ambsamp(hp,i,j+1), &ambsamp(hp,i+1,j+1),
430 +                                        &ambsamp(hp,i+1,j), hp->rp->rop);
431 +                comp_fftri(&fftr, ambsamp(hp,i,j+1).p, ambsamp(hp,i+1,j+1).p,
432 +                                hp->rp->rop);
433 +                if (hessrow != NULL) {
434 +                    comp_hessian(hesscol, &fftr, hp->rp->ron);
435 +                    rev_hessian(hessdia);
436 +                    add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
437 +                    if (i < hp->ns-2)
438 +                        rev_hessian(hessrow[j]);
439 +                }
440 +                if (gradrow != NULL) {
441 +                    comp_gradient(gradcol, &fftr, hp->rp->ron);
442 +                    rev_gradient(graddia);
443 +                    add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
444 +                    if (i < hp->ns-2)
445 +                        rev_gradient(gradrow[j]);
446 +                }
447 +            }
448 +        }
449 +                                        /* release row buffers */
450 +        if (hessrow != NULL) free(hessrow);
451 +        if (gradrow != NULL) free(gradrow);
452 +        
453 +        if (ra != NULL)                 /* extract eigenvectors & radii */
454 +                eigenvectors(uv, ra, hessian);
455 +        if (pg != NULL) {               /* project position gradient */
456 +                pg[0] = DOT(gradient, uv[0]);
457 +                pg[1] = DOT(gradient, uv[1]);
458 +        }
459 + }
460 +
461 +
462 + /* Compute direction gradient from a hemispherical sampling */
463 + static void
464 + ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
465 + {
466 +        struct s_ambsamp        *ap;
467 +        int                     n;
468 +
469 +        dg[0] = dg[1] = 0;
470 +        for (ap = hp->sa, n = hp->ns*hp->ns; n--; ap++) {
471 +                FVECT   vd;
472 +                double  gfact;
473 +                                        /* use vector for azimuth + 90deg */
474 +                VSUB(vd, ap->p, hp->rp->rop);
475 +                                        /* brightness with tangent factor */
476 +                gfact = ap->v[CIEY] / DOT(hp->rp->ron, vd);
477 +                                        /* sine = proj_radius/vd_length */
478 +                dg[0] -= DOT(uv[1], vd) * gfact ;
479 +                dg[1] += DOT(uv[0], vd) * gfact;
480 +        }
481 + }
482 +
483 +
484   int
485 < divsample(dp, h, r)                     /* sample a division */
486 < register AMBSAMP  *dp;
487 < AMBHEMI  *h;
488 < RAY  *r;
485 > doambient(                              /* compute ambient component */
486 >        COLOR   rcol,                   /* input/output color */
487 >        RAY     *r,
488 >        double  wt,
489 >        FVECT   uv[2],                  /* returned (optional) */
490 >        float   ra[2],                  /* returned (optional) */
491 >        float   pg[2],                  /* returned (optional) */
492 >        float   dg[2]                   /* returned (optional) */
493 > )
494   {
495 +        int                     cnt = 0;
496 +        FVECT                   my_uv[2];
497 +        AMBHEMI                 *hp;
498 +        double                  d, acol[3];
499 +        struct s_ambsamp        *ap;
500 +        int                     i, j;
501 +                                        /* initialize */
502 +        if ((hp = inithemi(rcol, r, wt)) == NULL)
503 +                return(0);
504 +        if (uv != NULL)
505 +                memset(uv, 0, sizeof(FVECT)*2);
506 +        if (ra != NULL)
507 +                ra[0] = ra[1] = 0.0;
508 +        if (pg != NULL)
509 +                pg[0] = pg[1] = 0.0;
510 +        if (dg != NULL)
511 +                dg[0] = dg[1] = 0.0;
512 +                                        /* sample the hemisphere */
513 +        acol[0] = acol[1] = acol[2] = 0.0;
514 +        for (i = hp->ns; i--; )
515 +                for (j = hp->ns; j--; )
516 +                        if (ambsample(hp, i, j)) {
517 +                                ap = &ambsamp(hp,i,j);
518 +                                addcolor(acol, ap->v);
519 +                                ++cnt;
520 +                        }
521 +        if (!cnt) {
522 +                setcolor(rcol, 0.0, 0.0, 0.0);
523 +                free(hp);
524 +                return(0);              /* no valid samples */
525 +        }
526 +        d = 1.0 / cnt;                  /* final indirect irradiance/PI */
527 +        acol[0] *= d; acol[1] *= d; acol[2] *= d;
528 +        copycolor(rcol, acol);
529 +        if (cnt < hp->ns*hp->ns ||      /* incomplete sampling? */
530 +                        (ra == NULL) & (pg == NULL) & (dg == NULL)) {
531 +                free(hp);
532 +                return(-1);             /* no radius or gradient calc. */
533 +        }
534 +        d = 0.01 * bright(rcol);        /* add in 1% before Hessian comp. */
535 +        if (d < FTINY) d = FTINY;
536 +        ap = hp->sa;                    /* using Y channel from here on... */
537 +        for (i = hp->ns*hp->ns; i--; ap++)
538 +                colval(ap->v,CIEY) = bright(ap->v) + d;
539 +
540 +        if (uv == NULL)                 /* make sure we have axis pointers */
541 +                uv = my_uv;
542 +                                        /* compute radii & pos. gradient */
543 +        ambHessian(hp, uv, ra, pg);
544 +        if (dg != NULL)                 /* compute direction gradient */
545 +                ambdirgrad(hp, uv, dg);
546 +        if (ra != NULL) {               /* adjust/clamp radii */
547 +                d = sqrt(sqrt((4.0/PI)*bright(rcol)/wt));
548 +                if ((ra[0] *= d) > maxarad)
549 +                        ra[0] = maxarad;
550 +                if ((ra[1] *= d) > 2.0*ra[0])
551 +                        ra[1] = 2.0*ra[0];
552 +        }
553 +        free(hp);                       /* clean up and return */
554 +        return(1);
555 + }
556 +
557 +
558 + #else /* ! NEWAMB */
559 +
560 +
561 + void
562 + inithemi(                       /* initialize sampling hemisphere */
563 +        AMBHEMI  *hp,
564 +        COLOR ac,
565 +        RAY  *r,
566 +        double  wt
567 + )
568 + {
569 +        double  d;
570 +        int  i;
571 +                                        /* set number of divisions */
572 +        if (ambacc <= FTINY &&
573 +                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
574 +                wt = d;                 /* avoid ray termination */
575 +        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
576 +        i = ambacc > FTINY ? 3 : 1;     /* minimum number of samples */
577 +        if (hp->nt < i)
578 +                hp->nt = i;
579 +        hp->np = PI * hp->nt + 0.5;
580 +                                        /* set number of super-samples */
581 +        hp->ns = ambssamp * wt + 0.5;
582 +                                        /* assign coefficient */
583 +        copycolor(hp->acoef, ac);
584 +        d = 1.0/(hp->nt*hp->np);
585 +        scalecolor(hp->acoef, d);
586 +                                        /* make axes */
587 +        VCOPY(hp->uz, r->ron);
588 +        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
589 +        for (i = 0; i < 3; i++)
590 +                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
591 +                        break;
592 +        if (i >= 3)
593 +                error(CONSISTENCY, "bad ray direction in inithemi");
594 +        hp->uy[i] = 1.0;
595 +        fcross(hp->ux, hp->uy, hp->uz);
596 +        normalize(hp->ux);
597 +        fcross(hp->uy, hp->uz, hp->ux);
598 + }
599 +
600 +
601 + int
602 + divsample(                              /* sample a division */
603 +        AMBSAMP  *dp,
604 +        AMBHEMI  *h,
605 +        RAY  *r
606 + )
607 + {
608          RAY  ar;
609          int  hlist[3];
610          double  spt[2];
611          double  xd, yd, zd;
612          double  b2;
613          double  phi;
614 <        register int  i;
615 <
616 <        if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0)
614 >        int  i;
615 >                                        /* ambient coefficient for weight */
616 >        if (ambacc > FTINY)
617 >                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
618 >        else
619 >                copycolor(ar.rcoef, h->acoef);
620 >        if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0)
621                  return(-1);
622 +        if (ambacc > FTINY) {
623 +                multcolor(ar.rcoef, h->acoef);
624 +                scalecolor(ar.rcoef, 1./AVGREFL);
625 +        }
626          hlist[0] = r->rno;
627          hlist[1] = dp->t;
628          hlist[2] = dp->p;
# Line 69 | Line 636 | RAY  *r;
636                  ar.rdir[i] =    xd*h->ux[i] +
637                                  yd*h->uy[i] +
638                                  zd*h->uz[i];
639 +        checknorm(ar.rdir);
640          dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
641          rayvalue(&ar);
642          ndims--;
643 +        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
644          addcolor(dp->v, ar.rcol);
645                                          /* use rt to improve gradient calc */
646          if (ar.rt > FTINY && ar.rt < FHUGE)
# Line 87 | Line 656 | RAY  *r;
656   }
657  
658  
659 + static int
660 + ambcmp(                                 /* decreasing order */
661 +        const void *p1,
662 +        const void *p2
663 + )
664 + {
665 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
666 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
667 +
668 +        if (d1->k < d2->k)
669 +                return(1);
670 +        if (d1->k > d2->k)
671 +                return(-1);
672 +        return(0);
673 + }
674 +
675 +
676 + static int
677 + ambnorm(                                /* standard order */
678 +        const void *p1,
679 +        const void *p2
680 + )
681 + {
682 +        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
683 +        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
684 +        int     c;
685 +
686 +        if ( (c = d1->t - d2->t) )
687 +                return(c);
688 +        return(d1->p - d2->p);
689 + }
690 +
691 +
692   double
693 < doambient(acol, r, wt, pg, dg)          /* compute ambient component */
694 < COLOR  acol;
695 < RAY  *r;
696 < double  wt;
697 < FVECT  pg, dg;
693 > doambient(                              /* compute ambient component */
694 >        COLOR  rcol,
695 >        RAY  *r,
696 >        double  wt,
697 >        FVECT  pg,
698 >        FVECT  dg
699 > )
700   {
701 <        double  b, d;
701 >        double  b, d=0;
702          AMBHEMI  hemi;
703          AMBSAMP  *div;
704          AMBSAMP  dnew;
705 <        register AMBSAMP  *dp;
705 >        double  acol[3];
706 >        AMBSAMP  *dp;
707          double  arad;
708 <        int  ndivs, ns;
709 <        register int  i, j;
105 <                                        /* initialize color */
106 <        setcolor(acol, 0.0, 0.0, 0.0);
708 >        int  divcnt;
709 >        int  i, j;
710                                          /* initialize hemisphere */
711 <        inithemi(&hemi, r, wt);
712 <        ndivs = hemi.nt * hemi.np;
713 <        if (ndivs == 0)
711 >        inithemi(&hemi, rcol, r, wt);
712 >        divcnt = hemi.nt * hemi.np;
713 >                                        /* initialize */
714 >        if (pg != NULL)
715 >                pg[0] = pg[1] = pg[2] = 0.0;
716 >        if (dg != NULL)
717 >                dg[0] = dg[1] = dg[2] = 0.0;
718 >        setcolor(rcol, 0.0, 0.0, 0.0);
719 >        if (divcnt == 0)
720                  return(0.0);
721 <                                        /* set number of super-samples */
722 <        ns = ambssamp * wt + 0.5;
723 <        if (ns > 0 || pg != NULL || dg != NULL) {
115 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
721 >                                        /* allocate super-samples */
722 >        if (hemi.ns > 0 || pg != NULL || dg != NULL) {
723 >                div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
724                  if (div == NULL)
725                          error(SYSTEM, "out of memory in doambient");
726          } else
727                  div = NULL;
728                                          /* sample the divisions */
729          arad = 0.0;
730 +        acol[0] = acol[1] = acol[2] = 0.0;
731          if ((dp = div) == NULL)
732                  dp = &dnew;
733 +        divcnt = 0;
734          for (i = 0; i < hemi.nt; i++)
735                  for (j = 0; j < hemi.np; j++) {
736                          dp->t = i; dp->p = j;
737                          setcolor(dp->v, 0.0, 0.0, 0.0);
738                          dp->r = 0.0;
739                          dp->n = 0;
740 <                        if (divsample(dp, &hemi, r) < 0)
741 <                                goto oopsy;
740 >                        if (divsample(dp, &hemi, r) < 0) {
741 >                                if (div != NULL)
742 >                                        dp++;
743 >                                continue;
744 >                        }
745                          arad += dp->r;
746 +                        divcnt++;
747                          if (div != NULL)
748                                  dp++;
749                          else
750                                  addcolor(acol, dp->v);
751                  }
752 <        if (ns > 0 && arad > FTINY && ndivs/arad < minarad)
753 <                ns = 0;                 /* close enough */
754 <        else if (ns > 0) {              /* else perform super-sampling */
752 >        if (!divcnt) {
753 >                if (div != NULL)
754 >                        free((void *)div);
755 >                return(0.0);            /* no samples taken */
756 >        }
757 >        if (divcnt < hemi.nt*hemi.np) {
758 >                pg = dg = NULL;         /* incomplete sampling */
759 >                hemi.ns = 0;
760 >        } else if (arad > FTINY && divcnt/arad < minarad) {
761 >                hemi.ns = 0;            /* close enough */
762 >        } else if (hemi.ns > 0) {       /* else perform super-sampling? */
763                  comperrs(div, &hemi);                   /* compute errors */
764 <                qsort(div, ndivs, sizeof(AMBSAMP), ambcmp);     /* sort divs */
764 >                qsort(div, divcnt, sizeof(AMBSAMP), ambcmp);    /* sort divs */
765                                                  /* super-sample */
766 <                for (i = ns; i > 0; i--) {
766 >                for (i = hemi.ns; i > 0; i--) {
767                          dnew = *div;
768 <                        if (divsample(&dnew, &hemi, r) < 0)
769 <                                goto oopsy;
770 <                                                        /* reinsert */
771 <                        dp = div;
772 <                        j = ndivs < i ? ndivs : i;
768 >                        if (divsample(&dnew, &hemi, r) < 0) {
769 >                                dp++;
770 >                                continue;
771 >                        }
772 >                        dp = div;               /* reinsert */
773 >                        j = divcnt < i ? divcnt : i;
774                          while (--j > 0 && dnew.k < dp[1].k) {
775                                  *dp = *(dp+1);
776                                  dp++;
# Line 155 | Line 778 | FVECT  pg, dg;
778                          *dp = dnew;
779                  }
780                  if (pg != NULL || dg != NULL)   /* restore order */
781 <                        qsort(div, ndivs, sizeof(AMBSAMP), ambnorm);
781 >                        qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
782          }
783                                          /* compute returned values */
784          if (div != NULL) {
785 <                arad = 0.0;
786 <                for (i = ndivs, dp = div; i-- > 0; dp++) {
785 >                arad = 0.0;             /* note: divcnt may be < nt*np */
786 >                for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
787                          arad += dp->r;
788                          if (dp->n > 1) {
789                                  b = 1.0/dp->n;
# Line 172 | Line 795 | FVECT  pg, dg;
795                  }
796                  b = bright(acol);
797                  if (b > FTINY) {
798 <                        b = ndivs/b;
798 >                        b = 1.0/b;      /* compute & normalize gradient(s) */
799                          if (pg != NULL) {
800                                  posgradient(pg, div, &hemi);
801                                  for (i = 0; i < 3; i++)
# Line 183 | Line 806 | FVECT  pg, dg;
806                                  for (i = 0; i < 3; i++)
807                                          dg[i] *= b;
808                          }
186                } else {
187                        if (pg != NULL)
188                                for (i = 0; i < 3; i++)
189                                        pg[i] = 0.0;
190                        if (dg != NULL)
191                                for (i = 0; i < 3; i++)
192                                        dg[i] = 0.0;
809                  }
810                  free((void *)div);
811          }
812 <        b = 1.0/ndivs;
197 <        scalecolor(acol, b);
812 >        copycolor(rcol, acol);
813          if (arad <= FTINY)
814                  arad = maxarad;
815          else
816 <                arad = (ndivs+ns)/arad;
816 >                arad = (divcnt+hemi.ns)/arad;
817          if (pg != NULL) {               /* reduce radius if gradient large */
818                  d = DOT(pg,pg);
819                  if (d*arad*arad > 1.0)
# Line 215 | Line 830 | FVECT  pg, dg;
830          if ((arad /= sqrt(wt)) > maxarad)
831                  arad = maxarad;
832          return(arad);
218 oopsy:
219        if (div != NULL)
220                free((void *)div);
221        return(0.0);
833   }
834  
835  
836   void
837 < inithemi(hp, r, wt)             /* initialize sampling hemisphere */
838 < register AMBHEMI  *hp;
839 < RAY  *r;
840 < double  wt;
837 > comperrs(                       /* compute initial error estimates */
838 >        AMBSAMP  *da,   /* assumes standard ordering */
839 >        AMBHEMI  *hp
840 > )
841   {
231        register int  i;
232                                        /* set number of divisions */
233        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
234        i = ambacc > FTINY ? 3 : 1;     /* minimum number of samples */
235        if (hp->nt < i)
236                hp->nt = i;
237        hp->np = PI * hp->nt + 0.5;
238                                        /* make axes */
239        VCOPY(hp->uz, r->ron);
240        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
241        for (i = 0; i < 3; i++)
242                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
243                        break;
244        if (i >= 3)
245                error(CONSISTENCY, "bad ray direction in inithemi");
246        hp->uy[i] = 1.0;
247        fcross(hp->ux, hp->uy, hp->uz);
248        normalize(hp->ux);
249        fcross(hp->uy, hp->uz, hp->ux);
250 }
251
252
253 void
254 comperrs(da, hp)                /* compute initial error estimates */
255 AMBSAMP  *da;           /* assumes standard ordering */
256 register AMBHEMI  *hp;
257 {
842          double  b, b2;
843          int  i, j;
844 <        register AMBSAMP  *dp;
844 >        AMBSAMP  *dp;
845                                  /* sum differences from neighbors */
846          dp = da;
847          for (i = 0; i < hp->nt; i++)
# Line 302 | Line 886 | register AMBHEMI  *hp;
886  
887  
888   void
889 < posgradient(gv, da, hp)                         /* compute position gradient */
890 < FVECT  gv;
891 < AMBSAMP  *da;                   /* assumes standard ordering */
892 < register AMBHEMI  *hp;
889 > posgradient(                                    /* compute position gradient */
890 >        FVECT  gv,
891 >        AMBSAMP  *da,                   /* assumes standard ordering */
892 >        AMBHEMI  *hp
893 > )
894   {
895 <        register int  i, j;
895 >        int  i, j;
896          double  nextsine, lastsine, b, d;
897          double  mag0, mag1;
898          double  phi, cosp, sinp, xd, yd;
899 <        register AMBSAMP  *dp;
899 >        AMBSAMP  *dp;
900  
901          xd = yd = 0.0;
902          for (j = 0; j < hp->np; j++) {
# Line 354 | Line 939 | register AMBHEMI  *hp;
939                  yd += mag0*sinp + mag1*cosp;
940          }
941          for (i = 0; i < 3; i++)
942 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI;
942 >                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
943   }
944  
945  
946   void
947 < dirgradient(gv, da, hp)                         /* compute direction gradient */
948 < FVECT  gv;
949 < AMBSAMP  *da;                   /* assumes standard ordering */
950 < register AMBHEMI  *hp;
947 > dirgradient(                                    /* compute direction gradient */
948 >        FVECT  gv,
949 >        AMBSAMP  *da,                   /* assumes standard ordering */
950 >        AMBHEMI  *hp
951 > )
952   {
953 <        register int  i, j;
953 >        int  i, j;
954          double  mag;
955          double  phi, xd, yd;
956 <        register AMBSAMP  *dp;
956 >        AMBSAMP  *dp;
957  
958          xd = yd = 0.0;
959          for (j = 0; j < hp->np; j++) {
# Line 388 | Line 974 | register AMBHEMI  *hp;
974                  yd += mag * tsin(phi);
975          }
976          for (i = 0; i < 3; i++)
977 <                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);
977 >                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
978   }
979 +
980 + #endif  /* ! NEWAMB */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines