ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.6
Committed: Tue Jan 23 17:01:33 1996 UTC (28 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +17 -16 lines
Log Message:
fixed pmblur view file naming problem

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