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.7 by schorsch, Sun Jul 27 22:12:04 2003 UTC vs.
Revision 3.23 by greg, Sat Dec 4 16:29:29 2021 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   * Compute rays corresponding to a given picture or view.
6   */
7  
8
9 #include "standard.h"
10
8   #include "platform.h"
9 + #include "standard.h"
10 + #include "random.h"
11   #include "view.h"
12  
13 < extern int      putf(), putd(), puta();
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 < int     (*putr)() = puta;
20 > static putfunc *putr = puta;
21  
22   VIEW    vw = STDVIEW;
23  
# Line 21 | Line 25 | 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 48 | 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 91 | Line 107 | char   *argv[];
107                                  exit(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                          break;
113 +                case 'p':                       /* pixel aspect or jitter */
114 +                        if (argv[i][2] == 'a')
115 +                                pa = atof(argv[++i]);
116 +                        else if (argv[i][2] == 'j')
117 +                                pj = atof(argv[++i]);
118 +                        else if (argv[i][2] == 'd')
119 +                                pd = atof(argv[++i]);
120 +                        else
121 +                                goto userr;
122 +                        break;
123                  case 'i':                       /* get pixels from stdin */
124                          fromstdin = 1;
125                          break;
126 +                case 'u':                       /* unbuffered output */
127 +                        unbuffered = 1;
128 +                        break;
129                  default:
130                          goto userr;
131                  }
# Line 108 | Line 137 | char   *argv[];
137                          fprintf(stderr, "%s: no view in picture\n", argv[i]);
138                          exit(1);
139                  }
140 <                if (i+1 < argc) {
141 <                        zfd = open(argv[i+1], O_RDONLY);
142 <                        if (zfd < 0) {
114 <                                fprintf(stderr,
115 <                                        "%s: cannot open depth buffer\n",
116 <                                                argv[i+1]);
140 >                if (!getdim & (i+1 < argc)) {
141 >                        zfd = open_float_depth(argv[i+1], (long)rs.xr*rs.yr);
142 >                        if (zfd < 0)
143                                  exit(1);
118                        }
144                  }
145          }
146          if ((err = setview(&vw)) != NULL) {
# Line 126 | Line 151 | char   *argv[];
151                  normaspect(viewaspect(&vw), &pa, &rs.xr, &rs.yr);
152          if (getdim) {
153                  printf("-x %d -y %d -ld%c\n", rs.xr, rs.yr,
154 <                                vw.vaft > FTINY ? '+' : '-');
154 >                                (i+1 == argc) & (vw.vaft > FTINY) ? '+' : '-');
155                  exit(0);
156          }
157          if (fromstdin)
# Line 136 | Line 161 | char   *argv[];
161          exit(0);
162   userr:
163          fprintf(stderr,
164 <        "Usage: %s [ -i -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n",
164 >        "Usage: %s [ -i -u -f{a|f|d} -c rept | -d ] { view opts .. | picture [zbuf] }\n",
165                          progname);
166          exit(1);
167   }
168  
169  
170 < pix2rays(FILE *fp)
170 > static void
171 > jitterloc(
172 >        RREAL   loc[2]
173 > )
174   {
175 +        if (pj > FTINY) {
176 +                loc[0] += pj*(.5 - frandom())/rs.xr;
177 +                loc[1] += pj*(.5 - frandom())/rs.yr;
178 +        }
179 + }
180 +
181 +
182 + static void
183 + pix2rays(
184 +        FILE *fp
185 + )
186 + {
187          static FVECT    rorg, rdir;
188          float   zval;
189          double  px, py;
190 +        RREAL   loc[2];
191          int     pp[2];
192          double  d;
193 <        register int    i;
193 >        int     i, c;
194  
195          while (fscanf(fp, "%lf %lf", &px, &py) == 2) {
196 <                if (px < 0 || px >= rs.xr ||
197 <                                py < 0 || py >= rs.yr) {
157 <                        fprintf(stderr,
158 <                                "%s: (x,y) pair (%.0f,%.0f) out of range\n",
159 <                                        progname, px, py);
160 <                        exit(1);
161 <                }
196 >                px += .5; py += .5;
197 >                loc[0] = px/rs.xr; loc[1] = py/rs.yr;
198                  if (zfd >= 0) {
199 <                        loc2pix(pp, &rs, px/rs.xr, py/rs.yr);
199 >                        if ((loc[0] < 0) | (loc[0] >= 1) |
200 >                                        (loc[1] < 0) | (loc[1] >= 1)) {
201 >                                fprintf(stderr, "%s: input pixel outside image\n",
202 >                                                progname);
203 >                                exit(1);
204 >                        }
205 >                        loc2pix(pp, &rs, loc[0], loc[1]);
206                          if (lseek(zfd,
207 <                                (pp[1]*scanlen(&rs)+pp[0])*sizeof(float), 0)
208 <                                        < 0 ||
207 >                                (pp[1]*scanlen(&rs)+pp[0])*sizeof(float),
208 >                                                SEEK_SET) < 0 ||
209                                          read(zfd, &zval, sizeof(float))
210 <                                        < sizeof(float)) {
210 >                                                < sizeof(float)) {
211                                  fprintf(stderr, "%s: depth buffer read error\n",
212                                                  progname);
213                                  exit(1);
214                          }
215                  }
216 <                d = viewray(rorg, rdir, &vw, px/rs.xr, py/rs.yr);
217 <                if (d < -FTINY)
218 <                        rorg[0] = rorg[1] = rorg[2] =
219 <                        rdir[0] = rdir[1] = rdir[2] = 0.;
220 <                else if (zfd >= 0)
221 <                        for (i = 0; i < 3; i++) {
222 <                                rorg[i] += rdir[i]*zval;
223 <                                rdir[i] = -rdir[i];
216 >                for (c = repeatcnt; c-- > 0; ) {
217 >                        jitterloc(loc);
218 >                        d = viewray(rorg, rdir, &vw, loc[0], loc[1]);
219 >                        if (d < -FTINY || !jitteraperture(rorg, rdir, &vw, pd))
220 >                                rorg[0] = rorg[1] = rorg[2] =
221 >                                rdir[0] = rdir[1] = rdir[2] = 0.;
222 >                        else if (zfd >= 0)
223 >                                for (i = 0; i < 3; i++) {
224 >                                        rorg[i] += rdir[i]*zval;
225 >                                        rdir[i] = -rdir[i];
226 >                                }
227 >                        else if (d > FTINY) {
228 >                                rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
229                          }
230 <                else if (d > FTINY) {
231 <                        rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
230 >                        (*putr)(rorg, rdir);
231 >                        if (c) {
232 >                                loc[0] = px/rs.xr; loc[1] = py/rs.yr;
233 >                        }
234                  }
235 <                (*putr)(rorg, rdir);
235 >                if (unbuffered)
236 >                        fflush(stdout);
237          }
238          if (!feof(fp)) {
239                  fprintf(stderr, "%s: expected px py on input\n", progname);
# Line 192 | Line 242 | pix2rays(FILE *fp)
242   }
243  
244  
245 < putrays()
245 > static void
246 > putrays(void)
247   {
248 <        static RREAL    loc[2];
249 <        static FVECT    rorg, rdir;
250 <        float   *zbuf;
248 >        RREAL   loc[2];
249 >        FVECT   rorg, rdir;
250 >        float   *zbuf = NULL;
251          int     sc;
252          double  d;
253 <        register int    si, i;
253 >        int     si, i, c;
254  
255          if (zfd >= 0) {
256                  zbuf = (float *)malloc(scanlen(&rs)*sizeof(float));
# Line 218 | Line 269 | putrays()
269                          }
270                  }
271                  for (si = 0; si < scanlen(&rs); si++) {
272 +                    for (c = repeatcnt; c-- > 0; ) {
273                          pix2loc(loc, &rs, si, sc);
274 +                        jitterloc(loc);
275                          d = viewray(rorg, rdir, &vw, loc[0], loc[1]);
276 <                        if (d < -FTINY)
276 >                        if (d < -FTINY || !jitteraperture(rorg, rdir, &vw, pd))
277                                  rorg[0] = rorg[1] = rorg[2] =
278                                  rdir[0] = rdir[1] = rdir[2] = 0.;
279                          else if (zfd >= 0)
280                                  for (i = 0; i < 3; i++) {
281 <                                        rorg[i] += rdir[i]*zbuf[si];
282 <                                        rdir[i] = -rdir[i];
281 >                                        rdir[i] = -rdir[i]*zbuf[si];
282 >                                        rorg[i] -= rdir[i];
283                                  }
284                          else if (d > FTINY) {
285                                  rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
286                          }
287                          (*putr)(rorg, rdir);
288 +                    }
289                  }
290          }
291          if (zfd >= 0)
# Line 239 | Line 293 | putrays()
293   }
294  
295  
296 < puta(ro, rd)            /* put out ray in ASCII format */
297 < FVECT   ro, rd;
296 > static void
297 > puta(           /* put out ray in ASCII format */
298 >        FVECT   ro,
299 >        FVECT   rd
300 > )
301   {
302          printf("%.5e %.5e %.5e %.5e %.5e %.5e\n",
303                          ro[0], ro[1], ro[2],
# Line 248 | Line 305 | FVECT  ro, rd;
305   }
306  
307  
308 < putf(ro, rd)            /* put out ray in float format */
309 < FVECT   ro, rd;
308 > static void
309 > putf(           /* put out ray in float format */
310 >        FVECT   ro,
311 >        FVECT   rd
312 > )
313   {
314          float v[6];
315  
316          v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2];
317          v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2];
318 <        fwrite(v, sizeof(float), 6, stdout);
318 >        putbinary(v, sizeof(float), 6, stdout);
319   }
320  
321  
322 < putd(ro, rd)            /* put out ray in double format */
323 < FVECT   ro, rd;
322 > static void
323 > putd(           /* put out ray in double format */
324 >        FVECT   ro,
325 >        FVECT   rd
326 > )
327   {
328          double v[6];
329  
330          v[0] = ro[0]; v[1] = ro[1]; v[2] = ro[2];
331          v[3] = rd[0]; v[4] = rd[1]; v[5] = rd[2];
332 <        fwrite(v, sizeof(double), 6, stdout);
332 >        putbinary(v, sizeof(double), 6, stdout);
333   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines