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, 2 weeks 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

# User Rev Content
1 gregl 3.1 #ifndef lint
2 greg 3.25 static const char RCSid[] = "$Id: vwrays.c,v 3.24 2022/04/09 17:18:08 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 greg 3.25 if (repeatcnt < 1) repeatcnt = 1;
113 greg 3.17 break;
114 greg 3.12 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 greg 3.15 pj = atof(argv[++i]);
119 greg 3.23 else if (argv[i][2] == 'd')
120     pd = atof(argv[++i]);
121 greg 3.12 else
122     goto userr;
123 gregl 3.1 break;
124 greg 3.4 case 'i': /* get pixels from stdin */
125     fromstdin = 1;
126     break;
127 greg 3.16 case 'u': /* unbuffered output */
128     unbuffered = 1;
129     break;
130 gregl 3.1 default:
131     goto userr;
132     }
133 schorsch 3.7 if ((i > argc) | (i+2 < argc))
134 gregl 3.1 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 greg 3.24 return(1);
140 gregl 3.1 }
141 greg 3.21 if (!getdim & (i+1 < argc)) {
142 greg 3.20 zfd = open_float_depth(argv[i+1], (long)rs.xr*rs.yr);
143     if (zfd < 0)
144 greg 3.24 return(1);
145 gregl 3.1 }
146     }
147     if ((err = setview(&vw)) != NULL) {
148     fprintf(stderr, "%s: %s\n", progname, err);
149 greg 3.24 return(1);
150 gregl 3.1 }
151     if (i == argc)
152     normaspect(viewaspect(&vw), &pa, &rs.xr, &rs.yr);
153     if (getdim) {
154 greg 3.24 printf("-x %d -y %d%s\n", rs.xr, rs.yr,
155     (vw.vaft > FTINY) ? " -ld+" : "");
156     return(0);
157 gregl 3.1 }
158 greg 3.25 if ((repeatcnt > 1) & (pj > FTINY))
159     initurand(1024);
160 greg 3.4 if (fromstdin)
161     pix2rays(stdin);
162     else
163     putrays();
164 greg 3.24 return(0);
165 gregl 3.1 userr:
166     fprintf(stderr,
167 greg 3.17 "Usage: %s [ -i -u -f{a|f|d} -c rept | -d ] { view opts .. | picture [zbuf] }\n",
168 gregl 3.1 progname);
169 greg 3.24 return(1);
170 gregl 3.1 }
171    
172    
173 schorsch 3.10 static void
174 greg 3.12 jitterloc(
175     RREAL loc[2]
176     )
177     {
178 greg 3.25 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 greg 3.12 }
193    
194    
195     static void
196 schorsch 3.10 pix2rays(
197     FILE *fp
198     )
199 greg 3.4 {
200     static FVECT rorg, rdir;
201     float zval;
202     double px, py;
203 greg 3.12 RREAL loc[2];
204 greg 3.4 int pp[2];
205     double d;
206 greg 3.22 int i, c;
207 greg 3.4
208     while (fscanf(fp, "%lf %lf", &px, &py) == 2) {
209 greg 3.11 px += .5; py += .5;
210 greg 3.12 loc[0] = px/rs.xr; loc[1] = py/rs.yr;
211 greg 3.4 if (zfd >= 0) {
212 greg 3.19 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 greg 3.12 loc2pix(pp, &rs, loc[0], loc[1]);
219 greg 3.4 if (lseek(zfd,
220 greg 3.8 (pp[1]*scanlen(&rs)+pp[0])*sizeof(float),
221     SEEK_SET) < 0 ||
222 greg 3.4 read(zfd, &zval, sizeof(float))
223 greg 3.8 < sizeof(float)) {
224 greg 3.4 fprintf(stderr, "%s: depth buffer read error\n",
225     progname);
226     exit(1);
227     }
228     }
229 greg 3.22 for (c = repeatcnt; c-- > 0; ) {
230     jitterloc(loc);
231     d = viewray(rorg, rdir, &vw, loc[0], loc[1]);
232 greg 3.23 if (d < -FTINY || !jitteraperture(rorg, rdir, &vw, pd))
233 greg 3.22 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 greg 3.4 }
247     }
248 greg 3.16 if (unbuffered)
249     fflush(stdout);
250 greg 3.4 }
251     if (!feof(fp)) {
252     fprintf(stderr, "%s: expected px py on input\n", progname);
253     exit(1);
254     }
255     }
256    
257    
258 schorsch 3.10 static void
259     putrays(void)
260 gregl 3.1 {
261 greg 3.12 RREAL loc[2];
262     FVECT rorg, rdir;
263 schorsch 3.10 float *zbuf = NULL;
264 gregl 3.1 int sc;
265 gregl 3.2 double d;
266 greg 3.17 int si, i, c;
267 gregl 3.1
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 greg 3.17 for (c = repeatcnt; c-- > 0; ) {
286 gregl 3.1 pix2loc(loc, &rs, si, sc);
287 greg 3.12 jitterloc(loc);
288 gregl 3.2 d = viewray(rorg, rdir, &vw, loc[0], loc[1]);
289 greg 3.23 if (d < -FTINY || !jitteraperture(rorg, rdir, &vw, pd))
290 gregl 3.1 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 greg 3.21 rdir[i] = -rdir[i]*zbuf[si];
295     rorg[i] -= rdir[i];
296 gregl 3.1 }
297 gregl 3.2 else if (d > FTINY) {
298     rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
299     }
300 gregl 3.1 (*putr)(rorg, rdir);
301 greg 3.17 }
302 gregl 3.1 }
303     }
304     if (zfd >= 0)
305 greg 3.4 free((void *)zbuf);
306 gregl 3.1 }
307    
308    
309 schorsch 3.10 static void
310     puta( /* put out ray in ASCII format */
311     FVECT ro,
312     FVECT rd
313     )
314 gregl 3.1 {
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 schorsch 3.10 static void
322     putf( /* put out ray in float format */
323     FVECT ro,
324     FVECT rd
325     )
326 gregl 3.1 {
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 greg 3.18 putbinary(v, sizeof(float), 6, stdout);
332 gregl 3.1 }
333    
334    
335 schorsch 3.10 static void
336     putd( /* put out ray in double format */
337     FVECT ro,
338     FVECT rd
339     )
340 gregl 3.1 {
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 greg 3.18 putbinary(v, sizeof(double), 6, stdout);
346 gregl 3.1 }