ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.135
Committed: Sat Jun 7 05:09:46 2025 UTC (16 hours, 31 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.134: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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