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

# Line 23 | Line 23 | static const char      RCSid[] = "$Id$";
23  
24   #ifdef NEWAMB
25  
26 /* #define AHEM_MARG    1.2     /* hem margin */
27
26   extern void             SDsquare2disk(double ds[2], double seedx, double seedy);
27  
28                                  /* vertex direction bit positions */
# Line 107 | 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 245 | 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 274 | 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 299 | 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 539 | 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 561 | 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 720 | Line 728 | ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
728   }
729  
730  
731 < /* Make sure radii don't extend beyond what we see in our periphery */
732 < static int
733 < hem_radii(AMBHEMI *hp, FVECT uv[2], float ra[2])
731 > /* Compute potential light leak direction flags for cache value */
732 > static uint32
733 > ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
734   {
735 < #ifdef AHEM_MARG
736 < #define MAXDACCUM       47
737 <        const double    hemarg = AHEM_MARG*ambacc;      /* hem margin */
738 <        float           radivisor2[MAXDACCUM+1];
739 <        int             i, j, k = hp->ns/10 + 1;        /* around 5%ile */
740 <        const int       n2accum = (k < MAXDACCUM) ? k : MAXDACCUM ;
741 <        int             na = 0;
742 <        double          d;
743 <                                        /* 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);
739                double  radiv2 = 0;
757                  FVECT   vec;
758 <                if (ap->d <= FTINY)
759 <                        continue;
758 >                double  u, v;
759 >                double  ang, a1;
760 >                int     abp;
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 <                for (k = 2; k--; ) {
765 <                        d = ap->d * DOT(vec, uv[k]) * ra[k];
766 <                        radiv2 += d*d;
767 <                }
768 <                radiv2 *= hemarg*hemarg * ap->d * ap->d;
769 <                if (radiv2 <= 1.0)
770 <                        continue;
751 <                                        /* insert in percentile list */
752 <                for (k = na; k && radiv2 > radivisor2[k-1]; k--)
753 <                        radivisor2[k] = radivisor2[k-1];
754 <                radivisor2[k] = radiv2;
755 <                na += (na < n2accum);
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 >                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 <        if (na < n2accum)               /* current radii are OK? */
758 <                return(0);
759 <                                        /* else apply divisor */
760 <        d = 1.0/sqrt(radivisor2[na-1]);
761 <        ra[0] *= d;
762 <        ra[1] *= d;
763 <        return(1);
764 < #undef MAXDACCUM
765 < #else
766 <        return(0);
767 < #endif
772 >        return(flgs);
773   }
774  
775  
# Line 776 | Line 781 | doambient(                             /* compute ambient component */
781          FVECT   uv[2],                  /* returned (optional) */
782          float   ra[2],                  /* returned (optional) */
783          float   pg[2],                  /* returned (optional) */
784 <        float   dg[2]                   /* returned (optional) */
784 >        float   dg[2],                  /* returned (optional) */
785 >        uint32  *crlp                   /* returned (optional) */
786   )
787   {
788          AMBHEMI *hp = inithemi(rcol, r, wt);
# Line 796 | Line 802 | doambient(                             /* compute ambient component */
802                  pg[0] = pg[1] = 0.0;
803          if (dg != NULL)
804                  dg[0] = dg[1] = 0.0;
805 +        if (crlp != NULL)
806 +                *crlp = 0;
807                                          /* sample the hemisphere */
808          acol[0] = acol[1] = acol[2] = 0.0;
809          cnt = 0;
# Line 816 | 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 830 | 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 852 | Line 861 | doambient(                             /* compute ambient component */
861                          if (ra[0] > ra[1])
862                                  ra[0] = ra[1];
863                  }
855                hem_radii(hp, uv, ra);
864                  if (ra[0] < minarad) {
865                          ra[0] = minarad;
866                          if (ra[1] < minarad)
# Line 866 | Line 874 | doambient(                             /* compute ambient component */
874                          if (ra[0] > maxarad)
875                                  ra[0] = maxarad;
876                  }
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];
882                          if (d > 1.0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines