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.47 by greg, Sat Nov 17 15:57:28 2007 UTC vs.
Revision 1.65 by greg, Wed Oct 5 17:20:55 2011 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include  "lookup.h"
17   #include  "calcomp.h"
18  
19 + #ifdef _WIN32
20 + typedef long    ssize_t;
21 + #endif
22 +
23   #ifndef MAXMODLIST
24   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
25   #endif
26  
27 < int     treebufsiz = BUFSIZ;            /* current tree buffer size */
27 > ssize_t treebufsiz = BUFSIZ;            /* current tree buffer size */
28  
29   typedef double  DCOLOR[3];              /* double-precision color */
30  
# Line 92 | Line 96 | struct rtproc {
96          struct rtproc   *next;          /* next in list of processes */
97          SUBPROC         pd;             /* rtrace pipe descriptors */
98          unsigned long   raynum;         /* ray number for this tree */
99 <        int             bsiz;           /* ray tree buffer length */
99 >        size_t          bsiz;           /* ray tree buffer length */
100          char            *buf;           /* ray tree buffer */
101 <        int             nbr;            /* number of bytes from rtrace */
101 >        size_t          nbr;            /* number of bytes from rtrace */
102   };                              /* rtrace process buffer */
103  
104                                          /* rtrace command and defaults */
105   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
106 <                                "-dj", ".5", "-dr", "3",
106 >                                "-dj", ".9", "-dr", "3",
107                                  "-ab", "1", "-ad", "350", };
108  
109   int  rtargc = 9;
# Line 107 | Line 111 | int  rtargc = 9;
111   char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
112                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
113  
114 < #define RTCOEFF         "-o~~TmWdp"     /* compute coefficients only */
115 < #define RTCONTRIB       "-o~~TmVdp"     /* compute ray contributions */
114 > #define RTCOEFF         "-o~~~TmWdp"    /* compute coefficients only */
115 > #define RTCONTRIB       "-o~~~TmVdp"    /* compute ray contributions */
116  
117   struct rtproc   rt0;                    /* head of rtrace process list */
118  
# Line 131 | Line 135 | int            inpfmt = 'a';           /* input format */
135   int             outfmt = 'a';           /* output format */
136  
137   int             header = 1;             /* output header? */
134 int             accumulate = 0;         /* accumulate ray values? */
138   int             force_open = 0;         /* truncate existing output? */
139   int             recover = 0;            /* recover previous output? */
140 + int             accumulate = 1;         /* input rays per output record */
141   int             xres = 0;               /* horiz. output resolution */
142   int             yres = 0;               /* vert. output resolution */
143  
144 + int             account;                /* current accumulation count */
145   unsigned long   raysleft;               /* number of rays left to trace */
146   long            waitflush;              /* how long until next flush */
147  
# Line 166 | Line 171 | void process_queue(void);
171  
172   void put_contrib(const DCOLOR cnt, FILE *fout);
173   void add_contrib(const char *modn);
174 < void done_contrib(void);
174 > void done_contrib(int navg);
175  
176   /* return number of open rtrace processes */
177   static int
# Line 245 | Line 250 | main(int argc, char *argv[])
250                  while ((j = expandarg(&argc, &argv, i)) > 0)
251                          ;
252                  if (j < 0) {
253 <                        fprintf(stderr, "%s: cannot expand '%s'",
253 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
254                                          argv[0], argv[i]);
255                          exit(1);
256                  }
# Line 274 | Line 279 | main(int argc, char *argv[])
279                                          continue;
280                                  }
281                                  break;
282 <                        case 'c':               /* accumulate ray values */
283 <                                switch (argv[i][2]) {
284 <                                case '\0':
285 <                                        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;
282 >                        case 'c':               /* input rays per output */
283 >                                if (argv[i][2] || i >= argc-2) break;
284 >                                accumulate = atoi(argv[++i]);
285 >                                continue;
286                          case 'r':               /* recover output */
287                                  if (argv[i][2]) break;
288                                  recover = 1;
# Line 352 | Line 344 | main(int argc, char *argv[])
344                          case 'b':               /* bin expression/count */
345                                  if (i >= argc-2) break;
346                                  if (argv[i][2] == 'n') {
347 <                                        bincnt = atoi(argv[++i]);
347 >                                        bincnt = (int)(eval(argv[++i]) + .5);
348                                          continue;
349                                  }
350                                  if (argv[i][2]) break;
# Line 380 | Line 372 | main(int argc, char *argv[])
372                          }
373                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
374          }
375 <        if (accumulate)         /* no output flushing for single record */
375 >        if (accumulate <= 0)    /* no output flushing for single record */
376                  xres = yres = 0;
377                                  /* set global argument list */
378          gargc = argc; gargv = argv;
# Line 390 | Line 382 | main(int argc, char *argv[])
382          rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
383                                  /* just asking for defaults? */
384          if (!strcmp(argv[i], "-defaults")) {
385 <                char    sxres[16], syres[16];
385 >                char    nps[8], sxres[16], syres[16];
386                  char    *rtpath;
387 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
387 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
388 >                                accumulate);
389                  printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
390                                  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");
391                  fflush(stdout);                 /* report OUR options */
392 +                rtargv[rtargc++] = "-n";
393 +                sprintf(nps, "%d", nprocs);
394 +                rtargv[rtargc++] = nps;
395                  rtargv[rtargc++] = header ? "-h+" : "-h-";
396                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
397                  rtargv[rtargc++] = fmt;
# Line 554 | Line 547 | init(int np)
547                          raysleft = yres;
548          } else
549                  raysleft = 0;
550 <        waitflush = xres;
550 >        if ((account = accumulate) > 0)
551 >                raysleft *= accumulate;
552 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
553          if (!recover)
554                  return;
555                                          /* recover previous values */
556 <        if (accumulate)
556 >        if (accumulate <= 0)
557                  reload_output();
558          else
559                  recover_output(stdin);
# Line 591 | Line 586 | addmodifier(char *modn, char *outf, char *binv, int bi
586                  error(USER, errmsg);
587          }
588          if (nmods >= MAXMODLIST)
589 <                error(USER, "too many modifiers");
589 >                error(INTERNAL, "too many modifiers");
590          modname[nmods++] = modn;        /* XXX assumes static string */
591          lep->key = modn;                /* XXX assumes static string */
592          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 599 | Line 594 | addmodifier(char *modn, char *outf, char *binv, int bi
594                  error(SYSTEM, "out of memory in addmodifier");
595          mp->outspec = outf;             /* XXX assumes static string */
596          mp->modname = modn;             /* XXX assumes static string */
597 <        if (binv != NULL)
598 <                mp->binv = eparse(binv);
599 <        else
600 <                mp->binv = eparse("0");
601 <        mp->nbins = 1;
597 >        if (binv == NULL)
598 >                binv = "0";             /* use single bin if unspecified */
599 >        mp->binv = eparse(binv);
600 >        if (mp->binv->type == NUM) {    /* check value if constant */
601 >                bincnt = (int)(evalue(mp->binv) + 1.5);
602 >                if (bincnt != 1) {
603 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
604 >                                        binv);
605 >                        error(USER, errmsg);
606 >                }
607 >        }
608 >        mp->nbins = 1;                  /* initialize results holder */
609          setcolor(mp->cbin[0], 0., 0., 0.);
610 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
609 <                bincnt = 1;
610 <        else if (bincnt > 1)
610 >        if (bincnt > 1)
611                  mp = growmodifier(mp, bincnt);
612          lep->data = (char *)mp;
613                                          /* allocate output streams */
# Line 670 | Line 670 | ofname(char *oname, const char *ospec, const char *mna
670                                  mnp = cp;
671                                  break;
672                          case 'd':
673 +                        case 'i':
674 +                        case 'o':
675 +                        case 'x':
676 +                        case 'X':
677                                  if (bnp != NULL)
678                                          return -1;
679                                  bnp = cp;
# Line 702 | Line 706 | void
706   printheader(FILE *fout, const char *info)
707   {
708          extern char     VersionID[];
709 <        FILE            *fin = fopen(octree, "r");
710 <        
711 <        if (fin == NULL)
712 <                quit(1);
713 <        checkheader(fin, "ignore", fout);       /* copy octree header */
714 <        fclose(fin);
709 >                                                /* copy octree header */
710 >        if (octree[0] == '!') {
711 >                newheader("RADIANCE", fout);
712 >                fputs(octree+1, fout);
713 >                if (octree[strlen(octree)-1] != '\n')
714 >                        fputc('\n', fout);
715 >        } else {
716 >                FILE    *fin = fopen(octree, "r");
717 >                if (fin == NULL)
718 >                        quit(1);
719 >                checkheader(fin, "ignore", fout);
720 >                fclose(fin);
721 >        }
722          printargs(gargc-1, gargv, fout);        /* add our command */
723          fprintf(fout, "SOFTWARE= %s\n", VersionID);
724          fputnow(fout);
# Line 736 | Line 747 | printresolu(FILE *fout, int xr, int yr)
747   {
748          if ((xr > 0) & (yr > 0))        /* resolution string */
749                  fprtresolu(xr, yr, fout);
739        if (xres > 0)                   /* global flush flag */
740                fflush(fout);
750   }
751  
752   /* Get output stream pointer (open and write header if new and noopen==0) */
# Line 758 | Line 767 | getostream(const char *ospec, const char *mname, int b
767                          if (header)
768                                  printheader(stdout, NULL);
769                          printresolu(stdout, xres, yres);
770 +                        if (waitflush > 0)
771 +                                fflush(stdout);
772                          stdos.xr = xres; stdos.yr = yres;
773                          using_stdout = 1;
774                  }
# Line 813 | Line 824 | getostream(const char *ospec, const char *mname, int b
824                          *cp = '\0';
825                          printheader(sop->ofp, info);
826                  }
827 <                if (!accumulate) {              /* global res. for -c- */
827 >                if (accumulate > 0) {           /* global resolution */
828                          sop->xr = xres; sop->yr = yres;
829                  }
830                  printresolu(sop->ofp, sop->xr, sop->yr);
831                                                  /* play catch-up */
832 <                for (i = accumulate ? 0 : lastdone; i--; ) {
832 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
833                          int     j = sop->reclen;
834                          if (j <= 0) j = 1;
835                          while (j--)
# Line 826 | Line 837 | getostream(const char *ospec, const char *mname, int b
837                          if (outfmt == 'a')
838                                  putc('\n', sop->ofp);
839                  }
840 <                if (xres > 0)
840 >                if (waitflush > 0)
841                          fflush(sop->ofp);
842          }
843          sop->reclen += noopen;                  /* add to length if noopen */
# Line 960 | Line 971 | put_contrib(const DCOLOR cnt, FILE *fout)
971  
972   /* output ray tallies and clear for next accumulation */
973   void
974 < done_contrib(void)
974 > done_contrib(int navg)
975   {
976 +        double          sf = 1.;
977          int             i, j;
978          MODCONT         *mp;
979          STREAMOUT       *sop;
980 +                                                /* set average scaling */
981 +        if (navg > 1)
982 +                sf = 1. / (double)navg;
983                                                  /* output modifiers in order */
984          for (i = 0; i < nmods; i++) {
985                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
986 +                if (navg > 1)                   /* average scaling */
987 +                        for (j = mp->nbins; j--; )
988 +                                scalecolor(mp->cbin[j], sf);
989                  sop = getostream(mp->outspec, mp->modname, 0,0);
990                  put_contrib(mp->cbin[0], sop->ofp);
991                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 986 | Line 1004 | done_contrib(void)
1004          if (using_stdout & (outfmt == 'a'))
1005                  putc('\n', stdout);
1006          if (!waitflush) {
1007 <                waitflush = xres;
1007 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
1008                  if (using_stdout)
1009                          fflush(stdout);
1010          }
# Line 1051 | Line 1069 | process_queue(void)
1069                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1070                          add_contrib(modname);
1071                  }
1072 <                if (!accumulate)
1073 <                        done_contrib(); /* sum up contributions & output */
1072 >                                        /* time to produce record? */
1073 >                if (account > 0 && !--account)
1074 >                        done_contrib(account = accumulate);
1075                  lastdone = rtp->raynum;
1076                  if (rtp->buf != NULL)   /* free up buffer space */
1077                          free(rtp->buf);
# Line 1067 | Line 1086 | wait_rproc(void)
1086   {
1087          struct rtproc   *rtfree = NULL;
1088          fd_set          readset, errset;
1089 <        int             nr;
1089 >        ssize_t         nr;
1090          struct rtproc   *rt;
1091          int             n;
1092          
# Line 1098 | Line 1117 | wait_rproc(void)
1117                                  continue;
1118                          if (rt->buf == NULL) {
1119                                  rt->bsiz = treebufsiz;
1120 <                                rt->buf = (char *)malloc(treebufsiz);
1120 >                                rt->buf = (char *)malloc(rt->bsiz);
1121                          } else if (rt->nbr + BUFSIZ > rt->bsiz) {
1122                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1123                                          rt->bsiz = treebufsiz;
1124 <                                else
1125 <                                        treebufsiz = rt->bsiz += BUFSIZ;
1124 >                                else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0)
1125 >                                        error(INTERNAL,
1126 >                                            "ray buffer does not fit memory");
1127                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1128                          }
1129                          if (rt->buf == NULL)
1130                                  error(SYSTEM, "out of memory in wait_rproc");
1131 <                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
1132 <                        if (nr <= 0)
1131 >                        nr = rt->bsiz - rt->nbr;
1132 >                        if (nr & ~0x7fffffff)   /* avoid 32-bit OS issues */
1133 >                                nr = 0x7fffffff;
1134 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, nr);
1135 >                        if (nr < 0)
1136 >                                error(SYSTEM, "read error from rtrace");
1137 >                        if (!nr)
1138                                  error(USER, "rtrace process died");
1139                          rt->nbr += nr;          /* advance & check */
1140 <                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
1141 <                                                        "~\t~\t", 4)) {
1142 <                                rt->nbr -= 4;   /* elide terminator */
1140 >                        if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6,
1141 >                                                        "~\t~\t~\t", 6)) {
1142 >                                rt->nbr -= 6;   /* elide terminator */
1143                                  queue_raytree(rt);
1144                                  rtfree = rt;    /* ready for next ray */
1145                          }
# Line 1139 | Line 1164 | get_rproc(void)
1164   void
1165   trace_contribs(FILE *fin)
1166   {
1167 +        static int      ignore_warning_given = 0;
1168          char            inpbuf[128];
1169          int             iblen;
1170          struct rtproc   *rtp;
1171                                                  /* loop over input */
1172          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1173 <                if (!iblen ||                   /* need reset? */
1173 >                if (!iblen && accumulate != 1) {
1174 >                        if (!ignore_warning_given++)
1175 >                                error(WARNING,
1176 >                                "dummy ray(s) ignored during accumulation\n");
1177 >                        continue;
1178 >                }
1179 >                if (!iblen ||                   /* need flush/reset? */
1180                                  queue_length() > 10*nrtprocs() ||
1181                                  lastray+1 < lastray) {
1182                          while (wait_rproc() != NULL)
1183                                  process_queue();
1184 <                        if (lastray+1 < lastray)
1153 <                                lastdone = lastray = 0;
1184 >                        lastdone = lastray = 0;
1185                  }
1186                  rtp = get_rproc();              /* get avail. rtrace process */
1187                  rtp->raynum = ++lastray;        /* assign ray */
1188                  if (iblen) {                    /* trace ray if valid */
1189                          writebuf(rtp->pd.w, inpbuf, iblen);
1190                  } else {                        /* else bypass dummy ray */
1191 <                        queue_raytree(rtp);     /* empty tree */
1192 <                        if ((yres <= 0) | (waitflush > 1))
1193 <                                waitflush = 1;  /* flush after this */
1191 >                        queue_raytree(rtp);     /* queue empty ray/record */
1192 >                        if ((yres <= 0) | (xres <= 0))
1193 >                                waitflush = 1;  /* flush right after */
1194                  }
1195                  process_queue();                /* catch up with results */
1196                  if (raysleft && !--raysleft)
# Line 1167 | Line 1198 | trace_contribs(FILE *fin)
1198          }
1199          while (wait_rproc() != NULL)            /* process outstanding rays */
1200                  process_queue();
1201 <        if (accumulate)
1202 <                done_contrib();                 /* output tallies */
1201 >        if (accumulate <= 0)
1202 >                done_contrib(0);                /* output tallies */
1203 >        else if (account < accumulate) {
1204 >                error(WARNING, "partial accumulation in final record");
1205 >                done_contrib(accumulate - account);
1206 >        }
1207          if (raysleft)
1208                  error(USER, "unexpected EOF on input");
1209          lu_done(&ofiletab);                     /* close output files */
# Line 1210 | Line 1245 | myclose(const LUENT *e, void *p)
1245          STREAMOUT       *sop = (STREAMOUT *)e->data;
1246          
1247          if (sop->ofp == NULL)
1248 <                return;
1248 >                return(0);
1249          fclose(sop->ofp);
1250          sop->ofp = NULL;
1251 +        return(0);
1252   }
1253  
1254   /* load previously accumulated values */
# Line 1293 | Line 1329 | reload_output(void)
1329                                          error(USER, errmsg);
1330                                  }
1331                          }
1296                        if (oent->key == NULL)  /* new file entry */
1297                                oent->key = strcpy((char *)
1298                                                malloc(strlen(oname)+1), oname);
1299                        if (oent->data == NULL)
1300                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1301                        *(STREAMOUT *)oent->data = sout;
1332                                                          /* read in RGB value */
1333                          if (!get_contrib(rgbv, sout.ofp)) {
1334 <                                if (!j)
1334 >                                if (!j) {
1335 >                                        fclose(sout.ofp);
1336                                          break;          /* ignore empty file */
1337 +                                }
1338                                  if (j < mp->nbins) {
1339                                          sprintf(errmsg, "missing data in '%s'",
1340                                                          oname);
# Line 1313 | Line 1345 | reload_output(void)
1345                          if (j >= mp->nbins)             /* grow modifier size */
1346                                  ment->data = (char *)(mp = growmodifier(mp, j+1));
1347                          copycolor(mp->cbin[j], rgbv);
1348 +                        if (oent->key == NULL)          /* new file entry */
1349 +                                oent->key = strcpy((char *)
1350 +                                                malloc(strlen(oname)+1), oname);
1351 +                        if (oent->data == NULL)
1352 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1353 +                        *(STREAMOUT *)oent->data = sout;
1354                  }
1355          }
1356          lu_doall(&ofiletab, myclose, NULL);     /* close all files */
# Line 1388 | Line 1426 | recover_output(FILE *fin)
1426                          } else {
1427                                  sout.reclen = 0;
1428                                  sout.outpipe = 0;
1391                                sout.xr = xres; sout.yr = yres;
1429                                  sout.ofp = NULL;
1430                          }
1431                          if (sout.ofp != NULL) { /* already open? */
# Line 1429 | Line 1466 | recover_output(FILE *fin)
1466                                                  oname);
1467                                  error(USER, errmsg);
1468                          }
1469 <                        if ((xres > 0) & (yres > 0) &&
1469 >                        sout.xr = xres; sout.yr = yres;
1470 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1471                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1472 <                                                (xr != xres) |
1473 <                                                (yr != yres))) {
1472 >                                                (xr != sout.xr) |
1473 >                                                (yr != sout.yr))) {
1474                                  sprintf(errmsg, "resolution mismatch for '%s'",
1475                                                  oname);
1476                                  error(USER, errmsg);
# Line 1461 | Line 1499 | recover_output(FILE *fin)
1499                  error(WARNING, "no output files to recover");
1500                  return;
1501          }
1502 <        if (raysleft && lastout >= raysleft) {
1502 >        if (raysleft && lastout >= raysleft/accumulate) {
1503                  error(WARNING, "output appears to be complete");
1504                  /* XXX should read & discard input? */
1505                  quit(0);
# Line 1473 | Line 1511 | recover_output(FILE *fin)
1511          for (nvals = 0; nvals < lastout; nvals++)
1512                  if (getinp(oname, fin) < 0)
1513                          error(USER, "unexpected EOF on input");
1514 <        lastray = lastdone = (unsigned long)lastout;
1514 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1515          if (raysleft)
1516                  raysleft -= lastray;
1517   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines