ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.39
Committed: Wed Oct 30 17:26:13 2024 UTC (6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.38: +1 -3 lines
Log Message:
fix(rcontrib,rxcontrib): reinstated .cal redefinition warnings

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rcmain.c,v 2.38 2024/10/30 02:22:23 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 /* option city */
217 for (i = 1; i < argc; i++) {
218 /* expand arguments */
219 while ((rval = expandarg(&argc, &argv, i)) > 0)
220 ;
221 if (rval < 0) {
222 sprintf(errmsg, "cannot expand '%s'", argv[i]);
223 error(SYSTEM, errmsg);
224 }
225 if (argv[i] == NULL || argv[i][0] != '-')
226 break; /* break from options */
227 if (!strcmp(argv[i], "-version")) {
228 puts(VersionID);
229 quit(0);
230 }
231 if (!strcmp(argv[i], "-defaults") ||
232 !strcmp(argv[i], "-help")) {
233 override_options();
234 printdefaults();
235 quit(0);
236 }
237 rval = getrenderopt(argc-i, argv+i);
238 if (rval >= 0) {
239 i += rval;
240 continue;
241 }
242 switch (argv[i][1]) {
243 case 'n': /* number of cores */
244 check(2,"i");
245 nproc = atoi(argv[++i]);
246 if (nproc <= 0)
247 error(USER, "bad number of processes");
248 break;
249 case 'V': /* output contributions */
250 check_bool(2,contrib);
251 break;
252 case 'x': /* x resolution */
253 check(2,"i");
254 xres = atoi(argv[++i]);
255 break;
256 case 'y': /* y resolution */
257 check(2,"i");
258 yres = atoi(argv[++i]);
259 break;
260 case 'w': /* warnings */
261 rval = (erract[WARNING].pf != NULL);
262 check_bool(2,rval);
263 if (rval) erract[WARNING].pf = wputs;
264 else erract[WARNING].pf = NULL;
265 break;
266 case 'e': /* expression */
267 check(2,"s");
268 scompile(argv[++i], NULL, 0);
269 break;
270 case 'l': /* limit distance */
271 if (argv[i][2] != 'd')
272 goto badopt;
273 check_bool(3,lim_dist);
274 break;
275 case 'I': /* immed. irradiance */
276 check_bool(2,imm_irrad);
277 break;
278 case 'f': /* file or force or format */
279 if (!argv[i][2]) {
280 check(2,"s");
281 loadfunc(argv[++i]);
282 break;
283 }
284 if (argv[i][2] == 'o') {
285 check_bool(3,force_open);
286 break;
287 }
288 setformat(argv[i]+2);
289 break;
290 case 'o': /* output */
291 check(2,"s");
292 curout = argv[++i];
293 break;
294 case 'r': /* recover output */
295 check_bool(2,recover);
296 break;
297 case 'h': /* header output */
298 check_bool(2,header);
299 break;
300 case 'p': /* parameter setting(s) */
301 check(2,"s");
302 set_eparams(prms = argv[++i]);
303 break;
304 case 'c': /* sample count */
305 check(2,"i");
306 accumulate = atoi(argv[++i]);
307 break;
308 case 'b': /* bin expression/count */
309 if (argv[i][2] == 'n') {
310 check(3,"s");
311 bincnt = (int)(eval(argv[++i]) + .5);
312 break;
313 }
314 check(2,"s");
315 binval = argv[++i];
316 break;
317 case 'm': /* modifier name */
318 check(2,"s");
319 addmodifier(argv[++i], curout, prms, binval, bincnt);
320 break;
321 case 'M': /* modifier file */
322 check(2,"s");
323 addmodfile(argv[++i], curout, prms, binval, bincnt);
324 break;
325 case 't': /* reporting interval */
326 check(2,"i");
327 report_intvl = atoi(argv[++i]);
328 break;
329 default:
330 goto badopt;
331 }
332 }
333 if (nmods <= 0)
334 error(USER, "missing required modifier argument");
335 /* override some option settings */
336 override_options();
337 /* set/check spectral sampling */
338 if (setspectrsamp(CNDX, WLPART) < 0)
339 error(USER, "unsupported spectral sampling");
340 /* initialize object types */
341 initotypes();
342 /* initialize urand */
343 if (rand_samp) {
344 srandom((long)time(0));
345 initurand(0);
346 } else {
347 srandom(0L);
348 initurand(2048);
349 }
350 /* set up signal handling */
351 sigdie(SIGINT, "Interrupt");
352 #ifdef SIGHUP
353 sigdie(SIGHUP, "Hangup");
354 #endif
355 sigdie(SIGTERM, "Terminate");
356 #ifdef SIGPIPE
357 sigdie(SIGPIPE, "Broken pipe");
358 #endif
359 #ifdef SIGALRM
360 sigdie(SIGALRM, "Alarm clock");
361 #endif
362 #ifdef SIGXCPU
363 sigdie(SIGXCPU, "CPU limit exceeded");
364 sigdie(SIGXFSZ, "File size exceeded");
365 #endif
366 #ifdef NICE
367 nice(NICE); /* lower priority */
368 #endif
369 /* get octree */
370 if (i == argc)
371 octname = NULL;
372 else if (i == argc-1)
373 octname = argv[i];
374 else
375 goto badopt;
376 if (octname == NULL)
377 error(USER, "missing octree argument");
378
379 readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL);
380 nsceneobjs = nobjects;
381
382 /* PMAP: set up & load photon maps */
383 ray_init_pmap();
384
385 marksources(); /* find and mark sources */
386
387 /* PMAP: init photon map for light source contributions */
388 initPmapContrib(&modconttab, nmods);
389
390 setambient(); /* initialize ambient calculation */
391
392 rcontrib(); /* trace ray contributions (loop) */
393
394 /* PMAP: free photon maps */
395 ray_done_pmap();
396
397 quit(0); /* exit clean */
398
399 badopt:
400 fprintf(stderr,
401 "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",
402 progname);
403 sprintf(errmsg, "command line error at '%s'", argv[i]);
404 error(USER, errmsg);
405 return(1); /* pro forma return */
406
407 #undef check
408 #undef check_bool
409 }
410
411
412 void
413 wputs( /* warning output function */
414 const char *s
415 )
416 {
417 int lasterrno = errno;
418 if (erract[WARNING].pf == NULL)
419 return; /* called by calcomp or someone */
420 eputs(s);
421 errno = lasterrno;
422 }
423
424
425 void
426 eputs( /* put string to stderr */
427 const char *s
428 )
429 {
430 static int midline = 0;
431
432 if (!*s)
433 return;
434 if (!midline++) {
435 fputs(progname, stderr);
436 fputs(": ", stderr);
437 }
438 fputs(s, stderr);
439 if (s[strlen(s)-1] == '\n') {
440 fflush(stderr);
441 midline = 0;
442 }
443 }