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.31 by greg, Thu Oct 6 05:49:44 2005 UTC vs.
Revision 1.47 by greg, Sat Nov 17 15:57:28 2007 UTC

# Line 27 | Line 27 | typedef double DCOLOR[3];              /* double-precision color *
27   /*
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-zero.  If outspec contains a %s in it, this will
30 > * if binv evaluates > 0.  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 #define CNT_UNKNOWN     0               /* unknown record length */
50 #define CNT_PIPE        (-1)            /* output to a pipe */
49   /*
50   * The STREAMOUT structure holds an open FILE pointer and a count of
51 < * the number of RGB triplets per record, or CNT_UNKNOWN (0) if
54 < * unknown or CNT_PIPE (-1) if writing to a command.
51 > * the number of RGB triplets per record, or 0 if unknown.
52   */
53   typedef struct {
54          FILE            *ofp;           /* output file pointer */
55 +        int             outpipe;        /* output is to a pipe */
56          int             reclen;         /* triplets/record */
57 +        int             xr, yr;         /* output resolution for picture */
58   } STREAMOUT;
59  
60   /* close output stream and free record */
# Line 63 | Line 62 | static void
62   closestream(void *p)
63   {
64          STREAMOUT       *sop = (STREAMOUT *)p;
65 <        if (sop->reclen == CNT_PIPE)
66 <                pclose(sop->ofp);
65 >        int             status;
66 >        if (sop->outpipe)
67 >                status = pclose(sop->ofp);
68          else
69 <                fclose(sop->ofp);
69 >                status = fclose(sop->ofp);
70 >        if (status)
71 >                error(SYSTEM, "error closing output stream");
72          free(p);
73   }
74  
# Line 75 | Line 77 | LUTAB  ofiletab = LU_SINIT(free,closestream);  /* output
77   #define OF_MODIFIER     01
78   #define OF_BIN          02
79  
80 < STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int bincnt);
80 > STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
81   int ofname(char *oname, const char *ospec, const char *mname, int bn);
82   void printheader(FILE *fout, const char *info);
83 < void printresolu(FILE *fout);
83 > void printresolu(FILE *fout, int xr, int yr);
84  
85   /*
86   * The rcont structure is used to manage i/o with a particular
# Line 98 | Line 100 | struct rtproc {
100                                          /* rtrace command and defaults */
101   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
102                                  "-dj", ".5", "-dr", "3",
103 <                                "-ab", "1", "-ad", "128", };
103 >                                "-ab", "1", "-ad", "350", };
104 >
105   int  rtargc = 9;
106                                          /* overriding rtrace options */
107 < char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
107 > char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
108                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
109  
110 + #define RTCOEFF         "-o~~TmWdp"     /* compute coefficients only */
111 + #define RTCONTRIB       "-o~~TmVdp"     /* compute ray contributions */
112 +
113   struct rtproc   rt0;                    /* head of rtrace process list */
114  
115   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
116  
117 < char    persistfn[] = "pfXXXXXX";       /* persist file name */
117 > #define PERSIST_NONE    0               /* no persist file */
118 > #define PERSIST_SINGLE  1               /* user set -P persist */
119 > #define PERSIST_PARALL  2               /* user set -PP persist */
120 > #define PERSIST_OURS    3               /* -PP persist belongs to us */
121 > int     persist_state = PERSIST_NONE;   /* persist file state */
122 > char    persistfn[] = "pfXXXXXX";       /* our persist file name, if set */
123  
124   int             gargc;                  /* global argc */
125   char            **gargv;                /* global argv */
# Line 120 | Line 131 | int            inpfmt = 'a';           /* input format */
131   int             outfmt = 'a';           /* output format */
132  
133   int             header = 1;             /* output header? */
134 + int             accumulate = 0;         /* accumulate ray values? */
135 + int             force_open = 0;         /* truncate existing output? */
136 + int             recover = 0;            /* recover previous output? */
137   int             xres = 0;               /* horiz. output resolution */
138   int             yres = 0;               /* vert. output resolution */
139  
# Line 142 | Line 156 | void addmodfile(char *fname, char *outf, char *binv, i
156  
157   void init(int np);
158   int done_rprocs(struct rtproc *rtp);
159 + void reload_output(void);
160   void recover_output(FILE *fin);
161   void trace_contribs(FILE *fin);
162   struct rtproc *wait_rproc(void);
# Line 204 | Line 219 | fmterr:
219   int
220   main(int argc, char *argv[])
221   {
222 +        int     contrib = 0;
223          int     nprocs = 1;
208        int     recover = 0;
224          char    *curout = NULL;
225          char    *binval = NULL;
226          int     bincnt = 0;
# Line 214 | Line 229 | main(int argc, char *argv[])
229                                  /* need at least one argument */
230          if (argc < 2) {
231                  fprintf(stderr,
232 < "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
232 > "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
233                          argv[0]);
234                  exit(1);
235          }
# Line 236 | Line 251 | main(int argc, char *argv[])
251                  }
252                  if (argv[i][0] == '-')
253                          switch (argv[i][1]) {
239                        case 'r':               /* recover output */
240                                if (argv[i][2]) break;
241                                recover++;
242                                continue;
254                          case 'n':               /* number of processes */
255                                  if (argv[i][2] || i >= argc-2) break;
256                                  nprocs = atoi(argv[++i]);
257                                  if (nprocs <= 0)
258                                          error(USER, "illegal number of processes");
259                                  continue;
260 +                        case 'V':               /* output contributions */
261 +                                switch (argv[i][2]) {
262 +                                case '\0':
263 +                                        contrib = !contrib;
264 +                                        continue;
265 +                                case '+': case '1':
266 +                                case 'T': case 't':
267 +                                case 'Y': case 'y':
268 +                                        contrib = 1;
269 +                                        continue;
270 +                                case '-': case '0':
271 +                                case 'F': case 'f':
272 +                                case 'N': case 'n':
273 +                                        contrib = 0;
274 +                                        continue;
275 +                                }
276 +                                break;
277 +                        case 'c':               /* accumulate ray values */
278 +                                switch (argv[i][2]) {
279 +                                case '\0':
280 +                                        accumulate = !accumulate;
281 +                                        continue;
282 +                                case '+': case '1':
283 +                                case 'T': case 't':
284 +                                case 'Y': case 'y':
285 +                                        accumulate = 1;
286 +                                        continue;
287 +                                case '-': case '0':
288 +                                case 'F': case 'f':
289 +                                case 'N': case 'n':
290 +                                        accumulate = 0;
291 +                                        continue;
292 +                                }
293 +                                break;
294 +                        case 'r':               /* recover output */
295 +                                if (argv[i][2]) break;
296 +                                recover = 1;
297 +                                continue;
298                          case 'h':               /* output header? */
299                                  switch (argv[i][2]) {
300                                  case '\0':
# Line 263 | Line 312 | main(int argc, char *argv[])
312                                          continue;
313                                  }
314                                  break;
315 <                        case 'f':               /* file or i/o format */
315 >                        case 'f':               /* file or force or format */
316                                  if (!argv[i][2]) {
317                                          char    *fpath;
318                                          if (i >= argc-2) break;
# Line 278 | Line 327 | main(int argc, char *argv[])
327                                          fcompile(fpath);
328                                          continue;
329                                  }
330 +                                if (argv[i][2] == 'o') {
331 +                                        force_open = 1;
332 +                                        continue;
333 +                                }
334                                  setformat(argv[i]+2);
335                                  continue;
336                          case 'e':               /* expression */
# Line 317 | Line 370 | main(int argc, char *argv[])
370                                  rtargv[rtargc++] = argv[++i];
371                                  addmodfile(argv[i], curout, binval, bincnt);
372                                  continue;
373 +                        case 'P':               /* persist file */
374 +                                if (i >= argc-2) break;
375 +                                persist_state = (argv[i][2] == 'P') ?
376 +                                                PERSIST_PARALL : PERSIST_SINGLE;
377 +                                rtargv[rtargc++] = argv[i];
378 +                                rtargv[rtargc++] = argv[++i];
379 +                                continue;
380                          }
381                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
382          }
383 +        if (accumulate)         /* no output flushing for single record */
384 +                xres = yres = 0;
385                                  /* set global argument list */
386          gargc = argc; gargv = argv;
387                                  /* add "mandatory" rtrace settings */
388          for (j = 0; myrtopts[j] != NULL; j++)
389                  rtargv[rtargc++] = myrtopts[j];
390 +        rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
391                                  /* just asking for defaults? */
392          if (!strcmp(argv[i], "-defaults")) {
393                  char    sxres[16], syres[16];
394                  char    *rtpath;
395                  printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
396 +                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
397 +                                contrib ? "contributions" : "coefficients");
398 +                printf("-c%c\t\t\t\t# %s\n", accumulate ? '+' : '-',
399 +                                accumulate ? "accumulate ray values" :
400 +                                        "one output record per ray");
401                  fflush(stdout);                 /* report OUR options */
402                  rtargv[rtargc++] = header ? "-h+" : "-h-";
403                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 340 | Line 408 | main(int argc, char *argv[])
408                  rtargv[rtargc++] = "-y";
409                  sprintf(syres, "%d", yres);
410                  rtargv[rtargc++] = syres;
343                rtargv[rtargc++] = "-oTW";
411                  rtargv[rtargc++] = "-defaults";
412                  rtargv[rtargc] = NULL;
413                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 354 | Line 421 | main(int argc, char *argv[])
421                  exit(1);
422          }
423          if (nprocs > 1) {       /* add persist file if parallel */
424 <                rtargv[rtargc++] = "-PP";
425 <                rtargv[rtargc++] = mktemp(persistfn);
424 >                if (persist_state == PERSIST_SINGLE)
425 >                        error(USER, "use -PP option for multiple processes");
426 >                if (persist_state == PERSIST_NONE) {
427 >                        rtargv[rtargc++] = "-PP";
428 >                        rtargv[rtargc++] = mktemp(persistfn);
429 >                        persist_state = PERSIST_OURS;
430 >                }
431          }
432                                  /* add format string */
433          sprintf(fmt, "-f%cf", inpfmt);
# Line 365 | Line 437 | main(int argc, char *argv[])
437                  error(USER, "missing octree argument");
438          rtargv[rtargc++] = octree = argv[i];
439          rtargv[rtargc] = NULL;
440 <                                /* start rtrace */
440 >                                /* start rtrace & recover if requested */
441          init(nprocs);
442 <        if (recover)            /* perform recovery if requested */
443 <                recover_output(stdin);
444 <        trace_contribs(stdin);  /* compute contributions */
442 >                                /* compute contributions */
443 >        trace_contribs(stdin);
444 >                                /* clean up */
445          quit(0);
446   }
447  
# Line 415 | Line 487 | quit(int status)
487   {
488          int     rtstat;
489  
490 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
490 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
491                  killpersist();
492                                          /* clean up rtrace process(es) */
493          rtstat = done_rprocs(&rt0);
# Line 483 | Line 555 | init(int np)
555          } else
556                  raysleft = 0;
557          waitflush = xres;
558 +        if (!recover)
559 +                return;
560 +                                        /* recover previous values */
561 +        if (accumulate)
562 +                reload_output();
563 +        else
564 +                recover_output(stdin);
565   }
566  
567   /* grow modifier to accommodate more bins */
# Line 505 | Line 584 | addmodifier(char *modn, char *outf, char *binv, int bi
584   {
585          LUENT   *lep = lu_find(&modconttab, modn);
586          MODCONT *mp;
587 +        int     i;
588          
589          if (lep->data != NULL) {
590                  sprintf(errmsg, "duplicate modifier '%s'", modn);
# Line 517 | Line 597 | addmodifier(char *modn, char *outf, char *binv, int bi
597          mp = (MODCONT *)malloc(sizeof(MODCONT));
598          if (mp == NULL)
599                  error(SYSTEM, "out of memory in addmodifier");
520        lep->data = (char *)mp;
600          mp->outspec = outf;             /* XXX assumes static string */
601          mp->modname = modn;             /* XXX assumes static string */
602          if (binv != NULL)
# Line 530 | Line 609 | addmodifier(char *modn, char *outf, char *binv, int bi
609                  bincnt = 1;
610          else if (bincnt > 1)
611                  mp = growmodifier(mp, bincnt);
612 <        if (bincnt > 0)                 /* allocate output stream */
613 <                getostream(mp->outspec, mp->modname, 0, bincnt);
612 >        lep->data = (char *)mp;
613 >                                        /* allocate output streams */
614 >        for (i = bincnt; i-- > 0; )
615 >                getostream(mp->outspec, mp->modname, i, 1);
616          return mp;
617   }
618  
# Line 651 | Line 732 | printheader(FILE *fout, const char *info)
732  
733   /* write resolution string to given output stream */
734   void
735 < printresolu(FILE *fout)
735 > printresolu(FILE *fout, int xr, int yr)
736   {
737 <        if (xres > 0) {
738 <                if (yres > 0)                   /* resolution string */
739 <                        fprtresolu(xres, yres, fout);
737 >        if ((xr > 0) & (yr > 0))        /* resolution string */
738 >                fprtresolu(xr, yr, fout);
739 >        if (xres > 0)                   /* global flush flag */
740                  fflush(fout);
660        }
741   }
742  
743 < /* Get output stream pointer (open and write header if new and bincnt==0) */
743 > /* Get output stream pointer (open and write header if new and noopen==0) */
744   STREAMOUT *
745 < getostream(const char *ospec, const char *mname, int bn, int bincnt)
745 > getostream(const char *ospec, const char *mname, int bn, int noopen)
746   {
747 +        static const DCOLOR     nocontrib = BLKCOLOR;
748          static STREAMOUT        stdos;
749          int                     ofl;
750          char                    oname[1024];
# Line 671 | Line 752 | getostream(const char *ospec, const char *mname, int b
752          STREAMOUT               *sop;
753          
754          if (ospec == NULL) {                    /* use stdout? */
755 <                if (!bincnt && !using_stdout) {
675 <                        stdos.reclen = 0;
755 >                if (!noopen && !using_stdout) {
756                          if (outfmt != 'a')
757                                  SET_FILE_BINARY(stdout);
758                          if (header)
759                                  printheader(stdout, NULL);
760 <                        printresolu(stdout);
760 >                        printresolu(stdout, xres, yres);
761 >                        stdos.xr = xres; stdos.yr = yres;
762                          using_stdout = 1;
763                  }
764                  stdos.ofp = stdout;
765 <                stdos.reclen += bincnt;
765 >                stdos.reclen += noopen;
766                  return &stdos;
767          }
768          ofl = ofname(oname, ospec, mname, bn);  /* get output name */
# Line 694 | Line 775 | getostream(const char *ospec, const char *mname, int b
775                  lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
776          sop = (STREAMOUT *)lep->data;
777          if (sop == NULL) {                      /* allocate stream */
697                if (ofl & OF_BIN && bincnt > 1) /* don't overcount bins */
698                        bincnt = 1;
778                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
779                  if (sop == NULL)
780                          error(SYSTEM, "out of memory in getostream");
781 <                sop->reclen = oname[0] == '!' ? CNT_PIPE : 0;
782 <                sop->ofp = NULL;                /* open iff bincnt==0 */
781 >                sop->outpipe = oname[0] == '!';
782 >                sop->reclen = 0;
783 >                sop->ofp = NULL;                /* open iff noopen==0 */
784 >                sop->xr = xres; sop->yr = yres;
785                  lep->data = (char *)sop;
786 +                if (!sop->outpipe & !force_open & !recover &&
787 +                                access(oname, F_OK) == 0) {
788 +                        errno = EEXIST;         /* file exists */
789 +                        goto openerr;
790 +                }
791          }
792 <        if (!bincnt && sop->ofp == NULL) {      /* open output stream */
793 <                int             i;
792 >        if (!noopen && sop->ofp == NULL) {      /* open output stream */
793 >                long            i;
794                  if (oname[0] == '!')            /* output to command */
795                          sop->ofp = popen(oname+1, "w");
796 <                else
796 >                else                            /* else open file */
797                          sop->ofp = fopen(oname, "w");
798 <                if (sop->ofp == NULL) {
799 <                        sprintf(errmsg, "cannot open '%s' for writing", oname);
714 <                        error(SYSTEM, errmsg);
715 <                }
798 >                if (sop->ofp == NULL)
799 >                        goto openerr;
800                  if (outfmt != 'a')
801                          SET_FILE_BINARY(sop->ofp);
802                  if (header) {
803                          char    info[512];
804                          char    *cp = info;
805 <                        if (ofl & OF_MODIFIER) {
805 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
806                                  sprintf(cp, "MODIFIER=%s\n", mname);
807                                  while (*cp) ++cp;
808                          }
# Line 729 | Line 813 | getostream(const char *ospec, const char *mname, int b
813                          *cp = '\0';
814                          printheader(sop->ofp, info);
815                  }
816 <                printresolu(sop->ofp);
816 >                if (!accumulate) {              /* global res. for -c- */
817 >                        sop->xr = xres; sop->yr = yres;
818 >                }
819 >                printresolu(sop->ofp, sop->xr, sop->yr);
820                                                  /* play catch-up */
821 <                for (i = 0; i < lastdone; i++) {
822 <                        static const DCOLOR     nocontrib = BLKCOLOR;
823 <                        put_contrib(nocontrib, sop->ofp);
821 >                for (i = accumulate ? 0 : lastdone; i--; ) {
822 >                        int     j = sop->reclen;
823 >                        if (j <= 0) j = 1;
824 >                        while (j--)
825 >                                put_contrib(nocontrib, sop->ofp);
826                          if (outfmt == 'a')
827                                  putc('\n', sop->ofp);
828                  }
829                  if (xres > 0)
830                          fflush(sop->ofp);
831          }
832 <        if (sop->reclen != CNT_PIPE)            /* add bincnt to count */
833 <                sop->reclen += bincnt;
834 <        return sop;                             /* return open stream */
832 >        sop->reclen += noopen;                  /* add to length if noopen */
833 >        return sop;                             /* return output stream */
834 > openerr:
835 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
836 >        error(SYSTEM, errmsg);
837 >        return NULL;    /* pro forma return */
838   }
839  
840   /* read input ray into buffer */
841   int
842   getinp(char *buf, FILE *fp)
843   {
844 +        double  dv[3], *dvp;
845 +        float   *fvp;
846          char    *cp;
847          int     i;
848  
# Line 757 | Line 851 | getinp(char *buf, FILE *fp)
851                  cp = buf;               /* make sure we get 6 floats */
852                  for (i = 0; i < 6; i++) {
853                          if (fgetword(cp, buf+127-cp, fp) == NULL)
854 <                                return 0;
854 >                                return -1;
855 >                        if (i >= 3)
856 >                                dv[i-3] = atof(cp);
857                          if ((cp = fskip(cp)) == NULL || *cp)
858 <                                return 0;
858 >                                return -1;
859                          *cp++ = ' ';
860                  }
861                  getc(fp);               /* get/put eol */
862                  *cp-- = '\0'; *cp = '\n';
863 +                if (DOT(dv,dv) <= FTINY*FTINY)
864 +                        return 0;       /* dummy ray */
865                  return strlen(buf);
866          case 'f':
867                  if (fread(buf, sizeof(float), 6, fp) < 6)
868 <                        return 0;
868 >                        return -1;
869 >                fvp = (float *)buf + 3;
870 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
871 >                        return 0;       /* dummy ray */
872                  return sizeof(float)*6;
873          case 'd':
874                  if (fread(buf, sizeof(double), 6, fp) < 6)
875 <                        return 0;
875 >                        return -1;
876 >                dvp = (double *)buf + 3;
877 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
878 >                        return 0;       /* dummy ray */
879                  return sizeof(double)*6;
880          }
881          error(INTERNAL, "botched input format");
882 <        return 0;       /* pro forma return */
882 >        return -1;      /* pro forma return */
883   }
884  
885   static float    rparams[9];             /* traced ray parameters */
# Line 839 | Line 943 | put_contrib(const DCOLOR cnt, FILE *fout)
943                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
944                  break;
945          case 'f':
946 <                fv[0] = cnt[0];
843 <                fv[1] = cnt[1];
844 <                fv[2] = cnt[2];
946 >                copycolor(fv, cnt);
947                  fwrite(fv, sizeof(float), 3, fout);
948                  break;
949          case 'd':
# Line 856 | Line 958 | put_contrib(const DCOLOR cnt, FILE *fout)
958          }
959   }
960  
961 < /* output ray tallies and clear for next primary */
961 > /* output ray tallies and clear for next accumulation */
962   void
963   done_contrib(void)
964   {
# Line 876 | Line 978 | done_contrib(void)
978                          for (j = 1; j < mp->nbins; j++)
979                                  put_contrib(mp->cbin[j],
980                                      getostream(mp->outspec,mp->modname,j,0)->ofp);
981 <                                                /* clear for next ray tree */
981 >                                                /* clear for next time */
982                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
983          }
984          --waitflush;                            /* terminate records */
# Line 935 | Line 1037 | process_queue(void)
1037                          if (!n || !(isalpha(*cp) | (*cp == '_')))
1038                                  error(USER, "bad modifier name from rtrace");
1039                                          /* get modifier name */
1040 <                        while (n > 0 && *cp != '\t') {
1040 >                        while (n > 1 && *cp != '\t') {
1041 >                                if (mnp - modname >= sizeof(modname)-2)
1042 >                                        error(INTERNAL, "modifier name too long");
1043                                  *mnp++ = *cp++; n--;
1044                          }
1045                          *mnp = '\0';
# Line 947 | Line 1051 | process_queue(void)
1051                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1052                          add_contrib(modname);
1053                  }
1054 <                done_contrib();         /* sum up contributions & output */
1054 >                if (!accumulate)
1055 >                        done_contrib(); /* sum up contributions & output */
1056                  lastdone = rtp->raynum;
1057 <                free(rtp->buf);         /* free up buffer space */
1057 >                if (rtp->buf != NULL)   /* free up buffer space */
1058 >                        free(rtp->buf);
1059                  rt_unproc = rtp->next;
1060                  free(rtp);              /* done with this ray tree */
1061          }
# Line 1037 | Line 1143 | trace_contribs(FILE *fin)
1143          int             iblen;
1144          struct rtproc   *rtp;
1145                                                  /* loop over input */
1146 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1147 <                if (lastray+1 < lastray ||      /* need reset? */
1148 <                                queue_length() > 5*nrtprocs()) {
1146 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1147 >                if (!iblen ||                   /* need reset? */
1148 >                                queue_length() > 10*nrtprocs() ||
1149 >                                lastray+1 < lastray) {
1150                          while (wait_rproc() != NULL)
1151                                  process_queue();
1152 <                        lastdone = lastray = 0;
1152 >                        if (lastray+1 < lastray)
1153 >                                lastdone = lastray = 0;
1154                  }
1155                  rtp = get_rproc();              /* get avail. rtrace process */
1156 <                rtp->raynum = ++lastray;        /* assign ray to it */
1157 <                writebuf(rtp->pd.w, inpbuf, iblen);
1158 <                if (raysleft && !--raysleft)
1159 <                        break;
1156 >                rtp->raynum = ++lastray;        /* assign ray */
1157 >                if (iblen) {                    /* trace ray if valid */
1158 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1159 >                } else {                        /* else bypass dummy ray */
1160 >                        queue_raytree(rtp);     /* empty tree */
1161 >                        if ((yres <= 0) | (waitflush > 1))
1162 >                                waitflush = 1;  /* flush after this */
1163 >                }
1164                  process_queue();                /* catch up with results */
1165 +                if (raysleft && !--raysleft)
1166 +                        break;                  /* preemptive EOI */
1167          }
1168          while (wait_rproc() != NULL)            /* process outstanding rays */
1169                  process_queue();
1170 +        if (accumulate)
1171 +                done_contrib();                 /* output tallies */
1172          if (raysleft)
1173                  error(USER, "unexpected EOF on input");
1174          lu_done(&ofiletab);                     /* close output files */
1175   }
1176  
1177 + /* get ray contribution from previous file */
1178 + static int
1179 + get_contrib(DCOLOR cnt, FILE *finp)
1180 + {
1181 +        COLOR   fv;
1182 +        COLR    cv;
1183 +
1184 +        switch (outfmt) {
1185 +        case 'a':
1186 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1187 +        case 'f':
1188 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1189 +                        return(0);
1190 +                copycolor(cnt, fv);
1191 +                return(1);
1192 +        case 'd':
1193 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1194 +        case 'c':
1195 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1196 +                        return(0);
1197 +                colr_color(fv, cv);
1198 +                copycolor(cnt, fv);
1199 +                return(1);
1200 +        default:
1201 +                error(INTERNAL, "botched output format");
1202 +        }
1203 +        return(0);      /* pro forma return */
1204 + }
1205 +
1206 + /* close output file opened for input */
1207 + static int
1208 + myclose(const LUENT *e, void *p)
1209 + {
1210 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1211 +        
1212 +        if (sop->ofp == NULL)
1213 +                return;
1214 +        fclose(sop->ofp);
1215 +        sop->ofp = NULL;
1216 + }
1217 +
1218 + /* load previously accumulated values */
1219 + void
1220 + reload_output(void)
1221 + {
1222 +        int             i, j;
1223 +        MODCONT         *mp;
1224 +        int             ofl;
1225 +        char            oname[1024];
1226 +        char            *fmode = "rb";
1227 +        char            *outvfmt;
1228 +        LUENT           *ment, *oent;
1229 +        int             xr, yr;
1230 +        STREAMOUT       sout;
1231 +        DCOLOR          rgbv;
1232 +
1233 +        switch (outfmt) {
1234 +        case 'a':
1235 +                outvfmt = "ascii";
1236 +                fmode = "r";
1237 +                break;
1238 +        case 'f':
1239 +                outvfmt = "float";
1240 +                break;
1241 +        case 'd':
1242 +                outvfmt = "double";
1243 +                break;
1244 +        case 'c':
1245 +                outvfmt = COLRFMT;
1246 +                break;
1247 +        default:
1248 +                error(INTERNAL, "botched output format");
1249 +                return;
1250 +        }
1251 +                                                /* reload modifier values */
1252 +        for (i = 0; i < nmods; i++) {
1253 +                ment = lu_find(&modconttab,modname[i]);
1254 +                mp = (MODCONT *)ment->data;
1255 +                if (mp->outspec == NULL)
1256 +                        error(USER, "cannot reload from stdout");
1257 +                if (mp->outspec[0] == '!')
1258 +                        error(USER, "cannot reload from command");
1259 +                for (j = 0; ; j++) {            /* load each modifier bin */
1260 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1261 +                        if (ofl < 0)
1262 +                                error(USER, "bad output file specification");
1263 +                        oent = lu_find(&ofiletab, oname);
1264 +                        if (oent->data != NULL) {
1265 +                                sout = *(STREAMOUT *)oent->data;
1266 +                        } else {
1267 +                                sout.reclen = 0;
1268 +                                sout.outpipe = 0;
1269 +                                sout.xr = xres; sout.yr = yres;
1270 +                                sout.ofp = NULL;
1271 +                        }
1272 +                        if (sout.ofp == NULL) { /* open output as input */
1273 +                                sout.ofp = fopen(oname, fmode);
1274 +                                if (sout.ofp == NULL) {
1275 +                                        if (j)
1276 +                                                break;  /* assume end of modifier */
1277 +                                        sprintf(errmsg, "missing reload file '%s'",
1278 +                                                        oname);
1279 +                                        error(WARNING, errmsg);
1280 +                                        break;
1281 +                                }
1282 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1283 +                                        sprintf(errmsg, "format mismatch for '%s'",
1284 +                                                        oname);
1285 +                                        error(USER, errmsg);
1286 +                                }
1287 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1288 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1289 +                                                        (xr != sout.xr) |
1290 +                                                        (yr != sout.yr))) {
1291 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1292 +                                                        oname);
1293 +                                        error(USER, errmsg);
1294 +                                }
1295 +                        }
1296 +                        if (oent->key == NULL)  /* new file entry */
1297 +                                oent->key = strcpy((char *)
1298 +                                                malloc(strlen(oname)+1), oname);
1299 +                        if (oent->data == NULL)
1300 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1301 +                        *(STREAMOUT *)oent->data = sout;
1302 +                                                        /* read in RGB value */
1303 +                        if (!get_contrib(rgbv, sout.ofp)) {
1304 +                                if (!j)
1305 +                                        break;          /* ignore empty file */
1306 +                                if (j < mp->nbins) {
1307 +                                        sprintf(errmsg, "missing data in '%s'",
1308 +                                                        oname);
1309 +                                        error(USER, errmsg);
1310 +                                }
1311 +                                break;
1312 +                        }
1313 +                        if (j >= mp->nbins)             /* grow modifier size */
1314 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1315 +                        copycolor(mp->cbin[j], rgbv);
1316 +                }
1317 +        }
1318 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1319 + }
1320 +
1321   /* seek on the given output file */
1322   static int
1323   myseeko(const LUENT *e, void *p)
# Line 1071 | Line 1331 | myseeko(const LUENT *e, void *p)
1331                  sprintf(errmsg, "seek error on file '%s'", e->key);
1332                  error(SYSTEM, errmsg);
1333          }
1334 +        return 0;
1335   }
1336  
1337   /* recover output if possible */
# Line 1126 | Line 1387 | recover_output(FILE *fin)
1387                                  sout = *(STREAMOUT *)oent->data;
1388                          } else {
1389                                  sout.reclen = 0;
1390 +                                sout.outpipe = 0;
1391 +                                sout.xr = xres; sout.yr = yres;
1392                                  sout.ofp = NULL;
1393                          }
1394                          if (sout.ofp != NULL) { /* already open? */
# Line 1140 | Line 1403 | recover_output(FILE *fin)
1403                                          break;  /* assume end of modifier */
1404                                  sprintf(errmsg, "missing recover file '%s'",
1405                                                  oname);
1406 <                                error(USER, errmsg);
1406 >                                error(WARNING, errmsg);
1407 >                                break;
1408                          }
1409                          nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1410                          if (nvals <= 0) {
# Line 1148 | Line 1412 | recover_output(FILE *fin)
1412                                  fclose(sout.ofp);
1413                                  break;
1414                          }
1415 <                        if (!sout.reclen) {     /* unspecified record length */
1415 >                        if (!sout.reclen) {
1416                                  if (!(ofl & OF_BIN)) {
1417                                          sprintf(errmsg,
1418 <                                                "guessing 1 value per record in '%s'",
1418 >                                                "need -bn to recover file '%s'",
1419                                                          oname);
1420 <                                        error(WARNING, errmsg);
1420 >                                        error(USER, errmsg);
1421                                  }
1422                                  recsiz = outvsiz;
1423                          } else
# Line 1167 | Line 1431 | recover_output(FILE *fin)
1431                          }
1432                          if ((xres > 0) & (yres > 0) &&
1433                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1434 <                                                xr != xres ||
1435 <                                                yr != yres)) {
1434 >                                                (xr != xres) |
1435 >                                                (yr != yres))) {
1436                                  sprintf(errmsg, "resolution mismatch for '%s'",
1437                                                  oname);
1438                                  error(USER, errmsg);
# Line 1207 | Line 1471 | recover_output(FILE *fin)
1471          lu_doall(&ofiletab, myseeko, &nvals);
1472                                                  /* skip repeated input */
1473          for (nvals = 0; nvals < lastout; nvals++)
1474 <                if (getinp(oname, fin) <= 0)
1474 >                if (getinp(oname, fin) < 0)
1475                          error(USER, "unexpected EOF on input");
1476          lastray = lastdone = (unsigned long)lastout;
1477          if (raysleft)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines