ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.11
Committed: Sun Aug 11 13:48:48 2013 UTC (10 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +3 -1 lines
Log Message:
Added missing check for missing modifier

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id: rcmain.c,v 2.10 2013/08/07 05:10:09 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 greg 2.10 #include <time.h>
12 greg 2.1 #include "rcontrib.h"
13 greg 2.6 #include "random.h"
14 greg 2.1 #include "source.h"
15     #include "ambient.h"
16    
17     int gargc; /* global argc */
18     char **gargv; /* global argv */
19     char *octname; /* global octree name */
20     char *progname; /* global argv[0] */
21    
22     char *sigerr[NSIG]; /* signal error messages */
23    
24     int nproc = 1; /* number of processes requested */
25     int nchild = 0; /* number of children (-1 in child) */
26    
27     int inpfmt = 'a'; /* input format */
28     int outfmt = 'a'; /* output format */
29    
30     int header = 1; /* output header? */
31     int force_open = 0; /* truncate existing output? */
32     int recover = 0; /* recover previous output? */
33     int accumulate = 1; /* input rays per output record */
34     int contrib = 0; /* computing contributions? */
35    
36     int xres = 0; /* horizontal (scan) size */
37     int yres = 0; /* vertical resolution */
38    
39     int using_stdout = 0; /* are we using stdout? */
40    
41     int imm_irrad = 0; /* compute immediate irradiance? */
42     int lim_dist = 0; /* limit distance? */
43    
44     const char *modname[MAXMODLIST]; /* ordered modifier name list */
45     int nmods = 0; /* number of modifiers */
46    
47 greg 2.4 void (*addobjnotify[8])() = {ambnotify, NULL};
48    
49 greg 2.9 char RCCONTEXT[] = "RC"; /* our special evaluation context */
50 greg 2.1
51    
52     static void
53     printdefaults(void) /* print default values to stdout */
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     char *curout = NULL;
172     char *binval = NULL;
173     int bincnt = 0;
174     int rval;
175     int i;
176     /* global program name */
177     progname = argv[0] = fixargv0(argv[0]);
178     gargv = argv;
179     gargc = argc;
180     /* initialize calcomp routines early */
181     initfunc();
182     setcontext(RCCONTEXT);
183     /* option city */
184     for (i = 1; i < argc; i++) {
185     /* expand arguments */
186     while ((rval = expandarg(&argc, &argv, i)) > 0)
187     ;
188     if (rval < 0) {
189     sprintf(errmsg, "cannot expand '%s'", argv[i]);
190     error(SYSTEM, errmsg);
191     }
192     if (argv[i] == NULL || argv[i][0] != '-')
193     break; /* break from options */
194     if (!strcmp(argv[i], "-version")) {
195     puts(VersionID);
196     quit(0);
197     }
198     if (!strcmp(argv[i], "-defaults") ||
199     !strcmp(argv[i], "-help")) {
200     override_options();
201     printdefaults();
202     quit(0);
203     }
204     rval = getrenderopt(argc-i, argv+i);
205     if (rval >= 0) {
206     i += rval;
207     continue;
208     }
209     switch (argv[i][1]) {
210     case 'n': /* number of cores */
211     check(2,"i");
212     nproc = atoi(argv[++i]);
213     if (nproc <= 0)
214     error(USER, "bad number of processes");
215     break;
216     case 'V': /* output contributions */
217     bool(2,contrib);
218     break;
219     case 'x': /* x resolution */
220     check(2,"i");
221     xres = atoi(argv[++i]);
222     break;
223     case 'y': /* y resolution */
224     check(2,"i");
225     yres = atoi(argv[++i]);
226     break;
227     case 'w': /* warnings */
228 greg 2.2 rval = (erract[WARNING].pf != NULL);
229 greg 2.1 bool(2,rval);
230     if (rval) erract[WARNING].pf = wputs;
231     else erract[WARNING].pf = NULL;
232     break;
233     case 'e': /* expression */
234     check(2,"s");
235     scompile(argv[++i], NULL, 0);
236     break;
237     case 'l': /* limit distance */
238     if (argv[i][2] != 'd')
239     goto badopt;
240     bool(3,lim_dist);
241     break;
242     case 'I': /* immed. irradiance */
243     bool(2,imm_irrad);
244     break;
245     case 'f': /* file or force or format */
246     if (!argv[i][2]) {
247     check(2,"s");
248     loadfunc(argv[++i]);
249     break;
250     }
251     if (argv[i][2] == 'o') {
252     bool(3,force_open);
253     break;
254     }
255     setformat(argv[i]+2);
256     break;
257     case 'o': /* output */
258     check(2,"s");
259     curout = argv[++i];
260     break;
261     case 'c': /* input rays per output */
262     check(2,"i");
263     accumulate = atoi(argv[++i]);
264     break;
265     case 'r': /* recover output */
266     bool(2,recover);
267     break;
268     case 'h': /* header output */
269     bool(2,header);
270     break;
271     case 'b': /* bin expression/count */
272     if (argv[i][2] == 'n') {
273     check(3,"s");
274     bincnt = (int)(eval(argv[++i]) + .5);
275     break;
276     }
277     check(2,"s");
278     binval = argv[++i];
279     break;
280     case 'm': /* modifier name */
281     check(2,"s");
282     addmodifier(argv[++i], curout, binval, bincnt);
283     break;
284     case 'M': /* modifier file */
285     check(2,"s");
286     addmodfile(argv[++i], curout, binval, bincnt);
287     break;
288     default:
289     goto badopt;
290     }
291     }
292 greg 2.11 if (nmods <= 0)
293     error(USER, "missing required modifier argument");
294 greg 2.1 /* override some option settings */
295     override_options();
296     /* initialize object types */
297     initotypes();
298     /* initialize urand */
299     if (rand_samp) {
300     srandom((long)time(0));
301     initurand(0);
302     } else {
303     srandom(0L);
304     initurand(2048);
305     }
306     /* set up signal handling */
307     sigdie(SIGINT, "Interrupt");
308     #ifdef SIGHUP
309     sigdie(SIGHUP, "Hangup");
310     #endif
311     sigdie(SIGTERM, "Terminate");
312     #ifdef SIGPIPE
313     sigdie(SIGPIPE, "Broken pipe");
314     #endif
315     #ifdef SIGALRM
316     sigdie(SIGALRM, "Alarm clock");
317     #endif
318     #ifdef SIGXCPU
319     sigdie(SIGXCPU, "CPU limit exceeded");
320     sigdie(SIGXFSZ, "File size exceeded");
321     #endif
322     #ifdef NICE
323     nice(NICE); /* lower priority */
324     #endif
325     /* get octree */
326     if (i == argc)
327     octname = NULL;
328     else if (i == argc-1)
329     octname = argv[i];
330     else
331     goto badopt;
332     if (octname == NULL)
333     error(USER, "missing octree argument");
334    
335     readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL);
336     nsceneobjs = nobjects;
337    
338     marksources(); /* find and mark sources */
339    
340     setambient(); /* initialize ambient calculation */
341    
342 greg 2.2 rcontrib(); /* trace ray contributions (loop) */
343 greg 2.1
344     ambsync(); /* flush ambient file */
345    
346     quit(0); /* exit clean */
347    
348     badopt:
349     fprintf(stderr,
350     "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv][-bn N] {-m mod | -M file} [rtrace options] octree\n",
351     progname);
352     sprintf(errmsg, "command line error at '%s'", argv[i]);
353     error(USER, errmsg);
354     return(1); /* pro forma return */
355    
356     #undef check
357     #undef bool
358     }
359    
360    
361     void
362     wputs( /* warning output function */
363     char *s
364     )
365     {
366     int lasterrno = errno;
367     eputs(s);
368     errno = lasterrno;
369     }
370    
371    
372     void
373     eputs( /* put string to stderr */
374     char *s
375     )
376     {
377     static int midline = 0;
378    
379     if (!*s)
380     return;
381     if (!midline++) {
382     fputs(progname, stderr);
383     fputs(": ", stderr);
384     }
385     fputs(s, stderr);
386     if (s[strlen(s)-1] == '\n') {
387     fflush(stderr);
388     midline = 0;
389     }
390     }