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.32 by greg, Thu Oct 6 16:28:59 2005 UTC vs.
Revision 1.53 by greg, Thu Jun 11 18:32:24 2009 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 78 | Line 80 | LUTAB  ofiletab = LU_SINIT(free,closestream);  /* output
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 97 | Line 99 | struct rtproc {
99  
100                                          /* rtrace command and defaults */
101   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
102 <                                "-dj", ".5", "-dr", "3",
103 <                                "-ab", "1", "-ad", "128", };
102 >                                "-dj", ".9", "-dr", "3",
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             force_open = 0;         /* truncate existing output? */
135 + int             recover = 0;            /* recover previous output? */
136 + int             accumulate = 1;         /* input rays per output record */
137   int             xres = 0;               /* horiz. output resolution */
138   int             yres = 0;               /* vert. output resolution */
139  
140 + int             account;                /* current accumulation count */
141   unsigned long   raysleft;               /* number of rays left to trace */
142   long            waitflush;              /* how long until next flush */
143  
# Line 142 | Line 157 | void addmodfile(char *fname, char *outf, char *binv, i
157  
158   void init(int np);
159   int done_rprocs(struct rtproc *rtp);
160 + void reload_output(void);
161   void recover_output(FILE *fin);
162   void trace_contribs(FILE *fin);
163   struct rtproc *wait_rproc(void);
# Line 151 | Line 167 | void process_queue(void);
167  
168   void put_contrib(const DCOLOR cnt, FILE *fout);
169   void add_contrib(const char *modn);
170 < void done_contrib(void);
170 > void done_contrib(int navg);
171  
172   /* return number of open rtrace processes */
173   static int
# Line 204 | Line 220 | fmterr:
220   int
221   main(int argc, char *argv[])
222   {
223 +        int     contrib = 0;
224          int     nprocs = 1;
208        int     recover = 0;
225          char    *curout = NULL;
226          char    *binval = NULL;
227          int     bincnt = 0;
# Line 214 | Line 230 | main(int argc, char *argv[])
230                                  /* need at least one argument */
231          if (argc < 2) {
232                  fprintf(stderr,
233 < "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
233 > "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
234                          argv[0]);
235                  exit(1);
236          }
# Line 230 | Line 246 | main(int argc, char *argv[])
246                  while ((j = expandarg(&argc, &argv, i)) > 0)
247                          ;
248                  if (j < 0) {
249 <                        fprintf(stderr, "%s: cannot expand '%s'",
249 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
250                                          argv[0], argv[i]);
251                          exit(1);
252                  }
253                  if (argv[i][0] == '-')
254                          switch (argv[i][1]) {
239                        case 'r':               /* recover output */
240                                if (argv[i][2]) break;
241                                recover++;
242                                continue;
255                          case 'n':               /* number of processes */
256                                  if (argv[i][2] || i >= argc-2) break;
257                                  nprocs = atoi(argv[++i]);
258                                  if (nprocs <= 0)
259                                          error(USER, "illegal number of processes");
260                                  continue;
261 +                        case 'V':               /* output contributions */
262 +                                switch (argv[i][2]) {
263 +                                case '\0':
264 +                                        contrib = !contrib;
265 +                                        continue;
266 +                                case '+': case '1':
267 +                                case 'T': case 't':
268 +                                case 'Y': case 'y':
269 +                                        contrib = 1;
270 +                                        continue;
271 +                                case '-': case '0':
272 +                                case 'F': case 'f':
273 +                                case 'N': case 'n':
274 +                                        contrib = 0;
275 +                                        continue;
276 +                                }
277 +                                break;
278 +                        case 'c':               /* input rays per output */
279 +                                if (argv[i][2] || i >= argc-2) break;
280 +                                accumulate = atoi(argv[++i]);
281 +                                continue;
282 +                        case 'r':               /* recover output */
283 +                                if (argv[i][2]) break;
284 +                                recover = 1;
285 +                                continue;
286                          case 'h':               /* output header? */
287                                  switch (argv[i][2]) {
288                                  case '\0':
# Line 263 | Line 300 | main(int argc, char *argv[])
300                                          continue;
301                                  }
302                                  break;
303 <                        case 'f':               /* file or i/o format */
303 >                        case 'f':               /* file or force or format */
304                                  if (!argv[i][2]) {
305                                          char    *fpath;
306                                          if (i >= argc-2) break;
# Line 278 | Line 315 | main(int argc, char *argv[])
315                                          fcompile(fpath);
316                                          continue;
317                                  }
318 +                                if (argv[i][2] == 'o') {
319 +                                        force_open = 1;
320 +                                        continue;
321 +                                }
322                                  setformat(argv[i]+2);
323                                  continue;
324                          case 'e':               /* expression */
# Line 317 | Line 358 | main(int argc, char *argv[])
358                                  rtargv[rtargc++] = argv[++i];
359                                  addmodfile(argv[i], curout, binval, bincnt);
360                                  continue;
361 +                        case 'P':               /* persist file */
362 +                                if (i >= argc-2) break;
363 +                                persist_state = (argv[i][2] == 'P') ?
364 +                                                PERSIST_PARALL : PERSIST_SINGLE;
365 +                                rtargv[rtargc++] = argv[i];
366 +                                rtargv[rtargc++] = argv[++i];
367 +                                continue;
368                          }
369                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
370          }
371 +        if (accumulate <= 0)    /* no output flushing for single record */
372 +                xres = yres = 0;
373                                  /* set global argument list */
374          gargc = argc; gargv = argv;
375                                  /* add "mandatory" rtrace settings */
376          for (j = 0; myrtopts[j] != NULL; j++)
377                  rtargv[rtargc++] = myrtopts[j];
378 +        rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
379                                  /* just asking for defaults? */
380          if (!strcmp(argv[i], "-defaults")) {
381                  char    sxres[16], syres[16];
382                  char    *rtpath;
383 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
383 >                printf("-n %-2d\t\t\t\t# number of processes\n", nprocs);
384 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
385 >                                accumulate);
386 >                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
387 >                                contrib ? "contributions" : "coefficients");
388                  fflush(stdout);                 /* report OUR options */
389                  rtargv[rtargc++] = header ? "-h+" : "-h-";
390                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 340 | Line 395 | main(int argc, char *argv[])
395                  rtargv[rtargc++] = "-y";
396                  sprintf(syres, "%d", yres);
397                  rtargv[rtargc++] = syres;
343                rtargv[rtargc++] = "-oTW";
398                  rtargv[rtargc++] = "-defaults";
399                  rtargv[rtargc] = NULL;
400                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 354 | Line 408 | main(int argc, char *argv[])
408                  exit(1);
409          }
410          if (nprocs > 1) {       /* add persist file if parallel */
411 <                rtargv[rtargc++] = "-PP";
412 <                rtargv[rtargc++] = mktemp(persistfn);
411 >                if (persist_state == PERSIST_SINGLE)
412 >                        error(USER, "use -PP option for multiple processes");
413 >                if (persist_state == PERSIST_NONE) {
414 >                        rtargv[rtargc++] = "-PP";
415 >                        rtargv[rtargc++] = mktemp(persistfn);
416 >                        persist_state = PERSIST_OURS;
417 >                }
418          }
419                                  /* add format string */
420          sprintf(fmt, "-f%cf", inpfmt);
# Line 365 | Line 424 | main(int argc, char *argv[])
424                  error(USER, "missing octree argument");
425          rtargv[rtargc++] = octree = argv[i];
426          rtargv[rtargc] = NULL;
427 <                                /* start rtrace */
427 >                                /* start rtrace & recover if requested */
428          init(nprocs);
429 <        if (recover)            /* perform recovery if requested */
430 <                recover_output(stdin);
431 <        trace_contribs(stdin);  /* compute contributions */
429 >                                /* compute contributions */
430 >        trace_contribs(stdin);
431 >                                /* clean up */
432          quit(0);
433   }
434  
# Line 415 | Line 474 | quit(int status)
474   {
475          int     rtstat;
476  
477 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
477 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
478                  killpersist();
479                                          /* clean up rtrace process(es) */
480          rtstat = done_rprocs(&rt0);
# Line 482 | Line 541 | init(int np)
541                          raysleft = yres;
542          } else
543                  raysleft = 0;
544 +        if ((account = accumulate) > 0)
545 +                raysleft *= accumulate;
546          waitflush = xres;
547 +        if (!recover)
548 +                return;
549 +                                        /* recover previous values */
550 +        if (accumulate <= 0)
551 +                reload_output();
552 +        else
553 +                recover_output(stdin);
554   }
555  
556   /* grow modifier to accommodate more bins */
# Line 532 | Line 600 | addmodifier(char *modn, char *outf, char *binv, int bi
600                  mp = growmodifier(mp, bincnt);
601          lep->data = (char *)mp;
602                                          /* allocate output streams */
603 <        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; )
603 >        for (i = bincnt; i-- > 0; )
604                  getostream(mp->outspec, mp->modname, i, 1);
605          return mp;
606   }
# Line 653 | Line 721 | printheader(FILE *fout, const char *info)
721  
722   /* write resolution string to given output stream */
723   void
724 < printresolu(FILE *fout)
724 > printresolu(FILE *fout, int xr, int yr)
725   {
726 <        if (xres > 0) {
727 <                if (yres > 0)                   /* resolution string */
728 <                        fprtresolu(xres, yres, fout);
726 >        if ((xr > 0) & (yr > 0))        /* resolution string */
727 >                fprtresolu(xr, yr, fout);
728 >        if (xres > 0)                   /* global flush flag */
729                  fflush(fout);
662        }
730   }
731  
732   /* Get output stream pointer (open and write header if new and noopen==0) */
733   STREAMOUT *
734   getostream(const char *ospec, const char *mname, int bn, int noopen)
735   {
736 +        static const DCOLOR     nocontrib = BLKCOLOR;
737          static STREAMOUT        stdos;
738          int                     ofl;
739          char                    oname[1024];
# Line 674 | Line 742 | getostream(const char *ospec, const char *mname, int b
742          
743          if (ospec == NULL) {                    /* use stdout? */
744                  if (!noopen && !using_stdout) {
677                        stdos.reclen = 0;
745                          if (outfmt != 'a')
746                                  SET_FILE_BINARY(stdout);
747                          if (header)
748                                  printheader(stdout, NULL);
749 <                        printresolu(stdout);
749 >                        printresolu(stdout, xres, yres);
750 >                        stdos.xr = xres; stdos.yr = yres;
751                          using_stdout = 1;
752                  }
753                  stdos.ofp = stdout;
# Line 699 | Line 767 | getostream(const char *ospec, const char *mname, int b
767                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
768                  if (sop == NULL)
769                          error(SYSTEM, "out of memory in getostream");
770 <                sop->reclen = oname[0] == '!' ? CNT_PIPE : 0;
770 >                sop->outpipe = oname[0] == '!';
771 >                sop->reclen = 0;
772                  sop->ofp = NULL;                /* open iff noopen==0 */
773 +                sop->xr = xres; sop->yr = yres;
774                  lep->data = (char *)sop;
775 +                if (!sop->outpipe & !force_open & !recover &&
776 +                                access(oname, F_OK) == 0) {
777 +                        errno = EEXIST;         /* file exists */
778 +                        goto openerr;
779 +                }
780          }
781          if (!noopen && sop->ofp == NULL) {      /* open output stream */
782 <                int             i;
782 >                long            i;
783                  if (oname[0] == '!')            /* output to command */
784                          sop->ofp = popen(oname+1, "w");
785 <                else
785 >                else                            /* else open file */
786                          sop->ofp = fopen(oname, "w");
787 <                if (sop->ofp == NULL) {
788 <                        sprintf(errmsg, "cannot open '%s' for writing", oname);
714 <                        error(SYSTEM, errmsg);
715 <                }
787 >                if (sop->ofp == NULL)
788 >                        goto openerr;
789                  if (outfmt != 'a')
790                          SET_FILE_BINARY(sop->ofp);
791                  if (header) {
# Line 729 | Line 802 | getostream(const char *ospec, const char *mname, int b
802                          *cp = '\0';
803                          printheader(sop->ofp, info);
804                  }
805 <                printresolu(sop->ofp);
805 >                if (accumulate > 0) {           /* global resolution */
806 >                        sop->xr = xres; sop->yr = yres;
807 >                }
808 >                printresolu(sop->ofp, sop->xr, sop->yr);
809                                                  /* play catch-up */
810 <                for (i = 0; i < lastdone; i++) {
811 <                        static const DCOLOR     nocontrib = BLKCOLOR;
812 <                        put_contrib(nocontrib, sop->ofp);
810 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
811 >                        int     j = sop->reclen;
812 >                        if (j <= 0) j = 1;
813 >                        while (j--)
814 >                                put_contrib(nocontrib, sop->ofp);
815                          if (outfmt == 'a')
816                                  putc('\n', sop->ofp);
817                  }
818                  if (xres > 0)
819                          fflush(sop->ofp);
820          }
821 <        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
822 <                sop->reclen += noopen;
823 <        return sop;                             /* return open stream */
821 >        sop->reclen += noopen;                  /* add to length if noopen */
822 >        return sop;                             /* return output stream */
823 > openerr:
824 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
825 >        error(SYSTEM, errmsg);
826 >        return NULL;    /* pro forma return */
827   }
828  
829   /* read input ray into buffer */
830   int
831   getinp(char *buf, FILE *fp)
832   {
833 +        double  dv[3], *dvp;
834 +        float   *fvp;
835          char    *cp;
836          int     i;
837  
# Line 757 | Line 840 | getinp(char *buf, FILE *fp)
840                  cp = buf;               /* make sure we get 6 floats */
841                  for (i = 0; i < 6; i++) {
842                          if (fgetword(cp, buf+127-cp, fp) == NULL)
843 <                                return 0;
843 >                                return -1;
844 >                        if (i >= 3)
845 >                                dv[i-3] = atof(cp);
846                          if ((cp = fskip(cp)) == NULL || *cp)
847 <                                return 0;
847 >                                return -1;
848                          *cp++ = ' ';
849                  }
850                  getc(fp);               /* get/put eol */
851                  *cp-- = '\0'; *cp = '\n';
852 +                if (DOT(dv,dv) <= FTINY*FTINY)
853 +                        return 0;       /* dummy ray */
854                  return strlen(buf);
855          case 'f':
856                  if (fread(buf, sizeof(float), 6, fp) < 6)
857 <                        return 0;
857 >                        return -1;
858 >                fvp = (float *)buf + 3;
859 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
860 >                        return 0;       /* dummy ray */
861                  return sizeof(float)*6;
862          case 'd':
863                  if (fread(buf, sizeof(double), 6, fp) < 6)
864 <                        return 0;
864 >                        return -1;
865 >                dvp = (double *)buf + 3;
866 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
867 >                        return 0;       /* dummy ray */
868                  return sizeof(double)*6;
869          }
870          error(INTERNAL, "botched input format");
871 <        return 0;       /* pro forma return */
871 >        return -1;      /* pro forma return */
872   }
873  
874   static float    rparams[9];             /* traced ray parameters */
# Line 839 | Line 932 | put_contrib(const DCOLOR cnt, FILE *fout)
932                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
933                  break;
934          case 'f':
935 <                fv[0] = cnt[0];
843 <                fv[1] = cnt[1];
844 <                fv[2] = cnt[2];
935 >                copycolor(fv, cnt);
936                  fwrite(fv, sizeof(float), 3, fout);
937                  break;
938          case 'd':
# Line 856 | Line 947 | put_contrib(const DCOLOR cnt, FILE *fout)
947          }
948   }
949  
950 < /* output ray tallies and clear for next primary */
950 > /* output ray tallies and clear for next accumulation */
951   void
952 < done_contrib(void)
952 > done_contrib(int navg)
953   {
954 +        double          sf = 1.;
955          int             i, j;
956          MODCONT         *mp;
957          STREAMOUT       *sop;
958 +                                                /* set average scaling */
959 +        if (navg > 1)
960 +                sf = 1. / (double)navg;
961                                                  /* output modifiers in order */
962          for (i = 0; i < nmods; i++) {
963                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
964 +                if (navg > 1)                   /* average scaling */
965 +                        for (j = mp->nbins; j--; )
966 +                                scalecolor(mp->cbin[j], sf);
967                  sop = getostream(mp->outspec, mp->modname, 0,0);
968                  put_contrib(mp->cbin[0], sop->ofp);
969                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 876 | Line 974 | done_contrib(void)
974                          for (j = 1; j < mp->nbins; j++)
975                                  put_contrib(mp->cbin[j],
976                                      getostream(mp->outspec,mp->modname,j,0)->ofp);
977 <                                                /* clear for next ray tree */
977 >                                                /* clear for next time */
978                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
979          }
980          --waitflush;                            /* terminate records */
# Line 935 | Line 1033 | process_queue(void)
1033                          if (!n || !(isalpha(*cp) | (*cp == '_')))
1034                                  error(USER, "bad modifier name from rtrace");
1035                                          /* get modifier name */
1036 <                        while (n > 0 && *cp != '\t') {
1036 >                        while (n > 1 && *cp != '\t') {
1037 >                                if (mnp - modname >= sizeof(modname)-2)
1038 >                                        error(INTERNAL, "modifier name too long");
1039                                  *mnp++ = *cp++; n--;
1040                          }
1041                          *mnp = '\0';
# Line 947 | Line 1047 | process_queue(void)
1047                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1048                          add_contrib(modname);
1049                  }
1050 <                done_contrib();         /* sum up contributions & output */
1050 >                                        /* time to produce record? */
1051 >                if (account > 0 && !--account)
1052 >                        done_contrib(account = accumulate);
1053                  lastdone = rtp->raynum;
1054 <                free(rtp->buf);         /* free up buffer space */
1054 >                if (rtp->buf != NULL)   /* free up buffer space */
1055 >                        free(rtp->buf);
1056                  rt_unproc = rtp->next;
1057                  free(rtp);              /* done with this ray tree */
1058          }
# Line 1033 | Line 1136 | get_rproc(void)
1136   void
1137   trace_contribs(FILE *fin)
1138   {
1139 +        static int      ignore_warning_given = 0;
1140          char            inpbuf[128];
1141          int             iblen;
1142          struct rtproc   *rtp;
1143                                                  /* loop over input */
1144 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1145 <                if (lastray+1 < lastray ||      /* need reset? */
1146 <                                queue_length() > 5*nrtprocs()) {
1144 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1145 >                if (!iblen && accumulate != 1) {
1146 >                        if (!ignore_warning_given++)
1147 >                                error(WARNING,
1148 >                                "dummy ray(s) ignored during accumulation\n");
1149 >                        continue;
1150 >                }
1151 >                if (!iblen ||                   /* need flush/reset? */
1152 >                                queue_length() > 10*nrtprocs() ||
1153 >                                lastray+1 < lastray) {
1154                          while (wait_rproc() != NULL)
1155                                  process_queue();
1156 <                        lastdone = lastray = 0;
1156 >                        if (lastray+1 < lastray)
1157 >                                lastdone = lastray = 0;
1158                  }
1159                  rtp = get_rproc();              /* get avail. rtrace process */
1160 <                rtp->raynum = ++lastray;        /* assign ray to it */
1161 <                writebuf(rtp->pd.w, inpbuf, iblen);
1162 <                if (raysleft && !--raysleft)
1163 <                        break;
1160 >                rtp->raynum = ++lastray;        /* assign ray */
1161 >                if (iblen) {                    /* trace ray if valid */
1162 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1163 >                } else {                        /* else bypass dummy ray */
1164 >                        queue_raytree(rtp);     /* queue empty ray/record */
1165 >                        if ((yres <= 0) | (xres <= 0))
1166 >                                waitflush = 1;  /* flush right after */
1167 >                }
1168                  process_queue();                /* catch up with results */
1169 +                if (raysleft && !--raysleft)
1170 +                        break;                  /* preemptive EOI */
1171          }
1172          while (wait_rproc() != NULL)            /* process outstanding rays */
1173                  process_queue();
1174 +        if (accumulate <= 0)
1175 +                done_contrib(0);                /* output tallies */
1176 +        else if (account < accumulate) {
1177 +                error(WARNING, "partial accumulation in final record");
1178 +                done_contrib(accumulate - account);
1179 +        }
1180          if (raysleft)
1181                  error(USER, "unexpected EOF on input");
1182          lu_done(&ofiletab);                     /* close output files */
1183   }
1184  
1185 + /* get ray contribution from previous file */
1186 + static int
1187 + get_contrib(DCOLOR cnt, FILE *finp)
1188 + {
1189 +        COLOR   fv;
1190 +        COLR    cv;
1191 +
1192 +        switch (outfmt) {
1193 +        case 'a':
1194 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1195 +        case 'f':
1196 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1197 +                        return(0);
1198 +                copycolor(cnt, fv);
1199 +                return(1);
1200 +        case 'd':
1201 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1202 +        case 'c':
1203 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1204 +                        return(0);
1205 +                colr_color(fv, cv);
1206 +                copycolor(cnt, fv);
1207 +                return(1);
1208 +        default:
1209 +                error(INTERNAL, "botched output format");
1210 +        }
1211 +        return(0);      /* pro forma return */
1212 + }
1213 +
1214 + /* close output file opened for input */
1215 + static int
1216 + myclose(const LUENT *e, void *p)
1217 + {
1218 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1219 +        
1220 +        if (sop->ofp == NULL)
1221 +                return(0);
1222 +        fclose(sop->ofp);
1223 +        sop->ofp = NULL;
1224 +        return(0);
1225 + }
1226 +
1227 + /* load previously accumulated values */
1228 + void
1229 + reload_output(void)
1230 + {
1231 +        int             i, j;
1232 +        MODCONT         *mp;
1233 +        int             ofl;
1234 +        char            oname[1024];
1235 +        char            *fmode = "rb";
1236 +        char            *outvfmt;
1237 +        LUENT           *ment, *oent;
1238 +        int             xr, yr;
1239 +        STREAMOUT       sout;
1240 +        DCOLOR          rgbv;
1241 +
1242 +        switch (outfmt) {
1243 +        case 'a':
1244 +                outvfmt = "ascii";
1245 +                fmode = "r";
1246 +                break;
1247 +        case 'f':
1248 +                outvfmt = "float";
1249 +                break;
1250 +        case 'd':
1251 +                outvfmt = "double";
1252 +                break;
1253 +        case 'c':
1254 +                outvfmt = COLRFMT;
1255 +                break;
1256 +        default:
1257 +                error(INTERNAL, "botched output format");
1258 +                return;
1259 +        }
1260 +                                                /* reload modifier values */
1261 +        for (i = 0; i < nmods; i++) {
1262 +                ment = lu_find(&modconttab,modname[i]);
1263 +                mp = (MODCONT *)ment->data;
1264 +                if (mp->outspec == NULL)
1265 +                        error(USER, "cannot reload from stdout");
1266 +                if (mp->outspec[0] == '!')
1267 +                        error(USER, "cannot reload from command");
1268 +                for (j = 0; ; j++) {            /* load each modifier bin */
1269 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1270 +                        if (ofl < 0)
1271 +                                error(USER, "bad output file specification");
1272 +                        oent = lu_find(&ofiletab, oname);
1273 +                        if (oent->data != NULL) {
1274 +                                sout = *(STREAMOUT *)oent->data;
1275 +                        } else {
1276 +                                sout.reclen = 0;
1277 +                                sout.outpipe = 0;
1278 +                                sout.xr = xres; sout.yr = yres;
1279 +                                sout.ofp = NULL;
1280 +                        }
1281 +                        if (sout.ofp == NULL) { /* open output as input */
1282 +                                sout.ofp = fopen(oname, fmode);
1283 +                                if (sout.ofp == NULL) {
1284 +                                        if (j)
1285 +                                                break;  /* assume end of modifier */
1286 +                                        sprintf(errmsg, "missing reload file '%s'",
1287 +                                                        oname);
1288 +                                        error(WARNING, errmsg);
1289 +                                        break;
1290 +                                }
1291 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1292 +                                        sprintf(errmsg, "format mismatch for '%s'",
1293 +                                                        oname);
1294 +                                        error(USER, errmsg);
1295 +                                }
1296 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1297 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1298 +                                                        (xr != sout.xr) |
1299 +                                                        (yr != sout.yr))) {
1300 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1301 +                                                        oname);
1302 +                                        error(USER, errmsg);
1303 +                                }
1304 +                        }
1305 +                                                        /* read in RGB value */
1306 +                        if (!get_contrib(rgbv, sout.ofp)) {
1307 +                                if (!j) {
1308 +                                        fclose(sout.ofp);
1309 +                                        break;          /* ignore empty file */
1310 +                                }
1311 +                                if (j < mp->nbins) {
1312 +                                        sprintf(errmsg, "missing data in '%s'",
1313 +                                                        oname);
1314 +                                        error(USER, errmsg);
1315 +                                }
1316 +                                break;
1317 +                        }
1318 +                        if (j >= mp->nbins)             /* grow modifier size */
1319 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1320 +                        copycolor(mp->cbin[j], rgbv);
1321 +                        if (oent->key == NULL)          /* new file entry */
1322 +                                oent->key = strcpy((char *)
1323 +                                                malloc(strlen(oname)+1), oname);
1324 +                        if (oent->data == NULL)
1325 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1326 +                        *(STREAMOUT *)oent->data = sout;
1327 +                }
1328 +        }
1329 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1330 + }
1331 +
1332   /* seek on the given output file */
1333   static int
1334   myseeko(const LUENT *e, void *p)
# Line 1071 | Line 1342 | myseeko(const LUENT *e, void *p)
1342                  sprintf(errmsg, "seek error on file '%s'", e->key);
1343                  error(SYSTEM, errmsg);
1344          }
1345 +        return 0;
1346   }
1347  
1348   /* recover output if possible */
# Line 1126 | Line 1398 | recover_output(FILE *fin)
1398                                  sout = *(STREAMOUT *)oent->data;
1399                          } else {
1400                                  sout.reclen = 0;
1401 +                                sout.outpipe = 0;
1402                                  sout.ofp = NULL;
1403                          }
1404                          if (sout.ofp != NULL) { /* already open? */
# Line 1140 | Line 1413 | recover_output(FILE *fin)
1413                                          break;  /* assume end of modifier */
1414                                  sprintf(errmsg, "missing recover file '%s'",
1415                                                  oname);
1416 <                                error(USER, errmsg);
1416 >                                error(WARNING, errmsg);
1417 >                                break;
1418                          }
1419                          nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1420                          if (nvals <= 0) {
# Line 1148 | Line 1422 | recover_output(FILE *fin)
1422                                  fclose(sout.ofp);
1423                                  break;
1424                          }
1425 <                        if (sout.reclen == CNT_UNKNOWN) {
1425 >                        if (!sout.reclen) {
1426                                  if (!(ofl & OF_BIN)) {
1427                                          sprintf(errmsg,
1428                                                  "need -bn to recover file '%s'",
# Line 1165 | Line 1439 | recover_output(FILE *fin)
1439                                                  oname);
1440                                  error(USER, errmsg);
1441                          }
1442 <                        if ((xres > 0) & (yres > 0) &&
1442 >                        sout.xr = xres; sout.yr = yres;
1443 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1444                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1445 <                                                xr != xres ||
1446 <                                                yr != yres)) {
1445 >                                                (xr != sout.xr) |
1446 >                                                (yr != sout.yr))) {
1447                                  sprintf(errmsg, "resolution mismatch for '%s'",
1448                                                  oname);
1449                                  error(USER, errmsg);
# Line 1197 | Line 1472 | recover_output(FILE *fin)
1472                  error(WARNING, "no output files to recover");
1473                  return;
1474          }
1475 <        if (raysleft && lastout >= raysleft) {
1475 >        if (raysleft && lastout >= raysleft/accumulate) {
1476                  error(WARNING, "output appears to be complete");
1477                  /* XXX should read & discard input? */
1478                  quit(0);
# Line 1207 | Line 1482 | recover_output(FILE *fin)
1482          lu_doall(&ofiletab, myseeko, &nvals);
1483                                                  /* skip repeated input */
1484          for (nvals = 0; nvals < lastout; nvals++)
1485 <                if (getinp(oname, fin) <= 0)
1485 >                if (getinp(oname, fin) < 0)
1486                          error(USER, "unexpected EOF on input");
1487 <        lastray = lastdone = (unsigned long)lastout;
1487 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1488          if (raysleft)
1489                  raysleft -= lastray;
1490   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines