ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.18
Committed: Thu Jan 18 19:43:43 2018 UTC (6 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad5R3
Changes since 2.17: +2 -2 lines
Log Message:
Changed context names to avoid collision with user-specified .cal file names

File Contents

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