ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.10
Committed: Tue Feb 20 16:13:53 1996 UTC (28 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.9: +22 -1 lines
Log Message:
added RSH variable to rename or replace remote shell execution

File Contents

# Content
1 /* Copyright (c) 1995 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Radiance animation control program
9 */
10
11 #include "standard.h"
12 #include <ctype.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include "view.h"
16 #include "vars.h"
17 #include "netproc.h"
18 /* default remote shell */
19 #ifdef _AUX_SOURCE
20 #define REMSH "remsh"
21 #else
22 #define REMSH "rsh"
23 #endif
24 /* input variables */
25 #define HOST 0 /* rendering host machine */
26 #define RENDER 1 /* rendering options */
27 #define PFILT 2 /* pfilt options */
28 #define PINTERP 3 /* pinterp options */
29 #define OCTREE 4 /* octree file name */
30 #define DIRECTORY 5 /* working (sub)directory */
31 #define BASENAME 6 /* output image base name */
32 #define VIEWFILE 7 /* animation frame views */
33 #define START 8 /* starting frame number */
34 #define END 9 /* ending frame number */
35 #define RIF 10 /* rad input file */
36 #define NEXTANIM 11 /* next animation file */
37 #define ANIMATE 12 /* animation command */
38 #define TRANSFER 13 /* frame transfer command */
39 #define ARCHIVE 14 /* archiving command */
40 #define INTERP 15 /* # frames to interpolate */
41 #define OVERSAMP 16 /* # times to oversample image */
42 #define MBLUR 17 /* samples for motion blur */
43 #define RTRACE 18 /* use rtrace with pinterp? */
44 #define DISKSPACE 19 /* how much disk space to use */
45 #define RESOLUTION 20 /* desired final resolution */
46 #define EXPOSURE 21 /* how to compute exposure */
47 #define RSH 22 /* remote shell script or program */
48
49 int NVARS = 23; /* total number of variables */
50
51 VARIABLE vv[] = { /* variable-value pairs */
52 {"host", 4, 0, NULL, NULL},
53 {"render", 3, 0, NULL, catvalues},
54 {"pfilt", 2, 0, NULL, catvalues},
55 {"pinterp", 2, 0, NULL, catvalues},
56 {"OCTREE", 3, 0, NULL, onevalue},
57 {"DIRECTORY", 3, 0, NULL, onevalue},
58 {"BASENAME", 3, 0, NULL, onevalue},
59 {"VIEWFILE", 2, 0, NULL, onevalue},
60 {"START", 2, 0, NULL, intvalue},
61 {"END", 2, 0, NULL, intvalue},
62 {"RIF", 3, 0, NULL, onevalue},
63 {"NEXTANIM", 3, 0, NULL, onevalue},
64 {"ANIMATE", 2, 0, NULL, onevalue},
65 {"TRANSFER", 2, 0, NULL, onevalue},
66 {"ARCHIVE", 2, 0, NULL, onevalue},
67 {"INTERPOLATE", 3, 0, NULL, intvalue},
68 {"OVERSAMPLE", 2, 0, NULL, fltvalue},
69 {"MBLUR", 2, 0, NULL, onevalue},
70 {"RTRACE", 2, 0, NULL, boolvalue},
71 {"DISKSPACE", 3, 0, NULL, fltvalue},
72 {"RESOLUTION", 3, 0, NULL, onevalue},
73 {"EXPOSURE", 3, 0, NULL, onevalue},
74 {"RSH", 3, 0, NULL, onevalue},
75 };
76
77 #define SFNAME "STATUS" /* status file name */
78
79 struct {
80 char host[64]; /* control host name */
81 int pid; /* control process id */
82 char cfname[128]; /* control file name */
83 int rnext; /* next frame to render */
84 int fnext; /* next frame to filter */
85 int tnext; /* next frame to transfer */
86 } astat; /* animation status */
87
88 char *progname; /* our program name */
89 char *cfname; /* our control file name */
90
91 int nowarn = 0; /* turn warnings off? */
92 int silent = 0; /* silent mode? */
93 int noaction = 0; /* take no action? */
94
95 char *remsh; /* remote shell program/script */
96 char rendopt[2048] = ""; /* rendering options */
97 char rresopt[32]; /* rendering resolution options */
98 char fresopt[32]; /* filter resolution options */
99 int pfiltalways; /* always use pfilt? */
100
101 char arcargs[10240]; /* files to archive */
102 char *arcfirst, *arcnext; /* pointers to first and next argument */
103
104 struct pslot {
105 int pid; /* process ID (0 if empty) */
106 int fout; /* output frame number */
107 int (*rcvf)(); /* recover function */
108 } *pslot; /* process slots */
109 int npslots; /* number of process slots */
110
111 int lastpid; /* ID of last completed background process */
112 PSERVER *lastpserver; /* last process server used */
113
114 #define phostname(ps) ((ps)->hostname[0] ? (ps)->hostname : astat.host)
115
116 struct pslot *findpslot();
117
118 VIEW *getview();
119 char *getexp();
120
121
122 main(argc, argv)
123 int argc;
124 char *argv[];
125 {
126 int explicate = 0;
127 int i;
128
129 progname = argv[0]; /* get arguments */
130 for (i = 1; i < argc && argv[i][0] == '-'; i++)
131 switch (argv[i][1]) {
132 case 'e': /* print variables */
133 explicate++;
134 break;
135 case 'w': /* turn off warnings */
136 nowarn++;
137 break;
138 case 's': /* silent mode */
139 silent++;
140 break;
141 case 'n': /* take no action */
142 noaction++;
143 break;
144 default:
145 goto userr;
146 }
147 if (i != argc-1)
148 goto userr;
149 cfname = argv[i];
150 /* load variables */
151 loadvars(cfname);
152 /* did we get DIRECTORY? */
153 checkdir();
154 /* check status */
155 if (getastat() < 0) {
156 fprintf(stderr, "%s: exiting\n", progname);
157 quit(1);
158 }
159 /* pfilt always if options given */
160 pfiltalways = vdef(PFILT);
161 /* load RIF if any */
162 if (vdef(RIF))
163 getradfile(vval(RIF));
164 /* set defaults */
165 setdefaults();
166 /* print variables */
167 if (explicate)
168 printvars(stdout);
169 /* set up process servers */
170 sethosts();
171 /* run animation */
172 animate();
173 /* all done */
174 if (vdef(NEXTANIM)) {
175 argv[i] = vval(NEXTANIM); /* just change input file */
176 if (!silent)
177 printargs(argc, argv, stdout);
178 execvp(progname, argv); /* pass to next */
179 quit(1); /* shouldn't return */
180 }
181 quit(0);
182 userr:
183 fprintf(stderr, "Usage: %s [-s][-n][-w][-e] anim_file\n", progname);
184 quit(1);
185 }
186
187
188 getastat() /* check/set animation status */
189 {
190 char buf[256];
191 FILE *fp;
192
193 sprintf(buf, "%s/%s", vval(DIRECTORY), SFNAME);
194 if ((fp = fopen(buf, "r")) == NULL) {
195 if (errno != ENOENT) {
196 perror(buf);
197 return(-1);
198 }
199 astat.rnext = astat.fnext = astat.tnext = 0;
200 goto setours;
201 }
202 if (fscanf(fp, "Control host: %s\n", astat.host) != 1)
203 goto fmterr;
204 if (fscanf(fp, "Control PID: %d\n", &astat.pid) != 1)
205 goto fmterr;
206 if (fscanf(fp, "Control file: %s\n", astat.cfname) != 1)
207 goto fmterr;
208 if (fscanf(fp, "Next render: %d\n", &astat.rnext) != 1)
209 goto fmterr;
210 if (fscanf(fp, "Next filter: %d\n", &astat.fnext) != 1)
211 goto fmterr;
212 if (fscanf(fp, "Next transfer: %d\n", &astat.tnext) != 1)
213 goto fmterr;
214 fclose(fp);
215 if (astat.pid != 0) { /* thinks it's still running */
216 if (strcmp(myhostname(), astat.host)) {
217 fprintf(stderr,
218 "%s: process %d may still be running on host %s\n",
219 progname, astat.pid, astat.host);
220 return(-1);
221 }
222 if (kill(astat.pid, 0) != -1 || errno != ESRCH) {
223 fprintf(stderr, "%s: process %d is still running\n",
224 progname, astat.pid);
225 return(-1);
226 }
227 /* assume it is dead */
228 }
229 if (strcmp(cfname, astat.cfname) && astat.tnext != 0) { /* other's */
230 fprintf(stderr, "%s: unfinished job \"%s\"\n",
231 progname, astat.cfname);
232 return(-1);
233 }
234 setours: /* set our values */
235 strcpy(astat.host, myhostname());
236 astat.pid = getpid();
237 strcpy(astat.cfname, cfname);
238 return(0);
239 fmterr:
240 fprintf(stderr, "%s: format error in status file \"%s\"\n",
241 progname, buf);
242 fclose(fp);
243 return(-1);
244 }
245
246
247 putastat() /* put out current status */
248 {
249 char buf[256];
250 FILE *fp;
251
252 if (noaction)
253 return;
254 sprintf(buf, "%s/%s", vval(DIRECTORY), SFNAME);
255 if ((fp = fopen(buf, "w")) == NULL) {
256 perror(buf);
257 quit(1);
258 }
259 fprintf(fp, "Control host: %s\n", astat.host);
260 fprintf(fp, "Control PID: %d\n", astat.pid);
261 fprintf(fp, "Control file: %s\n", astat.cfname);
262 fprintf(fp, "Next render: %d\n", astat.rnext);
263 fprintf(fp, "Next filter: %d\n", astat.fnext);
264 fprintf(fp, "Next transfer: %d\n", astat.tnext);
265 fclose(fp);
266 }
267
268
269 checkdir() /* make sure we have our directory */
270 {
271 struct stat stb;
272
273 if (!vdef(DIRECTORY)) {
274 fprintf(stderr, "%s: %s undefined\n",
275 progname, vnam(DIRECTORY));
276 quit(1);
277 }
278 if (stat(vval(DIRECTORY), &stb) == -1) {
279 if (errno == ENOENT && mkdir(vval(DIRECTORY), 0777) == 0)
280 return;
281 perror(vval(DIRECTORY));
282 quit(1);
283 }
284 if (!(stb.st_mode & S_IFDIR)) {
285 fprintf(stderr, "%s: not a directory\n", vval(DIRECTORY));
286 quit(1);
287 }
288 }
289
290
291 setdefaults() /* set default values */
292 {
293 extern char *atos();
294 char buf[256];
295
296 if (vdef(ANIMATE)) {
297 vval(OCTREE) = NULL;
298 vdef(OCTREE) = 0;
299 } else if (!vdef(OCTREE)) {
300 fprintf(stderr, "%s: either %s or %s must be defined\n",
301 progname, vnam(OCTREE), vnam(ANIMATE));
302 quit(1);
303 }
304 if (!vdef(VIEWFILE)) {
305 fprintf(stderr, "%s: %s undefined\n", progname, vnam(VIEWFILE));
306 quit(1);
307 }
308 if (!vdef(HOST)) {
309 vval(HOST) = LHOSTNAME;
310 vdef(HOST)++;
311 }
312 if (!vdef(START)) {
313 vval(START) = "1";
314 vdef(START)++;
315 }
316 if (!vdef(END)) {
317 sprintf(buf, "%d", countviews()+vint(START)-1);
318 vval(END) = savqstr(buf);
319 vdef(END)++;
320 }
321 if (vint(END) < vint(START)) {
322 fprintf(stderr, "%s: ending frame less than starting frame\n",
323 progname);
324 quit(1);
325 }
326 if (!vdef(BASENAME)) {
327 sprintf(buf, "%s/frame%%03d", vval(DIRECTORY));
328 vval(BASENAME) = savqstr(buf);
329 vdef(BASENAME)++;
330 }
331 if (!vdef(RESOLUTION)) {
332 vval(RESOLUTION) = "640";
333 vdef(RESOLUTION)++;
334 }
335 if (!vdef(OVERSAMP)) {
336 vval(OVERSAMP) = "2";
337 vdef(OVERSAMP)++;
338 }
339 if (!vdef(INTERP)) {
340 vval(INTERP) = "0";
341 vdef(INTERP)++;
342 }
343 if (!vdef(MBLUR)) {
344 vval(MBLUR) = "0";
345 vdef(MBLUR)++;
346 }
347 if (!vdef(RTRACE)) {
348 vval(RTRACE) = "F";
349 vdef(RTRACE)++;
350 }
351 if (!vdef(DISKSPACE)) {
352 if (!nowarn)
353 fprintf(stderr,
354 "%s: warning - no %s setting, assuming 100 Mbytes available\n",
355 progname, vnam(DISKSPACE));
356 vval(DISKSPACE) = "100";
357 vdef(DISKSPACE)++;
358 }
359 if (!vdef(RSH)) {
360 vval(RSH) = REMSH;
361 vdef(RSH)++;
362 }
363 /* locate remote shell program */
364 atos(buf, sizeof(buf), vval(RSH));
365 if ((remsh = getpath(buf, getenv("PATH"), X_OK)) != NULL)
366 remsh = savqstr(remsh);
367 else
368 remsh = vval(RSH); /* will generate error if used */
369
370 /* append rendering options */
371 if (vdef(RENDER))
372 sprintf(rendopt+strlen(rendopt), " %s", vval(RENDER));
373 }
374
375
376 sethosts() /* set up process servers */
377 {
378 extern char *iskip();
379 char buf[256], *dir, *uname;
380 int np;
381 register char *cp;
382 int i;
383
384 npslots = 0;
385 if (noaction)
386 return;
387 for (i = 0; i < vdef(HOST); i++) { /* add each host */
388 dir = uname = NULL;
389 np = 1;
390 strcpy(cp=buf, nvalue(HOST, i)); /* copy to buffer */
391 cp = sskip(cp); /* skip host name */
392 while (isspace(*cp))
393 *cp++ = '\0';
394 if (*cp) { /* has # processes? */
395 np = atoi(cp);
396 if ((cp = iskip(cp)) == NULL || (*cp && !isspace(*cp)))
397 badvalue(HOST);
398 while (isspace(*cp))
399 cp++;
400 if (*cp) { /* has directory? */
401 dir = cp;
402 cp = sskip(cp); /* skip dir. */
403 while (isspace(*cp))
404 *cp++ = '\0';
405 if (*cp) { /* has user? */
406 uname = cp;
407 if (*sskip(cp))
408 badvalue(HOST);
409 }
410 }
411 }
412 if (addpserver(buf, dir, uname, np) == NULL) {
413 if (!nowarn)
414 fprintf(stderr,
415 "%s: cannot execute on host \"%s\"\n",
416 progname, buf);
417 } else
418 npslots += np;
419 }
420 if (npslots == 0) {
421 fprintf(stderr, "%s: no working process servers\n", progname);
422 quit(1);
423 }
424 pslot = (struct pslot *)calloc(npslots, sizeof(struct pslot));
425 if (pslot == NULL) {
426 perror("malloc");
427 quit(1);
428 }
429 }
430
431
432 getradfile(rfargs) /* run rad and get needed variables */
433 char *rfargs;
434 {
435 static short mvar[] = {OCTREE,PFILT,RESOLUTION,EXPOSURE,-1};
436 char combuf[256];
437 register int i;
438 register char *cp;
439 /* create rad command */
440 sprintf(rendopt, " @%s/render.opt", vval(DIRECTORY));
441 sprintf(combuf,
442 "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
443 rfargs, rendopt+2);
444 cp = combuf;
445 while (*cp) cp++; /* match unset variables */
446 for (i = 0; mvar[i] >= 0; i++)
447 if (!vdef(mvar[i])) {
448 *cp++ = '|';
449 strcpy(cp, vnam(mvar[i]));
450 while (*cp) cp++;
451 }
452 sprintf(cp, ")[ \t]*=' > %s/radset.var", vval(DIRECTORY));
453 cp += 11; /* point to file name */
454 if (system(combuf)) {
455 fprintf(stderr, "%s: error executing rad command:\n\t%s\n",
456 progname, combuf);
457 quit(1);
458 }
459 loadvars(cp); /* load variables and remove file */
460 unlink(cp);
461 }
462
463
464 animate() /* run animation */
465 {
466 int xres, yres;
467 float pa, mult;
468 int frames_batch;
469 register int i;
470 double d1, d2;
471 /* compute rpict resolution */
472 i = sscanf(vval(RESOLUTION), "%d %d %f", &xres, &yres, &pa);
473 mult = vflt(OVERSAMP);
474 if (i == 3) {
475 sprintf(rresopt, "-x %d -y %d -pa %f", (int)(mult*xres),
476 (int)(mult*yres), pa);
477 sprintf(fresopt, "-x %d -y %d -pa %f", xres, yres, pa);
478 } else if (i) {
479 if (i == 1) yres = xres;
480 sprintf(rresopt, "-x %d -y %d", (int)(mult*xres),
481 (int)(mult*yres));
482 sprintf(fresopt, "-x %d -y %d -pa 1", xres, yres);
483 } else
484 badvalue(RESOLUTION);
485 /* consistency checks */
486 if (vdef(ANIMATE)) {
487 if (vint(INTERP)) {
488 if (!nowarn)
489 fprintf(stderr,
490 "%s: resetting %s=0 for animation\n",
491 progname, vnam(INTERP));
492 vval(INTERP) = "0";
493 }
494 if (atoi(vval(MBLUR))) { /* can't handle this yet */
495 if (!nowarn)
496 fprintf(stderr,
497 "%s: resetting %s=0 for animation\n",
498 progname, vnam(MBLUR));
499 vval(MBLUR) = "0";
500 }
501 }
502 /* figure # frames per batch */
503 d1 = mult*xres*mult*yres*4; /* space for orig. picture */
504 if ((i=vint(INTERP)) || atoi(vval(MBLUR)))
505 d1 += mult*xres*mult*yres*4; /* space for z-buffer */
506 d2 = xres*yres*4; /* space for final picture */
507 frames_batch = (i+1)*(vflt(DISKSPACE)*1048576.-d1)/(d1+i*d2);
508 if (frames_batch < i+2) {
509 fprintf(stderr, "%s: insufficient disk space allocated\n",
510 progname);
511 quit(1);
512 }
513 /* initialize archive argument list */
514 i = 16;
515 if (vdef(ARCHIVE) && strlen(vval(ARCHIVE)) > i)
516 i = strlen(vval(ARCHIVE));
517 arcnext = arcfirst = arcargs + i;
518 /* initialize status file */
519 if (astat.rnext == 0)
520 astat.rnext = astat.fnext = astat.tnext = vint(START);
521 putastat();
522 /* render in batches */
523 while (astat.tnext <= vint(END)) {
524 renderframes(frames_batch);
525 filterframes();
526 transferframes();
527 }
528 /* mark status as finished */
529 astat.pid = 0;
530 putastat();
531 /* close open files */
532 getview(0);
533 getexp(0);
534 }
535
536
537 renderframes(nframes) /* render next nframes frames */
538 int nframes;
539 {
540 static char vendbuf[16];
541 VIEW *vp;
542 FILE *fp = NULL;
543 char vfname[128];
544 int lastframe;
545 register int i;
546
547 if (astat.tnext < astat.rnext) /* other work to do first */
548 return;
549 /* create batch view file */
550 if (!vdef(ANIMATE)) {
551 sprintf(vfname, "%s/anim.vf", vval(DIRECTORY));
552 if ((fp = fopen(vfname, "w")) == NULL) {
553 perror(vfname);
554 quit(1);
555 }
556 }
557 /* bound batch properly */
558 lastframe = astat.rnext + nframes - 1;
559 if ((lastframe-1) % (vint(INTERP)+1)) /* need even interval */
560 lastframe += vint(INTERP)+1 - ((lastframe-1)%(vint(INTERP)+1));
561 if (lastframe > vint(END)) /* check for end */
562 lastframe = vint(END);
563 /* render each view */
564 for (i = astat.rnext; i <= lastframe; i++) {
565 if ((vp = getview(i)) == NULL) {
566 if (!nowarn)
567 fprintf(stderr,
568 "%s: ran out of views before last frame\n",
569 progname);
570 sprintf(vval(END)=vendbuf, "%d", i-1);
571 lastframe = i - 1;
572 break;
573 }
574 if (vdef(ANIMATE)) /* animate frame */
575 animrend(i, vp);
576 else { /* else record it */
577 fputs(VIEWSTR, fp);
578 fprintview(vp, fp);
579 putc('\n', fp);
580 }
581 }
582 if (vdef(ANIMATE)) /* wait for renderings to finish */
583 bwait(0);
584 else { /* else if walk-through */
585 fclose(fp); /* close view file */
586 walkwait(astat.rnext, lastframe, vfname); /* walk it */
587 unlink(vfname); /* remove view file */
588 }
589 astat.rnext = i; /* update status */
590 putastat();
591 }
592
593
594 filterframes() /* catch up with filtering */
595 {
596 VIEW *vp;
597 register int i;
598
599 if (astat.tnext < astat.fnext) /* other work to do first */
600 return;
601 /* filter each view */
602 for (i = astat.fnext; i < astat.rnext; i++) {
603 if ((vp = getview(i)) == NULL) { /* get view i */
604 fprintf(stderr,
605 "%s: unexpected error reading view for frame %d\n",
606 progname, i);
607 quit(1);
608 }
609 dofilt(i, vp, getexp(i), 0); /* filter frame */
610 }
611 bwait(0); /* wait for filter processes */
612 archive(); /* archive originals */
613 astat.fnext = i; /* update status */
614 putastat();
615 }
616
617
618 transferframes() /* catch up with picture transfers */
619 {
620 char combuf[10240];
621 register char *cp;
622 register int i;
623
624 if (astat.tnext >= astat.fnext) /* nothing to do, yet */
625 return;
626 if (!vdef(TRANSFER)) { /* no transfer function -- leave 'em */
627 astat.tnext = astat.fnext;
628 putastat(); /* update status */
629 return;
630 }
631 strcpy(combuf, vval(TRANSFER)); /* start transfer command */
632 cp = combuf + strlen(combuf);
633 /* make argument list */
634 for (i = astat.tnext; i < astat.fnext; i++) {
635 *cp++ = ' ';
636 sprintf(cp, vval(BASENAME), i);
637 while (*cp) cp++;
638 strcpy(cp, ".pic");
639 cp += 4;
640 }
641 if (runcom(combuf)) { /* transfer frames */
642 fprintf(stderr, "%s: error running transfer command\n",
643 progname);
644 quit(1);
645 }
646 astat.tnext = i; /* update status */
647 putastat();
648 }
649
650
651 animrend(frame, vp) /* start animation frame */
652 int frame;
653 VIEW *vp;
654 {
655 extern int recover();
656 char combuf[2048];
657 char fname[128];
658
659 sprintf(fname, vval(BASENAME), frame);
660 strcat(fname, ".unf");
661 if (access(fname, F_OK) == 0)
662 return;
663 sprintf(combuf, "%s %d | rpict%s%s -w0 %s > %s", vval(ANIMATE), frame,
664 rendopt, viewopt(vp), rresopt, fname);
665 bruncom(combuf, frame, recover); /* run in background */
666 }
667
668
669 walkwait(first, last, vfn) /* walk-through frames */
670 int first, last;
671 char *vfn;
672 {
673 char combuf[2048];
674 char *inspoint;
675 register int i;
676
677 if (!noaction && vint(INTERP)) /* create dummy frames */
678 for (i = first; i <= last; i++)
679 if (i < vint(END) && (i-1) % (vint(INTERP)+1)) {
680 sprintf(combuf, vval(BASENAME), i);
681 strcat(combuf, ".unf");
682 close(open(combuf, O_RDONLY|O_CREAT, 0666));
683 }
684 /* create command */
685 sprintf(combuf, "rpict%s -w0", rendopt);
686 if (vint(INTERP) || atoi(vval(MBLUR)))
687 sprintf(combuf+strlen(combuf), " -z %s.zbf", vval(BASENAME));
688 sprintf(combuf+strlen(combuf), " -o %s.unf %s -S %d",
689 vval(BASENAME), rresopt, first);
690 inspoint = combuf + strlen(combuf);
691 sprintf(inspoint, " %s < %s", vval(OCTREE), vfn);
692 /* run in parallel */
693 i = (last-first+1)/(vint(INTERP)+1);
694 if (i < 1) i = 1;
695 if (pruncom(combuf, inspoint, i)) {
696 fprintf(stderr, "%s: error rendering frames %d through %d\n",
697 progname, first, last);
698 quit(1);
699 }
700 if (!noaction && vint(INTERP)) /* remove dummy frames */
701 for (i = first; i <= last; i++)
702 if (i < vint(END) && (i-1) % (vint(INTERP)+1)) {
703 sprintf(combuf, vval(BASENAME), i);
704 strcat(combuf, ".unf");
705 unlink(combuf);
706 }
707 }
708
709
710 int
711 recover(frame) /* recover the specified frame */
712 int frame;
713 {
714 static int *rfrm; /* list of recovered frames */
715 static int nrfrms = 0;
716 char combuf[2048];
717 char fname[128];
718 register char *cp;
719 register int i;
720 /* check to see if recovered already */
721 for (i = nrfrms; i--; )
722 if (rfrm[i] == frame)
723 return(0);
724 /* build command */
725 sprintf(fname, vval(BASENAME), frame);
726 if (vdef(ANIMATE))
727 sprintf(combuf, "%s %d | rpict%s -w0",
728 vval(ANIMATE), frame, rendopt);
729 else
730 sprintf(combuf, "rpict%s -w0", rendopt);
731 cp = combuf + strlen(combuf);
732 if (vint(INTERP) || atoi(vval(MBLUR))) {
733 sprintf(cp, " -z %s.zbf", fname);
734 while (*cp) cp++;
735 }
736 sprintf(cp, " -ro %s.unf", fname);
737 while (*cp) cp++;
738 if (!vdef(ANIMATE)) {
739 *cp++ = ' ';
740 strcpy(cp, vval(OCTREE));
741 }
742 if (runcom(combuf)) /* run command */
743 return(1);
744 /* add frame to recovered list */
745 if (nrfrms)
746 rfrm = (int *)realloc((char *)rfrm, (nrfrms+1)*sizeof(int));
747 else
748 rfrm = (int *)malloc(sizeof(int));
749 if (rfrm == NULL) {
750 perror("malloc");
751 quit(1);
752 }
753 rfrm[nrfrms++] = frame;
754 return(0);
755 }
756
757
758 int
759 frecover(frame) /* recover filtered frame */
760 int frame;
761 {
762 VIEW *vp;
763 char *ex;
764
765 vp = getview(frame);
766 ex = getexp(frame);
767 if (dofilt(frame, vp, ex, 2) && dofilt(frame, vp, ex, 1))
768 return(1);
769 return(0);
770 }
771
772
773 archive() /* archive and remove renderings */
774 {
775 #define RMCOML (sizeof(rmcom)-1)
776 static char rmcom[] = "rm -f";
777 register int i;
778
779 if (arcnext == arcfirst)
780 return; /* nothing to do */
781 if (vdef(ARCHIVE)) { /* run archive command */
782 i = strlen(vval(ARCHIVE));
783 strncpy(arcfirst-i, vval(ARCHIVE), i);
784 if (runcom(arcfirst-i)) {
785 fprintf(stderr, "%s: error running archive command\n",
786 progname);
787 quit(1);
788 }
789 }
790 /* run remove command */
791 strncpy(arcfirst-RMCOML, rmcom, RMCOML);
792 runcom(arcfirst-RMCOML);
793 arcnext = arcfirst; /* reset argument list */
794 #undef RMCOML
795 }
796
797
798 int
799 dofilt(frame, vp, ep, rvr) /* filter frame */
800 int frame;
801 VIEW *vp;
802 char *ep;
803 int rvr;
804 {
805 extern int frecover();
806 static int iter = 0;
807 char fnbefore[128], fnafter[128];
808 char combuf[1024], fname0[128], fname1[128];
809 int usepinterp, usepfilt, nora_rgbe;
810 int frseq[2];
811 /* check what is needed */
812 usepinterp = atoi(vval(MBLUR));
813 usepfilt = pfiltalways | ep==NULL;
814 if (ep != NULL && !strcmp(ep, "1"))
815 ep = "+0";
816 nora_rgbe = strcmp(vval(OVERSAMP),"1") || ep==NULL ||
817 *ep != '+' || *ep != '-' || !isint(ep);
818 /* compute rendered views */
819 frseq[0] = frame - ((frame-1) % (vint(INTERP)+1));
820 frseq[1] = frseq[0] + vint(INTERP) + 1;
821 if (frseq[1] > vint(END))
822 frseq[1] = vint(END);
823 if (frseq[1] == frame) { /* pfilt only */
824 frseq[0] = frseq[1];
825 usepinterp = 0; /* update what's needed */
826 usepfilt |= nora_rgbe;
827 } else if (frseq[0] == frame) { /* no interpolation needed */
828 if (!rvr && frame > 1+vint(INTERP)) { /* archive previous */
829 *arcnext++ = ' ';
830 sprintf(arcnext, vval(BASENAME), frame-vint(INTERP)-1);
831 while (*arcnext) arcnext++;
832 strcpy(arcnext, ".unf");
833 arcnext += 4;
834 if (usepinterp || vint(INTERP)) { /* and z-buf */
835 *arcnext++ = ' ';
836 sprintf(arcnext, vval(BASENAME),
837 frame-vint(INTERP)-1);
838 while (*arcnext) arcnext++;
839 strcpy(arcnext, ".zbf");
840 arcnext += 4;
841 }
842 }
843 if (!usepinterp) /* update what's needed */
844 usepfilt |= nora_rgbe;
845 } else /* interpolation needed */
846 usepinterp++;
847 if (frseq[1] >= astat.rnext) /* next batch unavailable */
848 frseq[1] = frseq[0];
849 sprintf(fnbefore, vval(BASENAME), frseq[0]);
850 sprintf(fnafter, vval(BASENAME), frseq[1]);
851 if (rvr == 1 && recover(frseq[0])) /* recover before frame? */
852 return(1);
853 /* generate command */
854 if (usepinterp) { /* using pinterp */
855 if (rvr == 2 && recover(frseq[1])) /* recover after? */
856 return(1);
857 if (atoi(vval(MBLUR))) {
858 FILE *fp; /* motion blurring */
859 sprintf(fname0, "%s/vw0%c", vval(DIRECTORY),
860 'a'+(iter%26));
861 if ((fp = fopen(fname0, "w")) == NULL) {
862 perror(fname0); quit(1);
863 }
864 fputs(VIEWSTR, fp);
865 fprintview(vp, fp);
866 putc('\n', fp); fclose(fp);
867 if ((vp = getview(frame+1)) == NULL) {
868 fprintf(stderr,
869 "%s: unexpected error reading view for frame %d\n",
870 progname, frame+1);
871 quit(1);
872 }
873 sprintf(fname1, "%s/vw1%c", vval(DIRECTORY),
874 'a'+(iter%26));
875 if ((fp = fopen(fname1, "w")) == NULL) {
876 perror(fname1); quit(1);
877 }
878 fputs(VIEWSTR, fp);
879 fprintview(vp, fp);
880 putc('\n', fp); fclose(fp);
881 sprintf(combuf,
882 "(pmblur %s %d %s %s; rm -f %s %s) | pinterp -B",
883 *sskip(vval(MBLUR)) ? sskip2(vval(MBLUR),1) : "1",
884 atoi(vval(MBLUR)),
885 fname0, fname1, fname0, fname1);
886 iter++;
887 } else /* no blurring */
888 strcpy(combuf, "pinterp");
889 strcat(combuf, viewopt(vp));
890 if (vbool(RTRACE))
891 sprintf(combuf+strlen(combuf), " -ff -fr '%s -w0 %s'",
892 rendopt, vval(OCTREE));
893 if (vdef(PINTERP))
894 sprintf(combuf+strlen(combuf), " %s", vval(PINTERP));
895 if (usepfilt)
896 sprintf(combuf+strlen(combuf), " %s", rresopt);
897 else
898 sprintf(combuf+strlen(combuf), " %s -e %s",
899 fresopt, ep);
900 sprintf(combuf+strlen(combuf), " %s.unf %s.zbf",
901 fnbefore, fnbefore);
902 if (frseq[1] != frseq[0])
903 sprintf(combuf+strlen(combuf), " %s.unf %s.zbf",
904 fnafter, fnafter);
905 if (usepfilt) { /* also pfilt */
906 if (vdef(PFILT))
907 sprintf(combuf+strlen(combuf), " | pfilt %s",
908 vval(PFILT));
909 else
910 strcat(combuf, " | pfilt");
911 if (ep != NULL)
912 sprintf(combuf+strlen(combuf), " -1 -e %s %s",
913 ep, fresopt);
914 else
915 sprintf(combuf+strlen(combuf), " %s", fresopt);
916 }
917 } else if (usepfilt) { /* pfilt only */
918 if (rvr == 2)
919 return(1);
920 if (vdef(PFILT))
921 sprintf(combuf, "pfilt %s", vval(PFILT));
922 else
923 strcpy(combuf, "pfilt");
924 if (ep != NULL)
925 sprintf(combuf+strlen(combuf), " -1 -e %s %s %s.unf",
926 ep, fresopt, fnbefore);
927 else
928 sprintf(combuf+strlen(combuf), " %s %s.unf",
929 fresopt, fnbefore);
930 } else { /* else just check it */
931 if (rvr == 2)
932 return(1);
933 sprintf(combuf, "ra_rgbe -e %s -r %s.unf", ep, fnbefore);
934 }
935 /* output file name */
936 sprintf(fname0, vval(BASENAME), frame);
937 sprintf(combuf+strlen(combuf), " > %s.pic", fname0);
938 if (rvr) /* in recovery */
939 return(runcom(combuf));
940 bruncom(combuf, frame, frecover); /* else run in background */
941 return(0);
942 }
943
944
945 VIEW *
946 getview(n) /* get view number n */
947 int n;
948 {
949 static FILE *viewfp = NULL; /* view file pointer */
950 static int viewnum = 0; /* current view number */
951 static VIEW curview = STDVIEW; /* current view */
952 char linebuf[256];
953
954 if (n == 0) { /* signal to close file and clean up */
955 if (viewfp != NULL) {
956 fclose(viewfp);
957 viewfp = NULL;
958 viewnum = 0;
959 copystruct(&curview, &stdview);
960 }
961 return(NULL);
962 }
963 if (viewfp == NULL) { /* open file */
964 if ((viewfp = fopen(vval(VIEWFILE), "r")) == NULL) {
965 perror(vval(VIEWFILE));
966 quit(1);
967 }
968 } else if (n < viewnum) { /* rewind file */
969 if (viewnum == 1 && feof(viewfp))
970 return(&curview); /* just one view */
971 if (fseek(viewfp, 0L, 0) == EOF) {
972 perror(vval(VIEWFILE));
973 quit(1);
974 }
975 copystruct(&curview, &stdview);
976 viewnum = 0;
977 }
978 while (n > viewnum) { /* scan to desired view */
979 if (fgets(linebuf, sizeof(linebuf), viewfp) == NULL)
980 return(viewnum==1 ? &curview : NULL);
981 if (isview(linebuf) && sscanview(&curview, linebuf) > 0)
982 viewnum++;
983 }
984 return(&curview); /* return it */
985 }
986
987
988 int
989 countviews() /* count views in view file */
990 {
991 register int n = 0;
992
993 while (getview(n+1) != NULL)
994 n++;
995 return(n);
996 }
997
998
999 char *
1000 getexp(n) /* get exposure for nth frame */
1001 int n;
1002 {
1003 extern char *fskip();
1004 static char expval[32];
1005 static FILE *expfp = NULL;
1006 static long *exppos;
1007 static int curfrm;
1008 register char *cp;
1009
1010 if (n == 0) { /* signal to close file */
1011 if (expfp != NULL) {
1012 fclose(expfp);
1013 expfp = NULL;
1014 }
1015 return(NULL);
1016 }
1017 if (!vdef(EXPOSURE)) /* no setting (auto) */
1018 return(NULL);
1019 if (isflt(vval(EXPOSURE))) /* always the same */
1020 return(vval(EXPOSURE));
1021 if (expfp == NULL) { /* open exposure file */
1022 if ((expfp = fopen(vval(EXPOSURE), "r")) == NULL) {
1023 fprintf(stderr,
1024 "%s: cannot open exposure file \"%s\"\n",
1025 progname, vval(EXPOSURE));
1026 quit(1);
1027 }
1028 curfrm = vint(END) + 1; /* init lookup tab. */
1029 exppos = (long *)malloc(curfrm*sizeof(long *));
1030 if (exppos == NULL) {
1031 perror(progname);
1032 quit(1);
1033 }
1034 while (curfrm--)
1035 exppos[curfrm] = -1L;
1036 curfrm = 0;
1037 }
1038 /* find position in file */
1039 if (n-1 != curfrm && n != curfrm && exppos[n-1] >= 0 &&
1040 fseek(expfp, exppos[curfrm=n-1], 0) == EOF) {
1041 fprintf(stderr, "%s: seek error on exposure file\n", progname);
1042 quit(1);
1043 }
1044 while (n > curfrm) { /* read exposure */
1045 if (exppos[curfrm] < 0)
1046 exppos[curfrm] = ftell(expfp);
1047 if (fgets(expval, sizeof(expval), expfp) == NULL) {
1048 fprintf(stderr, "%s: too few exposures\n",
1049 vval(EXPOSURE));
1050 quit(1);
1051 }
1052 curfrm++;
1053 cp = fskip(expval); /* check format */
1054 if (cp == NULL || *cp != '\n') {
1055 fprintf(stderr,
1056 "%s: exposure format error on line %d\n",
1057 vval(EXPOSURE), curfrm);
1058 quit(1);
1059 }
1060 *cp = '\0';
1061 }
1062 return(expval); /* return value */
1063 }
1064
1065
1066 struct pslot *
1067 findpslot(pid) /* find or allocate a process slot */
1068 int pid;
1069 {
1070 register struct pslot *psempty = NULL;
1071 register int i;
1072
1073 for (i = 0; i < npslots; i++) { /* look for match */
1074 if (pslot[i].pid == pid)
1075 return(pslot+i);
1076 if (psempty == NULL && pslot[i].pid == 0)
1077 psempty = pslot+i;
1078 }
1079 return(psempty); /* return emtpy slot (error if NULL) */
1080 }
1081
1082
1083 int
1084 donecom(ps, pn, status) /* clean up after finished process */
1085 PSERVER *ps;
1086 int pn;
1087 int status;
1088 {
1089 register PROC *pp;
1090
1091 pp = ps->proc + pn;
1092 if (pp->elen) { /* pass errors */
1093 if (ps->hostname[0])
1094 fprintf(stderr, "%s: ", ps->hostname);
1095 fprintf(stderr, "Error output from: %s\n", pp->com);
1096 fputs(pp->errs, stderr);
1097 fflush(stderr);
1098 if (ps->hostname[0])
1099 status = 1; /* because rsh doesn't return status */
1100 }
1101 freestr(pp->com); /* free command string */
1102 lastpid = pp->pid; /* record PID for bwait() */
1103 lastpserver = ps; /* record server for serverdown() */
1104 return(status);
1105 }
1106
1107
1108 int
1109 serverdown() /* check status of last process server */
1110 {
1111 if (pserverOK(lastpserver)) /* server still up? */
1112 return(0);
1113 delpserver(lastpserver); /* else delete it */
1114 if (pslist == NULL) {
1115 fprintf(stderr, "%s: all process servers are down\n",
1116 progname);
1117 quit(1);
1118 }
1119 return(1);
1120 }
1121
1122
1123 int
1124 bruncom(com, fout, rf) /* run a command in the background */
1125 char *com;
1126 int fout;
1127 int (*rf)();
1128 {
1129 int pid;
1130 register struct pslot *psl;
1131
1132 if (noaction) {
1133 if (!silent)
1134 printf("\t%s\n", com); /* echo command */
1135 return(0);
1136 }
1137 /* else start it when we can */
1138 while ((pid = startjob(NULL, savestr(com), donecom)) == -1)
1139 bwait(1);
1140 if (!silent) { /* echo command */
1141 PSERVER *ps;
1142 int psn = pid;
1143 ps = findjob(&psn);
1144 printf("\t%s\n", com);
1145 printf("\tProcess started on %s\n", phostname(ps));
1146 fflush(stdout);
1147 }
1148 psl = findpslot(pid); /* record info. in appropriate slot */
1149 psl->pid = pid;
1150 psl->fout = fout;
1151 psl->rcvf = rf;
1152 return(pid);
1153 }
1154
1155
1156 bwait(ncoms) /* wait for batch job(s) to finish */
1157 int ncoms;
1158 {
1159 int status;
1160 register struct pslot *psl;
1161
1162 if (noaction)
1163 return;
1164 while ((status = wait4job(NULL, -1)) != -1) {
1165 psl = findpslot(lastpid);
1166 if (status) { /* attempt recovery */
1167 serverdown(); /* check server */
1168 if (psl->rcvf == NULL || (*psl->rcvf)(psl->fout)) {
1169 fprintf(stderr,
1170 "%s: error rendering frame %d\n",
1171 progname, psl->fout);
1172 quit(1);
1173 }
1174 }
1175 psl->pid = 0; /* free process slot */
1176 if (!--ncoms)
1177 return; /* done enough */
1178 }
1179 }
1180
1181
1182 int
1183 pruncom(com, ppins, maxcopies) /* run a command in parallel over network */
1184 char *com, *ppins;
1185 int maxcopies;
1186 {
1187 int retstatus = 0;
1188 int hostcopies;
1189 char com1buf[10240], *com1, *endcom1;
1190 int status;
1191 register PSERVER *ps;
1192
1193 if (!silent)
1194 printf("\t%s\n", com); /* echo command */
1195 if (noaction)
1196 return(0);
1197 fflush(stdout);
1198 /* start jobs on each server */
1199 for (ps = pslist; ps != NULL; ps = ps->next) {
1200 hostcopies = 0;
1201 if (maxcopies > 1 && ps->nprocs > 1 && ppins != NULL) {
1202 strcpy(com1=com1buf, com); /* build -PP command */
1203 sprintf(com1+(ppins-com), " -PP %s/%s.persist",
1204 vval(DIRECTORY), phostname(ps));
1205 strcat(com1, ppins);
1206 endcom1 = com1 + strlen(com1);
1207 sprintf(endcom1, "; kill `sed -n '1s/^[^ ]* //p' %s/%s.persist`",
1208 vval(DIRECTORY), phostname(ps));
1209 } else {
1210 com1 = com;
1211 endcom1 = NULL;
1212 }
1213 while (maxcopies > 0 &&
1214 startjob(ps, savestr(com1), donecom) != -1) {
1215 sleep(10);
1216 hostcopies++;
1217 maxcopies--;
1218 if (endcom1 != NULL)
1219 *endcom1 = '\0';
1220 }
1221 if (!silent && hostcopies) {
1222 if (hostcopies > 1)
1223 printf("\t%d duplicate processes", hostcopies);
1224 else
1225 printf("\tProcess");
1226 printf(" started on %s\n", phostname(ps));
1227 fflush(stdout);
1228 }
1229 }
1230 /* wait for jobs to finish */
1231 while ((status = wait4job(NULL, -1)) != -1)
1232 if (status)
1233 retstatus += !serverdown(); /* check server */
1234 return(retstatus);
1235 }
1236
1237
1238 runcom(cs) /* run a command locally and wait for it */
1239 char *cs;
1240 {
1241 if (!silent) /* echo it */
1242 printf("\t%s\n", cs);
1243 if (noaction)
1244 return(0);
1245 fflush(stdout); /* flush output and pass to shell */
1246 return(system(cs));
1247 }
1248
1249
1250 rmfile(fn) /* remove a file */
1251 char *fn;
1252 {
1253 if (!silent)
1254 #ifdef MSDOS
1255 printf("\tdel %s\n", fn);
1256 #else
1257 printf("\trm -f %s\n", fn);
1258 #endif
1259 if (noaction)
1260 return(0);
1261 return(unlink(fn));
1262 }
1263
1264
1265 badvalue(vc) /* report bad variable value and exit */
1266 int vc;
1267 {
1268 fprintf(stderr, "%s: bad value for variable '%s'\n",
1269 progname, vnam(vc));
1270 quit(1);
1271 }