--- ray/src/gen/mkillum.c 1991/07/25 14:50:03 1.8 +++ ray/src/gen/mkillum.c 2004/10/28 00:50:47 2.23 @@ -1,23 +1,23 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: mkillum.c,v 2.23 2004/10/28 00:50:47 greg Exp $"; #endif - /* * Make illum sources for optimizing rendering process */ -#include "mkillum.h" - #include - #include +#include "platform.h" +#include "rtprocess.h" +#include "mkillum.h" +#include "random.h" + /* default parameters */ #define SAMPDENS 48 /* points per projected steradian */ #define NSAMPS 32 /* samples per point */ #define DFLMAT "illum_mat" /* material name */ +#define DFLDAT "illum" /* data file name */ /* selection options */ #define S_NONE 0 /* select none */ #define S_ELEM 1 /* select specified element */ @@ -25,22 +25,24 @@ static char SCCSid[] = "$SunId$ LBL"; #define S_ALL 3 /* select all */ /* rtrace command and defaults */ -char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-di+", - "-ab", "2", "-ad", "256", "-as", "128", "-aa", ".15", }; +char *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-", + "-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", }; int rtargc = 14; /* overriding rtrace options */ -char *myrtopts[] = { "-I-", "-i-", "-ov", "-h-", "-fff", NULL }; +char *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-", + "-fff", "-y", "0", NULL }; -struct rtproc rt; /* our rtrace process */ +struct rtproc rt0; /* head of rtrace process list */ struct illum_args thisillum = { /* our illum and default values */ 0, DFLMAT, - DFLMAT, + DFLDAT, 0, VOIDID, SAMPDENS, NSAMPS, + 0., }; char matcheck[MAXSTR]; /* current material to include or exclude */ @@ -48,6 +50,8 @@ int matselect = S_ALL; /* selection criterion */ FUN ofun[NUMOTYPE] = INIT_OTYPE; /* object types */ +char persistfn[] = "pfXXXXXX"; /* persist file name */ + int gargc; /* global argc */ char **gargv; /* global argv */ #define progname gargv[0] @@ -57,38 +61,74 @@ int doneheader = 0; /* printed header yet? */ int warnings = 1; /* print warnings? */ -extern char *fgetline(), *fgetword(), *sskip(), - *atos(), *iskip(), *fskip(), *strcpy(); -extern FILE *popen(); +int done_rprocs(struct rtproc *rtp); +void init(int np); +void filter(register FILE *infp, char *name); +void xoptions(char *s, char *nm); +void printopts(void); +void printhead(register int ac, register char **av); +void xobject(FILE *fp, char *nm); -main(argc, argv) /* compute illum distributions using rtrace */ -int argc; -char *argv[]; +int +main( /* compute illum distributions using rtrace */ + int argc, + char *argv[] +) { - extern char *getenv(), *getpath(); + int nprocs = 1; char *rtpath; FILE *fp; register int i; /* set global arguments */ gargv = argv; + /* check for -n option */ + if (!strcmp(argv[1], "-n")) { + nprocs = atoi(argv[2]); + if (nprocs <= 0) + error(USER, "illegal number of processes"); + i = 3; + } else + i = 1; /* set up rtrace command */ - for (i = 1; i < argc; i++) { - if (argv[i][0] == '<' && !argv[i][1]) + for ( ; i < argc; i++) { + if (argv[i][0] == '<' && argv[i][1] == '\0') break; rtargv[rtargc++] = argv[i]; if (argv[i][0] == '-' && argv[i][1] == 'w') - warnings = !warnings; + switch (argv[i][2]) { + case '\0': + warnings = !warnings; + break; + case '+': + case 'T': case 't': + case 'Y': case 'y': + case '1': + warnings = 1; + break; + case '-': + case 'F': case 'f': + case 'N': case 'n': + case '0': + warnings = 0; + break; + } } - if ((gargc = i) < 2) - error(USER, "too few arguments"); - rtargc--; + gargc = i; + if (!strcmp(rtargv[--rtargc], "-defaults")) + nprocs = 0; + if (nprocs > 1) { /* add persist file if parallel invocation */ + rtargv[rtargc++] = "-PP"; + rtargv[rtargc++] = mktemp(persistfn); + } + /* add "mandatory" rtrace options */ for (i = 0; myrtopts[i] != NULL; i++) rtargv[rtargc++] = myrtopts[i]; + /* finally, put back final argument */ rtargv[rtargc++] = argv[gargc-1]; rtargv[rtargc] = NULL; - /* just asking for defaults? */ - if (!strcmp(argv[gargc-1], "-defaults")) { + if (!nprocs) { /* just asking for defaults? */ + printopts(); fflush(stdout); rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); if (rtpath == NULL) { eputs(rtargv[0]); @@ -96,11 +136,13 @@ char *argv[]; exit(1); } execv(rtpath, rtargv); - perror(rtpath); + perror(rtpath); /* execv() should not return */ exit(1); } + if (gargc < 2 || argv[gargc-1][0] == '-') + error(USER, "missing octree argument"); /* else initialize and run our calculation */ - init(); + init(nprocs); if (gargc+1 < argc) for (i = gargc+1; i < argc; i++) { if ((fp = fopen(argv[i], "r")) == NULL) { @@ -116,55 +158,101 @@ char *argv[]; quit(0); } +static void +killpersist(void) /* kill persistent rtrace process */ +{ + FILE *fp = fopen(persistfn, "r"); + int pid; -quit(status) /* exit with status */ -int status; + if (fp == NULL) + return; + if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0) + unlink(persistfn); + fclose(fp); +} + +int +done_rprocs(struct rtproc *rtp) { + int st0, st1 = 0; + + if (rtp->next != NULL) { /* close last opened first! */ + st1 = done_rprocs(rtp->next); + free((void *)rtp->next); + rtp->next = NULL; + } + st0 = close_process(&rtp->pd); + if (st0 < 0) + error(WARNING, "unknown return status from rtrace process"); + else if (st0 > 0) + return(st0); + return(st1); +} + +void +quit(int status) /* exit with status */ +{ int rtstat; - rtstat = close_process(rt.pd); + if (rt0.next != NULL) /* terminate persistent rtrace */ + killpersist(); + /* clean up rtrace process(es) */ + rtstat = done_rprocs(&rt0); if (status == 0) - if (rtstat < 0) - error(WARNING, - "unknown return status from rtrace process"); - else - status = rtstat; + status = rtstat; exit(status); } - -init() /* start rtrace and set up buffers */ +void +init(int np) /* start rtrace and set up buffers */ { - extern int o_face(), o_sphere(), o_ring(); + struct rtproc *rtp; + int i; int maxbytes; /* set up object functions */ ofun[OBJ_FACE].funp = o_face; ofun[OBJ_SPHERE].funp = o_sphere; ofun[OBJ_RING].funp = o_ring; /* set up signal handling */ +#ifdef SIGPIPE /* not present on Windows */ signal(SIGPIPE, quit); - /* start rtrace process */ - maxbytes = open_process(rt.pd, rtargv); - if (maxbytes == 0) { - eputs(rtargv[0]); - eputs(": command not found\n"); - exit(1); +#endif + rtp = &rt0; /* start rtrace process(es) */ + for (i = 0; i++ < np; ) { + errno = 0; + maxbytes = open_process(&rtp->pd, rtargv); + if (maxbytes == 0) { + eputs(rtargv[0]); + eputs(": command not found\n"); + exit(1); + } + if (maxbytes < 0) + error(SYSTEM, "cannot start rtrace process"); + if (!i && np > 1) + sleep(2); /* wait for persist file */ + rtp->bsiz = maxbytes/(6*sizeof(float)); + rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--); + rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *)); + if (rtp->buf == NULL || rtp->dest == NULL) + error(SYSTEM, "out of memory in init"); + rtp->nrays = 0; + if (i == np) /* last process? */ + break; + rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc)); + if (rtp->next == NULL) + error(SYSTEM, "out of memory in init"); + rtp = rtp->next; } - if (maxbytes < 0) - error(SYSTEM, "cannot start rtrace process"); - rt.bsiz = maxbytes/(6*sizeof(float)); - rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--); - rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz); - if (rt.buf == NULL || rt.dest == NULL) - error(SYSTEM, "out of memory in init"); - rt.nrays = 0; + rtp->next = NULL; /* set up urand */ - initurand(2048); + initurand(16384); } -eputs(s) /* put string to stderr */ -register char *s; +void +eputs( /* put string to stderr */ + register char *s +) { static int midline = 0; @@ -178,6 +266,7 @@ register char *s; } +void wputs(s) /* print warning if enabled */ char *s; { @@ -186,9 +275,11 @@ char *s; } -filter(infp, name) /* process stream */ -register FILE *infp; -char *name; +void +filter( /* process stream */ + register FILE *infp, + char *name +) { char buf[512]; FILE *pfp; @@ -218,9 +309,11 @@ char *name; } -xoptions(s, nm) /* process options in string s */ -char *s; -char *nm; +void +xoptions( /* process options in string s */ + char *s, + char *nm +) { extern FILE *freopen(); char buf[64]; @@ -237,12 +330,14 @@ char *nm; case ' ': case '\t': case '\n': + case '\r': + case '\f': cp++; continue; case 'm': /* material name */ if (*++cp != '=') break; - if (!*++cp) + if (!*++cp || isspace(*cp)) break; atos(thisillum.matname, MAXSTR, cp); cp = sskip(cp); @@ -254,8 +349,9 @@ char *nm; case 'f': /* data file name */ if (*++cp != '=') break; - if (!*++cp) { + if (!*++cp || isspace(*cp)) { strcpy(thisillum.datafile,thisillum.matname); + thisillum.dfnum = 0; thisillum.flags &= ~IL_DATCLB; continue; } @@ -303,7 +399,7 @@ char *nm; case 'd': /* point sample density */ if (*++cp != '=') break; - if (!isintd(++cp, " \t\n")) + if (!isintd(++cp, " \t\n\r")) break; thisillum.sampdens = atoi(cp); cp = sskip(cp); @@ -311,7 +407,7 @@ char *nm; case 's': /* point super-samples */ if (*++cp != '=') break; - if (!isintd(++cp, " \t\n")) + if (!isintd(++cp, " \t\n\r")) break; thisillum.nsamps = atoi(cp); cp = sskip(cp); @@ -326,10 +422,20 @@ char *nm; break; cp++; continue; + case 'b': /* brightness */ + if (*++cp != '=') + break; + if (!isfltd(++cp, " \t\n\r")) + break; + thisillum.minbrt = atof(cp); + if (thisillum.minbrt < 0.) + thisillum.minbrt = 0.; + cp = sskip(cp); + continue; case 'o': /* output file */ if (*++cp != '=') break; - if (!*++cp) + if (!*++cp || isspace(*cp)) break; atos(buf, sizeof(buf), cp); cp = sskip(cp); @@ -347,7 +453,8 @@ char *nm; return; } opterr: /* skip faulty option */ - cp = sskip(cp); + while (*cp && !isspace(*cp)) + cp++; nerrs++; } /* print header? */ @@ -365,10 +472,33 @@ char *nm; printf("# %s", s+2); } +void +printopts(void) /* print out option default values */ +{ + printf("m=%-15s\t\t# material name\n", thisillum.matname); + printf("f=%-15s\t\t# data file name\n", thisillum.datafile); + if (thisillum.flags & IL_COLAVG) + if (thisillum.flags & IL_COLDST) + printf("c=d\t\t\t\t# color distribution\n"); + else + printf("c=a\t\t\t\t# color average\n"); + else + printf("c=n\t\t\t\t# color none\n"); + if (thisillum.flags & IL_LIGHT) + printf("l+\t\t\t\t# light type on\n"); + else + printf("l-\t\t\t\t# light type off\n"); + printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens); + printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps); + printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt); +} -printhead(ac, av) /* print out header */ -register int ac; -register char **av; + +void +printhead( /* print out header */ + register int ac, + register char **av +) { putchar('#'); while (ac-- > 0) { @@ -379,9 +509,11 @@ register char **av; } -xobject(fp, nm) /* translate an object from fp */ -FILE *fp; -char *nm; +void +xobject( /* translate an object from fp */ + FILE *fp, + char *nm +) { OBJREC thisobj; char str[MAXSTR]; @@ -392,10 +524,10 @@ char *nm; if (fgetword(str, MAXSTR, fp) == NULL) goto readerr; /* is it an alias? */ - if (!strcmp(str, ALIASID)) { + if (!strcmp(str, ALIASKEY)) { if (fgetword(str, MAXSTR, fp) == NULL) goto readerr; - printf("\n%s %s %s", thisillum.altmat, ALIASID, str); + printf("\n%s %s %s", thisillum.altmat, ALIASKEY, str); if (fgetword(str, MAXSTR, fp) == NULL) goto readerr; printf("\t%s\n", str); @@ -432,7 +564,7 @@ char *nm; checkhead(); /* process object */ if (doit) - (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm); + (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm); else printobj(thisillum.altmat, &thisobj); /* free arguments */