--- ray/src/util/rad.c 2004/10/23 18:55:53 2.80 +++ ray/src/util/rad.c 2015/05/27 12:50:46 2.113 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rad.c,v 2.80 2004/10/23 18:55:53 schorsch Exp $"; +static const char RCSid[] = "$Id: rad.c,v 2.113 2015/05/27 12:50:46 greg Exp $"; #endif /* * Executive program for oconv, rpict and pfilt @@ -24,6 +24,7 @@ static const char RCSid[] = "$Id: rad.c,v 2.80 2004/10 #define RENAMECMD "mv" #include #include + #include #endif /* variables (alphabetical by name) */ @@ -35,26 +36,31 @@ static const char RCSid[] = "$Id: rad.c,v 2.80 2004/10 #define INDIRECT 5 /* indirection in lighting */ #define MATERIAL 6 /* material files */ #define MKILLUM 7 /* mkillum options */ -#define OBJECT 8 /* object files */ -#define OCONV 9 /* oconv options */ -#define OCTREE 10 /* octree file name */ -#define OPTFILE 11 /* rendering options file */ -#define PENUMBRAS 12 /* shadow penumbras are desired */ -#define PFILT 13 /* pfilt options */ -#define PICTURE 14 /* picture file root name */ -#define QUALITY 15 /* desired rendering quality */ -#define RAWFILE 16 /* raw picture file root name */ -#define RENDER 17 /* rendering options */ -#define REPORT 18 /* report frequency and errfile */ -#define RESOLUTION 19 /* maximum picture resolution */ -#define SCENE 20 /* scene files */ -#define UP 21 /* view up (X, Y or Z) */ -#define VARIABILITY 22 /* level of light variability */ -#define VIEWS 23 /* view(s) for picture(s) */ -#define ZFILE 24 /* distance file root name */ -#define ZONE 25 /* simulation zone */ +#define MKPMAP 8 /* mkpmap options */ +#define OBJECT 9 /* object files */ +#define OCONV 10 /* oconv options */ +#define OCTREE 11 /* octree file name */ +#define OPTFILE 12 /* rendering options file */ +#define PCMAP 13 /* caustic photon map */ +#define PENUMBRAS 14 /* shadow penumbras are desired */ +#define PFILT 15 /* pfilt options */ +#define PGMAP 16 /* global photon map */ +#define PICTURE 17 /* picture file root name */ +#define QUALITY 18 /* desired rendering quality */ +#define RAWFILE 19 /* raw picture file root name */ +#define RENDER 20 /* rendering options */ +#define REPORT 21 /* report frequency and errfile */ +#define RESOLUTION 22 /* maximum picture resolution */ +#define RPICT 23 /* rpict parameters */ +#define RVU 24 /* rvu parameters */ +#define SCENE 25 /* scene files */ +#define UP 26 /* view up (X, Y or Z) */ +#define VARIABILITY 27 /* level of light variability */ +#define VIEWS 28 /* view(s) for picture(s) */ +#define ZFILE 29 /* distance file root name */ +#define ZONE 30 /* simulation zone */ /* total number of variables */ -int NVARS = 26; +int NVARS = 31; VARIABLE vv[] = { /* variable-value pairs */ {"AMBFILE", 3, 0, NULL, onevalue}, @@ -65,18 +71,23 @@ VARIABLE vv[] = { /* variable-value pairs */ {"INDIRECT", 3, 0, NULL, intvalue}, {"materials", 3, 0, NULL, catvalues}, {"mkillum", 3, 0, NULL, catvalues}, + {"mkpmap", 3, 0, NULL, catvalues}, {"objects", 3, 0, NULL, catvalues}, {"oconv", 3, 0, NULL, catvalues}, {"OCTREE", 3, 0, NULL, onevalue}, {"OPTFILE", 3, 0, NULL, onevalue}, + {"PCMAP", 2, 0, NULL, onevalue}, {"PENUMBRAS", 3, 0, NULL, boolvalue}, {"pfilt", 2, 0, NULL, catvalues}, + {"PGMAP", 2, 0, NULL, onevalue}, {"PICTURE", 3, 0, NULL, onevalue}, {"QUALITY", 3, 0, NULL, qualvalue}, {"RAWFILE", 3, 0, NULL, onevalue}, {"render", 3, 0, NULL, catvalues}, {"REPORT", 3, 0, NULL, onevalue}, {"RESOLUTION", 3, 0, NULL, onevalue}, + {"rpict", 3, 0, NULL, catvalues}, + {"rvu", 3, 0, NULL, catvalues}, {"scene", 3, 0, NULL, catvalues}, {"UP", 2, 0, NULL, onevalue}, {"VARIABILITY", 3, 0, NULL, qualvalue}, @@ -103,6 +114,11 @@ time_t oct0date; /* date of pre-mkillum octree */ char *oct1name; /* name of post-mkillum octree */ time_t oct1date; /* date of post-mkillum octree (>= matdate) */ +char *pgmapname; /* name of global photon map */ +time_t pgmapdate; /* date of global photon map (>= oct1date) */ +char *pcmapname; /* name of caustic photon map */ +time_t pcmapdate; /* date of caustic photon map (>= oct1date) */ + int nowarn = 0; /* no warnings */ int explicate = 0; /* explicate variables */ int silent = 0; /* do work silently */ @@ -112,6 +128,17 @@ int sayview = 0; /* print view out */ char *rvdevice = NULL; /* rvu output device */ char *viewselect = NULL; /* specific view only */ +#define DEF_RPICT_PATH "rpict" /* default rpict path */ + + /* command paths */ +char c_oconv[256] = "oconv"; +char c_mkillum[256] = "mkillum"; +char c_mkpmap[256] = "mkpmap"; +char c_rvu[256] = "rvu"; +char c_rpict[256] = DEF_RPICT_PATH; +char c_rpiece[] = "rpiece"; +char c_pfilt[256] = "pfilt"; + int overture = 0; /* overture calculation needed */ int children_running = 0; /* set negative in children */ @@ -130,9 +157,11 @@ static void checkfiles(void); static void getoctcube(double org[3], double *sizp); static void setdefaults(void); static void oconv(void); +static void mkpmap(void); static char * addarg(char *op, char *arg); static void oconvopts(char *oo); static void mkillumopts(char *mo); +static void mkpmapopts(char *mo); static void checkambfile(void); static double ambval(void); static void renderopts(char *op, char *po); @@ -151,7 +180,7 @@ static int touch(char *fn); static int runcom(char *cs); static int rmfile(char *fn); static int mvfile(char *fold, char *fnew); -static int next_process(void); +static int next_process(int reserve); static void wait_process(int all); static void finish_process(void); static void badvalue(int vc); @@ -207,9 +236,6 @@ main( if (i >= argc) goto userr; rifname = argv[i]; - /* check command-line options */ - if ((nprocs > 1) & (viewselect != NULL)) - nprocs = 1; /* assign Radiance root file name */ rootname(radname, rifname); /* load variable values */ @@ -232,6 +258,8 @@ main( printvars(stdout); /* build octree (and run mkillum) */ oconv(); + /* run mkpmap if indicated */ + mkpmap(); /* check date on ambient file */ checkambfile(); /* run simulation */ @@ -253,8 +281,8 @@ userr: static void rootname( /* remove tail from end of fn */ - register char *rn, - register char *fn + char *rn, + char *fn ) { char *tp, *dp; @@ -271,7 +299,7 @@ rootname( /* remove tail from end of fn */ static time_t checklast( /* check files and find most recent */ - register char *fnames + char *fnames ) { char thisfile[PATH_MAX]; @@ -301,8 +329,8 @@ newfname( /* create modified file name */ int pred ) { - register char *cp; - register int n; + char *cp; + int n; int suffix; n = 0; cp = orig; suffix = -1; /* suffix position, length */ @@ -324,6 +352,7 @@ newfname( /* create modified file name */ static void checkfiles(void) /* check for existence and modified times */ { + char fntemp[256]; time_t objdate; if (!vdef(OCTREE)) { @@ -353,6 +382,29 @@ checkfiles(void) /* check for existence and modified vnam(OCTREE), vnam(SCENE), vnam(ILLUM)); quit(1); } + if (vdef(PGMAP)) { + if (!*sskip2(vval(PGMAP),1)) { + fprintf(stderr, "%s: '%s' missing # photons argument\n", + progname, vnam(PGMAP)); + quit(1); + } + atos(fntemp, sizeof(fntemp), vval(PGMAP)); + pgmapname = savqstr(fntemp); + pgmapdate = fdate(pgmapname); + } + if (vdef(PCMAP)) { + if (!*sskip2(vval(PCMAP),1)) { + fprintf(stderr, "%s: '%s' missing # photons argument\n", + progname, vnam(PCMAP)); + quit(1); + } + atos(fntemp, sizeof(fntemp), vval(PCMAP)); + pcmapname = savqstr(fntemp); + pcmapdate = fdate(pcmapname); + if (pgmapname == NULL && !nowarn) + fprintf(stderr, "%s: warning - '%s' assigned without '%s'\n", + progname, vnam(PCMAP), vnam(PGMAP)); + } matdate = checklast(vval(MATERIAL)); } @@ -367,7 +419,7 @@ getoctcube( /* get octree bounding cube */ double min[3], max[3]; char buf[1024]; FILE *fp; - register int i; + int i; if (osiz <= FTINY) { if (!nprocs && fdate(oct1name) < @@ -480,11 +532,11 @@ oconv(void) /* run oconv and mkillum if necessary * touch(vval(OCTREE)); else { /* build command */ if (vdef(MATERIAL)) - sprintf(combuf, "oconv%s %s %s > %s", ocopts, - vval(MATERIAL), vval(SCENE), - vval(OCTREE)); + sprintf(combuf, "%s%s %s %s > %s", c_oconv, + ocopts, vval(MATERIAL), + vval(SCENE), vval(OCTREE)); else - sprintf(combuf, "oconv%s %s > %s", ocopts, + sprintf(combuf, "%s%s %s > %s", c_oconv, ocopts, vval(SCENE), vval(OCTREE)); if (runcom(combuf)) { /* run it */ @@ -510,14 +562,16 @@ oconv(void) /* run oconv and mkillum if necessary * touch(oct0name); else { /* build command */ if (octreedate) - sprintf(combuf, "oconv%s -i %s %s > %s", ocopts, - vval(OCTREE), vval(ILLUM), oct0name); + sprintf(combuf, "%s%s -i %s %s > %s", c_oconv, + ocopts, vval(OCTREE), + vval(ILLUM), oct0name); else if (vdef(MATERIAL)) - sprintf(combuf, "oconv%s %s %s > %s", ocopts, - vval(MATERIAL), vval(ILLUM), oct0name); - else - sprintf(combuf, "oconv%s %s > %s", ocopts, + sprintf(combuf, "%s%s %s %s > %s", c_oconv, + ocopts, vval(MATERIAL), vval(ILLUM), oct0name); + else + sprintf(combuf, "%s%s %s > %s", c_oconv, + ocopts, vval(ILLUM), oct0name); if (runcom(combuf)) { /* run it */ fprintf(stderr, "%s: error generating octree\n\t%s removed\n", @@ -537,23 +591,24 @@ oconv(void) /* run oconv and mkillum if necessary * else { mkillumopts(mkopts); /* build mkillum command */ mktemp(illumtmp); - sprintf(combuf, "mkillum%s %s \"<\" %s > %s", mkopts, + sprintf(combuf, "%s%s %s \"<\" %s > %s", c_mkillum, mkopts, oct0name, vval(ILLUM), illumtmp); if (runcom(combuf)) { /* run it */ - fprintf(stderr, "%s: error running mkillum\n", - progname); + fprintf(stderr, "%s: error running %s\n", + progname, c_mkillum); unlink(illumtmp); quit(1); } + rmfile(oct0name); /* make octree1 (frozen) */ if (octreedate) - sprintf(combuf, "oconv%s -f -i %s %s > %s", ocopts, - vval(OCTREE), illumtmp, oct1name); + sprintf(combuf, "%s%s -f -i %s %s > %s", c_oconv, + ocopts, vval(OCTREE), illumtmp, oct1name); else if (vdef(MATERIAL)) - sprintf(combuf, "oconv%s -f %s %s > %s", ocopts, - vval(MATERIAL), illumtmp, oct1name); + sprintf(combuf, "%s%s -f %s %s > %s", c_oconv, + ocopts, vval(MATERIAL), illumtmp, oct1name); else - sprintf(combuf, "oconv%s -f %s > %s", ocopts, + sprintf(combuf, "%s%s -f %s > %s", c_oconv, ocopts, illumtmp, oct1name); if (runcom(combuf)) { /* run it */ fprintf(stderr, @@ -571,12 +626,79 @@ oconv(void) /* run oconv and mkillum if necessary * } +static void +mkpmap(void) /* run mkpmap if indicated */ +{ + char combuf[2048], *cp; + time_t tnow; + /* nothing to do? */ + if ((pgmapname == NULL) | (pgmapdate >= oct1date) && + (pcmapname == NULL) | (pcmapdate >= oct1date)) + return; + /* just update existing file dates? */ + if (touchonly && (pgmapname == NULL) | (pgmapdate > 0) && + (pcmapname == NULL) | (pcmapdate > 0)) { + if (pgmapname != NULL) + touch(pgmapname); + if (pcmapname != NULL) + touch(pcmapname); + } else { /* else need to (re)run pkpmap */ + strcpy(combuf, c_mkpmap); + for (cp = combuf; *cp; cp++) + ; + mkpmapopts(cp); + if (vdef(REPORT)) { + char errfile[256]; + int n; + double minutes; + n = sscanf(vval(REPORT), "%lf %s", &minutes, errfile); + if (n == 2) + sprintf(cp, " -t %d -e %s", (int)(minutes*60), errfile); + else if (n == 1) + sprintf(cp, " -t %d", (int)(minutes*60)); + else + badvalue(REPORT); + } + if (pgmapname != NULL && pgmapdate < oct1date) { + cp = addarg(cp, "-apg"); + addarg(cp, vval(PGMAP)); + cp = sskip(sskip(cp)); /* remove any bandwidth */ + *cp = '\0'; + } + if (pcmapname != NULL && pcmapdate < oct1date) { + cp = addarg(cp, "-apc"); + addarg(cp, vval(PCMAP)); + cp = sskip(sskip(cp)); /* remove any bandwidth */ + *cp = '\0'; + } + cp = addarg(cp, oct1name); + if (runcom(combuf)) { + fprintf(stderr, "%s: error running %s\n", + progname, c_mkpmap); + if (pgmapname != NULL) + unlink(pgmapname); + if (pcmapname != NULL) + unlink(pcmapname); + quit(1); + } + } + tnow = time((time_t *)NULL); + if (pgmapname != NULL) + pgmapdate = tnow; + if (pcmapname != NULL) + pcmapdate = tnow; + oct1date = tnow; /* trigger ambient file removal if needed */ +} + + static char * -addarg( /* add argument and advance pointer */ -register char *op, -register char *arg +addarg( /* append argument and advance pointer */ +char *op, +char *arg ) { + while (*op) + op++; *op = ' '; while ( (*++op = *arg++) ) ; @@ -586,14 +708,19 @@ register char *arg static void oconvopts( /* get oconv options */ - register char *oo + char *oo ) { /* BEWARE: This may be called via setdefaults(), so no assumptions */ *oo = '\0'; - if (vdef(OCONV)) - addarg(oo, vval(OCONV)); + if (!vdef(OCONV)) + return; + if (vval(OCONV)[0] != '-') { + atos(c_oconv, sizeof(c_oconv), vval(OCONV)); + oo = addarg(oo, sskip2(vval(OCONV), 1)); + } else + oo = addarg(oo, vval(OCONV)); } @@ -604,18 +731,39 @@ mkillumopts( /* get mkillum options */ { /* BEWARE: This may be called via setdefaults(), so no assumptions */ - if (nprocs > 1) { + if (nprocs > 1) sprintf(mo, " -n %d", nprocs); - while (*mo) - mo++; - } else + else *mo = '\0'; - if (vdef(MKILLUM)) - addarg(mo, vval(MKILLUM)); + if (!vdef(MKILLUM)) + return; + if (vval(MKILLUM)[0] != '-') { + atos(c_mkillum, sizeof(c_mkillum), vval(MKILLUM)); + mo = addarg(mo, sskip2(vval(MKILLUM), 1)); + } else + mo = addarg(mo, vval(MKILLUM)); } static void +mkpmapopts( /* get mkpmap options */ + char *mo +) +{ + /* BEWARE: This may be called via setdefaults(), so no assumptions */ + + *mo = '\0'; + if (!vdef(MKPMAP)) + return; + if (vval(MKPMAP)[0] != '-') { + atos(c_mkpmap, sizeof(c_mkpmap), vval(MKPMAP)); + mo = addarg(mo, sskip2(vval(MKPMAP), 1)); + } else + mo = addarg(mo, vval(MKPMAP)); +} + + +static void checkambfile(void) /* check date on ambient file */ { time_t afdate; @@ -656,6 +804,8 @@ renderopts( /* set rendering options */ char *po ) { + char pmapf[256], *bw; + switch(vscale(QUALITY)) { case LOW: lowqopts(op, po); @@ -667,12 +817,41 @@ renderopts( /* set rendering options */ hiqopts(op, po); break; } + if (vdef(PGMAP)) { + bw = sskip2(vval(PGMAP), 2); + atos(pmapf, sizeof(pmapf), vval(PGMAP)); + op = addarg(addarg(op, "-ap"), pmapf); + if (atoi(bw) > 0) op = addarg(op, bw); + } + if (vdef(PCMAP)) { + bw = sskip2(vval(PCMAP), 2); + atos(pmapf, sizeof(pmapf), vval(PCMAP)); + op = addarg(addarg(op, "-ap"), pmapf); + if (atoi(bw) > 0) op = addarg(op, bw); + } + if (vdef(RENDER)) + op = addarg(op, vval(RENDER)); + if (rvdevice != NULL) { + if (vdef(RVU)) + if (vval(RVU)[0] != '-') { + atos(c_rvu, sizeof(c_rvu), vval(RVU)); + po = addarg(po, sskip2(vval(RVU), 1)); + } else + po = addarg(po, vval(RVU)); + } else { + if (vdef(RPICT)) + if (vval(RPICT)[0] != '-') { + atos(c_rpict, sizeof(c_rpict), vval(RPICT)); + po = addarg(po, sskip2(vval(RPICT), 1)); + } else + po = addarg(po, vval(RPICT)); + } } static void lowqopts( /* low quality rendering options */ - register char *op, + char *op, char *po ) { @@ -713,7 +892,7 @@ lowqopts( /* low quality rendering options */ op = addarg(op, "-ds .4"); else op = addarg(op, "-ds 0"); - op = addarg(op, "-dt .2 -dc .25 -dr 0 -sj 0 -st .5"); + op = addarg(op, "-dt .2 -dc .25 -dr 0 -ss 0 -st .5"); if (vdef(AMBFILE)) { sprintf(op, " -af %s", vval(AMBFILE)); op += strlen(op); @@ -734,15 +913,13 @@ lowqopts( /* low quality rendering options */ d = ambval(); sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); - op = addarg(op, "-lr 6 -lw .01"); - if (vdef(RENDER)) - op = addarg(op, vval(RENDER)); + op = addarg(op, "-lr 6 -lw .003"); } static void medqopts( /* medium quality rendering options */ - register char *op, + char *op, char *po ) { @@ -787,10 +964,10 @@ medqopts( /* medium quality rendering options */ } po = addarg(po, "-pt .08"); if (vbool(PENUMBRAS)) - op = addarg(op, "-ds .2 -dj .5"); + op = addarg(op, "-ds .2 -dj .9"); else op = addarg(op, "-ds .3"); - op = addarg(op, "-dt .1 -dc .5 -dr 1 -sj .7 -st .1"); + op = addarg(op, "-dt .1 -dc .5 -dr 1 -ss 1 -st .1"); if ( (overture = vint(INDIRECT)) ) { sprintf(op, " -ab %d", overture); op += strlen(op); @@ -814,15 +991,13 @@ medqopts( /* medium quality rendering options */ d = ambval(); sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); - op = addarg(op, "-lr 8 -lw .002"); - if (vdef(RENDER)) - op = addarg(op, vval(RENDER)); + op = addarg(op, "-lr 8 -lw 1e-4"); } static void hiqopts( /* high quality rendering options */ - register char *op, + char *op, char *po ) { @@ -867,10 +1042,10 @@ hiqopts( /* high quality rendering options */ } po = addarg(po, "-pt .04"); if (vbool(PENUMBRAS)) - op = addarg(op, "-ds .1 -dj .65"); + op = addarg(op, "-ds .1 -dj .9"); else op = addarg(op, "-ds .2"); - op = addarg(op, "-dt .05 -dc .75 -dr 3 -sj 1 -st .01"); + op = addarg(op, "-dt .05 -dc .75 -dr 3 -ss 16 -st .01"); sprintf(op, " -ab %d", overture=vint(INDIRECT)+1); op += strlen(op); if (vdef(AMBFILE)) { @@ -892,19 +1067,40 @@ hiqopts( /* high quality rendering options */ d = ambval(); sprintf(op, " -av %.2g %.2g %.2g", d, d, d); op += strlen(op); - op = addarg(op, "-lr 12 -lw .0005"); - if (vdef(RENDER)) - op = addarg(op, vval(RENDER)); + op = addarg(op, "-lr 12 -lw 1e-5"); } +#ifdef _WIN32 static void +setenv( /* set an environment variable */ + char *vname, + char *value +) +{ + char *evp; + + evp = bmalloc(strlen(vname)+strlen(value)+2); + if (evp == NULL) + syserr(progname); + sprintf(evp, "%s=%s", vname, value); + if (putenv(evp) != 0) { + fprintf(stderr, "%s: out of environment space\n", progname); + quit(1); + } + if (!silent) + printf("set %s\n", evp); +} +#endif + + +static void xferopts( /* transfer options if indicated */ char *ro ) { int fd, n; - register char *cp; + char *cp; n = strlen(ro); if (n < 2) @@ -933,7 +1129,7 @@ xferopts( /* transfer options if indicated */ static void pfiltopts( /* get pfilt options */ - register char *po + char *po ) { *po = '\0'; @@ -950,14 +1146,18 @@ pfiltopts( /* get pfilt options */ break; } if (vdef(PFILT)) - po = addarg(po, vval(PFILT)); + if (vval(PFILT)[0] != '-') { + atos(c_pfilt, sizeof(c_pfilt), vval(PFILT)); + po = addarg(po, sskip2(vval(PFILT), 1)); + } else + po = addarg(po, vval(PFILT)); } static int matchword( /* match white-delimited words */ - register char *s1, - register char *s2 + char *s1, + char *s2 ) { while (isspace(*s1)) s1++; @@ -971,15 +1171,15 @@ matchword( /* match white-delimited words */ static char * specview( /* get proper view spec from vs */ - register char *vs + char *vs ) { static char vup[7][12] = {"-vu 0 0 -1","-vu 0 -1 0","-vu -1 0 0", "-vu 0 0 1", "-vu 1 0 0","-vu 0 1 0","-vu 0 0 1"}; static char viewopts[128]; - register char *cp; + char *cp; int xpos, ypos, zpos, viewtype, upax; - register int i; + int i; double cent[3], dim[3], mult, d; if (vs == NULL || *vs == '-') @@ -1012,9 +1212,19 @@ specview( /* get proper view spec from vs */ } else if (*vs == 'z') { zpos = -1; vs++; } - viewtype = 'v'; - if((*vs == 'v') | (*vs == 'l') | (*vs == 'a') | (*vs == 'h') | (*vs == 'c')) + switch (*vs) { + case VT_PER: + case VT_PAR: + case VT_ANG: + case VT_HEM: + case VT_PLS: + case VT_CYL: viewtype = *vs++; + break; + default: + viewtype = VT_PER; + break; + } cp = viewopts; if ((!*vs || isspace(*vs)) && (xpos|ypos|zpos)) { /* got one! */ *cp++ = '-'; *cp++ = 'v'; *cp++ = 't'; *cp++ = viewtype; @@ -1029,7 +1239,7 @@ specview( /* get proper view spec from vs */ cent[i] += .5*dim[i]; } mult = vlet(ZONE)=='E' ? 2. : .45 ; - sprintf(cp, " -vp %.2g %.2g %.2g -vd %.2g %.2g %.2g", + sprintf(cp, " -vp %.3g %.3g %.3g -vd %.3g %.3g %.3g", cent[0]+xpos*mult*dim[0], cent[1]+ypos*mult*dim[1], cent[2]+zpos*mult*dim[2], @@ -1056,19 +1266,20 @@ specview( /* get proper view spec from vs */ } cp = addarg(cp, vup[upax+3]); switch (viewtype) { - case 'v': + case VT_PER: cp = addarg(cp, "-vh 45 -vv 45"); break; - case 'l': + case VT_PAR: d = sqrt(dim[0]*dim[0]+dim[1]*dim[1]+dim[2]*dim[2]); - sprintf(cp, " -vh %.2g -vv %.2g", d, d); + sprintf(cp, " -vh %.3g -vv %.3g", d, d); cp += strlen(cp); break; - case 'a': - case 'h': + case VT_ANG: + case VT_HEM: + case VT_PLS: cp = addarg(cp, "-vh 180 -vv 180"); break; - case 'c': + case VT_CYL: cp = addarg(cp, "-vh 180 -vv 90"); break; } @@ -1076,7 +1287,7 @@ specview( /* get proper view spec from vs */ while (!isspace(*vs)) /* else skip id */ if (!*vs++) return(NULL); - if (upax) { /* specify up vector */ + if (upax) { /* prepend up vector */ strcpy(cp, vup[upax+3]); cp += strlen(cp); } @@ -1100,13 +1311,19 @@ getview( /* get view n, or NULL if none */ char *vn /* returned view name */ ) { - register char *mv; + char *mv; if (viewselect != NULL) { /* command-line selected */ if (n) /* only do one */ return(NULL); + + if (isint(viewselect)) { /* view number? */ + n = atoi(viewselect)-1; + goto numview; + } if (viewselect[0] == '-') { /* already specified */ - if (vn != NULL) *vn = '\0'; + if (vn != NULL) + strcpy(vn, "0"); return(viewselect); } if (vn != NULL) { @@ -1115,36 +1332,37 @@ getview( /* get view n, or NULL if none */ ; *vn = '\0'; } - /* view number? */ - if (isint(viewselect)) - return(specview(nvalue(VIEWS, atoi(viewselect)-1))); /* check list */ while ((mv = nvalue(VIEWS, n++)) != NULL) if (matchword(viewselect, mv)) return(specview(mv)); + return(specview(viewselect)); /* standard view? */ } +numview: mv = nvalue(VIEWS, n); /* use view n */ - if ((vn != NULL) & (mv != NULL)) { - register char *mv2 = mv; - if (*mv2 != '-') + if ((vn != NULL) & (mv != NULL)) + if (*mv != '-') { + char *mv2 = mv; while (*mv2 && !isspace(*mv2)) *vn++ = *mv2++; - *vn = '\0'; - } + *vn = '\0'; + } else + sprintf(vn, "%d", n+1); + return(specview(mv)); } static int myprintview( /* print out selected view */ - register char *vopts, + char *vopts, FILE *fp ) { VIEW vwr; char buf[128]; - register char *cp; + char *cp; #ifdef _WIN32 /* XXX Should we allow something like this for all platforms? */ /* XXX Or is it still required at all? */ @@ -1185,34 +1403,66 @@ rvu( /* run rvu with first view */ return; if (sayview) myprintview(vw, stdout); - sprintf(combuf, "rvu %s%s%s -R %s ", vw, po, opts, rifname); + sprintf(combuf, "%s %s%s%s -R %s ", c_rvu, vw, opts, po, rifname); + if (nprocs > 1) + sprintf(combuf+strlen(combuf), "-n %d ", nprocs); if (rvdevice != NULL) sprintf(combuf+strlen(combuf), "-o %s ", rvdevice); if (vdef(EXPOSURE)) sprintf(combuf+strlen(combuf), "-pe %s ", vval(EXPOSURE)); strcat(combuf, oct1name); if (runcom(combuf)) { /* run it */ - fprintf(stderr, "%s: error running rvu\n", progname); + fprintf(stderr, "%s: error running %s\n", progname, c_rvu); quit(1); } } +static int +syncf_done( /* check if an rpiece sync file is complete */ + char *sfname +) +{ + FILE *fp = fopen(sfname, "r"); + int todo = 1; + int x, y; + + if (fp == NULL) + return(0); + if (fscanf(fp, "%d %d", &x, &y) != 2) + goto checked; + todo = x*y; /* total number of tiles */ + if (fscanf(fp, "%d %d", &x, &y) != 2 || (x != 0) | (y != 0)) + goto checked; + /* XXX assume no redundant tiles */ + while (fscanf(fp, "%d %d", &x, &y) == 2) + if (!--todo) + break; +checked: + fclose(fp); + return(!todo); +} + + static void rpict( /* run rpict and pfilt for each view */ char *opts, char *po ) { - char combuf[PATH_MAX]; +#define do_rpiece (sfile[0]!='\0') + char combuf[5*PATH_MAX+512]; char rawfile[PATH_MAX], picfile[PATH_MAX]; char zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32]; - char rppopt[128], *pfile = NULL; + char rppopt[32], sfile[PATH_MAX], *pfile = NULL; char pfopts[128]; char vs[32], *vw; int vn, mult; FILE *fp; time_t rfdt, pfdt; + int xres, yres; + double aspect; + int n; /* get pfilt options */ pfiltopts(pfopts); /* get resolution, reporting */ @@ -1227,24 +1477,19 @@ rpict( /* run rpict and pfilt for each view */ mult = 3; break; } - { - int xres, yres; - double aspect; - int n; - n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect); - if (n == 3) - sprintf(res, "-x %d -y %d -pa %.3f", - mult*xres, mult*yres, aspect); - else if (n) { - if (n == 1) yres = xres; - sprintf(res, "-x %d -y %d", mult*xres, mult*yres); - } else - badvalue(RESOLUTION); - } + n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect); + if (n == 3) + sprintf(res, "-x %d -y %d -pa %.3f", + mult*xres, mult*yres, aspect); + else if (n) { + aspect = 1.; + if (n == 1) yres = xres; + sprintf(res, "-x %d -y %d", mult*xres, mult*yres); + } else + badvalue(RESOLUTION); rep[0] = '\0'; if (vdef(REPORT)) { double minutes; - int n; n = sscanf(vval(REPORT), "%lf %s", &minutes, rawfile); if (n == 2) sprintf(rep, " -t %d -e %s", (int)(minutes*60), rawfile); @@ -1254,19 +1499,32 @@ rpict( /* run rpict and pfilt for each view */ badvalue(REPORT); } /* set up parallel rendering */ - if ((nprocs > 1) & (!vdef(ZFILE))) { - strcpy(rppopt, "-S 1 -PP pfXXXXXX"); - pfile = rppopt+9; - if (mktemp(pfile) == NULL) + sfile[0] = '\0'; + if ((nprocs > 1) & !touchonly & !vdef(ZFILE) && + getview(0, vs) != NULL) { + if (!strcmp(c_rpict, DEF_RPICT_PATH) && + getview(1, NULL) == NULL) { + sprintf(sfile, "%s_%s_rpsync.txt", + vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE), + vs); + strcpy(rppopt, "-PP pfXXXXXX"); + } else { + strcpy(rppopt, "-S 1 -PP pfXXXXXX"); + } + pfile = rppopt + strlen(rppopt) - 8; + if (mktemp(pfile) == NULL) { + if (do_rpiece) { + fprintf(stderr, "%s: cannot create\n", pfile); + quit(1); + } pfile = NULL; + } } vn = 0; /* do each view */ while ((vw = getview(vn++, vs)) != NULL) { if (sayview) myprintview(vw, stdout); - if (!vs[0]) - sprintf(vs, "%d", vn); - sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs); + sprintf(picfile, "%s_%s.hdr", vval(PICTURE), vs); if (vdef(ZFILE)) sprintf(zopt, " -z %s_%s.zbf", vval(ZFILE), vs); else @@ -1287,66 +1545,122 @@ rpict( /* run rpict and pfilt for each view */ touch(picfile); continue; } - if (next_process()) { /* parallel running? */ + /* parallel running? */ + if (do_rpiece) { + if (rfdt < oct1date || !fdate(sfile)) { + int xdiv = 8+nprocs/3, ydiv = 8+nprocs/3; + if (rfdt >= oct1date) { + fprintf(stderr, + "%s: partial output not created with %s\n", rawfile, c_rpiece); + quit(1); + } + if (rfdt) { /* start fresh */ + rmfile(rawfile); + rfdt = 0; + } + if (!silent) + printf("\techo %d %d > %s\n", + xdiv, ydiv, sfile); + if ((fp = fopen(sfile, "w")) == NULL) { + fprintf(stderr, "%s: cannot create\n", + sfile); + quit(1); + } + fprintf(fp, "%d %d\n", xdiv, ydiv); + fclose(fp); + } + } else if (next_process(0)) { if (pfile != NULL) - sleep(20); + sleep(10); continue; - } + } else if (!inchild()) + pfile = NULL; /* XXX Remember to call finish_process() */ /* build rpict command */ - if (rfdt >= oct1date) { /* recover */ - sprintf(combuf, "rpict%s%s%s%s -ro %s %s", - rep, po, opts, zopt, rawfile, oct1name); - if (runcom(combuf)) /* run rpict */ + if (rfdt >= oct1date) { /* already in progress */ + if (do_rpiece) { + sprintf(combuf, "%s -R %s %s%s %s %s%s%s -o %s %s", + c_rpiece, sfile, rppopt, rep, vw, + res, opts, po, rawfile, oct1name); + while (next_process(1)) { + sleep(10); + combuf[strlen(c_rpiece)+2] = 'F'; + } + } else + sprintf(combuf, "%s%s%s%s%s -ro %s %s", c_rpict, + rep, opts, po, zopt, rawfile, oct1name); + if (runcom(combuf)) /* run rpict/rpiece */ goto rperror; } else { if (overture) { /* run overture calculation */ sprintf(combuf, - "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s", - rep, vw, opts, + "%s%s %s%s -x 64 -y 64 -ps 1 %s > %s", + c_rpict, rep, vw, opts, oct1name, overfile); - if (runcom(combuf)) { - fprintf(stderr, + if (!do_rpiece || !next_process(0)) { + if (runcom(combuf)) { + fprintf(stderr, "%s: error in overture for view %s\n", - progname, vs); - quit(1); - } + progname, vs); + quit(1); + } #ifndef NULL_DEVICE - rmfile(overfile); + rmfile(overfile); #endif + } else if (do_rpiece) + sleep(20); } - sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s", - rep, vw, res, po, opts, - zopt, oct1name, rawfile); - if (pfile != NULL && inchild()) { - /* rpict persistent mode */ - if (!silent) { + if (do_rpiece) { + sprintf(combuf, "%s -F %s %s%s %s %s%s%s -o %s %s", + c_rpiece, sfile, rppopt, rep, vw, + res, opts, po, rawfile, oct1name); + while (next_process(1)) + sleep(10); + } else { + sprintf(combuf, "%s%s %s %s%s%s%s %s > %s", + c_rpict, rep, vw, res, opts, po, + zopt, oct1name, rawfile); + } + if ((pfile != NULL) & !do_rpiece) { + if (!silent) printf("\t%s\n", combuf); - fflush(stdout); - } - sprintf(combuf, "rpict%s %s %s%s%s %s > %s", - rep, rppopt, res, po, opts, - oct1name, rawfile); fflush(stdout); + sprintf(combuf, "%s%s %s %s%s%s %s > %s", + c_rpict, rep, rppopt, res, + opts, po, oct1name, rawfile); fp = popen(combuf, "w"); if (fp == NULL) goto rperror; myprintview(vw, fp); if (pclose(fp)) goto rperror; - } else { /* rpict normal mode */ - if (runcom(combuf)) - goto rperror; + } else if (runcom(combuf)) + goto rperror; + } + if (do_rpiece) { /* need to finish raw, first */ + finish_process(); + wait_process(1); + if (!syncf_done(sfile)) { + fprintf(stderr, + "%s: %s did not complete rendering of view %s\n", + progname, c_rpiece, vs); + quit(1); } } if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) { /* build pfilt command */ - if (mult > 1) - sprintf(combuf, "pfilt%s -x /%d -y /%d %s > %s", - pfopts, mult, mult, rawfile, picfile); + if (do_rpiece) + sprintf(combuf, + "%s%s -x %d -y %d -p %.3f %s > %s", + c_pfilt, pfopts, xres, yres, aspect, + rawfile, picfile); + else if (mult > 1) + sprintf(combuf, "%s%s -x /%d -y /%d %s > %s", + c_pfilt, pfopts, mult, mult, + rawfile, picfile); else - sprintf(combuf, "pfilt%s %s > %s", pfopts, - rawfile, picfile); + sprintf(combuf, "%s%s %s > %s", c_pfilt, + pfopts, rawfile, picfile); if (runcom(combuf)) { /* run pfilt */ fprintf(stderr, "%s: error filtering view %s\n\t%s removed\n", @@ -1357,15 +1671,18 @@ rpict( /* run rpict and pfilt for each view */ } /* remove/rename raw file */ if (vdef(RAWFILE)) { - sprintf(combuf, "%s_%s.pic", vval(RAWFILE), vs); + sprintf(combuf, "%s_%s.hdr", vval(RAWFILE), vs); mvfile(rawfile, combuf); } else rmfile(rawfile); - finish_process(); /* exit if child */ + if (do_rpiece) /* done with sync file */ + rmfile(sfile); + else + finish_process(); /* exit if child */ } wait_process(1); /* wait for children to finish */ - if (pfile != NULL) { /* clean up rpict persistent mode */ - int pid; + if (pfile != NULL) { /* clean up persistent rpict */ + RT_PID pid; fp = fopen(pfile, "r"); if (fp != NULL) { if (fscanf(fp, "%*s %d", &pid) != 1 || @@ -1378,6 +1695,7 @@ rpict( /* run rpict and pfilt for each view */ rperror: fprintf(stderr, "%s: error rendering view %s\n", progname, vs); quit(1); +#undef do_rpiece } @@ -1390,11 +1708,6 @@ touch( /* update a file */ printf("\ttouch %s\n", fn); if (!nprocs) return(0); -#ifdef notused - if (access(fn, F_OK) == -1) /* create it */ - if (close(open(fn, O_WRONLY|O_CREAT, 0666)) == -1) - return(-1); -#endif return(setfdate(fn, time((time_t *)NULL))); } @@ -1408,7 +1721,7 @@ runcom( /* run command */ printf("\t%s\n", cs); if (!nprocs) return(0); - fflush(stdout); /* flush output and pass to shell */ + fflush(NULL); /* flush output and pass to shell */ return(system(cs)); } @@ -1442,9 +1755,9 @@ mvfile( /* move a file */ #ifdef RHAS_FORK_EXEC static int -next_process(void) /* fork the next process (max. nprocs) */ +next_process(int reserve) /* fork the next process */ { - int child_pid; + RT_PID child_pid; if (nprocs <= 1) return(0); /* it's us or no one */ @@ -1453,12 +1766,15 @@ next_process(void) /* fork the next process (max. np progname); quit(1); } + if (reserve > 0 && children_running >= nprocs-reserve) + return(0); /* caller holding back process(es) */ if (children_running >= nprocs) wait_process(0); /* wait for someone to finish */ - fflush(stdout); + fflush(NULL); /* flush output */ child_pid = fork(); /* split process */ if (child_pid == 0) { /* we're the child */ children_running = -1; + nprocs = 1; return(0); } if (child_pid > 0) { /* we're the parent */ @@ -1474,8 +1790,8 @@ wait_process( /* wait for process(es) to finish */ int all ) { - int ourstatus = 0; - int pid, status; + int ourstatus = 0, status; + RT_PID pid; if (all) all = children_running; @@ -1500,7 +1816,7 @@ wait_process( /* wait for process(es) to finish */ } #else /* ! RHAS_FORK_EXEC */ static int -next_process(void) +next_process(int reserve) { return(0); /* cannot start new process */ } @@ -1512,7 +1828,8 @@ int all; } int kill(pid, sig) /* win|unix_process.c should also wait and kill */ -int pid, sig; +RT_PID pid; +int sig; { return 0; } @@ -1525,25 +1842,6 @@ finish_process(void) /* exit a child process */ return; /* in parent -- noop */ exit(0); } - -#ifdef _WIN32 -setenv(vname, value) /* set an environment variable */ -char *vname, *value; -{ - register char *evp; - - evp = bmalloc(strlen(vname)+strlen(value)+2); - if (evp == NULL) - syserr(progname); - sprintf(evp, "%s=%s", vname, value); - if (putenv(evp) != 0) { - fprintf(stderr, "%s: out of environment space\n", progname); - quit(1); - } - if (!silent) - printf("set %s\n", evp); -} -#endif static void