5 |
|
* Gather rtrace output to compute contributions from particular sources |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "standard.h" |
9 |
|
#include <ctype.h> |
10 |
< |
#include "rtio.h" |
10 |
< |
#include "rterror.h" |
10 |
> |
#include <signal.h> |
11 |
|
#include "platform.h" |
12 |
|
#include "rtprocess.h" |
13 |
|
#include "selcall.h" |
16 |
|
#include "lookup.h" |
17 |
|
#include "calcomp.h" |
18 |
|
|
19 |
< |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
20 |
< |
#undef getc |
21 |
< |
#undef putc |
22 |
< |
#define getc getc_unlocked |
23 |
< |
#define putc putc_unlocked |
24 |
< |
#define ferror ferror_unlocked |
19 |
> |
#ifndef MAXMODLIST |
20 |
> |
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
21 |
|
#endif |
22 |
|
|
27 |
– |
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
28 |
– |
|
23 |
|
int treebufsiz = BUFSIZ; /* current tree buffer size */ |
24 |
|
|
25 |
+ |
typedef double DCOLOR[3]; /* double-precision color */ |
26 |
+ |
|
27 |
|
/* |
28 |
< |
* The modcont structure is used to accumulate ray contributions |
28 |
> |
* The MODCONT structure is used to accumulate ray contributions |
29 |
|
* for a particular modifier, which may be subdivided into bins |
30 |
< |
* if binv is non-NULL. If outspec contains a %s in it, this will |
30 |
> |
* if binv is non-zero. If outspec contains a %s in it, this will |
31 |
|
* be replaced with the modifier name. If outspec contains a %d in it, |
32 |
|
* this will be used to create one output file per bin, otherwise all bins |
33 |
|
* will be written to the same file, in order. If the global outfmt |
39 |
|
const char *modname; /* modifier name */ |
40 |
|
EPNODE *binv; /* bin value expression */ |
41 |
|
int nbins; /* number of accumulation bins */ |
42 |
< |
COLOR cbin[1]; /* contribution bins (extends struct) */ |
42 |
> |
DCOLOR cbin[1]; /* contribution bins (extends struct) */ |
43 |
|
} MODCONT; /* modifier contribution */ |
44 |
|
|
45 |
|
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); } |
46 |
|
|
47 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
48 |
|
|
49 |
< |
/* close output stream */ |
50 |
< |
static void closefile(void *p) { fclose((FILE *)p); } |
49 |
> |
#define CNT_UNKNOWN 0 /* unknown record length */ |
50 |
> |
#define CNT_PIPE (-1) /* output to a pipe */ |
51 |
> |
/* |
52 |
> |
* The STREAMOUT structure holds an open FILE pointer and a count of |
53 |
> |
* the number of RGB triplets per record, or CNT_UNKNOWN (0) if |
54 |
> |
* unknown or CNT_PIPE (-1) if writing to a command. |
55 |
> |
*/ |
56 |
> |
typedef struct { |
57 |
> |
FILE *ofp; /* output file pointer */ |
58 |
> |
int reclen; /* triplets/record */ |
59 |
> |
} STREAMOUT; |
60 |
|
|
61 |
< |
LUTAB ofiletab = LU_SINIT(free,closefile); /* output file table */ |
61 |
> |
/* close output stream and free record */ |
62 |
> |
static void |
63 |
> |
closestream(void *p) |
64 |
> |
{ |
65 |
> |
STREAMOUT *sop = (STREAMOUT *)p; |
66 |
> |
int status; |
67 |
> |
if (sop->reclen == CNT_PIPE) |
68 |
> |
status = pclose(sop->ofp); |
69 |
> |
else |
70 |
> |
status = fclose(sop->ofp); |
71 |
> |
if (status) |
72 |
> |
error(SYSTEM, "error closing output stream"); |
73 |
> |
free(p); |
74 |
> |
} |
75 |
|
|
76 |
< |
FILE *getofile(const char *ospec, const char *mname, int bn); |
76 |
> |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ |
77 |
|
|
78 |
+ |
#define OF_MODIFIER 01 |
79 |
+ |
#define OF_BIN 02 |
80 |
+ |
|
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); |
85 |
+ |
|
86 |
|
/* |
87 |
|
* The rcont structure is used to manage i/o with a particular |
88 |
|
* rtrace child process. Input is passed unchanged from stdin, |
92 |
|
struct rtproc { |
93 |
|
struct rtproc *next; /* next in list of processes */ |
94 |
|
SUBPROC pd; /* rtrace pipe descriptors */ |
95 |
< |
unsigned long raynum; /* ray number for this buffer */ |
96 |
< |
int bsiz; /* rtrace buffer length */ |
97 |
< |
char *buf; /* rtrace i/o buffer */ |
98 |
< |
char *bpos; /* current buffer position */ |
99 |
< |
}; /* rtrace process */ |
95 |
> |
unsigned long raynum; /* ray number for this tree */ |
96 |
> |
int bsiz; /* ray tree buffer length */ |
97 |
> |
char *buf; /* ray tree buffer */ |
98 |
> |
int nbr; /* number of bytes from rtrace */ |
99 |
> |
}; /* rtrace process buffer */ |
100 |
|
|
101 |
|
/* rtrace command and defaults */ |
102 |
< |
char *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3", |
103 |
< |
"-ab", "1", "-ad", "512", }; |
102 |
> |
char *rtargv[256+2*MAXMODLIST] = { "rtrace", |
103 |
> |
"-dj", ".5", "-dr", "3", |
104 |
> |
"-ab", "1", "-ad", "128", }; |
105 |
|
int rtargc = 9; |
106 |
|
/* overriding rtrace options */ |
107 |
< |
char *myrtopts[] = { "-o-TmWdp", "-h-", |
108 |
< |
"-x", "1", "-y", "0", NULL }; |
107 |
> |
char *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0", |
108 |
> |
"-dt", "0", "-as", "0", "-aa", "0", NULL }; |
109 |
|
|
110 |
|
struct rtproc rt0; /* head of rtrace process list */ |
111 |
|
|
112 |
|
struct rtproc *rt_unproc = NULL; /* unprocessed ray trees */ |
113 |
|
|
114 |
|
char persistfn[] = "pfXXXXXX"; /* persist file name */ |
88 |
– |
char fmt[8]; /* rtrace i/o format */ |
115 |
|
|
116 |
|
int gargc; /* global argc */ |
117 |
|
char **gargv; /* global argv */ |
123 |
|
int outfmt = 'a'; /* output format */ |
124 |
|
|
125 |
|
int header = 1; /* output header? */ |
126 |
+ |
int force_open = 0; /* truncate existing output? */ |
127 |
|
int xres = 0; /* horiz. output resolution */ |
128 |
|
int yres = 0; /* vert. output resolution */ |
129 |
|
|
130 |
< |
long raysleft; /* number of rays left to trace */ |
130 |
> |
unsigned long raysleft; /* number of rays left to trace */ |
131 |
|
long waitflush; /* how long until next flush */ |
132 |
|
|
133 |
|
unsigned long lastray = 0; /* last ray number sent */ |
134 |
|
unsigned long lastdone = 0; /* last ray processed */ |
135 |
|
|
136 |
+ |
int using_stdout = 0; /* are we using stdout? */ |
137 |
+ |
|
138 |
|
const char *modname[MAXMODLIST]; /* ordered modifier name list */ |
139 |
|
int nmods = 0; /* number of modifiers */ |
140 |
|
|
141 |
< |
MODCONT *addmodifier(char *modn, char *outf, char *binv); |
141 |
> |
#define queue_length() (lastray - lastdone) |
142 |
|
|
143 |
< |
int done_rprocs(struct rtproc *rtp); |
143 |
> |
MODCONT *growmodifier(MODCONT *mp, int nb); |
144 |
> |
MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt); |
145 |
> |
void addmodfile(char *fname, char *outf, char *binv, int bincnt); |
146 |
> |
|
147 |
|
void init(int np); |
148 |
< |
void tracecontribs(FILE *fp); |
148 |
> |
int done_rprocs(struct rtproc *rtp); |
149 |
> |
void recover_output(FILE *fin); |
150 |
> |
void trace_contribs(FILE *fin); |
151 |
|
struct rtproc *wait_rproc(void); |
152 |
|
struct rtproc *get_rproc(void); |
153 |
< |
void process_rays(struct rtproc *rtp); |
153 |
> |
void queue_raytree(struct rtproc *rtp); |
154 |
> |
void process_queue(void); |
155 |
|
|
156 |
< |
void add_contrib(const char *modn, float rayval[7]); |
156 |
> |
void put_contrib(const DCOLOR cnt, FILE *fout); |
157 |
> |
void add_contrib(const char *modn); |
158 |
|
void done_contrib(void); |
159 |
|
|
160 |
+ |
/* return number of open rtrace processes */ |
161 |
+ |
static int |
162 |
+ |
nrtprocs(void) |
163 |
+ |
{ |
164 |
+ |
int nrtp = 0; |
165 |
+ |
struct rtproc *rtp; |
166 |
+ |
|
167 |
+ |
for (rtp = &rt0; rtp != NULL; rtp = rtp->next) |
168 |
+ |
nrtp += rtp->pd.running; |
169 |
+ |
return(nrtp); |
170 |
+ |
} |
171 |
+ |
|
172 |
|
/* set input/output format */ |
173 |
|
static void |
174 |
|
setformat(const char *fmt) |
175 |
|
{ |
176 |
|
switch (fmt[0]) { |
129 |
– |
case 'a': |
177 |
|
case 'f': |
178 |
|
case 'd': |
179 |
+ |
SET_FILE_BINARY(stdin); |
180 |
+ |
/* fall through */ |
181 |
+ |
case 'a': |
182 |
|
inpfmt = fmt[0]; |
183 |
|
break; |
184 |
|
default: |
209 |
|
main(int argc, char *argv[]) |
210 |
|
{ |
211 |
|
int nprocs = 1; |
212 |
+ |
int recover = 0; |
213 |
|
char *curout = NULL; |
214 |
|
char *binval = NULL; |
215 |
< |
int i; |
216 |
< |
/* set global argument list */ |
217 |
< |
gargc = argc; |
215 |
> |
int bincnt = 0; |
216 |
> |
char fmt[8]; |
217 |
> |
int i, j; |
218 |
> |
/* need at least one argument */ |
219 |
> |
if (argc < 2) { |
220 |
> |
fprintf(stderr, |
221 |
> |
"Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", |
222 |
> |
argv[0]); |
223 |
> |
exit(1); |
224 |
> |
} |
225 |
> |
/* global program name */ |
226 |
|
gargv = argv; |
227 |
< |
/* set up calcomp functions */ |
227 |
> |
/* initialize calcomp routines */ |
228 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW; |
229 |
|
esupport &= ~(E_OUTCHAN); |
230 |
+ |
varset("PI", ':', PI); |
231 |
|
/* get our options */ |
232 |
< |
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
233 |
< |
switch (argv[i][1]) { |
234 |
< |
case 'n': /* number of processes */ |
235 |
< |
if (argv[i][2] || i >= argc-1) break; |
236 |
< |
nprocs = atoi(argv[++i]); |
237 |
< |
if (nprocs <= 0) |
238 |
< |
error(USER, "illegal number of processes"); |
239 |
< |
continue; |
240 |
< |
case 'h': /* output header? */ |
241 |
< |
switch (argv[i][2]) { |
242 |
< |
case '\0': |
243 |
< |
header = !header; |
232 |
> |
for (i = 1; i < argc-1; i++) { |
233 |
> |
/* expand arguments */ |
234 |
> |
while ((j = expandarg(&argc, &argv, i)) > 0) |
235 |
> |
; |
236 |
> |
if (j < 0) { |
237 |
> |
fprintf(stderr, "%s: cannot expand '%s'", |
238 |
> |
argv[0], argv[i]); |
239 |
> |
exit(1); |
240 |
> |
} |
241 |
> |
if (argv[i][0] == '-') |
242 |
> |
switch (argv[i][1]) { |
243 |
> |
case 'r': /* recover output */ |
244 |
> |
if (argv[i][2]) break; |
245 |
> |
recover++; |
246 |
|
continue; |
247 |
< |
case '+': case '1': case 'T': case 't': |
248 |
< |
header = 1; |
247 |
> |
case 'n': /* number of processes */ |
248 |
> |
if (argv[i][2] || i >= argc-2) break; |
249 |
> |
nprocs = atoi(argv[++i]); |
250 |
> |
if (nprocs <= 0) |
251 |
> |
error(USER, "illegal number of processes"); |
252 |
|
continue; |
253 |
< |
case '-': case '0': case 'F': case 'f': |
254 |
< |
header = 0; |
253 |
> |
case 'h': /* output header? */ |
254 |
> |
switch (argv[i][2]) { |
255 |
> |
case '\0': |
256 |
> |
header = !header; |
257 |
> |
continue; |
258 |
> |
case '+': case '1': |
259 |
> |
case 'T': case 't': |
260 |
> |
case 'Y': case 'y': |
261 |
> |
header = 1; |
262 |
> |
continue; |
263 |
> |
case '-': case '0': |
264 |
> |
case 'F': case 'f': |
265 |
> |
case 'N': case 'n': |
266 |
> |
header = 0; |
267 |
> |
continue; |
268 |
> |
} |
269 |
> |
break; |
270 |
> |
case 'f': /* file or force or format */ |
271 |
> |
if (!argv[i][2]) { |
272 |
> |
char *fpath; |
273 |
> |
if (i >= argc-2) break; |
274 |
> |
fpath = getpath(argv[++i], |
275 |
> |
getrlibpath(), R_OK); |
276 |
> |
if (fpath == NULL) { |
277 |
> |
sprintf(errmsg, |
278 |
> |
"cannot find file '%s'", |
279 |
> |
argv[i]); |
280 |
> |
error(USER, errmsg); |
281 |
> |
} |
282 |
> |
fcompile(fpath); |
283 |
> |
continue; |
284 |
> |
} |
285 |
> |
if (argv[i][2] == 'o') { |
286 |
> |
force_open++; |
287 |
> |
continue; |
288 |
> |
} |
289 |
> |
setformat(argv[i]+2); |
290 |
|
continue; |
291 |
< |
} |
292 |
< |
break; |
293 |
< |
case 'f': /* file or i/o format */ |
194 |
< |
if (!argv[i][2]) { |
195 |
< |
if (i >= argc-1) break; |
196 |
< |
fcompile(argv[++i]); |
291 |
> |
case 'e': /* expression */ |
292 |
> |
if (argv[i][2] || i >= argc-2) break; |
293 |
> |
scompile(argv[++i], NULL, 0); |
294 |
|
continue; |
295 |
+ |
case 'o': /* output file spec. */ |
296 |
+ |
if (argv[i][2] || i >= argc-2) break; |
297 |
+ |
curout = argv[++i]; |
298 |
+ |
continue; |
299 |
+ |
case 'x': /* horiz. output resolution */ |
300 |
+ |
if (argv[i][2] || i >= argc-2) break; |
301 |
+ |
xres = atoi(argv[++i]); |
302 |
+ |
continue; |
303 |
+ |
case 'y': /* vert. output resolution */ |
304 |
+ |
if (argv[i][2] || i >= argc-2) break; |
305 |
+ |
yres = atoi(argv[++i]); |
306 |
+ |
continue; |
307 |
+ |
case 'b': /* bin expression/count */ |
308 |
+ |
if (i >= argc-2) break; |
309 |
+ |
if (argv[i][2] == 'n') { |
310 |
+ |
bincnt = atoi(argv[++i]); |
311 |
+ |
continue; |
312 |
+ |
} |
313 |
+ |
if (argv[i][2]) break; |
314 |
+ |
binval = argv[++i]; |
315 |
+ |
continue; |
316 |
+ |
case 'm': /* modifier name */ |
317 |
+ |
if (argv[i][2] || i >= argc-2) break; |
318 |
+ |
rtargv[rtargc++] = "-ti"; |
319 |
+ |
rtargv[rtargc++] = argv[++i]; |
320 |
+ |
addmodifier(argv[i], curout, binval, bincnt); |
321 |
+ |
continue; |
322 |
+ |
case 'M': /* modifier file */ |
323 |
+ |
if (argv[i][2] || i >= argc-2) break; |
324 |
+ |
rtargv[rtargc++] = "-tI"; |
325 |
+ |
rtargv[rtargc++] = argv[++i]; |
326 |
+ |
addmodfile(argv[i], curout, binval, bincnt); |
327 |
+ |
continue; |
328 |
|
} |
329 |
< |
setformat(argv[i]+2); |
200 |
< |
continue; |
201 |
< |
case 'e': /* expression */ |
202 |
< |
if (argv[i][2] || i >= argc-1) break; |
203 |
< |
scompile(argv[++i], NULL, 0); |
204 |
< |
continue; |
205 |
< |
case 'o': /* output file spec. */ |
206 |
< |
if (argv[i][2] || i >= argc-1) break; |
207 |
< |
curout = argv[++i]; |
208 |
< |
continue; |
209 |
< |
case 'x': /* horiz. output resolution */ |
210 |
< |
if (argv[i][2] || i >= argc-1) break; |
211 |
< |
xres = atoi(argv[++i]); |
212 |
< |
continue; |
213 |
< |
case 'y': /* vert. output resolution */ |
214 |
< |
if (argv[i][2] || i >= argc-1) break; |
215 |
< |
yres = atoi(argv[++i]); |
216 |
< |
continue; |
217 |
< |
case 'l': /* limit distance? */ |
218 |
< |
if (argv[i][2] != 'd') break; |
219 |
< |
rtargv[rtargc++] = argv[i]; |
220 |
< |
continue; |
221 |
< |
case 'b': /* bin expression */ |
222 |
< |
if (argv[i][2] || i >= argc-1) break; |
223 |
< |
binval = argv[++i]; |
224 |
< |
continue; |
225 |
< |
case 'm': /* modifier name */ |
226 |
< |
if (argv[i][2] || i >= argc-1) break; |
227 |
< |
rtargv[rtargc++] = "-ti"; |
228 |
< |
rtargv[rtargc++] = argv[++i]; |
229 |
< |
addmodifier(argv[i], curout, binval); |
230 |
< |
continue; |
231 |
< |
} |
232 |
< |
break; /* break into rtrace options */ |
329 |
> |
rtargv[rtargc++] = argv[i]; /* assume rtrace option */ |
330 |
|
} |
331 |
< |
for ( ; i < argc; i++) /* transfer rtrace-specific options */ |
332 |
< |
rtargv[rtargc++] = argv[i]; |
236 |
< |
if (!strcmp(rtargv[--rtargc], "-defaults")) |
237 |
< |
nprocs = 0; |
238 |
< |
if (nprocs > 1) { /* add persist file if parallel invocation */ |
239 |
< |
rtargv[rtargc++] = "-PP"; |
240 |
< |
rtargv[rtargc++] = mktemp(persistfn); |
241 |
< |
} |
331 |
> |
/* set global argument list */ |
332 |
> |
gargc = argc; gargv = argv; |
333 |
|
/* add "mandatory" rtrace settings */ |
334 |
< |
for (i = 0; myrtopts[i] != NULL; i++) |
335 |
< |
rtargv[rtargc++] = myrtopts[i]; |
336 |
< |
if (!nprocs) { /* just asking for defaults? */ |
334 |
> |
for (j = 0; myrtopts[j] != NULL; j++) |
335 |
> |
rtargv[rtargc++] = myrtopts[j]; |
336 |
> |
/* just asking for defaults? */ |
337 |
> |
if (!strcmp(argv[i], "-defaults")) { |
338 |
|
char sxres[16], syres[16]; |
339 |
|
char *rtpath; |
340 |
< |
printf("-n 1\t\t\t\t# number of processes\n", nprocs); |
340 |
> |
printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); |
341 |
|
fflush(stdout); /* report OUR options */ |
342 |
|
rtargv[rtargc++] = header ? "-h+" : "-h-"; |
343 |
|
sprintf(fmt, "-f%c%c", inpfmt, outfmt); |
348 |
|
rtargv[rtargc++] = "-y"; |
349 |
|
sprintf(syres, "%d", yres); |
350 |
|
rtargv[rtargc++] = syres; |
351 |
< |
rtargv[rtargc++] = "-oW"; |
351 |
> |
rtargv[rtargc++] = "-oTW"; |
352 |
|
rtargv[rtargc++] = "-defaults"; |
353 |
|
rtargv[rtargc] = NULL; |
354 |
|
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); |
361 |
|
perror(rtpath); /* execv() should not return */ |
362 |
|
exit(1); |
363 |
|
} |
364 |
< |
if (!nmods) |
365 |
< |
error(USER, "No modifiers specified"); |
366 |
< |
if (argc < 2 || argv[argc-1][0] == '-') |
367 |
< |
error(USER, "missing octree argument"); |
364 |
> |
if (nprocs > 1) { /* add persist file if parallel */ |
365 |
> |
rtargv[rtargc++] = "-PP"; |
366 |
> |
rtargv[rtargc++] = mktemp(persistfn); |
367 |
> |
} |
368 |
|
/* add format string */ |
369 |
|
sprintf(fmt, "-f%cf", inpfmt); |
370 |
|
rtargv[rtargc++] = fmt; |
371 |
|
/* octree argument is last */ |
372 |
< |
rtargv[rtargc++] = octree = argv[argc-1]; |
372 |
> |
if (i <= 0 || i != argc-1 || argv[i][0] == '-') |
373 |
> |
error(USER, "missing octree argument"); |
374 |
> |
rtargv[rtargc++] = octree = argv[i]; |
375 |
|
rtargv[rtargc] = NULL; |
376 |
< |
/* start rtrace & sum contributions */ |
376 |
> |
/* start rtrace */ |
377 |
|
init(nprocs); |
378 |
< |
tracecontribs(stdin); |
378 |
> |
if (recover) /* perform recovery if requested */ |
379 |
> |
recover_output(stdin); |
380 |
> |
trace_contribs(stdin); /* compute contributions */ |
381 |
|
quit(0); |
382 |
|
} |
383 |
|
|
384 |
+ |
#ifndef SIGALRM |
385 |
+ |
#define SIGALRM SIGTERM |
386 |
+ |
#endif |
387 |
|
/* kill persistent rtrace process */ |
388 |
|
static void |
389 |
|
killpersist(void) |
390 |
|
{ |
391 |
|
FILE *fp = fopen(persistfn, "r"); |
392 |
< |
int pid; |
392 |
> |
RT_PID pid; |
393 |
|
|
394 |
|
if (fp == NULL) |
395 |
|
return; |
432 |
|
exit(status); /* flushes all output streams */ |
433 |
|
} |
434 |
|
|
435 |
< |
/* start rtrace and initialize buffers */ |
435 |
> |
/* start rtrace processes and initialize */ |
436 |
|
void |
437 |
|
init(int np) |
438 |
|
{ |
439 |
|
struct rtproc *rtp; |
440 |
|
int i; |
441 |
|
int maxbytes; |
442 |
+ |
/* make sure we have something to do */ |
443 |
+ |
if (!nmods) |
444 |
+ |
error(USER, "No modifiers specified"); |
445 |
|
/* assign ray variables */ |
446 |
|
scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0); |
447 |
|
scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0); |
448 |
|
/* set up signal handling */ |
449 |
< |
#ifdef SIGPIPE /* not present on Windows */ |
449 |
> |
signal(SIGINT, quit); |
450 |
> |
#ifdef SIGHUP |
451 |
> |
signal(SIGHUP, quit); |
452 |
> |
#endif |
453 |
> |
#ifdef SIGTERM |
454 |
> |
signal(SIGTERM, quit); |
455 |
> |
#endif |
456 |
> |
#ifdef SIGPIPE |
457 |
|
signal(SIGPIPE, quit); |
458 |
|
#endif |
459 |
|
rtp = &rt0; /* start rtrace process(es) */ |
469 |
|
error(SYSTEM, "cannot start rtrace process"); |
470 |
|
if (maxbytes > treebufsiz) |
471 |
|
treebufsiz = maxbytes; |
472 |
+ |
rtp->raynum = 0; |
473 |
|
rtp->bsiz = 0; |
474 |
|
rtp->buf = NULL; |
475 |
< |
rtp->raynum = 0; |
475 |
> |
rtp->nbr = 0; |
476 |
|
if (i == np) /* last process? */ |
477 |
|
break; |
478 |
|
if (i == 1) |
485 |
|
rtp->next = NULL; /* terminate list */ |
486 |
|
if (yres > 0) { |
487 |
|
if (xres > 0) |
488 |
< |
raysleft = xres*yres; |
488 |
> |
raysleft = (unsigned long)xres*yres; |
489 |
|
else |
490 |
|
raysleft = yres; |
491 |
|
} else |
493 |
|
waitflush = xres; |
494 |
|
} |
495 |
|
|
496 |
+ |
/* grow modifier to accommodate more bins */ |
497 |
+ |
MODCONT * |
498 |
+ |
growmodifier(MODCONT *mp, int nb) |
499 |
+ |
{ |
500 |
+ |
if (nb <= mp->nbins) |
501 |
+ |
return mp; |
502 |
+ |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1)); |
503 |
+ |
if (mp == NULL) |
504 |
+ |
error(SYSTEM, "out of memory in growmodifier"); |
505 |
+ |
memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins)); |
506 |
+ |
mp->nbins = nb; |
507 |
+ |
return mp; |
508 |
+ |
} |
509 |
+ |
|
510 |
|
/* add modifier to our list to track */ |
511 |
|
MODCONT * |
512 |
< |
addmodifier(char *modn, char *outf, char *binv) |
512 |
> |
addmodifier(char *modn, char *outf, char *binv, int bincnt) |
513 |
|
{ |
514 |
|
LUENT *lep = lu_find(&modconttab, modn); |
515 |
|
MODCONT *mp; |
516 |
+ |
int i; |
517 |
|
|
518 |
|
if (lep->data != NULL) { |
519 |
|
sprintf(errmsg, "duplicate modifier '%s'", modn); |
526 |
|
mp = (MODCONT *)malloc(sizeof(MODCONT)); |
527 |
|
if (mp == NULL) |
528 |
|
error(SYSTEM, "out of memory in addmodifier"); |
404 |
– |
lep->data = (char *)mp; |
529 |
|
mp->outspec = outf; /* XXX assumes static string */ |
530 |
|
mp->modname = modn; /* XXX assumes static string */ |
531 |
|
if (binv != NULL) |
534 |
|
mp->binv = eparse("0"); |
535 |
|
mp->nbins = 1; |
536 |
|
setcolor(mp->cbin[0], 0., 0., 0.); |
537 |
+ |
if (mp->binv->type == NUM) /* assume one bin if constant */ |
538 |
+ |
bincnt = 1; |
539 |
+ |
else if (bincnt > 1) |
540 |
+ |
mp = growmodifier(mp, bincnt); |
541 |
+ |
lep->data = (char *)mp; |
542 |
+ |
/* allocate output streams */ |
543 |
+ |
for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; ) |
544 |
+ |
getostream(mp->outspec, mp->modname, i, 1); |
545 |
|
return mp; |
546 |
|
} |
547 |
|
|
548 |
+ |
/* add modifiers from a file list */ |
549 |
+ |
void |
550 |
+ |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
551 |
+ |
{ |
552 |
+ |
char *mname[MAXMODLIST]; |
553 |
+ |
int i; |
554 |
+ |
/* find the file & store strings */ |
555 |
+ |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
556 |
+ |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
557 |
+ |
error(SYSTEM, errmsg); |
558 |
+ |
} |
559 |
+ |
for (i = 0; mname[i]; i++) /* add each one */ |
560 |
+ |
addmodifier(mname[i], outf, binv, bincnt); |
561 |
+ |
} |
562 |
+ |
|
563 |
|
/* put string to stderr */ |
564 |
|
void |
565 |
|
eputs(char *s) |
575 |
|
midline = s[strlen(s)-1] != '\n'; |
576 |
|
} |
577 |
|
|
578 |
+ |
/* construct output file name and return flags whether modifier/bin present */ |
579 |
+ |
int |
580 |
+ |
ofname(char *oname, const char *ospec, const char *mname, int bn) |
581 |
+ |
{ |
582 |
+ |
const char *mnp = NULL; |
583 |
+ |
const char *bnp = NULL; |
584 |
+ |
const char *cp; |
585 |
+ |
|
586 |
+ |
if (ospec == NULL) |
587 |
+ |
return -1; |
588 |
+ |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
589 |
+ |
if (*cp == '%') { |
590 |
+ |
do |
591 |
+ |
++cp; |
592 |
+ |
while (isdigit(*cp)); |
593 |
+ |
switch (*cp) { |
594 |
+ |
case '%': |
595 |
+ |
break; |
596 |
+ |
case 's': |
597 |
+ |
if (mnp != NULL) |
598 |
+ |
return -1; |
599 |
+ |
mnp = cp; |
600 |
+ |
break; |
601 |
+ |
case 'd': |
602 |
+ |
if (bnp != NULL) |
603 |
+ |
return -1; |
604 |
+ |
bnp = cp; |
605 |
+ |
break; |
606 |
+ |
default: |
607 |
+ |
return -1; |
608 |
+ |
} |
609 |
+ |
} |
610 |
+ |
if (mnp != NULL) { /* create file name */ |
611 |
+ |
if (bnp != NULL) { |
612 |
+ |
if (bnp > mnp) |
613 |
+ |
sprintf(oname, ospec, mname, bn); |
614 |
+ |
else |
615 |
+ |
sprintf(oname, ospec, bn, mname); |
616 |
+ |
return OF_MODIFIER|OF_BIN; |
617 |
+ |
} |
618 |
+ |
sprintf(oname, ospec, mname); |
619 |
+ |
return OF_MODIFIER; |
620 |
+ |
} |
621 |
+ |
if (bnp != NULL) { |
622 |
+ |
sprintf(oname, ospec, bn); |
623 |
+ |
return OF_BIN; |
624 |
+ |
} |
625 |
+ |
strcpy(oname, ospec); |
626 |
+ |
return 0; |
627 |
+ |
} |
628 |
+ |
|
629 |
|
/* write header to the given output stream */ |
630 |
|
void |
631 |
< |
printheader(FILE *fout) |
631 |
> |
printheader(FILE *fout, const char *info) |
632 |
|
{ |
633 |
|
extern char VersionID[]; |
634 |
|
FILE *fin = fopen(octree, "r"); |
635 |
|
|
636 |
|
if (fin == NULL) |
637 |
|
quit(1); |
638 |
< |
getheader(fin, (gethfunc *)fputs, fout); /* copy octree header */ |
638 |
> |
checkheader(fin, "ignore", fout); /* copy octree header */ |
639 |
|
fclose(fin); |
640 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
641 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
642 |
|
fputnow(fout); |
643 |
+ |
if (info != NULL) /* add extra info if given */ |
644 |
+ |
fputs(info, fout); |
645 |
|
switch (outfmt) { /* add output format */ |
646 |
|
case 'a': |
647 |
|
fputformat("ascii", fout); |
657 |
|
break; |
658 |
|
} |
659 |
|
fputc('\n', fout); |
660 |
+ |
} |
661 |
+ |
|
662 |
+ |
/* write resolution string to given output stream */ |
663 |
+ |
void |
664 |
+ |
printresolu(FILE *fout) |
665 |
+ |
{ |
666 |
|
if (xres > 0) { |
667 |
|
if (yres > 0) /* resolution string */ |
668 |
|
fprtresolu(xres, yres, fout); |
670 |
|
} |
671 |
|
} |
672 |
|
|
673 |
< |
/* Get output file pointer (open and write header if new) */ |
674 |
< |
FILE * |
675 |
< |
getofile(const char *ospec, const char *mname, int bn) |
673 |
> |
/* Get output stream pointer (open and write header if new and noopen==0) */ |
674 |
> |
STREAMOUT * |
675 |
> |
getostream(const char *ospec, const char *mname, int bn, int noopen) |
676 |
|
{ |
677 |
< |
static int using_stdout = 0; |
678 |
< |
const char *mnp = NULL; |
679 |
< |
const char *bnp = NULL; |
680 |
< |
const char *cp; |
681 |
< |
char ofname[1024]; |
476 |
< |
LUENT *lep; |
677 |
> |
static STREAMOUT stdos; |
678 |
> |
int ofl; |
679 |
> |
char oname[1024]; |
680 |
> |
LUENT *lep; |
681 |
> |
STREAMOUT *sop; |
682 |
|
|
683 |
|
if (ospec == NULL) { /* use stdout? */ |
684 |
< |
if (!using_stdout && header) |
685 |
< |
printheader(stdout); |
686 |
< |
using_stdout = 1; |
687 |
< |
return stdout; |
688 |
< |
} |
689 |
< |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
690 |
< |
if (*cp == '%') { |
691 |
< |
do |
487 |
< |
++cp; |
488 |
< |
while (isdigit(*cp)); |
489 |
< |
switch (*cp) { |
490 |
< |
case '%': |
491 |
< |
break; |
492 |
< |
case 's': |
493 |
< |
if (mnp != NULL) |
494 |
< |
goto badspec; |
495 |
< |
mnp = cp; |
496 |
< |
break; |
497 |
< |
case 'd': |
498 |
< |
if (bnp != NULL) |
499 |
< |
goto badspec; |
500 |
< |
bnp = cp; |
501 |
< |
break; |
502 |
< |
default: |
503 |
< |
goto badspec; |
504 |
< |
} |
684 |
> |
if (!noopen && !using_stdout) { |
685 |
> |
stdos.reclen = 0; |
686 |
> |
if (outfmt != 'a') |
687 |
> |
SET_FILE_BINARY(stdout); |
688 |
> |
if (header) |
689 |
> |
printheader(stdout, NULL); |
690 |
> |
printresolu(stdout); |
691 |
> |
using_stdout = 1; |
692 |
|
} |
693 |
< |
if (mnp != NULL) { /* create file name */ |
694 |
< |
if (bnp != NULL) { |
695 |
< |
if (bnp > mnp) |
696 |
< |
sprintf(ofname, ospec, mname, bn); |
697 |
< |
else |
698 |
< |
sprintf(ofname, ospec, bn, mname); |
699 |
< |
} else |
700 |
< |
sprintf(ofname, ospec, mname); |
701 |
< |
} else if (bnp != NULL) |
702 |
< |
sprintf(ofname, ospec, bn); |
516 |
< |
else |
517 |
< |
strcpy(ofname, ospec); |
518 |
< |
lep = lu_find(&ofiletab, ofname); /* look it up */ |
693 |
> |
stdos.ofp = stdout; |
694 |
> |
stdos.reclen += noopen; |
695 |
> |
return &stdos; |
696 |
> |
} |
697 |
> |
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
698 |
> |
if (ofl < 0) { |
699 |
> |
sprintf(errmsg, "bad output format '%s'", ospec); |
700 |
> |
error(USER, errmsg); |
701 |
> |
} |
702 |
> |
lep = lu_find(&ofiletab, oname); /* look it up */ |
703 |
|
if (lep->key == NULL) /* new entry */ |
704 |
< |
lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname); |
705 |
< |
if (lep->data == NULL) { /* open output file */ |
706 |
< |
lep->data = (char *)fopen(ofname, "w"); |
707 |
< |
if (lep->data == NULL) { |
708 |
< |
sprintf(errmsg, "cannot open '%s' for writing", ofname); |
704 |
> |
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
705 |
> |
sop = (STREAMOUT *)lep->data; |
706 |
> |
if (sop == NULL) { /* allocate stream */ |
707 |
> |
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); |
708 |
> |
if (sop == NULL) |
709 |
> |
error(SYSTEM, "out of memory in getostream"); |
710 |
> |
sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN; |
711 |
> |
sop->ofp = NULL; /* open iff noopen==0 */ |
712 |
> |
lep->data = (char *)sop; |
713 |
> |
} |
714 |
> |
if (!noopen && sop->ofp == NULL) { /* open output stream */ |
715 |
> |
long i; |
716 |
> |
if (oname[0] == '!') /* output to command */ |
717 |
> |
sop->ofp = popen(oname+1, "w"); |
718 |
> |
else if (!force_open && access(oname, F_OK) == 0) |
719 |
> |
errno = EEXIST; /* file exists */ |
720 |
> |
else /* else open it */ |
721 |
> |
sop->ofp = fopen(oname, "w"); |
722 |
> |
if (sop->ofp == NULL) { |
723 |
> |
sprintf(errmsg, "cannot open '%s' for writing", oname); |
724 |
|
error(SYSTEM, errmsg); |
725 |
|
} |
726 |
< |
if (header) |
727 |
< |
printheader((FILE *)lep->data); |
726 |
> |
if (outfmt != 'a') |
727 |
> |
SET_FILE_BINARY(sop->ofp); |
728 |
> |
if (header) { |
729 |
> |
char info[512]; |
730 |
> |
char *cp = info; |
731 |
> |
if (ofl & OF_MODIFIER || sop->reclen == 1) { |
732 |
> |
sprintf(cp, "MODIFIER=%s\n", mname); |
733 |
> |
while (*cp) ++cp; |
734 |
> |
} |
735 |
> |
if (ofl & OF_BIN) { |
736 |
> |
sprintf(cp, "BIN=%d\n", bn); |
737 |
> |
while (*cp) ++cp; |
738 |
> |
} |
739 |
> |
*cp = '\0'; |
740 |
> |
printheader(sop->ofp, info); |
741 |
> |
} |
742 |
> |
printresolu(sop->ofp); |
743 |
> |
/* play catch-up */ |
744 |
> |
for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone; |
745 |
> |
i--; ) { |
746 |
> |
static const DCOLOR nocontrib = BLKCOLOR; |
747 |
> |
put_contrib(nocontrib, sop->ofp); |
748 |
> |
if (outfmt == 'a') |
749 |
> |
putc('\n', sop->ofp); |
750 |
> |
} |
751 |
> |
if (xres > 0) |
752 |
> |
fflush(sop->ofp); |
753 |
|
} |
754 |
< |
return (FILE *)lep->data; /* return open file pointer */ |
755 |
< |
badspec: |
756 |
< |
sprintf(errmsg, "bad output format '%s'", ospec); |
533 |
< |
error(USER, errmsg); |
534 |
< |
return NULL; /* pro forma return */ |
754 |
> |
if (sop->reclen != CNT_PIPE) /* add to length if noopen */ |
755 |
> |
sop->reclen += noopen; |
756 |
> |
return sop; /* return open stream */ |
757 |
|
} |
758 |
|
|
759 |
|
/* read input ray into buffer */ |
760 |
|
int |
761 |
|
getinp(char *buf, FILE *fp) |
762 |
|
{ |
763 |
+ |
char *cp; |
764 |
+ |
int i; |
765 |
+ |
|
766 |
|
switch (inpfmt) { |
767 |
|
case 'a': |
768 |
< |
if (fgets(buf, 128, fp) == NULL) |
769 |
< |
return 0; |
768 |
> |
cp = buf; /* make sure we get 6 floats */ |
769 |
> |
for (i = 0; i < 6; i++) { |
770 |
> |
if (fgetword(cp, buf+127-cp, fp) == NULL) |
771 |
> |
return 0; |
772 |
> |
if ((cp = fskip(cp)) == NULL || *cp) |
773 |
> |
return 0; |
774 |
> |
*cp++ = ' '; |
775 |
> |
} |
776 |
> |
getc(fp); /* get/put eol */ |
777 |
> |
*cp-- = '\0'; *cp = '\n'; |
778 |
|
return strlen(buf); |
779 |
|
case 'f': |
780 |
|
if (fread(buf, sizeof(float), 6, fp) < 6) |
789 |
|
return 0; /* pro forma return */ |
790 |
|
} |
791 |
|
|
792 |
< |
static const float *rparams = NULL; /* ray parameter pointer */ |
792 |
> |
static float rparams[9]; /* traced ray parameters */ |
793 |
|
|
794 |
|
/* return channel (ray) value */ |
795 |
|
double |
797 |
|
{ |
798 |
|
if (--n < 0 || n >= 6) |
799 |
|
error(USER, "illegal channel number ($N)"); |
800 |
< |
if (rparams == NULL) |
568 |
< |
error(USER, "illegal use of $N in constant expression"); |
569 |
< |
return rparams[n]; |
800 |
> |
return rparams[n+3]; |
801 |
|
} |
802 |
|
|
803 |
< |
/* add ray contribution to the appropriate modifier bin */ |
803 |
> |
/* add current ray contribution to the appropriate modifier bin */ |
804 |
|
void |
805 |
< |
add_contrib(const char *modn, float rayval[9]) |
805 |
> |
add_contrib(const char *modn) |
806 |
|
{ |
807 |
|
LUENT *le = lu_find(&modconttab, modn); |
808 |
|
MODCONT *mp = (MODCONT *)le->data; |
812 |
|
sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn); |
813 |
|
error(USER, errmsg); |
814 |
|
} |
815 |
< |
rparams = rayval + 3; /* for chanvalue */ |
585 |
< |
eclock++; |
815 |
> |
eclock++; /* get bin number */ |
816 |
|
bn = (int)(evalue(mp->binv) + .5); |
817 |
|
if (bn <= 0) |
818 |
|
bn = 0; |
819 |
< |
else if (bn > mp->nbins) { /* new bin */ |
820 |
< |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + |
821 |
< |
bn*sizeof(COLOR)); |
592 |
< |
if (mp == NULL) |
593 |
< |
error(SYSTEM, "out of memory in add_contrib"); |
594 |
< |
memset(mp->cbin+mp->nbins, 0, sizeof(COLOR)*(bn+1-mp->nbins)); |
595 |
< |
mp->nbins = bn+1; |
596 |
< |
le->data = (char *)mp; |
597 |
< |
} |
598 |
< |
addcolor(mp->cbin[bn], rayval); |
599 |
< |
rparams = NULL; |
819 |
> |
else if (bn >= mp->nbins) /* new bin */ |
820 |
> |
le->data = (char *)(mp = growmodifier(mp, bn+1)); |
821 |
> |
addcolor(mp->cbin[bn], rparams); |
822 |
|
} |
823 |
|
|
824 |
|
/* output newline to ASCII file and/or flush as requested */ |
825 |
|
static int |
826 |
|
puteol(const LUENT *e, void *p) |
827 |
|
{ |
828 |
< |
FILE *fp = (FILE *)e->data; |
828 |
> |
STREAMOUT *sop = (STREAMOUT *)e->data; |
829 |
|
|
830 |
|
if (outfmt == 'a') |
831 |
< |
putc('\n', fp); |
831 |
> |
putc('\n', sop->ofp); |
832 |
|
if (!waitflush) |
833 |
< |
fflush(fp); |
834 |
< |
if (ferror(fp)) { |
833 |
> |
fflush(sop->ofp); |
834 |
> |
if (ferror(sop->ofp)) { |
835 |
|
sprintf(errmsg, "write error on file '%s'", e->key); |
836 |
|
error(SYSTEM, errmsg); |
837 |
|
} |
838 |
|
return 0; |
839 |
|
} |
840 |
|
|
841 |
+ |
/* put out ray contribution to file */ |
842 |
+ |
void |
843 |
+ |
put_contrib(const DCOLOR cnt, FILE *fout) |
844 |
+ |
{ |
845 |
+ |
float fv[3]; |
846 |
+ |
COLR cv; |
847 |
+ |
|
848 |
+ |
switch (outfmt) { |
849 |
+ |
case 'a': |
850 |
+ |
fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]); |
851 |
+ |
break; |
852 |
+ |
case 'f': |
853 |
+ |
fv[0] = cnt[0]; |
854 |
+ |
fv[1] = cnt[1]; |
855 |
+ |
fv[2] = cnt[2]; |
856 |
+ |
fwrite(fv, sizeof(float), 3, fout); |
857 |
+ |
break; |
858 |
+ |
case 'd': |
859 |
+ |
fwrite(cnt, sizeof(double), 3, fout); |
860 |
+ |
break; |
861 |
+ |
case 'c': |
862 |
+ |
setcolr(cv, cnt[0], cnt[1], cnt[2]); |
863 |
+ |
fwrite(cv, sizeof(cv), 1, fout); |
864 |
+ |
break; |
865 |
+ |
default: |
866 |
+ |
error(INTERNAL, "botched output format"); |
867 |
+ |
} |
868 |
+ |
} |
869 |
+ |
|
870 |
|
/* output ray tallies and clear for next primary */ |
871 |
|
void |
872 |
|
done_contrib(void) |
873 |
|
{ |
874 |
< |
int i, j; |
875 |
< |
MODCONT *mp; |
876 |
< |
FILE *fout; |
626 |
< |
double dv[3]; |
627 |
< |
COLR cv; |
874 |
> |
int i, j; |
875 |
> |
MODCONT *mp; |
876 |
> |
STREAMOUT *sop; |
877 |
|
/* output modifiers in order */ |
878 |
|
for (i = 0; i < nmods; i++) { |
879 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
880 |
< |
for (j = 0; j < mp->nbins; j++) { |
881 |
< |
fout = getofile(mp->outspec, mp->modname, j); |
882 |
< |
switch (outfmt) { |
883 |
< |
case 'a': |
884 |
< |
fprintf(fout, "%.6e\t%.6e\t%.6e\t", |
885 |
< |
mp->cbin[j][RED], |
886 |
< |
mp->cbin[j][GRN], |
887 |
< |
mp->cbin[j][BLU]); |
888 |
< |
break; |
889 |
< |
case 'f': |
641 |
< |
fwrite(mp->cbin[j], sizeof(float), 3, fout); |
642 |
< |
break; |
643 |
< |
case 'd': |
644 |
< |
dv[0] = mp->cbin[j][0]; |
645 |
< |
dv[1] = mp->cbin[j][1]; |
646 |
< |
dv[2] = mp->cbin[j][2]; |
647 |
< |
fwrite(dv, sizeof(double), 3, fout); |
648 |
< |
break; |
649 |
< |
case 'c': |
650 |
< |
setcolr(cv, mp->cbin[j][RED], |
651 |
< |
mp->cbin[j][GRN], mp->cbin[j][BLU]); |
652 |
< |
fwrite(cv, sizeof(COLR), 1, fout); |
653 |
< |
break; |
654 |
< |
default: |
655 |
< |
error(INTERNAL, "botched output format"); |
656 |
< |
} |
657 |
< |
} |
880 |
> |
sop = getostream(mp->outspec, mp->modname, 0,0); |
881 |
> |
put_contrib(mp->cbin[0], sop->ofp); |
882 |
> |
if (mp->nbins > 3 && /* minor optimization */ |
883 |
> |
sop == getostream(mp->outspec, mp->modname, 1,0)) |
884 |
> |
for (j = 1; j < mp->nbins; j++) |
885 |
> |
put_contrib(mp->cbin[j], sop->ofp); |
886 |
> |
else |
887 |
> |
for (j = 1; j < mp->nbins; j++) |
888 |
> |
put_contrib(mp->cbin[j], |
889 |
> |
getostream(mp->outspec,mp->modname,j,0)->ofp); |
890 |
|
/* clear for next ray tree */ |
891 |
< |
memset(mp->cbin, 0, sizeof(COLOR)*mp->nbins); |
891 |
> |
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
892 |
|
} |
893 |
|
--waitflush; /* terminate records */ |
894 |
|
lu_doall(&ofiletab, puteol, NULL); |
895 |
< |
if (!waitflush) |
895 |
> |
if (using_stdout & (outfmt == 'a')) |
896 |
> |
putc('\n', stdout); |
897 |
> |
if (!waitflush) { |
898 |
|
waitflush = xres; |
899 |
+ |
if (using_stdout) |
900 |
+ |
fflush(stdout); |
901 |
+ |
} |
902 |
|
} |
903 |
|
|
904 |
< |
/* process (or save) ray tree produced by rtrace process */ |
904 |
> |
/* queue completed ray tree produced by rtrace process */ |
905 |
|
void |
906 |
< |
process_rays(struct rtproc *rtp) |
906 |
> |
queue_raytree(struct rtproc *rtp) |
907 |
|
{ |
908 |
< |
struct rtproc *rtu; |
909 |
< |
/* check if time to process it */ |
910 |
< |
if (rtp->raynum == lastdone+1) { |
911 |
< |
int n = rtp->bpos - rtp->buf; |
908 |
> |
struct rtproc *rtu, *rtl = NULL; |
909 |
> |
/* insert following ray order */ |
910 |
> |
for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next) |
911 |
> |
if (rtp->raynum < rtu->raynum) |
912 |
> |
break; |
913 |
> |
rtu = (struct rtproc *)malloc(sizeof(struct rtproc)); |
914 |
> |
if (rtu == NULL) |
915 |
> |
error(SYSTEM, "out of memory in queue_raytree"); |
916 |
> |
*rtu = *rtp; |
917 |
> |
if (rtl == NULL) { |
918 |
> |
rtu->next = rt_unproc; |
919 |
> |
rt_unproc = rtu; |
920 |
> |
} else { |
921 |
> |
rtu->next = rtl->next; |
922 |
> |
rtl->next = rtu; |
923 |
> |
} |
924 |
> |
rtp->raynum = 0; /* clear path for next ray tree */ |
925 |
> |
rtp->bsiz = 0; |
926 |
> |
rtp->buf = NULL; |
927 |
> |
rtp->nbr = 0; |
928 |
> |
} |
929 |
> |
|
930 |
> |
/* process completed ray trees from our queue */ |
931 |
> |
void |
932 |
> |
process_queue(void) |
933 |
> |
{ |
934 |
> |
char modname[128]; |
935 |
> |
/* ray-ordered queue */ |
936 |
> |
while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) { |
937 |
> |
struct rtproc *rtp = rt_unproc; |
938 |
> |
int n = rtp->nbr; |
939 |
|
const char *cp = rtp->buf; |
940 |
|
while (n > 0) { /* process rays */ |
941 |
< |
char matname[128]; |
678 |
< |
char *mnp = matname; |
941 |
> |
register char *mnp = modname; |
942 |
|
/* skip leading tabs */ |
943 |
|
while (n > 0 && *cp == '\t') { |
944 |
|
cp++; n--; |
945 |
|
} |
946 |
< |
/* get material name */ |
946 |
> |
if (!n || !(isalpha(*cp) | (*cp == '_'))) |
947 |
> |
error(USER, "bad modifier name from rtrace"); |
948 |
> |
/* get modifier name */ |
949 |
|
while (n > 0 && *cp != '\t') { |
950 |
|
*mnp++ = *cp++; n--; |
951 |
|
} |
687 |
– |
if (!n) |
688 |
– |
error(USER, "botched modifer name from rtrace"); |
952 |
|
*mnp = '\0'; |
953 |
< |
cp++; n--; /* skip terminating tab */ |
953 |
> |
cp++; n--; /* eat following tab */ |
954 |
|
if (n < (int)(sizeof(float)*9)) |
955 |
|
error(USER, "incomplete ray value from rtrace"); |
956 |
|
/* add ray contribution */ |
957 |
< |
add_contrib(matname, (float *)cp); |
957 |
> |
memcpy(rparams, cp, sizeof(float)*9); |
958 |
|
cp += sizeof(float)*9; n -= sizeof(float)*9; |
959 |
+ |
add_contrib(modname); |
960 |
|
} |
961 |
|
done_contrib(); /* sum up contributions & output */ |
962 |
|
lastdone = rtp->raynum; |
963 |
< |
free(rtp->buf); |
964 |
< |
/* catch up with unprocessed list */ |
965 |
< |
while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) { |
702 |
< |
process_rays(rt_unproc); |
703 |
< |
rt_unproc = (rtu=rt_unproc)->next; |
704 |
< |
free(rtu); |
705 |
< |
} |
706 |
< |
} else { /* else insert in unprocessed list */ |
707 |
< |
struct rtproc *rtl = NULL; |
708 |
< |
for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next) |
709 |
< |
if (rtp->raynum < rtu->raynum) |
710 |
< |
break; |
711 |
< |
rtu = (struct rtproc *)malloc(sizeof(struct rtproc)); |
712 |
< |
if (rtu == NULL) |
713 |
< |
error(SYSTEM, "out of memory in process_rays"); |
714 |
< |
*rtu = *rtp; |
715 |
< |
if (rtl == NULL) { |
716 |
< |
rtu->next = rt_unproc; |
717 |
< |
rt_unproc = rtu; |
718 |
< |
} else { |
719 |
< |
rtu->next = rtl->next; |
720 |
< |
rtl->next = rtu; |
721 |
< |
} |
963 |
> |
free(rtp->buf); /* free up buffer space */ |
964 |
> |
rt_unproc = rtp->next; |
965 |
> |
free(rtp); /* done with this ray tree */ |
966 |
|
} |
723 |
– |
rtp->raynum = 0; /* clear path for next ray tree */ |
724 |
– |
rtp->bsiz = 0; |
725 |
– |
rtp->buf = NULL; |
967 |
|
} |
968 |
|
|
969 |
|
/* wait for rtrace process to finish with ray tree */ |
1004 |
|
if (rt->buf == NULL) { |
1005 |
|
rt->bsiz = treebufsiz; |
1006 |
|
rt->buf = (char *)malloc(treebufsiz); |
1007 |
< |
} else if (rt->bpos + BUFSIZ > rt->buf + rt->bsiz) { |
1007 |
> |
} else if (rt->nbr + BUFSIZ > rt->bsiz) { |
1008 |
|
if (rt->bsiz + BUFSIZ <= treebufsiz) |
1009 |
|
rt->bsiz = treebufsiz; |
1010 |
|
else |
1011 |
< |
rt->bsiz = treebufsiz += BUFSIZ; |
1011 |
> |
treebufsiz = rt->bsiz += BUFSIZ; |
1012 |
|
rt->buf = (char *)realloc(rt->buf, rt->bsiz); |
1013 |
|
} |
1014 |
|
if (rt->buf == NULL) |
1015 |
|
error(SYSTEM, "out of memory in wait_rproc"); |
1016 |
< |
nr = read(rt->pd.r, rt->bpos, |
1017 |
< |
rt->buf + rt->bsiz - rt->bpos); |
777 |
< |
if (!nr) |
1016 |
> |
nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr); |
1017 |
> |
if (nr <= 0) |
1018 |
|
error(USER, "rtrace process died"); |
1019 |
< |
rt->bpos += nr; /* advance buffer & check */ |
1020 |
< |
if (rt->bpos[-1] == '\t' && rt->bpos[-2] == '-') { |
1021 |
< |
rt->bpos -= 2; /* elide terminator */ |
1022 |
< |
process_rays(rt); |
1019 |
> |
rt->nbr += nr; /* advance & check */ |
1020 |
> |
if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4, |
1021 |
> |
"~\t~\t", 4)) { |
1022 |
> |
rt->nbr -= 4; /* elide terminator */ |
1023 |
> |
queue_raytree(rt); |
1024 |
|
rtfree = rt; /* ready for next ray */ |
1025 |
|
} |
1026 |
|
} |
1042 |
|
|
1043 |
|
/* trace ray contributions (main loop) */ |
1044 |
|
void |
1045 |
< |
tracecontribs(FILE *fin) |
1045 |
> |
trace_contribs(FILE *fin) |
1046 |
|
{ |
1047 |
|
char inpbuf[128]; |
1048 |
|
int iblen; |
1049 |
|
struct rtproc *rtp; |
1050 |
|
/* loop over input */ |
1051 |
|
while ((iblen = getinp(inpbuf, fin)) > 0) { |
1052 |
< |
if (lastray+1 < lastray) { /* counter rollover? */ |
1052 |
> |
if (lastray+1 < lastray || /* need reset? */ |
1053 |
> |
queue_length() > 10*nrtprocs()) { |
1054 |
|
while (wait_rproc() != NULL) |
1055 |
< |
; |
1056 |
< |
lastdone = lastray = 0; |
1055 |
> |
process_queue(); |
1056 |
> |
if (lastray+1 < lastray) |
1057 |
> |
lastdone = lastray = 0; |
1058 |
|
} |
1059 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
1060 |
< |
rtp->raynum = ++lastray; /* assign this ray to it */ |
1060 |
> |
rtp->raynum = ++lastray; /* assign ray to it */ |
1061 |
|
writebuf(rtp->pd.w, inpbuf, iblen); |
1062 |
< |
if (!--raysleft) |
1063 |
< |
break; /* explicit EOF */ |
1062 |
> |
if (raysleft && !--raysleft) |
1063 |
> |
break; |
1064 |
> |
process_queue(); /* catch up with results */ |
1065 |
|
} |
1066 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
1067 |
< |
; |
1067 |
> |
process_queue(); |
1068 |
> |
if (raysleft) |
1069 |
> |
error(USER, "unexpected EOF on input"); |
1070 |
> |
lu_done(&ofiletab); /* close output files */ |
1071 |
> |
} |
1072 |
> |
|
1073 |
> |
/* seek on the given output file */ |
1074 |
> |
static int |
1075 |
> |
myseeko(const LUENT *e, void *p) |
1076 |
> |
{ |
1077 |
> |
STREAMOUT *sop = (STREAMOUT *)e->data; |
1078 |
> |
off_t nbytes = *(off_t *)p; |
1079 |
> |
|
1080 |
> |
if (sop->reclen > 1) |
1081 |
> |
nbytes = nbytes * sop->reclen; |
1082 |
> |
if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) { |
1083 |
> |
sprintf(errmsg, "seek error on file '%s'", e->key); |
1084 |
> |
error(SYSTEM, errmsg); |
1085 |
> |
} |
1086 |
> |
} |
1087 |
> |
|
1088 |
> |
/* recover output if possible */ |
1089 |
> |
void |
1090 |
> |
recover_output(FILE *fin) |
1091 |
> |
{ |
1092 |
> |
off_t lastout = -1; |
1093 |
> |
int outvsiz, recsiz; |
1094 |
> |
char *outvfmt; |
1095 |
> |
int i, j; |
1096 |
> |
MODCONT *mp; |
1097 |
> |
int ofl; |
1098 |
> |
char oname[1024]; |
1099 |
> |
LUENT *ment, *oent; |
1100 |
> |
STREAMOUT sout; |
1101 |
> |
off_t nvals; |
1102 |
> |
int xr, yr; |
1103 |
> |
|
1104 |
> |
switch (outfmt) { |
1105 |
> |
case 'a': |
1106 |
> |
error(USER, "cannot recover ASCII output"); |
1107 |
> |
return; |
1108 |
> |
case 'f': |
1109 |
> |
outvsiz = sizeof(float)*3; |
1110 |
> |
outvfmt = "float"; |
1111 |
> |
break; |
1112 |
> |
case 'd': |
1113 |
> |
outvsiz = sizeof(double)*3; |
1114 |
> |
outvfmt = "double"; |
1115 |
> |
break; |
1116 |
> |
case 'c': |
1117 |
> |
outvsiz = sizeof(COLR); |
1118 |
> |
outvfmt = COLRFMT; |
1119 |
> |
break; |
1120 |
> |
default: |
1121 |
> |
error(INTERNAL, "botched output format"); |
1122 |
> |
return; |
1123 |
> |
} |
1124 |
> |
/* check modifier outputs */ |
1125 |
> |
for (i = 0; i < nmods; i++) { |
1126 |
> |
ment = lu_find(&modconttab,modname[i]); |
1127 |
> |
mp = (MODCONT *)ment->data; |
1128 |
> |
if (mp->outspec == NULL) |
1129 |
> |
error(USER, "cannot recover from stdout"); |
1130 |
> |
if (mp->outspec[0] == '!') |
1131 |
> |
error(USER, "cannot recover from command"); |
1132 |
> |
for (j = 0; ; j++) { /* check each bin's file */ |
1133 |
> |
ofl = ofname(oname, mp->outspec, mp->modname, j); |
1134 |
> |
if (ofl < 0) |
1135 |
> |
error(USER, "bad output file specification"); |
1136 |
> |
oent = lu_find(&ofiletab, oname); |
1137 |
> |
if (oent->data != NULL) { |
1138 |
> |
sout = *(STREAMOUT *)oent->data; |
1139 |
> |
} else { |
1140 |
> |
sout.reclen = CNT_UNKNOWN; |
1141 |
> |
sout.ofp = NULL; |
1142 |
> |
} |
1143 |
> |
if (sout.ofp != NULL) { /* already open? */ |
1144 |
> |
if (ofl & OF_BIN) |
1145 |
> |
continue; |
1146 |
> |
break; |
1147 |
> |
} |
1148 |
> |
/* open output */ |
1149 |
> |
sout.ofp = fopen(oname, "rb+"); |
1150 |
> |
if (sout.ofp == NULL) { |
1151 |
> |
if (j) |
1152 |
> |
break; /* assume end of modifier */ |
1153 |
> |
sprintf(errmsg, "missing recover file '%s'", |
1154 |
> |
oname); |
1155 |
> |
error(USER, errmsg); |
1156 |
> |
} |
1157 |
> |
nvals = lseek(fileno(sout.ofp), 0, SEEK_END); |
1158 |
> |
if (nvals <= 0) { |
1159 |
> |
lastout = 0; /* empty output, quit here */ |
1160 |
> |
fclose(sout.ofp); |
1161 |
> |
break; |
1162 |
> |
} |
1163 |
> |
if (sout.reclen == CNT_UNKNOWN) { |
1164 |
> |
if (!(ofl & OF_BIN)) { |
1165 |
> |
sprintf(errmsg, |
1166 |
> |
"need -bn to recover file '%s'", |
1167 |
> |
oname); |
1168 |
> |
error(USER, errmsg); |
1169 |
> |
} |
1170 |
> |
recsiz = outvsiz; |
1171 |
> |
} else |
1172 |
> |
recsiz = outvsiz * sout.reclen; |
1173 |
> |
|
1174 |
> |
lseek(fileno(sout.ofp), 0, SEEK_SET); |
1175 |
> |
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { |
1176 |
> |
sprintf(errmsg, "format mismatch for '%s'", |
1177 |
> |
oname); |
1178 |
> |
error(USER, errmsg); |
1179 |
> |
} |
1180 |
> |
if ((xres > 0) & (yres > 0) && |
1181 |
> |
(!fscnresolu(&xr, &yr, sout.ofp) || |
1182 |
> |
xr != xres || |
1183 |
> |
yr != yres)) { |
1184 |
> |
sprintf(errmsg, "resolution mismatch for '%s'", |
1185 |
> |
oname); |
1186 |
> |
error(USER, errmsg); |
1187 |
> |
} |
1188 |
> |
nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; |
1189 |
> |
if ((lastout < 0) | (nvals < lastout)) |
1190 |
> |
lastout = nvals; |
1191 |
> |
if (oent->key == NULL) /* new entry */ |
1192 |
> |
oent->key = strcpy((char *) |
1193 |
> |
malloc(strlen(oname)+1), oname); |
1194 |
> |
if (oent->data == NULL) |
1195 |
> |
oent->data = (char *)malloc(sizeof(STREAMOUT)); |
1196 |
> |
*(STREAMOUT *)oent->data = sout; |
1197 |
> |
if (!(ofl & OF_BIN)) |
1198 |
> |
break; /* no bin separation */ |
1199 |
> |
} |
1200 |
> |
if (!lastout) { /* empty output */ |
1201 |
> |
error(WARNING, "no previous data to recover"); |
1202 |
> |
lu_done(&ofiletab); /* reclose all outputs */ |
1203 |
> |
return; |
1204 |
> |
} |
1205 |
> |
if (j > mp->nbins) /* reallocate modifier bins */ |
1206 |
> |
ment->data = (char *)(mp = growmodifier(mp, j)); |
1207 |
> |
} |
1208 |
> |
if (lastout < 0) { |
1209 |
> |
error(WARNING, "no output files to recover"); |
1210 |
> |
return; |
1211 |
> |
} |
1212 |
> |
if (raysleft && lastout >= raysleft) { |
1213 |
> |
error(WARNING, "output appears to be complete"); |
1214 |
> |
/* XXX should read & discard input? */ |
1215 |
> |
quit(0); |
1216 |
> |
} |
1217 |
> |
/* seek on all files */ |
1218 |
> |
nvals = lastout * outvsiz; |
1219 |
> |
lu_doall(&ofiletab, myseeko, &nvals); |
1220 |
> |
/* skip repeated input */ |
1221 |
> |
for (nvals = 0; nvals < lastout; nvals++) |
1222 |
> |
if (getinp(oname, fin) <= 0) |
1223 |
> |
error(USER, "unexpected EOF on input"); |
1224 |
> |
lastray = lastdone = (unsigned long)lastout; |
1225 |
> |
if (raysleft) |
1226 |
> |
raysleft -= lastray; |
1227 |
|
} |