ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rtcontrib.c
(Generate patch)

Comparing ray/src/util/rtcontrib.c (file contents):
Revision 1.11 by greg, Thu Jun 2 18:51:46 2005 UTC vs.
Revision 1.38 by greg, Tue Oct 11 23:55:37 2005 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include  "lookup.h"
17   #include  "calcomp.h"
18  
19 + #ifndef MAXMODLIST
20   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
21 + #endif
22  
23   int     treebufsiz = BUFSIZ;            /* current tree buffer size */
24  
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 < FILE *getofile(const char *ospec, const char *mname, int bn);
76 > LUTAB   ofiletab = LU_SINIT(free,closestream);  /* output file table */
77  
78 + #define OF_MODIFIER     01
79 + #define OF_BIN          02
80 +
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
88   * rtrace child process.  Input is passed unchanged from stdin,
# Line 67 | Line 99 | struct rtproc {
99   };                              /* rtrace process buffer */
100  
101                                          /* rtrace command and defaults */
102 < char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
103 <                                "-ab", "1", "-ad", "128", "-lr", "-10", };
104 < int  rtargc = 11;
102 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
103 >                                "-dj", ".5", "-dr", "3",
104 >                                "-ab", "1", "-ad", "128", };
105 > int  rtargc = 9;
106                                          /* overriding rtrace options */
107   char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
108                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
# Line 90 | Line 123 | int            inpfmt = 'a';           /* input format */
123   int             outfmt = 'a';           /* output format */
124  
125   int             header = 1;             /* output header? */
126 + int             force_open = 0;         /* truncate existing output? */
127   int             xres = 0;               /* horiz. output resolution */
128   int             yres = 0;               /* vert. output resolution */
129  
130 < long            raysleft;               /* number of rays left to trace */
130 > unsigned long   raysleft;               /* number of rays left to trace */
131   long            waitflush;              /* how long until next flush */
132  
133   unsigned long   lastray = 0;            /* last ray number sent */
# Line 104 | Line 138 | int            using_stdout = 0;       /* are we using stdout? */
138   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
139   int             nmods = 0;              /* number of modifiers */
140  
141 < MODCONT *addmodifier(char *modn, char *outf, char *binv);
141 > #define queue_length()          (lastray - lastdone)
142  
143 + MODCONT *growmodifier(MODCONT *mp, int nb);
144 + MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt);
145 + void addmodfile(char *fname, char *outf, char *binv, int bincnt);
146 +
147   void init(int np);
148   int done_rprocs(struct rtproc *rtp);
149 < void trace_contribs(FILE *fp);
149 > void recover_output(FILE *fin);
150 > void trace_contribs(FILE *fin);
151   struct rtproc *wait_rproc(void);
152   struct rtproc *get_rproc(void);
153   void queue_raytree(struct rtproc *rtp);
154   void process_queue(void);
155  
156 < void putcontrib(const DCOLOR cnt, FILE *fout);
156 > void put_contrib(const DCOLOR cnt, FILE *fout);
157   void add_contrib(const char *modn);
158   void done_contrib(void);
159  
160 + /* return number of open rtrace processes */
161 + static int
162 + nrtprocs(void)
163 + {
164 +        int     nrtp = 0;
165 +        struct rtproc   *rtp;
166 +
167 +        for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
168 +                nrtp += rtp->pd.running;
169 +        return(nrtp);
170 + }
171 +
172   /* set input/output format */
173   static void
174   setformat(const char *fmt)
# Line 158 | Line 209 | int
209   main(int argc, char *argv[])
210   {
211          int     nprocs = 1;
212 +        int     recover = 0;
213          char    *curout = NULL;
214          char    *binval = NULL;
215 +        int     bincnt = 0;
216          char    fmt[8];
217          int     i, j;
218 +                                /* need at least one argument */
219 +        if (argc < 2) {
220 +                fprintf(stderr,
221 + "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
222 +                        argv[0]);
223 +                exit(1);
224 +        }
225                                  /* global program name */
226          gargv = argv;
227                                  /* initialize calcomp routines */
# Line 180 | Line 240 | main(int argc, char *argv[])
240                  }
241                  if (argv[i][0] == '-')
242                          switch (argv[i][1]) {
243 +                        case 'r':               /* recover output */
244 +                                if (argv[i][2]) break;
245 +                                recover++;
246 +                                continue;
247                          case 'n':               /* number of processes */
248 <                                if (argv[i][2] || i >= argc-1) break;
248 >                                if (argv[i][2] || i >= argc-2) break;
249                                  nprocs = atoi(argv[++i]);
250                                  if (nprocs <= 0)
251                                          error(USER, "illegal number of processes");
# Line 203 | Line 267 | main(int argc, char *argv[])
267                                          continue;
268                                  }
269                                  break;
270 <                        case 'f':               /* file or i/o format */
270 >                        case 'f':               /* file or force or format */
271                                  if (!argv[i][2]) {
272                                          char    *fpath;
273 <                                        if (i >= argc-1) break;
273 >                                        if (i >= argc-2) break;
274                                          fpath = getpath(argv[++i],
275                                                          getrlibpath(), R_OK);
276                                          if (fpath == NULL) {
# Line 218 | Line 282 | main(int argc, char *argv[])
282                                          fcompile(fpath);
283                                          continue;
284                                  }
285 +                                if (argv[i][2] == 'o') {
286 +                                        force_open++;
287 +                                        continue;
288 +                                }
289                                  setformat(argv[i]+2);
290                                  continue;
291                          case 'e':               /* expression */
292 <                                if (argv[i][2] || i >= argc-1) break;
292 >                                if (argv[i][2] || i >= argc-2) break;
293                                  scompile(argv[++i], NULL, 0);
294                                  continue;
295                          case 'o':               /* output file spec. */
296 <                                if (argv[i][2] || i >= argc-1) break;
296 >                                if (argv[i][2] || i >= argc-2) break;
297                                  curout = argv[++i];
298                                  continue;
299                          case 'x':               /* horiz. output resolution */
300 <                                if (argv[i][2] || i >= argc-1) break;
300 >                                if (argv[i][2] || i >= argc-2) break;
301                                  xres = atoi(argv[++i]);
302                                  continue;
303                          case 'y':               /* vert. output resolution */
304 <                                if (argv[i][2] || i >= argc-1) break;
304 >                                if (argv[i][2] || i >= argc-2) break;
305                                  yres = atoi(argv[++i]);
306                                  continue;
307 <                        case 'b':               /* bin expression */
308 <                                if (argv[i][2] || i >= argc-1) break;
307 >                        case 'b':               /* bin expression/count */
308 >                                if (i >= argc-2) break;
309 >                                if (argv[i][2] == 'n') {
310 >                                        bincnt = atoi(argv[++i]);
311 >                                        continue;
312 >                                }
313 >                                if (argv[i][2]) break;
314                                  binval = argv[++i];
315                                  continue;
316                          case 'm':               /* modifier name */
317 <                                if (argv[i][2] || i >= argc-1) break;
317 >                                if (argv[i][2] || i >= argc-2) break;
318                                  rtargv[rtargc++] = "-ti";
319                                  rtargv[rtargc++] = argv[++i];
320 <                                addmodifier(argv[i], curout, binval);
320 >                                addmodifier(argv[i], curout, binval, bincnt);
321                                  continue;
322 +                        case 'M':               /* modifier file */
323 +                                if (argv[i][2] || i >= argc-2) break;
324 +                                rtargv[rtargc++] = "-tI";
325 +                                rtargv[rtargc++] = argv[++i];
326 +                                addmodfile(argv[i], curout, binval, bincnt);
327 +                                continue;
328                          }
329                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
330          }
# Line 294 | Line 373 | main(int argc, char *argv[])
373                  error(USER, "missing octree argument");
374          rtargv[rtargc++] = octree = argv[i];
375          rtargv[rtargc] = NULL;
376 <                                /* start rtrace & compute contributions */
376 >                                /* start rtrace */
377          init(nprocs);
378 <        trace_contribs(stdin);
378 >        if (recover)            /* perform recovery if requested */
379 >                recover_output(stdin);
380 >        trace_contribs(stdin);  /* compute contributions */
381          quit(0);
382   }
383  
384 + #ifndef SIGALRM
385 + #define SIGALRM SIGTERM
386 + #endif
387   /* kill persistent rtrace process */
388   static void
389   killpersist(void)
390   {
391          FILE    *fp = fopen(persistfn, "r");
392 <        int     pid;
392 >        RT_PID  pid;
393  
394          if (fp == NULL)
395                  return;
# Line 401 | Line 485 | init(int np)
485          rtp->next = NULL;               /* terminate list */
486          if (yres > 0) {
487                  if (xres > 0)
488 <                        raysleft = xres*yres;
488 >                        raysleft = (unsigned long)xres*yres;
489                  else
490                          raysleft = yres;
491          } else
# Line 409 | Line 493 | init(int np)
493          waitflush = xres;
494   }
495  
496 + /* grow modifier to accommodate more bins */
497 + MODCONT *
498 + growmodifier(MODCONT *mp, int nb)
499 + {
500 +        if (nb <= mp->nbins)
501 +                return mp;
502 +        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
503 +        if (mp == NULL)
504 +                error(SYSTEM, "out of memory in growmodifier");
505 +        memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
506 +        mp->nbins = nb;
507 +        return mp;
508 + }
509 +
510   /* add modifier to our list to track */
511   MODCONT *
512 < addmodifier(char *modn, char *outf, char *binv)
512 > addmodifier(char *modn, char *outf, char *binv, int bincnt)
513   {
514          LUENT   *lep = lu_find(&modconttab, modn);
515          MODCONT *mp;
516 +        int     i;
517          
518          if (lep->data != NULL) {
519                  sprintf(errmsg, "duplicate modifier '%s'", modn);
# Line 427 | Line 526 | addmodifier(char *modn, char *outf, char *binv)
526          mp = (MODCONT *)malloc(sizeof(MODCONT));
527          if (mp == NULL)
528                  error(SYSTEM, "out of memory in addmodifier");
430        lep->data = (char *)mp;
529          mp->outspec = outf;             /* XXX assumes static string */
530          mp->modname = modn;             /* XXX assumes static string */
531          if (binv != NULL)
# Line 436 | Line 534 | addmodifier(char *modn, char *outf, char *binv)
534                  mp->binv = eparse("0");
535          mp->nbins = 1;
536          setcolor(mp->cbin[0], 0., 0., 0.);
537 +        if (mp->binv->type == NUM)      /* assume one bin if constant */
538 +                bincnt = 1;
539 +        else if (bincnt > 1)
540 +                mp = growmodifier(mp, bincnt);
541 +        lep->data = (char *)mp;
542 +                                        /* allocate output streams */
543 +        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; )
544 +                getostream(mp->outspec, mp->modname, i, 1);
545          return mp;
546   }
547  
548 + /* add modifiers from a file list */
549 + void
550 + addmodfile(char *fname, char *outf, char *binv, int bincnt)
551 + {
552 +        char    *mname[MAXMODLIST];
553 +        int     i;
554 +                                        /* find the file & store strings */
555 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
556 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
557 +                error(SYSTEM, errmsg);
558 +        }
559 +        for (i = 0; mname[i]; i++)      /* add each one */
560 +                addmodifier(mname[i], outf, binv, bincnt);
561 + }
562 +
563   /* put string to stderr */
564   void
565   eputs(char  *s)
# Line 454 | Line 575 | eputs(char  *s)
575          midline = s[strlen(s)-1] != '\n';
576   }
577  
578 + /* construct output file name and return flags whether modifier/bin present */
579 + int
580 + ofname(char *oname, const char *ospec, const char *mname, int bn)
581 + {
582 +        const char      *mnp = NULL;
583 +        const char      *bnp = NULL;
584 +        const char      *cp;
585 +        
586 +        if (ospec == NULL)
587 +                return -1;
588 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
589 +                if (*cp == '%') {
590 +                        do
591 +                                ++cp;
592 +                        while (isdigit(*cp));
593 +                        switch (*cp) {
594 +                        case '%':
595 +                                break;
596 +                        case 's':
597 +                                if (mnp != NULL)
598 +                                        return -1;
599 +                                mnp = cp;
600 +                                break;
601 +                        case 'd':
602 +                                if (bnp != NULL)
603 +                                        return -1;
604 +                                bnp = cp;
605 +                                break;
606 +                        default:
607 +                                return -1;
608 +                        }
609 +                }
610 +        if (mnp != NULL) {                      /* create file name */
611 +                if (bnp != NULL) {
612 +                        if (bnp > mnp)
613 +                                sprintf(oname, ospec, mname, bn);
614 +                        else
615 +                                sprintf(oname, ospec, bn, mname);
616 +                        return OF_MODIFIER|OF_BIN;
617 +                }
618 +                sprintf(oname, ospec, mname);
619 +                return OF_MODIFIER;
620 +        }
621 +        if (bnp != NULL) {
622 +                sprintf(oname, ospec, bn);
623 +                return OF_BIN;
624 +        }
625 +        strcpy(oname, ospec);
626 +        return 0;
627 + }
628 +
629   /* write header to the given output stream */
630   void
631 < printheader(FILE *fout)
631 > printheader(FILE *fout, const char *info)
632   {
633          extern char     VersionID[];
634          FILE            *fin = fopen(octree, "r");
# Line 468 | Line 640 | printheader(FILE *fout)
640          printargs(gargc-1, gargv, fout);        /* add our command */
641          fprintf(fout, "SOFTWARE= %s\n", VersionID);
642          fputnow(fout);
643 +        if (info != NULL)                       /* add extra info if given */
644 +                fputs(info, fout);
645          switch (outfmt) {                       /* add output format */
646          case 'a':
647                  fputformat("ascii", fout);
# Line 483 | Line 657 | printheader(FILE *fout)
657                  break;
658          }
659          fputc('\n', fout);
660 + }
661 +
662 + /* write resolution string to given output stream */
663 + void
664 + printresolu(FILE *fout)
665 + {
666          if (xres > 0) {
667                  if (yres > 0)                   /* resolution string */
668                          fprtresolu(xres, yres, fout);
# Line 490 | Line 670 | printheader(FILE *fout)
670          }
671   }
672  
673 < /* Get output file pointer (open and write header if new) */
674 < FILE *
675 < getofile(const char *ospec, const char *mname, int bn)
673 > /* Get output stream pointer (open and write header if new and noopen==0) */
674 > STREAMOUT *
675 > getostream(const char *ospec, const char *mname, int bn, int noopen)
676   {
677 <        const char      *mnp = NULL;
678 <        const char      *bnp = NULL;
679 <        const char      *cp;
680 <        char            ofname[1024];
681 <        LUENT           *lep;
677 >        static STREAMOUT        stdos;
678 >        int                     ofl;
679 >        char                    oname[1024];
680 >        LUENT                   *lep;
681 >        STREAMOUT               *sop;
682          
683          if (ospec == NULL) {                    /* use stdout? */
684 <                if (!using_stdout) {
684 >                if (!noopen && !using_stdout) {
685 >                        stdos.reclen = 0;
686                          if (outfmt != 'a')
687                                  SET_FILE_BINARY(stdout);
688                          if (header)
689 <                                printheader(stdout);
689 >                                printheader(stdout, NULL);
690 >                        printresolu(stdout);
691 >                        using_stdout = 1;
692                  }
693 <                using_stdout = 1;
694 <                return stdout;
693 >                stdos.ofp = stdout;
694 >                stdos.reclen += noopen;
695 >                return &stdos;
696          }
697 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
698 <                if (*cp == '%') {
699 <                        do
700 <                                ++cp;
701 <                        while (isdigit(*cp));
702 <                        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 */
697 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
698 >        if (ofl < 0) {
699 >                sprintf(errmsg, "bad output format '%s'", ospec);
700 >                error(USER, errmsg);
701 >        }
702 >        lep = lu_find(&ofiletab, oname);        /* look it up */
703          if (lep->key == NULL)                   /* new entry */
704 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
705 <        if (lep->data == NULL) {                /* open output file */
706 <                FILE            *fp;
707 <                int             i;
708 <                if (ofname[0] == '!')           /* output to command */
709 <                        fp = popen(ofname+1, "w");
710 <                else
711 <                        fp = fopen(ofname, "w");
712 <                if (fp == NULL) {
713 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
704 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
705 >        sop = (STREAMOUT *)lep->data;
706 >        if (sop == NULL) {                      /* allocate stream */
707 >                sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
708 >                if (sop == NULL)
709 >                        error(SYSTEM, "out of memory in getostream");
710 >                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
711 >                sop->ofp = NULL;                /* open iff noopen==0 */
712 >                lep->data = (char *)sop;
713 >        }
714 >        if (!noopen && sop->ofp == NULL) {      /* open output stream */
715 >                long            i;
716 >                if (oname[0] == '!')            /* output to command */
717 >                        sop->ofp = popen(oname+1, "w");
718 >                else if (!force_open && access(oname, F_OK) == 0)
719 >                        errno = EEXIST;         /* file exists */
720 >                else                            /* else open it */
721 >                        sop->ofp = fopen(oname, "w");
722 >                if (sop->ofp == NULL) {
723 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
724                          error(SYSTEM, errmsg);
725                  }
726                  if (outfmt != 'a')
727 <                        SET_FILE_BINARY(fp);
728 <                if (header)
729 <                        printheader(fp);
727 >                        SET_FILE_BINARY(sop->ofp);
728 >                if (header) {
729 >                        char    info[512];
730 >                        char    *cp = info;
731 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
732 >                                sprintf(cp, "MODIFIER=%s\n", mname);
733 >                                while (*cp) ++cp;
734 >                        }
735 >                        if (ofl & OF_BIN) {
736 >                                sprintf(cp, "BIN=%d\n", bn);
737 >                                while (*cp) ++cp;
738 >                        }
739 >                        *cp = '\0';
740 >                        printheader(sop->ofp, info);
741 >                }
742 >                printresolu(sop->ofp);
743                                                  /* play catch-up */
744 <                for (i = 0; i < lastdone; i++) {
744 >                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
745 >                                                                i--; ) {
746                          static const DCOLOR     nocontrib = BLKCOLOR;
747 <                        putcontrib(nocontrib, fp);
747 >                        put_contrib(nocontrib, sop->ofp);
748                          if (outfmt == 'a')
749 <                                putc('\n', fp);
749 >                                putc('\n', sop->ofp);
750                  }
751                  if (xres > 0)
752 <                        fflush(fp);
574 <                lep->data = (char *)fp;
752 >                        fflush(sop->ofp);
753          }
754 <        return (FILE *)lep->data;               /* return open file pointer */
755 < badspec:
756 <        sprintf(errmsg, "bad output format '%s'", ospec);
579 <        error(USER, errmsg);
580 <        return NULL;            /* pro forma return */
754 >        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
755 >                sop->reclen += noopen;
756 >        return sop;                             /* return open stream */
757   }
758  
759   /* read input ray into buffer */
760   int
761   getinp(char *buf, FILE *fp)
762   {
763 +        double  dv[3];
764 +        float   fv[3];
765          char    *cp;
766          int     i;
767  
# Line 592 | Line 770 | getinp(char *buf, FILE *fp)
770                  cp = buf;               /* make sure we get 6 floats */
771                  for (i = 0; i < 6; i++) {
772                          if (fgetword(cp, buf+127-cp, fp) == NULL)
773 <                                return 0;
773 >                                return -1;
774 >                        if (i >= 3)
775 >                                dv[i-3] = atof(cp);
776                          if ((cp = fskip(cp)) == NULL || *cp)
777 <                                return 0;
777 >                                return -1;
778                          *cp++ = ' ';
779                  }
780                  getc(fp);               /* get/put eol */
781                  *cp-- = '\0'; *cp = '\n';
782 +                if (DOT(dv,dv) <= FTINY*FTINY)
783 +                        return 0;       /* dummy ray */
784                  return strlen(buf);
785          case 'f':
786                  if (fread(buf, sizeof(float), 6, fp) < 6)
787 <                        return 0;
787 >                        return -1;
788 >                memcpy(fv, buf+3*sizeof(float), 3*sizeof(float));
789 >                if (DOT(fv,fv) <= FTINY*FTINY)
790 >                        return 0;       /* dummy ray */
791                  return sizeof(float)*6;
792          case 'd':
793                  if (fread(buf, sizeof(double), 6, fp) < 6)
794 <                        return 0;
794 >                        return -1;
795 >                memcpy(dv, buf+3*sizeof(double), 3*sizeof(double));
796 >                if (DOT(dv,dv) <= FTINY*FTINY)
797 >                        return 0;       /* dummy ray */
798                  return sizeof(double)*6;
799          }
800          error(INTERNAL, "botched input format");
801 <        return 0;       /* pro forma return */
801 >        return -1;      /* pro forma return */
802   }
803  
804   static float    rparams[9];             /* traced ray parameters */
# Line 640 | Line 828 | add_contrib(const char *modn)
828          bn = (int)(evalue(mp->binv) + .5);
829          if (bn <= 0)
830                  bn = 0;
831 <        else if (bn > mp->nbins) {      /* new bin */
832 <                mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
645 <                                                bn*sizeof(DCOLOR));
646 <                if (mp == NULL)
647 <                        error(SYSTEM, "out of memory in add_contrib");
648 <                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
649 <                mp->nbins = bn+1;
650 <                le->data = (char *)mp;
651 <        }
831 >        else if (bn >= mp->nbins)       /* new bin */
832 >                le->data = (char *)(mp = growmodifier(mp, bn+1));
833          addcolor(mp->cbin[bn], rparams);
834   }
835  
# Line 656 | Line 837 | add_contrib(const char *modn)
837   static int
838   puteol(const LUENT *e, void *p)
839   {
840 <        FILE    *fp = (FILE *)e->data;
840 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
841  
842          if (outfmt == 'a')
843 <                putc('\n', fp);
843 >                putc('\n', sop->ofp);
844          if (!waitflush)
845 <                fflush(fp);
846 <        if (ferror(fp)) {
845 >                fflush(sop->ofp);
846 >        if (ferror(sop->ofp)) {
847                  sprintf(errmsg, "write error on file '%s'", e->key);
848                  error(SYSTEM, errmsg);
849          }
# Line 671 | Line 852 | puteol(const LUENT *e, void *p)
852  
853   /* put out ray contribution to file */
854   void
855 < putcontrib(const DCOLOR cnt, FILE *fout)
855 > put_contrib(const DCOLOR cnt, FILE *fout)
856   {
857          float   fv[3];
858          COLR    cv;
# Line 702 | Line 883 | putcontrib(const DCOLOR cnt, FILE *fout)
883   void
884   done_contrib(void)
885   {
886 <        int     i, j;
887 <        MODCONT *mp;
886 >        int             i, j;
887 >        MODCONT         *mp;
888 >        STREAMOUT       *sop;
889                                                  /* output modifiers in order */
890          for (i = 0; i < nmods; i++) {
891                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
892 <                for (j = 0; j < mp->nbins; j++)
893 <                        putcontrib(mp->cbin[j],
894 <                                        getofile(mp->outspec, mp->modname, j));
892 >                sop = getostream(mp->outspec, mp->modname, 0,0);
893 >                put_contrib(mp->cbin[0], sop->ofp);
894 >                if (mp->nbins > 3 &&            /* minor optimization */
895 >                                sop == getostream(mp->outspec, mp->modname, 1,0))
896 >                        for (j = 1; j < mp->nbins; j++)
897 >                                put_contrib(mp->cbin[j], sop->ofp);
898 >                else
899 >                        for (j = 1; j < mp->nbins; j++)
900 >                                put_contrib(mp->cbin[j],
901 >                                    getostream(mp->outspec,mp->modname,j,0)->ofp);
902                                                  /* clear for next ray tree */
903                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
904          }
# Line 783 | Line 972 | process_queue(void)
972                  }
973                  done_contrib();         /* sum up contributions & output */
974                  lastdone = rtp->raynum;
975 <                free(rtp->buf);         /* free up buffer space */
975 >                if (rtp->buf != NULL)   /* free up buffer space */
976 >                        free(rtp->buf);
977                  rt_unproc = rtp->next;
978                  free(rtp);              /* done with this ray tree */
979          }
# Line 831 | Line 1021 | wait_rproc(void)
1021                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1022                                          rt->bsiz = treebufsiz;
1023                                  else
1024 <                                        rt->bsiz = treebufsiz += BUFSIZ;
1024 >                                        treebufsiz = rt->bsiz += BUFSIZ;
1025                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1026                          }
1027                          if (rt->buf == NULL)
# Line 871 | Line 1061 | trace_contribs(FILE *fin)
1061          int             iblen;
1062          struct rtproc   *rtp;
1063                                                  /* loop over input */
1064 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1065 <                if (lastray+1 < lastray) {      /* counter rollover? */
1064 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1065 >                if (!iblen ||                   /* need reset? */
1066 >                                queue_length() > 10*nrtprocs() ||
1067 >                                lastray+1 < lastray) {
1068                          while (wait_rproc() != NULL)
1069                                  process_queue();
1070 <                        lastdone = lastray = 0;
1070 >                        if (lastray+1 < lastray)
1071 >                                lastdone = lastray = 0;
1072                  }
1073                  rtp = get_rproc();              /* get avail. rtrace process */
1074 <                rtp->raynum = ++lastray;        /* assign ray to it */
1075 <                writebuf(rtp->pd.w, inpbuf, iblen);
1076 <                if (!--raysleft)
1077 <                        break;
1074 >                rtp->raynum = ++lastray;        /* assign ray */
1075 >                if (iblen) {                    /* trace ray if valid */
1076 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1077 >                } else {                        /* else bypass dummy ray */
1078 >                        queue_raytree(rtp);     /* empty tree */
1079 >                        if ((yres <= 0) | (waitflush > 1))
1080 >                                waitflush = 1;  /* flush after this */
1081 >                }
1082                  process_queue();                /* catch up with results */
1083 +                if (raysleft && !--raysleft)
1084 +                        break;                  /* preemptive EOI */
1085          }
1086          while (wait_rproc() != NULL)            /* process outstanding rays */
1087                  process_queue();
1088 <        if (raysleft > 0)
1088 >        if (raysleft)
1089                  error(USER, "unexpected EOF on input");
1090 +        lu_done(&ofiletab);                     /* close output files */
1091 + }
1092 +
1093 + /* seek on the given output file */
1094 + static int
1095 + myseeko(const LUENT *e, void *p)
1096 + {
1097 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1098 +        off_t           nbytes = *(off_t *)p;
1099 +        
1100 +        if (sop->reclen > 1)
1101 +                nbytes = nbytes * sop->reclen;
1102 +        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1103 +                sprintf(errmsg, "seek error on file '%s'", e->key);
1104 +                error(SYSTEM, errmsg);
1105 +        }
1106 + }
1107 +
1108 + /* recover output if possible */
1109 + void
1110 + recover_output(FILE *fin)
1111 + {
1112 +        off_t           lastout = -1;
1113 +        int             outvsiz, recsiz;
1114 +        char            *outvfmt;
1115 +        int             i, j;
1116 +        MODCONT         *mp;
1117 +        int             ofl;
1118 +        char            oname[1024];
1119 +        LUENT           *ment, *oent;
1120 +        STREAMOUT       sout;
1121 +        off_t           nvals;
1122 +        int             xr, yr;
1123 +
1124 +        switch (outfmt) {
1125 +        case 'a':
1126 +                error(USER, "cannot recover ASCII output");
1127 +                return;
1128 +        case 'f':
1129 +                outvsiz = sizeof(float)*3;
1130 +                outvfmt = "float";
1131 +                break;
1132 +        case 'd':
1133 +                outvsiz = sizeof(double)*3;
1134 +                outvfmt = "double";
1135 +                break;
1136 +        case 'c':
1137 +                outvsiz = sizeof(COLR);
1138 +                outvfmt = COLRFMT;
1139 +                break;
1140 +        default:
1141 +                error(INTERNAL, "botched output format");
1142 +                return;
1143 +        }
1144 +                                                /* check modifier outputs */
1145 +        for (i = 0; i < nmods; i++) {
1146 +                ment = lu_find(&modconttab,modname[i]);
1147 +                mp = (MODCONT *)ment->data;
1148 +                if (mp->outspec == NULL)
1149 +                        error(USER, "cannot recover from stdout");
1150 +                if (mp->outspec[0] == '!')
1151 +                        error(USER, "cannot recover from command");
1152 +                for (j = 0; ; j++) {            /* check each bin's file */
1153 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1154 +                        if (ofl < 0)
1155 +                                error(USER, "bad output file specification");
1156 +                        oent = lu_find(&ofiletab, oname);
1157 +                        if (oent->data != NULL) {
1158 +                                sout = *(STREAMOUT *)oent->data;
1159 +                        } else {
1160 +                                sout.reclen = CNT_UNKNOWN;
1161 +                                sout.ofp = NULL;
1162 +                        }
1163 +                        if (sout.ofp != NULL) { /* already open? */
1164 +                                if (ofl & OF_BIN)
1165 +                                        continue;
1166 +                                break;
1167 +                        }
1168 +                                                /* open output */
1169 +                        sout.ofp = fopen(oname, "rb+");
1170 +                        if (sout.ofp == NULL) {
1171 +                                if (j)
1172 +                                        break;  /* assume end of modifier */
1173 +                                sprintf(errmsg, "missing recover file '%s'",
1174 +                                                oname);
1175 +                                error(USER, errmsg);
1176 +                        }
1177 +                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1178 +                        if (nvals <= 0) {
1179 +                                lastout = 0;    /* empty output, quit here */
1180 +                                fclose(sout.ofp);
1181 +                                break;
1182 +                        }
1183 +                        if (sout.reclen == CNT_UNKNOWN) {
1184 +                                if (!(ofl & OF_BIN)) {
1185 +                                        sprintf(errmsg,
1186 +                                                "need -bn to recover file '%s'",
1187 +                                                        oname);
1188 +                                        error(USER, errmsg);
1189 +                                }
1190 +                                recsiz = outvsiz;
1191 +                        } else
1192 +                                recsiz = outvsiz * sout.reclen;
1193 +
1194 +                        lseek(fileno(sout.ofp), 0, SEEK_SET);
1195 +                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1196 +                                sprintf(errmsg, "format mismatch for '%s'",
1197 +                                                oname);
1198 +                                error(USER, errmsg);
1199 +                        }
1200 +                        if ((xres > 0) & (yres > 0) &&
1201 +                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
1202 +                                                xr != xres ||
1203 +                                                yr != yres)) {
1204 +                                sprintf(errmsg, "resolution mismatch for '%s'",
1205 +                                                oname);
1206 +                                error(USER, errmsg);
1207 +                        }
1208 +                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
1209 +                        if ((lastout < 0) | (nvals < lastout))
1210 +                                lastout = nvals;
1211 +                        if (oent->key == NULL)  /* new entry */
1212 +                                oent->key = strcpy((char *)
1213 +                                                malloc(strlen(oname)+1), oname);
1214 +                        if (oent->data == NULL)
1215 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1216 +                        *(STREAMOUT *)oent->data = sout;
1217 +                        if (!(ofl & OF_BIN))
1218 +                                break;          /* no bin separation */
1219 +                }
1220 +                if (!lastout) {                 /* empty output */
1221 +                        error(WARNING, "no previous data to recover");
1222 +                        lu_done(&ofiletab);     /* reclose all outputs */
1223 +                        return;
1224 +                }
1225 +                if (j > mp->nbins)              /* reallocate modifier bins */
1226 +                        ment->data = (char *)(mp = growmodifier(mp, j));
1227 +        }
1228 +        if (lastout < 0) {
1229 +                error(WARNING, "no output files to recover");
1230 +                return;
1231 +        }
1232 +        if (raysleft && lastout >= raysleft) {
1233 +                error(WARNING, "output appears to be complete");
1234 +                /* XXX should read & discard input? */
1235 +                quit(0);
1236 +        }
1237 +                                                /* seek on all files */
1238 +        nvals = lastout * outvsiz;
1239 +        lu_doall(&ofiletab, myseeko, &nvals);
1240 +                                                /* skip repeated input */
1241 +        for (nvals = 0; nvals < lastout; nvals++)
1242 +                if (getinp(oname, fin) < 0)
1243 +                        error(USER, "unexpected EOF on input");
1244 +        lastray = lastdone = (unsigned long)lastout;
1245 +        if (raysleft)
1246 +                raysleft -= lastray;
1247   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines