--- ray/src/util/rtcontrib.c 2009/02/07 05:40:47 1.52 +++ ray/src/util/rtcontrib.c 2011/04/06 00:39:07 1.62 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.52 2009/02/07 05:40:47 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.62 2011/04/06 00:39:07 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -20,7 +20,7 @@ static const char RCSid[] = "$Id: rtcontrib.c,v 1.52 2 #define MAXMODLIST 1024 /* maximum modifiers we'll track */ #endif -int treebufsiz = BUFSIZ; /* current tree buffer size */ +size_t treebufsiz = BUFSIZ; /* current tree buffer size */ typedef double DCOLOR[3]; /* double-precision color */ @@ -92,9 +92,9 @@ struct rtproc { struct rtproc *next; /* next in list of processes */ SUBPROC pd; /* rtrace pipe descriptors */ unsigned long raynum; /* ray number for this tree */ - int bsiz; /* ray tree buffer length */ + size_t bsiz; /* ray tree buffer length */ char *buf; /* ray tree buffer */ - int nbr; /* number of bytes from rtrace */ + size_t nbr; /* number of bytes from rtrace */ }; /* rtrace process buffer */ /* rtrace command and defaults */ @@ -107,8 +107,8 @@ int rtargc = 9; char *myrtopts[] = { "-h-", "-x", "1", "-y", "0", "-dt", "0", "-as", "0", "-aa", "0", NULL }; -#define RTCOEFF "-o~~TmWdp" /* compute coefficients only */ -#define RTCONTRIB "-o~~TmVdp" /* compute ray contributions */ +#define RTCOEFF "-o~~~TmWdp" /* compute coefficients only */ +#define RTCONTRIB "-o~~~TmVdp" /* compute ray contributions */ struct rtproc rt0; /* head of rtrace process list */ @@ -340,7 +340,7 @@ main(int argc, char *argv[]) case 'b': /* bin expression/count */ if (i >= argc-2) break; if (argv[i][2] == 'n') { - bincnt = atoi(argv[++i]); + bincnt = (int)(eval(argv[++i]) + .5); continue; } if (argv[i][2]) break; @@ -378,14 +378,16 @@ main(int argc, char *argv[]) rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF; /* just asking for defaults? */ if (!strcmp(argv[i], "-defaults")) { - char sxres[16], syres[16]; + char nps[8], sxres[16], syres[16]; char *rtpath; - printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); printf("-c %-5d\t\t\t# accumulated rays per record\n", accumulate); printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-', contrib ? "contributions" : "coefficients"); fflush(stdout); /* report OUR options */ + rtargv[rtargc++] = "-n"; + sprintf(nps, "%d", nprocs); + rtargv[rtargc++] = nps; rtargv[rtargc++] = header ? "-h+" : "-h-"; sprintf(fmt, "-f%c%c", inpfmt, outfmt); rtargv[rtargc++] = fmt; @@ -543,7 +545,7 @@ init(int np) raysleft = 0; if ((account = accumulate) > 0) raysleft *= accumulate; - waitflush = xres; + waitflush = (yres > 0) & (xres > 1) ? 0 : xres; if (!recover) return; /* recover previous values */ @@ -580,7 +582,7 @@ addmodifier(char *modn, char *outf, char *binv, int bi error(USER, errmsg); } if (nmods >= MAXMODLIST) - error(USER, "too many modifiers"); + error(INTERNAL, "too many modifiers"); modname[nmods++] = modn; /* XXX assumes static string */ lep->key = modn; /* XXX assumes static string */ mp = (MODCONT *)malloc(sizeof(MODCONT)); @@ -588,15 +590,20 @@ addmodifier(char *modn, char *outf, char *binv, int bi error(SYSTEM, "out of memory in addmodifier"); mp->outspec = outf; /* XXX assumes static string */ mp->modname = modn; /* XXX assumes static string */ - if (binv != NULL) - mp->binv = eparse(binv); - else - mp->binv = eparse("0"); - mp->nbins = 1; + if (binv == NULL) + binv = "0"; /* use single bin if unspecified */ + mp->binv = eparse(binv); + if (mp->binv->type == NUM) { /* check value if constant */ + bincnt = (int)(evalue(mp->binv) + 1.5); + if (bincnt != 1) { + sprintf(errmsg, "illegal non-zero constant for bin (%s)", + binv); + error(USER, errmsg); + } + } + mp->nbins = 1; /* initialize results holder */ setcolor(mp->cbin[0], 0., 0., 0.); - if (mp->binv->type == NUM) /* assume one bin if constant */ - bincnt = 1; - else if (bincnt > 1) + if (bincnt > 1) mp = growmodifier(mp, bincnt); lep->data = (char *)mp; /* allocate output streams */ @@ -659,6 +666,10 @@ ofname(char *oname, const char *ospec, const char *mna mnp = cp; break; case 'd': + case 'i': + case 'o': + case 'x': + case 'X': if (bnp != NULL) return -1; bnp = cp; @@ -725,8 +736,6 @@ printresolu(FILE *fout, int xr, int yr) { if ((xr > 0) & (yr > 0)) /* resolution string */ fprtresolu(xr, yr, fout); - if (xres > 0) /* global flush flag */ - fflush(fout); } /* Get output stream pointer (open and write header if new and noopen==0) */ @@ -747,6 +756,8 @@ getostream(const char *ospec, const char *mname, int b if (header) printheader(stdout, NULL); printresolu(stdout, xres, yres); + if (waitflush > 0) + fflush(stdout); stdos.xr = xres; stdos.yr = yres; using_stdout = 1; } @@ -815,7 +826,7 @@ getostream(const char *ospec, const char *mname, int b if (outfmt == 'a') putc('\n', sop->ofp); } - if (xres > 0) + if (waitflush > 0) fflush(sop->ofp); } sop->reclen += noopen; /* add to length if noopen */ @@ -982,7 +993,7 @@ done_contrib(int navg) if (using_stdout & (outfmt == 'a')) putc('\n', stdout); if (!waitflush) { - waitflush = xres; + waitflush = (yres > 0) & (xres > 1) ? 0 : xres; if (using_stdout) fflush(stdout); } @@ -1064,7 +1075,7 @@ wait_rproc(void) { struct rtproc *rtfree = NULL; fd_set readset, errset; - int nr; + ssize_t nr; struct rtproc *rt; int n; @@ -1095,7 +1106,7 @@ wait_rproc(void) continue; if (rt->buf == NULL) { rt->bsiz = treebufsiz; - rt->buf = (char *)malloc(treebufsiz); + rt->buf = (char *)malloc(rt->bsiz); } else if (rt->nbr + BUFSIZ > rt->bsiz) { if (rt->bsiz + BUFSIZ <= treebufsiz) rt->bsiz = treebufsiz; @@ -1109,9 +1120,9 @@ wait_rproc(void) if (nr <= 0) error(USER, "rtrace process died"); rt->nbr += nr; /* advance & check */ - if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4, - "~\t~\t", 4)) { - rt->nbr -= 4; /* elide terminator */ + if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6, + "~\t~\t~\t", 6)) { + rt->nbr -= 6; /* elide terminator */ queue_raytree(rt); rtfree = rt; /* ready for next ray */ } @@ -1148,22 +1159,21 @@ trace_contribs(FILE *fin) "dummy ray(s) ignored during accumulation\n"); continue; } - if (!iblen || /* need reset? */ + if (!iblen || /* need flush/reset? */ queue_length() > 10*nrtprocs() || lastray+1 < lastray) { while (wait_rproc() != NULL) process_queue(); - if (lastray+1 < lastray) - lastdone = lastray = 0; + lastdone = lastray = 0; } rtp = get_rproc(); /* get avail. rtrace process */ rtp->raynum = ++lastray; /* assign ray */ if (iblen) { /* trace ray if valid */ writebuf(rtp->pd.w, inpbuf, iblen); } else { /* else bypass dummy ray */ - queue_raytree(rtp); /* empty tree */ - if ((yres <= 0) | (waitflush > 1)) - waitflush = 1; /* flush after this */ + queue_raytree(rtp); /* queue empty ray/record */ + if ((yres <= 0) | (xres <= 0)) + waitflush = 1; /* flush right after */ } process_queue(); /* catch up with results */ if (raysleft && !--raysleft)