| 1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1994 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 |
|
|
| 28 |
|
static char ill_horiz[] = "illegal horizontal view size"; |
| 29 |
|
static char ill_vert[] = "illegal vertical view size"; |
| 30 |
|
|
| 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 (normalize(v->vdir) == 0.0) /* normalize direction */ |
| 36 |
|
return("zero view direction"); |
| 37 |
|
|
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
+ |
double |
| 115 |
|
viewray(orig, direc, v, x, y) /* compute ray origin and direction */ |
| 116 |
|
FVECT orig, direc; |
| 117 |
|
register VIEW *v; |
| 124 |
|
|
| 125 |
|
switch(v->type) { |
| 126 |
|
case VT_PAR: /* parallel view */ |
| 127 |
< |
orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0]; |
| 128 |
< |
orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1]; |
| 129 |
< |
orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2]; |
| 127 |
> |
orig[0] = v->vp[0] + v->vfore*v->vdir[0] |
| 128 |
> |
+ x*v->hvec[0] + y*v->vvec[0]; |
| 129 |
> |
orig[1] = v->vp[1] + v->vfore*v->vdir[1] |
| 130 |
> |
+ x*v->hvec[1] + y*v->vvec[1]; |
| 131 |
> |
orig[2] = v->vp[2] + v->vfore*v->vdir[2] |
| 132 |
> |
+ x*v->hvec[2] + y*v->vvec[2]; |
| 133 |
|
VCOPY(direc, v->vdir); |
| 134 |
< |
return(0); |
| 134 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
| 135 |
|
case VT_PER: /* perspective view */ |
| 124 |
– |
VCOPY(orig, v->vp); |
| 136 |
|
direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
| 137 |
|
direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
| 138 |
|
direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
| 139 |
< |
normalize(direc); |
| 140 |
< |
return(0); |
| 139 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
| 140 |
> |
orig[1] = v->vp[1] + v->vfore*direc[1]; |
| 141 |
> |
orig[2] = v->vp[2] + v->vfore*direc[2]; |
| 142 |
> |
d = normalize(direc); |
| 143 |
> |
return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0); |
| 144 |
|
case VT_HEM: /* hemispherical fisheye */ |
| 145 |
|
z = 1.0 - x*x*v->hn2 - y*y*v->vn2; |
| 146 |
|
if (z < 0.0) |
| 147 |
< |
return(-1); |
| 147 |
> |
return(-1.0); |
| 148 |
|
z = sqrt(z); |
| 135 |
– |
VCOPY(orig, v->vp); |
| 149 |
|
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
| 150 |
|
direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
| 151 |
|
direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
| 152 |
< |
return(0); |
| 152 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
| 153 |
> |
orig[1] = v->vp[1] + v->vfore*direc[1]; |
| 154 |
> |
orig[2] = v->vp[2] + v->vfore*direc[2]; |
| 155 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
| 156 |
|
case VT_ANG: /* angular fisheye */ |
| 157 |
|
x *= v->horiz/180.0; |
| 158 |
|
y *= v->vert/180.0; |
| 159 |
|
d = x*x + y*y; |
| 160 |
|
if (d > 1.0) |
| 161 |
< |
return(-1); |
| 146 |
< |
VCOPY(orig, v->vp); |
| 147 |
< |
if (d <= FTINY) { |
| 148 |
< |
VCOPY(direc, v->vdir); |
| 149 |
< |
return(0); |
| 150 |
< |
} |
| 161 |
> |
return(-1.0); |
| 162 |
|
d = sqrt(d); |
| 163 |
|
z = cos(PI*d); |
| 164 |
< |
d = sqrt(1 - z*z)/d; |
| 164 |
> |
d = d <= FTINY ? PI : sqrt(1 - z*z)/d; |
| 165 |
|
x *= d; |
| 166 |
|
y *= d; |
| 167 |
|
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
| 168 |
|
direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
| 169 |
|
direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
| 170 |
< |
return(0); |
| 170 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
| 171 |
> |
orig[1] = v->vp[1] + v->vfore*direc[1]; |
| 172 |
> |
orig[2] = v->vp[2] + v->vfore*direc[2]; |
| 173 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
| 174 |
|
} |
| 175 |
< |
return(-1); |
| 175 |
> |
return(-1.0); |
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
|
| 179 |
< |
viewpixel(xp, yp, zp, v, p) /* find image location for point */ |
| 180 |
< |
double *xp, *yp, *zp; |
| 179 |
> |
viewloc(ip, v, p) /* find image location for point */ |
| 180 |
> |
FVECT ip; |
| 181 |
|
register VIEW *v; |
| 182 |
|
FVECT p; |
| 183 |
|
{ |
| 190 |
|
|
| 191 |
|
switch (v->type) { |
| 192 |
|
case VT_PAR: /* parallel view */ |
| 193 |
< |
if (zp != NULL) |
| 180 |
< |
*zp = DOT(disp,v->vdir); |
| 193 |
> |
ip[2] = DOT(disp,v->vdir) - v->vfore; |
| 194 |
|
break; |
| 195 |
|
case VT_PER: /* perspective view */ |
| 196 |
|
d = DOT(disp,v->vdir); |
| 197 |
< |
if (zp != NULL) { |
| 198 |
< |
*zp = sqrt(DOT(disp,disp)); |
| 199 |
< |
if (d < 0.0) |
| 187 |
< |
*zp = -*zp; |
| 188 |
< |
} |
| 189 |
< |
if (d < 0.0) /* fold pyramid */ |
| 197 |
> |
ip[2] = sqrt(DOT(disp,disp)); |
| 198 |
> |
if (d < 0.0) { /* fold pyramid */ |
| 199 |
> |
ip[2] = -ip[2]; |
| 200 |
|
d = -d; |
| 201 |
+ |
} |
| 202 |
|
if (d > FTINY) { |
| 203 |
|
d = 1.0/d; |
| 204 |
|
disp[0] *= d; |
| 205 |
|
disp[1] *= d; |
| 206 |
|
disp[2] *= d; |
| 207 |
|
} |
| 208 |
+ |
ip[2] *= (1.0 - v->vfore*d); |
| 209 |
|
break; |
| 210 |
|
case VT_HEM: /* hemispherical fisheye */ |
| 211 |
|
d = normalize(disp); |
| 212 |
< |
if (zp != NULL) { |
| 213 |
< |
if (DOT(disp,v->vdir) < 0.0) |
| 214 |
< |
*zp = -d; |
| 215 |
< |
else |
| 216 |
< |
*zp = d; |
| 205 |
< |
} |
| 212 |
> |
if (DOT(disp,v->vdir) < 0.0) |
| 213 |
> |
ip[2] = -d; |
| 214 |
> |
else |
| 215 |
> |
ip[2] = d; |
| 216 |
> |
ip[2] -= v->vfore; |
| 217 |
|
break; |
| 218 |
|
case VT_ANG: /* angular fisheye */ |
| 219 |
< |
d = normalize(disp); |
| 220 |
< |
if (zp != NULL) |
| 221 |
< |
*zp = d; |
| 211 |
< |
*xp = 0.5 - v->hoff; |
| 212 |
< |
*yp = 0.5 - v->voff; |
| 219 |
> |
ip[0] = 0.5 - v->hoff; |
| 220 |
> |
ip[1] = 0.5 - v->voff; |
| 221 |
> |
ip[2] = normalize(disp) - v->vfore; |
| 222 |
|
d = DOT(disp,v->vdir); |
| 223 |
|
if (d >= 1.0-FTINY) |
| 224 |
|
return; |
| 225 |
|
if (d <= -(1.0-FTINY)) { |
| 226 |
< |
*xp += 180.0/v->horiz; |
| 227 |
< |
*yp += 180.0/v->vert; |
| 226 |
> |
ip[0] += 180.0/v->horiz; |
| 227 |
> |
ip[1] += 180.0/v->vert; |
| 228 |
|
return; |
| 229 |
|
} |
| 230 |
|
d = acos(d)/PI / sqrt(1.0 - d*d); |
| 231 |
< |
*xp += DOT(disp,v->hvec)*d*180.0/v->horiz; |
| 232 |
< |
*yp += DOT(disp,v->vvec)*d*180.0/v->vert; |
| 231 |
> |
ip[0] += DOT(disp,v->hvec)*d*180.0/v->horiz; |
| 232 |
> |
ip[1] += DOT(disp,v->vvec)*d*180.0/v->vert; |
| 233 |
|
return; |
| 234 |
|
} |
| 235 |
< |
*xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff; |
| 236 |
< |
*yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff; |
| 235 |
> |
ip[0] = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff; |
| 236 |
> |
ip[1] = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff; |
| 237 |
|
} |
| 238 |
|
|
| 239 |
|
|
| 240 |
+ |
pix2loc(loc, rp, px, py) /* compute image location from pixel pos. */ |
| 241 |
+ |
FLOAT loc[2]; |
| 242 |
+ |
register RESOLU *rp; |
| 243 |
+ |
int px, py; |
| 244 |
+ |
{ |
| 245 |
+ |
register int x, y; |
| 246 |
+ |
|
| 247 |
+ |
if (rp->or & YMAJOR) { |
| 248 |
+ |
x = px; |
| 249 |
+ |
y = py; |
| 250 |
+ |
} else { |
| 251 |
+ |
x = py; |
| 252 |
+ |
y = px; |
| 253 |
+ |
} |
| 254 |
+ |
if (rp->or & XDECR) |
| 255 |
+ |
x = rp->xr-1 - x; |
| 256 |
+ |
if (rp->or & YDECR) |
| 257 |
+ |
y = rp->yr-1 - y; |
| 258 |
+ |
loc[0] = (x+.5)/rp->xr; |
| 259 |
+ |
loc[1] = (y+.5)/rp->yr; |
| 260 |
+ |
} |
| 261 |
+ |
|
| 262 |
+ |
|
| 263 |
+ |
loc2pix(pp, rp, lx, ly) /* compute pixel pos. from image location */ |
| 264 |
+ |
int pp[2]; |
| 265 |
+ |
register RESOLU *rp; |
| 266 |
+ |
double lx, ly; |
| 267 |
+ |
{ |
| 268 |
+ |
register int x, y; |
| 269 |
+ |
|
| 270 |
+ |
x = lx * rp->xr; |
| 271 |
+ |
y = ly * rp->yr; |
| 272 |
+ |
if (rp->or & XDECR) |
| 273 |
+ |
x = rp->xr-1 - x; |
| 274 |
+ |
if (rp->or & YDECR) |
| 275 |
+ |
y = rp->yr-1 - y; |
| 276 |
+ |
if (rp->or & YMAJOR) { |
| 277 |
+ |
pp[0] = x; |
| 278 |
+ |
pp[1] = y; |
| 279 |
+ |
} else { |
| 280 |
+ |
pp[0] = y; |
| 281 |
+ |
pp[1] = x; |
| 282 |
+ |
} |
| 283 |
+ |
} |
| 284 |
+ |
|
| 285 |
+ |
|
| 286 |
|
int |
| 287 |
|
getviewopt(v, ac, av) /* process view argument */ |
| 288 |
|
register VIEW *v; |
| 289 |
|
int ac; |
| 290 |
|
register char *av[]; |
| 291 |
|
{ |
| 292 |
< |
#define check(c,n) if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1) |
| 293 |
< |
extern double atof(); |
| 292 |
> |
#define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \ |
| 293 |
> |
badarg(ac-1,av+1,l)) return(-1) |
| 294 |
|
|
| 295 |
|
if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v') |
| 296 |
|
return(-1); |
| 298 |
|
case 't': /* type */ |
| 299 |
|
if (!av[0][3] || av[0][3]==' ') |
| 300 |
|
return(-1); |
| 301 |
< |
check(4,0); |
| 301 |
> |
check(4,""); |
| 302 |
|
v->type = av[0][3]; |
| 303 |
|
return(0); |
| 304 |
|
case 'p': /* point */ |
| 305 |
< |
check(3,3); |
| 305 |
> |
check(3,"fff"); |
| 306 |
|
v->vp[0] = atof(av[1]); |
| 307 |
|
v->vp[1] = atof(av[2]); |
| 308 |
|
v->vp[2] = atof(av[3]); |
| 309 |
|
return(3); |
| 310 |
|
case 'd': /* direction */ |
| 311 |
< |
check(3,3); |
| 311 |
> |
check(3,"fff"); |
| 312 |
|
v->vdir[0] = atof(av[1]); |
| 313 |
|
v->vdir[1] = atof(av[2]); |
| 314 |
|
v->vdir[2] = atof(av[3]); |
| 315 |
|
return(3); |
| 316 |
|
case 'u': /* up */ |
| 317 |
< |
check(3,3); |
| 317 |
> |
check(3,"fff"); |
| 318 |
|
v->vup[0] = atof(av[1]); |
| 319 |
|
v->vup[1] = atof(av[2]); |
| 320 |
|
v->vup[2] = atof(av[3]); |
| 321 |
|
return(3); |
| 322 |
|
case 'h': /* horizontal size */ |
| 323 |
< |
check(3,1); |
| 323 |
> |
check(3,"f"); |
| 324 |
|
v->horiz = atof(av[1]); |
| 325 |
|
return(1); |
| 326 |
|
case 'v': /* vertical size */ |
| 327 |
< |
check(3,1); |
| 327 |
> |
check(3,"f"); |
| 328 |
|
v->vert = atof(av[1]); |
| 329 |
|
return(1); |
| 330 |
+ |
case 'o': /* fore clipping plane */ |
| 331 |
+ |
check(3,"f"); |
| 332 |
+ |
v->vfore = atof(av[1]); |
| 333 |
+ |
return(1); |
| 334 |
+ |
case 'a': /* aft clipping plane */ |
| 335 |
+ |
check(3,"f"); |
| 336 |
+ |
v->vaft = atof(av[1]); |
| 337 |
+ |
return(1); |
| 338 |
|
case 's': /* shift */ |
| 339 |
< |
check(3,1); |
| 339 |
> |
check(3,"f"); |
| 340 |
|
v->hoff = atof(av[1]); |
| 341 |
|
return(1); |
| 342 |
|
case 'l': /* lift */ |
| 343 |
< |
check(3,1); |
| 343 |
> |
check(3,"f"); |
| 344 |
|
v->voff = atof(av[1]); |
| 345 |
|
return(1); |
| 346 |
|
default: |
| 360 |
|
int na; |
| 361 |
|
int nvopts = 0; |
| 362 |
|
|
| 363 |
< |
while (*s == ' ') |
| 364 |
< |
s++; |
| 363 |
> |
if (*s != '-') |
| 364 |
> |
s = sskip(s); |
| 365 |
|
while (*s) { |
| 366 |
|
ac = 0; |
| 367 |
|
do { |
| 391 |
|
fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]); |
| 392 |
|
fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]); |
| 393 |
|
fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert); |
| 394 |
+ |
fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft); |
| 395 |
|
fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff); |
| 396 |
|
} |
| 397 |
|
|
| 398 |
|
|
| 399 |
< |
static VIEW *hview; /* view from header */ |
| 400 |
< |
static int gothview; /* success indicator */ |
| 401 |
< |
static char *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL}; |
| 399 |
> |
int |
| 400 |
> |
isview(s) /* is this a view string? */ |
| 401 |
> |
char *s; |
| 402 |
> |
{ |
| 403 |
> |
static char *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL}; |
| 404 |
> |
extern char *progname; |
| 405 |
> |
register char *cp; |
| 406 |
> |
register char **an; |
| 407 |
> |
/* add program name to list */ |
| 408 |
> |
if (altname[0] == NULL) { |
| 409 |
> |
for (cp = progname; *cp; cp++) |
| 410 |
> |
; |
| 411 |
> |
while (cp > progname && !ISDIRSEP(cp[-1])) |
| 412 |
> |
cp--; |
| 413 |
> |
altname[0] = cp; |
| 414 |
> |
} |
| 415 |
> |
/* skip leading path */ |
| 416 |
> |
cp = s; |
| 417 |
> |
while (*cp && *cp != ' ') |
| 418 |
> |
cp++; |
| 419 |
> |
while (cp > s && !ISDIRSEP(cp[-1])) |
| 420 |
> |
cp--; |
| 421 |
> |
for (an = altname; *an != NULL; an++) |
| 422 |
> |
if (!strncmp(*an, cp, strlen(*an))) |
| 423 |
> |
return(1); |
| 424 |
> |
return(0); |
| 425 |
> |
} |
| 426 |
|
|
| 427 |
|
|
| 428 |
+ |
struct myview { |
| 429 |
+ |
VIEW *hv; |
| 430 |
+ |
int ok; |
| 431 |
+ |
}; |
| 432 |
+ |
|
| 433 |
+ |
|
| 434 |
|
static |
| 435 |
< |
gethview(s) /* get view from header */ |
| 435 |
> |
gethview(s, v) /* get view from header */ |
| 436 |
|
char *s; |
| 437 |
+ |
register struct myview *v; |
| 438 |
|
{ |
| 439 |
< |
register char **an; |
| 440 |
< |
|
| 346 |
< |
for (an = altname; *an != NULL; an++) |
| 347 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
| 348 |
< |
if (sscanview(hview, s+strlen(*an)) > 0) |
| 349 |
< |
gothview++; |
| 350 |
< |
return; |
| 351 |
< |
} |
| 439 |
> |
if (isview(s) && sscanview(v->hv, s) > 0) |
| 440 |
> |
v->ok++; |
| 441 |
|
} |
| 442 |
|
|
| 443 |
|
|
| 444 |
|
int |
| 445 |
< |
viewfile(fname, vp, xp, yp) /* get view from file */ |
| 445 |
> |
viewfile(fname, vp, rp) /* get view from file */ |
| 446 |
|
char *fname; |
| 447 |
|
VIEW *vp; |
| 448 |
< |
int *xp, *yp; |
| 448 |
> |
RESOLU *rp; |
| 449 |
|
{ |
| 450 |
< |
extern char *progname; |
| 450 |
> |
struct myview mvs; |
| 451 |
|
FILE *fp; |
| 452 |
|
|
| 453 |
|
if ((fp = fopen(fname, "r")) == NULL) |
| 454 |
|
return(-1); |
| 455 |
|
|
| 456 |
< |
altname[0] = progname; |
| 457 |
< |
hview = vp; |
| 369 |
< |
gothview = 0; |
| 456 |
> |
mvs.hv = vp; |
| 457 |
> |
mvs.ok = 0; |
| 458 |
|
|
| 459 |
< |
getheader(fp, gethview); |
| 459 |
> |
getheader(fp, gethview, &mvs); |
| 460 |
|
|
| 461 |
< |
if (xp != NULL && yp != NULL |
| 462 |
< |
&& fgetresolu(xp, yp, fp) == -1) |
| 375 |
< |
gothview = 0; |
| 461 |
> |
if (rp != NULL && !fgetsresolu(rp, fp)) |
| 462 |
> |
mvs.ok = 0; |
| 463 |
|
|
| 464 |
|
fclose(fp); |
| 465 |
|
|
| 466 |
< |
return(gothview); |
| 466 |
> |
return(mvs.ok); |
| 467 |
|
} |