ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.8
Committed: Wed Jan 24 14:31:36 1996 UTC (28 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +3 -1 lines
Log Message:
added exception for VIEWFILE with just one view

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 (viewnum == 1 && feof(viewfp))
950 return(&curview); /* just one view */
951 if (fseek(viewfp, 0L, 0) == EOF) {
952 perror(vval(VIEWFILE));
953 quit(1);
954 }
955 copystruct(&curview, &stdview);
956 viewnum = 0;
957 }
958 while (n > viewnum) { /* scan to desired view */
959 if (fgets(linebuf, sizeof(linebuf), viewfp) == NULL)
960 return(viewnum==1 ? &curview : NULL);
961 if (isview(linebuf) && sscanview(&curview, linebuf) > 0)
962 viewnum++;
963 }
964 return(&curview); /* return it */
965 }
966
967
968 int
969 countviews() /* count views in view file */
970 {
971 register int n = 0;
972
973 while (getview(n+1) != NULL)
974 n++;
975 return(n);
976 }
977
978
979 char *
980 getexp(n) /* get exposure for nth frame */
981 int n;
982 {
983 extern char *fskip();
984 static char expval[32];
985 static FILE *expfp = NULL;
986 static long *exppos;
987 static int curfrm;
988 register char *cp;
989
990 if (n == 0) { /* signal to close file */
991 if (expfp != NULL) {
992 fclose(expfp);
993 expfp = NULL;
994 }
995 return(NULL);
996 }
997 if (!vdef(EXPOSURE)) /* no setting (auto) */
998 return(NULL);
999 if (isflt(vval(EXPOSURE))) /* always the same */
1000 return(vval(EXPOSURE));
1001 if (expfp == NULL) { /* open exposure file */
1002 if ((expfp = fopen(vval(EXPOSURE), "r")) == NULL) {
1003 fprintf(stderr,
1004 "%s: cannot open exposure file \"%s\"\n",
1005 progname, vval(EXPOSURE));
1006 quit(1);
1007 }
1008 curfrm = vint(END) + 1; /* init lookup tab. */
1009 exppos = (long *)malloc(curfrm*sizeof(long *));
1010 if (exppos == NULL) {
1011 perror(progname);
1012 quit(1);
1013 }
1014 while (curfrm--)
1015 exppos[curfrm] = -1L;
1016 curfrm = 0;
1017 }
1018 /* find position in file */
1019 if (n-1 != curfrm && n != curfrm && exppos[n-1] >= 0 &&
1020 fseek(expfp, exppos[curfrm=n-1], 0) == EOF) {
1021 fprintf(stderr, "%s: seek error on exposure file\n", progname);
1022 quit(1);
1023 }
1024 while (n > curfrm) { /* read exposure */
1025 if (exppos[curfrm] < 0)
1026 exppos[curfrm] = ftell(expfp);
1027 if (fgets(expval, sizeof(expval), expfp) == NULL) {
1028 fprintf(stderr, "%s: too few exposures\n",
1029 vval(EXPOSURE));
1030 quit(1);
1031 }
1032 curfrm++;
1033 cp = fskip(expval); /* check format */
1034 if (cp == NULL || *cp != '\n') {
1035 fprintf(stderr,
1036 "%s: exposure format error on line %d\n",
1037 vval(EXPOSURE), curfrm);
1038 quit(1);
1039 }
1040 *cp = '\0';
1041 }
1042 return(expval); /* return value */
1043 }
1044
1045
1046 struct pslot *
1047 findpslot(pid) /* find or allocate a process slot */
1048 int pid;
1049 {
1050 register struct pslot *psempty = NULL;
1051 register int i;
1052
1053 for (i = 0; i < npslots; i++) { /* look for match */
1054 if (pslot[i].pid == pid)
1055 return(pslot+i);
1056 if (psempty == NULL && pslot[i].pid == 0)
1057 psempty = pslot+i;
1058 }
1059 return(psempty); /* return emtpy slot (error if NULL) */
1060 }
1061
1062
1063 int
1064 donecom(ps, pn, status) /* clean up after finished process */
1065 PSERVER *ps;
1066 int pn;
1067 int status;
1068 {
1069 register PROC *pp;
1070
1071 pp = ps->proc + pn;
1072 if (pp->elen) { /* pass errors */
1073 if (ps->hostname[0])
1074 fprintf(stderr, "%s: ", ps->hostname);
1075 fprintf(stderr, "Error output from: %s\n", pp->com);
1076 fputs(pp->errs, stderr);
1077 fflush(stderr);
1078 if (ps->hostname[0])
1079 status = 1; /* because rsh doesn't return status */
1080 }
1081 freestr(pp->com); /* free command string */
1082 lastpid = pp->pid; /* record PID for bwait() */
1083 lastpserver = ps; /* record server for serverdown() */
1084 return(status);
1085 }
1086
1087
1088 int
1089 serverdown() /* check status of last process server */
1090 {
1091 if (pserverOK(lastpserver)) /* server still up? */
1092 return(0);
1093 delpserver(lastpserver); /* else delete it */
1094 if (pslist == NULL) {
1095 fprintf(stderr, "%s: all process servers are down\n",
1096 progname);
1097 quit(1);
1098 }
1099 return(1);
1100 }
1101
1102
1103 int
1104 bruncom(com, fout, rf) /* run a command in the background */
1105 char *com;
1106 int fout;
1107 int (*rf)();
1108 {
1109 int pid;
1110 register struct pslot *psl;
1111
1112 if (noaction) {
1113 if (!silent)
1114 printf("\t%s\n", com); /* echo command */
1115 return(0);
1116 }
1117 /* else start it when we can */
1118 while ((pid = startjob(NULL, savestr(com), donecom)) == -1)
1119 bwait(1);
1120 if (!silent) { /* echo command */
1121 PSERVER *ps;
1122 int psn = pid;
1123 ps = findjob(&psn);
1124 printf("\t%s\n", com);
1125 printf("\tProcess started on %s\n", phostname(ps));
1126 fflush(stdout);
1127 }
1128 psl = findpslot(pid); /* record info. in appropriate slot */
1129 psl->pid = pid;
1130 psl->fout = fout;
1131 psl->rcvf = rf;
1132 return(pid);
1133 }
1134
1135
1136 bwait(ncoms) /* wait for batch job(s) to finish */
1137 int ncoms;
1138 {
1139 int status;
1140 register struct pslot *psl;
1141
1142 if (noaction)
1143 return;
1144 while ((status = wait4job(NULL, -1)) != -1) {
1145 psl = findpslot(lastpid);
1146 if (status) { /* attempt recovery */
1147 serverdown(); /* check server */
1148 if (psl->rcvf == NULL || (*psl->rcvf)(psl->fout)) {
1149 fprintf(stderr,
1150 "%s: error rendering frame %d\n",
1151 progname, psl->fout);
1152 quit(1);
1153 }
1154 }
1155 psl->pid = 0; /* free process slot */
1156 if (!--ncoms)
1157 return; /* done enough */
1158 }
1159 }
1160
1161
1162 int
1163 pruncom(com, ppins, maxcopies) /* run a command in parallel over network */
1164 char *com, *ppins;
1165 int maxcopies;
1166 {
1167 int retstatus = 0;
1168 int hostcopies;
1169 char com1buf[10240], *com1, *endcom1;
1170 int status;
1171 register PSERVER *ps;
1172
1173 if (!silent)
1174 printf("\t%s\n", com); /* echo command */
1175 if (noaction)
1176 return(0);
1177 fflush(stdout);
1178 /* start jobs on each server */
1179 for (ps = pslist; ps != NULL; ps = ps->next) {
1180 hostcopies = 0;
1181 if (maxcopies > 1 && ps->nprocs > 1 && ppins != NULL) {
1182 strcpy(com1=com1buf, com); /* build -PP command */
1183 sprintf(com1+(ppins-com), " -PP %s/%s.persist",
1184 vval(DIRECTORY), phostname(ps));
1185 strcat(com1, ppins);
1186 endcom1 = com1 + strlen(com1);
1187 sprintf(endcom1, "; kill `sed -n '1s/^[^ ]* //p' %s/%s.persist`",
1188 vval(DIRECTORY), phostname(ps));
1189 } else {
1190 com1 = com;
1191 endcom1 = NULL;
1192 }
1193 while (maxcopies > 0 &&
1194 startjob(ps, savestr(com1), donecom) != -1) {
1195 sleep(10);
1196 hostcopies++;
1197 maxcopies--;
1198 if (endcom1 != NULL)
1199 *endcom1 = '\0';
1200 }
1201 if (!silent && hostcopies) {
1202 if (hostcopies > 1)
1203 printf("\t%d duplicate processes", hostcopies);
1204 else
1205 printf("\tProcess");
1206 printf(" started on %s\n", phostname(ps));
1207 fflush(stdout);
1208 }
1209 }
1210 /* wait for jobs to finish */
1211 while ((status = wait4job(NULL, -1)) != -1)
1212 if (status)
1213 retstatus += !serverdown(); /* check server */
1214 return(retstatus);
1215 }
1216
1217
1218 runcom(cs) /* run a command locally and wait for it */
1219 char *cs;
1220 {
1221 if (!silent) /* echo it */
1222 printf("\t%s\n", cs);
1223 if (noaction)
1224 return(0);
1225 fflush(stdout); /* flush output and pass to shell */
1226 return(system(cs));
1227 }
1228
1229
1230 rmfile(fn) /* remove a file */
1231 char *fn;
1232 {
1233 if (!silent)
1234 #ifdef MSDOS
1235 printf("\tdel %s\n", fn);
1236 #else
1237 printf("\trm -f %s\n", fn);
1238 #endif
1239 if (noaction)
1240 return(0);
1241 return(unlink(fn));
1242 }
1243
1244
1245 badvalue(vc) /* report bad variable value and exit */
1246 int vc;
1247 {
1248 fprintf(stderr, "%s: bad value for variable '%s'\n",
1249 progname, vnam(vc));
1250 quit(1);
1251 }