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.26 by schorsch, Mon Sep 19 11:30:11 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];
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  
333 + #ifndef SIGALRM
334 + #define SIGALRM SIGTERM
335 + #endif
336   /* kill persistent rtrace process */
337   static void
338   killpersist(void)
339   {
340          FILE    *fp = fopen(persistfn, "r");
341 <        int     pid;
341 >        RT_PID  pid;
342  
343          if (fp == NULL)
344                  return;
# Line 401 | Line 434 | init(int np)
434          rtp->next = NULL;               /* terminate list */
435          if (yres > 0) {
436                  if (xres > 0)
437 <                        raysleft = xres*yres;
437 >                        raysleft = (unsigned long)xres*yres;
438                  else
439                          raysleft = yres;
440          } else
# Line 439 | Line 472 | addmodifier(char *modn, char *outf, char *binv)
472          return mp;
473   }
474  
475 + /* add modifiers from a file list */
476 + void
477 + addmodfile(char *fname, char *outf, char *binv)
478 + {
479 +        char    *mname[MAXMODLIST];
480 +        int     i;
481 +                                        /* find the file & store strings */
482 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
483 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
484 +                error(SYSTEM, errmsg);
485 +        }
486 +        for (i = 0; mname[i]; i++)      /* add each one */
487 +                addmodifier(mname[i], outf, binv);
488 + }
489 +
490   /* put string to stderr */
491   void
492   eputs(char  *s)
# Line 454 | Line 502 | eputs(char  *s)
502          midline = s[strlen(s)-1] != '\n';
503   }
504  
505 + /* construct output file name and return flags whether modifier/bin present */
506 + int
507 + ofname(char *oname, const char *ospec, const char *mname, int bn)
508 + {
509 +        const char      *mnp = NULL;
510 +        const char      *bnp = NULL;
511 +        const char      *cp;
512 +        
513 +        if (ospec == NULL)
514 +                return -1;
515 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
516 +                if (*cp == '%') {
517 +                        do
518 +                                ++cp;
519 +                        while (isdigit(*cp));
520 +                        switch (*cp) {
521 +                        case '%':
522 +                                break;
523 +                        case 's':
524 +                                if (mnp != NULL)
525 +                                        return -1;
526 +                                mnp = cp;
527 +                                break;
528 +                        case 'd':
529 +                                if (bnp != NULL)
530 +                                        return -1;
531 +                                bnp = cp;
532 +                                break;
533 +                        default:
534 +                                return -1;
535 +                        }
536 +                }
537 +        if (mnp != NULL) {                      /* create file name */
538 +                if (bnp != NULL) {
539 +                        if (bnp > mnp)
540 +                                sprintf(oname, ospec, mname, bn);
541 +                        else
542 +                                sprintf(oname, ospec, bn, mname);
543 +                        return OF_MODIFIER|OF_BIN;
544 +                }
545 +                sprintf(oname, ospec, mname);
546 +                return OF_MODIFIER;
547 +        }
548 +        if (bnp != NULL) {
549 +                sprintf(oname, ospec, bn);
550 +                return OF_BIN;
551 +        }
552 +        strcpy(oname, ospec);
553 +        return 0;
554 + }
555 +
556   /* write header to the given output stream */
557   void
558 < printheader(FILE *fout)
558 > printheader(FILE *fout, const char *info)
559   {
560          extern char     VersionID[];
561          FILE            *fin = fopen(octree, "r");
# Line 468 | Line 567 | printheader(FILE *fout)
567          printargs(gargc-1, gargv, fout);        /* add our command */
568          fprintf(fout, "SOFTWARE= %s\n", VersionID);
569          fputnow(fout);
570 +        if (info != NULL)                       /* add extra info if given */
571 +                fputs(info, fout);
572          switch (outfmt) {                       /* add output format */
573          case 'a':
574                  fputformat("ascii", fout);
# Line 494 | Line 595 | printheader(FILE *fout)
595   FILE *
596   getofile(const char *ospec, const char *mname, int bn)
597   {
598 <        const char      *mnp = NULL;
599 <        const char      *bnp = NULL;
499 <        const char      *cp;
500 <        char            ofname[1024];
598 >        int             ofl;
599 >        char            oname[1024];
600          LUENT           *lep;
601          
602          if (ospec == NULL) {                    /* use stdout? */
# Line 505 | Line 604 | getofile(const char *ospec, const char *mname, int bn)
604                          if (outfmt != 'a')
605                                  SET_FILE_BINARY(stdout);
606                          if (header)
607 <                                printheader(stdout);
607 >                                printheader(stdout, NULL);
608                  }
609                  using_stdout = 1;
610                  return stdout;
611          }
612 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
613 <                if (*cp == '%') {
614 <                        do
615 <                                ++cp;
616 <                        while (isdigit(*cp));
617 <                        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 */
612 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
613 >        if (ofl < 0) {
614 >                sprintf(errmsg, "bad output format '%s'", ospec);
615 >                error(USER, errmsg);
616 >        }
617 >        lep = lu_find(&ofiletab, oname);        /* look it up */
618          if (lep->key == NULL)                   /* new entry */
619 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
619 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
620          if (lep->data == NULL) {                /* open output file */
621                  FILE            *fp;
622                  int             i;
623 <                if (ofname[0] == '!')           /* output to command */
624 <                        fp = popen(ofname+1, "w");
623 >                if (oname[0] == '!')            /* output to command */
624 >                        fp = popen(oname+1, "w");
625                  else
626 <                        fp = fopen(ofname, "w");
626 >                        fp = fopen(oname, "w");
627                  if (fp == NULL) {
628 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
628 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
629                          error(SYSTEM, errmsg);
630                  }
631                  if (outfmt != 'a')
632                          SET_FILE_BINARY(fp);
633 <                if (header)
634 <                        printheader(fp);
633 >                if (header) {
634 >                        char    info[512];
635 >                        char    *cp = info;
636 >                        sprintf(cp, "MODIFIER=%s\n", mname);
637 >                        while (*cp) ++cp;
638 >                        if (ofl & OF_BIN) {
639 >                                sprintf(cp, "BIN=%d\n", bn);
640 >                                while (*cp) ++cp;
641 >                        }
642 >                        *cp = '\0';
643 >                        printheader(fp, info);
644 >                }
645                                                  /* play catch-up */
646                  for (i = 0; i < lastdone; i++) {
647                          static const DCOLOR     nocontrib = BLKCOLOR;
648 <                        putcontrib(nocontrib, fp);
648 >                        put_contrib(nocontrib, fp);
649                          if (outfmt == 'a')
650                                  putc('\n', fp);
651                  }
# Line 574 | Line 654 | getofile(const char *ospec, const char *mname, int bn)
654                  lep->data = (char *)fp;
655          }
656          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 */
657   }
658  
659   /* read input ray into buffer */
# Line 671 | Line 747 | puteol(const LUENT *e, void *p)
747  
748   /* put out ray contribution to file */
749   void
750 < putcontrib(const DCOLOR cnt, FILE *fout)
750 > put_contrib(const DCOLOR cnt, FILE *fout)
751   {
752          float   fv[3];
753          COLR    cv;
# Line 704 | Line 780 | done_contrib(void)
780   {
781          int     i, j;
782          MODCONT *mp;
783 +        FILE    *fp;
784                                                  /* output modifiers in order */
785          for (i = 0; i < nmods; i++) {
709                FILE    *fp;
786                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
787                  fp = getofile(mp->outspec, mp->modname, 0);
788 <                putcontrib(mp->cbin[0], fp);
788 >                put_contrib(mp->cbin[0], fp);
789                  if (mp->nbins > 3 &&            /* minor optimization */
790                                  fp == getofile(mp->outspec, mp->modname, 1))
791                          for (j = 1; j < mp->nbins; j++)
792 <                                putcontrib(mp->cbin[j], fp);
792 >                                put_contrib(mp->cbin[j], fp);
793                  else
794                          for (j = 1; j < mp->nbins; j++)
795 <                                putcontrib(mp->cbin[j],
795 >                                put_contrib(mp->cbin[j],
796                                          getofile(mp->outspec, mp->modname, j));
797                                                  /* clear for next ray tree */
798                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
# Line 888 | Line 964 | trace_contribs(FILE *fin)
964                  rtp = get_rproc();              /* get avail. rtrace process */
965                  rtp->raynum = ++lastray;        /* assign ray to it */
966                  writebuf(rtp->pd.w, inpbuf, iblen);
967 <                if (!--raysleft)
967 >                if (raysleft && !--raysleft)
968                          break;
969                  process_queue();                /* catch up with results */
970          }
971          while (wait_rproc() != NULL)            /* process outstanding rays */
972                  process_queue();
973 <        if (raysleft > 0)
973 >        if (raysleft)
974                  error(USER, "unexpected EOF on input");
975 +        lu_done(&ofiletab);                     /* close output files */
976 + }
977 +
978 + /* seek on the given output file */
979 + static int
980 + myseeko(const LUENT *e, void *p)
981 + {
982 +        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
983 +                sprintf(errmsg, "seek error on file '%s'", e->key);
984 +                error(SYSTEM, errmsg);
985 +        }
986 + }
987 +
988 + /* recover output if possible */
989 + void
990 + recover_output(FILE *fin)
991 + {
992 +        off_t   lastout = -1;
993 +        int     outvsiz;
994 +        char    *outvfmt;
995 +        int     i, j;
996 +        MODCONT *mp;
997 +        int     ofl;
998 +        char    oname[1024];
999 +        LUENT   *ment, *oent;
1000 +        FILE    *fp;
1001 +        off_t   nvals;
1002 +
1003 +        switch (outfmt) {
1004 +        case 'a':
1005 +                error(USER, "cannot recover ASCII output");
1006 +                return;
1007 +        case 'f':
1008 +                outvsiz = sizeof(float)*3;
1009 +                outvfmt = "float";
1010 +                break;
1011 +        case 'd':
1012 +                outvsiz = sizeof(double)*3;
1013 +                outvfmt = "double";
1014 +                break;
1015 +        case 'c':
1016 +                outvsiz = sizeof(COLR);
1017 +                outvfmt = COLRFMT;
1018 +                break;
1019 +        default:
1020 +                error(INTERNAL, "botched output format");
1021 +                return;
1022 +        }
1023 +                                                /* check modifier outputs */
1024 +        for (i = 0; i < nmods; i++) {
1025 +                ment = lu_find(&modconttab,modname[i]);
1026 +                mp = (MODCONT *)ment->data;
1027 +                if (mp->outspec == NULL)
1028 +                        error(USER, "cannot recover from stdout");
1029 +                for (j = 0; ; j++) {            /* check each bin's file */
1030 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1031 +                        if (ofl < 0)
1032 +                                error(USER, "bad output file specification");
1033 +                        oent = lu_find(&ofiletab, oname);
1034 +                        if (oent->data != NULL) { /* saw this one already */
1035 +                                if (ofl & OF_BIN)
1036 +                                        continue;
1037 +                                break;
1038 +                        }
1039 +                        if (oname[0] == '!')
1040 +                                error(USER, "cannot recover from command");
1041 +                                                /* open output */
1042 +                        fp = fopen(oname, "rb+");
1043 +                        if (fp == NULL) {
1044 +                                if (j)
1045 +                                        break;  /* assume end of modifier */
1046 +                                sprintf(errmsg, "missing recover file '%s'",
1047 +                                                oname);
1048 +                                error(USER, errmsg);
1049 +                        }
1050 +                        nvals = lseek(fileno(fp), 0, SEEK_END);
1051 +                        if (nvals <= 0) {
1052 +                                lastout = 0;    /* empty output, quit here */
1053 +                                fclose(fp);
1054 +                                break;
1055 +                        }
1056 +                        lseek(fileno(fp), 0, SEEK_SET);
1057 +                        if (header) {
1058 +                                int     xr, yr;
1059 +                                if (checkheader(fp, outvfmt, NULL) != 1) {
1060 +                                        sprintf(errmsg,
1061 +                                                "format mismatch for '%s'",
1062 +                                                        oname);
1063 +                                        error(USER, errmsg);
1064 +                                }
1065 +                                if (xres > 0 && yres > 0 &&
1066 +                                                (!fscnresolu(&xr, &yr, fp) ||
1067 +                                                        xr != xres ||
1068 +                                                        yr != yres)) {
1069 +                                        sprintf(errmsg,
1070 +                                                "resolution mismatch for '%s'",
1071 +                                                        oname);
1072 +                                        error(USER, errmsg);
1073 +                                }
1074 +                        }
1075 +                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1076 +                        if (lastout < 0 || nvals < lastout)
1077 +                                lastout = nvals;
1078 +                        if (oent->key == NULL) /* new entry */
1079 +                                oent->key = strcpy((char *)
1080 +                                                malloc(strlen(oname)+1), oname);
1081 +                        oent->data = (char *)fp;
1082 +                        if (!(ofl & OF_BIN))
1083 +                                break;          /* no bin separation */
1084 +                }
1085 +                if (!lastout) {                 /* empty output */
1086 +                        error(WARNING, "no previous data to recover");
1087 +                        lu_done(&ofiletab);     /* reclose all outputs */
1088 +                        return;
1089 +                }
1090 +                if (j > mp->nbins) {            /* preallocate modifier bins */
1091 +                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1092 +                                                        (j-1)*sizeof(DCOLOR));
1093 +                        if (mp == NULL)
1094 +                                error(SYSTEM, "out of memory in recover_output");
1095 +                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1096 +                        mp->nbins = j;
1097 +                        ment->data = (char *)mp;
1098 +                }
1099 +        }
1100 +        if (lastout < 0) {
1101 +                error(WARNING, "no output files to recover");
1102 +                return;
1103 +        }
1104 +        if (raysleft && lastout >= raysleft) {
1105 +                error(WARNING, "output appears to be complete");
1106 +                /* XXX should read & discard input? */
1107 +                quit(0);
1108 +        }
1109 +                                                /* seek on all files */
1110 +        nvals = lastout * outvsiz;
1111 +        lu_doall(&ofiletab, myseeko, &nvals);
1112 +                                                /* discard input */
1113 +        for (nvals = 0; nvals < lastout; nvals++)
1114 +                if (getinp(oname, fin) <= 0)
1115 +                        error(USER, "unexpected EOF on input");
1116 +        lastray = lastdone = (unsigned long)lastout;
1117 +        if (raysleft)
1118 +                raysleft -= lastray;
1119   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines