--- ray/src/rt/ambcomp.c 2014/04/23 17:30:10 2.30 +++ ray/src/rt/ambcomp.c 2014/10/23 18:19:14 2.68 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.30 2014/04/23 17:30:10 greg Exp $"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.68 2014/10/23 18:19:14 greg Exp $"; #endif /* * Routines to compute "ambient" values using Monte Carlo @@ -8,6 +8,10 @@ static const char RCSid[] = "$Id: ambcomp.c,v 2.30 201 * for Irradiance Caching" by Schwarzhaupt, Wann Jensen, & Jarosz * from ACM SIGGRAPH Asia 2012 conference proceedings. * + * Added book-keeping optimization to avoid calculations that would + * cancel due to traversal both directions on edges that are adjacent + * to same-valued triangles. This cuts about half of Hessian math. + * * Declarations of external symbols in ambient.h */ @@ -17,147 +21,292 @@ static const char RCSid[] = "$Id: ambcomp.c,v 2.30 201 #include "ambient.h" #include "random.h" -#ifdef NEWAMB +#ifndef OLDAMB extern void SDsquare2disk(double ds[2], double seedx, double seedy); typedef struct { + COLOR v; /* hemisphere sample value */ + float d; /* reciprocal distance (1/rt) */ + FVECT p; /* intersection point */ +} AMBSAMP; /* sample value */ + +typedef struct { RAY *rp; /* originating ray sample */ - FVECT ux, uy; /* tangent axis unit vectors */ int ns; /* number of samples per axis */ + int sampOK; /* acquired full sample set? */ COLOR acoef; /* division contribution coefficient */ - struct s_ambsamp { - COLOR v; /* hemisphere sample value */ - float p[3]; /* intersection point */ - } sa[1]; /* sample array (extends struct) */ + double acol[3]; /* accumulated color */ + FVECT ux, uy; /* tangent axis unit vectors */ + AMBSAMP sa[1]; /* sample array (extends struct) */ } AMBHEMI; /* ambient sample hemisphere */ -#define ambsamp(h,i,j) (h)->sa[(i)*(h)->ns + (j)] +#define AI(h,i,j) ((i)*(h)->ns + (j)) +#define ambsam(h,i,j) (h)->sa[AI(h,i,j)] typedef struct { - FVECT r_i, r_i1, e_i, rI2_eJ2; - double nf, I1, I2; + FVECT r_i, r_i1, e_i, rcp, rI2_eJ2; + double I1, I2; } FFTRI; /* vectors and coefficients for Hessian calculation */ +static int +ambsample( /* initial ambient division sample */ + AMBHEMI *hp, + int i, + int j, + int n +) +{ + AMBSAMP *ap = &ambsam(hp,i,j); + RAY ar; + int hlist[3], ii; + double spt[2], zd; + /* generate hemispherical sample */ + /* ambient coefficient for weight */ + if (ambacc > FTINY) + setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); + else + copycolor(ar.rcoef, hp->acoef); + if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) + return(0); + if (ambacc > FTINY) { + multcolor(ar.rcoef, hp->acoef); + scalecolor(ar.rcoef, 1./AVGREFL); + } + hlist[0] = hp->rp->rno; + hlist[1] = j; + hlist[2] = i; + multisamp(spt, 2, urand(ilhash(hlist,3)+n)); + /* avoid coincident samples */ + if (!n && (0 < i) & (i < hp->ns-1) && + (0 < j) & (j < hp->ns-1)) { + if ((spt[0] < 0.1) | (spt[0] >= 0.9)) + spt[0] = 0.1 + 0.8*frandom(); + if ((spt[1] < 0.1) | (spt[1] >= 0.9)) + spt[1] = 0.1 + 0.8*frandom(); + } + SDsquare2disk(spt, (j+spt[1])/hp->ns, (i+spt[0])/hp->ns); + zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]); + for (ii = 3; ii--; ) + ar.rdir[ii] = spt[0]*hp->ux[ii] + + spt[1]*hp->uy[ii] + + zd*hp->rp->ron[ii]; + checknorm(ar.rdir); + dimlist[ndims++] = AI(hp,i,j) + 90171; + rayvalue(&ar); /* evaluate ray */ + ndims--; + if (ar.rt <= FTINY) + return(0); /* should never happen */ + multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ + if (ar.rt*ap->d < 1.0) /* new/closer distance? */ + ap->d = 1.0/ar.rt; + if (!n) { /* record first vertex & value */ + if (ar.rt > 10.0*thescene.cusize) + ar.rt = 10.0*thescene.cusize; + VSUM(ap->p, ar.rorg, ar.rdir, ar.rt); + copycolor(ap->v, ar.rcol); + } else { /* else update recorded value */ + hp->acol[RED] -= colval(ap->v,RED); + hp->acol[GRN] -= colval(ap->v,GRN); + hp->acol[BLU] -= colval(ap->v,BLU); + zd = 1.0/(double)(n+1); + scalecolor(ar.rcol, zd); + zd *= (double)n; + scalecolor(ap->v, zd); + addcolor(ap->v, ar.rcol); + } + addcolor(hp->acol, ap->v); /* add to our sum */ + return(1); +} + + +/* Estimate errors based on ambient division differences */ +static float * +getambdiffs(AMBHEMI *hp) +{ + float *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float)); + float *ep; + AMBSAMP *ap; + double b, d2; + int i, j; + + if (earr == NULL) /* out of memory? */ + return(NULL); + /* compute squared neighbor diffs */ + for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++) + for (j = 0; j < hp->ns; j++, ap++, ep++) { + b = bright(ap[0].v); + if (i) { /* from above */ + d2 = b - bright(ap[-hp->ns].v); + d2 *= d2; + ep[0] += d2; + ep[-hp->ns] += d2; + } + if (!j) continue; + /* from behind */ + d2 = b - bright(ap[-1].v); + d2 *= d2; + ep[0] += d2; + ep[-1] += d2; + if (!i) continue; + /* diagonal */ + d2 = b - bright(ap[-hp->ns-1].v); + d2 *= d2; + ep[0] += d2; + ep[-hp->ns-1] += d2; + } + /* correct for number of neighbors */ + earr[0] *= 8./3.; + earr[hp->ns-1] *= 8./3.; + earr[(hp->ns-1)*hp->ns] *= 8./3.; + earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.; + for (i = 1; i < hp->ns-1; i++) { + earr[i*hp->ns] *= 8./5.; + earr[i*hp->ns + hp->ns-1] *= 8./5.; + } + for (j = 1; j < hp->ns-1; j++) { + earr[j] *= 8./5.; + earr[(hp->ns-1)*hp->ns + j] *= 8./5.; + } + return(earr); +} + + +/* Perform super-sampling on hemisphere (introduces bias) */ +static void +ambsupersamp(AMBHEMI *hp, int cnt) +{ + float *earr = getambdiffs(hp); + double e2rem = 0; + AMBSAMP *ap; + float *ep; + int i, j, n, nss; + + if (earr == NULL) /* just skip calc. if no memory */ + return; + /* accumulate estimated variances */ + for (ep = earr + hp->ns*hp->ns; ep > earr; ) + e2rem += *--ep; + ep = earr; /* perform super-sampling */ + for (ap = hp->sa, i = 0; i < hp->ns; i++) + for (j = 0; j < hp->ns; j++, ap++) { + if (e2rem <= FTINY) + goto done; /* nothing left to do */ + nss = *ep/e2rem*cnt + frandom(); + for (n = 1; n <= nss && ambsample(hp,i,j,n); n++) + --cnt; + e2rem -= *ep++; /* update remainder */ + } +done: + free(earr); +} + + static AMBHEMI * -inithemi( /* initialize sampling hemisphere */ - COLOR ac, +samp_hemi( /* sample indirect hemisphere */ + COLOR rcol, RAY *r, double wt ) { AMBHEMI *hp; double d; - int n, i; + int n, i, j; /* set number of divisions */ if (ambacc <= FTINY && - wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight))) + wt > (d = 0.8*intens(rcol)*r->rweight/(ambdiv*minweight))) wt = d; /* avoid ray termination */ n = sqrt(ambdiv * wt) + 0.5; i = 1 + 5*(ambacc > FTINY); /* minimum number of samples */ if (n < i) n = i; /* allocate sampling array */ - hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + - sizeof(struct s_ambsamp)*(n*n - 1)); + hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1)); if (hp == NULL) - return(NULL); + error(SYSTEM, "out of memory in samp_hemi"); hp->rp = r; hp->ns = n; + hp->acol[RED] = hp->acol[GRN] = hp->acol[BLU] = 0.0; + memset(hp->sa, 0, sizeof(AMBSAMP)*n*n); + hp->sampOK = 0; /* assign coefficient */ - copycolor(hp->acoef, ac); + copycolor(hp->acoef, rcol); d = 1.0/(n*n); scalecolor(hp->acoef, d); /* make tangent plane axes */ - hp->uy[0] = 0.1 - 0.2*frandom(); - hp->uy[1] = 0.1 - 0.2*frandom(); - hp->uy[2] = 0.1 - 0.2*frandom(); - for (i = 0; i < 3; i++) - if (r->ron[i] < 0.6 && r->ron[i] > -0.6) + hp->uy[0] = 0.5 - frandom(); + hp->uy[1] = 0.5 - frandom(); + hp->uy[2] = 0.5 - frandom(); + for (i = 3; i--; ) + if ((-0.6 < r->ron[i]) & (r->ron[i] < 0.6)) break; - if (i >= 3) - error(CONSISTENCY, "bad ray direction in inithemi()"); + if (i < 0) + error(CONSISTENCY, "bad ray direction in samp_hemi"); hp->uy[i] = 1.0; VCROSS(hp->ux, hp->uy, r->ron); normalize(hp->ux); VCROSS(hp->uy, r->ron, hp->ux); - /* we're ready to sample */ - return(hp); + /* sample divisions */ + for (i = hp->ns; i--; ) + for (j = hp->ns; j--; ) + hp->sampOK += ambsample(hp, i, j, 0); + copycolor(rcol, hp->acol); + if (!hp->sampOK) { /* utter failure? */ + free(hp); + return(NULL); + } + if (hp->sampOK < hp->ns*hp->ns) { + hp->sampOK *= -1; /* soft failure */ + return(hp); + } + n = ambssamp*wt + 0.5; + if (n > 8) { /* perform super-sampling? */ + ambsupersamp(hp, n); + copycolor(rcol, hp->acol); + } + return(hp); /* all is well */ } -static struct s_ambsamp * -ambsample( /* sample an ambient direction */ - AMBHEMI *hp, - int i, - int j -) +/* Return brightness of farthest ambient sample */ +static double +back_ambval(AMBHEMI *hp, const int n1, const int n2, const int n3) { - struct s_ambsamp *ap = &ambsamp(hp,i,j); - RAY ar; - double spt[2], zd; - int ii; - /* ambient coefficient for weight */ - if (ambacc > FTINY) - setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); - else - copycolor(ar.rcoef, hp->acoef); - if (rayorigin(&ar, AMBIENT, hp->rp, ar.rcoef) < 0) { - setcolor(ap->v, 0., 0., 0.); - VCOPY(ap->p, hp->rp->rop); - return(NULL); /* no sample taken */ + if (hp->sa[n1].d <= hp->sa[n2].d) { + if (hp->sa[n1].d <= hp->sa[n3].d) + return(colval(hp->sa[n1].v,CIEY)); + return(colval(hp->sa[n3].v,CIEY)); } - if (ambacc > FTINY) { - multcolor(ar.rcoef, hp->acoef); - scalecolor(ar.rcoef, 1./AVGREFL); - } - /* generate hemispherical sample */ - SDsquare2disk(spt, (i+.1+.8*frandom())/hp->ns, - (j+.1+.8*frandom())/hp->ns ); - zd = sqrt(1. - spt[0]*spt[0] - spt[1]*spt[1]); - for (ii = 3; ii--; ) - ar.rdir[ii] = spt[0]*hp->ux[ii] + - spt[1]*hp->uy[ii] + - zd*hp->rp->ron[ii]; - checknorm(ar.rdir); - dimlist[ndims++] = i*hp->ns + j + 90171; - rayvalue(&ar); /* evaluate ray */ - ndims--; - multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ - copycolor(ap->v, ar.rcol); - if (ar.rt > 20.0*maxarad) /* limit vertex distance */ - VSUM(ap->p, ar.rorg, ar.rdir, 20.0*maxarad); - else - VCOPY(ap->p, ar.rop); - return(ap); + if (hp->sa[n2].d <= hp->sa[n3].d) + return(colval(hp->sa[n2].v,CIEY)); + return(colval(hp->sa[n3].v,CIEY)); } /* Compute vectors and coefficients for Hessian/gradient calcs */ static void -comp_fftri(FFTRI *ftp, float ap0[3], float ap1[3], FVECT rop) +comp_fftri(FFTRI *ftp, AMBHEMI *hp, const int n0, const int n1) { - FVECT vcp; - double dot_e, dot_er, dot_r, dot_r1, J2; - int i; + double rdot_cp, dot_e, dot_er, rdot_r, rdot_r1, J2; + int ii; - VSUB(ftp->r_i, ap0, rop); - VSUB(ftp->r_i1, ap1, rop); - VSUB(ftp->e_i, ap1, ap0); - VCROSS(vcp, ftp->e_i, ftp->r_i); - ftp->nf = 1.0/DOT(vcp,vcp); + VSUB(ftp->r_i, hp->sa[n0].p, hp->rp->rop); + VSUB(ftp->r_i1, hp->sa[n1].p, hp->rp->rop); + VSUB(ftp->e_i, hp->sa[n1].p, hp->sa[n0].p); + VCROSS(ftp->rcp, ftp->r_i, ftp->r_i1); + rdot_cp = 1.0/DOT(ftp->rcp,ftp->rcp); dot_e = DOT(ftp->e_i,ftp->e_i); dot_er = DOT(ftp->e_i, ftp->r_i); - dot_r = DOT(ftp->r_i,ftp->r_i); - dot_r1 = DOT(ftp->r_i1,ftp->r_i1); - ftp->I1 = acos( DOT(ftp->r_i, ftp->r_i1) / sqrt(dot_r*dot_r1) ) * - sqrt( ftp->nf ); - ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)/dot_r1 - dot_er/dot_r + - dot_e*ftp->I1 )*0.5*ftp->nf; - J2 = 0.5/dot_e*( 1.0/dot_r - 1.0/dot_r1 ) - dot_er/dot_e*ftp->I2; - for (i = 3; i--; ) - ftp->rI2_eJ2[i] = ftp->I2*ftp->r_i[i] + J2*ftp->e_i[i]; + rdot_r = 1.0/DOT(ftp->r_i,ftp->r_i); + rdot_r1 = 1.0/DOT(ftp->r_i1,ftp->r_i1); + ftp->I1 = acos( DOT(ftp->r_i, ftp->r_i1) * sqrt(rdot_r*rdot_r1) ) * + sqrt( rdot_cp ); + ftp->I2 = ( DOT(ftp->e_i, ftp->r_i1)*rdot_r1 - dot_er*rdot_r + + dot_e*ftp->I1 )*0.5*rdot_cp; + J2 = ( 0.5*(rdot_r - rdot_r1) - dot_er*ftp->I2 ) / dot_e; + for (ii = 3; ii--; ) + ftp->rI2_eJ2[ii] = ftp->I2*ftp->r_i[ii] + J2*ftp->e_i[ii]; } @@ -178,7 +327,7 @@ compose_matrix(FVECT mat[3], FVECT va, FVECT vb) static void comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm) { - FVECT vcp; + FVECT ncp; FVECT m1[3], m2[3], m3[3], m4[3]; double d1, d2, d3, d4; double I3, J3, K3; @@ -188,18 +337,17 @@ comp_hessian(FVECT hess[3], FFTRI *ftp, FVECT nrm) d2 = 1.0/DOT(ftp->r_i1,ftp->r_i1); d3 = 1.0/DOT(ftp->e_i,ftp->e_i); d4 = DOT(ftp->e_i, ftp->r_i); - I3 = 0.25*ftp->nf*( DOT(ftp->e_i, ftp->r_i1)*d2*d2 - d4*d1*d1 + - 3.0/d3*ftp->I2 ); + I3 = ( DOT(ftp->e_i, ftp->r_i1)*d2*d2 - d4*d1*d1 + 3.0/d3*ftp->I2 ) + / ( 4.0*DOT(ftp->rcp,ftp->rcp) ); J3 = 0.25*d3*(d1*d1 - d2*d2) - d4*d3*I3; K3 = d3*(ftp->I2 - I3/d1 - 2.0*d4*J3); /* intermediate matrices */ - VCROSS(vcp, nrm, ftp->e_i); - compose_matrix(m1, vcp, ftp->rI2_eJ2); + VCROSS(ncp, nrm, ftp->e_i); + compose_matrix(m1, ncp, ftp->rI2_eJ2); compose_matrix(m2, ftp->r_i, ftp->r_i); compose_matrix(m3, ftp->e_i, ftp->e_i); compose_matrix(m4, ftp->r_i, ftp->e_i); - VCROSS(vcp, ftp->r_i, ftp->e_i); - d1 = DOT(nrm, vcp); + d1 = DOT(nrm, ftp->rcp); d2 = -d1*ftp->I2; d1 *= 2.0; for (i = 3; i--; ) /* final matrix sum */ @@ -229,7 +377,7 @@ rev_hessian(FVECT hess[3]) /* Add to radiometric Hessian from the given triangle */ static void add2hessian(FVECT hess[3], FVECT ehess1[3], - FVECT ehess2[3], FVECT ehess3[3], COLORV v) + FVECT ehess2[3], FVECT ehess3[3], double v) { int i, j; @@ -243,15 +391,14 @@ add2hessian(FVECT hess[3], FVECT ehess1[3], static void comp_gradient(FVECT grad, FFTRI *ftp, FVECT nrm) { - FVECT vcp; + FVECT ncp; double f1; int i; - VCROSS(vcp, ftp->r_i, ftp->r_i1); - f1 = 2.0*DOT(nrm, vcp); - VCROSS(vcp, nrm, ftp->e_i); + f1 = 2.0*DOT(nrm, ftp->rcp); + VCROSS(ncp, nrm, ftp->e_i); for (i = 3; i--; ) - grad[i] = (0.5/PI)*( ftp->I1*vcp[i] + f1*ftp->rI2_eJ2[i] ); + grad[i] = (0.5/PI)*( ftp->I1*ncp[i] + f1*ftp->rI2_eJ2[i] ); } @@ -267,7 +414,7 @@ rev_gradient(FVECT grad) /* Add to displacement gradient from the given triangle */ static void -add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, COLORV v) +add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, FVECT egrad3, double v) { int i; @@ -276,34 +423,8 @@ add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, F } -/* Return brightness of furthest ambient sample */ -static COLORV -back_ambval(struct s_ambsamp *ap1, struct s_ambsamp *ap2, - struct s_ambsamp *ap3, FVECT orig) -{ - COLORV vback; - FVECT vec; - double d2, d2best; - - VSUB(vec, ap1->p, orig); - d2best = DOT(vec,vec); - vback = colval(ap1->v,CIEY); - VSUB(vec, ap2->p, orig); - d2 = DOT(vec,vec); - if (d2 > d2best) { - d2best = d2; - vback = colval(ap2->v,CIEY); - } - VSUB(vec, ap3->p, orig); - d2 = DOT(vec,vec); - if (d2 > d2best) - return(colval(ap3->v,CIEY)); - return(vback); -} - - /* Compute anisotropic radii and eigenvector directions */ -static int +static void eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3]) { double hess2[2][2]; @@ -319,13 +440,16 @@ eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3 hess2[0][1] = DOT(uv[0], b); hess2[1][0] = DOT(uv[1], a); hess2[1][1] = DOT(uv[1], b); - /* compute eigenvalues */ - if ( quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1], - hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]) != 2 || - (evalue[0] = fabs(evalue[0])) <= FTINY*FTINY || - (evalue[1] = fabs(evalue[1])) <= FTINY*FTINY ) - error(INTERNAL, "bad eigenvalue calculation"); - + /* compute eigenvalue(s) */ + i = quadratic(evalue, 1.0, -hess2[0][0]-hess2[1][1], + hess2[0][0]*hess2[1][1]-hess2[0][1]*hess2[1][0]); + if (i == 1) /* double-root (circle) */ + evalue[1] = evalue[0]; + if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) | + ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) { + ra[0] = ra[1] = maxarad; + return; + } if (evalue[0] > evalue[1]) { ra[0] = sqrt(sqrt(4.0/evalue[0])); ra[1] = sqrt(sqrt(4.0/evalue[1])); @@ -383,8 +507,7 @@ ambHessian( /* anisotropic radii & pos. gradient */ } /* compute first row of edges */ for (j = 0; j < hp->ns-1; j++) { - comp_fftri(&fftr, ambsamp(hp,0,j).p, - ambsamp(hp,0,j+1).p, hp->rp->rop); + comp_fftri(&fftr, hp, AI(hp,0,j), AI(hp,0,j+1)); if (hessrow != NULL) comp_hessian(hessrow[j], &fftr, hp->rp->ron); if (gradrow != NULL) @@ -394,8 +517,7 @@ ambHessian( /* anisotropic radii & pos. gradient */ for (i = 0; i < hp->ns-1; i++) { FVECT hesscol[3]; /* compute first vertical edge */ FVECT gradcol; - comp_fftri(&fftr, ambsamp(hp,i,0).p, - ambsamp(hp,i+1,0).p, hp->rp->rop); + comp_fftri(&fftr, hp, AI(hp,i,0), AI(hp,i+1,0)); if (hessrow != NULL) comp_hessian(hesscol, &fftr, hp->rp->ron); if (gradrow != NULL) @@ -403,34 +525,31 @@ ambHessian( /* anisotropic radii & pos. gradient */ for (j = 0; j < hp->ns-1; j++) { FVECT hessdia[3]; /* compute triangle contributions */ FVECT graddia; - COLORV backg; - backg = back_ambval(&ambsamp(hp,i,j), &ambsamp(hp,i,j+1), - &ambsamp(hp,i+1,j), hp->rp->rop); + double backg; + backg = back_ambval(hp, AI(hp,i,j), + AI(hp,i,j+1), AI(hp,i+1,j)); /* diagonal (inner) edge */ - comp_fftri(&fftr, ambsamp(hp,i,j+1).p, - ambsamp(hp,i+1,j).p, hp->rp->rop); + comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j)); if (hessrow != NULL) { comp_hessian(hessdia, &fftr, hp->rp->ron); rev_hessian(hesscol); add2hessian(hessian, hessrow[j], hessdia, hesscol, backg); } - if (gradient != NULL) { + if (gradrow != NULL) { comp_gradient(graddia, &fftr, hp->rp->ron); rev_gradient(gradcol); add2gradient(gradient, gradrow[j], graddia, gradcol, backg); } /* initialize edge in next row */ - comp_fftri(&fftr, ambsamp(hp,i+1,j+1).p, - ambsamp(hp,i+1,j).p, hp->rp->rop); + comp_fftri(&fftr, hp, AI(hp,i+1,j+1), AI(hp,i+1,j)); if (hessrow != NULL) comp_hessian(hessrow[j], &fftr, hp->rp->ron); if (gradrow != NULL) comp_gradient(gradrow[j], &fftr, hp->rp->ron); /* new column edge & paired triangle */ - backg = back_ambval(&ambsamp(hp,i,j+1), &ambsamp(hp,i+1,j+1), - &ambsamp(hp,i+1,j), hp->rp->rop); - comp_fftri(&fftr, ambsamp(hp,i,j+1).p, ambsamp(hp,i+1,j+1).p, - hp->rp->rop); + backg = back_ambval(hp, AI(hp,i+1,j+1), + AI(hp,i+1,j), AI(hp,i,j+1)); + comp_fftri(&fftr, hp, AI(hp,i,j+1), AI(hp,i+1,j+1)); if (hessrow != NULL) { comp_hessian(hesscol, &fftr, hp->rp->ron); rev_hessian(hessdia); @@ -453,9 +572,9 @@ ambHessian( /* anisotropic radii & pos. gradient */ if (ra != NULL) /* extract eigenvectors & radii */ eigenvectors(uv, ra, hessian); - if (pg != NULL) { /* tangential position gradient/PI */ - pg[0] = DOT(gradient, uv[0]) / PI; - pg[1] = DOT(gradient, uv[1]) / PI; + if (pg != NULL) { /* tangential position gradient */ + pg[0] = DOT(gradient, uv[0]); + pg[1] = DOT(gradient, uv[1]); } } @@ -464,11 +583,11 @@ ambHessian( /* anisotropic radii & pos. gradient */ static void ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2]) { - struct s_ambsamp *ap; - double dgsum[2]; - int n; - FVECT vd; - double gfact; + AMBSAMP *ap; + double dgsum[2]; + int n; + FVECT vd; + double gfact; dgsum[0] = dgsum[1] = 0.0; /* sum values times -tan(theta) */ for (ap = hp->sa, n = hp->ns*hp->ns; n--; ap++) { @@ -476,15 +595,73 @@ ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2]) VSUB(vd, ap->p, hp->rp->rop); /* brightness over cosine factor */ gfact = colval(ap->v,CIEY) / DOT(hp->rp->ron, vd); - /* -sine = -proj_radius/vd_length */ - dgsum[0] += DOT(uv[1], vd) * gfact; - dgsum[1] -= DOT(uv[0], vd) * gfact; + /* sine = proj_radius/vd_length */ + dgsum[0] -= DOT(uv[1], vd) * gfact; + dgsum[1] += DOT(uv[0], vd) * gfact; } dg[0] = dgsum[0] / (hp->ns*hp->ns); dg[1] = dgsum[1] / (hp->ns*hp->ns); } +/* Compute potential light leak direction flags for cache value */ +static uint32 +ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1) +{ + const double max_d = 1.0/(minarad*ambacc + 0.001); + const double ang_res = 0.5*PI/hp->ns; + const double ang_step = ang_res/((int)(16/PI*ang_res) + 1.01); + double avg_d = 0; + uint32 flgs = 0; + FVECT vec; + double u, v; + double ang, a1; + int i, j; + /* don't bother for a few samples */ + if (hp->ns < 12) + return(0); + /* check distances overhead */ + for (i = hp->ns*3/4; i-- > hp->ns>>2; ) + for (j = hp->ns*3/4; j-- > hp->ns>>2; ) + avg_d += ambsam(hp,i,j).d; + avg_d *= 4.0/(hp->ns*hp->ns); + if (avg_d*r0 >= 1.0) /* ceiling too low for corral? */ + return(0); + if (avg_d >= max_d) /* insurance */ + return(0); + /* else circle around perimeter */ + for (i = 0; i < hp->ns; i++) + for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) { + AMBSAMP *ap = &ambsam(hp,i,j); + if ((ap->d <= FTINY) | (ap->d >= max_d)) + continue; /* too far or too near */ + VSUB(vec, ap->p, hp->rp->rop); + u = DOT(vec, uv[0]); + v = DOT(vec, uv[1]); + if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= u*u + v*v) + continue; /* occluder outside ellipse */ + ang = atan2a(v, u); /* else set direction flags */ + for (a1 = ang-ang_res; a1 <= ang+ang_res; a1 += ang_step) + flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0))); + } + /* add low-angle incident (< 20deg) */ + if (fabs(hp->rp->rod) <= 0.342) { + u = -DOT(hp->rp->rdir, uv[0]); + v = -DOT(hp->rp->rdir, uv[1]); + if ((r0*r0*u*u + r1*r1*v*v) > hp->rp->rot*hp->rp->rot) { + ang = atan2a(v, u); + ang += 2.*PI*(ang < 0); + ang *= 16/PI; + if ((ang < .5) | (ang >= 31.5)) + flgs |= 0x80000001; + else + flgs |= 3L<<(int)(ang-.5); + } + } + return(flgs); +} + + int doambient( /* compute ambient component */ COLOR rcol, /* input/output color */ @@ -493,18 +670,16 @@ doambient( /* compute ambient component */ FVECT uv[2], /* returned (optional) */ float ra[2], /* returned (optional) */ float pg[2], /* returned (optional) */ - float dg[2] /* returned (optional) */ + float dg[2], /* returned (optional) */ + uint32 *crlp /* returned (optional) */ ) { - AMBHEMI *hp = inithemi(rcol, r, wt); - int cnt = 0; - FVECT my_uv[2]; - double d, acol[3]; - struct s_ambsamp *ap; - int i, j; - /* check/initialize */ - if (hp == NULL) - return(0); + AMBHEMI *hp = samp_hemi(rcol, r, wt); + FVECT my_uv[2]; + double d, K; + AMBSAMP *ap; + int i; + /* clear return values */ if (uv != NULL) memset(uv, 0, sizeof(FVECT)*2); if (ra != NULL) @@ -513,33 +688,28 @@ doambient( /* compute ambient component */ pg[0] = pg[1] = 0.0; if (dg != NULL) dg[0] = dg[1] = 0.0; - /* sample the hemisphere */ - acol[0] = acol[1] = acol[2] = 0.0; - for (i = hp->ns; i--; ) - for (j = hp->ns; j--; ) - if ((ap = ambsample(hp, i, j)) != NULL) { - addcolor(acol, ap->v); - ++cnt; - } - if (!cnt) { - setcolor(rcol, 0.0, 0.0, 0.0); - free(hp); - return(0); /* no valid samples */ + if (crlp != NULL) + *crlp = 0; + if (hp == NULL) /* sampling falure? */ + return(0); + + if ((ra == NULL) & (pg == NULL) & (dg == NULL) || + (hp->sampOK < 0) | (hp->ns < 6)) { + free(hp); /* Hessian not requested/possible */ + return(-1); /* value-only return value */ } - copycolor(rcol, acol); /* final indirect irradiance/PI */ - if (cnt < hp->ns*hp->ns || /* incomplete sampling? */ - (ra == NULL) & (pg == NULL) & (dg == NULL)) { - free(hp); - return(-1); /* no radius or gradient calc. */ + if ((d = bright(rcol)) > FTINY) { /* normalize Y values */ + d = 0.99*(hp->ns*hp->ns)/d; + K = 0.01; + } else { /* or fall back on geometric Hessian */ + K = 1.0; + pg = NULL; + dg = NULL; + crlp = NULL; } - multcolor(acol, hp->acoef); /* normalize Y values */ - if ((d = bright(acol)) > FTINY) - d = 1.0/d; - else - d = 0.0; ap = hp->sa; /* relative Y channel from here on... */ for (i = hp->ns*hp->ns; i--; ap++) - colval(ap->v,CIEY) = bright(ap->v)*d + 0.0314; + colval(ap->v,CIEY) = bright(ap->v)*d + K; if (uv == NULL) /* make sure we have axis pointers */ uv = my_uv; @@ -550,18 +720,37 @@ doambient( /* compute ambient component */ ambdirgrad(hp, uv, dg); if (ra != NULL) { /* scale/clamp radii */ + if (pg != NULL) { + if (ra[0]*(d = fabs(pg[0])) > 1.0) + ra[0] = 1.0/d; + if (ra[1]*(d = fabs(pg[1])) > 1.0) + ra[1] = 1.0/d; + if (ra[0] > ra[1]) + ra[0] = ra[1]; + } if (ra[0] < minarad) { ra[0] = minarad; if (ra[1] < minarad) ra[1] = minarad; } - ra[0] *= d = 1.0/sqrt(sqrt(wt)); + ra[0] *= d = 1.0/sqrt(wt); if ((ra[1] *= d) > 2.0*ra[0]) ra[1] = 2.0*ra[0]; if (ra[1] > maxarad) { ra[1] = maxarad; if (ra[0] > maxarad) ra[0] = maxarad; + } + /* flag encroached directions */ + if ((wt >= 0.89*AVGREFL) & (crlp != NULL)) + *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc); + if (pg != NULL) { /* cap gradient if necessary */ + d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1]; + if (d > 1.0) { + d = 1.0/sqrt(d); + pg[0] *= d; + pg[1] *= d; + } } } free(hp); /* clean up and return */