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