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.46 by greg, Fri May 2 21:58:50 2014 UTC vs.
Revision 2.54 by greg, Fri May 9 04:55:19 2014 UTC

# Line 50 | Line 50 | static const int  adjacent_trifl[8] = {
50  
51   typedef struct {
52          COLOR   v;              /* hemisphere sample value */
53 +        float   d;              /* reciprocal distance (1/rt) */
54          FVECT   p;              /* intersection point */
55   } AMBSAMP;              /* sample value */
56  
# Line 104 | 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 207 | Line 208 | ambsample(                             /* initial ambient division sample */
208          AMBSAMP *ap = &ambsam(hp,i,j);
209          RAY     ar;
210                                          /* generate hemispherical sample */
211 <        if (!getambsamp(&ar, hp, i, j, 0))
212 <                goto badsample;
213 <                                        /* limit vertex distance */
211 >        if (!getambsamp(&ar, hp, i, j, 0) || ar.rt <= FTINY) {
212 >                memset(ap, 0, sizeof(AMBSAMP));
213 >                return(NULL);
214 >        }
215 >        ap->d = 1.0/ar.rt;              /* limit vertex distance */
216          if (ar.rt > 10.0*thescene.cusize)
217                  ar.rt = 10.0*thescene.cusize;
215        else if (ar.rt <= FTINY)        /* should never happen! */
216                goto badsample;
218          VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
219          copycolor(ap->v, ar.rcol);
220          return(ap);
220 badsample:
221        setcolor(ap->v, 0., 0., 0.);
222        VCOPY(ap->p, hp->rp->rop);
223        return(NULL);
221   }
222  
223  
# Line 228 | Line 225 | badsample:
225   static float *
226   getambdiffs(AMBHEMI *hp)
227   {
228 <        float   *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float));
228 >        float   *earr = (float *)malloc(sizeof(float)*hp->ns*hp->ns);
229          float   *ep;
230          AMBSAMP *ap;
231          double  b, d2;
# Line 239 | Line 236 | getambdiffs(AMBHEMI *hp)
236                                          /* compute squared neighbor diffs */
237          for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++)
238              for (j = 0; j < hp->ns; j++, ap++, ep++) {
239 +                ep[0] = FTINY;
240                  b = bright(ap[0].v);
241                  if (i) {                /* from above */
242                          d2 = b - bright(ap[-hp->ns].v);
# Line 275 | Line 273 | static void
273   ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
274   {
275          float   *earr = getambdiffs(hp);
276 <        double  e2sum = 0;
276 >        double  e2rem = 0;
277          AMBSAMP *ap;
278          RAY     ar;
279 <        COLOR   asum;
279 >        double  asum[3];
280          float   *ep;
281          int     i, j, n;
282  
283          if (earr == NULL)               /* just skip calc. if no memory */
284                  return;
285 <                                        /* add up estimated variances */
285 >                                        /* accumulate estimated variances */
286          for (ep = earr + hp->ns*hp->ns; ep-- > earr; )
287 <                e2sum += *ep;
287 >                e2rem += *ep;
288          ep = earr;                      /* perform super-sampling */
289          for (ap = hp->sa, i = 0; i < hp->ns; i++)
290              for (j = 0; j < hp->ns; j++, ap++) {
291 <                int     nss = *ep/e2sum*cnt + frandom();
292 <                setcolor(asum, 0., 0., 0.);
291 >                int     nss = *ep/e2rem*cnt + frandom();
292 >                asum[0] = asum[1] = asum[2] = 0.0;
293                  for (n = 1; n <= nss; n++) {
294                          if (!getambsamp(&ar, hp, i, j, n)) {
295                                  nss = n-1;
# Line 300 | Line 298 | ambsupersamp(double acol[3], AMBHEMI *hp, int cnt)
298                          addcolor(asum, ar.rcol);
299                  }
300                  if (nss) {              /* update returned ambient value */
301 <                        const double    ssf = 1./(nss + 1);
301 >                        const double    ssf = 1./(nss + 1.);
302                          for (n = 3; n--; )
303 <                                acol[n] += ssf*colval(asum,n) +
303 >                                acol[n] += ssf*asum[n] +
304                                                  (ssf - 1.)*colval(ap->v,n);
305                  }
306 <                e2sum -= *ep++;         /* update remainders */
306 >                e2rem -= *ep++;         /* update remainders */
307                  cnt -= nss;
308          }
309          free(earr);
# Line 317 | Line 315 | static uby8 *
315   vertex_flags(AMBHEMI *hp)
316   {
317          uby8    *vflags = (uby8 *)calloc(hp->ns*hp->ns, sizeof(uby8));
320        double  *dist2a = (double *)malloc(sizeof(double)*hp->ns);
318          uby8    *vf;
319 +        AMBSAMP *ap;
320          int     i, j;
321  
322 <        if ((vflags == NULL) | (dist2a == NULL))
322 >        if (vflags == NULL)
323                  error(SYSTEM, "out of memory in vertex_flags()");
324 <        vf = vflags;            /* compute distances along first row */
325 <        for (j = 0; j < hp->ns; j++) {
326 <                dist2a[j] = dist2(ambsam(hp,0,j).p, hp->rp->rop);
327 <                ++vf;
328 <                if (!j) continue;
331 <                if (dist2a[j] >= dist2a[j-1])
332 <                        vf[0] |= 1<<VDB_x;
324 >        vf = vflags;
325 >        ap = hp->sa;            /* compute farthest along first row */
326 >        for (j = 0; j < hp->ns-1; j++, vf++, ap++)
327 >                if (ap[0].d <= ap[1].d)
328 >                        vf[0] |= 1<<VDB_X;
329                  else
330 <                        vf[-1] |= 1<<VDB_X;
331 <        }
330 >                        vf[1] |= 1<<VDB_x;
331 >        ++vf; ++ap;
332                                  /* flag subsequent rows */
333          for (i = 1; i < hp->ns; i++) {
334 <            double      d2n = dist2(ambsam(hp,i,0).p, hp->rp->rop);
335 <            for (j = 0; j < hp->ns-1; j++) {
340 <                double  d2 = d2n;
341 <                if (d2 >= dist2a[j])    /* row before */
334 >            for (j = 0; j < hp->ns-1; j++, vf++, ap++) {
335 >                if (ap[0].d <= ap[-hp->ns].d)   /* row before */
336                          vf[0] |= 1<<VDB_y;
337                  else
338                          vf[-hp->ns] |= 1<<VDB_Y;
339 <                dist2a[j] = d2n;
346 <                if (d2 >= dist2a[j+1])  /* diagonal we care about */
339 >                if (ap[0].d <= ap[1-hp->ns].d)  /* diagonal we care about */
340                          vf[0] |= 1<<VDB_Xy;
341                  else
342                          vf[1-hp->ns] |= 1<<VDB_xY;
343 <                d2n = dist2(ambsam(hp,i,j+1).p, hp->rp->rop);
351 <                if (d2 >= d2n)          /* column after */
343 >                if (ap[0].d <= ap[1].d)         /* column after */
344                          vf[0] |= 1<<VDB_X;
345                  else
346                          vf[1] |= 1<<VDB_x;
355                ++vf;
347              }
348 <            if (d2n >= dist2a[j])       /* final column edge */
348 >            if (ap[0].d <= ap[-hp->ns].d)       /* final column edge */
349                  vf[0] |= 1<<VDB_y;
350              else
351                  vf[-hp->ns] |= 1<<VDB_Y;
352 <            dist2a[j] = d2n;
362 <            ++vf;
352 >            ++vf; ++ap;
353          }
364        free(dist2a);
354          return(vflags);
355   }
356  
# Line 549 | Line 538 | add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, F
538  
539  
540   /* Compute anisotropic radii and eigenvector directions */
541 < static int
541 > static void
542   eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3])
543   {
544          double  hess2[2][2];
# Line 571 | Line 560 | eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3
560          if (i == 1)                     /* double-root (circle) */
561                  evalue[1] = evalue[0];
562          if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) |
563 <                        ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) )
564 <                error(INTERNAL, "bad eigenvalue calculation");
565 <
563 >                        ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) {
564 >                ra[0] = ra[1] = maxarad;
565 >                return;
566 >        }
567          if (evalue[0] > evalue[1]) {
568                  ra[0] = sqrt(sqrt(4.0/evalue[0]));
569                  ra[1] = sqrt(sqrt(4.0/evalue[1]));
# Line 730 | Line 720 | ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
720   }
721  
722  
723 + /* Compute potential light leak direction flags for cache value */
724 + static uint32
725 + ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1)
726 + {
727 +        const double    max_d = 1.0/(minarad*ambacc + 0.001);
728 +        const double    ang_res = 0.5*PI/(hp->ns-1);
729 +        const double    ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY));
730 +        double          avg_d = 0;
731 +        uint32          flgs = 0;
732 +        int             i, j;
733 +                                        /* don't bother for a few samples */
734 +        if (hp->ns < 12)
735 +                return(0);
736 +                                        /* check distances overhead */
737 +        for (i = hp->ns*3/4; i-- > hp->ns>>2; )
738 +            for (j = hp->ns*3/4; j-- > hp->ns>>2; )
739 +                avg_d += ambsam(hp,i,j).d;
740 +        avg_d *= 4.0/(hp->ns*hp->ns);
741 +        if (avg_d*r0 >= 1.0)            /* ceiling too low for corral? */
742 +                return(0);
743 +        if (avg_d >= max_d)             /* insurance */
744 +                return(0);
745 +                                        /* else circle around perimeter */
746 +        for (i = 0; i < hp->ns; i++)
747 +            for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) {
748 +                AMBSAMP *ap = &ambsam(hp,i,j);
749 +                FVECT   vec;
750 +                double  u, v;
751 +                double  ang, a1;
752 +                int     abp;
753 +                if ((ap->d <= FTINY) | (ap->d >= max_d))
754 +                        continue;       /* too far or too near */
755 +                VSUB(vec, ap->p, hp->rp->rop);
756 +                u = DOT(vec, uv[0]) * ap->d;
757 +                v = DOT(vec, uv[1]) * ap->d;
758 +                if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0)
759 +                        continue;       /* occluder outside ellipse */
760 +                ang = atan2a(v, u);     /* else set direction flags */
761 +                for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step)
762 +                        flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0)));
763 +            }
764 +        return(flgs);
765 + }
766 +
767 +
768   int
769   doambient(                              /* compute ambient component */
770          COLOR   rcol,                   /* input/output color */
# Line 738 | Line 773 | doambient(                             /* compute ambient component */
773          FVECT   uv[2],                  /* returned (optional) */
774          float   ra[2],                  /* returned (optional) */
775          float   pg[2],                  /* returned (optional) */
776 <        float   dg[2]                   /* returned (optional) */
776 >        float   dg[2],                  /* returned (optional) */
777 >        uint32  *crlp                   /* returned (optional) */
778   )
779   {
780          AMBHEMI *hp = inithemi(rcol, r, wt);
# Line 758 | Line 794 | doambient(                             /* compute ambient component */
794                  pg[0] = pg[1] = 0.0;
795          if (dg != NULL)
796                  dg[0] = dg[1] = 0.0;
797 +        if (crlp != NULL)
798 +                *crlp = 0;
799                                          /* sample the hemisphere */
800          acol[0] = acol[1] = acol[2] = 0.0;
801          cnt = 0;
# Line 778 | Line 816 | doambient(                             /* compute ambient component */
816                  return(-1);             /* return value w/o Hessian */
817          }
818          cnt = ambssamp*wt + 0.5;        /* perform super-sampling? */
819 <        if (cnt > 0)
819 >        if (cnt > 8)
820                  ambsupersamp(acol, hp, cnt);
821          copycolor(rcol, acol);          /* final indirect irradiance/PI */
822          if ((ra == NULL) & (pg == NULL) & (dg == NULL)) {
# Line 792 | Line 830 | doambient(                             /* compute ambient component */
830                  K = 1.0;
831                  pg = NULL;
832                  dg = NULL;
833 +                crlp = NULL;
834          }
835          ap = hp->sa;                    /* relative Y channel from here on... */
836          for (i = hp->ns*hp->ns; i--; ap++)
# Line 827 | Line 866 | doambient(                             /* compute ambient component */
866                          if (ra[0] > maxarad)
867                                  ra[0] = maxarad;
868                  }
869 +                                        /* flag encroached directions */
870 +                if ((wt >= 0.5-FTINY) & (crlp != NULL))
871 +                        *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc);
872                  if (pg != NULL) {       /* cap gradient if necessary */
873                          d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1];
874                          if (d > 1.0) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines