ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.92
Committed: Mon Feb 9 16:41:29 2009 UTC (15 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R0
Changes since 2.91: +16 -10 lines
Log Message:
Fixed rad -v N specification to use actual view names from file

File Contents

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