ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.76
Committed: Fri Mar 26 21:36:19 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.75: +179 -80 lines
Log Message:
Continued ANSIfication.

File Contents

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