ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.25
Committed: Thu Apr 23 20:29:14 2015 UTC (9 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 2.24: +2 -1 lines
Log Message:
Added missing external declaration

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.25 static const char RCSid[] = "$Id: rtmain.c,v 2.24 2015/04/23 17:42:04 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * rtmain.c - main for rtrace per-ray calculation program
6     */
7    
8 greg 2.2 #include "copyright.h"
9 greg 2.1
10 schorsch 2.4 #include <signal.h>
11    
12     #include "platform.h"
13 schorsch 2.7 #include "rtprocess.h" /* getpid() */
14 schorsch 2.9 #include "resolu.h"
15 greg 2.1 #include "ray.h"
16     #include "source.h"
17     #include "ambient.h"
18     #include "random.h"
19     #include "paths.h"
20 greg 2.23 #include "pmapray.h"
21 greg 2.1
22 greg 2.18 extern char *progname; /* global argv[0] */
23    
24     extern char *shm_boundary; /* boundary of shared memory */
25    
26 greg 2.1 /* 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 greg 2.18 int nproc = 1; /* number of processes */
37    
38 greg 2.1 extern char *formstr(); /* string from format */
39 greg 2.18 int inform = 'a'; /* input format */
40     int outform = 'a'; /* output format */
41     char *outvals = "v"; /* output specification */
42 greg 2.1
43 greg 2.18 int hresolu = 0; /* horizontal (scan) size */
44     int vresolu = 0; /* vertical resolution */
45 greg 2.1
46 greg 2.18 int imm_irrad = 0; /* compute immediate irradiance? */
47     int lim_dist = 0; /* limit distance? */
48    
49     #ifndef MAXMODLIST
50     #define MAXMODLIST 1024 /* maximum modifiers we'll track */
51     #endif
52 greg 2.1
53 greg 2.18 extern void (*addobjnotify[])(); /* object notification calls */
54     extern void tranotify(OBJECT obj);
55    
56     char *tralist[MAXMODLIST]; /* list of modifers to trace (or no) */
57     int traincl = -1; /* include == 1, exclude == 0 */
58 greg 2.1
59 greg 2.10 static int loadflags = ~IO_FILES; /* what to load from octree */
60    
61 schorsch 2.9 static void onsig(int signo);
62     static void sigdie(int signo, char *msg);
63     static void printdefaults(void);
64 greg 2.1
65    
66     int
67 schorsch 2.9 main(int argc, char *argv[])
68 greg 2.1 {
69     #define check(ol,al) if (argv[i][ol] || \
70     badarg(argc-i-1,argv+i+1,al)) \
71     goto badopt
72     #define bool(olen,var) switch (argv[i][olen]) { \
73     case '\0': var = !var; break; \
74     case 'y': case 'Y': case 't': case 'T': \
75     case '+': case '1': var = 1; break; \
76     case 'n': case 'N': case 'f': case 'F': \
77     case '-': case '0': var = 0; break; \
78     default: goto badopt; }
79 greg 2.25 extern char *octname;
80 greg 2.1 int persist = 0;
81 greg 2.18 char *octnm = NULL;
82 greg 2.22 char **tralp = NULL;
83     int duped1 = -1;
84 greg 2.1 int rval;
85     int i;
86     /* global program name */
87     progname = argv[0] = fixargv0(argv[0]);
88 greg 2.18 /* add trace notify function */
89     for (i = 0; addobjnotify[i] != NULL; i++)
90     ;
91     addobjnotify[i] = tranotify;
92     /* set our defaults */
93 greg 2.20 rand_samp = 1;
94 greg 2.18 maxdepth = -10;
95     minweight = 2e-3;
96 greg 2.1 /* 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 greg 2.18 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 greg 2.1 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     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     bool(3,lim_dist);
150     break;
151     case 'I': /* immed. irradiance */
152     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     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,
199 greg 2.3 getpath(argv[++i],getrlibpath(),R_OK));
200 greg 2.1 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,
221 greg 2.3 getpath(argv[++i],getrlibpath(),R_OK));
222 greg 2.1 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 greg 2.18 if (nproc > 1) {
255     if (persist)
256     error(USER, "multiprocessing incompatible with persist file");
257 greg 2.21 if (!vresolu && hresolu > 0 && hresolu < nproc)
258 greg 2.18 error(WARNING, "number of cores should not exceed horizontal resolution");
259     if (trace != NULL)
260     error(WARNING, "multiprocessing does not work properly with trace mode");
261     }
262 greg 2.1 /* initialize object types */
263     initotypes();
264     /* initialize urand */
265 greg 2.14 if (rand_samp) {
266     srandom((long)time(0));
267     initurand(0);
268     } else {
269     srandom(0L);
270     initurand(2048);
271     }
272 greg 2.1 /* set up signal handling */
273     sigdie(SIGINT, "Interrupt");
274 schorsch 2.5 #ifdef SIGHUP
275 greg 2.1 sigdie(SIGHUP, "Hangup");
276 schorsch 2.5 #endif
277 greg 2.1 sigdie(SIGTERM, "Terminate");
278 schorsch 2.5 #ifdef SIGPIPE
279 greg 2.1 sigdie(SIGPIPE, "Broken pipe");
280 schorsch 2.5 #endif
281     #ifdef SIGALRM
282 greg 2.1 sigdie(SIGALRM, "Alarm clock");
283 schorsch 2.5 #endif
284 greg 2.1 #ifdef SIGXCPU
285     sigdie(SIGXCPU, "CPU limit exceeded");
286     sigdie(SIGXFSZ, "File size exceeded");
287     #endif
288     /* open error file */
289     if (errfile != NULL) {
290     if (freopen(errfile, "a", stderr) == NULL)
291     quit(2);
292     fprintf(stderr, "**************\n*** PID %5d: ",
293     getpid());
294     printargs(argc, argv, stderr);
295     putc('\n', stderr);
296     fflush(stderr);
297     }
298     #ifdef NICE
299     nice(NICE); /* lower priority */
300     #endif
301     /* get octree */
302     if (i == argc)
303 greg 2.18 octnm = NULL;
304 greg 2.1 else if (i == argc-1)
305 greg 2.18 octnm = argv[i];
306 greg 2.1 else
307     goto badopt;
308 greg 2.18 if (octnm == NULL)
309 greg 2.1 error(USER, "missing octree argument");
310     /* set up output */
311     #ifdef PERSIST
312     if (persist) {
313     duped1 = dup(fileno(stdout)); /* don't lose our output */
314     openheader();
315     }
316     #endif
317     if (outform != 'a')
318 schorsch 2.4 SET_FILE_BINARY(stdout);
319 greg 2.24 readoct(octname = octnm, loadflags, &thescene, NULL);
320 greg 2.1 nsceneobjs = nobjects;
321    
322     if (loadflags & IO_INFO) { /* print header */
323     printargs(i, argv, stdout);
324     printf("SOFTWARE= %s\n", VersionID);
325     fputnow(stdout);
326     fputformat(formstr(outform), stdout);
327     putchar('\n');
328     }
329 greg 2.23
330     ray_init_pmap(); /* PMAP: set up & load photon maps */
331    
332 greg 2.1 marksources(); /* find and mark sources */
333    
334     setambient(); /* initialize ambient calculation */
335 greg 2.23
336 greg 2.1 #ifdef PERSIST
337     if (persist) {
338     fflush(stdout);
339     /* reconnect stdout */
340     dup2(duped1, fileno(stdout));
341     close(duped1);
342     if (persist == PARALLEL) { /* multiprocessing */
343     preload_objs(); /* preload scene */
344     shm_boundary = (char *)malloc(16);
345     strcpy(shm_boundary, "SHM_BOUNDARY");
346     while ((rval=fork()) == 0) { /* keep on forkin' */
347     pflock(1);
348     pfhold();
349 greg 2.15 ambsync(); /* load new values */
350 greg 2.1 }
351     if (rval < 0)
352     error(SYSTEM, "cannot fork child for persist function");
353 greg 2.15 pfdetach(); /* parent will run then exit */
354 greg 2.1 }
355     }
356     runagain:
357     if (persist)
358     dupheader(); /* send header to stdout */
359     #endif
360     /* trace rays */
361 greg 2.19 rtrace(NULL, nproc);
362 greg 2.1 /* flush ambient file */
363     ambsync();
364     #ifdef PERSIST
365     if (persist == PERSIST) { /* first run-through */
366     if ((rval=fork()) == 0) { /* child loops until killed */
367     pflock(1);
368     persist = PCHILD;
369     } else { /* original process exits */
370     if (rval < 0)
371     error(SYSTEM, "cannot fork child for persist function");
372     pfdetach(); /* parent exits */
373     }
374     }
375     if (persist == PCHILD) { /* wait for a signal then go again */
376     pfhold();
377     raynum = nrays = 0; /* reinitialize */
378     goto runagain;
379     }
380     #endif
381 greg 2.23
382     ray_done_pmap(); /* PMAP: free photon maps */
383    
384 greg 2.1 quit(0);
385    
386     badopt:
387     sprintf(errmsg, "command line error at '%s'", argv[i]);
388     error(USER, errmsg);
389 schorsch 2.9 return 1; /* pro forma return */
390 greg 2.1
391     #undef check
392     #undef bool
393     }
394    
395    
396     void
397 schorsch 2.9 wputs( /* warning output function */
398     char *s
399     )
400 greg 2.1 {
401     int lasterrno = errno;
402     eputs(s);
403     errno = lasterrno;
404     }
405    
406    
407     void
408 schorsch 2.9 eputs( /* put string to stderr */
409     register char *s
410     )
411 greg 2.1 {
412     static int midline = 0;
413    
414     if (!*s)
415     return;
416     if (!midline++) {
417     fputs(progname, stderr);
418     fputs(": ", stderr);
419     }
420     fputs(s, stderr);
421     if (s[strlen(s)-1] == '\n') {
422     fflush(stderr);
423     midline = 0;
424     }
425     }
426    
427    
428 schorsch 2.9 static void
429     onsig( /* fatal signal */
430     int signo
431     )
432 greg 2.1 {
433     static int gotsig = 0;
434    
435     if (gotsig++) /* two signals and we're gone! */
436     _exit(signo);
437    
438 schorsch 2.5 #ifdef SIGALRM
439 greg 2.1 alarm(15); /* allow 15 seconds to clean up */
440     signal(SIGALRM, SIG_DFL); /* make certain we do die */
441 schorsch 2.5 #endif
442 greg 2.1 eputs("signal - ");
443     eputs(sigerr[signo]);
444     eputs("\n");
445     quit(3);
446     }
447    
448    
449 schorsch 2.9 static void
450     sigdie( /* set fatal signal */
451     int signo,
452     char *msg
453     )
454 greg 2.1 {
455     if (signal(signo, onsig) == SIG_IGN)
456     signal(signo, SIG_IGN);
457     sigerr[signo] = msg;
458     }
459    
460    
461 schorsch 2.9 static void
462 schorsch 2.8 printdefaults(void) /* print default values to stdout */
463 greg 2.1 {
464     register char *cp;
465    
466     if (imm_irrad)
467     printf("-I+\t\t\t\t# immediate irradiance on\n");
468 greg 2.18 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
469 greg 2.21 printf("-x %-9d\t\t\t# %s\n", hresolu,
470     vresolu && hresolu ? "x resolution" : "flush interval");
471 greg 2.16 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
472 greg 2.1 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
473     "-ld-\t\t\t\t# limit distance off\n");
474 greg 2.10 printf("-h%c\t\t\t\t# %s header\n", loadflags & IO_INFO ? '+' : '-',
475     loadflags & IO_INFO ? "output" : "no");
476 greg 2.1 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
477     inform, outform, formstr(inform), formstr(outform));
478 greg 2.10 printf("-o%-9s\t\t\t# output", outvals);
479 greg 2.1 for (cp = outvals; *cp; cp++)
480     switch (*cp) {
481 greg 2.10 case 't': case 'T': printf(" trace"); break;
482 greg 2.1 case 'o': printf(" origin"); break;
483     case 'd': printf(" direction"); break;
484     case 'v': printf(" value"); break;
485 greg 2.13 case 'V': printf(" contribution"); break;
486 greg 2.1 case 'l': printf(" length"); break;
487     case 'L': printf(" first_length"); break;
488     case 'p': printf(" point"); break;
489     case 'n': printf(" normal"); break;
490     case 'N': printf(" unperturbed_normal"); break;
491     case 's': printf(" surface"); break;
492     case 'w': printf(" weight"); break;
493 greg 2.13 case 'W': printf(" coefficient"); break;
494 greg 2.1 case 'm': printf(" modifier"); break;
495 greg 2.10 case 'M': printf(" material"); break;
496     case '-': printf(" stroke"); break;
497 greg 2.1 }
498     putchar('\n');
499     printf(erract[WARNING].pf != NULL ?
500     "-w+\t\t\t\t# warning messages on\n" :
501     "-w-\t\t\t\t# warning messages off\n");
502     print_rdefaults();
503     }