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.18 by greg, Fri Jun 10 20:38:38 2005 UTC

# Line 49 | Line 49 | static void closefile(void *p) { fclose((FILE *)p); }
49  
50   LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
51  
52 + #define OF_MODIFIER     01
53 + #define OF_BIN          02
54 +
55   FILE *getofile(const char *ospec, const char *mname, int bn);
56 + int ofname(char *oname, const char *ospec, const char *mname, int bn);
57 + void printheader(FILE *fout, const char *info);
58  
59   /*
60   * The rcont structure is used to manage i/o with a particular
# Line 67 | Line 72 | struct rtproc {
72   };                              /* rtrace process buffer */
73  
74                                          /* rtrace command and defaults */
75 < char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
76 <                                "-ab", "1", "-ad", "128", "-lr", "-10", };
77 < int  rtargc = 11;
75 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
76 >                                "-dj", ".5", "-dr", "3",
77 >                                "-ab", "1", "-ad", "128", };
78 > int  rtargc = 9;
79                                          /* overriding rtrace options */
80   char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
81                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
# Line 93 | Line 99 | int            header = 1;             /* output header? */
99   int             xres = 0;               /* horiz. output resolution */
100   int             yres = 0;               /* vert. output resolution */
101  
102 < long            raysleft;               /* number of rays left to trace */
102 > unsigned long   raysleft;               /* number of rays left to trace */
103   long            waitflush;              /* how long until next flush */
104  
105   unsigned long   lastray = 0;            /* last ray number sent */
# Line 105 | Line 111 | const char     *modname[MAXMODLIST];   /* ordered modifier n
111   int             nmods = 0;              /* number of modifiers */
112  
113   MODCONT *addmodifier(char *modn, char *outf, char *binv);
114 + void addmodfile(char *fname, char *outf, char *binv);
115  
116   void init(int np);
117   int done_rprocs(struct rtproc *rtp);
118 < void trace_contribs(FILE *fp);
118 > void recover_output(FILE *fin);
119 > void trace_contribs(FILE *fin);
120   struct rtproc *wait_rproc(void);
121   struct rtproc *get_rproc(void);
122   void queue_raytree(struct rtproc *rtp);
123   void process_queue(void);
124  
125 < void putcontrib(const DCOLOR cnt, FILE *fout);
125 > void put_contrib(const DCOLOR cnt, FILE *fout);
126   void add_contrib(const char *modn);
127   void done_contrib(void);
128  
# Line 158 | Line 166 | int
166   main(int argc, char *argv[])
167   {
168          int     nprocs = 1;
169 +        int     recover = 0;
170          char    *curout = NULL;
171          char    *binval = NULL;
172          char    fmt[8];
# Line 180 | Line 189 | main(int argc, char *argv[])
189                  }
190                  if (argv[i][0] == '-')
191                          switch (argv[i][1]) {
192 +                        case 'r':               /* recover output */
193 +                                if (argv[i][2]) break;
194 +                                recover++;
195 +                                continue;
196                          case 'n':               /* number of processes */
197                                  if (argv[i][2] || i >= argc-1) break;
198                                  nprocs = atoi(argv[++i]);
# Line 246 | Line 259 | main(int argc, char *argv[])
259                                  rtargv[rtargc++] = argv[++i];
260                                  addmodifier(argv[i], curout, binval);
261                                  continue;
262 +                        case 'M':               /* modifier file */
263 +                                if (argv[i][2] || i >= argc-1) break;
264 +                                addmodfile(argv[++i], curout, binval);
265 +                                continue;
266                          }
267                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
268          }
# Line 296 | Line 313 | main(int argc, char *argv[])
313          rtargv[rtargc] = NULL;
314                                  /* start rtrace & compute contributions */
315          init(nprocs);
316 +        if (recover)            /* perform recovery if requested */
317 +                recover_output(stdin);
318          trace_contribs(stdin);
319          quit(0);
320   }
# Line 401 | Line 420 | init(int np)
420          rtp->next = NULL;               /* terminate list */
421          if (yres > 0) {
422                  if (xres > 0)
423 <                        raysleft = xres*yres;
423 >                        raysleft = (unsigned long)xres*yres;
424                  else
425                          raysleft = yres;
426          } else
# Line 439 | Line 458 | addmodifier(char *modn, char *outf, char *binv)
458          return mp;
459   }
460  
461 + /* add modifiers from a file list */
462 + void
463 + addmodfile(char *fname, char *outf, char *binv)
464 + {
465 +        char    *mname[MAXMODLIST];
466 +        int     i;
467 +                                        /* load the file & store strings */
468 +        wordfile(mname, fname);
469 +        for (i = 0; mname[i]; i++)      /* add each one */
470 +                addmodifier(mname[i], outf, binv);
471 + }
472 +
473   /* put string to stderr */
474   void
475   eputs(char  *s)
# Line 454 | Line 485 | eputs(char  *s)
485          midline = s[strlen(s)-1] != '\n';
486   }
487  
488 + /* construct output file name and return flags whether modifier/bin present */
489 + int
490 + ofname(char *oname, const char *ospec, const char *mname, int bn)
491 + {
492 +        const char      *mnp = NULL;
493 +        const char      *bnp = NULL;
494 +        const char      *cp;
495 +        
496 +        if (ospec == NULL)
497 +                return -1;
498 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
499 +                if (*cp == '%') {
500 +                        do
501 +                                ++cp;
502 +                        while (isdigit(*cp));
503 +                        switch (*cp) {
504 +                        case '%':
505 +                                break;
506 +                        case 's':
507 +                                if (mnp != NULL)
508 +                                        return -1;
509 +                                mnp = cp;
510 +                                break;
511 +                        case 'd':
512 +                                if (bnp != NULL)
513 +                                        return -1;
514 +                                bnp = cp;
515 +                                break;
516 +                        default:
517 +                                return -1;
518 +                        }
519 +                }
520 +        if (mnp != NULL) {                      /* create file name */
521 +                if (bnp != NULL) {
522 +                        if (bnp > mnp)
523 +                                sprintf(oname, ospec, mname, bn);
524 +                        else
525 +                                sprintf(oname, ospec, bn, mname);
526 +                        return OF_MODIFIER|OF_BIN;
527 +                }
528 +                sprintf(oname, ospec, mname);
529 +                return OF_MODIFIER;
530 +        }
531 +        if (bnp != NULL) {
532 +                sprintf(oname, ospec, bn);
533 +                return OF_BIN;
534 +        }
535 +        strcpy(oname, ospec);
536 +        return 0;
537 + }
538 +
539   /* write header to the given output stream */
540   void
541 < printheader(FILE *fout)
541 > printheader(FILE *fout, const char *info)
542   {
543          extern char     VersionID[];
544          FILE            *fin = fopen(octree, "r");
# Line 468 | Line 550 | printheader(FILE *fout)
550          printargs(gargc-1, gargv, fout);        /* add our command */
551          fprintf(fout, "SOFTWARE= %s\n", VersionID);
552          fputnow(fout);
553 +        if (info != NULL)                       /* add extra info if given */
554 +                fputs(info, fout);
555          switch (outfmt) {                       /* add output format */
556          case 'a':
557                  fputformat("ascii", fout);
# Line 494 | Line 578 | printheader(FILE *fout)
578   FILE *
579   getofile(const char *ospec, const char *mname, int bn)
580   {
581 <        const char      *mnp = NULL;
582 <        const char      *bnp = NULL;
499 <        const char      *cp;
500 <        char            ofname[1024];
581 >        int             ofl;
582 >        char            oname[1024];
583          LUENT           *lep;
584          
585          if (ospec == NULL) {                    /* use stdout? */
# Line 505 | Line 587 | getofile(const char *ospec, const char *mname, int bn)
587                          if (outfmt != 'a')
588                                  SET_FILE_BINARY(stdout);
589                          if (header)
590 <                                printheader(stdout);
590 >                                printheader(stdout, NULL);
591                  }
592                  using_stdout = 1;
593                  return stdout;
594          }
595 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
596 <                if (*cp == '%') {
597 <                        do
598 <                                ++cp;
599 <                        while (isdigit(*cp));
600 <                        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 */
595 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
596 >        if (ofl < 0) {
597 >                sprintf(errmsg, "bad output format '%s'", ospec);
598 >                error(USER, errmsg);
599 >        }
600 >        lep = lu_find(&ofiletab, oname);        /* look it up */
601          if (lep->key == NULL)                   /* new entry */
602 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
602 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
603          if (lep->data == NULL) {                /* open output file */
604                  FILE            *fp;
605                  int             i;
606 <                if (ofname[0] == '!')           /* output to command */
607 <                        fp = popen(ofname+1, "w");
606 >                if (oname[0] == '!')            /* output to command */
607 >                        fp = popen(oname+1, "w");
608                  else
609 <                        fp = fopen(ofname, "w");
609 >                        fp = fopen(oname, "w");
610                  if (fp == NULL) {
611 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
611 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
612                          error(SYSTEM, errmsg);
613                  }
614                  if (outfmt != 'a')
615                          SET_FILE_BINARY(fp);
616 <                if (header)
617 <                        printheader(fp);
616 >                if (header) {
617 >                        char    info[512];
618 >                        char    *cp = info;
619 >                        if (ofl & OF_MODIFIER) {
620 >                                sprintf(cp, "MODIFIER=%s\n", mname);
621 >                                while (*cp) ++cp;
622 >                        }
623 >                        if (ofl & OF_BIN) {
624 >                                sprintf(cp, "BIN=%d\n", bn);
625 >                                while (*cp) ++cp;
626 >                        }
627 >                        *cp = '\0';
628 >                        printheader(fp, info);
629 >                }
630                                                  /* play catch-up */
631                  for (i = 0; i < lastdone; i++) {
632                          static const DCOLOR     nocontrib = BLKCOLOR;
633 <                        putcontrib(nocontrib, fp);
633 >                        put_contrib(nocontrib, fp);
634                          if (outfmt == 'a')
635                                  putc('\n', fp);
636                  }
# Line 574 | Line 639 | getofile(const char *ospec, const char *mname, int bn)
639                  lep->data = (char *)fp;
640          }
641          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 */
642   }
643  
644   /* read input ray into buffer */
# Line 671 | Line 732 | puteol(const LUENT *e, void *p)
732  
733   /* put out ray contribution to file */
734   void
735 < putcontrib(const DCOLOR cnt, FILE *fout)
735 > put_contrib(const DCOLOR cnt, FILE *fout)
736   {
737          float   fv[3];
738          COLR    cv;
# Line 704 | Line 765 | done_contrib(void)
765   {
766          int     i, j;
767          MODCONT *mp;
768 +        FILE    *fp;
769                                                  /* output modifiers in order */
770          for (i = 0; i < nmods; i++) {
771                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
772 <                for (j = 0; j < mp->nbins; j++)
773 <                        putcontrib(mp->cbin[j],
772 >                fp = getofile(mp->outspec, mp->modname, 0);
773 >                put_contrib(mp->cbin[0], fp);
774 >                if (mp->nbins > 3 &&            /* minor optimization */
775 >                                fp == getofile(mp->outspec, mp->modname, 1))
776 >                        for (j = 1; j < mp->nbins; j++)
777 >                                put_contrib(mp->cbin[j], fp);
778 >                else
779 >                        for (j = 1; j < mp->nbins; j++)
780 >                                put_contrib(mp->cbin[j],
781                                          getofile(mp->outspec, mp->modname, j));
782                                                  /* clear for next ray tree */
783                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
# Line 880 | Line 949 | trace_contribs(FILE *fin)
949                  rtp = get_rproc();              /* get avail. rtrace process */
950                  rtp->raynum = ++lastray;        /* assign ray to it */
951                  writebuf(rtp->pd.w, inpbuf, iblen);
952 <                if (!--raysleft)
952 >                if (raysleft && !--raysleft)
953                          break;
954                  process_queue();                /* catch up with results */
955          }
956          while (wait_rproc() != NULL)            /* process outstanding rays */
957                  process_queue();
958 <        if (raysleft > 0)
958 >        if (raysleft)
959                  error(USER, "unexpected EOF on input");
960 +        lu_done(&ofiletab);                     /* close output files */
961 + }
962 +
963 + /* seek on the given output file */
964 + static int
965 + myseeko(const LUENT *e, void *p)
966 + {
967 +        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
968 +                sprintf(errmsg, "seek error on file '%s'", e->key);
969 +                error(SYSTEM, errmsg);
970 +        }
971 + }
972 +
973 + /* recover output if possible */
974 + void
975 + recover_output(FILE *fin)
976 + {
977 +        off_t   lastout = -1;
978 +        int     outvsiz;
979 +        char    *outvfmt;
980 +        int     i, j;
981 +        MODCONT *mp;
982 +        int     ofl;
983 +        char    oname[1024];
984 +        LUENT   *ment, *oent;
985 +        FILE    *fp;
986 +        off_t   nvals;
987 +
988 +        switch (outfmt) {
989 +        case 'a':
990 +                error(USER, "cannot recover ASCII output");
991 +                return;
992 +        case 'f':
993 +                outvsiz = sizeof(float)*3;
994 +                outvfmt = "float";
995 +                break;
996 +        case 'd':
997 +                outvsiz = sizeof(double)*3;
998 +                outvfmt = "double";
999 +                break;
1000 +        case 'c':
1001 +                outvsiz = sizeof(COLR);
1002 +                outvfmt = COLRFMT;
1003 +                break;
1004 +        default:
1005 +                error(INTERNAL, "botched output format");
1006 +                return;
1007 +        }
1008 +                                                /* check modifier outputs */
1009 +        for (i = 0; i < nmods; i++) {
1010 +                ment = lu_find(&modconttab,modname[i]);
1011 +                mp = (MODCONT *)ment->data;
1012 +                if (mp->outspec == NULL)
1013 +                        error(USER, "cannot recover from stdout");
1014 +                for (j = 0; ; j++) {            /* check each bin's file */
1015 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1016 +                        if (ofl < 0)
1017 +                                error(USER, "bad output file specification");
1018 +                        oent = lu_find(&ofiletab, oname);
1019 +                        if (oent->data != NULL) { /* saw this one already */
1020 +                                if (ofl & OF_BIN)
1021 +                                        continue;
1022 +                                break;
1023 +                        }
1024 +                        if (oname[0] == '!')
1025 +                                error(USER, "cannot recover from command");
1026 +                                                /* open output */
1027 +                        fp = fopen(oname, "rb+");
1028 +                        if (fp == NULL)
1029 +                                break;          /* must be end of modifier */
1030 +                        nvals = lseek(fileno(fp), 0, SEEK_END);
1031 +                        if (nvals <= 0) {
1032 +                                lastout = 0;    /* empty output, quit here */
1033 +                                fclose(fp);
1034 +                                break;
1035 +                        }
1036 +                        lseek(fileno(fp), 0, SEEK_SET);
1037 +                        if (header) {
1038 +                                int     xr, yr;
1039 +                                if (checkheader(fp, outvfmt, NULL) != 1) {
1040 +                                        sprintf(errmsg,
1041 +                                                "format mismatch for '%s'",
1042 +                                                        oname);
1043 +                                        error(USER, errmsg);
1044 +                                }
1045 +                                if (xres > 0 && yres > 0 &&
1046 +                                                (!fscnresolu(&xr, &yr, fp) ||
1047 +                                                        xr != xres ||
1048 +                                                        yr != yres)) {
1049 +                                        sprintf(errmsg,
1050 +                                                "resolution mismatch for '%s'",
1051 +                                                        oname);
1052 +                                        error(USER, errmsg);
1053 +                                }
1054 +                        }
1055 +                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1056 +                        if (lastout < 0 || nvals < lastout)
1057 +                                lastout = nvals;
1058 +                        if (oent->key == NULL) /* new entry */
1059 +                                oent->key = strcpy((char *)
1060 +                                                malloc(strlen(oname)+1), oname);
1061 +                        oent->data = (char *)fp;
1062 +                        if (!(ofl & OF_BIN))
1063 +                                break;          /* no bin separation */
1064 +                }
1065 +                if (!lastout) {                 /* empty output */
1066 +                        error(WARNING, "no previous data to recover");
1067 +                        lu_done(&ofiletab);     /* reclose all outputs */
1068 +                        return;
1069 +                }
1070 +                if (j > mp->nbins) {            /* preallocate modifier bins */
1071 +                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1072 +                                                        (j-1)*sizeof(DCOLOR));
1073 +                        if (mp == NULL)
1074 +                                error(SYSTEM, "out of memory in recover_output");
1075 +                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1076 +                        mp->nbins = j;
1077 +                        ment->data = (char *)mp;
1078 +                }
1079 +        }
1080 +        if (lastout < 0) {
1081 +                error(WARNING, "no output files to recover");
1082 +                return;
1083 +        }
1084 +                                                /* seek on all files */
1085 +        nvals = lastout * outvsiz;
1086 +        lu_doall(&ofiletab, myseeko, &nvals);
1087 +                                                /* discard input */
1088 +        for (nvals = 0; nvals < lastout; nvals++)
1089 +                if (getinp(oname, fin) <= 0)
1090 +                        error(USER, "unexpected EOF on input");
1091 +        lastray = lastdone = (unsigned long)lastout;
1092 +        if (raysleft)
1093 +                raysleft -= lastray;
1094   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines