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.1 by greg, Thu Mar 11 09:11:00 1993 UTC vs.
Revision 2.113 by greg, Wed May 27 12:50:46 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines