ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.17
Committed: Mon Jun 22 20:52:56 2009 UTC (14 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +1 -2 lines
Log Message:
Bug fix in -P option implementation

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rtmain.c,v 2.16 2008/04/18 18:06:29 greg Exp $";
3 #endif
4 /*
5 * rtmain.c - main for rtrace per-ray calculation program
6 */
7
8 #include "copyright.h"
9
10 #include <signal.h>
11
12 #include "platform.h"
13 #include "rtprocess.h" /* getpid() */
14 #include "resolu.h"
15 #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 static int loadflags = ~IO_FILES; /* what to load from octree */
49
50 static void onsig(int signo);
51 static void sigdie(int signo, char *msg);
52 static void printdefaults(void);
53
54
55 int
56 main(int argc, char *argv[])
57 {
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 getpath(argv[++i],getrlibpath(),R_OK));
173 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 getpath(argv[++i],getrlibpath(),R_OK));
195 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 if (rand_samp) {
231 srandom((long)time(0));
232 initurand(0);
233 } else {
234 srandom(0L);
235 initurand(2048);
236 }
237 /* set up signal handling */
238 sigdie(SIGINT, "Interrupt");
239 #ifdef SIGHUP
240 sigdie(SIGHUP, "Hangup");
241 #endif
242 sigdie(SIGTERM, "Terminate");
243 #ifdef SIGPIPE
244 sigdie(SIGPIPE, "Broken pipe");
245 #endif
246 #ifdef SIGALRM
247 sigdie(SIGALRM, "Alarm clock");
248 #endif
249 #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 SET_FILE_BINARY(stdout);
284 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 ambsync(); /* load new values */
313 }
314 if (rval < 0)
315 error(SYSTEM, "cannot fork child for persist function");
316 pfdetach(); /* parent will run then exit */
317 }
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 pfhold();
340 raynum = nrays = 0; /* reinitialize */
341 goto runagain;
342 }
343 #endif
344 quit(0);
345
346 badopt:
347 sprintf(errmsg, "command line error at '%s'", argv[i]);
348 error(USER, errmsg);
349 return 1; /* pro forma return */
350
351 #undef check
352 #undef bool
353 }
354
355
356 void
357 wputs( /* warning output function */
358 char *s
359 )
360 {
361 int lasterrno = errno;
362 eputs(s);
363 errno = lasterrno;
364 }
365
366
367 void
368 eputs( /* put string to stderr */
369 register char *s
370 )
371 {
372 static int midline = 0;
373
374 if (!*s)
375 return;
376 if (!midline++) {
377 fputs(progname, stderr);
378 fputs(": ", stderr);
379 }
380 fputs(s, stderr);
381 if (s[strlen(s)-1] == '\n') {
382 fflush(stderr);
383 midline = 0;
384 }
385 }
386
387
388 static void
389 onsig( /* fatal signal */
390 int signo
391 )
392 {
393 static int gotsig = 0;
394
395 if (gotsig++) /* two signals and we're gone! */
396 _exit(signo);
397
398 #ifdef SIGALRM
399 alarm(15); /* allow 15 seconds to clean up */
400 signal(SIGALRM, SIG_DFL); /* make certain we do die */
401 #endif
402 eputs("signal - ");
403 eputs(sigerr[signo]);
404 eputs("\n");
405 quit(3);
406 }
407
408
409 static void
410 sigdie( /* set fatal signal */
411 int signo,
412 char *msg
413 )
414 {
415 if (signal(signo, onsig) == SIG_IGN)
416 signal(signo, SIG_IGN);
417 sigerr[signo] = msg;
418 }
419
420
421 static void
422 printdefaults(void) /* print default values to stdout */
423 {
424 register char *cp;
425
426 if (imm_irrad)
427 printf("-I+\t\t\t\t# immediate irradiance on\n");
428 printf("-x %-9d\t\t\t# x resolution (flush interval)\n", hresolu);
429 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
430 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
431 "-ld-\t\t\t\t# limit distance off\n");
432 printf("-h%c\t\t\t\t# %s header\n", loadflags & IO_INFO ? '+' : '-',
433 loadflags & IO_INFO ? "output" : "no");
434 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
435 inform, outform, formstr(inform), formstr(outform));
436 printf("-o%-9s\t\t\t# output", outvals);
437 for (cp = outvals; *cp; cp++)
438 switch (*cp) {
439 case 't': case 'T': printf(" trace"); break;
440 case 'o': printf(" origin"); break;
441 case 'd': printf(" direction"); break;
442 case 'v': printf(" value"); break;
443 case 'V': printf(" contribution"); break;
444 case 'l': printf(" length"); break;
445 case 'L': printf(" first_length"); break;
446 case 'p': printf(" point"); break;
447 case 'n': printf(" normal"); break;
448 case 'N': printf(" unperturbed_normal"); break;
449 case 's': printf(" surface"); break;
450 case 'w': printf(" weight"); break;
451 case 'W': printf(" coefficient"); break;
452 case 'm': printf(" modifier"); break;
453 case 'M': printf(" material"); break;
454 case '-': printf(" stroke"); break;
455 }
456 putchar('\n');
457 printf(erract[WARNING].pf != NULL ?
458 "-w+\t\t\t\t# warning messages on\n" :
459 "-w-\t\t\t\t# warning messages off\n");
460 print_rdefaults();
461 }