ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
(Generate patch)

Comparing ray/src/util/rad.c (file contents):
Revision 2.22 by greg, Wed Aug 25 13:48:37 1993 UTC vs.
Revision 2.114 by greg, Wed May 27 13:29:57 2015 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Executive program for oconv, rpict and pfilt
6   */
7  
8   #include "standard.h"
9 < #include "paths.h"
9 >
10   #include <ctype.h>
11 + #include <time.h>
12  
13 + #include "platform.h"
14 + #include "rtprocess.h"
15 + #include "view.h"
16 + #include "paths.h"
17 + #include "vars.h"
18  
19 < typedef struct {
20 <        char    *name;          /* variable name */
21 <        short   nick;           /* # characters required for nickname */
22 <        short   nass;           /* # assignments made */
23 <        char    *value;         /* assigned value(s) */
24 <        int     (*fixval)();    /* assignment checking function */
25 < } VARIABLE;
19 > #ifdef _WIN32
20 >  #define DELCMD "del"
21 >  #define RENAMECMD "rename"
22 > #else
23 >  #define DELCMD "rm -f"
24 >  #define RENAMECMD "mv"
25 >  #include <sys/types.h>
26 >  #include <sys/wait.h>
27 >  #include <signal.h>
28 > #endif
29  
30 < int     onevalue(), catvalues();
31 <
32 <                                /* variables */
33 < #define OBJECT          0               /* object files */
34 < #define SCENE           1               /* scene files */
35 < #define MATERIAL        2               /* material files */
36 < #define RENDER          3               /* rendering options */
37 < #define OCONV           4               /* oconv options */
38 < #define PFILT           5               /* pfilt options */
39 < #define VIEW            6               /* view(s) for picture(s) */
40 < #define ZONE            7               /* simulation zone */
41 < #define QUALITY         8               /* desired rendering quality */
42 < #define OCTREE          9               /* octree file name */
37 < #define PICTURE         10              /* picture file name */
38 < #define AMBFILE         11              /* ambient file name */
30 >                                /* variables (alphabetical by name) */
31 > #define AMBFILE         0               /* ambient file name */
32 > #define DETAIL          1               /* level of scene detail */
33 > #define EXPOSURE        2               /* picture exposure setting */
34 > #define EYESEP          3               /* interocular distance */
35 > #define ILLUM           4               /* mkillum input files */
36 > #define INDIRECT        5               /* indirection in lighting */
37 > #define MATERIAL        6               /* material files */
38 > #define MKILLUM         7               /* mkillum options */
39 > #define MKPMAP          8               /* mkpmap options */
40 > #define OBJECT          9               /* object files */
41 > #define OCONV           10              /* oconv options */
42 > #define OCTREE          11              /* octree file name */
43   #define OPTFILE         12              /* rendering options file */
44 < #define EXPOSURE        13              /* picture exposure setting */
45 < #define RESOLUTION      14              /* maximum picture resolution */
46 < #define UP              15              /* view up (X, Y or Z) */
47 < #define INDIRECT        16              /* indirection in lighting */
48 < #define DETAIL          17              /* level of scene detail */
49 < #define PENUMBRAS       18              /* shadow penumbras are desired */
50 < #define VARIABILITY     19              /* level of light variability */
51 < #define REPORT          20              /* report frequency and errfile */
44 > #define PCMAP           13              /* caustic photon map */
45 > #define PENUMBRAS       14              /* shadow penumbras are desired */
46 > #define PFILT           15              /* pfilt options */
47 > #define PGMAP           16              /* global photon map */
48 > #define PICTURE         17              /* picture file root name */
49 > #define QUALITY         18              /* desired rendering quality */
50 > #define RAWFILE         19              /* raw picture file root name */
51 > #define RENDER          20              /* rendering options */
52 > #define REPORT          21              /* report frequency and errfile */
53 > #define RESOLUTION      22              /* maximum picture resolution */
54 > #define RPICT           23              /* rpict parameters */
55 > #define RVU             24              /* rvu parameters */
56 > #define SCENE           25              /* scene files */
57 > #define UP              26              /* view up (X, Y or Z) */
58 > #define VARIABILITY     27              /* level of light variability */
59 > #define VIEWS           28              /* view(s) for picture(s) */
60 > #define ZFILE           29              /* distance file root name */
61 > #define ZONE            30              /* simulation zone */
62                                  /* total number of variables */
63 < #define NVARS           21
63 > int NVARS = 31;
64  
65 < VARIABLE        vv[NVARS] = {           /* variable-value pairs */
66 <        {"objects",     3,      0,      NULL,   catvalues},
67 <        {"scene",       3,      0,      NULL,   catvalues},
65 > VARIABLE        vv[] = {                /* variable-value pairs */
66 >        {"AMBFILE",     3,      0,      NULL,   onevalue},
67 >        {"DETAIL",      3,      0,      NULL,   qualvalue},
68 >        {"EXPOSURE",    3,      0,      NULL,   fltvalue},
69 >        {"EYESEP",      3,      0,      NULL,   fltvalue},
70 >        {"illum",       3,      0,      NULL,   catvalues},
71 >        {"INDIRECT",    3,      0,      NULL,   intvalue},
72          {"materials",   3,      0,      NULL,   catvalues},
73 <        {"render",      3,      0,      NULL,   catvalues},
73 >        {"mkillum",     3,      0,      NULL,   catvalues},
74 >        {"mkpmap",      3,      0,      NULL,   catvalues},
75 >        {"objects",     3,      0,      NULL,   catvalues},
76          {"oconv",       3,      0,      NULL,   catvalues},
57        {"pfilt",       2,      0,      NULL,   catvalues},
58        {"view",        2,      0,      NULL,   NULL},
59        {"ZONE",        2,      0,      NULL,   onevalue},
60        {"QUALITY",     3,      0,      NULL,   onevalue},
77          {"OCTREE",      3,      0,      NULL,   onevalue},
62        {"PICTURE",     3,      0,      NULL,   onevalue},
63        {"AMBFILE",     3,      0,      NULL,   onevalue},
78          {"OPTFILE",     3,      0,      NULL,   onevalue},
79 <        {"EXPOSURE",    3,      0,      NULL,   onevalue},
79 >        {"PCMAP",       2,      0,      NULL,   onevalue},
80 >        {"PENUMBRAS",   3,      0,      NULL,   boolvalue},
81 >        {"pfilt",       2,      0,      NULL,   catvalues},
82 >        {"PGMAP",       2,      0,      NULL,   onevalue},
83 >        {"PICTURE",     3,      0,      NULL,   onevalue},
84 >        {"QUALITY",     3,      0,      NULL,   qualvalue},
85 >        {"RAWFILE",     3,      0,      NULL,   onevalue},
86 >        {"render",      3,      0,      NULL,   catvalues},
87 >        {"REPORT",      3,      0,      NULL,   onevalue},
88          {"RESOLUTION",  3,      0,      NULL,   onevalue},
89 +        {"rpict",       3,      0,      NULL,   catvalues},
90 +        {"rvu",         3,      0,      NULL,   catvalues},
91 +        {"scene",       3,      0,      NULL,   catvalues},
92          {"UP",          2,      0,      NULL,   onevalue},
93 <        {"INDIRECT",    3,      0,      NULL,   onevalue},
94 <        {"DETAIL",      3,      0,      NULL,   onevalue},
95 <        {"PENUMBRAS",   3,      0,      NULL,   onevalue},
96 <        {"VARIABILITY", 3,      0,      NULL,   onevalue},
72 <        {"REPORT",      3,      0,      NULL,   onevalue},
93 >        {"VARIABILITY", 3,      0,      NULL,   qualvalue},
94 >        {"view",        2,      0,      NULL,   NULL},
95 >        {"ZFILE",       2,      0,      NULL,   onevalue},
96 >        {"ZONE",        2,      0,      NULL,   onevalue},
97   };
98  
75 VARIABLE        *matchvar();
76 char    *nvalue();
77 int     vscale();
78
79 #define UPPER(c)        ((c)&~0x20)     /* ASCII trick */
80
81 #define vnam(vc)        (vv[vc].name)
82 #define vdef(vc)        (vv[vc].nass)
83 #define vval(vc)        (vv[vc].value)
84 #define vint(vc)        atoi(vval(vc))
85 #define vlet(vc)        UPPER(vval(vc)[0])
86 #define vbool(vc)       (vlet(vc)=='T')
87
88 #define HIGH            2
89 #define MEDIUM          1
90 #define LOW             0
91
92 int     lowqopts(), medqopts(), hiqopts();
93 int     (*setqopts[3])() = {lowqopts, medqopts, hiqopts};
94
95 #define renderopts      (*setqopts[vscale(QUALITY)])
96
99                                  /* overture calculation file */
100 < #ifdef NIX
101 < char    overfile[] = "overture.raw";
100 > #ifdef NULL_DEVICE
101 > char    overfile[] = NULL_DEVICE;
102   #else
103 < char    overfile[] = "/dev/null";
103 > char    overfile[] = "overture.unf";
104   #endif
105  
104 extern long     fdate(), time();
106  
107 < long    scenedate;              /* date of latest scene or object file */
108 < long    octreedate;             /* date of octree */
109 < long    matdate;                /* date of latest material file */
107 > time_t  scenedate;              /* date of latest scene or object file */
108 > time_t  octreedate;             /* date of octree */
109 > time_t  matdate;                /* date of latest material file */
110 > time_t  illumdate;              /* date of last illum file */
111  
112 + char    *oct0name;              /* name of pre-mkillum octree */
113 + time_t  oct0date;               /* date of pre-mkillum octree */
114 + char    *oct1name;              /* name of post-mkillum octree */
115 + time_t  oct1date;               /* date of post-mkillum octree (>= matdate) */
116 +
117 + char    *pgmapname;             /* name of global photon map */
118 + time_t  pgmapdate;              /* date of global photon map (>= oct1date) */
119 + char    *pcmapname;             /* name of caustic photon map */
120 + time_t  pcmapdate;              /* date of caustic photon map (>= oct1date) */
121 +
122 + int     nowarn = 0;             /* no warnings */
123   int     explicate = 0;          /* explicate variables */
124   int     silent = 0;             /* do work silently */
125 < int     noaction = 0;           /* don't do anything */
125 > int     touchonly = 0;          /* touch files only */
126 > int     nprocs = 1;             /* maximum executing processes */
127   int     sayview = 0;            /* print view out */
128 < char    *rvdevice = NULL;       /* rview output device */
128 > char    *rvdevice = NULL;       /* rvu output device */
129   char    *viewselect = NULL;     /* specific view only */
130  
131 + #define DEF_RPICT_PATH  "rpict"         /* default rpict path */
132 +
133 +                                /* command paths */
134 + char    c_oconv[256] = "oconv";
135 + char    c_mkillum[256] = "mkillum";
136 + char    c_mkpmap[256] = "mkpmap";
137 + char    c_rvu[256] = "rvu";
138 + char    c_rpict[256] = DEF_RPICT_PATH;
139 + char    c_rpiece[] = "rpiece";
140 + char    c_pfilt[256] = "pfilt";
141 +
142   int     overture = 0;           /* overture calculation needed */
143  
144 + int     children_running = 0;   /* set negative in children */
145 +
146   char    *progname;              /* global argv[0] */
147   char    *rifname;               /* global rad input file name */
148  
149 < char    radname[MAXPATH];       /* root Radiance file name */
149 > char    radname[PATH_MAX];      /* root Radiance file name */
150  
151 + #define inchild()       (children_running < 0)
152  
153 < main(argc, argv)
154 < int     argc;
155 < char    *argv[];
153 > static void rootname(char       *rn, char       *fn);
154 > static time_t checklast(char    *fnames);
155 > static char * newfname(char     *orig, int      pred);
156 > static void checkfiles(void);
157 > static void getoctcube(double   org[3], double  *sizp);
158 > static void setdefaults(void);
159 > static void oconv(void);
160 > static void mkpmap(void);
161 > static char * addarg(char       *op, char       *arg);
162 > static void oconvopts(char      *oo);
163 > static void mkillumopts(char    *mo);
164 > static void mkpmapopts(char     *mo);
165 > static void checkambfile(void);
166 > static double ambval(void);
167 > static void renderopts(char     *op, char       *po);
168 > static void lowqopts(char       *op, char       *po);
169 > static void medqopts(char       *op, char       *po);
170 > static void hiqopts(char        *op, char       *po);
171 > static void xferopts(char       *ro);
172 > static void pfiltopts(char      *po);
173 > static int matchword(char       *s1, char       *s2);
174 > static char * specview(char     *vs);
175 > static char * getview(int       n, char *vn);
176 > static int myprintview(char     *vopts, FILE    *fp);
177 > static void rvu(char    *opts, char     *po);
178 > static void rpict(char  *opts, char     *po);
179 > static int touch(char   *fn);
180 > static int runcom(char  *cs);
181 > static int rmfile(char  *fn);
182 > static int mvfile(char  *fold, char     *fnew);
183 > static int next_process(int     reserve);
184 > static void wait_process(int    all);
185 > static void finish_process(void);
186 > static void badvalue(int        vc);
187 > static void syserr(char *s);
188 >
189 >
190 > int
191 > main(
192 >        int     argc,
193 >        char    *argv[]
194 > )
195   {
196          char    ropts[512];
197 +        char    popts[64];
198          int     i;
199  
200          progname = argv[0];
# Line 137 | Line 205 | char   *argv[];
205                          silent++;
206                          break;
207                  case 'n':
208 <                        noaction++;
208 >                        nprocs = 0;
209                          break;
210 +                case 'N':
211 +                        nprocs = atoi(argv[++i]);
212 +                        if (nprocs < 0)
213 +                                nprocs = 0;
214 +                        break;
215 +                case 't':
216 +                        touchonly++;
217 +                        break;
218                  case 'e':
219                          explicate++;
220                          break;
# Line 151 | Line 227 | char   *argv[];
227                  case 'v':
228                          viewselect = argv[++i];
229                          break;
230 +                case 'w':
231 +                        nowarn++;
232 +                        break;
233                  default:
234                          goto userr;
235                  }
# Line 160 | Line 239 | char   *argv[];
239                                  /* assign Radiance root file name */
240          rootname(radname, rifname);
241                                  /* load variable values */
242 <        load(rifname);
242 >        loadvars(rifname);
243                                  /* get any additional assignments */
244          for (i++; i < argc; i++)
245 <                setvariable(argv[i]);
245 >                if (setvariable(argv[i], matchvar) < 0) {
246 >                        fprintf(stderr, "%s: unknown variable: %s\n",
247 >                                        progname, argv[i]);
248 >                        quit(1);
249 >                }
250                                  /* check assignments */
251          checkvalues();
252                                  /* check files and dates */
# Line 172 | Line 255 | char   *argv[];
255          setdefaults();
256                                  /* print all values if requested */
257          if (explicate)
258 <                printvals();
259 <                                /* build octree */
258 >                printvars(stdout);
259 >                                /* build octree (and run mkillum) */
260          oconv();
261 +                                /* run mkpmap if indicated */
262 +        mkpmap();
263                                  /* check date on ambient file */
264          checkambfile();
265                                  /* run simulation */
266 <        renderopts(ropts);
266 >        renderopts(ropts, popts);
267          xferopts(ropts);
268          if (rvdevice != NULL)
269 <                rview(ropts);
269 >                rvu(ropts, popts);
270          else
271 <                rpict(ropts);
272 <        exit(0);
271 >                rpict(ropts, popts);
272 >        quit(0);
273   userr:
274          fprintf(stderr,
275 <        "Usage: %s [-s][-n][-e][-v view][-o dev] rfile [VAR=value ..]\n",
275 > "Usage: %s [-w][-s][-n|-N npr][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
276                          progname);
277 <        exit(1);
277 >        quit(1);
278 >        return 1; /* pro forma return */
279   }
280  
281  
282 < rootname(rn, fn)                /* remove tail from end of fn */
283 < register char   *rn, *fn;
282 > static void
283 > rootname(               /* remove tail from end of fn */
284 >        char    *rn,
285 >        char    *fn
286 > )
287   {
288          char    *tp, *dp;
289  
290 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
290 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++)
291                  if (ISDIRSEP(*rn))
292                          dp = rn;
293                  else if (*rn == '.')
# Line 208 | Line 297 | register char  *rn, *fn;
297   }
298  
299  
300 < load(rfname)                    /* load Radiance simulation file */
301 < char    *rfname;
300 > static time_t
301 > checklast(                      /* check files and find most recent */
302 >        char    *fnames
303 > )
304   {
305 <        FILE    *fp;
306 <        char    buf[512];
216 <        register char   *cp;
305 >        char    thisfile[PATH_MAX];
306 >        time_t  thisdate, lastdate = 0;
307  
308 <        if (rfname == NULL)
309 <                fp = stdin;
310 <        else if ((fp = fopen(rfname, "r")) == NULL)
311 <                syserr(rfname);
312 <        while (fgetline(buf, sizeof(buf), fp) != NULL) {
313 <                for (cp = buf; *cp; cp++) {
314 <                        switch (*cp) {
315 <                        case '\\':
226 <                        case '\n':
227 <                                *cp = ' ';
228 <                                continue;
229 <                        case '#':
230 <                                *cp = '\0';
231 <                                break;
232 <                        default:
233 <                                continue;
234 <                        }
235 <                        break;
308 >        if (fnames == NULL)
309 >                return(0);
310 >        while ((fnames = nextword(thisfile, PATH_MAX, fnames)) != NULL) {
311 >                if (thisfile[0] == '!' ||
312 >                                (thisfile[0] == '\\' && thisfile[1] == '!')) {
313 >                        if (!lastdate)
314 >                                lastdate = 1;
315 >                        continue;
316                  }
317 <                setvariable(buf);
317 >                if (!(thisdate = fdate(thisfile)))
318 >                        syserr(thisfile);
319 >                if (thisdate > lastdate)
320 >                        lastdate = thisdate;
321          }
322 <        fclose(fp);
322 >        return(lastdate);
323   }
324  
325  
326 < setvariable(ass)                /* assign variable according to string */
327 < register char   *ass;
326 > static char *
327 > newfname(               /* create modified file name */
328 >        char    *orig,
329 >        int     pred
330 > )
331   {
332 <        char    varname[32];
247 <        register char   *cp;
248 <        register VARIABLE       *vp;
249 <        register int    i;
332 >        char    *cp;
333          int     n;
334 +        int     suffix;
335  
336 <        while (isspace(*ass))           /* skip leading space */
337 <                ass++;
338 <        cp = varname;                   /* extract name */
339 <        while (cp < varname+sizeof(varname)-1
340 <                        && *ass && !isspace(*ass) && *ass != '=')
257 <                *cp++ = *ass++;
258 <        *cp = '\0';
259 <        if (!varname[0])
260 <                return;         /* no variable name! */
261 <                                        /* trim value */
262 <        while (isspace(*ass) || *ass == '=')
263 <                ass++;
264 <        cp = ass + strlen(ass);
265 <        do
266 <                *cp-- = '\0';
267 <        while (cp >= ass && isspace(*cp));
268 <        n = cp - ass + 1;
269 <        if (!n) {
270 <                fprintf(stderr, "%s: warning - missing value for variable '%s'\n",
271 <                                progname, varname);
272 <                return;
336 >        n = 0; cp = orig; suffix = -1;          /* suffix position, length */
337 >        while (*cp) {
338 >                if (*cp == '.') suffix = n;
339 >                else if (ISDIRSEP(*cp)) suffix = -1;
340 >                cp++; n++;
341          }
342 <                                        /* match variable from list */
343 <        vp = matchvar(varname);
276 <        if (vp == NULL) {
277 <                fprintf(stderr, "%s: unknown variable '%s'\n",
278 <                                progname, varname);
279 <                exit(1);
280 <        }
281 <                                        /* assign new value */
282 <        if (i = vp->nass) {
283 <                cp = vp->value;
284 <                while (i--)
285 <                        while (*cp++)
286 <                                ;
287 <                i = cp - vp->value;
288 <                vp->value = realloc(vp->value, i+n+1);
289 <        } else
290 <                vp->value = malloc(n+1);
291 <        if (vp->value == NULL)
342 >        if (suffix == -1) suffix = n;
343 >        if ((cp = bmalloc(n+2)) == NULL)
344                  syserr(progname);
345 <        strcpy(vp->value+i, ass);
346 <        vp->nass++;
347 < }
296 <
297 <
298 < VARIABLE *
299 < matchvar(nam)                   /* match a variable by its name */
300 < char    *nam;
301 < {
302 <        int     n = strlen(nam);
303 <        register int    i;
304 <
305 <        for (i = 0; i < NVARS; i++)
306 <                if (n >= vv[i].nick && !strncmp(nam, vv[i].name, n))
307 <                        return(vv+i);
308 <        return(NULL);
309 < }
310 <
311 <
312 < char *
313 < nvalue(vp, n)                   /* return nth variable value */
314 < VARIABLE        *vp;
315 < register int    n;
316 < {
317 <        register char   *cp;
318 <
319 <        if (vp == NULL | n < 0 | n >= vp->nass)
320 <                return(NULL);
321 <        cp = vp->value;
322 <        while (n--)
323 <                while (*cp++)
324 <                        ;
345 >        strncpy(cp, orig, suffix);
346 >        cp[suffix] = pred;                      /* root name + pred + suffix */
347 >        strcpy(cp+suffix+1, orig+suffix);
348          return(cp);
349   }
350  
351  
352 < int
353 < vscale(vc)                      /* return scale for variable vc */
331 < int     vc;
352 > static void
353 > checkfiles(void)                        /* check for existence and modified times */
354   {
355 <        switch(vlet(vc)) {
356 <        case 'H':
335 <                return(HIGH);
336 <        case 'M':
337 <                return(MEDIUM);
338 <        case 'L':
339 <                return(LOW);
340 <        }
341 <        badvalue(vc);
342 < }
355 >        char    fntemp[256];
356 >        time_t  objdate;
357  
344
345 checkvalues()                   /* check assignments */
346 {
347        register int    i;
348
349        for (i = 0; i < NVARS; i++)
350                if (vv[i].fixval != NULL)
351                        (*vv[i].fixval)(vv+i);
352 }
353
354
355 onevalue(vp)                    /* only one assignment for this variable */
356 register VARIABLE       *vp;
357 {
358        if (vp->nass < 2)
359                return;
360        fprintf(stderr, "%s: warning - multiple assignment of variable '%s'\n",
361                        progname, vp->name);
362        do
363                vp->value += strlen(vp->value)+1;
364        while (--vp->nass > 1);
365 }
366
367
368 catvalues(vp)                   /* concatenate variable values */
369 register VARIABLE       *vp;
370 {
371        register char   *cp;
372
373        if (vp->nass < 2)
374                return;
375        for (cp = vp->value; vp->nass > 1; vp->nass--) {
376                while (*cp)
377                        cp++;
378                *cp++ = ' ';
379        }
380 }
381
382
383 long
384 checklast(fnames)                       /* check files and find most recent */
385 register char   *fnames;
386 {
387        char    thisfile[MAXPATH];
388        long    thisdate, lastdate = -1;
389        register char   *cp;
390
391        while (*fnames) {
392                while (isspace(*fnames)) fnames++;
393                cp = thisfile;
394                while (*fnames && !isspace(*fnames))
395                        *cp++ = *fnames++;
396                *cp = '\0';
397                if ((thisdate = fdate(thisfile)) < 0)
398                        syserr(thisfile);
399                if (thisdate > lastdate)
400                        lastdate = thisdate;
401        }
402        return(lastdate);
403 }
404
405
406 checkfiles()                    /* check for existence and modified times */
407 {
408        char    *cp;
409        long    objdate;
410
358          if (!vdef(OCTREE)) {
359 <                if ((cp = bmalloc(strlen(radname)+5)) == NULL)
359 >                if ((vval(OCTREE) = bmalloc(strlen(radname)+5)) == NULL)
360                          syserr(progname);
361 <                sprintf(cp, "%s.oct", radname);
415 <                vval(OCTREE) = cp;
361 >                sprintf(vval(OCTREE), "%s.oct", radname);
362                  vdef(OCTREE)++;
363 +        } else if (vval(OCTREE)[0] == '!') {
364 +                fprintf(stderr, "%s: illegal '%s' specification\n",
365 +                                progname, vnam(OCTREE));
366 +                quit(1);
367          }
368          octreedate = fdate(vval(OCTREE));
369 <        scenedate = -1;
370 <        if (vdef(SCENE)) {
371 <                scenedate = checklast(vval(SCENE));
372 <                if (vdef(OBJECT)) {
373 <                        objdate = checklast(vval(OBJECT));
374 <                        if (objdate > scenedate)
375 <                                scenedate = objdate;
369 >        if (vdef(ILLUM)) {              /* illum requires secondary octrees */
370 >                oct0name = newfname(vval(OCTREE), '0');
371 >                oct1name = newfname(vval(OCTREE), '1');
372 >                oct0date = fdate(oct0name);
373 >                oct1date = fdate(oct1name);
374 >        } else
375 >                oct0name = oct1name = vval(OCTREE);
376 >        if ((scenedate = checklast(vval(SCENE))) &&
377 >                        (objdate = checklast(vval(OBJECT))) > scenedate)
378 >                scenedate = objdate;
379 >        illumdate = checklast(vval(ILLUM));
380 >        if (!octreedate & !scenedate & !illumdate) {
381 >                fprintf(stderr, "%s: need '%s' or '%s' or '%s'\n", progname,
382 >                                vnam(OCTREE), vnam(SCENE), vnam(ILLUM));
383 >                quit(1);
384 >        }
385 >        if (vdef(PGMAP)) {
386 >                if (!*sskip2(vval(PGMAP),1)) {
387 >                        fprintf(stderr, "%s: '%s' missing # photons argument\n",
388 >                                        progname, vnam(PGMAP));
389 >                        quit(1);
390                  }
391 +                atos(fntemp, sizeof(fntemp), vval(PGMAP));
392 +                pgmapname = savqstr(fntemp);
393 +                pgmapdate = fdate(pgmapname);
394          }
395 <        if (octreedate < 0 & scenedate < 0) {
396 <                fprintf(stderr, "%s: need '%s' or '%s'\n", progname,
397 <                                vnam(OCTREE), vnam(SCENE));
398 <                exit(1);
395 >        if (vdef(PCMAP)) {
396 >                if (!*sskip2(vval(PCMAP),1)) {
397 >                        fprintf(stderr, "%s: '%s' missing # photons argument\n",
398 >                                        progname, vnam(PCMAP));
399 >                        quit(1);
400 >                }
401 >                atos(fntemp, sizeof(fntemp), vval(PCMAP));
402 >                pcmapname = savqstr(fntemp);
403 >                pcmapdate = fdate(pcmapname);
404          }
405 <        matdate = -1;
434 <        if (vdef(MATERIAL))
435 <                matdate = checklast(vval(MATERIAL));
405 >        matdate = checklast(vval(MATERIAL));
406   }      
407  
408  
409 < getoctcube(org, sizp)           /* get octree bounding cube */
410 < double  org[3], *sizp;
409 > static void
410 > getoctcube(             /* get octree bounding cube */
411 >        double  org[3],
412 >        double  *sizp
413 > )
414   {
442        extern FILE     *popen();
415          static double   oorg[3], osiz = 0.;
416 <        char    buf[MAXPATH+16];
416 >        double  min[3], max[3];
417 >        char    buf[1024];
418          FILE    *fp;
419 +        int     i;
420  
421          if (osiz <= FTINY) {
422 <                oconv();                /* does nothing if done already */
423 <                sprintf(buf, "getinfo -d < %s", vval(OCTREE));
424 <                if ((fp = popen(buf, "r")) == NULL)
425 <                        syserr("getinfo");
426 <                if (fscanf(fp, "%lf %lf %lf %lf", &oorg[0], &oorg[1],
427 <                                &oorg[2], &osiz) != 4) {
428 <                        fprintf(stderr,
422 >                if (!nprocs && fdate(oct1name) <
423 >                                (scenedate>illumdate?scenedate:illumdate)) {
424 >                                                        /* run getbbox */
425 >                        sprintf(buf, "getbbox -w -h %s",
426 >                                vdef(SCENE) ? vval(SCENE) : vval(ILLUM));
427 >                        if ((fp = popen(buf, "r")) == NULL)
428 >                                syserr("getbbox");
429 >                        if (fscanf(fp, "%lf %lf %lf %lf %lf %lf",
430 >                                        &min[0], &max[0], &min[1], &max[1],
431 >                                        &min[2], &max[2]) != 6) {
432 >                                fprintf(stderr,
433 >                        "%s: error reading bounding box from getbbox\n",
434 >                                                progname);
435 >                                quit(1);
436 >                        }
437 >                        for (i = 0; i < 3; i++)
438 >                                if (max[i] - min[i] > osiz)
439 >                                        osiz = max[i] - min[i];
440 >                        for (i = 0; i < 3; i++)
441 >                                oorg[i] = (max[i]+min[i]-osiz)*.5;
442 >                        pclose(fp);
443 >                } else {                                /* from octree */
444 >                        oconv();        /* does nothing if done already */
445 >                        sprintf(buf, "getinfo -d < %s", oct1name);
446 >                        if ((fp = popen(buf, "r")) == NULL)
447 >                                syserr("getinfo");
448 >                        if (fscanf(fp, "%lf %lf %lf %lf", &oorg[0], &oorg[1],
449 >                                        &oorg[2], &osiz) != 4) {
450 >                                fprintf(stderr,
451                          "%s: error reading bounding cube from getinfo\n",
452 <                                        progname);
453 <                        exit(1);
452 >                                                progname);
453 >                                quit(1);
454 >                        }
455 >                        pclose(fp);
456                  }
459                pclose(fp);
457          }
458          org[0] = oorg[0]; org[1] = oorg[1]; org[2] = oorg[2]; *sizp = osiz;
459   }
460  
461  
462 < setdefaults()                   /* set default values for unassigned var's */
462 > static void
463 > setdefaults(void)                       /* set default values for unassigned var's */
464   {
465 <        double  org[3], size;
465 >        double  org[3], lim[3], size;
466          char    buf[128];
467  
468          if (!vdef(ZONE)) {
# Line 474 | Line 472 | setdefaults()                  /* set default values for unassigned v
472                  vval(ZONE) = savqstr(buf);
473                  vdef(ZONE)++;
474          }
475 +        if (!vdef(EYESEP)) {
476 +                if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf",
477 +                                &org[0], &lim[0], &org[1], &lim[1],
478 +                                &org[2], &lim[2]) != 6)
479 +                        badvalue(ZONE);
480 +                sprintf(buf, "%f",
481 +                        0.01*(lim[0]-org[0]+lim[1]-org[1]+lim[2]-org[2]));
482 +                vval(EYESEP) = savqstr(buf);
483 +                vdef(EYESEP)++;
484 +        }
485          if (!vdef(INDIRECT)) {
486                  vval(INDIRECT) = "0";
487                  vdef(INDIRECT)++;
# Line 490 | Line 498 | setdefaults()                  /* set default values for unassigned v
498                  vval(PICTURE) = radname;
499                  vdef(PICTURE)++;
500          }
501 <        if (!vdef(VIEW)) {
502 <                vval(VIEW) = "X";
503 <                vdef(VIEW)++;
501 >        if (!vdef(VIEWS)) {
502 >                vval(VIEWS) = "X";
503 >                vdef(VIEWS)++;
504          }
505          if (!vdef(DETAIL)) {
506                  vval(DETAIL) = "M";
# Line 509 | Line 517 | setdefaults()                  /* set default values for unassigned v
517   }
518  
519  
520 < printvals()                     /* print variable values */
520 > static void
521 > oconv(void)                             /* run oconv and mkillum if necessary */
522   {
523 <        register int    i, j;
523 >        static char     illumtmp[] = "ilXXXXXX";
524 >        char    combuf[PATH_MAX], ocopts[64], mkopts[1024];
525  
526 <        for (i = 0; i < NVARS; i++)
527 <                for (j = 0; j < vdef(i); j++)
528 <                        printf("%s= %s\n", vnam(i), nvalue(vv+i, j));
529 <        fflush(stdout);
526 >        oconvopts(ocopts);              /* get options */
527 >        if (octreedate < scenedate) {   /* check date on original octree */
528 >                if (touchonly && octreedate)
529 >                        touch(vval(OCTREE));
530 >                else {                          /* build command */
531 >                        if (vdef(MATERIAL))
532 >                                sprintf(combuf, "%s%s %s %s > %s", c_oconv,
533 >                                                ocopts, vval(MATERIAL),
534 >                                                vval(SCENE), vval(OCTREE));
535 >                        else
536 >                                sprintf(combuf, "%s%s %s > %s", c_oconv, ocopts,
537 >                                                vval(SCENE), vval(OCTREE));
538 >                        
539 >                        if (runcom(combuf)) {           /* run it */
540 >                                fprintf(stderr,
541 >                                "%s: error generating octree\n\t%s removed\n",
542 >                                                progname, vval(OCTREE));
543 >                                unlink(vval(OCTREE));
544 >                                quit(1);
545 >                        }
546 >                }
547 >                octreedate = time((time_t *)NULL);
548 >                if (octreedate < scenedate)     /* in case clock is off */
549 >                        octreedate = scenedate;
550 >        }
551 >        if (oct1name == vval(OCTREE))           /* no mkillum? */
552 >                oct1date = octreedate > matdate ? octreedate : matdate;
553 >        if ((oct1date >= octreedate) & (oct1date >= matdate)
554 >                        & (oct1date >= illumdate))      /* all done */
555 >                return;
556 >                                                /* make octree0 */
557 >        if ((oct0date < scenedate) | (oct0date < illumdate)) {
558 >                if (touchonly && oct0date)
559 >                        touch(oct0name);
560 >                else {                          /* build command */
561 >                        if (octreedate)
562 >                                sprintf(combuf, "%s%s -i %s %s > %s", c_oconv,
563 >                                        ocopts, vval(OCTREE),
564 >                                        vval(ILLUM), oct0name);
565 >                        else if (vdef(MATERIAL))
566 >                                sprintf(combuf, "%s%s %s %s > %s", c_oconv,
567 >                                        ocopts, vval(MATERIAL),
568 >                                        vval(ILLUM), oct0name);
569 >                        else
570 >                                sprintf(combuf, "%s%s %s > %s", c_oconv,
571 >                                        ocopts, vval(ILLUM), oct0name);
572 >                        if (runcom(combuf)) {           /* run it */
573 >                                fprintf(stderr,
574 >                                "%s: error generating octree\n\t%s removed\n",
575 >                                                progname, oct0name);
576 >                                unlink(oct0name);
577 >                                quit(1);
578 >                        }
579 >                }
580 >                oct0date = time((time_t *)NULL);
581 >                if (oct0date < octreedate)      /* in case clock is off */
582 >                        oct0date = octreedate;
583 >                if (oct0date < illumdate)       /* ditto */
584 >                        oct0date = illumdate;
585 >                }
586 >        if (touchonly && oct1date)
587 >                touch(oct1name);
588 >        else {
589 >                mkillumopts(mkopts);            /* build mkillum command */
590 >                mktemp(illumtmp);
591 >                sprintf(combuf, "%s%s %s \"<\" %s > %s", c_mkillum, mkopts,
592 >                                oct0name, vval(ILLUM), illumtmp);
593 >                if (runcom(combuf)) {                   /* run it */
594 >                        fprintf(stderr, "%s: error running %s\n",
595 >                                        progname, c_mkillum);
596 >                        unlink(illumtmp);
597 >                        quit(1);
598 >                }
599 >                rmfile(oct0name);
600 >                                                /* make octree1 (frozen) */
601 >                if (octreedate)
602 >                        sprintf(combuf, "%s%s -f -i %s %s > %s", c_oconv,
603 >                                ocopts, vval(OCTREE), illumtmp, oct1name);
604 >                else if (vdef(MATERIAL))
605 >                        sprintf(combuf, "%s%s -f %s %s > %s", c_oconv,
606 >                                ocopts, vval(MATERIAL), illumtmp, oct1name);
607 >                else
608 >                        sprintf(combuf, "%s%s -f %s > %s", c_oconv, ocopts,
609 >                                illumtmp, oct1name);
610 >                if (runcom(combuf)) {           /* run it */
611 >                        fprintf(stderr,
612 >                                "%s: error generating octree\n\t%s removed\n",
613 >                                        progname, oct1name);
614 >                        unlink(oct1name);
615 >                        unlink(illumtmp);
616 >                        quit(1);
617 >                }
618 >                rmfile(illumtmp);
619 >        }
620 >        oct1date = time((time_t *)NULL);
621 >        if (oct1date < oct0date)        /* in case clock is off */
622 >                oct1date = oct0date;
623   }
624  
625  
626 < oconv()                         /* run oconv if necessary */
626 > static void
627 > mkpmap(void)                    /* run mkpmap if indicated */
628   {
629 <        char    combuf[512], ocopts[64];
630 <
631 <        if (octreedate >= scenedate)    /* check dates */
629 >        char    combuf[2048], *cp;
630 >        time_t  tnow;
631 >                                /* nothing to do? */
632 >        if ((pgmapname == NULL) | (pgmapdate >= oct1date) &&
633 >                        (pcmapname == NULL) | (pcmapdate >= oct1date))
634                  return;
635 <                                        /* build command */
636 <        oconvopts(ocopts);
637 <        if (vdef(MATERIAL))
638 <                sprintf(combuf, "oconv%s %s %s > %s", ocopts,
639 <                                vval(MATERIAL), vval(SCENE), vval(OCTREE));
640 <        else
641 <                sprintf(combuf, "oconv%s %s > %s", ocopts,
642 <                                vval(SCENE), vval(OCTREE));
643 <        
644 <        if (runcom(combuf)) {           /* run it */
645 <                fprintf(stderr, "%s: error generating octree\n\t%s removed\n",
646 <                                progname, vval(OCTREE));
647 <                unlink(vval(OCTREE));
648 <                exit(1);
635 >                                /* just update existing file dates? */
636 >        if (touchonly && (pgmapname == NULL) | (pgmapdate > 0) &&
637 >                        (pcmapname == NULL) | (pcmapdate > 0)) {
638 >                if (pgmapname != NULL)
639 >                        touch(pgmapname);
640 >                if (pcmapname != NULL)
641 >                        touch(pcmapname);
642 >        } else {                /* else need to (re)run pkpmap */
643 >                strcpy(combuf, c_mkpmap);
644 >                for (cp = combuf; *cp; cp++)
645 >                        ;
646 >                mkpmapopts(cp);
647 >                if (vdef(REPORT)) {
648 >                        char    errfile[256];
649 >                        int     n;
650 >                        double  minutes;
651 >                        n = sscanf(vval(REPORT), "%lf %s", &minutes, errfile);
652 >                        if (n == 2)
653 >                                sprintf(cp, " -t %d -e %s", (int)(minutes*60), errfile);
654 >                        else if (n == 1)
655 >                                sprintf(cp, " -t %d", (int)(minutes*60));
656 >                        else
657 >                                badvalue(REPORT);
658 >                }
659 >                if (pgmapname != NULL && pgmapdate < oct1date) {
660 >                        cp = addarg(cp, "-apg");
661 >                        addarg(cp, vval(PGMAP));
662 >                        cp = sskip(sskip(cp));  /* remove any bandwidth */
663 >                        *cp = '\0';
664 >                }
665 >                if (pcmapname != NULL && pcmapdate < oct1date) {
666 >                        cp = addarg(cp, "-apc");
667 >                        addarg(cp, vval(PCMAP));
668 >                        cp = sskip(sskip(cp));  /* remove any bandwidth */
669 >                        *cp = '\0';
670 >                }
671 >                cp = addarg(cp, oct1name);
672 >                if (runcom(combuf)) {
673 >                        fprintf(stderr, "%s: error running %s\n",
674 >                                        progname, c_mkpmap);
675 >                        if (pgmapname != NULL)
676 >                                unlink(pgmapname);
677 >                        if (pcmapname != NULL)
678 >                                unlink(pcmapname);
679 >                        quit(1);
680 >                }
681          }
682 <        octreedate = time(0);
682 >        tnow = time((time_t *)NULL);
683 >        if (pgmapname != NULL)
684 >                pgmapdate = tnow;
685 >        if (pcmapname != NULL)
686 >                pcmapdate = tnow;
687 >        oct1date = tnow;        /* trigger ambient file removal if needed */
688   }
689  
690  
691 < char *
692 < addarg(op, arg)                         /* add argument and advance pointer */
693 < register char   *op, *arg;
691 > static char *
692 > addarg(                         /* append argument and advance pointer */
693 > char    *op,
694 > char    *arg
695 > )
696   {
697 +        while (*op)
698 +                op++;
699          *op = ' ';
700 <        while (*++op = *arg++)
700 >        while ( (*++op = *arg++) )
701                  ;
702          return(op);
703   }
704  
705  
706 < oconvopts(oo)                           /* get oconv options */
707 < register char   *oo;
706 > static void
707 > oconvopts(                              /* get oconv options */
708 >        char    *oo
709 > )
710   {
711          /* BEWARE:  This may be called via setdefaults(), so no assumptions */
712  
713          *oo = '\0';
714 <        if (vdef(OCONV))
715 <                addarg(oo, vval(OCONV));
714 >        if (!vdef(OCONV))
715 >                return;
716 >        if (vval(OCONV)[0] != '-') {
717 >                atos(c_oconv, sizeof(c_oconv), vval(OCONV));
718 >                oo = addarg(oo, sskip2(vval(OCONV), 1));
719 >        } else
720 >                oo = addarg(oo, vval(OCONV));
721   }
722  
723  
724 < checkambfile()                  /* check date on ambient file */
724 > static void
725 > mkillumopts(                            /* get mkillum options */
726 >        char    *mo
727 > )
728   {
729 <        long    afdate;
729 >        /* BEWARE:  This may be called via setdefaults(), so no assumptions */
730  
731 +        if (nprocs > 1)
732 +                sprintf(mo, " -n %d", nprocs);
733 +        else
734 +                *mo = '\0';
735 +        if (!vdef(MKILLUM))
736 +                return;
737 +        if (vval(MKILLUM)[0] != '-') {
738 +                atos(c_mkillum, sizeof(c_mkillum), vval(MKILLUM));
739 +                mo = addarg(mo, sskip2(vval(MKILLUM), 1));
740 +        } else
741 +                mo = addarg(mo, vval(MKILLUM));
742 + }
743 +
744 +
745 + static void
746 + mkpmapopts(                             /* get mkpmap options */
747 +        char    *mo
748 + )
749 + {
750 +        /* BEWARE:  This may be called via setdefaults(), so no assumptions */
751 +
752 +        *mo = '\0';
753 +        if (!vdef(MKPMAP))
754 +                return;
755 +        if (vval(MKPMAP)[0] != '-') {
756 +                atos(c_mkpmap, sizeof(c_mkpmap), vval(MKPMAP));
757 +                mo = addarg(mo, sskip2(vval(MKPMAP), 1));
758 +        } else
759 +                mo = addarg(mo, vval(MKPMAP));
760 + }
761 +
762 +
763 + static void
764 + checkambfile(void)                      /* check date on ambient file */
765 + {
766 +        time_t  afdate;
767 +
768          if (!vdef(AMBFILE))
769                  return;
770 <        if ((afdate = fdate(vval(AMBFILE))) < 0)
770 >        if (!(afdate = fdate(vval(AMBFILE))))
771                  return;
772 <        if (octreedate > afdate | matdate > afdate)
773 <                rmfile(vval(AMBFILE));
772 >        if (oct1date > afdate) {
773 >                if (touchonly)
774 >                        touch(vval(AMBFILE));
775 >                else
776 >                        rmfile(vval(AMBFILE));
777 >        }
778   }
779  
780  
781 < double
782 < ambval()                                /* compute ambient value */
781 > static double
782 > ambval(void)                            /* compute ambient value */
783   {
784          if (vdef(EXPOSURE)) {
587                if (!isflt(vval(EXPOSURE)))
588                        badvalue(EXPOSURE);
785                  if (vval(EXPOSURE)[0] == '+' || vval(EXPOSURE)[0] == '-')
786 <                        return(.5/pow(2.,atof(vval(EXPOSURE))));
787 <                return(.5/atof(vval(EXPOSURE)));
786 >                        return(.5/pow(2.,vflt(EXPOSURE)));
787 >                return(.5/vflt(EXPOSURE));
788          }
789          if (vlet(ZONE) == 'E')
790                  return(10.);
791          if (vlet(ZONE) == 'I')
792                  return(.01);
793          badvalue(ZONE);
794 +        return 0; /* pro forma return */
795   }
796  
797  
798 < lowqopts(op)                            /* low quality rendering options */
799 < register char   *op;
798 > static void
799 > renderopts(                     /* set rendering options */
800 >        char    *op,
801 >        char    *po
802 > )
803   {
804 +        char    pmapf[256], *bw;
805 +
806 +        switch(vscale(QUALITY)) {
807 +        case LOW:
808 +                lowqopts(op, po);
809 +                break;
810 +        case MEDIUM:
811 +                medqopts(op, po);
812 +                break;
813 +        case HIGH:
814 +                hiqopts(op, po);
815 +                break;
816 +        }
817 +        if (vdef(PGMAP)) {
818 +                bw = sskip2(vval(PGMAP), 2);
819 +                atos(pmapf, sizeof(pmapf), vval(PGMAP));
820 +                op = addarg(addarg(op, "-ap"), pmapf);
821 +                if (atoi(bw) > 0) op = addarg(op, bw);
822 +        }
823 +        if (vdef(PCMAP)) {
824 +                bw = sskip2(vval(PCMAP), 2);
825 +                atos(pmapf, sizeof(pmapf), vval(PCMAP));
826 +                op = addarg(addarg(op, "-ap"), pmapf);
827 +                if (atoi(bw) > 0) op = addarg(op, bw);
828 +        }
829 +        if (vdef(RENDER))
830 +                op = addarg(op, vval(RENDER));
831 +        if (rvdevice != NULL) {
832 +                if (vdef(RVU))
833 +                        if (vval(RVU)[0] != '-') {
834 +                                atos(c_rvu, sizeof(c_rvu), vval(RVU));
835 +                                po = addarg(po, sskip2(vval(RVU), 1));
836 +                        } else
837 +                                po = addarg(po, vval(RVU));
838 +        } else {
839 +                if (vdef(RPICT))
840 +                        if (vval(RPICT)[0] != '-') {
841 +                                atos(c_rpict, sizeof(c_rpict), vval(RPICT));
842 +                                po = addarg(po, sskip2(vval(RPICT), 1));
843 +                        } else
844 +                                po = addarg(po, vval(RPICT));
845 +        }
846 + }
847 +
848 +
849 + static void
850 + lowqopts(                       /* low quality rendering options */
851 +        char    *op,
852 +        char    *po
853 + )
854 + {
855          double  d, org[3], siz[3];
856  
857          *op = '\0';
858 +        *po = '\0';
859          if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
860                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
861                  badvalue(ZONE);
862          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
863 +        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
864 +                badvalue(ZONE);
865          getoctcube(org, &d);
866          d *= 3./(siz[0]+siz[1]+siz[2]);
867          switch (vscale(DETAIL)) {
868          case LOW:
869 <                op = addarg(op, "-ps 16 -dp 16");
870 <                sprintf(op, " -ar %d", (int)(4*d));
869 >                po = addarg(po, "-ps 16");
870 >                op = addarg(op, "-dp 64");
871 >                sprintf(op, " -ar %d", (int)(8*d));
872                  op += strlen(op);
873                  break;
874          case MEDIUM:
875 <                op = addarg(op, "-ps 8 -dp 32");
876 <                sprintf(op, " -ar %d", (int)(8*d));
875 >                po = addarg(po, "-ps 8");
876 >                op = addarg(op, "-dp 128");
877 >                sprintf(op, " -ar %d", (int)(16*d));
878                  op += strlen(op);
879                  break;
880          case HIGH:
881 <                op = addarg(op, "-ps 4 -dp 64");
882 <                sprintf(op, " -ar %d", (int)(16*d));
881 >                po = addarg(po, "-ps 4");
882 >                op = addarg(op, "-dp 256");
883 >                sprintf(op, " -ar %d", (int)(32*d));
884                  op += strlen(op);
885                  break;
886          }
887 <        op = addarg(op, "-pt .16");
887 >        po = addarg(po, "-pt .16");
888          if (vbool(PENUMBRAS))
889                  op = addarg(op, "-ds .4");
890          else
891                  op = addarg(op, "-ds 0");
892 <        op = addarg(op, "-dt .2 -dc .25 -dr 0 -sj 0 -st .5");
892 >        op = addarg(op, "-dt .2 -dc .25 -dr 0 -ss 0 -st .5");
893          if (vdef(AMBFILE)) {
894                  sprintf(op, " -af %s", vval(AMBFILE));
895                  op += strlen(op);
# Line 640 | Line 897 | register char  *op;
897                  overture = 0;
898          switch (vscale(VARIABILITY)) {
899          case LOW:
900 <                op = addarg(op, "-aa .4 -ad 64");
900 >                op = addarg(op, "-aa .3 -ad 256");
901                  break;
902          case MEDIUM:
903 <                op = addarg(op, "-aa .3 -ad 128");
903 >                op = addarg(op, "-aa .25 -ad 512");
904                  break;
905          case HIGH:
906 <                op = addarg(op, "-aa .25 -ad 256");
906 >                op = addarg(op, "-aa .2 -ad 1024");
907                  break;
908          }
909          op = addarg(op, "-as 0");
910          d = ambval();
911          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
912          op += strlen(op);
913 <        op = addarg(op, "-lr 3 -lw .02");
657 <        if (vdef(RENDER))
658 <                op = addarg(op, vval(RENDER));
913 >        op = addarg(op, "-lr 6 -lw .003");
914   }
915  
916  
917 < medqopts(op)                            /* medium quality rendering options */
918 < register char   *op;
917 > static void
918 > medqopts(                       /* medium quality rendering options */
919 >        char    *op,
920 >        char    *po
921 > )
922   {
923 <        double  d, org[3], siz[3];
923 >        double  d, org[3], siz[3], asz;
924  
925          *op = '\0';
926 +        *po = '\0';
927          if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
928                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
929                  badvalue(ZONE);
930          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
931 +        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
932 +                badvalue(ZONE);
933          getoctcube(org, &d);
934 <        d *= 3./(siz[0]+siz[1]+siz[2]);
934 >        asz = (siz[0]+siz[1]+siz[2])/3.;
935 >        d /= asz;
936          switch (vscale(DETAIL)) {
937          case LOW:
938 <                op = addarg(op, vbool(PENUMBRAS) ? "-ps 4" : "-ps 8");
939 <                op = addarg(op, "-dp 64");
940 <                sprintf(op, " -ar %d", (int)(8*d));
938 >                po = addarg(po, vbool(PENUMBRAS) ? "-ps 4" : "-ps 8");
939 >                op = addarg(op, "-dp 256");
940 >                sprintf(op, " -ar %d", (int)(16*d));
941                  op += strlen(op);
942 +                sprintf(op, " -ms %.2g", asz/20.);
943 +                op += strlen(op);
944                  break;
945          case MEDIUM:
946 <                op = addarg(op, vbool(PENUMBRAS) ? "-ps 3" : "-ps 6");
947 <                op = addarg(op, "-dp 128");
948 <                sprintf(op, " -ar %d", (int)(16*d));
946 >                po = addarg(po, vbool(PENUMBRAS) ? "-ps 3" : "-ps 6");
947 >                op = addarg(op, "-dp 512");
948 >                sprintf(op, " -ar %d", (int)(32*d));
949                  op += strlen(op);
950 +                sprintf(op, " -ms %.2g", asz/40.);
951 +                op += strlen(op);
952                  break;
953          case HIGH:
954 <                op = addarg(op, vbool(PENUMBRAS) ? "-ps 2" : "-ps 4");
955 <                op = addarg(op, "-dp 256");
956 <                sprintf(op, " -ar %d", (int)(32*d));
954 >                po = addarg(po, vbool(PENUMBRAS) ? "-ps 2" : "-ps 4");
955 >                op = addarg(op, "-dp 1024");
956 >                sprintf(op, " -ar %d", (int)(64*d));
957                  op += strlen(op);
958 +                sprintf(op, " -ms %.2g", asz/80.);
959 +                op += strlen(op);
960                  break;
961          }
962 <        op = addarg(op, "-pt .08");
962 >        po = addarg(po, "-pt .08");
963          if (vbool(PENUMBRAS))
964 <                op = addarg(op, "-ds .2 -dj .35");
964 >                op = addarg(op, "-ds .2 -dj .9");
965          else
966                  op = addarg(op, "-ds .3");
967 <        op = addarg(op, "-dt .1 -dc .5 -dr 1 -sj .7 -st .1");
968 <        if (overture = vint(INDIRECT)) {
967 >        op = addarg(op, "-dt .1 -dc .5 -dr 1 -ss 1 -st .1");
968 >        if ( (overture = vint(INDIRECT)) ) {
969                  sprintf(op, " -ab %d", overture);
970                  op += strlen(op);
971          }
# Line 708 | Line 976 | register char  *op;
976                  overture = 0;
977          switch (vscale(VARIABILITY)) {
978          case LOW:
979 <                op = addarg(op, "-aa .25 -ad 196 -as 0");
979 >                op = addarg(op, "-aa .2 -ad 329 -as 42");
980                  break;
981          case MEDIUM:
982 <                op = addarg(op, "-aa .2 -ad 400 -as 64");
982 >                op = addarg(op, "-aa .15 -ad 800 -as 128");
983                  break;
984          case HIGH:
985 <                op = addarg(op, "-aa .15 -ad 768 -as 196");
985 >                op = addarg(op, "-aa .1 -ad 1536 -as 392");
986                  break;
987          }
988          d = ambval();
989          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
990          op += strlen(op);
991 <        op = addarg(op, "-lr 6 -lw .002");
724 <        if (vdef(RENDER))
725 <                op = addarg(op, vval(RENDER));
991 >        op = addarg(op, "-lr 8 -lw 1e-4");
992   }
993  
994  
995 < hiqopts(op)                             /* high quality rendering options */
996 < register char   *op;
995 > static void
996 > hiqopts(                                /* high quality rendering options */
997 >        char    *op,
998 >        char    *po
999 > )
1000   {
1001 <        double  d, org[3], siz[3];
1001 >        double  d, org[3], siz[3], asz;
1002  
1003          *op = '\0';
1004 +        *po = '\0';
1005          if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
1006                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
1007                  badvalue(ZONE);
1008          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
1009 +        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
1010 +                badvalue(ZONE);
1011          getoctcube(org, &d);
1012 <        d *= 3./(siz[0]+siz[1]+siz[2]);
1012 >        asz = (siz[0]+siz[1]+siz[2])/3.;
1013 >        d /= asz;
1014          switch (vscale(DETAIL)) {
1015          case LOW:
1016 <                op = addarg(op, vbool(PENUMBRAS) ? "-ps 1" : "-ps 8");
1017 <                op = addarg(op, "-dp 256");
1018 <                sprintf(op, " -ar %d", (int)(16*d));
1016 >                po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 8");
1017 >                op = addarg(op, "-dp 1024");
1018 >                sprintf(op, " -ar %d", (int)(32*d));
1019                  op += strlen(op);
1020 +                sprintf(op, " -ms %.2g", asz/40.);
1021 +                op += strlen(op);
1022                  break;
1023          case MEDIUM:
1024 <                op = addarg(op, vbool(PENUMBRAS) ? "-ps 1" : "-ps 5");
1025 <                op = addarg(op, "-dp 512");
1026 <                sprintf(op, " -ar %d", (int)(32*d));
1024 >                po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 5");
1025 >                op = addarg(op, "-dp 2048");
1026 >                sprintf(op, " -ar %d", (int)(64*d));
1027                  op += strlen(op);
1028 +                sprintf(op, " -ms %.2g", asz/80.);
1029 +                op += strlen(op);
1030                  break;
1031          case HIGH:
1032 <                op = addarg(op, vbool(PENUMBRAS) ? "-ps 1" : "-ps 3");
1033 <                op = addarg(op, "-dp 1024");
1034 <                sprintf(op, " -ar %d", (int)(64*d));
1032 >                po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 3");
1033 >                op = addarg(op, "-dp 4096");
1034 >                sprintf(op, " -ar %d", (int)(128*d));
1035                  op += strlen(op);
1036 +                sprintf(op, " -ms %.2g", asz/160.);
1037 +                op += strlen(op);
1038                  break;
1039          }
1040 <        op = addarg(op, "-pt .04");
1040 >        po = addarg(po, "-pt .04");
1041          if (vbool(PENUMBRAS))
1042 <                op = addarg(op, "-ds .1 -dj .7");
1042 >                op = addarg(op, "-ds .1 -dj .9");
1043          else
1044                  op = addarg(op, "-ds .2");
1045 <        op = addarg(op, "-dt .05 -dc .75 -dr 3 -sj 1 -st .01");
1045 >        op = addarg(op, "-dt .05 -dc .75 -dr 3 -ss 16 -st .01");
1046          sprintf(op, " -ab %d", overture=vint(INDIRECT)+1);
1047          op += strlen(op);
1048          if (vdef(AMBFILE)) {
# Line 773 | Line 1052 | register char  *op;
1052                  overture = 0;
1053          switch (vscale(VARIABILITY)) {
1054          case LOW:
1055 <                op = addarg(op, "-aa .15 -ad 256 -as 0");
1055 >                op = addarg(op, "-aa .125 -ad 512 -as 64");
1056                  break;
1057          case MEDIUM:
1058 <                op = addarg(op, "-aa .125 -ad 512 -as 256");
1058 >                op = addarg(op, "-aa .1 -ad 1536 -as 768");
1059                  break;
1060          case HIGH:
1061 <                op = addarg(op, "-aa .08 -ad 1024 -as 512");
1061 >                op = addarg(op, "-aa .075 -ad 4096 -as 2048");
1062                  break;
1063          }
1064          d = ambval();
1065          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
1066          op += strlen(op);
1067 <        op = addarg(op, "-lr 12 -lw .0005");
789 <        if (vdef(RENDER))
790 <                op = addarg(op, vval(RENDER));
1067 >        op = addarg(op, "-lr 12 -lw 1e-5");
1068   }
1069  
1070  
1071 < xferopts(ro)                            /* transfer options if indicated */
1072 < char    *ro;
1071 > #ifdef _WIN32
1072 > static void
1073 > setenv(                 /* set an environment variable */
1074 >        char    *vname,
1075 >        char    *value
1076 > )
1077   {
1078 +        char    *evp;
1079 +
1080 +        evp = bmalloc(strlen(vname)+strlen(value)+2);
1081 +        if (evp == NULL)
1082 +                syserr(progname);
1083 +        sprintf(evp, "%s=%s", vname, value);
1084 +        if (putenv(evp) != 0) {
1085 +                fprintf(stderr, "%s: out of environment space\n", progname);
1086 +                quit(1);
1087 +        }
1088 +        if (!silent)
1089 +                printf("set %s\n", evp);
1090 + }
1091 + #endif
1092 +
1093 +
1094 + static void
1095 + xferopts(                               /* transfer options if indicated */
1096 +        char    *ro
1097 + )
1098 + {
1099          int     fd, n;
1100 <        register char   *cp;
1100 >        char    *cp;
1101          
1102          n = strlen(ro);
1103          if (n < 2)
1104                  return;
1105          if (vdef(OPTFILE)) {
1106                  for (cp = ro; cp[1]; cp++)
1107 <                        if (isspace(cp[1]) && cp[2] == '-' && isalpha(cp[3]))
1107 >                        if (isspace(cp[1]) && (cp[2] == '@' ||
1108 >                                        (cp[2] == '-' && isalpha(cp[3]))))
1109                                  *cp = '\n';
1110                          else
1111                                  *cp = cp[1];
# Line 812 | Line 1115 | char   *ro;
1115                          syserr(vval(OPTFILE));
1116                  sprintf(ro, " @%s", vval(OPTFILE));
1117          }
1118 < #ifdef MSDOS
1118 > #ifdef _WIN32
1119          else if (n > 50) {
1120                  setenv("ROPT", ro+1);
1121                  strcpy(ro, " $ROPT");
# Line 821 | Line 1124 | char   *ro;
1124   }
1125  
1126  
1127 < pfiltopts(po)                           /* get pfilt options */
1128 < register char   *po;
1127 > static void
1128 > pfiltopts(                              /* get pfilt options */
1129 >        char    *po
1130 > )
1131   {
1132          *po = '\0';
1133          if (vdef(EXPOSURE)) {
# Line 831 | Line 1136 | register char  *po;
1136          }
1137          switch (vscale(QUALITY)) {
1138          case MEDIUM:
1139 <                po = addarg(po, "-r 1");
1139 >                po = addarg(po, "-r .6");
1140                  break;
1141          case HIGH:
1142                  po = addarg(po, "-m .25");
1143                  break;
1144          }
1145          if (vdef(PFILT))
1146 <                po = addarg(po, vval(PFILT));
1146 >                if (vval(PFILT)[0] != '-') {
1147 >                        atos(c_pfilt, sizeof(c_pfilt), vval(PFILT));
1148 >                        po = addarg(po, sskip2(vval(PFILT), 1));
1149 >                } else
1150 >                        po = addarg(po, vval(PFILT));
1151   }
1152  
1153  
1154 < matchword(s1, s2)                       /* match white-delimited words */
1155 < register char   *s1, *s2;
1154 > static int
1155 > matchword(                      /* match white-delimited words */
1156 >        char    *s1,
1157 >        char    *s2
1158 > )
1159   {
1160          while (isspace(*s1)) s1++;
1161          while (isspace(*s2)) s2++;
# Line 854 | Line 1166 | register char  *s1, *s2;
1166   }
1167  
1168  
1169 < char *
1170 < specview(vs)                            /* get proper view spec from vs */
1171 < register char   *vs;
1169 > static char *
1170 > specview(                               /* get proper view spec from vs */
1171 >        char    *vs
1172 > )
1173   {
1174          static char     vup[7][12] = {"-vu 0 0 -1","-vu 0 -1 0","-vu -1 0 0",
1175                          "-vu 0 0 1", "-vu 1 0 0","-vu 0 1 0","-vu 0 0 1"};
1176          static char     viewopts[128];
1177 <        register char   *cp;
1177 >        char    *cp;
1178          int     xpos, ypos, zpos, viewtype, upax;
1179 <        register int    i;
1179 >        int     i;
1180          double  cent[3], dim[3], mult, d;
1181  
1182          if (vs == NULL || *vs == '-')
# Line 874 | Line 1187 | register char  *vs;
1187                          upax = 1-'X'+UPPER(vval(UP)[1]);
1188                  else
1189                          upax = 1-'X'+vlet(UP);
1190 <                if (upax < 1 | upax > 3)
1190 >                if ((upax < 1) | (upax > 3))
1191                          badvalue(UP);
1192                  if (vval(UP)[0] == '-')
1193                          upax = -upax;
# Line 896 | Line 1209 | register char  *vs;
1209          } else if (*vs == 'z') {
1210                  zpos = -1; vs++;
1211          }
1212 <        viewtype = 'v';
1213 <        if (*vs == 'v' | *vs == 'l' | *vs == 'a' | *vs == 'h')
1212 >        switch (*vs) {
1213 >        case VT_PER:
1214 >        case VT_PAR:
1215 >        case VT_ANG:
1216 >        case VT_HEM:
1217 >        case VT_PLS:
1218 >        case VT_CYL:
1219                  viewtype = *vs++;
1220 +                break;
1221 +        default:
1222 +                viewtype = VT_PER;
1223 +                break;
1224 +        }
1225          cp = viewopts;
1226          if ((!*vs || isspace(*vs)) && (xpos|ypos|zpos)) {       /* got one! */
1227                  *cp++ = '-'; *cp++ = 'v'; *cp++ = 't'; *cp++ = viewtype;
# Line 908 | Line 1231 | register char  *vs;
1231                          badvalue(ZONE);
1232                  for (i = 0; i < 3; i++) {
1233                          dim[i] -= cent[i];
1234 +                        if (dim[i] <= FTINY)
1235 +                                badvalue(ZONE);
1236                          cent[i] += .5*dim[i];
1237                  }
1238                  mult = vlet(ZONE)=='E' ? 2. : .45 ;
1239 <                sprintf(cp, " -vp %.2g %.2g %.2g -vd %.2g %.2g %.2g",
1239 >                sprintf(cp, " -vp %.3g %.3g %.3g -vd %.3g %.3g %.3g",
1240                                  cent[0]+xpos*mult*dim[0],
1241                                  cent[1]+ypos*mult*dim[1],
1242                                  cent[2]+zpos*mult*dim[2],
# Line 938 | Line 1263 | register char  *vs;
1263                  }
1264                  cp = addarg(cp, vup[upax+3]);
1265                  switch (viewtype) {
1266 <                case 'v':
1266 >                case VT_PER:
1267                          cp = addarg(cp, "-vh 45 -vv 45");
1268                          break;
1269 <                case 'l':
1269 >                case VT_PAR:
1270                          d = sqrt(dim[0]*dim[0]+dim[1]*dim[1]+dim[2]*dim[2]);
1271 <                        sprintf(cp, " -vh %.2g -vv %.2g", d, d);
1271 >                        sprintf(cp, " -vh %.3g -vv %.3g", d, d);
1272                          cp += strlen(cp);
1273                          break;
1274 <                case 'a':
1275 <                case 'h':
1274 >                case VT_ANG:
1275 >                case VT_HEM:
1276 >                case VT_PLS:
1277                          cp = addarg(cp, "-vh 180 -vv 180");
1278                          break;
1279 +                case VT_CYL:
1280 +                        cp = addarg(cp, "-vh 180 -vv 90");
1281 +                        break;
1282                  }
1283          } else {
1284                  while (!isspace(*vs))           /* else skip id */
1285                          if (!*vs++)
1286                                  return(NULL);
1287 <                if (upax) {                     /* specify up vector */
1287 >                if (upax) {                     /* prepend up vector */
1288                          strcpy(cp, vup[upax+3]);
1289                          cp += strlen(cp);
1290                  }
1291          }
1292 <        strcpy(cp, vs);                 /* append any additional options */
1293 < #ifdef MSDOS
1292 >        if (cp == viewopts)             /* append any additional options */
1293 >                vs++;           /* skip prefixed space if unneeded */
1294 >        strcpy(cp, vs);
1295 > #ifdef _WIN32
1296          if (strlen(viewopts) > 40) {
1297                  setenv("VIEW", viewopts);
1298                  return("$VIEW");
# Line 971 | Line 1302 | register char  *vs;
1302   }
1303  
1304  
1305 < char *
1306 < getview(n, vn)                          /* get view n, or NULL if none */
1307 < int     n;
1308 < char    *vn;            /* returned view name */
1305 > static char *
1306 > getview(                                /* get view n, or NULL if none */
1307 >        int     n,
1308 >        char    *vn             /* returned view name */
1309 > )
1310   {
1311 <        register char   *mv;
1311 >        char    *mv;
1312  
1313          if (viewselect != NULL) {               /* command-line selected */
1314                  if (n)                          /* only do one */
1315                          return(NULL);
1316 +                                        
1317 +                if (isint(viewselect)) {        /* view number? */
1318 +                        n = atoi(viewselect)-1;
1319 +                        goto numview;
1320 +                }
1321                  if (viewselect[0] == '-') {     /* already specified */
1322 <                        if (vn != NULL) *vn = '\0';
1322 >                        if (vn != NULL)
1323 >                                strcpy(vn, "0");
1324                          return(viewselect);
1325                  }
1326                  if (vn != NULL) {
# Line 991 | Line 1329 | char   *vn;            /* returned view name */
1329                                  ;
1330                          *vn = '\0';
1331                  }
994                                                /* view number? */
995                if (isint(viewselect))
996                        return(specview(nvalue(vv+VIEW, atoi(viewselect)-1)));
1332                                                  /* check list */
1333 <                while ((mv = nvalue(vv+VIEW, n++)) != NULL)
1333 >                while ((mv = nvalue(VIEWS, n++)) != NULL)
1334                          if (matchword(viewselect, mv))
1335                                  return(specview(mv));
1336 +
1337                  return(specview(viewselect));   /* standard view? */
1338          }
1339 <        mv = nvalue(vv+VIEW, n);                /* use view n */
1340 <        if (vn != NULL & mv != NULL) {
1341 <                register char   *mv2 = mv;
1342 <                if (*mv2 != '-')
1339 > numview:
1340 >        mv = nvalue(VIEWS, n);          /* use view n */
1341 >        if ((vn != NULL) & (mv != NULL))
1342 >                if (*mv != '-') {
1343 >                        char    *mv2 = mv;
1344                          while (*mv2 && !isspace(*mv2))
1345                                  *vn++ = *mv2++;
1346 <                *vn = '\0';
1347 <        }
1346 >                        *vn = '\0';
1347 >                } else
1348 >                        sprintf(vn, "%d", n+1);
1349 >
1350          return(specview(mv));
1351   }
1352  
1353  
1354 < printview(vopts)                        /* print out selected view */
1355 < register char   *vopts;
1354 > static int
1355 > myprintview(                    /* print out selected view */
1356 >        char    *vopts,
1357 >        FILE    *fp
1358 > )
1359   {
1360 <        extern char     *atos();
1361 <        char    buf[256];
1362 <        FILE    *fp;
1363 <        register char   *cp;
1364 <
1360 >        VIEW    vwr;
1361 >        char    buf[128];
1362 >        char    *cp;
1363 > #ifdef _WIN32
1364 > /* XXX Should we allow something like this for all platforms? */
1365 > /* XXX Or is it still required at all? */
1366 > again:
1367 > #endif
1368          if (vopts == NULL)
1369                  return(-1);
1370 <        fputs("VIEW=", stdout);
1371 <        do {
1372 <                if (matchword(vopts, "-vf")) {          /* expand view file */
1373 <                        vopts = sskip(vopts);
1374 <                        if (!*atos(buf, sizeof(buf), vopts))
1375 <                                return(-1);
1376 <                        if ((fp = fopen(buf, "r")) == NULL)
1377 <                                return(-1);
1378 <                        for (buf[sizeof(buf)-2] = '\n';
1379 <                                        fgets(buf, sizeof(buf), fp) != NULL &&
1380 <                                                buf[0] != '\n';
1381 <                                        buf[sizeof(buf)-2] = '\n') {
1382 <                                if (buf[sizeof(buf)-2] != '\n') {
1383 <                                        ungetc(buf[sizeof(buf)-2], fp);
1384 <                                        buf[sizeof(buf)-2] = '\0';
1385 <                                }
1041 <                                if (matchword(buf, "VIEW=") ||
1042 <                                                matchword(buf, "rview")) {
1043 <                                        for (cp = sskip(buf); *cp && *cp != '\n'; cp++)
1044 <                                                putchar(*cp);
1045 <                                }
1046 <                        }
1047 <                        fclose(fp);
1048 <                        vopts = sskip(vopts);
1049 <                } else {
1050 <                        while (isspace(*vopts))
1051 <                                vopts++;
1052 <                        putchar(' ');
1053 <                        while (*vopts && !isspace(*vopts))
1054 <                                putchar(*vopts++);
1055 <                }
1056 <        } while (*vopts++);
1057 <        putchar('\n');
1370 > #ifdef _WIN32
1371 >        if (vopts[0] == '$') {
1372 >                vopts = getenv(vopts+1);
1373 >                goto again;
1374 >        }
1375 > #endif
1376 >        vwr = stdview;
1377 >        sscanview(&vwr, cp=vopts);              /* set initial options */
1378 >        while ((cp = strstr(cp, "-vf ")) != NULL &&
1379 >                        *atos(buf, sizeof(buf), cp += 4)) {
1380 >                viewfile(buf, &vwr, NULL);      /* load -vf file */
1381 >                sscanview(&vwr, cp);            /* reset tail */
1382 >        }
1383 >        fputs(VIEWSTR, fp);
1384 >        fprintview(&vwr, fp);                   /* print full spec. */
1385 >        fputc('\n', fp);
1386          return(0);
1387   }
1388  
1389  
1390 < rview(opts)                             /* run rview with first view */
1391 < char    *opts;
1390 > static void
1391 > rvu(                            /* run rvu with first view */
1392 >        char    *opts,
1393 >        char    *po
1394 > )
1395   {
1396          char    *vw;
1397 <        char    combuf[512];
1397 >        char    combuf[PATH_MAX];
1398                                          /* build command */
1399 <        if ((vw = getview(0, NULL)) == NULL)
1399 >        if (touchonly || (vw = getview(0, NULL)) == NULL)
1400                  return;
1401          if (sayview)
1402 <                printview(vw);
1403 <        sprintf(combuf, "rview %s%s -R %s ", vw, opts, rifname);
1402 >                myprintview(vw, stdout);
1403 >        sprintf(combuf, "%s %s%s%s -R %s ", c_rvu, vw, opts, po, rifname);
1404 >        if (nprocs > 1)
1405 >                sprintf(combuf+strlen(combuf), "-n %d ", nprocs);
1406          if (rvdevice != NULL)
1407                  sprintf(combuf+strlen(combuf), "-o %s ", rvdevice);
1408 <        strcat(combuf, vval(OCTREE));
1408 >        if (vdef(EXPOSURE))
1409 >                sprintf(combuf+strlen(combuf), "-pe %s ", vval(EXPOSURE));
1410 >        strcat(combuf, oct1name);
1411          if (runcom(combuf)) {           /* run it */
1412 <                fprintf(stderr, "%s: error running rview\n", progname);
1413 <                exit(1);
1412 >                fprintf(stderr, "%s: error running %s\n", progname, c_rvu);
1413 >                quit(1);
1414          }
1415   }
1416  
1417  
1418 < rpict(opts)                             /* run rpict and pfilt for each view */
1419 < char    *opts;
1418 > static int
1419 > syncf_done(                     /* check if an rpiece sync file is complete */
1420 >        char *sfname
1421 > )
1422   {
1423 <        char    combuf[1024];
1424 <        char    rawfile[MAXPATH], picfile[MAXPATH], rep[MAXPATH+16], res[32];
1423 >        FILE    *fp = fopen(sfname, "r");
1424 >        int     todo = 1;
1425 >        int     x, y;
1426 >
1427 >        if (fp == NULL)
1428 >                return(0);
1429 >        if (fscanf(fp, "%d %d", &x, &y) != 2)
1430 >                goto checked;
1431 >        todo = x*y;             /* total number of tiles */
1432 >        if (fscanf(fp, "%d %d", &x, &y) != 2 || (x != 0) | (y != 0))
1433 >                goto checked;
1434 >                                /* XXX assume no redundant tiles */
1435 >        while (fscanf(fp, "%d %d", &x, &y) == 2)
1436 >                if (!--todo)
1437 >                        break;
1438 > checked:
1439 >        fclose(fp);
1440 >        return(!todo);
1441 > }
1442 >
1443 >
1444 > static void
1445 > rpict(                          /* run rpict and pfilt for each view */
1446 >        char    *opts,
1447 >        char    *po
1448 > )
1449 > {
1450 > #define do_rpiece       (sfile[0]!='\0')
1451 >        char    combuf[5*PATH_MAX+512];
1452 >        char    rawfile[PATH_MAX], picfile[PATH_MAX];
1453 >        char    zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32];
1454 >        char    rppopt[32], sfile[PATH_MAX], *pfile = NULL;
1455          char    pfopts[128];
1456          char    vs[32], *vw;
1457          int     vn, mult;
1458 <        long    lastdate;
1458 >        FILE    *fp;
1459 >        time_t  rfdt, pfdt;
1460 >        int     xres, yres;
1461 >        double  aspect;
1462 >        int     n;
1463                                          /* get pfilt options */
1464          pfiltopts(pfopts);
1465                                          /* get resolution, reporting */
1466 <        mult = vscale(QUALITY)+1;
1467 <        {
1468 <                int     xres, yres;
1469 <                double  aspect;
1470 <                int     n;
1471 <                n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect);
1472 <                if (n == 3)
1473 <                        sprintf(res, "-x %d -y %d -pa %.3f",
1474 <                                        mult*xres, mult*yres, aspect);
1475 <                else if (n) {
1105 <                        if (n == 1) yres = xres;
1106 <                        sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1107 <                } else
1108 <                        badvalue(RESOLUTION);
1466 >        switch (vscale(QUALITY)) {
1467 >        case LOW:
1468 >                mult = 1;
1469 >                break;
1470 >        case MEDIUM:
1471 >                mult = 2;
1472 >                break;
1473 >        case HIGH:
1474 >                mult = 3;
1475 >                break;
1476          }
1477 +        n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect);
1478 +        if (n == 3)
1479 +                sprintf(res, "-x %d -y %d -pa %.3f",
1480 +                                mult*xres, mult*yres, aspect);
1481 +        else if (n) {
1482 +                aspect = 1.;
1483 +                if (n == 1) yres = xres;
1484 +                sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1485 +        } else
1486 +                badvalue(RESOLUTION);
1487          rep[0] = '\0';
1488          if (vdef(REPORT)) {
1489                  double  minutes;
1113                int     n;
1490                  n = sscanf(vval(REPORT), "%lf %s", &minutes, rawfile);
1491                  if (n == 2)
1492                          sprintf(rep, " -t %d -e %s", (int)(minutes*60), rawfile);
# Line 1119 | Line 1495 | char   *opts;
1495                  else
1496                          badvalue(REPORT);
1497          }
1498 <                                        /* get update time */
1499 <        lastdate = octreedate > matdate ? octreedate : matdate;
1500 <                                        /* do each view */
1501 <        vn = 0;
1498 >                                        /* set up parallel rendering */
1499 >        sfile[0] = '\0';
1500 >        if ((nprocs > 1) & !touchonly & !vdef(ZFILE) &&
1501 >                                        getview(0, vs) != NULL) {
1502 >                if (!strcmp(c_rpict, DEF_RPICT_PATH) &&
1503 >                                getview(1, NULL) == NULL) {
1504 >                        sprintf(sfile, "%s_%s_rpsync.txt",
1505 >                                vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE),
1506 >                                        vs);
1507 >                        strcpy(rppopt, "-PP pfXXXXXX");
1508 >                } else {
1509 >                        strcpy(rppopt, "-S 1 -PP pfXXXXXX");
1510 >                }
1511 >                pfile = rppopt + strlen(rppopt) - 8;
1512 >                if (mktemp(pfile) == NULL) {
1513 >                        if (do_rpiece) {
1514 >                                fprintf(stderr, "%s: cannot create\n", pfile);
1515 >                                quit(1);
1516 >                        }
1517 >                        pfile = NULL;
1518 >                }
1519 >        }
1520 >        vn = 0;                                 /* do each view */
1521          while ((vw = getview(vn++, vs)) != NULL) {
1522                  if (sayview)
1523 <                        printview(vw);
1524 <                if (!vs[0])
1525 <                        sprintf(vs, "%d", vn);
1526 <                sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs);
1523 >                        myprintview(vw, stdout);
1524 >                sprintf(picfile, "%s_%s.hdr", vval(PICTURE), vs);
1525 >                if (vdef(ZFILE))
1526 >                        sprintf(zopt, " -z %s_%s.zbf", vval(ZFILE), vs);
1527 >                else
1528 >                        zopt[0] = '\0';
1529                                                  /* check date on picture */
1530 <                if (fdate(picfile) >= lastdate)
1530 >                pfdt = fdate(picfile);
1531 >                if (pfdt >= oct1date)
1532                          continue;
1533 +                                                /* get raw file name */
1534 +                sprintf(rawfile, "%s_%s.unf",
1535 +                        vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE), vs);
1536 +                rfdt = fdate(rawfile);
1537 +                if (touchonly) {                /* update times only */
1538 +                        if (rfdt) {
1539 +                                if (rfdt < oct1date)
1540 +                                        touch(rawfile);
1541 +                        } else if (pfdt && pfdt < oct1date)
1542 +                                touch(picfile);
1543 +                        continue;
1544 +                }
1545 +                                                /* parallel running? */
1546 +                if (do_rpiece) {
1547 +                        if (rfdt < oct1date || !fdate(sfile)) {
1548 +                                int     xdiv = 8+nprocs/3, ydiv = 8+nprocs/3;
1549 +                                if (rfdt >= oct1date) {
1550 +                                        fprintf(stderr,
1551 +                "%s: partial output not created with %s\n", rawfile, c_rpiece);
1552 +                                        quit(1);
1553 +                                }
1554 +                                if (rfdt) {     /* start fresh */
1555 +                                        rmfile(rawfile);
1556 +                                        rfdt = 0;
1557 +                                }
1558 +                                if (!silent)
1559 +                                        printf("\techo %d %d > %s\n",
1560 +                                                        xdiv, ydiv, sfile);
1561 +                                if ((fp = fopen(sfile, "w")) == NULL) {
1562 +                                        fprintf(stderr, "%s: cannot create\n",
1563 +                                                        sfile);
1564 +                                        quit(1);
1565 +                                }
1566 +                                fprintf(fp, "%d %d\n", xdiv, ydiv);
1567 +                                fclose(fp);
1568 +                        }
1569 +                } else if (next_process(0)) {
1570 +                        if (pfile != NULL)
1571 +                                sleep(10);
1572 +                        continue;
1573 +                } else if (!inchild())
1574 +                        pfile = NULL;
1575 +                /* XXX Remember to call finish_process() */
1576                                                  /* build rpict command */
1577 <                sprintf(rawfile, "%s_%s.raw", vval(PICTURE), vs);
1578 <                if (fdate(rawfile) >= octreedate)       /* recover */
1579 <                        sprintf(combuf, "rpict%s%s -ro %s %s",
1580 <                                        rep, opts, rawfile, vval(OCTREE));
1581 <                else {
1577 >                if (rfdt >= oct1date) {         /* already in progress */
1578 >                        if (do_rpiece) {
1579 >                                sprintf(combuf, "%s -R %s %s%s %s %s%s%s -o %s %s",
1580 >                                                c_rpiece, sfile, rppopt, rep, vw,
1581 >                                                res, opts, po, rawfile, oct1name);
1582 >                                while (next_process(1)) {
1583 >                                        sleep(10);
1584 >                                        combuf[strlen(c_rpiece)+2] = 'F';
1585 >                                }
1586 >                        } else
1587 >                                sprintf(combuf, "%s%s%s%s%s -ro %s %s", c_rpict,
1588 >                                        rep, opts, po, zopt, rawfile, oct1name);
1589 >                        if (runcom(combuf))     /* run rpict/rpiece */
1590 >                                goto rperror;
1591 >                } else {
1592                          if (overture) {         /* run overture calculation */
1593                                  sprintf(combuf,
1594 <                                "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1595 <                                                rep, vw, opts,
1596 <                                                vval(OCTREE), overfile);
1597 <                                if (runcom(combuf)) {
1598 <                                        fprintf(stderr,
1594 >                                        "%s%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1595 >                                                c_rpict, rep, vw, opts,
1596 >                                                oct1name, overfile);
1597 >                                if (!do_rpiece || !next_process(0)) {
1598 >                                        if (runcom(combuf)) {
1599 >                                                fprintf(stderr,
1600                                          "%s: error in overture for view %s\n",
1601 <                                                progname, vs);
1602 <                                        exit(1);
1603 <                                }
1604 < #ifdef NIX
1605 <                                rmfile(overfile);
1601 >                                                        progname, vs);
1602 >                                                quit(1);
1603 >                                        }
1604 > #ifndef NULL_DEVICE
1605 >                                        rmfile(overfile);
1606   #endif
1607 +                                } else if (do_rpiece)
1608 +                                        sleep(20);
1609                          }
1610 <                        sprintf(combuf, "rpict%s %s %s%s %s > %s",
1611 <                                        rep, vw, res, opts,
1612 <                                        vval(OCTREE), rawfile);
1610 >                        if (do_rpiece) {
1611 >                                sprintf(combuf, "%s -F %s %s%s %s %s%s%s -o %s %s",
1612 >                                                c_rpiece, sfile, rppopt, rep, vw,
1613 >                                                res, opts, po, rawfile, oct1name);
1614 >                                while (next_process(1))
1615 >                                        sleep(10);
1616 >                        } else {
1617 >                                sprintf(combuf, "%s%s %s %s%s%s%s %s > %s",
1618 >                                                c_rpict, rep, vw, res, opts, po,
1619 >                                                zopt, oct1name, rawfile);
1620 >                        }
1621 >                        if ((pfile != NULL) & !do_rpiece) {
1622 >                                if (!silent)
1623 >                                        printf("\t%s\n", combuf);
1624 >                                fflush(stdout);
1625 >                                sprintf(combuf, "%s%s %s %s%s%s %s > %s",
1626 >                                                c_rpict, rep, rppopt, res,
1627 >                                                opts, po, oct1name, rawfile);
1628 >                                fp = popen(combuf, "w");
1629 >                                if (fp == NULL)
1630 >                                        goto rperror;
1631 >                                myprintview(vw, fp);
1632 >                                if (pclose(fp))
1633 >                                        goto rperror;
1634 >                        } else if (runcom(combuf))
1635 >                                goto rperror;
1636                  }
1637 <                if (runcom(combuf)) {           /* run rpict */
1638 <                        fprintf(stderr, "%s: error rendering view %s\n",
1639 <                                        progname, vs);
1640 <                        exit(1);
1637 >                if (do_rpiece) {                /* need to finish raw, first */
1638 >                        finish_process();
1639 >                        wait_process(1);
1640 >                        if (!syncf_done(sfile)) {
1641 >                                fprintf(stderr,
1642 >                        "%s: %s did not complete rendering of view %s\n",
1643 >                                                progname, c_rpiece, vs);
1644 >                                quit(1);
1645 >                        }
1646                  }
1647 +                if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) {
1648                                                  /* build pfilt command */
1649 <                if (mult > 1)
1650 <                        sprintf(combuf, "pfilt%s -x /%d -y /%d %s > %s",
1651 <                                        pfopts, mult, mult, rawfile, picfile);
1652 <                else
1170 <                        sprintf(combuf, "pfilt%s %s > %s", pfopts,
1649 >                        if (do_rpiece)
1650 >                                sprintf(combuf,
1651 >                                        "%s%s -x %d -y %d -p %.3f %s > %s",
1652 >                                        c_pfilt, pfopts, xres, yres, aspect,
1653                                          rawfile, picfile);
1654 <                if (runcom(combuf)) {           /* run pfilt */
1655 <                        fprintf(stderr,
1656 <                        "%s: error filtering view %s\n\t%s removed\n",
1657 <                                        progname, vs, picfile);
1658 <                        unlink(picfile);
1659 <                        exit(1);
1654 >                        else if (mult > 1)
1655 >                                sprintf(combuf, "%s%s -x /%d -y /%d %s > %s",
1656 >                                        c_pfilt, pfopts, mult, mult,
1657 >                                        rawfile, picfile);
1658 >                        else
1659 >                                sprintf(combuf, "%s%s %s > %s", c_pfilt,
1660 >                                        pfopts, rawfile, picfile);
1661 >                        if (runcom(combuf)) {   /* run pfilt */
1662 >                                fprintf(stderr,
1663 >                                "%s: error filtering view %s\n\t%s removed\n",
1664 >                                                progname, vs, picfile);
1665 >                                unlink(picfile);
1666 >                                quit(1);
1667 >                        }
1668                  }
1669 <                                                /* remove raw file */
1670 <                rmfile(rawfile);
1669 >                                                /* remove/rename raw file */
1670 >                if (vdef(RAWFILE)) {
1671 >                        sprintf(combuf, "%s_%s.hdr", vval(RAWFILE), vs);
1672 >                        mvfile(rawfile, combuf);
1673 >                } else
1674 >                        rmfile(rawfile);
1675 >                if (do_rpiece)                  /* done with sync file */
1676 >                        rmfile(sfile);
1677 >                else
1678 >                        finish_process();       /* exit if child */
1679          }
1680 +        wait_process(1);                /* wait for children to finish */
1681 +        if (pfile != NULL) {            /* clean up persistent rpict */
1682 +                RT_PID  pid;
1683 +                fp = fopen(pfile, "r");
1684 +                if (fp != NULL) {
1685 +                        if (fscanf(fp, "%*s %d", &pid) != 1 ||
1686 +                                        kill(pid, 1) < 0)
1687 +                                unlink(pfile);
1688 +                        fclose(fp);
1689 +                }
1690 +        }
1691 +        return;
1692 + rperror:
1693 +        fprintf(stderr, "%s: error rendering view %s\n", progname, vs);
1694 +        quit(1);
1695 + #undef do_rpiece
1696   }
1697  
1698  
1699 < runcom(cs)                      /* run command */
1700 < char    *cs;
1699 > static int
1700 > touch(                  /* update a file */
1701 >        char    *fn
1702 > )
1703   {
1704 +        if (!silent)
1705 +                printf("\ttouch %s\n", fn);
1706 +        if (!nprocs)
1707 +                return(0);
1708 +        return(setfdate(fn, time((time_t *)NULL)));
1709 + }
1710 +
1711 +
1712 + static int
1713 + runcom(                 /* run command */
1714 +        char    *cs
1715 + )
1716 + {
1717          if (!silent)            /* echo it */
1718                  printf("\t%s\n", cs);
1719 <        if (noaction)
1719 >        if (!nprocs)
1720                  return(0);
1721 <        fflush(stdout);         /* flush output and pass to shell */
1721 >        fflush(NULL);           /* flush output and pass to shell */
1722          return(system(cs));
1723   }
1724  
1725  
1726 < rmfile(fn)                      /* remove a file */
1727 < char    *fn;
1726 > static int
1727 > rmfile(                 /* remove a file */
1728 >        char    *fn
1729 > )
1730   {
1731          if (!silent)
1732 < #ifdef MSDOS
1733 <                printf("\tdel %s\n", fn);
1203 < #else
1204 <                printf("\trm -f %s\n", fn);
1205 < #endif
1206 <        if (noaction)
1732 >                printf("\t%s %s\n", DELCMD, fn);
1733 >        if (!nprocs)
1734                  return(0);
1735          return(unlink(fn));
1736   }
1737  
1738  
1739 < #ifdef MSDOS
1740 < setenv(vname, value)            /* set an environment variable */
1741 < char    *vname, *value;
1739 > static int
1740 > mvfile(         /* move a file */
1741 >        char    *fold,
1742 >        char    *fnew
1743 > )
1744   {
1745 <        register char   *evp;
1745 >        if (!silent)
1746 >                printf("\t%s %s %s\n", RENAMECMD, fold, fnew);
1747 >        if (!nprocs)
1748 >                return(0);
1749 >        return(rename(fold, fnew));
1750 > }
1751  
1752 <        evp = bmalloc(strlen(vname)+strlen(value)+2);
1753 <        if (evp == NULL)
1754 <                syserr(progname);
1755 <        sprintf(evp, "%s=%s", vname, value);
1756 <        if (putenv(evp) != 0) {
1757 <                fprintf(stderr, "%s: out of environment space\n", progname);
1758 <                exit(1);
1752 >
1753 > #ifdef RHAS_FORK_EXEC
1754 > static int
1755 > next_process(int reserve)               /* fork the next process */
1756 > {
1757 >        RT_PID  child_pid;
1758 >
1759 >        if (nprocs <= 1)
1760 >                return(0);              /* it's us or no one */
1761 >        if (inchild()) {
1762 >                fprintf(stderr, "%s: internal error 1 in next_process()\n",
1763 >                                progname);
1764 >                quit(1);
1765          }
1766 <        if (!silent)
1767 <                printf("set %s\n", evp);
1766 >        if (reserve > 0 && children_running >= nprocs-reserve)
1767 >                return(0);              /* caller holding back process(es) */
1768 >        if (children_running >= nprocs)
1769 >                wait_process(0);        /* wait for someone to finish */
1770 >        fflush(NULL);                   /* flush output */
1771 >        child_pid = fork();             /* split process */
1772 >        if (child_pid == 0) {           /* we're the child */
1773 >                children_running = -1;
1774 >                nprocs = 1;
1775 >                return(0);
1776 >        }
1777 >        if (child_pid > 0) {            /* we're the parent */
1778 >                ++children_running;
1779 >                return(1);
1780 >        }
1781 >        fprintf(stderr, "%s: warning -- fork() failed\n", progname);
1782 >        return(0);
1783   }
1229 #endif
1784  
1785 + static void
1786 + wait_process(                   /* wait for process(es) to finish */
1787 +        int     all
1788 + )
1789 + {
1790 +        int     ourstatus = 0, status;
1791 +        RT_PID  pid;
1792  
1793 < badvalue(vc)                    /* report bad variable value and exit */
1794 < int     vc;
1793 >        if (all)
1794 >                all = children_running;
1795 >        else if (children_running > 0)
1796 >                all = 1;
1797 >        while (all-- > 0) {
1798 >                pid = wait(&status);
1799 >                if (pid < 0)
1800 >                        syserr(progname);
1801 >                status = status>>8 & 0xff;
1802 >                --children_running;
1803 >                if (status != 0) {      /* child's problem is our problem */
1804 >                        if ((ourstatus == 0) & (children_running > 0))
1805 >                                fprintf(stderr, "%s: waiting for remaining processes\n",
1806 >                                                progname);
1807 >                        ourstatus = status;
1808 >                        all = children_running;
1809 >                }
1810 >        }
1811 >        if (ourstatus != 0)
1812 >                quit(ourstatus);        /* bad status from child */
1813 > }
1814 > #else   /* ! RHAS_FORK_EXEC */
1815 > static int
1816 > next_process(int reserve)
1817   {
1818 +        return(0);                      /* cannot start new process */
1819 + }
1820 + static void
1821 + wait_process(all)
1822 + int     all;
1823 + {
1824 +        (void)all;                      /* no one to wait for */
1825 + }
1826 + int
1827 + kill(pid, sig) /* win|unix_process.c should also wait and kill */
1828 + RT_PID pid;
1829 + int sig;
1830 + {
1831 +        return 0;
1832 + }
1833 + #endif  /* ! RHAS_FORK_EXEC */
1834 +
1835 + static void
1836 + finish_process(void)                    /* exit a child process */
1837 + {
1838 +        if (!inchild())
1839 +                return;                 /* in parent -- noop */
1840 +        exit(0);
1841 + }
1842 +
1843 +
1844 + static void
1845 + badvalue(                       /* report bad variable value and exit */
1846 +        int     vc
1847 + )
1848 + {
1849          fprintf(stderr, "%s: bad value for variable '%s'\n",
1850                          progname, vnam(vc));
1851 <        exit(1);
1851 >        quit(1);
1852   }
1853  
1854  
1855 < syserr(s)                       /* report a system error and exit */
1856 < char    *s;
1855 > static void
1856 > syserr(                 /* report a system error and exit */
1857 >        char    *s
1858 > )
1859   {
1860          perror(s);
1861 <        exit(1);
1861 >        quit(1);
1862 > }
1863 >
1864 >
1865 > void
1866 > quit(ec)                        /* exit program */
1867 > int     ec;
1868 > {
1869 >        exit(ec);
1870   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines