ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.49
Committed: Mon Dec 11 17:21:59 2023 UTC (5 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.48: +13 -10 lines
Log Message:
fix(rtrace): made spectral settings interaction more robust

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rtmain.c,v 2.48 2023/11/17 20:02:07 greg Exp $";
3 #endif
4 /*
5 * rtmain.c - main for rtrace per-ray calculation program
6 */
7
8 #include "copyright.h"
9
10 #include <signal.h>
11
12 #include "platform.h"
13 #include "rtprocess.h" /* getpid() */
14 #include "resolu.h"
15 #include "ray.h"
16 #include "source.h"
17 #include "ambient.h"
18 #include "random.h"
19 #include "paths.h"
20 #include "pmapray.h"
21
22 extern char *progname; /* global argv[0] */
23
24 extern char *shm_boundary; /* boundary of shared memory */
25
26 /* persistent processes define */
27 #ifdef F_SETLKW
28 #define PERSIST 1 /* normal persist */
29 #define PARALLEL 2 /* parallel persist */
30 #define PCHILD 3 /* child of normal persist */
31 #endif
32
33 char *sigerr[NSIG]; /* signal error messages */
34 char *errfile = NULL; /* error output file */
35
36 int nproc = 1; /* number of processes */
37
38 extern int setrtoutput(void); /* set output values */
39
40 int inform = 'a'; /* input format */
41 int outform = 'a'; /* output format */
42 char *outvals = "v"; /* output specification */
43
44 int hresolu = 0; /* horizontal (scan) size */
45 int vresolu = 0; /* vertical resolution */
46
47 extern int castonly; /* only doing ray-casting? */
48
49 int imm_irrad = 0; /* compute immediate irradiance? */
50 int lim_dist = 0; /* limit distance? */
51
52 #ifndef MAXMODLIST
53 #define MAXMODLIST 1024 /* maximum modifiers we'll track */
54 #endif
55
56 extern void (*addobjnotify[])(); /* object notification calls */
57 extern void tranotify(OBJECT obj);
58
59 char *tralist[MAXMODLIST]; /* list of modifers to trace (or no) */
60 int traincl = -1; /* include == 1, exclude == 0 */
61
62 double (*sens_curve)(SCOLOR scol) = NULL; /* spectral conversion for 1-channel */
63 double out_scalefactor = 1; /* output calibration scale factor */
64 RGBPRIMP out_prims = stdprims; /* output color primitives (NULL if spectral) */
65 static RGBPRIMS our_prims; /* private output color primitives */
66
67 static int loadflags = ~IO_FILES; /* what to load from octree */
68
69 static void onsig(int signo);
70 static void sigdie(int signo, char *msg);
71 static void printdefaults(void);
72
73 #ifdef PERSIST
74 #define RTRACE_FEATURES "Persist\nParallelPersist\nMultiprocessing\n" \
75 "IrradianceCalc\nImmediateIrradiance\nDistanceLimiting\n" \
76 "ParticipatingMedia=Mist\n" \
77 "HessianAmbientCache\nAmbientAveraging\n" \
78 "AmbientValueSharing\nAdaptiveShadowTesting\n" \
79 "InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \
80 "Outputs=o,d,v,V,w,W,l,L,c,p,n,N,s,m,M,r,x,R,X,~\n"
81 #else
82 #define RTRACE_FEATURES "IrradianceCalc\nIrradianceCalc\nDistanceLimiting\n" \
83 "ParticipatingMedia=Mist\n" \
84 "HessianAmbientCache\nAmbientAveraging\n" \
85 "AmbientValueSharing\nAdaptiveShadowTesting\n" \
86 "InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \
87 "Outputs=o,d,v,V,w,W,l,L,c,p,n,N,s,m,M,r,x,R,X,~\n"
88 #endif
89
90
91 int
92 main(int argc, char *argv[])
93 {
94 #define check(ol,al) if (argv[i][ol] || \
95 badarg(argc-i-1,argv+i+1,al)) \
96 goto badopt
97 #define check_bool(olen,var) switch (argv[i][olen]) { \
98 case '\0': var = !var; break; \
99 case 'y': case 'Y': case 't': case 'T': \
100 case '+': case '1': var = 1; break; \
101 case 'n': case 'N': case 'f': case 'F': \
102 case '-': case '0': var = 0; break; \
103 default: goto badopt; }
104 extern char *octname;
105 int persist = 0;
106 char *octnm = NULL;
107 char **tralp = NULL;
108 int duped1 = -1;
109 int rval;
110 int i;
111 /* global program name */
112 progname = argv[0] = fixargv0(argv[0]);
113 /* feature check only? */
114 strcat(RFeatureList, RTRACE_FEATURES);
115 if (argc > 1 && !strcmp(argv[1], "-features"))
116 return feature_status(argc-2, argv+2);
117 /* add trace notify function */
118 for (i = 0; addobjnotify[i] != NULL; i++)
119 ;
120 addobjnotify[i] = tranotify;
121 /* option city */
122 for (i = 1; i < argc; i++) {
123 /* expand arguments */
124 while ((rval = expandarg(&argc, &argv, i)) > 0)
125 ;
126 if (rval < 0) {
127 sprintf(errmsg, "cannot expand '%s'", argv[i]);
128 error(SYSTEM, errmsg);
129 }
130 if (argv[i] == NULL || argv[i][0] != '-')
131 break; /* break from options */
132 if (!strcmp(argv[i], "-version")) {
133 puts(VersionID);
134 quit(0);
135 }
136 if (!strcmp(argv[i], "-defaults") ||
137 !strcmp(argv[i], "-help")) {
138 printdefaults();
139 quit(0);
140 }
141 rval = getrenderopt(argc-i, argv+i);
142 if (rval >= 0) {
143 i += rval;
144 continue;
145 }
146 switch (argv[i][1]) {
147 case 'n': /* number of cores */
148 check(2,"i");
149 nproc = atoi(argv[++i]);
150 if (nproc <= 0)
151 error(USER, "bad number of processes");
152 break;
153 case 'x': /* x resolution */
154 check(2,"i");
155 hresolu = atoi(argv[++i]);
156 break;
157 case 'y': /* y resolution */
158 check(2,"i");
159 vresolu = atoi(argv[++i]);
160 break;
161 case 'w': /* warnings & spectral */
162 rval = erract[WARNING].pf != NULL;
163 check_bool(2,rval);
164 if (rval) erract[WARNING].pf = wputs;
165 else erract[WARNING].pf = NULL;
166 break;
167 case 'e': /* error file */
168 check(2,"s");
169 errfile = argv[++i];
170 break;
171 case 'l': /* limit distance */
172 if (argv[i][2] != 'd')
173 goto badopt;
174 check_bool(3,lim_dist);
175 break;
176 case 'I': /* immed. irradiance */
177 check_bool(2,imm_irrad);
178 break;
179 case 'f': /* format i/o */
180 switch (argv[i][2]) {
181 case 'a': /* ascii */
182 case 'f': /* float */
183 case 'd': /* double */
184 inform = argv[i][2];
185 break;
186 default:
187 goto badopt;
188 }
189 switch (argv[i][3]) {
190 case '\0':
191 outform = inform;
192 break;
193 case 'a': /* ascii */
194 case 'f': /* float */
195 case 'd': /* double */
196 case 'c': /* color */
197 check(4,"");
198 outform = argv[i][3];
199 break;
200 default:
201 goto badopt;
202 }
203 break;
204 case 'o': /* output */
205 outvals = argv[i]+2;
206 break;
207 case 'h': /* header output */
208 rval = loadflags & IO_INFO;
209 check_bool(2,rval);
210 loadflags = rval ? loadflags | IO_INFO :
211 loadflags & ~IO_INFO;
212 break;
213 case 't': /* trace */
214 switch (argv[i][2]) {
215 case 'i': /* include */
216 case 'I':
217 check(3,"s");
218 if (traincl != 1) {
219 traincl = 1;
220 tralp = tralist;
221 }
222 if (argv[i][2] == 'I') { /* file */
223 rval = wordfile(tralp, MAXMODLIST-(tralp-tralist),
224 getpath(argv[++i],getrlibpath(),R_OK));
225 if (rval < 0) {
226 sprintf(errmsg,
227 "cannot open trace include file \"%s\"",
228 argv[i]);
229 error(SYSTEM, errmsg);
230 }
231 tralp += rval;
232 } else {
233 *tralp++ = argv[++i];
234 *tralp = NULL;
235 }
236 break;
237 case 'e': /* exclude */
238 case 'E':
239 check(3,"s");
240 if (traincl != 0) {
241 traincl = 0;
242 tralp = tralist;
243 }
244 if (argv[i][2] == 'E') { /* file */
245 rval = wordfile(tralp, MAXMODLIST-(tralp-tralist),
246 getpath(argv[++i],getrlibpath(),R_OK));
247 if (rval < 0) {
248 sprintf(errmsg,
249 "cannot open trace exclude file \"%s\"",
250 argv[i]);
251 error(SYSTEM, errmsg);
252 }
253 tralp += rval;
254 } else {
255 *tralp++ = argv[++i];
256 *tralp = NULL;
257 }
258 break;
259 default:
260 goto badopt;
261 }
262 break;
263 case 'p': /* value output */
264 switch (argv[i][2]) {
265 case 'R': /* standard RGB output */
266 if (strcmp(argv[i]+2, "RGB"))
267 goto badopt;
268 out_prims = stdprims;
269 out_scalefactor = 1;
270 sens_curve = NULL;
271 break;
272 case 'X': /* XYZ output */
273 if (strcmp(argv[i]+2, "XYZ"))
274 goto badopt;
275 out_prims = xyzprims;
276 out_scalefactor = WHTEFFICACY;
277 sens_curve = NULL;
278 break;
279 case 'c': {
280 int j;
281 check(3,"ffffffff");
282 rval = 0;
283 for (j = 0; j < 8; j++) {
284 our_prims[0][j] = atof(argv[++i]);
285 rval |= fabs(our_prims[0][j]-stdprims[0][j]) > .001;
286 }
287 if (rval) {
288 if (!colorprimsOK(our_prims))
289 error(USER, "illegal primary chromaticities");
290 out_prims = our_prims;
291 } else
292 out_prims = stdprims;
293 out_scalefactor = 1;
294 sens_curve = NULL;
295 } break;
296 case 'Y': /* photopic response */
297 sens_curve = scolor_photopic;
298 out_scalefactor = WHTEFFICACY;
299 break;
300 case 'S': /* scotopic response */
301 sens_curve = scolor_scotopic;
302 out_scalefactor = WHTSCOTOPIC;
303 break;
304 case 'M': /* melanopic response */
305 sens_curve = scolor_melanopic;
306 out_scalefactor = WHTMELANOPIC;
307 break;
308 default:
309 goto badopt;
310 }
311 break;
312 #if MAXCSAMP>3
313 case 'c': /* output spectral results */
314 if (argv[i][2] != 'o')
315 goto badopt;
316 rval = (out_prims == NULL) & (sens_curve == NULL);
317 check_bool(3,rval);
318 if (rval) {
319 out_prims = NULL;
320 sens_curve = NULL;
321 } else if (out_prims == NULL)
322 out_prims = stdprims;
323 break;
324 #endif
325 #ifdef PERSIST
326 case 'P': /* persist file */
327 if (argv[i][2] == 'P') {
328 check(3,"s");
329 persist = PARALLEL;
330 } else {
331 check(2,"s");
332 persist = PERSIST;
333 }
334 persistfile(argv[++i]);
335 break;
336 #endif
337 default:
338 goto badopt;
339 }
340 }
341 /* set/check spectral sampling */
342 rval = setspectrsamp(CNDX, WLPART);
343 if (rval < 0)
344 error(USER, "unsupported spectral sampling");
345 if (out_prims != NULL) {
346 if (!rval)
347 error(WARNING, "spectral range incompatible with color output");
348 } else if (NCSAMP == 3)
349 out_prims = stdprims; /* 3 samples do not a spectrum make */
350 if (nproc > 1 && persist)
351 error(USER, "multiprocessing incompatible with persist file");
352 /* initialize object types */
353 initotypes();
354 /* initialize urand */
355 if (rand_samp) {
356 srandom((long)time(0));
357 initurand(0);
358 } else {
359 srandom(0L);
360 initurand(2048);
361 }
362 /* set up signal handling */
363 sigdie(SIGINT, "Interrupt");
364 #ifdef SIGHUP
365 sigdie(SIGHUP, "Hangup");
366 #endif
367 sigdie(SIGTERM, "Terminate");
368 #ifdef SIGPIPE
369 sigdie(SIGPIPE, "Broken pipe");
370 #endif
371 #ifdef SIGALRM
372 sigdie(SIGALRM, "Alarm clock");
373 #endif
374 #ifdef SIGXCPU
375 sigdie(SIGXCPU, "CPU limit exceeded");
376 sigdie(SIGXFSZ, "File size exceeded");
377 #endif
378 /* open error file */
379 if (errfile != NULL) {
380 if (freopen(errfile, "a", stderr) == NULL)
381 quit(2);
382 fprintf(stderr, "**************\n*** PID %5d: ",
383 getpid());
384 printargs(argc, argv, stderr);
385 putc('\n', stderr);
386 fflush(stderr);
387 }
388 #ifdef NICE
389 nice(NICE); /* lower priority */
390 #endif
391 /* get octree */
392 if (i == argc)
393 octnm = NULL;
394 else if (i == argc-1)
395 octnm = argv[i];
396 else
397 goto badopt;
398 if (octnm == NULL)
399 error(USER, "missing octree argument");
400 /* set up output */
401 #ifdef PERSIST
402 if (persist) {
403 duped1 = dup(fileno(stdout)); /* don't lose our output */
404 openheader();
405 }
406 #endif
407 if (outform != 'a')
408 SET_FILE_BINARY(stdout);
409 rval = setrtoutput();
410 octname = savqstr(octnm);
411 readoct(octname, loadflags, &thescene, NULL);
412 nsceneobjs = nobjects;
413
414 if (loadflags & IO_INFO) { /* print header */
415 printargs(i, argv, stdout);
416 printf("SOFTWARE= %s\n", VersionID);
417 fputnow(stdout);
418 if (rval > 0) /* saved from setrtoutput() call */
419 fputncomp(rval, stdout);
420 if (NCSAMP > 3)
421 fputwlsplit(WLPART, stdout);
422 if ((out_prims != stdprims) & (out_prims != NULL))
423 fputprims(out_prims, stdout);
424 if ((outform == 'f') | (outform == 'd'))
425 fputendian(stdout);
426 fputformat(formstr(outform), stdout);
427 fputc('\n', stdout); /* end of header */
428 }
429
430 if (!castonly) { /* any actual ray traversal to do? */
431
432 ray_init_pmap(); /* PMAP: set up & load photon maps */
433
434 marksources(); /* find and mark sources */
435
436 setambient(); /* initialize ambient calculation */
437 } else
438 distantsources(); /* else mark only distant sources */
439
440 fflush(stdout); /* in case we're duplicating header */
441
442 #ifdef PERSIST
443 if (persist) {
444 /* reconnect stdout */
445 dup2(duped1, fileno(stdout));
446 close(duped1);
447 if (persist == PARALLEL) { /* multiprocessing */
448 preload_objs(); /* preload scene */
449 shm_boundary = (char *)malloc(16);
450 strcpy(shm_boundary, "SHM_BOUNDARY");
451 while ((rval=fork()) == 0) { /* keep on forkin' */
452 pflock(1);
453 pfhold();
454 ambsync(); /* load new values */
455 }
456 if (rval < 0)
457 error(SYSTEM, "cannot fork child for persist function");
458 pfdetach(); /* parent will run then exit */
459 }
460 }
461 runagain:
462 if (persist)
463 dupheader(); /* send header to stdout */
464 #endif
465 /* trace rays */
466 rtrace(NULL, nproc);
467 /* flush ambient file */
468 ambsync();
469 #ifdef PERSIST
470 if (persist == PERSIST) { /* first run-through */
471 if ((rval=fork()) == 0) { /* child loops until killed */
472 pflock(1);
473 persist = PCHILD;
474 } else { /* original process exits */
475 if (rval < 0)
476 error(SYSTEM, "cannot fork child for persist function");
477 pfdetach(); /* parent exits */
478 }
479 }
480 if (persist == PCHILD) { /* wait for a signal then go again */
481 pfhold();
482 raynum = nrays = 0; /* reinitialize */
483 goto runagain;
484 }
485 #endif
486
487 ray_done_pmap(); /* PMAP: free photon maps */
488
489 quit(0);
490
491 badopt:
492 sprintf(errmsg, "command line error at '%s'", argv[i]);
493 error(USER, errmsg);
494 return 1; /* pro forma return */
495
496 #undef check
497 #undef check_bool
498 }
499
500
501 void
502 wputs( /* warning output function */
503 const char *s
504 )
505 {
506 int lasterrno = errno;
507 eputs(s);
508 errno = lasterrno;
509 }
510
511
512 void
513 eputs( /* put string to stderr */
514 const char *s
515 )
516 {
517 static int midline = 0;
518
519 if (!*s)
520 return;
521 if (!midline++) {
522 fputs(progname, stderr);
523 fputs(": ", stderr);
524 }
525 fputs(s, stderr);
526 if (s[strlen(s)-1] == '\n') {
527 fflush(stderr);
528 midline = 0;
529 }
530 }
531
532
533 static void
534 onsig( /* fatal signal */
535 int signo
536 )
537 {
538 static int gotsig = 0;
539
540 if (gotsig++) /* two signals and we're gone! */
541 _exit(signo);
542
543 #ifdef SIGALRM
544 alarm(15); /* allow 15 seconds to clean up */
545 signal(SIGALRM, SIG_DFL); /* make certain we do die */
546 #endif
547 eputs("signal - ");
548 eputs(sigerr[signo]);
549 eputs("\n");
550 quit(3);
551 }
552
553
554 static void
555 sigdie( /* set fatal signal */
556 int signo,
557 char *msg
558 )
559 {
560 if (signal(signo, onsig) == SIG_IGN)
561 signal(signo, SIG_IGN);
562 sigerr[signo] = msg;
563 }
564
565
566 static void
567 printdefaults(void) /* print default values to stdout */
568 {
569 char *cp;
570
571 printf(erract[WARNING].pf != NULL ?
572 "-w+\t\t\t\t# warning messages on\n" :
573 "-w-\t\t\t\t# warning messages off\n");
574 if (imm_irrad)
575 printf("-I+\t\t\t\t# immediate irradiance on\n");
576 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
577 printf("-x %-9d\t\t\t# %s\n", hresolu,
578 vresolu && hresolu ? "x resolution" : "flush interval");
579 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
580 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
581 "-ld-\t\t\t\t# limit distance off\n");
582 printf(loadflags & IO_INFO ? "-h+\t\t\t\t# output header\n" :
583 "-h-\t\t\t\t# no header\n");
584 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
585 inform, outform, formstr(inform), formstr(outform));
586 printf("-o%-9s\t\t\t# output", outvals);
587 for (cp = outvals; *cp; cp++)
588 switch (*cp) {
589 case 't': case 'T': printf(" trace"); break;
590 case 'o': printf(" origin"); break;
591 case 'd': printf(" direction"); break;
592 case 'r': printf(" reflect_contrib"); break;
593 case 'R': printf(" reflect_length"); break;
594 case 'x': printf(" unreflect_contrib"); break;
595 case 'X': printf(" unreflect_length"); break;
596 case 'v': printf(" value"); break;
597 case 'V': printf(" contribution"); break;
598 case 'l': printf(" length"); break;
599 case 'L': printf(" first_length"); break;
600 case 'p': printf(" point"); break;
601 case 'n': printf(" normal"); break;
602 case 'N': printf(" unperturbed_normal"); break;
603 case 's': printf(" surface"); break;
604 case 'w': printf(" weight"); break;
605 case 'W': printf(" coefficient"); break;
606 case 'm': printf(" modifier"); break;
607 case 'M': printf(" material"); break;
608 case '~': printf(" tilde"); break;
609 }
610 fputc('\n', stdout);
611 if (sens_curve == scolor_photopic)
612 printf("-pY\t\t\t\t# photopic output\n");
613 else if (sens_curve == scolor_scotopic)
614 printf("-pS\t\t\t\t# scotopic output\n");
615 else if (sens_curve == scolor_melanopic)
616 printf("-pM\t\t\t\t# melanopic output\n");
617 else if (out_prims == stdprims)
618 printf("-pRGB\t\t\t\t# standard RGB color output\n");
619 else if (out_prims == xyzprims)
620 printf("-pXYZ\t\t\t\t# CIE XYZ color output\n");
621 else if (out_prims != NULL)
622 printf("-pc %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\t# output color primaries and white point\n",
623 out_prims[RED][0], out_prims[RED][1],
624 out_prims[GRN][0], out_prims[GRN][1],
625 out_prims[BLU][0], out_prims[BLU][1],
626 out_prims[WHT][0], out_prims[WHT][1]);
627 if (NCSAMP > 3)
628 printf(out_prims != NULL ? "-co-\t\t\t\t# output tristimulus colors\n" :
629 "-co+\t\t\t\t# output spectral values\n");
630 print_rdefaults();
631 }