1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.37 |
static const char RCSid[] = "$Id: rcmain.c,v 2.36 2024/04/05 16:41:17 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 |
greg |
2.22 |
#if defined(_WIN32) || defined(_WIN64) |
57 |
greg |
2.23 |
#define RCONTRIB_FEATURES "Accumulation\nSummation\nRecovery\n" \ |
58 |
greg |
2.33 |
"ImmediateIrradiance\n" \ |
59 |
greg |
2.24 |
"ProgressReporting\nDistanceLimiting\n" \ |
60 |
greg |
2.31 |
"InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \ |
61 |
greg |
2.36 |
"Outputs=V,W\n" \ |
62 |
|
|
"OutputCS=RGB,spec\n" |
63 |
greg |
2.22 |
#else |
64 |
|
|
#define RCONTRIB_FEATURES "Multiprocessing\n" \ |
65 |
greg |
2.23 |
"Accumulation\nSummation\nRecovery\n" \ |
66 |
greg |
2.33 |
"ImmediateIrradiance\n" \ |
67 |
greg |
2.24 |
"ProgressReporting\nDistanceLimiting\n" \ |
68 |
greg |
2.31 |
"InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \ |
69 |
greg |
2.36 |
"Outputs=V,W\n" \ |
70 |
|
|
"OutputCS=RGB,spec\n" |
71 |
greg |
2.22 |
#endif |
72 |
greg |
2.1 |
|
73 |
|
|
static void |
74 |
|
|
printdefaults(void) /* print default values to stdout */ |
75 |
|
|
{ |
76 |
greg |
2.3 |
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 |
greg |
2.1 |
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 |
greg |
2.32 |
printf(header ? "-h+\t\t\t\t# output header\n" : |
88 |
|
|
"-h-\t\t\t\t# no header\n"); |
89 |
greg |
2.1 |
printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n", |
90 |
|
|
inpfmt, outfmt, formstr(inpfmt), formstr(outfmt)); |
91 |
greg |
2.37 |
if (report_intvl > 0) |
92 |
|
|
printf("-t %-9d\t\t\t# time between reports\n", report_intvl); |
93 |
greg |
2.1 |
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 |
schorsch |
2.15 |
#define check_bool(olen,var) switch (argv[i][olen]) { \ |
188 |
greg |
2.1 |
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 |
greg |
2.12 |
char *prms = NULL; |
196 |
greg |
2.1 |
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 |
greg |
2.22 |
/* feature check only? */ |
205 |
|
|
strcat(RFeatureList, RCONTRIB_FEATURES); |
206 |
greg |
2.26 |
if (argc > 1 && !strcmp(argv[1], "-features")) |
207 |
greg |
2.22 |
return feature_status(argc-2, argv+2); |
208 |
greg |
2.28 |
#if defined(_WIN32) || defined(_WIN64) /* increase file limit to maximum */ |
209 |
|
|
for (i = 8192; i > _IOB_ENTRIES; i >>= 1) |
210 |
greg |
2.30 |
if (_setmaxstdio(i) == i) |
211 |
greg |
2.28 |
break; |
212 |
greg |
2.17 |
#endif |
213 |
greg |
2.1 |
/* initialize calcomp routines early */ |
214 |
|
|
initfunc(); |
215 |
greg |
2.21 |
calcontext(RCCONTEXT); |
216 |
greg |
2.1 |
/* option city */ |
217 |
|
|
for (i = 1; i < argc; i++) { |
218 |
|
|
/* expand arguments */ |
219 |
|
|
while ((rval = expandarg(&argc, &argv, i)) > 0) |
220 |
|
|
; |
221 |
|
|
if (rval < 0) { |
222 |
|
|
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
223 |
|
|
error(SYSTEM, errmsg); |
224 |
|
|
} |
225 |
|
|
if (argv[i] == NULL || argv[i][0] != '-') |
226 |
|
|
break; /* break from options */ |
227 |
|
|
if (!strcmp(argv[i], "-version")) { |
228 |
|
|
puts(VersionID); |
229 |
|
|
quit(0); |
230 |
|
|
} |
231 |
|
|
if (!strcmp(argv[i], "-defaults") || |
232 |
|
|
!strcmp(argv[i], "-help")) { |
233 |
|
|
override_options(); |
234 |
|
|
printdefaults(); |
235 |
|
|
quit(0); |
236 |
|
|
} |
237 |
|
|
rval = getrenderopt(argc-i, argv+i); |
238 |
|
|
if (rval >= 0) { |
239 |
|
|
i += rval; |
240 |
|
|
continue; |
241 |
|
|
} |
242 |
|
|
switch (argv[i][1]) { |
243 |
|
|
case 'n': /* number of cores */ |
244 |
|
|
check(2,"i"); |
245 |
|
|
nproc = atoi(argv[++i]); |
246 |
|
|
if (nproc <= 0) |
247 |
|
|
error(USER, "bad number of processes"); |
248 |
|
|
break; |
249 |
|
|
case 'V': /* output contributions */ |
250 |
schorsch |
2.15 |
check_bool(2,contrib); |
251 |
greg |
2.1 |
break; |
252 |
|
|
case 'x': /* x resolution */ |
253 |
|
|
check(2,"i"); |
254 |
|
|
xres = atoi(argv[++i]); |
255 |
|
|
break; |
256 |
|
|
case 'y': /* y resolution */ |
257 |
|
|
check(2,"i"); |
258 |
|
|
yres = atoi(argv[++i]); |
259 |
|
|
break; |
260 |
|
|
case 'w': /* warnings */ |
261 |
greg |
2.2 |
rval = (erract[WARNING].pf != NULL); |
262 |
schorsch |
2.15 |
check_bool(2,rval); |
263 |
greg |
2.1 |
if (rval) erract[WARNING].pf = wputs; |
264 |
|
|
else erract[WARNING].pf = NULL; |
265 |
|
|
break; |
266 |
|
|
case 'e': /* expression */ |
267 |
|
|
check(2,"s"); |
268 |
|
|
scompile(argv[++i], NULL, 0); |
269 |
|
|
break; |
270 |
|
|
case 'l': /* limit distance */ |
271 |
|
|
if (argv[i][2] != 'd') |
272 |
|
|
goto badopt; |
273 |
schorsch |
2.15 |
check_bool(3,lim_dist); |
274 |
greg |
2.1 |
break; |
275 |
|
|
case 'I': /* immed. irradiance */ |
276 |
schorsch |
2.15 |
check_bool(2,imm_irrad); |
277 |
greg |
2.1 |
break; |
278 |
|
|
case 'f': /* file or force or format */ |
279 |
|
|
if (!argv[i][2]) { |
280 |
|
|
check(2,"s"); |
281 |
|
|
loadfunc(argv[++i]); |
282 |
|
|
break; |
283 |
|
|
} |
284 |
|
|
if (argv[i][2] == 'o') { |
285 |
schorsch |
2.15 |
check_bool(3,force_open); |
286 |
greg |
2.1 |
break; |
287 |
|
|
} |
288 |
|
|
setformat(argv[i]+2); |
289 |
|
|
break; |
290 |
|
|
case 'o': /* output */ |
291 |
|
|
check(2,"s"); |
292 |
|
|
curout = argv[++i]; |
293 |
|
|
break; |
294 |
|
|
case 'r': /* recover output */ |
295 |
schorsch |
2.15 |
check_bool(2,recover); |
296 |
greg |
2.1 |
break; |
297 |
|
|
case 'h': /* header output */ |
298 |
schorsch |
2.15 |
check_bool(2,header); |
299 |
greg |
2.1 |
break; |
300 |
greg |
2.12 |
case 'p': /* parameter setting(s) */ |
301 |
|
|
check(2,"s"); |
302 |
greg |
2.13 |
set_eparams(prms = argv[++i]); |
303 |
greg |
2.12 |
break; |
304 |
greg |
2.33 |
case 'c': /* sample count */ |
305 |
|
|
check(2,"i"); |
306 |
|
|
accumulate = atoi(argv[++i]); |
307 |
greg |
2.32 |
break; |
308 |
greg |
2.1 |
case 'b': /* bin expression/count */ |
309 |
|
|
if (argv[i][2] == 'n') { |
310 |
|
|
check(3,"s"); |
311 |
|
|
bincnt = (int)(eval(argv[++i]) + .5); |
312 |
|
|
break; |
313 |
|
|
} |
314 |
|
|
check(2,"s"); |
315 |
|
|
binval = argv[++i]; |
316 |
|
|
break; |
317 |
|
|
case 'm': /* modifier name */ |
318 |
|
|
check(2,"s"); |
319 |
greg |
2.12 |
addmodifier(argv[++i], curout, prms, binval, bincnt); |
320 |
greg |
2.1 |
break; |
321 |
|
|
case 'M': /* modifier file */ |
322 |
|
|
check(2,"s"); |
323 |
greg |
2.12 |
addmodfile(argv[++i], curout, prms, binval, bincnt); |
324 |
greg |
2.1 |
break; |
325 |
greg |
2.19 |
case 't': /* reporting interval */ |
326 |
|
|
check(2,"i"); |
327 |
|
|
report_intvl = atoi(argv[++i]); |
328 |
|
|
break; |
329 |
greg |
2.1 |
default: |
330 |
|
|
goto badopt; |
331 |
|
|
} |
332 |
|
|
} |
333 |
greg |
2.11 |
if (nmods <= 0) |
334 |
|
|
error(USER, "missing required modifier argument"); |
335 |
greg |
2.1 |
/* override some option settings */ |
336 |
|
|
override_options(); |
337 |
greg |
2.34 |
/* set/check spectral sampling */ |
338 |
greg |
2.35 |
if (setspectrsamp(CNDX, WLPART) < 0) |
339 |
greg |
2.34 |
error(USER, "unsupported spectral sampling"); |
340 |
greg |
2.1 |
/* initialize object types */ |
341 |
|
|
initotypes(); |
342 |
|
|
/* initialize urand */ |
343 |
|
|
if (rand_samp) { |
344 |
|
|
srandom((long)time(0)); |
345 |
|
|
initurand(0); |
346 |
|
|
} else { |
347 |
|
|
srandom(0L); |
348 |
|
|
initurand(2048); |
349 |
|
|
} |
350 |
|
|
/* set up signal handling */ |
351 |
|
|
sigdie(SIGINT, "Interrupt"); |
352 |
|
|
#ifdef SIGHUP |
353 |
|
|
sigdie(SIGHUP, "Hangup"); |
354 |
|
|
#endif |
355 |
|
|
sigdie(SIGTERM, "Terminate"); |
356 |
|
|
#ifdef SIGPIPE |
357 |
|
|
sigdie(SIGPIPE, "Broken pipe"); |
358 |
|
|
#endif |
359 |
|
|
#ifdef SIGALRM |
360 |
|
|
sigdie(SIGALRM, "Alarm clock"); |
361 |
|
|
#endif |
362 |
|
|
#ifdef SIGXCPU |
363 |
|
|
sigdie(SIGXCPU, "CPU limit exceeded"); |
364 |
|
|
sigdie(SIGXFSZ, "File size exceeded"); |
365 |
|
|
#endif |
366 |
|
|
#ifdef NICE |
367 |
|
|
nice(NICE); /* lower priority */ |
368 |
|
|
#endif |
369 |
|
|
/* get octree */ |
370 |
|
|
if (i == argc) |
371 |
|
|
octname = NULL; |
372 |
|
|
else if (i == argc-1) |
373 |
|
|
octname = argv[i]; |
374 |
|
|
else |
375 |
|
|
goto badopt; |
376 |
|
|
if (octname == NULL) |
377 |
|
|
error(USER, "missing octree argument"); |
378 |
|
|
|
379 |
|
|
readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL); |
380 |
|
|
nsceneobjs = nobjects; |
381 |
|
|
|
382 |
greg |
2.14 |
/* PMAP: set up & load photon maps */ |
383 |
|
|
ray_init_pmap(); |
384 |
|
|
|
385 |
greg |
2.1 |
marksources(); /* find and mark sources */ |
386 |
greg |
2.14 |
|
387 |
|
|
/* PMAP: init photon map for light source contributions */ |
388 |
|
|
initPmapContrib(&modconttab, nmods); |
389 |
greg |
2.1 |
|
390 |
|
|
setambient(); /* initialize ambient calculation */ |
391 |
greg |
2.14 |
|
392 |
greg |
2.2 |
rcontrib(); /* trace ray contributions (loop) */ |
393 |
greg |
2.1 |
|
394 |
greg |
2.14 |
/* PMAP: free photon maps */ |
395 |
|
|
ray_done_pmap(); |
396 |
|
|
|
397 |
greg |
2.1 |
quit(0); /* exit clean */ |
398 |
|
|
|
399 |
|
|
badopt: |
400 |
|
|
fprintf(stderr, |
401 |
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", |
402 |
greg |
2.1 |
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 |
schorsch |
2.15 |
#undef check_bool |
409 |
greg |
2.1 |
} |
410 |
|
|
|
411 |
|
|
|
412 |
|
|
void |
413 |
|
|
wputs( /* warning output function */ |
414 |
greg |
2.27 |
const char *s |
415 |
greg |
2.1 |
) |
416 |
|
|
{ |
417 |
|
|
int lasterrno = errno; |
418 |
|
|
eputs(s); |
419 |
|
|
errno = lasterrno; |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
|
423 |
|
|
void |
424 |
|
|
eputs( /* put string to stderr */ |
425 |
greg |
2.27 |
const char *s |
426 |
greg |
2.1 |
) |
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 |
|
|
} |