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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines