ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.73
Committed: Sat Oct 18 04:46:24 2003 UTC (20 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.72: +4 -4 lines
Log Message:
Minor comment changes

File Contents

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