ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.66
Committed: Thu Jul 3 15:00:19 2003 UTC (20 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.65: +98 -11 lines
Log Message:
Added -N option to rad for parallel rendering (preliminary w/o using -PP)

File Contents

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