ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/image.c
Revision: 2.53
Committed: Wed Jan 12 21:07:39 2022 UTC (2 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.52: +84 -10 lines
Log Message:
refactor: created cropview() function to manage view sections

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.53 static const char RCSid[] = "$Id: image.c,v 2.52 2021/02/12 00:47:08 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * image.c - routines for image generation.
6 greg 2.16 *
7     * External symbols declared in view.h
8     */
9    
10 greg 2.17 #include "copyright.h"
11 greg 1.1
12 greg 2.32 #include <ctype.h>
13 schorsch 2.22 #include "rtio.h"
14 greg 2.28 #include "rtmath.h"
15 greg 2.27 #include "paths.h"
16 greg 1.1 #include "view.h"
17    
18 greg 1.5 VIEW stdview = STDVIEW; /* default view parameters */
19 greg 1.1
20 schorsch 2.26 static gethfunc gethview;
21    
22 greg 1.1
23     char *
24 greg 2.35 setview( /* set hvec and vvec, return message on error */
25     VIEW *v
26     )
27 greg 1.3 {
28 greg 1.12 static char ill_horiz[] = "illegal horizontal view size";
29     static char ill_vert[] = "illegal vertical view size";
30    
31 greg 2.39 if ((v->vfore < -FTINY) | (v->vaft < -FTINY) ||
32     (v->vaft > FTINY) & (v->vaft <= v->vfore))
33 greg 2.7 return("illegal fore/aft clipping plane");
34    
35 greg 2.31 if (v->vdist <= FTINY)
36     return("illegal view distance");
37 greg 2.29 v->vdist *= normalize(v->vdir); /* normalize direction */
38     if (v->vdist == 0.0)
39 greg 1.1 return("zero view direction");
40    
41 greg 1.14 if (normalize(v->vup) == 0.0) /* normalize view up */
42     return("zero view up vector");
43    
44 greg 1.5 fcross(v->hvec, v->vdir, v->vup); /* compute horiz dir */
45    
46     if (normalize(v->hvec) == 0.0)
47 greg 1.14 return("view up parallel to view direction");
48 greg 1.1
49 greg 1.5 fcross(v->vvec, v->hvec, v->vdir); /* compute vert dir */
50    
51 greg 1.12 if (v->horiz <= FTINY)
52     return(ill_horiz);
53     if (v->vert <= FTINY)
54     return(ill_vert);
55    
56     switch (v->type) {
57     case VT_PAR: /* parallel view */
58 greg 1.5 v->hn2 = v->horiz;
59 greg 1.12 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 greg 2.53 v->hn2 = 2.0 * tan(v->horiz*(PI/360.));
67     v->vn2 = 2.0 * tan(v->vert*(PI/360.));
68 greg 1.12 break;
69 greg 2.9 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 greg 2.53 v->vn2 = 2.0 * tan(v->vert*(PI/360.));
76 greg 2.9 break;
77 greg 1.12 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 greg 2.9 v->hn2 = v->horiz * (PI/180.0);
83     v->vn2 = v->vert * (PI/180.0);
84 greg 1.12 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 greg 2.53 v->hn2 = 2.0 * sin(v->horiz*(PI/360.));
91     v->vn2 = 2.0 * sin(v->vert*(PI/360.));
92 greg 1.12 break;
93 greg 2.34 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 greg 2.53 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 greg 2.34 break;
103 greg 1.12 default:
104 greg 1.1 return("unknown view type");
105 greg 1.12 }
106 greg 2.34 if (v->type != VT_ANG && v->type != VT_PLS) {
107 greg 2.9 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 greg 1.12 v->vvec[0] *= v->vn2;
113     v->vvec[1] *= v->vn2;
114     v->vvec[2] *= v->vn2;
115     }
116 greg 1.5 v->hn2 *= v->hn2;
117     v->vn2 *= v->vn2;
118 greg 1.1
119     return(NULL);
120     }
121    
122    
123 greg 2.53 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     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 greg 2.16 void
198 greg 2.35 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 greg 1.6 {
205     if (*ap <= FTINY)
206 greg 1.7 *ap = va * *xp / *yp; /* compute pixel aspect */
207 greg 1.6 else if (va * *xp > *ap * *yp)
208 greg 1.10 *xp = *yp / va * *ap + .5; /* reduce x resolution */
209 greg 1.6 else
210 greg 1.10 *yp = *xp * va / *ap + .5; /* reduce y resolution */
211 greg 1.6 }
212    
213    
214 greg 2.7 double
215 greg 2.35 viewray( /* compute ray origin and direction */
216     FVECT orig,
217     FVECT direc,
218     VIEW *v,
219     double x,
220     double y
221     )
222 greg 1.1 {
223 greg 1.12 double d, z;
224    
225 greg 1.5 x += v->hoff - 0.5;
226     y += v->voff - 0.5;
227 greg 1.1
228 greg 1.12 switch(v->type) {
229     case VT_PAR: /* parallel view */
230 greg 2.7 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 greg 1.1 VCOPY(direc, v->vdir);
237 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
238 greg 1.12 case VT_PER: /* perspective view */
239 greg 1.5 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 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
243 greg 2.7 d = normalize(direc);
244 greg 2.8 return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
245 greg 1.12 case VT_HEM: /* hemispherical fisheye */
246 greg 1.13 z = 1.0 - x*x*v->hn2 - y*y*v->vn2;
247 greg 1.12 if (z < 0.0)
248 greg 2.7 return(-1.0);
249 greg 1.12 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 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
254 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
255 greg 2.9 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 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
263 greg 2.9 d = normalize(direc);
264     return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0);
265 greg 1.12 case VT_ANG: /* angular fisheye */
266 greg 2.34 x *= (1.0/180.0)*v->horiz;
267     y *= (1.0/180.0)*v->vert;
268 greg 1.12 d = x*x + y*y;
269     if (d > 1.0)
270 greg 2.7 return(-1.0);
271 greg 1.12 d = sqrt(d);
272     z = cos(PI*d);
273 greg 2.34 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 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
280 greg 2.34 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 greg 2.38 x *= (1. + z);
287     y *= (1. + z);
288 greg 1.12 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 greg 2.40 VSUM(orig, v->vp, direc, v->vfore);
292 greg 2.8 return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0);
293 greg 1.1 }
294 greg 2.7 return(-1.0);
295 greg 1.1 }
296    
297    
298 greg 2.41 int
299 greg 2.35 viewloc( /* find image location for point */
300     FVECT ip,
301     VIEW *v,
302     FVECT p
303 greg 2.46 ) /* Use VL_* flags to interpret return value */
304 greg 1.3 {
305 greg 2.46 int rflags = VL_GOOD;
306 gregl 2.13 double d, d2;
307 greg 1.3 FVECT disp;
308 greg 1.5
309 greg 2.34 VSUB(disp, p, v->vp);
310 greg 1.3
311 greg 1.12 switch (v->type) {
312     case VT_PAR: /* parallel view */
313 greg 2.7 ip[2] = DOT(disp,v->vdir) - v->vfore;
314 greg 1.13 break;
315 greg 1.12 case VT_PER: /* perspective view */
316 greg 1.11 d = DOT(disp,v->vdir);
317 greg 2.47 if ((v->vaft > FTINY) & (d >= v->vaft))
318 greg 2.46 rflags |= VL_BEYOND;
319 gregl 2.13 ip[2] = VLEN(disp);
320 greg 2.42 if (d < -FTINY) { /* fold pyramid */
321 greg 1.17 ip[2] = -ip[2];
322 greg 1.11 d = -d;
323 greg 2.42 } else if (d <= FTINY)
324 greg 2.46 return(VL_BAD); /* at infinite edge */
325 greg 2.41 d = 1.0/d;
326     disp[0] *= d;
327     disp[1] *= d;
328     disp[2] *= d;
329 greg 2.42 if (ip[2] < 0.0) d = -d;
330 greg 2.7 ip[2] *= (1.0 - v->vfore*d);
331 greg 1.13 break;
332 greg 1.12 case VT_HEM: /* hemispherical fisheye */
333     d = normalize(disp);
334 greg 1.17 if (DOT(disp,v->vdir) < 0.0)
335     ip[2] = -d;
336     else
337     ip[2] = d;
338 greg 2.7 ip[2] -= v->vfore;
339 greg 1.13 break;
340 greg 2.9 case VT_CYL: /* cylindrical panorama */
341     d = DOT(disp,v->hvec);
342 gregl 2.13 d2 = DOT(disp,v->vdir);
343     ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff;
344 greg 2.51 d2 = d*d + d2*d2;
345     if (d2 <= FTINY*FTINY)
346 greg 2.46 return(VL_BAD); /* at pole */
347 greg 2.51 if ((v->vaft > FTINY) & (d2 >= v->vaft*v->vaft))
348 greg 2.46 rflags |= VL_BEYOND;
349 greg 2.51 d = 1.0/sqrt(d2);
350 gregl 2.13 ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff;
351     ip[2] = VLEN(disp);
352 gwlarson 2.15 ip[2] *= (1.0 - v->vfore*d);
353 greg 2.42 goto gotall;
354 greg 1.12 case VT_ANG: /* angular fisheye */
355 greg 1.17 ip[0] = 0.5 - v->hoff;
356     ip[1] = 0.5 - v->voff;
357 greg 2.7 ip[2] = normalize(disp) - v->vfore;
358 greg 1.12 d = DOT(disp,v->vdir);
359     if (d >= 1.0-FTINY)
360 greg 2.42 goto gotall;
361 greg 1.12 if (d <= -(1.0-FTINY)) {
362 greg 2.7 ip[0] += 180.0/v->horiz;
363 greg 2.42 goto gotall;
364 greg 1.12 }
365 greg 2.34 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 greg 2.42 goto gotall;
369 greg 2.34 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 greg 2.42 goto gotall;
376 greg 2.34 if (d <= -(1.0-FTINY))
377 greg 2.46 return(VL_BAD);
378 greg 2.38 ip[0] += DOT(disp,v->hvec)/((1. + d)*sqrt(v->hn2));
379     ip[1] += DOT(disp,v->vvec)/((1. + d)*sqrt(v->vn2));
380 greg 2.42 goto gotall;
381 greg 2.45 default:
382 greg 2.46 return(VL_BAD);
383 greg 1.3 }
384 greg 1.17 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 greg 2.46 gotall: /* add appropriate return flags */
387 greg 2.48 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 greg 2.46 rflags |= VL_OUTSIDE*((0.0 >= ip[0]) | (ip[0] >= 1.0) |
393     (0.0 >= ip[1]) | (ip[1] >= 1.0));
394     return(rflags);
395 greg 1.3 }
396    
397    
398 greg 2.16 void
399 greg 2.35 pix2loc( /* compute image location from pixel pos. */
400     RREAL loc[2],
401     RESOLU *rp,
402     int px,
403     int py
404     )
405 greg 1.17 {
406 greg 2.36 int x, y;
407 greg 1.17
408 greg 2.16 if (rp->rt & YMAJOR) {
409 greg 1.17 x = px;
410     y = py;
411     } else {
412     x = py;
413     y = px;
414     }
415 greg 2.16 if (rp->rt & XDECR)
416 greg 1.17 x = rp->xr-1 - x;
417 greg 2.16 if (rp->rt & YDECR)
418 greg 1.17 y = rp->yr-1 - y;
419     loc[0] = (x+.5)/rp->xr;
420     loc[1] = (y+.5)/rp->yr;
421     }
422    
423    
424 greg 2.16 void
425 greg 2.35 loc2pix( /* compute pixel pos. from image location */
426     int pp[2],
427     RESOLU *rp,
428     double lx,
429     double ly
430     )
431 greg 1.17 {
432 greg 2.36 int x, y;
433 greg 1.17
434 greg 2.37 x = (int)(lx*rp->xr + .5 - (lx < 0.0));
435     y = (int)(ly*rp->yr + .5 - (ly < 0.0));
436    
437 greg 2.16 if (rp->rt & XDECR)
438 greg 1.17 x = rp->xr-1 - x;
439 greg 2.16 if (rp->rt & YDECR)
440 greg 1.17 y = rp->yr-1 - y;
441 greg 2.16 if (rp->rt & YMAJOR) {
442 greg 1.17 pp[0] = x;
443     pp[1] = y;
444     } else {
445     pp[0] = y;
446     pp[1] = x;
447     }
448     }
449    
450    
451 greg 1.5 int
452 greg 2.35 getviewopt( /* process view argument */
453     VIEW *v,
454     int ac,
455     char *av[]
456     )
457 greg 1.5 {
458 greg 2.49 #define check(c,l) if ((av[0][c]&&!isspace(av[0][c])) || \
459 greg 1.16 badarg(ac-1,av+1,l)) return(-1)
460 greg 1.5
461 greg 1.9 if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v')
462 greg 1.5 return(-1);
463     switch (av[0][2]) {
464     case 't': /* type */
465 greg 2.49 if (!av[0][3] || isspace(av[0][3]))
466 greg 1.5 return(-1);
467 greg 1.16 check(4,"");
468 greg 1.5 v->type = av[0][3];
469     return(0);
470     case 'p': /* point */
471 greg 1.16 check(3,"fff");
472 greg 1.5 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 greg 1.16 check(3,"fff");
478 greg 1.5 v->vdir[0] = atof(av[1]);
479     v->vdir[1] = atof(av[2]);
480     v->vdir[2] = atof(av[3]);
481 greg 2.30 v->vdist = 1.;
482 greg 1.5 return(3);
483     case 'u': /* up */
484 greg 1.16 check(3,"fff");
485 greg 1.5 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 greg 1.16 check(3,"f");
491 greg 1.5 v->horiz = atof(av[1]);
492     return(1);
493     case 'v': /* vertical size */
494 greg 1.16 check(3,"f");
495 greg 1.5 v->vert = atof(av[1]);
496     return(1);
497 greg 2.7 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 greg 1.5 case 's': /* shift */
506 greg 1.16 check(3,"f");
507 greg 1.5 v->hoff = atof(av[1]);
508     return(1);
509     case 'l': /* lift */
510 greg 1.16 check(3,"f");
511 greg 1.5 v->voff = atof(av[1]);
512     return(1);
513     default:
514     return(-1);
515     }
516     #undef check
517     }
518    
519    
520     int
521 greg 2.35 sscanview( /* get view parameters from string */
522     VIEW *vp,
523     char *s
524     )
525 greg 1.1 {
526 greg 1.5 int ac;
527     char *av[4];
528     int na;
529     int nvopts = 0;
530    
531 greg 2.32 while (isspace(*s))
532     if (!*s++)
533     return(0);
534 greg 1.9 while (*s) {
535 greg 1.5 ac = 0;
536     do {
537 greg 2.21 if (ac || *s == '-')
538     av[ac++] = s;
539 greg 2.32 while (*s && !isspace(*s))
540 greg 1.5 s++;
541 greg 2.32 while (isspace(*s))
542 greg 1.5 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 greg 1.9 } else if (ac > 1)
549     s = av[1];
550     }
551 greg 1.5 return(nvopts);
552 greg 1.1 }
553    
554    
555 greg 2.16 void
556 greg 2.35 fprintview( /* write out view parameters */
557     VIEW *vp,
558     FILE *fp
559     )
560 greg 1.1 {
561     fprintf(fp, " -vt%c", vp->type);
562     fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]);
563 greg 2.29 fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0]*vp->vdist,
564     vp->vdir[1]*vp->vdist,
565     vp->vdir[2]*vp->vdist);
566 greg 1.1 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 greg 2.7 fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft);
569 greg 1.9 fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff);
570 greg 2.11 }
571    
572    
573     char *
574 greg 2.35 viewopt( /* translate to minimal view string */
575     VIEW *vp
576     )
577 greg 2.11 {
578     static char vwstr[128];
579 greg 2.36 char *cp = vwstr;
580 greg 2.11
581 greg 2.33 *cp = '\0';
582 greg 2.11 if (vp->type != stdview.type) {
583     sprintf(cp, " -vt%c", vp->type);
584     cp += strlen(cp);
585     }
586 greg 2.52 if (!VABSEQ(vp->vp,stdview.vp)) {
587 greg 2.11 sprintf(cp, " -vp %.6g %.6g %.6g",
588     vp->vp[0], vp->vp[1], vp->vp[2]);
589     cp += strlen(cp);
590     }
591 greg 2.52 if (!FABSEQ(vp->vdist,stdview.vdist) || !VABSEQ(vp->vdir,stdview.vdir)) {
592 greg 2.11 sprintf(cp, " -vd %.6g %.6g %.6g",
593 greg 2.29 vp->vdir[0]*vp->vdist,
594     vp->vdir[1]*vp->vdist,
595     vp->vdir[2]*vp->vdist);
596 greg 2.11 cp += strlen(cp);
597     }
598 greg 2.52 if (!VABSEQ(vp->vup,stdview.vup)) {
599 greg 2.11 sprintf(cp, " -vu %.6g %.6g %.6g",
600     vp->vup[0], vp->vup[1], vp->vup[2]);
601     cp += strlen(cp);
602     }
603 greg 2.52 if (!FABSEQ(vp->horiz,stdview.horiz)) {
604 greg 2.11 sprintf(cp, " -vh %.6g", vp->horiz);
605     cp += strlen(cp);
606     }
607 greg 2.52 if (!FABSEQ(vp->vert,stdview.vert)) {
608 greg 2.11 sprintf(cp, " -vv %.6g", vp->vert);
609     cp += strlen(cp);
610     }
611 greg 2.52 if (!FABSEQ(vp->vfore,stdview.vfore)) {
612 greg 2.11 sprintf(cp, " -vo %.6g", vp->vfore);
613     cp += strlen(cp);
614     }
615 greg 2.52 if (!FABSEQ(vp->vaft,stdview.vaft)) {
616 greg 2.11 sprintf(cp, " -va %.6g", vp->vaft);
617     cp += strlen(cp);
618     }
619 greg 2.52 if (!FABSEQ(vp->hoff,stdview.hoff)) {
620 greg 2.11 sprintf(cp, " -vs %.6g", vp->hoff);
621     cp += strlen(cp);
622     }
623 greg 2.52 if (!FABSEQ(vp->voff,stdview.voff)) {
624 greg 2.11 sprintf(cp, " -vl %.6g", vp->voff);
625     cp += strlen(cp);
626     }
627     return(vwstr);
628 greg 1.1 }
629    
630    
631 greg 2.3 int
632 greg 2.35 isview( /* is this a view string? */
633     char *s
634     )
635 greg 2.3 {
636 greg 2.25 static char *altname[]={NULL,VIEWSTR,"rpict","rview","rvu","rpiece","pinterp",NULL};
637 greg 2.5 extern char *progname;
638 greg 2.36 char *cp;
639     char **an;
640 greg 2.3 /* add program name to list */
641 greg 2.5 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 greg 2.3 /* skip leading path */
649     cp = s;
650 greg 2.49 while (*cp && !isspace(*cp))
651 greg 2.3 cp++;
652 greg 2.5 while (cp > s && !ISDIRSEP(cp[-1]))
653 greg 2.3 cp--;
654     for (an = altname; *an != NULL; an++)
655     if (!strncmp(*an, cp, strlen(*an)))
656     return(1);
657     return(0);
658     }
659 greg 1.1
660 greg 2.3
661 greg 1.15 struct myview {
662     VIEW *hv;
663     int ok;
664     };
665 greg 1.1
666 greg 1.15
667 gwlarson 2.14 static int
668 schorsch 2.26 gethview( /* get view from header */
669     char *s,
670     void *v
671     )
672 greg 1.1 {
673 schorsch 2.26 if (isview(s) && sscanview(((struct myview*)v)->hv, s) > 0)
674     ((struct myview*)v)->ok++;
675 gwlarson 2.14 return(0);
676 greg 1.1 }
677    
678    
679     int
680 greg 2.35 viewfile( /* get view from file */
681     char *fname,
682     VIEW *vp,
683     RESOLU *rp
684     )
685 greg 1.1 {
686 greg 1.15 struct myview mvs;
687 greg 1.1 FILE *fp;
688    
689 greg 2.16 if (fname == NULL || !strcmp(fname, "-"))
690     fp = stdin;
691     else if ((fp = fopen(fname, "r")) == NULL)
692 greg 1.1 return(-1);
693    
694 greg 1.15 mvs.hv = vp;
695     mvs.ok = 0;
696 greg 1.1
697 schorsch 2.26 getheader(fp, gethview, &mvs);
698 greg 1.8
699 greg 1.17 if (rp != NULL && !fgetsresolu(rp, fp))
700 greg 1.15 mvs.ok = 0;
701 greg 1.1
702 greg 2.50 if (fp != stdin)
703     fclose(fp);
704 greg 1.1
705 greg 1.15 return(mvs.ok);
706 greg 1.1 }