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.11 by greg, Thu Jun 2 18:51:46 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 66 | Line 60 | FILE *getofile(const char *ospec, const char *mname, i
60   struct rtproc {
61          struct rtproc   *next;          /* next in list of processes */
62          SUBPROC         pd;             /* rtrace pipe descriptors */
63 <        unsigned long   raynum;         /* ray number for this buffer */
64 <        int             bsiz;           /* rtrace buffer length */
65 <        char            *buf;           /* rtrace i/o buffer */
66 <        char            *bpos;          /* current buffer position */
67 < };                              /* rtrace process */
63 >        unsigned long   raynum;         /* ray number for this tree */
64 >        int             bsiz;           /* ray tree buffer length */
65 >        char            *buf;           /* ray tree buffer */
66 >        int             nbr;            /* number of bytes from rtrace */
67 > };                              /* rtrace process buffer */
68  
69                                          /* rtrace command and defaults */
70   char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
71 <                                "-ab", "1", "-ad", "512", };
72 < int  rtargc = 9;
71 >                                "-ab", "1", "-ad", "128", "-lr", "-10", };
72 > int  rtargc = 11;
73                                          /* overriding rtrace options */
74 < char            *myrtopts[] = { "-o-TmWdp", "-h-",
75 <                                "-x", "1", "-y", "0", NULL };
74 > char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
75 >                                "-dt", "0", "-as", "0", "-aa", "0", NULL };
76  
77   struct rtproc   rt0;                    /* head of rtrace process list */
78  
79   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
80  
81   char    persistfn[] = "pfXXXXXX";       /* persist file name */
88 char    fmt[8];                         /* rtrace i/o format */
82  
83   int             gargc;                  /* global argc */
84   char            **gargv;                /* global argv */
# Line 106 | Line 99 | long           waitflush;              /* how long until next flush */
99   unsigned long   lastray = 0;            /* last ray number sent */
100   unsigned long   lastdone = 0;           /* last ray processed */
101  
102 + int             using_stdout = 0;       /* are we using stdout? */
103 +
104   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
105   int             nmods = 0;              /* number of modifiers */
106  
107   MODCONT *addmodifier(char *modn, char *outf, char *binv);
108  
114 int done_rprocs(struct rtproc *rtp);
109   void init(int np);
110 < void tracecontribs(FILE *fp);
110 > int done_rprocs(struct rtproc *rtp);
111 > void trace_contribs(FILE *fp);
112   struct rtproc *wait_rproc(void);
113   struct rtproc *get_rproc(void);
114 < void process_rays(struct rtproc *rtp);
114 > void queue_raytree(struct rtproc *rtp);
115 > void process_queue(void);
116  
117 < void add_contrib(const char *modn, float rayval[7]);
117 > void putcontrib(const DCOLOR cnt, FILE *fout);
118 > void add_contrib(const char *modn);
119   void done_contrib(void);
120  
121   /* set input/output format */
# Line 126 | Line 123 | static void
123   setformat(const char *fmt)
124   {
125          switch (fmt[0]) {
129        case 'a':
126          case 'f':
127          case 'd':
128 +                SET_FILE_BINARY(stdin);
129 +                /* fall through */
130 +        case 'a':
131                  inpfmt = fmt[0];
132                  break;
133          default:
# Line 161 | Line 160 | main(int argc, char *argv[])
160          int     nprocs = 1;
161          char    *curout = NULL;
162          char    *binval = NULL;
163 <        int     i;
164 <                                /* set global argument list */
165 <        gargc = argc;
163 >        char    fmt[8];
164 >        int     i, j;
165 >                                /* global program name */
166          gargv = argv;
167 <                                /* set up calcomp functions */
167 >                                /* initialize calcomp routines */
168          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
169          esupport &= ~(E_OUTCHAN);
170 +        varset("PI", ':', PI);
171                                  /* get our options */
172 <        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
173 <                switch (argv[i][1]) {
174 <                case 'n':                       /* number of processes */
175 <                        if (argv[i][2] || i >= argc-1) break;
176 <                        nprocs = atoi(argv[++i]);
177 <                        if (nprocs <= 0)
178 <                                error(USER, "illegal number of processes");
179 <                        continue;
180 <                case 'h':                       /* output header? */
181 <                        switch (argv[i][2]) {
182 <                        case '\0':
183 <                                header = !header;
172 >        for (i = 1; i < argc-1; i++) {
173 >                                                /* expand arguments */
174 >                while ((j = expandarg(&argc, &argv, i)) > 0)
175 >                        ;
176 >                if (j < 0) {
177 >                        fprintf(stderr, "%s: cannot expand '%s'",
178 >                                        argv[0], argv[i]);
179 >                        exit(1);
180 >                }
181 >                if (argv[i][0] == '-')
182 >                        switch (argv[i][1]) {
183 >                        case 'n':               /* number of processes */
184 >                                if (argv[i][2] || i >= argc-1) break;
185 >                                nprocs = atoi(argv[++i]);
186 >                                if (nprocs <= 0)
187 >                                        error(USER, "illegal number of processes");
188                                  continue;
189 <                        case '+': case '1': case 'T': case 't':
190 <                                header = 1;
189 >                        case 'h':               /* output header? */
190 >                                switch (argv[i][2]) {
191 >                                case '\0':
192 >                                        header = !header;
193 >                                        continue;
194 >                                case '+': case '1':
195 >                                case 'T': case 't':
196 >                                case 'Y': case 'y':
197 >                                        header = 1;
198 >                                        continue;
199 >                                case '-': case '0':
200 >                                case 'F': case 'f':
201 >                                case 'N': case 'n':
202 >                                        header = 0;
203 >                                        continue;
204 >                                }
205 >                                break;
206 >                        case 'f':               /* file or i/o format */
207 >                                if (!argv[i][2]) {
208 >                                        char    *fpath;
209 >                                        if (i >= argc-1) break;
210 >                                        fpath = getpath(argv[++i],
211 >                                                        getrlibpath(), R_OK);
212 >                                        if (fpath == NULL) {
213 >                                                sprintf(errmsg,
214 >                                                        "cannot find file '%s'",
215 >                                                                argv[i]);
216 >                                                error(USER, errmsg);
217 >                                        }
218 >                                        fcompile(fpath);
219 >                                        continue;
220 >                                }
221 >                                setformat(argv[i]+2);
222                                  continue;
223 <                        case '-': case '0': case 'F': case 'f':
224 <                                header = 0;
223 >                        case 'e':               /* expression */
224 >                                if (argv[i][2] || i >= argc-1) break;
225 >                                scompile(argv[++i], NULL, 0);
226                                  continue;
227 <                        }
228 <                        break;
229 <                case 'f':                       /* file or i/o format */
194 <                        if (!argv[i][2]) {
195 <                                if (i >= argc-1) break;
196 <                                fcompile(argv[++i]);
227 >                        case 'o':               /* output file spec. */
228 >                                if (argv[i][2] || i >= argc-1) break;
229 >                                curout = argv[++i];
230                                  continue;
231 +                        case 'x':               /* horiz. output resolution */
232 +                                if (argv[i][2] || i >= argc-1) break;
233 +                                xres = atoi(argv[++i]);
234 +                                continue;
235 +                        case 'y':               /* vert. output resolution */
236 +                                if (argv[i][2] || i >= argc-1) break;
237 +                                yres = atoi(argv[++i]);
238 +                                continue;
239 +                        case 'b':               /* bin expression */
240 +                                if (argv[i][2] || i >= argc-1) break;
241 +                                binval = argv[++i];
242 +                                continue;
243 +                        case 'm':               /* modifier name */
244 +                                if (argv[i][2] || i >= argc-1) break;
245 +                                rtargv[rtargc++] = "-ti";
246 +                                rtargv[rtargc++] = argv[++i];
247 +                                addmodifier(argv[i], curout, binval);
248 +                                continue;
249                          }
250 <                        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 */
250 >                rtargv[rtargc++] = argv[i];     /* assume rtrace option */
251          }
252 <        for ( ; i < argc; i++)  /* transfer rtrace-specific options */
253 <                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 <        }
252 >                                /* set global argument list */
253 >        gargc = argc; gargv = argv;
254                                  /* add "mandatory" rtrace settings */
255 <        for (i = 0; myrtopts[i] != NULL; i++)
256 <                rtargv[rtargc++] = myrtopts[i];
257 <        if (!nprocs) {          /* just asking for defaults? */
255 >        for (j = 0; myrtopts[j] != NULL; j++)
256 >                rtargv[rtargc++] = myrtopts[j];
257 >                                /* just asking for defaults? */
258 >        if (!strcmp(argv[i], "-defaults")) {
259                  char    sxres[16], syres[16];
260                  char    *rtpath;
261 <                printf("-n  1\t\t\t\t# number of processes\n", nprocs);
261 >                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
262                  fflush(stdout);                 /* report OUR options */
263                  rtargv[rtargc++] = header ? "-h+" : "-h-";
264                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 256 | Line 269 | main(int argc, char *argv[])
269                  rtargv[rtargc++] = "-y";
270                  sprintf(syres, "%d", yres);
271                  rtargv[rtargc++] = syres;
272 <                rtargv[rtargc++] = "-oW";
272 >                rtargv[rtargc++] = "-oTW";
273                  rtargv[rtargc++] = "-defaults";
274                  rtargv[rtargc] = NULL;
275                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 269 | Line 282 | main(int argc, char *argv[])
282                  perror(rtpath); /* execv() should not return */
283                  exit(1);
284          }
285 <        if (!nmods)
286 <                error(USER, "No modifiers specified");
287 <        if (argc < 2 || argv[argc-1][0] == '-')
288 <                error(USER, "missing octree argument");
285 >        if (nprocs > 1) {       /* add persist file if parallel */
286 >                rtargv[rtargc++] = "-PP";
287 >                rtargv[rtargc++] = mktemp(persistfn);
288 >        }
289                                  /* add format string */
290          sprintf(fmt, "-f%cf", inpfmt);
291          rtargv[rtargc++] = fmt;
292                                  /* octree argument is last */
293 <        rtargv[rtargc++] = octree = argv[argc-1];
293 >        if (i <= 0 || i != argc-1 || argv[i][0] == '-')
294 >                error(USER, "missing octree argument");
295 >        rtargv[rtargc++] = octree = argv[i];
296          rtargv[rtargc] = NULL;
297 <                                /* start rtrace & sum contributions */
297 >                                /* start rtrace & compute contributions */
298          init(nprocs);
299 <        tracecontribs(stdin);
299 >        trace_contribs(stdin);
300          quit(0);
301   }
302  
# Line 333 | Line 348 | quit(int status)
348          exit(status);                   /* flushes all output streams */
349   }
350  
351 < /* start rtrace and initialize buffers */
351 > /* start rtrace processes and initialize */
352   void
353   init(int np)
354   {
355          struct rtproc   *rtp;
356          int     i;
357          int     maxbytes;
358 +                                        /* make sure we have something to do */
359 +        if (!nmods)
360 +                error(USER, "No modifiers specified");
361                                          /* assign ray variables */
362          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
363          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
364                                          /* set up signal handling */
365 < #ifdef SIGPIPE /* not present on Windows */
365 >        signal(SIGINT, quit);
366 > #ifdef SIGHUP
367 >        signal(SIGHUP, quit);
368 > #endif
369 > #ifdef SIGTERM
370 >        signal(SIGTERM, quit);
371 > #endif
372 > #ifdef SIGPIPE
373          signal(SIGPIPE, quit);
374   #endif
375          rtp = &rt0;                     /* start rtrace process(es) */
# Line 360 | Line 385 | init(int np)
385                          error(SYSTEM, "cannot start rtrace process");
386                  if (maxbytes > treebufsiz)
387                          treebufsiz = maxbytes;
388 +                rtp->raynum = 0;
389                  rtp->bsiz = 0;
390                  rtp->buf = NULL;
391 <                rtp->raynum = 0;
391 >                rtp->nbr = 0;
392                  if (i == np)            /* last process? */
393                          break;
394                  if (i == 1)
# Line 437 | Line 463 | printheader(FILE *fout)
463          
464          if (fin == NULL)
465                  quit(1);
466 <        getheader(fin, (gethfunc *)fputs, fout);        /* copy octree header */
466 >        checkheader(fin, "ignore", fout);       /* copy octree header */
467          fclose(fin);
468          printargs(gargc-1, gargv, fout);        /* add our command */
469          fprintf(fout, "SOFTWARE= %s\n", VersionID);
# Line 468 | Line 494 | printheader(FILE *fout)
494   FILE *
495   getofile(const char *ospec, const char *mname, int bn)
496   {
471        static int      using_stdout = 0;
497          const char      *mnp = NULL;
498          const char      *bnp = NULL;
499          const char      *cp;
# Line 476 | Line 501 | getofile(const char *ospec, const char *mname, int bn)
501          LUENT           *lep;
502          
503          if (ospec == NULL) {                    /* use stdout? */
504 <                if (!using_stdout && header)
505 <                        printheader(stdout);
504 >                if (!using_stdout) {
505 >                        if (outfmt != 'a')
506 >                                SET_FILE_BINARY(stdout);
507 >                        if (header)
508 >                                printheader(stdout);
509 >                }
510                  using_stdout = 1;
511                  return stdout;
512          }
# Line 519 | Line 548 | getofile(const char *ospec, const char *mname, int bn)
548          if (lep->key == NULL)                   /* new entry */
549                  lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
550          if (lep->data == NULL) {                /* open output file */
551 <                lep->data = (char *)fopen(ofname, "w");
552 <                if (lep->data == NULL) {
551 >                FILE            *fp;
552 >                int             i;
553 >                if (ofname[0] == '!')           /* output to command */
554 >                        fp = popen(ofname+1, "w");
555 >                else
556 >                        fp = fopen(ofname, "w");
557 >                if (fp == NULL) {
558                          sprintf(errmsg, "cannot open '%s' for writing", ofname);
559                          error(SYSTEM, errmsg);
560                  }
561 +                if (outfmt != 'a')
562 +                        SET_FILE_BINARY(fp);
563                  if (header)
564 <                        printheader((FILE *)lep->data);
564 >                        printheader(fp);
565 >                                                /* play catch-up */
566 >                for (i = 0; i < lastdone; i++) {
567 >                        static const DCOLOR     nocontrib = BLKCOLOR;
568 >                        putcontrib(nocontrib, fp);
569 >                        if (outfmt == 'a')
570 >                                putc('\n', fp);
571 >                }
572 >                if (xres > 0)
573 >                        fflush(fp);
574 >                lep->data = (char *)fp;
575          }
576          return (FILE *)lep->data;               /* return open file pointer */
577   badspec:
# Line 538 | Line 584 | badspec:
584   int
585   getinp(char *buf, FILE *fp)
586   {
587 +        char    *cp;
588 +        int     i;
589 +
590          switch (inpfmt) {
591          case 'a':
592 <                if (fgets(buf, 128, fp) == NULL)
593 <                        return 0;
592 >                cp = buf;               /* make sure we get 6 floats */
593 >                for (i = 0; i < 6; i++) {
594 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
595 >                                return 0;
596 >                        if ((cp = fskip(cp)) == NULL || *cp)
597 >                                return 0;
598 >                        *cp++ = ' ';
599 >                }
600 >                getc(fp);               /* get/put eol */
601 >                *cp-- = '\0'; *cp = '\n';
602                  return strlen(buf);
603          case 'f':
604                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 556 | Line 613 | getinp(char *buf, FILE *fp)
613          return 0;       /* pro forma return */
614   }
615  
616 < static const float      *rparams = NULL;        /* ray parameter pointer */
616 > static float    rparams[9];             /* traced ray parameters */
617  
618   /* return channel (ray) value */
619   double
# Line 564 | Line 621 | chanvalue(int n)
621   {
622          if (--n < 0 || n >= 6)
623                  error(USER, "illegal channel number ($N)");
624 <        if (rparams == NULL)
568 <                error(USER, "illegal use of $N in constant expression");
569 <        return rparams[n];
624 >        return rparams[n+3];
625   }
626  
627 < /* add ray contribution to the appropriate modifier bin */
627 > /* add current ray contribution to the appropriate modifier bin */
628   void
629 < add_contrib(const char *modn, float rayval[9])
629 > add_contrib(const char *modn)
630   {
631          LUENT   *le = lu_find(&modconttab, modn);
632          MODCONT *mp = (MODCONT *)le->data;
# Line 581 | Line 636 | add_contrib(const char *modn, float rayval[9])
636                  sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn);
637                  error(USER, errmsg);
638          }
639 <        rparams = rayval + 3;           /* for chanvalue */
585 <        eclock++;
639 >        eclock++;                       /* get bin number */
640          bn = (int)(evalue(mp->binv) + .5);
641          if (bn <= 0)
642                  bn = 0;
643          else if (bn > mp->nbins) {      /* new bin */
644                  mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
645 <                                                bn*sizeof(COLOR));
645 >                                                bn*sizeof(DCOLOR));
646                  if (mp == NULL)
647                          error(SYSTEM, "out of memory in add_contrib");
648 <                memset(mp->cbin+mp->nbins, 0, sizeof(COLOR)*(bn+1-mp->nbins));
648 >                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
649                  mp->nbins = bn+1;
650                  le->data = (char *)mp;
651          }
652 <        addcolor(mp->cbin[bn], rayval);
599 <        rparams = NULL;
652 >        addcolor(mp->cbin[bn], rparams);
653   }
654  
655   /* output newline to ASCII file and/or flush as requested */
# Line 616 | Line 669 | puteol(const LUENT *e, void *p)
669          return 0;
670   }
671  
672 + /* put out ray contribution to file */
673 + void
674 + putcontrib(const DCOLOR cnt, FILE *fout)
675 + {
676 +        float   fv[3];
677 +        COLR    cv;
678 +
679 +        switch (outfmt) {
680 +        case 'a':
681 +                fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
682 +                break;
683 +        case 'f':
684 +                fv[0] = cnt[0];
685 +                fv[1] = cnt[1];
686 +                fv[2] = cnt[2];
687 +                fwrite(fv, sizeof(float), 3, fout);
688 +                break;
689 +        case 'd':
690 +                fwrite(cnt, sizeof(double), 3, fout);
691 +                break;
692 +        case 'c':
693 +                setcolr(cv, cnt[0], cnt[1], cnt[2]);
694 +                fwrite(cv, sizeof(cv), 1, fout);
695 +                break;
696 +        default:
697 +                error(INTERNAL, "botched output format");
698 +        }
699 + }
700 +
701   /* output ray tallies and clear for next primary */
702   void
703   done_contrib(void)
704   {
705          int     i, j;
706          MODCONT *mp;
625        FILE    *fout;
626        double  dv[3];
627        COLR    cv;
707                                                  /* output modifiers in order */
708          for (i = 0; i < nmods; i++) {
709                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
710 <                for (j = 0; j < mp->nbins; j++) {
711 <                        fout = getofile(mp->outspec, mp->modname, j);
712 <                        switch (outfmt) {
634 <                        case 'a':
635 <                                fprintf(fout, "%.6e\t%.6e\t%.6e\t",
636 <                                                mp->cbin[j][RED],
637 <                                                mp->cbin[j][GRN],
638 <                                                mp->cbin[j][BLU]);
639 <                                break;
640 <                        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 <                }
710 >                for (j = 0; j < mp->nbins; j++)
711 >                        putcontrib(mp->cbin[j],
712 >                                        getofile(mp->outspec, mp->modname, j));
713                                                  /* clear for next ray tree */
714 <                memset(mp->cbin, 0, sizeof(COLOR)*mp->nbins);
714 >                memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
715          }
716          --waitflush;                            /* terminate records */
717          lu_doall(&ofiletab, puteol, NULL);
718 <        if (!waitflush)
718 >        if (using_stdout & (outfmt == 'a'))
719 >                putc('\n', stdout);
720 >        if (!waitflush) {
721                  waitflush = xres;
722 +                if (using_stdout)
723 +                        fflush(stdout);
724 +        }
725   }
726  
727 < /* process (or save) ray tree produced by rtrace process */
727 > /* queue completed ray tree produced by rtrace process */
728   void
729 < process_rays(struct rtproc *rtp)
729 > queue_raytree(struct rtproc *rtp)
730   {
731 <        struct rtproc   *rtu;
732 <                                        /* check if time to process it */
733 <        if (rtp->raynum == lastdone+1) {
734 <                int             n = rtp->bpos - rtp->buf;
731 >        struct rtproc   *rtu, *rtl = NULL;
732 >                                        /* insert following ray order */
733 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
734 >                if (rtp->raynum < rtu->raynum)
735 >                        break;
736 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
737 >        if (rtu == NULL)
738 >                error(SYSTEM, "out of memory in queue_raytree");
739 >        *rtu = *rtp;
740 >        if (rtl == NULL) {
741 >                rtu->next = rt_unproc;
742 >                rt_unproc = rtu;
743 >        } else {
744 >                rtu->next = rtl->next;
745 >                rtl->next = rtu;
746 >        }
747 >        rtp->raynum = 0;                /* clear path for next ray tree */
748 >        rtp->bsiz = 0;
749 >        rtp->buf = NULL;
750 >        rtp->nbr = 0;
751 > }
752 >
753 > /* process completed ray trees from our queue */
754 > void
755 > process_queue(void)
756 > {
757 >        char    modname[128];
758 >                                        /* ray-ordered queue */
759 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
760 >                struct rtproc   *rtp = rt_unproc;
761 >                int             n = rtp->nbr;
762                  const char      *cp = rtp->buf;
763                  while (n > 0) {         /* process rays */
764 <                        char    matname[128];
678 <                        char    *mnp = matname;
764 >                        register char   *mnp = modname;
765                                          /* skip leading tabs */
766                          while (n > 0 && *cp == '\t') {
767                                  cp++; n--;
768                          }
769 <                                        /* get material name */
769 >                        if (!n || !(isalpha(*cp) | (*cp == '_')))
770 >                                error(USER, "bad modifier name from rtrace");
771 >                                        /* get modifier name */
772                          while (n > 0 && *cp != '\t') {
773                                  *mnp++ = *cp++; n--;
774                          }
687                        if (!n)
688                                error(USER, "botched modifer name from rtrace");
775                          *mnp = '\0';
776 <                        cp++; n--;      /* skip terminating tab */
776 >                        cp++; n--;      /* eat following tab */
777                          if (n < (int)(sizeof(float)*9))
778                                  error(USER, "incomplete ray value from rtrace");
779                                          /* add ray contribution */
780 <                        add_contrib(matname, (float *)cp);
780 >                        memcpy(rparams, cp, sizeof(float)*9);
781                          cp += sizeof(float)*9; n -= sizeof(float)*9;
782 +                        add_contrib(modname);
783                  }
784                  done_contrib();         /* sum up contributions & output */
785                  lastdone = rtp->raynum;
786 <                free(rtp->buf);
787 <                                        /* catch up with unprocessed list */
788 <                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 <                }
786 >                free(rtp->buf);         /* free up buffer space */
787 >                rt_unproc = rtp->next;
788 >                free(rtp);              /* done with this ray tree */
789          }
723        rtp->raynum = 0;                /* clear path for next ray tree */
724        rtp->bsiz = 0;
725        rtp->buf = NULL;
790   }
791  
792   /* wait for rtrace process to finish with ray tree */
# Line 763 | Line 827 | wait_rproc(void)
827                          if (rt->buf == NULL) {
828                                  rt->bsiz = treebufsiz;
829                                  rt->buf = (char *)malloc(treebufsiz);
830 <                        } else if (rt->bpos + BUFSIZ > rt->buf + rt->bsiz) {
830 >                        } else if (rt->nbr + BUFSIZ > rt->bsiz) {
831                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
832                                          rt->bsiz = treebufsiz;
833                                  else
# Line 772 | Line 836 | wait_rproc(void)
836                          }
837                          if (rt->buf == NULL)
838                                  error(SYSTEM, "out of memory in wait_rproc");
839 <                        nr = read(rt->pd.r, rt->bpos,
840 <                                        rt->buf + rt->bsiz - rt->bpos);
777 <                        if (!nr)
839 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
840 >                        if (nr <= 0)
841                                  error(USER, "rtrace process died");
842 <                        rt->bpos += nr;         /* advance buffer & check */
843 <                        if (rt->bpos[-1] == '\t' && rt->bpos[-2] == '-') {
844 <                                rt->bpos -= 2;  /* elide terminator */
845 <                                process_rays(rt);
842 >                        rt->nbr += nr;          /* advance & check */
843 >                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
844 >                                                        "~\t~\t", 4)) {
845 >                                rt->nbr -= 4;   /* elide terminator */
846 >                                queue_raytree(rt);
847                                  rtfree = rt;    /* ready for next ray */
848                          }
849                  }
# Line 801 | Line 865 | get_rproc(void)
865  
866   /* trace ray contributions (main loop) */
867   void
868 < tracecontribs(FILE *fin)
868 > trace_contribs(FILE *fin)
869   {
870          char            inpbuf[128];
871          int             iblen;
# Line 810 | Line 874 | tracecontribs(FILE *fin)
874          while ((iblen = getinp(inpbuf, fin)) > 0) {
875                  if (lastray+1 < lastray) {      /* counter rollover? */
876                          while (wait_rproc() != NULL)
877 <                                ;
877 >                                process_queue();
878                          lastdone = lastray = 0;
879                  }
880                  rtp = get_rproc();              /* get avail. rtrace process */
881 <                rtp->raynum = ++lastray;        /* assign this ray to it */
881 >                rtp->raynum = ++lastray;        /* assign ray to it */
882                  writebuf(rtp->pd.w, inpbuf, iblen);
883                  if (!--raysleft)
884 <                        break;                  /* explicit EOF */
884 >                        break;
885 >                process_queue();                /* catch up with results */
886          }
887          while (wait_rproc() != NULL)            /* process outstanding rays */
888 <                ;
888 >                process_queue();
889 >        if (raysleft > 0)
890 >                error(USER, "unexpected EOF on input");
891   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines