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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rtmain.c,v 1.3 2014/11/25 17:45:46 taschreg Exp taschreg $";
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(); /* string from format */
39 int inform = 'a'; /* input format */
40 int outform = 'a'; /* output format */
41 char *outvals = "v"; /* output specification */
42
43 int hresolu = 0; /* horizontal (scan) size */
44 int vresolu = 0; /* vertical resolution */
45
46 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
53 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
59 static int loadflags = ~IO_FILES; /* what to load from octree */
60
61 static void onsig(int signo);
62 static void sigdie(int signo, char *msg);
63 static void printdefaults(void);
64
65
66 int
67 main(int argc, char *argv[])
68 {
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 char *octnm = NULL;
81 char **tralp = NULL;
82 int duped1 = -1;
83 int rval;
84 int i;
85 /* global program name */
86 progname = argv[0] = fixargv0(argv[0]);
87 /* add trace notify function */
88 for (i = 0; addobjnotify[i] != NULL; i++)
89 ;
90 addobjnotify[i] = tranotify;
91 /* set our defaults */
92 rand_samp = 1;
93 maxdepth = -10;
94 minweight = 2e-3;
95 /* 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 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 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 getpath(argv[++i],getrlibpath(),R_OK));
199 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 getpath(argv[++i],getrlibpath(),R_OK));
221 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 if (nproc > 1) {
254 if (persist)
255 error(USER, "multiprocessing incompatible with persist file");
256 if (!vresolu && hresolu > 0 && hresolu < nproc)
257 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 /* initialize object types */
262 initotypes();
263 /* initialize urand */
264 if (rand_samp) {
265 srandom((long)time(0));
266 initurand(0);
267 } else {
268 srandom(0L);
269 initurand(2048);
270 }
271 /* set up signal handling */
272 sigdie(SIGINT, "Interrupt");
273 #ifdef SIGHUP
274 sigdie(SIGHUP, "Hangup");
275 #endif
276 sigdie(SIGTERM, "Terminate");
277 #ifdef SIGPIPE
278 sigdie(SIGPIPE, "Broken pipe");
279 #endif
280 #ifdef SIGALRM
281 sigdie(SIGALRM, "Alarm clock");
282 #endif
283 #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 octnm = NULL;
303 else if (i == argc-1)
304 octnm = argv[i];
305 else
306 goto badopt;
307 if (octnm == NULL)
308 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 SET_FILE_BINARY(stdout);
318 readoct(octnm, loadflags, &thescene, NULL);
319 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
329 ray_init_pmap(); /* PMAP: set up & load photon maps */
330
331 marksources(); /* find and mark sources */
332
333 setambient(); /* initialize ambient calculation */
334
335 #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 ambsync(); /* load new values */
349 }
350 if (rval < 0)
351 error(SYSTEM, "cannot fork child for persist function");
352 pfdetach(); /* parent will run then exit */
353 }
354 }
355 runagain:
356 if (persist)
357 dupheader(); /* send header to stdout */
358 #endif
359 /* trace rays */
360 rtrace(NULL, nproc);
361 /* 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
381 ray_done_pmap(); /* PMAP: free photon maps */
382
383 quit(0);
384
385 badopt:
386 sprintf(errmsg, "command line error at '%s'", argv[i]);
387 error(USER, errmsg);
388 return 1; /* pro forma return */
389
390 #undef check
391 #undef bool
392 }
393
394
395 void
396 wputs( /* warning output function */
397 char *s
398 )
399 {
400 int lasterrno = errno;
401 eputs(s);
402 errno = lasterrno;
403 }
404
405
406 void
407 eputs( /* put string to stderr */
408 register char *s
409 )
410 {
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 static void
428 onsig( /* fatal signal */
429 int signo
430 )
431 {
432 static int gotsig = 0;
433
434 if (gotsig++) /* two signals and we're gone! */
435 _exit(signo);
436
437 #ifdef SIGALRM
438 alarm(15); /* allow 15 seconds to clean up */
439 signal(SIGALRM, SIG_DFL); /* make certain we do die */
440 #endif
441 eputs("signal - ");
442 eputs(sigerr[signo]);
443 eputs("\n");
444 quit(3);
445 }
446
447
448 static void
449 sigdie( /* set fatal signal */
450 int signo,
451 char *msg
452 )
453 {
454 if (signal(signo, onsig) == SIG_IGN)
455 signal(signo, SIG_IGN);
456 sigerr[signo] = msg;
457 }
458
459
460 static void
461 printdefaults(void) /* print default values to stdout */
462 {
463 register char *cp;
464
465 if (imm_irrad)
466 printf("-I+\t\t\t\t# immediate irradiance on\n");
467 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
468 printf("-x %-9d\t\t\t# %s\n", hresolu,
469 vresolu && hresolu ? "x resolution" : "flush interval");
470 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
471 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
472 "-ld-\t\t\t\t# limit distance off\n");
473 printf("-h%c\t\t\t\t# %s header\n", loadflags & IO_INFO ? '+' : '-',
474 loadflags & IO_INFO ? "output" : "no");
475 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
476 inform, outform, formstr(inform), formstr(outform));
477 printf("-o%-9s\t\t\t# output", outvals);
478 for (cp = outvals; *cp; cp++)
479 switch (*cp) {
480 case 't': case 'T': printf(" trace"); break;
481 case 'o': printf(" origin"); break;
482 case 'd': printf(" direction"); break;
483 case 'v': printf(" value"); break;
484 case 'V': printf(" contribution"); break;
485 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 case 'W': printf(" coefficient"); break;
493 case 'm': printf(" modifier"); break;
494 case 'M': printf(" material"); break;
495 case '-': printf(" stroke"); break;
496 }
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 }