ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.2
Committed: Thu Jan 18 11:25:11 1996 UTC (28 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +329 -100 lines
Log Message:
added network processing to ranimate

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