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