ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
Revision: 3.25
Committed: Fri Jan 12 17:29:10 2024 UTC (3 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 3.24: +18 -5 lines
Log Message:
perf(vwrays): Stratified samples when -pj > 0 and -c > 1

File Contents

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