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