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.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  
# 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 {
# Line 30 | Line 34 | typedef struct {
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,
# 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 63 | Line 72 | inithemi(                      /* initialize sampling hemisphere */
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->rn[i] < 0.6 && r->rn[i] > -0.6)
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->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   }
# Line 80 | Line 89 | static int
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];
98 <        double                  spt[2], dz;
98 >        double                  spt[2], zd;
99          int                     ii;
100                                          /* ambient coefficient for weight */
101          if (ambacc > FTINY)
# Line 95 | Line 104 | ambsample(                             /* sample an ambient direction */
104                  copycolor(ar.rcoef, hp->acoef);
105          if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) {
106                  setcolor(ap->v, 0., 0., 0.);
107 <                ap->r = 0.;
107 >                VCOPY(ap->p, hp->rp->rop);
108                  return(0);              /* no sample taken */
109          }
110          if (ambacc > FTINY) {
# Line 103 | Line 112 | ambsample(                             /* sample an ambient direction */
112                  scalecolor(ar.rcoef, 1./AVGREFL);
113          }
114                                          /* generate hemispherical sample */
115 <        SDsquare2disk(spt, (i+frandom())/hp->ns, (j+frandom())/hp->ns);
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] +
# Line 122 | Line 132 | ambsample(                             /* sample an ambient direction */
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 + eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3])
307 + {
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 (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 */
# Line 130 | Line 356 | ambHessian(                            /* anisotropic radii & pos. gradient */
356          float   pg[2]                   /* returned */
357   )
358   {
359 <        if (ra != NULL) {               /* compute Hessian-derived radii */
360 <        } else {                        /* else copy original tangent axes */
361 <                VCOPY(uv[0], hp->ux);
362 <                VCOPY(uv[1], hp->uy);
363 <        }
364 <        if (pg == NULL)                 /* no position gradient requested? */
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   doambient(                              /* compute ambient component */
486          COLOR   rcol,                   /* input/output color */
487          RAY     *r,
488          double  wt,
489 <        FVECT   uv[2],                  /* returned */
490 <        float   ra[2],                  /* returned */
491 <        float   pg[2],                  /* returned */
492 <        float   dg[2]                   /* returned */
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;
# Line 169 | Line 511 | doambient(                             /* compute ambient component */
511                  dg[0] = dg[1] = 0.0;
512                                          /* sample the hemisphere */
513          acol[0] = acol[1] = acol[2] = 0.0;
514 <        for (i = hemi.ns; i--; )
515 <                for (j = hemi.ns; j--; )
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);
# Line 202 | Line 544 | doambient(                             /* compute ambient component */
544          if (dg != NULL)                 /* compute direction gradient */
545                  ambdirgrad(hp, uv, dg);
546          if (ra != NULL) {               /* adjust/clamp radii */
547 <                d = pow(wt, -0.25);
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])

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines