ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.11
Committed: Thu Jun 2 04:47:27 2005 UTC (18 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +1 -5 lines
Log Message:
Added binary i/o options to total and piped output to rtcontrib

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id: rtmain.c,v 2.10 2005/05/25 04:44:26 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 <sys/types.h>
11     #include <signal.h>
12    
13     #include "platform.h"
14 schorsch 2.7 #include "rtprocess.h" /* getpid() */
15 schorsch 2.9 #include "resolu.h"
16 greg 2.1 #include "ray.h"
17     #include "source.h"
18     #include "ambient.h"
19     #include "random.h"
20     #include "paths.h"
21    
22     /* persistent processes define */
23     #ifdef F_SETLKW
24     #define PERSIST 1 /* normal persist */
25     #define PARALLEL 2 /* parallel persist */
26     #define PCHILD 3 /* child of normal persist */
27     #endif
28    
29     char *progname; /* argv[0] */
30     char *octname; /* octree name */
31     char *sigerr[NSIG]; /* signal error messages */
32     char *shm_boundary = NULL; /* boundary of shared memory */
33     char *errfile = NULL; /* error output file */
34    
35     extern char *formstr(); /* string from format */
36     extern int inform; /* input format */
37     extern int outform; /* output format */
38     extern char *outvals; /* output values */
39    
40     extern int hresolu; /* horizontal resolution */
41     extern int vresolu; /* vertical resolution */
42    
43     extern int imm_irrad; /* compute immediate irradiance? */
44     extern int lim_dist; /* limit distance? */
45    
46     extern char *tralist[]; /* list of modifers to trace (or no) */
47     extern int traincl; /* include == 1, exclude == 0 */
48    
49 greg 2.10 static int loadflags = ~IO_FILES; /* what to load from octree */
50    
51 schorsch 2.9 static void onsig(int signo);
52     static void sigdie(int signo, char *msg);
53     static void printdefaults(void);
54 greg 2.1
55    
56     int
57 schorsch 2.9 main(int argc, char *argv[])
58 greg 2.1 {
59     #define check(ol,al) if (argv[i][ol] || \
60     badarg(argc-i-1,argv+i+1,al)) \
61     goto badopt
62     #define bool(olen,var) switch (argv[i][olen]) { \
63     case '\0': var = !var; break; \
64     case 'y': case 'Y': case 't': case 'T': \
65     case '+': case '1': var = 1; break; \
66     case 'n': case 'N': case 'f': case 'F': \
67     case '-': case '0': var = 0; break; \
68     default: goto badopt; }
69     int persist = 0;
70     char **tralp;
71     int duped1;
72     int rval;
73     int i;
74     /* global program name */
75     progname = argv[0] = fixargv0(argv[0]);
76     /* option city */
77     for (i = 1; i < argc; i++) {
78     /* expand arguments */
79     while ((rval = expandarg(&argc, &argv, i)) > 0)
80     ;
81     if (rval < 0) {
82     sprintf(errmsg, "cannot expand '%s'", argv[i]);
83     error(SYSTEM, errmsg);
84     }
85     if (argv[i] == NULL || argv[i][0] != '-')
86     break; /* break from options */
87     if (!strcmp(argv[i], "-version")) {
88     puts(VersionID);
89     quit(0);
90     }
91     if (!strcmp(argv[i], "-defaults") ||
92     !strcmp(argv[i], "-help")) {
93     printdefaults();
94     quit(0);
95     }
96     rval = getrenderopt(argc-i, argv+i);
97     if (rval >= 0) {
98     i += rval;
99     continue;
100     }
101     switch (argv[i][1]) {
102     case 'x': /* x resolution */
103     check(2,"i");
104     hresolu = atoi(argv[++i]);
105     break;
106     case 'y': /* y resolution */
107     check(2,"i");
108     vresolu = atoi(argv[++i]);
109     break;
110     case 'w': /* warnings */
111     rval = erract[WARNING].pf != NULL;
112     bool(2,rval);
113     if (rval) erract[WARNING].pf = wputs;
114     else erract[WARNING].pf = NULL;
115     break;
116     case 'e': /* error file */
117     check(2,"s");
118     errfile = argv[++i];
119     break;
120     case 'l': /* limit distance */
121     if (argv[i][2] != 'd')
122     goto badopt;
123     bool(3,lim_dist);
124     break;
125     case 'I': /* immed. irradiance */
126     bool(2,imm_irrad);
127     break;
128     case 'f': /* format i/o */
129     switch (argv[i][2]) {
130     case 'a': /* ascii */
131     case 'f': /* float */
132     case 'd': /* double */
133     inform = argv[i][2];
134     break;
135     default:
136     goto badopt;
137     }
138     switch (argv[i][3]) {
139     case '\0':
140     outform = inform;
141     break;
142     case 'a': /* ascii */
143     case 'f': /* float */
144     case 'd': /* double */
145     case 'c': /* color */
146     check(4,"");
147     outform = argv[i][3];
148     break;
149     default:
150     goto badopt;
151     }
152     break;
153     case 'o': /* output */
154     outvals = argv[i]+2;
155     break;
156     case 'h': /* header output */
157     rval = loadflags & IO_INFO;
158     bool(2,rval);
159     loadflags = rval ? loadflags | IO_INFO :
160     loadflags & ~IO_INFO;
161     break;
162     case 't': /* trace */
163     switch (argv[i][2]) {
164     case 'i': /* include */
165     case 'I':
166     check(3,"s");
167     if (traincl != 1) {
168     traincl = 1;
169     tralp = tralist;
170     }
171     if (argv[i][2] == 'I') { /* file */
172     rval = wordfile(tralp,
173 greg 2.3 getpath(argv[++i],getrlibpath(),R_OK));
174 greg 2.1 if (rval < 0) {
175     sprintf(errmsg,
176     "cannot open trace include file \"%s\"",
177     argv[i]);
178     error(SYSTEM, errmsg);
179     }
180     tralp += rval;
181     } else {
182     *tralp++ = argv[++i];
183     *tralp = NULL;
184     }
185     break;
186     case 'e': /* exclude */
187     case 'E':
188     check(3,"s");
189     if (traincl != 0) {
190     traincl = 0;
191     tralp = tralist;
192     }
193     if (argv[i][2] == 'E') { /* file */
194     rval = wordfile(tralp,
195 greg 2.3 getpath(argv[++i],getrlibpath(),R_OK));
196 greg 2.1 if (rval < 0) {
197     sprintf(errmsg,
198     "cannot open trace exclude file \"%s\"",
199     argv[i]);
200     error(SYSTEM, errmsg);
201     }
202     tralp += rval;
203     } else {
204     *tralp++ = argv[++i];
205     *tralp = NULL;
206     }
207     break;
208     default:
209     goto badopt;
210     }
211     break;
212     #ifdef PERSIST
213     case 'P': /* persist file */
214     if (argv[i][2] == 'P') {
215     check(3,"s");
216     persist = PARALLEL;
217     } else {
218     check(2,"s");
219     persist = PERSIST;
220     }
221     persistfile(argv[++i]);
222     break;
223     #endif
224     default:
225     goto badopt;
226     }
227     }
228     /* initialize object types */
229     initotypes();
230     /* initialize urand */
231     initurand(2048);
232     /* 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     case 'l': printf(" length"); break;
439     case 'L': printf(" first_length"); break;
440     case 'p': printf(" point"); break;
441     case 'n': printf(" normal"); break;
442     case 'N': printf(" unperturbed_normal"); break;
443     case 's': printf(" surface"); break;
444     case 'w': printf(" weight"); break;
445     case 'm': printf(" modifier"); break;
446 greg 2.10 case 'M': printf(" material"); break;
447     case 'W': printf(" contribution"); break;
448     case '-': printf(" stroke"); break;
449 greg 2.1 }
450     putchar('\n');
451     printf(erract[WARNING].pf != NULL ?
452     "-w+\t\t\t\t# warning messages on\n" :
453     "-w-\t\t\t\t# warning messages off\n");
454     print_rdefaults();
455     }