ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rcmain.c
Revision: 2.34
Committed: Wed Jan 17 01:31:08 2024 UTC (4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.33: +5 -1 lines
Log Message:
fix(rpict,rvu,rcontrib): Fixed luminance estimate during spectral rendering

File Contents

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