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.49 by greg, Wed May 7 01:16:02 2014 UTC vs.
Revision 2.55 by greg, Fri May 9 16:05:09 2014 UTC

# Line 105 | Line 105 | vdb_edge(int db1, int db2)
105          case VDB_xY:    return(db2==VDB_x ? VDB_y : VDB_X);
106          case VDB_Xy:    return(db2==VDB_y ? VDB_x : VDB_Y);
107          }
108 <        error(INTERNAL, "forbidden diagonal in vdb_edge()");
108 >        error(CONSISTENCY, "forbidden diagonal in vdb_edge()");
109          return(-1);
110   }
111  
# Line 243 | Line 243 | getambdiffs(AMBHEMI *hp)
243                          ep[0] += d2;
244                          ep[-hp->ns] += d2;
245                  }
246 <                if (j) {                /* from behind */
247 <                        d2 = b - bright(ap[-1].v);
248 <                        d2 *= d2;
249 <                        ep[0] += d2;
250 <                        ep[-1] += d2;
251 <                }
246 >                if (!j) continue;
247 >                                        /* from behind */
248 >                d2 = b - bright(ap[-1].v);
249 >                d2 *= d2;
250 >                ep[0] += d2;
251 >                ep[-1] += d2;
252 >                if (!i) continue;
253 >                                        /* diagonal */
254 >                d2 = b - bright(ap[-hp->ns-1].v);
255 >                d2 *= d2;
256 >                ep[0] += d2;
257 >                ep[-hp->ns-1] += d2;
258              }
259                                          /* correct for number of neighbors */
260 <        earr[0] *= 2.f;
261 <        earr[hp->ns-1] *= 2.f;
262 <        earr[(hp->ns-1)*hp->ns] *= 2.f;
263 <        earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 2.f;
260 >        earr[0] *= 8./3.;
261 >        earr[hp->ns-1] *= 8./3.;
262 >        earr[(hp->ns-1)*hp->ns] *= 8./3.;
263 >        earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.;
264          for (i = 1; i < hp->ns-1; i++) {
265 <                earr[i*hp->ns] *= 4./3.;
266 <                earr[i*hp->ns + hp->ns-1] *= 4./3.;
265 >                earr[i*hp->ns] *= 8./5.;
266 >                earr[i*hp->ns + hp->ns-1] *= 8./5.;
267          }
268          for (j = 1; j < hp->ns-1; j++) {
269 <                earr[j] *= 4./3.;
270 <                earr[(hp->ns-1)*hp->ns + j] *= 4./3.;
269 >                earr[j] *= 8./5.;
270 >                earr[(hp->ns-1)*hp->ns + j] *= 8./5.;
271          }
272          return(earr);
273   }
# Line 272 | Line 278 | static void
278   ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
279   {
280          float   *earr = getambdiffs(hp);
281 <        double  e2sum = 0;
281 >        double  e2rem = 0;
282          AMBSAMP *ap;
283          RAY     ar;
284 <        COLOR   asum;
284 >        double  asum[3];
285          float   *ep;
286 <        int     i, j, n;
286 >        int     i, j, n, nss;
287  
288          if (earr == NULL)               /* just skip calc. if no memory */
289                  return;
290 <                                        /* add up estimated variances */
291 <        for (ep = earr + hp->ns*hp->ns; ep-- > earr; )
292 <                e2sum += *ep;
290 >                                        /* accumulate estimated variances */
291 >        for (ep = earr + hp->ns*hp->ns; ep > earr; )
292 >                e2rem += *--ep;
293          ep = earr;                      /* perform super-sampling */
294          for (ap = hp->sa, i = 0; i < hp->ns; i++)
295              for (j = 0; j < hp->ns; j++, ap++) {
296 <                int     nss = *ep/e2sum*cnt + frandom();
297 <                setcolor(asum, 0., 0., 0.);
296 >                if (e2rem <= FTINY)
297 >                        goto done;      /* nothing left to do */
298 >                nss = *ep/e2rem*cnt + frandom();
299 >                asum[0] = asum[1] = asum[2] = 0.0;
300                  for (n = 1; n <= nss; n++) {
301                          if (!getambsamp(&ar, hp, i, j, n)) {
302                                  nss = n-1;
# Line 297 | Line 305 | ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
305                          addcolor(asum, ar.rcol);
306                  }
307                  if (nss) {              /* update returned ambient value */
308 <                        const double    ssf = 1./(nss + 1);
308 >                        const double    ssf = 1./(nss + 1.);
309                          for (n = 3; n--; )
310 <                                acol[n] += ssf*colval(asum,n) +
310 >                                acol[n] += ssf*asum[n] +
311                                                  (ssf - 1.)*colval(ap->v,n);
312                  }
313 <                e2sum -= *ep++;         /* update remainders */
313 >                e2rem -= *ep++;         /* update remainders */
314                  cnt -= nss;
315          }
316 + done:
317          free(earr);
318   }
319  
# Line 537 | Line 546 | add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, F
546  
547  
548   /* Compute anisotropic radii and eigenvector directions */
549 < static int
549 > static void
550   eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3])
551   {
552          double  hess2[2][2];
# Line 559 | Line 568 | eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3
568          if (i == 1)                     /* double-root (circle) */
569                  evalue[1] = evalue[0];
570          if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) |
571 <                        ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) )
572 <                error(INTERNAL, "bad eigenvalue calculation");
573 <
571 >                        ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) {
572 >                ra[0] = ra[1] = maxarad;
573 >                return;
574 >        }
575          if (evalue[0] > evalue[1]) {
576                  ra[0] = sqrt(sqrt(4.0/evalue[0]));
577                  ra[1] = sqrt(sqrt(4.0/evalue[1]));
# Line 722 | Line 732 | ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
732   static uint32
733   ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
734   {
735 <        uint32  flgs = 0;
736 <        int     i, j;
737 <                                        /* circle around perimeter */
735 >        const double    max_d = 1.0/(minarad*ambacc + 0.001);
736 >        const double    ang_res = 0.5*PI/(hp->ns-1);
737 >        const double    ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY));
738 >        double          avg_d = 0;
739 >        uint32          flgs = 0;
740 >        int             i, j;
741 >                                        /* don't bother for a few samples */
742 >        if (hp->ns < 12)
743 >                return(0);
744 >                                        /* check distances overhead */
745 >        for (i = hp->ns*3/4; i-- > hp->ns>>2; )
746 >            for (j = hp->ns*3/4; j-- > hp->ns>>2; )
747 >                avg_d += ambsam(hp,i,j).d;
748 >        avg_d *= 4.0/(hp->ns*hp->ns);
749 >        if (avg_d*r0 >= 1.0)            /* ceiling too low for corral? */
750 >                return(0);
751 >        if (avg_d >= max_d)             /* insurance */
752 >                return(0);
753 >                                        /* else circle around perimeter */
754          for (i = 0; i < hp->ns; i++)
755              for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) {
756                  AMBSAMP *ap = &ambsam(hp,i,j);
757                  FVECT   vec;
758                  double  u, v;
759 <                double  ang;
759 >                double  ang, a1;
760                  int     abp;
761 <                if (ap->d <= FTINY)
762 <                        continue;
761 >                if ((ap->d <= FTINY) | (ap->d >= max_d))
762 >                        continue;       /* too far or too near */
763                  VSUB(vec, ap->p, hp->rp->rop);
764                  u = DOT(vec, uv[0]) * ap->d;
765                  v = DOT(vec, uv[1]) * ap->d;
766                  if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0)
767                          continue;       /* occluder outside ellipse */
768                  ang = atan2a(v, u);     /* else set direction flags */
769 <                ang += 2.0*PI*(ang < 0);
770 <                ang *= 16./PI;
745 <                if ((ang < .5) | (ang >= 31.5))
746 <                        flgs |= 0x80000001;
747 <                else
748 <                        flgs |= 3L<<(int)(ang-.5);
769 >                for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step)
770 >                        flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0)));
771              }
772          return(flgs);
773   }
# Line 802 | Line 824 | doambient(                             /* compute ambient component */
824                  return(-1);             /* return value w/o Hessian */
825          }
826          cnt = ambssamp*wt + 0.5;        /* perform super-sampling? */
827 <        if (cnt > 0)
827 >        if (cnt > 8)
828                  ambsupersamp(acol, hp, cnt);
829          copycolor(rcol, acol);          /* final indirect irradiance/PI */
830          if ((ra == NULL) & (pg == NULL) & (dg == NULL)) {
# Line 816 | Line 838 | doambient(                             /* compute ambient component */
838                  K = 1.0;
839                  pg = NULL;
840                  dg = NULL;
841 +                crlp = NULL;
842          }
843          ap = hp->sa;                    /* relative Y channel from here on... */
844          for (i = hp->ns*hp->ns; i--; ap++)
# Line 851 | Line 874 | doambient(                             /* compute ambient component */
874                          if (ra[0] > maxarad)
875                                  ra[0] = maxarad;
876                  }
877 <                if (crlp != NULL)       /* flag encroached directions */
877 >                                        /* flag encroached directions */
878 >                if ((wt >= 0.5-FTINY) & (crlp != NULL))
879                          *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc);
880                  if (pg != NULL) {       /* cap gradient if necessary */
881                          d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines