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.11 by greg, Fri Sep 21 17:06:31 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;
25 <        
24 >        extern double  tan(), normalize();
25 >
26          if (normalize(v->vdir) == 0.0)          /* normalize direction */
27                  return("zero view direction");
28                
29        fcross(v->vhinc, v->vdir, v->vup);      /* compute horiz dir */
28  
29 <        if (normalize(v->vhinc) == 0.0)
29 >        fcross(v->hvec, v->vdir, v->vup);       /* compute horiz dir */
30 >
31 >        if (normalize(v->hvec) == 0.0)
32                  return("illegal view up vector");
33                
34        fcross(v->vvinc, v->vhinc, v->vdir);    /* compute vert dir */
33  
34 +        fcross(v->vvec, v->hvec, v->vdir);      /* compute vert dir */
35 +
36          if (v->type == VT_PAR)
37 <                dt =  v->horiz;
37 >                v->hn2 = v->horiz;
38          else if (v->type == VT_PER)
39 <                dt = 2.0 * tan(v->horiz*(PI/180.0/2.0));
39 >                v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0));
40          else
41                  return("unknown view type");
42  
43 <        if (dt <= FTINY || dt >= FHUGE)
43 >        if (v->hn2 <= FTINY || v->hn2 >= FHUGE)
44                  return("illegal horizontal view size");
45  
46 <        if (v->hresolu <= 0)
47 <                return("illegal horizontal resolution");
46 >        v->hvec[0] *= v->hn2;
47 >        v->hvec[1] *= v->hn2;
48 >        v->hvec[2] *= v->hn2;
49 >        v->hn2 *= v->hn2;
50  
49        dt /= (double)v->hresolu;
50        v->vhinc[0] *= dt;
51        v->vhinc[1] *= dt;
52        v->vhinc[2] *= dt;
53
51          if (v->type == VT_PAR)
52 <                dt = v->vert;
52 >                v->vn2 = v->vert;
53          else
54 <                dt = 2.0 * tan(v->vert*(PI/180.0/2.0));
54 >                v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0));
55  
56 <        if (dt <= FTINY || dt >= FHUGE)
56 >        if (v->vn2 <= FTINY || v->vn2 >= FHUGE)
57                  return("illegal vertical view size");
58  
59 <        if (v->vresolu <= 0)
60 <                return("illegal vertical resolution");
61 <        
62 <        dt /= (double)v->vresolu;
66 <        v->vvinc[0] *= dt;
67 <        v->vvinc[1] *= dt;
68 <        v->vvinc[2] *= dt;
59 >        v->vvec[0] *= v->vn2;
60 >        v->vvec[1] *= v->vn2;
61 >        v->vvec[2] *= v->vn2;
62 >        v->vn2 *= v->vn2;
63  
64          return(NULL);
65   }
66  
67  
68 < rayview(orig, direc, v, x, y)           /* compute ray origin and direction */
68 > normaspect(va, ap, xp, yp)              /* fix pixel aspect or resolution */
69 > double  va;                     /* view aspect ratio */
70 > double  *ap;                    /* pixel aspect in (or out if 0) */
71 > int  *xp, *yp;                  /* x and y resolution in (or out if *ap!=0) */
72 > {
73 >        if (*ap <= FTINY)
74 >                *ap = va * *xp / *yp;           /* compute pixel aspect */
75 >        else if (va * *xp > *ap * *yp)
76 >                *xp = *yp / va * *ap + .5;      /* reduce x resolution */
77 >        else
78 >                *yp = *xp * va / *ap + .5;      /* reduce y resolution */
79 > }
80 >
81 >
82 > viewray(orig, direc, v, x, y)           /* compute ray origin and direction */
83   FVECT  orig, direc;
84   register VIEW  *v;
85   double  x, y;
86   {
87 <        register int  i;
87 >        x += v->hoff - 0.5;
88 >        y += v->voff - 0.5;
89  
81        x -= 0.5 * v->hresolu;
82        y -= 0.5 * v->vresolu;
83
90          if (v->type == VT_PAR) {        /* parallel view */
91 <                for (i = 0; i < 3; i++)
92 <                        orig[i] = v->vp[i] + x*v->vhinc[i] + y*v->vvinc[i];
91 >                orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0];
92 >                orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1];
93 >                orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2];
94                  VCOPY(direc, v->vdir);
95          } else {                        /* perspective view */
96                  VCOPY(orig, v->vp);
97 <                for (i = 0; i < 3; i++)
98 <                        direc[i] = v->vdir[i] + x*v->vhinc[i] + y*v->vvinc[i];
97 >                direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
98 >                direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
99 >                direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
100                  normalize(direc);
101          }
102   }
103  
104  
105 < sscanview(vp, s)                        /* get view parameters */
106 < register VIEW  *vp;
107 < register char  *s;
105 > viewpixel(xp, yp, zp, v, p)             /* find image location for point */
106 > double  *xp, *yp, *zp;
107 > register VIEW  *v;
108 > FVECT  p;
109   {
110 <        for ( ; ; )
111 <                switch (*s++) {
112 <                case '\0':
113 <                case '\n':
114 <                        return(0);
115 <                case '-':
116 <                        switch (*s++) {
117 <                        case '\0':
118 <                                return(-1);
119 <                        case 'x':
120 <                                if (sscanf(s, "%d", &vp->hresolu) != 1)
121 <                                        return(-1);
122 <                                s = sskip(s);
123 <                                continue;
124 <                        case 'y':
125 <                                if (sscanf(s, "%d", &vp->vresolu) != 1)
126 <                                        return(-1);
118 <                                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;
110 >        extern double  sqrt();
111 >        double  d;
112 >        FVECT  disp;
113 >
114 >        disp[0] = p[0] - v->vp[0];
115 >        disp[1] = p[1] - v->vp[1];
116 >        disp[2] = p[2] - v->vp[2];
117 >
118 >        if (v->type == VT_PAR) {        /* parallel view */
119 >                if (zp != NULL)
120 >                        *zp = DOT(disp,v->vdir);
121 >        } else {                        /* perspective view */
122 >                d = DOT(disp,v->vdir);
123 >                if (zp != NULL) {
124 >                        *zp = sqrt(DOT(disp,disp));
125 >                        if (d < 0.0)
126 >                                *zp = -*zp;
127                  }
128 +                if (d < 0.0)            /* fold pyramid */
129 +                        d = -d;
130 +                if (d > FTINY) {
131 +                        d = 1.0/d;
132 +                        disp[0] *= d;
133 +                        disp[1] *= d;
134 +                        disp[2] *= d;
135 +                }
136 +        }
137 +        *xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
138 +        *yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
139   }
140  
141  
142 + int
143 + getviewopt(v, ac, av)                   /* process view argument */
144 + register VIEW  *v;
145 + int  ac;
146 + register char  *av[];
147 + {
148 + #define check(c,n)      if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1)
149 +        extern double  atof();
150 +
151 +        if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
152 +                return(-1);
153 +        switch (av[0][2]) {
154 +        case 't':                       /* type */
155 +                if (!av[0][3] || av[0][3]==' ')
156 +                        return(-1);
157 +                check(4,0);
158 +                v->type = av[0][3];
159 +                return(0);
160 +        case 'p':                       /* point */
161 +                check(3,3);
162 +                v->vp[0] = atof(av[1]);
163 +                v->vp[1] = atof(av[2]);
164 +                v->vp[2] = atof(av[3]);
165 +                return(3);
166 +        case 'd':                       /* direction */
167 +                check(3,3);
168 +                v->vdir[0] = atof(av[1]);
169 +                v->vdir[1] = atof(av[2]);
170 +                v->vdir[2] = atof(av[3]);
171 +                return(3);
172 +        case 'u':                       /* up */
173 +                check(3,3);
174 +                v->vup[0] = atof(av[1]);
175 +                v->vup[1] = atof(av[2]);
176 +                v->vup[2] = atof(av[3]);
177 +                return(3);
178 +        case 'h':                       /* horizontal size */
179 +                check(3,1);
180 +                v->horiz = atof(av[1]);
181 +                return(1);
182 +        case 'v':                       /* vertical size */
183 +                check(3,1);
184 +                v->vert = atof(av[1]);
185 +                return(1);
186 +        case 's':                       /* shift */
187 +                check(3,1);
188 +                v->hoff = atof(av[1]);
189 +                return(1);
190 +        case 'l':                       /* lift */
191 +                check(3,1);
192 +                v->voff = atof(av[1]);
193 +                return(1);
194 +        default:
195 +                return(-1);
196 +        }
197 + #undef check
198 + }
199 +
200 +
201 + int
202 + sscanview(vp, s)                        /* get view parameters from string */
203 + VIEW  *vp;
204 + register char  *s;
205 + {
206 +        int  ac;
207 +        char  *av[4];
208 +        int  na;
209 +        int  nvopts = 0;
210 +
211 +        while (*s == ' ')
212 +                s++;
213 +        while (*s) {
214 +                ac = 0;
215 +                do {
216 +                        av[ac++] = s;
217 +                        while (*s && *s != ' ')
218 +                                s++;
219 +                        while (*s == ' ')
220 +                                s++;
221 +                } while (*s && ac < 4);
222 +                if ((na = getviewopt(vp, ac, av)) >= 0) {
223 +                        if (na+1 < ac)
224 +                                s = av[na+1];
225 +                        nvopts++;
226 +                } else if (ac > 1)
227 +                        s = av[1];
228 +        }
229 +        return(nvopts);
230 + }
231 +
232 +
233   fprintview(vp, fp)                      /* write out view parameters */
234   register VIEW  *vp;
235   FILE  *fp;
# Line 177 | Line 239 | FILE  *fp;
239          fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
240          fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
241          fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
242 <        fprintf(fp, " -x %d -y %d", vp->hresolu, vp->vresolu);
242 >        fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
243   }
244  
245  
246   static VIEW  *hview;                    /* view from header */
247   static int  gothview;                   /* success indicator */
248 < static char  *altname[] = {NULL,"rpict","rview",VIEWSTR,NULL};
248 > static char  *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL};
249  
250  
251   static
# Line 194 | Line 256 | char  *s;
256  
257          for (an = altname; *an != NULL; an++)
258                  if (!strncmp(*an, s, strlen(*an))) {
259 <                        if (sscanview(hview, s+strlen(*an)) == 0)
259 >                        if (sscanview(hview, s+strlen(*an)) > 0)
260                                  gothview++;
261                          return;
262                  }
# Line 202 | Line 264 | char  *s;
264  
265  
266   int
267 < viewfile(fname, vp)                     /* get view from file */
267 > viewfile(fname, vp, xp, yp)             /* get view from file */
268   char  *fname;
269   VIEW  *vp;
270 + int  *xp, *yp;
271   {
272          extern char  *progname;
273          FILE  *fp;
# Line 217 | Line 280 | VIEW  *vp;
280          gothview = 0;
281  
282          getheader(fp, gethview);
283 +
284 +        if (xp != NULL && yp != NULL
285 +                        && fgetresolu(xp, yp, fp) == -1)
286 +                gothview = 0;
287  
288          fclose(fp);
289  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines