ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.4
Committed: Mon Jan 22 17:19:37 1996 UTC (28 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +31 -13 lines
Log Message:
hopefully added -PP option to parallel machine runs

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