ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.85
Committed: Mon Apr 21 07:31:30 2008 UTC (15 years, 11 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R9
Changes since 2.84: +5 -4 lines
Log Message:
Scons update for mingw.

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 schorsch 2.85 static const char RCSid[] = "$Id: rad.c,v 2.84 2008/03/11 12:42:07 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Executive program for oconv, rpict and pfilt
6     */
7    
8     #include "standard.h"
9 schorsch 2.64
10     #include <ctype.h>
11 schorsch 2.69 #include <time.h>
12 schorsch 2.64
13     #include "platform.h"
14 schorsch 2.74 #include "rtprocess.h"
15 gregl 2.55 #include "view.h"
16 greg 2.1 #include "paths.h"
17 greg 2.48 #include "vars.h"
18 greg 2.1
19 schorsch 2.69 #ifdef _WIN32
20     #define DELCMD "del"
21     #define RENAMECMD "rename"
22     #else
23     #define DELCMD "rm -f"
24     #define RENAMECMD "mv"
25 schorsch 2.76 #include <sys/types.h>
26     #include <sys/wait.h>
27 schorsch 2.69 #endif
28    
29 gwlarson 2.56 /* variables (alphabetical by name) */
30     #define AMBFILE 0 /* ambient file name */
31     #define DETAIL 1 /* level of scene detail */
32     #define EXPOSURE 2 /* picture exposure setting */
33 gwlarson 2.57 #define EYESEP 3 /* interocular distance */
34     #define ILLUM 4 /* mkillum input files */
35     #define INDIRECT 5 /* indirection in lighting */
36     #define MATERIAL 6 /* material files */
37     #define MKILLUM 7 /* mkillum options */
38     #define OBJECT 8 /* object files */
39     #define OCONV 9 /* oconv options */
40     #define OCTREE 10 /* octree file name */
41     #define OPTFILE 11 /* rendering options file */
42     #define PENUMBRAS 12 /* shadow penumbras are desired */
43     #define PFILT 13 /* pfilt options */
44     #define PICTURE 14 /* picture file root name */
45     #define QUALITY 15 /* desired rendering quality */
46     #define RAWFILE 16 /* raw picture file root name */
47     #define RENDER 17 /* rendering options */
48     #define REPORT 18 /* report frequency and errfile */
49     #define RESOLUTION 19 /* maximum picture resolution */
50     #define SCENE 20 /* scene files */
51     #define UP 21 /* view up (X, Y or Z) */
52     #define VARIABILITY 22 /* level of light variability */
53     #define VIEWS 23 /* view(s) for picture(s) */
54     #define ZFILE 24 /* distance file root name */
55     #define ZONE 25 /* simulation zone */
56 greg 2.1 /* total number of variables */
57 gwlarson 2.57 int NVARS = 26;
58 greg 2.1
59 greg 2.48 VARIABLE vv[] = { /* variable-value pairs */
60 gwlarson 2.56 {"AMBFILE", 3, 0, NULL, onevalue},
61     {"DETAIL", 3, 0, NULL, qualvalue},
62     {"EXPOSURE", 3, 0, NULL, fltvalue},
63 gwlarson 2.57 {"EYESEP", 3, 0, NULL, fltvalue},
64 gwlarson 2.56 {"illum", 3, 0, NULL, catvalues},
65     {"INDIRECT", 3, 0, NULL, intvalue},
66 greg 2.1 {"materials", 3, 0, NULL, catvalues},
67 greg 2.26 {"mkillum", 3, 0, NULL, catvalues},
68 gwlarson 2.56 {"objects", 3, 0, NULL, catvalues},
69 greg 2.1 {"oconv", 3, 0, NULL, catvalues},
70 gwlarson 2.56 {"OCTREE", 3, 0, NULL, onevalue},
71     {"OPTFILE", 3, 0, NULL, onevalue},
72     {"PENUMBRAS", 3, 0, NULL, boolvalue},
73 greg 2.1 {"pfilt", 2, 0, NULL, catvalues},
74 gwlarson 2.56 {"PICTURE", 3, 0, NULL, onevalue},
75 greg 2.30 {"QUALITY", 3, 0, NULL, qualvalue},
76 gwlarson 2.56 {"RAWFILE", 3, 0, NULL, onevalue},
77     {"render", 3, 0, NULL, catvalues},
78     {"REPORT", 3, 0, NULL, onevalue},
79 greg 2.1 {"RESOLUTION", 3, 0, NULL, onevalue},
80 gwlarson 2.56 {"scene", 3, 0, NULL, catvalues},
81 greg 2.1 {"UP", 2, 0, NULL, onevalue},
82 greg 2.30 {"VARIABILITY", 3, 0, NULL, qualvalue},
83 gwlarson 2.56 {"view", 2, 0, NULL, NULL},
84 greg 2.45 {"ZFILE", 2, 0, NULL, onevalue},
85 gwlarson 2.56 {"ZONE", 2, 0, NULL, onevalue},
86 greg 2.1 };
87    
88 greg 2.6 /* overture calculation file */
89 schorsch 2.65 #ifdef NULL_DEVICE
90     char overfile[] = NULL_DEVICE;
91     #else
92 greg 2.45 char overfile[] = "overture.unf";
93 greg 2.6 #endif
94    
95 greg 2.1
96 greg 2.35 time_t scenedate; /* date of latest scene or object file */
97     time_t octreedate; /* date of octree */
98     time_t matdate; /* date of latest material file */
99     time_t illumdate; /* date of last illum file */
100 greg 2.1
101 greg 2.26 char *oct0name; /* name of pre-mkillum octree */
102 greg 2.35 time_t oct0date; /* date of pre-mkillum octree */
103 greg 2.26 char *oct1name; /* name of post-mkillum octree */
104 greg 2.35 time_t oct1date; /* date of post-mkillum octree (>= matdate) */
105 greg 2.26
106 greg 2.37 int nowarn = 0; /* no warnings */
107 greg 2.1 int explicate = 0; /* explicate variables */
108     int silent = 0; /* do work silently */
109 greg 2.41 int touchonly = 0; /* touch files only */
110 greg 2.66 int nprocs = 1; /* maximum executing processes */
111 greg 2.21 int sayview = 0; /* print view out */
112 greg 2.75 char *rvdevice = NULL; /* rvu output device */
113 greg 2.1 char *viewselect = NULL; /* specific view only */
114    
115     int overture = 0; /* overture calculation needed */
116    
117 greg 2.66 int children_running = 0; /* set negative in children */
118    
119 greg 2.1 char *progname; /* global argv[0] */
120 greg 2.22 char *rifname; /* global rad input file name */
121 greg 2.1
122 schorsch 2.65 char radname[PATH_MAX]; /* root Radiance file name */
123 greg 2.1
124 greg 2.67 #define inchild() (children_running < 0)
125    
126 schorsch 2.76 static void rootname(char *rn, char *fn);
127     static time_t checklast(char *fnames);
128     static char * newfname(char *orig, int pred);
129     static void checkfiles(void);
130     static void getoctcube(double org[3], double *sizp);
131     static void setdefaults(void);
132     static void oconv(void);
133     static char * addarg(char *op, char *arg);
134     static void oconvopts(char *oo);
135     static void mkillumopts(char *mo);
136     static void checkambfile(void);
137     static double ambval(void);
138     static void renderopts(char *op, char *po);
139     static void lowqopts(char *op, char *po);
140     static void medqopts(char *op, char *po);
141     static void hiqopts(char *op, char *po);
142     static void xferopts(char *ro);
143     static void pfiltopts(char *po);
144     static int matchword(char *s1, char *s2);
145     static char * specview(char *vs);
146     static char * getview(int n, char *vn);
147     static int myprintview(char *vopts, FILE *fp);
148     static void rvu(char *opts, char *po);
149     static void rpict(char *opts, char *po);
150     static int touch(char *fn);
151     static int runcom(char *cs);
152     static int rmfile(char *fn);
153     static int mvfile(char *fold, char *fnew);
154     static int next_process(void);
155     static void wait_process(int all);
156     static void finish_process(void);
157     static void badvalue(int vc);
158     static void syserr(char *s);
159 greg 2.1
160 schorsch 2.76
161     int
162     main(
163     int argc,
164     char *argv[]
165     )
166 greg 2.1 {
167     char ropts[512];
168 greg 2.38 char popts[64];
169 greg 2.1 int i;
170    
171     progname = argv[0];
172     /* get options */
173     for (i = 1; i < argc && argv[i][0] == '-'; i++)
174     switch (argv[i][1]) {
175     case 's':
176     silent++;
177     break;
178     case 'n':
179 greg 2.66 nprocs = 0;
180     break;
181     case 'N':
182     nprocs = atoi(argv[++i]);
183     if (nprocs < 0)
184     nprocs = 0;
185 greg 2.1 break;
186 greg 2.41 case 't':
187     touchonly++;
188     break;
189 greg 2.1 case 'e':
190     explicate++;
191     break;
192     case 'o':
193     rvdevice = argv[++i];
194     break;
195 greg 2.20 case 'V':
196 greg 2.21 sayview++;
197     break;
198 greg 2.1 case 'v':
199     viewselect = argv[++i];
200     break;
201 greg 2.37 case 'w':
202     nowarn++;
203     break;
204 greg 2.1 default:
205     goto userr;
206     }
207     if (i >= argc)
208     goto userr;
209 greg 2.22 rifname = argv[i];
210 greg 2.67 /* check command-line options */
211 schorsch 2.71 if ((nprocs > 1) & (viewselect != NULL))
212 greg 2.67 nprocs = 1;
213 greg 2.1 /* assign Radiance root file name */
214 greg 2.22 rootname(radname, rifname);
215 greg 2.1 /* load variable values */
216 greg 2.48 loadvars(rifname);
217 greg 2.1 /* get any additional assignments */
218     for (i++; i < argc; i++)
219 greg 2.53 if (setvariable(argv[i], matchvar) < 0) {
220     fprintf(stderr, "%s: unknown variable: %s\n",
221     progname, argv[i]);
222     quit(1);
223     }
224 greg 2.1 /* check assignments */
225     checkvalues();
226     /* check files and dates */
227     checkfiles();
228     /* set default values as necessary */
229     setdefaults();
230     /* print all values if requested */
231     if (explicate)
232 greg 2.48 printvars(stdout);
233 greg 2.26 /* build octree (and run mkillum) */
234 greg 2.7 oconv();
235     /* check date on ambient file */
236     checkambfile();
237 greg 2.1 /* run simulation */
238 greg 2.38 renderopts(ropts, popts);
239 greg 2.1 xferopts(ropts);
240     if (rvdevice != NULL)
241 greg 2.75 rvu(ropts, popts);
242 greg 2.1 else
243 greg 2.38 rpict(ropts, popts);
244 greg 2.53 quit(0);
245 greg 2.1 userr:
246     fprintf(stderr,
247 greg 2.66 "Usage: %s [-w][-s][-n|-N npr][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
248 greg 2.1 progname);
249 greg 2.53 quit(1);
250 schorsch 2.76 return 1; /* pro forma return */
251 greg 2.1 }
252    
253    
254 schorsch 2.76 static void
255     rootname( /* remove tail from end of fn */
256     register char *rn,
257     register char *fn
258     )
259 greg 2.1 {
260     char *tp, *dp;
261    
262 schorsch 2.71 for (tp = NULL, dp = rn; (*rn = *fn++); rn++)
263 greg 2.1 if (ISDIRSEP(*rn))
264     dp = rn;
265     else if (*rn == '.')
266     tp = rn;
267     if (tp != NULL && tp > dp)
268     *tp = '\0';
269     }
270    
271    
272 schorsch 2.76 static time_t
273     checklast( /* check files and find most recent */
274     register char *fnames
275     )
276 greg 2.1 {
277 schorsch 2.65 char thisfile[PATH_MAX];
278 greg 2.35 time_t thisdate, lastdate = 0;
279 greg 2.1
280 greg 2.26 if (fnames == NULL)
281     return(0);
282 schorsch 2.65 while ((fnames = nextword(thisfile, PATH_MAX, fnames)) != NULL) {
283 gwlarson 2.60 if (thisfile[0] == '!' ||
284 greg 2.77 (thisfile[0] == '\\' && thisfile[1] == '!')) {
285     if (!lastdate)
286     lastdate = 1;
287 gwlarson 2.60 continue;
288 greg 2.77 }
289 greg 2.26 if (!(thisdate = fdate(thisfile)))
290 greg 2.3 syserr(thisfile);
291 greg 2.1 if (thisdate > lastdate)
292     lastdate = thisdate;
293     }
294     return(lastdate);
295     }
296    
297    
298 schorsch 2.76 static char *
299     newfname( /* create modified file name */
300     char *orig,
301     int pred
302     )
303 greg 2.26 {
304     register char *cp;
305     register int n;
306     int suffix;
307    
308 greg 2.27 n = 0; cp = orig; suffix = -1; /* suffix position, length */
309     while (*cp) {
310     if (*cp == '.') suffix = n;
311     else if (ISDIRSEP(*cp)) suffix = -1;
312     cp++; n++;
313     }
314     if (suffix == -1) suffix = n;
315 greg 2.26 if ((cp = bmalloc(n+2)) == NULL)
316     syserr(progname);
317     strncpy(cp, orig, suffix);
318     cp[suffix] = pred; /* root name + pred + suffix */
319     strcpy(cp+suffix+1, orig+suffix);
320     return(cp);
321     }
322    
323    
324 schorsch 2.76 static void
325     checkfiles(void) /* check for existence and modified times */
326 greg 2.1 {
327 greg 2.35 time_t objdate;
328 greg 2.1
329 greg 2.2 if (!vdef(OCTREE)) {
330 greg 2.26 if ((vval(OCTREE) = bmalloc(strlen(radname)+5)) == NULL)
331 greg 2.3 syserr(progname);
332 greg 2.26 sprintf(vval(OCTREE), "%s.oct", radname);
333 greg 2.2 vdef(OCTREE)++;
334 greg 2.61 } else if (vval(OCTREE)[0] == '!') {
335     fprintf(stderr, "%s: illegal '%s' specification\n",
336     progname, vnam(OCTREE));
337     quit(1);
338 greg 2.2 }
339     octreedate = fdate(vval(OCTREE));
340 greg 2.26 if (vdef(ILLUM)) { /* illum requires secondary octrees */
341     oct0name = newfname(vval(OCTREE), '0');
342     oct1name = newfname(vval(OCTREE), '1');
343     oct0date = fdate(oct0name);
344     oct1date = fdate(oct1name);
345     } else
346     oct0name = oct1name = vval(OCTREE);
347     if ((scenedate = checklast(vval(SCENE))) &&
348     (objdate = checklast(vval(OBJECT))) > scenedate)
349     scenedate = objdate;
350     illumdate = checklast(vval(ILLUM));
351     if (!octreedate & !scenedate & !illumdate) {
352     fprintf(stderr, "%s: need '%s' or '%s' or '%s'\n", progname,
353     vnam(OCTREE), vnam(SCENE), vnam(ILLUM));
354 greg 2.53 quit(1);
355 greg 2.1 }
356 greg 2.26 matdate = checklast(vval(MATERIAL));
357 greg 2.1 }
358    
359    
360 schorsch 2.76 static void
361     getoctcube( /* get octree bounding cube */
362     double org[3],
363     double *sizp
364     )
365 greg 2.1 {
366 greg 2.2 static double oorg[3], osiz = 0.;
367 greg 2.26 double min[3], max[3];
368 greg 2.46 char buf[1024];
369 greg 2.1 FILE *fp;
370 greg 2.26 register int i;
371 greg 2.1
372 schorsch 2.70 if (osiz <= FTINY) {
373 greg 2.66 if (!nprocs && fdate(oct1name) <
374 greg 2.26 (scenedate>illumdate?scenedate:illumdate)) {
375     /* run getbbox */
376     sprintf(buf, "getbbox -w -h %s",
377     vdef(SCENE) ? vval(SCENE) : vval(ILLUM));
378     if ((fp = popen(buf, "r")) == NULL)
379     syserr("getbbox");
380     if (fscanf(fp, "%lf %lf %lf %lf %lf %lf",
381     &min[0], &max[0], &min[1], &max[1],
382     &min[2], &max[2]) != 6) {
383     fprintf(stderr,
384     "%s: error reading bounding box from getbbox\n",
385     progname);
386 greg 2.53 quit(1);
387 greg 2.26 }
388     for (i = 0; i < 3; i++)
389     if (max[i] - min[i] > osiz)
390     osiz = max[i] - min[i];
391     for (i = 0; i < 3; i++)
392     oorg[i] = (max[i]+min[i]-osiz)*.5;
393     pclose(fp);
394     } else { /* from octree */
395     oconv(); /* does nothing if done already */
396     sprintf(buf, "getinfo -d < %s", oct1name);
397     if ((fp = popen(buf, "r")) == NULL)
398     syserr("getinfo");
399     if (fscanf(fp, "%lf %lf %lf %lf", &oorg[0], &oorg[1],
400     &oorg[2], &osiz) != 4) {
401     fprintf(stderr,
402 greg 2.2 "%s: error reading bounding cube from getinfo\n",
403 greg 2.26 progname);
404 greg 2.53 quit(1);
405 greg 2.26 }
406     pclose(fp);
407 greg 2.2 }
408 schorsch 2.70 }
409 greg 2.2 org[0] = oorg[0]; org[1] = oorg[1]; org[2] = oorg[2]; *sizp = osiz;
410     }
411    
412    
413 schorsch 2.76 static void
414     setdefaults(void) /* set default values for unassigned var's */
415 greg 2.2 {
416 gwlarson 2.58 double org[3], lim[3], size;
417 greg 2.2 char buf[128];
418    
419 greg 2.1 if (!vdef(ZONE)) {
420 greg 2.2 getoctcube(org, &size);
421     sprintf(buf, "E %g %g %g %g %g %g", org[0], org[0]+size,
422     org[1], org[1]+size, org[2], org[2]+size);
423 greg 2.1 vval(ZONE) = savqstr(buf);
424     vdef(ZONE)++;
425 gwlarson 2.58 }
426     if (!vdef(EYESEP)) {
427     if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf",
428     &org[0], &lim[0], &org[1], &lim[1],
429     &org[2], &lim[2]) != 6)
430     badvalue(ZONE);
431     sprintf(buf, "%f",
432     0.01*(lim[0]-org[0]+lim[1]-org[1]+lim[2]-org[2]));
433     vval(EYESEP) = savqstr(buf);
434     vdef(EYESEP)++;
435 greg 2.1 }
436     if (!vdef(INDIRECT)) {
437     vval(INDIRECT) = "0";
438     vdef(INDIRECT)++;
439     }
440     if (!vdef(QUALITY)) {
441     vval(QUALITY) = "L";
442     vdef(QUALITY)++;
443     }
444     if (!vdef(RESOLUTION)) {
445     vval(RESOLUTION) = "512";
446     vdef(RESOLUTION)++;
447     }
448     if (!vdef(PICTURE)) {
449     vval(PICTURE) = radname;
450     vdef(PICTURE)++;
451     }
452 gregl 2.55 if (!vdef(VIEWS)) {
453     vval(VIEWS) = "X";
454     vdef(VIEWS)++;
455 greg 2.1 }
456     if (!vdef(DETAIL)) {
457     vval(DETAIL) = "M";
458     vdef(DETAIL)++;
459     }
460     if (!vdef(PENUMBRAS)) {
461     vval(PENUMBRAS) = "F";
462     vdef(PENUMBRAS)++;
463     }
464     if (!vdef(VARIABILITY)) {
465     vval(VARIABILITY) = "L";
466     vdef(VARIABILITY)++;
467     }
468     }
469    
470    
471 schorsch 2.76 static void
472     oconv(void) /* run oconv and mkillum if necessary */
473 greg 2.1 {
474 greg 2.26 static char illumtmp[] = "ilXXXXXX";
475 greg 2.78 char combuf[PATH_MAX], ocopts[64], mkopts[1024];
476 greg 2.1
477 greg 2.26 oconvopts(ocopts); /* get options */
478     if (octreedate < scenedate) { /* check date on original octree */
479 greg 2.41 if (touchonly && octreedate)
480     touch(vval(OCTREE));
481     else { /* build command */
482     if (vdef(MATERIAL))
483     sprintf(combuf, "oconv%s %s %s > %s", ocopts,
484     vval(MATERIAL), vval(SCENE),
485     vval(OCTREE));
486     else
487     sprintf(combuf, "oconv%s %s > %s", ocopts,
488     vval(SCENE), vval(OCTREE));
489    
490     if (runcom(combuf)) { /* run it */
491     fprintf(stderr,
492 greg 2.26 "%s: error generating octree\n\t%s removed\n",
493 greg 2.41 progname, vval(OCTREE));
494     unlink(vval(OCTREE));
495 greg 2.53 quit(1);
496 greg 2.41 }
497 greg 2.26 }
498 greg 2.35 octreedate = time((time_t *)NULL);
499 greg 2.40 if (octreedate < scenedate) /* in case clock is off */
500     octreedate = scenedate;
501 greg 2.26 }
502     if (oct1name == vval(OCTREE)) /* no mkillum? */
503     oct1date = octreedate > matdate ? octreedate : matdate;
504 schorsch 2.71 if ((oct1date >= octreedate) & (oct1date >= matdate)
505     & (oct1date >= illumdate)) /* all done */
506 greg 2.1 return;
507 greg 2.26 /* make octree0 */
508 schorsch 2.71 if ((oct0date < scenedate) | (oct0date < illumdate)) {
509 greg 2.41 if (touchonly && oct0date)
510     touch(oct0name);
511     else { /* build command */
512     if (octreedate)
513     sprintf(combuf, "oconv%s -i %s %s > %s", ocopts,
514     vval(OCTREE), vval(ILLUM), oct0name);
515     else if (vdef(MATERIAL))
516     sprintf(combuf, "oconv%s %s %s > %s", ocopts,
517     vval(MATERIAL), vval(ILLUM), oct0name);
518     else
519     sprintf(combuf, "oconv%s %s > %s", ocopts,
520     vval(ILLUM), oct0name);
521     if (runcom(combuf)) { /* run it */
522     fprintf(stderr,
523     "%s: error generating octree\n\t%s removed\n",
524     progname, oct0name);
525     unlink(oct0name);
526 greg 2.53 quit(1);
527 greg 2.41 }
528     }
529     oct0date = time((time_t *)NULL);
530     if (oct0date < octreedate) /* in case clock is off */
531     oct0date = octreedate;
532     if (oct0date < illumdate) /* ditto */
533     oct0date = illumdate;
534     }
535     if (touchonly && oct1date)
536     touch(oct1name);
537     else {
538     mkillumopts(mkopts); /* build mkillum command */
539     mktemp(illumtmp);
540     sprintf(combuf, "mkillum%s %s \"<\" %s > %s", mkopts,
541     oct0name, vval(ILLUM), illumtmp);
542     if (runcom(combuf)) { /* run it */
543     fprintf(stderr, "%s: error running mkillum\n",
544     progname);
545     unlink(illumtmp);
546 greg 2.53 quit(1);
547 greg 2.41 }
548     /* make octree1 (frozen) */
549 greg 2.26 if (octreedate)
550 greg 2.41 sprintf(combuf, "oconv%s -f -i %s %s > %s", ocopts,
551     vval(OCTREE), illumtmp, oct1name);
552 greg 2.26 else if (vdef(MATERIAL))
553 greg 2.41 sprintf(combuf, "oconv%s -f %s %s > %s", ocopts,
554     vval(MATERIAL), illumtmp, oct1name);
555 greg 2.26 else
556 greg 2.41 sprintf(combuf, "oconv%s -f %s > %s", ocopts,
557     illumtmp, oct1name);
558 greg 2.26 if (runcom(combuf)) { /* run it */
559     fprintf(stderr,
560     "%s: error generating octree\n\t%s removed\n",
561 greg 2.41 progname, oct1name);
562     unlink(oct1name);
563 greg 2.46 unlink(illumtmp);
564 greg 2.53 quit(1);
565 greg 2.26 }
566 greg 2.41 rmfile(illumtmp);
567 greg 2.26 }
568 greg 2.35 oct1date = time((time_t *)NULL);
569 greg 2.40 if (oct1date < oct0date) /* in case clock is off */
570     oct1date = oct0date;
571 greg 2.1 }
572    
573    
574 schorsch 2.76 static char *
575     addarg( /* add argument and advance pointer */
576     register char *op,
577     register char *arg
578     )
579 greg 2.1 {
580     *op = ' ';
581 schorsch 2.71 while ( (*++op = *arg++) )
582 greg 2.1 ;
583     return(op);
584     }
585    
586    
587 schorsch 2.76 static void
588     oconvopts( /* get oconv options */
589     register char *oo
590     )
591 greg 2.1 {
592 greg 2.2 /* BEWARE: This may be called via setdefaults(), so no assumptions */
593    
594 greg 2.1 *oo = '\0';
595     if (vdef(OCONV))
596     addarg(oo, vval(OCONV));
597     }
598    
599    
600 schorsch 2.76 static void
601     mkillumopts( /* get mkillum options */
602 greg 2.78 char *mo
603 schorsch 2.76 )
604 greg 2.26 {
605     /* BEWARE: This may be called via setdefaults(), so no assumptions */
606    
607 greg 2.78 if (nprocs > 1) {
608     sprintf(mo, " -n %d", nprocs);
609     while (*mo)
610     mo++;
611     } else
612     *mo = '\0';
613 greg 2.26 if (vdef(MKILLUM))
614     addarg(mo, vval(MKILLUM));
615     }
616    
617    
618 schorsch 2.76 static void
619     checkambfile(void) /* check date on ambient file */
620 greg 2.7 {
621 greg 2.35 time_t afdate;
622 greg 2.7
623 greg 2.11 if (!vdef(AMBFILE))
624     return;
625 greg 2.26 if (!(afdate = fdate(vval(AMBFILE))))
626 greg 2.11 return;
627 schorsch 2.70 if (oct1date > afdate) {
628 greg 2.41 if (touchonly)
629     touch(vval(AMBFILE));
630     else
631     rmfile(vval(AMBFILE));
632 schorsch 2.70 }
633 greg 2.7 }
634    
635    
636 schorsch 2.76 static double
637     ambval(void) /* compute ambient value */
638 greg 2.1 {
639 greg 2.3 if (vdef(EXPOSURE)) {
640 greg 2.2 if (vval(EXPOSURE)[0] == '+' || vval(EXPOSURE)[0] == '-')
641 greg 2.50 return(.5/pow(2.,vflt(EXPOSURE)));
642     return(.5/vflt(EXPOSURE));
643 greg 2.3 }
644 greg 2.2 if (vlet(ZONE) == 'E')
645     return(10.);
646 greg 2.3 if (vlet(ZONE) == 'I')
647 greg 2.2 return(.01);
648 greg 2.3 badvalue(ZONE);
649 schorsch 2.76 return 0; /* pro forma return */
650 greg 2.30 }
651    
652    
653 schorsch 2.76 static void
654     renderopts( /* set rendering options */
655     char *op,
656     char *po
657     )
658 greg 2.30 {
659     switch(vscale(QUALITY)) {
660     case LOW:
661 greg 2.38 lowqopts(op, po);
662 greg 2.30 break;
663     case MEDIUM:
664 greg 2.38 medqopts(op, po);
665 greg 2.30 break;
666     case HIGH:
667 greg 2.38 hiqopts(op, po);
668 greg 2.30 break;
669     }
670 greg 2.2 }
671 greg 2.1
672 greg 2.2
673 schorsch 2.76 static void
674     lowqopts( /* low quality rendering options */
675     register char *op,
676     char *po
677     )
678 greg 2.2 {
679     double d, org[3], siz[3];
680    
681 greg 2.1 *op = '\0';
682 greg 2.38 *po = '\0';
683 greg 2.2 if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
684 greg 2.3 &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
685     badvalue(ZONE);
686 greg 2.2 siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
687 schorsch 2.71 if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
688 greg 2.39 badvalue(ZONE);
689 greg 2.2 getoctcube(org, &d);
690     d *= 3./(siz[0]+siz[1]+siz[2]);
691     switch (vscale(DETAIL)) {
692     case LOW:
693 greg 2.38 po = addarg(po, "-ps 16");
694     op = addarg(op, "-dp 64");
695 greg 2.72 sprintf(op, " -ar %d", (int)(8*d));
696 greg 2.2 op += strlen(op);
697     break;
698     case MEDIUM:
699 greg 2.38 po = addarg(po, "-ps 8");
700     op = addarg(op, "-dp 128");
701 greg 2.72 sprintf(op, " -ar %d", (int)(16*d));
702 greg 2.2 op += strlen(op);
703     break;
704     case HIGH:
705 greg 2.38 po = addarg(po, "-ps 4");
706     op = addarg(op, "-dp 256");
707 greg 2.72 sprintf(op, " -ar %d", (int)(32*d));
708 greg 2.2 op += strlen(op);
709     break;
710     }
711 greg 2.38 po = addarg(po, "-pt .16");
712 greg 2.2 if (vbool(PENUMBRAS))
713     op = addarg(op, "-ds .4");
714 greg 2.4 else
715     op = addarg(op, "-ds 0");
716 greg 2.17 op = addarg(op, "-dt .2 -dc .25 -dr 0 -sj 0 -st .5");
717 greg 2.2 if (vdef(AMBFILE)) {
718     sprintf(op, " -af %s", vval(AMBFILE));
719     op += strlen(op);
720     } else
721     overture = 0;
722     switch (vscale(VARIABILITY)) {
723     case LOW:
724 greg 2.72 op = addarg(op, "-aa .3 -ad 256");
725 greg 2.2 break;
726     case MEDIUM:
727 greg 2.72 op = addarg(op, "-aa .25 -ad 512");
728 greg 2.2 break;
729     case HIGH:
730 greg 2.72 op = addarg(op, "-aa .2 -ad 1024");
731 greg 2.2 break;
732     }
733     op = addarg(op, "-as 0");
734     d = ambval();
735     sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
736     op += strlen(op);
737 greg 2.72 op = addarg(op, "-lr 6 -lw .01");
738 greg 2.1 if (vdef(RENDER))
739     op = addarg(op, vval(RENDER));
740     }
741    
742    
743 schorsch 2.76 static void
744     medqopts( /* medium quality rendering options */
745     register char *op,
746     char *po
747     )
748 greg 2.1 {
749 greg 2.47 double d, org[3], siz[3], asz;
750 greg 2.1
751     *op = '\0';
752 greg 2.38 *po = '\0';
753 greg 2.2 if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
754 greg 2.3 &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
755     badvalue(ZONE);
756 greg 2.2 siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
757 schorsch 2.71 if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
758 greg 2.39 badvalue(ZONE);
759 greg 2.2 getoctcube(org, &d);
760 greg 2.47 asz = (siz[0]+siz[1]+siz[2])/3.;
761     d /= asz;
762 greg 2.2 switch (vscale(DETAIL)) {
763     case LOW:
764 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 4" : "-ps 8");
765 greg 2.23 op = addarg(op, "-dp 256");
766 greg 2.72 sprintf(op, " -ar %d", (int)(16*d));
767 greg 2.2 op += strlen(op);
768 greg 2.47 sprintf(op, " -ms %.2g", asz/20.);
769     op += strlen(op);
770 greg 2.2 break;
771     case MEDIUM:
772 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 3" : "-ps 6");
773 greg 2.23 op = addarg(op, "-dp 512");
774 greg 2.72 sprintf(op, " -ar %d", (int)(32*d));
775 greg 2.2 op += strlen(op);
776 greg 2.47 sprintf(op, " -ms %.2g", asz/40.);
777     op += strlen(op);
778 greg 2.2 break;
779     case HIGH:
780 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 2" : "-ps 4");
781 greg 2.23 op = addarg(op, "-dp 1024");
782 greg 2.72 sprintf(op, " -ar %d", (int)(64*d));
783 greg 2.2 op += strlen(op);
784 greg 2.47 sprintf(op, " -ms %.2g", asz/80.);
785     op += strlen(op);
786 greg 2.2 break;
787     }
788 greg 2.38 po = addarg(po, "-pt .08");
789 greg 2.4 if (vbool(PENUMBRAS))
790 greg 2.28 op = addarg(op, "-ds .2 -dj .5");
791 greg 2.4 else
792 greg 2.2 op = addarg(op, "-ds .3");
793 greg 2.17 op = addarg(op, "-dt .1 -dc .5 -dr 1 -sj .7 -st .1");
794 schorsch 2.71 if ( (overture = vint(INDIRECT)) ) {
795 greg 2.5 sprintf(op, " -ab %d", overture);
796     op += strlen(op);
797     }
798 greg 2.2 if (vdef(AMBFILE)) {
799     sprintf(op, " -af %s", vval(AMBFILE));
800     op += strlen(op);
801     } else
802     overture = 0;
803     switch (vscale(VARIABILITY)) {
804     case LOW:
805 greg 2.72 op = addarg(op, "-aa .2 -ad 329 -as 42");
806 greg 2.2 break;
807     case MEDIUM:
808 greg 2.72 op = addarg(op, "-aa .15 -ad 800 -as 128");
809 greg 2.2 break;
810     case HIGH:
811 greg 2.72 op = addarg(op, "-aa .1 -ad 1536 -as 392");
812 greg 2.2 break;
813     }
814     d = ambval();
815     sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
816     op += strlen(op);
817 greg 2.72 op = addarg(op, "-lr 8 -lw .002");
818 greg 2.1 if (vdef(RENDER))
819     op = addarg(op, vval(RENDER));
820     }
821    
822    
823 schorsch 2.76 static void
824     hiqopts( /* high quality rendering options */
825     register char *op,
826     char *po
827     )
828 greg 2.1 {
829 greg 2.47 double d, org[3], siz[3], asz;
830 greg 2.1
831     *op = '\0';
832 greg 2.38 *po = '\0';
833 greg 2.2 if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
834 greg 2.3 &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
835     badvalue(ZONE);
836 greg 2.2 siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
837 schorsch 2.71 if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
838 greg 2.39 badvalue(ZONE);
839 greg 2.2 getoctcube(org, &d);
840 greg 2.47 asz = (siz[0]+siz[1]+siz[2])/3.;
841     d /= asz;
842 greg 2.2 switch (vscale(DETAIL)) {
843     case LOW:
844 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 8");
845 greg 2.23 op = addarg(op, "-dp 1024");
846 greg 2.72 sprintf(op, " -ar %d", (int)(32*d));
847 greg 2.2 op += strlen(op);
848 greg 2.47 sprintf(op, " -ms %.2g", asz/40.);
849     op += strlen(op);
850 greg 2.2 break;
851     case MEDIUM:
852 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 5");
853 greg 2.23 op = addarg(op, "-dp 2048");
854 greg 2.72 sprintf(op, " -ar %d", (int)(64*d));
855 greg 2.2 op += strlen(op);
856 greg 2.47 sprintf(op, " -ms %.2g", asz/80.);
857     op += strlen(op);
858 greg 2.2 break;
859     case HIGH:
860 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 3");
861 greg 2.23 op = addarg(op, "-dp 4096");
862 greg 2.72 sprintf(op, " -ar %d", (int)(128*d));
863 greg 2.47 op += strlen(op);
864     sprintf(op, " -ms %.2g", asz/160.);
865 greg 2.2 op += strlen(op);
866     break;
867     }
868 greg 2.38 po = addarg(po, "-pt .04");
869 greg 2.4 if (vbool(PENUMBRAS))
870 greg 2.42 op = addarg(op, "-ds .1 -dj .65");
871 greg 2.4 else
872 greg 2.2 op = addarg(op, "-ds .2");
873 greg 2.17 op = addarg(op, "-dt .05 -dc .75 -dr 3 -sj 1 -st .01");
874 greg 2.2 sprintf(op, " -ab %d", overture=vint(INDIRECT)+1);
875     op += strlen(op);
876     if (vdef(AMBFILE)) {
877     sprintf(op, " -af %s", vval(AMBFILE));
878     op += strlen(op);
879     } else
880     overture = 0;
881     switch (vscale(VARIABILITY)) {
882     case LOW:
883 greg 2.72 op = addarg(op, "-aa .125 -ad 512 -as 64");
884 greg 2.2 break;
885     case MEDIUM:
886 greg 2.72 op = addarg(op, "-aa .1 -ad 1536 -as 768");
887 greg 2.2 break;
888     case HIGH:
889 greg 2.72 op = addarg(op, "-aa .075 -ad 4096 -as 2048");
890 greg 2.2 break;
891     }
892     d = ambval();
893     sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
894     op += strlen(op);
895 greg 2.4 op = addarg(op, "-lr 12 -lw .0005");
896 greg 2.1 if (vdef(RENDER))
897     op = addarg(op, vval(RENDER));
898     }
899    
900    
901 schorsch 2.76 static void
902     xferopts( /* transfer options if indicated */
903     char *ro
904     )
905 greg 2.1 {
906     int fd, n;
907 greg 2.10 register char *cp;
908 greg 2.1
909     n = strlen(ro);
910     if (n < 2)
911     return;
912     if (vdef(OPTFILE)) {
913 greg 2.10 for (cp = ro; cp[1]; cp++)
914 greg 2.49 if (isspace(cp[1]) && (cp[2] == '@' ||
915     (cp[2] == '-' && isalpha(cp[3]))))
916 greg 2.10 *cp = '\n';
917     else
918     *cp = cp[1];
919     *cp = '\n';
920     fd = open(vval(OPTFILE), O_WRONLY|O_CREAT|O_TRUNC, 0666);
921     if (fd < 0 || write(fd, ro, n) != n || close(fd) < 0)
922 greg 2.3 syserr(vval(OPTFILE));
923 greg 2.15 sprintf(ro, " @%s", vval(OPTFILE));
924 greg 2.1 }
925 schorsch 2.63 #ifdef _WIN32
926 greg 2.1 else if (n > 50) {
927 greg 2.8 setenv("ROPT", ro+1);
928 greg 2.1 strcpy(ro, " $ROPT");
929     }
930     #endif
931     }
932    
933    
934 schorsch 2.76 static void
935     pfiltopts( /* get pfilt options */
936     register char *po
937     )
938 greg 2.1 {
939     *po = '\0';
940     if (vdef(EXPOSURE)) {
941     po = addarg(po, "-1 -e");
942     po = addarg(po, vval(EXPOSURE));
943     }
944 greg 2.12 switch (vscale(QUALITY)) {
945     case MEDIUM:
946 greg 2.61 po = addarg(po, "-r .6");
947 greg 2.12 break;
948     case HIGH:
949 greg 2.14 po = addarg(po, "-m .25");
950 greg 2.12 break;
951     }
952 greg 2.1 if (vdef(PFILT))
953     po = addarg(po, vval(PFILT));
954     }
955    
956    
957 schorsch 2.76 static int
958     matchword( /* match white-delimited words */
959     register char *s1,
960     register char *s2
961     )
962 greg 2.1 {
963     while (isspace(*s1)) s1++;
964     while (isspace(*s2)) s2++;
965     while (*s1 && !isspace(*s1))
966     if (*s1++ != *s2++)
967     return(0);
968     return(!*s2 || isspace(*s2));
969     }
970    
971    
972 schorsch 2.76 static char *
973     specview( /* get proper view spec from vs */
974     register char *vs
975     )
976 greg 2.1 {
977 greg 2.3 static char vup[7][12] = {"-vu 0 0 -1","-vu 0 -1 0","-vu -1 0 0",
978     "-vu 0 0 1", "-vu 1 0 0","-vu 0 1 0","-vu 0 0 1"};
979 greg 2.1 static char viewopts[128];
980     register char *cp;
981 greg 2.3 int xpos, ypos, zpos, viewtype, upax;
982     register int i;
983 greg 2.1 double cent[3], dim[3], mult, d;
984    
985 greg 2.9 if (vs == NULL || *vs == '-')
986 greg 2.1 return(vs);
987 greg 2.3 upax = 0; /* get the up vector */
988     if (vdef(UP)) {
989     if (vval(UP)[0] == '-' || vval(UP)[0] == '+')
990     upax = 1-'X'+UPPER(vval(UP)[1]);
991     else
992     upax = 1-'X'+vlet(UP);
993 schorsch 2.71 if ((upax < 1) | (upax > 3))
994 greg 2.3 badvalue(UP);
995     if (vval(UP)[0] == '-')
996     upax = -upax;
997     }
998 greg 2.1 /* check standard view names */
999     xpos = ypos = zpos = 0;
1000     if (*vs == 'X') {
1001     xpos = 1; vs++;
1002     } else if (*vs == 'x') {
1003     xpos = -1; vs++;
1004     }
1005     if (*vs == 'Y') {
1006     ypos = 1; vs++;
1007     } else if (*vs == 'y') {
1008     ypos = -1; vs++;
1009     }
1010     if (*vs == 'Z') {
1011     zpos = 1; vs++;
1012     } else if (*vs == 'z') {
1013     zpos = -1; vs++;
1014     }
1015 greg 2.84 switch (*vs) {
1016     case VT_PER:
1017     case VT_PAR:
1018     case VT_ANG:
1019     case VT_HEM:
1020     case VT_PLS:
1021     case VT_CYL:
1022 greg 2.1 viewtype = *vs++;
1023 greg 2.84 break;
1024     default:
1025     viewtype = VT_PER;
1026     break;
1027     }
1028 greg 2.1 cp = viewopts;
1029 greg 2.3 if ((!*vs || isspace(*vs)) && (xpos|ypos|zpos)) { /* got one! */
1030 greg 2.1 *cp++ = '-'; *cp++ = 'v'; *cp++ = 't'; *cp++ = viewtype;
1031     if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf",
1032     &cent[0], &dim[0], &cent[1], &dim[1],
1033 greg 2.3 &cent[2], &dim[2]) != 6)
1034     badvalue(ZONE);
1035     for (i = 0; i < 3; i++) {
1036     dim[i] -= cent[i];
1037 greg 2.39 if (dim[i] <= FTINY)
1038     badvalue(ZONE);
1039 greg 2.3 cent[i] += .5*dim[i];
1040 greg 2.1 }
1041 greg 2.3 mult = vlet(ZONE)=='E' ? 2. : .45 ;
1042 greg 2.1 sprintf(cp, " -vp %.2g %.2g %.2g -vd %.2g %.2g %.2g",
1043     cent[0]+xpos*mult*dim[0],
1044     cent[1]+ypos*mult*dim[1],
1045     cent[2]+zpos*mult*dim[2],
1046     -xpos*dim[0], -ypos*dim[1], -zpos*dim[2]);
1047     cp += strlen(cp);
1048 greg 2.3 /* redirect up axis if necessary */
1049     switch (upax) {
1050     case 3: /* plus or minus Z axis */
1051     case -3:
1052     case 0:
1053     if (!(xpos|ypos))
1054     upax = 2;
1055 greg 2.1 break;
1056 greg 2.3 case 2: /* plus or minus Y axis */
1057     case -2:
1058     if (!(xpos|zpos))
1059     upax = 1;
1060     break;
1061     case 1: /* plus or minus X axis */
1062     case -1:
1063     if (!(ypos|zpos))
1064     upax = 3;
1065     break;
1066 greg 2.1 }
1067 greg 2.3 cp = addarg(cp, vup[upax+3]);
1068 greg 2.1 switch (viewtype) {
1069 greg 2.84 case VT_PER:
1070 greg 2.1 cp = addarg(cp, "-vh 45 -vv 45");
1071     break;
1072 greg 2.84 case VT_PAR:
1073 greg 2.1 d = sqrt(dim[0]*dim[0]+dim[1]*dim[1]+dim[2]*dim[2]);
1074     sprintf(cp, " -vh %.2g -vv %.2g", d, d);
1075     cp += strlen(cp);
1076     break;
1077 greg 2.84 case VT_ANG:
1078     case VT_HEM:
1079     case VT_PLS:
1080 greg 2.1 cp = addarg(cp, "-vh 180 -vv 180");
1081 greg 2.44 break;
1082 greg 2.84 case VT_CYL:
1083 greg 2.44 cp = addarg(cp, "-vh 180 -vv 90");
1084 greg 2.1 break;
1085     }
1086 greg 2.3 } else {
1087 greg 2.4 while (!isspace(*vs)) /* else skip id */
1088     if (!*vs++)
1089     return(NULL);
1090 greg 2.3 if (upax) { /* specify up vector */
1091     strcpy(cp, vup[upax+3]);
1092     cp += strlen(cp);
1093     }
1094     }
1095 greg 2.26 if (cp == viewopts) /* append any additional options */
1096     vs++; /* skip prefixed space if unneeded */
1097     strcpy(cp, vs);
1098 schorsch 2.63 #ifdef _WIN32
1099 greg 2.8 if (strlen(viewopts) > 40) {
1100     setenv("VIEW", viewopts);
1101     return("$VIEW");
1102     }
1103     #endif
1104 greg 2.1 return(viewopts);
1105     }
1106    
1107    
1108 schorsch 2.76 static char *
1109     getview( /* get view n, or NULL if none */
1110     int n,
1111     char *vn /* returned view name */
1112     )
1113 greg 2.1 {
1114 greg 2.19 register char *mv;
1115 greg 2.1
1116 greg 2.8 if (viewselect != NULL) { /* command-line selected */
1117 greg 2.1 if (n) /* only do one */
1118     return(NULL);
1119     if (viewselect[0] == '-') { /* already specified */
1120     if (vn != NULL) *vn = '\0';
1121     return(viewselect);
1122     }
1123     if (vn != NULL) {
1124     for (mv = viewselect; *mv && !isspace(*mv);
1125     *vn++ = *mv++)
1126     ;
1127     *vn = '\0';
1128     }
1129 greg 2.19 /* view number? */
1130 greg 2.20 if (isint(viewselect))
1131 gregl 2.55 return(specview(nvalue(VIEWS, atoi(viewselect)-1)));
1132 greg 2.1 /* check list */
1133 gregl 2.55 while ((mv = nvalue(VIEWS, n++)) != NULL)
1134 greg 2.1 if (matchword(viewselect, mv))
1135     return(specview(mv));
1136     return(specview(viewselect)); /* standard view? */
1137     }
1138 gregl 2.55 mv = nvalue(VIEWS, n); /* use view n */
1139 schorsch 2.71 if ((vn != NULL) & (mv != NULL)) {
1140 greg 2.19 register char *mv2 = mv;
1141     if (*mv2 != '-')
1142     while (*mv2 && !isspace(*mv2))
1143     *vn++ = *mv2++;
1144 greg 2.1 *vn = '\0';
1145     }
1146 greg 2.8 return(specview(mv));
1147 greg 2.20 }
1148    
1149    
1150 schorsch 2.76 static int
1151     myprintview( /* print out selected view */
1152     register char *vopts,
1153     FILE *fp
1154     )
1155 greg 2.20 {
1156 gregl 2.55 VIEW vwr;
1157     char buf[128];
1158 greg 2.21 register char *cp;
1159 schorsch 2.80 #ifdef _WIN32
1160     /* XXX Should we allow something like this for all platforms? */
1161     /* XXX Or is it still required at all? */
1162     again:
1163     #endif
1164 greg 2.20 if (vopts == NULL)
1165     return(-1);
1166 schorsch 2.63 #ifdef _WIN32
1167 gregl 2.55 if (vopts[0] == '$') {
1168     vopts = getenv(vopts+1);
1169     goto again;
1170     }
1171 greg 2.24 #endif
1172 schorsch 2.70 vwr = stdview;
1173 gwlarson 2.59 sscanview(&vwr, cp=vopts); /* set initial options */
1174 gregl 2.55 while ((cp = strstr(cp, "-vf ")) != NULL &&
1175 gwlarson 2.59 *atos(buf, sizeof(buf), cp += 4)) {
1176     viewfile(buf, &vwr, NULL); /* load -vf file */
1177     sscanview(&vwr, cp); /* reset tail */
1178     }
1179 greg 2.67 fputs(VIEWSTR, fp);
1180     fprintview(&vwr, fp); /* print full spec. */
1181     fputc('\n', fp);
1182 greg 2.20 return(0);
1183 greg 2.1 }
1184    
1185    
1186 schorsch 2.76 static void
1187     rvu( /* run rvu with first view */
1188     char *opts,
1189     char *po
1190     )
1191 greg 2.1 {
1192 greg 2.21 char *vw;
1193 schorsch 2.65 char combuf[PATH_MAX];
1194 greg 2.1 /* build command */
1195 greg 2.41 if (touchonly || (vw = getview(0, NULL)) == NULL)
1196 greg 2.21 return;
1197     if (sayview)
1198 greg 2.67 myprintview(vw, stdout);
1199 greg 2.75 sprintf(combuf, "rvu %s%s%s -R %s ", vw, po, opts, rifname);
1200 greg 2.1 if (rvdevice != NULL)
1201     sprintf(combuf+strlen(combuf), "-o %s ", rvdevice);
1202 greg 2.29 if (vdef(EXPOSURE))
1203     sprintf(combuf+strlen(combuf), "-pe %s ", vval(EXPOSURE));
1204 greg 2.26 strcat(combuf, oct1name);
1205 greg 2.4 if (runcom(combuf)) { /* run it */
1206 greg 2.75 fprintf(stderr, "%s: error running rvu\n", progname);
1207 greg 2.53 quit(1);
1208 greg 2.1 }
1209     }
1210    
1211    
1212 schorsch 2.76 static void
1213     rpict( /* run rpict and pfilt for each view */
1214     char *opts,
1215     char *po
1216     )
1217 greg 2.1 {
1218 schorsch 2.65 char combuf[PATH_MAX];
1219     char rawfile[PATH_MAX], picfile[PATH_MAX];
1220     char zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32];
1221 greg 2.67 char rppopt[128], *pfile = NULL;
1222 greg 2.3 char pfopts[128];
1223 greg 2.1 char vs[32], *vw;
1224     int vn, mult;
1225 greg 2.67 FILE *fp;
1226 greg 2.41 time_t rfdt, pfdt;
1227 greg 2.1 /* get pfilt options */
1228     pfiltopts(pfopts);
1229     /* get resolution, reporting */
1230 greg 2.31 switch (vscale(QUALITY)) {
1231     case LOW:
1232     mult = 1;
1233     break;
1234     case MEDIUM:
1235     mult = 2;
1236     break;
1237     case HIGH:
1238     mult = 3;
1239     break;
1240     }
1241 greg 2.1 {
1242     int xres, yres;
1243     double aspect;
1244     int n;
1245     n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect);
1246     if (n == 3)
1247     sprintf(res, "-x %d -y %d -pa %.3f",
1248     mult*xres, mult*yres, aspect);
1249     else if (n) {
1250     if (n == 1) yres = xres;
1251     sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1252 greg 2.3 } else
1253     badvalue(RESOLUTION);
1254 greg 2.1 }
1255     rep[0] = '\0';
1256     if (vdef(REPORT)) {
1257     double minutes;
1258     int n;
1259     n = sscanf(vval(REPORT), "%lf %s", &minutes, rawfile);
1260     if (n == 2)
1261     sprintf(rep, " -t %d -e %s", (int)(minutes*60), rawfile);
1262     else if (n == 1)
1263     sprintf(rep, " -t %d", (int)(minutes*60));
1264 greg 2.3 else
1265     badvalue(REPORT);
1266 greg 2.1 }
1267 greg 2.67 /* set up parallel rendering */
1268 schorsch 2.71 if ((nprocs > 1) & (!vdef(ZFILE))) {
1269 greg 2.67 strcpy(rppopt, "-S 1 -PP pfXXXXXX");
1270     pfile = rppopt+9;
1271     if (mktemp(pfile) == NULL)
1272     pfile = NULL;
1273     }
1274 greg 2.66 vn = 0; /* do each view */
1275 greg 2.1 while ((vw = getview(vn++, vs)) != NULL) {
1276 greg 2.21 if (sayview)
1277 greg 2.67 myprintview(vw, stdout);
1278 greg 2.1 if (!vs[0])
1279     sprintf(vs, "%d", vn);
1280     sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs);
1281 greg 2.45 if (vdef(ZFILE))
1282     sprintf(zopt, " -z %s_%s.zbf", vval(ZFILE), vs);
1283     else
1284     zopt[0] = '\0';
1285 greg 2.1 /* check date on picture */
1286 greg 2.41 pfdt = fdate(picfile);
1287     if (pfdt >= oct1date)
1288 greg 2.1 continue;
1289 greg 2.41 /* get raw file name */
1290 greg 2.45 sprintf(rawfile, "%s_%s.unf",
1291     vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE), vs);
1292 greg 2.41 rfdt = fdate(rawfile);
1293     if (touchonly) { /* update times only */
1294     if (rfdt) {
1295     if (rfdt < oct1date)
1296     touch(rawfile);
1297     } else if (pfdt && pfdt < oct1date)
1298     touch(picfile);
1299     continue;
1300     }
1301 greg 2.67 if (next_process()) { /* parallel running? */
1302     if (pfile != NULL)
1303     sleep(20);
1304 greg 2.66 continue;
1305 greg 2.67 }
1306 greg 2.66 /* XXX Remember to call finish_process() */
1307 greg 2.1 /* build rpict command */
1308 greg 2.67 if (rfdt >= oct1date) { /* recover */
1309 greg 2.45 sprintf(combuf, "rpict%s%s%s%s -ro %s %s",
1310     rep, po, opts, zopt, rawfile, oct1name);
1311 greg 2.67 if (runcom(combuf)) /* run rpict */
1312     goto rperror;
1313     } else {
1314 greg 2.1 if (overture) { /* run overture calculation */
1315     sprintf(combuf,
1316     "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1317     rep, vw, opts,
1318 greg 2.26 oct1name, overfile);
1319 greg 2.4 if (runcom(combuf)) {
1320 greg 2.1 fprintf(stderr,
1321 greg 2.6 "%s: error in overture for view %s\n",
1322     progname, vs);
1323 greg 2.53 quit(1);
1324 greg 2.1 }
1325 schorsch 2.65 #ifndef NULL_DEVICE
1326 greg 2.6 rmfile(overfile);
1327     #endif
1328 greg 2.1 }
1329 greg 2.45 sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s",
1330 greg 2.67 rep, vw, res, po, opts,
1331     zopt, oct1name, rawfile);
1332     if (pfile != NULL && inchild()) {
1333     /* rpict persistent mode */
1334 greg 2.82 if (!silent)
1335 greg 2.67 printf("\t%s\n", combuf);
1336 greg 2.82 fflush(stdout);
1337 greg 2.67 sprintf(combuf, "rpict%s %s %s%s%s %s > %s",
1338     rep, rppopt, res, po, opts,
1339     oct1name, rawfile);
1340     fp = popen(combuf, "w");
1341     if (fp == NULL)
1342     goto rperror;
1343     myprintview(vw, fp);
1344     if (pclose(fp))
1345     goto rperror;
1346     } else { /* rpict normal mode */
1347     if (runcom(combuf))
1348     goto rperror;
1349     }
1350 greg 2.1 }
1351 greg 2.45 if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) {
1352 greg 2.1 /* build pfilt command */
1353 greg 2.45 if (mult > 1)
1354     sprintf(combuf, "pfilt%s -x /%d -y /%d %s > %s",
1355 greg 2.1 pfopts, mult, mult, rawfile, picfile);
1356 greg 2.45 else
1357     sprintf(combuf, "pfilt%s %s > %s", pfopts,
1358     rawfile, picfile);
1359 greg 2.67 if (runcom(combuf)) { /* run pfilt */
1360 greg 2.45 fprintf(stderr,
1361     "%s: error filtering view %s\n\t%s removed\n",
1362     progname, vs, picfile);
1363     unlink(picfile);
1364 greg 2.53 quit(1);
1365 greg 2.45 }
1366 greg 2.1 }
1367 greg 2.43 /* remove/rename raw file */
1368 greg 2.45 if (vdef(RAWFILE)) {
1369     sprintf(combuf, "%s_%s.pic", vval(RAWFILE), vs);
1370 greg 2.43 mvfile(rawfile, combuf);
1371     } else
1372     rmfile(rawfile);
1373 greg 2.73 finish_process(); /* exit if child */
1374 greg 2.4 }
1375 greg 2.66 wait_process(1); /* wait for children to finish */
1376 greg 2.67 if (pfile != NULL) { /* clean up rpict persistent mode */
1377 schorsch 2.81 RT_PID pid;
1378 greg 2.67 fp = fopen(pfile, "r");
1379     if (fp != NULL) {
1380     if (fscanf(fp, "%*s %d", &pid) != 1 ||
1381 greg 2.73 kill(pid, 1) < 0)
1382 greg 2.67 unlink(pfile);
1383     fclose(fp);
1384     }
1385     }
1386     return;
1387     rperror:
1388     fprintf(stderr, "%s: error rendering view %s\n", progname, vs);
1389     quit(1);
1390 greg 2.41 }
1391    
1392    
1393 schorsch 2.76 static int
1394     touch( /* update a file */
1395     char *fn
1396     )
1397 greg 2.41 {
1398     if (!silent)
1399     printf("\ttouch %s\n", fn);
1400 greg 2.66 if (!nprocs)
1401 greg 2.41 return(0);
1402     #ifdef notused
1403     if (access(fn, F_OK) == -1) /* create it */
1404     if (close(open(fn, O_WRONLY|O_CREAT, 0666)) == -1)
1405     return(-1);
1406     #endif
1407     return(setfdate(fn, time((time_t *)NULL)));
1408 greg 2.4 }
1409    
1410    
1411 schorsch 2.76 static int
1412     runcom( /* run command */
1413     char *cs
1414     )
1415 greg 2.4 {
1416     if (!silent) /* echo it */
1417     printf("\t%s\n", cs);
1418 greg 2.66 if (!nprocs)
1419 greg 2.4 return(0);
1420 greg 2.82 fflush(NULL); /* flush output and pass to shell */
1421 greg 2.4 return(system(cs));
1422     }
1423    
1424    
1425 schorsch 2.76 static int
1426     rmfile( /* remove a file */
1427     char *fn
1428     )
1429 greg 2.4 {
1430     if (!silent)
1431 schorsch 2.69 printf("\t%s %s\n", DELCMD, fn);
1432 greg 2.66 if (!nprocs)
1433 greg 2.4 return(0);
1434     return(unlink(fn));
1435 greg 2.43 }
1436    
1437    
1438 schorsch 2.76 static int
1439     mvfile( /* move a file */
1440     char *fold,
1441     char *fnew
1442     )
1443 greg 2.43 {
1444     if (!silent)
1445 schorsch 2.69 printf("\t%s %s %s\n", RENAMECMD, fold, fnew);
1446 greg 2.66 if (!nprocs)
1447 greg 2.43 return(0);
1448     return(rename(fold, fnew));
1449 greg 2.3 }
1450 greg 2.8
1451 greg 2.66
1452     #ifdef RHAS_FORK_EXEC
1453 schorsch 2.76 static int
1454     next_process(void) /* fork the next process (max. nprocs) */
1455 greg 2.66 {
1456 schorsch 2.81 RT_PID child_pid;
1457 greg 2.66
1458     if (nprocs <= 1)
1459     return(0); /* it's us or no one */
1460 greg 2.67 if (inchild()) {
1461 greg 2.73 fprintf(stderr, "%s: internal error 1 in next_process()\n",
1462 greg 2.66 progname);
1463     quit(1);
1464     }
1465     if (children_running >= nprocs)
1466     wait_process(0); /* wait for someone to finish */
1467 greg 2.82 fflush(NULL); /* flush output */
1468 greg 2.66 child_pid = fork(); /* split process */
1469     if (child_pid == 0) { /* we're the child */
1470     children_running = -1;
1471     return(0);
1472     }
1473     if (child_pid > 0) { /* we're the parent */
1474     ++children_running;
1475     return(1);
1476     }
1477     fprintf(stderr, "%s: warning -- fork() failed\n", progname);
1478     return(0);
1479     }
1480    
1481 schorsch 2.76 static void
1482     wait_process( /* wait for process(es) to finish */
1483     int all
1484     )
1485 greg 2.66 {
1486 schorsch 2.85 int ourstatus = 0, status;
1487     RT_PID pid;
1488 greg 2.66
1489     if (all)
1490     all = children_running;
1491     else if (children_running > 0)
1492     all = 1;
1493     while (all-- > 0) {
1494     pid = wait(&status);
1495     if (pid < 0)
1496     syserr(progname);
1497     status = status>>8 & 0xff;
1498     --children_running;
1499     if (status != 0) { /* child's problem is our problem */
1500 schorsch 2.71 if ((ourstatus == 0) & (children_running > 0))
1501 greg 2.66 fprintf(stderr, "%s: waiting for remaining processes\n",
1502     progname);
1503     ourstatus = status;
1504     all = children_running;
1505     }
1506     }
1507     if (ourstatus != 0)
1508     quit(ourstatus); /* bad status from child */
1509     }
1510     #else /* ! RHAS_FORK_EXEC */
1511 schorsch 2.76 static int
1512     next_process(void)
1513 greg 2.66 {
1514     return(0); /* cannot start new process */
1515     }
1516 schorsch 2.76 static void
1517 greg 2.66 wait_process(all)
1518     int all;
1519     {
1520     (void)all; /* no one to wait for */
1521 schorsch 2.68 }
1522     int
1523     kill(pid, sig) /* win|unix_process.c should also wait and kill */
1524 schorsch 2.85 RT_PID pid;
1525     int sig;
1526 schorsch 2.68 {
1527     return 0;
1528 greg 2.66 }
1529     #endif /* ! RHAS_FORK_EXEC */
1530    
1531 schorsch 2.76 static void
1532     finish_process(void) /* exit a child process */
1533 greg 2.66 {
1534 greg 2.67 if (!inchild())
1535 greg 2.66 return; /* in parent -- noop */
1536     exit(0);
1537     }
1538 greg 2.8
1539 schorsch 2.63 #ifdef _WIN32
1540 greg 2.8 setenv(vname, value) /* set an environment variable */
1541     char *vname, *value;
1542     {
1543     register char *evp;
1544    
1545     evp = bmalloc(strlen(vname)+strlen(value)+2);
1546     if (evp == NULL)
1547     syserr(progname);
1548     sprintf(evp, "%s=%s", vname, value);
1549     if (putenv(evp) != 0) {
1550     fprintf(stderr, "%s: out of environment space\n", progname);
1551 greg 2.53 quit(1);
1552 greg 2.8 }
1553     if (!silent)
1554     printf("set %s\n", evp);
1555     }
1556     #endif
1557 greg 2.3
1558    
1559 schorsch 2.76 static void
1560     badvalue( /* report bad variable value and exit */
1561     int vc
1562     )
1563 greg 2.3 {
1564     fprintf(stderr, "%s: bad value for variable '%s'\n",
1565     progname, vnam(vc));
1566 greg 2.53 quit(1);
1567 greg 2.3 }
1568    
1569    
1570 schorsch 2.76 static void
1571     syserr( /* report a system error and exit */
1572     char *s
1573     )
1574 greg 2.3 {
1575     perror(s);
1576 greg 2.53 quit(1);
1577     }
1578    
1579    
1580 greg 2.61 void
1581 greg 2.53 quit(ec) /* exit program */
1582     int ec;
1583     {
1584     exit(ec);
1585 greg 2.1 }