ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.26
Committed: Tue Jun 3 21:31:51 2025 UTC (3 days, 9 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 3.25: +2 -4 lines
Log Message:
refactor: More consistent use of global char * progname and fixargv0()

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: vwrays.c,v 3.25 2024/01/12 17:29:10 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 double pd = 0.;
31
32 int zfd = -1;
33
34 int fromstdin = 0;
35
36 int unbuffered = 0;
37
38 int repeatcnt = 1;
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 fixargv0(argv[0]); /* sets global progname */
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 return(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 return(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 return(1);
106 }
107 break;
108 case 'c': /* repeat count */
109 repeatcnt = atoi(argv[++i]);
110 if (repeatcnt < 1) repeatcnt = 1;
111 break;
112 case 'p': /* pixel aspect or jitter */
113 if (argv[i][2] == 'a')
114 pa = atof(argv[++i]);
115 else if (argv[i][2] == 'j')
116 pj = atof(argv[++i]);
117 else if (argv[i][2] == 'd')
118 pd = atof(argv[++i]);
119 else
120 goto userr;
121 break;
122 case 'i': /* get pixels from stdin */
123 fromstdin = 1;
124 break;
125 case 'u': /* unbuffered output */
126 unbuffered = 1;
127 break;
128 default:
129 goto userr;
130 }
131 if ((i > argc) | (i+2 < argc))
132 goto userr;
133 if (i < argc) {
134 rval = viewfile(argv[i], &vw, &rs);
135 if (rval <= 0) {
136 fprintf(stderr, "%s: no view in picture\n", argv[i]);
137 return(1);
138 }
139 if (!getdim & (i+1 < argc)) {
140 zfd = open_float_depth(argv[i+1], (long)rs.xr*rs.yr);
141 if (zfd < 0)
142 return(1);
143 }
144 }
145 if ((err = setview(&vw)) != NULL) {
146 fprintf(stderr, "%s: %s\n", progname, err);
147 return(1);
148 }
149 if (i == argc)
150 normaspect(viewaspect(&vw), &pa, &rs.xr, &rs.yr);
151 if (getdim) {
152 printf("-x %d -y %d%s\n", rs.xr, rs.yr,
153 (vw.vaft > FTINY) ? " -ld+" : "");
154 return(0);
155 }
156 if ((repeatcnt > 1) & (pj > FTINY))
157 initurand(1024);
158 if (fromstdin)
159 pix2rays(stdin);
160 else
161 putrays();
162 return(0);
163 userr:
164 fprintf(stderr,
165 "Usage: %s [ -i -u -f{a|f|d} -c rept | -d ] { view opts .. | picture [zbuf] }\n",
166 progname);
167 return(1);
168 }
169
170
171 static void
172 jitterloc(
173 RREAL loc[2]
174 )
175 {
176 static int nsamp;
177 double xyr[2];
178
179 if (pj <= FTINY)
180 return;
181
182 if (repeatcnt == 1) {
183 xyr[0] = frandom();
184 xyr[1] = frandom();
185 } else /* stratify samples */
186 multisamp(xyr, 2, urand(nsamp++));
187
188 loc[0] += pj*(.5 - xyr[0])/rs.xr;
189 loc[1] += pj*(.5 - xyr[1])/rs.yr;
190 }
191
192
193 static void
194 pix2rays(
195 FILE *fp
196 )
197 {
198 static FVECT rorg, rdir;
199 float zval;
200 double px, py;
201 RREAL loc[2];
202 int pp[2];
203 double d;
204 int i, c;
205
206 while (fscanf(fp, "%lf %lf", &px, &py) == 2) {
207 px += .5; py += .5;
208 loc[0] = px/rs.xr; loc[1] = py/rs.yr;
209 if (zfd >= 0) {
210 if ((loc[0] < 0) | (loc[0] >= 1) |
211 (loc[1] < 0) | (loc[1] >= 1)) {
212 fprintf(stderr, "%s: input pixel outside image\n",
213 progname);
214 exit(1);
215 }
216 loc2pix(pp, &rs, loc[0], loc[1]);
217 if (lseek(zfd,
218 (pp[1]*scanlen(&rs)+pp[0])*sizeof(float),
219 SEEK_SET) < 0 ||
220 read(zfd, &zval, sizeof(float))
221 < sizeof(float)) {
222 fprintf(stderr, "%s: depth buffer read error\n",
223 progname);
224 exit(1);
225 }
226 }
227 for (c = repeatcnt; c-- > 0; ) {
228 jitterloc(loc);
229 d = viewray(rorg, rdir, &vw, loc[0], loc[1]);
230 if (d < -FTINY || !jitteraperture(rorg, rdir, &vw, pd))
231 rorg[0] = rorg[1] = rorg[2] =
232 rdir[0] = rdir[1] = rdir[2] = 0.;
233 else if (zfd >= 0)
234 for (i = 0; i < 3; i++) {
235 rorg[i] += rdir[i]*zval;
236 rdir[i] = -rdir[i];
237 }
238 else if (d > FTINY) {
239 rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
240 }
241 (*putr)(rorg, rdir);
242 if (c) {
243 loc[0] = px/rs.xr; loc[1] = py/rs.yr;
244 }
245 }
246 if (unbuffered)
247 fflush(stdout);
248 }
249 if (!feof(fp)) {
250 fprintf(stderr, "%s: expected px py on input\n", progname);
251 exit(1);
252 }
253 }
254
255
256 static void
257 putrays(void)
258 {
259 RREAL loc[2];
260 FVECT rorg, rdir;
261 float *zbuf = NULL;
262 int sc;
263 double d;
264 int si, i, c;
265
266 if (zfd >= 0) {
267 zbuf = (float *)malloc(scanlen(&rs)*sizeof(float));
268 if (zbuf == NULL) {
269 fprintf(stderr, "%s: not enough memory\n", progname);
270 exit(1);
271 }
272 }
273 for (sc = 0; sc < numscans(&rs); sc++) {
274 if (zfd >= 0) {
275 if (read(zfd, zbuf, scanlen(&rs)*sizeof(float)) <
276 scanlen(&rs)*sizeof(float)) {
277 fprintf(stderr, "%s: depth buffer read error\n",
278 progname);
279 exit(1);
280 }
281 }
282 for (si = 0; si < scanlen(&rs); si++) {
283 for (c = repeatcnt; c-- > 0; ) {
284 pix2loc(loc, &rs, si, sc);
285 jitterloc(loc);
286 d = viewray(rorg, rdir, &vw, loc[0], loc[1]);
287 if (d < -FTINY || !jitteraperture(rorg, rdir, &vw, pd))
288 rorg[0] = rorg[1] = rorg[2] =
289 rdir[0] = rdir[1] = rdir[2] = 0.;
290 else if (zfd >= 0)
291 for (i = 0; i < 3; i++) {
292 rdir[i] = -rdir[i]*zbuf[si];
293 rorg[i] -= rdir[i];
294 }
295 else if (d > FTINY) {
296 rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
297 }
298 (*putr)(rorg, rdir);
299 }
300 }
301 }
302 if (zfd >= 0)
303 free((void *)zbuf);
304 }
305
306
307 static void
308 puta( /* put out ray in ASCII format */
309 FVECT ro,
310 FVECT rd
311 )
312 {
313 printf("%.5e %.5e %.5e %.5e %.5e %.5e\n",
314 ro[0], ro[1], ro[2],
315 rd[0], rd[1], rd[2]);
316 }
317
318
319 static void
320 putf( /* put out ray in float format */
321 FVECT ro,
322 FVECT rd
323 )
324 {
325 float v[6];
326
327 v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2];
328 v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2];
329 putbinary(v, sizeof(float), 6, stdout);
330 }
331
332
333 static void
334 putd( /* put out ray in double format */
335 FVECT ro,
336 FVECT rd
337 )
338 {
339 double v[6];
340
341 v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2];
342 v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2];
343 putbinary(v, sizeof(double), 6, stdout);
344 }