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.27 by greg, Sat Apr 19 02:39:44 2014 UTC vs.
Revision 2.37 by greg, Sat Apr 26 05:09:54 2014 UTC

# Line 28 | Line 28 | typedef struct {
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;
39 <        double  nf, I1, I2, J2;
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  
# Line 69 | 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->ron[i] < 0.6 && r->ron[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->ron);
81          normalize(hp->ux);
# Line 85 | Line 85 | inithemi(                      /* initialize sampling hemisphere */
85   }
86  
87  
88 < static int
88 > static struct s_ambsamp *
89   ambsample(                              /* sample an ambient direction */
90          AMBHEMI *hp,
91          int     i,
# Line 94 | Line 94 | ambsample(                             /* sample an ambient direction */
94   {
95          struct s_ambsamp        *ap = &ambsamp(hp,i,j);
96          RAY                     ar;
97        int                     hlist[3];
97          double                  spt[2], zd;
98          int                     ii;
99                                          /* ambient coefficient for weight */
# Line 102 | Line 101 | ambsample(                             /* sample an ambient direction */
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.);
107 <                VCOPY(ap->p, hp->rp->rop);
108 <                return(0);              /* no sample taken */
109 <        }
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+.1+.8*frandom())/hp->ns,
112 <                                (j+.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 123 | 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, float ap0[3], float ap1[3], FVECT rop)
140 > comp_fftri(FFTRI *ftp, FVECT ap0, FVECT ap1, FVECT rop)
141   {
142 <        FVECT   v1;
143 <        double  dot_e, dot_er, dot_r, dot_r1;
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(v1, ftp->e_i, ftp->r_i);
149 <        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);
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 <        dot_r = DOT(ftp->r_i,ftp->r_i);
153 <        dot_r1 = DOT(ftp->r_i1,ftp->r_i1);
154 <        ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)/dot_r1 - dot_er/dot_r +
155 <                        dot_e*ftp->I1 )*0.5*ftp->nf;
156 <        ftp->J2 =  0.25*ftp->nf*( 1.0/dot_r - 1.0/dot_r1 ) -
157 <                        dot_er/dot_e*ftp->I2;
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 matrix from two vectors */
164 > /* Compose 3x3 matrix from two vectors */
165   static void
166   compose_matrix(FVECT mat[3], FVECT va, FVECT vb)
167   {
# Line 174 | Line 178 | compose_matrix(FVECT mat[3], FVECT va, FVECT vb)
178   static void
179   comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
180   {
181 <        FVECT   v1, v2;
181 >        FVECT   ncp;
182          FVECT   m1[3], m2[3], m3[3], m4[3];
183          double  d1, d2, d3, d4;
184          double  I3, J3, K3;
# Line 184 | Line 188 | comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
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 = 0.25*ftp->nf*( DOT(ftp->e_i, ftp->r_i1)*d2*d2 - d4*d1*d1 +
192 <                                3.0*ftp->I2*d3 );
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(v1, nrm, ftp->e_i);
197 <        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 >        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 <        VCROSS(v1, ftp->r_i, ftp->e_i);
200 <        d1 = DOT(nrm, v1);
201 >        d1 = DOT(nrm, ftp->rcp);
202          d2 = -d1*ftp->I2;
203          d1 *= 2.0;
204          for (i = 3; i--; )              /* final matrix sum */
# Line 205 | Line 206 | comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
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;
209 >                hess[i][j] *= 1.0/PI;
210              }
211   }
212  
# Line 241 | Line 242 | add2hessian(FVECT hess[3], FVECT ehess1[3],
242   static void
243   comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm)
244   {
245 <        FVECT   vcp;
245 >        FVECT   ncp;
246          double  f1;
247          int     i;
248  
249 <        VCROSS(vcp, ftp->r_i, ftp->r_i1);
250 <        f1 = 2.0*DOT(nrm, vcp);
250 <        VCROSS(vcp, nrm, ftp->e_i);
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*vcp[i] +
253 <                            f1*(ftp->I2*ftp->r_i[i] + ftp->J2*ftp->e_i[i]) );
252 >                grad[i] = (-0.5/PI)*( ftp->I1*ncp[i] + f1*ftp->rI2_eJ2[i] );
253   }
254  
255  
# Line 286 | Line 285 | back_ambval(struct s_ambsamp *ap1, struct s_ambsamp *a
285  
286          VSUB(vec, ap1->p, orig);
287          d2best = DOT(vec,vec);
288 <        vback = ap1->v[CIEY];
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 = ap2->v[CIEY];
293 >                vback = colval(ap2->v,CIEY);
294          }
295          VSUB(vec, ap3->p, orig);
296          d2 = DOT(vec,vec);
297          if (d2 > d2best)
298 <                return(ap3->v[CIEY]);
298 >                return(colval(ap3->v,CIEY));
299          return(vback);
300   }
301  
# Line 319 | Line 318 | eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3
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],
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*FTINY ||
324 <                        (evalue[1] = fabs(evalue[1])) <= FTINY*FTINY*FTINY)
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] = 1.0/sqrt(sqrt(evalue[0]));
329 <                ra[1] = 1.0/sqrt(sqrt(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] = 1.0/sqrt(sqrt(evalue[1]));
333 <                ra[1] = 1.0/sqrt(sqrt(evalue[0]));
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 */
# Line 352 | Line 351 | 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          static char     memerrmsg[] = "out of memory in ambHessian()";
# Line 368 | Line 367 | ambHessian(                            /* anisotropic radii & pos. gradient */
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);
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);
377 >                gradrow = (FVECT *)malloc(sizeof(FVECT)*(hp->ns-1));
378                  if (gradrow == NULL)
379                          error(SYSTEM, memerrmsg);
380                  memset(gradient, 0, sizeof(gradient));
# Line 452 | Line 451 | ambHessian(                            /* anisotropic radii & pos. gradient */
451          
452          if (ra != NULL)                 /* extract eigenvectors & radii */
453                  eigenvectors(uv, ra, hessian);
454 <        if (pg != NULL) {               /* project position gradient */
454 >        if (pg != NULL) {               /* tangential position gradient */
455                  pg[0] = DOT(gradient, uv[0]);
456                  pg[1] = DOT(gradient, uv[1]);
457          }
# Line 464 | Line 463 | 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 <        dg[0] = dg[1] = 0;
471 >        dgsum[0] = dgsum[1] = 0.0;      /* sum values times -tan(theta) */
472          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;
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  
# Line 492 | Line 494 | doambient(                             /* compute ambient component */
494          float   dg[2]                   /* returned (optional) */
495   )
496   {
497 +        AMBHEMI                 *hp = inithemi(rcol, r, wt);
498          int                     cnt = 0;
499          FVECT                   my_uv[2];
497        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 513 | Line 515 | doambient(                             /* compute ambient component */
515          acol[0] = acol[1] = acol[2] = 0.0;
516          for (i = hp->ns; i--; )
517                  for (j = hp->ns; j--; )
518 <                        if (ambsample(hp, i, j)) {
517 <                                ap = &ambsamp(hp,i,j);
518 >                        if ((ap = ambsample(hp, i, j)) != NULL) {
519                                  addcolor(acol, ap->v);
520                                  ++cnt;
521                          }
# Line 523 | 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 */
527 <        acol[0] *= d; acol[1] *= d; acol[2] *= d;
528 <        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 = sqrt(sqrt((4.0/PI)*bright(rcol)/wt));
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