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.79 by greg, Tue Jan 9 00:52:35 2018 UTC vs.
Revision 2.86 by greg, Wed Feb 17 01:29:22 2021 UTC

# Line 21 | Line 21 | static const char      RCSid[] = "$Id$";
21   #include  "ambient.h"
22   #include  "random.h"
23  
24 < #ifndef OLDAMB
24 > #ifndef MINADIV
25 > #define MINADIV         7       /* minimum # divisions in each dimension */
26 > #endif
27  
28   extern void             SDsquare2disk(double ds[2], double seedx, double seedy);
29  
30   typedef struct {
31          COLOR   v;              /* hemisphere sample value */
32 <        float   d;              /* reciprocal distance (1/rt) */
32 >        float   d;              /* reciprocal distance */
33          FVECT   p;              /* intersection point */
34   } AMBSAMP;              /* sample value */
35  
# Line 131 | Line 133 | resample:
133          dimlist[ndims++] = AI(hp,i,j) + 90171;
134          rayvalue(&ar);                  /* evaluate ray */
135          ndims--;
136 <        if (ar.rt <= FTINY)
136 >        zd = raydistance(&ar);
137 >        if (zd <= FTINY)
138                  return(0);              /* should never happen */
139          multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
140 <        if (ar.rt*ap->d < 1.0)          /* new/closer distance? */
141 <                ap->d = 1.0/ar.rt;
140 >        if (zd*ap->d < 1.0)             /* new/closer distance? */
141 >                ap->d = 1.0/zd;
142          if (!n) {                       /* record first vertex & value */
143 <                if (ar.rt > 10.0*thescene.cusize + 1000.)
144 <                        ar.rt = 10.0*thescene.cusize + 1000.;
145 <                VSUM(ap->p, ar.rorg, ar.rdir, ar.rt);
143 >                if (zd > 10.0*thescene.cusize + 1000.)
144 >                        zd = 10.0*thescene.cusize + 1000.;
145 >                VSUM(ap->p, ar.rorg, ar.rdir, zd);
146                  copycolor(ap->v, ar.rcol);
147          } else {                        /* else update recorded value */
148                  hp->acol[RED] -= colval(ap->v,RED);
# Line 156 | Line 159 | resample:
159   }
160  
161  
162 < /* Estimate errors based on ambient division differences */
162 > /* Estimate variance based on ambient division differences */
163   static float *
164   getambdiffs(AMBHEMI *hp)
165   {
# Line 164 | Line 167 | getambdiffs(AMBHEMI *hp)
167          float   *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float));
168          float   *ep;
169          AMBSAMP *ap;
170 <        double  b, d2;
170 >        double  b, b1, d2;
171          int     i, j;
172  
173          if (earr == NULL)               /* out of memory? */
174                  return(NULL);
175 <                                        /* compute squared neighbor diffs */
175 >                                        /* sum squared neighbor diffs */
176          for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++)
177              for (j = 0; j < hp->ns; j++, ap++, ep++) {
178                  b = bright(ap[0].v);
179                  if (i) {                /* from above */
180 <                        d2 = normf*(b - bright(ap[-hp->ns].v));
181 <                        d2 *= d2;
180 >                        b1 = bright(ap[-hp->ns].v);
181 >                        d2 = b - b1;
182 >                        d2 *= d2*normf/(b + b1);
183                          ep[0] += d2;
184                          ep[-hp->ns] += d2;
185                  }
186                  if (!j) continue;
187                                          /* from behind */
188 <                d2 = normf*(b - bright(ap[-1].v));
189 <                d2 *= d2;
188 >                b1 = bright(ap[-1].v);
189 >                d2 = b - b1;
190 >                d2 *= d2*normf/(b + b1);
191                  ep[0] += d2;
192                  ep[-1] += d2;
193                  if (!i) continue;
194                                          /* diagonal */
195 <                d2 = normf*(b - bright(ap[-hp->ns-1].v));
196 <                d2 *= d2;
195 >                b1 = bright(ap[-hp->ns-1].v);
196 >                d2 = b - b1;
197 >                d2 *= d2*normf/(b + b1);
198                  ep[0] += d2;
199                  ep[-hp->ns-1] += d2;
200              }
# Line 215 | Line 221 | ambsupersamp(AMBHEMI *hp, int cnt)
221   {
222          float   *earr = getambdiffs(hp);
223          double  e2rem = 0;
218        AMBSAMP *ap;
224          float   *ep;
225          int     i, j, n, nss;
226  
# Line 225 | Line 230 | ambsupersamp(AMBHEMI *hp, int cnt)
230          for (ep = earr + hp->ns*hp->ns; ep > earr; )
231                  e2rem += *--ep;
232          ep = earr;                      /* perform super-sampling */
233 <        for (ap = hp->sa, i = 0; i < hp->ns; i++)
234 <            for (j = 0; j < hp->ns; j++, ap++) {
233 >        for (i = 0; i < hp->ns; i++)
234 >            for (j = 0; j < hp->ns; j++) {
235                  if (e2rem <= FTINY)
236                          goto done;      /* nothing left to do */
237                  nss = *ep/e2rem*cnt + frandom();
# Line 257 | Line 262 | samp_hemi(                             /* sample indirect hemisphere */
262                          wt > (d = 0.8*intens(rcol)*r->rweight/(ambdiv*minweight)))
263                  wt = d;                 /* avoid ray termination */
264          n = sqrt(ambdiv * wt) + 0.5;
265 <        i = 1 + 5*(ambacc > FTINY);     /* minimum number of samples */
266 <        if (n < i)
265 >        i = 1 + (MINADIV-1)*(ambacc > FTINY);
266 >        if (n < i)                      /* use minimum number of samples? */
267                  n = i;
268                                          /* allocate sampling array */
269          hp = (AMBHEMI *)malloc(sizeof(AMBHEMI) + sizeof(AMBSAMP)*(n*n - 1));
# Line 290 | Line 295 | samp_hemi(                             /* sample indirect hemisphere */
295                  hp->sampOK *= -1;       /* soft failure */
296                  return(hp);
297          }
298 +        if (hp->sampOK <= MINADIV*MINADIV)
299 +                return(hp);             /* don't bother super-sampling */
300          n = ambssamp*wt + 0.5;
301          if (n > 8) {                    /* perform super-sampling? */
302                  ambsupersamp(hp, n);
# Line 646 | Line 653 | ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, c
653          FVECT           vec;
654          double          u, v;
655          double          ang, a1;
649        OBJREC          *m;
656          int             i, j;
657                                          /* don't bother for a few samples */
658          if (hp->ns < 8)
# Line 711 | Line 717 | doambient(                             /* compute ambient component */
717                  return(0);
718  
719          if ((ra == NULL) & (pg == NULL) & (dg == NULL) ||
720 <                        (hp->sampOK < 0) | (hp->ns < 6)) {
720 >                        (hp->sampOK < 0) | (hp->ns < MINADIV)) {
721                  free(hp);               /* Hessian not requested/possible */
722                  return(-1);             /* value-only return value */
723          }
# Line 774 | Line 780 | doambient(                             /* compute ambient component */
780          return(1);
781   }
782  
777
778 #else /* ! NEWAMB */
779
780
781 void
782 inithemi(                       /* initialize sampling hemisphere */
783        AMBHEMI  *hp,
784        COLOR ac,
785        RAY  *r,
786        double  wt
787 )
788 {
789        double  d;
790        int  i;
791                                        /* set number of divisions */
792        if (ambacc <= FTINY &&
793                        wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight)))
794                wt = d;                 /* avoid ray termination */
795        hp->nt = sqrt(ambdiv * wt / PI) + 0.5;
796        i = ambacc > FTINY ? 3 : 1;     /* minimum number of samples */
797        if (hp->nt < i)
798                hp->nt = i;
799        hp->np = PI * hp->nt + 0.5;
800                                        /* set number of super-samples */
801        hp->ns = ambssamp * wt + 0.5;
802                                        /* assign coefficient */
803        copycolor(hp->acoef, ac);
804        d = 1.0/(hp->nt*hp->np);
805        scalecolor(hp->acoef, d);
806                                        /* make axes */
807        VCOPY(hp->uz, r->ron);
808        hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0;
809        for (i = 0; i < 3; i++)
810                if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6)
811                        break;
812        if (i >= 3)
813                error(CONSISTENCY, "bad ray direction in inithemi");
814        hp->uy[i] = 1.0;
815        fcross(hp->ux, hp->uy, hp->uz);
816        normalize(hp->ux);
817        fcross(hp->uy, hp->uz, hp->ux);
818 }
819
820
821 int
822 divsample(                              /* sample a division */
823        AMBSAMP  *dp,
824        AMBHEMI  *h,
825        RAY  *r
826 )
827 {
828        RAY  ar;
829        int  hlist[3];
830        double  spt[2];
831        double  xd, yd, zd;
832        double  b2;
833        double  phi;
834        int  i;
835                                        /* ambient coefficient for weight */
836        if (ambacc > FTINY)
837                setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL);
838        else
839                copycolor(ar.rcoef, h->acoef);
840        if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0)
841                return(-1);
842        if (ambacc > FTINY) {
843                multcolor(ar.rcoef, h->acoef);
844                scalecolor(ar.rcoef, 1./AVGREFL);
845        }
846        hlist[0] = r->rno;
847        hlist[1] = dp->t;
848        hlist[2] = dp->p;
849        multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n));
850        zd = sqrt((dp->t + spt[0])/h->nt);
851        phi = 2.0*PI * (dp->p + spt[1])/h->np;
852        xd = tcos(phi) * zd;
853        yd = tsin(phi) * zd;
854        zd = sqrt(1.0 - zd*zd);
855        for (i = 0; i < 3; i++)
856                ar.rdir[i] =    xd*h->ux[i] +
857                                yd*h->uy[i] +
858                                zd*h->uz[i];
859        checknorm(ar.rdir);
860        dimlist[ndims++] = dp->t*h->np + dp->p + 90171;
861        rayvalue(&ar);
862        ndims--;
863        multcolor(ar.rcol, ar.rcoef);   /* apply coefficient */
864        addcolor(dp->v, ar.rcol);
865                                        /* use rt to improve gradient calc */
866        if (ar.rt > FTINY && ar.rt < FHUGE)
867                dp->r += 1.0/ar.rt;
868                                        /* (re)initialize error */
869        if (dp->n++) {
870                b2 = bright(dp->v)/dp->n - bright(ar.rcol);
871                b2 = b2*b2 + dp->k*((dp->n-1)*(dp->n-1));
872                dp->k = b2/(dp->n*dp->n);
873        } else
874                dp->k = 0.0;
875        return(0);
876 }
877
878
879 static int
880 ambcmp(                                 /* decreasing order */
881        const void *p1,
882        const void *p2
883 )
884 {
885        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
886        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
887
888        if (d1->k < d2->k)
889                return(1);
890        if (d1->k > d2->k)
891                return(-1);
892        return(0);
893 }
894
895
896 static int
897 ambnorm(                                /* standard order */
898        const void *p1,
899        const void *p2
900 )
901 {
902        const AMBSAMP   *d1 = (const AMBSAMP *)p1;
903        const AMBSAMP   *d2 = (const AMBSAMP *)p2;
904        int     c;
905
906        if ( (c = d1->t - d2->t) )
907                return(c);
908        return(d1->p - d2->p);
909 }
910
911
912 double
913 doambient(                              /* compute ambient component */
914        COLOR  rcol,
915        RAY  *r,
916        double  wt,
917        FVECT  pg,
918        FVECT  dg
919 )
920 {
921        double  b, d=0;
922        AMBHEMI  hemi;
923        AMBSAMP  *div;
924        AMBSAMP  dnew;
925        double  acol[3];
926        AMBSAMP  *dp;
927        double  arad;
928        int  divcnt;
929        int  i, j;
930                                        /* initialize hemisphere */
931        inithemi(&hemi, rcol, r, wt);
932        divcnt = hemi.nt * hemi.np;
933                                        /* initialize */
934        if (pg != NULL)
935                pg[0] = pg[1] = pg[2] = 0.0;
936        if (dg != NULL)
937                dg[0] = dg[1] = dg[2] = 0.0;
938        setcolor(rcol, 0.0, 0.0, 0.0);
939        if (divcnt == 0)
940                return(0.0);
941                                        /* allocate super-samples */
942        if (hemi.ns > 0 || pg != NULL || dg != NULL) {
943                div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP));
944                if (div == NULL)
945                        error(SYSTEM, "out of memory in doambient");
946        } else
947                div = NULL;
948                                        /* sample the divisions */
949        arad = 0.0;
950        acol[0] = acol[1] = acol[2] = 0.0;
951        if ((dp = div) == NULL)
952                dp = &dnew;
953        divcnt = 0;
954        for (i = 0; i < hemi.nt; i++)
955                for (j = 0; j < hemi.np; j++) {
956                        dp->t = i; dp->p = j;
957                        setcolor(dp->v, 0.0, 0.0, 0.0);
958                        dp->r = 0.0;
959                        dp->n = 0;
960                        if (divsample(dp, &hemi, r) < 0) {
961                                if (div != NULL)
962                                        dp++;
963                                continue;
964                        }
965                        arad += dp->r;
966                        divcnt++;
967                        if (div != NULL)
968                                dp++;
969                        else
970                                addcolor(acol, dp->v);
971                }
972        if (!divcnt) {
973                if (div != NULL)
974                        free((void *)div);
975                return(0.0);            /* no samples taken */
976        }
977        if (divcnt < hemi.nt*hemi.np) {
978                pg = dg = NULL;         /* incomplete sampling */
979                hemi.ns = 0;
980        } else if (arad > FTINY && divcnt/arad < minarad) {
981                hemi.ns = 0;            /* close enough */
982        } else if (hemi.ns > 0) {       /* else perform super-sampling? */
983                comperrs(div, &hemi);                   /* compute errors */
984                qsort(div, divcnt, sizeof(AMBSAMP), ambcmp);    /* sort divs */
985                                                /* super-sample */
986                for (i = hemi.ns; i > 0; i--) {
987                        dnew = *div;
988                        if (divsample(&dnew, &hemi, r) < 0) {
989                                dp++;
990                                continue;
991                        }
992                        dp = div;               /* reinsert */
993                        j = divcnt < i ? divcnt : i;
994                        while (--j > 0 && dnew.k < dp[1].k) {
995                                *dp = *(dp+1);
996                                dp++;
997                        }
998                        *dp = dnew;
999                }
1000                if (pg != NULL || dg != NULL)   /* restore order */
1001                        qsort(div, divcnt, sizeof(AMBSAMP), ambnorm);
1002        }
1003                                        /* compute returned values */
1004        if (div != NULL) {
1005                arad = 0.0;             /* note: divcnt may be < nt*np */
1006                for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) {
1007                        arad += dp->r;
1008                        if (dp->n > 1) {
1009                                b = 1.0/dp->n;
1010                                scalecolor(dp->v, b);
1011                                dp->r *= b;
1012                                dp->n = 1;
1013                        }
1014                        addcolor(acol, dp->v);
1015                }
1016                b = bright(acol);
1017                if (b > FTINY) {
1018                        b = 1.0/b;      /* compute & normalize gradient(s) */
1019                        if (pg != NULL) {
1020                                posgradient(pg, div, &hemi);
1021                                for (i = 0; i < 3; i++)
1022                                        pg[i] *= b;
1023                        }
1024                        if (dg != NULL) {
1025                                dirgradient(dg, div, &hemi);
1026                                for (i = 0; i < 3; i++)
1027                                        dg[i] *= b;
1028                        }
1029                }
1030                free((void *)div);
1031        }
1032        copycolor(rcol, acol);
1033        if (arad <= FTINY)
1034                arad = maxarad;
1035        else
1036                arad = (divcnt+hemi.ns)/arad;
1037        if (pg != NULL) {               /* reduce radius if gradient large */
1038                d = DOT(pg,pg);
1039                if (d*arad*arad > 1.0)
1040                        arad = 1.0/sqrt(d);
1041        }
1042        if (arad < minarad) {
1043                arad = minarad;
1044                if (pg != NULL && d*arad*arad > 1.0) {  /* cap gradient */
1045                        d = 1.0/arad/sqrt(d);
1046                        for (i = 0; i < 3; i++)
1047                                pg[i] *= d;
1048                }
1049        }
1050        if ((arad /= sqrt(wt)) > maxarad)
1051                arad = maxarad;
1052        return(arad);
1053 }
1054
1055
1056 void
1057 comperrs(                       /* compute initial error estimates */
1058        AMBSAMP  *da,   /* assumes standard ordering */
1059        AMBHEMI  *hp
1060 )
1061 {
1062        double  b, b2;
1063        int  i, j;
1064        AMBSAMP  *dp;
1065                                /* sum differences from neighbors */
1066        dp = da;
1067        for (i = 0; i < hp->nt; i++)
1068                for (j = 0; j < hp->np; j++) {
1069 #ifdef  DEBUG
1070                        if (dp->t != i || dp->p != j)
1071                                error(CONSISTENCY,
1072                                        "division order in comperrs");
1073 #endif
1074                        b = bright(dp[0].v);
1075                        if (i > 0) {            /* from above */
1076                                b2 = bright(dp[-hp->np].v) - b;
1077                                b2 *= b2 * 0.25;
1078                                dp[0].k += b2;
1079                                dp[-hp->np].k += b2;
1080                        }
1081                        if (j > 0) {            /* from behind */
1082                                b2 = bright(dp[-1].v) - b;
1083                                b2 *= b2 * 0.25;
1084                                dp[0].k += b2;
1085                                dp[-1].k += b2;
1086                        } else {                /* around */
1087                                b2 = bright(dp[hp->np-1].v) - b;
1088                                b2 *= b2 * 0.25;
1089                                dp[0].k += b2;
1090                                dp[hp->np-1].k += b2;
1091                        }
1092                        dp++;
1093                }
1094                                /* divide by number of neighbors */
1095        dp = da;
1096        for (j = 0; j < hp->np; j++)            /* top row */
1097                (dp++)->k *= 1.0/3.0;
1098        if (hp->nt < 2)
1099                return;
1100        for (i = 1; i < hp->nt-1; i++)          /* central region */
1101                for (j = 0; j < hp->np; j++)
1102                        (dp++)->k *= 0.25;
1103        for (j = 0; j < hp->np; j++)            /* bottom row */
1104                (dp++)->k *= 1.0/3.0;
1105 }
1106
1107
1108 void
1109 posgradient(                                    /* compute position gradient */
1110        FVECT  gv,
1111        AMBSAMP  *da,                   /* assumes standard ordering */
1112        AMBHEMI  *hp
1113 )
1114 {
1115        int  i, j;
1116        double  nextsine, lastsine, b, d;
1117        double  mag0, mag1;
1118        double  phi, cosp, sinp, xd, yd;
1119        AMBSAMP  *dp;
1120
1121        xd = yd = 0.0;
1122        for (j = 0; j < hp->np; j++) {
1123                dp = da + j;
1124                mag0 = mag1 = 0.0;
1125                lastsine = 0.0;
1126                for (i = 0; i < hp->nt; i++) {
1127 #ifdef  DEBUG
1128                        if (dp->t != i || dp->p != j)
1129                                error(CONSISTENCY,
1130                                        "division order in posgradient");
1131 #endif
1132                        b = bright(dp->v);
1133                        if (i > 0) {
1134                                d = dp[-hp->np].r;
1135                                if (dp[0].r > d) d = dp[0].r;
1136                                                        /* sin(t)*cos(t)^2 */
1137                                d *= lastsine * (1.0 - (double)i/hp->nt);
1138                                mag0 += d*(b - bright(dp[-hp->np].v));
1139                        }
1140                        nextsine = sqrt((double)(i+1)/hp->nt);
1141                        if (j > 0) {
1142                                d = dp[-1].r;
1143                                if (dp[0].r > d) d = dp[0].r;
1144                                mag1 += d * (nextsine - lastsine) *
1145                                                (b - bright(dp[-1].v));
1146                        } else {
1147                                d = dp[hp->np-1].r;
1148                                if (dp[0].r > d) d = dp[0].r;
1149                                mag1 += d * (nextsine - lastsine) *
1150                                                (b - bright(dp[hp->np-1].v));
1151                        }
1152                        dp += hp->np;
1153                        lastsine = nextsine;
1154                }
1155                mag0 *= 2.0*PI / hp->np;
1156                phi = 2.0*PI * (double)j/hp->np;
1157                cosp = tcos(phi); sinp = tsin(phi);
1158                xd += mag0*cosp - mag1*sinp;
1159                yd += mag0*sinp + mag1*cosp;
1160        }
1161        for (i = 0; i < 3; i++)
1162                gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI;
1163 }
1164
1165
1166 void
1167 dirgradient(                                    /* compute direction gradient */
1168        FVECT  gv,
1169        AMBSAMP  *da,                   /* assumes standard ordering */
1170        AMBHEMI  *hp
1171 )
1172 {
1173        int  i, j;
1174        double  mag;
1175        double  phi, xd, yd;
1176        AMBSAMP  *dp;
1177
1178        xd = yd = 0.0;
1179        for (j = 0; j < hp->np; j++) {
1180                dp = da + j;
1181                mag = 0.0;
1182                for (i = 0; i < hp->nt; i++) {
1183 #ifdef  DEBUG
1184                        if (dp->t != i || dp->p != j)
1185                                error(CONSISTENCY,
1186                                        "division order in dirgradient");
1187 #endif
1188                                                        /* tan(t) */
1189                        mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0);
1190                        dp += hp->np;
1191                }
1192                phi = 2.0*PI * (j+.5)/hp->np + PI/2.0;
1193                xd += mag * tcos(phi);
1194                yd += mag * tsin(phi);
1195        }
1196        for (i = 0; i < 3; i++)
1197                gv[i] = xd*hp->ux[i] + yd*hp->uy[i];
1198 }
1199
1200 #endif  /* ! NEWAMB */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines