ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.2
Committed: Sun Jun 10 05:25:42 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +3 -10 lines
Log Message:
Tweaks and bug fixes to new code

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rcmain.c,v 2.1 2012/06/09 07:16:47 greg Exp $";
3 #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 if (imm_irrad)
58 printf("-I+\t\t\t\t# immediate irradiance on\n");
59 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
60 printf("-x %-9d\t\t\t# %s\n", xres,
61 yres && xres ? "x resolution" : "flush interval");
62 printf("-y %-9d\t\t\t# y resolution\n", yres);
63 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
64 "-ld-\t\t\t\t# limit distance off\n");
65 printf("-h%c\t\t\t\t# %s header\n", header ? '+' : '-',
66 header ? "output" : "no");
67 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
68 inpfmt, outfmt, formstr(inpfmt), formstr(outfmt));
69 printf(erract[WARNING].pf != NULL ?
70 "-w+\t\t\t\t# warning messages on\n" :
71 "-w-\t\t\t\t# warning messages off\n");
72 print_rdefaults();
73 }
74
75
76 static void
77 onsig( /* fatal signal */
78 int signo
79 )
80 {
81 static int gotsig = 0;
82
83 if (gotsig++) /* two signals and we're gone! */
84 _exit(signo);
85
86 #ifdef SIGALRM
87 alarm(15); /* allow 15 seconds to clean up */
88 signal(SIGALRM, SIG_DFL); /* make certain we do die */
89 #endif
90 eputs("signal - ");
91 eputs(sigerr[signo]);
92 eputs("\n");
93 quit(3);
94 }
95
96
97 static void
98 sigdie( /* set fatal signal */
99 int signo,
100 char *msg
101 )
102 {
103 if (signal(signo, onsig) == SIG_IGN)
104 signal(signo, SIG_IGN);
105 sigerr[signo] = msg;
106 }
107
108
109 /* set input/output format */
110 static void
111 setformat(const char *fmt)
112 {
113 switch (fmt[0]) {
114 case 'f':
115 case 'd':
116 SET_FILE_BINARY(stdin);
117 /* fall through */
118 case 'a':
119 inpfmt = fmt[0];
120 break;
121 default:
122 goto fmterr;
123 }
124 switch (fmt[1]) {
125 case '\0':
126 outfmt = inpfmt;
127 return;
128 case 'a':
129 case 'f':
130 case 'd':
131 case 'c':
132 outfmt = fmt[1];
133 break;
134 default:
135 goto fmterr;
136 }
137 if (!fmt[2])
138 return;
139 fmterr:
140 sprintf(errmsg, "Illegal i/o format: -f%s", fmt);
141 error(USER, errmsg);
142 }
143
144
145 /* Set overriding options */
146 static void
147 override_options(void)
148 {
149 shadthresh = 0;
150 ambssamp = 0;
151 ambacc = 0;
152 if (accumulate <= 0) /* no output flushing for single record */
153 xres = yres = 0;
154 }
155
156
157 int
158 main(int argc, char *argv[])
159 {
160 #define check(ol,al) if (argv[i][ol] || \
161 badarg(argc-i-1,argv+i+1,al)) \
162 goto badopt
163 #define bool(olen,var) switch (argv[i][olen]) { \
164 case '\0': var = !var; break; \
165 case 'y': case 'Y': case 't': case 'T': \
166 case '+': case '1': var = 1; break; \
167 case 'n': case 'N': case 'f': case 'F': \
168 case '-': case '0': var = 0; break; \
169 default: goto badopt; }
170 int nprocs = 1;
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 rval = (erract[WARNING].pf != NULL);
229 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 /* override some option settings */
293 override_options();
294 /* initialize object types */
295 initotypes();
296 /* initialize urand */
297 if (rand_samp) {
298 srandom((long)time(0));
299 initurand(0);
300 } else {
301 srandom(0L);
302 initurand(2048);
303 }
304 /* set up signal handling */
305 sigdie(SIGINT, "Interrupt");
306 #ifdef SIGHUP
307 sigdie(SIGHUP, "Hangup");
308 #endif
309 sigdie(SIGTERM, "Terminate");
310 #ifdef SIGPIPE
311 sigdie(SIGPIPE, "Broken pipe");
312 #endif
313 #ifdef SIGALRM
314 sigdie(SIGALRM, "Alarm clock");
315 #endif
316 #ifdef SIGXCPU
317 sigdie(SIGXCPU, "CPU limit exceeded");
318 sigdie(SIGXFSZ, "File size exceeded");
319 #endif
320 #ifdef NICE
321 nice(NICE); /* lower priority */
322 #endif
323 /* get octree */
324 if (i == argc)
325 octname = NULL;
326 else if (i == argc-1)
327 octname = argv[i];
328 else
329 goto badopt;
330 if (octname == NULL)
331 error(USER, "missing octree argument");
332
333 readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL);
334 nsceneobjs = nobjects;
335
336 marksources(); /* find and mark sources */
337
338 setambient(); /* initialize ambient calculation */
339
340 rcontrib(); /* trace ray contributions (loop) */
341
342 ambsync(); /* flush ambient file */
343
344 quit(0); /* exit clean */
345
346 badopt:
347 fprintf(stderr,
348 "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv][-bn N] {-m mod | -M file} [rtrace options] octree\n",
349 progname);
350 sprintf(errmsg, "command line error at '%s'", argv[i]);
351 error(USER, errmsg);
352 return(1); /* pro forma return */
353
354 #undef check
355 #undef bool
356 }
357
358
359 void
360 wputs( /* warning output function */
361 char *s
362 )
363 {
364 int lasterrno = errno;
365 eputs(s);
366 errno = lasterrno;
367 }
368
369
370 void
371 eputs( /* put string to stderr */
372 char *s
373 )
374 {
375 static int midline = 0;
376
377 if (!*s)
378 return;
379 if (!midline++) {
380 fputs(progname, stderr);
381 fputs(": ", stderr);
382 }
383 fputs(s, stderr);
384 if (s[strlen(s)-1] == '\n') {
385 fflush(stderr);
386 midline = 0;
387 }
388 }