ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.57
Committed: Wed Jun 10 17:51:06 1998 UTC (25 years, 10 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.56: +28 -26 lines
Log Message:
added EYESEP variable for stereo viewing

File Contents

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