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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines