ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.20
Committed: Tue Aug 24 21:44:55 1993 UTC (30 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +58 -4 lines
Log Message:
added -V option for spitting out view -- not foolproof with -vf

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