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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines