ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.23
Committed: Tue Feb 24 19:39:27 2015 UTC (9 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.22: +9 -3 lines
Log Message:
Initial check-in of photon map addition by Roland Schregle

File Contents

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