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