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.26 by greg, Wed Apr 16 20:32:00 2014 UTC vs.
Revision 2.34 by greg, Thu Apr 24 23:15:10 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  
# Line 19 | Line 23 | extern void            SDsquare2disk(double ds[2], double seedx,
23  
24   typedef struct {
25          RAY     *rp;            /* originating ray sample */
26 <        FVECT   ux, uy;         /* tangent axis directions */
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 */
31 >                FVECT   p;              /* 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, rI2_eJ2;
39 +        double  nf, I1, I2;
40 + } FFTRI;                /* vectors and coefficients for Hessian calculation */
41  
42 +
43   static AMBHEMI *
44   inithemi(                       /* initialize sampling hemisphere */
45          COLOR   ac,
# Line 46 | Line 55 | inithemi(                      /* initialize sampling hemisphere */
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 + 4*(ambacc > FTINY);     /* minimum number of samples */
58 >        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
59          if (n < i)
60                  n = i;
61                                          /* allocate sampling array */
# Line 60 | Line 69 | inithemi(                      /* initialize sampling hemisphere */
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;
72 >                                        /* make tangent plane axes */
73 >        hp->uy[0] = 0.1 - 0.2*frandom();
74 >        hp->uy[1] = 0.1 - 0.2*frandom();
75 >        hp->uy[2] = 0.1 - 0.2*frandom();
76          for (i = 0; i < 3; i++)
77 <                if (r->rn[i] < 0.6 && r->rn[i] > -0.6)
77 >                if (r->ron[i] < 0.6 && r->ron[i] > -0.6)
78                          break;
79          if (i >= 3)
80                  error(CONSISTENCY, "bad ray direction in inithemi()");
81          hp->uy[i] = 1.0;
82 <        VCROSS(hp->ux, hp->uy, r->rn);
82 >        VCROSS(hp->ux, hp->uy, r->ron);
83          normalize(hp->ux);
84 <        VCROSS(hp->uy, r->rn, hp->ux);
84 >        VCROSS(hp->uy, r->ron, hp->ux);
85                                          /* we're ready to sample */
86          return(hp);
87   }
88  
89  
90 < static int
90 > static struct s_ambsamp *
91   ambsample(                              /* sample an ambient direction */
92          AMBHEMI *hp,
93          int     i,
94 <        int     j,
94 >        int     j
95   )
96   {
97          struct s_ambsamp        *ap = &ambsamp(hp,i,j);
98          RAY                     ar;
99 <        int                     hlist[3];
89 <        double                  spt[2], dz;
99 >        double                  spt[2], zd;
100          int                     ii;
101                                          /* ambient coefficient for weight */
102          if (ambacc > FTINY)
103                  setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
104          else
105                  copycolor(ar.rcoef, hp->acoef);
106 <        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) {
107 <                setcolor(ap->v, 0., 0., 0.);
98 <                ap->r = 0.;
99 <                return(0);              /* no sample taken */
100 <        }
106 >        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0)
107 >                goto badsample;
108          if (ambacc > FTINY) {
109                  multcolor(ar.rcoef, hp->acoef);
110                  scalecolor(ar.rcoef, 1./AVGREFL);
111          }
112                                          /* generate hemispherical sample */
113 <        SDsquare2disk(spt, (i+frandom())/hp->ns, (j+frandom())/hp->ns);
113 >        SDsquare2disk(spt,      (i+.1+.8*frandom())/hp->ns,
114 >                                (j+.1+.8*frandom())/hp->ns );
115          zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
116          for (ii = 3; ii--; )
117                  ar.rdir[ii] =   spt[0]*hp->ux[ii] +
# Line 113 | Line 121 | ambsample(                             /* sample an ambient direction */
121          dimlist[ndims++] = i*hp->ns + j + 90171;
122          rayvalue(&ar);                  /* evaluate ray */
123          ndims--;
124 +                                        /* limit vertex distance */
125 +        if (ar.rt > 10.0*thescene.cusize)
126 +                ar.rt = 10.0*thescene.cusize;
127 +        else if (ar.rt <= FTINY)        /* should never happen! */
128 +                goto badsample;
129 +        VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
130          multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
131          copycolor(ap->v, ar.rcol);
132 <        if (ar.rt > 20.0*maxarad)       /* limit vertex distance */
133 <                ar.rt = 20.0*maxarad;
134 <        VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
135 <        return(1);
132 >        return(ap);
133 > badsample:
134 >        setcolor(ap->v, 0., 0., 0.);
135 >        VCOPY(ap->p, hp->rp->rop);
136 >        return(NULL);
137   }
138  
139  
140 + /* Compute vectors and coefficients for Hessian/gradient calcs */
141   static void
142 + comp_fftri(FFTRI *ftp, FVECT ap0, FVECT ap1, FVECT rop)
143 + {
144 +        FVECT   vcp;
145 +        double  dot_e, dot_er, rdot_r, rdot_r1, J2;
146 +        int     i;
147 +
148 +        VSUB(ftp->r_i, ap0, rop);
149 +        VSUB(ftp->r_i1, ap1, rop);
150 +        VSUB(ftp->e_i, ap1, ap0);
151 +        VCROSS(vcp, ftp->e_i, ftp->r_i);
152 +        ftp->nf = 1.0/DOT(vcp,vcp);
153 +        dot_e = DOT(ftp->e_i,ftp->e_i);
154 +        dot_er = DOT(ftp->e_i, ftp->r_i);
155 +        rdot_r = 1.0/DOT(ftp->r_i,ftp->r_i);
156 +        rdot_r1 = 1.0/DOT(ftp->r_i1,ftp->r_i1);
157 +        ftp->I1 = acos( DOT(ftp->r_i, ftp->r_i1) * sqrt(rdot_r*rdot_r1) ) *
158 +                        sqrt( ftp->nf );
159 +        ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)*rdot_r1 - dot_er*rdot_r +
160 +                        dot_e*ftp->I1 )*0.5*ftp->nf;
161 +        J2 =  ( 0.5*(rdot_r - rdot_r1) - dot_er*ftp->I2 ) / dot_e;
162 +        for (i = 3; i--; )
163 +                ftp->rI2_eJ2[i] = ftp->I2*ftp->r_i[i] + J2*ftp->e_i[i];
164 + }
165 +
166 +
167 + /* Compose 3x3 matrix from two vectors */
168 + static void
169 + compose_matrix(FVECT mat[3], FVECT va, FVECT vb)
170 + {
171 +        mat[0][0] = 2.0*va[0]*vb[0];
172 +        mat[1][1] = 2.0*va[1]*vb[1];
173 +        mat[2][2] = 2.0*va[2]*vb[2];
174 +        mat[0][1] = mat[1][0] = va[0]*vb[1] + va[1]*vb[0];
175 +        mat[0][2] = mat[2][0] = va[0]*vb[2] + va[2]*vb[0];
176 +        mat[1][2] = mat[2][1] = va[1]*vb[2] + va[2]*vb[1];
177 + }
178 +
179 +
180 + /* Compute partial 3x3 Hessian matrix for edge */
181 + static void
182 + comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
183 + {
184 +        FVECT   vcp;
185 +        FVECT   m1[3], m2[3], m3[3], m4[3];
186 +        double  d1, d2, d3, d4;
187 +        double  I3, J3, K3;
188 +        int     i, j;
189 +                                        /* compute intermediate coefficients */
190 +        d1 = 1.0/DOT(ftp->r_i,ftp->r_i);
191 +        d2 = 1.0/DOT(ftp->r_i1,ftp->r_i1);
192 +        d3 = 1.0/DOT(ftp->e_i,ftp->e_i);
193 +        d4 = DOT(ftp->e_i, ftp->r_i);
194 +        I3 = 0.25*ftp->nf*( DOT(ftp->e_i, ftp->r_i1)*d2*d2 - d4*d1*d1 +
195 +                                3.0/d3*ftp->I2 );
196 +        J3 = 0.25*d3*(d1*d1 - d2*d2) - d4*d3*I3;
197 +        K3 = d3*(ftp->I2 - I3/d1 - 2.0*d4*J3);
198 +                                        /* intermediate matrices */
199 +        VCROSS(vcp, nrm, ftp->e_i);
200 +        compose_matrix(m1, vcp, ftp->rI2_eJ2);
201 +        compose_matrix(m2, ftp->r_i, ftp->r_i);
202 +        compose_matrix(m3, ftp->e_i, ftp->e_i);
203 +        compose_matrix(m4, ftp->r_i, ftp->e_i);
204 +        VCROSS(vcp, ftp->r_i, ftp->e_i);
205 +        d1 = DOT(nrm, vcp);
206 +        d2 = -d1*ftp->I2;
207 +        d1 *= 2.0;
208 +        for (i = 3; i--; )              /* final matrix sum */
209 +            for (j = 3; j--; ) {
210 +                hess[i][j] = m1[i][j] + d1*( I3*m2[i][j] + K3*m3[i][j] +
211 +                                                2.0*J3*m4[i][j] );
212 +                hess[i][j] += d2*(i==j);
213 +                hess[i][j] *= 1.0/PI;
214 +            }
215 + }
216 +
217 +
218 + /* Reverse hessian calculation result for edge in other direction */
219 + static void
220 + rev_hessian(FVECT hess[3])
221 + {
222 +        int     i;
223 +
224 +        for (i = 3; i--; ) {
225 +                hess[i][0] = -hess[i][0];
226 +                hess[i][1] = -hess[i][1];
227 +                hess[i][2] = -hess[i][2];
228 +        }
229 + }
230 +
231 +
232 + /* Add to radiometric Hessian from the given triangle */
233 + static void
234 + add2hessian(FVECT hess[3], FVECT ehess1[3],
235 +                FVECT ehess2[3], FVECT ehess3[3], COLORV v)
236 + {
237 +        int     i, j;
238 +
239 +        for (i = 3; i--; )
240 +            for (j = 3; j--; )
241 +                hess[i][j] += v*( ehess1[i][j] + ehess2[i][j] + ehess3[i][j] );
242 + }
243 +
244 +
245 + /* Compute partial displacement form factor gradient for edge */
246 + static void
247 + comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm)
248 + {
249 +        FVECT   vcp;
250 +        double  f1;
251 +        int     i;
252 +
253 +        VCROSS(vcp, ftp->r_i, ftp->r_i1);
254 +        f1 = 2.0*DOT(nrm, vcp);
255 +        VCROSS(vcp, nrm, ftp->e_i);
256 +        for (i = 3; i--; )
257 +                grad[i] = (-0.5/PI)*( ftp->I1*vcp[i] + f1*ftp->rI2_eJ2[i] );
258 + }
259 +
260 +
261 + /* Reverse gradient calculation result for edge in other direction */
262 + static void
263 + rev_gradient(FVECT grad)
264 + {
265 +        grad[0] = -grad[0];
266 +        grad[1] = -grad[1];
267 +        grad[2] = -grad[2];
268 + }
269 +
270 +
271 + /* Add to displacement gradient from the given triangle */
272 + static void
273 + add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, COLORV v)
274 + {
275 +        int     i;
276 +
277 +        for (i = 3; i--; )
278 +                grad[i] += v*( egrad1[i] + egrad2[i] + egrad3[i] );
279 + }
280 +
281 +
282 + /* Return brightness of furthest ambient sample */
283 + static COLORV
284 + back_ambval(struct s_ambsamp *ap1, struct s_ambsamp *ap2,
285 +                struct s_ambsamp *ap3, FVECT orig)
286 + {
287 +        COLORV  vback;
288 +        FVECT   vec;
289 +        double  d2, d2best;
290 +
291 +        VSUB(vec, ap1->p, orig);
292 +        d2best = DOT(vec,vec);
293 +        vback = colval(ap1->v,CIEY);
294 +        VSUB(vec, ap2->p, orig);
295 +        d2 = DOT(vec,vec);
296 +        if (d2 > d2best) {
297 +                d2best = d2;
298 +                vback = colval(ap2->v,CIEY);
299 +        }
300 +        VSUB(vec, ap3->p, orig);
301 +        d2 = DOT(vec,vec);
302 +        if (d2 > d2best)
303 +                return(colval(ap3->v,CIEY));
304 +        return(vback);
305 + }
306 +
307 +
308 + /* Compute anisotropic radii and eigenvector directions */
309 + static int
310 + eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3])
311 + {
312 +        double  hess2[2][2];
313 +        FVECT   a, b;
314 +        double  evalue[2], slope1, xmag1;
315 +        int     i;
316 +                                        /* project Hessian to sample plane */
317 +        for (i = 3; i--; ) {
318 +                a[i] = DOT(hessian[i], uv[0]);
319 +                b[i] = DOT(hessian[i], uv[1]);
320 +        }
321 +        hess2[0][0] = DOT(uv[0], a);
322 +        hess2[0][1] = DOT(uv[0], b);
323 +        hess2[1][0] = DOT(uv[1], a);
324 +        hess2[1][1] = DOT(uv[1], b);
325 +                                        /* compute eigenvalues */
326 +        if ( quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1],
327 +                        hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]) != 2 ||
328 +                        (evalue[0] = fabs(evalue[0])) <= FTINY*FTINY ||
329 +                        (evalue[1] = fabs(evalue[1])) <= FTINY*FTINY )
330 +                error(INTERNAL, "bad eigenvalue calculation");
331 +
332 +        if (evalue[0] > evalue[1]) {
333 +                ra[0] = sqrt(sqrt(4.0/evalue[0]));
334 +                ra[1] = sqrt(sqrt(4.0/evalue[1]));
335 +                slope1 = evalue[1];
336 +        } else {
337 +                ra[0] = sqrt(sqrt(4.0/evalue[1]));
338 +                ra[1] = sqrt(sqrt(4.0/evalue[0]));
339 +                slope1 = evalue[0];
340 +        }
341 +                                        /* compute unit eigenvectors */
342 +        if (fabs(hess2[0][1]) <= FTINY)
343 +                return;                 /* uv OK as is */
344 +        slope1 = (slope1 - hess2[0][0]) / hess2[0][1];
345 +        xmag1 = sqrt(1.0/(1.0 + slope1*slope1));
346 +        for (i = 3; i--; ) {
347 +                b[i] = xmag1*uv[0][i] + slope1*xmag1*uv[1][i];
348 +                a[i] = slope1*xmag1*uv[0][i] - xmag1*uv[1][i];
349 +        }
350 +        VCOPY(uv[0], a);
351 +        VCOPY(uv[1], b);
352 + }
353 +
354 +
355 + static void
356   ambHessian(                             /* anisotropic radii & pos. gradient */
357          AMBHEMI *hp,
358          FVECT   uv[2],                  /* returned */
359 <        float   ra[2],                  /* returned */
360 <        float   pg[2]                   /* returned */
359 >        float   ra[2],                  /* returned (optional) */
360 >        float   pg[2]                   /* returned (optional) */
361   )
362   {
363 <        if (ra != NULL) {               /* compute Hessian-derived radii */
364 <        } else {                        /* else copy original tangent axes */
365 <                VCOPY(uv[0], hp->ux);
366 <                VCOPY(uv[1], hp->uy);
367 <        }
368 <        if (pg == NULL)                 /* no position gradient requested? */
363 >        static char     memerrmsg[] = "out of memory in ambHessian()";
364 >        FVECT           (*hessrow)[3] = NULL;
365 >        FVECT           *gradrow = NULL;
366 >        FVECT           hessian[3];
367 >        FVECT           gradient;
368 >        FFTRI           fftr;
369 >        int             i, j;
370 >                                        /* be sure to assign unit vectors */
371 >        VCOPY(uv[0], hp->ux);
372 >        VCOPY(uv[1], hp->uy);
373 >                        /* clock-wise vertex traversal from sample POV */
374 >        if (ra != NULL) {               /* initialize Hessian row buffer */
375 >                hessrow = (FVECT (*)[3])malloc(sizeof(FVECT)*3*(hp->ns-1));
376 >                if (hessrow == NULL)
377 >                        error(SYSTEM, memerrmsg);
378 >                memset(hessian, 0, sizeof(hessian));
379 >        } else if (pg == NULL)          /* bogus call? */
380                  return;
381 +        if (pg != NULL) {               /* initialize form factor row buffer */
382 +                gradrow = (FVECT *)malloc(sizeof(FVECT)*(hp->ns-1));
383 +                if (gradrow == NULL)
384 +                        error(SYSTEM, memerrmsg);
385 +                memset(gradient, 0, sizeof(gradient));
386 +        }
387 +                                        /* compute first row of edges */
388 +        for (j = 0; j < hp->ns-1; j++) {
389 +                comp_fftri(&fftr, ambsamp(hp,0,j).p,
390 +                                ambsamp(hp,0,j+1).p, hp->rp->rop);
391 +                if (hessrow != NULL)
392 +                        comp_hessian(hessrow[j], &fftr, hp->rp->ron);
393 +                if (gradrow != NULL)
394 +                        comp_gradient(gradrow[j], &fftr, hp->rp->ron);
395 +        }
396 +                                        /* sum each row of triangles */
397 +        for (i = 0; i < hp->ns-1; i++) {
398 +            FVECT       hesscol[3];     /* compute first vertical edge */
399 +            FVECT       gradcol;
400 +            comp_fftri(&fftr, ambsamp(hp,i,0).p,
401 +                        ambsamp(hp,i+1,0).p, hp->rp->rop);
402 +            if (hessrow != NULL)
403 +                comp_hessian(hesscol, &fftr, hp->rp->ron);
404 +            if (gradrow != NULL)
405 +                comp_gradient(gradcol, &fftr, hp->rp->ron);
406 +            for (j = 0; j < hp->ns-1; j++) {
407 +                FVECT   hessdia[3];     /* compute triangle contributions */
408 +                FVECT   graddia;
409 +                COLORV  backg;
410 +                backg = back_ambval(&ambsamp(hp,i,j), &ambsamp(hp,i,j+1),
411 +                                        &ambsamp(hp,i+1,j), hp->rp->rop);
412 +                                        /* diagonal (inner) edge */
413 +                comp_fftri(&fftr, ambsamp(hp,i,j+1).p,
414 +                                ambsamp(hp,i+1,j).p, hp->rp->rop);
415 +                if (hessrow != NULL) {
416 +                    comp_hessian(hessdia, &fftr, hp->rp->ron);
417 +                    rev_hessian(hesscol);
418 +                    add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
419 +                }
420 +                if (gradient != NULL) {
421 +                    comp_gradient(graddia, &fftr, hp->rp->ron);
422 +                    rev_gradient(gradcol);
423 +                    add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
424 +                }
425 +                                        /* initialize edge in next row */
426 +                comp_fftri(&fftr, ambsamp(hp,i+1,j+1).p,
427 +                                ambsamp(hp,i+1,j).p, hp->rp->rop);
428 +                if (hessrow != NULL)
429 +                    comp_hessian(hessrow[j], &fftr, hp->rp->ron);
430 +                if (gradrow != NULL)
431 +                    comp_gradient(gradrow[j], &fftr, hp->rp->ron);
432 +                                        /* new column edge & paired triangle */
433 +                backg = back_ambval(&ambsamp(hp,i,j+1), &ambsamp(hp,i+1,j+1),
434 +                                        &ambsamp(hp,i+1,j), hp->rp->rop);
435 +                comp_fftri(&fftr, ambsamp(hp,i,j+1).p, ambsamp(hp,i+1,j+1).p,
436 +                                hp->rp->rop);
437 +                if (hessrow != NULL) {
438 +                    comp_hessian(hesscol, &fftr, hp->rp->ron);
439 +                    rev_hessian(hessdia);
440 +                    add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
441 +                    if (i < hp->ns-2)
442 +                        rev_hessian(hessrow[j]);
443 +                }
444 +                if (gradrow != NULL) {
445 +                    comp_gradient(gradcol, &fftr, hp->rp->ron);
446 +                    rev_gradient(graddia);
447 +                    add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
448 +                    if (i < hp->ns-2)
449 +                        rev_gradient(gradrow[j]);
450 +                }
451 +            }
452 +        }
453 +                                        /* release row buffers */
454 +        if (hessrow != NULL) free(hessrow);
455 +        if (gradrow != NULL) free(gradrow);
456 +        
457 +        if (ra != NULL)                 /* extract eigenvectors & radii */
458 +                eigenvectors(uv, ra, hessian);
459 +        if (pg != NULL) {               /* tangential position gradient */
460 +                pg[0] = DOT(gradient, uv[0]);
461 +                pg[1] = DOT(gradient, uv[1]);
462 +        }
463   }
464  
465 +
466 + /* Compute direction gradient from a hemispherical sampling */
467 + static void
468 + ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
469 + {
470 +        struct s_ambsamp        *ap;
471 +        double                  dgsum[2];
472 +        int                     n;
473 +        FVECT                   vd;
474 +        double                  gfact;
475 +
476 +        dgsum[0] = dgsum[1] = 0.0;      /* sum values times -tan(theta) */
477 +        for (ap = hp->sa, n = hp->ns*hp->ns; n--; ap++) {
478 +                                        /* use vector for azimuth + 90deg */
479 +                VSUB(vd, ap->p, hp->rp->rop);
480 +                                        /* brightness over cosine factor */
481 +                gfact = colval(ap->v,CIEY) / DOT(hp->rp->ron, vd);
482 +                                        /* -sine = -proj_radius/vd_length */
483 +                dgsum[0] += DOT(uv[1], vd) * gfact;
484 +                dgsum[1] -= DOT(uv[0], vd) * gfact;
485 +        }
486 +        dg[0] = dgsum[0] / (hp->ns*hp->ns);
487 +        dg[1] = dgsum[1] / (hp->ns*hp->ns);
488 + }
489 +
490 +
491   int
492   doambient(                              /* compute ambient component */
493          COLOR   rcol,                   /* input/output color */
494          RAY     *r,
495          double  wt,
496 <        FVECT   uv[2],                  /* returned */
497 <        float   ra[2],                  /* returned */
498 <        float   pg[2],                  /* returned */
499 <        float   dg[2]                   /* returned */
496 >        FVECT   uv[2],                  /* returned (optional) */
497 >        float   ra[2],                  /* returned (optional) */
498 >        float   pg[2],                  /* returned (optional) */
499 >        float   dg[2]                   /* returned (optional) */
500   )
501   {
502 +        AMBHEMI                 *hp = inithemi(rcol, r, wt);
503          int                     cnt = 0;
504          FVECT                   my_uv[2];
155        AMBHEMI                 *hp;
505          double                  d, acol[3];
506          struct s_ambsamp        *ap;
507          int                     i, j;
508 <                                        /* initialize */
509 <        if ((hp = inithemi(rcol, r, wt)) == NULL)
508 >                                        /* check/initialize */
509 >        if (hp == NULL)
510                  return(0);
511          if (uv != NULL)
512                  memset(uv, 0, sizeof(FVECT)*2);
# Line 169 | Line 518 | doambient(                             /* compute ambient component */
518                  dg[0] = dg[1] = 0.0;
519                                          /* sample the hemisphere */
520          acol[0] = acol[1] = acol[2] = 0.0;
521 <        for (i = hemi.ns; i--; )
522 <                for (j = hemi.ns; j--; )
523 <                        if (ambsample(hp, i, j)) {
175 <                                ap = &ambsamp(hp,i,j);
521 >        for (i = hp->ns; i--; )
522 >                for (j = hp->ns; j--; )
523 >                        if ((ap = ambsample(hp, i, j)) != NULL) {
524                                  addcolor(acol, ap->v);
525                                  ++cnt;
526                          }
# Line 181 | Line 529 | doambient(                             /* compute ambient component */
529                  free(hp);
530                  return(0);              /* no valid samples */
531          }
532 <        d = 1.0 / cnt;                  /* final indirect irradiance/PI */
185 <        acol[0] *= d; acol[1] *= d; acol[2] *= d;
186 <        copycolor(rcol, acol);
532 >        copycolor(rcol, acol);          /* final indirect irradiance/PI */
533          if (cnt < hp->ns*hp->ns ||      /* incomplete sampling? */
534                          (ra == NULL) & (pg == NULL) & (dg == NULL)) {
535                  free(hp);
536                  return(-1);             /* no radius or gradient calc. */
537          }
538 <        d = 0.01 * bright(rcol);        /* add in 1% before Hessian comp. */
539 <        if (d < FTINY) d = FTINY;
540 <        ap = hp->sa;                    /* using Y channel from here on... */
538 >        if (bright(acol) > FTINY)       /* normalize Y values */
539 >                d = cnt/bright(acol);
540 >        else
541 >                d = 0.0;
542 >        ap = hp->sa;                    /* relative Y channel from here on... */
543          for (i = hp->ns*hp->ns; i--; ap++)
544 <                colval(ap->v,CIEY) = bright(ap->v) + d;
544 >                colval(ap->v,CIEY) = bright(ap->v)*d + 0.01;
545  
546          if (uv == NULL)                 /* make sure we have axis pointers */
547                  uv = my_uv;
548                                          /* compute radii & pos. gradient */
549          ambHessian(hp, uv, ra, pg);
550 +
551          if (dg != NULL)                 /* compute direction gradient */
552                  ambdirgrad(hp, uv, dg);
553 <        if (ra != NULL) {               /* adjust/clamp radii */
554 <                d = pow(wt, -0.25);
555 <                if ((ra[0] *= d) > maxarad)
556 <                        ra[0] = maxarad;
553 >
554 >        if (ra != NULL) {               /* scale/clamp radii */
555 >                if (ra[0] < minarad) {
556 >                        ra[0] = minarad;
557 >                        if (ra[1] < minarad)
558 >                                ra[1] = minarad;
559 >                                        /* cap gradient if necessary */
560 >                        if (pg != NULL) {
561 >                                d = pg[0]*pg[0]*ra[0]*ra[0] +
562 >                                                pg[1]*pg[1]*ra[1]*ra[1];
563 >                                if (d > 1.0) {
564 >                                        d = 1.0/sqrt(d);
565 >                                        pg[0] *= d;
566 >                                        pg[1] *= d;
567 >                                }
568 >                        }
569 >                }
570 >                ra[0] *= d = 1.0/sqrt(sqrt(wt));
571                  if ((ra[1] *= d) > 2.0*ra[0])
572                          ra[1] = 2.0*ra[0];
573 +                if (ra[1] > maxarad) {
574 +                        ra[1] = maxarad;
575 +                        if (ra[0] > maxarad)
576 +                                ra[0] = maxarad;
577 +                }
578          }
579          free(hp);                       /* clean up and return */
580          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines