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.40 by greg, Sun Feb 5 22:22:21 2006 UTC vs.
Revision 1.63 by greg, Sat Apr 9 18:05:18 2011 UTC

# Line 20 | Line 20 | static const char RCSid[] = "$Id$";
20   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
21   #endif
22  
23 < int     treebufsiz = BUFSIZ;            /* current tree buffer size */
23 > size_t  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
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 64 | Line 63 | closestream(void *p)
63   {
64          STREAMOUT       *sop = (STREAMOUT *)p;
65          int             status;
66 <        if (sop->reclen == CNT_PIPE)
66 >        if (sop->outpipe)
67                  status = pclose(sop->ofp);
68          else
69                  status = fclose(sop->ofp);
# Line 81 | 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 93 | Line 92 | struct rtproc {
92          struct rtproc   *next;          /* next in list of processes */
93          SUBPROC         pd;             /* rtrace pipe descriptors */
94          unsigned long   raynum;         /* ray number for this tree */
95 <        int             bsiz;           /* ray tree buffer length */
95 >        size_t          bsiz;           /* ray tree buffer length */
96          char            *buf;           /* ray tree buffer */
97 <        int             nbr;            /* number of bytes from rtrace */
97 >        size_t          nbr;            /* number of bytes from rtrace */
98   };                              /* rtrace process buffer */
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[] = { "-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 */
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 128 | Line 132 | 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 150 | 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 159 | 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 214 | Line 222 | main(int argc, char *argv[])
222   {
223          int     contrib = 0;
224          int     nprocs = 1;
217        int     recover = 0;
225          char    *curout = NULL;
226          char    *binval = NULL;
227          int     bincnt = 0;
# Line 239 | 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                  }
# Line 268 | Line 275 | main(int argc, char *argv[])
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++;
284 >                                recover = 1;
285                                  continue;
286                          case 'h':               /* output header? */
287                                  switch (argv[i][2]) {
# Line 305 | Line 316 | main(int argc, char *argv[])
316                                          continue;
317                                  }
318                                  if (argv[i][2] == 'o') {
319 <                                        force_open++;
319 >                                        force_open = 1;
320                                          continue;
321                                  }
322                                  setformat(argv[i]+2);
# Line 329 | Line 340 | main(int argc, char *argv[])
340                          case 'b':               /* bin expression/count */
341                                  if (i >= argc-2) break;
342                                  if (argv[i][2] == 'n') {
343 <                                        bincnt = atoi(argv[++i]);
343 >                                        bincnt = (int)(eval(argv[++i]) + .5);
344                                          continue;
345                                  }
346                                  if (argv[i][2]) break;
# Line 348 | Line 359 | main(int argc, char *argv[])
359                                  addmodfile(argv[i], curout, binval, bincnt);
360                                  continue;
361                          case 'P':               /* persist file */
362 <                                error(USER, "persist file is automatic");
363 <                                break;
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 */
# Line 361 | Line 378 | main(int argc, char *argv[])
378          rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
379                                  /* just asking for defaults? */
380          if (!strcmp(argv[i], "-defaults")) {
381 <                char    sxres[16], syres[16];
381 >                char    nps[8], sxres[16], syres[16];
382                  char    *rtpath;
383 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
383 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
384 >                                accumulate);
385                  printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
386                                  contrib ? "contributions" : "coefficients");
387                  fflush(stdout);                 /* report OUR options */
388 +                rtargv[rtargc++] = "-n";
389 +                sprintf(nps, "%d", nprocs);
390 +                rtargv[rtargc++] = nps;
391                  rtargv[rtargc++] = header ? "-h+" : "-h-";
392                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
393                  rtargv[rtargc++] = fmt;
# Line 389 | Line 410 | main(int argc, char *argv[])
410                  exit(1);
411          }
412          if (nprocs > 1) {       /* add persist file if parallel */
413 <                rtargv[rtargc++] = "-PP";
414 <                rtargv[rtargc++] = mktemp(persistfn);
413 >                if (persist_state == PERSIST_SINGLE)
414 >                        error(USER, "use -PP option for multiple processes");
415 >                if (persist_state == PERSIST_NONE) {
416 >                        rtargv[rtargc++] = "-PP";
417 >                        rtargv[rtargc++] = mktemp(persistfn);
418 >                        persist_state = PERSIST_OURS;
419 >                }
420          }
421                                  /* add format string */
422          sprintf(fmt, "-f%cf", inpfmt);
# Line 400 | Line 426 | main(int argc, char *argv[])
426                  error(USER, "missing octree argument");
427          rtargv[rtargc++] = octree = argv[i];
428          rtargv[rtargc] = NULL;
429 <                                /* start rtrace */
429 >                                /* start rtrace & recover if requested */
430          init(nprocs);
431 <        if (recover)            /* perform recovery if requested */
432 <                recover_output(stdin);
433 <        trace_contribs(stdin);  /* compute contributions */
431 >                                /* compute contributions */
432 >        trace_contribs(stdin);
433 >                                /* clean up */
434          quit(0);
435   }
436  
# Line 450 | Line 476 | quit(int status)
476   {
477          int     rtstat;
478  
479 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
479 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
480                  killpersist();
481                                          /* clean up rtrace process(es) */
482          rtstat = done_rprocs(&rt0);
# Line 517 | Line 543 | init(int np)
543                          raysleft = yres;
544          } else
545                  raysleft = 0;
546 <        waitflush = xres;
546 >        if ((account = accumulate) > 0)
547 >                raysleft *= accumulate;
548 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
549 >        if (!recover)
550 >                return;
551 >                                        /* recover previous values */
552 >        if (accumulate <= 0)
553 >                reload_output();
554 >        else
555 >                recover_output(stdin);
556   }
557  
558   /* grow modifier to accommodate more bins */
# Line 547 | Line 582 | addmodifier(char *modn, char *outf, char *binv, int bi
582                  error(USER, errmsg);
583          }
584          if (nmods >= MAXMODLIST)
585 <                error(USER, "too many modifiers");
585 >                error(INTERNAL, "too many modifiers");
586          modname[nmods++] = modn;        /* XXX assumes static string */
587          lep->key = modn;                /* XXX assumes static string */
588          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 555 | Line 590 | addmodifier(char *modn, char *outf, char *binv, int bi
590                  error(SYSTEM, "out of memory in addmodifier");
591          mp->outspec = outf;             /* XXX assumes static string */
592          mp->modname = modn;             /* XXX assumes static string */
593 <        if (binv != NULL)
594 <                mp->binv = eparse(binv);
595 <        else
596 <                mp->binv = eparse("0");
597 <        mp->nbins = 1;
593 >        if (binv == NULL)
594 >                binv = "0";             /* use single bin if unspecified */
595 >        mp->binv = eparse(binv);
596 >        if (mp->binv->type == NUM) {    /* check value if constant */
597 >                bincnt = (int)(evalue(mp->binv) + 1.5);
598 >                if (bincnt != 1) {
599 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
600 >                                        binv);
601 >                        error(USER, errmsg);
602 >                }
603 >        }
604 >        mp->nbins = 1;                  /* initialize results holder */
605          setcolor(mp->cbin[0], 0., 0., 0.);
606 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
565 <                bincnt = 1;
566 <        else if (bincnt > 1)
606 >        if (bincnt > 1)
607                  mp = growmodifier(mp, bincnt);
608          lep->data = (char *)mp;
609                                          /* allocate output streams */
610 <        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; )
610 >        for (i = bincnt; i-- > 0; )
611                  getostream(mp->outspec, mp->modname, i, 1);
612          return mp;
613   }
# Line 626 | Line 666 | ofname(char *oname, const char *ospec, const char *mna
666                                  mnp = cp;
667                                  break;
668                          case 'd':
669 +                        case 'i':
670 +                        case 'o':
671 +                        case 'x':
672 +                        case 'X':
673                                  if (bnp != NULL)
674                                          return -1;
675                                  bnp = cp;
# Line 658 | Line 702 | void
702   printheader(FILE *fout, const char *info)
703   {
704          extern char     VersionID[];
705 <        FILE            *fin = fopen(octree, "r");
706 <        
707 <        if (fin == NULL)
708 <                quit(1);
709 <        checkheader(fin, "ignore", fout);       /* copy octree header */
710 <        fclose(fin);
705 >                                                /* copy octree header */
706 >        if (octree[0] == '!') {
707 >                newheader("RADIANCE", fout);
708 >                fputs(octree+1, fout);
709 >                if (octree[strlen(octree)-1] != '\n')
710 >                        fputc('\n', fout);
711 >        } else {
712 >                FILE    *fin = fopen(octree, "r");
713 >                if (fin == NULL)
714 >                        quit(1);
715 >                checkheader(fin, "ignore", fout);
716 >                fclose(fin);
717 >        }
718          printargs(gargc-1, gargv, fout);        /* add our command */
719          fprintf(fout, "SOFTWARE= %s\n", VersionID);
720          fputnow(fout);
# Line 688 | Line 739 | printheader(FILE *fout, const char *info)
739  
740   /* write resolution string to given output stream */
741   void
742 < printresolu(FILE *fout)
742 > printresolu(FILE *fout, int xr, int yr)
743   {
744 <        if (xres > 0) {
745 <                if (yres > 0)                   /* resolution string */
695 <                        fprtresolu(xres, yres, fout);
696 <                fflush(fout);
697 <        }
744 >        if ((xr > 0) & (yr > 0))        /* resolution string */
745 >                fprtresolu(xr, yr, fout);
746   }
747  
748   /* Get output stream pointer (open and write header if new and noopen==0) */
749   STREAMOUT *
750   getostream(const char *ospec, const char *mname, int bn, int noopen)
751   {
752 +        static const DCOLOR     nocontrib = BLKCOLOR;
753          static STREAMOUT        stdos;
754          int                     ofl;
755          char                    oname[1024];
# Line 709 | Line 758 | getostream(const char *ospec, const char *mname, int b
758          
759          if (ospec == NULL) {                    /* use stdout? */
760                  if (!noopen && !using_stdout) {
712                        stdos.reclen = 0;
761                          if (outfmt != 'a')
762                                  SET_FILE_BINARY(stdout);
763                          if (header)
764                                  printheader(stdout, NULL);
765 <                        printresolu(stdout);
765 >                        printresolu(stdout, xres, yres);
766 >                        if (waitflush > 0)
767 >                                fflush(stdout);
768 >                        stdos.xr = xres; stdos.yr = yres;
769                          using_stdout = 1;
770                  }
771                  stdos.ofp = stdout;
# Line 734 | Line 785 | getostream(const char *ospec, const char *mname, int b
785                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
786                  if (sop == NULL)
787                          error(SYSTEM, "out of memory in getostream");
788 <                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
788 >                sop->outpipe = oname[0] == '!';
789 >                sop->reclen = 0;
790                  sop->ofp = NULL;                /* open iff noopen==0 */
791 +                sop->xr = xres; sop->yr = yres;
792                  lep->data = (char *)sop;
793 +                if (!sop->outpipe & !force_open & !recover &&
794 +                                access(oname, F_OK) == 0) {
795 +                        errno = EEXIST;         /* file exists */
796 +                        goto openerr;
797 +                }
798          }
799          if (!noopen && sop->ofp == NULL) {      /* open output stream */
800                  long            i;
801                  if (oname[0] == '!')            /* output to command */
802                          sop->ofp = popen(oname+1, "w");
803 <                else if (!force_open && access(oname, F_OK) == 0)
746 <                        errno = EEXIST;         /* file exists */
747 <                else                            /* else open it */
803 >                else                            /* else open file */
804                          sop->ofp = fopen(oname, "w");
805 <                if (sop->ofp == NULL) {
806 <                        sprintf(errmsg, "cannot open '%s' for writing", oname);
751 <                        error(SYSTEM, errmsg);
752 <                }
805 >                if (sop->ofp == NULL)
806 >                        goto openerr;
807                  if (outfmt != 'a')
808                          SET_FILE_BINARY(sop->ofp);
809                  if (header) {
# Line 766 | Line 820 | getostream(const char *ospec, const char *mname, int b
820                          *cp = '\0';
821                          printheader(sop->ofp, info);
822                  }
823 <                printresolu(sop->ofp);
823 >                if (accumulate > 0) {           /* global resolution */
824 >                        sop->xr = xres; sop->yr = yres;
825 >                }
826 >                printresolu(sop->ofp, sop->xr, sop->yr);
827                                                  /* play catch-up */
828 <                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
829 <                                                                i--; ) {
830 <                        static const DCOLOR     nocontrib = BLKCOLOR;
831 <                        put_contrib(nocontrib, sop->ofp);
828 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
829 >                        int     j = sop->reclen;
830 >                        if (j <= 0) j = 1;
831 >                        while (j--)
832 >                                put_contrib(nocontrib, sop->ofp);
833                          if (outfmt == 'a')
834                                  putc('\n', sop->ofp);
835                  }
836 <                if (xres > 0)
836 >                if (waitflush > 0)
837                          fflush(sop->ofp);
838          }
839 <        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
840 <                sop->reclen += noopen;
841 <        return sop;                             /* return open stream */
839 >        sop->reclen += noopen;                  /* add to length if noopen */
840 >        return sop;                             /* return output stream */
841 > openerr:
842 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
843 >        error(SYSTEM, errmsg);
844 >        return NULL;    /* pro forma return */
845   }
846  
847   /* read input ray into buffer */
848   int
849   getinp(char *buf, FILE *fp)
850   {
851 <        double  dv[3];
852 <        float   fv[3];
851 >        double  dv[3], *dvp;
852 >        float   *fvp;
853          char    *cp;
854          int     i;
855  
# Line 812 | Line 873 | getinp(char *buf, FILE *fp)
873          case 'f':
874                  if (fread(buf, sizeof(float), 6, fp) < 6)
875                          return -1;
876 <                memcpy(fv, buf+3*sizeof(float), 3*sizeof(float));
877 <                if (DOT(fv,fv) <= FTINY*FTINY)
876 >                fvp = (float *)buf + 3;
877 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
878                          return 0;       /* dummy ray */
879                  return sizeof(float)*6;
880          case 'd':
881                  if (fread(buf, sizeof(double), 6, fp) < 6)
882                          return -1;
883 <                memcpy(dv, buf+3*sizeof(double), 3*sizeof(double));
884 <                if (DOT(dv,dv) <= FTINY*FTINY)
883 >                dvp = (double *)buf + 3;
884 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
885                          return 0;       /* dummy ray */
886                  return sizeof(double)*6;
887          }
# Line 889 | Line 950 | put_contrib(const DCOLOR cnt, FILE *fout)
950                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
951                  break;
952          case 'f':
953 <                fv[0] = cnt[0];
893 <                fv[1] = cnt[1];
894 <                fv[2] = cnt[2];
953 >                copycolor(fv, cnt);
954                  fwrite(fv, sizeof(float), 3, fout);
955                  break;
956          case 'd':
# Line 906 | Line 965 | put_contrib(const DCOLOR cnt, FILE *fout)
965          }
966   }
967  
968 < /* output ray tallies and clear for next primary */
968 > /* output ray tallies and clear for next accumulation */
969   void
970 < done_contrib(void)
970 > done_contrib(int navg)
971   {
972 +        double          sf = 1.;
973          int             i, j;
974          MODCONT         *mp;
975          STREAMOUT       *sop;
976 +                                                /* set average scaling */
977 +        if (navg > 1)
978 +                sf = 1. / (double)navg;
979                                                  /* output modifiers in order */
980          for (i = 0; i < nmods; i++) {
981                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
982 +                if (navg > 1)                   /* average scaling */
983 +                        for (j = mp->nbins; j--; )
984 +                                scalecolor(mp->cbin[j], sf);
985                  sop = getostream(mp->outspec, mp->modname, 0,0);
986                  put_contrib(mp->cbin[0], sop->ofp);
987                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 926 | Line 992 | done_contrib(void)
992                          for (j = 1; j < mp->nbins; j++)
993                                  put_contrib(mp->cbin[j],
994                                      getostream(mp->outspec,mp->modname,j,0)->ofp);
995 <                                                /* clear for next ray tree */
995 >                                                /* clear for next time */
996                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
997          }
998          --waitflush;                            /* terminate records */
# Line 934 | Line 1000 | done_contrib(void)
1000          if (using_stdout & (outfmt == 'a'))
1001                  putc('\n', stdout);
1002          if (!waitflush) {
1003 <                waitflush = xres;
1003 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
1004                  if (using_stdout)
1005                          fflush(stdout);
1006          }
# Line 999 | Line 1065 | process_queue(void)
1065                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1066                          add_contrib(modname);
1067                  }
1068 <                done_contrib();         /* sum up contributions & output */
1068 >                                        /* time to produce record? */
1069 >                if (account > 0 && !--account)
1070 >                        done_contrib(account = accumulate);
1071                  lastdone = rtp->raynum;
1072                  if (rtp->buf != NULL)   /* free up buffer space */
1073                          free(rtp->buf);
# Line 1014 | Line 1082 | wait_rproc(void)
1082   {
1083          struct rtproc   *rtfree = NULL;
1084          fd_set          readset, errset;
1085 <        int             nr;
1085 >        ssize_t         nr;
1086          struct rtproc   *rt;
1087          int             n;
1088          
# Line 1045 | Line 1113 | wait_rproc(void)
1113                                  continue;
1114                          if (rt->buf == NULL) {
1115                                  rt->bsiz = treebufsiz;
1116 <                                rt->buf = (char *)malloc(treebufsiz);
1116 >                                rt->buf = (char *)malloc(rt->bsiz);
1117                          } else if (rt->nbr + BUFSIZ > rt->bsiz) {
1118                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1119                                          rt->bsiz = treebufsiz;
# Line 1059 | Line 1127 | wait_rproc(void)
1127                          if (nr <= 0)
1128                                  error(USER, "rtrace process died");
1129                          rt->nbr += nr;          /* advance & check */
1130 <                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
1131 <                                                        "~\t~\t", 4)) {
1132 <                                rt->nbr -= 4;   /* elide terminator */
1130 >                        if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6,
1131 >                                                        "~\t~\t~\t", 6)) {
1132 >                                rt->nbr -= 6;   /* elide terminator */
1133                                  queue_raytree(rt);
1134                                  rtfree = rt;    /* ready for next ray */
1135                          }
# Line 1086 | Line 1154 | get_rproc(void)
1154   void
1155   trace_contribs(FILE *fin)
1156   {
1157 +        static int      ignore_warning_given = 0;
1158          char            inpbuf[128];
1159          int             iblen;
1160          struct rtproc   *rtp;
1161                                                  /* loop over input */
1162          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1163 <                if (!iblen ||                   /* need reset? */
1163 >                if (!iblen && accumulate != 1) {
1164 >                        if (!ignore_warning_given++)
1165 >                                error(WARNING,
1166 >                                "dummy ray(s) ignored during accumulation\n");
1167 >                        continue;
1168 >                }
1169 >                if (!iblen ||                   /* need flush/reset? */
1170                                  queue_length() > 10*nrtprocs() ||
1171                                  lastray+1 < lastray) {
1172                          while (wait_rproc() != NULL)
1173                                  process_queue();
1174 <                        if (lastray+1 < lastray)
1100 <                                lastdone = lastray = 0;
1174 >                        lastdone = lastray = 0;
1175                  }
1176                  rtp = get_rproc();              /* get avail. rtrace process */
1177                  rtp->raynum = ++lastray;        /* assign ray */
1178                  if (iblen) {                    /* trace ray if valid */
1179                          writebuf(rtp->pd.w, inpbuf, iblen);
1180                  } else {                        /* else bypass dummy ray */
1181 <                        queue_raytree(rtp);     /* empty tree */
1182 <                        if ((yres <= 0) | (waitflush > 1))
1183 <                                waitflush = 1;  /* flush after this */
1181 >                        queue_raytree(rtp);     /* queue empty ray/record */
1182 >                        if ((yres <= 0) | (xres <= 0))
1183 >                                waitflush = 1;  /* flush right after */
1184                  }
1185                  process_queue();                /* catch up with results */
1186                  if (raysleft && !--raysleft)
# Line 1114 | Line 1188 | trace_contribs(FILE *fin)
1188          }
1189          while (wait_rproc() != NULL)            /* process outstanding rays */
1190                  process_queue();
1191 +        if (accumulate <= 0)
1192 +                done_contrib(0);                /* output tallies */
1193 +        else if (account < accumulate) {
1194 +                error(WARNING, "partial accumulation in final record");
1195 +                done_contrib(accumulate - account);
1196 +        }
1197          if (raysleft)
1198                  error(USER, "unexpected EOF on input");
1199          lu_done(&ofiletab);                     /* close output files */
1200   }
1201  
1202 + /* get ray contribution from previous file */
1203 + static int
1204 + get_contrib(DCOLOR cnt, FILE *finp)
1205 + {
1206 +        COLOR   fv;
1207 +        COLR    cv;
1208 +
1209 +        switch (outfmt) {
1210 +        case 'a':
1211 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1212 +        case 'f':
1213 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1214 +                        return(0);
1215 +                copycolor(cnt, fv);
1216 +                return(1);
1217 +        case 'd':
1218 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1219 +        case 'c':
1220 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1221 +                        return(0);
1222 +                colr_color(fv, cv);
1223 +                copycolor(cnt, fv);
1224 +                return(1);
1225 +        default:
1226 +                error(INTERNAL, "botched output format");
1227 +        }
1228 +        return(0);      /* pro forma return */
1229 + }
1230 +
1231 + /* close output file opened for input */
1232 + static int
1233 + myclose(const LUENT *e, void *p)
1234 + {
1235 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1236 +        
1237 +        if (sop->ofp == NULL)
1238 +                return(0);
1239 +        fclose(sop->ofp);
1240 +        sop->ofp = NULL;
1241 +        return(0);
1242 + }
1243 +
1244 + /* load previously accumulated values */
1245 + void
1246 + reload_output(void)
1247 + {
1248 +        int             i, j;
1249 +        MODCONT         *mp;
1250 +        int             ofl;
1251 +        char            oname[1024];
1252 +        char            *fmode = "rb";
1253 +        char            *outvfmt;
1254 +        LUENT           *ment, *oent;
1255 +        int             xr, yr;
1256 +        STREAMOUT       sout;
1257 +        DCOLOR          rgbv;
1258 +
1259 +        switch (outfmt) {
1260 +        case 'a':
1261 +                outvfmt = "ascii";
1262 +                fmode = "r";
1263 +                break;
1264 +        case 'f':
1265 +                outvfmt = "float";
1266 +                break;
1267 +        case 'd':
1268 +                outvfmt = "double";
1269 +                break;
1270 +        case 'c':
1271 +                outvfmt = COLRFMT;
1272 +                break;
1273 +        default:
1274 +                error(INTERNAL, "botched output format");
1275 +                return;
1276 +        }
1277 +                                                /* reload modifier values */
1278 +        for (i = 0; i < nmods; i++) {
1279 +                ment = lu_find(&modconttab,modname[i]);
1280 +                mp = (MODCONT *)ment->data;
1281 +                if (mp->outspec == NULL)
1282 +                        error(USER, "cannot reload from stdout");
1283 +                if (mp->outspec[0] == '!')
1284 +                        error(USER, "cannot reload from command");
1285 +                for (j = 0; ; j++) {            /* load each modifier bin */
1286 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1287 +                        if (ofl < 0)
1288 +                                error(USER, "bad output file specification");
1289 +                        oent = lu_find(&ofiletab, oname);
1290 +                        if (oent->data != NULL) {
1291 +                                sout = *(STREAMOUT *)oent->data;
1292 +                        } else {
1293 +                                sout.reclen = 0;
1294 +                                sout.outpipe = 0;
1295 +                                sout.xr = xres; sout.yr = yres;
1296 +                                sout.ofp = NULL;
1297 +                        }
1298 +                        if (sout.ofp == NULL) { /* open output as input */
1299 +                                sout.ofp = fopen(oname, fmode);
1300 +                                if (sout.ofp == NULL) {
1301 +                                        if (j)
1302 +                                                break;  /* assume end of modifier */
1303 +                                        sprintf(errmsg, "missing reload file '%s'",
1304 +                                                        oname);
1305 +                                        error(WARNING, errmsg);
1306 +                                        break;
1307 +                                }
1308 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1309 +                                        sprintf(errmsg, "format mismatch for '%s'",
1310 +                                                        oname);
1311 +                                        error(USER, errmsg);
1312 +                                }
1313 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1314 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1315 +                                                        (xr != sout.xr) |
1316 +                                                        (yr != sout.yr))) {
1317 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1318 +                                                        oname);
1319 +                                        error(USER, errmsg);
1320 +                                }
1321 +                        }
1322 +                                                        /* read in RGB value */
1323 +                        if (!get_contrib(rgbv, sout.ofp)) {
1324 +                                if (!j) {
1325 +                                        fclose(sout.ofp);
1326 +                                        break;          /* ignore empty file */
1327 +                                }
1328 +                                if (j < mp->nbins) {
1329 +                                        sprintf(errmsg, "missing data in '%s'",
1330 +                                                        oname);
1331 +                                        error(USER, errmsg);
1332 +                                }
1333 +                                break;
1334 +                        }
1335 +                        if (j >= mp->nbins)             /* grow modifier size */
1336 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1337 +                        copycolor(mp->cbin[j], rgbv);
1338 +                        if (oent->key == NULL)          /* new file entry */
1339 +                                oent->key = strcpy((char *)
1340 +                                                malloc(strlen(oname)+1), oname);
1341 +                        if (oent->data == NULL)
1342 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1343 +                        *(STREAMOUT *)oent->data = sout;
1344 +                }
1345 +        }
1346 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1347 + }
1348 +
1349   /* seek on the given output file */
1350   static int
1351   myseeko(const LUENT *e, void *p)
# Line 1132 | Line 1359 | myseeko(const LUENT *e, void *p)
1359                  sprintf(errmsg, "seek error on file '%s'", e->key);
1360                  error(SYSTEM, errmsg);
1361          }
1362 +        return 0;
1363   }
1364  
1365   /* recover output if possible */
# Line 1186 | Line 1414 | recover_output(FILE *fin)
1414                          if (oent->data != NULL) {
1415                                  sout = *(STREAMOUT *)oent->data;
1416                          } else {
1417 <                                sout.reclen = CNT_UNKNOWN;
1417 >                                sout.reclen = 0;
1418 >                                sout.outpipe = 0;
1419                                  sout.ofp = NULL;
1420                          }
1421                          if (sout.ofp != NULL) { /* already open? */
# Line 1201 | Line 1430 | recover_output(FILE *fin)
1430                                          break;  /* assume end of modifier */
1431                                  sprintf(errmsg, "missing recover file '%s'",
1432                                                  oname);
1433 <                                error(USER, errmsg);
1433 >                                error(WARNING, errmsg);
1434 >                                break;
1435                          }
1436                          nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1437                          if (nvals <= 0) {
# Line 1209 | Line 1439 | recover_output(FILE *fin)
1439                                  fclose(sout.ofp);
1440                                  break;
1441                          }
1442 <                        if (sout.reclen == CNT_UNKNOWN) {
1442 >                        if (!sout.reclen) {
1443                                  if (!(ofl & OF_BIN)) {
1444                                          sprintf(errmsg,
1445                                                  "need -bn to recover file '%s'",
# Line 1226 | Line 1456 | recover_output(FILE *fin)
1456                                                  oname);
1457                                  error(USER, errmsg);
1458                          }
1459 <                        if ((xres > 0) & (yres > 0) &&
1459 >                        sout.xr = xres; sout.yr = yres;
1460 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1461                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1462 <                                                xr != xres ||
1463 <                                                yr != yres)) {
1462 >                                                (xr != sout.xr) |
1463 >                                                (yr != sout.yr))) {
1464                                  sprintf(errmsg, "resolution mismatch for '%s'",
1465                                                  oname);
1466                                  error(USER, errmsg);
# Line 1258 | Line 1489 | recover_output(FILE *fin)
1489                  error(WARNING, "no output files to recover");
1490                  return;
1491          }
1492 <        if (raysleft && lastout >= raysleft) {
1492 >        if (raysleft && lastout >= raysleft/accumulate) {
1493                  error(WARNING, "output appears to be complete");
1494                  /* XXX should read & discard input? */
1495                  quit(0);
# Line 1270 | Line 1501 | recover_output(FILE *fin)
1501          for (nvals = 0; nvals < lastout; nvals++)
1502                  if (getinp(oname, fin) < 0)
1503                          error(USER, "unexpected EOF on input");
1504 <        lastray = lastdone = (unsigned long)lastout;
1504 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1505          if (raysleft)
1506                  raysleft -= lastray;
1507   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines