ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.5
Committed: Tue Jan 23 16:14:28 1996 UTC (28 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +16 -8 lines
Log Message:
minor improvements

File Contents

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