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.15 by greg, Thu Jun 9 18:27:44 2005 UTC vs.
Revision 1.40 by greg, Sun Feb 5 22:22:21 2006 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  
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 44 | 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 72 | Line 99 | struct rtproc {
99   };                              /* rtrace process buffer */
100  
101                                          /* rtrace command and defaults */
102 < char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
102 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
103 >                                "-dj", ".5", "-dr", "3",
104                                  "-ab", "1", "-ad", "128", };
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 */
# Line 95 | Line 127 | int            inpfmt = 'a';           /* input format */
127   int             outfmt = 'a';           /* output format */
128  
129   int             header = 1;             /* output header? */
130 + int             force_open = 0;         /* truncate existing output? */
131   int             xres = 0;               /* horiz. output resolution */
132   int             yres = 0;               /* vert. output resolution */
133  
# Line 109 | Line 142 | int            using_stdout = 0;       /* are we using stdout? */
142   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
143   int             nmods = 0;              /* number of modifiers */
144  
145 < MODCONT *addmodifier(char *modn, char *outf, char *binv);
145 > #define queue_length()          (lastray - lastdone)
146  
147 + MODCONT *growmodifier(MODCONT *mp, int nb);
148 + MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt);
149 + void addmodfile(char *fname, char *outf, char *binv, int bincnt);
150 +
151   void init(int np);
152   int done_rprocs(struct rtproc *rtp);
153   void recover_output(FILE *fin);
# Line 124 | Line 161 | void put_contrib(const DCOLOR cnt, FILE *fout);
161   void add_contrib(const char *modn);
162   void done_contrib(void);
163  
164 + /* return number of open rtrace processes */
165 + static int
166 + nrtprocs(void)
167 + {
168 +        int     nrtp = 0;
169 +        struct rtproc   *rtp;
170 +
171 +        for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
172 +                nrtp += rtp->pd.running;
173 +        return(nrtp);
174 + }
175 +
176   /* set input/output format */
177   static void
178   setformat(const char *fmt)
# Line 163 | Line 212 | fmterr:
212   int
213   main(int argc, char *argv[])
214   {
215 +        int     contrib = 0;
216          int     nprocs = 1;
217          int     recover = 0;
218          char    *curout = NULL;
219          char    *binval = NULL;
220 +        int     bincnt = 0;
221          char    fmt[8];
222          int     i, j;
223 +                                /* need at least one argument */
224 +        if (argc < 2) {
225 +                fprintf(stderr,
226 + "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
227 +                        argv[0]);
228 +                exit(1);
229 +        }
230                                  /* global program name */
231          gargv = argv;
232                                  /* initialize calcomp routines */
# Line 187 | Line 245 | main(int argc, char *argv[])
245                  }
246                  if (argv[i][0] == '-')
247                          switch (argv[i][1]) {
190                        case 'r':               /* recover output */
191                                if (argv[i][2]) break;
192                                recover++;
193                                continue;
248                          case 'n':               /* number of processes */
249 <                                if (argv[i][2] || i >= argc-1) break;
249 >                                if (argv[i][2] || i >= argc-2) break;
250                                  nprocs = atoi(argv[++i]);
251                                  if (nprocs <= 0)
252                                          error(USER, "illegal number of processes");
253                                  continue;
254 +                        case 'V':               /* output contributions */
255 +                                switch (argv[i][2]) {
256 +                                case '\0':
257 +                                        contrib = !contrib;
258 +                                        continue;
259 +                                case '+': case '1':
260 +                                case 'T': case 't':
261 +                                case 'Y': case 'y':
262 +                                        contrib = 1;
263 +                                        continue;
264 +                                case '-': case '0':
265 +                                case 'F': case 'f':
266 +                                case 'N': case 'n':
267 +                                        contrib = 0;
268 +                                        continue;
269 +                                }
270 +                                break;
271 +                        case 'r':               /* recover output */
272 +                                if (argv[i][2]) break;
273 +                                recover++;
274 +                                continue;
275                          case 'h':               /* output header? */
276                                  switch (argv[i][2]) {
277                                  case '\0':
# Line 214 | Line 289 | main(int argc, char *argv[])
289                                          continue;
290                                  }
291                                  break;
292 <                        case 'f':               /* file or i/o format */
292 >                        case 'f':               /* file or force or format */
293                                  if (!argv[i][2]) {
294                                          char    *fpath;
295 <                                        if (i >= argc-1) break;
295 >                                        if (i >= argc-2) break;
296                                          fpath = getpath(argv[++i],
297                                                          getrlibpath(), R_OK);
298                                          if (fpath == NULL) {
# Line 229 | Line 304 | main(int argc, char *argv[])
304                                          fcompile(fpath);
305                                          continue;
306                                  }
307 +                                if (argv[i][2] == 'o') {
308 +                                        force_open++;
309 +                                        continue;
310 +                                }
311                                  setformat(argv[i]+2);
312                                  continue;
313                          case 'e':               /* expression */
314 <                                if (argv[i][2] || i >= argc-1) break;
314 >                                if (argv[i][2] || i >= argc-2) break;
315                                  scompile(argv[++i], NULL, 0);
316                                  continue;
317                          case 'o':               /* output file spec. */
318 <                                if (argv[i][2] || i >= argc-1) break;
318 >                                if (argv[i][2] || i >= argc-2) break;
319                                  curout = argv[++i];
320                                  continue;
321                          case 'x':               /* horiz. output resolution */
322 <                                if (argv[i][2] || i >= argc-1) break;
322 >                                if (argv[i][2] || i >= argc-2) break;
323                                  xres = atoi(argv[++i]);
324                                  continue;
325                          case 'y':               /* vert. output resolution */
326 <                                if (argv[i][2] || i >= argc-1) break;
326 >                                if (argv[i][2] || i >= argc-2) break;
327                                  yres = atoi(argv[++i]);
328                                  continue;
329 <                        case 'b':               /* bin expression */
330 <                                if (argv[i][2] || i >= argc-1) break;
329 >                        case 'b':               /* bin expression/count */
330 >                                if (i >= argc-2) break;
331 >                                if (argv[i][2] == 'n') {
332 >                                        bincnt = atoi(argv[++i]);
333 >                                        continue;
334 >                                }
335 >                                if (argv[i][2]) break;
336                                  binval = argv[++i];
337                                  continue;
338                          case 'm':               /* modifier name */
339 <                                if (argv[i][2] || i >= argc-1) break;
339 >                                if (argv[i][2] || i >= argc-2) break;
340                                  rtargv[rtargc++] = "-ti";
341                                  rtargv[rtargc++] = argv[++i];
342 <                                addmodifier(argv[i], curout, binval);
342 >                                addmodifier(argv[i], curout, binval, bincnt);
343                                  continue;
344 +                        case 'M':               /* modifier file */
345 +                                if (argv[i][2] || i >= argc-2) break;
346 +                                rtargv[rtargc++] = "-tI";
347 +                                rtargv[rtargc++] = argv[++i];
348 +                                addmodfile(argv[i], curout, binval, bincnt);
349 +                                continue;
350 +                        case 'P':               /* persist file */
351 +                                error(USER, "persist file is automatic");
352 +                                break;
353                          }
354                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
355          }
# Line 265 | Line 358 | main(int argc, char *argv[])
358                                  /* add "mandatory" rtrace settings */
359          for (j = 0; myrtopts[j] != NULL; j++)
360                  rtargv[rtargc++] = myrtopts[j];
361 +        rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
362                                  /* just asking for defaults? */
363          if (!strcmp(argv[i], "-defaults")) {
364                  char    sxres[16], syres[16];
365                  char    *rtpath;
366                  printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
367 +                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
368 +                                contrib ? "contributions" : "coefficients");
369                  fflush(stdout);                 /* report OUR options */
370                  rtargv[rtargc++] = header ? "-h+" : "-h-";
371                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 280 | Line 376 | main(int argc, char *argv[])
376                  rtargv[rtargc++] = "-y";
377                  sprintf(syres, "%d", yres);
378                  rtargv[rtargc++] = syres;
283                rtargv[rtargc++] = "-oTW";
379                  rtargv[rtargc++] = "-defaults";
380                  rtargv[rtargc] = NULL;
381                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 305 | Line 400 | main(int argc, char *argv[])
400                  error(USER, "missing octree argument");
401          rtargv[rtargc++] = octree = argv[i];
402          rtargv[rtargc] = NULL;
403 <                                /* start rtrace & compute contributions */
403 >                                /* start rtrace */
404          init(nprocs);
405          if (recover)            /* perform recovery if requested */
406                  recover_output(stdin);
407 <        trace_contribs(stdin);
407 >        trace_contribs(stdin);  /* compute contributions */
408          quit(0);
409   }
410  
411 + #ifndef SIGALRM
412 + #define SIGALRM SIGTERM
413 + #endif
414   /* kill persistent rtrace process */
415   static void
416   killpersist(void)
417   {
418          FILE    *fp = fopen(persistfn, "r");
419 <        int     pid;
419 >        RT_PID  pid;
420  
421          if (fp == NULL)
422                  return;
# Line 422 | Line 520 | init(int np)
520          waitflush = xres;
521   }
522  
523 + /* grow modifier to accommodate more bins */
524 + MODCONT *
525 + growmodifier(MODCONT *mp, int nb)
526 + {
527 +        if (nb <= mp->nbins)
528 +                return mp;
529 +        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
530 +        if (mp == NULL)
531 +                error(SYSTEM, "out of memory in growmodifier");
532 +        memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
533 +        mp->nbins = nb;
534 +        return mp;
535 + }
536 +
537   /* add modifier to our list to track */
538   MODCONT *
539 < addmodifier(char *modn, char *outf, char *binv)
539 > addmodifier(char *modn, char *outf, char *binv, int bincnt)
540   {
541          LUENT   *lep = lu_find(&modconttab, modn);
542          MODCONT *mp;
543 +        int     i;
544          
545          if (lep->data != NULL) {
546                  sprintf(errmsg, "duplicate modifier '%s'", modn);
# Line 440 | Line 553 | addmodifier(char *modn, char *outf, char *binv)
553          mp = (MODCONT *)malloc(sizeof(MODCONT));
554          if (mp == NULL)
555                  error(SYSTEM, "out of memory in addmodifier");
443        lep->data = (char *)mp;
556          mp->outspec = outf;             /* XXX assumes static string */
557          mp->modname = modn;             /* XXX assumes static string */
558          if (binv != NULL)
# Line 449 | Line 561 | addmodifier(char *modn, char *outf, char *binv)
561                  mp->binv = eparse("0");
562          mp->nbins = 1;
563          setcolor(mp->cbin[0], 0., 0., 0.);
564 +        if (mp->binv->type == NUM)      /* assume one bin if constant */
565 +                bincnt = 1;
566 +        else if (bincnt > 1)
567 +                mp = growmodifier(mp, bincnt);
568 +        lep->data = (char *)mp;
569 +                                        /* allocate output streams */
570 +        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; )
571 +                getostream(mp->outspec, mp->modname, i, 1);
572          return mp;
573   }
574  
575 + /* add modifiers from a file list */
576 + void
577 + addmodfile(char *fname, char *outf, char *binv, int bincnt)
578 + {
579 +        char    *mname[MAXMODLIST];
580 +        int     i;
581 +                                        /* find the file & store strings */
582 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
583 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
584 +                error(SYSTEM, errmsg);
585 +        }
586 +        for (i = 0; mname[i]; i++)      /* add each one */
587 +                addmodifier(mname[i], outf, binv, bincnt);
588 + }
589 +
590   /* put string to stderr */
591   void
592   eputs(char  *s)
# Line 549 | Line 684 | printheader(FILE *fout, const char *info)
684                  break;
685          }
686          fputc('\n', fout);
687 + }
688 +
689 + /* write resolution string to given output stream */
690 + void
691 + printresolu(FILE *fout)
692 + {
693          if (xres > 0) {
694                  if (yres > 0)                   /* resolution string */
695                          fprtresolu(xres, yres, fout);
# Line 556 | Line 697 | printheader(FILE *fout, const char *info)
697          }
698   }
699  
700 < /* Get output file pointer (open and write header if new) */
701 < FILE *
702 < getofile(const char *ospec, const char *mname, int bn)
700 > /* Get output stream pointer (open and write header if new and noopen==0) */
701 > STREAMOUT *
702 > getostream(const char *ospec, const char *mname, int bn, int noopen)
703   {
704 <        int             ofl;
705 <        char            oname[1024];
706 <        LUENT           *lep;
704 >        static STREAMOUT        stdos;
705 >        int                     ofl;
706 >        char                    oname[1024];
707 >        LUENT                   *lep;
708 >        STREAMOUT               *sop;
709          
710          if (ospec == NULL) {                    /* use stdout? */
711 <                if (!using_stdout) {
711 >                if (!noopen && !using_stdout) {
712 >                        stdos.reclen = 0;
713                          if (outfmt != 'a')
714                                  SET_FILE_BINARY(stdout);
715                          if (header)
716                                  printheader(stdout, NULL);
717 +                        printresolu(stdout);
718 +                        using_stdout = 1;
719                  }
720 <                using_stdout = 1;
721 <                return stdout;
720 >                stdos.ofp = stdout;
721 >                stdos.reclen += noopen;
722 >                return &stdos;
723          }
724          ofl = ofname(oname, ospec, mname, bn);  /* get output name */
725          if (ofl < 0) {
# Line 582 | Line 729 | getofile(const char *ospec, const char *mname, int bn)
729          lep = lu_find(&ofiletab, oname);        /* look it up */
730          if (lep->key == NULL)                   /* new entry */
731                  lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
732 <        if (lep->data == NULL) {                /* open output file */
733 <                FILE            *fp;
734 <                int             i;
732 >        sop = (STREAMOUT *)lep->data;
733 >        if (sop == NULL) {                      /* allocate stream */
734 >                sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
735 >                if (sop == NULL)
736 >                        error(SYSTEM, "out of memory in getostream");
737 >                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
738 >                sop->ofp = NULL;                /* open iff noopen==0 */
739 >                lep->data = (char *)sop;
740 >        }
741 >        if (!noopen && sop->ofp == NULL) {      /* open output stream */
742 >                long            i;
743                  if (oname[0] == '!')            /* output to command */
744 <                        fp = popen(oname+1, "w");
745 <                else
746 <                        fp = fopen(oname, "w");
747 <                if (fp == NULL) {
744 >                        sop->ofp = popen(oname+1, "w");
745 >                else if (!force_open && access(oname, F_OK) == 0)
746 >                        errno = EEXIST;         /* file exists */
747 >                else                            /* else open it */
748 >                        sop->ofp = fopen(oname, "w");
749 >                if (sop->ofp == NULL) {
750                          sprintf(errmsg, "cannot open '%s' for writing", oname);
751                          error(SYSTEM, errmsg);
752                  }
753                  if (outfmt != 'a')
754 <                        SET_FILE_BINARY(fp);
754 >                        SET_FILE_BINARY(sop->ofp);
755                  if (header) {
756                          char    info[512];
757                          char    *cp = info;
758 <                        if (ofl & OF_MODIFIER) {
758 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
759                                  sprintf(cp, "MODIFIER=%s\n", mname);
760                                  while (*cp) ++cp;
761                          }
# Line 607 | Line 764 | getofile(const char *ospec, const char *mname, int bn)
764                                  while (*cp) ++cp;
765                          }
766                          *cp = '\0';
767 <                        printheader(fp, info);
767 >                        printheader(sop->ofp, info);
768                  }
769 +                printresolu(sop->ofp);
770                                                  /* play catch-up */
771 <                for (i = 0; i < lastdone; i++) {
771 >                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
772 >                                                                i--; ) {
773                          static const DCOLOR     nocontrib = BLKCOLOR;
774 <                        put_contrib(nocontrib, fp);
774 >                        put_contrib(nocontrib, sop->ofp);
775                          if (outfmt == 'a')
776 <                                putc('\n', fp);
776 >                                putc('\n', sop->ofp);
777                  }
778                  if (xres > 0)
779 <                        fflush(fp);
621 <                lep->data = (char *)fp;
779 >                        fflush(sop->ofp);
780          }
781 <        return (FILE *)lep->data;               /* return open file pointer */
781 >        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
782 >                sop->reclen += noopen;
783 >        return sop;                             /* return open stream */
784   }
785  
786   /* read input ray into buffer */
787   int
788   getinp(char *buf, FILE *fp)
789   {
790 +        double  dv[3];
791 +        float   fv[3];
792          char    *cp;
793          int     i;
794  
# Line 635 | Line 797 | getinp(char *buf, FILE *fp)
797                  cp = buf;               /* make sure we get 6 floats */
798                  for (i = 0; i < 6; i++) {
799                          if (fgetword(cp, buf+127-cp, fp) == NULL)
800 <                                return 0;
800 >                                return -1;
801 >                        if (i >= 3)
802 >                                dv[i-3] = atof(cp);
803                          if ((cp = fskip(cp)) == NULL || *cp)
804 <                                return 0;
804 >                                return -1;
805                          *cp++ = ' ';
806                  }
807                  getc(fp);               /* get/put eol */
808                  *cp-- = '\0'; *cp = '\n';
809 +                if (DOT(dv,dv) <= FTINY*FTINY)
810 +                        return 0;       /* dummy ray */
811                  return strlen(buf);
812          case 'f':
813                  if (fread(buf, sizeof(float), 6, fp) < 6)
814 <                        return 0;
814 >                        return -1;
815 >                memcpy(fv, buf+3*sizeof(float), 3*sizeof(float));
816 >                if (DOT(fv,fv) <= FTINY*FTINY)
817 >                        return 0;       /* dummy ray */
818                  return sizeof(float)*6;
819          case 'd':
820                  if (fread(buf, sizeof(double), 6, fp) < 6)
821 <                        return 0;
821 >                        return -1;
822 >                memcpy(dv, buf+3*sizeof(double), 3*sizeof(double));
823 >                if (DOT(dv,dv) <= FTINY*FTINY)
824 >                        return 0;       /* dummy ray */
825                  return sizeof(double)*6;
826          }
827          error(INTERNAL, "botched input format");
828 <        return 0;       /* pro forma return */
828 >        return -1;      /* pro forma return */
829   }
830  
831   static float    rparams[9];             /* traced ray parameters */
# Line 683 | Line 855 | add_contrib(const char *modn)
855          bn = (int)(evalue(mp->binv) + .5);
856          if (bn <= 0)
857                  bn = 0;
858 <        else if (bn > mp->nbins) {      /* new bin */
859 <                mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
688 <                                                bn*sizeof(DCOLOR));
689 <                if (mp == NULL)
690 <                        error(SYSTEM, "out of memory in add_contrib");
691 <                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
692 <                mp->nbins = bn+1;
693 <                le->data = (char *)mp;
694 <        }
858 >        else if (bn >= mp->nbins)       /* new bin */
859 >                le->data = (char *)(mp = growmodifier(mp, bn+1));
860          addcolor(mp->cbin[bn], rparams);
861   }
862  
# Line 699 | Line 864 | add_contrib(const char *modn)
864   static int
865   puteol(const LUENT *e, void *p)
866   {
867 <        FILE    *fp = (FILE *)e->data;
867 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
868  
869          if (outfmt == 'a')
870 <                putc('\n', fp);
870 >                putc('\n', sop->ofp);
871          if (!waitflush)
872 <                fflush(fp);
873 <        if (ferror(fp)) {
872 >                fflush(sop->ofp);
873 >        if (ferror(sop->ofp)) {
874                  sprintf(errmsg, "write error on file '%s'", e->key);
875                  error(SYSTEM, errmsg);
876          }
# Line 745 | Line 910 | put_contrib(const DCOLOR cnt, FILE *fout)
910   void
911   done_contrib(void)
912   {
913 <        int     i, j;
914 <        MODCONT *mp;
915 <        FILE    *fp;
913 >        int             i, j;
914 >        MODCONT         *mp;
915 >        STREAMOUT       *sop;
916                                                  /* output modifiers in order */
917          for (i = 0; i < nmods; i++) {
918                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
919 <                fp = getofile(mp->outspec, mp->modname, 0);
920 <                put_contrib(mp->cbin[0], fp);
919 >                sop = getostream(mp->outspec, mp->modname, 0,0);
920 >                put_contrib(mp->cbin[0], sop->ofp);
921                  if (mp->nbins > 3 &&            /* minor optimization */
922 <                                fp == getofile(mp->outspec, mp->modname, 1))
922 >                                sop == getostream(mp->outspec, mp->modname, 1,0))
923                          for (j = 1; j < mp->nbins; j++)
924 <                                put_contrib(mp->cbin[j], fp);
924 >                                put_contrib(mp->cbin[j], sop->ofp);
925                  else
926                          for (j = 1; j < mp->nbins; j++)
927                                  put_contrib(mp->cbin[j],
928 <                                        getofile(mp->outspec, mp->modname, j));
928 >                                    getostream(mp->outspec,mp->modname,j,0)->ofp);
929                                                  /* clear for next ray tree */
930                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
931          }
# Line 820 | Line 985 | process_queue(void)
985                          if (!n || !(isalpha(*cp) | (*cp == '_')))
986                                  error(USER, "bad modifier name from rtrace");
987                                          /* get modifier name */
988 <                        while (n > 0 && *cp != '\t') {
988 >                        while (n > 1 && *cp != '\t') {
989 >                                if (mnp - modname >= sizeof(modname)-2)
990 >                                        error(INTERNAL, "modifier name too long");
991                                  *mnp++ = *cp++; n--;
992                          }
993                          *mnp = '\0';
# Line 834 | Line 1001 | process_queue(void)
1001                  }
1002                  done_contrib();         /* sum up contributions & output */
1003                  lastdone = rtp->raynum;
1004 <                free(rtp->buf);         /* free up buffer space */
1004 >                if (rtp->buf != NULL)   /* free up buffer space */
1005 >                        free(rtp->buf);
1006                  rt_unproc = rtp->next;
1007                  free(rtp);              /* done with this ray tree */
1008          }
# Line 882 | Line 1050 | wait_rproc(void)
1050                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1051                                          rt->bsiz = treebufsiz;
1052                                  else
1053 <                                        rt->bsiz = treebufsiz += BUFSIZ;
1053 >                                        treebufsiz = rt->bsiz += BUFSIZ;
1054                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1055                          }
1056                          if (rt->buf == NULL)
# Line 922 | Line 1090 | trace_contribs(FILE *fin)
1090          int             iblen;
1091          struct rtproc   *rtp;
1092                                                  /* loop over input */
1093 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1094 <                if (lastray+1 < lastray) {      /* counter rollover? */
1093 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1094 >                if (!iblen ||                   /* need reset? */
1095 >                                queue_length() > 10*nrtprocs() ||
1096 >                                lastray+1 < lastray) {
1097                          while (wait_rproc() != NULL)
1098                                  process_queue();
1099 <                        lastdone = lastray = 0;
1099 >                        if (lastray+1 < lastray)
1100 >                                lastdone = lastray = 0;
1101                  }
1102                  rtp = get_rproc();              /* get avail. rtrace process */
1103 <                rtp->raynum = ++lastray;        /* assign ray to it */
1104 <                writebuf(rtp->pd.w, inpbuf, iblen);
1105 <                if (raysleft && !--raysleft)
1106 <                        break;
1103 >                rtp->raynum = ++lastray;        /* assign ray */
1104 >                if (iblen) {                    /* trace ray if valid */
1105 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1106 >                } else {                        /* else bypass dummy ray */
1107 >                        queue_raytree(rtp);     /* empty tree */
1108 >                        if ((yres <= 0) | (waitflush > 1))
1109 >                                waitflush = 1;  /* flush after this */
1110 >                }
1111                  process_queue();                /* catch up with results */
1112 +                if (raysleft && !--raysleft)
1113 +                        break;                  /* preemptive EOI */
1114          }
1115          while (wait_rproc() != NULL)            /* process outstanding rays */
1116                  process_queue();
# Line 946 | Line 1123 | trace_contribs(FILE *fin)
1123   static int
1124   myseeko(const LUENT *e, void *p)
1125   {
1126 <        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
1126 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
1127 >        off_t           nbytes = *(off_t *)p;
1128 >        
1129 >        if (sop->reclen > 1)
1130 >                nbytes = nbytes * sop->reclen;
1131 >        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1132                  sprintf(errmsg, "seek error on file '%s'", e->key);
1133                  error(SYSTEM, errmsg);
1134          }
# Line 956 | Line 1138 | myseeko(const LUENT *e, void *p)
1138   void
1139   recover_output(FILE *fin)
1140   {
1141 <        off_t   lastout = -1;
1142 <        int     outvsiz;
1143 <        char    *outvfmt;
1144 <        int     i, j;
1145 <        MODCONT *mp;
1146 <        int     ofl;
1147 <        char    oname[1024];
1148 <        LUENT   *ment, *oent;
1149 <        FILE    *fp;
1150 <        off_t   nvals;
1141 >        off_t           lastout = -1;
1142 >        int             outvsiz, recsiz;
1143 >        char            *outvfmt;
1144 >        int             i, j;
1145 >        MODCONT         *mp;
1146 >        int             ofl;
1147 >        char            oname[1024];
1148 >        LUENT           *ment, *oent;
1149 >        STREAMOUT       sout;
1150 >        off_t           nvals;
1151 >        int             xr, yr;
1152  
1153          switch (outfmt) {
1154          case 'a':
# Line 993 | Line 1176 | recover_output(FILE *fin)
1176                  mp = (MODCONT *)ment->data;
1177                  if (mp->outspec == NULL)
1178                          error(USER, "cannot recover from stdout");
1179 +                if (mp->outspec[0] == '!')
1180 +                        error(USER, "cannot recover from command");
1181                  for (j = 0; ; j++) {            /* check each bin's file */
1182                          ofl = ofname(oname, mp->outspec, mp->modname, j);
1183                          if (ofl < 0)
1184                                  error(USER, "bad output file specification");
1185                          oent = lu_find(&ofiletab, oname);
1186 <                        if (oent->data != NULL) { /* saw this one already */
1186 >                        if (oent->data != NULL) {
1187 >                                sout = *(STREAMOUT *)oent->data;
1188 >                        } else {
1189 >                                sout.reclen = CNT_UNKNOWN;
1190 >                                sout.ofp = NULL;
1191 >                        }
1192 >                        if (sout.ofp != NULL) { /* already open? */
1193                                  if (ofl & OF_BIN)
1194                                          continue;
1195                                  break;
1196                          }
1006                        if (oname[0] == '!')
1007                                error(USER, "cannot recover from command");
1197                                                  /* open output */
1198 <                        fp = fopen(oname, "rb+");
1199 <                        if (fp == NULL)
1200 <                                break;          /* must be end of modifier */
1201 <                        nvals = lseek(fileno(fp), 0, SEEK_END);
1198 >                        sout.ofp = fopen(oname, "rb+");
1199 >                        if (sout.ofp == NULL) {
1200 >                                if (j)
1201 >                                        break;  /* assume end of modifier */
1202 >                                sprintf(errmsg, "missing recover file '%s'",
1203 >                                                oname);
1204 >                                error(USER, errmsg);
1205 >                        }
1206 >                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1207                          if (nvals <= 0) {
1208                                  lastout = 0;    /* empty output, quit here */
1209 <                                fclose(fp);
1209 >                                fclose(sout.ofp);
1210                                  break;
1211                          }
1212 <                        lseek(fileno(fp), 0, SEEK_SET);
1213 <                        if (header) {
1020 <                                int     xr, yr;
1021 <                                if (checkheader(fp, outvfmt, NULL) != 1) {
1212 >                        if (sout.reclen == CNT_UNKNOWN) {
1213 >                                if (!(ofl & OF_BIN)) {
1214                                          sprintf(errmsg,
1215 <                                                "format mismatch for '%s'",
1215 >                                                "need -bn to recover file '%s'",
1216                                                          oname);
1217                                          error(USER, errmsg);
1218                                  }
1219 <                                if (xres > 0 && yres > 0 &&
1220 <                                                (!fscnresolu(&xr, &yr, fp) ||
1221 <                                                        xr != xres ||
1222 <                                                        yr != yres)) {
1223 <                                        sprintf(errmsg,
1224 <                                                "resolution mismatch for '%s'",
1225 <                                                        oname);
1226 <                                        error(USER, errmsg);
1227 <                                }
1219 >                                recsiz = outvsiz;
1220 >                        } else
1221 >                                recsiz = outvsiz * sout.reclen;
1222 >
1223 >                        lseek(fileno(sout.ofp), 0, SEEK_SET);
1224 >                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1225 >                                sprintf(errmsg, "format mismatch for '%s'",
1226 >                                                oname);
1227 >                                error(USER, errmsg);
1228                          }
1229 <                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1230 <                        if (lastout < 0 || nvals < lastout)
1229 >                        if ((xres > 0) & (yres > 0) &&
1230 >                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
1231 >                                                xr != xres ||
1232 >                                                yr != yres)) {
1233 >                                sprintf(errmsg, "resolution mismatch for '%s'",
1234 >                                                oname);
1235 >                                error(USER, errmsg);
1236 >                        }
1237 >                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
1238 >                        if ((lastout < 0) | (nvals < lastout))
1239                                  lastout = nvals;
1240 <                        if (oent->key == NULL) /* new entry */
1240 >                        if (oent->key == NULL)  /* new entry */
1241                                  oent->key = strcpy((char *)
1242                                                  malloc(strlen(oname)+1), oname);
1243 <                        oent->data = (char *)fp;
1243 >                        if (oent->data == NULL)
1244 >                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1245 >                        *(STREAMOUT *)oent->data = sout;
1246                          if (!(ofl & OF_BIN))
1247                                  break;          /* no bin separation */
1248                  }
1249                  if (!lastout) {                 /* empty output */
1250 <                        error(WARNING, "no data to recover");
1250 >                        error(WARNING, "no previous data to recover");
1251                          lu_done(&ofiletab);     /* reclose all outputs */
1252                          return;
1253                  }
1254 <                if (j > mp->nbins) {            /* preallocate modifier bins */
1255 <                        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 <                }
1254 >                if (j > mp->nbins)              /* reallocate modifier bins */
1255 >                        ment->data = (char *)(mp = growmodifier(mp, j));
1256          }
1257          if (lastout < 0) {
1258 <                error(WARNING, "no existing output to recover");
1258 >                error(WARNING, "no output files to recover");
1259                  return;
1260          }
1261 +        if (raysleft && lastout >= raysleft) {
1262 +                error(WARNING, "output appears to be complete");
1263 +                /* XXX should read & discard input? */
1264 +                quit(0);
1265 +        }
1266                                                  /* seek on all files */
1267          nvals = lastout * outvsiz;
1268          lu_doall(&ofiletab, myseeko, &nvals);
1269 <                                                /* discard input */
1269 >                                                /* skip repeated input */
1270          for (nvals = 0; nvals < lastout; nvals++)
1271 <                if (getinp(oname, fin) <= 0)
1271 >                if (getinp(oname, fin) < 0)
1272                          error(USER, "unexpected EOF on input");
1273          lastray = lastdone = (unsigned long)lastout;
1274          if (raysleft)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines