ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.17
Committed: Thu Jun 14 05:19:05 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2, rad4R2P1
Changes since 3.16: +12 -5 lines
Log Message:
Added -c option to repeat pixels

File Contents

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