ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.13
Committed: Sun Feb 5 22:22:20 2006 UTC (18 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +3 -2 lines
Log Message:
Added -V option to rtcontrib and -oV option to rtrace to report contributions

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.13 static const char RCSid[] = "$Id: rtmain.c,v 2.12 2005/06/15 15:36:52 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     initurand(2048);
231 greg 2.12 srandom(rand_samp ? (long)time(0) : 0L);
232 greg 2.1 /* set up signal handling */
233     sigdie(SIGINT, "Interrupt");
234 schorsch 2.5 #ifdef SIGHUP
235 greg 2.1 sigdie(SIGHUP, "Hangup");
236 schorsch 2.5 #endif
237 greg 2.1 sigdie(SIGTERM, "Terminate");
238 schorsch 2.5 #ifdef SIGPIPE
239 greg 2.1 sigdie(SIGPIPE, "Broken pipe");
240 schorsch 2.5 #endif
241     #ifdef SIGALRM
242 greg 2.1 sigdie(SIGALRM, "Alarm clock");
243 schorsch 2.5 #endif
244 greg 2.1 #ifdef SIGXCPU
245     sigdie(SIGXCPU, "CPU limit exceeded");
246     sigdie(SIGXFSZ, "File size exceeded");
247     #endif
248     /* open error file */
249     if (errfile != NULL) {
250     if (freopen(errfile, "a", stderr) == NULL)
251     quit(2);
252     fprintf(stderr, "**************\n*** PID %5d: ",
253     getpid());
254     printargs(argc, argv, stderr);
255     putc('\n', stderr);
256     fflush(stderr);
257     }
258     #ifdef NICE
259     nice(NICE); /* lower priority */
260     #endif
261     /* get octree */
262     if (i == argc)
263     octname = NULL;
264     else if (i == argc-1)
265     octname = argv[i];
266     else
267     goto badopt;
268     if (octname == NULL)
269     error(USER, "missing octree argument");
270     /* set up output */
271     #ifdef PERSIST
272     if (persist) {
273     duped1 = dup(fileno(stdout)); /* don't lose our output */
274     openheader();
275     }
276     #endif
277     if (outform != 'a')
278 schorsch 2.4 SET_FILE_BINARY(stdout);
279 greg 2.1 readoct(octname, loadflags, &thescene, NULL);
280     nsceneobjs = nobjects;
281    
282     if (loadflags & IO_INFO) { /* print header */
283     printargs(i, argv, stdout);
284     printf("SOFTWARE= %s\n", VersionID);
285     fputnow(stdout);
286     fputformat(formstr(outform), stdout);
287     putchar('\n');
288     }
289    
290     marksources(); /* find and mark sources */
291    
292     setambient(); /* initialize ambient calculation */
293    
294     #ifdef PERSIST
295     if (persist) {
296     fflush(stdout);
297     /* reconnect stdout */
298     dup2(duped1, fileno(stdout));
299     close(duped1);
300     if (persist == PARALLEL) { /* multiprocessing */
301     preload_objs(); /* preload scene */
302     shm_boundary = (char *)malloc(16);
303     strcpy(shm_boundary, "SHM_BOUNDARY");
304     while ((rval=fork()) == 0) { /* keep on forkin' */
305     pflock(1);
306     pfhold();
307     }
308     if (rval < 0)
309     error(SYSTEM, "cannot fork child for persist function");
310     pfdetach(); /* parent exits */
311     }
312     }
313     runagain:
314     if (persist)
315     dupheader(); /* send header to stdout */
316     #endif
317     /* trace rays */
318     rtrace(NULL);
319     /* flush ambient file */
320     ambsync();
321     #ifdef PERSIST
322     if (persist == PERSIST) { /* first run-through */
323     if ((rval=fork()) == 0) { /* child loops until killed */
324     pflock(1);
325     persist = PCHILD;
326     } else { /* original process exits */
327     if (rval < 0)
328     error(SYSTEM, "cannot fork child for persist function");
329     pfdetach(); /* parent exits */
330     }
331     }
332     if (persist == PCHILD) { /* wait for a signal then go again */
333     close(duped1); /* release output handle */
334     pfhold();
335     raynum = nrays = 0; /* reinitialize */
336     goto runagain;
337     }
338     #endif
339     quit(0);
340    
341     badopt:
342     sprintf(errmsg, "command line error at '%s'", argv[i]);
343     error(USER, errmsg);
344 schorsch 2.9 return 1; /* pro forma return */
345 greg 2.1
346     #undef check
347     #undef bool
348     }
349    
350    
351     void
352 schorsch 2.9 wputs( /* warning output function */
353     char *s
354     )
355 greg 2.1 {
356     int lasterrno = errno;
357     eputs(s);
358     errno = lasterrno;
359     }
360    
361    
362     void
363 schorsch 2.9 eputs( /* put string to stderr */
364     register char *s
365     )
366 greg 2.1 {
367     static int midline = 0;
368    
369     if (!*s)
370     return;
371     if (!midline++) {
372     fputs(progname, stderr);
373     fputs(": ", stderr);
374     }
375     fputs(s, stderr);
376     if (s[strlen(s)-1] == '\n') {
377     fflush(stderr);
378     midline = 0;
379     }
380     }
381    
382    
383 schorsch 2.9 static void
384     onsig( /* fatal signal */
385     int signo
386     )
387 greg 2.1 {
388     static int gotsig = 0;
389    
390     if (gotsig++) /* two signals and we're gone! */
391     _exit(signo);
392    
393 schorsch 2.5 #ifdef SIGALRM
394 greg 2.1 alarm(15); /* allow 15 seconds to clean up */
395     signal(SIGALRM, SIG_DFL); /* make certain we do die */
396 schorsch 2.5 #endif
397 greg 2.1 eputs("signal - ");
398     eputs(sigerr[signo]);
399     eputs("\n");
400     quit(3);
401     }
402    
403    
404 schorsch 2.9 static void
405     sigdie( /* set fatal signal */
406     int signo,
407     char *msg
408     )
409 greg 2.1 {
410     if (signal(signo, onsig) == SIG_IGN)
411     signal(signo, SIG_IGN);
412     sigerr[signo] = msg;
413     }
414    
415    
416 schorsch 2.9 static void
417 schorsch 2.8 printdefaults(void) /* print default values to stdout */
418 greg 2.1 {
419     register char *cp;
420    
421     if (imm_irrad)
422     printf("-I+\t\t\t\t# immediate irradiance on\n");
423 greg 2.10 printf("-x %-9d\t\t\t# x resolution (flush interval)\n", hresolu);
424 greg 2.1 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
425     printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
426     "-ld-\t\t\t\t# limit distance off\n");
427 greg 2.10 printf("-h%c\t\t\t\t# %s header\n", loadflags & IO_INFO ? '+' : '-',
428     loadflags & IO_INFO ? "output" : "no");
429 greg 2.1 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
430     inform, outform, formstr(inform), formstr(outform));
431 greg 2.10 printf("-o%-9s\t\t\t# output", outvals);
432 greg 2.1 for (cp = outvals; *cp; cp++)
433     switch (*cp) {
434 greg 2.10 case 't': case 'T': printf(" trace"); break;
435 greg 2.1 case 'o': printf(" origin"); break;
436     case 'd': printf(" direction"); break;
437     case 'v': printf(" value"); break;
438 greg 2.13 case 'V': printf(" contribution"); break;
439 greg 2.1 case 'l': printf(" length"); break;
440     case 'L': printf(" first_length"); break;
441     case 'p': printf(" point"); break;
442     case 'n': printf(" normal"); break;
443     case 'N': printf(" unperturbed_normal"); break;
444     case 's': printf(" surface"); break;
445     case 'w': printf(" weight"); break;
446 greg 2.13 case 'W': printf(" coefficient"); break;
447 greg 2.1 case 'm': printf(" modifier"); break;
448 greg 2.10 case 'M': printf(" material"); break;
449     case '-': printf(" stroke"); break;
450 greg 2.1 }
451     putchar('\n');
452     printf(erract[WARNING].pf != NULL ?
453     "-w+\t\t\t\t# warning messages on\n" :
454     "-w-\t\t\t\t# warning messages off\n");
455     print_rdefaults();
456     }