ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.20
Committed: Wed Jul 17 17:45:11 1996 UTC (27 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +1 -1 lines
Log Message:
bug fix in status check on finished job

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