ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.58
Committed: Tue Apr 22 17:12:25 2025 UTC (9 days, 18 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.57: +4 -1 lines
Log Message:
feat(rpict,rtrace,rvu,rxpict,rxtrace,rxpiece): Added -e expr and -f file.cal options

File Contents

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