ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.111
Committed: Sun Oct 26 17:35:53 2014 UTC (9 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2
Changes since 2.110: +2 -1 lines
Log Message:
Fixing minor compiler warnings

File Contents

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