ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.26
Committed: Fri Sep 24 16:49:02 1993 UTC (30 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.25: +213 -90 lines
Log Message:
changed fdate() return to unsigned long
added illum and mkillum variables for calling mkillum program
changed getoctcube() to call getbbox program with -n and no previous octree

File Contents

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