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

File Contents

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