ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.22
Committed: Wed Aug 7 05:10:09 2013 UTC (10 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad4R2, rad4R2P1
Changes since 2.21: +3 -3 lines
Log Message:
Eliminated a number of minor warnings (all innocuous)

File Contents

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