ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.7
Committed: Wed Jan 24 12:22:15 1996 UTC (28 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +52 -50 lines
Log Message:
more bug fixes

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