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.11 by greg, Thu Jun 2 18:51:46 2005 UTC vs.
Revision 1.25 by greg, Tue Sep 13 22:58:22 2005 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include  "lookup.h"
17   #include  "calcomp.h"
18  
19 + #ifndef MAXMODLIST
20   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
21 + #endif
22  
23   int     treebufsiz = BUFSIZ;            /* current tree buffer size */
24  
# Line 49 | Line 51 | static void closefile(void *p) { fclose((FILE *)p); }
51  
52   LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
53  
54 + #define OF_MODIFIER     01
55 + #define OF_BIN          02
56 +
57   FILE *getofile(const char *ospec, const char *mname, int bn);
58 + int ofname(char *oname, const char *ospec, const char *mname, int bn);
59 + void printheader(FILE *fout, const char *info);
60  
61   /*
62   * The rcont structure is used to manage i/o with a particular
# Line 67 | Line 74 | struct rtproc {
74   };                              /* rtrace process buffer */
75  
76                                          /* rtrace command and defaults */
77 < char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
78 <                                "-ab", "1", "-ad", "128", "-lr", "-10", };
79 < int  rtargc = 11;
77 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
78 >                                "-dj", ".5", "-dr", "3",
79 >                                "-ab", "1", "-ad", "128", };
80 > int  rtargc = 9;
81                                          /* overriding rtrace options */
82   char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
83                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
# Line 93 | Line 101 | int            header = 1;             /* output header? */
101   int             xres = 0;               /* horiz. output resolution */
102   int             yres = 0;               /* vert. output resolution */
103  
104 < long            raysleft;               /* number of rays left to trace */
104 > unsigned long   raysleft;               /* number of rays left to trace */
105   long            waitflush;              /* how long until next flush */
106  
107   unsigned long   lastray = 0;            /* last ray number sent */
# Line 105 | Line 113 | const char     *modname[MAXMODLIST];   /* ordered modifier n
113   int             nmods = 0;              /* number of modifiers */
114  
115   MODCONT *addmodifier(char *modn, char *outf, char *binv);
116 + void addmodfile(char *fname, char *outf, char *binv);
117  
118   void init(int np);
119   int done_rprocs(struct rtproc *rtp);
120 < void trace_contribs(FILE *fp);
120 > void recover_output(FILE *fin);
121 > void trace_contribs(FILE *fin);
122   struct rtproc *wait_rproc(void);
123   struct rtproc *get_rproc(void);
124   void queue_raytree(struct rtproc *rtp);
125   void process_queue(void);
126  
127 < void putcontrib(const DCOLOR cnt, FILE *fout);
127 > void put_contrib(const DCOLOR cnt, FILE *fout);
128   void add_contrib(const char *modn);
129   void done_contrib(void);
130  
# Line 158 | Line 168 | int
168   main(int argc, char *argv[])
169   {
170          int     nprocs = 1;
171 +        int     recover = 0;
172          char    *curout = NULL;
173          char    *binval = NULL;
174          char    fmt[8];
175          int     i, j;
176 +                                /* need at least one argument */
177 +        if (argc < 2) {
178 +                fprintf(stderr,
179 + "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
180 +                        argv[0]);
181 +                exit(1);
182 +        }
183                                  /* global program name */
184          gargv = argv;
185                                  /* initialize calcomp routines */
# Line 180 | Line 198 | main(int argc, char *argv[])
198                  }
199                  if (argv[i][0] == '-')
200                          switch (argv[i][1]) {
201 +                        case 'r':               /* recover output */
202 +                                if (argv[i][2]) break;
203 +                                recover++;
204 +                                continue;
205                          case 'n':               /* number of processes */
206 <                                if (argv[i][2] || i >= argc-1) break;
206 >                                if (argv[i][2] || i >= argc-2) break;
207                                  nprocs = atoi(argv[++i]);
208                                  if (nprocs <= 0)
209                                          error(USER, "illegal number of processes");
# Line 206 | Line 228 | main(int argc, char *argv[])
228                          case 'f':               /* file or i/o format */
229                                  if (!argv[i][2]) {
230                                          char    *fpath;
231 <                                        if (i >= argc-1) break;
231 >                                        if (i >= argc-2) break;
232                                          fpath = getpath(argv[++i],
233                                                          getrlibpath(), R_OK);
234                                          if (fpath == NULL) {
# Line 221 | Line 243 | main(int argc, char *argv[])
243                                  setformat(argv[i]+2);
244                                  continue;
245                          case 'e':               /* expression */
246 <                                if (argv[i][2] || i >= argc-1) break;
246 >                                if (argv[i][2] || i >= argc-2) break;
247                                  scompile(argv[++i], NULL, 0);
248                                  continue;
249                          case 'o':               /* output file spec. */
250 <                                if (argv[i][2] || i >= argc-1) break;
250 >                                if (argv[i][2] || i >= argc-2) break;
251                                  curout = argv[++i];
252                                  continue;
253                          case 'x':               /* horiz. output resolution */
254 <                                if (argv[i][2] || i >= argc-1) break;
254 >                                if (argv[i][2] || i >= argc-2) break;
255                                  xres = atoi(argv[++i]);
256                                  continue;
257                          case 'y':               /* vert. output resolution */
258 <                                if (argv[i][2] || i >= argc-1) break;
258 >                                if (argv[i][2] || i >= argc-2) break;
259                                  yres = atoi(argv[++i]);
260                                  continue;
261                          case 'b':               /* bin expression */
262 <                                if (argv[i][2] || i >= argc-1) break;
262 >                                if (argv[i][2] || i >= argc-2) break;
263                                  binval = argv[++i];
264                                  continue;
265                          case 'm':               /* modifier name */
266 <                                if (argv[i][2] || i >= argc-1) break;
266 >                                if (argv[i][2] || i >= argc-2) break;
267                                  rtargv[rtargc++] = "-ti";
268                                  rtargv[rtargc++] = argv[++i];
269                                  addmodifier(argv[i], curout, binval);
270                                  continue;
271 +                        case 'M':               /* modifier file */
272 +                                if (argv[i][2] || i >= argc-2) break;
273 +                                rtargv[rtargc++] = "-tI";
274 +                                rtargv[rtargc++] = argv[++i];
275 +                                addmodfile(argv[i], curout, binval);
276 +                                continue;
277                          }
278                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
279          }
# Line 294 | Line 322 | main(int argc, char *argv[])
322                  error(USER, "missing octree argument");
323          rtargv[rtargc++] = octree = argv[i];
324          rtargv[rtargc] = NULL;
325 <                                /* start rtrace & compute contributions */
325 >                                /* start rtrace */
326          init(nprocs);
327 <        trace_contribs(stdin);
327 >        if (recover)            /* perform recovery if requested */
328 >                recover_output(stdin);
329 >        trace_contribs(stdin);  /* compute contributions */
330          quit(0);
331   }
332  
# Line 401 | Line 431 | init(int np)
431          rtp->next = NULL;               /* terminate list */
432          if (yres > 0) {
433                  if (xres > 0)
434 <                        raysleft = xres*yres;
434 >                        raysleft = (unsigned long)xres*yres;
435                  else
436                          raysleft = yres;
437          } else
# Line 439 | Line 469 | addmodifier(char *modn, char *outf, char *binv)
469          return mp;
470   }
471  
472 + /* add modifiers from a file list */
473 + void
474 + addmodfile(char *fname, char *outf, char *binv)
475 + {
476 +        char    *mname[MAXMODLIST];
477 +        int     i;
478 +                                        /* find the file & store strings */
479 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
480 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
481 +                error(SYSTEM, errmsg);
482 +        }
483 +        for (i = 0; mname[i]; i++)      /* add each one */
484 +                addmodifier(mname[i], outf, binv);
485 + }
486 +
487   /* put string to stderr */
488   void
489   eputs(char  *s)
# Line 454 | Line 499 | eputs(char  *s)
499          midline = s[strlen(s)-1] != '\n';
500   }
501  
502 + /* construct output file name and return flags whether modifier/bin present */
503 + int
504 + ofname(char *oname, const char *ospec, const char *mname, int bn)
505 + {
506 +        const char      *mnp = NULL;
507 +        const char      *bnp = NULL;
508 +        const char      *cp;
509 +        
510 +        if (ospec == NULL)
511 +                return -1;
512 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
513 +                if (*cp == '%') {
514 +                        do
515 +                                ++cp;
516 +                        while (isdigit(*cp));
517 +                        switch (*cp) {
518 +                        case '%':
519 +                                break;
520 +                        case 's':
521 +                                if (mnp != NULL)
522 +                                        return -1;
523 +                                mnp = cp;
524 +                                break;
525 +                        case 'd':
526 +                                if (bnp != NULL)
527 +                                        return -1;
528 +                                bnp = cp;
529 +                                break;
530 +                        default:
531 +                                return -1;
532 +                        }
533 +                }
534 +        if (mnp != NULL) {                      /* create file name */
535 +                if (bnp != NULL) {
536 +                        if (bnp > mnp)
537 +                                sprintf(oname, ospec, mname, bn);
538 +                        else
539 +                                sprintf(oname, ospec, bn, mname);
540 +                        return OF_MODIFIER|OF_BIN;
541 +                }
542 +                sprintf(oname, ospec, mname);
543 +                return OF_MODIFIER;
544 +        }
545 +        if (bnp != NULL) {
546 +                sprintf(oname, ospec, bn);
547 +                return OF_BIN;
548 +        }
549 +        strcpy(oname, ospec);
550 +        return 0;
551 + }
552 +
553   /* write header to the given output stream */
554   void
555 < printheader(FILE *fout)
555 > printheader(FILE *fout, const char *info)
556   {
557          extern char     VersionID[];
558          FILE            *fin = fopen(octree, "r");
# Line 468 | Line 564 | printheader(FILE *fout)
564          printargs(gargc-1, gargv, fout);        /* add our command */
565          fprintf(fout, "SOFTWARE= %s\n", VersionID);
566          fputnow(fout);
567 +        if (info != NULL)                       /* add extra info if given */
568 +                fputs(info, fout);
569          switch (outfmt) {                       /* add output format */
570          case 'a':
571                  fputformat("ascii", fout);
# Line 494 | Line 592 | printheader(FILE *fout)
592   FILE *
593   getofile(const char *ospec, const char *mname, int bn)
594   {
595 <        const char      *mnp = NULL;
596 <        const char      *bnp = NULL;
499 <        const char      *cp;
500 <        char            ofname[1024];
595 >        int             ofl;
596 >        char            oname[1024];
597          LUENT           *lep;
598          
599          if (ospec == NULL) {                    /* use stdout? */
# Line 505 | Line 601 | getofile(const char *ospec, const char *mname, int bn)
601                          if (outfmt != 'a')
602                                  SET_FILE_BINARY(stdout);
603                          if (header)
604 <                                printheader(stdout);
604 >                                printheader(stdout, NULL);
605                  }
606                  using_stdout = 1;
607                  return stdout;
608          }
609 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
610 <                if (*cp == '%') {
611 <                        do
612 <                                ++cp;
613 <                        while (isdigit(*cp));
614 <                        switch (*cp) {
519 <                        case '%':
520 <                                break;
521 <                        case 's':
522 <                                if (mnp != NULL)
523 <                                        goto badspec;
524 <                                mnp = cp;
525 <                                break;
526 <                        case 'd':
527 <                                if (bnp != NULL)
528 <                                        goto badspec;
529 <                                bnp = cp;
530 <                                break;
531 <                        default:
532 <                                goto badspec;
533 <                        }
534 <                }
535 <        if (mnp != NULL) {                      /* create file name */
536 <                if (bnp != NULL) {
537 <                        if (bnp > mnp)
538 <                                sprintf(ofname, ospec, mname, bn);
539 <                        else
540 <                                sprintf(ofname, ospec, bn, mname);
541 <                } else
542 <                        sprintf(ofname, ospec, mname);
543 <        } else if (bnp != NULL)
544 <                sprintf(ofname, ospec, bn);
545 <        else
546 <                strcpy(ofname, ospec);
547 <        lep = lu_find(&ofiletab, ofname);       /* look it up */
609 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
610 >        if (ofl < 0) {
611 >                sprintf(errmsg, "bad output format '%s'", ospec);
612 >                error(USER, errmsg);
613 >        }
614 >        lep = lu_find(&ofiletab, oname);        /* look it up */
615          if (lep->key == NULL)                   /* new entry */
616 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
616 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
617          if (lep->data == NULL) {                /* open output file */
618                  FILE            *fp;
619                  int             i;
620 <                if (ofname[0] == '!')           /* output to command */
621 <                        fp = popen(ofname+1, "w");
620 >                if (oname[0] == '!')            /* output to command */
621 >                        fp = popen(oname+1, "w");
622                  else
623 <                        fp = fopen(ofname, "w");
623 >                        fp = fopen(oname, "w");
624                  if (fp == NULL) {
625 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
625 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
626                          error(SYSTEM, errmsg);
627                  }
628                  if (outfmt != 'a')
629                          SET_FILE_BINARY(fp);
630 <                if (header)
631 <                        printheader(fp);
630 >                if (header) {
631 >                        char    info[512];
632 >                        char    *cp = info;
633 >                        sprintf(cp, "MODIFIER=%s\n", mname);
634 >                        while (*cp) ++cp;
635 >                        if (ofl & OF_BIN) {
636 >                                sprintf(cp, "BIN=%d\n", bn);
637 >                                while (*cp) ++cp;
638 >                        }
639 >                        *cp = '\0';
640 >                        printheader(fp, info);
641 >                }
642                                                  /* play catch-up */
643                  for (i = 0; i < lastdone; i++) {
644                          static const DCOLOR     nocontrib = BLKCOLOR;
645 <                        putcontrib(nocontrib, fp);
645 >                        put_contrib(nocontrib, fp);
646                          if (outfmt == 'a')
647                                  putc('\n', fp);
648                  }
# Line 574 | Line 651 | getofile(const char *ospec, const char *mname, int bn)
651                  lep->data = (char *)fp;
652          }
653          return (FILE *)lep->data;               /* return open file pointer */
577 badspec:
578        sprintf(errmsg, "bad output format '%s'", ospec);
579        error(USER, errmsg);
580        return NULL;            /* pro forma return */
654   }
655  
656   /* read input ray into buffer */
# Line 671 | Line 744 | puteol(const LUENT *e, void *p)
744  
745   /* put out ray contribution to file */
746   void
747 < putcontrib(const DCOLOR cnt, FILE *fout)
747 > put_contrib(const DCOLOR cnt, FILE *fout)
748   {
749          float   fv[3];
750          COLR    cv;
# Line 704 | Line 777 | done_contrib(void)
777   {
778          int     i, j;
779          MODCONT *mp;
780 +        FILE    *fp;
781                                                  /* output modifiers in order */
782          for (i = 0; i < nmods; i++) {
783                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
784 <                for (j = 0; j < mp->nbins; j++)
785 <                        putcontrib(mp->cbin[j],
784 >                fp = getofile(mp->outspec, mp->modname, 0);
785 >                put_contrib(mp->cbin[0], fp);
786 >                if (mp->nbins > 3 &&            /* minor optimization */
787 >                                fp == getofile(mp->outspec, mp->modname, 1))
788 >                        for (j = 1; j < mp->nbins; j++)
789 >                                put_contrib(mp->cbin[j], fp);
790 >                else
791 >                        for (j = 1; j < mp->nbins; j++)
792 >                                put_contrib(mp->cbin[j],
793                                          getofile(mp->outspec, mp->modname, j));
794                                                  /* clear for next ray tree */
795                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
# Line 880 | Line 961 | trace_contribs(FILE *fin)
961                  rtp = get_rproc();              /* get avail. rtrace process */
962                  rtp->raynum = ++lastray;        /* assign ray to it */
963                  writebuf(rtp->pd.w, inpbuf, iblen);
964 <                if (!--raysleft)
964 >                if (raysleft && !--raysleft)
965                          break;
966                  process_queue();                /* catch up with results */
967          }
968          while (wait_rproc() != NULL)            /* process outstanding rays */
969                  process_queue();
970 <        if (raysleft > 0)
970 >        if (raysleft)
971                  error(USER, "unexpected EOF on input");
972 +        lu_done(&ofiletab);                     /* close output files */
973 + }
974 +
975 + /* seek on the given output file */
976 + static int
977 + myseeko(const LUENT *e, void *p)
978 + {
979 +        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
980 +                sprintf(errmsg, "seek error on file '%s'", e->key);
981 +                error(SYSTEM, errmsg);
982 +        }
983 + }
984 +
985 + /* recover output if possible */
986 + void
987 + recover_output(FILE *fin)
988 + {
989 +        off_t   lastout = -1;
990 +        int     outvsiz;
991 +        char    *outvfmt;
992 +        int     i, j;
993 +        MODCONT *mp;
994 +        int     ofl;
995 +        char    oname[1024];
996 +        LUENT   *ment, *oent;
997 +        FILE    *fp;
998 +        off_t   nvals;
999 +
1000 +        switch (outfmt) {
1001 +        case 'a':
1002 +                error(USER, "cannot recover ASCII output");
1003 +                return;
1004 +        case 'f':
1005 +                outvsiz = sizeof(float)*3;
1006 +                outvfmt = "float";
1007 +                break;
1008 +        case 'd':
1009 +                outvsiz = sizeof(double)*3;
1010 +                outvfmt = "double";
1011 +                break;
1012 +        case 'c':
1013 +                outvsiz = sizeof(COLR);
1014 +                outvfmt = COLRFMT;
1015 +                break;
1016 +        default:
1017 +                error(INTERNAL, "botched output format");
1018 +                return;
1019 +        }
1020 +                                                /* check modifier outputs */
1021 +        for (i = 0; i < nmods; i++) {
1022 +                ment = lu_find(&modconttab,modname[i]);
1023 +                mp = (MODCONT *)ment->data;
1024 +                if (mp->outspec == NULL)
1025 +                        error(USER, "cannot recover from stdout");
1026 +                for (j = 0; ; j++) {            /* check each bin's file */
1027 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1028 +                        if (ofl < 0)
1029 +                                error(USER, "bad output file specification");
1030 +                        oent = lu_find(&ofiletab, oname);
1031 +                        if (oent->data != NULL) { /* saw this one already */
1032 +                                if (ofl & OF_BIN)
1033 +                                        continue;
1034 +                                break;
1035 +                        }
1036 +                        if (oname[0] == '!')
1037 +                                error(USER, "cannot recover from command");
1038 +                                                /* open output */
1039 +                        fp = fopen(oname, "rb+");
1040 +                        if (fp == NULL) {
1041 +                                if (j)
1042 +                                        break;  /* assume end of modifier */
1043 +                                sprintf(errmsg, "missing recover file '%s'",
1044 +                                                oname);
1045 +                                error(USER, errmsg);
1046 +                        }
1047 +                        nvals = lseek(fileno(fp), 0, SEEK_END);
1048 +                        if (nvals <= 0) {
1049 +                                lastout = 0;    /* empty output, quit here */
1050 +                                fclose(fp);
1051 +                                break;
1052 +                        }
1053 +                        lseek(fileno(fp), 0, SEEK_SET);
1054 +                        if (header) {
1055 +                                int     xr, yr;
1056 +                                if (checkheader(fp, outvfmt, NULL) != 1) {
1057 +                                        sprintf(errmsg,
1058 +                                                "format mismatch for '%s'",
1059 +                                                        oname);
1060 +                                        error(USER, errmsg);
1061 +                                }
1062 +                                if (xres > 0 && yres > 0 &&
1063 +                                                (!fscnresolu(&xr, &yr, fp) ||
1064 +                                                        xr != xres ||
1065 +                                                        yr != yres)) {
1066 +                                        sprintf(errmsg,
1067 +                                                "resolution mismatch for '%s'",
1068 +                                                        oname);
1069 +                                        error(USER, errmsg);
1070 +                                }
1071 +                        }
1072 +                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1073 +                        if (lastout < 0 || nvals < lastout)
1074 +                                lastout = nvals;
1075 +                        if (oent->key == NULL) /* new entry */
1076 +                                oent->key = strcpy((char *)
1077 +                                                malloc(strlen(oname)+1), oname);
1078 +                        oent->data = (char *)fp;
1079 +                        if (!(ofl & OF_BIN))
1080 +                                break;          /* no bin separation */
1081 +                }
1082 +                if (!lastout) {                 /* empty output */
1083 +                        error(WARNING, "no previous data to recover");
1084 +                        lu_done(&ofiletab);     /* reclose all outputs */
1085 +                        return;
1086 +                }
1087 +                if (j > mp->nbins) {            /* preallocate modifier bins */
1088 +                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1089 +                                                        (j-1)*sizeof(DCOLOR));
1090 +                        if (mp == NULL)
1091 +                                error(SYSTEM, "out of memory in recover_output");
1092 +                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1093 +                        mp->nbins = j;
1094 +                        ment->data = (char *)mp;
1095 +                }
1096 +        }
1097 +        if (lastout < 0) {
1098 +                error(WARNING, "no output files to recover");
1099 +                return;
1100 +        }
1101 +        if (raysleft && lastout >= raysleft) {
1102 +                error(WARNING, "output appears to be complete");
1103 +                /* XXX should read & discard input? */
1104 +                quit(0);
1105 +        }
1106 +                                                /* seek on all files */
1107 +        nvals = lastout * outvsiz;
1108 +        lu_doall(&ofiletab, myseeko, &nvals);
1109 +                                                /* discard input */
1110 +        for (nvals = 0; nvals < lastout; nvals++)
1111 +                if (getinp(oname, fin) <= 0)
1112 +                        error(USER, "unexpected EOF on input");
1113 +        lastray = lastdone = (unsigned long)lastout;
1114 +        if (raysleft)
1115 +                raysleft -= lastray;
1116   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines