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