ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.45
Committed: Tue Aug 15 00:46:56 2023 UTC (9 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.44: +4 -2 lines
Log Message:
fix(rtrace,rcontrib): Added i/o format options to features list

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rtmain.c,v 2.44 2023/02/06 22:40:21 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 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 "HessianAmbientCache\nAmbientAveraging\n" \
72 "AmbientValueSharing\nAdaptiveShadowTesting\n" \
73 "InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \
74 "Outputs=o,d,v,V,w,W,l,L,c,p,n,N,s,m,M,r,x,R,X,~\n"
75 #else
76 #define RTRACE_FEATURES "IrradianceCalc\nIrradianceCalc\nDistanceLimiting\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 #endif
82
83
84 int
85 main(int argc, char *argv[])
86 {
87 #define check(ol,al) if (argv[i][ol] || \
88 badarg(argc-i-1,argv+i+1,al)) \
89 goto badopt
90 #define check_bool(olen,var) switch (argv[i][olen]) { \
91 case '\0': var = !var; break; \
92 case 'y': case 'Y': case 't': case 'T': \
93 case '+': case '1': var = 1; break; \
94 case 'n': case 'N': case 'f': case 'F': \
95 case '-': case '0': var = 0; break; \
96 default: goto badopt; }
97 extern char *octname;
98 int persist = 0;
99 char *octnm = NULL;
100 char **tralp = NULL;
101 int duped1 = -1;
102 int rval;
103 int i;
104 /* global program name */
105 progname = argv[0] = fixargv0(argv[0]);
106 /* feature check only? */
107 strcat(RFeatureList, RTRACE_FEATURES);
108 if (argc > 1 && !strcmp(argv[1], "-features"))
109 return feature_status(argc-2, argv+2);
110 /* add trace notify function */
111 for (i = 0; addobjnotify[i] != NULL; i++)
112 ;
113 addobjnotify[i] = tranotify;
114 /* option city */
115 for (i = 1; i < argc; i++) {
116 /* expand arguments */
117 while ((rval = expandarg(&argc, &argv, i)) > 0)
118 ;
119 if (rval < 0) {
120 sprintf(errmsg, "cannot expand '%s'", argv[i]);
121 error(SYSTEM, errmsg);
122 }
123 if (argv[i] == NULL || argv[i][0] != '-')
124 break; /* break from options */
125 if (!strcmp(argv[i], "-version")) {
126 puts(VersionID);
127 quit(0);
128 }
129 if (!strcmp(argv[i], "-defaults") ||
130 !strcmp(argv[i], "-help")) {
131 printdefaults();
132 quit(0);
133 }
134 rval = getrenderopt(argc-i, argv+i);
135 if (rval >= 0) {
136 i += rval;
137 continue;
138 }
139 switch (argv[i][1]) {
140 case 'n': /* number of cores */
141 check(2,"i");
142 nproc = atoi(argv[++i]);
143 if (nproc <= 0)
144 error(USER, "bad number of processes");
145 break;
146 case 'x': /* x resolution */
147 check(2,"i");
148 hresolu = atoi(argv[++i]);
149 break;
150 case 'y': /* y resolution */
151 check(2,"i");
152 vresolu = atoi(argv[++i]);
153 break;
154 case 'w': /* warnings */
155 rval = erract[WARNING].pf != NULL;
156 check_bool(2,rval);
157 if (rval) erract[WARNING].pf = wputs;
158 else erract[WARNING].pf = NULL;
159 break;
160 case 'e': /* error file */
161 check(2,"s");
162 errfile = argv[++i];
163 break;
164 case 'l': /* limit distance */
165 if (argv[i][2] != 'd')
166 goto badopt;
167 check_bool(3,lim_dist);
168 break;
169 case 'I': /* immed. irradiance */
170 check_bool(2,imm_irrad);
171 break;
172 case 'f': /* format i/o */
173 switch (argv[i][2]) {
174 case 'a': /* ascii */
175 case 'f': /* float */
176 case 'd': /* double */
177 inform = argv[i][2];
178 break;
179 default:
180 goto badopt;
181 }
182 switch (argv[i][3]) {
183 case '\0':
184 outform = inform;
185 break;
186 case 'a': /* ascii */
187 case 'f': /* float */
188 case 'd': /* double */
189 case 'c': /* color */
190 check(4,"");
191 outform = argv[i][3];
192 break;
193 default:
194 goto badopt;
195 }
196 break;
197 case 'o': /* output */
198 outvals = argv[i]+2;
199 break;
200 case 'h': /* header output */
201 rval = loadflags & IO_INFO;
202 check_bool(2,rval);
203 loadflags = rval ? loadflags | IO_INFO :
204 loadflags & ~IO_INFO;
205 break;
206 case 't': /* trace */
207 switch (argv[i][2]) {
208 case 'i': /* include */
209 case 'I':
210 check(3,"s");
211 if (traincl != 1) {
212 traincl = 1;
213 tralp = tralist;
214 }
215 if (argv[i][2] == 'I') { /* file */
216 rval = wordfile(tralp, MAXMODLIST-(tralp-tralist),
217 getpath(argv[++i],getrlibpath(),R_OK));
218 if (rval < 0) {
219 sprintf(errmsg,
220 "cannot open trace include file \"%s\"",
221 argv[i]);
222 error(SYSTEM, errmsg);
223 }
224 tralp += rval;
225 } else {
226 *tralp++ = argv[++i];
227 *tralp = NULL;
228 }
229 break;
230 case 'e': /* exclude */
231 case 'E':
232 check(3,"s");
233 if (traincl != 0) {
234 traincl = 0;
235 tralp = tralist;
236 }
237 if (argv[i][2] == 'E') { /* file */
238 rval = wordfile(tralp, MAXMODLIST-(tralp-tralist),
239 getpath(argv[++i],getrlibpath(),R_OK));
240 if (rval < 0) {
241 sprintf(errmsg,
242 "cannot open trace exclude file \"%s\"",
243 argv[i]);
244 error(SYSTEM, errmsg);
245 }
246 tralp += rval;
247 } else {
248 *tralp++ = argv[++i];
249 *tralp = NULL;
250 }
251 break;
252 default:
253 goto badopt;
254 }
255 break;
256 #ifdef PERSIST
257 case 'P': /* persist file */
258 if (argv[i][2] == 'P') {
259 check(3,"s");
260 persist = PARALLEL;
261 } else {
262 check(2,"s");
263 persist = PERSIST;
264 }
265 persistfile(argv[++i]);
266 break;
267 #endif
268 default:
269 goto badopt;
270 }
271 }
272 if (nproc > 1 && persist)
273 error(USER, "multiprocessing incompatible with persist file");
274 /* initialize object types */
275 initotypes();
276 /* initialize urand */
277 if (rand_samp) {
278 srandom((long)time(0));
279 initurand(0);
280 } else {
281 srandom(0L);
282 initurand(2048);
283 }
284 /* set up signal handling */
285 sigdie(SIGINT, "Interrupt");
286 #ifdef SIGHUP
287 sigdie(SIGHUP, "Hangup");
288 #endif
289 sigdie(SIGTERM, "Terminate");
290 #ifdef SIGPIPE
291 sigdie(SIGPIPE, "Broken pipe");
292 #endif
293 #ifdef SIGALRM
294 sigdie(SIGALRM, "Alarm clock");
295 #endif
296 #ifdef SIGXCPU
297 sigdie(SIGXCPU, "CPU limit exceeded");
298 sigdie(SIGXFSZ, "File size exceeded");
299 #endif
300 /* open error file */
301 if (errfile != NULL) {
302 if (freopen(errfile, "a", stderr) == NULL)
303 quit(2);
304 fprintf(stderr, "**************\n*** PID %5d: ",
305 getpid());
306 printargs(argc, argv, stderr);
307 putc('\n', stderr);
308 fflush(stderr);
309 }
310 #ifdef NICE
311 nice(NICE); /* lower priority */
312 #endif
313 /* get octree */
314 if (i == argc)
315 octnm = NULL;
316 else if (i == argc-1)
317 octnm = argv[i];
318 else
319 goto badopt;
320 if (octnm == NULL)
321 error(USER, "missing octree argument");
322 /* set up output */
323 #ifdef PERSIST
324 if (persist) {
325 duped1 = dup(fileno(stdout)); /* don't lose our output */
326 openheader();
327 }
328 #endif
329 if (outform != 'a')
330 SET_FILE_BINARY(stdout);
331 rval = setrtoutput();
332 octname = savqstr(octnm);
333 readoct(octname, loadflags, &thescene, NULL);
334 nsceneobjs = nobjects;
335
336 if (loadflags & IO_INFO) { /* print header */
337 printargs(i, argv, stdout);
338 printf("SOFTWARE= %s\n", VersionID);
339 fputnow(stdout);
340 if (rval > 0) /* saved from setrtoutput() call */
341 printf("NCOMP=%d\n", rval);
342 if ((outform == 'f') | (outform == 'd'))
343 fputendian(stdout);
344 fputformat(formstr(outform), stdout);
345 putchar('\n');
346 }
347
348 if (!castonly) { /* any actual ray traversal to do? */
349
350 ray_init_pmap(); /* PMAP: set up & load photon maps */
351
352 marksources(); /* find and mark sources */
353
354 setambient(); /* initialize ambient calculation */
355 } else
356 distantsources(); /* else mark only distant sources */
357
358 fflush(stdout); /* in case we're duplicating header */
359
360 #ifdef PERSIST
361 if (persist) {
362 /* reconnect stdout */
363 dup2(duped1, fileno(stdout));
364 close(duped1);
365 if (persist == PARALLEL) { /* multiprocessing */
366 preload_objs(); /* preload scene */
367 shm_boundary = (char *)malloc(16);
368 strcpy(shm_boundary, "SHM_BOUNDARY");
369 while ((rval=fork()) == 0) { /* keep on forkin' */
370 pflock(1);
371 pfhold();
372 ambsync(); /* load new values */
373 }
374 if (rval < 0)
375 error(SYSTEM, "cannot fork child for persist function");
376 pfdetach(); /* parent will run then exit */
377 }
378 }
379 runagain:
380 if (persist)
381 dupheader(); /* send header to stdout */
382 #endif
383 /* trace rays */
384 rtrace(NULL, nproc);
385 /* flush ambient file */
386 ambsync();
387 #ifdef PERSIST
388 if (persist == PERSIST) { /* first run-through */
389 if ((rval=fork()) == 0) { /* child loops until killed */
390 pflock(1);
391 persist = PCHILD;
392 } else { /* original process exits */
393 if (rval < 0)
394 error(SYSTEM, "cannot fork child for persist function");
395 pfdetach(); /* parent exits */
396 }
397 }
398 if (persist == PCHILD) { /* wait for a signal then go again */
399 pfhold();
400 raynum = nrays = 0; /* reinitialize */
401 goto runagain;
402 }
403 #endif
404
405 ray_done_pmap(); /* PMAP: free photon maps */
406
407 quit(0);
408
409 badopt:
410 sprintf(errmsg, "command line error at '%s'", argv[i]);
411 error(USER, errmsg);
412 return 1; /* pro forma return */
413
414 #undef check
415 #undef check_bool
416 }
417
418
419 void
420 wputs( /* warning output function */
421 const char *s
422 )
423 {
424 int lasterrno = errno;
425 eputs(s);
426 errno = lasterrno;
427 }
428
429
430 void
431 eputs( /* put string to stderr */
432 const char *s
433 )
434 {
435 static int midline = 0;
436
437 if (!*s)
438 return;
439 if (!midline++) {
440 fputs(progname, stderr);
441 fputs(": ", stderr);
442 }
443 fputs(s, stderr);
444 if (s[strlen(s)-1] == '\n') {
445 fflush(stderr);
446 midline = 0;
447 }
448 }
449
450
451 static void
452 onsig( /* fatal signal */
453 int signo
454 )
455 {
456 static int gotsig = 0;
457
458 if (gotsig++) /* two signals and we're gone! */
459 _exit(signo);
460
461 #ifdef SIGALRM
462 alarm(15); /* allow 15 seconds to clean up */
463 signal(SIGALRM, SIG_DFL); /* make certain we do die */
464 #endif
465 eputs("signal - ");
466 eputs(sigerr[signo]);
467 eputs("\n");
468 quit(3);
469 }
470
471
472 static void
473 sigdie( /* set fatal signal */
474 int signo,
475 char *msg
476 )
477 {
478 if (signal(signo, onsig) == SIG_IGN)
479 signal(signo, SIG_IGN);
480 sigerr[signo] = msg;
481 }
482
483
484 static void
485 printdefaults(void) /* print default values to stdout */
486 {
487 char *cp;
488
489 if (imm_irrad)
490 printf("-I+\t\t\t\t# immediate irradiance on\n");
491 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
492 printf("-x %-9d\t\t\t# %s\n", hresolu,
493 vresolu && hresolu ? "x resolution" : "flush interval");
494 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
495 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
496 "-ld-\t\t\t\t# limit distance off\n");
497 printf("-h%c\t\t\t\t# %s header\n", loadflags & IO_INFO ? '+' : '-',
498 loadflags & IO_INFO ? "output" : "no");
499 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
500 inform, outform, formstr(inform), formstr(outform));
501 printf("-o%-9s\t\t\t# output", outvals);
502 for (cp = outvals; *cp; cp++)
503 switch (*cp) {
504 case 't': case 'T': printf(" trace"); break;
505 case 'o': printf(" origin"); break;
506 case 'd': printf(" direction"); break;
507 case 'r': printf(" reflect_contrib"); break;
508 case 'R': printf(" reflect_length"); break;
509 case 'x': printf(" unreflect_contrib"); break;
510 case 'X': printf(" unreflect_length"); break;
511 case 'v': printf(" value"); break;
512 case 'V': printf(" contribution"); break;
513 case 'l': printf(" length"); break;
514 case 'L': printf(" first_length"); break;
515 case 'p': printf(" point"); break;
516 case 'n': printf(" normal"); break;
517 case 'N': printf(" unperturbed_normal"); break;
518 case 's': printf(" surface"); break;
519 case 'w': printf(" weight"); break;
520 case 'W': printf(" coefficient"); break;
521 case 'm': printf(" modifier"); break;
522 case 'M': printf(" material"); break;
523 case '~': printf(" tilde"); break;
524 }
525 putchar('\n');
526 printf(erract[WARNING].pf != NULL ?
527 "-w+\t\t\t\t# warning messages on\n" :
528 "-w-\t\t\t\t# warning messages off\n");
529 print_rdefaults();
530 }