ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rtcontrib.c
(Generate patch)

Comparing ray/src/util/rtcontrib.c (file contents):
Revision 1.48 by greg, Sat Nov 17 16:27:14 2007 UTC vs.
Revision 1.57 by greg, Sun Sep 26 15:44:00 2010 UTC

# Line 14 | Line 14 | static const char RCSid[] = "$Id$";
14   #include  "color.h"
15   #include  "resolu.h"
16   #include  "lookup.h"
17 + #include  "random.h"
18   #include  "calcomp.h"
19  
20   #ifndef MAXMODLIST
# Line 99 | Line 100 | struct rtproc {
100  
101                                          /* rtrace command and defaults */
102   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
103 <                                "-dj", ".5", "-dr", "3",
103 >                                "-dj", ".9", "-dr", "3",
104                                  "-ab", "1", "-ad", "350", };
105  
106   int  rtargc = 9;
# Line 131 | Line 132 | int            inpfmt = 'a';           /* input format */
132   int             outfmt = 'a';           /* output format */
133  
134   int             header = 1;             /* output header? */
134 int             accumulate = 0;         /* accumulate ray values? */
135   int             force_open = 0;         /* truncate existing output? */
136   int             recover = 0;            /* recover previous output? */
137 + int             accumulate = 1;         /* input rays per output record */
138   int             xres = 0;               /* horiz. output resolution */
139   int             yres = 0;               /* vert. output resolution */
140  
141 + int             account;                /* current accumulation count */
142   unsigned long   raysleft;               /* number of rays left to trace */
143   long            waitflush;              /* how long until next flush */
144  
# Line 166 | Line 168 | void process_queue(void);
168  
169   void put_contrib(const DCOLOR cnt, FILE *fout);
170   void add_contrib(const char *modn);
171 < void done_contrib(void);
171 > void done_contrib(int navg);
172  
173   /* return number of open rtrace processes */
174   static int
# Line 245 | Line 247 | main(int argc, char *argv[])
247                  while ((j = expandarg(&argc, &argv, i)) > 0)
248                          ;
249                  if (j < 0) {
250 <                        fprintf(stderr, "%s: cannot expand '%s'",
250 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
251                                          argv[0], argv[i]);
252                          exit(1);
253                  }
# Line 274 | Line 276 | main(int argc, char *argv[])
276                                          continue;
277                                  }
278                                  break;
279 <                        case 'c':               /* accumulate ray values */
280 <                                switch (argv[i][2]) {
281 <                                case '\0':
282 <                                        accumulate = !accumulate;
281 <                                        continue;
282 <                                case '+': case '1':
283 <                                case 'T': case 't':
284 <                                case 'Y': case 'y':
285 <                                        accumulate = 1;
286 <                                        continue;
287 <                                case '-': case '0':
288 <                                case 'F': case 'f':
289 <                                case 'N': case 'n':
290 <                                        accumulate = 0;
291 <                                        continue;
292 <                                }
293 <                                break;
279 >                        case 'c':               /* input rays per output */
280 >                                if (argv[i][2] || i >= argc-2) break;
281 >                                accumulate = atoi(argv[++i]);
282 >                                continue;
283                          case 'r':               /* recover output */
284                                  if (argv[i][2]) break;
285                                  recover = 1;
# Line 352 | Line 341 | main(int argc, char *argv[])
341                          case 'b':               /* bin expression/count */
342                                  if (i >= argc-2) break;
343                                  if (argv[i][2] == 'n') {
344 <                                        bincnt = atoi(argv[++i]);
344 >                                        bincnt = (int)(eval(argv[++i]) + .5);
345                                          continue;
346                                  }
347                                  if (argv[i][2]) break;
# Line 380 | Line 369 | main(int argc, char *argv[])
369                          }
370                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
371          }
372 <        if (accumulate)         /* no output flushing for single record */
372 >        if (accumulate <= 0)    /* no output flushing for single record */
373                  xres = yres = 0;
374                                  /* set global argument list */
375          gargc = argc; gargv = argv;
# Line 390 | Line 379 | main(int argc, char *argv[])
379          rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
380                                  /* just asking for defaults? */
381          if (!strcmp(argv[i], "-defaults")) {
382 <                char    sxres[16], syres[16];
382 >                char    nps[8], sxres[16], syres[16];
383                  char    *rtpath;
384 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
384 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
385 >                                accumulate);
386                  printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
387                                  contrib ? "contributions" : "coefficients");
398                printf("-c%c\t\t\t\t# %s\n", accumulate ? '+' : '-',
399                                accumulate ? "accumulate ray values" :
400                                        "one output record per ray");
388                  fflush(stdout);                 /* report OUR options */
389 +                rtargv[rtargc++] = "-n";
390 +                sprintf(nps, "%d", nprocs);
391 +                rtargv[rtargc++] = nps;
392                  rtargv[rtargc++] = header ? "-h+" : "-h-";
393                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
394                  rtargv[rtargc++] = fmt;
# Line 554 | Line 544 | init(int np)
544                          raysleft = yres;
545          } else
546                  raysleft = 0;
547 <        waitflush = xres;
547 >        if ((account = accumulate) > 0)
548 >                raysleft *= accumulate;
549 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
550          if (!recover)
551                  return;
552                                          /* recover previous values */
553 <        if (accumulate)
553 >        if (accumulate <= 0)
554                  reload_output();
555          else
556                  recover_output(stdin);
# Line 591 | Line 583 | addmodifier(char *modn, char *outf, char *binv, int bi
583                  error(USER, errmsg);
584          }
585          if (nmods >= MAXMODLIST)
586 <                error(USER, "too many modifiers");
586 >                error(INTERNAL, "too many modifiers");
587          modname[nmods++] = modn;        /* XXX assumes static string */
588          lep->key = modn;                /* XXX assumes static string */
589          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 599 | Line 591 | addmodifier(char *modn, char *outf, char *binv, int bi
591                  error(SYSTEM, "out of memory in addmodifier");
592          mp->outspec = outf;             /* XXX assumes static string */
593          mp->modname = modn;             /* XXX assumes static string */
594 <        if (binv != NULL)
595 <                mp->binv = eparse(binv);
596 <        else
597 <                mp->binv = eparse("0");
598 <        mp->nbins = 1;
594 >        if (binv == NULL)
595 >                binv = "0";             /* use single bin if unspecified */
596 >        mp->binv = eparse(binv);
597 >        if (mp->binv->type == NUM) {    /* check value if constant */
598 >                bincnt = (int)(evalue(mp->binv) + 1.5);
599 >                if (bincnt != 1) {
600 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
601 >                                        binv);
602 >                        error(USER, errmsg);
603 >                }
604 >        }
605 >        mp->nbins = 1;                  /* initialize results holder */
606          setcolor(mp->cbin[0], 0., 0., 0.);
607 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
609 <                bincnt = 1;
610 <        else if (bincnt > 1)
607 >        if (bincnt > 1)
608                  mp = growmodifier(mp, bincnt);
609          lep->data = (char *)mp;
610                                          /* allocate output streams */
# Line 670 | Line 667 | ofname(char *oname, const char *ospec, const char *mna
667                                  mnp = cp;
668                                  break;
669                          case 'd':
670 +                        case 'i':
671 +                        case 'o':
672 +                        case 'x':
673 +                        case 'X':
674                                  if (bnp != NULL)
675                                          return -1;
676                                  bnp = cp;
# Line 736 | Line 737 | printresolu(FILE *fout, int xr, int yr)
737   {
738          if ((xr > 0) & (yr > 0))        /* resolution string */
739                  fprtresolu(xr, yr, fout);
739        if (xres > 0)                   /* global flush flag */
740                fflush(fout);
740   }
741  
742   /* Get output stream pointer (open and write header if new and noopen==0) */
# Line 758 | Line 757 | getostream(const char *ospec, const char *mname, int b
757                          if (header)
758                                  printheader(stdout, NULL);
759                          printresolu(stdout, xres, yres);
760 +                        if (waitflush > 0)
761 +                                fflush(stdout);
762                          stdos.xr = xres; stdos.yr = yres;
763                          using_stdout = 1;
764                  }
# Line 813 | Line 814 | getostream(const char *ospec, const char *mname, int b
814                          *cp = '\0';
815                          printheader(sop->ofp, info);
816                  }
817 <                if (!accumulate) {              /* global res. for -c- */
817 >                if (accumulate > 0) {           /* global resolution */
818                          sop->xr = xres; sop->yr = yres;
819                  }
820                  printresolu(sop->ofp, sop->xr, sop->yr);
821                                                  /* play catch-up */
822 <                for (i = accumulate ? 0 : lastdone; i--; ) {
822 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
823                          int     j = sop->reclen;
824                          if (j <= 0) j = 1;
825                          while (j--)
# Line 826 | Line 827 | getostream(const char *ospec, const char *mname, int b
827                          if (outfmt == 'a')
828                                  putc('\n', sop->ofp);
829                  }
830 <                if (xres > 0)
830 >                if (waitflush > 0)
831                          fflush(sop->ofp);
832          }
833          sop->reclen += noopen;                  /* add to length if noopen */
# Line 956 | Line 957 | put_contrib(const DCOLOR cnt, FILE *fout)
957          default:
958                  error(INTERNAL, "botched output format");
959          }
960 +        if (waitflush < 0 && frandom() < 0.001)
961 +                fflush(fout);                   /* staggers writes */
962   }
963  
964   /* output ray tallies and clear for next accumulation */
965   void
966 < done_contrib(void)
966 > done_contrib(int navg)
967   {
968 +        double          sf = 1.;
969          int             i, j;
970          MODCONT         *mp;
971          STREAMOUT       *sop;
972 +                                                /* set average scaling */
973 +        if (navg > 1)
974 +                sf = 1. / (double)navg;
975                                                  /* output modifiers in order */
976          for (i = 0; i < nmods; i++) {
977                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
978 +                if (navg > 1)                   /* average scaling */
979 +                        for (j = mp->nbins; j--; )
980 +                                scalecolor(mp->cbin[j], sf);
981                  sop = getostream(mp->outspec, mp->modname, 0,0);
982                  put_contrib(mp->cbin[0], sop->ofp);
983                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 986 | Line 996 | done_contrib(void)
996          if (using_stdout & (outfmt == 'a'))
997                  putc('\n', stdout);
998          if (!waitflush) {
999 <                waitflush = xres;
999 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
1000                  if (using_stdout)
1001                          fflush(stdout);
1002          }
# Line 1051 | Line 1061 | process_queue(void)
1061                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1062                          add_contrib(modname);
1063                  }
1064 <                if (!accumulate)
1065 <                        done_contrib(); /* sum up contributions & output */
1064 >                                        /* time to produce record? */
1065 >                if (account > 0 && !--account)
1066 >                        done_contrib(account = accumulate);
1067                  lastdone = rtp->raynum;
1068                  if (rtp->buf != NULL)   /* free up buffer space */
1069                          free(rtp->buf);
# Line 1139 | Line 1150 | get_rproc(void)
1150   void
1151   trace_contribs(FILE *fin)
1152   {
1153 +        static int      ignore_warning_given = 0;
1154          char            inpbuf[128];
1155          int             iblen;
1156          struct rtproc   *rtp;
1157                                                  /* loop over input */
1158          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1159 <                if (!iblen ||                   /* need reset? */
1159 >                if (!iblen && accumulate != 1) {
1160 >                        if (!ignore_warning_given++)
1161 >                                error(WARNING,
1162 >                                "dummy ray(s) ignored during accumulation\n");
1163 >                        continue;
1164 >                }
1165 >                if (!iblen ||                   /* need flush/reset? */
1166                                  queue_length() > 10*nrtprocs() ||
1167                                  lastray+1 < lastray) {
1168                          while (wait_rproc() != NULL)
# Line 1157 | Line 1175 | trace_contribs(FILE *fin)
1175                  if (iblen) {                    /* trace ray if valid */
1176                          writebuf(rtp->pd.w, inpbuf, iblen);
1177                  } else {                        /* else bypass dummy ray */
1178 <                        queue_raytree(rtp);     /* empty tree */
1179 <                        if ((yres <= 0) | (waitflush > 1))
1180 <                                waitflush = 1;  /* flush after this */
1178 >                        queue_raytree(rtp);     /* queue empty ray/record */
1179 >                        if ((yres <= 0) | (xres <= 0))
1180 >                                waitflush = 1;  /* flush right after */
1181                  }
1182                  process_queue();                /* catch up with results */
1183                  if (raysleft && !--raysleft)
# Line 1167 | Line 1185 | trace_contribs(FILE *fin)
1185          }
1186          while (wait_rproc() != NULL)            /* process outstanding rays */
1187                  process_queue();
1188 <        if (accumulate)
1189 <                done_contrib();                 /* output tallies */
1188 >        if (accumulate <= 0)
1189 >                done_contrib(0);                /* output tallies */
1190 >        else if (account < accumulate) {
1191 >                error(WARNING, "partial accumulation in final record");
1192 >                done_contrib(accumulate - account);
1193 >        }
1194          if (raysleft)
1195                  error(USER, "unexpected EOF on input");
1196          lu_done(&ofiletab);                     /* close output files */
# Line 1210 | Line 1232 | myclose(const LUENT *e, void *p)
1232          STREAMOUT       *sop = (STREAMOUT *)e->data;
1233          
1234          if (sop->ofp == NULL)
1235 <                return;
1235 >                return(0);
1236          fclose(sop->ofp);
1237          sop->ofp = NULL;
1238 +        return(0);
1239   }
1240  
1241   /* load previously accumulated values */
# Line 1463 | Line 1486 | recover_output(FILE *fin)
1486                  error(WARNING, "no output files to recover");
1487                  return;
1488          }
1489 <        if (raysleft && lastout >= raysleft) {
1489 >        if (raysleft && lastout >= raysleft/accumulate) {
1490                  error(WARNING, "output appears to be complete");
1491                  /* XXX should read & discard input? */
1492                  quit(0);
# Line 1475 | Line 1498 | recover_output(FILE *fin)
1498          for (nvals = 0; nvals < lastout; nvals++)
1499                  if (getinp(oname, fin) < 0)
1500                          error(USER, "unexpected EOF on input");
1501 <        lastray = lastdone = (unsigned long)lastout;
1501 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1502          if (raysleft)
1503                  raysleft -= lastray;
1504   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines