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 2.53 by greg, Wed Jan 12 21:07:39 2022 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  image.c - routines for image generation.
6   *
7 < *     10/17/85
7 > *  External symbols declared in view.h
8   */
9  
10 < #include  <ctype.h>
10 > #include "copyright.h"
11  
12 < #include  "standard.h"
13 <
12 > #include  <ctype.h>
13 > #include  "rtio.h"
14 > #include  "rtmath.h"
15 > #include  "paths.h"
16   #include  "view.h"
17  
18 < VIEW  stdview = STDVIEW(512);           /* default view parameters */
18 > VIEW  stdview = STDVIEW;                /* default view parameters */
19  
20 + static gethfunc gethview;
21  
22 char *
23 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 }
22  
31
23   char *
24 < setview(v)              /* set vhinc and vvinc, return message on error */
25 < register VIEW  *v;
24 > setview(                /* set hvec and vvec, return message on error */
25 > VIEW  *v
26 > )
27   {
28 <        double  tan(), dt;
28 >        static char  ill_horiz[] = "illegal horizontal view size";
29 >        static char  ill_vert[] = "illegal vertical view size";
30          
31 <        if (normalize(v->vdir) == 0.0)          /* normalize direction */
31 >        if ((v->vfore < -FTINY) | (v->vaft < -FTINY) ||
32 >                        (v->vaft > FTINY) & (v->vaft <= v->vfore))
33 >                return("illegal fore/aft clipping plane");
34 >
35 >        if (v->vdist <= FTINY)
36 >                return("illegal view distance");
37 >        v->vdist *= normalize(v->vdir);         /* normalize direction */
38 >        if (v->vdist == 0.0)
39                  return("zero view direction");
40                
41        fcross(v->vhinc, v->vdir, v->vup);      /* compute horiz dir */
40  
41 <        if (normalize(v->vhinc) == 0.0)
42 <                return("illegal view up vector");
45 <                
46 <        fcross(v->vvinc, v->vhinc, v->vdir);    /* compute vert dir */
41 >        if (normalize(v->vup) == 0.0)           /* normalize view up */
42 >                return("zero view up vector");
43  
44 <        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");
44 >        fcross(v->hvec, v->vdir, v->vup);       /* compute horiz dir */
45  
46 <        if (dt <= FTINY || dt >= FHUGE)
47 <                return("illegal horizontal view size");
46 >        if (normalize(v->hvec) == 0.0)
47 >                return("view up parallel to view direction");
48  
49 <        if (v->hresolu <= 0)
59 <                return("illegal horizontal resolution");
49 >        fcross(v->vvec, v->hvec, v->vdir);      /* compute vert dir */
50  
51 <        dt /= (double)v->hresolu;
52 <        v->vhinc[0] *= dt;
53 <        v->vhinc[1] *= dt;
54 <        v->vhinc[2] *= dt;
51 >        if (v->horiz <= FTINY)
52 >                return(ill_horiz);
53 >        if (v->vert <= FTINY)
54 >                return(ill_vert);
55  
56 <        if (v->type == VT_PAR)
57 <                dt = v->vert;
58 <        else
59 <                dt = 2.0 * tan(v->vert*(PI/180.0/2.0));
56 >        switch (v->type) {
57 >        case VT_PAR:                            /* parallel view */
58 >                v->hn2 = v->horiz;
59 >                v->vn2 = v->vert;
60 >                break;
61 >        case VT_PER:                            /* perspective view */
62 >                if (v->horiz >= 180.0-FTINY)
63 >                        return(ill_horiz);
64 >                if (v->vert >= 180.0-FTINY)
65 >                        return(ill_vert);
66 >                v->hn2 = 2.0 * tan(v->horiz*(PI/360.));
67 >                v->vn2 = 2.0 * tan(v->vert*(PI/360.));
68 >                break;
69 >        case VT_CYL:                            /* cylindrical panorama */
70 >                if (v->horiz > 360.0+FTINY)
71 >                        return(ill_horiz);
72 >                if (v->vert >= 180.0-FTINY)
73 >                        return(ill_vert);
74 >                v->hn2 = v->horiz * (PI/180.0);
75 >                v->vn2 = 2.0 * tan(v->vert*(PI/360.));
76 >                break;
77 >        case VT_ANG:                            /* angular fisheye */
78 >                if (v->horiz > 360.0+FTINY)
79 >                        return(ill_horiz);
80 >                if (v->vert > 360.0+FTINY)
81 >                        return(ill_vert);
82 >                v->hn2 = v->horiz * (PI/180.0);
83 >                v->vn2 = v->vert * (PI/180.0);
84 >                break;
85 >        case VT_HEM:                            /* hemispherical fisheye */
86 >                if (v->horiz > 180.0+FTINY)
87 >                        return(ill_horiz);
88 >                if (v->vert > 180.0+FTINY)
89 >                        return(ill_vert);
90 >                v->hn2 = 2.0 * sin(v->horiz*(PI/360.));
91 >                v->vn2 = 2.0 * sin(v->vert*(PI/360.));
92 >                break;
93 >        case VT_PLS:                            /* planispheric fisheye */
94 >                if (v->horiz >= 360.0-FTINY)
95 >                        return(ill_horiz);
96 >                if (v->vert >= 360.0-FTINY)
97 >                        return(ill_vert);
98 >                v->hn2 = 2.*sin(v->horiz*(PI/360.)) /
99 >                                (1.0 + cos(v->horiz*(PI/360.)));
100 >                v->vn2 = 2.*sin(v->vert*(PI/360.)) /
101 >                                (1.0 + cos(v->vert*(PI/360.)));
102 >                break;
103 >        default:
104 >                return("unknown view type");
105 >        }
106 >        if (v->type != VT_ANG && v->type != VT_PLS) {
107 >                if (v->type != VT_CYL) {
108 >                        v->hvec[0] *= v->hn2;
109 >                        v->hvec[1] *= v->hn2;
110 >                        v->hvec[2] *= v->hn2;
111 >                }
112 >                v->vvec[0] *= v->vn2;
113 >                v->vvec[1] *= v->vn2;
114 >                v->vvec[2] *= v->vn2;
115 >        }
116 >        v->hn2 *= v->hn2;
117 >        v->vn2 *= v->vn2;
118  
119 <        if (dt <= FTINY || dt >= FHUGE)
120 <                return("illegal vertical view size");
119 >        return(NULL);
120 > }
121  
74        if (v->vresolu <= 0)
75                return("illegal vertical resolution");
76        
77        dt /= (double)v->vresolu;
78        v->vvinc[0] *= dt;
79        v->vvinc[1] *= dt;
80        v->vvinc[2] *= dt;
122  
123 <        v->vhn2 = DOT(v->vhinc,v->vhinc);
124 <        v->vvn2 = DOT(v->vvinc,v->vvinc);
123 > char *
124 > cropview(                       /* crop a view to the indicated bounds */
125 > VIEW *v,
126 > double x0,
127 > double y0,
128 > double x1,
129 > double y1
130 > )
131 > {
132 >        static char     ill_crop[] = "zero crop area";
133 >        static char     ill_hemi[] = "illegal crop for hemispherical view";
134 >        double  d;
135 >                                        /* order crop extrema */
136 >        if (x0 > x1) { d=x0; x0=x1; x1=d; }
137 >        if (y0 > y1) { d=y0; y0=y1; y1=d; }
138  
139 <        return(NULL);
139 >        d = x1 - x0;
140 >        if (d == .0)
141 >                return(ill_crop);
142 >        if (!FABSEQ(d, 1.))             /* adjust horizontal size? */
143 >                switch (v->type) {
144 >                case VT_PER:
145 >                        v->horiz = 360./PI*atan( d*tan(PI/360.*v->horiz) );
146 >                        break;
147 >                case VT_PAR:
148 >                case VT_ANG:
149 >                case VT_CYL:
150 >                        v->horiz *= d;
151 >                        break;
152 >                case VT_HEM:
153 >                        d *= sin(PI/360.*v->horiz);
154 >                        if (d > 1.)
155 >                                return(ill_hemi);
156 >                        v->horiz = 360./PI*asin( d );
157 >                        break;
158 >                case VT_PLS:
159 >                        d *= sin(PI/360.*v->horiz) /
160 >                                        (1. + cos(PI/360.*v->horiz));
161 >                        v->horiz = 360./PI*acos( (1. - d*d) / (1. + d*d) );
162 >                        break;
163 >                }
164 >        d = y1 - y0;
165 >        if (d == .0)
166 >                return(ill_crop);
167 >        if (!FABSEQ(d, 1.))             /* adjust vertical size? */
168 >                switch (v->type) {
169 >                case VT_PER:
170 >                case VT_CYL:
171 >                        v->vert = 360./PI*atan( d*tan(PI/360.*v->vert) );
172 >                        break;
173 >                case VT_PAR:
174 >                case VT_ANG:
175 >                        v->vert *= d;
176 >                        break;
177 >                case VT_HEM:
178 >                        d *= sin(PI/360.*v->vert);
179 >                        if (d > 1.)
180 >                                return(ill_hemi);
181 >                        v->vert = 360./PI*asin( d );
182 >                        break;
183 >                case VT_PLS:
184 >                        d *= sin(PI/360.*v->vert) /
185 >                                        (1. + cos(PI/360.*v->vert));
186 >                        v->vert = 360./PI*acos( (1. - d*d) / (1. + d*d) );
187 >                        break;
188 >                }
189 >                                        /* fix offsets */
190 >        v->hoff = ((x0 + x1)*.5 - .5 + v->hoff) / (x1 - x0);
191 >        v->voff = ((y0 + y1)*.5 - .5 + v->voff) / (y1 - y0);
192 >
193 >        return(setview(v));             /* final error checks & set-up */
194   }
195  
196  
197 < rayview(orig, direc, v, x, y)           /* compute ray origin and direction */
198 < FVECT  orig, direc;
199 < register VIEW  *v;
200 < double  x, y;
197 > void
198 > normaspect(                             /* fix pixel aspect or resolution */
199 > double  va,                     /* view aspect ratio */
200 > double  *ap,                    /* pixel aspect in (or out if 0) */
201 > int  *xp,
202 > int  *yp                        /* x and y resolution in (or out if *ap!=0) */
203 > )
204   {
205 <        x -= 0.5 * v->hresolu;
206 <        y -= 0.5 * v->vresolu;
205 >        if (*ap <= FTINY)
206 >                *ap = va * *xp / *yp;           /* compute pixel aspect */
207 >        else if (va * *xp > *ap * *yp)
208 >                *xp = *yp / va * *ap + .5;      /* reduce x resolution */
209 >        else
210 >                *yp = *xp * va / *ap + .5;      /* reduce y resolution */
211 > }
212  
213 <        if (v->type == VT_PAR) {        /* parallel view */
214 <                orig[0] = v->vp[0] + x*v->vhinc[0] + y*v->vvinc[0];
215 <                orig[1] = v->vp[1] + x*v->vhinc[1] + y*v->vvinc[1];
216 <                orig[2] = v->vp[2] + x*v->vhinc[2] + y*v->vvinc[2];
213 >
214 > double
215 > viewray(                                /* compute ray origin and direction */
216 > FVECT  orig,
217 > FVECT  direc,
218 > VIEW  *v,
219 > double  x,
220 > double  y
221 > )
222 > {
223 >        double  d, z;
224 >        
225 >        x += v->hoff - 0.5;
226 >        y += v->voff - 0.5;
227 >
228 >        switch(v->type) {
229 >        case VT_PAR:                    /* parallel view */
230 >                orig[0] = v->vp[0] + v->vfore*v->vdir[0]
231 >                                + x*v->hvec[0] + y*v->vvec[0];
232 >                orig[1] = v->vp[1] + v->vfore*v->vdir[1]
233 >                                + x*v->hvec[1] + y*v->vvec[1];
234 >                orig[2] = v->vp[2] + v->vfore*v->vdir[2]
235 >                                + x*v->hvec[2] + y*v->vvec[2];
236                  VCOPY(direc, v->vdir);
237 <        } else {                        /* perspective view */
238 <                VCOPY(orig, v->vp);
239 <                direc[0] = v->vdir[0] + x*v->vhinc[0] + y*v->vvinc[0];
240 <                direc[1] = v->vdir[1] + x*v->vhinc[1] + y*v->vvinc[1];
241 <                direc[2] = v->vdir[2] + x*v->vhinc[2] + y*v->vvinc[2];
242 <                normalize(direc);
237 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
238 >        case VT_PER:                    /* perspective view */
239 >                direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
240 >                direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
241 >                direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
242 >                VSUM(orig, v->vp, direc, v->vfore);
243 >                d = normalize(direc);
244 >                return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
245 >        case VT_HEM:                    /* hemispherical fisheye */
246 >                z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
247 >                if (z < 0.0)
248 >                        return(-1.0);
249 >                z = sqrt(z);
250 >                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
251 >                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
252 >                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
253 >                VSUM(orig, v->vp, direc, v->vfore);
254 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
255 >        case VT_CYL:                    /* cylindrical panorama */
256 >                d = x * v->horiz * (PI/180.0);
257 >                z = cos(d);
258 >                x = sin(d);
259 >                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
260 >                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
261 >                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
262 >                VSUM(orig, v->vp, direc, v->vfore);
263 >                d = normalize(direc);
264 >                return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
265 >        case VT_ANG:                    /* angular fisheye */
266 >                x *= (1.0/180.0)*v->horiz;
267 >                y *= (1.0/180.0)*v->vert;
268 >                d = x*x + y*y;
269 >                if (d > 1.0)
270 >                        return(-1.0);
271 >                d = sqrt(d);
272 >                z = cos(PI*d);
273 >                d = d <= FTINY ? PI : sqrt(1.0 - z*z)/d;
274 >                x *= d;
275 >                y *= d;
276 >                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
277 >                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
278 >                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
279 >                VSUM(orig, v->vp, direc, v->vfore);
280 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
281 >        case VT_PLS:                    /* planispheric fisheye */
282 >                x *= sqrt(v->hn2);
283 >                y *= sqrt(v->vn2);
284 >                d = x*x + y*y;
285 >                z = (1. - d)/(1. + d);
286 >                x *= (1. + z);
287 >                y *= (1. + z);
288 >                direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0];
289 >                direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1];
290 >                direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2];
291 >                VSUM(orig, v->vp, direc, v->vfore);
292 >                return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
293          }
294 +        return(-1.0);
295   }
296  
297  
298 < pixelview(xp, yp, zp, v, p)             /* find image location for point */
299 < double  *xp, *yp, *zp;
300 < register VIEW  *v;
301 < FVECT  p;
298 > int
299 > viewloc(                        /* find image location for point */
300 > FVECT  ip,
301 > VIEW  *v,
302 > FVECT  p
303 > )       /* Use VL_* flags to interpret return value */
304   {
305 <        extern double  sqrt();
306 <        double  d;
305 >        int     rflags = VL_GOOD;
306 >        double  d, d2;
307          FVECT  disp;
120        
121        disp[0] = p[0] - v->vp[0];
122        disp[1] = p[1] - v->vp[1];
123        disp[2] = p[2] - v->vp[2];
308  
309 <        if (v->type == VT_PAR) {        /* parallel view */
310 <                if (zp != NULL)
311 <                        *zp = DOT(disp,v->vdir);
312 <        } else {                        /* perspective view */
313 <                d = 1.0/DOT(disp,v->vdir);
314 <                if (zp != NULL) {
315 <                        *zp = sqrt(DOT(disp,disp));
316 <                        if (d < 0.0)
317 <                                *zp = -*zp;
318 <                }
309 >        VSUB(disp, p, v->vp);
310 >
311 >        switch (v->type) {
312 >        case VT_PAR:                    /* parallel view */
313 >                ip[2] = DOT(disp,v->vdir) - v->vfore;
314 >                break;
315 >        case VT_PER:                    /* perspective view */
316 >                d = DOT(disp,v->vdir);
317 >                if ((v->vaft > FTINY) & (d >= v->vaft))
318 >                        rflags |= VL_BEYOND;
319 >                ip[2] = VLEN(disp);
320 >                if (d < -FTINY) {       /* fold pyramid */
321 >                        ip[2] = -ip[2];
322 >                        d = -d;
323 >                } else if (d <= FTINY)
324 >                        return(VL_BAD); /* at infinite edge */
325 >                d = 1.0/d;
326                  disp[0] *= d;
327                  disp[1] *= d;
328                  disp[2] *= d;
329 +                if (ip[2] < 0.0) d = -d;
330 +                ip[2] *= (1.0 - v->vfore*d);
331 +                break;
332 +        case VT_HEM:                    /* hemispherical fisheye */
333 +                d = normalize(disp);
334 +                if (DOT(disp,v->vdir) < 0.0)
335 +                        ip[2] = -d;
336 +                else
337 +                        ip[2] = d;
338 +                ip[2] -= v->vfore;
339 +                break;
340 +        case VT_CYL:                    /* cylindrical panorama */
341 +                d = DOT(disp,v->hvec);
342 +                d2 = DOT(disp,v->vdir);
343 +                ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff;
344 +                d2 = d*d + d2*d2;
345 +                if (d2 <= FTINY*FTINY)
346 +                        return(VL_BAD); /* at pole */
347 +                if ((v->vaft > FTINY) & (d2 >= v->vaft*v->vaft))
348 +                        rflags |= VL_BEYOND;
349 +                d = 1.0/sqrt(d2);
350 +                ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff;
351 +                ip[2] = VLEN(disp);
352 +                ip[2] *= (1.0 - v->vfore*d);
353 +                goto gotall;
354 +        case VT_ANG:                    /* angular fisheye */
355 +                ip[0] = 0.5 - v->hoff;
356 +                ip[1] = 0.5 - v->voff;
357 +                ip[2] = normalize(disp) - v->vfore;
358 +                d = DOT(disp,v->vdir);
359 +                if (d >= 1.0-FTINY)
360 +                        goto gotall;
361 +                if (d <= -(1.0-FTINY)) {
362 +                        ip[0] += 180.0/v->horiz;
363 +                        goto gotall;
364 +                }
365 +                d = (180.0/PI)*acos(d) / sqrt(1.0 - d*d);
366 +                ip[0] += DOT(disp,v->hvec)*d/v->horiz;
367 +                ip[1] += DOT(disp,v->vvec)*d/v->vert;
368 +                goto gotall;
369 +        case VT_PLS:                    /* planispheric fisheye */
370 +                ip[0] = 0.5 - v->hoff;
371 +                ip[1] = 0.5 - v->voff;
372 +                ip[2] = normalize(disp) - v->vfore;
373 +                d = DOT(disp,v->vdir);
374 +                if (d >= 1.0-FTINY)
375 +                        goto gotall;
376 +                if (d <= -(1.0-FTINY))
377 +                        return(VL_BAD);
378 +                ip[0] += DOT(disp,v->hvec)/((1. + d)*sqrt(v->hn2));
379 +                ip[1] += DOT(disp,v->vvec)/((1. + d)*sqrt(v->vn2));
380 +                goto gotall;
381 +        default:
382 +                return(VL_BAD);
383          }
384 <        *xp = DOT(disp,v->vhinc)/v->vhn2 + 0.5*v->hresolu;
385 <        *yp = DOT(disp,v->vvinc)/v->vvn2 + 0.5*v->vresolu;
384 >        ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff;
385 >        ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff;
386 > gotall:                                 /* add appropriate return flags */
387 >        if (ip[2] <= 0.0)
388 >                rflags |= VL_BEHIND;
389 >        else if ((v->type != VT_PER) & (v->type != VT_CYL))
390 >                rflags |= VL_BEYOND*((v->vaft > FTINY) &
391 >                                        (ip[2] >= v->vaft - v->vfore));
392 >        rflags |= VL_OUTSIDE*((0.0 >= ip[0]) | (ip[0] >= 1.0) |
393 >                                (0.0 >= ip[1]) | (ip[1] >= 1.0));
394 >        return(rflags);
395   }
396  
397  
398 < sscanview(vp, s)                        /* get view parameters */
399 < register VIEW  *vp;
400 < register char  *s;
398 > void
399 > pix2loc(                /* compute image location from pixel pos. */
400 > RREAL  loc[2],
401 > RESOLU  *rp,
402 > int  px,
403 > int  py
404 > )
405   {
406 <        for ( ; ; )
407 <                switch (*s++) {
408 <                case '\0':
409 <                case '\n':
406 >        int  x, y;
407 >
408 >        if (rp->rt & YMAJOR) {
409 >                x = px;
410 >                y = py;
411 >        } else {
412 >                x = py;
413 >                y = px;
414 >        }
415 >        if (rp->rt & XDECR)
416 >                x = rp->xr-1 - x;
417 >        if (rp->rt & YDECR)
418 >                y = rp->yr-1 - y;
419 >        loc[0] = (x+.5)/rp->xr;
420 >        loc[1] = (y+.5)/rp->yr;
421 > }
422 >
423 >
424 > void
425 > loc2pix(                        /* compute pixel pos. from image location */
426 > int  pp[2],
427 > RESOLU  *rp,
428 > double  lx,
429 > double  ly
430 > )
431 > {
432 >        int  x, y;
433 >
434 >        x = (int)(lx*rp->xr + .5 - (lx < 0.0));
435 >        y = (int)(ly*rp->yr + .5 - (ly < 0.0));
436 >
437 >        if (rp->rt & XDECR)
438 >                x = rp->xr-1 - x;
439 >        if (rp->rt & YDECR)
440 >                y = rp->yr-1 - y;
441 >        if (rp->rt & YMAJOR) {
442 >                pp[0] = x;
443 >                pp[1] = y;
444 >        } else {
445 >                pp[0] = y;
446 >                pp[1] = x;
447 >        }
448 > }
449 >
450 >
451 > int
452 > getviewopt(                             /* process view argument */
453 > VIEW  *v,
454 > int  ac,
455 > char  *av[]
456 > )
457 > {
458 > #define check(c,l)      if ((av[0][c]&&!isspace(av[0][c])) || \
459 >                        badarg(ac-1,av+1,l)) return(-1)
460 >
461 >        if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
462 >                return(-1);
463 >        switch (av[0][2]) {
464 >        case 't':                       /* type */
465 >                if (!av[0][3] || isspace(av[0][3]))
466 >                        return(-1);
467 >                check(4,"");
468 >                v->type = av[0][3];
469 >                return(0);
470 >        case 'p':                       /* point */
471 >                check(3,"fff");
472 >                v->vp[0] = atof(av[1]);
473 >                v->vp[1] = atof(av[2]);
474 >                v->vp[2] = atof(av[3]);
475 >                return(3);
476 >        case 'd':                       /* direction */
477 >                check(3,"fff");
478 >                v->vdir[0] = atof(av[1]);
479 >                v->vdir[1] = atof(av[2]);
480 >                v->vdir[2] = atof(av[3]);
481 >                v->vdist = 1.;
482 >                return(3);
483 >        case 'u':                       /* up */
484 >                check(3,"fff");
485 >                v->vup[0] = atof(av[1]);
486 >                v->vup[1] = atof(av[2]);
487 >                v->vup[2] = atof(av[3]);
488 >                return(3);
489 >        case 'h':                       /* horizontal size */
490 >                check(3,"f");
491 >                v->horiz = atof(av[1]);
492 >                return(1);
493 >        case 'v':                       /* vertical size */
494 >                check(3,"f");
495 >                v->vert = atof(av[1]);
496 >                return(1);
497 >        case 'o':                       /* fore clipping plane */
498 >                check(3,"f");
499 >                v->vfore = atof(av[1]);
500 >                return(1);
501 >        case 'a':                       /* aft clipping plane */
502 >                check(3,"f");
503 >                v->vaft = atof(av[1]);
504 >                return(1);
505 >        case 's':                       /* shift */
506 >                check(3,"f");
507 >                v->hoff = atof(av[1]);
508 >                return(1);
509 >        case 'l':                       /* lift */
510 >                check(3,"f");
511 >                v->voff = atof(av[1]);
512 >                return(1);
513 >        default:
514 >                return(-1);
515 >        }
516 > #undef check
517 > }
518 >
519 >
520 > int
521 > sscanview(                              /* get view parameters from string */
522 > VIEW  *vp,
523 > char  *s
524 > )
525 > {
526 >        int  ac;
527 >        char  *av[4];
528 >        int  na;
529 >        int  nvopts = 0;
530 >
531 >        while (isspace(*s))
532 >                if (!*s++)
533                          return(0);
534 <                case '-':
535 <                        switch (*s++) {
536 <                        case '\0':
537 <                                return(-1);
538 <                        case 'x':
539 <                                if (sscanf(s, "%d", &vp->hresolu) != 1)
540 <                                        return(-1);
541 <                                s = sskip(s);
542 <                                continue;
543 <                        case 'y':
544 <                                if (sscanf(s, "%d", &vp->vresolu) != 1)
545 <                                        return(-1);
546 <                                s = sskip(s);
547 <                                continue;
548 <                        case 'v':
549 <                                switch (*s++) {
550 <                                case '\0':
551 <                                        return(-1);
171 <                                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 <                }
534 >        while (*s) {
535 >                ac = 0;
536 >                do {
537 >                        if (ac || *s == '-')
538 >                                av[ac++] = s;
539 >                        while (*s && !isspace(*s))
540 >                                s++;
541 >                        while (isspace(*s))
542 >                                s++;
543 >                } while (*s && ac < 4);
544 >                if ((na = getviewopt(vp, ac, av)) >= 0) {
545 >                        if (na+1 < ac)
546 >                                s = av[na+1];
547 >                        nvopts++;
548 >                } else if (ac > 1)
549 >                        s = av[1];
550 >        }
551 >        return(nvopts);
552   }
553  
554  
555 < fprintview(vp, fp)                      /* write out view parameters */
556 < register VIEW  *vp;
557 < FILE  *fp;
555 > void
556 > fprintview(                             /* write out view parameters */
557 > VIEW  *vp,
558 > FILE  *fp
559 > )
560   {
561          fprintf(fp, " -vt%c", vp->type);
562          fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]);
563 <        fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]);
563 >        fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0]*vp->vdist,
564 >                                                vp->vdir[1]*vp->vdist,
565 >                                                vp->vdir[2]*vp->vdist);
566          fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]);
567          fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert);
568 <        fprintf(fp, " -x %d -y %d", vp->hresolu, vp->vresolu);
568 >        fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
569 >        fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
570   }
571  
572  
573 < static VIEW  *hview;                    /* view from header */
574 < static int  gothview;                   /* success indicator */
575 < static char  *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL};
573 > char *
574 > viewopt(                                /* translate to minimal view string */
575 > VIEW  *vp
576 > )
577 > {
578 >        static char  vwstr[128];
579 >        char  *cp = vwstr;
580  
581 +        *cp = '\0';
582 +        if (vp->type != stdview.type) {
583 +                sprintf(cp, " -vt%c", vp->type);
584 +                cp += strlen(cp);
585 +        }
586 +        if (!VABSEQ(vp->vp,stdview.vp)) {
587 +                sprintf(cp, " -vp %.6g %.6g %.6g",
588 +                                vp->vp[0], vp->vp[1], vp->vp[2]);
589 +                cp += strlen(cp);
590 +        }
591 +        if (!FABSEQ(vp->vdist,stdview.vdist) || !VABSEQ(vp->vdir,stdview.vdir)) {
592 +                sprintf(cp, " -vd %.6g %.6g %.6g",
593 +                                vp->vdir[0]*vp->vdist,
594 +                                vp->vdir[1]*vp->vdist,
595 +                                vp->vdir[2]*vp->vdist);
596 +                cp += strlen(cp);
597 +        }
598 +        if (!VABSEQ(vp->vup,stdview.vup)) {
599 +                sprintf(cp, " -vu %.6g %.6g %.6g",
600 +                                vp->vup[0], vp->vup[1], vp->vup[2]);
601 +                cp += strlen(cp);
602 +        }
603 +        if (!FABSEQ(vp->horiz,stdview.horiz)) {
604 +                sprintf(cp, " -vh %.6g", vp->horiz);
605 +                cp += strlen(cp);
606 +        }
607 +        if (!FABSEQ(vp->vert,stdview.vert)) {
608 +                sprintf(cp, " -vv %.6g", vp->vert);
609 +                cp += strlen(cp);
610 +        }
611 +        if (!FABSEQ(vp->vfore,stdview.vfore)) {
612 +                sprintf(cp, " -vo %.6g", vp->vfore);
613 +                cp += strlen(cp);
614 +        }
615 +        if (!FABSEQ(vp->vaft,stdview.vaft)) {
616 +                sprintf(cp, " -va %.6g", vp->vaft);
617 +                cp += strlen(cp);
618 +        }
619 +        if (!FABSEQ(vp->hoff,stdview.hoff)) {
620 +                sprintf(cp, " -vs %.6g", vp->hoff);
621 +                cp += strlen(cp);
622 +        }
623 +        if (!FABSEQ(vp->voff,stdview.voff)) {
624 +                sprintf(cp, " -vl %.6g", vp->voff);
625 +                cp += strlen(cp);
626 +        }
627 +        return(vwstr);
628 + }
629  
236 static
237 gethview(s)                             /* get view from header */
238 char  *s;
239 {
240        register char  **an;
630  
631 + int
632 + isview(                                 /* is this a view string? */
633 + char  *s
634 + )
635 + {
636 +        static char  *altname[]={NULL,VIEWSTR,"rpict","rview","rvu","rpiece","pinterp",NULL};
637 +        extern char  *progname;
638 +        char  *cp;
639 +        char  **an;
640 +                                        /* add program name to list */
641 +        if (altname[0] == NULL) {
642 +                for (cp = progname; *cp; cp++)
643 +                        ;
644 +                while (cp > progname && !ISDIRSEP(cp[-1]))
645 +                        cp--;
646 +                altname[0] = cp;
647 +        }
648 +                                        /* skip leading path */
649 +        cp = s;
650 +        while (*cp && !isspace(*cp))
651 +                cp++;
652 +        while (cp > s && !ISDIRSEP(cp[-1]))
653 +                cp--;
654          for (an = altname; *an != NULL; an++)
655 <                if (!strncmp(*an, s, strlen(*an))) {
656 <                        if (sscanview(hview, s+strlen(*an)) == 0)
657 <                                gothview++;
246 <                        return;
247 <                }
655 >                if (!strncmp(*an, cp, strlen(*an)))
656 >                        return(1);
657 >        return(0);
658   }
659  
660  
661 + struct myview {
662 +        VIEW    *hv;
663 +        int     ok;
664 + };
665 +
666 +
667 + static int
668 + gethview(                               /* get view from header */
669 +        char  *s,
670 +        void  *v
671 + )
672 + {
673 +        if (isview(s) && sscanview(((struct myview*)v)->hv, s) > 0)
674 +                ((struct myview*)v)->ok++;
675 +        return(0);
676 + }
677 +
678 +
679   int
680 < viewfile(fname, vp)                     /* get view from file */
681 < char  *fname;
682 < VIEW  *vp;
680 > viewfile(                               /* get view from file */
681 > char  *fname,
682 > VIEW  *vp,
683 > RESOLU  *rp
684 > )
685   {
686 <        extern char  *progname;
686 >        struct myview   mvs;
687          FILE  *fp;
688  
689 <        if ((fp = fopen(fname, "r")) == NULL)
689 >        if (fname == NULL || !strcmp(fname, "-"))
690 >                fp = stdin;
691 >        else if ((fp = fopen(fname, "r")) == NULL)
692                  return(-1);
693  
694 <        altname[0] = progname;
695 <        hview = vp;
264 <        gothview = 0;
694 >        mvs.hv = vp;
695 >        mvs.ok = 0;
696  
697 <        getheader(fp, gethview);
697 >        getheader(fp, gethview, &mvs);
698  
699 <        fclose(fp);
699 >        if (rp != NULL && !fgetsresolu(rp, fp))
700 >                mvs.ok = 0;
701  
702 <        return(gothview);
702 >        if (fp != stdin)
703 >                fclose(fp);
704 >
705 >        return(mvs.ok);
706   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines