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