ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.59
Committed: Sat Jun 7 05:09:46 2025 UTC (10 hours, 43 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.58: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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