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

Comparing ray/src/common/image.c (file contents):
Revision 1.4 by greg, Fri Dec 22 08:21:53 1989 UTC vs.
Revision 1.12 by greg, Sat Oct 13 20:56:01 1990 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10   *     10/17/85
11   */
12  
13 #include  <ctype.h>
14
13   #include  "standard.h"
14  
15   #include  "view.h"
16  
17 < VIEW  stdview = STDVIEW(512);           /* default view parameters */
17 > VIEW  stdview = STDVIEW;                /* default view parameters */
18  
19  
20   char *
21 < sskip(s)                /* skip a word */
24 < register char  *s;
25 < {
26 <        while (isspace(*s)) s++;
27 <        while (*s && !isspace(*s)) s++;
28 <        return(s);
29 < }
30 <
31 <
32 < char *
33 < setview(v)              /* set vhinc and vvinc, return message on error */
21 > setview(v)              /* set hvec and vvec, return message on error */
22   register VIEW  *v;
23   {
24 <        double  tan(), dt;
24 >        static char  ill_horiz[] = "illegal horizontal view size";
25 >        static char  ill_vert[] = "illegal vertical view size";
26          
27          if (normalize(v->vdir) == 0.0)          /* normalize direction */
28                  return("zero view direction");
40                
41        fcross(v->vhinc, v->vdir, v->vup);      /* compute horiz dir */
29  
30 <        if (normalize(v->vhinc) == 0.0)
30 >        fcross(v->hvec, v->vdir, v->vup);       /* compute horiz dir */
31 >
32 >        if (normalize(v->hvec) == 0.0)
33                  return("illegal view up vector");
45                
46        fcross(v->vvinc, v->vhinc, v->vdir);    /* compute vert dir */
34  
35 <        if (v->type == VT_PAR)
49 <                dt =  v->horiz;
50 <        else if (v->type == VT_PER)
51 <                dt = 2.0 * tan(v->horiz*(PI/180.0/2.0));
52 <        else
53 <                return("unknown view type");
35 >        fcross(v->vvec, v->hvec, v->vdir);      /* compute vert dir */
36  
37 <        if (dt <= FTINY || dt >= FHUGE)
38 <                return("illegal horizontal view size");
37 >        if (v->horiz <= FTINY)
38 >                return(ill_horiz);
39 >        if (v->vert <= FTINY)
40 >                return(ill_vert);
41  
42 <        if (v->hresolu <= 0)
43 <                return("illegal horizontal resolution");
42 >        switch (v->type) {
43 >        case VT_PAR:                            /* parallel view */
44 >                v->hn2 = v->horiz;
45 >                v->vn2 = v->vert;
46 >                break;
47 >        case VT_PER:                            /* perspective view */
48 >                if (v->horiz >= 180.0-FTINY)
49 >                        return(ill_horiz);
50 >                if (v->vert >= 180.0-FTINY)
51 >                        return(ill_vert);
52 >                v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
53 >                v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
54 >                break;
55 >        case VT_ANG:                            /* angular fisheye */
56 >                if (v->horiz > 360.0+FTINY)
57 >                        return(ill_horiz);
58 >                if (v->vert > 360.0+FTINY)
59 >                        return(ill_vert);
60 >                v->hn2 = v->horiz / 90.0;
61 >                v->vn2 = v->vert / 90.0;
62 >                break;
63 >        case VT_HEM:                            /* hemispherical fisheye */
64 >                if (v->horiz > 180.0+FTINY)
65 >                        return(ill_horiz);
66 >                if (v->vert > 180.0+FTINY)
67 >                        return(ill_vert);
68 >                v->hn2 = 2.0 * sin(v->horiz*(PI/180.0/2.0));
69 >                v->vn2 = 2.0 * sin(v->vert*(PI/180.0/2.0));
70 >                break;
71 >        default:
72 >                return("unknown view type");
73 >        }
74 >        if (v->type == VT_PAR || v->type == VT_PER) {
75 >                v->hvec[0] *= v->hn2;
76 >                v->hvec[1] *= v->hn2;
77 >                v->hvec[2] *= v->hn2;
78 >                v->vvec[0] *= v->vn2;
79 >                v->vvec[1] *= v->vn2;
80 >                v->vvec[2] *= v->vn2;
81 >        }
82 >        v->hn2 *= v->hn2;
83 >        v->vn2 *= v->vn2;
84  
85 <        dt /= (double)v->hresolu;
86 <        v->vhinc[0] *= dt;
63 <        v->vhinc[1] *= dt;
64 <        v->vhinc[2] *= dt;
85 >        return(NULL);
86 > }
87  
66        if (v->type == VT_PAR)
67                dt = v->vert;
68        else
69                dt = 2.0 * tan(v->vert*(PI/180.0/2.0));
88  
89 <        if (dt <= FTINY || dt >= FHUGE)
90 <                return("illegal vertical view size");
91 <
92 <        if (v->vresolu <= 0)
93 <                return("illegal vertical resolution");
94 <        
95 <        dt /= (double)v->vresolu;
96 <        v->vvinc[0] *= dt;
97 <        v->vvinc[1] *= dt;
98 <        v->vvinc[2] *= dt;
99 <
82 <        v->vhn2 = DOT(v->vhinc,v->vhinc);
83 <        v->vvn2 = DOT(v->vvinc,v->vvinc);
84 <
85 <        return(NULL);
89 > normaspect(va, ap, xp, yp)              /* fix pixel aspect or resolution */
90 > double  va;                     /* view aspect ratio */
91 > double  *ap;                    /* pixel aspect in (or out if 0) */
92 > int  *xp, *yp;                  /* x and y resolution in (or out if *ap!=0) */
93 > {
94 >        if (*ap <= FTINY)
95 >                *ap = va * *xp / *yp;           /* compute pixel aspect */
96 >        else if (va * *xp > *ap * *yp)
97 >                *xp = *yp / va * *ap + .5;      /* reduce x resolution */
98 >        else
99 >                *yp = *xp * va / *ap + .5;      /* reduce y resolution */
100   }
101  
102  
103 < rayview(orig, direc, v, x, y)           /* compute ray origin and direction */
103 > viewray(orig, direc, v, x, y)           /* compute ray origin and direction */
104   FVECT  orig, direc;
105   register VIEW  *v;
106   double  x, y;
107   {
108 <        x -= 0.5 * v->hresolu;
109 <        y -= 0.5 * v->vresolu;
108 >        double  d, z;
109 >        
110 >        x += v->hoff - 0.5;
111 >        y += v->voff - 0.5;
112  
113 <        if (v->type == VT_PAR) {        /* parallel view */
114 <                orig[0] = v->vp[0] + x*v->vhinc[0] + y*v->vvinc[0];
115 <                orig[1] = v->vp[1] + x*v->vhinc[1] + y*v->vvinc[1];
116 <                orig[2] = v->vp[2] + x*v->vhinc[2] + y*v->vvinc[2];
113 >        switch(v->type) {
114 >        case VT_PAR:                    /* parallel view */
115 >                orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0];
116 >                orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1];
117 >                orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2];
118                  VCOPY(direc, v->vdir);
119 <        } else {                        /* perspective view */
119 >                return(0);
120 >        case VT_PER:                    /* perspective view */
121                  VCOPY(orig, v->vp);
122 <                direc[0] = v->vdir[0] + x*v->vhinc[0] + y*v->vvinc[0];
123 <                direc[1] = v->vdir[1] + x*v->vhinc[1] + y*v->vvinc[1];
124 <                direc[2] = v->vdir[2] + x*v->vhinc[2] + y*v->vvinc[2];
122 >                direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
123 >                direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
124 >                direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
125                  normalize(direc);
126 +                return(0);
127 +        case VT_HEM:                    /* hemispherical fisheye */
128 +                x *= v->horiz/90.0;
129 +                y *= v->vert/90.0;
130 +                z = 1.0 - x*x - y*y;
131 +                if (z < 0.0)
132 +                        return(-1);
133 +                z = sqrt(z);
134 +                VCOPY(orig, v->vp);
135 +                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
136 +                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
137 +                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
138 +                return(0);
139 +        case VT_ANG:                    /* angular fisheye */
140 +                x *= v->horiz/180.0;
141 +                y *= v->vert/180.0;
142 +                d = x*x + y*y;
143 +                if (d > 1.0)
144 +                        return(-1);
145 +                VCOPY(orig, v->vp);
146 +                if (d <= FTINY) {
147 +                        VCOPY(direc, v->vdir);
148 +                        return(0);
149 +                }
150 +                d = sqrt(d);
151 +                z = cos(PI*d);
152 +                d = sqrt(1 - z*z)/d;
153 +                x *= d;
154 +                y *= d;
155 +                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
156 +                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
157 +                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
158 +                return(0);
159          }
160 +        return(-1);
161   }
162  
163  
164 < pixelview(xp, yp, zp, v, p)             /* find image location for point */
164 > viewpixel(xp, yp, zp, v, p)             /* find image location for point */
165   double  *xp, *yp, *zp;
166   register VIEW  *v;
167   FVECT  p;
168   {
117        extern double  sqrt();
169          double  d;
170          FVECT  disp;
171 <        
171 >
172          disp[0] = p[0] - v->vp[0];
173          disp[1] = p[1] - v->vp[1];
174          disp[2] = p[2] - v->vp[2];
175  
176 <        if (v->type == VT_PAR) {        /* parallel view */
176 >        switch (v->type) {
177 >        case VT_PAR:                    /* parallel view */
178                  if (zp != NULL)
179                          *zp = DOT(disp,v->vdir);
180 <        } else {                        /* perspective view */
181 <                d = 1.0/DOT(disp,v->vdir);
180 >                *xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
181 >                *yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
182 >                return;
183 >        case VT_PER:                    /* perspective view */
184 >                d = DOT(disp,v->vdir);
185                  if (zp != NULL) {
186                          *zp = sqrt(DOT(disp,disp));
187                          if (d < 0.0)
188                                  *zp = -*zp;
189                  }
190 <                disp[0] *= d;
191 <                disp[1] *= d;
192 <                disp[2] *= d;
190 >                if (d < 0.0)            /* fold pyramid */
191 >                        d = -d;
192 >                if (d > FTINY) {
193 >                        d = 1.0/d;
194 >                        disp[0] *= d;
195 >                        disp[1] *= d;
196 >                        disp[2] *= d;
197 >                }
198 >                *xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
199 >                *yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
200 >                return;
201 >        case VT_HEM:                    /* hemispherical fisheye */
202 >                d = normalize(disp);
203 >                if (zp != NULL) {
204 >                        if (DOT(disp,v->vdir) < 0.0)
205 >                                *zp = -d;
206 >                        else
207 >                                *zp = d;
208 >                }
209 >                *xp = DOT(disp,v->hvec)*90.0/v->horiz + 0.5 - v->hoff;
210 >                *yp = DOT(disp,v->vvec)*90.0/v->vert + 0.5 - v->voff;
211 >                return;
212 >        case VT_ANG:                    /* angular fisheye */
213 >                d = normalize(disp);
214 >                if (zp != NULL)
215 >                        *zp = d;
216 >                *xp = 0.5 - v->hoff;
217 >                *yp = 0.5 - v->voff;
218 >                d = DOT(disp,v->vdir);
219 >                if (d >= 1.0-FTINY)
220 >                        return;
221 >                if (d <= -(1.0-FTINY)) {
222 >                        *xp += 180.0/v->horiz;
223 >                        *yp += 180.0/v->vert;
224 >                        return;
225 >                }
226 >                d = acos(d)/PI / sqrt(1.0 - d*d);
227 >                *xp += DOT(disp,v->hvec)*d*180.0/v->horiz;
228 >                *yp += DOT(disp,v->vvec)*d*180.0/v->vert;
229 >                return;
230          }
139        *xp = DOT(disp,v->vhinc)/v->vhn2 + 0.5*v->hresolu;
140        *yp = DOT(disp,v->vvinc)/v->vvn2 + 0.5*v->vresolu;
231   }
232  
233  
234 < sscanview(vp, s)                        /* get view parameters */
235 < register VIEW  *vp;
234 > int
235 > getviewopt(v, ac, av)                   /* process view argument */
236 > register VIEW  *v;
237 > int  ac;
238 > register char  *av[];
239 > {
240 > #define check(c,n)      if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1)
241 >        extern double  atof();
242 >
243 >        if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
244 >                return(-1);
245 >        switch (av[0][2]) {
246 >        case 't':                       /* type */
247 >                if (!av[0][3] || av[0][3]==' ')
248 >                        return(-1);
249 >                check(4,0);
250 >                v->type = av[0][3];
251 >                return(0);
252 >        case 'p':                       /* point */
253 >                check(3,3);
254 >                v->vp[0] = atof(av[1]);
255 >                v->vp[1] = atof(av[2]);
256 >                v->vp[2] = atof(av[3]);
257 >                return(3);
258 >        case 'd':                       /* direction */
259 >                check(3,3);
260 >                v->vdir[0] = atof(av[1]);
261 >                v->vdir[1] = atof(av[2]);
262 >                v->vdir[2] = atof(av[3]);
263 >                return(3);
264 >        case 'u':                       /* up */
265 >                check(3,3);
266 >                v->vup[0] = atof(av[1]);
267 >                v->vup[1] = atof(av[2]);
268 >                v->vup[2] = atof(av[3]);
269 >                return(3);
270 >        case 'h':                       /* horizontal size */
271 >                check(3,1);
272 >                v->horiz = atof(av[1]);
273 >                return(1);
274 >        case 'v':                       /* vertical size */
275 >                check(3,1);
276 >                v->vert = atof(av[1]);
277 >                return(1);
278 >        case 's':                       /* shift */
279 >                check(3,1);
280 >                v->hoff = atof(av[1]);
281 >                return(1);
282 >        case 'l':                       /* lift */
283 >                check(3,1);
284 >                v->voff = atof(av[1]);
285 >                return(1);
286 >        default:
287 >                return(-1);
288 >        }
289 > #undef check
290 > }
291 >
292 >
293 > int
294 > sscanview(vp, s)                        /* get view parameters from string */
295 > VIEW  *vp;
296   register char  *s;
297   {
298 <        for ( ; ; )
299 <                switch (*s++) {
300 <                case '\0':
301 <                case '\n':
302 <                        return(0);
303 <                case '-':
304 <                        switch (*s++) {
305 <                        case '\0':
306 <                                return(-1);
307 <                        case 'x':
308 <                                if (sscanf(s, "%d", &vp->hresolu) != 1)
309 <                                        return(-1);
310 <                                s = sskip(s);
311 <                                continue;
312 <                        case 'y':
313 <                                if (sscanf(s, "%d", &vp->vresolu) != 1)
314 <                                        return(-1);
315 <                                s = sskip(s);
316 <                                continue;
317 <                        case 'v':
318 <                                switch (*s++) {
319 <                                case '\0':
320 <                                        return(-1);
321 <                                case 't':
172 <                                        vp->type = *s++;
173 <                                        continue;
174 <                                case 'p':
175 <                                        if (sscanf(s, "%lf %lf %lf",
176 <                                                        &vp->vp[0],
177 <                                                        &vp->vp[1],
178 <                                                        &vp->vp[2]) != 3)
179 <                                                return(-1);
180 <                                        s = sskip(sskip(sskip(s)));
181 <                                        continue;
182 <                                case 'd':
183 <                                        if (sscanf(s, "%lf %lf %lf",
184 <                                                        &vp->vdir[0],
185 <                                                        &vp->vdir[1],
186 <                                                        &vp->vdir[2]) != 3)
187 <                                                return(-1);
188 <                                        s = sskip(sskip(sskip(s)));
189 <                                        continue;
190 <                                case 'u':
191 <                                        if (sscanf(s, "%lf %lf %lf",
192 <                                                        &vp->vup[0],
193 <                                                        &vp->vup[1],
194 <                                                        &vp->vup[2]) != 3)
195 <                                                return(-1);
196 <                                        s = sskip(sskip(sskip(s)));
197 <                                        continue;
198 <                                case 'h':
199 <                                        if (sscanf(s, "%lf",
200 <                                                        &vp->horiz) != 1)
201 <                                                return(-1);
202 <                                        s = sskip(s);
203 <                                        continue;
204 <                                case 'v':
205 <                                        if (sscanf(s, "%lf",
206 <                                                        &vp->vert) != 1)
207 <                                                return(-1);
208 <                                        s = sskip(s);
209 <                                        continue;
210 <                                }
211 <                                continue;
212 <                        }
213 <                        continue;
214 <                }
298 >        int  ac;
299 >        char  *av[4];
300 >        int  na;
301 >        int  nvopts = 0;
302 >
303 >        while (*s == ' ')
304 >                s++;
305 >        while (*s) {
306 >                ac = 0;
307 >                do {
308 >                        av[ac++] = s;
309 >                        while (*s && *s != ' ')
310 >                                s++;
311 >                        while (*s == ' ')
312 >                                s++;
313 >                } while (*s && ac < 4);
314 >                if ((na = getviewopt(vp, ac, av)) >= 0) {
315 >                        if (na+1 < ac)
316 >                                s = av[na+1];
317 >                        nvopts++;
318 >                } else if (ac > 1)
319 >                        s = av[1];
320 >        }
321 >        return(nvopts);
322   }
323  
324  
# Line 224 | Line 331 | FILE  *fp;
331          fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
332          fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
333          fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
334 <        fprintf(fp, " -x %d -y %d", vp->hresolu, vp->vresolu);
334 >        fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
335   }
336  
337  
# Line 241 | Line 348 | char  *s;
348  
349          for (an = altname; *an != NULL; an++)
350                  if (!strncmp(*an, s, strlen(*an))) {
351 <                        if (sscanview(hview, s+strlen(*an)) == 0)
351 >                        if (sscanview(hview, s+strlen(*an)) > 0)
352                                  gothview++;
353                          return;
354                  }
# Line 249 | Line 356 | char  *s;
356  
357  
358   int
359 < viewfile(fname, vp)                     /* get view from file */
359 > viewfile(fname, vp, xp, yp)             /* get view from file */
360   char  *fname;
361   VIEW  *vp;
362 + int  *xp, *yp;
363   {
364          extern char  *progname;
365          FILE  *fp;
# Line 264 | Line 372 | VIEW  *vp;
372          gothview = 0;
373  
374          getheader(fp, gethview);
375 +
376 +        if (xp != NULL && yp != NULL
377 +                        && fgetresolu(xp, yp, fp) == -1)
378 +                gothview = 0;
379  
380          fclose(fp);
381  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines