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.9 by greg, Wed Jun 1 16:11:01 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 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 >                                        char    *fpath;
206 >                                        if (i >= argc-1) break;
207 >                                        fpath = getpath(argv[++i],
208 >                                                        getrlibpath(), R_OK);
209 >                                        if (fpath == NULL) {
210 >                                                sprintf(errmsg,
211 >                                                        "cannot find file '%s'",
212 >                                                                argv[i]);
213 >                                                error(USER, errmsg);
214 >                                        }
215 >                                        fcompile(fpath);
216 >                                        continue;
217 >                                }
218 >                                setformat(argv[i]+2);
219                                  continue;
220 <                        case '-': case '0': case 'F': case 'f':
221 <                                header = 0;
220 >                        case 'e':               /* expression */
221 >                                if (argv[i][2] || i >= argc-1) break;
222 >                                scompile(argv[++i], NULL, 0);
223                                  continue;
224 <                        }
225 <                        break;
226 <                case 'f':                       /* file or i/o format */
194 <                        if (!argv[i][2]) {
195 <                                if (i >= argc-1) break;
196 <                                fcompile(argv[++i]);
224 >                        case 'o':               /* output file spec. */
225 >                                if (argv[i][2] || i >= argc-1) break;
226 >                                curout = argv[++i];
227                                  continue;
228 +                        case 'x':               /* horiz. output resolution */
229 +                                if (argv[i][2] || i >= argc-1) break;
230 +                                xres = atoi(argv[++i]);
231 +                                continue;
232 +                        case 'y':               /* vert. output resolution */
233 +                                if (argv[i][2] || i >= argc-1) break;
234 +                                yres = atoi(argv[++i]);
235 +                                continue;
236 +                        case 'b':               /* bin expression */
237 +                                if (argv[i][2] || i >= argc-1) break;
238 +                                binval = argv[++i];
239 +                                continue;
240 +                        case 'm':               /* modifier name */
241 +                                if (argv[i][2] || i >= argc-1) break;
242 +                                rtargv[rtargc++] = "-ti";
243 +                                rtargv[rtargc++] = argv[++i];
244 +                                addmodifier(argv[i], curout, binval);
245 +                                continue;
246                          }
247 <                        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 */
247 >                rtargv[rtargc++] = argv[i];     /* assume rtrace option */
248          }
249 <        for ( ; i < argc; i++)  /* transfer rtrace-specific options */
250 <                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 <        }
249 >                                /* set global argument list */
250 >        gargc = argc; gargv = argv;
251                                  /* add "mandatory" rtrace settings */
252 <        for (i = 0; myrtopts[i] != NULL; i++)
253 <                rtargv[rtargc++] = myrtopts[i];
254 <        if (!nprocs) {          /* just asking for defaults? */
252 >        for (j = 0; myrtopts[j] != NULL; j++)
253 >                rtargv[rtargc++] = myrtopts[j];
254 >                                /* just asking for defaults? */
255 >        if (!strcmp(argv[i], "-defaults")) {
256                  char    sxres[16], syres[16];
257                  char    *rtpath;
258 <                printf("-n  1\t\t\t\t# number of processes\n", nprocs);
258 >                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
259                  fflush(stdout);                 /* report OUR options */
260                  rtargv[rtargc++] = header ? "-h+" : "-h-";
261                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 256 | Line 266 | main(int argc, char *argv[])
266                  rtargv[rtargc++] = "-y";
267                  sprintf(syres, "%d", yres);
268                  rtargv[rtargc++] = syres;
269 <                rtargv[rtargc++] = "-oW";
269 >                rtargv[rtargc++] = "-oTW";
270                  rtargv[rtargc++] = "-defaults";
271                  rtargv[rtargc] = NULL;
272                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 269 | Line 279 | main(int argc, char *argv[])
279                  perror(rtpath); /* execv() should not return */
280                  exit(1);
281          }
282 <        if (!nmods)
283 <                error(USER, "No modifiers specified");
284 <        if (argc < 2 || argv[argc-1][0] == '-')
285 <                error(USER, "missing octree argument");
282 >        if (nprocs > 1) {       /* add persist file if parallel */
283 >                rtargv[rtargc++] = "-PP";
284 >                rtargv[rtargc++] = mktemp(persistfn);
285 >        }
286                                  /* add format string */
287          sprintf(fmt, "-f%cf", inpfmt);
288          rtargv[rtargc++] = fmt;
289                                  /* octree argument is last */
290 <        rtargv[rtargc++] = octree = argv[argc-1];
290 >        if (i <= 0 || i != argc-1 || argv[i][0] == '-')
291 >                error(USER, "missing octree argument");
292 >        rtargv[rtargc++] = octree = argv[i];
293          rtargv[rtargc] = NULL;
294 <                                /* start rtrace & sum contributions */
294 >                                /* start rtrace & compute contributions */
295          init(nprocs);
296 <        tracecontribs(stdin);
296 >        trace_contribs(stdin);
297          quit(0);
298   }
299  
# Line 333 | Line 345 | quit(int status)
345          exit(status);                   /* flushes all output streams */
346   }
347  
348 < /* start rtrace and initialize buffers */
348 > /* start rtrace processes and initialize */
349   void
350   init(int np)
351   {
352          struct rtproc   *rtp;
353          int     i;
354          int     maxbytes;
355 +                                        /* make sure we have something to do */
356 +        if (!nmods)
357 +                error(USER, "No modifiers specified");
358                                          /* assign ray variables */
359          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
360          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
361                                          /* set up signal handling */
362 < #ifdef SIGPIPE /* not present on Windows */
362 >        signal(SIGINT, quit);
363 > #ifdef SIGHUP
364 >        signal(SIGHUP, quit);
365 > #endif
366 > #ifdef SIGTERM
367 >        signal(SIGTERM, quit);
368 > #endif
369 > #ifdef SIGPIPE
370          signal(SIGPIPE, quit);
371   #endif
372          rtp = &rt0;                     /* start rtrace process(es) */
# Line 360 | Line 382 | init(int np)
382                          error(SYSTEM, "cannot start rtrace process");
383                  if (maxbytes > treebufsiz)
384                          treebufsiz = maxbytes;
385 +                rtp->raynum = 0;
386                  rtp->bsiz = 0;
387                  rtp->buf = NULL;
388 <                rtp->raynum = 0;
388 >                rtp->nbr = 0;
389                  if (i == np)            /* last process? */
390                          break;
391                  if (i == 1)
# Line 437 | Line 460 | printheader(FILE *fout)
460          
461          if (fin == NULL)
462                  quit(1);
463 <        getheader(fin, (gethfunc *)fputs, fout);        /* copy octree header */
463 >        checkheader(fin, "ignore", fout);       /* copy octree header */
464          fclose(fin);
465          printargs(gargc-1, gargv, fout);        /* add our command */
466          fprintf(fout, "SOFTWARE= %s\n", VersionID);
# Line 468 | Line 491 | printheader(FILE *fout)
491   FILE *
492   getofile(const char *ospec, const char *mname, int bn)
493   {
471        static int      using_stdout = 0;
494          const char      *mnp = NULL;
495          const char      *bnp = NULL;
496          const char      *cp;
# Line 519 | Line 541 | getofile(const char *ospec, const char *mname, int bn)
541          if (lep->key == NULL)                   /* new entry */
542                  lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
543          if (lep->data == NULL) {                /* open output file */
544 <                lep->data = (char *)fopen(ofname, "w");
545 <                if (lep->data == NULL) {
544 >                FILE            *fp = fopen(ofname, "w");
545 >                int             i;
546 >                if (fp == NULL) {
547                          sprintf(errmsg, "cannot open '%s' for writing", ofname);
548                          error(SYSTEM, errmsg);
549                  }
550                  if (header)
551 <                        printheader((FILE *)lep->data);
551 >                        printheader(fp);
552 >                                                /* play catch-up */
553 >                for (i = 0; i < lastdone; i++) {
554 >                        static const DCOLOR     nocontrib = BLKCOLOR;
555 >                        putcontrib(nocontrib, fp);
556 >                        if (outfmt == 'a')
557 >                                putc('\n', fp);
558 >                }
559 >                if (xres > 0)
560 >                        fflush(fp);
561 >                lep->data = (char *)fp;
562          }
563          return (FILE *)lep->data;               /* return open file pointer */
564   badspec:
# Line 538 | Line 571 | badspec:
571   int
572   getinp(char *buf, FILE *fp)
573   {
574 +        char    *cp;
575 +        int     i;
576 +
577          switch (inpfmt) {
578          case 'a':
579 <                if (fgets(buf, 128, fp) == NULL)
580 <                        return 0;
579 >                cp = buf;               /* make sure we get 6 floats */
580 >                for (i = 0; i < 6; i++) {
581 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
582 >                                return 0;
583 >                        if ((cp = fskip(cp)) == NULL || *cp)
584 >                                return 0;
585 >                        *cp++ = ' ';
586 >                }
587 >                getc(fp);               /* get/put eol */
588 >                *cp-- = '\0'; *cp = '\n';
589                  return strlen(buf);
590          case 'f':
591                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 556 | Line 600 | getinp(char *buf, FILE *fp)
600          return 0;       /* pro forma return */
601   }
602  
603 < static const float      *rparams = NULL;        /* ray parameter pointer */
603 > static float    rparams[9];             /* traced ray parameters */
604  
605   /* return channel (ray) value */
606   double
# Line 564 | Line 608 | chanvalue(int n)
608   {
609          if (--n < 0 || n >= 6)
610                  error(USER, "illegal channel number ($N)");
611 <        if (rparams == NULL)
568 <                error(USER, "illegal use of $N in constant expression");
569 <        return rparams[n];
611 >        return rparams[n+3];
612   }
613  
614 < /* add ray contribution to the appropriate modifier bin */
614 > /* add current ray contribution to the appropriate modifier bin */
615   void
616 < add_contrib(const char *modn, float rayval[9])
616 > add_contrib(const char *modn)
617   {
618          LUENT   *le = lu_find(&modconttab, modn);
619          MODCONT *mp = (MODCONT *)le->data;
# Line 581 | Line 623 | add_contrib(const char *modn, float rayval[9])
623                  sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn);
624                  error(USER, errmsg);
625          }
626 <        rparams = rayval + 3;           /* for chanvalue */
585 <        eclock++;
626 >        eclock++;                       /* get bin number */
627          bn = (int)(evalue(mp->binv) + .5);
628          if (bn <= 0)
629                  bn = 0;
630          else if (bn > mp->nbins) {      /* new bin */
631                  mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
632 <                                                bn*sizeof(COLOR));
632 >                                                bn*sizeof(DCOLOR));
633                  if (mp == NULL)
634                          error(SYSTEM, "out of memory in add_contrib");
635 <                memset(mp->cbin+mp->nbins, 0, sizeof(COLOR)*(bn+1-mp->nbins));
635 >                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
636                  mp->nbins = bn+1;
637                  le->data = (char *)mp;
638          }
639 <        addcolor(mp->cbin[bn], rayval);
599 <        rparams = NULL;
639 >        addcolor(mp->cbin[bn], rparams);
640   }
641  
642   /* output newline to ASCII file and/or flush as requested */
# Line 616 | Line 656 | puteol(const LUENT *e, void *p)
656          return 0;
657   }
658  
659 + /* put out ray contribution to file */
660 + void
661 + putcontrib(const DCOLOR cnt, FILE *fout)
662 + {
663 +        float   fv[3];
664 +        COLR    cv;
665 +
666 +        switch (outfmt) {
667 +        case 'a':
668 +                fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
669 +                break;
670 +        case 'f':
671 +                fv[0] = cnt[0];
672 +                fv[1] = cnt[1];
673 +                fv[2] = cnt[2];
674 +                fwrite(fv, sizeof(float), 3, fout);
675 +                break;
676 +        case 'd':
677 +                fwrite(cnt, sizeof(double), 3, fout);
678 +                break;
679 +        case 'c':
680 +                setcolr(cv, cnt[0], cnt[1], cnt[2]);
681 +                fwrite(cv, sizeof(cv), 1, fout);
682 +                break;
683 +        default:
684 +                error(INTERNAL, "botched output format");
685 +        }
686 + }
687 +
688   /* output ray tallies and clear for next primary */
689   void
690   done_contrib(void)
691   {
692          int     i, j;
693          MODCONT *mp;
625        FILE    *fout;
626        double  dv[3];
627        COLR    cv;
694                                                  /* output modifiers in order */
695          for (i = 0; i < nmods; i++) {
696                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
697 <                for (j = 0; j < mp->nbins; j++) {
698 <                        fout = getofile(mp->outspec, mp->modname, j);
699 <                        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 <                }
697 >                for (j = 0; j < mp->nbins; j++)
698 >                        putcontrib(mp->cbin[j],
699 >                                        getofile(mp->outspec, mp->modname, j));
700                                                  /* clear for next ray tree */
701 <                memset(mp->cbin, 0, sizeof(COLOR)*mp->nbins);
701 >                memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
702          }
703          --waitflush;                            /* terminate records */
704          lu_doall(&ofiletab, puteol, NULL);
705 <        if (!waitflush)
705 >        if (using_stdout & (outfmt == 'a'))
706 >                putc('\n', stdout);
707 >        if (!waitflush) {
708                  waitflush = xres;
709 +                if (using_stdout)
710 +                        fflush(stdout);
711 +        }
712   }
713  
714 < /* process (or save) ray tree produced by rtrace process */
714 > /* queue completed ray tree produced by rtrace process */
715   void
716 < process_rays(struct rtproc *rtp)
716 > queue_raytree(struct rtproc *rtp)
717   {
718 <        struct rtproc   *rtu;
719 <                                        /* check if time to process it */
720 <        if (rtp->raynum == lastdone+1) {
721 <                int             n = rtp->bpos - rtp->buf;
718 >        struct rtproc   *rtu, *rtl = NULL;
719 >                                        /* insert following ray order */
720 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
721 >                if (rtp->raynum < rtu->raynum)
722 >                        break;
723 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
724 >        if (rtu == NULL)
725 >                error(SYSTEM, "out of memory in queue_raytree");
726 >        *rtu = *rtp;
727 >        if (rtl == NULL) {
728 >                rtu->next = rt_unproc;
729 >                rt_unproc = rtu;
730 >        } else {
731 >                rtu->next = rtl->next;
732 >                rtl->next = rtu;
733 >        }
734 >        rtp->raynum = 0;                /* clear path for next ray tree */
735 >        rtp->bsiz = 0;
736 >        rtp->buf = NULL;
737 >        rtp->nbr = 0;
738 > }
739 >
740 > /* process completed ray trees from our queue */
741 > void
742 > process_queue(void)
743 > {
744 >        char    modname[128];
745 >                                        /* ray-ordered queue */
746 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
747 >                struct rtproc   *rtp = rt_unproc;
748 >                int             n = rtp->nbr;
749                  const char      *cp = rtp->buf;
750                  while (n > 0) {         /* process rays */
751 <                        char    matname[128];
678 <                        char    *mnp = matname;
751 >                        register char   *mnp = modname;
752                                          /* skip leading tabs */
753                          while (n > 0 && *cp == '\t') {
754                                  cp++; n--;
755                          }
756 <                                        /* get material name */
756 >                        if (!n || !(isalpha(*cp) | (*cp == '_')))
757 >                                error(USER, "bad modifier name from rtrace");
758 >                                        /* get modifier name */
759                          while (n > 0 && *cp != '\t') {
760                                  *mnp++ = *cp++; n--;
761                          }
687                        if (!n)
688                                error(USER, "botched modifer name from rtrace");
762                          *mnp = '\0';
763 <                        cp++; n--;      /* skip terminating tab */
763 >                        cp++; n--;      /* eat following tab */
764                          if (n < (int)(sizeof(float)*9))
765                                  error(USER, "incomplete ray value from rtrace");
766                                          /* add ray contribution */
767 <                        add_contrib(matname, (float *)cp);
767 >                        memcpy(rparams, cp, sizeof(float)*9);
768                          cp += sizeof(float)*9; n -= sizeof(float)*9;
769 +                        add_contrib(modname);
770                  }
771                  done_contrib();         /* sum up contributions & output */
772                  lastdone = rtp->raynum;
773 <                free(rtp->buf);
774 <                                        /* catch up with unprocessed list */
775 <                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 <                }
773 >                free(rtp->buf);         /* free up buffer space */
774 >                rt_unproc = rtp->next;
775 >                free(rtp);              /* done with this ray tree */
776          }
723        rtp->raynum = 0;                /* clear path for next ray tree */
724        rtp->bsiz = 0;
725        rtp->buf = NULL;
777   }
778  
779   /* wait for rtrace process to finish with ray tree */
# Line 763 | Line 814 | wait_rproc(void)
814                          if (rt->buf == NULL) {
815                                  rt->bsiz = treebufsiz;
816                                  rt->buf = (char *)malloc(treebufsiz);
817 <                        } else if (rt->bpos + BUFSIZ > rt->buf + rt->bsiz) {
817 >                        } else if (rt->nbr + BUFSIZ > rt->bsiz) {
818                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
819                                          rt->bsiz = treebufsiz;
820                                  else
# Line 772 | Line 823 | wait_rproc(void)
823                          }
824                          if (rt->buf == NULL)
825                                  error(SYSTEM, "out of memory in wait_rproc");
826 <                        nr = read(rt->pd.r, rt->bpos,
827 <                                        rt->buf + rt->bsiz - rt->bpos);
777 <                        if (!nr)
826 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
827 >                        if (nr <= 0)
828                                  error(USER, "rtrace process died");
829 <                        rt->bpos += nr;         /* advance buffer & check */
830 <                        if (rt->bpos[-1] == '\t' && rt->bpos[-2] == '-') {
831 <                                rt->bpos -= 2;  /* elide terminator */
832 <                                process_rays(rt);
829 >                        rt->nbr += nr;          /* advance & check */
830 >                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
831 >                                                        "~\t~\t", 4)) {
832 >                                rt->nbr -= 4;   /* elide terminator */
833 >                                queue_raytree(rt);
834                                  rtfree = rt;    /* ready for next ray */
835                          }
836                  }
# Line 801 | Line 852 | get_rproc(void)
852  
853   /* trace ray contributions (main loop) */
854   void
855 < tracecontribs(FILE *fin)
855 > trace_contribs(FILE *fin)
856   {
857          char            inpbuf[128];
858          int             iblen;
# Line 810 | Line 861 | tracecontribs(FILE *fin)
861          while ((iblen = getinp(inpbuf, fin)) > 0) {
862                  if (lastray+1 < lastray) {      /* counter rollover? */
863                          while (wait_rproc() != NULL)
864 <                                ;
864 >                                process_queue();
865                          lastdone = lastray = 0;
866                  }
867                  rtp = get_rproc();              /* get avail. rtrace process */
868 <                rtp->raynum = ++lastray;        /* assign this ray to it */
868 >                rtp->raynum = ++lastray;        /* assign ray to it */
869                  writebuf(rtp->pd.w, inpbuf, iblen);
870                  if (!--raysleft)
871 <                        break;                  /* explicit EOF */
871 >                        break;
872 >                process_queue();                /* catch up with results */
873          }
874          while (wait_rproc() != NULL)            /* process outstanding rays */
875 <                ;
875 >                process_queue();
876 >        if (raysleft > 0)
877 >                error(USER, "unexpected EOF on input");
878   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines