1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: vwrays.c,v 3.8 2003/10/20 16:01:55 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Compute rays corresponding to a given picture or view. |
6 |
*/ |
7 |
|
8 |
#include "platform.h" |
9 |
#include "standard.h" |
10 |
#include "view.h" |
11 |
|
12 |
extern int putf(), putd(), puta(); |
13 |
|
14 |
int (*putr)() = puta; |
15 |
|
16 |
VIEW vw = STDVIEW; |
17 |
|
18 |
RESOLU rs = {PIXSTANDARD, 512, 512}; |
19 |
|
20 |
double pa = 1.; |
21 |
|
22 |
int zfd = -1; |
23 |
|
24 |
int fromstdin = 0; |
25 |
|
26 |
char *progname; |
27 |
|
28 |
|
29 |
main(argc, argv) |
30 |
int argc; |
31 |
char *argv[]; |
32 |
{ |
33 |
char *err; |
34 |
int rval, getdim = 0; |
35 |
register int i; |
36 |
|
37 |
progname = argv[0]; |
38 |
if (argc < 2) |
39 |
goto userr; |
40 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
41 |
switch (argv[i][1]) { |
42 |
case 'f': /* output format */ |
43 |
switch (argv[i][2]) { |
44 |
case 'a': /* ASCII */ |
45 |
putr = puta; |
46 |
break; |
47 |
case 'f': /* float */ |
48 |
putr = putf; |
49 |
break; |
50 |
case 'd': /* double */ |
51 |
putr = putd; |
52 |
break; |
53 |
default: |
54 |
goto userr; |
55 |
} |
56 |
break; |
57 |
case 'v': /* view file or option */ |
58 |
if (argv[i][2] == 'f') { |
59 |
rval = viewfile(argv[++i], &vw, NULL); |
60 |
if (rval <= 0) { |
61 |
fprintf(stderr, |
62 |
"%s: no view in file\n", |
63 |
argv[i]); |
64 |
exit(1); |
65 |
} |
66 |
break; |
67 |
} |
68 |
rval = getviewopt(&vw, argc-i, argv+i); |
69 |
if (rval < 0) |
70 |
goto userr; |
71 |
i += rval; |
72 |
break; |
73 |
case 'd': /* report dimensions only */ |
74 |
getdim++; |
75 |
break; |
76 |
case 'x': /* x resolution */ |
77 |
rs.xr = atoi(argv[++i]); |
78 |
if (rs.xr <= 0) { |
79 |
fprintf(stderr, "%s: bad x resolution\n", |
80 |
progname); |
81 |
exit(1); |
82 |
} |
83 |
break; |
84 |
case 'y': /* y resolution */ |
85 |
rs.yr = atoi(argv[++i]); |
86 |
if (rs.yr <= 0) { |
87 |
fprintf(stderr, "%s: bad y resolution\n", |
88 |
progname); |
89 |
exit(1); |
90 |
} |
91 |
break; |
92 |
case 'p': /* pixel aspect ratio */ |
93 |
pa = atof(argv[++i]); |
94 |
break; |
95 |
case 'i': /* get pixels from stdin */ |
96 |
fromstdin = 1; |
97 |
break; |
98 |
default: |
99 |
goto userr; |
100 |
} |
101 |
if ((i > argc) | (i+2 < argc)) |
102 |
goto userr; |
103 |
if (i < argc) { |
104 |
rval = viewfile(argv[i], &vw, &rs); |
105 |
if (rval <= 0) { |
106 |
fprintf(stderr, "%s: no view in picture\n", argv[i]); |
107 |
exit(1); |
108 |
} |
109 |
if (i+1 < argc) { |
110 |
zfd = open(argv[i+1], O_RDONLY); |
111 |
if (zfd < 0) { |
112 |
fprintf(stderr, |
113 |
"%s: cannot open depth buffer\n", |
114 |
argv[i+1]); |
115 |
exit(1); |
116 |
} |
117 |
} |
118 |
} |
119 |
if ((err = setview(&vw)) != NULL) { |
120 |
fprintf(stderr, "%s: %s\n", progname, err); |
121 |
exit(1); |
122 |
} |
123 |
if (i == argc) |
124 |
normaspect(viewaspect(&vw), &pa, &rs.xr, &rs.yr); |
125 |
if (getdim) { |
126 |
printf("-x %d -y %d -ld%c\n", rs.xr, rs.yr, |
127 |
vw.vaft > FTINY ? '+' : '-'); |
128 |
exit(0); |
129 |
} |
130 |
if (fromstdin) |
131 |
pix2rays(stdin); |
132 |
else |
133 |
putrays(); |
134 |
exit(0); |
135 |
userr: |
136 |
fprintf(stderr, |
137 |
"Usage: %s [ -i -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n", |
138 |
progname); |
139 |
exit(1); |
140 |
} |
141 |
|
142 |
|
143 |
pix2rays(FILE *fp) |
144 |
{ |
145 |
static FVECT rorg, rdir; |
146 |
float zval; |
147 |
double px, py; |
148 |
int pp[2]; |
149 |
double d; |
150 |
register int i; |
151 |
|
152 |
while (fscanf(fp, "%lf %lf", &px, &py) == 2) { |
153 |
if (px < 0 || px >= rs.xr || |
154 |
py < 0 || py >= rs.yr) { |
155 |
fprintf(stderr, |
156 |
"%s: (x,y) pair (%.0f,%.0f) out of range\n", |
157 |
progname, px, py); |
158 |
exit(1); |
159 |
} |
160 |
if (zfd >= 0) { |
161 |
loc2pix(pp, &rs, px/rs.xr, py/rs.yr); |
162 |
if (lseek(zfd, |
163 |
(pp[1]*scanlen(&rs)+pp[0])*sizeof(float), |
164 |
SEEK_SET) < 0 || |
165 |
read(zfd, &zval, sizeof(float)) |
166 |
< sizeof(float)) { |
167 |
fprintf(stderr, "%s: depth buffer read error\n", |
168 |
progname); |
169 |
exit(1); |
170 |
} |
171 |
} |
172 |
d = viewray(rorg, rdir, &vw, px/rs.xr, py/rs.yr); |
173 |
if (d < -FTINY) |
174 |
rorg[0] = rorg[1] = rorg[2] = |
175 |
rdir[0] = rdir[1] = rdir[2] = 0.; |
176 |
else if (zfd >= 0) |
177 |
for (i = 0; i < 3; i++) { |
178 |
rorg[i] += rdir[i]*zval; |
179 |
rdir[i] = -rdir[i]; |
180 |
} |
181 |
else if (d > FTINY) { |
182 |
rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; |
183 |
} |
184 |
(*putr)(rorg, rdir); |
185 |
} |
186 |
if (!feof(fp)) { |
187 |
fprintf(stderr, "%s: expected px py on input\n", progname); |
188 |
exit(1); |
189 |
} |
190 |
} |
191 |
|
192 |
|
193 |
putrays() |
194 |
{ |
195 |
static RREAL loc[2]; |
196 |
static FVECT rorg, rdir; |
197 |
float *zbuf; |
198 |
int sc; |
199 |
double d; |
200 |
register int si, i; |
201 |
|
202 |
if (zfd >= 0) { |
203 |
zbuf = (float *)malloc(scanlen(&rs)*sizeof(float)); |
204 |
if (zbuf == NULL) { |
205 |
fprintf(stderr, "%s: not enough memory\n", progname); |
206 |
exit(1); |
207 |
} |
208 |
} |
209 |
for (sc = 0; sc < numscans(&rs); sc++) { |
210 |
if (zfd >= 0) { |
211 |
if (read(zfd, zbuf, scanlen(&rs)*sizeof(float)) < |
212 |
scanlen(&rs)*sizeof(float)) { |
213 |
fprintf(stderr, "%s: depth buffer read error\n", |
214 |
progname); |
215 |
exit(1); |
216 |
} |
217 |
} |
218 |
for (si = 0; si < scanlen(&rs); si++) { |
219 |
pix2loc(loc, &rs, si, sc); |
220 |
d = viewray(rorg, rdir, &vw, loc[0], loc[1]); |
221 |
if (d < -FTINY) |
222 |
rorg[0] = rorg[1] = rorg[2] = |
223 |
rdir[0] = rdir[1] = rdir[2] = 0.; |
224 |
else if (zfd >= 0) |
225 |
for (i = 0; i < 3; i++) { |
226 |
rorg[i] += rdir[i]*zbuf[si]; |
227 |
rdir[i] = -rdir[i]; |
228 |
} |
229 |
else if (d > FTINY) { |
230 |
rdir[0] *= d; rdir[1] *= d; rdir[2] *= d; |
231 |
} |
232 |
(*putr)(rorg, rdir); |
233 |
} |
234 |
} |
235 |
if (zfd >= 0) |
236 |
free((void *)zbuf); |
237 |
} |
238 |
|
239 |
|
240 |
puta(ro, rd) /* put out ray in ASCII format */ |
241 |
FVECT ro, rd; |
242 |
{ |
243 |
printf("%.5e %.5e %.5e %.5e %.5e %.5e\n", |
244 |
ro[0], ro[1], ro[2], |
245 |
rd[0], rd[1], rd[2]); |
246 |
} |
247 |
|
248 |
|
249 |
putf(ro, rd) /* put out ray in float format */ |
250 |
FVECT ro, rd; |
251 |
{ |
252 |
float v[6]; |
253 |
|
254 |
v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; |
255 |
v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; |
256 |
fwrite(v, sizeof(float), 6, stdout); |
257 |
} |
258 |
|
259 |
|
260 |
putd(ro, rd) /* put out ray in double format */ |
261 |
FVECT ro, rd; |
262 |
{ |
263 |
double v[6]; |
264 |
|
265 |
v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2]; |
266 |
v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2]; |
267 |
fwrite(v, sizeof(double), 6, stdout); |
268 |
} |