--- ray/src/util/rtcontrib.c 2005/05/26 15:14:42 1.3 +++ ray/src/util/rtcontrib.c 2006/02/05 22:22:21 1.40 @@ -1,13 +1,13 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.3 2005/05/26 15:14:42 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.40 2006/02/05 22:22:21 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,16 +16,18 @@ static const char RCSid[] = "$Id: rtcontrib.c,v 1.3 20 #include "lookup.h" #include "calcomp.h" +#ifndef MAXMODLIST #define MAXMODLIST 1024 /* maximum modifiers we'll track */ +#endif int treebufsiz = BUFSIZ; /* current tree buffer size */ typedef double DCOLOR[3]; /* double-precision color */ /* - * The modcont structure is used to accumulate ray contributions + * The MODCONT structure is used to accumulate ray contributions * for a particular modifier, which may be subdivided into bins - * if binv is non-NULL. If outspec contains a %s in it, this will + * if binv is non-zero. If outspec contains a %s in it, this will * be replaced with the modifier name. If outspec contains a %d in it, * this will be used to create one output file per bin, otherwise all bins * will be written to the same file, in order. If the global outfmt @@ -44,13 +46,43 @@ static void mcfree(void *p) { epfree((*(MODCONT *)p).b LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ -/* close output stream */ -static void closefile(void *p) { fclose((FILE *)p); } +#define CNT_UNKNOWN 0 /* unknown record length */ +#define CNT_PIPE (-1) /* output to a pipe */ +/* + * The STREAMOUT structure holds an open FILE pointer and a count of + * the number of RGB triplets per record, or CNT_UNKNOWN (0) if + * unknown or CNT_PIPE (-1) if writing to a command. + */ +typedef struct { + FILE *ofp; /* output file pointer */ + int reclen; /* triplets/record */ +} STREAMOUT; -LUTAB ofiletab = LU_SINIT(free,closefile); /* output file table */ +/* close output stream and free record */ +static void +closestream(void *p) +{ + STREAMOUT *sop = (STREAMOUT *)p; + int status; + if (sop->reclen == CNT_PIPE) + status = pclose(sop->ofp); + else + status = fclose(sop->ofp); + if (status) + error(SYSTEM, "error closing output stream"); + free(p); +} -FILE *getofile(const char *ospec, const char *mname, int bn); +LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ +#define OF_MODIFIER 01 +#define OF_BIN 02 + +STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen); +int ofname(char *oname, const char *ospec, const char *mname, int bn); +void printheader(FILE *fout, const char *info); +void printresolu(FILE *fout); + /* * The rcont structure is used to manage i/o with a particular * rtrace child process. Input is passed unchanged from stdin, @@ -64,23 +96,26 @@ struct rtproc { int bsiz; /* ray tree buffer length */ char *buf; /* ray tree buffer */ int nbr; /* number of bytes from rtrace */ -}; /* rtrace process */ +}; /* rtrace process buffer */ /* rtrace command and defaults */ -char *rtargv[256] = { "rtrace", "-dt", "0", "-dj", ".5", "-dr", "3", +char *rtargv[256+2*MAXMODLIST] = { "rtrace", + "-dj", ".5", "-dr", "3", "-ab", "1", "-ad", "128", }; -int rtargc = 11; + +int rtargc = 9; /* overriding rtrace options */ -char *myrtopts[] = { "-o~~TmWdp", "-h-", - "-x", "1", "-y", "0", - "-as", "0", "-aa", "0", NULL }; +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 */ char persistfn[] = "pfXXXXXX"; /* persist file name */ -char fmt[8]; /* rtrace i/o format */ int gargc; /* global argc */ char **gargv; /* global argv */ @@ -92,10 +127,11 @@ 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 */ -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 */ @@ -106,28 +142,47 @@ 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); +#define queue_length() (lastray - lastdone) -int done_rprocs(struct rtproc *rtp); +MODCONT *growmodifier(MODCONT *mp, int nb); +MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt); +void addmodfile(char *fname, char *outf, char *binv, int bincnt); + 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 queue_raytree(struct rtproc *rtp); void process_queue(void); -void putcontrib(const DCOLOR cnt, FILE *fout); +void put_contrib(const DCOLOR cnt, FILE *fout); void add_contrib(const char *modn); void done_contrib(void); +/* return number of open rtrace processes */ +static int +nrtprocs(void) +{ + int nrtp = 0; + struct rtproc *rtp; + + for (rtp = &rt0; rtp != NULL; rtp = rtp->next) + nrtp += rtp->pd.running; + return(nrtp); +} + /* set input/output format */ 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: @@ -157,15 +212,27 @@ fmterr: int main(int argc, char *argv[]) { + int contrib = 0; int nprocs = 1; + int recover = 0; char *curout = NULL; char *binval = NULL; + int bincnt = 0; + char fmt[8]; int i, j; + /* need at least one argument */ + if (argc < 2) { + fprintf(stderr, +"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); + } /* global program name */ gargv = argv; - /* set up calcomp mode */ + /* 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-1; i++) { /* expand arguments */ @@ -179,11 +246,32 @@ main(int argc, char *argv[]) if (argv[i][0] == '-') switch (argv[i][1]) { case 'n': /* number of processes */ - if (argv[i][2] || i >= argc-1) break; + 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': @@ -201,40 +289,67 @@ 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]) { - if (i >= argc-1) break; - fcompile(argv[++i]); + char *fpath; + if (i >= argc-2) 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; } + if (argv[i][2] == 'o') { + force_open++; + continue; + } setformat(argv[i]+2); continue; case 'e': /* expression */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; scompile(argv[++i], NULL, 0); continue; case 'o': /* output file spec. */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; curout = argv[++i]; continue; case 'x': /* horiz. output resolution */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; xres = atoi(argv[++i]); continue; case 'y': /* vert. output resolution */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; yres = atoi(argv[++i]); continue; - case 'b': /* bin expression */ - if (argv[i][2] || i >= argc-1) break; + case 'b': /* bin expression/count */ + if (i >= argc-2) break; + if (argv[i][2] == 'n') { + bincnt = atoi(argv[++i]); + continue; + } + if (argv[i][2]) break; binval = argv[++i]; continue; case 'm': /* modifier name */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; rtargv[rtargc++] = "-ti"; rtargv[rtargc++] = argv[++i]; - addmodifier(argv[i], curout, binval); + addmodifier(argv[i], curout, binval, bincnt); continue; + case 'M': /* modifier file */ + if (argv[i][2] || i >= argc-2) break; + rtargv[rtargc++] = "-tI"; + 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 */ } @@ -243,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); @@ -258,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); @@ -270,7 +387,8 @@ main(int argc, char *argv[]) execv(rtpath, rtargv); perror(rtpath); /* execv() should not return */ exit(1); - } else if (nprocs > 1) { /* add persist file if parallel */ + } + if (nprocs > 1) { /* add persist file if parallel */ rtargv[rtargc++] = "-PP"; rtargv[rtargc++] = mktemp(persistfn); } @@ -282,18 +400,23 @@ main(int argc, char *argv[]) error(USER, "missing octree argument"); rtargv[rtargc++] = octree = argv[i]; rtargv[rtargc] = NULL; - /* start rtrace & compute contributions */ + /* start rtrace */ init(nprocs); - tracecontribs(stdin); + if (recover) /* perform recovery if requested */ + recover_output(stdin); + trace_contribs(stdin); /* compute contributions */ quit(0); } +#ifndef SIGALRM +#define SIGALRM SIGTERM +#endif /* kill persistent rtrace process */ static void killpersist(void) { FILE *fp = fopen(persistfn, "r"); - int pid; + RT_PID pid; if (fp == NULL) return; @@ -336,7 +459,7 @@ quit(int status) exit(status); /* flushes all output streams */ } -/* start rtrace and initialize buffers */ +/* start rtrace processes and initialize */ void init(int np) { @@ -389,7 +512,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 @@ -397,12 +520,27 @@ init(int np) waitflush = xres; } +/* grow modifier to accommodate more bins */ +MODCONT * +growmodifier(MODCONT *mp, int nb) +{ + if (nb <= mp->nbins) + return mp; + mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1)); + if (mp == NULL) + error(SYSTEM, "out of memory in growmodifier"); + memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins)); + mp->nbins = nb; + return mp; +} + /* add modifier to our list to track */ MODCONT * -addmodifier(char *modn, char *outf, char *binv) +addmodifier(char *modn, char *outf, char *binv, int bincnt) { LUENT *lep = lu_find(&modconttab, modn); MODCONT *mp; + int i; if (lep->data != NULL) { sprintf(errmsg, "duplicate modifier '%s'", modn); @@ -415,7 +553,6 @@ addmodifier(char *modn, char *outf, char *binv) mp = (MODCONT *)malloc(sizeof(MODCONT)); if (mp == NULL) error(SYSTEM, "out of memory in addmodifier"); - lep->data = (char *)mp; mp->outspec = outf; /* XXX assumes static string */ mp->modname = modn; /* XXX assumes static string */ if (binv != NULL) @@ -424,9 +561,32 @@ addmodifier(char *modn, char *outf, char *binv) mp->binv = eparse("0"); mp->nbins = 1; setcolor(mp->cbin[0], 0., 0., 0.); + if (mp->binv->type == NUM) /* assume one bin if constant */ + bincnt = 1; + else if (bincnt > 1) + mp = growmodifier(mp, bincnt); + lep->data = (char *)mp; + /* allocate output streams */ + for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; ) + getostream(mp->outspec, mp->modname, i, 1); return mp; } +/* add modifiers from a file list */ +void +addmodfile(char *fname, char *outf, char *binv, int bincnt) +{ + char *mname[MAXMODLIST]; + int i; + /* find the file & store strings */ + if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { + sprintf(errmsg, "cannot find modifier file '%s'", fname); + error(SYSTEM, errmsg); + } + for (i = 0; mname[i]; i++) /* add each one */ + addmodifier(mname[i], outf, binv, bincnt); +} + /* put string to stderr */ void eputs(char *s) @@ -442,9 +602,60 @@ 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"); @@ -456,6 +667,8 @@ printheader(FILE *fout) 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); @@ -471,6 +684,12 @@ printheader(FILE *fout) break; } fputc('\n', fout); +} + +/* write resolution string to given output stream */ +void +printresolu(FILE *fout) +{ if (xres > 0) { if (yres > 0) /* resolution string */ fprtresolu(xres, yres, fout); @@ -478,106 +697,135 @@ printheader(FILE *fout) } } -/* Get output file pointer (open and write header if new) */ -FILE * -getofile(const char *ospec, const char *mname, int bn) +/* Get output stream pointer (open and write header if new and noopen==0) */ +STREAMOUT * +getostream(const char *ospec, const char *mname, int bn, int noopen) { - const char *mnp = NULL; - const char *bnp = NULL; - const char *cp; - char ofname[1024]; - LUENT *lep; + static STREAMOUT stdos; + int ofl; + char oname[1024]; + LUENT *lep; + STREAMOUT *sop; if (ospec == NULL) { /* use stdout? */ - if (!using_stdout && header) - printheader(stdout); - 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 (!noopen && !using_stdout) { + stdos.reclen = 0; + if (outfmt != 'a') + SET_FILE_BINARY(stdout); + if (header) + printheader(stdout, NULL); + printresolu(stdout); + using_stdout = 1; } - 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 */ + stdos.ofp = stdout; + stdos.reclen += noopen; + return &stdos; + } + 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); - if (lep->data == NULL) { /* open output file */ - FILE *fp = fopen(ofname, "w"); - int i; - if (fp == NULL) { - sprintf(errmsg, "cannot open '%s' for writing", ofname); + lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); + sop = (STREAMOUT *)lep->data; + if (sop == NULL) { /* allocate stream */ + sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); + if (sop == NULL) + error(SYSTEM, "out of memory in getostream"); + 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 */ + long i; + if (oname[0] == '!') /* output to command */ + sop->ofp = popen(oname+1, "w"); + 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); error(SYSTEM, errmsg); } - if (header) - printheader(fp); + if (outfmt != 'a') + SET_FILE_BINARY(sop->ofp); + if (header) { + char info[512]; + char *cp = info; + if (ofl & OF_MODIFIER || sop->reclen == 1) { + 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(sop->ofp, info); + } + 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; - putcontrib(nocontrib, fp); + put_contrib(nocontrib, sop->ofp); if (outfmt == 'a') - putc('\n', fp); + putc('\n', sop->ofp); } if (xres > 0) - fflush(fp); - lep->data = (char *)fp; + fflush(sop->ofp); } - return (FILE *)lep->data; /* return open file pointer */ -badspec: - sprintf(errmsg, "bad output format '%s'", ospec); - error(USER, errmsg); - return NULL; /* pro forma return */ + if (sop->reclen != CNT_PIPE) /* add to length if noopen */ + sop->reclen += noopen; + return sop; /* return open stream */ } /* read input ray into buffer */ int getinp(char *buf, FILE *fp) { + double dv[3]; + float fv[3]; + 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 -1; + if (i >= 3) + dv[i-3] = atof(cp); + if ((cp = fskip(cp)) == NULL || *cp) + 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 */ @@ -607,15 +855,8 @@ add_contrib(const char *modn) 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(DCOLOR)); - if (mp == NULL) - error(SYSTEM, "out of memory in add_contrib"); - memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins)); - mp->nbins = bn+1; - le->data = (char *)mp; - } + else if (bn >= mp->nbins) /* new bin */ + le->data = (char *)(mp = growmodifier(mp, bn+1)); addcolor(mp->cbin[bn], rparams); } @@ -623,13 +864,13 @@ add_contrib(const char *modn) static int puteol(const LUENT *e, void *p) { - FILE *fp = (FILE *)e->data; + STREAMOUT *sop = (STREAMOUT *)e->data; if (outfmt == 'a') - putc('\n', fp); + putc('\n', sop->ofp); if (!waitflush) - fflush(fp); - if (ferror(fp)) { + fflush(sop->ofp); + if (ferror(sop->ofp)) { sprintf(errmsg, "write error on file '%s'", e->key); error(SYSTEM, errmsg); } @@ -638,7 +879,7 @@ puteol(const LUENT *e, void *p) /* put out ray contribution to file */ void -putcontrib(const DCOLOR cnt, FILE *fout) +put_contrib(const DCOLOR cnt, FILE *fout) { float fv[3]; COLR cv; @@ -669,14 +910,22 @@ putcontrib(const DCOLOR cnt, FILE *fout) void done_contrib(void) { - int i, j; - MODCONT *mp; + int i, j; + MODCONT *mp; + STREAMOUT *sop; /* 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++) - putcontrib(mp->cbin[j], - getofile(mp->outspec, mp->modname, j)); + sop = getostream(mp->outspec, mp->modname, 0,0); + put_contrib(mp->cbin[0], sop->ofp); + if (mp->nbins > 3 && /* minor optimization */ + sop == getostream(mp->outspec, mp->modname, 1,0)) + for (j = 1; j < mp->nbins; j++) + put_contrib(mp->cbin[j], sop->ofp); + else + for (j = 1; j < mp->nbins; j++) + put_contrib(mp->cbin[j], + getostream(mp->outspec,mp->modname,j,0)->ofp); /* clear for next ray tree */ memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); } @@ -736,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'; @@ -750,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 */ } @@ -798,7 +1050,7 @@ wait_rproc(void) if (rt->bsiz + BUFSIZ <= treebufsiz) rt->bsiz = treebufsiz; else - rt->bsiz = treebufsiz += BUFSIZ; + treebufsiz = rt->bsiz += BUFSIZ; rt->buf = (char *)realloc(rt->buf, rt->bsiz); } if (rt->buf == NULL) @@ -832,27 +1084,193 @@ get_rproc(void) /* trace ray contributions (main loop) */ void -tracecontribs(FILE *fin) +trace_contribs(FILE *fin) { char inpbuf[128]; int iblen; struct rtproc *rtp; /* loop over input */ - while ((iblen = getinp(inpbuf, fin)) > 0) { - if (lastray+1 < lastray) { /* counter rollover? */ + 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) - 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(); - if (raysleft > 0) + 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) +{ + STREAMOUT *sop = (STREAMOUT *)e->data; + off_t nbytes = *(off_t *)p; + + if (sop->reclen > 1) + nbytes = nbytes * sop->reclen; + if (fseeko(sop->ofp, nbytes, 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, recsiz; + char *outvfmt; + int i, j; + MODCONT *mp; + int ofl; + char oname[1024]; + LUENT *ment, *oent; + STREAMOUT sout; + off_t nvals; + int xr, yr; + + 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"); + if (mp->outspec[0] == '!') + error(USER, "cannot recover from command"); + 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) { + sout = *(STREAMOUT *)oent->data; + } else { + sout.reclen = CNT_UNKNOWN; + sout.ofp = NULL; + } + if (sout.ofp != NULL) { /* already open? */ + if (ofl & OF_BIN) + continue; + break; + } + /* open output */ + sout.ofp = fopen(oname, "rb+"); + if (sout.ofp == NULL) { + if (j) + break; /* assume end of modifier */ + sprintf(errmsg, "missing recover file '%s'", + oname); + error(USER, errmsg); + } + nvals = lseek(fileno(sout.ofp), 0, SEEK_END); + if (nvals <= 0) { + lastout = 0; /* empty output, quit here */ + fclose(sout.ofp); + break; + } + if (sout.reclen == CNT_UNKNOWN) { + if (!(ofl & OF_BIN)) { + sprintf(errmsg, + "need -bn to recover file '%s'", + oname); + error(USER, errmsg); + } + recsiz = outvsiz; + } else + recsiz = outvsiz * sout.reclen; + + lseek(fileno(sout.ofp), 0, SEEK_SET); + if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { + sprintf(errmsg, "format mismatch for '%s'", + oname); + error(USER, errmsg); + } + if ((xres > 0) & (yres > 0) && + (!fscnresolu(&xr, &yr, sout.ofp) || + xr != xres || + yr != yres)) { + sprintf(errmsg, "resolution mismatch for '%s'", + oname); + error(USER, errmsg); + } + nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; + if ((lastout < 0) | (nvals < lastout)) + lastout = nvals; + if (oent->key == NULL) /* new entry */ + oent->key = strcpy((char *) + malloc(strlen(oname)+1), oname); + if (oent->data == NULL) + oent->data = (char *)malloc(sizeof(STREAMOUT)); + *(STREAMOUT *)oent->data = sout; + 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) /* reallocate modifier bins */ + ment->data = (char *)(mp = growmodifier(mp, j)); + } + if (lastout < 0) { + error(WARNING, "no output files to recover"); + return; + } + if (raysleft && lastout >= raysleft) { + error(WARNING, "output appears to be complete"); + /* XXX should read & discard input? */ + quit(0); + } + /* seek on all files */ + nvals = lastout * outvsiz; + lu_doall(&ofiletab, myseeko, &nvals); + /* skip repeated 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; }