ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.5
Committed: Sat Mar 13 09:33:17 1993 UTC (31 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +4 -2 lines
Log Message:
minor change in setting of -ab

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