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.14 by greg, Thu Jun 9 17:27:28 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 93 | Line 98 | int            header = 1;             /* output header? */
98   int             xres = 0;               /* horiz. output resolution */
99   int             yres = 0;               /* vert. output resolution */
100  
101 < long            raysleft;               /* number of rays left to trace */
101 > unsigned long   raysleft;               /* number of rays left to trace */
102   long            waitflush;              /* how long until next flush */
103  
104   unsigned long   lastray = 0;            /* last ray number sent */
# Line 108 | Line 113 | MODCONT *addmodifier(char *modn, char *outf, char *bin
113  
114   void init(int np);
115   int done_rprocs(struct rtproc *rtp);
116 < void trace_contribs(FILE *fp);
116 > void recover_output(FILE *fin);
117 > void trace_contribs(FILE *fin);
118   struct rtproc *wait_rproc(void);
119   struct rtproc *get_rproc(void);
120   void queue_raytree(struct rtproc *rtp);
121   void process_queue(void);
122  
123 < void putcontrib(const DCOLOR cnt, FILE *fout);
123 > void put_contrib(const DCOLOR cnt, FILE *fout);
124   void add_contrib(const char *modn);
125   void done_contrib(void);
126  
# Line 158 | Line 164 | int
164   main(int argc, char *argv[])
165   {
166          int     nprocs = 1;
167 +        int     recover = 0;
168          char    *curout = NULL;
169          char    *binval = NULL;
170          char    fmt[8];
# Line 180 | Line 187 | main(int argc, char *argv[])
187                  }
188                  if (argv[i][0] == '-')
189                          switch (argv[i][1]) {
190 +                        case 'r':               /* recover output */
191 +                                if (argv[i][2]) break;
192 +                                recover++;
193 +                                continue;
194                          case 'n':               /* number of processes */
195                                  if (argv[i][2] || i >= argc-1) break;
196                                  nprocs = atoi(argv[++i]);
# Line 296 | Line 307 | main(int argc, char *argv[])
307          rtargv[rtargc] = NULL;
308                                  /* start rtrace & compute contributions */
309          init(nprocs);
310 +        if (recover)            /* perform recovery if requested */
311 +                recover_output(stdin);
312          trace_contribs(stdin);
313          quit(0);
314   }
# Line 401 | Line 414 | init(int np)
414          rtp->next = NULL;               /* terminate list */
415          if (yres > 0) {
416                  if (xres > 0)
417 <                        raysleft = xres*yres;
417 >                        raysleft = (unsigned long)xres*yres;
418                  else
419                          raysleft = yres;
420          } else
# Line 454 | Line 467 | eputs(char  *s)
467          midline = s[strlen(s)-1] != '\n';
468   }
469  
470 + /* construct output file name and return flags whether modifier/bin present */
471 + int
472 + ofname(char *oname, const char *ospec, const char *mname, int bn)
473 + {
474 +        const char      *mnp = NULL;
475 +        const char      *bnp = NULL;
476 +        const char      *cp;
477 +        
478 +        if (ospec == NULL)
479 +                return -1;
480 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
481 +                if (*cp == '%') {
482 +                        do
483 +                                ++cp;
484 +                        while (isdigit(*cp));
485 +                        switch (*cp) {
486 +                        case '%':
487 +                                break;
488 +                        case 's':
489 +                                if (mnp != NULL)
490 +                                        return -1;
491 +                                mnp = cp;
492 +                                break;
493 +                        case 'd':
494 +                                if (bnp != NULL)
495 +                                        return -1;
496 +                                bnp = cp;
497 +                                break;
498 +                        default:
499 +                                return -1;
500 +                        }
501 +                }
502 +        if (mnp != NULL) {                      /* create file name */
503 +                if (bnp != NULL) {
504 +                        if (bnp > mnp)
505 +                                sprintf(oname, ospec, mname, bn);
506 +                        else
507 +                                sprintf(oname, ospec, bn, mname);
508 +                        return OF_MODIFIER|OF_BIN;
509 +                }
510 +                sprintf(oname, ospec, mname);
511 +                return OF_MODIFIER;
512 +        }
513 +        if (bnp != NULL) {
514 +                sprintf(oname, ospec, bn);
515 +                return OF_BIN;
516 +        }
517 +        strcpy(oname, ospec);
518 +        return 0;
519 + }
520 +
521   /* write header to the given output stream */
522   void
523 < printheader(FILE *fout)
523 > printheader(FILE *fout, const char *info)
524   {
525          extern char     VersionID[];
526          FILE            *fin = fopen(octree, "r");
# Line 468 | Line 532 | printheader(FILE *fout)
532          printargs(gargc-1, gargv, fout);        /* add our command */
533          fprintf(fout, "SOFTWARE= %s\n", VersionID);
534          fputnow(fout);
535 +        if (info != NULL)                       /* add extra info if given */
536 +                fputs(info, fout);
537          switch (outfmt) {                       /* add output format */
538          case 'a':
539                  fputformat("ascii", fout);
# Line 494 | Line 560 | printheader(FILE *fout)
560   FILE *
561   getofile(const char *ospec, const char *mname, int bn)
562   {
563 <        const char      *mnp = NULL;
564 <        const char      *bnp = NULL;
499 <        const char      *cp;
500 <        char            ofname[1024];
563 >        int             ofl;
564 >        char            oname[1024];
565          LUENT           *lep;
566          
567          if (ospec == NULL) {                    /* use stdout? */
# Line 505 | Line 569 | getofile(const char *ospec, const char *mname, int bn)
569                          if (outfmt != 'a')
570                                  SET_FILE_BINARY(stdout);
571                          if (header)
572 <                                printheader(stdout);
572 >                                printheader(stdout, NULL);
573                  }
574                  using_stdout = 1;
575                  return stdout;
576          }
577 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
578 <                if (*cp == '%') {
579 <                        do
580 <                                ++cp;
581 <                        while (isdigit(*cp));
582 <                        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 */
577 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
578 >        if (ofl < 0) {
579 >                sprintf(errmsg, "bad output format '%s'", ospec);
580 >                error(USER, errmsg);
581 >        }
582 >        lep = lu_find(&ofiletab, oname);        /* look it up */
583          if (lep->key == NULL)                   /* new entry */
584 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
584 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
585          if (lep->data == NULL) {                /* open output file */
586                  FILE            *fp;
587                  int             i;
588 <                if (ofname[0] == '!')           /* output to command */
589 <                        fp = popen(ofname+1, "w");
588 >                if (oname[0] == '!')            /* output to command */
589 >                        fp = popen(oname+1, "w");
590                  else
591 <                        fp = fopen(ofname, "w");
591 >                        fp = fopen(oname, "w");
592                  if (fp == NULL) {
593 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
593 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
594                          error(SYSTEM, errmsg);
595                  }
596                  if (outfmt != 'a')
597                          SET_FILE_BINARY(fp);
598 <                if (header)
599 <                        printheader(fp);
598 >                if (header) {
599 >                        char    info[512];
600 >                        char    *cp = info;
601 >                        if (ofl & OF_MODIFIER) {
602 >                                sprintf(cp, "MODIFIER=%s\n", mname);
603 >                                while (*cp) ++cp;
604 >                        }
605 >                        if (ofl & OF_BIN) {
606 >                                sprintf(cp, "BIN=%d\n", bn);
607 >                                while (*cp) ++cp;
608 >                        }
609 >                        *cp = '\0';
610 >                        printheader(fp, info);
611 >                }
612                                                  /* play catch-up */
613                  for (i = 0; i < lastdone; i++) {
614                          static const DCOLOR     nocontrib = BLKCOLOR;
615 <                        putcontrib(nocontrib, fp);
615 >                        put_contrib(nocontrib, fp);
616                          if (outfmt == 'a')
617                                  putc('\n', fp);
618                  }
# Line 574 | Line 621 | getofile(const char *ospec, const char *mname, int bn)
621                  lep->data = (char *)fp;
622          }
623          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 */
624   }
625  
626   /* read input ray into buffer */
# Line 671 | Line 714 | puteol(const LUENT *e, void *p)
714  
715   /* put out ray contribution to file */
716   void
717 < putcontrib(const DCOLOR cnt, FILE *fout)
717 > put_contrib(const DCOLOR cnt, FILE *fout)
718   {
719          float   fv[3];
720          COLR    cv;
# Line 704 | Line 747 | done_contrib(void)
747   {
748          int     i, j;
749          MODCONT *mp;
750 +        FILE    *fp;
751                                                  /* output modifiers in order */
752          for (i = 0; i < nmods; i++) {
709                FILE    *fp;
753                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
754                  fp = getofile(mp->outspec, mp->modname, 0);
755 <                putcontrib(mp->cbin[0], fp);
755 >                put_contrib(mp->cbin[0], fp);
756                  if (mp->nbins > 3 &&            /* minor optimization */
757                                  fp == getofile(mp->outspec, mp->modname, 1))
758                          for (j = 1; j < mp->nbins; j++)
759 <                                putcontrib(mp->cbin[j], fp);
759 >                                put_contrib(mp->cbin[j], fp);
760                  else
761                          for (j = 1; j < mp->nbins; j++)
762 <                                putcontrib(mp->cbin[j],
762 >                                put_contrib(mp->cbin[j],
763                                          getofile(mp->outspec, mp->modname, j));
764                                                  /* clear for next ray tree */
765                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
# Line 888 | Line 931 | trace_contribs(FILE *fin)
931                  rtp = get_rproc();              /* get avail. rtrace process */
932                  rtp->raynum = ++lastray;        /* assign ray to it */
933                  writebuf(rtp->pd.w, inpbuf, iblen);
934 <                if (!--raysleft)
934 >                if (raysleft && !--raysleft)
935                          break;
936                  process_queue();                /* catch up with results */
937          }
938          while (wait_rproc() != NULL)            /* process outstanding rays */
939                  process_queue();
940 <        if (raysleft > 0)
940 >        if (raysleft)
941                  error(USER, "unexpected EOF on input");
942 +        lu_done(&ofiletab);                     /* close output files */
943 + }
944 +
945 + /* seek on the given output file */
946 + static int
947 + myseeko(const LUENT *e, void *p)
948 + {
949 +        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
950 +                sprintf(errmsg, "seek error on file '%s'", e->key);
951 +                error(SYSTEM, errmsg);
952 +        }
953 + }
954 +
955 + /* recover output if possible */
956 + void
957 + recover_output(FILE *fin)
958 + {
959 +        off_t   lastout = -1;
960 +        int     outvsiz;
961 +        char    *outvfmt;
962 +        int     i, j;
963 +        MODCONT *mp;
964 +        int     ofl;
965 +        char    oname[1024];
966 +        LUENT   *ment, *oent;
967 +        FILE    *fp;
968 +        off_t   nvals;
969 +
970 +        switch (outfmt) {
971 +        case 'a':
972 +                error(USER, "cannot recover ASCII output");
973 +                return;
974 +        case 'f':
975 +                outvsiz = sizeof(float)*3;
976 +                outvfmt = "float";
977 +                break;
978 +        case 'd':
979 +                outvsiz = sizeof(double)*3;
980 +                outvfmt = "double";
981 +                break;
982 +        case 'c':
983 +                outvsiz = sizeof(COLR);
984 +                outvfmt = COLRFMT;
985 +                break;
986 +        default:
987 +                error(INTERNAL, "botched output format");
988 +                return;
989 +        }
990 +                                                /* check modifier outputs */
991 +        for (i = 0; i < nmods; i++) {
992 +                ment = lu_find(&modconttab,modname[i]);
993 +                mp = (MODCONT *)ment->data;
994 +                if (mp->outspec == NULL)
995 +                        error(USER, "cannot recover from stdout");
996 +                for (j = 0; ; j++) {            /* check each bin's file */
997 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
998 +                        if (ofl < 0)
999 +                                error(USER, "bad output file specification");
1000 +                        oent = lu_find(&ofiletab, oname);
1001 +                        if (oent->data != NULL) { /* saw this one already */
1002 +                                if (ofl & OF_BIN)
1003 +                                        continue;
1004 +                                break;
1005 +                        }
1006 +                        if (oname[0] == '!')
1007 +                                error(USER, "cannot recover from command");
1008 +                                                /* open output */
1009 +                        fp = fopen(oname, "rb+");
1010 +                        if (fp == NULL)
1011 +                                break;          /* must be end of modifier */
1012 +                        nvals = lseek(fileno(fp), 0, SEEK_END);
1013 +                        if (nvals <= 0) {
1014 +                                lastout = 0;    /* empty output, quit here */
1015 +                                fclose(fp);
1016 +                                break;
1017 +                        }
1018 +                        lseek(fileno(fp), 0, SEEK_SET);
1019 +                        if (header) {
1020 +                                int     xr, yr;
1021 +                                if (checkheader(fp, outvfmt, NULL) != 1) {
1022 +                                        sprintf(errmsg,
1023 +                                                "format mismatch for '%s'",
1024 +                                                        oname);
1025 +                                        error(USER, errmsg);
1026 +                                }
1027 +                                if (xres > 0 && yres > 0 &&
1028 +                                                (!fscnresolu(&xr, &yr, fp) ||
1029 +                                                        xr != xres ||
1030 +                                                        yr != yres)) {
1031 +                                        sprintf(errmsg,
1032 +                                                "resolution mismatch for '%s'",
1033 +                                                        oname);
1034 +                                        error(USER, errmsg);
1035 +                                }
1036 +                        }
1037 +                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1038 +                        if (lastout < 0 || nvals < lastout)
1039 +                                lastout = nvals;
1040 +                        if (oent->key == NULL) /* new entry */
1041 +                                oent->key = strcpy((char *)
1042 +                                                malloc(strlen(oname)+1), oname);
1043 +                        oent->data = (char *)fp;
1044 +                        if (!(ofl & OF_BIN))
1045 +                                break;          /* no bin separation */
1046 +                }
1047 +                if (!lastout) {                 /* empty output */
1048 +                        error(WARNING, "no data to recover");
1049 +                        lu_done(&ofiletab);     /* reclose all outputs */
1050 +                        return;
1051 +                }
1052 +                if (j > mp->nbins) {            /* preallocate modifier bins */
1053 +                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1054 +                                                        (j-1)*sizeof(DCOLOR));
1055 +                        if (mp == NULL)
1056 +                                error(SYSTEM, "out of memory in recover_output");
1057 +                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1058 +                        mp->nbins = j;
1059 +                        ment->data = (char *)mp;
1060 +                }
1061 +        }
1062 +                                                /* seek on all files */
1063 +        nvals = lastout * outvsiz;
1064 +        lu_doall(&ofiletab, myseeko, &nvals);
1065 +                                                /* discard input */
1066 +        for (nvals = 0; nvals < lastout; nvals++)
1067 +                if (getinp(oname, fin) <= 0)
1068 +                        error(USER, "unexpected EOF on input");
1069 +        lastray = lastdone = (unsigned long)lastout;
1070 +        if (raysleft)
1071 +                raysleft -= lastray;
1072   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines