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