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.1 by greg, Wed May 25 04:45:22 2005 UTC vs.
Revision 1.27 by greg, Tue Sep 20 17:05:11 2005 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * Gather rtrace output to compute contributions from particular sources
6   */
7  
8 + #include  "standard.h"
9   #include  <ctype.h>
10 < #include  "rtio.h"
10 < #include  "rterror.h"
10 > #include  <signal.h>
11   #include  "platform.h"
12   #include  "rtprocess.h"
13   #include  "selcall.h"
# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include  "lookup.h"
17   #include  "calcomp.h"
18  
19 < #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
20 < #undef getc
21 < #undef putc
22 < #define getc    getc_unlocked
23 < #define putc    putc_unlocked
24 < #define ferror  ferror_unlocked
19 > #ifndef MAXMODLIST
20 > #define MAXMODLIST      1024            /* maximum modifiers we'll track */
21   #endif
22  
27 #define MAXMODLIST      1024            /* maximum modifiers we'll track */
28
23   int     treebufsiz = BUFSIZ;            /* current tree buffer size */
24  
25 + typedef double  DCOLOR[3];              /* double-precision color */
26 +
27   /*
28   * The modcont structure is used to accumulate ray contributions
29   * for a particular modifier, which may be subdivided into bins
# Line 43 | Line 39 | typedef struct {
39          const char      *modname;       /* modifier name */
40          EPNODE          *binv;          /* bin value expression */
41          int             nbins;          /* number of accumulation bins */
42 <        COLOR           cbin[1];        /* contribution bins (extends struct) */
42 >        DCOLOR          cbin[1];        /* contribution bins (extends struct) */
43   } MODCONT;                      /* modifier contribution */
44  
45   static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); }
# Line 55 | Line 51 | static void closefile(void *p) { fclose((FILE *)p); }
51  
52   LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
53  
54 + #define OF_MODIFIER     01
55 + #define OF_BIN          02
56 +
57   FILE *getofile(const char *ospec, const char *mname, int bn);
58 + int ofname(char *oname, const char *ospec, const char *mname, int bn);
59 + void printheader(FILE *fout, const char *info);
60  
61   /*
62   * The rcont structure is used to manage i/o with a particular
# Line 66 | Line 67 | FILE *getofile(const char *ospec, const char *mname, i
67   struct rtproc {
68          struct rtproc   *next;          /* next in list of processes */
69          SUBPROC         pd;             /* rtrace pipe descriptors */
70 <        unsigned long   raynum;         /* ray number for this buffer */
71 <        int             bsiz;           /* rtrace buffer length */
72 <        char            *buf;           /* rtrace i/o buffer */
73 <        char            *bpos;          /* current buffer position */
74 < };                              /* rtrace process */
70 >        unsigned long   raynum;         /* ray number for this tree */
71 >        int             bsiz;           /* ray tree buffer length */
72 >        char            *buf;           /* ray tree buffer */
73 >        int             nbr;            /* number of bytes from rtrace */
74 > };                              /* rtrace process buffer */
75  
76                                          /* rtrace command and defaults */
77 < char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
78 <                                "-ab", "1", "-ad", "512", };
77 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
78 >                                "-dj", ".5", "-dr", "3",
79 >                                "-ab", "1", "-ad", "128", };
80   int  rtargc = 9;
81                                          /* overriding rtrace options */
82 < char            *myrtopts[] = { "-o-TmWdp", "-h-",
83 <                                "-x", "1", "-y", "0", NULL };
82 > char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
83 >                                "-dt", "0", "-as", "0", "-aa", "0", NULL };
84  
85   struct rtproc   rt0;                    /* head of rtrace process list */
86  
87   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
88  
89   char    persistfn[] = "pfXXXXXX";       /* persist file name */
88 char    fmt[8];                         /* rtrace i/o format */
90  
91   int             gargc;                  /* global argc */
92   char            **gargv;                /* global argv */
# Line 100 | Line 101 | int            header = 1;             /* output header? */
101   int             xres = 0;               /* horiz. output resolution */
102   int             yres = 0;               /* vert. output resolution */
103  
104 < long            raysleft;               /* number of rays left to trace */
104 > unsigned long   raysleft;               /* number of rays left to trace */
105   long            waitflush;              /* how long until next flush */
106  
107   unsigned long   lastray = 0;            /* last ray number sent */
108   unsigned long   lastdone = 0;           /* last ray processed */
109  
110 + int             using_stdout = 0;       /* are we using stdout? */
111 +
112   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
113   int             nmods = 0;              /* number of modifiers */
114  
115   MODCONT *addmodifier(char *modn, char *outf, char *binv);
116 + void addmodfile(char *fname, char *outf, char *binv);
117  
114 int done_rprocs(struct rtproc *rtp);
118   void init(int np);
119 < void tracecontribs(FILE *fp);
119 > int done_rprocs(struct rtproc *rtp);
120 > void recover_output(FILE *fin);
121 > void trace_contribs(FILE *fin);
122   struct rtproc *wait_rproc(void);
123   struct rtproc *get_rproc(void);
124 < void process_rays(struct rtproc *rtp);
124 > void queue_raytree(struct rtproc *rtp);
125 > void process_queue(void);
126  
127 < void add_contrib(const char *modn, float rayval[7]);
127 > void put_contrib(const DCOLOR cnt, FILE *fout);
128 > void add_contrib(const char *modn);
129   void done_contrib(void);
130  
131   /* set input/output format */
# Line 126 | Line 133 | static void
133   setformat(const char *fmt)
134   {
135          switch (fmt[0]) {
129        case 'a':
136          case 'f':
137          case 'd':
138 +                SET_FILE_BINARY(stdin);
139 +                /* fall through */
140 +        case 'a':
141                  inpfmt = fmt[0];
142                  break;
143          default:
# Line 159 | Line 168 | int
168   main(int argc, char *argv[])
169   {
170          int     nprocs = 1;
171 +        int     recover = 0;
172          char    *curout = NULL;
173          char    *binval = NULL;
174 <        int     i;
175 <                                /* set global argument list */
176 <        gargc = argc;
174 >        char    fmt[8];
175 >        int     i, j;
176 >                                /* need at least one argument */
177 >        if (argc < 2) {
178 >                fprintf(stderr,
179 > "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
180 >                        argv[0]);
181 >                exit(1);
182 >        }
183 >                                /* global program name */
184          gargv = argv;
185 <                                /* set up calcomp functions */
185 >                                /* initialize calcomp routines */
186          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
187          esupport &= ~(E_OUTCHAN);
188 +        varset("PI", ':', PI);
189                                  /* get our options */
190 <        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
191 <                switch (argv[i][1]) {
192 <                case 'n':                       /* number of processes */
193 <                        if (argv[i][2] || i >= argc-1) break;
194 <                        nprocs = atoi(argv[++i]);
195 <                        if (nprocs <= 0)
196 <                                error(USER, "illegal number of processes");
197 <                        continue;
198 <                case 'h':                       /* output header? */
199 <                        switch (argv[i][2]) {
200 <                        case '\0':
201 <                                header = !header;
190 >        for (i = 1; i < argc-1; i++) {
191 >                                                /* expand arguments */
192 >                while ((j = expandarg(&argc, &argv, i)) > 0)
193 >                        ;
194 >                if (j < 0) {
195 >                        fprintf(stderr, "%s: cannot expand '%s'",
196 >                                        argv[0], argv[i]);
197 >                        exit(1);
198 >                }
199 >                if (argv[i][0] == '-')
200 >                        switch (argv[i][1]) {
201 >                        case 'r':               /* recover output */
202 >                                if (argv[i][2]) break;
203 >                                recover++;
204                                  continue;
205 <                        case '+': case '1': case 'T': case 't':
206 <                                header = 1;
205 >                        case 'n':               /* number of processes */
206 >                                if (argv[i][2] || i >= argc-2) break;
207 >                                nprocs = atoi(argv[++i]);
208 >                                if (nprocs <= 0)
209 >                                        error(USER, "illegal number of processes");
210                                  continue;
211 <                        case '-': case '0': case 'F': case 'f':
212 <                                header = 0;
211 >                        case 'h':               /* output header? */
212 >                                switch (argv[i][2]) {
213 >                                case '\0':
214 >                                        header = !header;
215 >                                        continue;
216 >                                case '+': case '1':
217 >                                case 'T': case 't':
218 >                                case 'Y': case 'y':
219 >                                        header = 1;
220 >                                        continue;
221 >                                case '-': case '0':
222 >                                case 'F': case 'f':
223 >                                case 'N': case 'n':
224 >                                        header = 0;
225 >                                        continue;
226 >                                }
227 >                                break;
228 >                        case 'f':               /* file or i/o format */
229 >                                if (!argv[i][2]) {
230 >                                        char    *fpath;
231 >                                        if (i >= argc-2) break;
232 >                                        fpath = getpath(argv[++i],
233 >                                                        getrlibpath(), R_OK);
234 >                                        if (fpath == NULL) {
235 >                                                sprintf(errmsg,
236 >                                                        "cannot find file '%s'",
237 >                                                                argv[i]);
238 >                                                error(USER, errmsg);
239 >                                        }
240 >                                        fcompile(fpath);
241 >                                        continue;
242 >                                }
243 >                                setformat(argv[i]+2);
244                                  continue;
245 <                        }
246 <                        break;
247 <                case 'f':                       /* file or i/o format */
194 <                        if (!argv[i][2]) {
195 <                                if (i >= argc-1) break;
196 <                                fcompile(argv[++i]);
245 >                        case 'e':               /* expression */
246 >                                if (argv[i][2] || i >= argc-2) break;
247 >                                scompile(argv[++i], NULL, 0);
248                                  continue;
249 +                        case 'o':               /* output file spec. */
250 +                                if (argv[i][2] || i >= argc-2) break;
251 +                                curout = argv[++i];
252 +                                continue;
253 +                        case 'x':               /* horiz. output resolution */
254 +                                if (argv[i][2] || i >= argc-2) break;
255 +                                xres = atoi(argv[++i]);
256 +                                continue;
257 +                        case 'y':               /* vert. output resolution */
258 +                                if (argv[i][2] || i >= argc-2) break;
259 +                                yres = atoi(argv[++i]);
260 +                                continue;
261 +                        case 'b':               /* bin expression */
262 +                                if (argv[i][2] || i >= argc-2) break;
263 +                                binval = argv[++i];
264 +                                continue;
265 +                        case 'm':               /* modifier name */
266 +                                if (argv[i][2] || i >= argc-2) break;
267 +                                rtargv[rtargc++] = "-ti";
268 +                                rtargv[rtargc++] = argv[++i];
269 +                                addmodifier(argv[i], curout, binval);
270 +                                continue;
271 +                        case 'M':               /* modifier file */
272 +                                if (argv[i][2] || i >= argc-2) break;
273 +                                rtargv[rtargc++] = "-tI";
274 +                                rtargv[rtargc++] = argv[++i];
275 +                                addmodfile(argv[i], curout, binval);
276 +                                continue;
277                          }
278 <                        setformat(argv[i]+2);
200 <                        continue;
201 <                case 'e':                       /* expression */
202 <                        if (argv[i][2] || i >= argc-1) break;
203 <                        scompile(argv[++i], NULL, 0);
204 <                        continue;
205 <                case 'o':                       /* output file spec. */
206 <                        if (argv[i][2] || i >= argc-1) break;
207 <                        curout = argv[++i];
208 <                        continue;
209 <                case 'x':                       /* horiz. output resolution */
210 <                        if (argv[i][2] || i >= argc-1) break;
211 <                        xres = atoi(argv[++i]);
212 <                        continue;
213 <                case 'y':                       /* vert. output resolution */
214 <                        if (argv[i][2] || i >= argc-1) break;
215 <                        yres = atoi(argv[++i]);
216 <                        continue;
217 <                case 'l':                       /* limit distance? */
218 <                        if (argv[i][2] != 'd') break;
219 <                        rtargv[rtargc++] = argv[i];
220 <                        continue;
221 <                case 'b':                       /* bin expression */
222 <                        if (argv[i][2] || i >= argc-1) break;
223 <                        binval = argv[++i];
224 <                        continue;
225 <                case 'm':                       /* modifier name */
226 <                        if (argv[i][2] || i >= argc-1) break;
227 <                        rtargv[rtargc++] = "-ti";
228 <                        rtargv[rtargc++] = argv[++i];
229 <                        addmodifier(argv[i], curout, binval);
230 <                        continue;
231 <                }
232 <                break;          /* break into rtrace options */
278 >                rtargv[rtargc++] = argv[i];     /* assume rtrace option */
279          }
280 <        for ( ; i < argc; i++)  /* transfer rtrace-specific options */
281 <                rtargv[rtargc++] = argv[i];
236 <        if (!strcmp(rtargv[--rtargc], "-defaults"))
237 <                nprocs = 0;
238 <        if (nprocs > 1) {       /* add persist file if parallel invocation */
239 <                rtargv[rtargc++] = "-PP";
240 <                rtargv[rtargc++] = mktemp(persistfn);
241 <        }
280 >                                /* set global argument list */
281 >        gargc = argc; gargv = argv;
282                                  /* add "mandatory" rtrace settings */
283 <        for (i = 0; myrtopts[i] != NULL; i++)
284 <                rtargv[rtargc++] = myrtopts[i];
285 <        if (!nprocs) {          /* just asking for defaults? */
283 >        for (j = 0; myrtopts[j] != NULL; j++)
284 >                rtargv[rtargc++] = myrtopts[j];
285 >                                /* just asking for defaults? */
286 >        if (!strcmp(argv[i], "-defaults")) {
287                  char    sxres[16], syres[16];
288                  char    *rtpath;
289 <                printf("-n  1\t\t\t\t# number of processes\n", nprocs);
289 >                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
290                  fflush(stdout);                 /* report OUR options */
291                  rtargv[rtargc++] = header ? "-h+" : "-h-";
292                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 256 | Line 297 | main(int argc, char *argv[])
297                  rtargv[rtargc++] = "-y";
298                  sprintf(syres, "%d", yres);
299                  rtargv[rtargc++] = syres;
300 <                rtargv[rtargc++] = "-oW";
300 >                rtargv[rtargc++] = "-oTW";
301                  rtargv[rtargc++] = "-defaults";
302                  rtargv[rtargc] = NULL;
303                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 269 | Line 310 | main(int argc, char *argv[])
310                  perror(rtpath); /* execv() should not return */
311                  exit(1);
312          }
313 <        if (!nmods)
314 <                error(USER, "No modifiers specified");
315 <        if (argc < 2 || argv[argc-1][0] == '-')
316 <                error(USER, "missing octree argument");
313 >        if (nprocs > 1) {       /* add persist file if parallel */
314 >                rtargv[rtargc++] = "-PP";
315 >                rtargv[rtargc++] = mktemp(persistfn);
316 >        }
317                                  /* add format string */
318          sprintf(fmt, "-f%cf", inpfmt);
319          rtargv[rtargc++] = fmt;
320                                  /* octree argument is last */
321 <        rtargv[rtargc++] = octree = argv[argc-1];
321 >        if (i <= 0 || i != argc-1 || argv[i][0] == '-')
322 >                error(USER, "missing octree argument");
323 >        rtargv[rtargc++] = octree = argv[i];
324          rtargv[rtargc] = NULL;
325 <                                /* start rtrace & sum contributions */
325 >                                /* start rtrace */
326          init(nprocs);
327 <        tracecontribs(stdin);
327 >        if (recover)            /* perform recovery if requested */
328 >                recover_output(stdin);
329 >        trace_contribs(stdin);  /* compute contributions */
330          quit(0);
331   }
332  
333 + #ifndef SIGALRM
334 + #define SIGALRM SIGTERM
335 + #endif
336   /* kill persistent rtrace process */
337   static void
338   killpersist(void)
339   {
340          FILE    *fp = fopen(persistfn, "r");
341 <        int     pid;
341 >        RT_PID  pid;
342  
343          if (fp == NULL)
344                  return;
# Line 333 | Line 381 | quit(int status)
381          exit(status);                   /* flushes all output streams */
382   }
383  
384 < /* start rtrace and initialize buffers */
384 > /* start rtrace processes and initialize */
385   void
386   init(int np)
387   {
388          struct rtproc   *rtp;
389          int     i;
390          int     maxbytes;
391 +                                        /* make sure we have something to do */
392 +        if (!nmods)
393 +                error(USER, "No modifiers specified");
394                                          /* assign ray variables */
395          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
396          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
397                                          /* set up signal handling */
398 < #ifdef SIGPIPE /* not present on Windows */
398 >        signal(SIGINT, quit);
399 > #ifdef SIGHUP
400 >        signal(SIGHUP, quit);
401 > #endif
402 > #ifdef SIGTERM
403 >        signal(SIGTERM, quit);
404 > #endif
405 > #ifdef SIGPIPE
406          signal(SIGPIPE, quit);
407   #endif
408          rtp = &rt0;                     /* start rtrace process(es) */
# Line 360 | Line 418 | init(int np)
418                          error(SYSTEM, "cannot start rtrace process");
419                  if (maxbytes > treebufsiz)
420                          treebufsiz = maxbytes;
421 +                rtp->raynum = 0;
422                  rtp->bsiz = 0;
423                  rtp->buf = NULL;
424 <                rtp->raynum = 0;
424 >                rtp->nbr = 0;
425                  if (i == np)            /* last process? */
426                          break;
427                  if (i == 1)
# Line 375 | Line 434 | init(int np)
434          rtp->next = NULL;               /* terminate list */
435          if (yres > 0) {
436                  if (xres > 0)
437 <                        raysleft = xres*yres;
437 >                        raysleft = (unsigned long)xres*yres;
438                  else
439                          raysleft = yres;
440          } else
# Line 413 | Line 472 | addmodifier(char *modn, char *outf, char *binv)
472          return mp;
473   }
474  
475 + /* add modifiers from a file list */
476 + void
477 + addmodfile(char *fname, char *outf, char *binv)
478 + {
479 +        char    *mname[MAXMODLIST];
480 +        int     i;
481 +                                        /* find the file & store strings */
482 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
483 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
484 +                error(SYSTEM, errmsg);
485 +        }
486 +        for (i = 0; mname[i]; i++)      /* add each one */
487 +                addmodifier(mname[i], outf, binv);
488 + }
489 +
490   /* put string to stderr */
491   void
492   eputs(char  *s)
# Line 428 | Line 502 | eputs(char  *s)
502          midline = s[strlen(s)-1] != '\n';
503   }
504  
505 + /* construct output file name and return flags whether modifier/bin present */
506 + int
507 + ofname(char *oname, const char *ospec, const char *mname, int bn)
508 + {
509 +        const char      *mnp = NULL;
510 +        const char      *bnp = NULL;
511 +        const char      *cp;
512 +        
513 +        if (ospec == NULL)
514 +                return -1;
515 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
516 +                if (*cp == '%') {
517 +                        do
518 +                                ++cp;
519 +                        while (isdigit(*cp));
520 +                        switch (*cp) {
521 +                        case '%':
522 +                                break;
523 +                        case 's':
524 +                                if (mnp != NULL)
525 +                                        return -1;
526 +                                mnp = cp;
527 +                                break;
528 +                        case 'd':
529 +                                if (bnp != NULL)
530 +                                        return -1;
531 +                                bnp = cp;
532 +                                break;
533 +                        default:
534 +                                return -1;
535 +                        }
536 +                }
537 +        if (mnp != NULL) {                      /* create file name */
538 +                if (bnp != NULL) {
539 +                        if (bnp > mnp)
540 +                                sprintf(oname, ospec, mname, bn);
541 +                        else
542 +                                sprintf(oname, ospec, bn, mname);
543 +                        return OF_MODIFIER|OF_BIN;
544 +                }
545 +                sprintf(oname, ospec, mname);
546 +                return OF_MODIFIER;
547 +        }
548 +        if (bnp != NULL) {
549 +                sprintf(oname, ospec, bn);
550 +                return OF_BIN;
551 +        }
552 +        strcpy(oname, ospec);
553 +        return 0;
554 + }
555 +
556   /* write header to the given output stream */
557   void
558 < printheader(FILE *fout)
558 > printheader(FILE *fout, const char *info)
559   {
560          extern char     VersionID[];
561          FILE            *fin = fopen(octree, "r");
562          
563          if (fin == NULL)
564                  quit(1);
565 <        getheader(fin, (gethfunc *)fputs, fout);        /* copy octree header */
565 >        checkheader(fin, "ignore", fout);       /* copy octree header */
566          fclose(fin);
567          printargs(gargc-1, gargv, fout);        /* add our command */
568          fprintf(fout, "SOFTWARE= %s\n", VersionID);
569          fputnow(fout);
570 +        if (info != NULL)                       /* add extra info if given */
571 +                fputs(info, fout);
572          switch (outfmt) {                       /* add output format */
573          case 'a':
574                  fputformat("ascii", fout);
# Line 468 | Line 595 | printheader(FILE *fout)
595   FILE *
596   getofile(const char *ospec, const char *mname, int bn)
597   {
598 <        static int      using_stdout = 0;
599 <        const char      *mnp = NULL;
473 <        const char      *bnp = NULL;
474 <        const char      *cp;
475 <        char            ofname[1024];
598 >        int             ofl;
599 >        char            oname[1024];
600          LUENT           *lep;
601          
602          if (ospec == NULL) {                    /* use stdout? */
603 <                if (!using_stdout && header)
604 <                        printheader(stdout);
603 >                if (!using_stdout) {
604 >                        if (outfmt != 'a')
605 >                                SET_FILE_BINARY(stdout);
606 >                        if (header)
607 >                                printheader(stdout, NULL);
608 >                }
609                  using_stdout = 1;
610                  return stdout;
611          }
612 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
613 <                if (*cp == '%') {
614 <                        do
615 <                                ++cp;
616 <                        while (isdigit(*cp));
617 <                        switch (*cp) {
490 <                        case '%':
491 <                                break;
492 <                        case 's':
493 <                                if (mnp != NULL)
494 <                                        goto badspec;
495 <                                mnp = cp;
496 <                                break;
497 <                        case 'd':
498 <                                if (bnp != NULL)
499 <                                        goto badspec;
500 <                                bnp = cp;
501 <                                break;
502 <                        default:
503 <                                goto badspec;
504 <                        }
505 <                }
506 <        if (mnp != NULL) {                      /* create file name */
507 <                if (bnp != NULL) {
508 <                        if (bnp > mnp)
509 <                                sprintf(ofname, ospec, mname, bn);
510 <                        else
511 <                                sprintf(ofname, ospec, bn, mname);
512 <                } else
513 <                        sprintf(ofname, ospec, mname);
514 <        } else if (bnp != NULL)
515 <                sprintf(ofname, ospec, bn);
516 <        else
517 <                strcpy(ofname, ospec);
518 <        lep = lu_find(&ofiletab, ofname);       /* look it up */
612 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
613 >        if (ofl < 0) {
614 >                sprintf(errmsg, "bad output format '%s'", ospec);
615 >                error(USER, errmsg);
616 >        }
617 >        lep = lu_find(&ofiletab, oname);        /* look it up */
618          if (lep->key == NULL)                   /* new entry */
619 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
619 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
620          if (lep->data == NULL) {                /* open output file */
621 <                lep->data = (char *)fopen(ofname, "w");
622 <                if (lep->data == NULL) {
623 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
621 >                FILE            *fp;
622 >                int             i;
623 >                if (oname[0] == '!')            /* output to command */
624 >                        fp = popen(oname+1, "w");
625 >                else
626 >                        fp = fopen(oname, "w");
627 >                if (fp == NULL) {
628 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
629                          error(SYSTEM, errmsg);
630                  }
631 <                if (header)
632 <                        printheader((FILE *)lep->data);
631 >                if (outfmt != 'a')
632 >                        SET_FILE_BINARY(fp);
633 >                if (header) {
634 >                        char    info[512];
635 >                        char    *cp = info;
636 >                        sprintf(cp, "MODIFIER=%s\n", mname);
637 >                        while (*cp) ++cp;
638 >                        if (ofl & OF_BIN) {
639 >                                sprintf(cp, "BIN=%d\n", bn);
640 >                                while (*cp) ++cp;
641 >                        }
642 >                        *cp = '\0';
643 >                        printheader(fp, info);
644 >                }
645 >                                                /* play catch-up */
646 >                for (i = 0; i < lastdone; i++) {
647 >                        static const DCOLOR     nocontrib = BLKCOLOR;
648 >                        put_contrib(nocontrib, fp);
649 >                        if (outfmt == 'a')
650 >                                putc('\n', fp);
651 >                }
652 >                if (xres > 0)
653 >                        fflush(fp);
654 >                lep->data = (char *)fp;
655          }
656          return (FILE *)lep->data;               /* return open file pointer */
531 badspec:
532        sprintf(errmsg, "bad output format '%s'", ospec);
533        error(USER, errmsg);
534        return NULL;            /* pro forma return */
657   }
658  
659   /* read input ray into buffer */
660   int
661   getinp(char *buf, FILE *fp)
662   {
663 +        char    *cp;
664 +        int     i;
665 +
666          switch (inpfmt) {
667          case 'a':
668 <                if (fgets(buf, 128, fp) == NULL)
669 <                        return 0;
668 >                cp = buf;               /* make sure we get 6 floats */
669 >                for (i = 0; i < 6; i++) {
670 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
671 >                                return 0;
672 >                        if ((cp = fskip(cp)) == NULL || *cp)
673 >                                return 0;
674 >                        *cp++ = ' ';
675 >                }
676 >                getc(fp);               /* get/put eol */
677 >                *cp-- = '\0'; *cp = '\n';
678                  return strlen(buf);
679          case 'f':
680                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 556 | Line 689 | getinp(char *buf, FILE *fp)
689          return 0;       /* pro forma return */
690   }
691  
692 < static const float      *rparams = NULL;        /* ray parameter pointer */
692 > static float    rparams[9];             /* traced ray parameters */
693  
694   /* return channel (ray) value */
695   double
# Line 564 | Line 697 | chanvalue(int n)
697   {
698          if (--n < 0 || n >= 6)
699                  error(USER, "illegal channel number ($N)");
700 <        if (rparams == NULL)
568 <                error(USER, "illegal use of $N in constant expression");
569 <        return rparams[n];
700 >        return rparams[n+3];
701   }
702  
703 < /* add ray contribution to the appropriate modifier bin */
703 > /* add current ray contribution to the appropriate modifier bin */
704   void
705 < add_contrib(const char *modn, float rayval[9])
705 > add_contrib(const char *modn)
706   {
707          LUENT   *le = lu_find(&modconttab, modn);
708          MODCONT *mp = (MODCONT *)le->data;
# Line 581 | Line 712 | add_contrib(const char *modn, float rayval[9])
712                  sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn);
713                  error(USER, errmsg);
714          }
715 <        rparams = rayval + 3;           /* for chanvalue */
585 <        eclock++;
715 >        eclock++;                       /* get bin number */
716          bn = (int)(evalue(mp->binv) + .5);
717          if (bn <= 0)
718                  bn = 0;
719 <        else if (bn > mp->nbins) {      /* new bin */
719 >        else if (bn >= mp->nbins) {     /* new bin */
720                  mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
721 <                                                bn*sizeof(COLOR));
721 >                                                bn*sizeof(DCOLOR));
722                  if (mp == NULL)
723                          error(SYSTEM, "out of memory in add_contrib");
724 <                memset(mp->cbin+mp->nbins, 0, sizeof(COLOR)*(bn+1-mp->nbins));
724 >                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
725                  mp->nbins = bn+1;
726                  le->data = (char *)mp;
727          }
728 <        addcolor(mp->cbin[bn], rayval);
599 <        rparams = NULL;
728 >        addcolor(mp->cbin[bn], rparams);
729   }
730  
731   /* output newline to ASCII file and/or flush as requested */
# Line 616 | Line 745 | puteol(const LUENT *e, void *p)
745          return 0;
746   }
747  
748 + /* put out ray contribution to file */
749 + void
750 + put_contrib(const DCOLOR cnt, FILE *fout)
751 + {
752 +        float   fv[3];
753 +        COLR    cv;
754 +
755 +        switch (outfmt) {
756 +        case 'a':
757 +                fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
758 +                break;
759 +        case 'f':
760 +                fv[0] = cnt[0];
761 +                fv[1] = cnt[1];
762 +                fv[2] = cnt[2];
763 +                fwrite(fv, sizeof(float), 3, fout);
764 +                break;
765 +        case 'd':
766 +                fwrite(cnt, sizeof(double), 3, fout);
767 +                break;
768 +        case 'c':
769 +                setcolr(cv, cnt[0], cnt[1], cnt[2]);
770 +                fwrite(cv, sizeof(cv), 1, fout);
771 +                break;
772 +        default:
773 +                error(INTERNAL, "botched output format");
774 +        }
775 + }
776 +
777   /* output ray tallies and clear for next primary */
778   void
779   done_contrib(void)
780   {
781          int     i, j;
782          MODCONT *mp;
783 <        FILE    *fout;
626 <        double  dv[3];
627 <        COLR    cv;
783 >        FILE    *fp;
784                                                  /* output modifiers in order */
785          for (i = 0; i < nmods; i++) {
786                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
787 <                for (j = 0; j < mp->nbins; j++) {
788 <                        fout = getofile(mp->outspec, mp->modname, j);
789 <                        switch (outfmt) {
790 <                        case 'a':
791 <                                fprintf(fout, "%.6e\t%.6e\t%.6e\t",
792 <                                                mp->cbin[j][RED],
793 <                                                mp->cbin[j][GRN],
794 <                                                mp->cbin[j][BLU]);
795 <                                break;
796 <                        case 'f':
641 <                                fwrite(mp->cbin[j], sizeof(float), 3, fout);
642 <                                break;
643 <                        case 'd':
644 <                                dv[0] = mp->cbin[j][0];
645 <                                dv[1] = mp->cbin[j][1];
646 <                                dv[2] = mp->cbin[j][2];
647 <                                fwrite(dv, sizeof(double), 3, fout);
648 <                                break;
649 <                        case 'c':
650 <                                setcolr(cv, mp->cbin[j][RED],
651 <                                        mp->cbin[j][GRN], mp->cbin[j][BLU]);
652 <                                fwrite(cv, sizeof(COLR), 1, fout);
653 <                                break;
654 <                        default:
655 <                                error(INTERNAL, "botched output format");
656 <                        }
657 <                }
787 >                fp = getofile(mp->outspec, mp->modname, 0);
788 >                put_contrib(mp->cbin[0], fp);
789 >                if (mp->nbins > 3 &&            /* minor optimization */
790 >                                fp == getofile(mp->outspec, mp->modname, 1))
791 >                        for (j = 1; j < mp->nbins; j++)
792 >                                put_contrib(mp->cbin[j], fp);
793 >                else
794 >                        for (j = 1; j < mp->nbins; j++)
795 >                                put_contrib(mp->cbin[j],
796 >                                        getofile(mp->outspec, mp->modname, j));
797                                                  /* clear for next ray tree */
798 <                memset(mp->cbin, 0, sizeof(COLOR)*mp->nbins);
798 >                memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
799          }
800          --waitflush;                            /* terminate records */
801          lu_doall(&ofiletab, puteol, NULL);
802 <        if (!waitflush)
802 >        if (using_stdout & (outfmt == 'a'))
803 >                putc('\n', stdout);
804 >        if (!waitflush) {
805                  waitflush = xres;
806 +                if (using_stdout)
807 +                        fflush(stdout);
808 +        }
809   }
810  
811 < /* process (or save) ray tree produced by rtrace process */
811 > /* queue completed ray tree produced by rtrace process */
812   void
813 < process_rays(struct rtproc *rtp)
813 > queue_raytree(struct rtproc *rtp)
814   {
815 <        struct rtproc   *rtu;
816 <                                        /* check if time to process it */
817 <        if (rtp->raynum == lastdone+1) {
818 <                int             n = rtp->bpos - rtp->buf;
815 >        struct rtproc   *rtu, *rtl = NULL;
816 >                                        /* insert following ray order */
817 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
818 >                if (rtp->raynum < rtu->raynum)
819 >                        break;
820 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
821 >        if (rtu == NULL)
822 >                error(SYSTEM, "out of memory in queue_raytree");
823 >        *rtu = *rtp;
824 >        if (rtl == NULL) {
825 >                rtu->next = rt_unproc;
826 >                rt_unproc = rtu;
827 >        } else {
828 >                rtu->next = rtl->next;
829 >                rtl->next = rtu;
830 >        }
831 >        rtp->raynum = 0;                /* clear path for next ray tree */
832 >        rtp->bsiz = 0;
833 >        rtp->buf = NULL;
834 >        rtp->nbr = 0;
835 > }
836 >
837 > /* process completed ray trees from our queue */
838 > void
839 > process_queue(void)
840 > {
841 >        char    modname[128];
842 >                                        /* ray-ordered queue */
843 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
844 >                struct rtproc   *rtp = rt_unproc;
845 >                int             n = rtp->nbr;
846                  const char      *cp = rtp->buf;
847                  while (n > 0) {         /* process rays */
848 <                        char    matname[128];
678 <                        char    *mnp = matname;
848 >                        register char   *mnp = modname;
849                                          /* skip leading tabs */
850                          while (n > 0 && *cp == '\t') {
851                                  cp++; n--;
852                          }
853 <                                        /* get material name */
853 >                        if (!n || !(isalpha(*cp) | (*cp == '_')))
854 >                                error(USER, "bad modifier name from rtrace");
855 >                                        /* get modifier name */
856                          while (n > 0 && *cp != '\t') {
857                                  *mnp++ = *cp++; n--;
858                          }
687                        if (!n)
688                                error(USER, "botched modifer name from rtrace");
859                          *mnp = '\0';
860 <                        cp++; n--;      /* skip terminating tab */
860 >                        cp++; n--;      /* eat following tab */
861                          if (n < (int)(sizeof(float)*9))
862                                  error(USER, "incomplete ray value from rtrace");
863                                          /* add ray contribution */
864 <                        add_contrib(matname, (float *)cp);
864 >                        memcpy(rparams, cp, sizeof(float)*9);
865                          cp += sizeof(float)*9; n -= sizeof(float)*9;
866 +                        add_contrib(modname);
867                  }
868                  done_contrib();         /* sum up contributions & output */
869                  lastdone = rtp->raynum;
870 <                free(rtp->buf);
871 <                                        /* catch up with unprocessed list */
872 <                while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
702 <                        process_rays(rt_unproc);
703 <                        rt_unproc = (rtu=rt_unproc)->next;
704 <                        free(rtu);
705 <                }
706 <        } else {                        /* else insert in unprocessed list */
707 <                struct rtproc   *rtl = NULL;
708 <                for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
709 <                        if (rtp->raynum < rtu->raynum)
710 <                                break;
711 <                rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
712 <                if (rtu == NULL)
713 <                        error(SYSTEM, "out of memory in process_rays");
714 <                *rtu = *rtp;
715 <                if (rtl == NULL) {
716 <                        rtu->next = rt_unproc;
717 <                        rt_unproc = rtu;
718 <                } else {
719 <                        rtu->next = rtl->next;
720 <                        rtl->next = rtu;
721 <                }
870 >                free(rtp->buf);         /* free up buffer space */
871 >                rt_unproc = rtp->next;
872 >                free(rtp);              /* done with this ray tree */
873          }
723        rtp->raynum = 0;                /* clear path for next ray tree */
724        rtp->bsiz = 0;
725        rtp->buf = NULL;
874   }
875  
876   /* wait for rtrace process to finish with ray tree */
# Line 763 | Line 911 | wait_rproc(void)
911                          if (rt->buf == NULL) {
912                                  rt->bsiz = treebufsiz;
913                                  rt->buf = (char *)malloc(treebufsiz);
914 <                        } else if (rt->bpos + BUFSIZ > rt->buf + rt->bsiz) {
914 >                        } else if (rt->nbr + BUFSIZ > rt->bsiz) {
915                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
916                                          rt->bsiz = treebufsiz;
917                                  else
# Line 772 | Line 920 | wait_rproc(void)
920                          }
921                          if (rt->buf == NULL)
922                                  error(SYSTEM, "out of memory in wait_rproc");
923 <                        nr = read(rt->pd.r, rt->bpos,
924 <                                        rt->buf + rt->bsiz - rt->bpos);
777 <                        if (!nr)
923 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
924 >                        if (nr <= 0)
925                                  error(USER, "rtrace process died");
926 <                        rt->bpos += nr;         /* advance buffer & check */
927 <                        if (rt->bpos[-1] == '\t' && rt->bpos[-2] == '-') {
928 <                                rt->bpos -= 2;  /* elide terminator */
929 <                                process_rays(rt);
926 >                        rt->nbr += nr;          /* advance & check */
927 >                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
928 >                                                        "~\t~\t", 4)) {
929 >                                rt->nbr -= 4;   /* elide terminator */
930 >                                queue_raytree(rt);
931                                  rtfree = rt;    /* ready for next ray */
932                          }
933                  }
# Line 801 | Line 949 | get_rproc(void)
949  
950   /* trace ray contributions (main loop) */
951   void
952 < tracecontribs(FILE *fin)
952 > trace_contribs(FILE *fin)
953   {
954          char            inpbuf[128];
955          int             iblen;
# Line 810 | Line 958 | tracecontribs(FILE *fin)
958          while ((iblen = getinp(inpbuf, fin)) > 0) {
959                  if (lastray+1 < lastray) {      /* counter rollover? */
960                          while (wait_rproc() != NULL)
961 <                                ;
961 >                                process_queue();
962                          lastdone = lastray = 0;
963                  }
964                  rtp = get_rproc();              /* get avail. rtrace process */
965 <                rtp->raynum = ++lastray;        /* assign this ray to it */
965 >                rtp->raynum = ++lastray;        /* assign ray to it */
966                  writebuf(rtp->pd.w, inpbuf, iblen);
967 <                if (!--raysleft)
968 <                        break;                  /* explicit EOF */
967 >                if (raysleft && !--raysleft)
968 >                        break;
969 >                process_queue();                /* catch up with results */
970          }
971          while (wait_rproc() != NULL)            /* process outstanding rays */
972 <                ;
972 >                process_queue();
973 >        if (raysleft)
974 >                error(USER, "unexpected EOF on input");
975 >        lu_done(&ofiletab);                     /* close output files */
976 > }
977 >
978 > /* seek on the given output file */
979 > static int
980 > myseeko(const LUENT *e, void *p)
981 > {
982 >        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
983 >                sprintf(errmsg, "seek error on file '%s'", e->key);
984 >                error(SYSTEM, errmsg);
985 >        }
986 > }
987 >
988 > /* recover output if possible */
989 > void
990 > recover_output(FILE *fin)
991 > {
992 >        off_t   lastout = -1;
993 >        int     outvsiz;
994 >        char    *outvfmt;
995 >        int     i, j;
996 >        MODCONT *mp;
997 >        int     ofl;
998 >        char    oname[1024];
999 >        LUENT   *ment, *oent;
1000 >        FILE    *fp;
1001 >        off_t   nvals;
1002 >
1003 >        switch (outfmt) {
1004 >        case 'a':
1005 >                error(USER, "cannot recover ASCII output");
1006 >                return;
1007 >        case 'f':
1008 >                outvsiz = sizeof(float)*3;
1009 >                outvfmt = "float";
1010 >                break;
1011 >        case 'd':
1012 >                outvsiz = sizeof(double)*3;
1013 >                outvfmt = "double";
1014 >                break;
1015 >        case 'c':
1016 >                outvsiz = sizeof(COLR);
1017 >                outvfmt = COLRFMT;
1018 >                break;
1019 >        default:
1020 >                error(INTERNAL, "botched output format");
1021 >                return;
1022 >        }
1023 >                                                /* check modifier outputs */
1024 >        for (i = 0; i < nmods; i++) {
1025 >                ment = lu_find(&modconttab,modname[i]);
1026 >                mp = (MODCONT *)ment->data;
1027 >                if (mp->outspec == NULL)
1028 >                        error(USER, "cannot recover from stdout");
1029 >                for (j = 0; ; j++) {            /* check each bin's file */
1030 >                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1031 >                        if (ofl < 0)
1032 >                                error(USER, "bad output file specification");
1033 >                        oent = lu_find(&ofiletab, oname);
1034 >                        if (oent->data != NULL) { /* saw this one already */
1035 >                                if (ofl & OF_BIN)
1036 >                                        continue;
1037 >                                break;
1038 >                        }
1039 >                        if (oname[0] == '!')
1040 >                                error(USER, "cannot recover from command");
1041 >                                                /* open output */
1042 >                        fp = fopen(oname, "rb+");
1043 >                        if (fp == NULL) {
1044 >                                if (j)
1045 >                                        break;  /* assume end of modifier */
1046 >                                sprintf(errmsg, "missing recover file '%s'",
1047 >                                                oname);
1048 >                                error(USER, errmsg);
1049 >                        }
1050 >                        nvals = lseek(fileno(fp), 0, SEEK_END);
1051 >                        if (nvals <= 0) {
1052 >                                lastout = 0;    /* empty output, quit here */
1053 >                                fclose(fp);
1054 >                                break;
1055 >                        }
1056 >                        lseek(fileno(fp), 0, SEEK_SET);
1057 >                        if (header) {
1058 >                                int     xr, yr;
1059 >                                if (checkheader(fp, outvfmt, NULL) != 1) {
1060 >                                        sprintf(errmsg,
1061 >                                                "format mismatch for '%s'",
1062 >                                                        oname);
1063 >                                        error(USER, errmsg);
1064 >                                }
1065 >                                if (xres > 0 && yres > 0 &&
1066 >                                                (!fscnresolu(&xr, &yr, fp) ||
1067 >                                                        xr != xres ||
1068 >                                                        yr != yres)) {
1069 >                                        sprintf(errmsg,
1070 >                                                "resolution mismatch for '%s'",
1071 >                                                        oname);
1072 >                                        error(USER, errmsg);
1073 >                                }
1074 >                        }
1075 >                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1076 >                        if (lastout < 0 || nvals < lastout)
1077 >                                lastout = nvals;
1078 >                        if (oent->key == NULL) /* new entry */
1079 >                                oent->key = strcpy((char *)
1080 >                                                malloc(strlen(oname)+1), oname);
1081 >                        oent->data = (char *)fp;
1082 >                        if (!(ofl & OF_BIN))
1083 >                                break;          /* no bin separation */
1084 >                }
1085 >                if (!lastout) {                 /* empty output */
1086 >                        error(WARNING, "no previous data to recover");
1087 >                        lu_done(&ofiletab);     /* reclose all outputs */
1088 >                        return;
1089 >                }
1090 >                if (j > mp->nbins) {            /* preallocate modifier bins */
1091 >                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1092 >                                                        (j-1)*sizeof(DCOLOR));
1093 >                        if (mp == NULL)
1094 >                                error(SYSTEM, "out of memory in recover_output");
1095 >                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1096 >                        mp->nbins = j;
1097 >                        ment->data = (char *)mp;
1098 >                }
1099 >        }
1100 >        if (lastout < 0) {
1101 >                error(WARNING, "no output files to recover");
1102 >                return;
1103 >        }
1104 >        if (raysleft && lastout >= raysleft) {
1105 >                error(WARNING, "output appears to be complete");
1106 >                /* XXX should read & discard input? */
1107 >                quit(0);
1108 >        }
1109 >                                                /* seek on all files */
1110 >        nvals = lastout * outvsiz;
1111 >        lu_doall(&ofiletab, myseeko, &nvals);
1112 >                                                /* discard input */
1113 >        for (nvals = 0; nvals < lastout; nvals++)
1114 >                if (getinp(oname, fin) <= 0)
1115 >                        error(USER, "unexpected EOF on input");
1116 >        lastray = lastdone = (unsigned long)lastout;
1117 >        if (raysleft)
1118 >                raysleft -= lastray;
1119   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines