ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.28
Committed: Mon Jul 6 18:17:39 1998 UTC (25 years, 8 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.27: +3 -7 lines
Log Message:
fixed bug by eliminated exit code check on rad (sigh)

File Contents

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