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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines