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