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.29 by greg, Wed Apr 23 06:04:17 2014 UTC

# 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 */
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->ron[i] < 0.6 && r->ron[i] > -0.6)
78                          break;
# Line 85 | Line 87 | inithemi(                      /* initialize sampling hemisphere */
87   }
88  
89  
90 < static int
90 > static struct s_ambsamp *
91   ambsample(                              /* sample an ambient direction */
92          AMBHEMI *hp,
93          int     i,
# Line 94 | Line 96 | ambsample(                             /* sample an ambient direction */
96   {
97          struct s_ambsamp        *ap = &ambsamp(hp,i,j);
98          RAY                     ar;
97        int                     hlist[3];
99          double                  spt[2], zd;
100          int                     ii;
101                                          /* ambient coefficient for weight */
# Line 105 | Line 106 | ambsample(                             /* sample an ambient direction */
106          if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) {
107                  setcolor(ap->v, 0., 0., 0.);
108                  VCOPY(ap->p, hp->rp->rop);
109 <                return(0);              /* no sample taken */
109 >                return(NULL);           /* no sample taken */
110          }
111          if (ambacc > FTINY) {
112                  multcolor(ar.rcoef, hp->acoef);
# Line 113 | Line 114 | ambsample(                             /* sample an ambient direction */
114          }
115                                          /* generate hemispherical sample */
116          SDsquare2disk(spt,      (i+.1+.8*frandom())/hp->ns,
117 <                                (j+.1+.8*frandom())/hp->ns);
117 >                                (j+.1+.8*frandom())/hp->ns );
118          zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
119          for (ii = 3; ii--; )
120                  ar.rdir[ii] =   spt[0]*hp->ux[ii] +
# Line 126 | Line 127 | ambsample(                             /* sample an ambient direction */
127          multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
128          copycolor(ap->v, ar.rcol);
129          if (ar.rt > 20.0*maxarad)       /* limit vertex distance */
130 <                ar.rt = 20.0*maxarad;
131 <        VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
132 <        return(1);
130 >                VSUM(ap->p, ar.rorg, ar.rdir, 20.0*maxarad);
131 >        else
132 >                VCOPY(ap->p, ar.rop);
133 >        return(ap);
134   }
135  
136  
# Line 144 | Line 146 | comp_fftri(FFTRI *ftp, float ap0[3], float ap1[3], FVE
146          VSUB(ftp->e_i, ap1, ap0);
147          VCROSS(v1, ftp->e_i, ftp->r_i);
148          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->I1 = acos( DOT(ftp->r_i, ftp->r_i1) / sqrt(dot_r*dot_r1) ) *
154 +                        sqrt( ftp->nf );
155          ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)/dot_r1 - dot_er/dot_r +
156                          dot_e*ftp->I1 )*0.5*ftp->nf;
157 <        ftp->J2 =  0.25*ftp->nf*( 1.0/dot_r - 1.0/dot_r1 ) -
157 >        ftp->J2 =  0.5/dot_e*( 1.0/dot_r - 1.0/dot_r1 ) -
158                          dot_er/dot_e*ftp->I2;
159   }
160  
161  
162 < /* Compose matrix from two vectors */
162 > /* Compose 3x3 matrix from two vectors */
163   static void
164   compose_matrix(FVECT mat[3], FVECT va, FVECT vb)
165   {
# Line 185 | Line 187 | comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm)
187          d3 = 1.0/DOT(ftp->e_i,ftp->e_i);
188          d4 = DOT(ftp->e_i, ftp->r_i);
189          I3 = 0.25*ftp->nf*( DOT(ftp->e_i, ftp->r_i1)*d2*d2 - d4*d1*d1 +
190 <                                3.0*ftp->I2*d3 );
190 >                                3.0/d3*ftp->I2 );
191          J3 = 0.25*d3*(d1*d1 - d2*d2) - d4*d3*I3;
192          K3 = d3*(ftp->I2 - I3/d1 - 2.0*d4*J3);
193                                          /* intermediate matrices */
194          VCROSS(v1, nrm, ftp->e_i);
195          for (j = 3; j--; )
196 <                v2[i] = ftp->I2*ftp->r_i[j] + ftp->J2*ftp->e_i[j];
196 >                v2[j] = ftp->I2*ftp->r_i[j] + ftp->J2*ftp->e_i[j];
197          compose_matrix(m1, v1, v2);
198          compose_matrix(m2, ftp->r_i, ftp->r_i);
199          compose_matrix(m3, ftp->e_i, ftp->e_i);
# Line 286 | Line 288 | back_ambval(struct s_ambsamp *ap1, struct s_ambsamp *a
288  
289          VSUB(vec, ap1->p, orig);
290          d2best = DOT(vec,vec);
291 <        vback = ap1->v[CIEY];
291 >        vback = colval(ap1->v,CIEY);
292          VSUB(vec, ap2->p, orig);
293          d2 = DOT(vec,vec);
294          if (d2 > d2best) {
295                  d2best = d2;
296 <                vback = ap2->v[CIEY];
296 >                vback = colval(ap2->v,CIEY);
297          }
298          VSUB(vec, ap3->p, orig);
299          d2 = DOT(vec,vec);
300          if (d2 > d2best)
301 <                return(ap3->v[CIEY]);
301 >                return(colval(ap3->v,CIEY));
302          return(vback);
303   }
304  
# Line 319 | Line 321 | eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3
321          hess2[1][0] = DOT(uv[1], a);
322          hess2[1][1] = DOT(uv[1], b);
323                                          /* compute eigenvalues */
324 <        if (quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1],
324 >        if ( quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1],
325                          hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]) != 2 ||
326 <                        (evalue[0] = fabs(evalue[0])) <= FTINY*FTINY*FTINY ||
327 <                        (evalue[1] = fabs(evalue[1])) <= FTINY*FTINY*FTINY)
326 >                        (evalue[0] = fabs(evalue[0])) <= FTINY*FTINY ||
327 >                        (evalue[1] = fabs(evalue[1])) <= FTINY*FTINY )
328                  error(INTERNAL, "bad eigenvalue calculation");
329  
330          if (evalue[0] > evalue[1]) {
331 <                ra[0] = 1.0/sqrt(sqrt(evalue[0]));
332 <                ra[1] = 1.0/sqrt(sqrt(evalue[1]));
331 >                ra[0] = sqrt(sqrt(4.0/evalue[0]));
332 >                ra[1] = sqrt(sqrt(4.0/evalue[1]));
333                  slope1 = evalue[1];
334          } else {
335 <                ra[0] = 1.0/sqrt(sqrt(evalue[1]));
336 <                ra[1] = 1.0/sqrt(sqrt(evalue[0]));
335 >                ra[0] = sqrt(sqrt(4.0/evalue[1]));
336 >                ra[1] = sqrt(sqrt(4.0/evalue[0]));
337                  slope1 = evalue[0];
338          }
339                                          /* compute unit eigenvectors */
# Line 352 | Line 354 | static void
354   ambHessian(                             /* anisotropic radii & pos. gradient */
355          AMBHEMI *hp,
356          FVECT   uv[2],                  /* returned */
357 <        float   ra[2],                  /* returned */
358 <        float   pg[2]                   /* returned */
357 >        float   ra[2],                  /* returned (optional) */
358 >        float   pg[2]                   /* returned (optional) */
359   )
360   {
361          static char     memerrmsg[] = "out of memory in ambHessian()";
# Line 368 | Line 370 | ambHessian(                            /* anisotropic radii & pos. gradient */
370          VCOPY(uv[1], hp->uy);
371                          /* clock-wise vertex traversal from sample POV */
372          if (ra != NULL) {               /* initialize Hessian row buffer */
373 <                hessrow = (FVECT (*)[3])malloc(sizeof(FVECT)*3*hp->ns);
373 >                hessrow = (FVECT (*)[3])malloc(sizeof(FVECT)*3*(hp->ns-1));
374                  if (hessrow == NULL)
375                          error(SYSTEM, memerrmsg);
376                  memset(hessian, 0, sizeof(hessian));
377          } else if (pg == NULL)          /* bogus call? */
378                  return;
379          if (pg != NULL) {               /* initialize form factor row buffer */
380 <                gradrow = (FVECT *)malloc(sizeof(FVECT)*hp->ns);
380 >                gradrow = (FVECT *)malloc(sizeof(FVECT)*(hp->ns-1));
381                  if (gradrow == NULL)
382                          error(SYSTEM, memerrmsg);
383                  memset(gradient, 0, sizeof(gradient));
# Line 452 | Line 454 | ambHessian(                            /* anisotropic radii & pos. gradient */
454          
455          if (ra != NULL)                 /* extract eigenvectors & radii */
456                  eigenvectors(uv, ra, hessian);
457 <        if (pg != NULL) {               /* project position gradient */
458 <                pg[0] = DOT(gradient, uv[0]);
459 <                pg[1] = DOT(gradient, uv[1]);
457 >        if (pg != NULL) {               /* tangential position gradient/PI */
458 >                pg[0] = DOT(gradient, uv[0]) / PI;
459 >                pg[1] = DOT(gradient, uv[1]) / PI;
460          }
461   }
462  
# Line 464 | Line 466 | static void
466   ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
467   {
468          struct s_ambsamp        *ap;
469 +        double                  dgsum[2];
470          int                     n;
471 +        FVECT                   vd;
472 +        double                  gfact;
473  
474 <        dg[0] = dg[1] = 0;
474 >        dgsum[0] = dgsum[1] = 0.0;      /* sum values times -tan(theta) */
475          for (ap = hp->sa, n = hp->ns*hp->ns; n--; ap++) {
471                FVECT   vd;
472                double  gfact;
476                                          /* use vector for azimuth + 90deg */
477                  VSUB(vd, ap->p, hp->rp->rop);
478 <                                        /* brightness with tangent factor */
479 <                gfact = ap->v[CIEY] / DOT(hp->rp->ron, vd);
480 <                                        /* sine = proj_radius/vd_length */
481 <                dg[0] -= DOT(uv[1], vd) * gfact ;
482 <                dg[1] += DOT(uv[0], vd) * gfact;
478 >                                        /* brightness over cosine factor */
479 >                gfact = colval(ap->v,CIEY) / DOT(hp->rp->ron, vd);
480 >                                        /* -sine = -proj_radius/vd_length */
481 >                dgsum[0] += DOT(uv[1], vd) * gfact;
482 >                dgsum[1] -= DOT(uv[0], vd) * gfact;
483          }
484 +        dg[0] = dgsum[0] / (hp->ns*hp->ns);
485 +        dg[1] = dgsum[1] / (hp->ns*hp->ns);
486   }
487  
488  
# Line 492 | Line 497 | doambient(                             /* compute ambient component */
497          float   dg[2]                   /* returned (optional) */
498   )
499   {
500 +        AMBHEMI                 *hp = inithemi(rcol, r, wt);
501          int                     cnt = 0;
502          FVECT                   my_uv[2];
497        AMBHEMI                 *hp;
503          double                  d, acol[3];
504          struct s_ambsamp        *ap;
505          int                     i, j;
506 <                                        /* initialize */
507 <        if ((hp = inithemi(rcol, r, wt)) == NULL)
506 >                                        /* check/initialize */
507 >        if (hp == NULL)
508                  return(0);
509          if (uv != NULL)
510                  memset(uv, 0, sizeof(FVECT)*2);
# Line 513 | Line 518 | doambient(                             /* compute ambient component */
518          acol[0] = acol[1] = acol[2] = 0.0;
519          for (i = hp->ns; i--; )
520                  for (j = hp->ns; j--; )
521 <                        if (ambsample(hp, i, j)) {
517 <                                ap = &ambsamp(hp,i,j);
521 >                        if ((ap = ambsample(hp, i, j)) != NULL) {
522                                  addcolor(acol, ap->v);
523                                  ++cnt;
524                          }
# Line 523 | Line 527 | doambient(                             /* compute ambient component */
527                  free(hp);
528                  return(0);              /* no valid samples */
529          }
530 <        d = 1.0 / cnt;                  /* final indirect irradiance/PI */
527 <        acol[0] *= d; acol[1] *= d; acol[2] *= d;
528 <        copycolor(rcol, acol);
530 >        copycolor(rcol, acol);          /* final indirect irradiance/PI */
531          if (cnt < hp->ns*hp->ns ||      /* incomplete sampling? */
532                          (ra == NULL) & (pg == NULL) & (dg == NULL)) {
533                  free(hp);
534                  return(-1);             /* no radius or gradient calc. */
535          }
536 <        d = 0.01 * bright(rcol);        /* add in 1% before Hessian comp. */
537 <        if (d < FTINY) d = FTINY;
538 <        ap = hp->sa;                    /* using Y channel from here on... */
536 >        multcolor(acol, hp->acoef);     /* normalize Y values */
537 >        if ((d = bright(acol)) > FTINY)
538 >                d = 1.0/d;
539 >        else
540 >                d = 0.0;
541 >        ap = hp->sa;                    /* relative Y channel from here on... */
542          for (i = hp->ns*hp->ns; i--; ap++)
543 <                colval(ap->v,CIEY) = bright(ap->v) + d;
543 >                colval(ap->v,CIEY) = bright(ap->v)*d + 0.0314;
544  
545          if (uv == NULL)                 /* make sure we have axis pointers */
546                  uv = my_uv;
547                                          /* compute radii & pos. gradient */
548          ambHessian(hp, uv, ra, pg);
549 +
550          if (dg != NULL)                 /* compute direction gradient */
551                  ambdirgrad(hp, uv, dg);
552 <        if (ra != NULL) {               /* adjust/clamp radii */
553 <                d = sqrt(sqrt((4.0/PI)*bright(rcol)/wt));
554 <                if ((ra[0] *= d) > maxarad)
555 <                        ra[0] = maxarad;
552 >
553 >        if (ra != NULL) {               /* scale/clamp radii */
554 >                if (ra[0] < minarad) {
555 >                        ra[0] = minarad;
556 >                        if (ra[1] < minarad)
557 >                                ra[1] = minarad;
558 >                }
559 >                ra[0] *= d = 1.0/sqrt(sqrt(wt));
560                  if ((ra[1] *= d) > 2.0*ra[0])
561                          ra[1] = 2.0*ra[0];
562 +                if (ra[1] > maxarad) {
563 +                        ra[1] = maxarad;
564 +                        if (ra[0] > maxarad)
565 +                                ra[0] = maxarad;
566 +                }
567          }
568          free(hp);                       /* clean up and return */
569          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines