1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.15 |
static const char RCSid[] = "$Id: rtmain.c,v 2.14 2006/04/05 06:22:57 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* rtmain.c - main for rtrace per-ray calculation program |
6 |
|
|
*/ |
7 |
|
|
|
8 |
greg |
2.2 |
#include "copyright.h" |
9 |
greg |
2.1 |
|
10 |
schorsch |
2.4 |
#include <signal.h> |
11 |
|
|
|
12 |
|
|
#include "platform.h" |
13 |
schorsch |
2.7 |
#include "rtprocess.h" /* getpid() */ |
14 |
schorsch |
2.9 |
#include "resolu.h" |
15 |
greg |
2.1 |
#include "ray.h" |
16 |
|
|
#include "source.h" |
17 |
|
|
#include "ambient.h" |
18 |
|
|
#include "random.h" |
19 |
|
|
#include "paths.h" |
20 |
|
|
|
21 |
|
|
/* persistent processes define */ |
22 |
|
|
#ifdef F_SETLKW |
23 |
|
|
#define PERSIST 1 /* normal persist */ |
24 |
|
|
#define PARALLEL 2 /* parallel persist */ |
25 |
|
|
#define PCHILD 3 /* child of normal persist */ |
26 |
|
|
#endif |
27 |
|
|
|
28 |
|
|
char *progname; /* argv[0] */ |
29 |
|
|
char *octname; /* octree name */ |
30 |
|
|
char *sigerr[NSIG]; /* signal error messages */ |
31 |
|
|
char *shm_boundary = NULL; /* boundary of shared memory */ |
32 |
|
|
char *errfile = NULL; /* error output file */ |
33 |
|
|
|
34 |
|
|
extern char *formstr(); /* string from format */ |
35 |
|
|
extern int inform; /* input format */ |
36 |
|
|
extern int outform; /* output format */ |
37 |
|
|
extern char *outvals; /* output values */ |
38 |
|
|
|
39 |
|
|
extern int hresolu; /* horizontal resolution */ |
40 |
|
|
extern int vresolu; /* vertical resolution */ |
41 |
|
|
|
42 |
|
|
extern int imm_irrad; /* compute immediate irradiance? */ |
43 |
|
|
extern int lim_dist; /* limit distance? */ |
44 |
|
|
|
45 |
|
|
extern char *tralist[]; /* list of modifers to trace (or no) */ |
46 |
|
|
extern int traincl; /* include == 1, exclude == 0 */ |
47 |
|
|
|
48 |
greg |
2.10 |
static int loadflags = ~IO_FILES; /* what to load from octree */ |
49 |
|
|
|
50 |
schorsch |
2.9 |
static void onsig(int signo); |
51 |
|
|
static void sigdie(int signo, char *msg); |
52 |
|
|
static void printdefaults(void); |
53 |
greg |
2.1 |
|
54 |
|
|
|
55 |
|
|
int |
56 |
schorsch |
2.9 |
main(int argc, char *argv[]) |
57 |
greg |
2.1 |
{ |
58 |
|
|
#define check(ol,al) if (argv[i][ol] || \ |
59 |
|
|
badarg(argc-i-1,argv+i+1,al)) \ |
60 |
|
|
goto badopt |
61 |
|
|
#define bool(olen,var) switch (argv[i][olen]) { \ |
62 |
|
|
case '\0': var = !var; break; \ |
63 |
|
|
case 'y': case 'Y': case 't': case 'T': \ |
64 |
|
|
case '+': case '1': var = 1; break; \ |
65 |
|
|
case 'n': case 'N': case 'f': case 'F': \ |
66 |
|
|
case '-': case '0': var = 0; break; \ |
67 |
|
|
default: goto badopt; } |
68 |
|
|
int persist = 0; |
69 |
|
|
char **tralp; |
70 |
|
|
int duped1; |
71 |
|
|
int rval; |
72 |
|
|
int i; |
73 |
|
|
/* global program name */ |
74 |
|
|
progname = argv[0] = fixargv0(argv[0]); |
75 |
|
|
/* option city */ |
76 |
|
|
for (i = 1; i < argc; i++) { |
77 |
|
|
/* expand arguments */ |
78 |
|
|
while ((rval = expandarg(&argc, &argv, i)) > 0) |
79 |
|
|
; |
80 |
|
|
if (rval < 0) { |
81 |
|
|
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
82 |
|
|
error(SYSTEM, errmsg); |
83 |
|
|
} |
84 |
|
|
if (argv[i] == NULL || argv[i][0] != '-') |
85 |
|
|
break; /* break from options */ |
86 |
|
|
if (!strcmp(argv[i], "-version")) { |
87 |
|
|
puts(VersionID); |
88 |
|
|
quit(0); |
89 |
|
|
} |
90 |
|
|
if (!strcmp(argv[i], "-defaults") || |
91 |
|
|
!strcmp(argv[i], "-help")) { |
92 |
|
|
printdefaults(); |
93 |
|
|
quit(0); |
94 |
|
|
} |
95 |
|
|
rval = getrenderopt(argc-i, argv+i); |
96 |
|
|
if (rval >= 0) { |
97 |
|
|
i += rval; |
98 |
|
|
continue; |
99 |
|
|
} |
100 |
|
|
switch (argv[i][1]) { |
101 |
|
|
case 'x': /* x resolution */ |
102 |
|
|
check(2,"i"); |
103 |
|
|
hresolu = atoi(argv[++i]); |
104 |
|
|
break; |
105 |
|
|
case 'y': /* y resolution */ |
106 |
|
|
check(2,"i"); |
107 |
|
|
vresolu = atoi(argv[++i]); |
108 |
|
|
break; |
109 |
|
|
case 'w': /* warnings */ |
110 |
|
|
rval = erract[WARNING].pf != NULL; |
111 |
|
|
bool(2,rval); |
112 |
|
|
if (rval) erract[WARNING].pf = wputs; |
113 |
|
|
else erract[WARNING].pf = NULL; |
114 |
|
|
break; |
115 |
|
|
case 'e': /* error file */ |
116 |
|
|
check(2,"s"); |
117 |
|
|
errfile = argv[++i]; |
118 |
|
|
break; |
119 |
|
|
case 'l': /* limit distance */ |
120 |
|
|
if (argv[i][2] != 'd') |
121 |
|
|
goto badopt; |
122 |
|
|
bool(3,lim_dist); |
123 |
|
|
break; |
124 |
|
|
case 'I': /* immed. irradiance */ |
125 |
|
|
bool(2,imm_irrad); |
126 |
|
|
break; |
127 |
|
|
case 'f': /* format i/o */ |
128 |
|
|
switch (argv[i][2]) { |
129 |
|
|
case 'a': /* ascii */ |
130 |
|
|
case 'f': /* float */ |
131 |
|
|
case 'd': /* double */ |
132 |
|
|
inform = argv[i][2]; |
133 |
|
|
break; |
134 |
|
|
default: |
135 |
|
|
goto badopt; |
136 |
|
|
} |
137 |
|
|
switch (argv[i][3]) { |
138 |
|
|
case '\0': |
139 |
|
|
outform = inform; |
140 |
|
|
break; |
141 |
|
|
case 'a': /* ascii */ |
142 |
|
|
case 'f': /* float */ |
143 |
|
|
case 'd': /* double */ |
144 |
|
|
case 'c': /* color */ |
145 |
|
|
check(4,""); |
146 |
|
|
outform = argv[i][3]; |
147 |
|
|
break; |
148 |
|
|
default: |
149 |
|
|
goto badopt; |
150 |
|
|
} |
151 |
|
|
break; |
152 |
|
|
case 'o': /* output */ |
153 |
|
|
outvals = argv[i]+2; |
154 |
|
|
break; |
155 |
|
|
case 'h': /* header output */ |
156 |
|
|
rval = loadflags & IO_INFO; |
157 |
|
|
bool(2,rval); |
158 |
|
|
loadflags = rval ? loadflags | IO_INFO : |
159 |
|
|
loadflags & ~IO_INFO; |
160 |
|
|
break; |
161 |
|
|
case 't': /* trace */ |
162 |
|
|
switch (argv[i][2]) { |
163 |
|
|
case 'i': /* include */ |
164 |
|
|
case 'I': |
165 |
|
|
check(3,"s"); |
166 |
|
|
if (traincl != 1) { |
167 |
|
|
traincl = 1; |
168 |
|
|
tralp = tralist; |
169 |
|
|
} |
170 |
|
|
if (argv[i][2] == 'I') { /* file */ |
171 |
|
|
rval = wordfile(tralp, |
172 |
greg |
2.3 |
getpath(argv[++i],getrlibpath(),R_OK)); |
173 |
greg |
2.1 |
if (rval < 0) { |
174 |
|
|
sprintf(errmsg, |
175 |
|
|
"cannot open trace include file \"%s\"", |
176 |
|
|
argv[i]); |
177 |
|
|
error(SYSTEM, errmsg); |
178 |
|
|
} |
179 |
|
|
tralp += rval; |
180 |
|
|
} else { |
181 |
|
|
*tralp++ = argv[++i]; |
182 |
|
|
*tralp = NULL; |
183 |
|
|
} |
184 |
|
|
break; |
185 |
|
|
case 'e': /* exclude */ |
186 |
|
|
case 'E': |
187 |
|
|
check(3,"s"); |
188 |
|
|
if (traincl != 0) { |
189 |
|
|
traincl = 0; |
190 |
|
|
tralp = tralist; |
191 |
|
|
} |
192 |
|
|
if (argv[i][2] == 'E') { /* file */ |
193 |
|
|
rval = wordfile(tralp, |
194 |
greg |
2.3 |
getpath(argv[++i],getrlibpath(),R_OK)); |
195 |
greg |
2.1 |
if (rval < 0) { |
196 |
|
|
sprintf(errmsg, |
197 |
|
|
"cannot open trace exclude file \"%s\"", |
198 |
|
|
argv[i]); |
199 |
|
|
error(SYSTEM, errmsg); |
200 |
|
|
} |
201 |
|
|
tralp += rval; |
202 |
|
|
} else { |
203 |
|
|
*tralp++ = argv[++i]; |
204 |
|
|
*tralp = NULL; |
205 |
|
|
} |
206 |
|
|
break; |
207 |
|
|
default: |
208 |
|
|
goto badopt; |
209 |
|
|
} |
210 |
|
|
break; |
211 |
|
|
#ifdef PERSIST |
212 |
|
|
case 'P': /* persist file */ |
213 |
|
|
if (argv[i][2] == 'P') { |
214 |
|
|
check(3,"s"); |
215 |
|
|
persist = PARALLEL; |
216 |
|
|
} else { |
217 |
|
|
check(2,"s"); |
218 |
|
|
persist = PERSIST; |
219 |
|
|
} |
220 |
|
|
persistfile(argv[++i]); |
221 |
|
|
break; |
222 |
|
|
#endif |
223 |
|
|
default: |
224 |
|
|
goto badopt; |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
/* initialize object types */ |
228 |
|
|
initotypes(); |
229 |
|
|
/* initialize urand */ |
230 |
greg |
2.14 |
if (rand_samp) { |
231 |
|
|
srandom((long)time(0)); |
232 |
|
|
initurand(0); |
233 |
|
|
} else { |
234 |
|
|
srandom(0L); |
235 |
|
|
initurand(2048); |
236 |
|
|
} |
237 |
greg |
2.1 |
/* set up signal handling */ |
238 |
|
|
sigdie(SIGINT, "Interrupt"); |
239 |
schorsch |
2.5 |
#ifdef SIGHUP |
240 |
greg |
2.1 |
sigdie(SIGHUP, "Hangup"); |
241 |
schorsch |
2.5 |
#endif |
242 |
greg |
2.1 |
sigdie(SIGTERM, "Terminate"); |
243 |
schorsch |
2.5 |
#ifdef SIGPIPE |
244 |
greg |
2.1 |
sigdie(SIGPIPE, "Broken pipe"); |
245 |
schorsch |
2.5 |
#endif |
246 |
|
|
#ifdef SIGALRM |
247 |
greg |
2.1 |
sigdie(SIGALRM, "Alarm clock"); |
248 |
schorsch |
2.5 |
#endif |
249 |
greg |
2.1 |
#ifdef SIGXCPU |
250 |
|
|
sigdie(SIGXCPU, "CPU limit exceeded"); |
251 |
|
|
sigdie(SIGXFSZ, "File size exceeded"); |
252 |
|
|
#endif |
253 |
|
|
/* open error file */ |
254 |
|
|
if (errfile != NULL) { |
255 |
|
|
if (freopen(errfile, "a", stderr) == NULL) |
256 |
|
|
quit(2); |
257 |
|
|
fprintf(stderr, "**************\n*** PID %5d: ", |
258 |
|
|
getpid()); |
259 |
|
|
printargs(argc, argv, stderr); |
260 |
|
|
putc('\n', stderr); |
261 |
|
|
fflush(stderr); |
262 |
|
|
} |
263 |
|
|
#ifdef NICE |
264 |
|
|
nice(NICE); /* lower priority */ |
265 |
|
|
#endif |
266 |
|
|
/* get octree */ |
267 |
|
|
if (i == argc) |
268 |
|
|
octname = NULL; |
269 |
|
|
else if (i == argc-1) |
270 |
|
|
octname = argv[i]; |
271 |
|
|
else |
272 |
|
|
goto badopt; |
273 |
|
|
if (octname == NULL) |
274 |
|
|
error(USER, "missing octree argument"); |
275 |
|
|
/* set up output */ |
276 |
|
|
#ifdef PERSIST |
277 |
|
|
if (persist) { |
278 |
|
|
duped1 = dup(fileno(stdout)); /* don't lose our output */ |
279 |
|
|
openheader(); |
280 |
|
|
} |
281 |
|
|
#endif |
282 |
|
|
if (outform != 'a') |
283 |
schorsch |
2.4 |
SET_FILE_BINARY(stdout); |
284 |
greg |
2.1 |
readoct(octname, loadflags, &thescene, NULL); |
285 |
|
|
nsceneobjs = nobjects; |
286 |
|
|
|
287 |
|
|
if (loadflags & IO_INFO) { /* print header */ |
288 |
|
|
printargs(i, argv, stdout); |
289 |
|
|
printf("SOFTWARE= %s\n", VersionID); |
290 |
|
|
fputnow(stdout); |
291 |
|
|
fputformat(formstr(outform), stdout); |
292 |
|
|
putchar('\n'); |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
marksources(); /* find and mark sources */ |
296 |
|
|
|
297 |
|
|
setambient(); /* initialize ambient calculation */ |
298 |
|
|
|
299 |
|
|
#ifdef PERSIST |
300 |
|
|
if (persist) { |
301 |
|
|
fflush(stdout); |
302 |
|
|
/* reconnect stdout */ |
303 |
|
|
dup2(duped1, fileno(stdout)); |
304 |
|
|
close(duped1); |
305 |
|
|
if (persist == PARALLEL) { /* multiprocessing */ |
306 |
|
|
preload_objs(); /* preload scene */ |
307 |
|
|
shm_boundary = (char *)malloc(16); |
308 |
|
|
strcpy(shm_boundary, "SHM_BOUNDARY"); |
309 |
|
|
while ((rval=fork()) == 0) { /* keep on forkin' */ |
310 |
|
|
pflock(1); |
311 |
|
|
pfhold(); |
312 |
greg |
2.15 |
ambsync(); /* load new values */ |
313 |
greg |
2.1 |
} |
314 |
|
|
if (rval < 0) |
315 |
|
|
error(SYSTEM, "cannot fork child for persist function"); |
316 |
greg |
2.15 |
pfdetach(); /* parent will run then exit */ |
317 |
greg |
2.1 |
} |
318 |
|
|
} |
319 |
|
|
runagain: |
320 |
|
|
if (persist) |
321 |
|
|
dupheader(); /* send header to stdout */ |
322 |
|
|
#endif |
323 |
|
|
/* trace rays */ |
324 |
|
|
rtrace(NULL); |
325 |
|
|
/* flush ambient file */ |
326 |
|
|
ambsync(); |
327 |
|
|
#ifdef PERSIST |
328 |
|
|
if (persist == PERSIST) { /* first run-through */ |
329 |
|
|
if ((rval=fork()) == 0) { /* child loops until killed */ |
330 |
|
|
pflock(1); |
331 |
|
|
persist = PCHILD; |
332 |
|
|
} else { /* original process exits */ |
333 |
|
|
if (rval < 0) |
334 |
|
|
error(SYSTEM, "cannot fork child for persist function"); |
335 |
|
|
pfdetach(); /* parent exits */ |
336 |
|
|
} |
337 |
|
|
} |
338 |
|
|
if (persist == PCHILD) { /* wait for a signal then go again */ |
339 |
|
|
close(duped1); /* release output handle */ |
340 |
|
|
pfhold(); |
341 |
|
|
raynum = nrays = 0; /* reinitialize */ |
342 |
|
|
goto runagain; |
343 |
|
|
} |
344 |
|
|
#endif |
345 |
|
|
quit(0); |
346 |
|
|
|
347 |
|
|
badopt: |
348 |
|
|
sprintf(errmsg, "command line error at '%s'", argv[i]); |
349 |
|
|
error(USER, errmsg); |
350 |
schorsch |
2.9 |
return 1; /* pro forma return */ |
351 |
greg |
2.1 |
|
352 |
|
|
#undef check |
353 |
|
|
#undef bool |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
|
357 |
|
|
void |
358 |
schorsch |
2.9 |
wputs( /* warning output function */ |
359 |
|
|
char *s |
360 |
|
|
) |
361 |
greg |
2.1 |
{ |
362 |
|
|
int lasterrno = errno; |
363 |
|
|
eputs(s); |
364 |
|
|
errno = lasterrno; |
365 |
|
|
} |
366 |
|
|
|
367 |
|
|
|
368 |
|
|
void |
369 |
schorsch |
2.9 |
eputs( /* put string to stderr */ |
370 |
|
|
register char *s |
371 |
|
|
) |
372 |
greg |
2.1 |
{ |
373 |
|
|
static int midline = 0; |
374 |
|
|
|
375 |
|
|
if (!*s) |
376 |
|
|
return; |
377 |
|
|
if (!midline++) { |
378 |
|
|
fputs(progname, stderr); |
379 |
|
|
fputs(": ", stderr); |
380 |
|
|
} |
381 |
|
|
fputs(s, stderr); |
382 |
|
|
if (s[strlen(s)-1] == '\n') { |
383 |
|
|
fflush(stderr); |
384 |
|
|
midline = 0; |
385 |
|
|
} |
386 |
|
|
} |
387 |
|
|
|
388 |
|
|
|
389 |
schorsch |
2.9 |
static void |
390 |
|
|
onsig( /* fatal signal */ |
391 |
|
|
int signo |
392 |
|
|
) |
393 |
greg |
2.1 |
{ |
394 |
|
|
static int gotsig = 0; |
395 |
|
|
|
396 |
|
|
if (gotsig++) /* two signals and we're gone! */ |
397 |
|
|
_exit(signo); |
398 |
|
|
|
399 |
schorsch |
2.5 |
#ifdef SIGALRM |
400 |
greg |
2.1 |
alarm(15); /* allow 15 seconds to clean up */ |
401 |
|
|
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
402 |
schorsch |
2.5 |
#endif |
403 |
greg |
2.1 |
eputs("signal - "); |
404 |
|
|
eputs(sigerr[signo]); |
405 |
|
|
eputs("\n"); |
406 |
|
|
quit(3); |
407 |
|
|
} |
408 |
|
|
|
409 |
|
|
|
410 |
schorsch |
2.9 |
static void |
411 |
|
|
sigdie( /* set fatal signal */ |
412 |
|
|
int signo, |
413 |
|
|
char *msg |
414 |
|
|
) |
415 |
greg |
2.1 |
{ |
416 |
|
|
if (signal(signo, onsig) == SIG_IGN) |
417 |
|
|
signal(signo, SIG_IGN); |
418 |
|
|
sigerr[signo] = msg; |
419 |
|
|
} |
420 |
|
|
|
421 |
|
|
|
422 |
schorsch |
2.9 |
static void |
423 |
schorsch |
2.8 |
printdefaults(void) /* print default values to stdout */ |
424 |
greg |
2.1 |
{ |
425 |
|
|
register char *cp; |
426 |
|
|
|
427 |
|
|
if (imm_irrad) |
428 |
|
|
printf("-I+\t\t\t\t# immediate irradiance on\n"); |
429 |
greg |
2.10 |
printf("-x %-9d\t\t\t# x resolution (flush interval)\n", hresolu); |
430 |
greg |
2.1 |
printf("-y %-9d\t\t\t# y resolution\n", vresolu); |
431 |
|
|
printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" : |
432 |
|
|
"-ld-\t\t\t\t# limit distance off\n"); |
433 |
greg |
2.10 |
printf("-h%c\t\t\t\t# %s header\n", loadflags & IO_INFO ? '+' : '-', |
434 |
|
|
loadflags & IO_INFO ? "output" : "no"); |
435 |
greg |
2.1 |
printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n", |
436 |
|
|
inform, outform, formstr(inform), formstr(outform)); |
437 |
greg |
2.10 |
printf("-o%-9s\t\t\t# output", outvals); |
438 |
greg |
2.1 |
for (cp = outvals; *cp; cp++) |
439 |
|
|
switch (*cp) { |
440 |
greg |
2.10 |
case 't': case 'T': printf(" trace"); break; |
441 |
greg |
2.1 |
case 'o': printf(" origin"); break; |
442 |
|
|
case 'd': printf(" direction"); break; |
443 |
|
|
case 'v': printf(" value"); break; |
444 |
greg |
2.13 |
case 'V': printf(" contribution"); break; |
445 |
greg |
2.1 |
case 'l': printf(" length"); break; |
446 |
|
|
case 'L': printf(" first_length"); break; |
447 |
|
|
case 'p': printf(" point"); break; |
448 |
|
|
case 'n': printf(" normal"); break; |
449 |
|
|
case 'N': printf(" unperturbed_normal"); break; |
450 |
|
|
case 's': printf(" surface"); break; |
451 |
|
|
case 'w': printf(" weight"); break; |
452 |
greg |
2.13 |
case 'W': printf(" coefficient"); break; |
453 |
greg |
2.1 |
case 'm': printf(" modifier"); break; |
454 |
greg |
2.10 |
case 'M': printf(" material"); break; |
455 |
|
|
case '-': printf(" stroke"); break; |
456 |
greg |
2.1 |
} |
457 |
|
|
putchar('\n'); |
458 |
|
|
printf(erract[WARNING].pf != NULL ? |
459 |
|
|
"-w+\t\t\t\t# warning messages on\n" : |
460 |
|
|
"-w-\t\t\t\t# warning messages off\n"); |
461 |
|
|
print_rdefaults(); |
462 |
|
|
} |