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.15 by greg, Thu Jun 9 18:27:44 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
25 #endif
26
19   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
20  
21   int     treebufsiz = BUFSIZ;            /* current tree buffer size */
22  
23 + typedef double  DCOLOR[3];              /* double-precision color */
24 +
25   /*
26   * The modcont structure is used to accumulate ray contributions
27   * for a particular modifier, which may be subdivided into bins
# Line 43 | Line 37 | typedef struct {
37          const char      *modname;       /* modifier name */
38          EPNODE          *binv;          /* bin value expression */
39          int             nbins;          /* number of accumulation bins */
40 <        COLOR           cbin[1];        /* contribution bins (extends struct) */
40 >        DCOLOR          cbin[1];        /* contribution bins (extends struct) */
41   } MODCONT;                      /* modifier contribution */
42  
43   static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); }
# Line 55 | Line 49 | static void closefile(void *p) { fclose((FILE *)p); }
49  
50   LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
51  
52 + #define OF_MODIFIER     01
53 + #define OF_BIN          02
54 +
55   FILE *getofile(const char *ospec, const char *mname, int bn);
56 + int ofname(char *oname, const char *ospec, const char *mname, int bn);
57 + void printheader(FILE *fout, const char *info);
58  
59   /*
60   * The rcont structure is used to manage i/o with a particular
# Line 66 | Line 65 | FILE *getofile(const char *ospec, const char *mname, i
65   struct rtproc {
66          struct rtproc   *next;          /* next in list of processes */
67          SUBPROC         pd;             /* rtrace pipe descriptors */
68 <        unsigned long   raynum;         /* ray number for this buffer */
69 <        int             bsiz;           /* rtrace buffer length */
70 <        char            *buf;           /* rtrace i/o buffer */
71 <        char            *bpos;          /* current buffer position */
72 < };                              /* rtrace process */
68 >        unsigned long   raynum;         /* ray number for this tree */
69 >        int             bsiz;           /* ray tree buffer length */
70 >        char            *buf;           /* ray tree buffer */
71 >        int             nbr;            /* number of bytes from rtrace */
72 > };                              /* rtrace process buffer */
73  
74                                          /* rtrace command and defaults */
75   char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
76 <                                "-ab", "1", "-ad", "512", };
76 >                                "-ab", "1", "-ad", "128", };
77   int  rtargc = 9;
78                                          /* overriding rtrace options */
79 < char            *myrtopts[] = { "-o-TmWdp", "-h-",
80 <                                "-x", "1", "-y", "0", NULL };
79 > char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
80 >                                "-dt", "0", "-as", "0", "-aa", "0", NULL };
81  
82   struct rtproc   rt0;                    /* head of rtrace process list */
83  
84   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
85  
86   char    persistfn[] = "pfXXXXXX";       /* persist file name */
88 char    fmt[8];                         /* rtrace i/o format */
87  
88   int             gargc;                  /* global argc */
89   char            **gargv;                /* global argv */
# Line 100 | Line 98 | int            header = 1;             /* output header? */
98   int             xres = 0;               /* horiz. output resolution */
99   int             yres = 0;               /* vert. output resolution */
100  
101 < long            raysleft;               /* number of rays left to trace */
101 > unsigned long   raysleft;               /* number of rays left to trace */
102   long            waitflush;              /* how long until next flush */
103  
104   unsigned long   lastray = 0;            /* last ray number sent */
105   unsigned long   lastdone = 0;           /* last ray processed */
106  
107 + int             using_stdout = 0;       /* are we using stdout? */
108 +
109   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
110   int             nmods = 0;              /* number of modifiers */
111  
112   MODCONT *addmodifier(char *modn, char *outf, char *binv);
113  
114 int done_rprocs(struct rtproc *rtp);
114   void init(int np);
115 < void tracecontribs(FILE *fp);
115 > int done_rprocs(struct rtproc *rtp);
116 > void recover_output(FILE *fin);
117 > void trace_contribs(FILE *fin);
118   struct rtproc *wait_rproc(void);
119   struct rtproc *get_rproc(void);
120 < void process_rays(struct rtproc *rtp);
120 > void queue_raytree(struct rtproc *rtp);
121 > void process_queue(void);
122  
123 < void add_contrib(const char *modn, float rayval[7]);
123 > void put_contrib(const DCOLOR cnt, FILE *fout);
124 > void add_contrib(const char *modn);
125   void done_contrib(void);
126  
127   /* set input/output format */
# Line 126 | Line 129 | static void
129   setformat(const char *fmt)
130   {
131          switch (fmt[0]) {
129        case 'a':
132          case 'f':
133          case 'd':
134 +                SET_FILE_BINARY(stdin);
135 +                /* fall through */
136 +        case 'a':
137                  inpfmt = fmt[0];
138                  break;
139          default:
# Line 159 | Line 164 | int
164   main(int argc, char *argv[])
165   {
166          int     nprocs = 1;
167 +        int     recover = 0;
168          char    *curout = NULL;
169          char    *binval = NULL;
170 <        int     i;
171 <                                /* set global argument list */
172 <        gargc = argc;
170 >        char    fmt[8];
171 >        int     i, j;
172 >                                /* global program name */
173          gargv = argv;
174 <                                /* set up calcomp functions */
174 >                                /* initialize calcomp routines */
175          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
176          esupport &= ~(E_OUTCHAN);
177 +        varset("PI", ':', PI);
178                                  /* get our options */
179 <        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
180 <                switch (argv[i][1]) {
181 <                case 'n':                       /* number of processes */
182 <                        if (argv[i][2] || i >= argc-1) break;
183 <                        nprocs = atoi(argv[++i]);
184 <                        if (nprocs <= 0)
185 <                                error(USER, "illegal number of processes");
186 <                        continue;
187 <                case 'h':                       /* output header? */
188 <                        switch (argv[i][2]) {
189 <                        case '\0':
190 <                                header = !header;
179 >        for (i = 1; i < argc-1; i++) {
180 >                                                /* expand arguments */
181 >                while ((j = expandarg(&argc, &argv, i)) > 0)
182 >                        ;
183 >                if (j < 0) {
184 >                        fprintf(stderr, "%s: cannot expand '%s'",
185 >                                        argv[0], argv[i]);
186 >                        exit(1);
187 >                }
188 >                if (argv[i][0] == '-')
189 >                        switch (argv[i][1]) {
190 >                        case 'r':               /* recover output */
191 >                                if (argv[i][2]) break;
192 >                                recover++;
193                                  continue;
194 <                        case '+': case '1': case 'T': case 't':
195 <                                header = 1;
194 >                        case 'n':               /* number of processes */
195 >                                if (argv[i][2] || i >= argc-1) break;
196 >                                nprocs = atoi(argv[++i]);
197 >                                if (nprocs <= 0)
198 >                                        error(USER, "illegal number of processes");
199                                  continue;
200 <                        case '-': case '0': case 'F': case 'f':
201 <                                header = 0;
200 >                        case 'h':               /* output header? */
201 >                                switch (argv[i][2]) {
202 >                                case '\0':
203 >                                        header = !header;
204 >                                        continue;
205 >                                case '+': case '1':
206 >                                case 'T': case 't':
207 >                                case 'Y': case 'y':
208 >                                        header = 1;
209 >                                        continue;
210 >                                case '-': case '0':
211 >                                case 'F': case 'f':
212 >                                case 'N': case 'n':
213 >                                        header = 0;
214 >                                        continue;
215 >                                }
216 >                                break;
217 >                        case 'f':               /* file or i/o format */
218 >                                if (!argv[i][2]) {
219 >                                        char    *fpath;
220 >                                        if (i >= argc-1) break;
221 >                                        fpath = getpath(argv[++i],
222 >                                                        getrlibpath(), R_OK);
223 >                                        if (fpath == NULL) {
224 >                                                sprintf(errmsg,
225 >                                                        "cannot find file '%s'",
226 >                                                                argv[i]);
227 >                                                error(USER, errmsg);
228 >                                        }
229 >                                        fcompile(fpath);
230 >                                        continue;
231 >                                }
232 >                                setformat(argv[i]+2);
233                                  continue;
234 <                        }
235 <                        break;
236 <                case 'f':                       /* file or i/o format */
194 <                        if (!argv[i][2]) {
195 <                                if (i >= argc-1) break;
196 <                                fcompile(argv[++i]);
234 >                        case 'e':               /* expression */
235 >                                if (argv[i][2] || i >= argc-1) break;
236 >                                scompile(argv[++i], NULL, 0);
237                                  continue;
238 +                        case 'o':               /* output file spec. */
239 +                                if (argv[i][2] || i >= argc-1) break;
240 +                                curout = argv[++i];
241 +                                continue;
242 +                        case 'x':               /* horiz. output resolution */
243 +                                if (argv[i][2] || i >= argc-1) break;
244 +                                xres = atoi(argv[++i]);
245 +                                continue;
246 +                        case 'y':               /* vert. output resolution */
247 +                                if (argv[i][2] || i >= argc-1) break;
248 +                                yres = atoi(argv[++i]);
249 +                                continue;
250 +                        case 'b':               /* bin expression */
251 +                                if (argv[i][2] || i >= argc-1) break;
252 +                                binval = argv[++i];
253 +                                continue;
254 +                        case 'm':               /* modifier name */
255 +                                if (argv[i][2] || i >= argc-1) break;
256 +                                rtargv[rtargc++] = "-ti";
257 +                                rtargv[rtargc++] = argv[++i];
258 +                                addmodifier(argv[i], curout, binval);
259 +                                continue;
260                          }
261 <                        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 */
261 >                rtargv[rtargc++] = argv[i];     /* assume rtrace option */
262          }
263 <        for ( ; i < argc; i++)  /* transfer rtrace-specific options */
264 <                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 <        }
263 >                                /* set global argument list */
264 >        gargc = argc; gargv = argv;
265                                  /* add "mandatory" rtrace settings */
266 <        for (i = 0; myrtopts[i] != NULL; i++)
267 <                rtargv[rtargc++] = myrtopts[i];
268 <        if (!nprocs) {          /* just asking for defaults? */
266 >        for (j = 0; myrtopts[j] != NULL; j++)
267 >                rtargv[rtargc++] = myrtopts[j];
268 >                                /* just asking for defaults? */
269 >        if (!strcmp(argv[i], "-defaults")) {
270                  char    sxres[16], syres[16];
271                  char    *rtpath;
272 <                printf("-n  1\t\t\t\t# number of processes\n", nprocs);
272 >                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
273                  fflush(stdout);                 /* report OUR options */
274                  rtargv[rtargc++] = header ? "-h+" : "-h-";
275                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 256 | Line 280 | main(int argc, char *argv[])
280                  rtargv[rtargc++] = "-y";
281                  sprintf(syres, "%d", yres);
282                  rtargv[rtargc++] = syres;
283 <                rtargv[rtargc++] = "-oW";
283 >                rtargv[rtargc++] = "-oTW";
284                  rtargv[rtargc++] = "-defaults";
285                  rtargv[rtargc] = NULL;
286                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 269 | Line 293 | main(int argc, char *argv[])
293                  perror(rtpath); /* execv() should not return */
294                  exit(1);
295          }
296 <        if (!nmods)
297 <                error(USER, "No modifiers specified");
298 <        if (argc < 2 || argv[argc-1][0] == '-')
299 <                error(USER, "missing octree argument");
296 >        if (nprocs > 1) {       /* add persist file if parallel */
297 >                rtargv[rtargc++] = "-PP";
298 >                rtargv[rtargc++] = mktemp(persistfn);
299 >        }
300                                  /* add format string */
301          sprintf(fmt, "-f%cf", inpfmt);
302          rtargv[rtargc++] = fmt;
303                                  /* octree argument is last */
304 <        rtargv[rtargc++] = octree = argv[argc-1];
304 >        if (i <= 0 || i != argc-1 || argv[i][0] == '-')
305 >                error(USER, "missing octree argument");
306 >        rtargv[rtargc++] = octree = argv[i];
307          rtargv[rtargc] = NULL;
308 <                                /* start rtrace & sum contributions */
308 >                                /* start rtrace & compute contributions */
309          init(nprocs);
310 <        tracecontribs(stdin);
310 >        if (recover)            /* perform recovery if requested */
311 >                recover_output(stdin);
312 >        trace_contribs(stdin);
313          quit(0);
314   }
315  
# Line 333 | Line 361 | quit(int status)
361          exit(status);                   /* flushes all output streams */
362   }
363  
364 < /* start rtrace and initialize buffers */
364 > /* start rtrace processes and initialize */
365   void
366   init(int np)
367   {
368          struct rtproc   *rtp;
369          int     i;
370          int     maxbytes;
371 +                                        /* make sure we have something to do */
372 +        if (!nmods)
373 +                error(USER, "No modifiers specified");
374                                          /* assign ray variables */
375          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
376          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
377                                          /* set up signal handling */
378 < #ifdef SIGPIPE /* not present on Windows */
378 >        signal(SIGINT, quit);
379 > #ifdef SIGHUP
380 >        signal(SIGHUP, quit);
381 > #endif
382 > #ifdef SIGTERM
383 >        signal(SIGTERM, quit);
384 > #endif
385 > #ifdef SIGPIPE
386          signal(SIGPIPE, quit);
387   #endif
388          rtp = &rt0;                     /* start rtrace process(es) */
# Line 360 | Line 398 | init(int np)
398                          error(SYSTEM, "cannot start rtrace process");
399                  if (maxbytes > treebufsiz)
400                          treebufsiz = maxbytes;
401 +                rtp->raynum = 0;
402                  rtp->bsiz = 0;
403                  rtp->buf = NULL;
404 <                rtp->raynum = 0;
404 >                rtp->nbr = 0;
405                  if (i == np)            /* last process? */
406                          break;
407                  if (i == 1)
# Line 375 | Line 414 | init(int np)
414          rtp->next = NULL;               /* terminate list */
415          if (yres > 0) {
416                  if (xres > 0)
417 <                        raysleft = xres*yres;
417 >                        raysleft = (unsigned long)xres*yres;
418                  else
419                          raysleft = yres;
420          } else
# Line 428 | Line 467 | eputs(char  *s)
467          midline = s[strlen(s)-1] != '\n';
468   }
469  
470 + /* construct output file name and return flags whether modifier/bin present */
471 + int
472 + ofname(char *oname, const char *ospec, const char *mname, int bn)
473 + {
474 +        const char      *mnp = NULL;
475 +        const char      *bnp = NULL;
476 +        const char      *cp;
477 +        
478 +        if (ospec == NULL)
479 +                return -1;
480 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
481 +                if (*cp == '%') {
482 +                        do
483 +                                ++cp;
484 +                        while (isdigit(*cp));
485 +                        switch (*cp) {
486 +                        case '%':
487 +                                break;
488 +                        case 's':
489 +                                if (mnp != NULL)
490 +                                        return -1;
491 +                                mnp = cp;
492 +                                break;
493 +                        case 'd':
494 +                                if (bnp != NULL)
495 +                                        return -1;
496 +                                bnp = cp;
497 +                                break;
498 +                        default:
499 +                                return -1;
500 +                        }
501 +                }
502 +        if (mnp != NULL) {                      /* create file name */
503 +                if (bnp != NULL) {
504 +                        if (bnp > mnp)
505 +                                sprintf(oname, ospec, mname, bn);
506 +                        else
507 +                                sprintf(oname, ospec, bn, mname);
508 +                        return OF_MODIFIER|OF_BIN;
509 +                }
510 +                sprintf(oname, ospec, mname);
511 +                return OF_MODIFIER;
512 +        }
513 +        if (bnp != NULL) {
514 +                sprintf(oname, ospec, bn);
515 +                return OF_BIN;
516 +        }
517 +        strcpy(oname, ospec);
518 +        return 0;
519 + }
520 +
521   /* write header to the given output stream */
522   void
523 < printheader(FILE *fout)
523 > printheader(FILE *fout, const char *info)
524   {
525          extern char     VersionID[];
526          FILE            *fin = fopen(octree, "r");
527          
528          if (fin == NULL)
529                  quit(1);
530 <        getheader(fin, (gethfunc *)fputs, fout);        /* copy octree header */
530 >        checkheader(fin, "ignore", fout);       /* copy octree header */
531          fclose(fin);
532          printargs(gargc-1, gargv, fout);        /* add our command */
533          fprintf(fout, "SOFTWARE= %s\n", VersionID);
534          fputnow(fout);
535 +        if (info != NULL)                       /* add extra info if given */
536 +                fputs(info, fout);
537          switch (outfmt) {                       /* add output format */
538          case 'a':
539                  fputformat("ascii", fout);
# Line 468 | Line 560 | printheader(FILE *fout)
560   FILE *
561   getofile(const char *ospec, const char *mname, int bn)
562   {
563 <        static int      using_stdout = 0;
564 <        const char      *mnp = NULL;
473 <        const char      *bnp = NULL;
474 <        const char      *cp;
475 <        char            ofname[1024];
563 >        int             ofl;
564 >        char            oname[1024];
565          LUENT           *lep;
566          
567          if (ospec == NULL) {                    /* use stdout? */
568 <                if (!using_stdout && header)
569 <                        printheader(stdout);
568 >                if (!using_stdout) {
569 >                        if (outfmt != 'a')
570 >                                SET_FILE_BINARY(stdout);
571 >                        if (header)
572 >                                printheader(stdout, NULL);
573 >                }
574                  using_stdout = 1;
575                  return stdout;
576          }
577 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
578 <                if (*cp == '%') {
579 <                        do
580 <                                ++cp;
581 <                        while (isdigit(*cp));
582 <                        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 */
577 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
578 >        if (ofl < 0) {
579 >                sprintf(errmsg, "bad output format '%s'", ospec);
580 >                error(USER, errmsg);
581 >        }
582 >        lep = lu_find(&ofiletab, oname);        /* look it up */
583          if (lep->key == NULL)                   /* new entry */
584 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
584 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
585          if (lep->data == NULL) {                /* open output file */
586 <                lep->data = (char *)fopen(ofname, "w");
587 <                if (lep->data == NULL) {
588 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
586 >                FILE            *fp;
587 >                int             i;
588 >                if (oname[0] == '!')            /* output to command */
589 >                        fp = popen(oname+1, "w");
590 >                else
591 >                        fp = fopen(oname, "w");
592 >                if (fp == NULL) {
593 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
594                          error(SYSTEM, errmsg);
595                  }
596 <                if (header)
597 <                        printheader((FILE *)lep->data);
596 >                if (outfmt != 'a')
597 >                        SET_FILE_BINARY(fp);
598 >                if (header) {
599 >                        char    info[512];
600 >                        char    *cp = info;
601 >                        if (ofl & OF_MODIFIER) {
602 >                                sprintf(cp, "MODIFIER=%s\n", mname);
603 >                                while (*cp) ++cp;
604 >                        }
605 >                        if (ofl & OF_BIN) {
606 >                                sprintf(cp, "BIN=%d\n", bn);
607 >                                while (*cp) ++cp;
608 >                        }
609 >                        *cp = '\0';
610 >                        printheader(fp, info);
611 >                }
612 >                                                /* play catch-up */
613 >                for (i = 0; i < lastdone; i++) {
614 >                        static const DCOLOR     nocontrib = BLKCOLOR;
615 >                        put_contrib(nocontrib, fp);
616 >                        if (outfmt == 'a')
617 >                                putc('\n', fp);
618 >                }
619 >                if (xres > 0)
620 >                        fflush(fp);
621 >                lep->data = (char *)fp;
622          }
623          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 */
624   }
625  
626   /* read input ray into buffer */
627   int
628   getinp(char *buf, FILE *fp)
629   {
630 +        char    *cp;
631 +        int     i;
632 +
633          switch (inpfmt) {
634          case 'a':
635 <                if (fgets(buf, 128, fp) == NULL)
636 <                        return 0;
635 >                cp = buf;               /* make sure we get 6 floats */
636 >                for (i = 0; i < 6; i++) {
637 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
638 >                                return 0;
639 >                        if ((cp = fskip(cp)) == NULL || *cp)
640 >                                return 0;
641 >                        *cp++ = ' ';
642 >                }
643 >                getc(fp);               /* get/put eol */
644 >                *cp-- = '\0'; *cp = '\n';
645                  return strlen(buf);
646          case 'f':
647                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 556 | Line 656 | getinp(char *buf, FILE *fp)
656          return 0;       /* pro forma return */
657   }
658  
659 < static const float      *rparams = NULL;        /* ray parameter pointer */
659 > static float    rparams[9];             /* traced ray parameters */
660  
661   /* return channel (ray) value */
662   double
# Line 564 | Line 664 | chanvalue(int n)
664   {
665          if (--n < 0 || n >= 6)
666                  error(USER, "illegal channel number ($N)");
667 <        if (rparams == NULL)
568 <                error(USER, "illegal use of $N in constant expression");
569 <        return rparams[n];
667 >        return rparams[n+3];
668   }
669  
670 < /* add ray contribution to the appropriate modifier bin */
670 > /* add current ray contribution to the appropriate modifier bin */
671   void
672 < add_contrib(const char *modn, float rayval[9])
672 > add_contrib(const char *modn)
673   {
674          LUENT   *le = lu_find(&modconttab, modn);
675          MODCONT *mp = (MODCONT *)le->data;
# Line 581 | Line 679 | add_contrib(const char *modn, float rayval[9])
679                  sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn);
680                  error(USER, errmsg);
681          }
682 <        rparams = rayval + 3;           /* for chanvalue */
585 <        eclock++;
682 >        eclock++;                       /* get bin number */
683          bn = (int)(evalue(mp->binv) + .5);
684          if (bn <= 0)
685                  bn = 0;
686          else if (bn > mp->nbins) {      /* new bin */
687                  mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
688 <                                                bn*sizeof(COLOR));
688 >                                                bn*sizeof(DCOLOR));
689                  if (mp == NULL)
690                          error(SYSTEM, "out of memory in add_contrib");
691 <                memset(mp->cbin+mp->nbins, 0, sizeof(COLOR)*(bn+1-mp->nbins));
691 >                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
692                  mp->nbins = bn+1;
693                  le->data = (char *)mp;
694          }
695 <        addcolor(mp->cbin[bn], rayval);
599 <        rparams = NULL;
695 >        addcolor(mp->cbin[bn], rparams);
696   }
697  
698   /* output newline to ASCII file and/or flush as requested */
# Line 616 | Line 712 | puteol(const LUENT *e, void *p)
712          return 0;
713   }
714  
715 + /* put out ray contribution to file */
716 + void
717 + put_contrib(const DCOLOR cnt, FILE *fout)
718 + {
719 +        float   fv[3];
720 +        COLR    cv;
721 +
722 +        switch (outfmt) {
723 +        case 'a':
724 +                fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
725 +                break;
726 +        case 'f':
727 +                fv[0] = cnt[0];
728 +                fv[1] = cnt[1];
729 +                fv[2] = cnt[2];
730 +                fwrite(fv, sizeof(float), 3, fout);
731 +                break;
732 +        case 'd':
733 +                fwrite(cnt, sizeof(double), 3, fout);
734 +                break;
735 +        case 'c':
736 +                setcolr(cv, cnt[0], cnt[1], cnt[2]);
737 +                fwrite(cv, sizeof(cv), 1, fout);
738 +                break;
739 +        default:
740 +                error(INTERNAL, "botched output format");
741 +        }
742 + }
743 +
744   /* output ray tallies and clear for next primary */
745   void
746   done_contrib(void)
747   {
748          int     i, j;
749          MODCONT *mp;
750 <        FILE    *fout;
626 <        double  dv[3];
627 <        COLR    cv;
750 >        FILE    *fp;
751                                                  /* output modifiers in order */
752          for (i = 0; i < nmods; i++) {
753                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
754 <                for (j = 0; j < mp->nbins; j++) {
755 <                        fout = getofile(mp->outspec, mp->modname, j);
756 <                        switch (outfmt) {
757 <                        case 'a':
758 <                                fprintf(fout, "%.6e\t%.6e\t%.6e\t",
759 <                                                mp->cbin[j][RED],
760 <                                                mp->cbin[j][GRN],
761 <                                                mp->cbin[j][BLU]);
762 <                                break;
763 <                        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 <                }
754 >                fp = getofile(mp->outspec, mp->modname, 0);
755 >                put_contrib(mp->cbin[0], fp);
756 >                if (mp->nbins > 3 &&            /* minor optimization */
757 >                                fp == getofile(mp->outspec, mp->modname, 1))
758 >                        for (j = 1; j < mp->nbins; j++)
759 >                                put_contrib(mp->cbin[j], fp);
760 >                else
761 >                        for (j = 1; j < mp->nbins; j++)
762 >                                put_contrib(mp->cbin[j],
763 >                                        getofile(mp->outspec, mp->modname, j));
764                                                  /* clear for next ray tree */
765 <                memset(mp->cbin, 0, sizeof(COLOR)*mp->nbins);
765 >                memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
766          }
767          --waitflush;                            /* terminate records */
768          lu_doall(&ofiletab, puteol, NULL);
769 <        if (!waitflush)
769 >        if (using_stdout & (outfmt == 'a'))
770 >                putc('\n', stdout);
771 >        if (!waitflush) {
772                  waitflush = xres;
773 +                if (using_stdout)
774 +                        fflush(stdout);
775 +        }
776   }
777  
778 < /* process (or save) ray tree produced by rtrace process */
778 > /* queue completed ray tree produced by rtrace process */
779   void
780 < process_rays(struct rtproc *rtp)
780 > queue_raytree(struct rtproc *rtp)
781   {
782 <        struct rtproc   *rtu;
783 <                                        /* check if time to process it */
784 <        if (rtp->raynum == lastdone+1) {
785 <                int             n = rtp->bpos - rtp->buf;
782 >        struct rtproc   *rtu, *rtl = NULL;
783 >                                        /* insert following ray order */
784 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
785 >                if (rtp->raynum < rtu->raynum)
786 >                        break;
787 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
788 >        if (rtu == NULL)
789 >                error(SYSTEM, "out of memory in queue_raytree");
790 >        *rtu = *rtp;
791 >        if (rtl == NULL) {
792 >                rtu->next = rt_unproc;
793 >                rt_unproc = rtu;
794 >        } else {
795 >                rtu->next = rtl->next;
796 >                rtl->next = rtu;
797 >        }
798 >        rtp->raynum = 0;                /* clear path for next ray tree */
799 >        rtp->bsiz = 0;
800 >        rtp->buf = NULL;
801 >        rtp->nbr = 0;
802 > }
803 >
804 > /* process completed ray trees from our queue */
805 > void
806 > process_queue(void)
807 > {
808 >        char    modname[128];
809 >                                        /* ray-ordered queue */
810 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
811 >                struct rtproc   *rtp = rt_unproc;
812 >                int             n = rtp->nbr;
813                  const char      *cp = rtp->buf;
814                  while (n > 0) {         /* process rays */
815 <                        char    matname[128];
678 <                        char    *mnp = matname;
815 >                        register char   *mnp = modname;
816                                          /* skip leading tabs */
817                          while (n > 0 && *cp == '\t') {
818                                  cp++; n--;
819                          }
820 <                                        /* get material name */
820 >                        if (!n || !(isalpha(*cp) | (*cp == '_')))
821 >                                error(USER, "bad modifier name from rtrace");
822 >                                        /* get modifier name */
823                          while (n > 0 && *cp != '\t') {
824                                  *mnp++ = *cp++; n--;
825                          }
687                        if (!n)
688                                error(USER, "botched modifer name from rtrace");
826                          *mnp = '\0';
827 <                        cp++; n--;      /* skip terminating tab */
827 >                        cp++; n--;      /* eat following tab */
828                          if (n < (int)(sizeof(float)*9))
829                                  error(USER, "incomplete ray value from rtrace");
830                                          /* add ray contribution */
831 <                        add_contrib(matname, (float *)cp);
831 >                        memcpy(rparams, cp, sizeof(float)*9);
832                          cp += sizeof(float)*9; n -= sizeof(float)*9;
833 +                        add_contrib(modname);
834                  }
835                  done_contrib();         /* sum up contributions & output */
836                  lastdone = rtp->raynum;
837 <                free(rtp->buf);
838 <                                        /* catch up with unprocessed list */
839 <                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 <                }
837 >                free(rtp->buf);         /* free up buffer space */
838 >                rt_unproc = rtp->next;
839 >                free(rtp);              /* done with this ray tree */
840          }
723        rtp->raynum = 0;                /* clear path for next ray tree */
724        rtp->bsiz = 0;
725        rtp->buf = NULL;
841   }
842  
843   /* wait for rtrace process to finish with ray tree */
# Line 763 | Line 878 | wait_rproc(void)
878                          if (rt->buf == NULL) {
879                                  rt->bsiz = treebufsiz;
880                                  rt->buf = (char *)malloc(treebufsiz);
881 <                        } else if (rt->bpos + BUFSIZ > rt->buf + rt->bsiz) {
881 >                        } else if (rt->nbr + BUFSIZ > rt->bsiz) {
882                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
883                                          rt->bsiz = treebufsiz;
884                                  else
# Line 772 | Line 887 | wait_rproc(void)
887                          }
888                          if (rt->buf == NULL)
889                                  error(SYSTEM, "out of memory in wait_rproc");
890 <                        nr = read(rt->pd.r, rt->bpos,
891 <                                        rt->buf + rt->bsiz - rt->bpos);
777 <                        if (!nr)
890 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
891 >                        if (nr <= 0)
892                                  error(USER, "rtrace process died");
893 <                        rt->bpos += nr;         /* advance buffer & check */
894 <                        if (rt->bpos[-1] == '\t' && rt->bpos[-2] == '-') {
895 <                                rt->bpos -= 2;  /* elide terminator */
896 <                                process_rays(rt);
893 >                        rt->nbr += nr;          /* advance & check */
894 >                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
895 >                                                        "~\t~\t", 4)) {
896 >                                rt->nbr -= 4;   /* elide terminator */
897 >                                queue_raytree(rt);
898                                  rtfree = rt;    /* ready for next ray */
899                          }
900                  }
# Line 801 | Line 916 | get_rproc(void)
916  
917   /* trace ray contributions (main loop) */
918   void
919 < tracecontribs(FILE *fin)
919 > trace_contribs(FILE *fin)
920   {
921          char            inpbuf[128];
922          int             iblen;
# Line 810 | Line 925 | tracecontribs(FILE *fin)
925          while ((iblen = getinp(inpbuf, fin)) > 0) {
926                  if (lastray+1 < lastray) {      /* counter rollover? */
927                          while (wait_rproc() != NULL)
928 <                                ;
928 >                                process_queue();
929                          lastdone = lastray = 0;
930                  }
931                  rtp = get_rproc();              /* get avail. rtrace process */
932 <                rtp->raynum = ++lastray;        /* assign this ray to it */
932 >                rtp->raynum = ++lastray;        /* assign ray to it */
933                  writebuf(rtp->pd.w, inpbuf, iblen);
934 <                if (!--raysleft)
935 <                        break;                  /* explicit EOF */
934 >                if (raysleft && !--raysleft)
935 >                        break;
936 >                process_queue();                /* catch up with results */
937          }
938          while (wait_rproc() != NULL)            /* process outstanding rays */
939 <                ;
939 >                process_queue();
940 >        if (raysleft)
941 >                error(USER, "unexpected EOF on input");
942 >        lu_done(&ofiletab);                     /* close output files */
943 > }
944 >
945 > /* seek on the given output file */
946 > static int
947 > myseeko(const LUENT *e, void *p)
948 > {
949 >        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
950 >                sprintf(errmsg, "seek error on file '%s'", e->key);
951 >                error(SYSTEM, errmsg);
952 >        }
953 > }
954 >
955 > /* recover output if possible */
956 > void
957 > recover_output(FILE *fin)
958 > {
959 >        off_t   lastout = -1;
960 >        int     outvsiz;
961 >        char    *outvfmt;
962 >        int     i, j;
963 >        MODCONT *mp;
964 >        int     ofl;
965 >        char    oname[1024];
966 >        LUENT   *ment, *oent;
967 >        FILE    *fp;
968 >        off_t   nvals;
969 >
970 >        switch (outfmt) {
971 >        case 'a':
972 >                error(USER, "cannot recover ASCII output");
973 >                return;
974 >        case 'f':
975 >                outvsiz = sizeof(float)*3;
976 >                outvfmt = "float";
977 >                break;
978 >        case 'd':
979 >                outvsiz = sizeof(double)*3;
980 >                outvfmt = "double";
981 >                break;
982 >        case 'c':
983 >                outvsiz = sizeof(COLR);
984 >                outvfmt = COLRFMT;
985 >                break;
986 >        default:
987 >                error(INTERNAL, "botched output format");
988 >                return;
989 >        }
990 >                                                /* check modifier outputs */
991 >        for (i = 0; i < nmods; i++) {
992 >                ment = lu_find(&modconttab,modname[i]);
993 >                mp = (MODCONT *)ment->data;
994 >                if (mp->outspec == NULL)
995 >                        error(USER, "cannot recover from stdout");
996 >                for (j = 0; ; j++) {            /* check each bin's file */
997 >                        ofl = ofname(oname, mp->outspec, mp->modname, j);
998 >                        if (ofl < 0)
999 >                                error(USER, "bad output file specification");
1000 >                        oent = lu_find(&ofiletab, oname);
1001 >                        if (oent->data != NULL) { /* saw this one already */
1002 >                                if (ofl & OF_BIN)
1003 >                                        continue;
1004 >                                break;
1005 >                        }
1006 >                        if (oname[0] == '!')
1007 >                                error(USER, "cannot recover from command");
1008 >                                                /* open output */
1009 >                        fp = fopen(oname, "rb+");
1010 >                        if (fp == NULL)
1011 >                                break;          /* must be end of modifier */
1012 >                        nvals = lseek(fileno(fp), 0, SEEK_END);
1013 >                        if (nvals <= 0) {
1014 >                                lastout = 0;    /* empty output, quit here */
1015 >                                fclose(fp);
1016 >                                break;
1017 >                        }
1018 >                        lseek(fileno(fp), 0, SEEK_SET);
1019 >                        if (header) {
1020 >                                int     xr, yr;
1021 >                                if (checkheader(fp, outvfmt, NULL) != 1) {
1022 >                                        sprintf(errmsg,
1023 >                                                "format mismatch for '%s'",
1024 >                                                        oname);
1025 >                                        error(USER, errmsg);
1026 >                                }
1027 >                                if (xres > 0 && yres > 0 &&
1028 >                                                (!fscnresolu(&xr, &yr, fp) ||
1029 >                                                        xr != xres ||
1030 >                                                        yr != yres)) {
1031 >                                        sprintf(errmsg,
1032 >                                                "resolution mismatch for '%s'",
1033 >                                                        oname);
1034 >                                        error(USER, errmsg);
1035 >                                }
1036 >                        }
1037 >                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1038 >                        if (lastout < 0 || nvals < lastout)
1039 >                                lastout = nvals;
1040 >                        if (oent->key == NULL) /* new entry */
1041 >                                oent->key = strcpy((char *)
1042 >                                                malloc(strlen(oname)+1), oname);
1043 >                        oent->data = (char *)fp;
1044 >                        if (!(ofl & OF_BIN))
1045 >                                break;          /* no bin separation */
1046 >                }
1047 >                if (!lastout) {                 /* empty output */
1048 >                        error(WARNING, "no data to recover");
1049 >                        lu_done(&ofiletab);     /* reclose all outputs */
1050 >                        return;
1051 >                }
1052 >                if (j > mp->nbins) {            /* preallocate modifier bins */
1053 >                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1054 >                                                        (j-1)*sizeof(DCOLOR));
1055 >                        if (mp == NULL)
1056 >                                error(SYSTEM, "out of memory in recover_output");
1057 >                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1058 >                        mp->nbins = j;
1059 >                        ment->data = (char *)mp;
1060 >                }
1061 >        }
1062 >        if (lastout < 0) {
1063 >                error(WARNING, "no existing output to recover");
1064 >                return;
1065 >        }
1066 >                                                /* seek on all files */
1067 >        nvals = lastout * outvsiz;
1068 >        lu_doall(&ofiletab, myseeko, &nvals);
1069 >                                                /* discard input */
1070 >        for (nvals = 0; nvals < lastout; nvals++)
1071 >                if (getinp(oname, fin) <= 0)
1072 >                        error(USER, "unexpected EOF on input");
1073 >        lastray = lastdone = (unsigned long)lastout;
1074 >        if (raysleft)
1075 >                raysleft -= lastray;
1076   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines