--- ray/src/util/rtcontrib.c 2005/05/25 04:45:22 1.1 +++ ray/src/util/rtcontrib.c 2005/06/10 20:44:00 1.19 @@ -1,13 +1,13 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.1 2005/05/25 04:45:22 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.19 2005/06/10 20:44:00 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources */ +#include "standard.h" #include -#include "rtio.h" -#include "rterror.h" +#include #include "platform.h" #include "rtprocess.h" #include "selcall.h" @@ -16,18 +16,14 @@ static const char RCSid[] = "$Id: rtcontrib.c,v 1.1 20 #include "lookup.h" #include "calcomp.h" -#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ -#undef getc -#undef putc -#define getc getc_unlocked -#define putc putc_unlocked -#define ferror ferror_unlocked +#ifndef MAXMODLIST +#define MAXMODLIST 1024 /* maximum modifiers we'll track */ #endif -#define MAXMODLIST 1024 /* maximum modifiers we'll track */ - int treebufsiz = BUFSIZ; /* current tree buffer size */ +typedef double DCOLOR[3]; /* double-precision color */ + /* * The modcont structure is used to accumulate ray contributions * for a particular modifier, which may be subdivided into bins @@ -43,7 +39,7 @@ typedef struct { const char *modname; /* modifier name */ EPNODE *binv; /* bin value expression */ int nbins; /* number of accumulation bins */ - COLOR cbin[1]; /* contribution bins (extends struct) */ + DCOLOR cbin[1]; /* contribution bins (extends struct) */ } MODCONT; /* modifier contribution */ static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } @@ -55,7 +51,12 @@ static void closefile(void *p) { fclose((FILE *)p); } LUTAB ofiletab = LU_SINIT(free,closefile); /* output file table */ +#define OF_MODIFIER 01 +#define OF_BIN 02 + FILE *getofile(const char *ospec, const char *mname, int bn); +int ofname(char *oname, const char *ospec, const char *mname, int bn); +void printheader(FILE *fout, const char *info); /* * The rcont structure is used to manage i/o with a particular @@ -66,26 +67,26 @@ FILE *getofile(const char *ospec, const char *mname, i struct rtproc { struct rtproc *next; /* next in list of processes */ SUBPROC pd; /* rtrace pipe descriptors */ - unsigned long raynum; /* ray number for this buffer */ - int bsiz; /* rtrace buffer length */ - char *buf; /* rtrace i/o buffer */ - char *bpos; /* current buffer position */ -}; /* rtrace process */ + unsigned long raynum; /* ray number for this tree */ + int bsiz; /* ray tree buffer length */ + char *buf; /* ray tree buffer */ + int nbr; /* number of bytes from rtrace */ +}; /* rtrace process buffer */ /* rtrace command and defaults */ -char *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3", - "-ab", "1", "-ad", "512", }; +char *rtargv[256+2*MAXMODLIST] = { "rtrace", + "-dj", ".5", "-dr", "3", + "-ab", "1", "-ad", "128", }; int rtargc = 9; /* overriding rtrace options */ -char *myrtopts[] = { "-o-TmWdp", "-h-", - "-x", "1", "-y", "0", NULL }; +char *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0", + "-dt", "0", "-as", "0", "-aa", "0", NULL }; struct rtproc rt0; /* head of rtrace process list */ struct rtproc *rt_unproc = NULL; /* unprocessed ray trees */ char persistfn[] = "pfXXXXXX"; /* persist file name */ -char fmt[8]; /* rtrace i/o format */ int gargc; /* global argc */ char **gargv; /* global argv */ @@ -100,25 +101,31 @@ int header = 1; /* output header? */ int xres = 0; /* horiz. output resolution */ int yres = 0; /* vert. output resolution */ -long raysleft; /* number of rays left to trace */ +unsigned long raysleft; /* number of rays left to trace */ long waitflush; /* how long until next flush */ unsigned long lastray = 0; /* last ray number sent */ unsigned long lastdone = 0; /* last ray processed */ +int using_stdout = 0; /* are we using stdout? */ + const char *modname[MAXMODLIST]; /* ordered modifier name list */ int nmods = 0; /* number of modifiers */ MODCONT *addmodifier(char *modn, char *outf, char *binv); +void addmodfile(char *fname, char *outf, char *binv); -int done_rprocs(struct rtproc *rtp); void init(int np); -void tracecontribs(FILE *fp); +int done_rprocs(struct rtproc *rtp); +void recover_output(FILE *fin); +void trace_contribs(FILE *fin); struct rtproc *wait_rproc(void); struct rtproc *get_rproc(void); -void process_rays(struct rtproc *rtp); +void queue_raytree(struct rtproc *rtp); +void process_queue(void); -void add_contrib(const char *modn, float rayval[7]); +void put_contrib(const DCOLOR cnt, FILE *fout); +void add_contrib(const char *modn); void done_contrib(void); /* set input/output format */ @@ -126,9 +133,11 @@ static void setformat(const char *fmt) { switch (fmt[0]) { - case 'a': case 'f': case 'd': + SET_FILE_BINARY(stdin); + /* fall through */ + case 'a': inpfmt = fmt[0]; break; default: @@ -159,93 +168,118 @@ int main(int argc, char *argv[]) { int nprocs = 1; + int recover = 0; char *curout = NULL; char *binval = NULL; - int i; - /* set global argument list */ - gargc = argc; + char fmt[8]; + int i, j; + /* global program name */ gargv = argv; - /* set up calcomp functions */ + /* initialize calcomp routines */ esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW; esupport &= ~(E_OUTCHAN); + varset("PI", ':', PI); /* get our options */ - for (i = 1; i < argc && argv[i][0] == '-'; i++) { - switch (argv[i][1]) { - case 'n': /* number of processes */ - if (argv[i][2] || i >= argc-1) break; - nprocs = atoi(argv[++i]); - if (nprocs <= 0) - error(USER, "illegal number of processes"); - continue; - case 'h': /* output header? */ - switch (argv[i][2]) { - case '\0': - header = !header; + for (i = 1; i < argc-1; i++) { + /* expand arguments */ + while ((j = expandarg(&argc, &argv, i)) > 0) + ; + if (j < 0) { + fprintf(stderr, "%s: cannot expand '%s'", + argv[0], argv[i]); + exit(1); + } + if (argv[i][0] == '-') + switch (argv[i][1]) { + case 'r': /* recover output */ + if (argv[i][2]) break; + recover++; continue; - case '+': case '1': case 'T': case 't': - header = 1; + case 'n': /* number of processes */ + if (argv[i][2] || i >= argc-1) break; + nprocs = atoi(argv[++i]); + if (nprocs <= 0) + error(USER, "illegal number of processes"); continue; - case '-': case '0': case 'F': case 'f': - header = 0; + case 'h': /* output header? */ + switch (argv[i][2]) { + case '\0': + header = !header; + continue; + case '+': case '1': + case 'T': case 't': + case 'Y': case 'y': + header = 1; + continue; + case '-': case '0': + case 'F': case 'f': + case 'N': case 'n': + header = 0; + continue; + } + break; + case 'f': /* file or i/o format */ + if (!argv[i][2]) { + char *fpath; + if (i >= argc-1) break; + fpath = getpath(argv[++i], + getrlibpath(), R_OK); + if (fpath == NULL) { + sprintf(errmsg, + "cannot find file '%s'", + argv[i]); + error(USER, errmsg); + } + fcompile(fpath); + continue; + } + setformat(argv[i]+2); continue; - } - break; - case 'f': /* file or i/o format */ - if (!argv[i][2]) { - if (i >= argc-1) break; - fcompile(argv[++i]); + case 'e': /* expression */ + if (argv[i][2] || i >= argc-1) break; + scompile(argv[++i], NULL, 0); continue; + case 'o': /* output file spec. */ + if (argv[i][2] || i >= argc-1) break; + curout = argv[++i]; + continue; + case 'x': /* horiz. output resolution */ + if (argv[i][2] || i >= argc-1) break; + xres = atoi(argv[++i]); + continue; + case 'y': /* vert. output resolution */ + if (argv[i][2] || i >= argc-1) break; + yres = atoi(argv[++i]); + continue; + case 'b': /* bin expression */ + if (argv[i][2] || i >= argc-1) break; + binval = argv[++i]; + continue; + case 'm': /* modifier name */ + if (argv[i][2] || i >= argc-1) break; + rtargv[rtargc++] = "-ti"; + rtargv[rtargc++] = argv[++i]; + addmodifier(argv[i], curout, binval); + continue; + case 'M': /* modifier file */ + if (argv[i][2] || i >= argc-1) break; + rtargv[rtargc++] = "-tI"; + rtargv[rtargc++] = argv[++i]; + addmodfile(argv[i], curout, binval); + continue; } - setformat(argv[i]+2); - continue; - case 'e': /* expression */ - if (argv[i][2] || i >= argc-1) break; - scompile(argv[++i], NULL, 0); - continue; - case 'o': /* output file spec. */ - if (argv[i][2] || i >= argc-1) break; - curout = argv[++i]; - continue; - case 'x': /* horiz. output resolution */ - if (argv[i][2] || i >= argc-1) break; - xres = atoi(argv[++i]); - continue; - case 'y': /* vert. output resolution */ - if (argv[i][2] || i >= argc-1) break; - yres = atoi(argv[++i]); - continue; - case 'l': /* limit distance? */ - if (argv[i][2] != 'd') break; - rtargv[rtargc++] = argv[i]; - continue; - case 'b': /* bin expression */ - if (argv[i][2] || i >= argc-1) break; - binval = argv[++i]; - continue; - case 'm': /* modifier name */ - if (argv[i][2] || i >= argc-1) break; - rtargv[rtargc++] = "-ti"; - rtargv[rtargc++] = argv[++i]; - addmodifier(argv[i], curout, binval); - continue; - } - break; /* break into rtrace options */ + rtargv[rtargc++] = argv[i]; /* assume rtrace option */ } - for ( ; i < argc; i++) /* transfer rtrace-specific options */ - rtargv[rtargc++] = argv[i]; - if (!strcmp(rtargv[--rtargc], "-defaults")) - nprocs = 0; - if (nprocs > 1) { /* add persist file if parallel invocation */ - rtargv[rtargc++] = "-PP"; - rtargv[rtargc++] = mktemp(persistfn); - } + /* set global argument list */ + gargc = argc; gargv = argv; /* add "mandatory" rtrace settings */ - for (i = 0; myrtopts[i] != NULL; i++) - rtargv[rtargc++] = myrtopts[i]; - if (!nprocs) { /* just asking for defaults? */ + for (j = 0; myrtopts[j] != NULL; j++) + rtargv[rtargc++] = myrtopts[j]; + /* just asking for defaults? */ + if (!strcmp(argv[i], "-defaults")) { char sxres[16], syres[16]; char *rtpath; - printf("-n 1\t\t\t\t# number of processes\n", nprocs); + printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); fflush(stdout); /* report OUR options */ rtargv[rtargc++] = header ? "-h+" : "-h-"; sprintf(fmt, "-f%c%c", inpfmt, outfmt); @@ -256,7 +290,7 @@ main(int argc, char *argv[]) rtargv[rtargc++] = "-y"; sprintf(syres, "%d", yres); rtargv[rtargc++] = syres; - rtargv[rtargc++] = "-oW"; + rtargv[rtargc++] = "-oTW"; rtargv[rtargc++] = "-defaults"; rtargv[rtargc] = NULL; rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); @@ -269,19 +303,23 @@ main(int argc, char *argv[]) perror(rtpath); /* execv() should not return */ exit(1); } - if (!nmods) - error(USER, "No modifiers specified"); - if (argc < 2 || argv[argc-1][0] == '-') - error(USER, "missing octree argument"); + if (nprocs > 1) { /* add persist file if parallel */ + rtargv[rtargc++] = "-PP"; + rtargv[rtargc++] = mktemp(persistfn); + } /* add format string */ sprintf(fmt, "-f%cf", inpfmt); rtargv[rtargc++] = fmt; /* octree argument is last */ - rtargv[rtargc++] = octree = argv[argc-1]; + if (i <= 0 || i != argc-1 || argv[i][0] == '-') + error(USER, "missing octree argument"); + rtargv[rtargc++] = octree = argv[i]; rtargv[rtargc] = NULL; - /* start rtrace & sum contributions */ + /* start rtrace & compute contributions */ init(nprocs); - tracecontribs(stdin); + if (recover) /* perform recovery if requested */ + recover_output(stdin); + trace_contribs(stdin); quit(0); } @@ -333,18 +371,28 @@ quit(int status) exit(status); /* flushes all output streams */ } -/* start rtrace and initialize buffers */ +/* start rtrace processes and initialize */ void init(int np) { struct rtproc *rtp; int i; int maxbytes; + /* make sure we have something to do */ + if (!nmods) + error(USER, "No modifiers specified"); /* assign ray variables */ scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0); scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0); /* set up signal handling */ -#ifdef SIGPIPE /* not present on Windows */ + signal(SIGINT, quit); +#ifdef SIGHUP + signal(SIGHUP, quit); +#endif +#ifdef SIGTERM + signal(SIGTERM, quit); +#endif +#ifdef SIGPIPE signal(SIGPIPE, quit); #endif rtp = &rt0; /* start rtrace process(es) */ @@ -360,9 +408,10 @@ init(int np) error(SYSTEM, "cannot start rtrace process"); if (maxbytes > treebufsiz) treebufsiz = maxbytes; + rtp->raynum = 0; rtp->bsiz = 0; rtp->buf = NULL; - rtp->raynum = 0; + rtp->nbr = 0; if (i == np) /* last process? */ break; if (i == 1) @@ -375,7 +424,7 @@ init(int np) rtp->next = NULL; /* terminate list */ if (yres > 0) { if (xres > 0) - raysleft = xres*yres; + raysleft = (unsigned long)xres*yres; else raysleft = yres; } else @@ -413,6 +462,18 @@ addmodifier(char *modn, char *outf, char *binv) return mp; } +/* add modifiers from a file list */ +void +addmodfile(char *fname, char *outf, char *binv) +{ + char *mname[MAXMODLIST]; + int i; + /* load the file & store strings */ + wordfile(mname, fname); + for (i = 0; mname[i]; i++) /* add each one */ + addmodifier(mname[i], outf, binv); +} + /* put string to stderr */ void eputs(char *s) @@ -428,20 +489,73 @@ eputs(char *s) midline = s[strlen(s)-1] != '\n'; } +/* construct output file name and return flags whether modifier/bin present */ +int +ofname(char *oname, const char *ospec, const char *mname, int bn) +{ + const char *mnp = NULL; + const char *bnp = NULL; + const char *cp; + + if (ospec == NULL) + return -1; + for (cp = ospec; *cp; cp++) /* check format position(s) */ + if (*cp == '%') { + do + ++cp; + while (isdigit(*cp)); + switch (*cp) { + case '%': + break; + case 's': + if (mnp != NULL) + return -1; + mnp = cp; + break; + case 'd': + if (bnp != NULL) + return -1; + bnp = cp; + break; + default: + return -1; + } + } + if (mnp != NULL) { /* create file name */ + if (bnp != NULL) { + if (bnp > mnp) + sprintf(oname, ospec, mname, bn); + else + sprintf(oname, ospec, bn, mname); + return OF_MODIFIER|OF_BIN; + } + sprintf(oname, ospec, mname); + return OF_MODIFIER; + } + if (bnp != NULL) { + sprintf(oname, ospec, bn); + return OF_BIN; + } + strcpy(oname, ospec); + return 0; +} + /* write header to the given output stream */ void -printheader(FILE *fout) +printheader(FILE *fout, const char *info) { extern char VersionID[]; FILE *fin = fopen(octree, "r"); if (fin == NULL) quit(1); - getheader(fin, (gethfunc *)fputs, fout); /* copy octree header */ + checkheader(fin, "ignore", fout); /* copy octree header */ fclose(fin); printargs(gargc-1, gargv, fout); /* add our command */ fprintf(fout, "SOFTWARE= %s\n", VersionID); fputnow(fout); + if (info != NULL) /* add extra info if given */ + fputs(info, fout); switch (outfmt) { /* add output format */ case 'a': fputformat("ascii", fout); @@ -468,80 +582,88 @@ printheader(FILE *fout) FILE * getofile(const char *ospec, const char *mname, int bn) { - static int using_stdout = 0; - const char *mnp = NULL; - const char *bnp = NULL; - const char *cp; - char ofname[1024]; + int ofl; + char oname[1024]; LUENT *lep; if (ospec == NULL) { /* use stdout? */ - if (!using_stdout && header) - printheader(stdout); + if (!using_stdout) { + if (outfmt != 'a') + SET_FILE_BINARY(stdout); + if (header) + printheader(stdout, NULL); + } using_stdout = 1; return stdout; } - for (cp = ospec; *cp; cp++) /* check format position(s) */ - if (*cp == '%') { - do - ++cp; - while (isdigit(*cp)); - switch (*cp) { - case '%': - break; - case 's': - if (mnp != NULL) - goto badspec; - mnp = cp; - break; - case 'd': - if (bnp != NULL) - goto badspec; - bnp = cp; - break; - default: - goto badspec; - } - } - if (mnp != NULL) { /* create file name */ - if (bnp != NULL) { - if (bnp > mnp) - sprintf(ofname, ospec, mname, bn); - else - sprintf(ofname, ospec, bn, mname); - } else - sprintf(ofname, ospec, mname); - } else if (bnp != NULL) - sprintf(ofname, ospec, bn); - else - strcpy(ofname, ospec); - lep = lu_find(&ofiletab, ofname); /* look it up */ + ofl = ofname(oname, ospec, mname, bn); /* get output name */ + if (ofl < 0) { + sprintf(errmsg, "bad output format '%s'", ospec); + error(USER, errmsg); + } + lep = lu_find(&ofiletab, oname); /* look it up */ if (lep->key == NULL) /* new entry */ - lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname); + lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); if (lep->data == NULL) { /* open output file */ - lep->data = (char *)fopen(ofname, "w"); - if (lep->data == NULL) { - sprintf(errmsg, "cannot open '%s' for writing", ofname); + FILE *fp; + int i; + if (oname[0] == '!') /* output to command */ + fp = popen(oname+1, "w"); + else + fp = fopen(oname, "w"); + if (fp == NULL) { + sprintf(errmsg, "cannot open '%s' for writing", oname); error(SYSTEM, errmsg); } - if (header) - printheader((FILE *)lep->data); + if (outfmt != 'a') + SET_FILE_BINARY(fp); + if (header) { + char info[512]; + char *cp = info; + if (ofl & OF_MODIFIER) { + sprintf(cp, "MODIFIER=%s\n", mname); + while (*cp) ++cp; + } + if (ofl & OF_BIN) { + sprintf(cp, "BIN=%d\n", bn); + while (*cp) ++cp; + } + *cp = '\0'; + printheader(fp, info); + } + /* play catch-up */ + for (i = 0; i < lastdone; i++) { + static const DCOLOR nocontrib = BLKCOLOR; + put_contrib(nocontrib, fp); + if (outfmt == 'a') + putc('\n', fp); + } + if (xres > 0) + fflush(fp); + lep->data = (char *)fp; } return (FILE *)lep->data; /* return open file pointer */ -badspec: - sprintf(errmsg, "bad output format '%s'", ospec); - error(USER, errmsg); - return NULL; /* pro forma return */ } /* read input ray into buffer */ int getinp(char *buf, FILE *fp) { + char *cp; + int i; + switch (inpfmt) { case 'a': - if (fgets(buf, 128, fp) == NULL) - return 0; + cp = buf; /* make sure we get 6 floats */ + for (i = 0; i < 6; i++) { + if (fgetword(cp, buf+127-cp, fp) == NULL) + return 0; + if ((cp = fskip(cp)) == NULL || *cp) + return 0; + *cp++ = ' '; + } + getc(fp); /* get/put eol */ + *cp-- = '\0'; *cp = '\n'; return strlen(buf); case 'f': if (fread(buf, sizeof(float), 6, fp) < 6) @@ -556,7 +678,7 @@ getinp(char *buf, FILE *fp) return 0; /* pro forma return */ } -static const float *rparams = NULL; /* ray parameter pointer */ +static float rparams[9]; /* traced ray parameters */ /* return channel (ray) value */ double @@ -564,14 +686,12 @@ chanvalue(int n) { if (--n < 0 || n >= 6) error(USER, "illegal channel number ($N)"); - if (rparams == NULL) - error(USER, "illegal use of $N in constant expression"); - return rparams[n]; + return rparams[n+3]; } -/* add ray contribution to the appropriate modifier bin */ +/* add current ray contribution to the appropriate modifier bin */ void -add_contrib(const char *modn, float rayval[9]) +add_contrib(const char *modn) { LUENT *le = lu_find(&modconttab, modn); MODCONT *mp = (MODCONT *)le->data; @@ -581,22 +701,20 @@ add_contrib(const char *modn, float rayval[9]) sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn); error(USER, errmsg); } - rparams = rayval + 3; /* for chanvalue */ - eclock++; + eclock++; /* get bin number */ bn = (int)(evalue(mp->binv) + .5); if (bn <= 0) bn = 0; else if (bn > mp->nbins) { /* new bin */ mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + - bn*sizeof(COLOR)); + bn*sizeof(DCOLOR)); if (mp == NULL) error(SYSTEM, "out of memory in add_contrib"); - memset(mp->cbin+mp->nbins, 0, sizeof(COLOR)*(bn+1-mp->nbins)); + memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins)); mp->nbins = bn+1; le->data = (char *)mp; } - addcolor(mp->cbin[bn], rayval); - rparams = NULL; + addcolor(mp->cbin[bn], rparams); } /* output newline to ASCII file and/or flush as requested */ @@ -616,113 +734,132 @@ puteol(const LUENT *e, void *p) return 0; } +/* put out ray contribution to file */ +void +put_contrib(const DCOLOR cnt, FILE *fout) +{ + float fv[3]; + COLR cv; + + switch (outfmt) { + case 'a': + fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]); + break; + case 'f': + fv[0] = cnt[0]; + fv[1] = cnt[1]; + fv[2] = cnt[2]; + fwrite(fv, sizeof(float), 3, fout); + break; + case 'd': + fwrite(cnt, sizeof(double), 3, fout); + break; + case 'c': + setcolr(cv, cnt[0], cnt[1], cnt[2]); + fwrite(cv, sizeof(cv), 1, fout); + break; + default: + error(INTERNAL, "botched output format"); + } +} + /* output ray tallies and clear for next primary */ void done_contrib(void) { int i, j; MODCONT *mp; - FILE *fout; - double dv[3]; - COLR cv; + FILE *fp; /* output modifiers in order */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; - for (j = 0; j < mp->nbins; j++) { - fout = getofile(mp->outspec, mp->modname, j); - switch (outfmt) { - case 'a': - fprintf(fout, "%.6e\t%.6e\t%.6e\t", - mp->cbin[j][RED], - mp->cbin[j][GRN], - mp->cbin[j][BLU]); - break; - case 'f': - fwrite(mp->cbin[j], sizeof(float), 3, fout); - break; - case 'd': - dv[0] = mp->cbin[j][0]; - dv[1] = mp->cbin[j][1]; - dv[2] = mp->cbin[j][2]; - fwrite(dv, sizeof(double), 3, fout); - break; - case 'c': - setcolr(cv, mp->cbin[j][RED], - mp->cbin[j][GRN], mp->cbin[j][BLU]); - fwrite(cv, sizeof(COLR), 1, fout); - break; - default: - error(INTERNAL, "botched output format"); - } - } + fp = getofile(mp->outspec, mp->modname, 0); + put_contrib(mp->cbin[0], fp); + if (mp->nbins > 3 && /* minor optimization */ + fp == getofile(mp->outspec, mp->modname, 1)) + for (j = 1; j < mp->nbins; j++) + put_contrib(mp->cbin[j], fp); + else + for (j = 1; j < mp->nbins; j++) + put_contrib(mp->cbin[j], + getofile(mp->outspec, mp->modname, j)); /* clear for next ray tree */ - memset(mp->cbin, 0, sizeof(COLOR)*mp->nbins); + memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); } --waitflush; /* terminate records */ lu_doall(&ofiletab, puteol, NULL); - if (!waitflush) + if (using_stdout & (outfmt == 'a')) + putc('\n', stdout); + if (!waitflush) { waitflush = xres; + if (using_stdout) + fflush(stdout); + } } -/* process (or save) ray tree produced by rtrace process */ +/* queue completed ray tree produced by rtrace process */ void -process_rays(struct rtproc *rtp) +queue_raytree(struct rtproc *rtp) { - struct rtproc *rtu; - /* check if time to process it */ - if (rtp->raynum == lastdone+1) { - int n = rtp->bpos - rtp->buf; + struct rtproc *rtu, *rtl = NULL; + /* insert following ray order */ + for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next) + if (rtp->raynum < rtu->raynum) + break; + rtu = (struct rtproc *)malloc(sizeof(struct rtproc)); + if (rtu == NULL) + error(SYSTEM, "out of memory in queue_raytree"); + *rtu = *rtp; + if (rtl == NULL) { + rtu->next = rt_unproc; + rt_unproc = rtu; + } else { + rtu->next = rtl->next; + rtl->next = rtu; + } + rtp->raynum = 0; /* clear path for next ray tree */ + rtp->bsiz = 0; + rtp->buf = NULL; + rtp->nbr = 0; +} + +/* process completed ray trees from our queue */ +void +process_queue(void) +{ + char modname[128]; + /* ray-ordered queue */ + while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) { + struct rtproc *rtp = rt_unproc; + int n = rtp->nbr; const char *cp = rtp->buf; while (n > 0) { /* process rays */ - char matname[128]; - char *mnp = matname; + register char *mnp = modname; /* skip leading tabs */ while (n > 0 && *cp == '\t') { cp++; n--; } - /* get material name */ + if (!n || !(isalpha(*cp) | (*cp == '_'))) + error(USER, "bad modifier name from rtrace"); + /* get modifier name */ while (n > 0 && *cp != '\t') { *mnp++ = *cp++; n--; } - if (!n) - error(USER, "botched modifer name from rtrace"); *mnp = '\0'; - cp++; n--; /* skip terminating tab */ + cp++; n--; /* eat following tab */ if (n < (int)(sizeof(float)*9)) error(USER, "incomplete ray value from rtrace"); /* add ray contribution */ - add_contrib(matname, (float *)cp); + memcpy(rparams, cp, sizeof(float)*9); cp += sizeof(float)*9; n -= sizeof(float)*9; + add_contrib(modname); } done_contrib(); /* sum up contributions & output */ lastdone = rtp->raynum; - free(rtp->buf); - /* catch up with unprocessed list */ - while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) { - process_rays(rt_unproc); - rt_unproc = (rtu=rt_unproc)->next; - free(rtu); - } - } else { /* else insert in unprocessed list */ - struct rtproc *rtl = NULL; - for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next) - if (rtp->raynum < rtu->raynum) - break; - rtu = (struct rtproc *)malloc(sizeof(struct rtproc)); - if (rtu == NULL) - error(SYSTEM, "out of memory in process_rays"); - *rtu = *rtp; - if (rtl == NULL) { - rtu->next = rt_unproc; - rt_unproc = rtu; - } else { - rtu->next = rtl->next; - rtl->next = rtu; - } + free(rtp->buf); /* free up buffer space */ + rt_unproc = rtp->next; + free(rtp); /* done with this ray tree */ } - rtp->raynum = 0; /* clear path for next ray tree */ - rtp->bsiz = 0; - rtp->buf = NULL; } /* wait for rtrace process to finish with ray tree */ @@ -763,7 +900,7 @@ wait_rproc(void) if (rt->buf == NULL) { rt->bsiz = treebufsiz; rt->buf = (char *)malloc(treebufsiz); - } else if (rt->bpos + BUFSIZ > rt->buf + rt->bsiz) { + } else if (rt->nbr + BUFSIZ > rt->bsiz) { if (rt->bsiz + BUFSIZ <= treebufsiz) rt->bsiz = treebufsiz; else @@ -772,14 +909,14 @@ wait_rproc(void) } if (rt->buf == NULL) error(SYSTEM, "out of memory in wait_rproc"); - nr = read(rt->pd.r, rt->bpos, - rt->buf + rt->bsiz - rt->bpos); - if (!nr) + nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr); + if (nr <= 0) error(USER, "rtrace process died"); - rt->bpos += nr; /* advance buffer & check */ - if (rt->bpos[-1] == '\t' && rt->bpos[-2] == '-') { - rt->bpos -= 2; /* elide terminator */ - process_rays(rt); + rt->nbr += nr; /* advance & check */ + if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4, + "~\t~\t", 4)) { + rt->nbr -= 4; /* elide terminator */ + queue_raytree(rt); rtfree = rt; /* ready for next ray */ } } @@ -801,7 +938,7 @@ get_rproc(void) /* trace ray contributions (main loop) */ void -tracecontribs(FILE *fin) +trace_contribs(FILE *fin) { char inpbuf[128]; int iblen; @@ -810,15 +947,152 @@ tracecontribs(FILE *fin) while ((iblen = getinp(inpbuf, fin)) > 0) { if (lastray+1 < lastray) { /* counter rollover? */ while (wait_rproc() != NULL) - ; + process_queue(); lastdone = lastray = 0; } rtp = get_rproc(); /* get avail. rtrace process */ - rtp->raynum = ++lastray; /* assign this ray to it */ + rtp->raynum = ++lastray; /* assign ray to it */ writebuf(rtp->pd.w, inpbuf, iblen); - if (!--raysleft) - break; /* explicit EOF */ + if (raysleft && !--raysleft) + break; + process_queue(); /* catch up with results */ } while (wait_rproc() != NULL) /* process outstanding rays */ - ; + process_queue(); + if (raysleft) + error(USER, "unexpected EOF on input"); + lu_done(&ofiletab); /* close output files */ +} + +/* seek on the given output file */ +static int +myseeko(const LUENT *e, void *p) +{ + if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) { + sprintf(errmsg, "seek error on file '%s'", e->key); + error(SYSTEM, errmsg); + } +} + +/* recover output if possible */ +void +recover_output(FILE *fin) +{ + off_t lastout = -1; + int outvsiz; + char *outvfmt; + int i, j; + MODCONT *mp; + int ofl; + char oname[1024]; + LUENT *ment, *oent; + FILE *fp; + off_t nvals; + + switch (outfmt) { + case 'a': + error(USER, "cannot recover ASCII output"); + return; + case 'f': + outvsiz = sizeof(float)*3; + outvfmt = "float"; + break; + case 'd': + outvsiz = sizeof(double)*3; + outvfmt = "double"; + break; + case 'c': + outvsiz = sizeof(COLR); + outvfmt = COLRFMT; + break; + default: + error(INTERNAL, "botched output format"); + return; + } + /* check modifier outputs */ + for (i = 0; i < nmods; i++) { + ment = lu_find(&modconttab,modname[i]); + mp = (MODCONT *)ment->data; + if (mp->outspec == NULL) + error(USER, "cannot recover from stdout"); + for (j = 0; ; j++) { /* check each bin's file */ + ofl = ofname(oname, mp->outspec, mp->modname, j); + if (ofl < 0) + error(USER, "bad output file specification"); + oent = lu_find(&ofiletab, oname); + if (oent->data != NULL) { /* saw this one already */ + if (ofl & OF_BIN) + continue; + break; + } + if (oname[0] == '!') + error(USER, "cannot recover from command"); + /* open output */ + fp = fopen(oname, "rb+"); + if (fp == NULL) + break; /* must be end of modifier */ + nvals = lseek(fileno(fp), 0, SEEK_END); + if (nvals <= 0) { + lastout = 0; /* empty output, quit here */ + fclose(fp); + break; + } + lseek(fileno(fp), 0, SEEK_SET); + if (header) { + int xr, yr; + if (checkheader(fp, outvfmt, NULL) != 1) { + sprintf(errmsg, + "format mismatch for '%s'", + oname); + error(USER, errmsg); + } + if (xres > 0 && yres > 0 && + (!fscnresolu(&xr, &yr, fp) || + xr != xres || + yr != yres)) { + sprintf(errmsg, + "resolution mismatch for '%s'", + oname); + error(USER, errmsg); + } + } + nvals = (nvals - (off_t)ftell(fp)) / outvsiz; + if (lastout < 0 || nvals < lastout) + lastout = nvals; + if (oent->key == NULL) /* new entry */ + oent->key = strcpy((char *) + malloc(strlen(oname)+1), oname); + oent->data = (char *)fp; + if (!(ofl & OF_BIN)) + break; /* no bin separation */ + } + if (!lastout) { /* empty output */ + error(WARNING, "no previous data to recover"); + lu_done(&ofiletab); /* reclose all outputs */ + return; + } + if (j > mp->nbins) { /* preallocate modifier bins */ + mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + + (j-1)*sizeof(DCOLOR)); + if (mp == NULL) + error(SYSTEM, "out of memory in recover_output"); + memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); + mp->nbins = j; + ment->data = (char *)mp; + } + } + if (lastout < 0) { + error(WARNING, "no output files to recover"); + return; + } + /* seek on all files */ + nvals = lastout * outvsiz; + lu_doall(&ofiletab, myseeko, &nvals); + /* discard input */ + for (nvals = 0; nvals < lastout; nvals++) + if (getinp(oname, fin) <= 0) + error(USER, "unexpected EOF on input"); + lastray = lastdone = (unsigned long)lastout; + if (raysleft) + raysleft -= lastray; }