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.13 by greg, Sun Jun 5 19:52:01 2005 UTC vs.
Revision 1.23 by greg, Thu Jun 16 19:37:26 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",
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 */
# 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];
# Line 180 | Line 191 | main(int argc, char *argv[])
191                  }
192                  if (argv[i][0] == '-')
193                          switch (argv[i][1]) {
194 +                        case 'r':               /* recover output */
195 +                                if (argv[i][2]) break;
196 +                                recover++;
197 +                                continue;
198                          case 'n':               /* number of processes */
199 <                                if (argv[i][2] || i >= argc-1) break;
199 >                                if (argv[i][2] || i >= argc-2) break;
200                                  nprocs = atoi(argv[++i]);
201                                  if (nprocs <= 0)
202                                          error(USER, "illegal number of processes");
# Line 206 | Line 221 | main(int argc, char *argv[])
221                          case 'f':               /* file or i/o format */
222                                  if (!argv[i][2]) {
223                                          char    *fpath;
224 <                                        if (i >= argc-1) break;
224 >                                        if (i >= argc-2) break;
225                                          fpath = getpath(argv[++i],
226                                                          getrlibpath(), R_OK);
227                                          if (fpath == NULL) {
# Line 221 | Line 236 | main(int argc, char *argv[])
236                                  setformat(argv[i]+2);
237                                  continue;
238                          case 'e':               /* expression */
239 <                                if (argv[i][2] || i >= argc-1) break;
239 >                                if (argv[i][2] || i >= argc-2) break;
240                                  scompile(argv[++i], NULL, 0);
241                                  continue;
242                          case 'o':               /* output file spec. */
243 <                                if (argv[i][2] || i >= argc-1) break;
243 >                                if (argv[i][2] || i >= argc-2) break;
244                                  curout = argv[++i];
245                                  continue;
246                          case 'x':               /* horiz. output resolution */
247 <                                if (argv[i][2] || i >= argc-1) break;
247 >                                if (argv[i][2] || i >= argc-2) break;
248                                  xres = atoi(argv[++i]);
249                                  continue;
250                          case 'y':               /* vert. output resolution */
251 <                                if (argv[i][2] || i >= argc-1) break;
251 >                                if (argv[i][2] || i >= argc-2) break;
252                                  yres = atoi(argv[++i]);
253                                  continue;
254                          case 'b':               /* bin expression */
255 <                                if (argv[i][2] || i >= argc-1) break;
255 >                                if (argv[i][2] || i >= argc-2) break;
256                                  binval = argv[++i];
257                                  continue;
258                          case 'm':               /* modifier name */
259 <                                if (argv[i][2] || i >= argc-1) break;
259 >                                if (argv[i][2] || i >= argc-2) break;
260                                  rtargv[rtargc++] = "-ti";
261                                  rtargv[rtargc++] = argv[++i];
262                                  addmodifier(argv[i], curout, binval);
263                                  continue;
264 +                        case 'M':               /* modifier file */
265 +                                if (argv[i][2] || i >= argc-2) break;
266 +                                rtargv[rtargc++] = "-tI";
267 +                                rtargv[rtargc++] = argv[++i];
268 +                                addmodfile(argv[i], curout, binval);
269 +                                continue;
270                          }
271                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
272          }
# Line 296 | Line 317 | main(int argc, char *argv[])
317          rtargv[rtargc] = NULL;
318                                  /* start rtrace & compute contributions */
319          init(nprocs);
320 +        if (recover)            /* perform recovery if requested */
321 +                recover_output(stdin);
322          trace_contribs(stdin);
323          quit(0);
324   }
# Line 401 | Line 424 | init(int np)
424          rtp->next = NULL;               /* terminate list */
425          if (yres > 0) {
426                  if (xres > 0)
427 <                        raysleft = xres*yres;
427 >                        raysleft = (unsigned long)xres*yres;
428                  else
429                          raysleft = yres;
430          } else
# Line 439 | Line 462 | addmodifier(char *modn, char *outf, char *binv)
462          return mp;
463   }
464  
465 + /* add modifiers from a file list */
466 + void
467 + addmodfile(char *fname, char *outf, char *binv)
468 + {
469 +        char    *mname[MAXMODLIST];
470 +        int     i;
471 +                                        /* find the file & store strings */
472 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
473 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
474 +                error(SYSTEM, errmsg);
475 +        }
476 +        for (i = 0; mname[i]; i++)      /* add each one */
477 +                addmodifier(mname[i], outf, binv);
478 + }
479 +
480   /* put string to stderr */
481   void
482   eputs(char  *s)
# Line 454 | Line 492 | eputs(char  *s)
492          midline = s[strlen(s)-1] != '\n';
493   }
494  
495 + /* construct output file name and return flags whether modifier/bin present */
496 + int
497 + ofname(char *oname, const char *ospec, const char *mname, int bn)
498 + {
499 +        const char      *mnp = NULL;
500 +        const char      *bnp = NULL;
501 +        const char      *cp;
502 +        
503 +        if (ospec == NULL)
504 +                return -1;
505 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
506 +                if (*cp == '%') {
507 +                        do
508 +                                ++cp;
509 +                        while (isdigit(*cp));
510 +                        switch (*cp) {
511 +                        case '%':
512 +                                break;
513 +                        case 's':
514 +                                if (mnp != NULL)
515 +                                        return -1;
516 +                                mnp = cp;
517 +                                break;
518 +                        case 'd':
519 +                                if (bnp != NULL)
520 +                                        return -1;
521 +                                bnp = cp;
522 +                                break;
523 +                        default:
524 +                                return -1;
525 +                        }
526 +                }
527 +        if (mnp != NULL) {                      /* create file name */
528 +                if (bnp != NULL) {
529 +                        if (bnp > mnp)
530 +                                sprintf(oname, ospec, mname, bn);
531 +                        else
532 +                                sprintf(oname, ospec, bn, mname);
533 +                        return OF_MODIFIER|OF_BIN;
534 +                }
535 +                sprintf(oname, ospec, mname);
536 +                return OF_MODIFIER;
537 +        }
538 +        if (bnp != NULL) {
539 +                sprintf(oname, ospec, bn);
540 +                return OF_BIN;
541 +        }
542 +        strcpy(oname, ospec);
543 +        return 0;
544 + }
545 +
546   /* write header to the given output stream */
547   void
548 < printheader(FILE *fout)
548 > printheader(FILE *fout, const char *info)
549   {
550          extern char     VersionID[];
551          FILE            *fin = fopen(octree, "r");
# Line 468 | Line 557 | printheader(FILE *fout)
557          printargs(gargc-1, gargv, fout);        /* add our command */
558          fprintf(fout, "SOFTWARE= %s\n", VersionID);
559          fputnow(fout);
560 +        if (info != NULL)                       /* add extra info if given */
561 +                fputs(info, fout);
562          switch (outfmt) {                       /* add output format */
563          case 'a':
564                  fputformat("ascii", fout);
# Line 494 | Line 585 | printheader(FILE *fout)
585   FILE *
586   getofile(const char *ospec, const char *mname, int bn)
587   {
588 <        const char      *mnp = NULL;
589 <        const char      *bnp = NULL;
499 <        const char      *cp;
500 <        char            ofname[1024];
588 >        int             ofl;
589 >        char            oname[1024];
590          LUENT           *lep;
591          
592          if (ospec == NULL) {                    /* use stdout? */
# Line 505 | Line 594 | getofile(const char *ospec, const char *mname, int bn)
594                          if (outfmt != 'a')
595                                  SET_FILE_BINARY(stdout);
596                          if (header)
597 <                                printheader(stdout);
597 >                                printheader(stdout, NULL);
598                  }
599                  using_stdout = 1;
600                  return stdout;
601          }
602 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
603 <                if (*cp == '%') {
604 <                        do
605 <                                ++cp;
606 <                        while (isdigit(*cp));
607 <                        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 */
602 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
603 >        if (ofl < 0) {
604 >                sprintf(errmsg, "bad output format '%s'", ospec);
605 >                error(USER, errmsg);
606 >        }
607 >        lep = lu_find(&ofiletab, oname);        /* look it up */
608          if (lep->key == NULL)                   /* new entry */
609 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
609 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
610          if (lep->data == NULL) {                /* open output file */
611                  FILE            *fp;
612                  int             i;
613 <                if (ofname[0] == '!')           /* output to command */
614 <                        fp = popen(ofname+1, "w");
613 >                if (oname[0] == '!')            /* output to command */
614 >                        fp = popen(oname+1, "w");
615                  else
616 <                        fp = fopen(ofname, "w");
616 >                        fp = fopen(oname, "w");
617                  if (fp == NULL) {
618 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
618 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
619                          error(SYSTEM, errmsg);
620                  }
621                  if (outfmt != 'a')
622                          SET_FILE_BINARY(fp);
623 <                if (header)
624 <                        printheader(fp);
623 >                if (header) {
624 >                        char    info[512];
625 >                        char    *cp = info;
626 >                        sprintf(cp, "MODIFIER=%s\n", mname);
627 >                        while (*cp) ++cp;
628 >                        if (ofl & OF_BIN) {
629 >                                sprintf(cp, "BIN=%d\n", bn);
630 >                                while (*cp) ++cp;
631 >                        }
632 >                        *cp = '\0';
633 >                        printheader(fp, info);
634 >                }
635                                                  /* play catch-up */
636                  for (i = 0; i < lastdone; i++) {
637                          static const DCOLOR     nocontrib = BLKCOLOR;
638 <                        putcontrib(nocontrib, fp);
638 >                        put_contrib(nocontrib, fp);
639                          if (outfmt == 'a')
640                                  putc('\n', fp);
641                  }
# Line 574 | Line 644 | getofile(const char *ospec, const char *mname, int bn)
644                  lep->data = (char *)fp;
645          }
646          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 */
647   }
648  
649   /* read input ray into buffer */
# Line 671 | Line 737 | puteol(const LUENT *e, void *p)
737  
738   /* put out ray contribution to file */
739   void
740 < putcontrib(const DCOLOR cnt, FILE *fout)
740 > put_contrib(const DCOLOR cnt, FILE *fout)
741   {
742          float   fv[3];
743          COLR    cv;
# Line 704 | Line 770 | done_contrib(void)
770   {
771          int     i, j;
772          MODCONT *mp;
773 +        FILE    *fp;
774                                                  /* output modifiers in order */
775          for (i = 0; i < nmods; i++) {
709                FILE    *fp;
776                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
777                  fp = getofile(mp->outspec, mp->modname, 0);
778 <                putcontrib(mp->cbin[0], fp);
778 >                put_contrib(mp->cbin[0], fp);
779                  if (mp->nbins > 3 &&            /* minor optimization */
780                                  fp == getofile(mp->outspec, mp->modname, 1))
781                          for (j = 1; j < mp->nbins; j++)
782 <                                putcontrib(mp->cbin[j], fp);
782 >                                put_contrib(mp->cbin[j], fp);
783                  else
784                          for (j = 1; j < mp->nbins; j++)
785 <                                putcontrib(mp->cbin[j],
785 >                                put_contrib(mp->cbin[j],
786                                          getofile(mp->outspec, mp->modname, j));
787                                                  /* clear for next ray tree */
788                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
# Line 888 | Line 954 | trace_contribs(FILE *fin)
954                  rtp = get_rproc();              /* get avail. rtrace process */
955                  rtp->raynum = ++lastray;        /* assign ray to it */
956                  writebuf(rtp->pd.w, inpbuf, iblen);
957 <                if (!--raysleft)
957 >                if (raysleft && !--raysleft)
958                          break;
959                  process_queue();                /* catch up with results */
960          }
961          while (wait_rproc() != NULL)            /* process outstanding rays */
962                  process_queue();
963 <        if (raysleft > 0)
963 >        if (raysleft)
964                  error(USER, "unexpected EOF on input");
965 +        lu_done(&ofiletab);                     /* close output files */
966 + }
967 +
968 + /* seek on the given output file */
969 + static int
970 + myseeko(const LUENT *e, void *p)
971 + {
972 +        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
973 +                sprintf(errmsg, "seek error on file '%s'", e->key);
974 +                error(SYSTEM, errmsg);
975 +        }
976 + }
977 +
978 + /* recover output if possible */
979 + void
980 + recover_output(FILE *fin)
981 + {
982 +        off_t   lastout = -1;
983 +        int     outvsiz;
984 +        char    *outvfmt;
985 +        int     i, j;
986 +        MODCONT *mp;
987 +        int     ofl;
988 +        char    oname[1024];
989 +        LUENT   *ment, *oent;
990 +        FILE    *fp;
991 +        off_t   nvals;
992 +
993 +        switch (outfmt) {
994 +        case 'a':
995 +                error(USER, "cannot recover ASCII output");
996 +                return;
997 +        case 'f':
998 +                outvsiz = sizeof(float)*3;
999 +                outvfmt = "float";
1000 +                break;
1001 +        case 'd':
1002 +                outvsiz = sizeof(double)*3;
1003 +                outvfmt = "double";
1004 +                break;
1005 +        case 'c':
1006 +                outvsiz = sizeof(COLR);
1007 +                outvfmt = COLRFMT;
1008 +                break;
1009 +        default:
1010 +                error(INTERNAL, "botched output format");
1011 +                return;
1012 +        }
1013 +                                                /* check modifier outputs */
1014 +        for (i = 0; i < nmods; i++) {
1015 +                ment = lu_find(&modconttab,modname[i]);
1016 +                mp = (MODCONT *)ment->data;
1017 +                if (mp->outspec == NULL)
1018 +                        error(USER, "cannot recover from stdout");
1019 +                for (j = 0; ; j++) {            /* check each bin's file */
1020 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1021 +                        if (ofl < 0)
1022 +                                error(USER, "bad output file specification");
1023 +                        oent = lu_find(&ofiletab, oname);
1024 +                        if (oent->data != NULL) { /* saw this one already */
1025 +                                if (ofl & OF_BIN)
1026 +                                        continue;
1027 +                                break;
1028 +                        }
1029 +                        if (oname[0] == '!')
1030 +                                error(USER, "cannot recover from command");
1031 +                                                /* open output */
1032 +                        fp = fopen(oname, "rb+");
1033 +                        if (fp == NULL) {
1034 +                                if (j)
1035 +                                        break;  /* assume end of modifier */
1036 +                                sprintf(errmsg, "missing recover file '%s'",
1037 +                                                oname);
1038 +                                error(USER, errmsg);
1039 +                        }
1040 +                        nvals = lseek(fileno(fp), 0, SEEK_END);
1041 +                        if (nvals <= 0) {
1042 +                                lastout = 0;    /* empty output, quit here */
1043 +                                fclose(fp);
1044 +                                break;
1045 +                        }
1046 +                        lseek(fileno(fp), 0, SEEK_SET);
1047 +                        if (header) {
1048 +                                int     xr, yr;
1049 +                                if (checkheader(fp, outvfmt, NULL) != 1) {
1050 +                                        sprintf(errmsg,
1051 +                                                "format mismatch for '%s'",
1052 +                                                        oname);
1053 +                                        error(USER, errmsg);
1054 +                                }
1055 +                                if (xres > 0 && yres > 0 &&
1056 +                                                (!fscnresolu(&xr, &yr, fp) ||
1057 +                                                        xr != xres ||
1058 +                                                        yr != yres)) {
1059 +                                        sprintf(errmsg,
1060 +                                                "resolution mismatch for '%s'",
1061 +                                                        oname);
1062 +                                        error(USER, errmsg);
1063 +                                }
1064 +                        }
1065 +                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1066 +                        if (lastout < 0 || nvals < lastout)
1067 +                                lastout = nvals;
1068 +                        if (oent->key == NULL) /* new entry */
1069 +                                oent->key = strcpy((char *)
1070 +                                                malloc(strlen(oname)+1), oname);
1071 +                        oent->data = (char *)fp;
1072 +                        if (!(ofl & OF_BIN))
1073 +                                break;          /* no bin separation */
1074 +                }
1075 +                if (!lastout) {                 /* empty output */
1076 +                        error(WARNING, "no previous data to recover");
1077 +                        lu_done(&ofiletab);     /* reclose all outputs */
1078 +                        return;
1079 +                }
1080 +                if (j > mp->nbins) {            /* preallocate modifier bins */
1081 +                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1082 +                                                        (j-1)*sizeof(DCOLOR));
1083 +                        if (mp == NULL)
1084 +                                error(SYSTEM, "out of memory in recover_output");
1085 +                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1086 +                        mp->nbins = j;
1087 +                        ment->data = (char *)mp;
1088 +                }
1089 +        }
1090 +        if (lastout < 0) {
1091 +                error(WARNING, "no output files to recover");
1092 +                return;
1093 +        }
1094 +                                                /* seek on all files */
1095 +        nvals = lastout * outvsiz;
1096 +        lu_doall(&ofiletab, myseeko, &nvals);
1097 +                                                /* discard input */
1098 +        for (nvals = 0; nvals < lastout; nvals++)
1099 +                if (getinp(oname, fin) <= 0)
1100 +                        error(USER, "unexpected EOF on input");
1101 +        lastray = lastdone = (unsigned long)lastout;
1102 +        if (raysleft)
1103 +                raysleft -= lastray;
1104   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines