ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.38
Committed: Wed Oct 30 02:22:23 2024 UTC (6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.37: +5 -1 lines
Log Message:
fix(rcontrib,rxcontrib): Eliminated spurious warning messages

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rcmain.c,v 2.37 2024/10/27 00:01:21 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 "ImmediateIrradiance\n" \
59 "ProgressReporting\nDistanceLimiting\n" \
60 "InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \
61 "Outputs=V,W\n" \
62 "OutputCS=RGB,spec\n"
63 #else
64 #define RCONTRIB_FEATURES "Multiprocessing\n" \
65 "Accumulation\nSummation\nRecovery\n" \
66 "ImmediateIrradiance\n" \
67 "ProgressReporting\nDistanceLimiting\n" \
68 "InputFormats=a,f,d\nOutputFormats=a,f,d,c\n" \
69 "Outputs=V,W\n" \
70 "OutputCS=RGB,spec\n"
71 #endif
72
73 static void
74 printdefaults(void) /* print default values to stdout */
75 {
76 printf("-c %-5d\t\t\t# accumulated rays per record\n", accumulate);
77 printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
78 contrib ? "contributions" : "coefficients");
79 if (imm_irrad)
80 printf("-I+\t\t\t\t# immediate irradiance on\n");
81 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
82 printf("-x %-9d\t\t\t# %s\n", xres,
83 yres && xres ? "x resolution" : "flush interval");
84 printf("-y %-9d\t\t\t# y resolution\n", yres);
85 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
86 "-ld-\t\t\t\t# limit distance off\n");
87 printf(header ? "-h+\t\t\t\t# output header\n" :
88 "-h-\t\t\t\t# no header\n");
89 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
90 inpfmt, outfmt, formstr(inpfmt), formstr(outfmt));
91 if (report_intvl > 0)
92 printf("-t %-9d\t\t\t# time between reports\n", report_intvl);
93 printf(erract[WARNING].pf != NULL ?
94 "-w+\t\t\t\t# warning messages on\n" :
95 "-w-\t\t\t\t# warning messages off\n");
96 print_rdefaults();
97 }
98
99
100 static void
101 onsig( /* fatal signal */
102 int signo
103 )
104 {
105 static int gotsig = 0;
106
107 if (gotsig++) /* two signals and we're gone! */
108 _exit(signo);
109
110 #ifdef SIGALRM
111 alarm(15); /* allow 15 seconds to clean up */
112 signal(SIGALRM, SIG_DFL); /* make certain we do die */
113 #endif
114 eputs("signal - ");
115 eputs(sigerr[signo]);
116 eputs("\n");
117 quit(3);
118 }
119
120
121 static void
122 sigdie( /* set fatal signal */
123 int signo,
124 char *msg
125 )
126 {
127 if (signal(signo, onsig) == SIG_IGN)
128 signal(signo, SIG_IGN);
129 sigerr[signo] = msg;
130 }
131
132
133 /* set input/output format */
134 static void
135 setformat(const char *fmt)
136 {
137 switch (fmt[0]) {
138 case 'f':
139 case 'd':
140 SET_FILE_BINARY(stdin);
141 /* fall through */
142 case 'a':
143 inpfmt = fmt[0];
144 break;
145 default:
146 goto fmterr;
147 }
148 switch (fmt[1]) {
149 case '\0':
150 outfmt = inpfmt;
151 return;
152 case 'a':
153 case 'f':
154 case 'd':
155 case 'c':
156 outfmt = fmt[1];
157 break;
158 default:
159 goto fmterr;
160 }
161 if (!fmt[2])
162 return;
163 fmterr:
164 sprintf(errmsg, "Illegal i/o format: -f%s", fmt);
165 error(USER, errmsg);
166 }
167
168
169 /* Set overriding options */
170 static void
171 override_options(void)
172 {
173 shadthresh = 0;
174 ambssamp = 0;
175 ambacc = 0;
176 if (accumulate <= 0) /* no output flushing for single record */
177 xres = yres = 0;
178 }
179
180
181 int
182 main(int argc, char *argv[])
183 {
184 #define check(ol,al) if (argv[i][ol] || \
185 badarg(argc-i-1,argv+i+1,al)) \
186 goto badopt
187 #define check_bool(olen,var) switch (argv[i][olen]) { \
188 case '\0': var = !var; break; \
189 case 'y': case 'Y': case 't': case 'T': \
190 case '+': case '1': var = 1; break; \
191 case 'n': case 'N': case 'f': case 'F': \
192 case '-': case '0': var = 0; break; \
193 default: goto badopt; }
194 char *curout = NULL;
195 char *prms = NULL;
196 char *binval = NULL;
197 int bincnt = 0;
198 int rval;
199 int i;
200 /* global program name */
201 progname = argv[0] = fixargv0(argv[0]);
202 gargv = argv;
203 gargc = argc;
204 /* feature check only? */
205 strcat(RFeatureList, RCONTRIB_FEATURES);
206 if (argc > 1 && !strcmp(argv[1], "-features"))
207 return feature_status(argc-2, argv+2);
208 #if defined(_WIN32) || defined(_WIN64) /* increase file limit to maximum */
209 for (i = 8192; i > _IOB_ENTRIES; i >>= 1)
210 if (_setmaxstdio(i) == i)
211 break;
212 #endif
213 /* initialize calcomp routines early */
214 initfunc();
215 calcontext(RCCONTEXT);
216 esupport &= ~E_REDEFW; /* temporary */
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': /* sample count */
306 check(2,"i");
307 accumulate = atoi(argv[++i]);
308 break;
309 case 'b': /* bin expression/count */
310 if (argv[i][2] == 'n') {
311 check(3,"s");
312 bincnt = (int)(eval(argv[++i]) + .5);
313 break;
314 }
315 check(2,"s");
316 binval = argv[++i];
317 break;
318 case 'm': /* modifier name */
319 check(2,"s");
320 addmodifier(argv[++i], curout, prms, binval, bincnt);
321 break;
322 case 'M': /* modifier file */
323 check(2,"s");
324 addmodfile(argv[++i], curout, prms, binval, bincnt);
325 break;
326 case 't': /* reporting interval */
327 check(2,"i");
328 report_intvl = atoi(argv[++i]);
329 break;
330 default:
331 goto badopt;
332 }
333 }
334 if (nmods <= 0)
335 error(USER, "missing required modifier argument");
336 esupport |= E_REDEFW;
337 /* override some option settings */
338 override_options();
339 /* set/check spectral sampling */
340 if (setspectrsamp(CNDX, WLPART) < 0)
341 error(USER, "unsupported spectral sampling");
342 /* initialize object types */
343 initotypes();
344 /* initialize urand */
345 if (rand_samp) {
346 srandom((long)time(0));
347 initurand(0);
348 } else {
349 srandom(0L);
350 initurand(2048);
351 }
352 /* set up signal handling */
353 sigdie(SIGINT, "Interrupt");
354 #ifdef SIGHUP
355 sigdie(SIGHUP, "Hangup");
356 #endif
357 sigdie(SIGTERM, "Terminate");
358 #ifdef SIGPIPE
359 sigdie(SIGPIPE, "Broken pipe");
360 #endif
361 #ifdef SIGALRM
362 sigdie(SIGALRM, "Alarm clock");
363 #endif
364 #ifdef SIGXCPU
365 sigdie(SIGXCPU, "CPU limit exceeded");
366 sigdie(SIGXFSZ, "File size exceeded");
367 #endif
368 #ifdef NICE
369 nice(NICE); /* lower priority */
370 #endif
371 /* get octree */
372 if (i == argc)
373 octname = NULL;
374 else if (i == argc-1)
375 octname = argv[i];
376 else
377 goto badopt;
378 if (octname == NULL)
379 error(USER, "missing octree argument");
380
381 readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL);
382 nsceneobjs = nobjects;
383
384 /* PMAP: set up & load photon maps */
385 ray_init_pmap();
386
387 marksources(); /* find and mark sources */
388
389 /* PMAP: init photon map for light source contributions */
390 initPmapContrib(&modconttab, nmods);
391
392 setambient(); /* initialize ambient calculation */
393
394 rcontrib(); /* trace ray contributions (loop) */
395
396 /* PMAP: free photon maps */
397 ray_done_pmap();
398
399 quit(0); /* exit clean */
400
401 badopt:
402 fprintf(stderr,
403 "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",
404 progname);
405 sprintf(errmsg, "command line error at '%s'", argv[i]);
406 error(USER, errmsg);
407 return(1); /* pro forma return */
408
409 #undef check
410 #undef check_bool
411 }
412
413
414 void
415 wputs( /* warning output function */
416 const char *s
417 )
418 {
419 int lasterrno = errno;
420 if (erract[WARNING].pf == NULL)
421 return; /* called by calcomp or someone */
422 eputs(s);
423 errno = lasterrno;
424 }
425
426
427 void
428 eputs( /* put string to stderr */
429 const char *s
430 )
431 {
432 static int midline = 0;
433
434 if (!*s)
435 return;
436 if (!midline++) {
437 fputs(progname, stderr);
438 fputs(": ", stderr);
439 }
440 fputs(s, stderr);
441 if (s[strlen(s)-1] == '\n') {
442 fflush(stderr);
443 midline = 0;
444 }
445 }