ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.26
Committed: Sat Dec 13 10:38:26 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.25: +1 -4 lines
Log Message:
allow rholo to create a holodeck even if there is nothing to do

File Contents

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