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.27 by greg, Tue Sep 20 17:05:11 2005 UTC vs.
Revision 1.44 by greg, Thu Nov 15 18:32:34 2007 UTC

# Line 25 | Line 25 | int    treebufsiz = BUFSIZ;            /* current tree buffer size
25   typedef double  DCOLOR[3];              /* double-precision color */
26  
27   /*
28 < * The modcont structure is used to accumulate ray contributions
28 > * The MODCONT structure is used to accumulate ray contributions
29   * for a particular modifier, which may be subdivided into bins
30 < * if binv is non-NULL.  If outspec contains a %s in it, this will
30 > * if binv is non-zero.  If outspec contains a %s in it, this will
31   * be replaced with the modifier name.  If outspec contains a %d in it,
32   * this will be used to create one output file per bin, otherwise all bins
33   * will be written to the same file, in order.  If the global outfmt
# Line 46 | Line 46 | static void mcfree(void *p) { epfree((*(MODCONT *)p).b
46  
47   LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */
48  
49 < /* close output stream */
50 < static void closefile(void *p) { fclose((FILE *)p); }
49 > #define CNT_UNKNOWN     0               /* unknown record length */
50 > #define CNT_PIPE        (-1)            /* output to a pipe */
51 > /*
52 > * The STREAMOUT structure holds an open FILE pointer and a count of
53 > * the number of RGB triplets per record, or CNT_UNKNOWN (0) if
54 > * unknown or CNT_PIPE (-1) if writing to a command.
55 > */
56 > typedef struct {
57 >        FILE            *ofp;           /* output file pointer */
58 >        int             reclen;         /* triplets/record */
59 > } STREAMOUT;
60  
61 < LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
61 > /* close output stream and free record */
62 > static void
63 > closestream(void *p)
64 > {
65 >        STREAMOUT       *sop = (STREAMOUT *)p;
66 >        int             status;
67 >        if (sop->reclen == CNT_PIPE)
68 >                status = pclose(sop->ofp);
69 >        else
70 >                status = fclose(sop->ofp);
71 >        if (status)
72 >                error(SYSTEM, "error closing output stream");
73 >        free(p);
74 > }
75  
76 + LUTAB   ofiletab = LU_SINIT(free,closestream);  /* output file table */
77 +
78   #define OF_MODIFIER     01
79   #define OF_BIN          02
80  
81 < FILE *getofile(const char *ospec, const char *mname, int bn);
81 > STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
82   int ofname(char *oname, const char *ospec, const char *mname, int bn);
83   void printheader(FILE *fout, const char *info);
84 + void printresolu(FILE *fout);
85  
86   /*
87   * The rcont structure is used to manage i/o with a particular
# Line 76 | Line 101 | struct rtproc {
101                                          /* rtrace command and defaults */
102   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
103                                  "-dj", ".5", "-dr", "3",
104 <                                "-ab", "1", "-ad", "128", };
104 >                                "-ab", "1", "-ad", "350", };
105 >
106   int  rtargc = 9;
107                                          /* overriding rtrace options */
108 < char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
108 > char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
109                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
110  
111 + #define RTCOEFF         "-o~~TmWdp"     /* compute coefficients only */
112 + #define RTCONTRIB       "-o~~TmVdp"     /* compute ray contributions */
113 +
114   struct rtproc   rt0;                    /* head of rtrace process list */
115  
116   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
117  
118 < char    persistfn[] = "pfXXXXXX";       /* persist file name */
118 > #define PERSIST_NONE    0               /* no persist file */
119 > #define PERSIST_SINGLE  1               /* user set -P persist */
120 > #define PERSIST_PARALL  2               /* user set -PP persist */
121 > #define PERSIST_OURS    3               /* -PP persist belongs to us */
122 > int     persist_state = PERSIST_NONE;   /* persist file state */
123 > char    persistfn[] = "pfXXXXXX";       /* our persist file name, if set */
124  
125   int             gargc;                  /* global argc */
126   char            **gargv;                /* global argv */
# Line 98 | Line 132 | int            inpfmt = 'a';           /* input format */
132   int             outfmt = 'a';           /* output format */
133  
134   int             header = 1;             /* output header? */
135 + int             force_open = 0;         /* truncate existing output? */
136   int             xres = 0;               /* horiz. output resolution */
137   int             yres = 0;               /* vert. output resolution */
138  
# Line 112 | Line 147 | int            using_stdout = 0;       /* are we using stdout? */
147   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
148   int             nmods = 0;              /* number of modifiers */
149  
150 < MODCONT *addmodifier(char *modn, char *outf, char *binv);
116 < void addmodfile(char *fname, char *outf, char *binv);
150 > #define queue_length()          (lastray - lastdone)
151  
152 + MODCONT *growmodifier(MODCONT *mp, int nb);
153 + MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt);
154 + void addmodfile(char *fname, char *outf, char *binv, int bincnt);
155 +
156   void init(int np);
157   int done_rprocs(struct rtproc *rtp);
158   void recover_output(FILE *fin);
# Line 128 | Line 166 | void put_contrib(const DCOLOR cnt, FILE *fout);
166   void add_contrib(const char *modn);
167   void done_contrib(void);
168  
169 + /* return number of open rtrace processes */
170 + static int
171 + nrtprocs(void)
172 + {
173 +        int     nrtp = 0;
174 +        struct rtproc   *rtp;
175 +
176 +        for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
177 +                nrtp += rtp->pd.running;
178 +        return(nrtp);
179 + }
180 +
181   /* set input/output format */
182   static void
183   setformat(const char *fmt)
# Line 167 | Line 217 | fmterr:
217   int
218   main(int argc, char *argv[])
219   {
220 +        int     contrib = 0;
221          int     nprocs = 1;
222          int     recover = 0;
223          char    *curout = NULL;
224          char    *binval = NULL;
225 +        int     bincnt = 0;
226          char    fmt[8];
227          int     i, j;
228                                  /* need at least one argument */
229          if (argc < 2) {
230                  fprintf(stderr,
231 < "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
231 > "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
232                          argv[0]);
233                  exit(1);
234          }
# Line 198 | Line 250 | main(int argc, char *argv[])
250                  }
251                  if (argv[i][0] == '-')
252                          switch (argv[i][1]) {
201                        case 'r':               /* recover output */
202                                if (argv[i][2]) break;
203                                recover++;
204                                continue;
253                          case 'n':               /* number of processes */
254                                  if (argv[i][2] || i >= argc-2) break;
255                                  nprocs = atoi(argv[++i]);
256                                  if (nprocs <= 0)
257                                          error(USER, "illegal number of processes");
258                                  continue;
259 +                        case 'V':               /* output contributions */
260 +                                switch (argv[i][2]) {
261 +                                case '\0':
262 +                                        contrib = !contrib;
263 +                                        continue;
264 +                                case '+': case '1':
265 +                                case 'T': case 't':
266 +                                case 'Y': case 'y':
267 +                                        contrib = 1;
268 +                                        continue;
269 +                                case '-': case '0':
270 +                                case 'F': case 'f':
271 +                                case 'N': case 'n':
272 +                                        contrib = 0;
273 +                                        continue;
274 +                                }
275 +                                break;
276 +                        case 'r':               /* recover output */
277 +                                if (argv[i][2]) break;
278 +                                recover++;
279 +                                continue;
280                          case 'h':               /* output header? */
281                                  switch (argv[i][2]) {
282                                  case '\0':
# Line 225 | Line 294 | main(int argc, char *argv[])
294                                          continue;
295                                  }
296                                  break;
297 <                        case 'f':               /* file or i/o format */
297 >                        case 'f':               /* file or force or format */
298                                  if (!argv[i][2]) {
299                                          char    *fpath;
300                                          if (i >= argc-2) break;
# Line 240 | Line 309 | main(int argc, char *argv[])
309                                          fcompile(fpath);
310                                          continue;
311                                  }
312 +                                if (argv[i][2] == 'o') {
313 +                                        force_open++;
314 +                                        continue;
315 +                                }
316                                  setformat(argv[i]+2);
317                                  continue;
318                          case 'e':               /* expression */
# Line 258 | Line 331 | main(int argc, char *argv[])
331                                  if (argv[i][2] || i >= argc-2) break;
332                                  yres = atoi(argv[++i]);
333                                  continue;
334 <                        case 'b':               /* bin expression */
335 <                                if (argv[i][2] || i >= argc-2) break;
334 >                        case 'b':               /* bin expression/count */
335 >                                if (i >= argc-2) break;
336 >                                if (argv[i][2] == 'n') {
337 >                                        bincnt = atoi(argv[++i]);
338 >                                        continue;
339 >                                }
340 >                                if (argv[i][2]) break;
341                                  binval = argv[++i];
342                                  continue;
343                          case 'm':               /* modifier name */
344                                  if (argv[i][2] || i >= argc-2) break;
345                                  rtargv[rtargc++] = "-ti";
346                                  rtargv[rtargc++] = argv[++i];
347 <                                addmodifier(argv[i], curout, binval);
347 >                                addmodifier(argv[i], curout, binval, bincnt);
348                                  continue;
349                          case 'M':               /* modifier file */
350                                  if (argv[i][2] || i >= argc-2) break;
351                                  rtargv[rtargc++] = "-tI";
352                                  rtargv[rtargc++] = argv[++i];
353 <                                addmodfile(argv[i], curout, binval);
353 >                                addmodfile(argv[i], curout, binval, bincnt);
354                                  continue;
355 +                        case 'P':               /* persist file */
356 +                                if (i >= argc-2) break;
357 +                                persist_state = (argv[i][2] == 'P') ?
358 +                                                PERSIST_PARALL : PERSIST_SINGLE;
359 +                                rtargv[rtargc++] = argv[i];
360 +                                rtargv[rtargc++] = argv[++i];
361 +                                continue;
362                          }
363                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
364          }
# Line 282 | Line 367 | main(int argc, char *argv[])
367                                  /* add "mandatory" rtrace settings */
368          for (j = 0; myrtopts[j] != NULL; j++)
369                  rtargv[rtargc++] = myrtopts[j];
370 +        rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
371                                  /* just asking for defaults? */
372          if (!strcmp(argv[i], "-defaults")) {
373                  char    sxres[16], syres[16];
374                  char    *rtpath;
375                  printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
376 +                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
377 +                                contrib ? "contributions" : "coefficients");
378                  fflush(stdout);                 /* report OUR options */
379                  rtargv[rtargc++] = header ? "-h+" : "-h-";
380                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 297 | Line 385 | main(int argc, char *argv[])
385                  rtargv[rtargc++] = "-y";
386                  sprintf(syres, "%d", yres);
387                  rtargv[rtargc++] = syres;
300                rtargv[rtargc++] = "-oTW";
388                  rtargv[rtargc++] = "-defaults";
389                  rtargv[rtargc] = NULL;
390                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 311 | Line 398 | main(int argc, char *argv[])
398                  exit(1);
399          }
400          if (nprocs > 1) {       /* add persist file if parallel */
401 <                rtargv[rtargc++] = "-PP";
402 <                rtargv[rtargc++] = mktemp(persistfn);
401 >                if (persist_state == PERSIST_SINGLE)
402 >                        error(USER, "use -PP option for multiple processes");
403 >                if (persist_state == PERSIST_NONE) {
404 >                        rtargv[rtargc++] = "-PP";
405 >                        rtargv[rtargc++] = mktemp(persistfn);
406 >                        persist_state = PERSIST_OURS;
407 >                }
408          }
409                                  /* add format string */
410          sprintf(fmt, "-f%cf", inpfmt);
# Line 372 | Line 464 | quit(int status)
464   {
465          int     rtstat;
466  
467 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
467 >        if (persist_state == PERSIST_OURS)  /* terminate persistent rtrace */
468                  killpersist();
469                                          /* clean up rtrace process(es) */
470          rtstat = done_rprocs(&rt0);
# Line 442 | Line 534 | init(int np)
534          waitflush = xres;
535   }
536  
537 + /* grow modifier to accommodate more bins */
538 + MODCONT *
539 + growmodifier(MODCONT *mp, int nb)
540 + {
541 +        if (nb <= mp->nbins)
542 +                return mp;
543 +        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
544 +        if (mp == NULL)
545 +                error(SYSTEM, "out of memory in growmodifier");
546 +        memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
547 +        mp->nbins = nb;
548 +        return mp;
549 + }
550 +
551   /* add modifier to our list to track */
552   MODCONT *
553 < addmodifier(char *modn, char *outf, char *binv)
553 > addmodifier(char *modn, char *outf, char *binv, int bincnt)
554   {
555          LUENT   *lep = lu_find(&modconttab, modn);
556          MODCONT *mp;
557 +        int     i;
558          
559          if (lep->data != NULL) {
560                  sprintf(errmsg, "duplicate modifier '%s'", modn);
# Line 460 | Line 567 | addmodifier(char *modn, char *outf, char *binv)
567          mp = (MODCONT *)malloc(sizeof(MODCONT));
568          if (mp == NULL)
569                  error(SYSTEM, "out of memory in addmodifier");
463        lep->data = (char *)mp;
570          mp->outspec = outf;             /* XXX assumes static string */
571          mp->modname = modn;             /* XXX assumes static string */
572          if (binv != NULL)
# Line 469 | Line 575 | addmodifier(char *modn, char *outf, char *binv)
575                  mp->binv = eparse("0");
576          mp->nbins = 1;
577          setcolor(mp->cbin[0], 0., 0., 0.);
578 +        if (mp->binv->type == NUM)      /* assume one bin if constant */
579 +                bincnt = 1;
580 +        else if (bincnt > 1)
581 +                mp = growmodifier(mp, bincnt);
582 +        lep->data = (char *)mp;
583 +                                        /* allocate output streams */
584 +        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i-- > 0; )
585 +                getostream(mp->outspec, mp->modname, i, 1);
586          return mp;
587   }
588  
589   /* add modifiers from a file list */
590   void
591 < addmodfile(char *fname, char *outf, char *binv)
591 > addmodfile(char *fname, char *outf, char *binv, int bincnt)
592   {
593          char    *mname[MAXMODLIST];
594          int     i;
# Line 484 | Line 598 | addmodfile(char *fname, char *outf, char *binv)
598                  error(SYSTEM, errmsg);
599          }
600          for (i = 0; mname[i]; i++)      /* add each one */
601 <                addmodifier(mname[i], outf, binv);
601 >                addmodifier(mname[i], outf, binv, bincnt);
602   }
603  
604   /* put string to stderr */
# Line 584 | Line 698 | printheader(FILE *fout, const char *info)
698                  break;
699          }
700          fputc('\n', fout);
701 + }
702 +
703 + /* write resolution string to given output stream */
704 + void
705 + printresolu(FILE *fout)
706 + {
707          if (xres > 0) {
708                  if (yres > 0)                   /* resolution string */
709                          fprtresolu(xres, yres, fout);
# Line 591 | Line 711 | printheader(FILE *fout, const char *info)
711          }
712   }
713  
714 < /* Get output file pointer (open and write header if new) */
715 < FILE *
716 < getofile(const char *ospec, const char *mname, int bn)
714 > /* Get output stream pointer (open and write header if new and noopen==0) */
715 > STREAMOUT *
716 > getostream(const char *ospec, const char *mname, int bn, int noopen)
717   {
718 <        int             ofl;
719 <        char            oname[1024];
720 <        LUENT           *lep;
718 >        static STREAMOUT        stdos;
719 >        int                     ofl;
720 >        char                    oname[1024];
721 >        LUENT                   *lep;
722 >        STREAMOUT               *sop;
723          
724          if (ospec == NULL) {                    /* use stdout? */
725 <                if (!using_stdout) {
725 >                if (!noopen && !using_stdout) {
726 >                        stdos.reclen = 0;
727                          if (outfmt != 'a')
728                                  SET_FILE_BINARY(stdout);
729                          if (header)
730                                  printheader(stdout, NULL);
731 +                        printresolu(stdout);
732 +                        using_stdout = 1;
733                  }
734 <                using_stdout = 1;
735 <                return stdout;
734 >                stdos.ofp = stdout;
735 >                stdos.reclen += noopen;
736 >                return &stdos;
737          }
738          ofl = ofname(oname, ospec, mname, bn);  /* get output name */
739          if (ofl < 0) {
# Line 617 | Line 743 | getofile(const char *ospec, const char *mname, int bn)
743          lep = lu_find(&ofiletab, oname);        /* look it up */
744          if (lep->key == NULL)                   /* new entry */
745                  lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
746 <        if (lep->data == NULL) {                /* open output file */
747 <                FILE            *fp;
748 <                int             i;
746 >        sop = (STREAMOUT *)lep->data;
747 >        if (sop == NULL) {                      /* allocate stream */
748 >                sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
749 >                if (sop == NULL)
750 >                        error(SYSTEM, "out of memory in getostream");
751 >                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
752 >                sop->ofp = NULL;                /* open iff noopen==0 */
753 >                lep->data = (char *)sop;
754 >        }
755 >        if (!noopen && sop->ofp == NULL) {      /* open output stream */
756 >                long            i;
757                  if (oname[0] == '!')            /* output to command */
758 <                        fp = popen(oname+1, "w");
759 <                else
760 <                        fp = fopen(oname, "w");
761 <                if (fp == NULL) {
758 >                        sop->ofp = popen(oname+1, "w");
759 >                else if (!force_open && access(oname, F_OK) == 0)
760 >                        errno = EEXIST;         /* file exists */
761 >                else                            /* else open it */
762 >                        sop->ofp = fopen(oname, "w");
763 >                if (sop->ofp == NULL) {
764                          sprintf(errmsg, "cannot open '%s' for writing", oname);
765                          error(SYSTEM, errmsg);
766                  }
767                  if (outfmt != 'a')
768 <                        SET_FILE_BINARY(fp);
768 >                        SET_FILE_BINARY(sop->ofp);
769                  if (header) {
770                          char    info[512];
771                          char    *cp = info;
772 <                        sprintf(cp, "MODIFIER=%s\n", mname);
773 <                        while (*cp) ++cp;
772 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
773 >                                sprintf(cp, "MODIFIER=%s\n", mname);
774 >                                while (*cp) ++cp;
775 >                        }
776                          if (ofl & OF_BIN) {
777                                  sprintf(cp, "BIN=%d\n", bn);
778                                  while (*cp) ++cp;
779                          }
780                          *cp = '\0';
781 <                        printheader(fp, info);
781 >                        printheader(sop->ofp, info);
782                  }
783 +                printresolu(sop->ofp);
784                                                  /* play catch-up */
785 <                for (i = 0; i < lastdone; i++) {
785 >                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
786 >                                                                i--; ) {
787                          static const DCOLOR     nocontrib = BLKCOLOR;
788 <                        put_contrib(nocontrib, fp);
788 >                        put_contrib(nocontrib, sop->ofp);
789                          if (outfmt == 'a')
790 <                                putc('\n', fp);
790 >                                putc('\n', sop->ofp);
791                  }
792                  if (xres > 0)
793 <                        fflush(fp);
654 <                lep->data = (char *)fp;
793 >                        fflush(sop->ofp);
794          }
795 <        return (FILE *)lep->data;               /* return open file pointer */
795 >        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
796 >                sop->reclen += noopen;
797 >        return sop;                             /* return open stream */
798   }
799  
800   /* read input ray into buffer */
801   int
802   getinp(char *buf, FILE *fp)
803   {
804 +        double  dv[3];
805 +        float   fv[3];
806          char    *cp;
807          int     i;
808  
# Line 668 | Line 811 | getinp(char *buf, FILE *fp)
811                  cp = buf;               /* make sure we get 6 floats */
812                  for (i = 0; i < 6; i++) {
813                          if (fgetword(cp, buf+127-cp, fp) == NULL)
814 <                                return 0;
814 >                                return -1;
815 >                        if (i >= 3)
816 >                                dv[i-3] = atof(cp);
817                          if ((cp = fskip(cp)) == NULL || *cp)
818 <                                return 0;
818 >                                return -1;
819                          *cp++ = ' ';
820                  }
821                  getc(fp);               /* get/put eol */
822                  *cp-- = '\0'; *cp = '\n';
823 +                if (DOT(dv,dv) <= FTINY*FTINY)
824 +                        return 0;       /* dummy ray */
825                  return strlen(buf);
826          case 'f':
827                  if (fread(buf, sizeof(float), 6, fp) < 6)
828 <                        return 0;
828 >                        return -1;
829 >                memcpy(fv, buf+3*sizeof(float), 3*sizeof(float));
830 >                if (DOT(fv,fv) <= FTINY*FTINY)
831 >                        return 0;       /* dummy ray */
832                  return sizeof(float)*6;
833          case 'd':
834                  if (fread(buf, sizeof(double), 6, fp) < 6)
835 <                        return 0;
835 >                        return -1;
836 >                memcpy(dv, buf+3*sizeof(double), 3*sizeof(double));
837 >                if (DOT(dv,dv) <= FTINY*FTINY)
838 >                        return 0;       /* dummy ray */
839                  return sizeof(double)*6;
840          }
841          error(INTERNAL, "botched input format");
842 <        return 0;       /* pro forma return */
842 >        return -1;      /* pro forma return */
843   }
844  
845   static float    rparams[9];             /* traced ray parameters */
# Line 716 | Line 869 | add_contrib(const char *modn)
869          bn = (int)(evalue(mp->binv) + .5);
870          if (bn <= 0)
871                  bn = 0;
872 <        else if (bn >= mp->nbins) {     /* new bin */
873 <                mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
721 <                                                bn*sizeof(DCOLOR));
722 <                if (mp == NULL)
723 <                        error(SYSTEM, "out of memory in add_contrib");
724 <                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
725 <                mp->nbins = bn+1;
726 <                le->data = (char *)mp;
727 <        }
872 >        else if (bn >= mp->nbins)       /* new bin */
873 >                le->data = (char *)(mp = growmodifier(mp, bn+1));
874          addcolor(mp->cbin[bn], rparams);
875   }
876  
# Line 732 | Line 878 | add_contrib(const char *modn)
878   static int
879   puteol(const LUENT *e, void *p)
880   {
881 <        FILE    *fp = (FILE *)e->data;
881 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
882  
883          if (outfmt == 'a')
884 <                putc('\n', fp);
884 >                putc('\n', sop->ofp);
885          if (!waitflush)
886 <                fflush(fp);
887 <        if (ferror(fp)) {
886 >                fflush(sop->ofp);
887 >        if (ferror(sop->ofp)) {
888                  sprintf(errmsg, "write error on file '%s'", e->key);
889                  error(SYSTEM, errmsg);
890          }
# Line 778 | Line 924 | put_contrib(const DCOLOR cnt, FILE *fout)
924   void
925   done_contrib(void)
926   {
927 <        int     i, j;
928 <        MODCONT *mp;
929 <        FILE    *fp;
927 >        int             i, j;
928 >        MODCONT         *mp;
929 >        STREAMOUT       *sop;
930                                                  /* output modifiers in order */
931          for (i = 0; i < nmods; i++) {
932                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
933 <                fp = getofile(mp->outspec, mp->modname, 0);
934 <                put_contrib(mp->cbin[0], fp);
933 >                sop = getostream(mp->outspec, mp->modname, 0,0);
934 >                put_contrib(mp->cbin[0], sop->ofp);
935                  if (mp->nbins > 3 &&            /* minor optimization */
936 <                                fp == getofile(mp->outspec, mp->modname, 1))
936 >                                sop == getostream(mp->outspec, mp->modname, 1,0))
937                          for (j = 1; j < mp->nbins; j++)
938 <                                put_contrib(mp->cbin[j], fp);
938 >                                put_contrib(mp->cbin[j], sop->ofp);
939                  else
940                          for (j = 1; j < mp->nbins; j++)
941                                  put_contrib(mp->cbin[j],
942 <                                        getofile(mp->outspec, mp->modname, j));
942 >                                    getostream(mp->outspec,mp->modname,j,0)->ofp);
943                                                  /* clear for next ray tree */
944                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
945          }
# Line 853 | Line 999 | process_queue(void)
999                          if (!n || !(isalpha(*cp) | (*cp == '_')))
1000                                  error(USER, "bad modifier name from rtrace");
1001                                          /* get modifier name */
1002 <                        while (n > 0 && *cp != '\t') {
1002 >                        while (n > 1 && *cp != '\t') {
1003 >                                if (mnp - modname >= sizeof(modname)-2)
1004 >                                        error(INTERNAL, "modifier name too long");
1005                                  *mnp++ = *cp++; n--;
1006                          }
1007                          *mnp = '\0';
# Line 867 | Line 1015 | process_queue(void)
1015                  }
1016                  done_contrib();         /* sum up contributions & output */
1017                  lastdone = rtp->raynum;
1018 <                free(rtp->buf);         /* free up buffer space */
1018 >                if (rtp->buf != NULL)   /* free up buffer space */
1019 >                        free(rtp->buf);
1020                  rt_unproc = rtp->next;
1021                  free(rtp);              /* done with this ray tree */
1022          }
# Line 915 | Line 1064 | wait_rproc(void)
1064                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1065                                          rt->bsiz = treebufsiz;
1066                                  else
1067 <                                        rt->bsiz = treebufsiz += BUFSIZ;
1067 >                                        treebufsiz = rt->bsiz += BUFSIZ;
1068                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1069                          }
1070                          if (rt->buf == NULL)
# Line 955 | Line 1104 | trace_contribs(FILE *fin)
1104          int             iblen;
1105          struct rtproc   *rtp;
1106                                                  /* loop over input */
1107 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1108 <                if (lastray+1 < lastray) {      /* counter rollover? */
1107 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1108 >                if (!iblen ||                   /* need reset? */
1109 >                                queue_length() > 10*nrtprocs() ||
1110 >                                lastray+1 < lastray) {
1111                          while (wait_rproc() != NULL)
1112                                  process_queue();
1113 <                        lastdone = lastray = 0;
1113 >                        if (lastray+1 < lastray)
1114 >                                lastdone = lastray = 0;
1115                  }
1116                  rtp = get_rproc();              /* get avail. rtrace process */
1117 <                rtp->raynum = ++lastray;        /* assign ray to it */
1118 <                writebuf(rtp->pd.w, inpbuf, iblen);
1119 <                if (raysleft && !--raysleft)
1120 <                        break;
1117 >                rtp->raynum = ++lastray;        /* assign ray */
1118 >                if (iblen) {                    /* trace ray if valid */
1119 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1120 >                } else {                        /* else bypass dummy ray */
1121 >                        queue_raytree(rtp);     /* empty tree */
1122 >                        if ((yres <= 0) | (waitflush > 1))
1123 >                                waitflush = 1;  /* flush after this */
1124 >                }
1125                  process_queue();                /* catch up with results */
1126 +                if (raysleft && !--raysleft)
1127 +                        break;                  /* preemptive EOI */
1128          }
1129          while (wait_rproc() != NULL)            /* process outstanding rays */
1130                  process_queue();
# Line 979 | Line 1137 | trace_contribs(FILE *fin)
1137   static int
1138   myseeko(const LUENT *e, void *p)
1139   {
1140 <        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
1140 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
1141 >        off_t           nbytes = *(off_t *)p;
1142 >        
1143 >        if (sop->reclen > 1)
1144 >                nbytes = nbytes * sop->reclen;
1145 >        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1146                  sprintf(errmsg, "seek error on file '%s'", e->key);
1147                  error(SYSTEM, errmsg);
1148          }
1149 +        return 0;
1150   }
1151  
1152   /* recover output if possible */
1153   void
1154   recover_output(FILE *fin)
1155   {
1156 <        off_t   lastout = -1;
1157 <        int     outvsiz;
1158 <        char    *outvfmt;
1159 <        int     i, j;
1160 <        MODCONT *mp;
1161 <        int     ofl;
1162 <        char    oname[1024];
1163 <        LUENT   *ment, *oent;
1164 <        FILE    *fp;
1165 <        off_t   nvals;
1156 >        off_t           lastout = -1;
1157 >        int             outvsiz, recsiz;
1158 >        char            *outvfmt;
1159 >        int             i, j;
1160 >        MODCONT         *mp;
1161 >        int             ofl;
1162 >        char            oname[1024];
1163 >        LUENT           *ment, *oent;
1164 >        STREAMOUT       sout;
1165 >        off_t           nvals;
1166 >        int             xr, yr;
1167  
1168          switch (outfmt) {
1169          case 'a':
# Line 1026 | Line 1191 | recover_output(FILE *fin)
1191                  mp = (MODCONT *)ment->data;
1192                  if (mp->outspec == NULL)
1193                          error(USER, "cannot recover from stdout");
1194 +                if (mp->outspec[0] == '!')
1195 +                        error(USER, "cannot recover from command");
1196                  for (j = 0; ; j++) {            /* check each bin's file */
1197                          ofl = ofname(oname, mp->outspec, mp->modname, j);
1198                          if (ofl < 0)
1199                                  error(USER, "bad output file specification");
1200                          oent = lu_find(&ofiletab, oname);
1201 <                        if (oent->data != NULL) { /* saw this one already */
1201 >                        if (oent->data != NULL) {
1202 >                                sout = *(STREAMOUT *)oent->data;
1203 >                        } else {
1204 >                                sout.reclen = CNT_UNKNOWN;
1205 >                                sout.ofp = NULL;
1206 >                        }
1207 >                        if (sout.ofp != NULL) { /* already open? */
1208                                  if (ofl & OF_BIN)
1209                                          continue;
1210                                  break;
1211                          }
1039                        if (oname[0] == '!')
1040                                error(USER, "cannot recover from command");
1212                                                  /* open output */
1213 <                        fp = fopen(oname, "rb+");
1214 <                        if (fp == NULL) {
1213 >                        sout.ofp = fopen(oname, "rb+");
1214 >                        if (sout.ofp == NULL) {
1215                                  if (j)
1216                                          break;  /* assume end of modifier */
1217                                  sprintf(errmsg, "missing recover file '%s'",
1218                                                  oname);
1219                                  error(USER, errmsg);
1220                          }
1221 <                        nvals = lseek(fileno(fp), 0, SEEK_END);
1221 >                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1222                          if (nvals <= 0) {
1223                                  lastout = 0;    /* empty output, quit here */
1224 <                                fclose(fp);
1224 >                                fclose(sout.ofp);
1225                                  break;
1226                          }
1227 <                        lseek(fileno(fp), 0, SEEK_SET);
1228 <                        if (header) {
1058 <                                int     xr, yr;
1059 <                                if (checkheader(fp, outvfmt, NULL) != 1) {
1227 >                        if (sout.reclen == CNT_UNKNOWN) {
1228 >                                if (!(ofl & OF_BIN)) {
1229                                          sprintf(errmsg,
1230 <                                                "format mismatch for '%s'",
1230 >                                                "need -bn to recover file '%s'",
1231                                                          oname);
1232                                          error(USER, errmsg);
1233                                  }
1234 <                                if (xres > 0 && yres > 0 &&
1235 <                                                (!fscnresolu(&xr, &yr, fp) ||
1236 <                                                        xr != xres ||
1237 <                                                        yr != yres)) {
1238 <                                        sprintf(errmsg,
1239 <                                                "resolution mismatch for '%s'",
1240 <                                                        oname);
1241 <                                        error(USER, errmsg);
1242 <                                }
1234 >                                recsiz = outvsiz;
1235 >                        } else
1236 >                                recsiz = outvsiz * sout.reclen;
1237 >
1238 >                        lseek(fileno(sout.ofp), 0, SEEK_SET);
1239 >                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1240 >                                sprintf(errmsg, "format mismatch for '%s'",
1241 >                                                oname);
1242 >                                error(USER, errmsg);
1243                          }
1244 <                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1245 <                        if (lastout < 0 || nvals < lastout)
1244 >                        if ((xres > 0) & (yres > 0) &&
1245 >                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
1246 >                                                xr != xres ||
1247 >                                                yr != yres)) {
1248 >                                sprintf(errmsg, "resolution mismatch for '%s'",
1249 >                                                oname);
1250 >                                error(USER, errmsg);
1251 >                        }
1252 >                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
1253 >                        if ((lastout < 0) | (nvals < lastout))
1254                                  lastout = nvals;
1255 <                        if (oent->key == NULL) /* new entry */
1255 >                        if (oent->key == NULL)  /* new entry */
1256                                  oent->key = strcpy((char *)
1257                                                  malloc(strlen(oname)+1), oname);
1258 <                        oent->data = (char *)fp;
1258 >                        if (oent->data == NULL)
1259 >                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1260 >                        *(STREAMOUT *)oent->data = sout;
1261                          if (!(ofl & OF_BIN))
1262                                  break;          /* no bin separation */
1263                  }
# Line 1087 | Line 1266 | recover_output(FILE *fin)
1266                          lu_done(&ofiletab);     /* reclose all outputs */
1267                          return;
1268                  }
1269 <                if (j > mp->nbins) {            /* preallocate modifier bins */
1270 <                        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 <                }
1269 >                if (j > mp->nbins)              /* reallocate modifier bins */
1270 >                        ment->data = (char *)(mp = growmodifier(mp, j));
1271          }
1272          if (lastout < 0) {
1273                  error(WARNING, "no output files to recover");
# Line 1109 | Line 1281 | recover_output(FILE *fin)
1281                                                  /* seek on all files */
1282          nvals = lastout * outvsiz;
1283          lu_doall(&ofiletab, myseeko, &nvals);
1284 <                                                /* discard input */
1284 >                                                /* skip repeated input */
1285          for (nvals = 0; nvals < lastout; nvals++)
1286 <                if (getinp(oname, fin) <= 0)
1286 >                if (getinp(oname, fin) < 0)
1287                          error(USER, "unexpected EOF on input");
1288          lastray = lastdone = (unsigned long)lastout;
1289          if (raysleft)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines