ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.24
Committed: Sat Apr 9 17:18:08 2022 UTC (23 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4
Changes since 3.23: +12 -12 lines
Log Message:
fix(vwrays): No longer spits out -ld- with -d option, only -ld+

File Contents

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