54 |
|
|
55 |
|
static RAY thisray; /* for our convenience */ |
56 |
|
|
57 |
+ |
static FILE *inpfp = NULL; /* input stream pointer */ |
58 |
+ |
|
59 |
+ |
static FVECT *inp_queue = NULL; /* ray input queue if flushing */ |
60 |
+ |
static int inp_qpos = 0; /* next ray to return */ |
61 |
+ |
static int inp_qend = 0; /* number of rays in this work group */ |
62 |
+ |
|
63 |
|
typedef void putf_t(RREAL *v, int n); |
64 |
|
static putf_t puta, putd, putf, putrgbe; |
65 |
|
|
68 |
|
oputr, oputR, oputx, oputX, oputn, oputN, oputs, |
69 |
|
oputw, oputW, oputm, oputM, oputtilde; |
70 |
|
|
65 |
– |
static void setoutput(char *vs); |
71 |
|
extern void tranotify(OBJECT obj); |
72 |
+ |
static void setoutput(char *vs); |
73 |
+ |
static int is_fifo(FILE *fp); |
74 |
|
static void bogusray(void); |
75 |
|
static void raycast(RAY *r); |
76 |
|
static void rayirrad(RAY *r); |
77 |
|
static void rtcompute(FVECT org, FVECT dir, double dmax); |
78 |
|
static int printvals(RAY *r); |
79 |
|
static int getvec(FVECT vec, int fmt, FILE *fp); |
80 |
+ |
static int iszerovec(const FVECT vec); |
81 |
+ |
static double nextray(FVECT org, FVECT dir); |
82 |
|
static void tabin(RAY *r); |
83 |
|
static void ourtrace(RAY *r); |
84 |
|
|
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
< |
extern void |
121 |
> |
void |
122 |
|
rtrace( /* trace rays from file */ |
123 |
|
char *fname, |
124 |
|
int nproc |
133 |
|
FVECT orig, direc; |
134 |
|
/* set up input */ |
135 |
|
if (fname == NULL) |
136 |
< |
fp = stdin; |
137 |
< |
else if ((fp = fopen(fname, "r")) == NULL) { |
136 |
> |
inpfp = stdin; |
137 |
> |
else if ((inpfp = fopen(fname, "r")) == NULL) { |
138 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
139 |
|
error(SYSTEM, errmsg); |
140 |
|
} |
141 |
+ |
#ifdef getc_unlocked |
142 |
+ |
flockfile(inpfp); /* avoid lock/unlock overhead */ |
143 |
+ |
flockfile(stdout); |
144 |
+ |
#endif |
145 |
|
if (inform != 'a') |
146 |
< |
SET_FILE_BINARY(fp); |
146 |
> |
SET_FILE_BINARY(inpfp); |
147 |
|
/* set up output */ |
135 |
– |
setoutput(outvals); |
148 |
|
if (imm_irrad) |
149 |
|
castonly = 0; |
150 |
< |
else if (castonly) |
150 |
> |
else if (castonly || every_out[0] != NULL) |
151 |
|
nproc = 1; /* don't bother multiprocessing */ |
152 |
|
if ((nextflush > 0) & (nproc > nextflush)) { |
153 |
|
error(WARNING, "reducing number of processes to match flush interval"); |
158 |
|
case 'f': putreal = putf; break; |
159 |
|
case 'd': putreal = putd; break; |
160 |
|
case 'c': |
161 |
< |
if (outvals[0] && (outvals[1] || !strchr("vrx", outvals[0]))) |
161 |
> |
if (outvals[1] || !strchr("vrx", outvals[0])) |
162 |
|
error(USER, "color format only with -ov, -or, -ox"); |
163 |
|
putreal = putrgbe; break; |
164 |
|
default: |
174 |
|
else |
175 |
|
fflush(stdout); |
176 |
|
} |
177 |
< |
/* process file */ |
178 |
< |
while (getvec(orig, inform, fp) == 0 && |
167 |
< |
getvec(direc, inform, fp) == 0) { |
168 |
< |
|
169 |
< |
d = normalize(direc); |
177 |
> |
/* process input rays */ |
178 |
> |
while ((d = nextray(orig, direc)) >= 0.0) { |
179 |
|
if (d == 0.0) { /* flush request? */ |
180 |
|
if (something2flush) { |
181 |
|
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
188 |
|
bogusray(); |
189 |
|
} else { /* compute and print */ |
190 |
|
rtcompute(orig, direc, lim_dist ? d : 0.0); |
191 |
< |
/* flush if time */ |
183 |
< |
if (!--nextflush) { |
191 |
> |
if (!--nextflush) { /* flush if time */ |
192 |
|
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
193 |
|
error(USER, "child(ren) died"); |
194 |
|
fflush(stdout); |
206 |
|
error(USER, "unable to complete processing"); |
207 |
|
ray_pclose(0); |
208 |
|
} |
209 |
+ |
if (vcount) |
210 |
+ |
error(WARNING, "unexpected EOF on input"); |
211 |
|
if (fflush(stdout) < 0) |
212 |
|
error(SYSTEM, "write error"); |
213 |
< |
if (vcount) |
214 |
< |
error(USER, "unexpected EOF on input"); |
215 |
< |
if (fname != NULL) |
216 |
< |
fclose(fp); |
213 |
> |
if (fname != NULL) { |
214 |
> |
fclose(inpfp); |
215 |
> |
inpfp = NULL; |
216 |
> |
} |
217 |
> |
nextray(NULL, NULL); |
218 |
|
} |
219 |
|
|
220 |
|
|
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
< |
static void |
232 |
< |
setoutput( /* set up output tables */ |
222 |
< |
char *vs |
223 |
< |
) |
231 |
> |
int |
232 |
> |
setrtoutput(void) /* set up output tables, return #comp */ |
233 |
|
{ |
234 |
+ |
char *vs = outvals; |
235 |
|
oputf_t **table = ray_out; |
236 |
+ |
int ncomp = 0; |
237 |
|
|
238 |
+ |
if (!*vs) |
239 |
+ |
error(USER, "empty output specification"); |
240 |
+ |
|
241 |
|
castonly = 1; |
242 |
< |
while (*vs) |
243 |
< |
switch (*vs++) { |
242 |
> |
do |
243 |
> |
switch (*vs) { |
244 |
|
case 'T': /* trace sources */ |
245 |
< |
if (!*vs) break; |
245 |
> |
if (!vs[1]) break; |
246 |
|
trace_sources(); |
247 |
|
/* fall through */ |
248 |
|
case 't': /* trace */ |
249 |
< |
if (!*vs) break; |
249 |
> |
if (!vs[1]) break; |
250 |
|
*table = NULL; |
251 |
|
table = every_out; |
252 |
|
trace = ourtrace; |
254 |
|
break; |
255 |
|
case 'o': /* origin */ |
256 |
|
*table++ = oputo; |
257 |
+ |
ncomp += 3; |
258 |
|
break; |
259 |
|
case 'd': /* direction */ |
260 |
|
*table++ = oputd; |
261 |
+ |
ncomp += 3; |
262 |
|
break; |
263 |
|
case 'r': /* reflected contrib. */ |
264 |
|
*table++ = oputr; |
265 |
+ |
ncomp += 3; |
266 |
|
castonly = 0; |
267 |
|
break; |
268 |
|
case 'R': /* reflected distance */ |
269 |
|
*table++ = oputR; |
270 |
+ |
ncomp++; |
271 |
|
castonly = 0; |
272 |
|
break; |
273 |
|
case 'x': /* xmit contrib. */ |
274 |
|
*table++ = oputx; |
275 |
+ |
ncomp += 3; |
276 |
|
castonly = 0; |
277 |
|
break; |
278 |
|
case 'X': /* xmit distance */ |
279 |
|
*table++ = oputX; |
280 |
+ |
ncomp++; |
281 |
|
castonly = 0; |
282 |
|
break; |
283 |
|
case 'v': /* value */ |
284 |
|
*table++ = oputv; |
285 |
+ |
ncomp += 3; |
286 |
|
castonly = 0; |
287 |
|
break; |
288 |
|
case 'V': /* contribution */ |
289 |
|
*table++ = oputV; |
290 |
+ |
ncomp += 3; |
291 |
|
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
292 |
|
error(WARNING, |
293 |
|
"-otV accuracy depends on -aa 0 -as 0"); |
294 |
|
break; |
295 |
|
case 'l': /* effective distance */ |
296 |
|
*table++ = oputl; |
297 |
+ |
ncomp++; |
298 |
|
castonly = 0; |
299 |
|
break; |
300 |
|
case 'c': /* local coordinates */ |
301 |
|
*table++ = oputc; |
302 |
+ |
ncomp += 2; |
303 |
|
break; |
304 |
|
case 'L': /* single ray length */ |
305 |
|
*table++ = oputL; |
306 |
+ |
ncomp++; |
307 |
|
break; |
308 |
|
case 'p': /* point */ |
309 |
|
*table++ = oputp; |
310 |
+ |
ncomp += 3; |
311 |
|
break; |
312 |
|
case 'n': /* perturbed normal */ |
313 |
|
*table++ = oputn; |
314 |
+ |
ncomp += 3; |
315 |
|
castonly = 0; |
316 |
|
break; |
317 |
|
case 'N': /* unperturbed normal */ |
318 |
|
*table++ = oputN; |
319 |
+ |
ncomp += 3; |
320 |
|
break; |
321 |
|
case 's': /* surface */ |
322 |
|
*table++ = oputs; |
323 |
+ |
ncomp++; |
324 |
|
break; |
325 |
|
case 'w': /* weight */ |
326 |
|
*table++ = oputw; |
327 |
+ |
ncomp++; |
328 |
|
break; |
329 |
|
case 'W': /* coefficient */ |
330 |
|
*table++ = oputW; |
331 |
+ |
ncomp += 3; |
332 |
|
castonly = 0; |
333 |
|
if (ambounce > 0 && (ambacc > FTINY) | (ambssamp > 0)) |
334 |
|
error(WARNING, |
336 |
|
break; |
337 |
|
case 'm': /* modifier */ |
338 |
|
*table++ = oputm; |
339 |
+ |
ncomp++; |
340 |
|
break; |
341 |
|
case 'M': /* material */ |
342 |
|
*table++ = oputM; |
343 |
+ |
ncomp++; |
344 |
|
break; |
345 |
|
case '~': /* tilde */ |
346 |
|
*table++ = oputtilde; |
347 |
|
break; |
348 |
+ |
default: |
349 |
+ |
sprintf(errmsg, "unrecognized output option '%c'", *vs); |
350 |
+ |
error(USER, errmsg); |
351 |
|
} |
352 |
+ |
while (*++vs); |
353 |
+ |
|
354 |
|
*table = NULL; |
355 |
+ |
if (*every_out != NULL) |
356 |
+ |
ncomp = 0; |
357 |
|
/* compatibility */ |
358 |
|
for (table = ray_out; *table != NULL; table++) { |
359 |
|
if ((*table == oputV) | (*table == oputW)) |
363 |
|
(*table == oputx) | (*table == oputX)) |
364 |
|
error(WARNING, "-orRxX options incompatible with -I+ and -i+"); |
365 |
|
} |
366 |
+ |
return(ncomp); |
367 |
|
} |
368 |
|
|
369 |
|
|
462 |
|
|
463 |
|
|
464 |
|
static int |
465 |
< |
getvec( /* get a vector from fp */ |
465 |
> |
is_fifo( /* check if file pointer connected to pipe */ |
466 |
> |
FILE *fp |
467 |
> |
) |
468 |
> |
{ |
469 |
> |
#ifdef S_ISFIFO |
470 |
> |
struct stat sbuf; |
471 |
> |
|
472 |
> |
if (fstat(fileno(fp), &sbuf) < 0) |
473 |
> |
error(SYSTEM, "fstat() failed on input stream"); |
474 |
> |
return(S_ISFIFO(sbuf.st_mode)); |
475 |
> |
#else |
476 |
> |
return (fp == stdin); /* just a guess, really */ |
477 |
> |
#endif |
478 |
> |
} |
479 |
> |
|
480 |
> |
|
481 |
> |
static int |
482 |
> |
getvec( /* get a vector from fp */ |
483 |
|
FVECT vec, |
484 |
|
int fmt, |
485 |
|
FILE *fp |
516 |
|
} |
517 |
|
|
518 |
|
|
519 |
+ |
static int |
520 |
+ |
iszerovec(const FVECT vec) |
521 |
+ |
{ |
522 |
+ |
return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0); |
523 |
+ |
} |
524 |
+ |
|
525 |
+ |
|
526 |
+ |
static double |
527 |
+ |
nextray( /* return next ray in work group (-1.0 if EOF) */ |
528 |
+ |
FVECT org, |
529 |
+ |
FVECT dir |
530 |
+ |
) |
531 |
+ |
{ |
532 |
+ |
const size_t qlength = !vresolu * hresolu; |
533 |
+ |
|
534 |
+ |
if ((org == NULL) | (dir == NULL)) { |
535 |
+ |
if (inp_queue != NULL) /* asking to free queue */ |
536 |
+ |
free(inp_queue); |
537 |
+ |
inp_queue = NULL; |
538 |
+ |
inp_qpos = inp_qend = 0; |
539 |
+ |
return(-1.); |
540 |
+ |
} |
541 |
+ |
if (!inp_qend) { /* initialize FIFO queue */ |
542 |
+ |
int rsiz = 6*20; /* conservative ascii ray size */ |
543 |
+ |
if (inform == 'f') rsiz = 6*sizeof(float); |
544 |
+ |
else if (inform == 'd') rsiz = 6*sizeof(double); |
545 |
+ |
/* check against pipe limit */ |
546 |
+ |
if (qlength*rsiz > 512 && is_fifo(inpfp)) |
547 |
+ |
inp_queue = (FVECT *)malloc(sizeof(FVECT)*2*qlength); |
548 |
+ |
inp_qend = -(inp_queue == NULL); /* flag for no queue */ |
549 |
+ |
} |
550 |
+ |
if (inp_qend < 0) { /* not queuing? */ |
551 |
+ |
if (getvec(org, inform, inpfp) < 0 || |
552 |
+ |
getvec(dir, inform, inpfp) < 0) |
553 |
+ |
return(-1.); |
554 |
+ |
return normalize(dir); |
555 |
+ |
} |
556 |
+ |
if (inp_qpos >= inp_qend) { /* need to refill input queue? */ |
557 |
+ |
for (inp_qend = 0; inp_qend < qlength; inp_qend++) { |
558 |
+ |
if (getvec(inp_queue[2*inp_qend], inform, inpfp) < 0 |
559 |
+ |
|| getvec(inp_queue[2*inp_qend+1], |
560 |
+ |
inform, inpfp) < 0) |
561 |
+ |
break; /* hit EOF */ |
562 |
+ |
if (iszerovec(inp_queue[2*inp_qend+1])) { |
563 |
+ |
++inp_qend; /* flush request */ |
564 |
+ |
break; |
565 |
+ |
} |
566 |
+ |
} |
567 |
+ |
inp_qpos = 0; |
568 |
+ |
} |
569 |
+ |
if (inp_qpos >= inp_qend) /* unexpected EOF? */ |
570 |
+ |
return(-1.); |
571 |
+ |
VCOPY(org, inp_queue[2*inp_qpos]); |
572 |
+ |
VCOPY(dir, inp_queue[2*inp_qpos+1]); |
573 |
+ |
++inp_qpos; |
574 |
+ |
return normalize(dir); |
575 |
+ |
} |
576 |
+ |
|
577 |
+ |
|
578 |
|
void |
579 |
|
tranotify( /* record new modifier */ |
580 |
|
OBJECT obj |
767 |
|
RAY *r |
768 |
|
) |
769 |
|
{ |
770 |
< |
if (r->rot < FHUGE) |
770 |
> |
if (r->rot < FHUGE*.99) |
771 |
|
(*putreal)(r->rop, 3); |
772 |
|
else |
773 |
|
(*putreal)(vdummy, 3); |
779 |
|
RAY *r |
780 |
|
) |
781 |
|
{ |
782 |
< |
if (r->rot < FHUGE) { |
782 |
> |
if (r->rot < FHUGE*.99) { |
783 |
|
if (r->rflips & 1) { /* undo any flippin' flips */ |
784 |
|
FVECT unrm; |
785 |
|
unrm[0] = -r->ron[0]; |
800 |
|
{ |
801 |
|
FVECT pnorm; |
802 |
|
|
803 |
< |
if (r->rot >= FHUGE) { |
803 |
> |
if (r->rot >= FHUGE*.99) { |
804 |
|
(*putreal)(vdummy, 3); |
805 |
|
return; |
806 |
|
} |