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.91 by greg, Fri Nov 17 20:02:07 2023 UTC vs.
Revision 2.96 by greg, Fri Nov 15 20:47:42 2024 UTC

# Line 35 | Line 35 | typedef struct {
35          RAY     *rp;            /* originating ray sample */
36          int     ns;             /* number of samples per axis */
37          int     sampOK;         /* acquired full sample set? */
38 +        int     atyp;           /* RAMBIENT or TAMBIENT */
39          SCOLOR  acoef;          /* division contribution coefficient */
40          SCOLOR  acol;           /* accumulated color */
41 +        FVECT   onrm;           /* oriented unperturbed surface normal */
42          FVECT   ux, uy;         /* tangent axis unit vectors */
43          AMBSAMP sa[1];          /* sample array (extends struct) */
44   }  AMBHEMI;             /* ambient sample hemisphere */
# Line 98 | Line 100 | ambsample(                             /* initial ambient division sample */
100          AMBSAMP *ap = &ambsam(hp,i,j);
101          RAY     ar;
102          int     hlist[3], ii;
103 +        double  ss[2];
104          RREAL   spt[2];
105          double  zd;
106                                          /* generate hemispherical sample */
# Line 106 | Line 109 | ambsample(                             /* initial ambient division sample */
109                  setscolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
110          else
111                  copyscolor(ar.rcoef, hp->acoef);
112 <        if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0)
112 >        if (rayorigin(&ar, hp->atyp, hp->rp, ar.rcoef) < 0)
113                  return(0);
114          if (ambacc > FTINY) {
115 + #ifdef SSKIPOPT
116 +                ar.rsrc = -1;           /* protect cache from source opt. */
117 +                ar.scorr = 1.f;
118 + #endif
119                  smultscolor(ar.rcoef, hp->acoef);
120                  scalescolor(ar.rcoef, 1./AVGREFL);
121          }
122          hlist[0] = hp->rp->rno;
123 <        hlist[1] = j;
124 <        hlist[2] = i;
125 <        multisamp(spt, 2, urand(ilhash(hlist,3)+n));
123 >        hlist[1] = AI(hp,i,j);
124 >        hlist[2] = samplendx;
125 >        multisamp(ss, 2, urand(ilhash(hlist,3)+n));
126   resample:
127 <        square2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns);
127 >        square2disk(spt, (j+ss[1])/hp->ns, (i+ss[0])/hp->ns);
128          zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]);
129          for (ii = 3; ii--; )
130                  ar.rdir[ii] =   spt[0]*hp->ux[ii] +
131                                  spt[1]*hp->uy[ii] +
132 <                                zd*hp->rp->ron[ii];
132 >                                zd*hp->onrm[ii];
133          checknorm(ar.rdir);
134                                          /* avoid coincident samples */
135 <        if (!n && ambcollision(hp, i, j, ar.rdir)) {
136 <                spt[0] = frandom(); spt[1] = frandom();
135 >        if (!n && hp->ns >= 4 && ambcollision(hp, i, j, ar.rdir)) {
136 >                ss[0] = frandom(); ss[1] = frandom();
137                  goto resample;          /* reject this sample */
138          }
139          dimlist[ndims++] = AI(hp,i,j) + 90171;
# Line 160 | Line 167 | resample:
167   static float *
168   getambdiffs(AMBHEMI *hp)
169   {
170 <        const double    normf = 1./bright(hp->acoef);
171 <        float   *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float));
170 >        const double    normf = 1./(pbright(hp->acoef) + FTINY);
171 >        float   *earr = (float *)calloc(2*hp->ns*hp->ns, sizeof(float));
172          float   *ep;
173          AMBSAMP *ap;
174          double  b, b1, d2;
# Line 170 | Line 177 | getambdiffs(AMBHEMI *hp)
177          if (earr == NULL)               /* out of memory? */
178                  return(NULL);
179                                          /* sum squared neighbor diffs */
180 <        for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++)
180 >        ap = hp->sa;
181 >        ep = earr + hp->ns*hp->ns;      /* original estimates to scratch */
182 >        for (i = 0; i < hp->ns; i++)
183              for (j = 0; j < hp->ns; j++, ap++, ep++) {
184                  b = pbright(ap[0].v);
185                  if (i) {                /* from above */
# Line 196 | Line 205 | getambdiffs(AMBHEMI *hp)
205                  ep[-hp->ns-1] += d2;
206              }
207                                          /* correct for number of neighbors */
208 <        earr[0] *= 8./3.;
209 <        earr[hp->ns-1] *= 8./3.;
210 <        earr[(hp->ns-1)*hp->ns] *= 8./3.;
211 <        earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.;
208 >        ep = earr + hp->ns*hp->ns;
209 >        ep[0] *= 6./3.;
210 >        ep[hp->ns-1] *= 6./3.;
211 >        ep[(hp->ns-1)*hp->ns] *= 6./3.;
212 >        ep[(hp->ns-1)*hp->ns + hp->ns-1] *= 6./3.;
213          for (i = 1; i < hp->ns-1; i++) {
214 <                earr[i*hp->ns] *= 8./5.;
215 <                earr[i*hp->ns + hp->ns-1] *= 8./5.;
214 >                ep[i*hp->ns] *= 6./5.;
215 >                ep[i*hp->ns + hp->ns-1] *= 6./5.;
216          }
217          for (j = 1; j < hp->ns-1; j++) {
218 <                earr[j] *= 8./5.;
219 <                earr[(hp->ns-1)*hp->ns + j] *= 8./5.;
218 >                ep[j] *= 6./5.;
219 >                ep[(hp->ns-1)*hp->ns + j] *= 6./5.;
220          }
221 +                                        /* blur final map to reduce bias */
222 +        for (i = 0; i < hp->ns-1; i++) {
223 +            float  *ep2;
224 +            ep = earr + i*hp->ns;
225 +            ep2 = ep + hp->ns*hp->ns;
226 +            for (j = 0; j < hp->ns-1; j++, ep++, ep2++) {
227 +                ep[0] += .5*ep2[0] + .125*(ep2[1] + ep2[hp->ns]);
228 +                ep[1] += .125*ep2[0];
229 +                ep[hp->ns] += .125*ep2[0];
230 +            }
231 +        }
232          return(earr);
233   }
234  
# Line 248 | Line 269 | samp_hemi(                             /* sample indirect hemisphere */
269          double  wt
270   )
271   {
272 +        int     backside = (wt < 0);
273          AMBHEMI *hp;
274          double  d;
275          int     n, i, j;
# Line 256 | Line 278 | samp_hemi(                             /* sample indirect hemisphere */
278          if (d <= FTINY)
279                  return(NULL);
280                                          /* set number of divisions */
281 +        if (backside) wt = -wt;
282          if (ambacc <= FTINY &&
283 <                        wt > (d *= 0.8*r->rweight/(ambdiv*minweight)))
283 >                        wt > (d *= 0.8*r->rweight/(ambdiv*minweight + 1e-20)))
284                  wt = d;                 /* avoid ray termination */
285          n = sqrt(ambdiv * wt) + 0.5;
286          i = 1 + (MINADIV-1)*(ambacc > FTINY);
# Line 267 | Line 290 | samp_hemi(                             /* sample indirect hemisphere */
290          hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
291          if (hp == NULL)
292                  error(SYSTEM, "out of memory in samp_hemi");
293 +
294 +        if (backside) {
295 +                hp->atyp = TAMBIENT;
296 +                hp->onrm[0] = -r->ron[0];
297 +                hp->onrm[1] = -r->ron[1];
298 +                hp->onrm[2] = -r->ron[2];
299 +        } else {
300 +                hp->atyp = RAMBIENT;
301 +                VCOPY(hp->onrm, r->ron);
302 +        }
303          hp->rp = r;
304          hp->ns = n;
305          scolorblack(hp->acol);
# Line 277 | Line 310 | samp_hemi(                             /* sample indirect hemisphere */
310          d = 1.0/(n*n);
311          scalescolor(hp->acoef, d);
312                                          /* make tangent plane axes */
313 <        if (!getperpendicular(hp->ux, r->ron, 1))
313 >        if (!getperpendicular(hp->ux, hp->onrm, 1))
314                  error(CONSISTENCY, "bad ray direction in samp_hemi");
315 <        VCROSS(hp->uy, r->ron, hp->ux);
315 >        VCROSS(hp->uy, hp->onrm, hp->ux);
316                                          /* sample divisions */
317          for (i = hp->ns; i--; )
318              for (j = hp->ns; j--; )
# Line 296 | Line 329 | samp_hemi(                             /* sample indirect hemisphere */
329          if (hp->sampOK <= MINADIV*MINADIV)
330                  return(hp);             /* don't bother super-sampling */
331          n = ambssamp*wt + 0.5;
332 <        if (n > 8) {                    /* perform super-sampling? */
332 >        if (n >= 4*hp->ns) {            /* perform super-sampling? */
333                  ambsupersamp(hp, n);
334                  copyscolor(rcol, hp->acol);
335          }
# Line 544 | Line 577 | ambHessian(                            /* anisotropic radii & pos. gradient */
577          for (j = 0; j < hp->ns-1; j++) {
578                  comp_fftri(&fftr, hp, AI(hp,0,j), AI(hp,0,j+1));
579                  if (hessrow != NULL)
580 <                        comp_hessian(hessrow[j], &fftr, hp->rp->ron);
580 >                        comp_hessian(hessrow[j], &fftr, hp->onrm);
581                  if (gradrow != NULL)
582 <                        comp_gradient(gradrow[j], &fftr, hp->rp->ron);
582 >                        comp_gradient(gradrow[j], &fftr, hp->onrm);
583          }
584                                          /* sum each row of triangles */
585          for (i = 0; i < hp->ns-1; i++) {
# Line 554 | Line 587 | ambHessian(                            /* anisotropic radii & pos. gradient */
587              FVECT       gradcol;
588              comp_fftri(&fftr, hp, AI(hp,i,0), AI(hp,i+1,0));
589              if (hessrow != NULL)
590 <                comp_hessian(hesscol, &fftr, hp->rp->ron);
590 >                comp_hessian(hesscol, &fftr, hp->onrm);
591              if (gradrow != NULL)
592 <                comp_gradient(gradcol, &fftr, hp->rp->ron);
592 >                comp_gradient(gradcol, &fftr, hp->onrm);
593              for (j = 0; j < hp->ns-1; j++) {
594                  FVECT   hessdia[3];     /* compute triangle contributions */
595                  FVECT   graddia;
# Line 566 | Line 599 | ambHessian(                            /* anisotropic radii & pos. gradient */
599                                          /* diagonal (inner) edge */
600                  comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j));
601                  if (hessrow != NULL) {
602 <                    comp_hessian(hessdia, &fftr, hp->rp->ron);
602 >                    comp_hessian(hessdia, &fftr, hp->onrm);
603                      rev_hessian(hesscol);
604                      add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
605                  }
606                  if (gradrow != NULL) {
607 <                    comp_gradient(graddia, &fftr, hp->rp->ron);
607 >                    comp_gradient(graddia, &fftr, hp->onrm);
608                      rev_gradient(gradcol);
609                      add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
610                  }
611                                          /* initialize edge in next row */
612                  comp_fftri(&fftr, hp, AI(hp,i+1,j+1), AI(hp,i+1,j));
613                  if (hessrow != NULL)
614 <                    comp_hessian(hessrow[j], &fftr, hp->rp->ron);
614 >                    comp_hessian(hessrow[j], &fftr, hp->onrm);
615                  if (gradrow != NULL)
616 <                    comp_gradient(gradrow[j], &fftr, hp->rp->ron);
616 >                    comp_gradient(gradrow[j], &fftr, hp->onrm);
617                                          /* new column edge & paired triangle */
618                  backg = back_ambval(hp, AI(hp,i+1,j+1),
619                                          AI(hp,i+1,j), AI(hp,i,j+1));
620                  comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j+1));
621                  if (hessrow != NULL) {
622 <                    comp_hessian(hesscol, &fftr, hp->rp->ron);
622 >                    comp_hessian(hesscol, &fftr, hp->onrm);
623                      rev_hessian(hessdia);
624                      add2hessian(hessian, hessrow[j], hessdia, hesscol, backg);
625                      if (i < hp->ns-2)
626                          rev_hessian(hessrow[j]);
627                  }
628                  if (gradrow != NULL) {
629 <                    comp_gradient(gradcol, &fftr, hp->rp->ron);
629 >                    comp_gradient(gradcol, &fftr, hp->onrm);
630                      rev_gradient(graddia);
631                      add2gradient(gradient, gradrow[j], graddia, gradcol, backg);
632                      if (i < hp->ns-2)
# Line 629 | Line 662 | ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2])
662                                          /* use vector for azimuth + 90deg */
663                  VSUB(vd, ap->p, hp->rp->rop);
664                                          /* brightness over cosine factor */
665 <                gfact = ap->v[0] / DOT(hp->rp->ron, vd);
665 >                gfact = ap->v[0] / DOT(hp->onrm, vd);
666                                          /* sine = proj_radius/vd_length */
667                  dgsum[0] -= DOT(uv[1], vd) * gfact;
668                  dgsum[1] += DOT(uv[0], vd) * gfact;
# Line 687 | Line 720 | int
720   doambient(                              /* compute ambient component */
721          SCOLOR  rcol,                   /* input/output color */
722          RAY     *r,
723 <        double  wt,
723 >        double  wt,                     /* negative for back side */
724          FVECT   uv[2],                  /* returned (optional) */
725          float   ra[2],                  /* returned (optional) */
726          float   pg[2],                  /* returned (optional) */
# Line 754 | Line 787 | doambient(                             /* compute ambient component */
787                          if (ra[1] < minarad)
788                                  ra[1] = minarad;
789                  }
790 <                ra[0] *= d = 1.0/sqrt(wt);
790 >                ra[0] *= d = 1.0/sqrt(fabs(wt));
791                  if ((ra[1] *= d) > 2.0*ra[0])
792                          ra[1] = 2.0*ra[0];
793                  if (ra[1] > maxarad) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines