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