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.3 by greg, Thu May 26 15:14:42 2005 UTC

# 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 */
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 */
68  
69                                          /* rtrace command and defaults */
70 < char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
71 <                                "-ab", "1", "-ad", "512", };
72 < int  rtargc = 9;
70 > char            *rtargv[256] = { "rtrace", "-dt", "0", "-dj", ".5", "-dr", "3",
71 >                                "-ab", "1", "-ad", "128", };
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-",
75 >                                "-x", "1", "-y", "0",
76 >                                "-as", "0", "-aa", "0", NULL };
77  
78   struct rtproc   rt0;                    /* head of rtrace process list */
79  
# Line 106 | Line 101 | long           waitflush;              /* how long until next flush */
101   unsigned long   lastray = 0;            /* last ray number sent */
102   unsigned long   lastdone = 0;           /* last ray processed */
103  
104 + int             using_stdout = 0;       /* are we using stdout? */
105 +
106   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
107   int             nmods = 0;              /* number of modifiers */
108  
# Line 116 | Line 113 | void init(int np);
113   void tracecontribs(FILE *fp);
114   struct rtproc *wait_rproc(void);
115   struct rtproc *get_rproc(void);
116 < void process_rays(struct rtproc *rtp);
116 > void queue_raytree(struct rtproc *rtp);
117 > void process_queue(void);
118  
119 < void add_contrib(const char *modn, float rayval[7]);
119 > void putcontrib(const DCOLOR cnt, FILE *fout);
120 > void add_contrib(const char *modn);
121   void done_contrib(void);
122  
123   /* set input/output format */
# Line 161 | Line 160 | main(int argc, char *argv[])
160          int     nprocs = 1;
161          char    *curout = NULL;
162          char    *binval = NULL;
163 <        int     i;
164 <                                /* set global argument list */
166 <        gargc = argc;
163 >        int     i, j;
164 >                                /* global program name */
165          gargv = argv;
166 <                                /* set up calcomp functions */
166 >                                /* set up calcomp mode */
167          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
168          esupport &= ~(E_OUTCHAN);
169                                  /* get our options */
170 <        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
171 <                switch (argv[i][1]) {
172 <                case 'n':                       /* number of processes */
173 <                        if (argv[i][2] || i >= argc-1) break;
174 <                        nprocs = atoi(argv[++i]);
175 <                        if (nprocs <= 0)
176 <                                error(USER, "illegal number of processes");
177 <                        continue;
178 <                case 'h':                       /* output header? */
179 <                        switch (argv[i][2]) {
180 <                        case '\0':
181 <                                header = !header;
170 >        for (i = 1; i < argc-1; i++) {
171 >                                                /* expand arguments */
172 >                while ((j = expandarg(&argc, &argv, i)) > 0)
173 >                        ;
174 >                if (j < 0) {
175 >                        fprintf(stderr, "%s: cannot expand '%s'",
176 >                                        argv[0], argv[i]);
177 >                        exit(1);
178 >                }
179 >                if (argv[i][0] == '-')
180 >                        switch (argv[i][1]) {
181 >                        case 'n':               /* number of processes */
182 >                                if (argv[i][2] || i >= argc-1) break;
183 >                                nprocs = atoi(argv[++i]);
184 >                                if (nprocs <= 0)
185 >                                        error(USER, "illegal number of processes");
186                                  continue;
187 <                        case '+': case '1': case 'T': case 't':
188 <                                header = 1;
187 >                        case 'h':               /* output header? */
188 >                                switch (argv[i][2]) {
189 >                                case '\0':
190 >                                        header = !header;
191 >                                        continue;
192 >                                case '+': case '1':
193 >                                case 'T': case 't':
194 >                                case 'Y': case 'y':
195 >                                        header = 1;
196 >                                        continue;
197 >                                case '-': case '0':
198 >                                case 'F': case 'f':
199 >                                case 'N': case 'n':
200 >                                        header = 0;
201 >                                        continue;
202 >                                }
203 >                                break;
204 >                        case 'f':               /* file or i/o format */
205 >                                if (!argv[i][2]) {
206 >                                        if (i >= argc-1) break;
207 >                                        fcompile(argv[++i]);
208 >                                        continue;
209 >                                }
210 >                                setformat(argv[i]+2);
211                                  continue;
212 <                        case '-': case '0': case 'F': case 'f':
213 <                                header = 0;
212 >                        case 'e':               /* expression */
213 >                                if (argv[i][2] || i >= argc-1) break;
214 >                                scompile(argv[++i], NULL, 0);
215                                  continue;
216 <                        }
217 <                        break;
218 <                case 'f':                       /* file or i/o format */
194 <                        if (!argv[i][2]) {
195 <                                if (i >= argc-1) break;
196 <                                fcompile(argv[++i]);
216 >                        case 'o':               /* output file spec. */
217 >                                if (argv[i][2] || i >= argc-1) break;
218 >                                curout = argv[++i];
219                                  continue;
220 +                        case 'x':               /* horiz. output resolution */
221 +                                if (argv[i][2] || i >= argc-1) break;
222 +                                xres = atoi(argv[++i]);
223 +                                continue;
224 +                        case 'y':               /* vert. output resolution */
225 +                                if (argv[i][2] || i >= argc-1) break;
226 +                                yres = atoi(argv[++i]);
227 +                                continue;
228 +                        case 'b':               /* bin expression */
229 +                                if (argv[i][2] || i >= argc-1) break;
230 +                                binval = argv[++i];
231 +                                continue;
232 +                        case 'm':               /* modifier name */
233 +                                if (argv[i][2] || i >= argc-1) break;
234 +                                rtargv[rtargc++] = "-ti";
235 +                                rtargv[rtargc++] = argv[++i];
236 +                                addmodifier(argv[i], curout, binval);
237 +                                continue;
238                          }
239 <                        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 */
239 >                rtargv[rtargc++] = argv[i];     /* assume rtrace option */
240          }
241 <        for ( ; i < argc; i++)  /* transfer rtrace-specific options */
242 <                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 <        }
241 >                                /* set global argument list */
242 >        gargc = argc; gargv = argv;
243                                  /* add "mandatory" rtrace settings */
244 <        for (i = 0; myrtopts[i] != NULL; i++)
245 <                rtargv[rtargc++] = myrtopts[i];
246 <        if (!nprocs) {          /* just asking for defaults? */
244 >        for (j = 0; myrtopts[j] != NULL; j++)
245 >                rtargv[rtargc++] = myrtopts[j];
246 >                                /* just asking for defaults? */
247 >        if (!strcmp(argv[i], "-defaults")) {
248                  char    sxres[16], syres[16];
249                  char    *rtpath;
250 <                printf("-n  1\t\t\t\t# number of processes\n", nprocs);
250 >                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
251                  fflush(stdout);                 /* report OUR options */
252                  rtargv[rtargc++] = header ? "-h+" : "-h-";
253                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 256 | Line 258 | main(int argc, char *argv[])
258                  rtargv[rtargc++] = "-y";
259                  sprintf(syres, "%d", yres);
260                  rtargv[rtargc++] = syres;
261 <                rtargv[rtargc++] = "-oW";
261 >                rtargv[rtargc++] = "-oTW";
262                  rtargv[rtargc++] = "-defaults";
263                  rtargv[rtargc] = NULL;
264                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 268 | Line 270 | main(int argc, char *argv[])
270                  execv(rtpath, rtargv);
271                  perror(rtpath); /* execv() should not return */
272                  exit(1);
273 <        }
274 <        if (!nmods)
275 <                error(USER, "No modifiers specified");
276 <        if (argc < 2 || argv[argc-1][0] == '-')
275 <                error(USER, "missing octree argument");
273 >        } else 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);
288          quit(0);
# Line 340 | Line 343 | init(int np)
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 556 | Line 580 | getinp(char *buf, FILE *fp)
580          return 0;       /* pro forma return */
581   }
582  
583 < static const float      *rparams = NULL;        /* ray parameter pointer */
583 > static float    rparams[9];             /* traced ray parameters */
584  
585   /* return channel (ray) value */
586   double
# Line 564 | Line 588 | chanvalue(int n)
588   {
589          if (--n < 0 || n >= 6)
590                  error(USER, "illegal channel number ($N)");
591 <        if (rparams == NULL)
568 <                error(USER, "illegal use of $N in constant expression");
569 <        return rparams[n];
591 >        return rparams[n+3];
592   }
593  
594 < /* add ray contribution to the appropriate modifier bin */
594 > /* add current ray contribution to the appropriate modifier bin */
595   void
596 < add_contrib(const char *modn, float rayval[9])
596 > add_contrib(const char *modn)
597   {
598          LUENT   *le = lu_find(&modconttab, modn);
599          MODCONT *mp = (MODCONT *)le->data;
# Line 581 | Line 603 | add_contrib(const char *modn, float rayval[9])
603                  sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn);
604                  error(USER, errmsg);
605          }
606 <        rparams = rayval + 3;           /* for chanvalue */
585 <        eclock++;
606 >        eclock++;                       /* get bin number */
607          bn = (int)(evalue(mp->binv) + .5);
608          if (bn <= 0)
609                  bn = 0;
610          else if (bn > mp->nbins) {      /* new bin */
611                  mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
612 <                                                bn*sizeof(COLOR));
612 >                                                bn*sizeof(DCOLOR));
613                  if (mp == NULL)
614                          error(SYSTEM, "out of memory in add_contrib");
615 <                memset(mp->cbin+mp->nbins, 0, sizeof(COLOR)*(bn+1-mp->nbins));
615 >                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
616                  mp->nbins = bn+1;
617                  le->data = (char *)mp;
618          }
619 <        addcolor(mp->cbin[bn], rayval);
599 <        rparams = NULL;
619 >        addcolor(mp->cbin[bn], rparams);
620   }
621  
622   /* output newline to ASCII file and/or flush as requested */
# Line 616 | Line 636 | puteol(const LUENT *e, void *p)
636          return 0;
637   }
638  
639 + /* put out ray contribution to file */
640 + void
641 + putcontrib(const DCOLOR cnt, FILE *fout)
642 + {
643 +        float   fv[3];
644 +        COLR    cv;
645 +
646 +        switch (outfmt) {
647 +        case 'a':
648 +                fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
649 +                break;
650 +        case 'f':
651 +                fv[0] = cnt[0];
652 +                fv[1] = cnt[1];
653 +                fv[2] = cnt[2];
654 +                fwrite(fv, sizeof(float), 3, fout);
655 +                break;
656 +        case 'd':
657 +                fwrite(cnt, sizeof(double), 3, fout);
658 +                break;
659 +        case 'c':
660 +                setcolr(cv, cnt[0], cnt[1], cnt[2]);
661 +                fwrite(cv, sizeof(cv), 1, fout);
662 +                break;
663 +        default:
664 +                error(INTERNAL, "botched output format");
665 +        }
666 + }
667 +
668   /* output ray tallies and clear for next primary */
669   void
670   done_contrib(void)
671   {
672          int     i, j;
673          MODCONT *mp;
625        FILE    *fout;
626        double  dv[3];
627        COLR    cv;
674                                                  /* output modifiers in order */
675          for (i = 0; i < nmods; i++) {
676                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
677 <                for (j = 0; j < mp->nbins; j++) {
678 <                        fout = getofile(mp->outspec, mp->modname, j);
679 <                        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 <                }
677 >                for (j = 0; j < mp->nbins; j++)
678 >                        putcontrib(mp->cbin[j],
679 >                                        getofile(mp->outspec, mp->modname, j));
680                                                  /* clear for next ray tree */
681 <                memset(mp->cbin, 0, sizeof(COLOR)*mp->nbins);
681 >                memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
682          }
683          --waitflush;                            /* terminate records */
684          lu_doall(&ofiletab, puteol, NULL);
685 <        if (!waitflush)
685 >        if (using_stdout & (outfmt == 'a'))
686 >                putc('\n', stdout);
687 >        if (!waitflush) {
688                  waitflush = xres;
689 +                if (using_stdout)
690 +                        fflush(stdout);
691 +        }
692   }
693  
694 < /* process (or save) ray tree produced by rtrace process */
694 > /* queue completed ray tree produced by rtrace process */
695   void
696 < process_rays(struct rtproc *rtp)
696 > queue_raytree(struct rtproc *rtp)
697   {
698 <        struct rtproc   *rtu;
699 <                                        /* check if time to process it */
700 <        if (rtp->raynum == lastdone+1) {
701 <                int             n = rtp->bpos - rtp->buf;
698 >        struct rtproc   *rtu, *rtl = NULL;
699 >                                        /* insert following ray order */
700 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
701 >                if (rtp->raynum < rtu->raynum)
702 >                        break;
703 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
704 >        if (rtu == NULL)
705 >                error(SYSTEM, "out of memory in queue_raytree");
706 >        *rtu = *rtp;
707 >        if (rtl == NULL) {
708 >                rtu->next = rt_unproc;
709 >                rt_unproc = rtu;
710 >        } else {
711 >                rtu->next = rtl->next;
712 >                rtl->next = rtu;
713 >        }
714 >        rtp->raynum = 0;                /* clear path for next ray tree */
715 >        rtp->bsiz = 0;
716 >        rtp->buf = NULL;
717 >        rtp->nbr = 0;
718 > }
719 >
720 > /* process completed ray trees from our queue */
721 > void
722 > process_queue(void)
723 > {
724 >        char    modname[128];
725 >                                        /* ray-ordered queue */
726 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
727 >                struct rtproc   *rtp = rt_unproc;
728 >                int             n = rtp->nbr;
729                  const char      *cp = rtp->buf;
730                  while (n > 0) {         /* process rays */
731 <                        char    matname[128];
678 <                        char    *mnp = matname;
731 >                        register char   *mnp = modname;
732                                          /* skip leading tabs */
733                          while (n > 0 && *cp == '\t') {
734                                  cp++; n--;
735                          }
736 <                                        /* get material name */
736 >                        if (!n || !(isalpha(*cp) | (*cp == '_')))
737 >                                error(USER, "bad modifier name from rtrace");
738 >                                        /* get modifier name */
739                          while (n > 0 && *cp != '\t') {
740                                  *mnp++ = *cp++; n--;
741                          }
687                        if (!n)
688                                error(USER, "botched modifer name from rtrace");
742                          *mnp = '\0';
743 <                        cp++; n--;      /* skip terminating tab */
743 >                        cp++; n--;      /* eat following tab */
744                          if (n < (int)(sizeof(float)*9))
745                                  error(USER, "incomplete ray value from rtrace");
746                                          /* add ray contribution */
747 <                        add_contrib(matname, (float *)cp);
747 >                        memcpy(rparams, cp, sizeof(float)*9);
748                          cp += sizeof(float)*9; n -= sizeof(float)*9;
749 +                        add_contrib(modname);
750                  }
751                  done_contrib();         /* sum up contributions & output */
752                  lastdone = rtp->raynum;
753 <                free(rtp->buf);
754 <                                        /* catch up with unprocessed list */
755 <                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 <                }
753 >                free(rtp->buf);         /* free up buffer space */
754 >                rt_unproc = rtp->next;
755 >                free(rtp);              /* done with this ray tree */
756          }
723        rtp->raynum = 0;                /* clear path for next ray tree */
724        rtp->bsiz = 0;
725        rtp->buf = NULL;
757   }
758  
759   /* wait for rtrace process to finish with ray tree */
# Line 763 | Line 794 | wait_rproc(void)
794                          if (rt->buf == NULL) {
795                                  rt->bsiz = treebufsiz;
796                                  rt->buf = (char *)malloc(treebufsiz);
797 <                        } else if (rt->bpos + BUFSIZ > rt->buf + rt->bsiz) {
797 >                        } else if (rt->nbr + BUFSIZ > rt->bsiz) {
798                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
799                                          rt->bsiz = treebufsiz;
800                                  else
# Line 772 | Line 803 | wait_rproc(void)
803                          }
804                          if (rt->buf == NULL)
805                                  error(SYSTEM, "out of memory in wait_rproc");
806 <                        nr = read(rt->pd.r, rt->bpos,
807 <                                        rt->buf + rt->bsiz - rt->bpos);
777 <                        if (!nr)
806 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
807 >                        if (nr <= 0)
808                                  error(USER, "rtrace process died");
809 <                        rt->bpos += nr;         /* advance buffer & check */
810 <                        if (rt->bpos[-1] == '\t' && rt->bpos[-2] == '-') {
811 <                                rt->bpos -= 2;  /* elide terminator */
812 <                                process_rays(rt);
809 >                        rt->nbr += nr;          /* advance & check */
810 >                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
811 >                                                        "~\t~\t", 4)) {
812 >                                rt->nbr -= 4;   /* elide terminator */
813 >                                queue_raytree(rt);
814                                  rtfree = rt;    /* ready for next ray */
815                          }
816                  }
# Line 810 | Line 841 | tracecontribs(FILE *fin)
841          while ((iblen = getinp(inpbuf, fin)) > 0) {
842                  if (lastray+1 < lastray) {      /* counter rollover? */
843                          while (wait_rproc() != NULL)
844 <                                ;
844 >                                process_queue();
845                          lastdone = lastray = 0;
846                  }
847                  rtp = get_rproc();              /* get avail. rtrace process */
848 <                rtp->raynum = ++lastray;        /* assign this ray to it */
848 >                rtp->raynum = ++lastray;        /* assign ray to it */
849                  writebuf(rtp->pd.w, inpbuf, iblen);
850                  if (!--raysleft)
851 <                        break;                  /* explicit EOF */
851 >                        break;
852 >                process_queue();                /* catch up with results */
853          }
854          while (wait_rproc() != NULL)            /* process outstanding rays */
855 <                ;
855 >                process_queue();
856 >        if (raysleft > 0)
857 >                error(USER, "unexpected EOF on input");
858   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines