ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.18
Committed: Sat Dec 12 19:01:00 2009 UTC (14 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.17: +54 -17 lines
Log Message:
Added -n option to rtrace and moved quit() funciton out of raypcalls

File Contents

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