ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.11
Committed: Tue Apr 16 10:38:19 1996 UTC (28 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +6 -3 lines
Log Message:
changed call to execvp() to getpath() + execv()

File Contents

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