ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.7
Committed: Sun Jul 27 22:12:04 2003 UTC (21 years, 4 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.6: +2 -2 lines
Log Message:
Added grouping parens to reduce ambiguity warnings.

File Contents

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