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.6 by greg, Sat May 28 22:27:54 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>
9 #include  "rtio.h"
10 #include  "rterror.h"
10   #include  "platform.h"
11   #include  "rtprocess.h"
12   #include  "selcall.h"
# Line 16 | Line 15 | static const char RCSid[] = "$Id$";
15   #include  "lookup.h"
16   #include  "calcomp.h"
17  
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
18   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
19  
20   int     treebufsiz = BUFSIZ;            /* current tree buffer size */
21  
22 + typedef double  DCOLOR[3];              /* double-precision color */
23 +
24   /*
25   * The modcont structure is used to accumulate ray contributions
26   * for a particular modifier, which may be subdivided into bins
# Line 43 | Line 36 | typedef struct {
36          const char      *modname;       /* modifier name */
37          EPNODE          *binv;          /* bin value expression */
38          int             nbins;          /* number of accumulation bins */
39 <        COLOR           cbin[1];        /* contribution bins (extends struct) */
39 >        DCOLOR          cbin[1];        /* contribution bins (extends struct) */
40   } MODCONT;                      /* modifier contribution */
41  
42   static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); }
# Line 66 | Line 59 | FILE *getofile(const char *ospec, const char *mname, i
59   struct rtproc {
60          struct rtproc   *next;          /* next in list of processes */
61          SUBPROC         pd;             /* rtrace pipe descriptors */
62 <        unsigned long   raynum;         /* ray number for this buffer */
63 <        int             bsiz;           /* rtrace buffer length */
64 <        char            *buf;           /* rtrace i/o buffer */
65 <        char            *bpos;          /* current buffer position */
66 < };                              /* rtrace process */
62 >        unsigned long   raynum;         /* ray number for this tree */
63 >        int             bsiz;           /* ray tree buffer length */
64 >        char            *buf;           /* ray tree buffer */
65 >        int             nbr;            /* number of bytes from rtrace */
66 > };                              /* rtrace process buffer */
67  
68                                          /* rtrace command and defaults */
69   char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
70 <                                "-ab", "1", "-ad", "512", };
70 >                                "-ab", "1", "-ad", "128", };
71   int  rtargc = 9;
72                                          /* overriding rtrace options */
73 < char            *myrtopts[] = { "-o-TmWdp", "-h-",
74 <                                "-x", "1", "-y", "0", NULL };
73 > char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
74 >                                "-dt", "0", "-as", "0", "-aa", "0", NULL };
75  
76   struct rtproc   rt0;                    /* head of rtrace process list */
77  
78   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
79  
80   char    persistfn[] = "pfXXXXXX";       /* persist file name */
88 char    fmt[8];                         /* rtrace i/o format */
81  
82   int             gargc;                  /* global argc */
83   char            **gargv;                /* global argv */
# Line 106 | Line 98 | long           waitflush;              /* how long until next flush */
98   unsigned long   lastray = 0;            /* last ray number sent */
99   unsigned long   lastdone = 0;           /* last ray processed */
100  
101 + int             using_stdout = 0;       /* are we using stdout? */
102 +
103   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
104   int             nmods = 0;              /* number of modifiers */
105  
106   MODCONT *addmodifier(char *modn, char *outf, char *binv);
107  
114 int done_rprocs(struct rtproc *rtp);
108   void init(int np);
109 < void tracecontribs(FILE *fp);
109 > int done_rprocs(struct rtproc *rtp);
110 > void trace_contribs(FILE *fp);
111   struct rtproc *wait_rproc(void);
112   struct rtproc *get_rproc(void);
113 < void process_rays(struct rtproc *rtp);
113 > void queue_raytree(struct rtproc *rtp);
114 > void process_queue(void);
115  
116 < void add_contrib(const char *modn, float rayval[7]);
116 > void putcontrib(const DCOLOR cnt, FILE *fout);
117 > void add_contrib(const char *modn);
118   void done_contrib(void);
119  
120   /* set input/output format */
# Line 161 | Line 157 | main(int argc, char *argv[])
157          int     nprocs = 1;
158          char    *curout = NULL;
159          char    *binval = NULL;
160 <        int     i;
161 <                                /* set global argument list */
162 <        gargc = argc;
160 >        char    fmt[8];
161 >        int     i, j;
162 >                                /* global program name */
163          gargv = argv;
164 <                                /* set up calcomp functions */
164 >                                /* set up calcomp mode */
165          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
166          esupport &= ~(E_OUTCHAN);
167                                  /* get our options */
168 <        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
169 <                switch (argv[i][1]) {
170 <                case 'n':                       /* number of processes */
171 <                        if (argv[i][2] || i >= argc-1) break;
172 <                        nprocs = atoi(argv[++i]);
173 <                        if (nprocs <= 0)
174 <                                error(USER, "illegal number of processes");
175 <                        continue;
176 <                case 'h':                       /* output header? */
177 <                        switch (argv[i][2]) {
178 <                        case '\0':
179 <                                header = !header;
168 >        for (i = 1; i < argc-1; i++) {
169 >                                                /* expand arguments */
170 >                while ((j = expandarg(&argc, &argv, i)) > 0)
171 >                        ;
172 >                if (j < 0) {
173 >                        fprintf(stderr, "%s: cannot expand '%s'",
174 >                                        argv[0], argv[i]);
175 >                        exit(1);
176 >                }
177 >                if (argv[i][0] == '-')
178 >                        switch (argv[i][1]) {
179 >                        case 'n':               /* number of processes */
180 >                                if (argv[i][2] || i >= argc-1) break;
181 >                                nprocs = atoi(argv[++i]);
182 >                                if (nprocs <= 0)
183 >                                        error(USER, "illegal number of processes");
184                                  continue;
185 <                        case '+': case '1': case 'T': case 't':
186 <                                header = 1;
185 >                        case 'h':               /* output header? */
186 >                                switch (argv[i][2]) {
187 >                                case '\0':
188 >                                        header = !header;
189 >                                        continue;
190 >                                case '+': case '1':
191 >                                case 'T': case 't':
192 >                                case 'Y': case 'y':
193 >                                        header = 1;
194 >                                        continue;
195 >                                case '-': case '0':
196 >                                case 'F': case 'f':
197 >                                case 'N': case 'n':
198 >                                        header = 0;
199 >                                        continue;
200 >                                }
201 >                                break;
202 >                        case 'f':               /* file or i/o format */
203 >                                if (!argv[i][2]) {
204 >                                        if (i >= argc-1) break;
205 >                                        fcompile(argv[++i]);
206 >                                        continue;
207 >                                }
208 >                                setformat(argv[i]+2);
209                                  continue;
210 <                        case '-': case '0': case 'F': case 'f':
211 <                                header = 0;
210 >                        case 'e':               /* expression */
211 >                                if (argv[i][2] || i >= argc-1) break;
212 >                                scompile(argv[++i], NULL, 0);
213                                  continue;
214 <                        }
215 <                        break;
216 <                case 'f':                       /* file or i/o format */
194 <                        if (!argv[i][2]) {
195 <                                if (i >= argc-1) break;
196 <                                fcompile(argv[++i]);
214 >                        case 'o':               /* output file spec. */
215 >                                if (argv[i][2] || i >= argc-1) break;
216 >                                curout = argv[++i];
217                                  continue;
218 +                        case 'x':               /* horiz. output resolution */
219 +                                if (argv[i][2] || i >= argc-1) break;
220 +                                xres = atoi(argv[++i]);
221 +                                continue;
222 +                        case 'y':               /* vert. output resolution */
223 +                                if (argv[i][2] || i >= argc-1) break;
224 +                                yres = atoi(argv[++i]);
225 +                                continue;
226 +                        case 'b':               /* bin expression */
227 +                                if (argv[i][2] || i >= argc-1) break;
228 +                                binval = argv[++i];
229 +                                continue;
230 +                        case 'm':               /* modifier name */
231 +                                if (argv[i][2] || i >= argc-1) break;
232 +                                rtargv[rtargc++] = "-ti";
233 +                                rtargv[rtargc++] = argv[++i];
234 +                                addmodifier(argv[i], curout, binval);
235 +                                continue;
236                          }
237 <                        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 */
237 >                rtargv[rtargc++] = argv[i];     /* assume rtrace option */
238          }
239 <        for ( ; i < argc; i++)  /* transfer rtrace-specific options */
240 <                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 <        }
239 >                                /* set global argument list */
240 >        gargc = argc; gargv = argv;
241                                  /* add "mandatory" rtrace settings */
242 <        for (i = 0; myrtopts[i] != NULL; i++)
243 <                rtargv[rtargc++] = myrtopts[i];
244 <        if (!nprocs) {          /* just asking for defaults? */
242 >        for (j = 0; myrtopts[j] != NULL; j++)
243 >                rtargv[rtargc++] = myrtopts[j];
244 >                                /* just asking for defaults? */
245 >        if (!strcmp(argv[i], "-defaults")) {
246                  char    sxres[16], syres[16];
247                  char    *rtpath;
248 <                printf("-n  1\t\t\t\t# number of processes\n", nprocs);
248 >                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
249                  fflush(stdout);                 /* report OUR options */
250                  rtargv[rtargc++] = header ? "-h+" : "-h-";
251                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 256 | Line 256 | main(int argc, char *argv[])
256                  rtargv[rtargc++] = "-y";
257                  sprintf(syres, "%d", yres);
258                  rtargv[rtargc++] = syres;
259 <                rtargv[rtargc++] = "-oW";
259 >                rtargv[rtargc++] = "-oTW";
260                  rtargv[rtargc++] = "-defaults";
261                  rtargv[rtargc] = NULL;
262                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 269 | Line 269 | main(int argc, char *argv[])
269                  perror(rtpath); /* execv() should not return */
270                  exit(1);
271          }
272 <        if (!nmods)
273 <                error(USER, "No modifiers specified");
274 <        if (argc < 2 || argv[argc-1][0] == '-')
275 <                error(USER, "missing octree argument");
272 >        if (nprocs > 1) {       /* add persist file if parallel */
273 >                rtargv[rtargc++] = "-PP";
274 >                rtargv[rtargc++] = mktemp(persistfn);
275 >        }
276                                  /* add format string */
277          sprintf(fmt, "-f%cf", inpfmt);
278          rtargv[rtargc++] = fmt;
279                                  /* octree argument is last */
280 <        rtargv[rtargc++] = octree = argv[argc-1];
280 >        if (i <= 0 || i != argc-1 || argv[i][0] == '-')
281 >                error(USER, "missing octree argument");
282 >        rtargv[rtargc++] = octree = argv[i];
283          rtargv[rtargc] = NULL;
284 <                                /* start rtrace & sum contributions */
284 >                                /* start rtrace & compute contributions */
285          init(nprocs);
286 <        tracecontribs(stdin);
286 >        trace_contribs(stdin);
287          quit(0);
288   }
289  
# Line 333 | Line 335 | quit(int status)
335          exit(status);                   /* flushes all output streams */
336   }
337  
338 < /* start rtrace and initialize buffers */
338 > /* start rtrace processes and initialize */
339   void
340   init(int np)
341   {
342          struct rtproc   *rtp;
343          int     i;
344          int     maxbytes;
345 +                                        /* make sure we have something to do */
346 +        if (!nmods)
347 +                error(USER, "No modifiers specified");
348                                          /* assign ray variables */
349          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
350          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
351                                          /* set up signal handling */
352 < #ifdef SIGPIPE /* not present on Windows */
352 >        signal(SIGINT, quit);
353 > #ifdef SIGHUP
354 >        signal(SIGHUP, quit);
355 > #endif
356 > #ifdef SIGTERM
357 >        signal(SIGTERM, quit);
358 > #endif
359 > #ifdef SIGPIPE
360          signal(SIGPIPE, quit);
361   #endif
362          rtp = &rt0;                     /* start rtrace process(es) */
# Line 360 | Line 372 | init(int np)
372                          error(SYSTEM, "cannot start rtrace process");
373                  if (maxbytes > treebufsiz)
374                          treebufsiz = maxbytes;
375 +                rtp->raynum = 0;
376                  rtp->bsiz = 0;
377                  rtp->buf = NULL;
378 <                rtp->raynum = 0;
378 >                rtp->nbr = 0;
379                  if (i == np)            /* last process? */
380                          break;
381                  if (i == 1)
# Line 437 | Line 450 | printheader(FILE *fout)
450          
451          if (fin == NULL)
452                  quit(1);
453 <        getheader(fin, (gethfunc *)fputs, fout);        /* copy octree header */
453 >        checkheader(fin, "ignore", fout);       /* copy octree header */
454          fclose(fin);
455          printargs(gargc-1, gargv, fout);        /* add our command */
456          fprintf(fout, "SOFTWARE= %s\n", VersionID);
# Line 468 | Line 481 | printheader(FILE *fout)
481   FILE *
482   getofile(const char *ospec, const char *mname, int bn)
483   {
471        static int      using_stdout = 0;
484          const char      *mnp = NULL;
485          const char      *bnp = NULL;
486          const char      *cp;
# Line 519 | Line 531 | getofile(const char *ospec, const char *mname, int bn)
531          if (lep->key == NULL)                   /* new entry */
532                  lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
533          if (lep->data == NULL) {                /* open output file */
534 <                lep->data = (char *)fopen(ofname, "w");
535 <                if (lep->data == NULL) {
534 >                FILE            *fp = fopen(ofname, "w");
535 >                int             i;
536 >                if (fp == NULL) {
537                          sprintf(errmsg, "cannot open '%s' for writing", ofname);
538                          error(SYSTEM, errmsg);
539                  }
540                  if (header)
541 <                        printheader((FILE *)lep->data);
541 >                        printheader(fp);
542 >                                                /* play catch-up */
543 >                for (i = 0; i < lastdone; i++) {
544 >                        static const DCOLOR     nocontrib = BLKCOLOR;
545 >                        putcontrib(nocontrib, fp);
546 >                        if (outfmt == 'a')
547 >                                putc('\n', fp);
548 >                }
549 >                if (xres > 0)
550 >                        fflush(fp);
551 >                lep->data = (char *)fp;
552          }
553          return (FILE *)lep->data;               /* return open file pointer */
554   badspec:
# Line 538 | Line 561 | badspec:
561   int
562   getinp(char *buf, FILE *fp)
563   {
564 +        char    *cp;
565 +        int     i;
566 +
567          switch (inpfmt) {
568          case 'a':
569 <                if (fgets(buf, 128, fp) == NULL)
570 <                        return 0;
569 >                cp = buf;               /* make sure we get 6 floats */
570 >                for (i = 0; i < 6; i++) {
571 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
572 >                                return 0;
573 >                        if ((cp = fskip(cp)) == NULL || *cp)
574 >                                return 0;
575 >                        *cp++ = ' ';
576 >                }
577 >                getc(fp);               /* get/put eol */
578 >                *cp-- = '\0'; *cp = '\n';
579                  return strlen(buf);
580          case 'f':
581                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 556 | Line 590 | getinp(char *buf, FILE *fp)
590          return 0;       /* pro forma return */
591   }
592  
593 < static const float      *rparams = NULL;        /* ray parameter pointer */
593 > static float    rparams[9];             /* traced ray parameters */
594  
595   /* return channel (ray) value */
596   double
# Line 564 | Line 598 | chanvalue(int n)
598   {
599          if (--n < 0 || n >= 6)
600                  error(USER, "illegal channel number ($N)");
601 <        if (rparams == NULL)
568 <                error(USER, "illegal use of $N in constant expression");
569 <        return rparams[n];
601 >        return rparams[n+3];
602   }
603  
604 < /* add ray contribution to the appropriate modifier bin */
604 > /* add current ray contribution to the appropriate modifier bin */
605   void
606 < add_contrib(const char *modn, float rayval[9])
606 > add_contrib(const char *modn)
607   {
608          LUENT   *le = lu_find(&modconttab, modn);
609          MODCONT *mp = (MODCONT *)le->data;
# Line 581 | Line 613 | add_contrib(const char *modn, float rayval[9])
613                  sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn);
614                  error(USER, errmsg);
615          }
616 <        rparams = rayval + 3;           /* for chanvalue */
585 <        eclock++;
616 >        eclock++;                       /* get bin number */
617          bn = (int)(evalue(mp->binv) + .5);
618          if (bn <= 0)
619                  bn = 0;
620          else if (bn > mp->nbins) {      /* new bin */
621                  mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
622 <                                                bn*sizeof(COLOR));
622 >                                                bn*sizeof(DCOLOR));
623                  if (mp == NULL)
624                          error(SYSTEM, "out of memory in add_contrib");
625 <                memset(mp->cbin+mp->nbins, 0, sizeof(COLOR)*(bn+1-mp->nbins));
625 >                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
626                  mp->nbins = bn+1;
627                  le->data = (char *)mp;
628          }
629 <        addcolor(mp->cbin[bn], rayval);
599 <        rparams = NULL;
629 >        addcolor(mp->cbin[bn], rparams);
630   }
631  
632   /* output newline to ASCII file and/or flush as requested */
# Line 616 | Line 646 | puteol(const LUENT *e, void *p)
646          return 0;
647   }
648  
649 + /* put out ray contribution to file */
650 + void
651 + putcontrib(const DCOLOR cnt, FILE *fout)
652 + {
653 +        float   fv[3];
654 +        COLR    cv;
655 +
656 +        switch (outfmt) {
657 +        case 'a':
658 +                fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
659 +                break;
660 +        case 'f':
661 +                fv[0] = cnt[0];
662 +                fv[1] = cnt[1];
663 +                fv[2] = cnt[2];
664 +                fwrite(fv, sizeof(float), 3, fout);
665 +                break;
666 +        case 'd':
667 +                fwrite(cnt, sizeof(double), 3, fout);
668 +                break;
669 +        case 'c':
670 +                setcolr(cv, cnt[0], cnt[1], cnt[2]);
671 +                fwrite(cv, sizeof(cv), 1, fout);
672 +                break;
673 +        default:
674 +                error(INTERNAL, "botched output format");
675 +        }
676 + }
677 +
678   /* output ray tallies and clear for next primary */
679   void
680   done_contrib(void)
681   {
682          int     i, j;
683          MODCONT *mp;
625        FILE    *fout;
626        double  dv[3];
627        COLR    cv;
684                                                  /* output modifiers in order */
685          for (i = 0; i < nmods; i++) {
686                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
687 <                for (j = 0; j < mp->nbins; j++) {
688 <                        fout = getofile(mp->outspec, mp->modname, j);
689 <                        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 <                }
687 >                for (j = 0; j < mp->nbins; j++)
688 >                        putcontrib(mp->cbin[j],
689 >                                        getofile(mp->outspec, mp->modname, j));
690                                                  /* clear for next ray tree */
691 <                memset(mp->cbin, 0, sizeof(COLOR)*mp->nbins);
691 >                memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
692          }
693          --waitflush;                            /* terminate records */
694          lu_doall(&ofiletab, puteol, NULL);
695 <        if (!waitflush)
695 >        if (using_stdout & (outfmt == 'a'))
696 >                putc('\n', stdout);
697 >        if (!waitflush) {
698                  waitflush = xres;
699 +                if (using_stdout)
700 +                        fflush(stdout);
701 +        }
702   }
703  
704 < /* process (or save) ray tree produced by rtrace process */
704 > /* queue completed ray tree produced by rtrace process */
705   void
706 < process_rays(struct rtproc *rtp)
706 > queue_raytree(struct rtproc *rtp)
707   {
708 <        struct rtproc   *rtu;
709 <                                        /* check if time to process it */
710 <        if (rtp->raynum == lastdone+1) {
711 <                int             n = rtp->bpos - rtp->buf;
708 >        struct rtproc   *rtu, *rtl = NULL;
709 >                                        /* insert following ray order */
710 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
711 >                if (rtp->raynum < rtu->raynum)
712 >                        break;
713 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
714 >        if (rtu == NULL)
715 >                error(SYSTEM, "out of memory in queue_raytree");
716 >        *rtu = *rtp;
717 >        if (rtl == NULL) {
718 >                rtu->next = rt_unproc;
719 >                rt_unproc = rtu;
720 >        } else {
721 >                rtu->next = rtl->next;
722 >                rtl->next = rtu;
723 >        }
724 >        rtp->raynum = 0;                /* clear path for next ray tree */
725 >        rtp->bsiz = 0;
726 >        rtp->buf = NULL;
727 >        rtp->nbr = 0;
728 > }
729 >
730 > /* process completed ray trees from our queue */
731 > void
732 > process_queue(void)
733 > {
734 >        char    modname[128];
735 >                                        /* ray-ordered queue */
736 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
737 >                struct rtproc   *rtp = rt_unproc;
738 >                int             n = rtp->nbr;
739                  const char      *cp = rtp->buf;
740                  while (n > 0) {         /* process rays */
741 <                        char    matname[128];
678 <                        char    *mnp = matname;
741 >                        register char   *mnp = modname;
742                                          /* skip leading tabs */
743                          while (n > 0 && *cp == '\t') {
744                                  cp++; n--;
745                          }
746 <                                        /* get material name */
746 >                        if (!n || !(isalpha(*cp) | (*cp == '_')))
747 >                                error(USER, "bad modifier name from rtrace");
748 >                                        /* get modifier name */
749                          while (n > 0 && *cp != '\t') {
750                                  *mnp++ = *cp++; n--;
751                          }
687                        if (!n)
688                                error(USER, "botched modifer name from rtrace");
752                          *mnp = '\0';
753 <                        cp++; n--;      /* skip terminating tab */
753 >                        cp++; n--;      /* eat following tab */
754                          if (n < (int)(sizeof(float)*9))
755                                  error(USER, "incomplete ray value from rtrace");
756                                          /* add ray contribution */
757 <                        add_contrib(matname, (float *)cp);
757 >                        memcpy(rparams, cp, sizeof(float)*9);
758                          cp += sizeof(float)*9; n -= sizeof(float)*9;
759 +                        add_contrib(modname);
760                  }
761                  done_contrib();         /* sum up contributions & output */
762                  lastdone = rtp->raynum;
763 <                free(rtp->buf);
764 <                                        /* catch up with unprocessed list */
765 <                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 <                }
763 >                free(rtp->buf);         /* free up buffer space */
764 >                rt_unproc = rtp->next;
765 >                free(rtp);              /* done with this ray tree */
766          }
723        rtp->raynum = 0;                /* clear path for next ray tree */
724        rtp->bsiz = 0;
725        rtp->buf = NULL;
767   }
768  
769   /* wait for rtrace process to finish with ray tree */
# Line 763 | Line 804 | wait_rproc(void)
804                          if (rt->buf == NULL) {
805                                  rt->bsiz = treebufsiz;
806                                  rt->buf = (char *)malloc(treebufsiz);
807 <                        } else if (rt->bpos + BUFSIZ > rt->buf + rt->bsiz) {
807 >                        } else if (rt->nbr + BUFSIZ > rt->bsiz) {
808                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
809                                          rt->bsiz = treebufsiz;
810                                  else
# Line 772 | Line 813 | wait_rproc(void)
813                          }
814                          if (rt->buf == NULL)
815                                  error(SYSTEM, "out of memory in wait_rproc");
816 <                        nr = read(rt->pd.r, rt->bpos,
817 <                                        rt->buf + rt->bsiz - rt->bpos);
777 <                        if (!nr)
816 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
817 >                        if (nr <= 0)
818                                  error(USER, "rtrace process died");
819 <                        rt->bpos += nr;         /* advance buffer & check */
820 <                        if (rt->bpos[-1] == '\t' && rt->bpos[-2] == '-') {
821 <                                rt->bpos -= 2;  /* elide terminator */
822 <                                process_rays(rt);
819 >                        rt->nbr += nr;          /* advance & check */
820 >                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
821 >                                                        "~\t~\t", 4)) {
822 >                                rt->nbr -= 4;   /* elide terminator */
823 >                                queue_raytree(rt);
824                                  rtfree = rt;    /* ready for next ray */
825                          }
826                  }
# Line 801 | Line 842 | get_rproc(void)
842  
843   /* trace ray contributions (main loop) */
844   void
845 < tracecontribs(FILE *fin)
845 > trace_contribs(FILE *fin)
846   {
847          char            inpbuf[128];
848          int             iblen;
# Line 810 | Line 851 | tracecontribs(FILE *fin)
851          while ((iblen = getinp(inpbuf, fin)) > 0) {
852                  if (lastray+1 < lastray) {      /* counter rollover? */
853                          while (wait_rproc() != NULL)
854 <                                ;
854 >                                process_queue();
855                          lastdone = lastray = 0;
856                  }
857                  rtp = get_rproc();              /* get avail. rtrace process */
858 <                rtp->raynum = ++lastray;        /* assign this ray to it */
858 >                rtp->raynum = ++lastray;        /* assign ray to it */
859                  writebuf(rtp->pd.w, inpbuf, iblen);
860                  if (!--raysleft)
861 <                        break;                  /* explicit EOF */
861 >                        break;
862 >                process_queue();                /* catch up with results */
863          }
864          while (wait_rproc() != NULL)            /* process outstanding rays */
865 <                ;
865 >                process_queue();
866 >        if (raysleft > 0)
867 >                error(USER, "unexpected EOF on input");
868   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines