ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/rt/rtmain.c
Revision: 2.62
Committed: Fri Dec 5 17:51:34 2025 UTC (3 weeks, 4 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.61: +9 -3 lines
Log Message:
feat(rtrace,rxtrace): Added -pA output option for radiometric flux

File Contents

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