14 |
|
|
15 |
|
#include <fcntl.h> |
16 |
|
|
17 |
+ |
#include <ctype.h> |
18 |
+ |
|
19 |
|
#include "view.h" |
20 |
|
|
21 |
|
#include "color.h" |
32 |
|
#define F_FORE 1 /* fill foreground */ |
33 |
|
#define F_BACK 2 /* fill background */ |
34 |
|
|
35 |
< |
#define PACKSIZ 42 /* calculation packet size */ |
35 |
> |
#define PACKSIZ 256 /* max. calculation packet size */ |
36 |
|
|
37 |
< |
#define RTCOM "rtrace -h -ovl -fff %s" |
37 |
> |
#define RTCOM "rtrace -h- -ovl -fff" |
38 |
|
|
39 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
40 |
|
|
69 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
70 |
|
int hasmatrix = 0; /* has transformation matrix */ |
71 |
|
|
72 |
< |
int childpid = -1; /* id of fill process */ |
73 |
< |
FILE *psend, *precv; /* pipes to/from fill calculation */ |
74 |
< |
int queue[PACKSIZ][2]; /* pending pixels */ |
72 |
> |
int PDesc[3] = {-1,-1,-1}; /* rtrace process descriptor */ |
73 |
> |
#define childpid (PDesc[2]) |
74 |
> |
unsigned short queue[PACKSIZ][2]; /* pending pixels */ |
75 |
> |
int packsiz; /* actual packet size */ |
76 |
|
int queuesiz; /* number of pixels pending */ |
77 |
|
|
78 |
|
|
80 |
|
int argc; |
81 |
|
char *argv[]; |
82 |
|
{ |
83 |
< |
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
84 |
< |
extern double atof(); |
83 |
> |
#define check(ol,al) if (argv[i][ol] || \ |
84 |
> |
badarg(argc-i-1,argv+i+1,al)) \ |
85 |
> |
goto badopt |
86 |
|
int gotvfile = 0; |
87 |
|
char *zfile = NULL; |
88 |
|
char *err; |
98 |
|
} |
99 |
|
switch (argv[i][1]) { |
100 |
|
case 't': /* threshold */ |
101 |
< |
check(2,1); |
101 |
> |
check(2,"f"); |
102 |
|
zeps = atof(argv[++i]); |
103 |
|
break; |
104 |
|
case 'n': /* dist. normalized? */ |
105 |
< |
check(2,0); |
105 |
> |
check(2,NULL); |
106 |
|
normdist = !normdist; |
107 |
|
break; |
108 |
|
case 'f': /* fill type */ |
109 |
|
switch (argv[i][2]) { |
110 |
|
case '0': /* none */ |
111 |
< |
check(3,0); |
111 |
> |
check(3,NULL); |
112 |
|
fillo = 0; |
113 |
|
break; |
114 |
|
case 'f': /* foreground */ |
115 |
< |
check(3,0); |
115 |
> |
check(3,NULL); |
116 |
|
fillo = F_FORE; |
117 |
|
break; |
118 |
|
case 'b': /* background */ |
119 |
< |
check(3,0); |
119 |
> |
check(3,NULL); |
120 |
|
fillo = F_BACK; |
121 |
|
break; |
122 |
|
case 'a': /* all */ |
123 |
< |
check(3,0); |
123 |
> |
check(3,NULL); |
124 |
|
fillo = F_FORE|F_BACK; |
125 |
|
break; |
126 |
|
case 's': /* sample */ |
127 |
< |
check(3,1); |
127 |
> |
check(3,"i"); |
128 |
|
fillsamp = atoi(argv[++i]); |
129 |
|
break; |
130 |
|
case 'c': /* color */ |
131 |
< |
check(3,3); |
131 |
> |
check(3,"fff"); |
132 |
|
fillfunc = backfill; |
133 |
|
setcolr(backcolr, atof(argv[i+1]), |
134 |
|
atof(argv[i+2]), atof(argv[i+3])); |
135 |
|
i += 3; |
136 |
|
break; |
137 |
|
case 'z': /* z value */ |
138 |
< |
check(3,1); |
138 |
> |
check(3,"f"); |
139 |
|
fillfunc = backfill; |
140 |
|
backz = atof(argv[++i]); |
141 |
|
break; |
142 |
|
case 'r': /* rtrace */ |
143 |
< |
check(3,1); |
143 |
> |
check(3,"s"); |
144 |
|
fillfunc = rcalfill; |
145 |
|
calstart(RTCOM, argv[++i]); |
146 |
|
break; |
149 |
|
} |
150 |
|
break; |
151 |
|
case 'z': /* z file */ |
152 |
< |
check(2,1); |
152 |
> |
check(2,"s"); |
153 |
|
zfile = argv[++i]; |
154 |
|
break; |
155 |
|
case 'x': /* x resolution */ |
156 |
< |
check(2,1); |
156 |
> |
check(2,"i"); |
157 |
|
hresolu = atoi(argv[++i]); |
158 |
|
break; |
159 |
|
case 'y': /* y resolution */ |
160 |
< |
check(2,1); |
160 |
> |
check(2,"i"); |
161 |
|
vresolu = atoi(argv[++i]); |
162 |
|
break; |
163 |
|
case 'p': /* pixel aspect */ |
164 |
< |
check(2,1); |
164 |
> |
check(2,"f"); |
165 |
|
pixaspect = atof(argv[++i]); |
166 |
|
break; |
167 |
|
case 'v': /* view file */ |
168 |
|
if (argv[i][2] != 'f') |
169 |
|
goto badopt; |
170 |
< |
check(3,1); |
170 |
> |
check(3,"s"); |
171 |
|
gotvfile = viewfile(argv[++i], &ourview, 0, 0); |
172 |
|
if (gotvfile < 0) { |
173 |
|
perror(argv[i]); |
188 |
|
/* check arguments */ |
189 |
|
if ((argc-i)%2) |
190 |
|
goto userr; |
191 |
+ |
if (fillsamp == 1) |
192 |
+ |
fillo &= ~F_BACK; |
193 |
|
/* set view */ |
194 |
|
if (err = setview(&ourview)) { |
195 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
271 |
|
addpicture(pfile, zspec) /* add picture to output */ |
272 |
|
char *pfile, *zspec; |
273 |
|
{ |
268 |
– |
extern double atof(); |
274 |
|
FILE *pfp; |
275 |
|
int zfd; |
276 |
|
char *err; |
698 |
|
} |
699 |
|
|
700 |
|
|
701 |
< |
calstart(prog, args) /* start fill calculation */ |
701 |
> |
calstart(prog, args) /* start fill calculation */ |
702 |
|
char *prog, *args; |
703 |
|
{ |
704 |
|
char combuf[512]; |
705 |
< |
int p0[2], p1[2]; |
705 |
> |
char *argv[64]; |
706 |
> |
int rval; |
707 |
> |
register char **wp, *cp; |
708 |
|
|
709 |
|
if (childpid != -1) { |
710 |
|
fprintf(stderr, "%s: too many calculations\n", progname); |
711 |
|
exit(1); |
712 |
|
} |
713 |
< |
sprintf(combuf, prog, args); |
714 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) |
715 |
< |
syserror(); |
716 |
< |
if ((childpid = vfork()) == 0) { /* fork calculation */ |
717 |
< |
close(p0[1]); |
718 |
< |
close(p1[0]); |
719 |
< |
if (p0[0] != 0) { |
720 |
< |
dup2(p0[0], 0); |
721 |
< |
close(p0[0]); |
722 |
< |
} |
723 |
< |
if (p1[1] != 1) { |
717 |
< |
dup2(p1[1], 1); |
718 |
< |
close(p1[1]); |
719 |
< |
} |
720 |
< |
execl("/bin/sh", "sh", "-c", combuf, 0); |
721 |
< |
perror("/bin/sh"); |
722 |
< |
_exit(127); |
713 |
> |
strcpy(combuf, prog); |
714 |
> |
strcat(combuf, args); |
715 |
> |
cp = combuf; |
716 |
> |
wp = argv; |
717 |
> |
for ( ; ; ) { |
718 |
> |
while (isspace(*cp)) cp++; |
719 |
> |
if (!*cp) break; |
720 |
> |
*wp++ = cp; |
721 |
> |
while (!isspace(*cp)) |
722 |
> |
if (!*cp++) goto done; |
723 |
> |
*cp++ = '\0'; |
724 |
|
} |
725 |
< |
if (childpid == -1) |
725 |
> |
done: |
726 |
> |
*wp = NULL; |
727 |
> |
/* start process */ |
728 |
> |
if ((rval = open_process(PDesc, argv)) < 0) |
729 |
|
syserror(); |
730 |
< |
close(p0[0]); |
731 |
< |
close(p1[1]); |
732 |
< |
if ((psend = fdopen(p0[1], "w")) == NULL) |
733 |
< |
syserror(); |
734 |
< |
if ((precv = fdopen(p1[0], "r")) == NULL) |
735 |
< |
syserror(); |
730 |
> |
if (rval == 0) { |
731 |
> |
fprintf(stderr, "%s: command not found\n", argv[0]); |
732 |
> |
exit(1); |
733 |
> |
} |
734 |
> |
packsiz = rval/(6*sizeof(float)) - 1; |
735 |
> |
if (packsiz > PACKSIZ) |
736 |
> |
packsiz = PACKSIZ; |
737 |
|
queuesiz = 0; |
738 |
|
} |
739 |
|
|
740 |
|
|
741 |
< |
caldone() /* done with calculation */ |
741 |
> |
caldone() /* done with calculation */ |
742 |
|
{ |
738 |
– |
int pid; |
739 |
– |
|
743 |
|
if (childpid == -1) |
744 |
|
return; |
745 |
|
clearqueue(); |
746 |
< |
fclose(psend); |
744 |
< |
fclose(precv); |
745 |
< |
while ((pid = wait(0)) != -1 && pid != childpid) |
746 |
< |
; |
746 |
> |
close_process(PDesc); |
747 |
|
childpid = -1; |
748 |
|
} |
749 |
|
|
751 |
|
rcalfill(x, y) /* fill with ray-calculated pixel */ |
752 |
|
int x, y; |
753 |
|
{ |
754 |
< |
if (queuesiz >= PACKSIZ) /* flush queue if needed */ |
754 |
> |
if (queuesiz >= packsiz) /* flush queue if needed */ |
755 |
|
clearqueue(); |
756 |
|
/* add position to queue */ |
757 |
|
queue[queuesiz][0] = x; |
763 |
|
clearqueue() /* process queue */ |
764 |
|
{ |
765 |
|
FVECT orig, dir; |
766 |
< |
float fbuf[6]; |
766 |
> |
float fbuf[6*(PACKSIZ+1)]; |
767 |
> |
register float *fbp; |
768 |
|
register int i; |
769 |
|
|
770 |
+ |
fbp = fbuf; |
771 |
|
for (i = 0; i < queuesiz; i++) { |
772 |
|
viewray(orig, dir, &ourview, |
773 |
|
(queue[i][0]+.5)/hresolu, |
774 |
|
(queue[i][1]+.5)/vresolu); |
775 |
< |
fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2]; |
776 |
< |
fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2]; |
775 |
< |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
775 |
> |
*fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2]; |
776 |
> |
*fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2]; |
777 |
|
} |
778 |
< |
/* flush output and get results */ |
779 |
< |
fbuf[3] = fbuf[4] = fbuf[5] = 0.0; /* mark */ |
780 |
< |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
781 |
< |
if (fflush(psend) == EOF) |
782 |
< |
syserror(); |
778 |
> |
/* mark end and get results */ |
779 |
> |
bzero((char *)fbp, 6*sizeof(float)); |
780 |
> |
if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz, |
781 |
> |
6*sizeof(float)*(queuesiz+1)) != |
782 |
> |
4*sizeof(float)*queuesiz) { |
783 |
> |
fprintf(stderr, "%s: error reading from rtrace process\n", |
784 |
> |
progname); |
785 |
> |
exit(1); |
786 |
> |
} |
787 |
> |
fbp = fbuf; |
788 |
|
for (i = 0; i < queuesiz; i++) { |
783 |
– |
if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) { |
784 |
– |
fprintf(stderr, "%s: read error in clearqueue\n", |
785 |
– |
progname); |
786 |
– |
exit(1); |
787 |
– |
} |
789 |
|
if (ourexp > 0 && ourexp != 1.0) { |
790 |
< |
fbuf[0] *= ourexp; |
791 |
< |
fbuf[1] *= ourexp; |
792 |
< |
fbuf[2] *= ourexp; |
790 |
> |
fbp[0] *= ourexp; |
791 |
> |
fbp[1] *= ourexp; |
792 |
> |
fbp[2] *= ourexp; |
793 |
|
} |
794 |
|
setcolr(pscan(queue[i][1])[queue[i][0]], |
795 |
< |
fbuf[0], fbuf[1], fbuf[2]); |
796 |
< |
zscan(queue[i][1])[queue[i][0]] = fbuf[3]; |
795 |
> |
fbp[0], fbp[1], fbp[2]); |
796 |
> |
zscan(queue[i][1])[queue[i][0]] = fbp[3]; |
797 |
> |
fbp += 4; |
798 |
|
} |
799 |
|
queuesiz = 0; |
800 |
|
} |