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