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