ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.13
Committed: Sun May 7 15:44:28 2006 UTC (18 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R8, rad3R9
Changes since 3.12: +3 -1 lines
Log Message:
Made missing calls to SET_FILE_BINARY() for Windows

File Contents

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