ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.12
Committed: Mon May 20 11:50:19 1996 UTC (27 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +12 -5 lines
Log Message:
added warning about updates to control file

File Contents

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