ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpmain.c
Revision: 2.18
Committed: Thu Mar 10 18:25:46 2016 UTC (8 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad5R1
Changes since 2.17: +4 -4 lines
Log Message:
newer C++ compilers have a standard type named "bool". rename macro to get rid of warnings.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rpmain.c,v 2.17 2016/03/06 01:13:18 schorsch Exp $";
3 #endif
4 /*
5 * rpmain.c - main for rpict batch rendering program
6 */
7
8 #include "copyright.h"
9
10 #include <time.h>
11 #include <signal.h>
12
13 #include "platform.h"
14 #include "rtprocess.h" /* getpid() */
15 #include "ray.h"
16 #include "source.h"
17 #include "ambient.h"
18 #include "random.h"
19 #include "paths.h"
20 #include "view.h"
21 #include "pmapray.h"
22
23 /* persistent processes define */
24 #ifdef F_SETLKW
25 #define PERSIST 1 /* normal persist */
26 #define PARALLEL 2 /* parallel persist */
27 #define PCHILD 3 /* child of normal persist */
28 #endif
29
30 char *progname; /* argv[0] */
31 char *octname; /* octree name */
32 char *sigerr[NSIG]; /* signal error messages */
33 char *shm_boundary = NULL; /* boundary of shared memory */
34 char *errfile = NULL; /* error output file */
35
36 extern time_t time();
37 extern time_t tstart; /* start time */
38
39 extern int ralrm; /* seconds between reports */
40
41 extern VIEW ourview; /* viewing parameters */
42
43 extern int hresolu; /* horizontal resolution */
44 extern int vresolu; /* vertical resolution */
45 extern double pixaspect; /* pixel aspect ratio */
46
47 extern int psample; /* pixel sample size */
48 extern double maxdiff; /* max. sample difference */
49 extern double dstrpix; /* square pixel distribution */
50
51 extern double mblur; /* motion blur parameter */
52
53 extern double dblur; /* depth-of-field blur parameter */
54
55 static void onsig(int signo);
56 static void sigdie(int signo, char *msg);
57 static void printdefaults(void);
58
59
60 int
61 main(int argc, char *argv[])
62 {
63 #define check(ol,al) if (argv[i][ol] || \
64 badarg(argc-i-1,argv+i+1,al)) \
65 goto badopt
66 #define check_bool(olen,var) switch (argv[i][olen]) { \
67 case '\0': var = !var; break; \
68 case 'y': case 'Y': case 't': case 'T': \
69 case '+': case '1': var = 1; break; \
70 case 'n': case 'N': case 'f': case 'F': \
71 case '-': case '0': var = 0; break; \
72 default: goto badopt; }
73 char *err;
74 char *recover = NULL;
75 char *outfile = NULL;
76 char *zfile = NULL;
77 int loadflags = ~IO_FILES;
78 int seqstart = 0;
79 int persist = 0;
80 int duped1 = -1;
81 int rval;
82 int i;
83 /* record start time */
84 tstart = time((time_t *)NULL);
85 /* global program name */
86 progname = argv[0] = fixargv0(argv[0]);
87 /* option city */
88 for (i = 1; i < argc; i++) {
89 /* expand arguments */
90 while ((rval = expandarg(&argc, &argv, i)) > 0)
91 ;
92 if (rval < 0) {
93 sprintf(errmsg, "cannot expand '%s'", argv[i]);
94 error(SYSTEM, errmsg);
95 }
96 if (argv[i] == NULL || argv[i][0] != '-')
97 break; /* break from options */
98 if (!strcmp(argv[i], "-version")) {
99 puts(VersionID);
100 quit(0);
101 }
102 if (!strcmp(argv[i], "-defaults") ||
103 !strcmp(argv[i], "-help")) {
104 printdefaults();
105 quit(0);
106 }
107 rval = getrenderopt(argc-i, argv+i);
108 if (rval >= 0) {
109 i += rval;
110 continue;
111 }
112 rval = getviewopt(&ourview, argc-i, argv+i);
113 if (rval >= 0) {
114 i += rval;
115 continue;
116 }
117 /* rpict options */
118 switch (argv[i][1]) {
119 case 'v': /* view file */
120 if (argv[i][2] != 'f')
121 goto badopt;
122 check(3,"s");
123 rval = viewfile(argv[++i], &ourview, NULL);
124 if (rval < 0) {
125 sprintf(errmsg,
126 "cannot open view file \"%s\"",
127 argv[i]);
128 error(SYSTEM, errmsg);
129 } else if (rval == 0) {
130 sprintf(errmsg,
131 "bad view file \"%s\"",
132 argv[i]);
133 error(USER, errmsg);
134 }
135 break;
136 case 'p': /* pixel */
137 switch (argv[i][2]) {
138 case 's': /* sample */
139 check(3,"i");
140 psample = atoi(argv[++i]);
141 break;
142 case 't': /* threshold */
143 check(3,"f");
144 maxdiff = atof(argv[++i]);
145 break;
146 case 'j': /* jitter */
147 check(3,"f");
148 dstrpix = atof(argv[++i]);
149 break;
150 case 'a': /* aspect */
151 check(3,"f");
152 pixaspect = atof(argv[++i]);
153 break;
154 case 'm': /* motion */
155 check(3,"f");
156 mblur = atof(argv[++i]);
157 break;
158 case 'd': /* aperture */
159 check(3,"f");
160 dblur = atof(argv[++i]);
161 break;
162 default:
163 goto badopt;
164 }
165 break;
166 case 'x': /* x resolution */
167 check(2,"i");
168 hresolu = atoi(argv[++i]);
169 break;
170 case 'y': /* y resolution */
171 check(2,"i");
172 vresolu = atoi(argv[++i]);
173 break;
174 case 'S': /* slave index */
175 check(2,"i");
176 seqstart = atoi(argv[++i]);
177 break;
178 case 'o': /* output file */
179 check(2,"s");
180 outfile = argv[++i];
181 break;
182 case 'z': /* z file */
183 check(2,"s");
184 zfile = argv[++i];
185 break;
186 case 'r': /* recover file */
187 if (argv[i][2] == 'o') { /* +output */
188 check(3,"s");
189 outfile = argv[i+1];
190 } else
191 check(2,"s");
192 recover = argv[++i];
193 break;
194 case 't': /* timer */
195 check(2,"i");
196 ralrm = atoi(argv[++i]);
197 break;
198 #ifdef PERSIST
199 case 'P': /* persist file */
200 if (argv[i][2] == 'P') {
201 check(3,"s");
202 persist = PARALLEL;
203 } else {
204 check(2,"s");
205 persist = PERSIST;
206 }
207 persistfile(argv[++i]);
208 break;
209 #endif
210 case 'w': /* warnings */
211 rval = erract[WARNING].pf != NULL;
212 check_bool(2,rval);
213 if (rval) erract[WARNING].pf = wputs;
214 else erract[WARNING].pf = NULL;
215 break;
216 case 'e': /* error file */
217 check(2,"s");
218 errfile = argv[++i];
219 break;
220 default:
221 goto badopt;
222 }
223 }
224 err = setview(&ourview); /* set viewing parameters */
225 if (err != NULL)
226 error(USER, err);
227 /* initialize object types */
228 initotypes();
229 /* initialize urand */
230 if (rand_samp) {
231 srandom((long)time(0));
232 initurand(0);
233 } else {
234 srandom(0L);
235 initurand(2048);
236 }
237 /* set up signal handling */
238 sigdie(SIGINT, "Interrupt");
239 #ifdef SIGHUP
240 sigdie(SIGHUP, "Hangup");
241 #endif
242 sigdie(SIGTERM, "Terminate");
243 #ifdef SIGPIPE
244 sigdie(SIGPIPE, "Broken pipe");
245 #endif
246 #ifdef SIGALRM
247 sigdie(SIGALRM, "Alarm clock");
248 #endif
249 #ifdef SIGXCPU
250 sigdie(SIGXCPU, "CPU limit exceeded");
251 sigdie(SIGXFSZ, "File size exceeded");
252 #endif
253 /* open error file */
254 if (errfile != NULL) {
255 if (freopen(errfile, "a", stderr) == NULL)
256 quit(2);
257 fprintf(stderr, "**************\n*** PID %5d: ",
258 getpid());
259 printargs(argc, argv, stderr);
260 putc('\n', stderr);
261 fflush(stderr);
262 }
263 #ifdef NICE
264 nice(NICE); /* lower priority */
265 #endif
266 /* get octree */
267 if (i == argc)
268 octname = NULL;
269 else if (i == argc-1)
270 octname = argv[i];
271 else
272 goto badopt;
273 if (seqstart > 0 && octname == NULL)
274 error(USER, "missing octree argument");
275 /* set up output */
276 #ifdef PERSIST
277 if (persist) {
278 if (recover != NULL)
279 error(USER, "persist option used with recover file");
280 if (seqstart <= 0)
281 error(USER, "persist option only for sequences");
282 if (outfile == NULL)
283 duped1 = dup(fileno(stdout)); /* don't lose our output */
284 openheader();
285 } else
286 #endif
287 if (outfile != NULL)
288 openheader();
289 #if defined(_WIN32) || defined(_WIN64)
290 SET_FILE_BINARY(stdout);
291 if (octname == NULL)
292 SET_FILE_BINARY(stdin);
293 #endif
294 readoct(octname, loadflags, &thescene, NULL);
295 nsceneobjs = nobjects;
296
297 if (loadflags & IO_INFO) { /* print header */
298 printargs(i, argv, stdout);
299 printf("SOFTWARE= %s\n", VersionID);
300 }
301
302 ray_init_pmap(); /* PMAP: set up & load photon maps */
303
304 marksources(); /* find and mark sources */
305
306 setambient(); /* initialize ambient calculation */
307
308 #ifdef PERSIST
309 if (persist) {
310 fflush(stdout);
311 if (outfile == NULL) { /* reconnect stdout */
312 dup2(duped1, fileno(stdout));
313 close(duped1);
314 }
315 if (persist == PARALLEL) { /* multiprocessing */
316 preload_objs(); /* preload scene */
317 shm_boundary = (char *)malloc(16);
318 strcpy(shm_boundary, "SHM_BOUNDARY");
319 while ((rval=fork()) == 0) { /* keep on forkin' */
320 pflock(1);
321 pfhold();
322 tstart = time((time_t *)NULL);
323 ambsync(); /* load new values */
324 }
325 if (rval < 0)
326 error(SYSTEM, "cannot fork child for persist function");
327 pfdetach(); /* parent will run then exit */
328 }
329 }
330 runagain:
331 if (persist) {
332 if (outfile == NULL) /* if out to stdout */
333 dupheader(); /* send header */
334 else /* if out to file */
335 duped1 = dup(fileno(stdout)); /* hang onto pipe */
336 }
337 #endif
338 /* batch render picture(s) */
339 rpict(seqstart, outfile, zfile, recover);
340 /* flush ambient file */
341 ambsync();
342 #ifdef PERSIST
343 if (persist == PERSIST) { /* first run-through */
344 if ((rval=fork()) == 0) { /* child loops until killed */
345 pflock(1);
346 persist = PCHILD;
347 } else { /* original process exits */
348 if (rval < 0)
349 error(SYSTEM, "cannot fork child for persist function");
350 pfdetach(); /* parent exits */
351 }
352 }
353 if (persist == PCHILD) { /* wait for a signal then go again */
354 if (outfile != NULL)
355 close(duped1); /* release output handle */
356 pfhold();
357 tstart = time((time_t *)NULL); /* reinitialize */
358 raynum = nrays = 0;
359 goto runagain;
360 }
361 #endif
362
363
364 ray_done_pmap(); /* PMAP: free photon maps */
365
366 quit(0);
367
368 badopt:
369 sprintf(errmsg, "command line error at '%s'", argv[i]);
370 error(USER, errmsg);
371 return 1; /* pro forma return */
372
373 #undef check
374 #undef check_bool
375 }
376
377
378 void
379 wputs( /* warning output function */
380 char *s
381 )
382 {
383 int lasterrno = errno;
384 eputs(s);
385 errno = lasterrno;
386 }
387
388
389 void
390 eputs( /* put string to stderr */
391 register char *s
392 )
393 {
394 static int midline = 0;
395
396 if (!*s)
397 return;
398 if (!midline++) {
399 fputs(progname, stderr);
400 fputs(": ", stderr);
401 }
402 fputs(s, stderr);
403 if (s[strlen(s)-1] == '\n') {
404 fflush(stderr);
405 midline = 0;
406 }
407 }
408
409
410 static void
411 onsig( /* fatal signal */
412 int signo
413 )
414 {
415 static int gotsig = 0;
416
417 if (gotsig++) /* two signals and we're gone! */
418 _exit(signo);
419
420 #ifdef SIGALRM /* XXX how critical is this? */
421 alarm(15); /* allow 15 seconds to clean up */
422 signal(SIGALRM, SIG_DFL); /* make certain we do die */
423 #endif
424 eputs("signal - ");
425 eputs(sigerr[signo]);
426 eputs("\n");
427 quit(3);
428 }
429
430
431 static void
432 sigdie( /* set fatal signal */
433 int signo,
434 char *msg
435 )
436 {
437 if (signal(signo, onsig) == SIG_IGN)
438 signal(signo, SIG_IGN);
439 sigerr[signo] = msg;
440 }
441
442
443 static void
444 printdefaults(void) /* print default values to stdout */
445 {
446 printf("-vt%c\t\t\t\t# view type %s\n", ourview.type,
447 ourview.type==VT_PER ? "perspective" :
448 ourview.type==VT_PAR ? "parallel" :
449 ourview.type==VT_HEM ? "hemispherical" :
450 ourview.type==VT_ANG ? "angular" :
451 ourview.type==VT_CYL ? "cylindrical" :
452 ourview.type==VT_PLS ? "planisphere" :
453 "unknown");
454 printf("-vp %f %f %f\t# view point\n",
455 ourview.vp[0], ourview.vp[1], ourview.vp[2]);
456 printf("-vd %f %f %f\t# view direction\n",
457 ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
458 printf("-vu %f %f %f\t# view up\n",
459 ourview.vup[0], ourview.vup[1], ourview.vup[2]);
460 printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz);
461 printf("-vv %f\t\t\t# view vertical size\n", ourview.vert);
462 printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore);
463 printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft);
464 printf("-vs %f\t\t\t# view shift\n", ourview.hoff);
465 printf("-vl %f\t\t\t# view lift\n", ourview.voff);
466 printf("-x %-9d\t\t\t# x resolution\n", hresolu);
467 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
468 printf("-pa %f\t\t\t# pixel aspect ratio\n", pixaspect);
469 printf("-pj %f\t\t\t# pixel jitter\n", dstrpix);
470 printf("-pm %f\t\t\t# pixel motion\n", mblur);
471 printf("-pd %f\t\t\t# pixel depth-of-field\n", dblur);
472 printf("-ps %-9d\t\t\t# pixel sample\n", psample);
473 printf("-pt %f\t\t\t# pixel threshold\n", maxdiff);
474 printf("-t %-9d\t\t\t# time between reports\n", ralrm);
475 printf(erract[WARNING].pf != NULL ?
476 "-w+\t\t\t\t# warning messages on\n" :
477 "-w-\t\t\t\t# warning messages off\n");
478 print_rdefaults();
479 }