ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.4
Committed: Mon Nov 3 18:33:12 1997 UTC (26 years, 6 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.3: +13 -9 lines
Log Message:
added more routines for display support

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Radiance holodeck generation controller
9     */
10    
11     #include "rholo.h"
12     #include "paths.h"
13     #include <sys/types.h>
14    
15     /* the following must be consistent with rholo.h */
16     int NVARS = NRHVARS; /* total number of variables */
17    
18     VARIABLE vv[] = RHVINIT; /* variable-value pairs */
19    
20     char *progname; /* our program name */
21     char *hdkfile; /* holodeck file name */
22     char froot[MAXPATH]; /* root file name */
23    
24     int nowarn = 0; /* turn warnings off? */
25    
26     double expval = 1.; /* global exposure value */
27    
28     int ncprocs = 0; /* desired number of compute processes */
29    
30     char *outdev = NULL; /* output device name */
31    
32     time_t starttime; /* time we got started */
33     time_t endtime; /* time we should end by */
34     time_t reporttime; /* time for next report */
35    
36     int rtargc = 1; /* rtrace command */
37     char *rtargv[128] = {"rtrace", NULL};
38    
39     long nraysdone = 0L; /* number of rays done */
40     long npacksdone = 0L; /* number of packets done */
41    
42     PACKET *freepacks; /* available packets */
43    
44     extern time_t time();
45    
46    
47     main(argc, argv)
48     int argc;
49     char *argv[];
50     {
51     HDGRID hdg;
52     int i;
53     int force = 0;
54 gregl 3.3 /* mark start time */
55     starttime = time(NULL);
56 gregl 3.1 progname = argv[0]; /* get arguments */
57     for (i = 1; i < argc && argv[i][0] == '-'; i++)
58     switch (argv[i][1]) {
59     case 'w': /* turn off warnings */
60     nowarn++;
61     break;
62     case 'f': /* force overwrite */
63     force++;
64     break;
65     case 'n': /* compute processes */
66     if (i >= argc-2)
67     goto userr;
68     ncprocs = atoi(argv[++i]);
69     break;
70     case 'o': /* output display */
71     if (i >= argc-2)
72     goto userr;
73     outdev = argv[++i];
74     break;
75     default:
76     goto userr;
77     }
78     /* do we have a job? */
79     if (outdev == NULL && ncprocs <= 0)
80     goto userr;
81     /* get root file name */
82     rootname(froot, hdkfile=argv[i++]);
83     /* load... */
84     if (i < argc) { /* variables */
85     loadvars(argv[i++]);
86     /* cmdline settings */
87     for ( ; i < argc; i++)
88     if (setvariable(argv[i], matchvar) < 0) {
89     sprintf(errmsg, "unknown variable: %s",
90     argv[i]);
91     error(USER, errmsg);
92     }
93     /* check settings */
94     checkvalues();
95     /* load RIF if any */
96     if (vdef(RIF))
97     getradfile(vval(RIF));
98     /* set defaults */
99     setdefaults(&hdg);
100     /* holodeck exists? */
101     if (!force && access(hdkfile, R_OK|W_OK) == 0)
102     error(USER,
103     "holodeck file exists -- use -f to overwrite");
104     /* create holodeck */
105     creatholo(&hdg);
106 gregl 3.3 } else { /* else load holodeck */
107 gregl 3.1 loadholo();
108 gregl 3.3 if (vdef(RIF)) /* load RIF if any */
109     getradfile(vval(RIF));
110     }
111 gregl 3.1 /* initialize */
112     initrholo();
113     /* run */
114     while (rholo())
115     ;
116     /* done */
117     quit(0);
118     userr:
119     fprintf(stderr,
120     "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
121     progname);
122     quit(1);
123     }
124    
125    
126     initrholo() /* get our holodeck running */
127     {
128     extern int global_packet();
129     register int i;
130 gregl 3.4
131     if (outdev != NULL) /* open output device */
132     disp_open(outdev);
133 gregl 3.1 else if (ncprocs > 0) /* else use global ray feed */
134     init_global();
135 gregl 3.3 /* record end time */
136 gregl 3.1 if (!vdef(TIME) || vflt(TIME) <= FTINY)
137     endtime = 0;
138     else
139     endtime = starttime + vflt(TIME)*3600.;
140     /* set up memory cache */
141     hdcachesize = 1024.*1024.*vflt(CACHE);
142     /* open report file */
143     if (vdef(REPORT)) {
144     register char *s = sskip2(vval(REPORT), 1);
145     if (*s && freopen(s, "a", stderr) == NULL)
146     quit(2);
147     }
148     /* start rtrace */
149     if (ncprocs > 0) {
150     i = start_rtrace();
151     if (i < 1)
152     error(USER, "cannot start rtrace process");
153     if (vdef(REPORT)) { /* make first report */
154     printargs(rtargc, rtargv, stderr);
155     report(0);
156     }
157     /* allocate packets */
158     freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
159     if (freepacks == NULL)
160     goto memerr;
161     freepacks[--i].nr = 0;
162     freepacks[i].next = NULL;
163     if (!vbool(OBSTRUCTIONS)) {
164     freepacks[i].offset = (float *)bmalloc(
165     RPACKSIZ*sizeof(float)*(i+1) );
166     if (freepacks[i].offset == NULL)
167     goto memerr;
168     } else
169     freepacks[i].offset = NULL;
170     while (i--) {
171     freepacks[i].nr = 0;
172     freepacks[i].offset = freepacks[i+1].offset == NULL ?
173     NULL : freepacks[i+1].offset+RPACKSIZ ;
174     freepacks[i].next = &freepacks[i+1];
175     }
176     }
177     return;
178     memerr:
179     error(SYSTEM, "out of memory in initrholo");
180     }
181    
182    
183     rholo() /* holodeck main loop */
184     {
185     static int idle = 1;
186     PACKET *pl = NULL, *plend;
187     register PACKET *p;
188     time_t t;
189     long l;
190 gregl 3.4
191     if (outdev != NULL) /* check display */
192     if (!disp_check(idle))
193     return(0);
194 gregl 3.1 /* display only? */
195     if (ncprocs <= 0)
196     return(1);
197     /* check file size */
198 gregl 3.2 if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 &&
199 gregl 3.1 hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l)
200     return(0);
201     /* check time */
202 gregl 3.3 if (endtime > 0 || reporttime > 0)
203 gregl 3.1 t = time(NULL);
204     if (endtime > 0 && t >= endtime)
205     return(0);
206 gregl 3.3 if (reporttime > 0 && t >= reporttime)
207 gregl 3.1 report(t);
208     /* get packets to process */
209     while (freepacks != NULL) {
210     p = freepacks; freepacks = p->next; p->next = NULL;
211     if (!next_packet(p)) {
212     p->next = freepacks; freepacks = p;
213     break;
214     }
215     if (pl == NULL) pl = p;
216     else plend->next = p;
217     plend = p;
218     }
219     idle = pl == NULL && freepacks != NULL;
220     /* are we out of stuff to do? */
221     if (idle && outdev == NULL)
222     return(0);
223     /* else process packets */
224     done_packets(do_packets(pl));
225     return(1); /* and continue */
226     }
227    
228    
229     report(t) /* report progress so far */
230     time_t t;
231     {
232     if (t == 0)
233     t = time(NULL);
234 gregl 3.2 fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
235 gregl 3.1 progname, npacksdone, nraysdone, (t-starttime)/3600.);
236 gregl 3.2 fflush(stderr);
237 gregl 3.3 if (vdef(REPORT))
238     reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
239 gregl 3.1 }
240    
241    
242     setdefaults(gp) /* set default values */
243     register HDGRID *gp;
244     {
245     extern char *atos();
246     register int i;
247     double len[3], maxlen, d;
248     char buf[64];
249    
250     if (!vdef(SECTION)) {
251     sprintf(errmsg, "%s must be defined", vnam(SECTION));
252     error(USER, errmsg);
253     }
254     if (vdef(SECTION) > 1) {
255     sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
256     error(WARNING, errmsg);
257     }
258     if (sscanf(vval(SECTION),
259     "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
260     &gp->orig[0], &gp->orig[1], &gp->orig[2],
261     &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
262     &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
263     &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
264     badvalue(SECTION);
265     maxlen = 0.;
266     for (i = 0; i < 3; i++)
267     if ((len[i] = VLEN(gp->xv[i])) > maxlen)
268     maxlen = len[i];
269     if (!vdef(GRID)) {
270 gregl 3.2 sprintf(buf, "%.4f", maxlen/8.);
271 gregl 3.1 vval(GRID) = savqstr(buf);
272     vdef(GRID)++;
273     }
274     if ((d = vflt(GRID)) <= FTINY)
275     badvalue(GRID);
276     for (i = 0; i < 3; i++)
277     gp->grid[i] = len[i]/d + (1.-FTINY);
278     if (!vdef(EXPOSURE)) {
279     sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
280     error(USER, errmsg);
281     }
282     expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
283     pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
284     if (!vdef(OCTREE)) {
285     if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
286     error(SYSTEM, "out of memory");
287     sprintf(vval(OCTREE), "%s.oct", froot);
288     vdef(OCTREE)++;
289     }
290     if (!vdef(DISKSPACE)) {
291     sprintf(errmsg,
292     "no %s setting, assuming 100 Mbytes available",
293     vnam(DISKSPACE));
294     error(WARNING, errmsg);
295     vval(DISKSPACE) = "100";
296     vdef(DISKSPACE)++;
297     }
298     if (!vdef(CACHE)) {
299     sprintf(errmsg,
300     "no %s setting, assuming 10 Mbytes available",
301     vnam(CACHE));
302     error(WARNING, errmsg);
303     vval(CACHE) = "10";
304     vdef(CACHE)++;
305     }
306     if (!vdef(OBSTRUCTIONS)) {
307     vval(OBSTRUCTIONS) = "T";
308     vdef(OBSTRUCTIONS)++;
309     }
310     if (!vdef(OCCUPANCY)) {
311     vval(OCCUPANCY) = "U";
312     vdef(OCCUPANCY)++;
313     }
314     /* append rendering options */
315     if (vdef(RENDER))
316     rtargc += wordstring(rtargv+rtargc, vval(RENDER));
317     }
318    
319    
320     creatholo(gp) /* create a holodeck output file */
321     HDGRID *gp;
322     {
323     long endloc = 0;
324     FILE *fp;
325     /* open & truncate file */
326     if ((fp = fopen(hdkfile, "w+")) == NULL) {
327     sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
328     error(SYSTEM, errmsg);
329     }
330     /* write information header */
331     newheader("RADIANCE", fp);
332     printvars(fp);
333     fputformat(HOLOFMT, fp);
334     fputc('\n', fp);
335     putw(HOLOMAGIC, fp); /* put magic number & terminus */
336     fwrite(&endloc, sizeof(long), 1, fp);
337     fflush(fp); /* flush buffer */
338 gregl 3.2 hdinit(fileno(fp), gp); /* allocate and initialize index */
339 gregl 3.1 /* we're dropping fp here.... */
340     }
341    
342    
343     headline(s) /* process information header line */
344     char *s;
345     {
346     extern char FMTSTR[];
347     register char *cp;
348     char fmt[32];
349    
350 gregl 3.2 if (formatval(fmt, s)) {
351 gregl 3.1 if (strcmp(fmt, HOLOFMT)) {
352     sprintf(errmsg, "%s file \"%s\" has %s%s",
353     HOLOFMT, hdkfile, FMTSTR, fmt);
354     error(USER, errmsg);
355     }
356     return;
357     }
358     for (cp = s; *cp; cp++) /* take off any comments */
359     if (*cp == '#') {
360     *cp = '\0';
361     break;
362     }
363     setvariable(s, matchvar); /* don't flag errors */
364     }
365    
366    
367     loadholo() /* start loading a holodeck from fname */
368     {
369     FILE *fp;
370     long endloc;
371 gregl 3.4 /* open holodeck file */
372     if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
373     sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
374     ncprocs>0 ? "appending" : "reading");
375 gregl 3.1 error(SYSTEM, errmsg);
376     }
377     /* load variables from header */
378     getheader(fp, headline, NULL);
379     /* check magic number */
380     if (getw(fp) != HOLOMAGIC) {
381     sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
382     hdkfile);
383     error(USER, errmsg);
384     }
385     fread(&endloc, sizeof(long), 1, fp);
386     if (endloc != 0)
387     error(WARNING, "ignoring multiple sections in holodeck file");
388     fseek(fp, 0L, 1); /* align system file pointer */
389 gregl 3.2 hdinit(fileno(fp), NULL); /* allocate and load index */
390 gregl 3.1 /* we're dropping fp here.... */
391     }
392    
393    
394     done_packets(pl) /* handle finished packets */
395     PACKET *pl;
396     {
397     register PACKET *p;
398    
399     while (pl != NULL) {
400     p = pl; pl = p->next; p->next = NULL;
401     if (p->nr > 0) { /* add to holodeck */
402     bcopy((char *)p->ra,
403     (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
404     p->nr*sizeof(RAYVAL));
405     if (outdev != NULL) /* display it */
406     disp_packet(p);
407     }
408     nraysdone += p->nr;
409     npacksdone++;
410     p->nr = 0; /* push onto free list */
411     p->next = freepacks;
412     freepacks = p;
413     }
414     }
415    
416    
417     getradfile(rfargs) /* run rad and get needed variables */
418     char *rfargs;
419     {
420 gregl 3.2 static short mvar[] = {VIEW,OCTREE,EXPOSURE,-1};
421 gregl 3.1 static char tf1[] = TEMPLATE;
422     char tf2[64];
423     char combuf[256];
424 gregl 3.3 char *pippt;
425 gregl 3.1 register int i;
426     register char *cp;
427     /* create rad command */
428     mktemp(tf1);
429     sprintf(tf2, "%s.rif", tf1);
430     sprintf(combuf,
431     "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
432     rfargs, tf1);
433     cp = combuf;
434 gregl 3.3 while (*cp){
435     if (*cp == '|') pippt = cp;
436     cp++;
437     } /* match unset variables */
438 gregl 3.1 for (i = 0; mvar[i] >= 0; i++)
439     if (!vdef(mvar[i])) {
440     *cp++ = '|';
441     strcpy(cp, vnam(mvar[i]));
442     while (*cp) cp++;
443 gregl 3.3 pippt = NULL;
444 gregl 3.1 }
445 gregl 3.3 if (pippt != NULL)
446     strcpy(pippt, "> /dev/null"); /* nothing to match */
447     else
448     sprintf(cp, ")[ \t]*=' > %s", tf2);
449 gregl 3.1 if (system(combuf)) {
450     error(SYSTEM, "cannot execute rad command");
451     unlink(tf2); /* clean up */
452     unlink(tf1);
453     quit(1);
454     }
455 gregl 3.3 if (pippt == NULL) {
456     loadvars(tf2); /* load variables */
457     unlink(tf2);
458     }
459 gregl 3.1 rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
460 gregl 3.3 unlink(tf1); /* clean up */
461 gregl 3.1 }
462    
463    
464     rootname(rn, fn) /* remove tail from end of fn */
465     register char *rn, *fn;
466     {
467     char *tp, *dp;
468    
469     for (tp = NULL, dp = rn; *rn = *fn++; rn++)
470     if (ISDIRSEP(*rn))
471     dp = rn;
472     else if (*rn == '.')
473     tp = rn;
474     if (tp != NULL && tp > dp)
475     *tp = '\0';
476     }
477    
478    
479     badvalue(vc) /* report bad variable value and exit */
480     int vc;
481     {
482     sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
483     error(USER, errmsg);
484     }
485    
486    
487     eputs(s) /* put error message to stderr */
488     register char *s;
489     {
490     static int midline = 0;
491    
492     if (!*s)
493     return;
494     if (!midline++) { /* prepend line with program name */
495     fputs(progname, stderr);
496     fputs(": ", stderr);
497     }
498     fputs(s, stderr);
499     if (s[strlen(s)-1] == '\n') {
500     fflush(stderr);
501     midline = 0;
502     }
503     }
504    
505    
506     wputs(s) /* put warning string to stderr */
507     char *s;
508     {
509     if (!nowarn)
510     eputs(s);
511     }
512    
513    
514     quit(ec) /* exit program gracefully */
515     int ec;
516     {
517     int status = 0;
518    
519 gregl 3.4 if (outdev != NULL) /* close display */
520     disp_close();
521 gregl 3.1 if (hdlist[0] != NULL) { /* flush holodeck */
522     if (ncprocs > 0) {
523     done_packets(flush_queue());
524     status = end_rtrace(); /* close rtrace */
525     hdflush(NULL);
526     if (vdef(REPORT)) {
527     long fsiz, fuse;
528     report(0);
529     fsiz = lseek(hdlist[0]->fd, 0L, 2);
530     fuse = hdfiluse(hdlist[0]->fd, 1);
531     fprintf(stderr,
532 gregl 3.3 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
533 gregl 3.1 hdkfile, fsiz/(1024.*1024.),
534     100.*(fsiz-fuse)/fsiz);
535     }
536     } else
537     hdflush(NULL);
538     }
539     exit(ec ? ec : status); /* exit */
540     }