ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.109
Committed: Fri Nov 8 18:08:07 2013 UTC (10 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.108: +47 -43 lines
Log Message:
Minor fixes that should not affect operation

File Contents

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