45 |
|
extern int hresolu; /* horizontal resolution */ |
46 |
|
extern int vresolu; /* vertical resolution */ |
47 |
|
|
48 |
< |
static int castonly = 0; |
48 |
> |
int castonly = 0; /* only doing ray-casting? */ |
49 |
|
|
50 |
|
#ifndef MAXTSET |
51 |
|
#define MAXTSET 8191 /* maximum number in trace set */ |
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 int is_fifo(FILE *fp); |
73 |
|
static void bogusray(void); |
74 |
|
static void raycast(RAY *r); |
75 |
|
static void rayirrad(RAY *r); |
76 |
|
static void rtcompute(FVECT org, FVECT dir, double dmax); |
77 |
|
static int printvals(RAY *r); |
78 |
|
static int getvec(FVECT vec, int fmt, FILE *fp); |
79 |
+ |
static int iszerovec(const FVECT vec); |
80 |
+ |
static double nextray(FVECT org, FVECT dir); |
81 |
|
static void tabin(RAY *r); |
82 |
|
static void ourtrace(RAY *r); |
83 |
|
|
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
< |
extern void |
120 |
> |
void |
121 |
|
rtrace( /* trace rays from file */ |
122 |
|
char *fname, |
123 |
|
int nproc |
125 |
|
{ |
126 |
|
unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu |
127 |
|
: (unsigned long)vresolu; |
128 |
< |
long nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : hresolu; |
128 |
> |
long nextflush = (!vresolu | (hresolu <= 1)) * hresolu; |
129 |
|
int something2flush = 0; |
130 |
|
FILE *fp; |
131 |
|
double d; |
132 |
|
FVECT orig, direc; |
133 |
|
/* set up input */ |
134 |
|
if (fname == NULL) |
135 |
< |
fp = stdin; |
136 |
< |
else if ((fp = fopen(fname, "r")) == NULL) { |
135 |
> |
inpfp = stdin; |
136 |
> |
else if ((inpfp = fopen(fname, "r")) == NULL) { |
137 |
|
sprintf(errmsg, "cannot open input file \"%s\"", fname); |
138 |
|
error(SYSTEM, errmsg); |
139 |
|
} |
140 |
+ |
#ifdef getc_unlocked |
141 |
+ |
flockfile(inpfp); /* avoid lock/unlock overhead */ |
142 |
+ |
flockfile(stdout); |
143 |
+ |
#endif |
144 |
|
if (inform != 'a') |
145 |
< |
SET_FILE_BINARY(fp); |
145 |
> |
SET_FILE_BINARY(inpfp); |
146 |
|
/* set up output */ |
147 |
< |
setoutput(outvals); |
136 |
< |
if (imm_irrad) |
137 |
< |
castonly = 0; |
138 |
< |
else if (castonly) |
147 |
> |
if (castonly || every_out[0] != NULL) |
148 |
|
nproc = 1; /* don't bother multiprocessing */ |
149 |
|
if ((nextflush > 0) & (nproc > nextflush)) { |
150 |
|
error(WARNING, "reducing number of processes to match flush interval"); |
155 |
|
case 'f': putreal = putf; break; |
156 |
|
case 'd': putreal = putd; break; |
157 |
|
case 'c': |
158 |
< |
if (outvals[0] && (outvals[1] || !strchr("vrx", outvals[0]))) |
158 |
> |
if (outvals[1] || !strchr("vrx", outvals[0])) |
159 |
|
error(USER, "color format only with -ov, -or, -ox"); |
160 |
|
putreal = putrgbe; break; |
161 |
|
default: |
171 |
|
else |
172 |
|
fflush(stdout); |
173 |
|
} |
174 |
< |
/* process file */ |
175 |
< |
while (getvec(orig, inform, fp) == 0 && |
176 |
< |
getvec(direc, inform, fp) == 0) { |
177 |
< |
|
169 |
< |
d = normalize(direc); |
170 |
< |
if (d == 0.0) { /* zero ==> flush */ |
171 |
< |
if ((--nextflush <= 0) | !vcount && something2flush) { |
174 |
> |
/* process input rays */ |
175 |
> |
while ((d = nextray(orig, direc)) >= 0.0) { |
176 |
> |
if (d == 0.0) { /* flush request? */ |
177 |
> |
if (something2flush) { |
178 |
|
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
179 |
|
error(USER, "child(ren) died"); |
180 |
|
bogusray(); |
181 |
|
fflush(stdout); |
182 |
+ |
nextflush = (!vresolu | (hresolu <= 1)) * hresolu; |
183 |
|
something2flush = 0; |
177 |
– |
nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : |
178 |
– |
hresolu; |
184 |
|
} else |
185 |
|
bogusray(); |
186 |
|
} else { /* compute and print */ |
187 |
|
rtcompute(orig, direc, lim_dist ? d : 0.0); |
188 |
< |
/* flush if time */ |
184 |
< |
if (!--nextflush) { |
188 |
> |
if (!--nextflush) { /* flush if time */ |
189 |
|
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
190 |
|
error(USER, "child(ren) died"); |
191 |
|
fflush(stdout); |
203 |
|
error(USER, "unable to complete processing"); |
204 |
|
ray_pclose(0); |
205 |
|
} |
206 |
+ |
if (vcount) |
207 |
+ |
error(WARNING, "unexpected EOF on input"); |
208 |
|
if (fflush(stdout) < 0) |
209 |
|
error(SYSTEM, "write error"); |
210 |
< |
if (vcount) |
211 |
< |
error(USER, "unexpected EOF on input"); |
212 |
< |
if (fname != NULL) |
213 |
< |
fclose(fp); |
210 |
> |
if (fname != NULL) { |
211 |
> |
fclose(inpfp); |
212 |
> |
inpfp = NULL; |
213 |
> |
} |
214 |
> |
nextray(NULL, NULL); |
215 |
|
} |
216 |
|
|
217 |
|
|
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
< |
static void |
229 |
< |
setoutput( /* set up output tables */ |
223 |
< |
char *vs |
224 |
< |
) |
228 |
> |
int |
229 |
> |
setrtoutput(void) /* set up output tables, return #comp */ |
230 |
|
{ |
231 |
+ |
char *vs = outvals; |
232 |
|
oputf_t **table = ray_out; |
233 |
+ |
int ncomp = 0; |
234 |
|
|
235 |
< |
castonly = 1; |
236 |
< |
while (*vs) |
237 |
< |
switch (*vs++) { |
235 |
> |
if (!*vs) |
236 |
> |
error(USER, "empty output specification"); |
237 |
> |
|
238 |
> |
castonly = 1; /* sets castonly as side-effect */ |
239 |
> |
do |
240 |
> |
switch (*vs) { |
241 |
|
case 'T': /* trace sources */ |
242 |
< |
if (!*vs) break; |
242 |
> |
if (!vs[1]) break; |
243 |
|
trace_sources(); |
244 |
|
/* fall through */ |
245 |
|
case 't': /* trace */ |
246 |
< |
if (!*vs) break; |
246 |
> |
if (!vs[1]) break; |
247 |
|
*table = NULL; |
248 |
|
table = every_out; |
249 |
|
trace = ourtrace; |
251 |
|
break; |
252 |
|
case 'o': /* origin */ |
253 |
|
*table++ = oputo; |
254 |
+ |
ncomp += 3; |
255 |
|
break; |
256 |
|
case 'd': /* direction */ |
257 |
|
*table++ = oputd; |
258 |
+ |
ncomp += 3; |
259 |
|
break; |
260 |
|
case 'r': /* reflected contrib. */ |
261 |
|
*table++ = oputr; |
262 |
+ |
ncomp += 3; |
263 |
|
castonly = 0; |
264 |
|
break; |
265 |
|
case 'R': /* reflected distance */ |
266 |
|
*table++ = oputR; |
267 |
+ |
ncomp++; |
268 |
|
castonly = 0; |
269 |
|
break; |
270 |
|
case 'x': /* xmit contrib. */ |
271 |
|
*table++ = oputx; |
272 |
+ |
ncomp += 3; |
273 |
|
castonly = 0; |
274 |
|
break; |
275 |
|
case 'X': /* xmit distance */ |
276 |
|
*table++ = oputX; |
277 |
+ |
ncomp++; |
278 |
|
castonly = 0; |
279 |
|
break; |
280 |
|
case 'v': /* value */ |
281 |
|
*table++ = oputv; |
282 |
+ |
ncomp += 3; |
283 |
|
castonly = 0; |
284 |
|
break; |
285 |
|
case 'V': /* contribution */ |
286 |
|
*table++ = oputV; |
287 |
+ |
ncomp += 3; |
288 |
|
castonly = 0; |
289 |
|
if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0)) |
290 |
|
error(WARNING, |
292 |
|
break; |
293 |
|
case 'l': /* effective distance */ |
294 |
|
*table++ = oputl; |
295 |
+ |
ncomp++; |
296 |
|
castonly = 0; |
297 |
|
break; |
298 |
|
case 'c': /* local coordinates */ |
299 |
|
*table++ = oputc; |
300 |
+ |
ncomp += 2; |
301 |
|
break; |
302 |
|
case 'L': /* single ray length */ |
303 |
|
*table++ = oputL; |
304 |
+ |
ncomp++; |
305 |
|
break; |
306 |
|
case 'p': /* point */ |
307 |
|
*table++ = oputp; |
308 |
+ |
ncomp += 3; |
309 |
|
break; |
310 |
|
case 'n': /* perturbed normal */ |
311 |
|
*table++ = oputn; |
312 |
+ |
ncomp += 3; |
313 |
|
castonly = 0; |
314 |
|
break; |
315 |
|
case 'N': /* unperturbed normal */ |
316 |
|
*table++ = oputN; |
317 |
+ |
ncomp += 3; |
318 |
|
break; |
319 |
|
case 's': /* surface */ |
320 |
|
*table++ = oputs; |
321 |
+ |
ncomp++; |
322 |
|
break; |
323 |
|
case 'w': /* weight */ |
324 |
|
*table++ = oputw; |
325 |
+ |
ncomp++; |
326 |
|
break; |
327 |
|
case 'W': /* coefficient */ |
328 |
|
*table++ = oputW; |
329 |
+ |
ncomp += 3; |
330 |
|
castonly = 0; |
331 |
|
if (ambounce > 0 && (ambacc > FTINY) | (ambssamp > 0)) |
332 |
|
error(WARNING, |
334 |
|
break; |
335 |
|
case 'm': /* modifier */ |
336 |
|
*table++ = oputm; |
337 |
+ |
ncomp++; |
338 |
|
break; |
339 |
|
case 'M': /* material */ |
340 |
|
*table++ = oputM; |
341 |
+ |
ncomp++; |
342 |
|
break; |
343 |
|
case '~': /* tilde */ |
344 |
|
*table++ = oputtilde; |
345 |
|
break; |
346 |
+ |
default: |
347 |
+ |
sprintf(errmsg, "unrecognized output option '%c'", *vs); |
348 |
+ |
error(USER, errmsg); |
349 |
|
} |
350 |
+ |
while (*++vs); |
351 |
+ |
|
352 |
|
*table = NULL; |
353 |
+ |
if (*every_out != NULL) |
354 |
+ |
ncomp = 0; |
355 |
|
/* compatibility */ |
356 |
+ |
if ((do_irrad | imm_irrad) && castonly) |
357 |
+ |
error(USER, "-I+ and -i+ options require some value output"); |
358 |
|
for (table = ray_out; *table != NULL; table++) { |
359 |
|
if ((*table == oputV) | (*table == oputW)) |
360 |
|
error(WARNING, "-oVW options require trace mode"); |
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 |
|
} |