ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.24
Committed: Thu Feb 26 10:27:45 1998 UTC (26 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.23: +13 -4 lines
Log Message:
fixed infinite loop in countviews()

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