ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.66
Committed: Thu Jul 3 15:00:19 2003 UTC (20 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.65: +98 -11 lines
Log Message:
Added -N option to rad for parallel rendering (preliminary w/o using -PP)

File Contents

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