--- ray/src/util/rtcontrib.c 2005/10/07 03:45:15 1.33 +++ ray/src/util/rtcontrib.c 2006/04/05 14:33:34 1.42 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.33 2005/10/07 03:45:15 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.42 2006/04/05 14:33:34 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -101,12 +101,16 @@ struct rtproc { /* rtrace command and defaults */ char *rtargv[256+2*MAXMODLIST] = { "rtrace", "-dj", ".5", "-dr", "3", - "-ab", "1", "-ad", "128", }; + "-ab", "1", "-ad", "350", }; + int rtargc = 9; /* overriding rtrace options */ -char *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0", +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 */ + struct rtproc rt0; /* head of rtrace process list */ struct rtproc *rt_unproc = NULL; /* unprocessed ray trees */ @@ -123,6 +127,7 @@ int inpfmt = 'a'; /* input format */ int outfmt = 'a'; /* output format */ int header = 1; /* output header? */ +int force_open = 0; /* truncate existing output? */ int xres = 0; /* horiz. output resolution */ int yres = 0; /* vert. output resolution */ @@ -207,6 +212,7 @@ fmterr: int main(int argc, char *argv[]) { + int contrib = 0; int nprocs = 1; int recover = 0; char *curout = NULL; @@ -217,7 +223,7 @@ main(int argc, char *argv[]) /* need at least one argument */ if (argc < 2) { fprintf(stderr, -"Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", +"Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", argv[0]); exit(1); } @@ -239,16 +245,33 @@ main(int argc, char *argv[]) } if (argv[i][0] == '-') switch (argv[i][1]) { - case 'r': /* recover output */ - if (argv[i][2]) break; - recover++; - continue; case 'n': /* number of processes */ if (argv[i][2] || i >= argc-2) break; nprocs = atoi(argv[++i]); if (nprocs <= 0) error(USER, "illegal number of processes"); continue; + case 'V': /* output contributions */ + switch (argv[i][2]) { + case '\0': + contrib = !contrib; + continue; + case '+': case '1': + case 'T': case 't': + case 'Y': case 'y': + contrib = 1; + continue; + case '-': case '0': + case 'F': case 'f': + case 'N': case 'n': + contrib = 0; + continue; + } + break; + case 'r': /* recover output */ + if (argv[i][2]) break; + recover++; + continue; case 'h': /* output header? */ switch (argv[i][2]) { case '\0': @@ -266,7 +289,7 @@ main(int argc, char *argv[]) continue; } break; - case 'f': /* file or i/o format */ + case 'f': /* file or force or format */ if (!argv[i][2]) { char *fpath; if (i >= argc-2) break; @@ -281,6 +304,10 @@ main(int argc, char *argv[]) fcompile(fpath); continue; } + if (argv[i][2] == 'o') { + force_open++; + continue; + } setformat(argv[i]+2); continue; case 'e': /* expression */ @@ -320,6 +347,9 @@ main(int argc, char *argv[]) rtargv[rtargc++] = argv[++i]; addmodfile(argv[i], curout, binval, bincnt); continue; + case 'P': /* persist file */ + error(USER, "persist file is automatic"); + break; } rtargv[rtargc++] = argv[i]; /* assume rtrace option */ } @@ -328,11 +358,14 @@ main(int argc, char *argv[]) /* add "mandatory" rtrace settings */ for (j = 0; myrtopts[j] != NULL; j++) rtargv[rtargc++] = myrtopts[j]; + rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF; /* just asking for defaults? */ if (!strcmp(argv[i], "-defaults")) { char sxres[16], syres[16]; char *rtpath; printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); + printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-', + contrib ? "contributions" : "coefficients"); fflush(stdout); /* report OUR options */ rtargv[rtargc++] = header ? "-h+" : "-h-"; sprintf(fmt, "-f%c%c", inpfmt, outfmt); @@ -343,7 +376,6 @@ main(int argc, char *argv[]) rtargv[rtargc++] = "-y"; sprintf(syres, "%d", yres); rtargv[rtargc++] = syres; - rtargv[rtargc++] = "-oTW"; rtargv[rtargc++] = "-defaults"; rtargv[rtargc] = NULL; rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); @@ -535,7 +567,7 @@ addmodifier(char *modn, char *outf, char *binv, int bi mp = growmodifier(mp, bincnt); lep->data = (char *)mp; /* allocate output streams */ - for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; ) + for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i-- > 0; ) getostream(mp->outspec, mp->modname, i, 1); return mp; } @@ -702,15 +734,17 @@ getostream(const char *ospec, const char *mname, int b sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); if (sop == NULL) error(SYSTEM, "out of memory in getostream"); - sop->reclen = oname[0] == '!' ? CNT_PIPE : 0; + sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN; sop->ofp = NULL; /* open iff noopen==0 */ lep->data = (char *)sop; } if (!noopen && sop->ofp == NULL) { /* open output stream */ - int i; + long i; if (oname[0] == '!') /* output to command */ sop->ofp = popen(oname+1, "w"); - else + else if (!force_open && access(oname, F_OK) == 0) + errno = EEXIST; /* file exists */ + else /* else open it */ sop->ofp = fopen(oname, "w"); if (sop->ofp == NULL) { sprintf(errmsg, "cannot open '%s' for writing", oname); @@ -734,7 +768,8 @@ getostream(const char *ospec, const char *mname, int b } printresolu(sop->ofp); /* play catch-up */ - for (i = 0; i < lastdone; i++) { + for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone; + i--; ) { static const DCOLOR nocontrib = BLKCOLOR; put_contrib(nocontrib, sop->ofp); if (outfmt == 'a') @@ -752,6 +787,8 @@ getostream(const char *ospec, const char *mname, int b int getinp(char *buf, FILE *fp) { + double dv[3]; + float fv[3]; char *cp; int i; @@ -760,25 +797,35 @@ getinp(char *buf, FILE *fp) cp = buf; /* make sure we get 6 floats */ for (i = 0; i < 6; i++) { if (fgetword(cp, buf+127-cp, fp) == NULL) - return 0; + return -1; + if (i >= 3) + dv[i-3] = atof(cp); if ((cp = fskip(cp)) == NULL || *cp) - return 0; + return -1; *cp++ = ' '; } getc(fp); /* get/put eol */ *cp-- = '\0'; *cp = '\n'; + if (DOT(dv,dv) <= FTINY*FTINY) + return 0; /* dummy ray */ return strlen(buf); case 'f': if (fread(buf, sizeof(float), 6, fp) < 6) - return 0; + return -1; + memcpy(fv, buf+3*sizeof(float), 3*sizeof(float)); + if (DOT(fv,fv) <= FTINY*FTINY) + return 0; /* dummy ray */ return sizeof(float)*6; case 'd': if (fread(buf, sizeof(double), 6, fp) < 6) - return 0; + return -1; + memcpy(dv, buf+3*sizeof(double), 3*sizeof(double)); + if (DOT(dv,dv) <= FTINY*FTINY) + return 0; /* dummy ray */ return sizeof(double)*6; } error(INTERNAL, "botched input format"); - return 0; /* pro forma return */ + return -1; /* pro forma return */ } static float rparams[9]; /* traced ray parameters */ @@ -938,7 +985,9 @@ process_queue(void) if (!n || !(isalpha(*cp) | (*cp == '_'))) error(USER, "bad modifier name from rtrace"); /* get modifier name */ - while (n > 0 && *cp != '\t') { + while (n > 1 && *cp != '\t') { + if (mnp - modname >= sizeof(modname)-2) + error(INTERNAL, "modifier name too long"); *mnp++ = *cp++; n--; } *mnp = '\0'; @@ -952,7 +1001,8 @@ process_queue(void) } done_contrib(); /* sum up contributions & output */ lastdone = rtp->raynum; - free(rtp->buf); /* free up buffer space */ + if (rtp->buf != NULL) /* free up buffer space */ + free(rtp->buf); rt_unproc = rtp->next; free(rtp); /* done with this ray tree */ } @@ -1040,19 +1090,27 @@ trace_contribs(FILE *fin) int iblen; struct rtproc *rtp; /* loop over input */ - while ((iblen = getinp(inpbuf, fin)) > 0) { - if (lastray+1 < lastray || /* need reset? */ - queue_length() > 5*nrtprocs()) { + while ((iblen = getinp(inpbuf, fin)) >= 0) { + if (!iblen || /* need reset? */ + queue_length() > 10*nrtprocs() || + lastray+1 < lastray) { while (wait_rproc() != NULL) process_queue(); - lastdone = lastray = 0; + if (lastray+1 < lastray) + lastdone = lastray = 0; } rtp = get_rproc(); /* get avail. rtrace process */ - rtp->raynum = ++lastray; /* assign ray to it */ - writebuf(rtp->pd.w, inpbuf, iblen); - if (raysleft && !--raysleft) - break; + 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 */ + } process_queue(); /* catch up with results */ + if (raysleft && !--raysleft) + break; /* preemptive EOI */ } while (wait_rproc() != NULL) /* process outstanding rays */ process_queue(); @@ -1128,7 +1186,7 @@ recover_output(FILE *fin) if (oent->data != NULL) { sout = *(STREAMOUT *)oent->data; } else { - sout.reclen = 0; + sout.reclen = CNT_UNKNOWN; sout.ofp = NULL; } if (sout.ofp != NULL) { /* already open? */ @@ -1210,7 +1268,7 @@ recover_output(FILE *fin) lu_doall(&ofiletab, myseeko, &nvals); /* skip repeated input */ for (nvals = 0; nvals < lastout; nvals++) - if (getinp(oname, fin) <= 0) + if (getinp(oname, fin) < 0) error(USER, "unexpected EOF on input"); lastray = lastdone = (unsigned long)lastout; if (raysleft)