ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.133
Committed: Mon Dec 23 01:39:27 2024 UTC (2 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.132: +4 -3 lines
Log Message:
feat(rad): Increased command-line length limits

File Contents

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