ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.10
Committed: Wed May 25 04:44:26 2005 UTC (18 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.9: +11 -5 lines
Log Message:
Created rtcontrib program for computing ray contributions and coefficients

File Contents

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