ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.32
Committed: Wed Nov 15 18:02:53 2023 UTC (6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.31: +31 -9 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: rcmain.c,v 2.31 2023/08/15 00:46:56 greg Exp $";
3 #endif
4 /*
5 * rcmain.c - main for rtcontrib ray contribution tracer
6 */
7
8 #include "copyright.h"
9
10 #include <signal.h>
11 #include <time.h>
12 #include "rcontrib.h"
13 #include "random.h"
14 #include "source.h"
15 #include "ambient.h"
16 #include "pmapray.h"
17 #include "pmapcontrib.h"
18
19 int gargc; /* global argc */
20 char **gargv; /* global argv */
21 char *octname; /* global octree name */
22 char *progname; /* global argv[0] */
23
24 char *sigerr[NSIG]; /* signal error messages */
25
26 int nproc = 1; /* number of processes requested */
27 int nchild = 0; /* number of children (-1 in child) */
28
29 int inpfmt = 'a'; /* input format */
30 int outfmt = 'a'; /* output format */
31
32 int header = 1; /* output header? */
33 int force_open = 0; /* truncate existing output? */
34 int recover = 0; /* recover previous output? */
35 int accumulate = 1; /* input rays per output record */
36 int contrib = 0; /* computing contributions? */
37
38 int xres = 0; /* horizontal (scan) size */
39 int yres = 0; /* vertical resolution */
40
41 int using_stdout = 0; /* are we using stdout? */
42
43 int imm_irrad = 0; /* compute immediate irradiance? */
44 int lim_dist = 0; /* limit distance? */
45
46 int report_intvl = 0; /* reporting interval (seconds) */
47
48 char **modname = NULL; /* ordered modifier name list */
49 int nmods = 0; /* number of modifiers */
50 int modasiz = 0; /* allocated modifier array size */
51
52 void (*addobjnotify[8])() = {ambnotify, NULL};
53
54 char RCCONTEXT[] = "RC."; /* our special evaluation context */
55
56 #if defined(_WIN32) || defined(_WIN64)
57 #define RCONTRIB_FEATURES "Accumulation\nSummation\nRecovery\n" \
58 "Hyperspectral\nImmediateIrradiance\n" \
59 "ProgressReporting\nDistanceLimiting\n" \
60 "InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \
61 "Outputs=V,W\n"
62 #else
63 #define RCONTRIB_FEATURES "Multiprocessing\n" \
64 "Accumulation\nSummation\nRecovery\n" \
65 "Hyperspectral\nImmediateIrradiance\n" \
66 "ProgressReporting\nDistanceLimiting\n" \
67 "InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \
68 "Outputs=V,W\n"
69 #endif
70
71 static void
72 printdefaults(void) /* print default values to stdout */
73 {
74 printf("-c %-5d\t\t\t# accumulated rays per record\n", accumulate);
75 if (NCSAMP > 3) {
76 printf("-cs %-2d\t\t\t\t# number of spectral bins\n", NCSAMP);
77 printf("-cw %3.0f %3.0f\t\t\t# wavelength limits (nm)\n",
78 WLPART[3], WLPART[0]);
79 }
80 printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
81 contrib ? "contributions" : "coefficients");
82 if (imm_irrad)
83 printf("-I+\t\t\t\t# immediate irradiance on\n");
84 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
85 printf("-x %-9d\t\t\t# %s\n", xres,
86 yres && xres ? "x resolution" : "flush interval");
87 printf("-y %-9d\t\t\t# y resolution\n", yres);
88 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
89 "-ld-\t\t\t\t# limit distance off\n");
90 printf(header ? "-h+\t\t\t\t# output header\n" :
91 "-h-\t\t\t\t# no header\n");
92 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
93 inpfmt, outfmt, formstr(inpfmt), formstr(outfmt));
94 printf(erract[WARNING].pf != NULL ?
95 "-w+\t\t\t\t# warning messages on\n" :
96 "-w-\t\t\t\t# warning messages off\n");
97 print_rdefaults();
98 }
99
100
101 static void
102 onsig( /* fatal signal */
103 int signo
104 )
105 {
106 static int gotsig = 0;
107
108 if (gotsig++) /* two signals and we're gone! */
109 _exit(signo);
110
111 #ifdef SIGALRM
112 alarm(15); /* allow 15 seconds to clean up */
113 signal(SIGALRM, SIG_DFL); /* make certain we do die */
114 #endif
115 eputs("signal - ");
116 eputs(sigerr[signo]);
117 eputs("\n");
118 quit(3);
119 }
120
121
122 static void
123 sigdie( /* set fatal signal */
124 int signo,
125 char *msg
126 )
127 {
128 if (signal(signo, onsig) == SIG_IGN)
129 signal(signo, SIG_IGN);
130 sigerr[signo] = msg;
131 }
132
133
134 /* set input/output format */
135 static void
136 setformat(const char *fmt)
137 {
138 switch (fmt[0]) {
139 case 'f':
140 case 'd':
141 SET_FILE_BINARY(stdin);
142 /* fall through */
143 case 'a':
144 inpfmt = fmt[0];
145 break;
146 default:
147 goto fmterr;
148 }
149 switch (fmt[1]) {
150 case '\0':
151 outfmt = inpfmt;
152 return;
153 case 'a':
154 case 'f':
155 case 'd':
156 case 'c':
157 outfmt = fmt[1];
158 break;
159 default:
160 goto fmterr;
161 }
162 if (!fmt[2])
163 return;
164 fmterr:
165 sprintf(errmsg, "Illegal i/o format: -f%s", fmt);
166 error(USER, errmsg);
167 }
168
169
170 /* Set overriding options */
171 static void
172 override_options(void)
173 {
174 shadthresh = 0;
175 ambssamp = 0;
176 ambacc = 0;
177 if (accumulate <= 0) /* no output flushing for single record */
178 xres = yres = 0;
179 }
180
181
182 int
183 main(int argc, char *argv[])
184 {
185 #define check(ol,al) if (argv[i][ol] || \
186 badarg(argc-i-1,argv+i+1,al)) \
187 goto badopt
188 #define check_bool(olen,var) switch (argv[i][olen]) { \
189 case '\0': var = !var; break; \
190 case 'y': case 'Y': case 't': case 'T': \
191 case '+': case '1': var = 1; break; \
192 case 'n': case 'N': case 'f': case 'F': \
193 case '-': case '0': var = 0; break; \
194 default: goto badopt; }
195 char *curout = NULL;
196 char *prms = NULL;
197 char *binval = NULL;
198 int bincnt = 0;
199 int rval;
200 int i;
201 /* global program name */
202 progname = argv[0] = fixargv0(argv[0]);
203 gargv = argv;
204 gargc = argc;
205 /* feature check only? */
206 strcat(RFeatureList, RCONTRIB_FEATURES);
207 if (argc > 1 && !strcmp(argv[1], "-features"))
208 return feature_status(argc-2, argv+2);
209 #if defined(_WIN32) || defined(_WIN64) /* increase file limit to maximum */
210 for (i = 8192; i > _IOB_ENTRIES; i >>= 1)
211 if (_setmaxstdio(i) == i)
212 break;
213 #endif
214 /* initialize calcomp routines early */
215 initfunc();
216 calcontext(RCCONTEXT);
217 /* option city */
218 for (i = 1; i < argc; i++) {
219 /* expand arguments */
220 while ((rval = expandarg(&argc, &argv, i)) > 0)
221 ;
222 if (rval < 0) {
223 sprintf(errmsg, "cannot expand '%s'", argv[i]);
224 error(SYSTEM, errmsg);
225 }
226 if (argv[i] == NULL || argv[i][0] != '-')
227 break; /* break from options */
228 if (!strcmp(argv[i], "-version")) {
229 puts(VersionID);
230 quit(0);
231 }
232 if (!strcmp(argv[i], "-defaults") ||
233 !strcmp(argv[i], "-help")) {
234 override_options();
235 printdefaults();
236 quit(0);
237 }
238 rval = getrenderopt(argc-i, argv+i);
239 if (rval >= 0) {
240 i += rval;
241 continue;
242 }
243 switch (argv[i][1]) {
244 case 'n': /* number of cores */
245 check(2,"i");
246 nproc = atoi(argv[++i]);
247 if (nproc <= 0)
248 error(USER, "bad number of processes");
249 break;
250 case 'V': /* output contributions */
251 check_bool(2,contrib);
252 break;
253 case 'x': /* x resolution */
254 check(2,"i");
255 xres = atoi(argv[++i]);
256 break;
257 case 'y': /* y resolution */
258 check(2,"i");
259 yres = atoi(argv[++i]);
260 break;
261 case 'w': /* warnings */
262 rval = (erract[WARNING].pf != NULL);
263 check_bool(2,rval);
264 if (rval) erract[WARNING].pf = wputs;
265 else erract[WARNING].pf = NULL;
266 break;
267 case 'e': /* expression */
268 check(2,"s");
269 scompile(argv[++i], NULL, 0);
270 break;
271 case 'l': /* limit distance */
272 if (argv[i][2] != 'd')
273 goto badopt;
274 check_bool(3,lim_dist);
275 break;
276 case 'I': /* immed. irradiance */
277 check_bool(2,imm_irrad);
278 break;
279 case 'f': /* file or force or format */
280 if (!argv[i][2]) {
281 check(2,"s");
282 loadfunc(argv[++i]);
283 break;
284 }
285 if (argv[i][2] == 'o') {
286 check_bool(3,force_open);
287 break;
288 }
289 setformat(argv[i]+2);
290 break;
291 case 'o': /* output */
292 check(2,"s");
293 curout = argv[++i];
294 break;
295 case 'r': /* recover output */
296 check_bool(2,recover);
297 break;
298 case 'h': /* header output */
299 check_bool(2,header);
300 break;
301 case 'p': /* parameter setting(s) */
302 check(2,"s");
303 set_eparams(prms = argv[++i]);
304 break;
305 case 'c': /* spectral sampling or count */
306 switch (argv[i][2]) {
307 #if MAXCSAMP>3
308 case 's': /* spectral bin count */
309 check(3,"i");
310 NCSAMP = atoi(argv[++i]);
311 break;
312 case 'w': /* wavelength extrema */
313 check(3,"ff");
314 WLPART[0] = atof(argv[++i]);
315 WLPART[3] = atof(argv[++i]);
316 break;
317 #endif
318 case '\0': /* sample count */
319 check(2,"i");
320 accumulate = atoi(argv[++i]);
321 break;
322 default:
323 goto badopt;
324 }
325 break;
326 case 'b': /* bin expression/count */
327 if (argv[i][2] == 'n') {
328 check(3,"s");
329 bincnt = (int)(eval(argv[++i]) + .5);
330 break;
331 }
332 check(2,"s");
333 binval = argv[++i];
334 break;
335 case 'm': /* modifier name */
336 check(2,"s");
337 addmodifier(argv[++i], curout, prms, binval, bincnt);
338 break;
339 case 'M': /* modifier file */
340 check(2,"s");
341 addmodfile(argv[++i], curout, prms, binval, bincnt);
342 break;
343 case 't': /* reporting interval */
344 check(2,"i");
345 report_intvl = atoi(argv[++i]);
346 break;
347 default:
348 goto badopt;
349 }
350 }
351 if (nmods <= 0)
352 error(USER, "missing required modifier argument");
353 /* override some option settings */
354 override_options();
355 /* initialize object types */
356 initotypes();
357 /* initialize urand */
358 if (rand_samp) {
359 srandom((long)time(0));
360 initurand(0);
361 } else {
362 srandom(0L);
363 initurand(2048);
364 }
365 /* set up signal handling */
366 sigdie(SIGINT, "Interrupt");
367 #ifdef SIGHUP
368 sigdie(SIGHUP, "Hangup");
369 #endif
370 sigdie(SIGTERM, "Terminate");
371 #ifdef SIGPIPE
372 sigdie(SIGPIPE, "Broken pipe");
373 #endif
374 #ifdef SIGALRM
375 sigdie(SIGALRM, "Alarm clock");
376 #endif
377 #ifdef SIGXCPU
378 sigdie(SIGXCPU, "CPU limit exceeded");
379 sigdie(SIGXFSZ, "File size exceeded");
380 #endif
381 #ifdef NICE
382 nice(NICE); /* lower priority */
383 #endif
384 /* get octree */
385 if (i == argc)
386 octname = NULL;
387 else if (i == argc-1)
388 octname = argv[i];
389 else
390 goto badopt;
391 if (octname == NULL)
392 error(USER, "missing octree argument");
393
394 readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL);
395 nsceneobjs = nobjects;
396
397 /* PMAP: set up & load photon maps */
398 ray_init_pmap();
399
400 marksources(); /* find and mark sources */
401
402 /* PMAP: init photon map for light source contributions */
403 initPmapContrib(&modconttab, nmods);
404
405 setambient(); /* initialize ambient calculation */
406
407 rcontrib(); /* trace ray contributions (loop) */
408
409 ambsync(); /* flush ambient file */
410
411 /* PMAP: free photon maps */
412 ray_done_pmap();
413
414 quit(0); /* exit clean */
415
416 badopt:
417 fprintf(stderr,
418 "Usage: %s [-n nprocs][-V][-c count][-r][-e expr][-f source][-o ospec][-p p1=V1,p2=V2][-b binv][-bn N] {-m mod | -M file} [rtrace options] octree\n",
419 progname);
420 sprintf(errmsg, "command line error at '%s'", argv[i]);
421 error(USER, errmsg);
422 return(1); /* pro forma return */
423
424 #undef check
425 #undef check_bool
426 }
427
428
429 void
430 wputs( /* warning output function */
431 const char *s
432 )
433 {
434 int lasterrno = errno;
435 eputs(s);
436 errno = lasterrno;
437 }
438
439
440 void
441 eputs( /* put string to stderr */
442 const char *s
443 )
444 {
445 static int midline = 0;
446
447 if (!*s)
448 return;
449 if (!midline++) {
450 fputs(progname, stderr);
451 fputs(": ", stderr);
452 }
453 fputs(s, stderr);
454 if (s[strlen(s)-1] == '\n') {
455 fflush(stderr);
456 midline = 0;
457 }
458 }