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.5 by schorsch, Thu Jun 26 00:58:11 2003 UTC vs.
Revision 3.26 by greg, Tue Jun 3 21:31:51 2025 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
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 < 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 20 | 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 < char    *progname;
36 > int     unbuffered = 0;
37  
38 + int     repeatcnt = 1;
39  
40 < main(argc, argv)
41 < int     argc;
42 < char    *argv[];
40 >
41 > int
42 > main(
43 >        int     argc,
44 >        char    *argv[]
45 > )
46   {
47          char    *err;
48          int     rval, getdim = 0;
49 <        register int    i;
49 >        int     i;
50  
51 <        progname = argv[0];
51 >        fixargv0(argv[0]);              /* sets global progname */
52          if (argc < 2)
53                  goto userr;
54          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 47 | Line 60 | char   *argv[];
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;
# Line 62 | Line 77 | char   *argv[];
77                                          fprintf(stderr,
78                                                  "%s: no view in file\n",
79                                                          argv[i]);
80 <                                        exit(1);
80 >                                        return(1);
81                                  }
82                                  break;
83                          }
# Line 79 | Line 94 | char   *argv[];
94                          if (rs.xr <= 0) {
95                                  fprintf(stderr, "%s: bad x resolution\n",
96                                                  progname);
97 <                                exit(1);
97 >                                return(1);
98                          }
99                          break;
100                  case 'y':                       /* y resolution */
# Line 87 | Line 102 | char   *argv[];
102                          if (rs.yr <= 0) {
103                                  fprintf(stderr, "%s: bad y resolution\n",
104                                                  progname);
105 <                                exit(1);
105 >                                return(1);
106                          }
107                          break;
108 <                case 'p':                       /* pixel aspect ratio */
109 <                        pa = atof(argv[++i]);
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)
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 <                        exit(1);
137 >                        return(1);
138                  }
139 <                if (i+1 < argc) {
140 <                        zfd = open(argv[i+1], O_RDONLY);
141 <                        if (zfd < 0) {
142 <                                fprintf(stderr,
114 <                                        "%s: cannot open depth buffer\n",
115 <                                                argv[i+1]);
116 <                                exit(1);
117 <                        }
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 <                exit(1);
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 -ld%c\n", rs.xr, rs.yr,
153 <                                vw.vaft > FTINY ? '+' : '-');
154 <                exit(0);
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 <        exit(0);
162 >        return(0);
163   userr:
164          fprintf(stderr,
165 <        "Usage: %s [ -i -f{a|f|d} | -d ] { view opts .. | picture [zbuf] }\n",
165 >        "Usage: %s [ -i -u -f{a|f|d} -c rept | -d ] { view opts .. | picture [zbuf] }\n",
166                          progname);
167 <        exit(1);
167 >        return(1);
168   }
169  
170  
171 < pix2rays(FILE *fp)
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 <        register int    i;
204 >        int     i, c;
205  
206          while (fscanf(fp, "%lf %lf", &px, &py) == 2) {
207 <                if (px < 0 || px >= rs.xr ||
208 <                                py < 0 || py >= rs.yr) {
156 <                        fprintf(stderr,
157 <                                "%s: (x,y) pair (%.0f,%.0f) out of range\n",
158 <                                        progname, px, py);
159 <                        exit(1);
160 <                }
207 >                px += .5; py += .5;
208 >                loc[0] = px/rs.xr; loc[1] = py/rs.yr;
209                  if (zfd >= 0) {
210 <                        loc2pix(pp, &rs, px/rs.xr, py/rs.yr);
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), 0)
219 <                                        < 0 ||
218 >                                (pp[1]*scanlen(&rs)+pp[0])*sizeof(float),
219 >                                                SEEK_SET) < 0 ||
220                                          read(zfd, &zval, sizeof(float))
221 <                                        < sizeof(float)) {
221 >                                                < sizeof(float)) {
222                                  fprintf(stderr, "%s: depth buffer read error\n",
223                                                  progname);
224                                  exit(1);
225                          }
226                  }
227 <                d = viewray(rorg, rdir, &vw, px/rs.xr, py/rs.yr);
228 <                if (d < -FTINY)
229 <                        rorg[0] = rorg[1] = rorg[2] =
230 <                        rdir[0] = rdir[1] = rdir[2] = 0.;
231 <                else if (zfd >= 0)
232 <                        for (i = 0; i < 3; i++) {
233 <                                rorg[i] += rdir[i]*zval;
234 <                                rdir[i] = -rdir[i];
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 <                else if (d > FTINY) {
242 <                        rdir[0] *= d; rdir[1] *= d; rdir[2] *= d;
241 >                        (*putr)(rorg, rdir);
242 >                        if (c) {
243 >                                loc[0] = px/rs.xr; loc[1] = py/rs.yr;
244 >                        }
245                  }
246 <                (*putr)(rorg, rdir);
246 >                if (unbuffered)
247 >                        fflush(stdout);
248          }
249          if (!feof(fp)) {
250                  fprintf(stderr, "%s: expected px py on input\n", progname);
# Line 191 | Line 253 | pix2rays(FILE *fp)
253   }
254  
255  
256 < putrays()
256 > static void
257 > putrays(void)
258   {
259 <        static RREAL    loc[2];
260 <        static FVECT    rorg, rdir;
261 <        float   *zbuf;
259 >        RREAL   loc[2];
260 >        FVECT   rorg, rdir;
261 >        float   *zbuf = NULL;
262          int     sc;
263          double  d;
264 <        register int    si, i;
264 >        int     si, i, c;
265  
266          if (zfd >= 0) {
267                  zbuf = (float *)malloc(scanlen(&rs)*sizeof(float));
# Line 217 | Line 280 | putrays()
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)
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 <                                        rorg[i] += rdir[i]*zbuf[si];
293 <                                        rdir[i] = -rdir[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)
# Line 238 | Line 304 | putrays()
304   }
305  
306  
307 < puta(ro, rd)            /* put out ray in ASCII format */
308 < FVECT   ro, rd;
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],
# Line 247 | Line 316 | FVECT  ro, rd;
316   }
317  
318  
319 < putf(ro, rd)            /* put out ray in float format */
320 < FVECT   ro, rd;
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 <        fwrite(v, sizeof(float), 6, stdout);
329 >        putbinary(v, sizeof(float), 6, stdout);
330   }
331  
332  
333 < putd(ro, rd)            /* put out ray in double format */
334 < FVECT   ro, rd;
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 <        fwrite(v, sizeof(double), 6, stdout);
343 >        putbinary(v, sizeof(double), 6, stdout);
344   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines