ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.3
Committed: Mon Jun 11 05:07:55 2012 UTC (12 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +4 -1 lines
Log Message:
Fixed a number of bugs -- still testing

File Contents

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