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 |
/* mark start time */ |
55 |
starttime = time(NULL); |
56 |
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 |
} else { /* else load holodeck */ |
107 |
loadholo(); |
108 |
if (vdef(RIF)) /* load RIF if any */ |
109 |
getradfile(vval(RIF)); |
110 |
} |
111 |
/* 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 |
|
131 |
if (outdev != NULL) /* open output device */ |
132 |
disp_open(outdev); |
133 |
else if (ncprocs > 0) /* else use global ray feed */ |
134 |
init_global(); |
135 |
/* record end time */ |
136 |
if (!vdef(TIME) || vflt(TIME) <= FTINY) |
137 |
endtime = 0; |
138 |
else |
139 |
endtime = starttime + vflt(TIME)*3600.; |
140 |
/* set up memory cache */ |
141 |
if (outdev == NULL) |
142 |
hdcachesize = 0; /* manual flushing */ |
143 |
else if (vdef(CACHE)) |
144 |
hdcachesize = 1024.*1024.*vflt(CACHE); |
145 |
/* open report file */ |
146 |
if (vdef(REPORT)) { |
147 |
register char *s = sskip2(vval(REPORT), 1); |
148 |
if (*s && freopen(s, "a", stderr) == NULL) |
149 |
quit(2); |
150 |
} |
151 |
/* start rtrace */ |
152 |
if (ncprocs > 0) { |
153 |
i = start_rtrace(); |
154 |
if (i < 1) |
155 |
error(USER, "cannot start rtrace process"); |
156 |
if (vdef(REPORT)) { /* make first report */ |
157 |
printargs(rtargc, rtargv, stderr); |
158 |
report(0); |
159 |
} |
160 |
/* allocate packets */ |
161 |
freepacks = (PACKET *)bmalloc(i*sizeof(PACKET)); |
162 |
if (freepacks == NULL) |
163 |
goto memerr; |
164 |
freepacks[--i].nr = 0; |
165 |
freepacks[i].next = NULL; |
166 |
if (!vbool(OBSTRUCTIONS)) { |
167 |
freepacks[i].offset = (float *)bmalloc( |
168 |
RPACKSIZ*sizeof(float)*(i+1) ); |
169 |
if (freepacks[i].offset == NULL) |
170 |
goto memerr; |
171 |
} else |
172 |
freepacks[i].offset = NULL; |
173 |
while (i--) { |
174 |
freepacks[i].nr = 0; |
175 |
freepacks[i].offset = freepacks[i+1].offset == NULL ? |
176 |
NULL : freepacks[i+1].offset+RPACKSIZ ; |
177 |
freepacks[i].next = &freepacks[i+1]; |
178 |
} |
179 |
} |
180 |
return; |
181 |
memerr: |
182 |
error(SYSTEM, "out of memory in initrholo"); |
183 |
} |
184 |
|
185 |
|
186 |
rholo() /* holodeck main loop */ |
187 |
{ |
188 |
static int idle = 1; |
189 |
PACKET *pl = NULL, *plend; |
190 |
register PACKET *p; |
191 |
time_t t; |
192 |
long l; |
193 |
|
194 |
if (outdev != NULL) /* check display */ |
195 |
if (!disp_check(idle)) |
196 |
return(0); |
197 |
/* display only? */ |
198 |
if (ncprocs <= 0) |
199 |
return(1); |
200 |
/* check file size */ |
201 |
if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 && |
202 |
hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l) |
203 |
return(0); |
204 |
/* check time */ |
205 |
if (endtime > 0 || reporttime > 0) |
206 |
t = time(NULL); |
207 |
if (endtime > 0 && t >= endtime) |
208 |
return(0); |
209 |
if (reporttime > 0 && t >= reporttime) |
210 |
report(t); |
211 |
/* get packets to process */ |
212 |
while (freepacks != NULL) { |
213 |
p = freepacks; freepacks = p->next; p->next = NULL; |
214 |
if (!next_packet(p)) { |
215 |
p->next = freepacks; freepacks = p; |
216 |
break; |
217 |
} |
218 |
if (pl == NULL) pl = p; |
219 |
else plend->next = p; |
220 |
plend = p; |
221 |
} |
222 |
idle = pl == NULL && freepacks != NULL; |
223 |
/* are we out of stuff to do? */ |
224 |
if (idle && outdev == NULL) |
225 |
return(0); |
226 |
/* else process packets */ |
227 |
done_packets(do_packets(pl)); |
228 |
return(1); /* and continue */ |
229 |
} |
230 |
|
231 |
|
232 |
report(t) /* report progress so far */ |
233 |
time_t t; |
234 |
{ |
235 |
if (t == 0) |
236 |
t = time(NULL); |
237 |
fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n", |
238 |
progname, npacksdone, nraysdone, (t-starttime)/3600.); |
239 |
fflush(stderr); |
240 |
if (vdef(REPORT)) |
241 |
reporttime = t + (time_t)(vflt(REPORT)*60.+.5); |
242 |
} |
243 |
|
244 |
|
245 |
setdefaults(gp) /* set default values */ |
246 |
register HDGRID *gp; |
247 |
{ |
248 |
extern char *atos(); |
249 |
register int i; |
250 |
double len[3], maxlen, d; |
251 |
char buf[64]; |
252 |
|
253 |
if (!vdef(SECTION)) { |
254 |
sprintf(errmsg, "%s must be defined", vnam(SECTION)); |
255 |
error(USER, errmsg); |
256 |
} |
257 |
if (vdef(SECTION) > 1) { |
258 |
sprintf(errmsg, "ignoring all but first %s", vnam(SECTION)); |
259 |
error(WARNING, errmsg); |
260 |
} |
261 |
if (sscanf(vval(SECTION), |
262 |
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", |
263 |
&gp->orig[0], &gp->orig[1], &gp->orig[2], |
264 |
&gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], |
265 |
&gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], |
266 |
&gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12) |
267 |
badvalue(SECTION); |
268 |
maxlen = 0.; |
269 |
for (i = 0; i < 3; i++) |
270 |
if ((len[i] = VLEN(gp->xv[i])) > maxlen) |
271 |
maxlen = len[i]; |
272 |
if (!vdef(GRID)) { |
273 |
sprintf(buf, "%.4f", maxlen/8.); |
274 |
vval(GRID) = savqstr(buf); |
275 |
vdef(GRID)++; |
276 |
} |
277 |
if ((d = vflt(GRID)) <= FTINY) |
278 |
badvalue(GRID); |
279 |
for (i = 0; i < 3; i++) |
280 |
gp->grid[i] = len[i]/d + (1.-FTINY); |
281 |
if (!vdef(EXPOSURE)) { |
282 |
sprintf(errmsg, "%s must be defined", vnam(EXPOSURE)); |
283 |
error(USER, errmsg); |
284 |
} |
285 |
expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ? |
286 |
pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE); |
287 |
if (!vdef(OCTREE)) { |
288 |
if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) |
289 |
error(SYSTEM, "out of memory"); |
290 |
sprintf(vval(OCTREE), "%s.oct", froot); |
291 |
vdef(OCTREE)++; |
292 |
} |
293 |
if (!vdef(DISKSPACE)) { |
294 |
sprintf(errmsg, |
295 |
"no %s setting, assuming 100 Mbytes available", |
296 |
vnam(DISKSPACE)); |
297 |
error(WARNING, errmsg); |
298 |
vval(DISKSPACE) = "100"; |
299 |
vdef(DISKSPACE)++; |
300 |
} |
301 |
if (!vdef(OBSTRUCTIONS)) { |
302 |
vval(OBSTRUCTIONS) = "T"; |
303 |
vdef(OBSTRUCTIONS)++; |
304 |
} |
305 |
if (!vdef(OCCUPANCY)) { |
306 |
vval(OCCUPANCY) = "U"; |
307 |
vdef(OCCUPANCY)++; |
308 |
} |
309 |
/* append rendering options */ |
310 |
if (vdef(RENDER)) |
311 |
rtargc += wordstring(rtargv+rtargc, vval(RENDER)); |
312 |
} |
313 |
|
314 |
|
315 |
creatholo(gp) /* create a holodeck output file */ |
316 |
HDGRID *gp; |
317 |
{ |
318 |
long endloc = 0; |
319 |
FILE *fp; |
320 |
/* open & truncate file */ |
321 |
if ((fp = fopen(hdkfile, "w+")) == NULL) { |
322 |
sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile); |
323 |
error(SYSTEM, errmsg); |
324 |
} |
325 |
/* write information header */ |
326 |
newheader("RADIANCE", fp); |
327 |
printvars(fp); |
328 |
fputformat(HOLOFMT, fp); |
329 |
fputc('\n', fp); |
330 |
putw(HOLOMAGIC, fp); /* put magic number & terminus */ |
331 |
fwrite(&endloc, sizeof(long), 1, fp); |
332 |
fflush(fp); /* flush buffer */ |
333 |
hdinit(fileno(fp), gp); /* allocate and initialize index */ |
334 |
/* we're dropping fp here.... */ |
335 |
} |
336 |
|
337 |
|
338 |
headline(s) /* process information header line */ |
339 |
char *s; |
340 |
{ |
341 |
extern char FMTSTR[]; |
342 |
register char *cp; |
343 |
char fmt[32]; |
344 |
|
345 |
if (formatval(fmt, s)) { |
346 |
if (strcmp(fmt, HOLOFMT)) { |
347 |
sprintf(errmsg, "%s file \"%s\" has %s%s", |
348 |
HOLOFMT, hdkfile, FMTSTR, fmt); |
349 |
error(USER, errmsg); |
350 |
} |
351 |
return; |
352 |
} |
353 |
for (cp = s; *cp; cp++) /* take off any comments */ |
354 |
if (*cp == '#') { |
355 |
*cp = '\0'; |
356 |
break; |
357 |
} |
358 |
setvariable(s, matchvar); /* don't flag errors */ |
359 |
} |
360 |
|
361 |
|
362 |
loadholo() /* start loading a holodeck from fname */ |
363 |
{ |
364 |
FILE *fp; |
365 |
long endloc; |
366 |
/* open holodeck file */ |
367 |
if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { |
368 |
sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, |
369 |
ncprocs>0 ? "appending" : "reading"); |
370 |
error(SYSTEM, errmsg); |
371 |
} |
372 |
/* load variables from header */ |
373 |
getheader(fp, headline, NULL); |
374 |
/* check magic number */ |
375 |
if (getw(fp) != HOLOMAGIC) { |
376 |
sprintf(errmsg, "bad magic number in holodeck file \"%s\"", |
377 |
hdkfile); |
378 |
error(USER, errmsg); |
379 |
} |
380 |
fread(&endloc, sizeof(long), 1, fp); |
381 |
if (endloc != 0) |
382 |
error(WARNING, "ignoring multiple sections in holodeck file"); |
383 |
fseek(fp, 0L, 1); /* align system file pointer */ |
384 |
hdinit(fileno(fp), NULL); /* allocate and load index */ |
385 |
/* we're dropping fp here.... */ |
386 |
} |
387 |
|
388 |
|
389 |
done_packets(pl) /* handle finished packets */ |
390 |
PACKET *pl; |
391 |
{ |
392 |
static int nunflushed = 0; |
393 |
register PACKET *p; |
394 |
|
395 |
while (pl != NULL) { |
396 |
p = pl; pl = p->next; p->next = NULL; |
397 |
if (p->nr > 0) { /* add to holodeck */ |
398 |
bcopy((char *)p->ra, |
399 |
(char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), |
400 |
p->nr*sizeof(RAYVAL)); |
401 |
if (outdev != NULL) /* display it */ |
402 |
disp_packet(p); |
403 |
else |
404 |
nunflushed += p->nr; |
405 |
nraysdone += p->nr; |
406 |
npacksdone++; |
407 |
} |
408 |
p->nr = 0; /* push onto free list */ |
409 |
p->next = freepacks; |
410 |
freepacks = p; |
411 |
} |
412 |
if (nunflushed >= 256*RPACKSIZ) { |
413 |
hdflush(NULL); /* flush holodeck buffers */ |
414 |
nunflushed = 0; |
415 |
} |
416 |
} |
417 |
|
418 |
|
419 |
getradfile(rfargs) /* run rad and get needed variables */ |
420 |
char *rfargs; |
421 |
{ |
422 |
static short mvar[] = {VIEW,OCTREE,EXPOSURE,-1}; |
423 |
static char tf1[] = TEMPLATE; |
424 |
char tf2[64]; |
425 |
char combuf[256]; |
426 |
char *pippt; |
427 |
register int i; |
428 |
register char *cp; |
429 |
/* create rad command */ |
430 |
mktemp(tf1); |
431 |
sprintf(tf2, "%s.rif", tf1); |
432 |
sprintf(combuf, |
433 |
"rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", |
434 |
rfargs, tf1); |
435 |
cp = combuf; |
436 |
while (*cp){ |
437 |
if (*cp == '|') pippt = cp; |
438 |
cp++; |
439 |
} /* match unset variables */ |
440 |
for (i = 0; mvar[i] >= 0; i++) |
441 |
if (!vdef(mvar[i])) { |
442 |
*cp++ = '|'; |
443 |
strcpy(cp, vnam(mvar[i])); |
444 |
while (*cp) cp++; |
445 |
pippt = NULL; |
446 |
} |
447 |
if (pippt != NULL) |
448 |
strcpy(pippt, "> /dev/null"); /* nothing to match */ |
449 |
else |
450 |
sprintf(cp, ")[ \t]*=' > %s", tf2); |
451 |
if (system(combuf)) { |
452 |
error(SYSTEM, "cannot execute rad command"); |
453 |
unlink(tf2); /* clean up */ |
454 |
unlink(tf1); |
455 |
quit(1); |
456 |
} |
457 |
if (pippt == NULL) { |
458 |
loadvars(tf2); /* load variables */ |
459 |
unlink(tf2); |
460 |
} |
461 |
rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ |
462 |
unlink(tf1); /* clean up */ |
463 |
} |
464 |
|
465 |
|
466 |
rootname(rn, fn) /* remove tail from end of fn */ |
467 |
register char *rn, *fn; |
468 |
{ |
469 |
char *tp, *dp; |
470 |
|
471 |
for (tp = NULL, dp = rn; *rn = *fn++; rn++) |
472 |
if (ISDIRSEP(*rn)) |
473 |
dp = rn; |
474 |
else if (*rn == '.') |
475 |
tp = rn; |
476 |
if (tp != NULL && tp > dp) |
477 |
*tp = '\0'; |
478 |
} |
479 |
|
480 |
|
481 |
badvalue(vc) /* report bad variable value and exit */ |
482 |
int vc; |
483 |
{ |
484 |
sprintf(errmsg, "bad value for variable '%s'", vnam(vc)); |
485 |
error(USER, errmsg); |
486 |
} |
487 |
|
488 |
|
489 |
eputs(s) /* put error message to stderr */ |
490 |
register char *s; |
491 |
{ |
492 |
static int midline = 0; |
493 |
|
494 |
if (!*s) |
495 |
return; |
496 |
if (!midline++) { /* prepend line with program name */ |
497 |
fputs(progname, stderr); |
498 |
fputs(": ", stderr); |
499 |
} |
500 |
fputs(s, stderr); |
501 |
if (s[strlen(s)-1] == '\n') { |
502 |
fflush(stderr); |
503 |
midline = 0; |
504 |
} |
505 |
} |
506 |
|
507 |
|
508 |
wputs(s) /* put warning string to stderr */ |
509 |
char *s; |
510 |
{ |
511 |
if (!nowarn) |
512 |
eputs(s); |
513 |
} |
514 |
|
515 |
|
516 |
quit(ec) /* exit program gracefully */ |
517 |
int ec; |
518 |
{ |
519 |
int status = 0; |
520 |
|
521 |
if (outdev != NULL) /* close display */ |
522 |
disp_close(); |
523 |
if (hdlist[0] != NULL) { /* flush holodeck */ |
524 |
if (ncprocs > 0) { |
525 |
done_packets(flush_queue()); |
526 |
status = end_rtrace(); /* close rtrace */ |
527 |
hdflush(NULL); |
528 |
if (vdef(REPORT)) { |
529 |
long fsiz, fuse; |
530 |
report(0); |
531 |
fsiz = lseek(hdlist[0]->fd, 0L, 2); |
532 |
fuse = hdfiluse(hdlist[0]->fd, 1); |
533 |
fprintf(stderr, |
534 |
"%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", |
535 |
hdkfile, fsiz/(1024.*1024.), |
536 |
100.*(fsiz-fuse)/fsiz); |
537 |
} |
538 |
} else |
539 |
hdflush(NULL); |
540 |
} |
541 |
exit(ec ? ec : status); /* exit */ |
542 |
} |