ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.14
Committed: Mon Dec 1 16:34:21 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.13: +6 -0 lines
Log Message:
added "VDISTANCE" variable for virtual distance calculation
changed ray sampling to low discrepency sequence

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