ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.12
Committed: Mon Sep 19 04:26:09 2005 UTC (18 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.11: +31 -7 lines
Log Message:
Added -pj option to vwrays for ray jittering

File Contents

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