ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.34
Committed: Wed Apr 8 00:57:39 2020 UTC (4 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 2.33: +3 -9 lines
Log Message:
Removed superfluous warnings

File Contents

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