14 |
|
#include "color.h" |
15 |
|
#include "resolu.h" |
16 |
|
#include "lookup.h" |
17 |
+ |
#include "random.h" |
18 |
|
#include "calcomp.h" |
19 |
|
|
20 |
|
#ifndef MAXMODLIST |
21 |
|
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
22 |
|
#endif |
23 |
|
|
24 |
< |
int treebufsiz = BUFSIZ; /* current tree buffer size */ |
24 |
> |
size_t treebufsiz = BUFSIZ; /* current tree buffer size */ |
25 |
|
|
26 |
|
typedef double DCOLOR[3]; /* double-precision color */ |
27 |
|
|
28 |
|
/* |
29 |
|
* The MODCONT structure is used to accumulate ray contributions |
30 |
|
* for a particular modifier, which may be subdivided into bins |
31 |
< |
* if binv is non-zero. If outspec contains a %s in it, this will |
31 |
> |
* if binv evaluates > 0. If outspec contains a %s in it, this will |
32 |
|
* be replaced with the modifier name. If outspec contains a %d in it, |
33 |
|
* this will be used to create one output file per bin, otherwise all bins |
34 |
|
* will be written to the same file, in order. If the global outfmt |
47 |
|
|
48 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
49 |
|
|
49 |
– |
#define CNT_UNKNOWN 0 /* unknown record length */ |
50 |
– |
#define CNT_PIPE (-1) /* output to a pipe */ |
50 |
|
/* |
51 |
|
* The STREAMOUT structure holds an open FILE pointer and a count of |
52 |
< |
* the number of RGB triplets per record, or CNT_UNKNOWN (0) if |
54 |
< |
* unknown or CNT_PIPE (-1) if writing to a command. |
52 |
> |
* the number of RGB triplets per record, or 0 if unknown. |
53 |
|
*/ |
54 |
|
typedef struct { |
55 |
|
FILE *ofp; /* output file pointer */ |
56 |
+ |
int outpipe; /* output is to a pipe */ |
57 |
|
int reclen; /* triplets/record */ |
58 |
+ |
int xr, yr; /* output resolution for picture */ |
59 |
|
} STREAMOUT; |
60 |
|
|
61 |
|
/* close output stream and free record */ |
63 |
|
closestream(void *p) |
64 |
|
{ |
65 |
|
STREAMOUT *sop = (STREAMOUT *)p; |
66 |
< |
if (sop->reclen == CNT_PIPE) |
67 |
< |
pclose(sop->ofp); |
66 |
> |
int status; |
67 |
> |
if (sop->outpipe) |
68 |
> |
status = pclose(sop->ofp); |
69 |
|
else |
70 |
< |
fclose(sop->ofp); |
70 |
> |
status = fclose(sop->ofp); |
71 |
> |
if (status) |
72 |
> |
error(SYSTEM, "error closing output stream"); |
73 |
|
free(p); |
74 |
|
} |
75 |
|
|
78 |
|
#define OF_MODIFIER 01 |
79 |
|
#define OF_BIN 02 |
80 |
|
|
81 |
< |
STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int bincnt); |
81 |
> |
STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen); |
82 |
|
int ofname(char *oname, const char *ospec, const char *mname, int bn); |
83 |
|
void printheader(FILE *fout, const char *info); |
84 |
< |
void printresolu(FILE *fout); |
84 |
> |
void printresolu(FILE *fout, int xr, int yr); |
85 |
|
|
86 |
|
/* |
87 |
|
* The rcont structure is used to manage i/o with a particular |
93 |
|
struct rtproc *next; /* next in list of processes */ |
94 |
|
SUBPROC pd; /* rtrace pipe descriptors */ |
95 |
|
unsigned long raynum; /* ray number for this tree */ |
96 |
< |
int bsiz; /* ray tree buffer length */ |
96 |
> |
size_t bsiz; /* ray tree buffer length */ |
97 |
|
char *buf; /* ray tree buffer */ |
98 |
< |
int nbr; /* number of bytes from rtrace */ |
98 |
> |
size_t nbr; /* number of bytes from rtrace */ |
99 |
|
}; /* rtrace process buffer */ |
100 |
|
|
101 |
|
/* rtrace command and defaults */ |
102 |
|
char *rtargv[256+2*MAXMODLIST] = { "rtrace", |
103 |
< |
"-dj", ".5", "-dr", "3", |
104 |
< |
"-ab", "1", "-ad", "128", }; |
103 |
> |
"-dj", ".9", "-dr", "3", |
104 |
> |
"-ab", "1", "-ad", "350", }; |
105 |
> |
|
106 |
|
int rtargc = 9; |
107 |
|
/* overriding rtrace options */ |
108 |
< |
char *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0", |
108 |
> |
char *myrtopts[] = { "-h-", "-x", "1", "-y", "0", |
109 |
|
"-dt", "0", "-as", "0", "-aa", "0", NULL }; |
110 |
|
|
111 |
+ |
#define RTCOEFF "-o~~TmWdp" /* compute coefficients only */ |
112 |
+ |
#define RTCONTRIB "-o~~TmVdp" /* compute ray contributions */ |
113 |
+ |
|
114 |
|
struct rtproc rt0; /* head of rtrace process list */ |
115 |
|
|
116 |
|
struct rtproc *rt_unproc = NULL; /* unprocessed ray trees */ |
117 |
|
|
118 |
< |
char persistfn[] = "pfXXXXXX"; /* persist file name */ |
118 |
> |
#define PERSIST_NONE 0 /* no persist file */ |
119 |
> |
#define PERSIST_SINGLE 1 /* user set -P persist */ |
120 |
> |
#define PERSIST_PARALL 2 /* user set -PP persist */ |
121 |
> |
#define PERSIST_OURS 3 /* -PP persist belongs to us */ |
122 |
> |
int persist_state = PERSIST_NONE; /* persist file state */ |
123 |
> |
char persistfn[] = "pfXXXXXX"; /* our persist file name, if set */ |
124 |
|
|
125 |
|
int gargc; /* global argc */ |
126 |
|
char **gargv; /* global argv */ |
132 |
|
int outfmt = 'a'; /* output format */ |
133 |
|
|
134 |
|
int header = 1; /* output header? */ |
135 |
+ |
int force_open = 0; /* truncate existing output? */ |
136 |
+ |
int recover = 0; /* recover previous output? */ |
137 |
+ |
int accumulate = 1; /* input rays per output record */ |
138 |
|
int xres = 0; /* horiz. output resolution */ |
139 |
|
int yres = 0; /* vert. output resolution */ |
140 |
|
|
141 |
+ |
int account; /* current accumulation count */ |
142 |
|
unsigned long raysleft; /* number of rays left to trace */ |
143 |
|
long waitflush; /* how long until next flush */ |
144 |
|
|
158 |
|
|
159 |
|
void init(int np); |
160 |
|
int done_rprocs(struct rtproc *rtp); |
161 |
+ |
void reload_output(void); |
162 |
|
void recover_output(FILE *fin); |
163 |
|
void trace_contribs(FILE *fin); |
164 |
|
struct rtproc *wait_rproc(void); |
168 |
|
|
169 |
|
void put_contrib(const DCOLOR cnt, FILE *fout); |
170 |
|
void add_contrib(const char *modn); |
171 |
< |
void done_contrib(void); |
171 |
> |
void done_contrib(int navg); |
172 |
|
|
173 |
|
/* return number of open rtrace processes */ |
174 |
|
static int |
221 |
|
int |
222 |
|
main(int argc, char *argv[]) |
223 |
|
{ |
224 |
+ |
int contrib = 0; |
225 |
|
int nprocs = 1; |
208 |
– |
int recover = 0; |
226 |
|
char *curout = NULL; |
227 |
|
char *binval = NULL; |
228 |
|
int bincnt = 0; |
231 |
|
/* need at least one argument */ |
232 |
|
if (argc < 2) { |
233 |
|
fprintf(stderr, |
234 |
< |
"Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", |
234 |
> |
"Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", |
235 |
|
argv[0]); |
236 |
|
exit(1); |
237 |
|
} |
247 |
|
while ((j = expandarg(&argc, &argv, i)) > 0) |
248 |
|
; |
249 |
|
if (j < 0) { |
250 |
< |
fprintf(stderr, "%s: cannot expand '%s'", |
250 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
251 |
|
argv[0], argv[i]); |
252 |
|
exit(1); |
253 |
|
} |
254 |
|
if (argv[i][0] == '-') |
255 |
|
switch (argv[i][1]) { |
239 |
– |
case 'r': /* recover output */ |
240 |
– |
if (argv[i][2]) break; |
241 |
– |
recover++; |
242 |
– |
continue; |
256 |
|
case 'n': /* number of processes */ |
257 |
|
if (argv[i][2] || i >= argc-2) break; |
258 |
|
nprocs = atoi(argv[++i]); |
259 |
|
if (nprocs <= 0) |
260 |
|
error(USER, "illegal number of processes"); |
261 |
|
continue; |
262 |
+ |
case 'V': /* output contributions */ |
263 |
+ |
switch (argv[i][2]) { |
264 |
+ |
case '\0': |
265 |
+ |
contrib = !contrib; |
266 |
+ |
continue; |
267 |
+ |
case '+': case '1': |
268 |
+ |
case 'T': case 't': |
269 |
+ |
case 'Y': case 'y': |
270 |
+ |
contrib = 1; |
271 |
+ |
continue; |
272 |
+ |
case '-': case '0': |
273 |
+ |
case 'F': case 'f': |
274 |
+ |
case 'N': case 'n': |
275 |
+ |
contrib = 0; |
276 |
+ |
continue; |
277 |
+ |
} |
278 |
+ |
break; |
279 |
+ |
case 'c': /* input rays per output */ |
280 |
+ |
if (argv[i][2] || i >= argc-2) break; |
281 |
+ |
accumulate = atoi(argv[++i]); |
282 |
+ |
continue; |
283 |
+ |
case 'r': /* recover output */ |
284 |
+ |
if (argv[i][2]) break; |
285 |
+ |
recover = 1; |
286 |
+ |
continue; |
287 |
|
case 'h': /* output header? */ |
288 |
|
switch (argv[i][2]) { |
289 |
|
case '\0': |
301 |
|
continue; |
302 |
|
} |
303 |
|
break; |
304 |
< |
case 'f': /* file or i/o format */ |
304 |
> |
case 'f': /* file or force or format */ |
305 |
|
if (!argv[i][2]) { |
306 |
|
char *fpath; |
307 |
|
if (i >= argc-2) break; |
316 |
|
fcompile(fpath); |
317 |
|
continue; |
318 |
|
} |
319 |
+ |
if (argv[i][2] == 'o') { |
320 |
+ |
force_open = 1; |
321 |
+ |
continue; |
322 |
+ |
} |
323 |
|
setformat(argv[i]+2); |
324 |
|
continue; |
325 |
|
case 'e': /* expression */ |
341 |
|
case 'b': /* bin expression/count */ |
342 |
|
if (i >= argc-2) break; |
343 |
|
if (argv[i][2] == 'n') { |
344 |
< |
bincnt = atoi(argv[++i]); |
344 |
> |
bincnt = (int)(eval(argv[++i]) + .5); |
345 |
|
continue; |
346 |
|
} |
347 |
|
if (argv[i][2]) break; |
359 |
|
rtargv[rtargc++] = argv[++i]; |
360 |
|
addmodfile(argv[i], curout, binval, bincnt); |
361 |
|
continue; |
362 |
+ |
case 'P': /* persist file */ |
363 |
+ |
if (i >= argc-2) break; |
364 |
+ |
persist_state = (argv[i][2] == 'P') ? |
365 |
+ |
PERSIST_PARALL : PERSIST_SINGLE; |
366 |
+ |
rtargv[rtargc++] = argv[i]; |
367 |
+ |
rtargv[rtargc++] = argv[++i]; |
368 |
+ |
continue; |
369 |
|
} |
370 |
|
rtargv[rtargc++] = argv[i]; /* assume rtrace option */ |
371 |
|
} |
372 |
+ |
if (accumulate <= 0) /* no output flushing for single record */ |
373 |
+ |
xres = yres = 0; |
374 |
|
/* set global argument list */ |
375 |
|
gargc = argc; gargv = argv; |
376 |
|
/* add "mandatory" rtrace settings */ |
377 |
|
for (j = 0; myrtopts[j] != NULL; j++) |
378 |
|
rtargv[rtargc++] = myrtopts[j]; |
379 |
+ |
rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF; |
380 |
|
/* just asking for defaults? */ |
381 |
|
if (!strcmp(argv[i], "-defaults")) { |
382 |
< |
char sxres[16], syres[16]; |
382 |
> |
char nps[8], sxres[16], syres[16]; |
383 |
|
char *rtpath; |
384 |
< |
printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); |
384 |
> |
printf("-c %-5d\t\t\t# accumulated rays per record\n", |
385 |
> |
accumulate); |
386 |
> |
printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-', |
387 |
> |
contrib ? "contributions" : "coefficients"); |
388 |
|
fflush(stdout); /* report OUR options */ |
389 |
+ |
rtargv[rtargc++] = "-n"; |
390 |
+ |
sprintf(nps, "%d", nprocs); |
391 |
+ |
rtargv[rtargc++] = nps; |
392 |
|
rtargv[rtargc++] = header ? "-h+" : "-h-"; |
393 |
|
sprintf(fmt, "-f%c%c", inpfmt, outfmt); |
394 |
|
rtargv[rtargc++] = fmt; |
398 |
|
rtargv[rtargc++] = "-y"; |
399 |
|
sprintf(syres, "%d", yres); |
400 |
|
rtargv[rtargc++] = syres; |
343 |
– |
rtargv[rtargc++] = "-oTW"; |
401 |
|
rtargv[rtargc++] = "-defaults"; |
402 |
|
rtargv[rtargc] = NULL; |
403 |
|
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); |
411 |
|
exit(1); |
412 |
|
} |
413 |
|
if (nprocs > 1) { /* add persist file if parallel */ |
414 |
< |
rtargv[rtargc++] = "-PP"; |
415 |
< |
rtargv[rtargc++] = mktemp(persistfn); |
414 |
> |
if (persist_state == PERSIST_SINGLE) |
415 |
> |
error(USER, "use -PP option for multiple processes"); |
416 |
> |
if (persist_state == PERSIST_NONE) { |
417 |
> |
rtargv[rtargc++] = "-PP"; |
418 |
> |
rtargv[rtargc++] = mktemp(persistfn); |
419 |
> |
persist_state = PERSIST_OURS; |
420 |
> |
} |
421 |
|
} |
422 |
|
/* add format string */ |
423 |
|
sprintf(fmt, "-f%cf", inpfmt); |
427 |
|
error(USER, "missing octree argument"); |
428 |
|
rtargv[rtargc++] = octree = argv[i]; |
429 |
|
rtargv[rtargc] = NULL; |
430 |
< |
/* start rtrace */ |
430 |
> |
/* start rtrace & recover if requested */ |
431 |
|
init(nprocs); |
432 |
< |
if (recover) /* perform recovery if requested */ |
433 |
< |
recover_output(stdin); |
434 |
< |
trace_contribs(stdin); /* compute contributions */ |
432 |
> |
/* compute contributions */ |
433 |
> |
trace_contribs(stdin); |
434 |
> |
/* clean up */ |
435 |
|
quit(0); |
436 |
|
} |
437 |
|
|
477 |
|
{ |
478 |
|
int rtstat; |
479 |
|
|
480 |
< |
if (rt0.next != NULL) /* terminate persistent rtrace */ |
480 |
> |
if (persist_state == PERSIST_OURS) /* terminate waiting rtrace */ |
481 |
|
killpersist(); |
482 |
|
/* clean up rtrace process(es) */ |
483 |
|
rtstat = done_rprocs(&rt0); |
544 |
|
raysleft = yres; |
545 |
|
} else |
546 |
|
raysleft = 0; |
547 |
< |
waitflush = xres; |
547 |
> |
if ((account = accumulate) > 0) |
548 |
> |
raysleft *= accumulate; |
549 |
> |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
550 |
> |
if (!recover) |
551 |
> |
return; |
552 |
> |
/* recover previous values */ |
553 |
> |
if (accumulate <= 0) |
554 |
> |
reload_output(); |
555 |
> |
else |
556 |
> |
recover_output(stdin); |
557 |
|
} |
558 |
|
|
559 |
|
/* grow modifier to accommodate more bins */ |
576 |
|
{ |
577 |
|
LUENT *lep = lu_find(&modconttab, modn); |
578 |
|
MODCONT *mp; |
579 |
+ |
int i; |
580 |
|
|
581 |
|
if (lep->data != NULL) { |
582 |
|
sprintf(errmsg, "duplicate modifier '%s'", modn); |
583 |
|
error(USER, errmsg); |
584 |
|
} |
585 |
|
if (nmods >= MAXMODLIST) |
586 |
< |
error(USER, "too many modifiers"); |
586 |
> |
error(INTERNAL, "too many modifiers"); |
587 |
|
modname[nmods++] = modn; /* XXX assumes static string */ |
588 |
|
lep->key = modn; /* XXX assumes static string */ |
589 |
|
mp = (MODCONT *)malloc(sizeof(MODCONT)); |
590 |
|
if (mp == NULL) |
591 |
|
error(SYSTEM, "out of memory in addmodifier"); |
520 |
– |
lep->data = (char *)mp; |
592 |
|
mp->outspec = outf; /* XXX assumes static string */ |
593 |
|
mp->modname = modn; /* XXX assumes static string */ |
594 |
< |
if (binv != NULL) |
595 |
< |
mp->binv = eparse(binv); |
596 |
< |
else |
597 |
< |
mp->binv = eparse("0"); |
598 |
< |
mp->nbins = 1; |
594 |
> |
if (binv == NULL) |
595 |
> |
binv = "0"; /* use single bin if unspecified */ |
596 |
> |
mp->binv = eparse(binv); |
597 |
> |
if (mp->binv->type == NUM) { /* check value if constant */ |
598 |
> |
bincnt = (int)(evalue(mp->binv) + 1.5); |
599 |
> |
if (bincnt != 1) { |
600 |
> |
sprintf(errmsg, "illegal non-zero constant for bin (%s)", |
601 |
> |
binv); |
602 |
> |
error(USER, errmsg); |
603 |
> |
} |
604 |
> |
} |
605 |
> |
mp->nbins = 1; /* initialize results holder */ |
606 |
|
setcolor(mp->cbin[0], 0., 0., 0.); |
607 |
< |
if (mp->binv->type == NUM) /* assume one bin if constant */ |
530 |
< |
bincnt = 1; |
531 |
< |
else if (bincnt > 1) |
607 |
> |
if (bincnt > 1) |
608 |
|
mp = growmodifier(mp, bincnt); |
609 |
< |
if (bincnt > 0) /* allocate output stream */ |
610 |
< |
getostream(mp->outspec, mp->modname, 0, bincnt); |
609 |
> |
lep->data = (char *)mp; |
610 |
> |
/* allocate output streams */ |
611 |
> |
for (i = bincnt; i-- > 0; ) |
612 |
> |
getostream(mp->outspec, mp->modname, i, 1); |
613 |
|
return mp; |
614 |
|
} |
615 |
|
|
667 |
|
mnp = cp; |
668 |
|
break; |
669 |
|
case 'd': |
670 |
+ |
case 'i': |
671 |
+ |
case 'o': |
672 |
+ |
case 'x': |
673 |
+ |
case 'X': |
674 |
|
if (bnp != NULL) |
675 |
|
return -1; |
676 |
|
bnp = cp; |
733 |
|
|
734 |
|
/* write resolution string to given output stream */ |
735 |
|
void |
736 |
< |
printresolu(FILE *fout) |
736 |
> |
printresolu(FILE *fout, int xr, int yr) |
737 |
|
{ |
738 |
< |
if (xres > 0) { |
739 |
< |
if (yres > 0) /* resolution string */ |
658 |
< |
fprtresolu(xres, yres, fout); |
659 |
< |
fflush(fout); |
660 |
< |
} |
738 |
> |
if ((xr > 0) & (yr > 0)) /* resolution string */ |
739 |
> |
fprtresolu(xr, yr, fout); |
740 |
|
} |
741 |
|
|
742 |
< |
/* Get output stream pointer (open and write header if new and bincnt==0) */ |
742 |
> |
/* Get output stream pointer (open and write header if new and noopen==0) */ |
743 |
|
STREAMOUT * |
744 |
< |
getostream(const char *ospec, const char *mname, int bn, int bincnt) |
744 |
> |
getostream(const char *ospec, const char *mname, int bn, int noopen) |
745 |
|
{ |
746 |
+ |
static const DCOLOR nocontrib = BLKCOLOR; |
747 |
|
static STREAMOUT stdos; |
748 |
|
int ofl; |
749 |
|
char oname[1024]; |
751 |
|
STREAMOUT *sop; |
752 |
|
|
753 |
|
if (ospec == NULL) { /* use stdout? */ |
754 |
< |
if (!bincnt && !using_stdout) { |
675 |
< |
stdos.reclen = 0; |
754 |
> |
if (!noopen && !using_stdout) { |
755 |
|
if (outfmt != 'a') |
756 |
|
SET_FILE_BINARY(stdout); |
757 |
|
if (header) |
758 |
|
printheader(stdout, NULL); |
759 |
< |
printresolu(stdout); |
759 |
> |
printresolu(stdout, xres, yres); |
760 |
> |
if (waitflush > 0) |
761 |
> |
fflush(stdout); |
762 |
> |
stdos.xr = xres; stdos.yr = yres; |
763 |
|
using_stdout = 1; |
764 |
|
} |
765 |
|
stdos.ofp = stdout; |
766 |
< |
stdos.reclen += bincnt; |
766 |
> |
stdos.reclen += noopen; |
767 |
|
return &stdos; |
768 |
|
} |
769 |
|
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
776 |
|
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
777 |
|
sop = (STREAMOUT *)lep->data; |
778 |
|
if (sop == NULL) { /* allocate stream */ |
697 |
– |
if (ofl & OF_BIN && bincnt > 1) /* don't overcount bins */ |
698 |
– |
bincnt = 1; |
779 |
|
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); |
780 |
|
if (sop == NULL) |
781 |
|
error(SYSTEM, "out of memory in getostream"); |
782 |
< |
sop->reclen = oname[0] == '!' ? CNT_PIPE : 0; |
783 |
< |
sop->ofp = NULL; /* open iff bincnt==0 */ |
782 |
> |
sop->outpipe = oname[0] == '!'; |
783 |
> |
sop->reclen = 0; |
784 |
> |
sop->ofp = NULL; /* open iff noopen==0 */ |
785 |
> |
sop->xr = xres; sop->yr = yres; |
786 |
|
lep->data = (char *)sop; |
787 |
+ |
if (!sop->outpipe & !force_open & !recover && |
788 |
+ |
access(oname, F_OK) == 0) { |
789 |
+ |
errno = EEXIST; /* file exists */ |
790 |
+ |
goto openerr; |
791 |
+ |
} |
792 |
|
} |
793 |
< |
if (!bincnt && sop->ofp == NULL) { /* open output stream */ |
794 |
< |
int i; |
793 |
> |
if (!noopen && sop->ofp == NULL) { /* open output stream */ |
794 |
> |
long i; |
795 |
|
if (oname[0] == '!') /* output to command */ |
796 |
|
sop->ofp = popen(oname+1, "w"); |
797 |
< |
else |
797 |
> |
else /* else open file */ |
798 |
|
sop->ofp = fopen(oname, "w"); |
799 |
< |
if (sop->ofp == NULL) { |
800 |
< |
sprintf(errmsg, "cannot open '%s' for writing", oname); |
714 |
< |
error(SYSTEM, errmsg); |
715 |
< |
} |
799 |
> |
if (sop->ofp == NULL) |
800 |
> |
goto openerr; |
801 |
|
if (outfmt != 'a') |
802 |
|
SET_FILE_BINARY(sop->ofp); |
803 |
|
if (header) { |
804 |
|
char info[512]; |
805 |
|
char *cp = info; |
806 |
< |
if (ofl & OF_MODIFIER) { |
806 |
> |
if (ofl & OF_MODIFIER || sop->reclen == 1) { |
807 |
|
sprintf(cp, "MODIFIER=%s\n", mname); |
808 |
|
while (*cp) ++cp; |
809 |
|
} |
814 |
|
*cp = '\0'; |
815 |
|
printheader(sop->ofp, info); |
816 |
|
} |
817 |
< |
printresolu(sop->ofp); |
817 |
> |
if (accumulate > 0) { /* global resolution */ |
818 |
> |
sop->xr = xres; sop->yr = yres; |
819 |
> |
} |
820 |
> |
printresolu(sop->ofp, sop->xr, sop->yr); |
821 |
|
/* play catch-up */ |
822 |
< |
for (i = 0; i < lastdone; i++) { |
823 |
< |
static const DCOLOR nocontrib = BLKCOLOR; |
824 |
< |
put_contrib(nocontrib, sop->ofp); |
822 |
> |
for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) { |
823 |
> |
int j = sop->reclen; |
824 |
> |
if (j <= 0) j = 1; |
825 |
> |
while (j--) |
826 |
> |
put_contrib(nocontrib, sop->ofp); |
827 |
|
if (outfmt == 'a') |
828 |
|
putc('\n', sop->ofp); |
829 |
|
} |
830 |
< |
if (xres > 0) |
830 |
> |
if (waitflush > 0) |
831 |
|
fflush(sop->ofp); |
832 |
|
} |
833 |
< |
if (sop->reclen != CNT_PIPE) /* add bincnt to count */ |
834 |
< |
sop->reclen += bincnt; |
835 |
< |
return sop; /* return open stream */ |
833 |
> |
sop->reclen += noopen; /* add to length if noopen */ |
834 |
> |
return sop; /* return output stream */ |
835 |
> |
openerr: |
836 |
> |
sprintf(errmsg, "cannot open '%s' for writing", oname); |
837 |
> |
error(SYSTEM, errmsg); |
838 |
> |
return NULL; /* pro forma return */ |
839 |
|
} |
840 |
|
|
841 |
|
/* read input ray into buffer */ |
842 |
|
int |
843 |
|
getinp(char *buf, FILE *fp) |
844 |
|
{ |
845 |
+ |
double dv[3], *dvp; |
846 |
+ |
float *fvp; |
847 |
|
char *cp; |
848 |
|
int i; |
849 |
|
|
852 |
|
cp = buf; /* make sure we get 6 floats */ |
853 |
|
for (i = 0; i < 6; i++) { |
854 |
|
if (fgetword(cp, buf+127-cp, fp) == NULL) |
855 |
< |
return 0; |
855 |
> |
return -1; |
856 |
> |
if (i >= 3) |
857 |
> |
dv[i-3] = atof(cp); |
858 |
|
if ((cp = fskip(cp)) == NULL || *cp) |
859 |
< |
return 0; |
859 |
> |
return -1; |
860 |
|
*cp++ = ' '; |
861 |
|
} |
862 |
|
getc(fp); /* get/put eol */ |
863 |
|
*cp-- = '\0'; *cp = '\n'; |
864 |
+ |
if (DOT(dv,dv) <= FTINY*FTINY) |
865 |
+ |
return 0; /* dummy ray */ |
866 |
|
return strlen(buf); |
867 |
|
case 'f': |
868 |
|
if (fread(buf, sizeof(float), 6, fp) < 6) |
869 |
< |
return 0; |
869 |
> |
return -1; |
870 |
> |
fvp = (float *)buf + 3; |
871 |
> |
if (DOT(fvp,fvp) <= FTINY*FTINY) |
872 |
> |
return 0; /* dummy ray */ |
873 |
|
return sizeof(float)*6; |
874 |
|
case 'd': |
875 |
|
if (fread(buf, sizeof(double), 6, fp) < 6) |
876 |
< |
return 0; |
876 |
> |
return -1; |
877 |
> |
dvp = (double *)buf + 3; |
878 |
> |
if (DOT(dvp,dvp) <= FTINY*FTINY) |
879 |
> |
return 0; /* dummy ray */ |
880 |
|
return sizeof(double)*6; |
881 |
|
} |
882 |
|
error(INTERNAL, "botched input format"); |
883 |
< |
return 0; /* pro forma return */ |
883 |
> |
return -1; /* pro forma return */ |
884 |
|
} |
885 |
|
|
886 |
|
static float rparams[9]; /* traced ray parameters */ |
944 |
|
fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]); |
945 |
|
break; |
946 |
|
case 'f': |
947 |
< |
fv[0] = cnt[0]; |
843 |
< |
fv[1] = cnt[1]; |
844 |
< |
fv[2] = cnt[2]; |
947 |
> |
copycolor(fv, cnt); |
948 |
|
fwrite(fv, sizeof(float), 3, fout); |
949 |
|
break; |
950 |
|
case 'd': |
957 |
|
default: |
958 |
|
error(INTERNAL, "botched output format"); |
959 |
|
} |
960 |
+ |
if (waitflush < 0 && frandom() < 0.001) |
961 |
+ |
fflush(fout); /* staggers writes */ |
962 |
|
} |
963 |
|
|
964 |
< |
/* output ray tallies and clear for next primary */ |
964 |
> |
/* output ray tallies and clear for next accumulation */ |
965 |
|
void |
966 |
< |
done_contrib(void) |
966 |
> |
done_contrib(int navg) |
967 |
|
{ |
968 |
+ |
double sf = 1.; |
969 |
|
int i, j; |
970 |
|
MODCONT *mp; |
971 |
|
STREAMOUT *sop; |
972 |
+ |
/* set average scaling */ |
973 |
+ |
if (navg > 1) |
974 |
+ |
sf = 1. / (double)navg; |
975 |
|
/* output modifiers in order */ |
976 |
|
for (i = 0; i < nmods; i++) { |
977 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
978 |
+ |
if (navg > 1) /* average scaling */ |
979 |
+ |
for (j = mp->nbins; j--; ) |
980 |
+ |
scalecolor(mp->cbin[j], sf); |
981 |
|
sop = getostream(mp->outspec, mp->modname, 0,0); |
982 |
|
put_contrib(mp->cbin[0], sop->ofp); |
983 |
|
if (mp->nbins > 3 && /* minor optimization */ |
988 |
|
for (j = 1; j < mp->nbins; j++) |
989 |
|
put_contrib(mp->cbin[j], |
990 |
|
getostream(mp->outspec,mp->modname,j,0)->ofp); |
991 |
< |
/* clear for next ray tree */ |
991 |
> |
/* clear for next time */ |
992 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
993 |
|
} |
994 |
|
--waitflush; /* terminate records */ |
996 |
|
if (using_stdout & (outfmt == 'a')) |
997 |
|
putc('\n', stdout); |
998 |
|
if (!waitflush) { |
999 |
< |
waitflush = xres; |
999 |
> |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres; |
1000 |
|
if (using_stdout) |
1001 |
|
fflush(stdout); |
1002 |
|
} |
1047 |
|
if (!n || !(isalpha(*cp) | (*cp == '_'))) |
1048 |
|
error(USER, "bad modifier name from rtrace"); |
1049 |
|
/* get modifier name */ |
1050 |
< |
while (n > 0 && *cp != '\t') { |
1050 |
> |
while (n > 1 && *cp != '\t') { |
1051 |
> |
if (mnp - modname >= sizeof(modname)-2) |
1052 |
> |
error(INTERNAL, "modifier name too long"); |
1053 |
|
*mnp++ = *cp++; n--; |
1054 |
|
} |
1055 |
|
*mnp = '\0'; |
1061 |
|
cp += sizeof(float)*9; n -= sizeof(float)*9; |
1062 |
|
add_contrib(modname); |
1063 |
|
} |
1064 |
< |
done_contrib(); /* sum up contributions & output */ |
1064 |
> |
/* time to produce record? */ |
1065 |
> |
if (account > 0 && !--account) |
1066 |
> |
done_contrib(account = accumulate); |
1067 |
|
lastdone = rtp->raynum; |
1068 |
< |
free(rtp->buf); /* free up buffer space */ |
1068 |
> |
if (rtp->buf != NULL) /* free up buffer space */ |
1069 |
> |
free(rtp->buf); |
1070 |
|
rt_unproc = rtp->next; |
1071 |
|
free(rtp); /* done with this ray tree */ |
1072 |
|
} |
1078 |
|
{ |
1079 |
|
struct rtproc *rtfree = NULL; |
1080 |
|
fd_set readset, errset; |
1081 |
< |
int nr; |
1081 |
> |
ssize_t nr; |
1082 |
|
struct rtproc *rt; |
1083 |
|
int n; |
1084 |
|
|
1150 |
|
void |
1151 |
|
trace_contribs(FILE *fin) |
1152 |
|
{ |
1153 |
+ |
static int ignore_warning_given = 0; |
1154 |
|
char inpbuf[128]; |
1155 |
|
int iblen; |
1156 |
|
struct rtproc *rtp; |
1157 |
|
/* loop over input */ |
1158 |
< |
while ((iblen = getinp(inpbuf, fin)) > 0) { |
1159 |
< |
if (lastray+1 < lastray || /* need reset? */ |
1160 |
< |
queue_length() > 5*nrtprocs()) { |
1158 |
> |
while ((iblen = getinp(inpbuf, fin)) >= 0) { |
1159 |
> |
if (!iblen && accumulate != 1) { |
1160 |
> |
if (!ignore_warning_given++) |
1161 |
> |
error(WARNING, |
1162 |
> |
"dummy ray(s) ignored during accumulation\n"); |
1163 |
> |
continue; |
1164 |
> |
} |
1165 |
> |
if (!iblen || /* need flush/reset? */ |
1166 |
> |
queue_length() > 10*nrtprocs() || |
1167 |
> |
lastray+1 < lastray) { |
1168 |
|
while (wait_rproc() != NULL) |
1169 |
|
process_queue(); |
1170 |
< |
lastdone = lastray = 0; |
1170 |
> |
if (lastray+1 < lastray) |
1171 |
> |
lastdone = lastray = 0; |
1172 |
|
} |
1173 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
1174 |
< |
rtp->raynum = ++lastray; /* assign ray to it */ |
1175 |
< |
writebuf(rtp->pd.w, inpbuf, iblen); |
1176 |
< |
if (raysleft && !--raysleft) |
1177 |
< |
break; |
1174 |
> |
rtp->raynum = ++lastray; /* assign ray */ |
1175 |
> |
if (iblen) { /* trace ray if valid */ |
1176 |
> |
writebuf(rtp->pd.w, inpbuf, iblen); |
1177 |
> |
} else { /* else bypass dummy ray */ |
1178 |
> |
queue_raytree(rtp); /* queue empty ray/record */ |
1179 |
> |
if ((yres <= 0) | (xres <= 0)) |
1180 |
> |
waitflush = 1; /* flush right after */ |
1181 |
> |
} |
1182 |
|
process_queue(); /* catch up with results */ |
1183 |
+ |
if (raysleft && !--raysleft) |
1184 |
+ |
break; /* preemptive EOI */ |
1185 |
|
} |
1186 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
1187 |
|
process_queue(); |
1188 |
+ |
if (accumulate <= 0) |
1189 |
+ |
done_contrib(0); /* output tallies */ |
1190 |
+ |
else if (account < accumulate) { |
1191 |
+ |
error(WARNING, "partial accumulation in final record"); |
1192 |
+ |
done_contrib(accumulate - account); |
1193 |
+ |
} |
1194 |
|
if (raysleft) |
1195 |
|
error(USER, "unexpected EOF on input"); |
1196 |
|
lu_done(&ofiletab); /* close output files */ |
1197 |
|
} |
1198 |
|
|
1199 |
+ |
/* get ray contribution from previous file */ |
1200 |
+ |
static int |
1201 |
+ |
get_contrib(DCOLOR cnt, FILE *finp) |
1202 |
+ |
{ |
1203 |
+ |
COLOR fv; |
1204 |
+ |
COLR cv; |
1205 |
+ |
|
1206 |
+ |
switch (outfmt) { |
1207 |
+ |
case 'a': |
1208 |
+ |
return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3); |
1209 |
+ |
case 'f': |
1210 |
+ |
if (fread(fv, sizeof(fv[0]), 3, finp) != 3) |
1211 |
+ |
return(0); |
1212 |
+ |
copycolor(cnt, fv); |
1213 |
+ |
return(1); |
1214 |
+ |
case 'd': |
1215 |
+ |
return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3); |
1216 |
+ |
case 'c': |
1217 |
+ |
if (fread(cv, sizeof(cv), 1, finp) != 1) |
1218 |
+ |
return(0); |
1219 |
+ |
colr_color(fv, cv); |
1220 |
+ |
copycolor(cnt, fv); |
1221 |
+ |
return(1); |
1222 |
+ |
default: |
1223 |
+ |
error(INTERNAL, "botched output format"); |
1224 |
+ |
} |
1225 |
+ |
return(0); /* pro forma return */ |
1226 |
+ |
} |
1227 |
+ |
|
1228 |
+ |
/* close output file opened for input */ |
1229 |
+ |
static int |
1230 |
+ |
myclose(const LUENT *e, void *p) |
1231 |
+ |
{ |
1232 |
+ |
STREAMOUT *sop = (STREAMOUT *)e->data; |
1233 |
+ |
|
1234 |
+ |
if (sop->ofp == NULL) |
1235 |
+ |
return(0); |
1236 |
+ |
fclose(sop->ofp); |
1237 |
+ |
sop->ofp = NULL; |
1238 |
+ |
return(0); |
1239 |
+ |
} |
1240 |
+ |
|
1241 |
+ |
/* load previously accumulated values */ |
1242 |
+ |
void |
1243 |
+ |
reload_output(void) |
1244 |
+ |
{ |
1245 |
+ |
int i, j; |
1246 |
+ |
MODCONT *mp; |
1247 |
+ |
int ofl; |
1248 |
+ |
char oname[1024]; |
1249 |
+ |
char *fmode = "rb"; |
1250 |
+ |
char *outvfmt; |
1251 |
+ |
LUENT *ment, *oent; |
1252 |
+ |
int xr, yr; |
1253 |
+ |
STREAMOUT sout; |
1254 |
+ |
DCOLOR rgbv; |
1255 |
+ |
|
1256 |
+ |
switch (outfmt) { |
1257 |
+ |
case 'a': |
1258 |
+ |
outvfmt = "ascii"; |
1259 |
+ |
fmode = "r"; |
1260 |
+ |
break; |
1261 |
+ |
case 'f': |
1262 |
+ |
outvfmt = "float"; |
1263 |
+ |
break; |
1264 |
+ |
case 'd': |
1265 |
+ |
outvfmt = "double"; |
1266 |
+ |
break; |
1267 |
+ |
case 'c': |
1268 |
+ |
outvfmt = COLRFMT; |
1269 |
+ |
break; |
1270 |
+ |
default: |
1271 |
+ |
error(INTERNAL, "botched output format"); |
1272 |
+ |
return; |
1273 |
+ |
} |
1274 |
+ |
/* reload modifier values */ |
1275 |
+ |
for (i = 0; i < nmods; i++) { |
1276 |
+ |
ment = lu_find(&modconttab,modname[i]); |
1277 |
+ |
mp = (MODCONT *)ment->data; |
1278 |
+ |
if (mp->outspec == NULL) |
1279 |
+ |
error(USER, "cannot reload from stdout"); |
1280 |
+ |
if (mp->outspec[0] == '!') |
1281 |
+ |
error(USER, "cannot reload from command"); |
1282 |
+ |
for (j = 0; ; j++) { /* load each modifier bin */ |
1283 |
+ |
ofl = ofname(oname, mp->outspec, mp->modname, j); |
1284 |
+ |
if (ofl < 0) |
1285 |
+ |
error(USER, "bad output file specification"); |
1286 |
+ |
oent = lu_find(&ofiletab, oname); |
1287 |
+ |
if (oent->data != NULL) { |
1288 |
+ |
sout = *(STREAMOUT *)oent->data; |
1289 |
+ |
} else { |
1290 |
+ |
sout.reclen = 0; |
1291 |
+ |
sout.outpipe = 0; |
1292 |
+ |
sout.xr = xres; sout.yr = yres; |
1293 |
+ |
sout.ofp = NULL; |
1294 |
+ |
} |
1295 |
+ |
if (sout.ofp == NULL) { /* open output as input */ |
1296 |
+ |
sout.ofp = fopen(oname, fmode); |
1297 |
+ |
if (sout.ofp == NULL) { |
1298 |
+ |
if (j) |
1299 |
+ |
break; /* assume end of modifier */ |
1300 |
+ |
sprintf(errmsg, "missing reload file '%s'", |
1301 |
+ |
oname); |
1302 |
+ |
error(WARNING, errmsg); |
1303 |
+ |
break; |
1304 |
+ |
} |
1305 |
+ |
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { |
1306 |
+ |
sprintf(errmsg, "format mismatch for '%s'", |
1307 |
+ |
oname); |
1308 |
+ |
error(USER, errmsg); |
1309 |
+ |
} |
1310 |
+ |
if ((sout.xr > 0) & (sout.yr > 0) && |
1311 |
+ |
(!fscnresolu(&xr, &yr, sout.ofp) || |
1312 |
+ |
(xr != sout.xr) | |
1313 |
+ |
(yr != sout.yr))) { |
1314 |
+ |
sprintf(errmsg, "resolution mismatch for '%s'", |
1315 |
+ |
oname); |
1316 |
+ |
error(USER, errmsg); |
1317 |
+ |
} |
1318 |
+ |
} |
1319 |
+ |
/* read in RGB value */ |
1320 |
+ |
if (!get_contrib(rgbv, sout.ofp)) { |
1321 |
+ |
if (!j) { |
1322 |
+ |
fclose(sout.ofp); |
1323 |
+ |
break; /* ignore empty file */ |
1324 |
+ |
} |
1325 |
+ |
if (j < mp->nbins) { |
1326 |
+ |
sprintf(errmsg, "missing data in '%s'", |
1327 |
+ |
oname); |
1328 |
+ |
error(USER, errmsg); |
1329 |
+ |
} |
1330 |
+ |
break; |
1331 |
+ |
} |
1332 |
+ |
if (j >= mp->nbins) /* grow modifier size */ |
1333 |
+ |
ment->data = (char *)(mp = growmodifier(mp, j+1)); |
1334 |
+ |
copycolor(mp->cbin[j], rgbv); |
1335 |
+ |
if (oent->key == NULL) /* new file entry */ |
1336 |
+ |
oent->key = strcpy((char *) |
1337 |
+ |
malloc(strlen(oname)+1), oname); |
1338 |
+ |
if (oent->data == NULL) |
1339 |
+ |
oent->data = (char *)malloc(sizeof(STREAMOUT)); |
1340 |
+ |
*(STREAMOUT *)oent->data = sout; |
1341 |
+ |
} |
1342 |
+ |
} |
1343 |
+ |
lu_doall(&ofiletab, myclose, NULL); /* close all files */ |
1344 |
+ |
} |
1345 |
+ |
|
1346 |
|
/* seek on the given output file */ |
1347 |
|
static int |
1348 |
|
myseeko(const LUENT *e, void *p) |
1356 |
|
sprintf(errmsg, "seek error on file '%s'", e->key); |
1357 |
|
error(SYSTEM, errmsg); |
1358 |
|
} |
1359 |
+ |
return 0; |
1360 |
|
} |
1361 |
|
|
1362 |
|
/* recover output if possible */ |
1412 |
|
sout = *(STREAMOUT *)oent->data; |
1413 |
|
} else { |
1414 |
|
sout.reclen = 0; |
1415 |
+ |
sout.outpipe = 0; |
1416 |
|
sout.ofp = NULL; |
1417 |
|
} |
1418 |
|
if (sout.ofp != NULL) { /* already open? */ |
1427 |
|
break; /* assume end of modifier */ |
1428 |
|
sprintf(errmsg, "missing recover file '%s'", |
1429 |
|
oname); |
1430 |
< |
error(USER, errmsg); |
1430 |
> |
error(WARNING, errmsg); |
1431 |
> |
break; |
1432 |
|
} |
1433 |
|
nvals = lseek(fileno(sout.ofp), 0, SEEK_END); |
1434 |
|
if (nvals <= 0) { |
1436 |
|
fclose(sout.ofp); |
1437 |
|
break; |
1438 |
|
} |
1439 |
< |
if (!sout.reclen) { /* unspecified record length */ |
1439 |
> |
if (!sout.reclen) { |
1440 |
|
if (!(ofl & OF_BIN)) { |
1441 |
|
sprintf(errmsg, |
1442 |
< |
"guessing 1 value per record in '%s'", |
1442 |
> |
"need -bn to recover file '%s'", |
1443 |
|
oname); |
1444 |
< |
error(WARNING, errmsg); |
1444 |
> |
error(USER, errmsg); |
1445 |
|
} |
1446 |
|
recsiz = outvsiz; |
1447 |
|
} else |
1453 |
|
oname); |
1454 |
|
error(USER, errmsg); |
1455 |
|
} |
1456 |
< |
if ((xres > 0) & (yres > 0) && |
1456 |
> |
sout.xr = xres; sout.yr = yres; |
1457 |
> |
if ((sout.xr > 0) & (sout.yr > 0) && |
1458 |
|
(!fscnresolu(&xr, &yr, sout.ofp) || |
1459 |
< |
xr != xres || |
1460 |
< |
yr != yres)) { |
1459 |
> |
(xr != sout.xr) | |
1460 |
> |
(yr != sout.yr))) { |
1461 |
|
sprintf(errmsg, "resolution mismatch for '%s'", |
1462 |
|
oname); |
1463 |
|
error(USER, errmsg); |
1486 |
|
error(WARNING, "no output files to recover"); |
1487 |
|
return; |
1488 |
|
} |
1489 |
< |
if (raysleft && lastout >= raysleft) { |
1489 |
> |
if (raysleft && lastout >= raysleft/accumulate) { |
1490 |
|
error(WARNING, "output appears to be complete"); |
1491 |
|
/* XXX should read & discard input? */ |
1492 |
|
quit(0); |
1496 |
|
lu_doall(&ofiletab, myseeko, &nvals); |
1497 |
|
/* skip repeated input */ |
1498 |
|
for (nvals = 0; nvals < lastout; nvals++) |
1499 |
< |
if (getinp(oname, fin) <= 0) |
1499 |
> |
if (getinp(oname, fin) < 0) |
1500 |
|
error(USER, "unexpected EOF on input"); |
1501 |
< |
lastray = lastdone = (unsigned long)lastout; |
1501 |
> |
lastray = lastdone = (unsigned long)lastout * accumulate; |
1502 |
|
if (raysleft) |
1503 |
|
raysleft -= lastray; |
1504 |
|
} |