ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.5
Committed: Thu Jun 21 17:14:32 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +1 -3 lines
Log Message:
Accommodations for Windows

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.5 static const char RCSid[] = "$Id: rcmain.c,v 2.4 2012/06/15 00:57:40 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * rcmain.c - main for rtcontrib ray contribution tracer
6     */
7    
8     #include "copyright.h"
9    
10     #include <signal.h>
11     #include "rcontrib.h"
12     #include "source.h"
13     #include "ambient.h"
14    
15     int gargc; /* global argc */
16     char **gargv; /* global argv */
17     char *octname; /* global octree name */
18     char *progname; /* global argv[0] */
19    
20     char *sigerr[NSIG]; /* signal error messages */
21    
22     int nproc = 1; /* number of processes requested */
23     int nchild = 0; /* number of children (-1 in child) */
24    
25     int inpfmt = 'a'; /* input format */
26     int outfmt = 'a'; /* output format */
27    
28     int header = 1; /* output header? */
29     int force_open = 0; /* truncate existing output? */
30     int recover = 0; /* recover previous output? */
31     int accumulate = 1; /* input rays per output record */
32     int contrib = 0; /* computing contributions? */
33    
34     int xres = 0; /* horizontal (scan) size */
35     int yres = 0; /* vertical resolution */
36    
37     int using_stdout = 0; /* are we using stdout? */
38    
39     int imm_irrad = 0; /* compute immediate irradiance? */
40     int lim_dist = 0; /* limit distance? */
41    
42     const char *modname[MAXMODLIST]; /* ordered modifier name list */
43     int nmods = 0; /* number of modifiers */
44    
45 greg 2.4 void (*addobjnotify[8])() = {ambnotify, NULL};
46    
47 greg 2.1 char RCCONTEXT[] = "RCONTRIB"; /* our special evaluation context */
48    
49    
50     static void
51     printdefaults(void) /* print default values to stdout */
52     {
53     char *cp;
54    
55 greg 2.3 printf("-c %-5d\t\t\t# accumulated rays per record\n", accumulate);
56     printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
57     contrib ? "contributions" : "coefficients");
58 greg 2.1 if (imm_irrad)
59     printf("-I+\t\t\t\t# immediate irradiance on\n");
60     printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
61     printf("-x %-9d\t\t\t# %s\n", xres,
62     yres && xres ? "x resolution" : "flush interval");
63     printf("-y %-9d\t\t\t# y resolution\n", yres);
64     printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
65     "-ld-\t\t\t\t# limit distance off\n");
66     printf("-h%c\t\t\t\t# %s header\n", header ? '+' : '-',
67     header ? "output" : "no");
68     printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
69     inpfmt, outfmt, formstr(inpfmt), formstr(outfmt));
70     printf(erract[WARNING].pf != NULL ?
71     "-w+\t\t\t\t# warning messages on\n" :
72     "-w-\t\t\t\t# warning messages off\n");
73     print_rdefaults();
74     }
75    
76    
77     static void
78     onsig( /* fatal signal */
79     int signo
80     )
81     {
82     static int gotsig = 0;
83    
84     if (gotsig++) /* two signals and we're gone! */
85     _exit(signo);
86    
87     #ifdef SIGALRM
88     alarm(15); /* allow 15 seconds to clean up */
89     signal(SIGALRM, SIG_DFL); /* make certain we do die */
90     #endif
91     eputs("signal - ");
92     eputs(sigerr[signo]);
93     eputs("\n");
94     quit(3);
95     }
96    
97    
98     static void
99     sigdie( /* set fatal signal */
100     int signo,
101     char *msg
102     )
103     {
104     if (signal(signo, onsig) == SIG_IGN)
105     signal(signo, SIG_IGN);
106     sigerr[signo] = msg;
107     }
108    
109    
110     /* set input/output format */
111     static void
112     setformat(const char *fmt)
113     {
114     switch (fmt[0]) {
115     case 'f':
116     case 'd':
117     SET_FILE_BINARY(stdin);
118     /* fall through */
119     case 'a':
120     inpfmt = fmt[0];
121     break;
122     default:
123     goto fmterr;
124     }
125     switch (fmt[1]) {
126     case '\0':
127     outfmt = inpfmt;
128     return;
129     case 'a':
130     case 'f':
131     case 'd':
132     case 'c':
133     outfmt = fmt[1];
134     break;
135     default:
136     goto fmterr;
137     }
138     if (!fmt[2])
139     return;
140     fmterr:
141     sprintf(errmsg, "Illegal i/o format: -f%s", fmt);
142     error(USER, errmsg);
143     }
144    
145    
146     /* Set overriding options */
147     static void
148     override_options(void)
149     {
150     shadthresh = 0;
151     ambssamp = 0;
152     ambacc = 0;
153     if (accumulate <= 0) /* no output flushing for single record */
154     xres = yres = 0;
155     }
156    
157    
158     int
159     main(int argc, char *argv[])
160     {
161     #define check(ol,al) if (argv[i][ol] || \
162     badarg(argc-i-1,argv+i+1,al)) \
163     goto badopt
164     #define bool(olen,var) switch (argv[i][olen]) { \
165     case '\0': var = !var; break; \
166     case 'y': case 'Y': case 't': case 'T': \
167     case '+': case '1': var = 1; break; \
168     case 'n': case 'N': case 'f': case 'F': \
169     case '-': case '0': var = 0; break; \
170     default: goto badopt; }
171     int nprocs = 1;
172     char *curout = NULL;
173     char *binval = NULL;
174     int bincnt = 0;
175     int rval;
176     int i;
177     /* global program name */
178     progname = argv[0] = fixargv0(argv[0]);
179     gargv = argv;
180     gargc = argc;
181     /* initialize calcomp routines early */
182     initfunc();
183     setcontext(RCCONTEXT);
184     /* option city */
185     for (i = 1; i < argc; i++) {
186     /* expand arguments */
187     while ((rval = expandarg(&argc, &argv, i)) > 0)
188     ;
189     if (rval < 0) {
190     sprintf(errmsg, "cannot expand '%s'", argv[i]);
191     error(SYSTEM, errmsg);
192     }
193     if (argv[i] == NULL || argv[i][0] != '-')
194     break; /* break from options */
195     if (!strcmp(argv[i], "-version")) {
196     puts(VersionID);
197     quit(0);
198     }
199     if (!strcmp(argv[i], "-defaults") ||
200     !strcmp(argv[i], "-help")) {
201     override_options();
202     printdefaults();
203     quit(0);
204     }
205     rval = getrenderopt(argc-i, argv+i);
206     if (rval >= 0) {
207     i += rval;
208     continue;
209     }
210     switch (argv[i][1]) {
211     case 'n': /* number of cores */
212     check(2,"i");
213     nproc = atoi(argv[++i]);
214     if (nproc <= 0)
215     error(USER, "bad number of processes");
216     break;
217     case 'V': /* output contributions */
218     bool(2,contrib);
219     break;
220     case 'x': /* x resolution */
221     check(2,"i");
222     xres = atoi(argv[++i]);
223     break;
224     case 'y': /* y resolution */
225     check(2,"i");
226     yres = atoi(argv[++i]);
227     break;
228     case 'w': /* warnings */
229 greg 2.2 rval = (erract[WARNING].pf != NULL);
230 greg 2.1 bool(2,rval);
231     if (rval) erract[WARNING].pf = wputs;
232     else erract[WARNING].pf = NULL;
233     break;
234     case 'e': /* expression */
235     check(2,"s");
236     scompile(argv[++i], NULL, 0);
237     break;
238     case 'l': /* limit distance */
239     if (argv[i][2] != 'd')
240     goto badopt;
241     bool(3,lim_dist);
242     break;
243     case 'I': /* immed. irradiance */
244     bool(2,imm_irrad);
245     break;
246     case 'f': /* file or force or format */
247     if (!argv[i][2]) {
248     check(2,"s");
249     loadfunc(argv[++i]);
250     break;
251     }
252     if (argv[i][2] == 'o') {
253     bool(3,force_open);
254     break;
255     }
256     setformat(argv[i]+2);
257     break;
258     case 'o': /* output */
259     check(2,"s");
260     curout = argv[++i];
261     break;
262     case 'c': /* input rays per output */
263     check(2,"i");
264     accumulate = atoi(argv[++i]);
265     break;
266     case 'r': /* recover output */
267     bool(2,recover);
268     break;
269     case 'h': /* header output */
270     bool(2,header);
271     break;
272     case 'b': /* bin expression/count */
273     if (argv[i][2] == 'n') {
274     check(3,"s");
275     bincnt = (int)(eval(argv[++i]) + .5);
276     break;
277     }
278     check(2,"s");
279     binval = argv[++i];
280     break;
281     case 'm': /* modifier name */
282     check(2,"s");
283     addmodifier(argv[++i], curout, binval, bincnt);
284     break;
285     case 'M': /* modifier file */
286     check(2,"s");
287     addmodfile(argv[++i], curout, binval, bincnt);
288     break;
289     default:
290     goto badopt;
291     }
292     }
293     /* override some option settings */
294     override_options();
295     /* initialize object types */
296     initotypes();
297     /* initialize urand */
298     if (rand_samp) {
299     srandom((long)time(0));
300     initurand(0);
301     } else {
302     srandom(0L);
303     initurand(2048);
304     }
305     /* set up signal handling */
306     sigdie(SIGINT, "Interrupt");
307     #ifdef SIGHUP
308     sigdie(SIGHUP, "Hangup");
309     #endif
310     sigdie(SIGTERM, "Terminate");
311     #ifdef SIGPIPE
312     sigdie(SIGPIPE, "Broken pipe");
313     #endif
314     #ifdef SIGALRM
315     sigdie(SIGALRM, "Alarm clock");
316     #endif
317     #ifdef SIGXCPU
318     sigdie(SIGXCPU, "CPU limit exceeded");
319     sigdie(SIGXFSZ, "File size exceeded");
320     #endif
321     #ifdef NICE
322     nice(NICE); /* lower priority */
323     #endif
324     /* get octree */
325     if (i == argc)
326     octname = NULL;
327     else if (i == argc-1)
328     octname = argv[i];
329     else
330     goto badopt;
331     if (octname == NULL)
332     error(USER, "missing octree argument");
333    
334     readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL);
335     nsceneobjs = nobjects;
336    
337     marksources(); /* find and mark sources */
338    
339     setambient(); /* initialize ambient calculation */
340    
341 greg 2.2 rcontrib(); /* trace ray contributions (loop) */
342 greg 2.1
343     ambsync(); /* flush ambient file */
344    
345     quit(0); /* exit clean */
346    
347     badopt:
348     fprintf(stderr,
349     "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv][-bn N] {-m mod | -M file} [rtrace options] octree\n",
350     progname);
351     sprintf(errmsg, "command line error at '%s'", argv[i]);
352     error(USER, errmsg);
353     return(1); /* pro forma return */
354    
355     #undef check
356     #undef bool
357     }
358    
359    
360     void
361     wputs( /* warning output function */
362     char *s
363     )
364     {
365     int lasterrno = errno;
366     eputs(s);
367     errno = lasterrno;
368     }
369    
370    
371     void
372     eputs( /* put string to stderr */
373     char *s
374     )
375     {
376     static int midline = 0;
377    
378     if (!*s)
379     return;
380     if (!midline++) {
381     fputs(progname, stderr);
382     fputs(": ", stderr);
383     }
384     fputs(s, stderr);
385     if (s[strlen(s)-1] == '\n') {
386     fflush(stderr);
387     midline = 0;
388     }
389     }