ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.11
Committed: Thu Jan 20 23:52:02 2005 UTC (19 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1
Changes since 3.10: +2 -1 lines
Log Message:
Corrected center of pixels when given on input

File Contents

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