1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rxtmain.cpp,v 2.13 2025/04/22 17:12:25 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* rxtmain.cpp - main for per-ray calculation program |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include <signal.h> |
11 |
|
12 |
#include "rtprocess.h" /* getpid() */ |
13 |
#include "platform.h" |
14 |
#include "RtraceSimulManager.h" |
15 |
#include "func.h" |
16 |
|
17 |
static const char *sigerr[NSIG]; /* signal error messages */ |
18 |
char *errfile = NULL; /* error output file */ |
19 |
|
20 |
extern const char *formstr(int f); /* string from format */ |
21 |
extern int setrtoutput(const char *outvals); /* set output values */ |
22 |
|
23 |
int inform = 'a'; /* input format */ |
24 |
int outform = 'a'; /* output format */ |
25 |
|
26 |
int hresolu = 0; /* horizontal (scan) size */ |
27 |
int vresolu = 0; /* vertical resolution */ |
28 |
|
29 |
RtraceSimulManager myRTmanager; // global simulation manager |
30 |
|
31 |
static const char *outvals = "v"; /* output specification */ |
32 |
static int nproc = 1; /* number of requested threads */ |
33 |
static int doheader = 1; /* include information header? */ |
34 |
|
35 |
#ifndef MAXMODLIST |
36 |
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
37 |
#endif |
38 |
|
39 |
extern void (*addobjnotify[])(OBJECT); /* object notification calls */ |
40 |
extern void tranotify(OBJECT obj); |
41 |
|
42 |
char *tralist[MAXMODLIST]; /* list of modifers to trace (or no) */ |
43 |
int traincl = -1; /* include == 1, exclude == 0 */ |
44 |
|
45 |
double (*sens_curve)(const SCOLOR scol) = NULL; /* spectral conversion for 1-channel */ |
46 |
double out_scalefactor = 1; /* output calibration scale factor */ |
47 |
RGBPRIMP out_prims = stdprims; /* output color primitives (NULL if spectral) */ |
48 |
static RGBPRIMS our_prims; /* private output color primitives */ |
49 |
|
50 |
static void onsig(int signo); |
51 |
static void sigdie(int signo, const char *msg); |
52 |
static void printdefaults(void); |
53 |
|
54 |
#define RXTRACE_FEATURES "IrradianceCalc\nMultiprocessing\nDistanceLimiting\n" \ |
55 |
"HessianAmbientCache\nAmbientAveraging\n" \ |
56 |
"AmbientValueSharing\nAdaptiveShadowTesting\n" \ |
57 |
"Outputs=o,d,v,V,w,W,l,L,c,p,n,N,s,m,M,r,x,R,X,~\n" \ |
58 |
"OutputCS=RGB,XYZ,Y,S,M,prims,spec\n" |
59 |
|
60 |
int |
61 |
main(int argc, char *argv[]) |
62 |
{ |
63 |
#define check(ol,al) if (argv[i][ol] || \ |
64 |
badarg(argc-i-1,argv+i+1,al)) \ |
65 |
goto badopt |
66 |
#define check_bool(olen,var) switch (argv[i][olen]) { \ |
67 |
case '\0': var = !var; break; \ |
68 |
case 'y': case 'Y': case 't': case 'T': \ |
69 |
case '+': case '1': var = 1; break; \ |
70 |
case 'n': case 'N': case 'f': case 'F': \ |
71 |
case '-': case '0': var = 0; break; \ |
72 |
default: goto badopt; } |
73 |
char **tralp = NULL; |
74 |
int rval; |
75 |
int i; |
76 |
/* global program name */ |
77 |
progname = argv[0]; |
78 |
/* feature check only? */ |
79 |
strcat(RFeatureList, RXTRACE_FEATURES); |
80 |
if (argc > 1 && !strcmp(argv[1], "-features")) |
81 |
return feature_status(argc-2, argv+2); |
82 |
/* initialize calcomp routines */ |
83 |
initfunc(); |
84 |
/* add trace notify function */ |
85 |
for (i = 0; addobjnotify[i] != NULL; i++) |
86 |
; |
87 |
addobjnotify[i] = tranotify; |
88 |
/* option city */ |
89 |
for (i = 1; i < argc; i++) { |
90 |
/* expand arguments */ |
91 |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
92 |
; |
93 |
if (rval < 0) { |
94 |
sprintf(errmsg, "cannot expand '%s'", argv[i]); |
95 |
error(SYSTEM, errmsg); |
96 |
} |
97 |
if (argv[i] == NULL || argv[i][0] != '-') |
98 |
break; /* break from options */ |
99 |
if (!strcmp(argv[i], "-version")) { |
100 |
puts(VersionID); |
101 |
quit(0); |
102 |
} |
103 |
if (!strcmp(argv[i], "-defaults") || |
104 |
!strcmp(argv[i], "-help")) { |
105 |
printdefaults(); |
106 |
quit(0); |
107 |
} |
108 |
rval = getrenderopt(argc-i, argv+i); |
109 |
if (rval >= 0) { |
110 |
i += rval; |
111 |
continue; |
112 |
} |
113 |
switch (argv[i][1]) { |
114 |
case 'n': /* number of cores */ |
115 |
check(2,"i"); |
116 |
nproc = atoi(argv[++i]); |
117 |
break; |
118 |
case 'x': /* x resolution */ |
119 |
check(2,"i"); |
120 |
hresolu = atoi(argv[++i]); |
121 |
break; |
122 |
case 'y': /* y resolution */ |
123 |
check(2,"i"); |
124 |
vresolu = atoi(argv[++i]); |
125 |
break; |
126 |
case 'w': /* warnings */ |
127 |
rval = erract[WARNING].pf != NULL; |
128 |
check_bool(2,rval); |
129 |
if (rval) erract[WARNING].pf = wputs; |
130 |
else erract[WARNING].pf = NULL; |
131 |
break; |
132 |
case 'e': /* error file */ |
133 |
check(2,"s"); |
134 |
errfile = argv[++i]; |
135 |
break; |
136 |
case 'l': /* limit distance */ |
137 |
if (argv[i][2] != 'd') |
138 |
goto badopt; |
139 |
rval = myRTmanager.rtFlags & RTlimDist; |
140 |
check_bool(3,rval); |
141 |
if (rval) myRTmanager.rtFlags |= RTlimDist; |
142 |
else myRTmanager.rtFlags &= ~RTlimDist; |
143 |
break; |
144 |
case 'I': /* immed. irradiance */ |
145 |
rval = myRTmanager.rtFlags & RTimmIrrad; |
146 |
check_bool(2,rval); |
147 |
if (rval) myRTmanager.rtFlags |= RTimmIrrad; |
148 |
else myRTmanager.rtFlags &= ~RTimmIrrad; |
149 |
break; |
150 |
case 'f': /* format i/o */ |
151 |
switch (argv[i][2]) { |
152 |
case 'a': /* ascii */ |
153 |
case 'f': /* float */ |
154 |
case 'd': /* double */ |
155 |
inform = argv[i][2]; |
156 |
break; |
157 |
default: |
158 |
goto badopt; |
159 |
} |
160 |
switch (argv[i][3]) { |
161 |
case '\0': |
162 |
outform = inform; |
163 |
break; |
164 |
case 'a': /* ascii */ |
165 |
case 'f': /* float */ |
166 |
case 'd': /* double */ |
167 |
case 'c': /* color */ |
168 |
check(4,""); |
169 |
outform = argv[i][3]; |
170 |
break; |
171 |
default: |
172 |
goto badopt; |
173 |
} |
174 |
break; |
175 |
case 'o': /* output */ |
176 |
outvals = argv[i]+2; |
177 |
break; |
178 |
case 'h': /* header output */ |
179 |
check_bool(2,doheader); |
180 |
break; |
181 |
case 't': /* trace */ |
182 |
switch (argv[i][2]) { |
183 |
case 'i': /* include */ |
184 |
case 'I': |
185 |
check(3,"s"); |
186 |
if (traincl != 1) { |
187 |
traincl = 1; |
188 |
tralp = tralist; |
189 |
} |
190 |
if (argv[i][2] == 'I') { /* file */ |
191 |
rval = wordfile(tralp, MAXMODLIST-(tralp-tralist), |
192 |
getpath(argv[++i],getrlibpath(),R_OK)); |
193 |
if (rval < 0) { |
194 |
sprintf(errmsg, |
195 |
"cannot open trace include file \"%s\"", |
196 |
argv[i]); |
197 |
error(SYSTEM, errmsg); |
198 |
} |
199 |
tralp += rval; |
200 |
} else { |
201 |
*tralp++ = argv[++i]; |
202 |
*tralp = NULL; |
203 |
} |
204 |
break; |
205 |
case 'e': /* exclude */ |
206 |
case 'E': |
207 |
check(3,"s"); |
208 |
if (traincl != 0) { |
209 |
traincl = 0; |
210 |
tralp = tralist; |
211 |
} |
212 |
if (argv[i][2] == 'E') { /* file */ |
213 |
rval = wordfile(tralp, MAXMODLIST-(tralp-tralist), |
214 |
getpath(argv[++i],getrlibpath(),R_OK)); |
215 |
if (rval < 0) { |
216 |
sprintf(errmsg, |
217 |
"cannot open trace exclude file \"%s\"", |
218 |
argv[i]); |
219 |
error(SYSTEM, errmsg); |
220 |
} |
221 |
tralp += rval; |
222 |
} else { |
223 |
*tralp++ = argv[++i]; |
224 |
*tralp = NULL; |
225 |
} |
226 |
break; |
227 |
default: |
228 |
goto badopt; |
229 |
} |
230 |
break; |
231 |
case 'p': /* value output */ |
232 |
switch (argv[i][2]) { |
233 |
case 'R': /* standard RGB output */ |
234 |
if (strcmp(argv[i]+2, "RGB")) |
235 |
goto badopt; |
236 |
out_prims = stdprims; |
237 |
out_scalefactor = 1; |
238 |
sens_curve = NULL; |
239 |
break; |
240 |
case 'X': /* XYZ output */ |
241 |
if (strcmp(argv[i]+2, "XYZ")) |
242 |
goto badopt; |
243 |
out_prims = xyzprims; |
244 |
out_scalefactor = WHTEFFICACY; |
245 |
sens_curve = NULL; |
246 |
break; |
247 |
case 'c': { |
248 |
int j; |
249 |
check(3,"ffffffff"); |
250 |
rval = 0; |
251 |
for (j = 0; j < 8; j++) { |
252 |
our_prims[0][j] = atof(argv[++i]); |
253 |
rval |= fabs(our_prims[0][j]-stdprims[0][j]) > .001; |
254 |
} |
255 |
if (rval) { |
256 |
if (!colorprimsOK(our_prims)) |
257 |
error(USER, "illegal primary chromaticities"); |
258 |
out_prims = our_prims; |
259 |
} else |
260 |
out_prims = stdprims; |
261 |
out_scalefactor = 1; |
262 |
sens_curve = NULL; |
263 |
} break; |
264 |
case 'Y': /* photopic response */ |
265 |
if (argv[i][3]) |
266 |
goto badopt; |
267 |
sens_curve = scolor_photopic; |
268 |
out_scalefactor = WHTEFFICACY; |
269 |
break; |
270 |
case 'S': /* scotopic response */ |
271 |
if (argv[i][3]) |
272 |
goto badopt; |
273 |
sens_curve = scolor_scotopic; |
274 |
out_scalefactor = WHTSCOTOPIC; |
275 |
break; |
276 |
case 'M': /* melanopic response */ |
277 |
if (argv[i][3]) |
278 |
goto badopt; |
279 |
sens_curve = scolor_melanopic; |
280 |
out_scalefactor = WHTMELANOPIC; |
281 |
break; |
282 |
default: |
283 |
goto badopt; |
284 |
} |
285 |
break; |
286 |
#if MAXCSAMP>3 |
287 |
case 'c': /* output spectral results */ |
288 |
if (argv[i][2] != 'o') |
289 |
goto badopt; |
290 |
rval = (out_prims == NULL) & (sens_curve == NULL); |
291 |
check_bool(3,rval); |
292 |
if (rval) { |
293 |
out_prims = NULL; |
294 |
sens_curve = NULL; |
295 |
} else if (out_prims == NULL) |
296 |
out_prims = stdprims; |
297 |
break; |
298 |
#endif |
299 |
default: |
300 |
goto badopt; |
301 |
} |
302 |
} |
303 |
/* set/check spectral sampling */ |
304 |
rval = setspectrsamp(CNDX, WLPART); |
305 |
if (rval < 0) |
306 |
error(USER, "unsupported spectral sampling"); |
307 |
if (sens_curve != NULL) |
308 |
out_prims = NULL; |
309 |
else if (out_prims != NULL) { |
310 |
if (!rval) |
311 |
error(WARNING, "spectral range incompatible with color output"); |
312 |
} else if (NCSAMP == 3) |
313 |
out_prims = stdprims; /* 3 samples do not a spectrum make */ |
314 |
/* set up signal handling */ |
315 |
sigdie(SIGINT, "Interrupt"); |
316 |
#ifdef SIGHUP |
317 |
sigdie(SIGHUP, "Hangup"); |
318 |
#endif |
319 |
sigdie(SIGTERM, "Terminate"); |
320 |
#ifdef SIGPIPE |
321 |
sigdie(SIGPIPE, "Broken pipe"); |
322 |
#endif |
323 |
#ifdef SIGALRM |
324 |
sigdie(SIGALRM, "Alarm clock"); |
325 |
#endif |
326 |
#ifdef SIGXCPU |
327 |
sigdie(SIGXCPU, "CPU limit exceeded"); |
328 |
sigdie(SIGXFSZ, "File size exceeded"); |
329 |
#endif |
330 |
/* open error file */ |
331 |
if (errfile != NULL) { |
332 |
if (freopen(errfile, "a", stderr) == NULL) |
333 |
quit(2); |
334 |
fprintf(stderr, "**************\n*** PID %5d: ", |
335 |
getpid()); |
336 |
printargs(argc, argv, stderr); |
337 |
putc('\n', stderr); |
338 |
fflush(stderr); |
339 |
} |
340 |
#ifdef NICE |
341 |
nice(NICE); /* lower priority */ |
342 |
#endif |
343 |
/* get octree name */ |
344 |
if (i == argc) |
345 |
error(USER, "missing octree argument"); |
346 |
if (i != argc-1) |
347 |
goto badopt; |
348 |
/* set output options */ |
349 |
rval = setrtoutput(outvals); |
350 |
/* load octree */ |
351 |
if (!myRTmanager.LoadOctree(argv[i])) |
352 |
quit(1); |
353 |
/* set up output */ |
354 |
if (outform != 'a') |
355 |
SET_FILE_BINARY(stdout); |
356 |
if (doheader) { /* print header? */ |
357 |
newheader("RADIANCE", stdout); |
358 |
fputs(myRTmanager.GetHeadStr(), stdout); |
359 |
printargs(i, argv, stdout); |
360 |
printf("SOFTWARE= %s\n", VersionID); |
361 |
fputnow(stdout); |
362 |
if (rval > 0) /* saved from setrtoutput() call */ |
363 |
fputncomp(rval, stdout); |
364 |
if (NCSAMP > 3) |
365 |
fputwlsplit(WLPART, stdout); |
366 |
if ((out_prims != stdprims) & (out_prims != NULL)) |
367 |
fputprims(out_prims, stdout); |
368 |
if ((outform == 'f') | (outform == 'd')) |
369 |
fputendian(stdout); |
370 |
fputformat(formstr(outform), stdout); |
371 |
fputc('\n', stdout); /* end of header */ |
372 |
} |
373 |
rtrace(NULL, nproc); /* trace rays */ |
374 |
quit(0); /* clean up & exit */ |
375 |
|
376 |
badopt: |
377 |
sprintf(errmsg, "command line error at '%s'", argv[i]); |
378 |
error(USER, errmsg); |
379 |
return 1; /* pro forma return */ |
380 |
|
381 |
#undef check |
382 |
#undef check_bool |
383 |
} |
384 |
|
385 |
void |
386 |
wputs( /* warning output function */ |
387 |
char *s |
388 |
) |
389 |
{ |
390 |
int lasterrno = errno; |
391 |
eputs(s); |
392 |
errno = lasterrno; |
393 |
} |
394 |
|
395 |
void |
396 |
eputs( /* put string to stderr */ |
397 |
const char *s |
398 |
) |
399 |
{ |
400 |
static int midline = 0; |
401 |
|
402 |
if (!*s) |
403 |
return; |
404 |
if (!midline++) { |
405 |
fputs(progname, stderr); |
406 |
fputs(": ", stderr); |
407 |
} |
408 |
fputs(s, stderr); |
409 |
if (s[strlen(s)-1] == '\n') { |
410 |
fflush(stderr); |
411 |
midline = 0; |
412 |
} |
413 |
} |
414 |
|
415 |
static void |
416 |
onsig( /* fatal signal */ |
417 |
int signo |
418 |
) |
419 |
{ |
420 |
static int gotsig = 0; |
421 |
|
422 |
if (gotsig++) /* two signals and we're gone! */ |
423 |
_exit(signo); |
424 |
|
425 |
#ifdef SIGALRM |
426 |
alarm(15); /* allow 15 seconds to clean up */ |
427 |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
428 |
#endif |
429 |
eputs("signal - "); |
430 |
eputs(sigerr[signo]); |
431 |
eputs("\n"); |
432 |
quit(3); |
433 |
} |
434 |
|
435 |
static void |
436 |
sigdie( /* set fatal signal */ |
437 |
int signo, |
438 |
const char *msg |
439 |
) |
440 |
{ |
441 |
if (signal(signo, onsig) == SIG_IGN) |
442 |
signal(signo, SIG_IGN); |
443 |
sigerr[signo] = msg; |
444 |
} |
445 |
|
446 |
static void |
447 |
printdefaults(void) /* print default values to stdout */ |
448 |
{ |
449 |
const char *cp; |
450 |
|
451 |
if (myRTmanager.rtFlags & RTimmIrrad) |
452 |
printf("-I+\t\t\t\t# immediate irradiance on\n"); |
453 |
printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc); |
454 |
printf("-x %-9d\t\t\t# %s\n", hresolu, |
455 |
vresolu && hresolu ? "x resolution" : "flush interval"); |
456 |
printf("-y %-9d\t\t\t# y resolution\n", vresolu); |
457 |
printf(myRTmanager.rtFlags&RTlimDist ? "-ld+\t\t\t\t# limit distance on\n" : |
458 |
"-ld-\t\t\t\t# limit distance off\n"); |
459 |
printf("-h%c\t\t\t\t# %s header\n", doheader ? '+' : '-', |
460 |
doheader ? "output" : "no"); |
461 |
printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n", |
462 |
inform, outform, formstr(inform), formstr(outform)); |
463 |
printf("-o%-9s\t\t\t# output", outvals); |
464 |
for (cp = outvals; *cp; cp++) |
465 |
switch (*cp) { |
466 |
case 't': case 'T': printf(" trace"); break; |
467 |
case 'o': printf(" origin"); break; |
468 |
case 'd': printf(" direction"); break; |
469 |
case 'r': printf(" reflect_contrib"); break; |
470 |
case 'R': printf(" reflect_length"); break; |
471 |
case 'x': printf(" unreflect_contrib"); break; |
472 |
case 'X': printf(" unreflect_length"); break; |
473 |
case 'v': printf(" value"); break; |
474 |
case 'V': printf(" contribution"); break; |
475 |
case 'l': printf(" length"); break; |
476 |
case 'L': printf(" first_length"); break; |
477 |
case 'p': printf(" point"); break; |
478 |
case 'n': printf(" normal"); break; |
479 |
case 'N': printf(" unperturbed_normal"); break; |
480 |
case 's': printf(" surface"); break; |
481 |
case 'w': printf(" weight"); break; |
482 |
case 'W': printf(" coefficient"); break; |
483 |
case 'm': printf(" modifier"); break; |
484 |
case 'M': printf(" material"); break; |
485 |
case '~': printf(" tilde"); break; |
486 |
} |
487 |
putchar('\n'); |
488 |
if (sens_curve == scolor_photopic) |
489 |
printf("-pY\t\t\t\t# photopic output\n"); |
490 |
else if (sens_curve == scolor_scotopic) |
491 |
printf("-pS\t\t\t\t# scotopic output\n"); |
492 |
else if (sens_curve == scolor_melanopic) |
493 |
printf("-pM\t\t\t\t# melanopic output\n"); |
494 |
else if (out_prims == stdprims) |
495 |
printf("-pRGB\t\t\t\t# standard RGB color output\n"); |
496 |
else if (out_prims == xyzprims) |
497 |
printf("-pXYZ\t\t\t\t# CIE XYZ color output\n"); |
498 |
else if (out_prims != NULL) |
499 |
printf("-pc %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\t# output color primaries and white point\n", |
500 |
out_prims[RED][0], out_prims[RED][1], |
501 |
out_prims[GRN][0], out_prims[GRN][1], |
502 |
out_prims[BLU][0], out_prims[BLU][1], |
503 |
out_prims[WHT][0], out_prims[WHT][1]); |
504 |
if ((sens_curve == NULL) & (NCSAMP > 3)) |
505 |
printf(out_prims != NULL ? "-co-\t\t\t\t# output tristimulus colors\n" : |
506 |
"-co+\t\t\t\t# output spectral values\n"); |
507 |
printf(erract[WARNING].pf != NULL ? |
508 |
"-w+\t\t\t\t# warning messages on\n" : |
509 |
"-w-\t\t\t\t# warning messages off\n"); |
510 |
print_rdefaults(); |
511 |
} |