28 |
|
#include "ambient.h" |
29 |
|
#include "source.h" |
30 |
|
#include "otypes.h" |
31 |
+ |
#include "otspecial.h" |
32 |
|
#include "resolu.h" |
33 |
|
#include "random.h" |
34 |
|
|
54 |
|
|
55 |
|
static RAY thisray; /* for our convenience */ |
56 |
|
|
57 |
< |
typedef void putf_t(double v); |
57 |
> |
typedef void putf_t(RREAL *v, int n); |
58 |
|
static putf_t puta, putd, putf; |
59 |
|
|
60 |
|
typedef void oputf_t(RAY *r); |
64 |
|
static void setoutput(char *vs); |
65 |
|
extern void tranotify(OBJECT obj); |
66 |
|
static void bogusray(void); |
67 |
< |
static void rad(FVECT org, FVECT dir, double dmax); |
68 |
< |
static void irrad(FVECT org, FVECT dir); |
67 |
> |
static void raycast(RAY *r); |
68 |
> |
static void rayirrad(RAY *r); |
69 |
> |
static void rtcompute(FVECT org, FVECT dir, double dmax); |
70 |
|
static int printvals(RAY *r); |
71 |
|
static int getvec(FVECT vec, int fmt, FILE *fp); |
72 |
|
static void tabin(RAY *r); |
84 |
|
if (ray_pnprocs > 0) /* close children if any */ |
85 |
|
ray_pclose(0); |
86 |
|
#ifndef NON_POSIX |
87 |
< |
headclean(); /* delete header file */ |
88 |
< |
pfclean(); /* clean up persist files */ |
87 |
> |
else if (!ray_pnprocs) { |
88 |
> |
headclean(); /* delete header file */ |
89 |
> |
pfclean(); /* clean up persist files */ |
90 |
> |
} |
91 |
|
#endif |
92 |
|
exit(code); |
93 |
|
} |
110 |
|
|
111 |
|
extern void |
112 |
|
rtrace( /* trace rays from file */ |
113 |
< |
char *fname |
113 |
> |
char *fname, |
114 |
> |
int nproc |
115 |
|
) |
116 |
|
{ |
117 |
|
unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu |
118 |
< |
: vresolu; |
119 |
< |
long nextflush = hresolu; |
118 |
> |
: (unsigned long)vresolu; |
119 |
> |
long nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : hresolu; |
120 |
|
FILE *fp; |
121 |
|
double d; |
122 |
|
FVECT orig, direc; |
131 |
|
SET_FILE_BINARY(fp); |
132 |
|
/* set up output */ |
133 |
|
setoutput(outvals); |
134 |
+ |
if (imm_irrad) |
135 |
+ |
castonly = 0; |
136 |
+ |
else if (castonly) |
137 |
+ |
nproc = 1; /* don't bother multiprocessing */ |
138 |
|
switch (outform) { |
139 |
|
case 'a': putreal = puta; break; |
140 |
|
case 'f': putreal = putf; break; |
146 |
|
default: |
147 |
|
error(CONSISTENCY, "botched output format"); |
148 |
|
} |
149 |
< |
ray_fifo_out = printvals; |
149 |
> |
if (nproc > 1) { /* start multiprocessing */ |
150 |
> |
ray_popen(nproc); |
151 |
> |
ray_fifo_out = printvals; |
152 |
> |
} |
153 |
|
if (hresolu > 0) { |
154 |
|
if (vresolu > 0) |
155 |
|
fprtresolu(hresolu, vresolu, stdout); |
161 |
|
|
162 |
|
d = normalize(direc); |
163 |
|
if (d == 0.0) { /* zero ==> flush */ |
152 |
– |
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
153 |
– |
error(USER, "lost children"); |
154 |
– |
bogusray(); |
164 |
|
if (--nextflush <= 0 || !vcount) { |
165 |
+ |
if (nproc > 1 && ray_fifo_flush() < 0) |
166 |
+ |
error(USER, "child(ren) died"); |
167 |
+ |
bogusray(); |
168 |
|
fflush(stdout); |
169 |
< |
nextflush = hresolu; |
170 |
< |
} |
169 |
> |
nextflush = (vresolu > 0) & (hresolu > 1) ? 0 : |
170 |
> |
hresolu; |
171 |
> |
} else |
172 |
> |
bogusray(); |
173 |
|
} else { /* compute and print */ |
174 |
< |
if (imm_irrad) |
161 |
< |
irrad(orig, direc); |
162 |
< |
else |
163 |
< |
rad(orig, direc, lim_dist ? d : 0.0); |
174 |
> |
rtcompute(orig, direc, lim_dist ? d : 0.0); |
175 |
|
/* flush if time */ |
176 |
|
if (!--nextflush) { |
177 |
< |
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
178 |
< |
error(USER, "lost children"); |
177 |
> |
if (nproc > 1 && ray_fifo_flush() < 0) |
178 |
> |
error(USER, "child(ren) died"); |
179 |
|
fflush(stdout); |
180 |
|
nextflush = hresolu; |
181 |
|
} |
185 |
|
if (vcount && !--vcount) /* check for end */ |
186 |
|
break; |
187 |
|
} |
188 |
< |
if (ray_pnprocs > 1 && ray_fifo_flush() < 0) |
189 |
< |
error(USER, "unable to complete processing"); |
188 |
> |
if (nproc > 1) { /* clean up children */ |
189 |
> |
if (ray_fifo_flush() < 0) |
190 |
> |
error(USER, "unable to complete processing"); |
191 |
> |
ray_pclose(0); |
192 |
> |
} |
193 |
|
if (fflush(stdout) < 0) |
194 |
|
error(SYSTEM, "write error"); |
195 |
|
if (vcount) |
304 |
|
|
305 |
|
|
306 |
|
static void |
307 |
< |
rad( /* compute and print ray value(s) */ |
308 |
< |
FVECT org, |
295 |
< |
FVECT dir, |
296 |
< |
double dmax |
307 |
> |
raycast( /* compute first ray intersection only */ |
308 |
> |
RAY *r |
309 |
|
) |
310 |
|
{ |
311 |
< |
VCOPY(thisray.rorg, org); |
312 |
< |
VCOPY(thisray.rdir, dir); |
313 |
< |
thisray.rmax = dmax; |
314 |
< |
if (ray_pnprocs > 1) { |
315 |
< |
if (ray_fifo_in(&thisray) < 0) |
316 |
< |
error(USER, "lost children"); |
305 |
< |
return; |
311 |
> |
if (!localhit(r, &thescene)) { |
312 |
> |
if (r->ro == &Aftplane) { /* clipped */ |
313 |
> |
r->ro = NULL; |
314 |
> |
r->rot = FHUGE; |
315 |
> |
} else |
316 |
> |
sourcehit(r); |
317 |
|
} |
307 |
– |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
308 |
– |
if (castonly) { |
309 |
– |
if (!localhit(&thisray, &thescene)) { |
310 |
– |
if (thisray.ro == &Aftplane) { /* clipped */ |
311 |
– |
thisray.ro = NULL; |
312 |
– |
thisray.rot = FHUGE; |
313 |
– |
} else |
314 |
– |
sourcehit(&thisray); |
315 |
– |
} |
316 |
– |
} else |
317 |
– |
ray_trace(&thisray); |
318 |
– |
printvals(&thisray); |
318 |
|
} |
319 |
|
|
320 |
|
|
321 |
|
static void |
322 |
< |
irrad( /* compute immediate irradiance value */ |
322 |
> |
rayirrad( /* compute irradiance rather than radiance */ |
323 |
> |
RAY *r |
324 |
> |
) |
325 |
> |
{ |
326 |
> |
void (*old_revf)(RAY *) = r->revf; |
327 |
> |
|
328 |
> |
r->rot = 1e-5; /* pretend we hit surface */ |
329 |
> |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
330 |
> |
r->ron[0] = -r->rdir[0]; |
331 |
> |
r->ron[1] = -r->rdir[1]; |
332 |
> |
r->ron[2] = -r->rdir[2]; |
333 |
> |
r->rod = 1.0; |
334 |
> |
/* compute result */ |
335 |
> |
r->revf = raytrace; |
336 |
> |
(*ofun[Lamb.otype].funp)(&Lamb, r); |
337 |
> |
r->revf = old_revf; |
338 |
> |
} |
339 |
> |
|
340 |
> |
|
341 |
> |
static void |
342 |
> |
rtcompute( /* compute and print ray value(s) */ |
343 |
|
FVECT org, |
344 |
< |
FVECT dir |
344 |
> |
FVECT dir, |
345 |
> |
double dmax |
346 |
|
) |
347 |
|
{ |
348 |
< |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
329 |
< |
thisray.rdir[0] = -dir[0]; |
330 |
< |
thisray.rdir[1] = -dir[1]; |
331 |
< |
thisray.rdir[2] = -dir[2]; |
332 |
< |
thisray.rmax = 0.0; |
348 |
> |
/* set up ray */ |
349 |
|
rayorigin(&thisray, PRIMARY, NULL, NULL); |
350 |
< |
/* pretend we hit surface */ |
351 |
< |
thisray.rot = 1e-5; |
352 |
< |
thisray.rod = 1.0; |
353 |
< |
VCOPY(thisray.ron, dir); |
354 |
< |
VSUM(thisray.rop, org, dir, 1e-4); |
355 |
< |
/* compute and print */ |
356 |
< |
(*ofun[Lamb.otype].funp)(&Lamb, &thisray); |
350 |
> |
if (imm_irrad) { |
351 |
> |
VSUM(thisray.rorg, org, dir, 1.1e-4); |
352 |
> |
thisray.rdir[0] = -dir[0]; |
353 |
> |
thisray.rdir[1] = -dir[1]; |
354 |
> |
thisray.rdir[2] = -dir[2]; |
355 |
> |
thisray.rmax = 0.0; |
356 |
> |
thisray.revf = rayirrad; |
357 |
> |
} else { |
358 |
> |
VCOPY(thisray.rorg, org); |
359 |
> |
VCOPY(thisray.rdir, dir); |
360 |
> |
thisray.rmax = dmax; |
361 |
> |
if (castonly) |
362 |
> |
thisray.revf = raycast; |
363 |
> |
} |
364 |
> |
if (ray_pnprocs > 1) { /* multiprocessing FIFO? */ |
365 |
> |
if (ray_fifo_in(&thisray) < 0) |
366 |
> |
error(USER, "lost children"); |
367 |
> |
return; |
368 |
> |
} |
369 |
> |
samplendx++; /* else do it ourselves */ |
370 |
> |
rayvalue(&thisray); |
371 |
|
printvals(&thisray); |
372 |
|
} |
373 |
|
|
411 |
|
} |
412 |
|
break; |
413 |
|
case 'f': /* binary float */ |
414 |
< |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
414 |
> |
if (getbinary(vf, sizeof(float), 3, fp) != 3) |
415 |
|
return(-1); |
416 |
< |
vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2]; |
416 |
> |
VCOPY(vec, vf); |
417 |
|
break; |
418 |
|
case 'd': /* binary double */ |
419 |
< |
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
419 |
> |
if (getbinary(vd, sizeof(double), 3, fp) != 3) |
420 |
|
return(-1); |
421 |
< |
vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2]; |
421 |
> |
VCOPY(vec, vd); |
422 |
|
break; |
423 |
|
default: |
424 |
|
error(CONSISTENCY, "botched input format"); |
495 |
|
RAY *r |
496 |
|
) |
497 |
|
{ |
498 |
< |
(*putreal)(r->rorg[0]); |
469 |
< |
(*putreal)(r->rorg[1]); |
470 |
< |
(*putreal)(r->rorg[2]); |
498 |
> |
(*putreal)(r->rorg, 3); |
499 |
|
} |
500 |
|
|
501 |
|
|
504 |
|
RAY *r |
505 |
|
) |
506 |
|
{ |
507 |
< |
(*putreal)(r->rdir[0]); |
480 |
< |
(*putreal)(r->rdir[1]); |
481 |
< |
(*putreal)(r->rdir[2]); |
507 |
> |
(*putreal)(r->rdir, 3); |
508 |
|
} |
509 |
|
|
510 |
|
|
513 |
|
RAY *r |
514 |
|
) |
515 |
|
{ |
516 |
+ |
RREAL cval[3]; |
517 |
+ |
|
518 |
|
if (outform == 'c') { |
519 |
|
COLR cout; |
520 |
|
setcolr(cout, colval(r->rcol,RED), |
521 |
|
colval(r->rcol,GRN), |
522 |
|
colval(r->rcol,BLU)); |
523 |
< |
fwrite((char *)cout, sizeof(cout), 1, stdout); |
523 |
> |
putbinary(cout, sizeof(cout), 1, stdout); |
524 |
|
return; |
525 |
|
} |
526 |
< |
(*putreal)(colval(r->rcol,RED)); |
527 |
< |
(*putreal)(colval(r->rcol,GRN)); |
528 |
< |
(*putreal)(colval(r->rcol,BLU)); |
526 |
> |
cval[0] = colval(r->rcol,RED); |
527 |
> |
cval[1] = colval(r->rcol,GRN); |
528 |
> |
cval[2] = colval(r->rcol,BLU); |
529 |
> |
(*putreal)(cval, 3); |
530 |
|
} |
531 |
|
|
532 |
|
|
535 |
|
RAY *r |
536 |
|
) |
537 |
|
{ |
538 |
< |
double contr[3]; |
538 |
> |
RREAL contr[3]; |
539 |
|
|
540 |
|
raycontrib(contr, r, PRIMARY); |
541 |
|
multcolor(contr, r->rcol); |
542 |
< |
(*putreal)(contr[RED]); |
514 |
< |
(*putreal)(contr[GRN]); |
515 |
< |
(*putreal)(contr[BLU]); |
542 |
> |
(*putreal)(contr, 3); |
543 |
|
} |
544 |
|
|
545 |
|
|
548 |
|
RAY *r |
549 |
|
) |
550 |
|
{ |
551 |
< |
(*putreal)(r->rt); |
551 |
> |
(*putreal)(&r->rt, 1); |
552 |
|
} |
553 |
|
|
554 |
|
|
557 |
|
RAY *r |
558 |
|
) |
559 |
|
{ |
560 |
< |
(*putreal)(r->rot); |
560 |
> |
(*putreal)(&r->rot, 1); |
561 |
|
} |
562 |
|
|
563 |
|
|
566 |
|
RAY *r |
567 |
|
) |
568 |
|
{ |
569 |
< |
(*putreal)(r->uv[0]); |
543 |
< |
(*putreal)(r->uv[1]); |
569 |
> |
(*putreal)(r->uv, 2); |
570 |
|
} |
571 |
|
|
572 |
|
|
573 |
+ |
static RREAL vdummy[3] = {0.0, 0.0, 0.0}; |
574 |
+ |
|
575 |
+ |
|
576 |
|
static void |
577 |
|
oputp( /* print point */ |
578 |
|
RAY *r |
579 |
|
) |
580 |
|
{ |
581 |
< |
if (r->rot < FHUGE) { |
582 |
< |
(*putreal)(r->rop[0]); |
583 |
< |
(*putreal)(r->rop[1]); |
584 |
< |
(*putreal)(r->rop[2]); |
556 |
< |
} else { |
557 |
< |
(*putreal)(0.0); |
558 |
< |
(*putreal)(0.0); |
559 |
< |
(*putreal)(0.0); |
560 |
< |
} |
581 |
> |
if (r->rot < FHUGE) |
582 |
> |
(*putreal)(r->rop, 3); |
583 |
> |
else |
584 |
> |
(*putreal)(vdummy, 3); |
585 |
|
} |
586 |
|
|
587 |
|
|
590 |
|
RAY *r |
591 |
|
) |
592 |
|
{ |
593 |
< |
if (r->rot < FHUGE) { |
594 |
< |
(*putreal)(r->ron[0]); |
595 |
< |
(*putreal)(r->ron[1]); |
596 |
< |
(*putreal)(r->ron[2]); |
573 |
< |
} else { |
574 |
< |
(*putreal)(0.0); |
575 |
< |
(*putreal)(0.0); |
576 |
< |
(*putreal)(0.0); |
577 |
< |
} |
593 |
> |
if (r->rot < FHUGE) |
594 |
> |
(*putreal)(r->ron, 3); |
595 |
> |
else |
596 |
> |
(*putreal)(vdummy, 3); |
597 |
|
} |
598 |
|
|
599 |
|
|
605 |
|
FVECT pnorm; |
606 |
|
|
607 |
|
if (r->rot >= FHUGE) { |
608 |
< |
(*putreal)(0.0); |
590 |
< |
(*putreal)(0.0); |
591 |
< |
(*putreal)(0.0); |
608 |
> |
(*putreal)(vdummy, 3); |
609 |
|
return; |
610 |
|
} |
611 |
|
raynormal(pnorm, r); |
612 |
< |
(*putreal)(pnorm[0]); |
596 |
< |
(*putreal)(pnorm[1]); |
597 |
< |
(*putreal)(pnorm[2]); |
612 |
> |
(*putreal)(pnorm, 3); |
613 |
|
} |
614 |
|
|
615 |
|
|
631 |
|
RAY *r |
632 |
|
) |
633 |
|
{ |
634 |
< |
(*putreal)(r->rweight); |
634 |
> |
RREAL rwt = r->rweight; |
635 |
> |
|
636 |
> |
(*putreal)(&rwt, 1); |
637 |
|
} |
638 |
|
|
639 |
|
|
642 |
|
RAY *r |
643 |
|
) |
644 |
|
{ |
645 |
< |
double contr[3]; |
645 |
> |
RREAL contr[3]; |
646 |
> |
/* shadow ray not on source? */ |
647 |
> |
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
648 |
> |
setcolor(contr, 0.0, 0.0, 0.0); |
649 |
> |
else |
650 |
> |
raycontrib(contr, r, PRIMARY); |
651 |
|
|
652 |
< |
raycontrib(contr, r, PRIMARY); |
631 |
< |
(*putreal)(contr[RED]); |
632 |
< |
(*putreal)(contr[GRN]); |
633 |
< |
(*putreal)(contr[BLU]); |
652 |
> |
(*putreal)(contr, 3); |
653 |
|
} |
654 |
|
|
655 |
|
|
697 |
|
|
698 |
|
|
699 |
|
static void |
700 |
< |
puta( /* print ascii value */ |
701 |
< |
double v |
700 |
> |
puta( /* print ascii value(s) */ |
701 |
> |
RREAL *v, int n |
702 |
|
) |
703 |
|
{ |
704 |
< |
printf("%e\t", v); |
704 |
> |
if (n == 3) { |
705 |
> |
printf("%e\t%e\t%e\t", v[0], v[1], v[2]); |
706 |
> |
return; |
707 |
> |
} |
708 |
> |
while (n--) |
709 |
> |
printf("%e\t", *v++); |
710 |
|
} |
711 |
|
|
712 |
|
|
713 |
|
static void |
714 |
< |
putd(v) /* print binary double */ |
691 |
< |
double v; |
714 |
> |
putd(RREAL *v, int n) /* print binary double(s) */ |
715 |
|
{ |
716 |
< |
fwrite((char *)&v, sizeof(v), 1, stdout); |
716 |
> |
#ifdef SMLFLT |
717 |
> |
double da[3]; |
718 |
> |
int i; |
719 |
> |
|
720 |
> |
if (n > 3) |
721 |
> |
error(INTERNAL, "code error in putd()"); |
722 |
> |
for (i = n; i--; ) |
723 |
> |
da[i] = v[i]; |
724 |
> |
putbinary(da, sizeof(double), n, stdout); |
725 |
> |
#else |
726 |
> |
putbinary(v, sizeof(RREAL), n, stdout); |
727 |
> |
#endif |
728 |
|
} |
729 |
|
|
730 |
|
|
731 |
|
static void |
732 |
< |
putf(v) /* print binary float */ |
699 |
< |
double v; |
732 |
> |
putf(RREAL *v, int n) /* print binary float(s) */ |
733 |
|
{ |
734 |
< |
float f = v; |
734 |
> |
#ifndef SMLFLT |
735 |
> |
float fa[3]; |
736 |
> |
int i; |
737 |
|
|
738 |
< |
fwrite((char *)&f, sizeof(f), 1, stdout); |
738 |
> |
if (n > 3) |
739 |
> |
error(INTERNAL, "code error in putf()"); |
740 |
> |
for (i = n; i--; ) |
741 |
> |
fa[i] = v[i]; |
742 |
> |
putbinary(fa, sizeof(float), n, stdout); |
743 |
> |
#else |
744 |
> |
putbinary(v, sizeof(RREAL), n, stdout); |
745 |
> |
#endif |
746 |
|
} |