1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
14 |
|
|
15 |
|
#include "view.h" |
16 |
|
|
17 |
+ |
#include "resolu.h" |
18 |
+ |
|
19 |
+ |
#include "paths.h" |
20 |
+ |
|
21 |
|
VIEW stdview = STDVIEW; /* default view parameters */ |
22 |
|
|
23 |
|
|
31 |
|
if (normalize(v->vdir) == 0.0) /* normalize direction */ |
32 |
|
return("zero view direction"); |
33 |
|
|
34 |
+ |
if (normalize(v->vup) == 0.0) /* normalize view up */ |
35 |
+ |
return("zero view up vector"); |
36 |
+ |
|
37 |
|
fcross(v->hvec, v->vdir, v->vup); /* compute horiz dir */ |
38 |
|
|
39 |
|
if (normalize(v->hvec) == 0.0) |
40 |
< |
return("illegal view up vector"); |
40 |
> |
return("view up parallel to view direction"); |
41 |
|
|
42 |
|
fcross(v->vvec, v->hvec, v->vdir); /* compute vert dir */ |
43 |
|
|
78 |
|
default: |
79 |
|
return("unknown view type"); |
80 |
|
} |
81 |
< |
if (v->type == VT_PAR || v->type == VT_PER) { |
81 |
> |
if (v->type != VT_ANG) { |
82 |
|
v->hvec[0] *= v->hn2; |
83 |
|
v->hvec[1] *= v->hn2; |
84 |
|
v->hvec[2] *= v->hn2; |
132 |
|
normalize(direc); |
133 |
|
return(0); |
134 |
|
case VT_HEM: /* hemispherical fisheye */ |
135 |
< |
x *= v->horiz/90.0; |
129 |
< |
y *= v->vert/90.0; |
130 |
< |
z = 1.0 - x*x - y*y; |
135 |
> |
z = 1.0 - x*x*v->hn2 - y*y*v->vn2; |
136 |
|
if (z < 0.0) |
137 |
|
return(-1); |
138 |
|
z = sqrt(z); |
148 |
|
if (d > 1.0) |
149 |
|
return(-1); |
150 |
|
VCOPY(orig, v->vp); |
146 |
– |
if (d <= FTINY) { |
147 |
– |
VCOPY(direc, v->vdir); |
148 |
– |
return(0); |
149 |
– |
} |
151 |
|
d = sqrt(d); |
152 |
|
z = cos(PI*d); |
153 |
< |
d = sqrt(1 - z*z)/d; |
153 |
> |
d = d <= FTINY ? PI : sqrt(1 - z*z)/d; |
154 |
|
x *= d; |
155 |
|
y *= d; |
156 |
|
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
< |
viewpixel(xp, yp, zp, v, p) /* find image location for point */ |
166 |
< |
double *xp, *yp, *zp; |
165 |
> |
viewloc(ip, v, p) /* find image location for point */ |
166 |
> |
FVECT ip; |
167 |
|
register VIEW *v; |
168 |
|
FVECT p; |
169 |
|
{ |
176 |
|
|
177 |
|
switch (v->type) { |
178 |
|
case VT_PAR: /* parallel view */ |
179 |
< |
if (zp != NULL) |
180 |
< |
*zp = DOT(disp,v->vdir); |
180 |
< |
*xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff; |
181 |
< |
*yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff; |
182 |
< |
return; |
179 |
> |
ip[2] = DOT(disp,v->vdir); |
180 |
> |
break; |
181 |
|
case VT_PER: /* perspective view */ |
182 |
|
d = DOT(disp,v->vdir); |
183 |
< |
if (zp != NULL) { |
184 |
< |
*zp = sqrt(DOT(disp,disp)); |
185 |
< |
if (d < 0.0) |
188 |
< |
*zp = -*zp; |
189 |
< |
} |
190 |
< |
if (d < 0.0) /* fold pyramid */ |
183 |
> |
ip[2] = sqrt(DOT(disp,disp)); |
184 |
> |
if (d < 0.0) { /* fold pyramid */ |
185 |
> |
ip[2] = -ip[2]; |
186 |
|
d = -d; |
187 |
< |
if (d > FTINY) { |
187 |
> |
} else if (d > FTINY) { |
188 |
|
d = 1.0/d; |
189 |
|
disp[0] *= d; |
190 |
|
disp[1] *= d; |
191 |
|
disp[2] *= d; |
192 |
|
} |
193 |
< |
*xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff; |
199 |
< |
*yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff; |
200 |
< |
return; |
193 |
> |
break; |
194 |
|
case VT_HEM: /* hemispherical fisheye */ |
195 |
|
d = normalize(disp); |
196 |
< |
if (zp != NULL) { |
197 |
< |
if (DOT(disp,v->vdir) < 0.0) |
198 |
< |
*zp = -d; |
199 |
< |
else |
200 |
< |
*zp = d; |
208 |
< |
} |
209 |
< |
*xp = DOT(disp,v->hvec)*90.0/v->horiz + 0.5 - v->hoff; |
210 |
< |
*yp = DOT(disp,v->vvec)*90.0/v->vert + 0.5 - v->voff; |
211 |
< |
return; |
196 |
> |
if (DOT(disp,v->vdir) < 0.0) |
197 |
> |
ip[2] = -d; |
198 |
> |
else |
199 |
> |
ip[2] = d; |
200 |
> |
break; |
201 |
|
case VT_ANG: /* angular fisheye */ |
202 |
< |
d = normalize(disp); |
203 |
< |
if (zp != NULL) |
204 |
< |
*zp = d; |
216 |
< |
*xp = 0.5 - v->hoff; |
217 |
< |
*yp = 0.5 - v->voff; |
202 |
> |
ip[0] = 0.5 - v->hoff; |
203 |
> |
ip[1] = 0.5 - v->voff; |
204 |
> |
ip[2] = normalize(disp); |
205 |
|
d = DOT(disp,v->vdir); |
206 |
|
if (d >= 1.0-FTINY) |
207 |
|
return; |
208 |
|
if (d <= -(1.0-FTINY)) { |
209 |
< |
*xp += 180.0/v->horiz; |
210 |
< |
*yp += 180.0/v->vert; |
209 |
> |
ip[1] += 180.0/v->horiz; |
210 |
> |
ip[2] += 180.0/v->vert; |
211 |
|
return; |
212 |
|
} |
213 |
|
d = acos(d)/PI / sqrt(1.0 - d*d); |
214 |
< |
*xp += DOT(disp,v->hvec)*d*180.0/v->horiz; |
215 |
< |
*yp += DOT(disp,v->vvec)*d*180.0/v->vert; |
214 |
> |
ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz; |
215 |
> |
ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert; |
216 |
|
return; |
217 |
|
} |
218 |
+ |
ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff; |
219 |
+ |
ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff; |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
+ |
pix2loc(loc, rp, px, py) /* compute image location from pixel pos. */ |
224 |
+ |
FLOAT loc[2]; |
225 |
+ |
register RESOLU *rp; |
226 |
+ |
int px, py; |
227 |
+ |
{ |
228 |
+ |
register int x, y; |
229 |
+ |
|
230 |
+ |
if (rp->or & YMAJOR) { |
231 |
+ |
x = px; |
232 |
+ |
y = py; |
233 |
+ |
} else { |
234 |
+ |
x = py; |
235 |
+ |
y = px; |
236 |
+ |
} |
237 |
+ |
if (rp->or & XDECR) |
238 |
+ |
x = rp->xr-1 - x; |
239 |
+ |
if (rp->or & YDECR) |
240 |
+ |
y = rp->yr-1 - y; |
241 |
+ |
loc[0] = (x+.5)/rp->xr; |
242 |
+ |
loc[1] = (y+.5)/rp->yr; |
243 |
+ |
} |
244 |
+ |
|
245 |
+ |
|
246 |
+ |
loc2pix(pp, rp, lx, ly) /* compute pixel pos. from image location */ |
247 |
+ |
int pp[2]; |
248 |
+ |
register RESOLU *rp; |
249 |
+ |
double lx, ly; |
250 |
+ |
{ |
251 |
+ |
register int x, y; |
252 |
+ |
|
253 |
+ |
x = lx * rp->xr; |
254 |
+ |
y = ly * rp->yr; |
255 |
+ |
if (rp->or & XDECR) |
256 |
+ |
x = rp->xr-1 - x; |
257 |
+ |
if (rp->or & YDECR) |
258 |
+ |
y = rp->yr-1 - y; |
259 |
+ |
if (rp->or & YMAJOR) { |
260 |
+ |
pp[0] = x; |
261 |
+ |
pp[1] = y; |
262 |
+ |
} else { |
263 |
+ |
pp[0] = y; |
264 |
+ |
pp[1] = x; |
265 |
+ |
} |
266 |
+ |
} |
267 |
+ |
|
268 |
+ |
|
269 |
|
int |
270 |
|
getviewopt(v, ac, av) /* process view argument */ |
271 |
|
register VIEW *v; |
272 |
|
int ac; |
273 |
|
register char *av[]; |
274 |
|
{ |
275 |
< |
#define check(c,n) if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1) |
276 |
< |
extern double atof(); |
275 |
> |
#define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \ |
276 |
> |
badarg(ac-1,av+1,l)) return(-1) |
277 |
|
|
278 |
|
if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v') |
279 |
|
return(-1); |
281 |
|
case 't': /* type */ |
282 |
|
if (!av[0][3] || av[0][3]==' ') |
283 |
|
return(-1); |
284 |
< |
check(4,0); |
284 |
> |
check(4,""); |
285 |
|
v->type = av[0][3]; |
286 |
|
return(0); |
287 |
|
case 'p': /* point */ |
288 |
< |
check(3,3); |
288 |
> |
check(3,"fff"); |
289 |
|
v->vp[0] = atof(av[1]); |
290 |
|
v->vp[1] = atof(av[2]); |
291 |
|
v->vp[2] = atof(av[3]); |
292 |
|
return(3); |
293 |
|
case 'd': /* direction */ |
294 |
< |
check(3,3); |
294 |
> |
check(3,"fff"); |
295 |
|
v->vdir[0] = atof(av[1]); |
296 |
|
v->vdir[1] = atof(av[2]); |
297 |
|
v->vdir[2] = atof(av[3]); |
298 |
|
return(3); |
299 |
|
case 'u': /* up */ |
300 |
< |
check(3,3); |
300 |
> |
check(3,"fff"); |
301 |
|
v->vup[0] = atof(av[1]); |
302 |
|
v->vup[1] = atof(av[2]); |
303 |
|
v->vup[2] = atof(av[3]); |
304 |
|
return(3); |
305 |
|
case 'h': /* horizontal size */ |
306 |
< |
check(3,1); |
306 |
> |
check(3,"f"); |
307 |
|
v->horiz = atof(av[1]); |
308 |
|
return(1); |
309 |
|
case 'v': /* vertical size */ |
310 |
< |
check(3,1); |
310 |
> |
check(3,"f"); |
311 |
|
v->vert = atof(av[1]); |
312 |
|
return(1); |
313 |
|
case 's': /* shift */ |
314 |
< |
check(3,1); |
314 |
> |
check(3,"f"); |
315 |
|
v->hoff = atof(av[1]); |
316 |
|
return(1); |
317 |
|
case 'l': /* lift */ |
318 |
< |
check(3,1); |
318 |
> |
check(3,"f"); |
319 |
|
v->voff = atof(av[1]); |
320 |
|
return(1); |
321 |
|
default: |
335 |
|
int na; |
336 |
|
int nvopts = 0; |
337 |
|
|
338 |
< |
while (*s == ' ') |
339 |
< |
s++; |
338 |
> |
if (*s != '-') |
339 |
> |
s = sskip(s); |
340 |
|
while (*s) { |
341 |
|
ac = 0; |
342 |
|
do { |
370 |
|
} |
371 |
|
|
372 |
|
|
373 |
< |
static VIEW *hview; /* view from header */ |
374 |
< |
static int gothview; /* success indicator */ |
375 |
< |
static char *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL}; |
373 |
> |
int |
374 |
> |
isview(s) /* is this a view string? */ |
375 |
> |
char *s; |
376 |
> |
{ |
377 |
> |
static char *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL}; |
378 |
> |
extern char *progname; |
379 |
> |
register char *cp; |
380 |
> |
register char **an; |
381 |
> |
/* add program name to list */ |
382 |
> |
if (altname[0] == NULL) { |
383 |
> |
for (cp = progname; *cp; cp++) |
384 |
> |
; |
385 |
> |
while (cp > progname && !ISDIRSEP(cp[-1])) |
386 |
> |
cp--; |
387 |
> |
altname[0] = cp; |
388 |
> |
} |
389 |
> |
/* skip leading path */ |
390 |
> |
cp = s; |
391 |
> |
while (*cp && *cp != ' ') |
392 |
> |
cp++; |
393 |
> |
while (cp > s && !ISDIRSEP(cp[-1])) |
394 |
> |
cp--; |
395 |
> |
for (an = altname; *an != NULL; an++) |
396 |
> |
if (!strncmp(*an, cp, strlen(*an))) |
397 |
> |
return(1); |
398 |
> |
return(0); |
399 |
> |
} |
400 |
|
|
401 |
|
|
402 |
+ |
struct myview { |
403 |
+ |
VIEW *hv; |
404 |
+ |
int ok; |
405 |
+ |
}; |
406 |
+ |
|
407 |
+ |
|
408 |
|
static |
409 |
< |
gethview(s) /* get view from header */ |
409 |
> |
gethview(s, v) /* get view from header */ |
410 |
|
char *s; |
411 |
+ |
register struct myview *v; |
412 |
|
{ |
413 |
< |
register char **an; |
414 |
< |
|
349 |
< |
for (an = altname; *an != NULL; an++) |
350 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
351 |
< |
if (sscanview(hview, s+strlen(*an)) > 0) |
352 |
< |
gothview++; |
353 |
< |
return; |
354 |
< |
} |
413 |
> |
if (isview(s) && sscanview(v->hv, s) > 0) |
414 |
> |
v->ok++; |
415 |
|
} |
416 |
|
|
417 |
|
|
418 |
|
int |
419 |
< |
viewfile(fname, vp, xp, yp) /* get view from file */ |
419 |
> |
viewfile(fname, vp, rp) /* get view from file */ |
420 |
|
char *fname; |
421 |
|
VIEW *vp; |
422 |
< |
int *xp, *yp; |
422 |
> |
RESOLU *rp; |
423 |
|
{ |
424 |
< |
extern char *progname; |
424 |
> |
struct myview mvs; |
425 |
|
FILE *fp; |
426 |
|
|
427 |
|
if ((fp = fopen(fname, "r")) == NULL) |
428 |
|
return(-1); |
429 |
|
|
430 |
< |
altname[0] = progname; |
431 |
< |
hview = vp; |
372 |
< |
gothview = 0; |
430 |
> |
mvs.hv = vp; |
431 |
> |
mvs.ok = 0; |
432 |
|
|
433 |
< |
getheader(fp, gethview); |
433 |
> |
getheader(fp, gethview, &mvs); |
434 |
|
|
435 |
< |
if (xp != NULL && yp != NULL |
436 |
< |
&& fgetresolu(xp, yp, fp) == -1) |
378 |
< |
gothview = 0; |
435 |
> |
if (rp != NULL && !fgetsresolu(rp, fp)) |
436 |
> |
mvs.ok = 0; |
437 |
|
|
438 |
|
fclose(fp); |
439 |
|
|
440 |
< |
return(gothview); |
440 |
> |
return(mvs.ok); |
441 |
|
} |