ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.18
Committed: Mon Jul 8 14:53:17 1996 UTC (27 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.17: +42 -36 lines
Log Message:
fixed problem in multiple recovery by putting important code in donecom()

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