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.65 by greg, Wed Oct 5 17:20:55 2011 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include  "lookup.h"
17   #include  "calcomp.h"
18  
19 + #ifdef _WIN32
20 + typedef long    ssize_t;
21 + #endif
22 +
23   #ifndef MAXMODLIST
24   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
25   #endif
26  
27 < int     treebufsiz = BUFSIZ;            /* current tree buffer size */
27 > ssize_t treebufsiz = BUFSIZ;            /* current tree buffer size */
28  
29   typedef double  DCOLOR[3];              /* double-precision color */
30  
31   /*
32   * The MODCONT structure is used to accumulate ray contributions
33   * for a particular modifier, which may be subdivided into bins
34 < * if binv is non-zero.  If outspec contains a %s in it, this will
34 > * if binv evaluates > 0.  If outspec contains a %s in it, this will
35   * be replaced with the modifier name.  If outspec contains a %d in it,
36   * this will be used to create one output file per bin, otherwise all bins
37   * will be written to the same file, in order.  If the global outfmt
# Line 46 | Line 50 | static void mcfree(void *p) { epfree((*(MODCONT *)p).b
50  
51   LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */
52  
49 #define CNT_UNKNOWN     0               /* unknown record length */
50 #define CNT_PIPE        (-1)            /* output to a pipe */
53   /*
54   * The STREAMOUT structure holds an open FILE pointer and a count of
55 < * the number of RGB triplets per record, or CNT_UNKNOWN (0) if
54 < * unknown or CNT_PIPE (-1) if writing to a command.
55 > * the number of RGB triplets per record, or 0 if unknown.
56   */
57   typedef struct {
58          FILE            *ofp;           /* output file pointer */
59 +        int             outpipe;        /* output is to a pipe */
60          int             reclen;         /* triplets/record */
61 +        int             xr, yr;         /* output resolution for picture */
62   } STREAMOUT;
63  
64   /* close output stream and free record */
# Line 63 | Line 66 | static void
66   closestream(void *p)
67   {
68          STREAMOUT       *sop = (STREAMOUT *)p;
69 <        if (sop->reclen == CNT_PIPE)
70 <                pclose(sop->ofp);
69 >        int             status;
70 >        if (sop->outpipe)
71 >                status = pclose(sop->ofp);
72          else
73 <                fclose(sop->ofp);
73 >                status = fclose(sop->ofp);
74 >        if (status)
75 >                error(SYSTEM, "error closing output stream");
76          free(p);
77   }
78  
# Line 78 | Line 84 | LUTAB  ofiletab = LU_SINIT(free,closestream);  /* output
84   STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
85   int ofname(char *oname, const char *ospec, const char *mname, int bn);
86   void printheader(FILE *fout, const char *info);
87 < void printresolu(FILE *fout);
87 > void printresolu(FILE *fout, int xr, int yr);
88  
89   /*
90   * The rcont structure is used to manage i/o with a particular
# Line 90 | Line 96 | struct rtproc {
96          struct rtproc   *next;          /* next in list of processes */
97          SUBPROC         pd;             /* rtrace pipe descriptors */
98          unsigned long   raynum;         /* ray number for this tree */
99 <        int             bsiz;           /* ray tree buffer length */
99 >        size_t          bsiz;           /* ray tree buffer length */
100          char            *buf;           /* ray tree buffer */
101 <        int             nbr;            /* number of bytes from rtrace */
101 >        size_t          nbr;            /* number of bytes from rtrace */
102   };                              /* rtrace process buffer */
103  
104                                          /* rtrace command and defaults */
105   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
106 <                                "-dj", ".5", "-dr", "3",
107 <                                "-ab", "1", "-ad", "128", };
106 >                                "-dj", ".9", "-dr", "3",
107 >                                "-ab", "1", "-ad", "350", };
108 >
109   int  rtargc = 9;
110                                          /* overriding rtrace options */
111 < char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
111 > char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
112                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
113  
114 + #define RTCOEFF         "-o~~~TmWdp"    /* compute coefficients only */
115 + #define RTCONTRIB       "-o~~~TmVdp"    /* compute ray contributions */
116 +
117   struct rtproc   rt0;                    /* head of rtrace process list */
118  
119   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
120  
121 < char    persistfn[] = "pfXXXXXX";       /* persist file name */
121 > #define PERSIST_NONE    0               /* no persist file */
122 > #define PERSIST_SINGLE  1               /* user set -P persist */
123 > #define PERSIST_PARALL  2               /* user set -PP persist */
124 > #define PERSIST_OURS    3               /* -PP persist belongs to us */
125 > int     persist_state = PERSIST_NONE;   /* persist file state */
126 > char    persistfn[] = "pfXXXXXX";       /* our persist file name, if set */
127  
128   int             gargc;                  /* global argc */
129   char            **gargv;                /* global argv */
# Line 120 | Line 135 | int            inpfmt = 'a';           /* input format */
135   int             outfmt = 'a';           /* output format */
136  
137   int             header = 1;             /* output header? */
138 + int             force_open = 0;         /* truncate existing output? */
139 + int             recover = 0;            /* recover previous output? */
140 + int             accumulate = 1;         /* input rays per output record */
141   int             xres = 0;               /* horiz. output resolution */
142   int             yres = 0;               /* vert. output resolution */
143  
144 + int             account;                /* current accumulation count */
145   unsigned long   raysleft;               /* number of rays left to trace */
146   long            waitflush;              /* how long until next flush */
147  
# Line 142 | Line 161 | void addmodfile(char *fname, char *outf, char *binv, i
161  
162   void init(int np);
163   int done_rprocs(struct rtproc *rtp);
164 + void reload_output(void);
165   void recover_output(FILE *fin);
166   void trace_contribs(FILE *fin);
167   struct rtproc *wait_rproc(void);
# Line 151 | Line 171 | void process_queue(void);
171  
172   void put_contrib(const DCOLOR cnt, FILE *fout);
173   void add_contrib(const char *modn);
174 < void done_contrib(void);
174 > void done_contrib(int navg);
175  
176   /* return number of open rtrace processes */
177   static int
# Line 204 | Line 224 | fmterr:
224   int
225   main(int argc, char *argv[])
226   {
227 +        int     contrib = 0;
228          int     nprocs = 1;
208        int     recover = 0;
229          char    *curout = NULL;
230          char    *binval = NULL;
231          int     bincnt = 0;
# Line 214 | Line 234 | main(int argc, char *argv[])
234                                  /* need at least one argument */
235          if (argc < 2) {
236                  fprintf(stderr,
237 < "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
237 > "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
238                          argv[0]);
239                  exit(1);
240          }
# Line 230 | Line 250 | main(int argc, char *argv[])
250                  while ((j = expandarg(&argc, &argv, i)) > 0)
251                          ;
252                  if (j < 0) {
253 <                        fprintf(stderr, "%s: cannot expand '%s'",
253 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
254                                          argv[0], argv[i]);
255                          exit(1);
256                  }
257                  if (argv[i][0] == '-')
258                          switch (argv[i][1]) {
239                        case 'r':               /* recover output */
240                                if (argv[i][2]) break;
241                                recover++;
242                                continue;
259                          case 'n':               /* number of processes */
260                                  if (argv[i][2] || i >= argc-2) break;
261                                  nprocs = atoi(argv[++i]);
262                                  if (nprocs <= 0)
263                                          error(USER, "illegal number of processes");
264                                  continue;
265 +                        case 'V':               /* output contributions */
266 +                                switch (argv[i][2]) {
267 +                                case '\0':
268 +                                        contrib = !contrib;
269 +                                        continue;
270 +                                case '+': case '1':
271 +                                case 'T': case 't':
272 +                                case 'Y': case 'y':
273 +                                        contrib = 1;
274 +                                        continue;
275 +                                case '-': case '0':
276 +                                case 'F': case 'f':
277 +                                case 'N': case 'n':
278 +                                        contrib = 0;
279 +                                        continue;
280 +                                }
281 +                                break;
282 +                        case 'c':               /* input rays per output */
283 +                                if (argv[i][2] || i >= argc-2) break;
284 +                                accumulate = atoi(argv[++i]);
285 +                                continue;
286 +                        case 'r':               /* recover output */
287 +                                if (argv[i][2]) break;
288 +                                recover = 1;
289 +                                continue;
290                          case 'h':               /* output header? */
291                                  switch (argv[i][2]) {
292                                  case '\0':
# Line 263 | Line 304 | main(int argc, char *argv[])
304                                          continue;
305                                  }
306                                  break;
307 <                        case 'f':               /* file or i/o format */
307 >                        case 'f':               /* file or force or format */
308                                  if (!argv[i][2]) {
309                                          char    *fpath;
310                                          if (i >= argc-2) break;
# Line 278 | Line 319 | main(int argc, char *argv[])
319                                          fcompile(fpath);
320                                          continue;
321                                  }
322 +                                if (argv[i][2] == 'o') {
323 +                                        force_open = 1;
324 +                                        continue;
325 +                                }
326                                  setformat(argv[i]+2);
327                                  continue;
328                          case 'e':               /* expression */
# Line 299 | Line 344 | main(int argc, char *argv[])
344                          case 'b':               /* bin expression/count */
345                                  if (i >= argc-2) break;
346                                  if (argv[i][2] == 'n') {
347 <                                        bincnt = atoi(argv[++i]);
347 >                                        bincnt = (int)(eval(argv[++i]) + .5);
348                                          continue;
349                                  }
350                                  if (argv[i][2]) break;
# Line 317 | Line 362 | main(int argc, char *argv[])
362                                  rtargv[rtargc++] = argv[++i];
363                                  addmodfile(argv[i], curout, binval, bincnt);
364                                  continue;
365 +                        case 'P':               /* persist file */
366 +                                if (i >= argc-2) break;
367 +                                persist_state = (argv[i][2] == 'P') ?
368 +                                                PERSIST_PARALL : PERSIST_SINGLE;
369 +                                rtargv[rtargc++] = argv[i];
370 +                                rtargv[rtargc++] = argv[++i];
371 +                                continue;
372                          }
373                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
374          }
375 +        if (accumulate <= 0)    /* no output flushing for single record */
376 +                xres = yres = 0;
377                                  /* set global argument list */
378          gargc = argc; gargv = argv;
379                                  /* add "mandatory" rtrace settings */
380          for (j = 0; myrtopts[j] != NULL; j++)
381                  rtargv[rtargc++] = myrtopts[j];
382 +        rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
383                                  /* just asking for defaults? */
384          if (!strcmp(argv[i], "-defaults")) {
385 <                char    sxres[16], syres[16];
385 >                char    nps[8], sxres[16], syres[16];
386                  char    *rtpath;
387 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
387 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
388 >                                accumulate);
389 >                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
390 >                                contrib ? "contributions" : "coefficients");
391                  fflush(stdout);                 /* report OUR options */
392 +                rtargv[rtargc++] = "-n";
393 +                sprintf(nps, "%d", nprocs);
394 +                rtargv[rtargc++] = nps;
395                  rtargv[rtargc++] = header ? "-h+" : "-h-";
396                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
397                  rtargv[rtargc++] = fmt;
# Line 340 | Line 401 | main(int argc, char *argv[])
401                  rtargv[rtargc++] = "-y";
402                  sprintf(syres, "%d", yres);
403                  rtargv[rtargc++] = syres;
343                rtargv[rtargc++] = "-oTW";
404                  rtargv[rtargc++] = "-defaults";
405                  rtargv[rtargc] = NULL;
406                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 354 | Line 414 | main(int argc, char *argv[])
414                  exit(1);
415          }
416          if (nprocs > 1) {       /* add persist file if parallel */
417 <                rtargv[rtargc++] = "-PP";
418 <                rtargv[rtargc++] = mktemp(persistfn);
417 >                if (persist_state == PERSIST_SINGLE)
418 >                        error(USER, "use -PP option for multiple processes");
419 >                if (persist_state == PERSIST_NONE) {
420 >                        rtargv[rtargc++] = "-PP";
421 >                        rtargv[rtargc++] = mktemp(persistfn);
422 >                        persist_state = PERSIST_OURS;
423 >                }
424          }
425                                  /* add format string */
426          sprintf(fmt, "-f%cf", inpfmt);
# Line 365 | Line 430 | main(int argc, char *argv[])
430                  error(USER, "missing octree argument");
431          rtargv[rtargc++] = octree = argv[i];
432          rtargv[rtargc] = NULL;
433 <                                /* start rtrace */
433 >                                /* start rtrace & recover if requested */
434          init(nprocs);
435 <        if (recover)            /* perform recovery if requested */
436 <                recover_output(stdin);
437 <        trace_contribs(stdin);  /* compute contributions */
435 >                                /* compute contributions */
436 >        trace_contribs(stdin);
437 >                                /* clean up */
438          quit(0);
439   }
440  
# Line 415 | Line 480 | quit(int status)
480   {
481          int     rtstat;
482  
483 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
483 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
484                  killpersist();
485                                          /* clean up rtrace process(es) */
486          rtstat = done_rprocs(&rt0);
# Line 482 | Line 547 | init(int np)
547                          raysleft = yres;
548          } else
549                  raysleft = 0;
550 <        waitflush = xres;
550 >        if ((account = accumulate) > 0)
551 >                raysleft *= accumulate;
552 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
553 >        if (!recover)
554 >                return;
555 >                                        /* recover previous values */
556 >        if (accumulate <= 0)
557 >                reload_output();
558 >        else
559 >                recover_output(stdin);
560   }
561  
562   /* grow modifier to accommodate more bins */
# Line 512 | Line 586 | addmodifier(char *modn, char *outf, char *binv, int bi
586                  error(USER, errmsg);
587          }
588          if (nmods >= MAXMODLIST)
589 <                error(USER, "too many modifiers");
589 >                error(INTERNAL, "too many modifiers");
590          modname[nmods++] = modn;        /* XXX assumes static string */
591          lep->key = modn;                /* XXX assumes static string */
592          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 520 | Line 594 | addmodifier(char *modn, char *outf, char *binv, int bi
594                  error(SYSTEM, "out of memory in addmodifier");
595          mp->outspec = outf;             /* XXX assumes static string */
596          mp->modname = modn;             /* XXX assumes static string */
597 <        if (binv != NULL)
598 <                mp->binv = eparse(binv);
599 <        else
600 <                mp->binv = eparse("0");
601 <        mp->nbins = 1;
597 >        if (binv == NULL)
598 >                binv = "0";             /* use single bin if unspecified */
599 >        mp->binv = eparse(binv);
600 >        if (mp->binv->type == NUM) {    /* check value if constant */
601 >                bincnt = (int)(evalue(mp->binv) + 1.5);
602 >                if (bincnt != 1) {
603 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
604 >                                        binv);
605 >                        error(USER, errmsg);
606 >                }
607 >        }
608 >        mp->nbins = 1;                  /* initialize results holder */
609          setcolor(mp->cbin[0], 0., 0., 0.);
610 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
530 <                bincnt = 1;
531 <        else if (bincnt > 1)
610 >        if (bincnt > 1)
611                  mp = growmodifier(mp, bincnt);
612          lep->data = (char *)mp;
613                                          /* allocate output streams */
614 <        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; )
614 >        for (i = bincnt; i-- > 0; )
615                  getostream(mp->outspec, mp->modname, i, 1);
616          return mp;
617   }
# Line 591 | Line 670 | ofname(char *oname, const char *ospec, const char *mna
670                                  mnp = cp;
671                                  break;
672                          case 'd':
673 +                        case 'i':
674 +                        case 'o':
675 +                        case 'x':
676 +                        case 'X':
677                                  if (bnp != NULL)
678                                          return -1;
679                                  bnp = cp;
# Line 623 | Line 706 | void
706   printheader(FILE *fout, const char *info)
707   {
708          extern char     VersionID[];
709 <        FILE            *fin = fopen(octree, "r");
710 <        
711 <        if (fin == NULL)
712 <                quit(1);
713 <        checkheader(fin, "ignore", fout);       /* copy octree header */
714 <        fclose(fin);
709 >                                                /* copy octree header */
710 >        if (octree[0] == '!') {
711 >                newheader("RADIANCE", fout);
712 >                fputs(octree+1, fout);
713 >                if (octree[strlen(octree)-1] != '\n')
714 >                        fputc('\n', fout);
715 >        } else {
716 >                FILE    *fin = fopen(octree, "r");
717 >                if (fin == NULL)
718 >                        quit(1);
719 >                checkheader(fin, "ignore", fout);
720 >                fclose(fin);
721 >        }
722          printargs(gargc-1, gargv, fout);        /* add our command */
723          fprintf(fout, "SOFTWARE= %s\n", VersionID);
724          fputnow(fout);
# Line 653 | Line 743 | printheader(FILE *fout, const char *info)
743  
744   /* write resolution string to given output stream */
745   void
746 < printresolu(FILE *fout)
746 > printresolu(FILE *fout, int xr, int yr)
747   {
748 <        if (xres > 0) {
749 <                if (yres > 0)                   /* resolution string */
660 <                        fprtresolu(xres, yres, fout);
661 <                fflush(fout);
662 <        }
748 >        if ((xr > 0) & (yr > 0))        /* resolution string */
749 >                fprtresolu(xr, yr, fout);
750   }
751  
752   /* Get output stream pointer (open and write header if new and noopen==0) */
753   STREAMOUT *
754   getostream(const char *ospec, const char *mname, int bn, int noopen)
755   {
756 +        static const DCOLOR     nocontrib = BLKCOLOR;
757          static STREAMOUT        stdos;
758          int                     ofl;
759          char                    oname[1024];
# Line 674 | Line 762 | getostream(const char *ospec, const char *mname, int b
762          
763          if (ospec == NULL) {                    /* use stdout? */
764                  if (!noopen && !using_stdout) {
677                        stdos.reclen = 0;
765                          if (outfmt != 'a')
766                                  SET_FILE_BINARY(stdout);
767                          if (header)
768                                  printheader(stdout, NULL);
769 <                        printresolu(stdout);
769 >                        printresolu(stdout, xres, yres);
770 >                        if (waitflush > 0)
771 >                                fflush(stdout);
772 >                        stdos.xr = xres; stdos.yr = yres;
773                          using_stdout = 1;
774                  }
775                  stdos.ofp = stdout;
# Line 699 | Line 789 | getostream(const char *ospec, const char *mname, int b
789                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
790                  if (sop == NULL)
791                          error(SYSTEM, "out of memory in getostream");
792 <                sop->reclen = oname[0] == '!' ? CNT_PIPE : 0;
792 >                sop->outpipe = oname[0] == '!';
793 >                sop->reclen = 0;
794                  sop->ofp = NULL;                /* open iff noopen==0 */
795 +                sop->xr = xres; sop->yr = yres;
796                  lep->data = (char *)sop;
797 +                if (!sop->outpipe & !force_open & !recover &&
798 +                                access(oname, F_OK) == 0) {
799 +                        errno = EEXIST;         /* file exists */
800 +                        goto openerr;
801 +                }
802          }
803          if (!noopen && sop->ofp == NULL) {      /* open output stream */
804 <                int             i;
804 >                long            i;
805                  if (oname[0] == '!')            /* output to command */
806                          sop->ofp = popen(oname+1, "w");
807 <                else
807 >                else                            /* else open file */
808                          sop->ofp = fopen(oname, "w");
809 <                if (sop->ofp == NULL) {
810 <                        sprintf(errmsg, "cannot open '%s' for writing", oname);
714 <                        error(SYSTEM, errmsg);
715 <                }
809 >                if (sop->ofp == NULL)
810 >                        goto openerr;
811                  if (outfmt != 'a')
812                          SET_FILE_BINARY(sop->ofp);
813                  if (header) {
# Line 729 | Line 824 | getostream(const char *ospec, const char *mname, int b
824                          *cp = '\0';
825                          printheader(sop->ofp, info);
826                  }
827 <                printresolu(sop->ofp);
827 >                if (accumulate > 0) {           /* global resolution */
828 >                        sop->xr = xres; sop->yr = yres;
829 >                }
830 >                printresolu(sop->ofp, sop->xr, sop->yr);
831                                                  /* play catch-up */
832 <                for (i = 0; i < lastdone; i++) {
833 <                        static const DCOLOR     nocontrib = BLKCOLOR;
834 <                        put_contrib(nocontrib, sop->ofp);
832 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
833 >                        int     j = sop->reclen;
834 >                        if (j <= 0) j = 1;
835 >                        while (j--)
836 >                                put_contrib(nocontrib, sop->ofp);
837                          if (outfmt == 'a')
838                                  putc('\n', sop->ofp);
839                  }
840 <                if (xres > 0)
840 >                if (waitflush > 0)
841                          fflush(sop->ofp);
842          }
843 <        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
844 <                sop->reclen += noopen;
845 <        return sop;                             /* return open stream */
843 >        sop->reclen += noopen;                  /* add to length if noopen */
844 >        return sop;                             /* return output stream */
845 > openerr:
846 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
847 >        error(SYSTEM, errmsg);
848 >        return NULL;    /* pro forma return */
849   }
850  
851   /* read input ray into buffer */
852   int
853   getinp(char *buf, FILE *fp)
854   {
855 +        double  dv[3], *dvp;
856 +        float   *fvp;
857          char    *cp;
858          int     i;
859  
# Line 757 | Line 862 | getinp(char *buf, FILE *fp)
862                  cp = buf;               /* make sure we get 6 floats */
863                  for (i = 0; i < 6; i++) {
864                          if (fgetword(cp, buf+127-cp, fp) == NULL)
865 <                                return 0;
865 >                                return -1;
866 >                        if (i >= 3)
867 >                                dv[i-3] = atof(cp);
868                          if ((cp = fskip(cp)) == NULL || *cp)
869 <                                return 0;
869 >                                return -1;
870                          *cp++ = ' ';
871                  }
872                  getc(fp);               /* get/put eol */
873                  *cp-- = '\0'; *cp = '\n';
874 +                if (DOT(dv,dv) <= FTINY*FTINY)
875 +                        return 0;       /* dummy ray */
876                  return strlen(buf);
877          case 'f':
878                  if (fread(buf, sizeof(float), 6, fp) < 6)
879 <                        return 0;
879 >                        return -1;
880 >                fvp = (float *)buf + 3;
881 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
882 >                        return 0;       /* dummy ray */
883                  return sizeof(float)*6;
884          case 'd':
885                  if (fread(buf, sizeof(double), 6, fp) < 6)
886 <                        return 0;
886 >                        return -1;
887 >                dvp = (double *)buf + 3;
888 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
889 >                        return 0;       /* dummy ray */
890                  return sizeof(double)*6;
891          }
892          error(INTERNAL, "botched input format");
893 <        return 0;       /* pro forma return */
893 >        return -1;      /* pro forma return */
894   }
895  
896   static float    rparams[9];             /* traced ray parameters */
# Line 839 | Line 954 | put_contrib(const DCOLOR cnt, FILE *fout)
954                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
955                  break;
956          case 'f':
957 <                fv[0] = cnt[0];
843 <                fv[1] = cnt[1];
844 <                fv[2] = cnt[2];
957 >                copycolor(fv, cnt);
958                  fwrite(fv, sizeof(float), 3, fout);
959                  break;
960          case 'd':
# Line 856 | Line 969 | put_contrib(const DCOLOR cnt, FILE *fout)
969          }
970   }
971  
972 < /* output ray tallies and clear for next primary */
972 > /* output ray tallies and clear for next accumulation */
973   void
974 < done_contrib(void)
974 > done_contrib(int navg)
975   {
976 +        double          sf = 1.;
977          int             i, j;
978          MODCONT         *mp;
979          STREAMOUT       *sop;
980 +                                                /* set average scaling */
981 +        if (navg > 1)
982 +                sf = 1. / (double)navg;
983                                                  /* output modifiers in order */
984          for (i = 0; i < nmods; i++) {
985                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
986 +                if (navg > 1)                   /* average scaling */
987 +                        for (j = mp->nbins; j--; )
988 +                                scalecolor(mp->cbin[j], sf);
989                  sop = getostream(mp->outspec, mp->modname, 0,0);
990                  put_contrib(mp->cbin[0], sop->ofp);
991                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 876 | Line 996 | done_contrib(void)
996                          for (j = 1; j < mp->nbins; j++)
997                                  put_contrib(mp->cbin[j],
998                                      getostream(mp->outspec,mp->modname,j,0)->ofp);
999 <                                                /* clear for next ray tree */
999 >                                                /* clear for next time */
1000                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1001          }
1002          --waitflush;                            /* terminate records */
# Line 884 | Line 1004 | done_contrib(void)
1004          if (using_stdout & (outfmt == 'a'))
1005                  putc('\n', stdout);
1006          if (!waitflush) {
1007 <                waitflush = xres;
1007 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
1008                  if (using_stdout)
1009                          fflush(stdout);
1010          }
# Line 935 | Line 1055 | process_queue(void)
1055                          if (!n || !(isalpha(*cp) | (*cp == '_')))
1056                                  error(USER, "bad modifier name from rtrace");
1057                                          /* get modifier name */
1058 <                        while (n > 0 && *cp != '\t') {
1058 >                        while (n > 1 && *cp != '\t') {
1059 >                                if (mnp - modname >= sizeof(modname)-2)
1060 >                                        error(INTERNAL, "modifier name too long");
1061                                  *mnp++ = *cp++; n--;
1062                          }
1063                          *mnp = '\0';
# Line 947 | Line 1069 | process_queue(void)
1069                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1070                          add_contrib(modname);
1071                  }
1072 <                done_contrib();         /* sum up contributions & output */
1072 >                                        /* time to produce record? */
1073 >                if (account > 0 && !--account)
1074 >                        done_contrib(account = accumulate);
1075                  lastdone = rtp->raynum;
1076 <                free(rtp->buf);         /* free up buffer space */
1076 >                if (rtp->buf != NULL)   /* free up buffer space */
1077 >                        free(rtp->buf);
1078                  rt_unproc = rtp->next;
1079                  free(rtp);              /* done with this ray tree */
1080          }
# Line 961 | Line 1086 | wait_rproc(void)
1086   {
1087          struct rtproc   *rtfree = NULL;
1088          fd_set          readset, errset;
1089 <        int             nr;
1089 >        ssize_t         nr;
1090          struct rtproc   *rt;
1091          int             n;
1092          
# Line 992 | Line 1117 | wait_rproc(void)
1117                                  continue;
1118                          if (rt->buf == NULL) {
1119                                  rt->bsiz = treebufsiz;
1120 <                                rt->buf = (char *)malloc(treebufsiz);
1120 >                                rt->buf = (char *)malloc(rt->bsiz);
1121                          } else if (rt->nbr + BUFSIZ > rt->bsiz) {
1122                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1123                                          rt->bsiz = treebufsiz;
1124 <                                else
1125 <                                        treebufsiz = rt->bsiz += BUFSIZ;
1124 >                                else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0)
1125 >                                        error(INTERNAL,
1126 >                                            "ray buffer does not fit memory");
1127                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1128                          }
1129                          if (rt->buf == NULL)
1130                                  error(SYSTEM, "out of memory in wait_rproc");
1131 <                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
1132 <                        if (nr <= 0)
1131 >                        nr = rt->bsiz - rt->nbr;
1132 >                        if (nr & ~0x7fffffff)   /* avoid 32-bit OS issues */
1133 >                                nr = 0x7fffffff;
1134 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, nr);
1135 >                        if (nr < 0)
1136 >                                error(SYSTEM, "read error from rtrace");
1137 >                        if (!nr)
1138                                  error(USER, "rtrace process died");
1139                          rt->nbr += nr;          /* advance & check */
1140 <                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
1141 <                                                        "~\t~\t", 4)) {
1142 <                                rt->nbr -= 4;   /* elide terminator */
1140 >                        if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6,
1141 >                                                        "~\t~\t~\t", 6)) {
1142 >                                rt->nbr -= 6;   /* elide terminator */
1143                                  queue_raytree(rt);
1144                                  rtfree = rt;    /* ready for next ray */
1145                          }
# Line 1033 | Line 1164 | get_rproc(void)
1164   void
1165   trace_contribs(FILE *fin)
1166   {
1167 +        static int      ignore_warning_given = 0;
1168          char            inpbuf[128];
1169          int             iblen;
1170          struct rtproc   *rtp;
1171                                                  /* loop over input */
1172 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1173 <                if (lastray+1 < lastray ||      /* need reset? */
1174 <                                queue_length() > 5*nrtprocs()) {
1172 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1173 >                if (!iblen && accumulate != 1) {
1174 >                        if (!ignore_warning_given++)
1175 >                                error(WARNING,
1176 >                                "dummy ray(s) ignored during accumulation\n");
1177 >                        continue;
1178 >                }
1179 >                if (!iblen ||                   /* need flush/reset? */
1180 >                                queue_length() > 10*nrtprocs() ||
1181 >                                lastray+1 < lastray) {
1182                          while (wait_rproc() != NULL)
1183                                  process_queue();
1184                          lastdone = lastray = 0;
1185                  }
1186                  rtp = get_rproc();              /* get avail. rtrace process */
1187 <                rtp->raynum = ++lastray;        /* assign ray to it */
1188 <                writebuf(rtp->pd.w, inpbuf, iblen);
1189 <                if (raysleft && !--raysleft)
1190 <                        break;
1187 >                rtp->raynum = ++lastray;        /* assign ray */
1188 >                if (iblen) {                    /* trace ray if valid */
1189 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1190 >                } else {                        /* else bypass dummy ray */
1191 >                        queue_raytree(rtp);     /* queue empty ray/record */
1192 >                        if ((yres <= 0) | (xres <= 0))
1193 >                                waitflush = 1;  /* flush right after */
1194 >                }
1195                  process_queue();                /* catch up with results */
1196 +                if (raysleft && !--raysleft)
1197 +                        break;                  /* preemptive EOI */
1198          }
1199          while (wait_rproc() != NULL)            /* process outstanding rays */
1200                  process_queue();
1201 +        if (accumulate <= 0)
1202 +                done_contrib(0);                /* output tallies */
1203 +        else if (account < accumulate) {
1204 +                error(WARNING, "partial accumulation in final record");
1205 +                done_contrib(accumulate - account);
1206 +        }
1207          if (raysleft)
1208                  error(USER, "unexpected EOF on input");
1209          lu_done(&ofiletab);                     /* close output files */
1210   }
1211  
1212 + /* get ray contribution from previous file */
1213 + static int
1214 + get_contrib(DCOLOR cnt, FILE *finp)
1215 + {
1216 +        COLOR   fv;
1217 +        COLR    cv;
1218 +
1219 +        switch (outfmt) {
1220 +        case 'a':
1221 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1222 +        case 'f':
1223 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1224 +                        return(0);
1225 +                copycolor(cnt, fv);
1226 +                return(1);
1227 +        case 'd':
1228 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1229 +        case 'c':
1230 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1231 +                        return(0);
1232 +                colr_color(fv, cv);
1233 +                copycolor(cnt, fv);
1234 +                return(1);
1235 +        default:
1236 +                error(INTERNAL, "botched output format");
1237 +        }
1238 +        return(0);      /* pro forma return */
1239 + }
1240 +
1241 + /* close output file opened for input */
1242 + static int
1243 + myclose(const LUENT *e, void *p)
1244 + {
1245 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1246 +        
1247 +        if (sop->ofp == NULL)
1248 +                return(0);
1249 +        fclose(sop->ofp);
1250 +        sop->ofp = NULL;
1251 +        return(0);
1252 + }
1253 +
1254 + /* load previously accumulated values */
1255 + void
1256 + reload_output(void)
1257 + {
1258 +        int             i, j;
1259 +        MODCONT         *mp;
1260 +        int             ofl;
1261 +        char            oname[1024];
1262 +        char            *fmode = "rb";
1263 +        char            *outvfmt;
1264 +        LUENT           *ment, *oent;
1265 +        int             xr, yr;
1266 +        STREAMOUT       sout;
1267 +        DCOLOR          rgbv;
1268 +
1269 +        switch (outfmt) {
1270 +        case 'a':
1271 +                outvfmt = "ascii";
1272 +                fmode = "r";
1273 +                break;
1274 +        case 'f':
1275 +                outvfmt = "float";
1276 +                break;
1277 +        case 'd':
1278 +                outvfmt = "double";
1279 +                break;
1280 +        case 'c':
1281 +                outvfmt = COLRFMT;
1282 +                break;
1283 +        default:
1284 +                error(INTERNAL, "botched output format");
1285 +                return;
1286 +        }
1287 +                                                /* reload modifier values */
1288 +        for (i = 0; i < nmods; i++) {
1289 +                ment = lu_find(&modconttab,modname[i]);
1290 +                mp = (MODCONT *)ment->data;
1291 +                if (mp->outspec == NULL)
1292 +                        error(USER, "cannot reload from stdout");
1293 +                if (mp->outspec[0] == '!')
1294 +                        error(USER, "cannot reload from command");
1295 +                for (j = 0; ; j++) {            /* load each modifier bin */
1296 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1297 +                        if (ofl < 0)
1298 +                                error(USER, "bad output file specification");
1299 +                        oent = lu_find(&ofiletab, oname);
1300 +                        if (oent->data != NULL) {
1301 +                                sout = *(STREAMOUT *)oent->data;
1302 +                        } else {
1303 +                                sout.reclen = 0;
1304 +                                sout.outpipe = 0;
1305 +                                sout.xr = xres; sout.yr = yres;
1306 +                                sout.ofp = NULL;
1307 +                        }
1308 +                        if (sout.ofp == NULL) { /* open output as input */
1309 +                                sout.ofp = fopen(oname, fmode);
1310 +                                if (sout.ofp == NULL) {
1311 +                                        if (j)
1312 +                                                break;  /* assume end of modifier */
1313 +                                        sprintf(errmsg, "missing reload file '%s'",
1314 +                                                        oname);
1315 +                                        error(WARNING, errmsg);
1316 +                                        break;
1317 +                                }
1318 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1319 +                                        sprintf(errmsg, "format mismatch for '%s'",
1320 +                                                        oname);
1321 +                                        error(USER, errmsg);
1322 +                                }
1323 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1324 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1325 +                                                        (xr != sout.xr) |
1326 +                                                        (yr != sout.yr))) {
1327 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1328 +                                                        oname);
1329 +                                        error(USER, errmsg);
1330 +                                }
1331 +                        }
1332 +                                                        /* read in RGB value */
1333 +                        if (!get_contrib(rgbv, sout.ofp)) {
1334 +                                if (!j) {
1335 +                                        fclose(sout.ofp);
1336 +                                        break;          /* ignore empty file */
1337 +                                }
1338 +                                if (j < mp->nbins) {
1339 +                                        sprintf(errmsg, "missing data in '%s'",
1340 +                                                        oname);
1341 +                                        error(USER, errmsg);
1342 +                                }
1343 +                                break;
1344 +                        }
1345 +                        if (j >= mp->nbins)             /* grow modifier size */
1346 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1347 +                        copycolor(mp->cbin[j], rgbv);
1348 +                        if (oent->key == NULL)          /* new file entry */
1349 +                                oent->key = strcpy((char *)
1350 +                                                malloc(strlen(oname)+1), oname);
1351 +                        if (oent->data == NULL)
1352 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1353 +                        *(STREAMOUT *)oent->data = sout;
1354 +                }
1355 +        }
1356 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1357 + }
1358 +
1359   /* seek on the given output file */
1360   static int
1361   myseeko(const LUENT *e, void *p)
# Line 1071 | Line 1369 | myseeko(const LUENT *e, void *p)
1369                  sprintf(errmsg, "seek error on file '%s'", e->key);
1370                  error(SYSTEM, errmsg);
1371          }
1372 +        return 0;
1373   }
1374  
1375   /* recover output if possible */
# Line 1126 | Line 1425 | recover_output(FILE *fin)
1425                                  sout = *(STREAMOUT *)oent->data;
1426                          } else {
1427                                  sout.reclen = 0;
1428 +                                sout.outpipe = 0;
1429                                  sout.ofp = NULL;
1430                          }
1431                          if (sout.ofp != NULL) { /* already open? */
# Line 1140 | Line 1440 | recover_output(FILE *fin)
1440                                          break;  /* assume end of modifier */
1441                                  sprintf(errmsg, "missing recover file '%s'",
1442                                                  oname);
1443 <                                error(USER, errmsg);
1443 >                                error(WARNING, errmsg);
1444 >                                break;
1445                          }
1446                          nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1447                          if (nvals <= 0) {
# Line 1148 | Line 1449 | recover_output(FILE *fin)
1449                                  fclose(sout.ofp);
1450                                  break;
1451                          }
1452 <                        if (sout.reclen == CNT_UNKNOWN) {
1452 >                        if (!sout.reclen) {
1453                                  if (!(ofl & OF_BIN)) {
1454                                          sprintf(errmsg,
1455                                                  "need -bn to recover file '%s'",
# Line 1165 | Line 1466 | recover_output(FILE *fin)
1466                                                  oname);
1467                                  error(USER, errmsg);
1468                          }
1469 <                        if ((xres > 0) & (yres > 0) &&
1469 >                        sout.xr = xres; sout.yr = yres;
1470 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1471                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1472 <                                                xr != xres ||
1473 <                                                yr != yres)) {
1472 >                                                (xr != sout.xr) |
1473 >                                                (yr != sout.yr))) {
1474                                  sprintf(errmsg, "resolution mismatch for '%s'",
1475                                                  oname);
1476                                  error(USER, errmsg);
# Line 1197 | Line 1499 | recover_output(FILE *fin)
1499                  error(WARNING, "no output files to recover");
1500                  return;
1501          }
1502 <        if (raysleft && lastout >= raysleft) {
1502 >        if (raysleft && lastout >= raysleft/accumulate) {
1503                  error(WARNING, "output appears to be complete");
1504                  /* XXX should read & discard input? */
1505                  quit(0);
# Line 1207 | Line 1509 | recover_output(FILE *fin)
1509          lu_doall(&ofiletab, myseeko, &nvals);
1510                                                  /* skip repeated input */
1511          for (nvals = 0; nvals < lastout; nvals++)
1512 <                if (getinp(oname, fin) <= 0)
1512 >                if (getinp(oname, fin) < 0)
1513                          error(USER, "unexpected EOF on input");
1514 <        lastray = lastdone = (unsigned long)lastout;
1514 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1515          if (raysleft)
1516                  raysleft -= lastray;
1517   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines