ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpmain.c
Revision: 2.28
Committed: Wed Nov 15 18:02:53 2023 UTC (6 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.27: +73 -3 lines
Log Message:
feat(rpict,rtrace,rcontrib,rtpict): Hyperspectral rendering (except photon map)

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rpmain.c,v 2.27 2023/08/15 01:19:37 greg 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 RGBPRIMP out_prims = stdprims; /* output color primitives */
56 static RGBPRIMS our_prims; /* private output color primitives */
57
58 static void onsig(int signo);
59 static void sigdie(int signo, char *msg);
60 static void printdefaults(void);
61 /* rpict additional features */
62 #ifdef PERSIST
63 #define RPICT_FEATURES "Persist\nParallelPersist\n" \
64 "Hyperspectral\nParticipatingMedia=Mist\n" \
65 "Recovery\nIrradianceCalc\nViewTypes=v,l,a,h,s,c\n" \
66 "HessianAmbientCache\nAmbientAveraging\nAmbientValueSharing\n" \
67 "PixelJitter\nPixelSampling\nPixelMotion\nPixelDepthOfField\n" \
68 "SmallSourceDrawing\nViewSequence\nProgressReporting\n" \
69 "AdaptiveShadowTesting\nOutputs=v,l\n"
70 #else
71 #define RPICT_FEATURES "Recovery\nIrradianceCalc\nViewTypes=v,l,a,h,s,c\n" \
72 "Hyperspectral\nParticipatingMedia=Mist\n" \
73 "HessianAmbientCache\nAmbientAveraging\nAmbientValueSharing\n" \
74 "PixelJitter\nPixelSampling\nPixelMotion\nPixelDepthOfField\n" \
75 "SmallSourceDrawing\nViewSequence\nProgressReporting\n" \
76 "AdaptiveShadowTesting\nOutputs=v,l\n"
77 #endif
78
79
80 int
81 main(int argc, char *argv[])
82 {
83 #define check(ol,al) if (argv[i][ol] || \
84 badarg(argc-i-1,argv+i+1,al)) \
85 goto badopt
86 #define check_bool(olen,var) switch (argv[i][olen]) { \
87 case '\0': var = !var; break; \
88 case 'y': case 'Y': case 't': case 'T': \
89 case '+': case '1': var = 1; break; \
90 case 'n': case 'N': case 'f': case 'F': \
91 case '-': case '0': var = 0; break; \
92 default: goto badopt; }
93 char *err;
94 char *recover = NULL;
95 char *outfile = NULL;
96 char *zfile = NULL;
97 int loadflags = ~IO_FILES;
98 int seqstart = 0;
99 int persist = 0;
100 int duped1 = -1;
101 int rval;
102 int i;
103 /* record start time */
104 tstart = time((time_t *)NULL);
105 /* global program name */
106 progname = argv[0] = fixargv0(argv[0]);
107 /* feature check only? */
108 strcat(RFeatureList, RPICT_FEATURES);
109 if (argc > 1 && !strcmp(argv[1], "-features"))
110 return feature_status(argc-2, argv+2);
111 /* option city */
112 for (i = 1; i < argc; i++) {
113 /* expand arguments */
114 while ((rval = expandarg(&argc, &argv, i)) > 0)
115 ;
116 if (rval < 0) {
117 sprintf(errmsg, "cannot expand '%s'", argv[i]);
118 error(SYSTEM, errmsg);
119 }
120 if (argv[i] == NULL || argv[i][0] != '-')
121 break; /* break from options */
122 if (!strcmp(argv[i], "-version")) {
123 puts(VersionID);
124 quit(0);
125 }
126 if (!strcmp(argv[i], "-defaults") ||
127 !strcmp(argv[i], "-help")) {
128 printdefaults();
129 quit(0);
130 }
131 rval = getrenderopt(argc-i, argv+i);
132 if (rval >= 0) {
133 i += rval;
134 continue;
135 }
136 rval = getviewopt(&ourview, argc-i, argv+i);
137 if (rval >= 0) {
138 i += rval;
139 continue;
140 }
141 /* rpict options */
142 switch (argv[i][1]) {
143 case 'v': /* view file */
144 if (argv[i][2] != 'f')
145 goto badopt;
146 check(3,"s");
147 rval = viewfile(argv[++i], &ourview, NULL);
148 if (rval < 0) {
149 sprintf(errmsg,
150 "cannot open view file \"%s\"",
151 argv[i]);
152 error(SYSTEM, errmsg);
153 } else if (rval == 0) {
154 sprintf(errmsg,
155 "bad view file \"%s\"",
156 argv[i]);
157 error(USER, errmsg);
158 }
159 break;
160 case 'p': /* pixel */
161 switch (argv[i][2]) {
162 case 's': /* sample */
163 check(3,"i");
164 psample = atoi(argv[++i]);
165 break;
166 case 't': /* threshold */
167 check(3,"f");
168 maxdiff = atof(argv[++i]);
169 break;
170 case 'j': /* jitter */
171 check(3,"f");
172 dstrpix = atof(argv[++i]);
173 break;
174 case 'a': /* aspect */
175 check(3,"f");
176 pixaspect = atof(argv[++i]);
177 break;
178 case 'm': /* motion */
179 check(3,"f");
180 mblur = atof(argv[++i]);
181 break;
182 case 'd': /* aperture */
183 check(3,"f");
184 dblur = atof(argv[++i]);
185 break;
186 case 'R': /* standard RGB output */
187 if (strcmp(argv[i]+2, "RGB"))
188 goto badopt;
189 out_prims = stdprims;
190 break;
191 case 'X': /* XYZ output */
192 if (strcmp(argv[i]+2, "XYZ"))
193 goto badopt;
194 out_prims = xyzprims;
195 break;
196 case 'c': { /* chromaticities */
197 int j;
198 check(3,"ffffffff");
199 rval = 0;
200 for (j = 0; j < 8; j++) {
201 our_prims[0][j] = atof(argv[++i]);
202 rval |= fabs(our_prims[0][j]-stdprims[0][j]) > .001;
203 }
204 if (rval) {
205 if (!colorprimsOK(our_prims))
206 error(USER, "illegal primary chromaticities");
207 out_prims = our_prims;
208 } else
209 out_prims = stdprims;
210 } break;
211 default:
212 goto badopt;
213 }
214 break;
215 case 'x': /* x resolution */
216 check(2,"i");
217 hresolu = atoi(argv[++i]);
218 break;
219 case 'y': /* y resolution */
220 check(2,"i");
221 vresolu = atoi(argv[++i]);
222 break;
223 case 'S': /* slave index */
224 check(2,"i");
225 seqstart = atoi(argv[++i]);
226 break;
227 case 'o': /* output file */
228 check(2,"s");
229 outfile = argv[++i];
230 break;
231 case 'z': /* z file */
232 check(2,"s");
233 zfile = argv[++i];
234 break;
235 case 'r': /* recover file */
236 if (argv[i][2] == 'o') { /* +output */
237 check(3,"s");
238 outfile = argv[i+1];
239 } else
240 check(2,"s");
241 recover = argv[++i];
242 break;
243 case 't': /* timer */
244 check(2,"i");
245 ralrm = atoi(argv[++i]);
246 break;
247 #if MAXCSAMP>3
248 case 'c': /* spectral sampling */
249 switch (argv[i][2]) {
250 case 's': /* spectral bin count */
251 check(3,"i");
252 NCSAMP = atoi(argv[++i]);
253 break;
254 case 'w': /* wavelength extrema */
255 check(3,"ff");
256 WLPART[0] = atof(argv[++i]);
257 WLPART[3] = atof(argv[++i]);
258 break;
259 #if 0
260 case 'o': /* output spectral results */
261 rval = (out_prims == NULL);
262 check_bool(3,rval);
263 if (rval) out_prims = NULL;
264 else if (out_prims == NULL) out_prims = stdprims;
265 break;
266 #endif
267 default:
268 goto badopt;
269 }
270 break;
271 #endif
272 #ifdef PERSIST
273 case 'P': /* persist file */
274 if (argv[i][2] == 'P') {
275 check(3,"s");
276 persist = PARALLEL;
277 } else {
278 check(2,"s");
279 persist = PERSIST;
280 }
281 persistfile(argv[++i]);
282 break;
283 #endif
284 case 'w': /* warnings */
285 rval = erract[WARNING].pf != NULL;
286 check_bool(2,rval);
287 if (rval) erract[WARNING].pf = wputs;
288 else erract[WARNING].pf = NULL;
289 break;
290 case 'e': /* error file */
291 check(2,"s");
292 errfile = argv[++i];
293 break;
294 default:
295 goto badopt;
296 }
297 }
298 err = setview(&ourview); /* set viewing parameters */
299 if (err != NULL)
300 error(USER, err);
301 /* initialize object types */
302 initotypes();
303 /* initialize urand */
304 if (rand_samp) {
305 srandom((long)time(0));
306 initurand(0);
307 } else {
308 srandom(0L);
309 initurand(2048);
310 }
311 /* set up signal handling */
312 sigdie(SIGINT, "Interrupt");
313 #ifdef SIGHUP
314 sigdie(SIGHUP, "Hangup");
315 #endif
316 sigdie(SIGTERM, "Terminate");
317 #ifdef SIGPIPE
318 sigdie(SIGPIPE, "Broken pipe");
319 #endif
320 #ifdef SIGALRM
321 sigdie(SIGALRM, "Alarm clock");
322 #endif
323 #ifdef SIGXCPU
324 sigdie(SIGXCPU, "CPU limit exceeded");
325 sigdie(SIGXFSZ, "File size exceeded");
326 #endif
327 /* open error file */
328 if (errfile != NULL) {
329 if (freopen(errfile, "a", stderr) == NULL)
330 quit(2);
331 fprintf(stderr, "**************\n*** PID %5d: ",
332 getpid());
333 printargs(argc, argv, stderr);
334 putc('\n', stderr);
335 fflush(stderr);
336 }
337 #ifdef NICE
338 nice(NICE); /* lower priority */
339 #endif
340 /* get octree */
341 if (i == argc)
342 octname = NULL;
343 else if (i == argc-1)
344 octname = argv[i];
345 else
346 goto badopt;
347 if (seqstart > 0 && octname == NULL)
348 error(USER, "missing octree argument");
349 /* set up output */
350 #ifdef PERSIST
351 if (persist) {
352 if (recover != NULL)
353 error(USER, "persist option used with recover file");
354 if (seqstart <= 0)
355 error(USER, "persist option only for sequences");
356 if (outfile == NULL)
357 duped1 = dup(fileno(stdout)); /* don't lose our output */
358 openheader();
359 } else
360 #endif
361 if (outfile != NULL)
362 openheader();
363 SET_FILE_BINARY(stdout);
364 if (octname == NULL)
365 SET_FILE_BINARY(stdin);
366 readoct(octname, loadflags, &thescene, NULL);
367 nsceneobjs = nobjects;
368
369 if (loadflags & IO_INFO) { /* print header */
370 printargs(i, argv, stdout);
371 printf("SOFTWARE= %s\n", VersionID);
372 }
373
374 ray_init_pmap(); /* PMAP: set up & load photon maps */
375
376 marksources(); /* find and mark sources */
377
378 setambient(); /* initialize ambient calculation */
379
380 fflush(stdout); /* in case we're duplicating header */
381
382 #ifdef PERSIST
383 if (persist) {
384 if (outfile == NULL) { /* reconnect stdout */
385 dup2(duped1, fileno(stdout));
386 close(duped1);
387 }
388 if (persist == PARALLEL) { /* multiprocessing */
389 preload_objs(); /* preload scene */
390 shm_boundary = (char *)malloc(16);
391 strcpy(shm_boundary, "SHM_BOUNDARY");
392 while ((rval=fork()) == 0) { /* keep on forkin' */
393 pflock(1);
394 pfhold();
395 tstart = time((time_t *)NULL);
396 ambsync(); /* load new values */
397 }
398 if (rval < 0)
399 error(SYSTEM, "cannot fork child for persist function");
400 pfdetach(); /* parent will run then exit */
401 }
402 }
403 runagain:
404 if (persist) {
405 if (outfile == NULL) /* if out to stdout */
406 dupheader(); /* send header */
407 else /* if out to file */
408 duped1 = dup(fileno(stdout)); /* hang onto pipe */
409 }
410 #endif
411 /* batch render picture(s) */
412 rpict(seqstart, outfile, zfile, recover);
413 /* flush ambient file */
414 ambsync();
415 #ifdef PERSIST
416 if (persist == PERSIST) { /* first run-through */
417 if ((rval=fork()) == 0) { /* child loops until killed */
418 pflock(1);
419 persist = PCHILD;
420 } else { /* original process exits */
421 if (rval < 0)
422 error(SYSTEM, "cannot fork child for persist function");
423 pfdetach(); /* parent exits */
424 }
425 }
426 if (persist == PCHILD) { /* wait for a signal then go again */
427 if (outfile != NULL)
428 close(duped1); /* release output handle */
429 pfhold();
430 tstart = time((time_t *)NULL); /* reinitialize */
431 raynum = nrays = 0;
432 goto runagain;
433 }
434 #endif
435
436
437 ray_done_pmap(); /* PMAP: free photon maps */
438
439 quit(0);
440
441 badopt:
442 sprintf(errmsg, "command line error at '%s'", argv[i]);
443 error(USER, errmsg);
444 return 1; /* pro forma return */
445
446 #undef check
447 #undef check_bool
448 }
449
450
451 void
452 wputs( /* warning output function */
453 const char *s
454 )
455 {
456 int lasterrno = errno;
457 eputs(s);
458 errno = lasterrno;
459 }
460
461
462 void
463 eputs( /* put string to stderr */
464 const char *s
465 )
466 {
467 static int midline = 0;
468
469 if (!*s)
470 return;
471 if (!midline++) {
472 fputs(progname, stderr);
473 fputs(": ", stderr);
474 }
475 fputs(s, stderr);
476 if (s[strlen(s)-1] == '\n') {
477 fflush(stderr);
478 midline = 0;
479 }
480 }
481
482
483 static void
484 onsig( /* fatal signal */
485 int signo
486 )
487 {
488 static int gotsig = 0;
489
490 if (gotsig++) /* two signals and we're gone! */
491 _exit(signo);
492
493 #ifdef SIGALRM /* XXX how critical is this? */
494 alarm(15); /* allow 15 seconds to clean up */
495 signal(SIGALRM, SIG_DFL); /* make certain we do die */
496 #endif
497 eputs("signal - ");
498 eputs(sigerr[signo]);
499 eputs("\n");
500 quit(3);
501 }
502
503
504 static void
505 sigdie( /* set fatal signal */
506 int signo,
507 char *msg
508 )
509 {
510 if (signal(signo, onsig) == SIG_IGN)
511 signal(signo, SIG_IGN);
512 sigerr[signo] = msg;
513 }
514
515
516 static void
517 printdefaults(void) /* print default values to stdout */
518 {
519 printf("-vt%c\t\t\t\t# view type %s\n", ourview.type,
520 ourview.type==VT_PER ? "perspective" :
521 ourview.type==VT_PAR ? "parallel" :
522 ourview.type==VT_HEM ? "hemispherical" :
523 ourview.type==VT_ANG ? "angular" :
524 ourview.type==VT_CYL ? "cylindrical" :
525 ourview.type==VT_PLS ? "planisphere" :
526 "unknown");
527 printf("-vp %f %f %f\t# view point\n",
528 ourview.vp[0], ourview.vp[1], ourview.vp[2]);
529 printf("-vd %f %f %f\t# view direction\n",
530 ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
531 printf("-vu %f %f %f\t# view up\n",
532 ourview.vup[0], ourview.vup[1], ourview.vup[2]);
533 printf("-vh %f\t\t\t# view horizontal size\n", ourview.horiz);
534 printf("-vv %f\t\t\t# view vertical size\n", ourview.vert);
535 printf("-vo %f\t\t\t# view fore clipping plane\n", ourview.vfore);
536 printf("-va %f\t\t\t# view aft clipping plane\n", ourview.vaft);
537 printf("-vs %f\t\t\t# view shift\n", ourview.hoff);
538 printf("-vl %f\t\t\t# view lift\n", ourview.voff);
539 printf("-x %-9d\t\t\t# x resolution\n", hresolu);
540 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
541 if (NCSAMP > 3) {
542 printf("-cs %-2d\t\t\t\t# number of spectral bins\n", NCSAMP);
543 printf("-cw %3.0f %3.0f\t\t\t# wavelength limits (nm)\n",
544 WLPART[3], WLPART[0]);
545 /* printf(out_prims != NULL ? "-co-\t\t\t\t# output tristimulus colors\n" :
546 "-co+\t\t\t\t# output spectral values\n");
547 */ }
548 if (out_prims == stdprims)
549 printf("-pRGB\t\t\t\t# standard RGB color output\n");
550 else if (out_prims == xyzprims)
551 printf("-pXYZ\t\t\t\t# CIE XYZ color output\n");
552 else if (out_prims != NULL)
553 printf("-pc %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f\t# output color primaries and white point\n",
554 out_prims[RED][0], out_prims[RED][1],
555 out_prims[GRN][0], out_prims[GRN][1],
556 out_prims[BLU][0], out_prims[BLU][1],
557 out_prims[WHT][0], out_prims[WHT][1]);
558 printf("-pa %f\t\t\t# pixel aspect ratio\n", pixaspect);
559 printf("-pj %f\t\t\t# pixel jitter\n", dstrpix);
560 printf("-pm %f\t\t\t# pixel motion\n", mblur);
561 printf("-pd %f\t\t\t# pixel depth-of-field\n", dblur);
562 printf("-ps %-9d\t\t\t# pixel sample\n", psample);
563 printf("-pt %f\t\t\t# pixel threshold\n", maxdiff);
564 printf("-t %-9d\t\t\t# time between reports\n", ralrm);
565 printf(erract[WARNING].pf != NULL ?
566 "-w+\t\t\t\t# warning messages on\n" :
567 "-w-\t\t\t\t# warning messages off\n");
568 print_rdefaults();
569 }