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.2 by greg, Fri Oct 20 16:53:03 1989 UTC vs.
Revision 1.13 by greg, Mon Oct 15 14:17:05 1990 UTC

# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
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 < 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");
28                
29        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");
33                
34        fcross(v->vvinc, v->vhinc, v->vdir);    /* compute vert dir */
34  
35 <        if (v->type == VT_PAR)
37 <                dt =  v->horiz;
38 <        else if (v->type == VT_PER)
39 <                dt = 2.0 * tan(v->horiz*(PI/180.0/2.0));
40 <        else
41 <                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_ANG) {
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;
51 <        v->vhinc[1] *= dt;
52 <        v->vhinc[2] *= dt;
85 >        return(NULL);
86 > }
87  
54        if (v->type == VT_PAR)
55                dt = v->vert;
56        else
57                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 <
70 <        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 <        register int  i;
108 >        double  d, z;
109 >        
110 >        x += v->hoff - 0.5;
111 >        y += v->voff - 0.5;
112  
113 <        x -= 0.5 * v->hresolu;
114 <        y -= 0.5 * v->vresolu;
115 <
116 <        if (v->type == VT_PAR) {        /* parallel view */
117 <                for (i = 0; i < 3; i++)
86 <                        orig[i] = v->vp[i] + x*v->vhinc[i] + y*v->vvinc[i];
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 <                for (i = 0; i < 3; i++)
123 <                        direc[i] = v->vdir[i] + x*v->vhinc[i] + y*v->vvinc[i];
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 +                z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
129 +                if (z < 0.0)
130 +                        return(-1);
131 +                z = sqrt(z);
132 +                VCOPY(orig, v->vp);
133 +                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
134 +                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
135 +                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
136 +                return(0);
137 +        case VT_ANG:                    /* angular fisheye */
138 +                x *= v->horiz/180.0;
139 +                y *= v->vert/180.0;
140 +                d = x*x + y*y;
141 +                if (d > 1.0)
142 +                        return(-1);
143 +                VCOPY(orig, v->vp);
144 +                if (d <= FTINY) {
145 +                        VCOPY(direc, v->vdir);
146 +                        return(0);
147 +                }
148 +                d = sqrt(d);
149 +                z = cos(PI*d);
150 +                d = sqrt(1 - z*z)/d;
151 +                x *= d;
152 +                y *= d;
153 +                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
154 +                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
155 +                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
156 +                return(0);
157          }
158 +        return(-1);
159   }
160  
161  
162 < sscanview(vp, s)                        /* get view parameters */
163 < register VIEW  *vp;
164 < register char  *s;
162 > viewpixel(xp, yp, zp, v, p)             /* find image location for point */
163 > double  *xp, *yp, *zp;
164 > register VIEW  *v;
165 > FVECT  p;
166   {
167 <        for ( ; ; )
168 <                switch (*s++) {
169 <                case '\0':
170 <                case '\n':
171 <                        return(0);
172 <                case '-':
173 <                        switch (*s++) {
174 <                        case '\0':
175 <                                return(-1);
176 <                        case 'x':
177 <                                if (sscanf(s, "%d", &vp->hresolu) != 1)
178 <                                        return(-1);
179 <                                s = sskip(s);
180 <                                continue;
181 <                        case 'y':
182 <                                if (sscanf(s, "%d", &vp->vresolu) != 1)
183 <                                        return(-1);
184 <                                s = sskip(s);
119 <                                continue;
120 <                        case 'v':
121 <                                switch (*s++) {
122 <                                case '\0':
123 <                                        return(-1);
124 <                                case 't':
125 <                                        vp->type = *s++;
126 <                                        continue;
127 <                                case 'p':
128 <                                        if (sscanf(s, "%lf %lf %lf",
129 <                                                        &vp->vp[0],
130 <                                                        &vp->vp[1],
131 <                                                        &vp->vp[2]) != 3)
132 <                                                return(-1);
133 <                                        s = sskip(sskip(sskip(s)));
134 <                                        continue;
135 <                                case 'd':
136 <                                        if (sscanf(s, "%lf %lf %lf",
137 <                                                        &vp->vdir[0],
138 <                                                        &vp->vdir[1],
139 <                                                        &vp->vdir[2]) != 3)
140 <                                                return(-1);
141 <                                        s = sskip(sskip(sskip(s)));
142 <                                        continue;
143 <                                case 'u':
144 <                                        if (sscanf(s, "%lf %lf %lf",
145 <                                                        &vp->vup[0],
146 <                                                        &vp->vup[1],
147 <                                                        &vp->vup[2]) != 3)
148 <                                                return(-1);
149 <                                        s = sskip(sskip(sskip(s)));
150 <                                        continue;
151 <                                case 'h':
152 <                                        if (sscanf(s, "%lf",
153 <                                                        &vp->horiz) != 1)
154 <                                                return(-1);
155 <                                        s = sskip(s);
156 <                                        continue;
157 <                                case 'v':
158 <                                        if (sscanf(s, "%lf",
159 <                                                        &vp->vert) != 1)
160 <                                                return(-1);
161 <                                        s = sskip(s);
162 <                                        continue;
163 <                                }
164 <                                continue;
165 <                        }
166 <                        continue;
167 >        double  d;
168 >        FVECT  disp;
169 >
170 >        disp[0] = p[0] - v->vp[0];
171 >        disp[1] = p[1] - v->vp[1];
172 >        disp[2] = p[2] - v->vp[2];
173 >
174 >        switch (v->type) {
175 >        case VT_PAR:                    /* parallel view */
176 >                if (zp != NULL)
177 >                        *zp = DOT(disp,v->vdir);
178 >                break;
179 >        case VT_PER:                    /* perspective view */
180 >                d = DOT(disp,v->vdir);
181 >                if (zp != NULL) {
182 >                        *zp = sqrt(DOT(disp,disp));
183 >                        if (d < 0.0)
184 >                                *zp = -*zp;
185                  }
186 +                if (d < 0.0)            /* fold pyramid */
187 +                        d = -d;
188 +                if (d > FTINY) {
189 +                        d = 1.0/d;
190 +                        disp[0] *= d;
191 +                        disp[1] *= d;
192 +                        disp[2] *= d;
193 +                }
194 +                break;
195 +        case VT_HEM:                    /* hemispherical fisheye */
196 +                d = normalize(disp);
197 +                if (zp != NULL) {
198 +                        if (DOT(disp,v->vdir) < 0.0)
199 +                                *zp = -d;
200 +                        else
201 +                                *zp = d;
202 +                }
203 +                break;
204 +        case VT_ANG:                    /* angular fisheye */
205 +                d = normalize(disp);
206 +                if (zp != NULL)
207 +                        *zp = d;
208 +                *xp = 0.5 - v->hoff;
209 +                *yp = 0.5 - v->voff;
210 +                d = DOT(disp,v->vdir);
211 +                if (d >= 1.0-FTINY)
212 +                        return;
213 +                if (d <= -(1.0-FTINY)) {
214 +                        *xp += 180.0/v->horiz;
215 +                        *yp += 180.0/v->vert;
216 +                        return;
217 +                }
218 +                d = acos(d)/PI / sqrt(1.0 - d*d);
219 +                *xp += DOT(disp,v->hvec)*d*180.0/v->horiz;
220 +                *yp += DOT(disp,v->vvec)*d*180.0/v->vert;
221 +                return;
222 +        }
223 +        *xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
224 +        *yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
225   }
226  
227  
228 + int
229 + getviewopt(v, ac, av)                   /* process view argument */
230 + register VIEW  *v;
231 + int  ac;
232 + register char  *av[];
233 + {
234 + #define check(c,n)      if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1)
235 +        extern double  atof();
236 +
237 +        if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
238 +                return(-1);
239 +        switch (av[0][2]) {
240 +        case 't':                       /* type */
241 +                if (!av[0][3] || av[0][3]==' ')
242 +                        return(-1);
243 +                check(4,0);
244 +                v->type = av[0][3];
245 +                return(0);
246 +        case 'p':                       /* point */
247 +                check(3,3);
248 +                v->vp[0] = atof(av[1]);
249 +                v->vp[1] = atof(av[2]);
250 +                v->vp[2] = atof(av[3]);
251 +                return(3);
252 +        case 'd':                       /* direction */
253 +                check(3,3);
254 +                v->vdir[0] = atof(av[1]);
255 +                v->vdir[1] = atof(av[2]);
256 +                v->vdir[2] = atof(av[3]);
257 +                return(3);
258 +        case 'u':                       /* up */
259 +                check(3,3);
260 +                v->vup[0] = atof(av[1]);
261 +                v->vup[1] = atof(av[2]);
262 +                v->vup[2] = atof(av[3]);
263 +                return(3);
264 +        case 'h':                       /* horizontal size */
265 +                check(3,1);
266 +                v->horiz = atof(av[1]);
267 +                return(1);
268 +        case 'v':                       /* vertical size */
269 +                check(3,1);
270 +                v->vert = atof(av[1]);
271 +                return(1);
272 +        case 's':                       /* shift */
273 +                check(3,1);
274 +                v->hoff = atof(av[1]);
275 +                return(1);
276 +        case 'l':                       /* lift */
277 +                check(3,1);
278 +                v->voff = atof(av[1]);
279 +                return(1);
280 +        default:
281 +                return(-1);
282 +        }
283 + #undef check
284 + }
285 +
286 +
287 + int
288 + sscanview(vp, s)                        /* get view parameters from string */
289 + VIEW  *vp;
290 + register char  *s;
291 + {
292 +        int  ac;
293 +        char  *av[4];
294 +        int  na;
295 +        int  nvopts = 0;
296 +
297 +        while (*s == ' ')
298 +                s++;
299 +        while (*s) {
300 +                ac = 0;
301 +                do {
302 +                        av[ac++] = s;
303 +                        while (*s && *s != ' ')
304 +                                s++;
305 +                        while (*s == ' ')
306 +                                s++;
307 +                } while (*s && ac < 4);
308 +                if ((na = getviewopt(vp, ac, av)) >= 0) {
309 +                        if (na+1 < ac)
310 +                                s = av[na+1];
311 +                        nvopts++;
312 +                } else if (ac > 1)
313 +                        s = av[1];
314 +        }
315 +        return(nvopts);
316 + }
317 +
318 +
319   fprintview(vp, fp)                      /* write out view parameters */
320   register VIEW  *vp;
321   FILE  *fp;
# Line 177 | Line 325 | FILE  *fp;
325          fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
326          fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
327          fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
328 <        fprintf(fp, " -x %d -y %d", vp->hresolu, vp->vresolu);
328 >        fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
329   }
330  
331  
332   static VIEW  *hview;                    /* view from header */
333   static int  gothview;                   /* success indicator */
334 < static char  *altname[] = {NULL,"rpict","rview",VIEWSTR,NULL};
334 > static char  *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL};
335  
336  
337   static
# Line 194 | Line 342 | char  *s;
342  
343          for (an = altname; *an != NULL; an++)
344                  if (!strncmp(*an, s, strlen(*an))) {
345 <                        if (sscanview(hview, s+strlen(*an)) == 0)
345 >                        if (sscanview(hview, s+strlen(*an)) > 0)
346                                  gothview++;
347                          return;
348                  }
# Line 202 | Line 350 | char  *s;
350  
351  
352   int
353 < viewfile(fname, vp)                     /* get view from file */
353 > viewfile(fname, vp, xp, yp)             /* get view from file */
354   char  *fname;
355   VIEW  *vp;
356 + int  *xp, *yp;
357   {
358          extern char  *progname;
359          FILE  *fp;
# Line 217 | Line 366 | VIEW  *vp;
366          gothview = 0;
367  
368          getheader(fp, gethview);
369 +
370 +        if (xp != NULL && yp != NULL
371 +                        && fgetresolu(xp, yp, fp) == -1)
372 +                gothview = 0;
373  
374          fclose(fp);
375  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines