ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.130
Committed: Tue Jan 24 21:54:49 2023 UTC (14 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.129: +3 -3 lines
Log Message:
perf: Updated default -lw value to smaller tolerance

File Contents

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