ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/vwrays.c
(Generate patch)

Comparing ray/src/util/vwrays.c (file contents):
Revision 3.3 by gregl, Fri Oct 17 10:42:45 1997 UTC vs.
Revision 3.25 by greg, Fri Jan 12 17:29:10 2024 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Compute rays corresponding to a given picture or view.
6   */
7  
8 <
8 > #include "platform.h"
9   #include "standard.h"
10 <
10 > #include "random.h"
11   #include "view.h"
12  
13 < #include "resolu.h"
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 < extern int      putf(), putd(), puta();
20 > static putfunc *putr = puta;
21  
20 int     (*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 < main(argc, argv)
44 < int     argc;
45 < char    *argv[];
43 > int
44 > main(
45 >        int     argc,
46 >        char    *argv[]
47 > )
48   {
49          char    *err;
50          int     rval, getdim = 0;
51 <        register int    i;
51 >        int     i;
52  
53          progname = argv[0];
54          if (argc < 2)
# Line 50 | Line 62 | char   *argv[];
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;
# Line 65 | Line 79 | char   *argv[];
79                                          fprintf(stderr,
80                                                  "%s: no view in file\n",
81                                                          argv[i]);
82 <                                        exit(1);
82 >                                        return(1);
83                                  }
84                                  break;
85                          }
# Line 82 | Line 96 | char   *argv[];
96                          if (rs.xr <= 0) {
97                                  fprintf(stderr, "%s: bad x resolution\n",
98                                                  progname);
99 <                                exit(1);
99 >                                return(1);
100                          }
101                          break;
102                  case 'y':                       /* y resolution */
# Line 90 | Line 104 | char   *argv[];
104                          if (rs.yr <= 0) {
105                                  fprintf(stderr, "%s: bad y resolution\n",
106                                                  progname);
107 <                                exit(1);
107 >                                return(1);
108                          }
109                          break;
110 <                case 'p':                       /* pixel aspect ratio */
111 <                        pa = atof(argv[++i]);
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)
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 <                        exit(1);
139 >                        return(1);
140                  }
141 <                if (i+1 < argc) {
142 <                        zfd = open(argv[i+1], O_RDONLY);
143 <                        if (zfd < 0) {
144 <                                fprintf(stderr,
114 <                                        "%s: cannot open depth buffer\n",
115 <                                                argv[i+1]);
116 <                                exit(1);
117 <                        }
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 <                exit(1);
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 -ld%c\n", rs.xr, rs.yr,
155 <                                vw.vaft > FTINY ? '+' : '-');
156 <                exit(0);
154 >                printf("-x %d -y %d%s\n", rs.xr, rs.yr,
155 >                                (vw.vaft > FTINY) ? " -ld+" : "");
156 >                return(0);
157          }
158 <        putrays();
159 <        exit(0);
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 [ -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n",
167 >        "Usage: %s [ -i -u -f{a|f|d} -c rept | -d ] { view opts .. | picture [zbuf] }\n",
168                          progname);
169 <        exit(1);
169 >        return(1);
170   }
171  
172  
173 < putrays()
173 > static void
174 > jitterloc(
175 >        RREAL   loc[2]
176 > )
177   {
178 <        static FLOAT    loc[2];
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   *zbuf;
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 <        register int    si, i;
266 >        int     si, i, c;
267  
268          if (zfd >= 0) {
269                  zbuf = (float *)malloc(scanlen(&rs)*sizeof(float));
# Line 164 | Line 282 | putrays()
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)
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 <                                        rorg[i] += rdir[i]*zbuf[si];
295 <                                        rdir[i] = -rdir[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((char *)zbuf);
305 >                free((void *)zbuf);
306   }
307  
308  
309 < puta(ro, rd)            /* put out ray in ASCII format */
310 < FVECT   ro, rd;
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],
# Line 194 | Line 318 | FVECT  ro, rd;
318   }
319  
320  
321 < putf(ro, rd)            /* put out ray in float format */
322 < FVECT   ro, rd;
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 <        fwrite(v, sizeof(float), 6, stdout);
331 >        putbinary(v, sizeof(float), 6, stdout);
332   }
333  
334  
335 < putd(ro, rd)            /* put out ray in double format */
336 < FVECT   ro, rd;
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 <        fwrite(v, sizeof(double), 6, stdout);
345 >        putbinary(v, sizeof(double), 6, stdout);
346   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines