ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rxcmain.cpp
Revision: 2.1
Committed: Tue Oct 29 00:36:54 2024 UTC (7 months ago) by greg
Branch: MAIN
Log Message:
feat(rxcontrib): First compiled version of rxcontrib tool to test new C++ classes

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rcmain.c,v 2.36 2024/04/05 16:41:17 greg Exp $";
3 #endif
4 /*
5 * rxcmain.c - main for rxcontrib ray contribution tracer
6 */
7
8 #include "copyright.h"
9
10 #include <signal.h>
11 #include <time.h>
12 #include <ctype.h>
13 #include "RcontribSimulManager.h"
14 #include "platform.h"
15 #include "func.h"
16
17 const char *sigerr[NSIG]; /* signal error messages */
18
19 int nproc = 1; /* number of processes requested */
20
21 int inpfmt = 'a'; /* input format */
22 int outfmt = 'f'; /* output format */
23
24 int contrib = 0; /* computing contributions? */
25
26 int xres = 0; /* horizontal (scan) size */
27 int yres = 0; /* vertical resolution */
28
29 int imm_irrad = 0; /* compute immediate irradiance? */
30 int lim_dist = 0; /* limit distance? */
31
32 int report_intvl = 0; /* reporting interval (seconds) */
33
34 extern char * progname; // global argv[0]
35
36 RcontribSimulManager myRCmanager; // global rcontrib simulation manager
37
38 #define RCONTRIB_FEATURES "Multiprocessing\n" \
39 "Accumulation\nRecovery\n" \
40 "ImmediateIrradiance\n" \
41 "ProgressReporting?\nDistanceLimiting\n" \
42 "InputFormats=a,f,d\nOutputFormats=f,d,c\n" \
43 "Outputs=V,W\n" \
44 "OutputCS=RGB,spec\n"
45
46 static void rxcontrib(const int rstart = 0);
47
48 static void
49 printdefaults(void) /* print default values to stdout */
50 {
51 printf("-c %-5d\t\t\t# accumulated rays per record\n", myRCmanager.accum);
52 printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
53 contrib ? "contributions" : "coefficients");
54 if (imm_irrad)
55 printf("-I+\t\t\t\t# immediate irradiance on\n");
56 printf("-n %-2d\t\t\t\t# number of rendering processes\n", nproc);
57 if (xres > 0)
58 printf("-x %-9d\t\t\t# x resolution\n", xres);
59 printf("-y %-9d\t\t\t# y resolution\n", yres);
60 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
61 "-ld-\t\t\t\t# limit distance off\n");
62 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
63 inpfmt, outfmt, formstr(inpfmt), formstr(outfmt));
64 if (report_intvl > 0)
65 printf("-t %-9d\t\t\t# time between reports\n", report_intvl);
66 printf(erract[WARNING].pf != NULL ?
67 "-w+\t\t\t\t# warning messages on\n" :
68 "-w-\t\t\t\t# warning messages off\n");
69 print_rdefaults();
70 }
71
72
73 static void
74 onsig( /* fatal signal */
75 int signo
76 )
77 {
78 static int gotsig = 0;
79
80 if (gotsig++) /* two signals and we're gone! */
81 _exit(signo);
82
83 #ifdef SIGALRM
84 alarm(15); /* allow 15 seconds to clean up */
85 signal(SIGALRM, SIG_DFL); /* make certain we do die */
86 #endif
87 eputs("signal - ");
88 eputs(sigerr[signo]);
89 eputs("\n");
90 quit(3);
91 }
92
93
94 static void
95 sigdie( /* set fatal signal */
96 int signo,
97 const char *msg
98 )
99 {
100 if (signal(signo, onsig) == SIG_IGN)
101 signal(signo, SIG_IGN);
102 sigerr[signo] = msg;
103 }
104
105 const char *
106 formstr(int f) // return format identifier
107 {
108 switch (f) {
109 case 'a': return("ascii");
110 case 'f': return("float");
111 case 'd': return("double");
112 case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT);
113 }
114 return("unknown");
115 }
116
117 /* set input/output format */
118 static void
119 setformat(const char *fmt)
120 {
121 switch (fmt[0]) {
122 case 'f':
123 case 'd':
124 SET_FILE_BINARY(stdin);
125 /* fall through */
126 case 'a':
127 inpfmt = fmt[0];
128 break;
129 default:
130 goto fmterr;
131 }
132 switch (fmt[1]) {
133 case '\0':
134 if (inpfmt == 'a')
135 goto fmterr;
136 outfmt = inpfmt;
137 return;
138 case 'f':
139 case 'd':
140 case 'c':
141 outfmt = fmt[1];
142 break;
143 default:
144 goto fmterr;
145 }
146 if (!fmt[2])
147 return;
148 fmterr:
149 sprintf(errmsg, "Unsupported i/o format: -f%s", fmt);
150 error(USER, errmsg);
151 }
152
153
154 /* Set overriding options */
155 static void
156 override_options(void)
157 {
158 shadthresh = 0;
159 ambssamp = 0;
160 ambacc = 0;
161 }
162
163
164 int
165 main(int argc, char *argv[])
166 {
167 #define check(ol,al) if (argv[i][ol] || \
168 badarg(argc-i-1,argv+i+1,al)) \
169 goto badopt
170 #define check_bool(olen,var) switch (argv[i][olen]) { \
171 case '\0': var = !var; break; \
172 case 'y': case 'Y': case 't': case 'T': \
173 case '+': case '1': var = 1; break; \
174 case 'n': case 'N': case 'f': case 'F': \
175 case '-': case '0': var = 0; break; \
176 default: goto badopt; }
177 int force_open = 0;
178 int recover = 0;
179 char *curout = NULL;
180 char *prms = NULL;
181 char *binval = NULL;
182 int bincnt = 0;
183 int rval;
184 int i;
185 /* global program name */
186 progname = argv[0];
187 /* feature check only? */
188 strcat(RFeatureList, RCONTRIB_FEATURES);
189 if (argc > 1 && !strcmp(argv[1], "-features"))
190 return feature_status(argc-2, argv+2);
191 /* initialize calcomp routines early */
192 initfunc();
193 calcontext(RCCONTEXT);
194 /* option city */
195 for (i = 1; i < argc; i++) {
196 /* expand arguments */
197 while ((rval = expandarg(&argc, &argv, i)) > 0)
198 ;
199 if (rval < 0) {
200 sprintf(errmsg, "cannot expand '%s'", argv[i]);
201 error(SYSTEM, errmsg);
202 }
203 if (argv[i] == NULL || argv[i][0] != '-')
204 break; /* break from options */
205 if (!strcmp(argv[i], "-version")) {
206 puts(VersionID);
207 quit(0);
208 }
209 if (!strcmp(argv[i], "-defaults") ||
210 !strcmp(argv[i], "-help")) {
211 override_options();
212 printdefaults();
213 quit(0);
214 }
215 rval = getrenderopt(argc-i, argv+i);
216 if (rval >= 0) {
217 i += rval;
218 continue;
219 }
220 switch (argv[i][1]) {
221 case 'n': /* number of cores */
222 check(2,"i");
223 nproc = atoi(argv[++i]);
224 if (nproc < 0 && (nproc += RadSimulManager::GetNCores()) <= 0)
225 nproc = 1;
226 break;
227 case 'V': /* output contributions */
228 check_bool(2,contrib);
229 break;
230 case 'x': /* x resolution */
231 check(2,"i");
232 xres = atoi(argv[++i]);
233 break;
234 case 'y': /* y resolution */
235 check(2,"i");
236 yres = atoi(argv[++i]);
237 break;
238 case 'w': /* warnings */
239 rval = (erract[WARNING].pf != NULL);
240 check_bool(2,rval);
241 if (rval) erract[WARNING].pf = wputs;
242 else erract[WARNING].pf = NULL;
243 break;
244 case 'e': /* expression */
245 check(2,"s");
246 scompile(argv[++i], NULL, 0);
247 break;
248 case 'l': /* limit distance */
249 if (argv[i][2] != 'd')
250 goto badopt;
251 check_bool(3,lim_dist);
252 break;
253 case 'I': /* immed. irradiance */
254 check_bool(2,imm_irrad);
255 break;
256 case 'f': /* file or force or format */
257 if (!argv[i][2]) {
258 check(2,"s");
259 loadfunc(argv[++i]);
260 break;
261 }
262 if (argv[i][2] == 'o') {
263 check_bool(3,force_open);
264 break;
265 }
266 setformat(argv[i]+2);
267 myRCmanager.SetDataFormat(outfmt);
268 break;
269 case 'o': /* output */
270 check(2,"s");
271 curout = argv[++i];
272 break;
273 case 'r': /* recover output */
274 check_bool(2,recover);
275 break;
276 case 'p': /* parameter setting(s) */
277 check(2,"s");
278 set_eparams(prms = argv[++i]);
279 break;
280 case 'c': /* sample count */
281 check(2,"i");
282 myRCmanager.accum = atoi(argv[++i]);
283 break;
284 case 'b': /* bin expression/count */
285 if (argv[i][2] == 'n') {
286 check(3,"s");
287 bincnt = (int)(eval(argv[++i]) + .5);
288 break;
289 }
290 check(2,"s");
291 binval = argv[++i];
292 break;
293 case 'm': /* modifier name */
294 check(2,"s");
295 myRCmanager.AddModifier(argv[++i], curout, prms, binval, bincnt);
296 break;
297 case 'M': /* modifier file */
298 check(2,"s");
299 myRCmanager.AddModFile(argv[++i], curout, prms, binval, bincnt);
300 break;
301 case 't': /* reporting interval */
302 check(2,"i");
303 report_intvl = atoi(argv[++i]);
304 break;
305 default:
306 goto badopt;
307 }
308 }
309 if (i != argc-1)
310 error(USER, "expected single octree argument");
311 // get ready to rock...
312 if (setspectrsamp(CNDX, WLPART) < 0)
313 error(USER, "unsupported spectral sampling");
314
315 if (!myRCmanager.GetOutputs(NULL)) // check that we're ready
316 error(USER, "missing required modifier argument");
317 /* override some option settings */
318 override_options();
319 /* set up signal handling */
320 sigdie(SIGINT, "Interrupt");
321 #ifdef SIGHUP
322 sigdie(SIGHUP, "Hangup");
323 #endif
324 sigdie(SIGTERM, "Terminate");
325 #ifdef SIGPIPE
326 sigdie(SIGPIPE, "Broken pipe");
327 #endif
328 #ifdef SIGALRM
329 sigdie(SIGALRM, "Alarm clock");
330 #endif
331 #ifdef SIGXCPU
332 sigdie(SIGXCPU, "CPU limit exceeded");
333 sigdie(SIGXFSZ, "File size exceeded");
334 #endif
335 #ifdef NICE
336 nice(NICE); /* lower priority */
337 #endif
338 // load octree
339 myRCmanager.LoadOctree(argv[argc-1]);
340 // add to header
341 myRCmanager.AddHeader(argc-1, argv);
342 // prepare output files
343 if (recover)
344 myRCmanager.outOp = RCOrecover;
345 else if (force_open)
346 myRCmanager.outOp = RCOforce;
347 else
348 myRCmanager.outOp = RCOnew;
349 // rval = # rows recovered
350 rval = myRCmanager.PrepOutput();
351 // check if all done
352 if (recover && rval >= myRCmanager.GetRowMax()) {
353 error(WARNING, "nothing left to compute");
354 quit(0);
355 } // add processes as requested
356 myRCmanager.SetThreadCount(nproc);
357
358 rxcontrib(rval); /* trace ray contributions (loop) */
359
360 quit(0); /* exit clean */
361
362 badopt:
363 fprintf(stderr,
364 "Usage: %s [-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",
365 progname);
366 sprintf(errmsg, "command line error at '%s'", argv[i]);
367 error(USER, errmsg);
368 return(1); /* pro forma return */
369
370 #undef check
371 #undef check_bool
372 }
373
374
375 // skip specified number of bytes, return false if EOF
376 static bool
377 skipBytes(int n2skip)
378 {
379 while (n2skip-- > 0)
380 if (getchar() == EOF)
381 return false;
382 return true;
383 }
384
385
386 // skip specified number of whitespace-separated words, return false if EOF
387 static bool
388 skipWords(int n2skip)
389 {
390 int c;
391
392 while (n2skip-- > 0) {
393 do {
394 c = getchar();
395 } while (isspace(c));
396 do {
397 if (c == EOF) return false;
398 c = getchar();
399 } while (!isspace(c));
400 }
401 return true;
402 }
403
404
405 // read a bundle of myRCmanager.accum ray origins and directions
406 static bool
407 getRayBundle(FVECT *orig_dir = NULL)
408 {
409 int n2go = myRCmanager.accum;
410
411 switch (inpfmt) {
412 case 'a': // ASCII input
413 if (!orig_dir)
414 return skipWords(6*n2go);
415 while (n2go-- > 0) {
416 if (scanf(FVFORMAT, &orig_dir[0][0],
417 &orig_dir[0][1], &orig_dir[0][2]) != 3)
418 return false;
419 if (scanf(FVFORMAT, &orig_dir[1][0],
420 &orig_dir[1][1], &orig_dir[1][2]) != 3)
421 return false;
422 orig_dir += 2;
423 }
424 break;
425 case 'f': // float input
426 if (!orig_dir)
427 return skipBytes(6*sizeof(float)*n2go);
428 #ifdef SMLFLT
429 if (getbinary(orig_dir, sizeof(FVECT), 2*n2go, stdin) != 2*n2go)
430 return false;
431 #else
432 while (n2go-- > 0) {
433 float fvecs[6];
434 if (getbinary(fvecs, sizeof(fvecs), 1, stdin) != 1)
435 return false;
436 for (int i = 6; i--; )
437 orig_dir[0][i] = fvecs[i];
438 orig_dir += 2;
439 }
440 #endif
441 break;
442 case 'd': // double input
443 if (!orig_dir)
444 return skipBytes(6*sizeof(double)*n2go);
445 #ifndef SMLFLT
446 if (getbinary(orig_dir, sizeof(FVECT), 2*n2go, stdin) != 2*n2go)
447 return false;
448 #else
449 while (n2go-- > 0) {
450 double dvecs[6];
451 if (getbinary(dvecs, sizeof(dvecs), 1, stdin) != 1)
452 return false;
453 for (int i = 6; i--; )
454 orig_dir[0][i] = dvecs[i];
455 orig_dir += 2;
456 }
457 #endif
458 break;
459 default:
460 error(INTERNAL, "unsupported format in getRayBundle()");
461 return false;
462 }
463 int warned = 0; // normalize directions
464 n2go = myRCmanager.accum;
465 while (n2go-- > 0) {
466 orig_dir -= 2;
467 if (normalize(orig_dir[1]) == 0)
468 if (!warned++)
469 error(WARNING, "zero ray direction on input");
470 }
471 return true;
472 }
473
474
475 // Run loop to load data, report progress, etc.
476 void
477 rxcontrib(const int rstart)
478 {
479 const int totRows = myRCmanager.GetRowMax();
480 FVECT * odarr = (FVECT *)emalloc(sizeof(FVECT)*2*myRCmanager.accum);
481 time_t tstart, last_report;
482 int r = 0;
483
484 while (r < rstart) { // skip input rays already done
485 if (!getRayBundle())
486 goto readerr;
487 r++;
488 }
489 if (report_intvl > 0) { // set up reporting
490 if (r > 0) {
491 sprintf(errmsg, "recovered %.2f%% of total\n",
492 100.*r/totRows);
493 eputs(errmsg);
494 }
495 last_report = tstart = time(0);
496 }
497 while (r < totRows) { // getting to work...
498 time_t tnow;
499 if (!getRayBundle(odarr))
500 goto readerr;
501 if (myRCmanager.ComputeRecord(odarr) < 0)
502 return; // error reported, hopefully...
503 r++;
504 if (report_intvl <= 0)
505 continue;
506 if ((r < totRows) & ((tnow = time(0)) < last_report+report_intvl))
507 continue;
508 sprintf(errmsg, "%.2f%% done after %.3f hours\n",
509 100.*r/totRows, (1./3600.)*(tnow - tstart));
510 eputs(errmsg);
511 last_report = tnow;
512 }
513 efree(odarr);
514 return;
515 readerr:
516 sprintf(errmsg, "unexpected EOF on standard input (record %d of %d)",
517 r, totRows);
518 error(USER, errmsg);
519 }
520
521
522 void
523 wputs( /* warning output function */
524 const char *s
525 )
526 {
527 int lasterrno = errno;
528 eputs(s);
529 errno = lasterrno;
530 }
531
532
533 void
534 eputs( /* put string to stderr */
535 const char *s
536 )
537 {
538 static int midline = 0;
539
540 if (!*s)
541 return;
542 if (!midline++) {
543 fputs(progname, stderr);
544 fputs(": ", stderr);
545 }
546 fputs(s, stderr);
547 if (s[strlen(s)-1] == '\n') {
548 fflush(stderr);
549 midline = 0;
550 }
551 }
552
553
554 /* Quit program */
555 void
556 quit(
557 int code
558 )
559 {
560 if (!code && myRCmanager.Ready()) // clean up on normal exit
561 code = myRCmanager.Cleanup();
562
563 exit(code);
564 }