ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.22
Committed: Fri Dec 3 17:22:28 2021 UTC (2 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.21: +19 -14 lines
Log Message:
feat(vwrays): added support for -c combined with -i option

File Contents

# User Rev Content
1 gregl 3.1 #ifndef lint
2 greg 3.22 static const char RCSid[] = "$Id: vwrays.c,v 3.21 2020/06/12 22:45:20 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 greg 3.16 int unbuffered = 0;
35    
36 greg 3.17 int repeatcnt = 1;
37    
38 gregl 3.1 char *progname;
39    
40    
41 schorsch 3.10 int
42     main(
43     int argc,
44     char *argv[]
45     )
46 gregl 3.1 {
47     char *err;
48     int rval, getdim = 0;
49 greg 3.17 int i;
50 gregl 3.1
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 greg 3.13 SET_FILE_BINARY(stdout);
64 gregl 3.1 break;
65     case 'd': /* double */
66     putr = putd;
67 greg 3.13 SET_FILE_BINARY(stdout);
68 gregl 3.1 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 greg 3.17 case 'c': /* repeat count */
109     repeatcnt = atoi(argv[++i]);
110     break;
111 greg 3.12 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 greg 3.15 pj = atof(argv[++i]);
116 greg 3.12 else
117     goto userr;
118 gregl 3.1 break;
119 greg 3.4 case 'i': /* get pixels from stdin */
120     fromstdin = 1;
121     break;
122 greg 3.16 case 'u': /* unbuffered output */
123     unbuffered = 1;
124     break;
125 gregl 3.1 default:
126     goto userr;
127     }
128 schorsch 3.7 if ((i > argc) | (i+2 < argc))
129 gregl 3.1 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 greg 3.21 if (!getdim & (i+1 < argc)) {
137 greg 3.20 zfd = open_float_depth(argv[i+1], (long)rs.xr*rs.yr);
138     if (zfd < 0)
139 gregl 3.1 exit(1);
140     }
141     }
142     if ((err = setview(&vw)) != NULL) {
143     fprintf(stderr, "%s: %s\n", progname, err);
144     exit(1);
145     }
146     if (i == argc)
147     normaspect(viewaspect(&vw), &pa, &rs.xr, &rs.yr);
148     if (getdim) {
149 gregl 3.3 printf("-x %d -y %d -ld%c\n", rs.xr, rs.yr,
150 greg 3.21 (i+1 == argc) & (vw.vaft > FTINY) ? '+' : '-');
151 gregl 3.1 exit(0);
152     }
153 greg 3.4 if (fromstdin)
154     pix2rays(stdin);
155     else
156     putrays();
157 gregl 3.1 exit(0);
158     userr:
159     fprintf(stderr,
160 greg 3.17 "Usage: %s [ -i -u -f{a|f|d} -c rept | -d ] { view opts .. | picture [zbuf] }\n",
161 gregl 3.1 progname);
162     exit(1);
163     }
164    
165    
166 schorsch 3.10 static void
167 greg 3.12 jitterloc(
168     RREAL loc[2]
169     )
170     {
171     if (pj > FTINY) {
172     loc[0] += pj*(.5 - frandom())/rs.xr;
173     loc[1] += pj*(.5 - frandom())/rs.yr;
174     }
175     }
176    
177    
178     static void
179 schorsch 3.10 pix2rays(
180     FILE *fp
181     )
182 greg 3.4 {
183     static FVECT rorg, rdir;
184     float zval;
185     double px, py;
186 greg 3.12 RREAL loc[2];
187 greg 3.4 int pp[2];
188     double d;
189 greg 3.22 int i, c;
190 greg 3.4
191     while (fscanf(fp, "%lf %lf", &px, &py) == 2) {
192 greg 3.11 px += .5; py += .5;
193 greg 3.12 loc[0] = px/rs.xr; loc[1] = py/rs.yr;
194 greg 3.4 if (zfd >= 0) {
195 greg 3.19 if ((loc[0] < 0) | (loc[0] >= 1) |
196     (loc[1] < 0) | (loc[1] >= 1)) {
197     fprintf(stderr, "%s: input pixel outside image\n",
198     progname);
199     exit(1);
200     }
201 greg 3.12 loc2pix(pp, &rs, loc[0], loc[1]);
202 greg 3.4 if (lseek(zfd,
203 greg 3.8 (pp[1]*scanlen(&rs)+pp[0])*sizeof(float),
204     SEEK_SET) < 0 ||
205 greg 3.4 read(zfd, &zval, sizeof(float))
206 greg 3.8 < sizeof(float)) {
207 greg 3.4 fprintf(stderr, "%s: depth buffer read error\n",
208     progname);
209     exit(1);
210     }
211     }
212 greg 3.22 for (c = repeatcnt; c-- > 0; ) {
213     jitterloc(loc);
214     d = viewray(rorg, rdir, &vw, loc[0], loc[1]);
215     if (d < -FTINY)
216     rorg[0] = rorg[1] = rorg[2] =
217     rdir[0] = rdir[1] = rdir[2] = 0.;
218     else if (zfd >= 0)
219     for (i = 0; i < 3; i++) {
220     rorg[i] += rdir[i]*zval;
221     rdir[i] = -rdir[i];
222     }
223     else if (d > FTINY) {
224     rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
225     }
226     (*putr)(rorg, rdir);
227     if (c) {
228     loc[0] = px/rs.xr; loc[1] = py/rs.yr;
229 greg 3.4 }
230     }
231 greg 3.16 if (unbuffered)
232     fflush(stdout);
233 greg 3.4 }
234     if (!feof(fp)) {
235     fprintf(stderr, "%s: expected px py on input\n", progname);
236     exit(1);
237     }
238     }
239    
240    
241 schorsch 3.10 static void
242     putrays(void)
243 gregl 3.1 {
244 greg 3.12 RREAL loc[2];
245     FVECT rorg, rdir;
246 schorsch 3.10 float *zbuf = NULL;
247 gregl 3.1 int sc;
248 gregl 3.2 double d;
249 greg 3.17 int si, i, c;
250 gregl 3.1
251     if (zfd >= 0) {
252     zbuf = (float *)malloc(scanlen(&rs)*sizeof(float));
253     if (zbuf == NULL) {
254     fprintf(stderr, "%s: not enough memory\n", progname);
255     exit(1);
256     }
257     }
258     for (sc = 0; sc < numscans(&rs); sc++) {
259     if (zfd >= 0) {
260     if (read(zfd, zbuf, scanlen(&rs)*sizeof(float)) <
261     scanlen(&rs)*sizeof(float)) {
262     fprintf(stderr, "%s: depth buffer read error\n",
263     progname);
264     exit(1);
265     }
266     }
267     for (si = 0; si < scanlen(&rs); si++) {
268 greg 3.17 for (c = repeatcnt; c-- > 0; ) {
269 gregl 3.1 pix2loc(loc, &rs, si, sc);
270 greg 3.12 jitterloc(loc);
271 gregl 3.2 d = viewray(rorg, rdir, &vw, loc[0], loc[1]);
272     if (d < -FTINY)
273 gregl 3.1 rorg[0] = rorg[1] = rorg[2] =
274     rdir[0] = rdir[1] = rdir[2] = 0.;
275     else if (zfd >= 0)
276     for (i = 0; i < 3; i++) {
277 greg 3.21 rdir[i] = -rdir[i]*zbuf[si];
278     rorg[i] -= rdir[i];
279 gregl 3.1 }
280 gregl 3.2 else if (d > FTINY) {
281     rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
282     }
283 gregl 3.1 (*putr)(rorg, rdir);
284 greg 3.17 }
285 gregl 3.1 }
286     }
287     if (zfd >= 0)
288 greg 3.4 free((void *)zbuf);
289 gregl 3.1 }
290    
291    
292 schorsch 3.10 static void
293     puta( /* put out ray in ASCII format */
294     FVECT ro,
295     FVECT rd
296     )
297 gregl 3.1 {
298     printf("%.5e %.5e %.5e %.5e %.5e %.5e\n",
299     ro[0], ro[1], ro[2],
300     rd[0], rd[1], rd[2]);
301     }
302    
303    
304 schorsch 3.10 static void
305     putf( /* put out ray in float format */
306     FVECT ro,
307     FVECT rd
308     )
309 gregl 3.1 {
310     float 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 greg 3.18 putbinary(v, sizeof(float), 6, stdout);
315 gregl 3.1 }
316    
317    
318 schorsch 3.10 static void
319     putd( /* put out ray in double format */
320     FVECT ro,
321     FVECT rd
322     )
323 gregl 3.1 {
324     double v[6];
325    
326     v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2];
327     v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2];
328 greg 3.18 putbinary(v, sizeof(double), 6, stdout);
329 gregl 3.1 }