1 |
– |
/* Copyright (c) 1994 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 "copyright.h" |
11 |
+ |
|
12 |
|
#include "standard.h" |
13 |
|
|
14 |
|
#include "view.h" |
15 |
|
|
17 |
– |
#include "resolu.h" |
18 |
– |
|
16 |
|
#include "paths.h" |
17 |
|
|
18 |
+ |
#define FEQ(x,y) (fabs((x)-(y)) <= FTINY) |
19 |
+ |
#define VEQ(v,w) (FEQ((v)[0],(w)[0]) && FEQ((v)[1],(w)[1]) \ |
20 |
+ |
&& FEQ((v)[2],(w)[2])) |
21 |
+ |
|
22 |
|
VIEW stdview = STDVIEW; /* default view parameters */ |
23 |
|
|
24 |
|
|
29 |
|
static char ill_horiz[] = "illegal horizontal view size"; |
30 |
|
static char ill_vert[] = "illegal vertical view size"; |
31 |
|
|
32 |
< |
if (v->vfore < -FTINY || v->vaft < -FTINY || |
32 |
< |
(v->vaft > FTINY && v->vaft <= v->vfore)) |
32 |
> |
if (v->vaft < -FTINY || (v->vaft > FTINY && v->vaft <= v->vfore)) |
33 |
|
return("illegal fore/aft clipping plane"); |
34 |
|
|
35 |
|
if (normalize(v->vdir) == 0.0) /* normalize direction */ |
63 |
|
v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0)); |
64 |
|
v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0)); |
65 |
|
break; |
66 |
+ |
case VT_CYL: /* cylindrical panorama */ |
67 |
+ |
if (v->horiz > 360.0+FTINY) |
68 |
+ |
return(ill_horiz); |
69 |
+ |
if (v->vert >= 180.0-FTINY) |
70 |
+ |
return(ill_vert); |
71 |
+ |
v->hn2 = v->horiz * (PI/180.0); |
72 |
+ |
v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0)); |
73 |
+ |
break; |
74 |
|
case VT_ANG: /* angular fisheye */ |
75 |
|
if (v->horiz > 360.0+FTINY) |
76 |
|
return(ill_horiz); |
77 |
|
if (v->vert > 360.0+FTINY) |
78 |
|
return(ill_vert); |
79 |
< |
v->hn2 = v->horiz / 90.0; |
80 |
< |
v->vn2 = v->vert / 90.0; |
79 |
> |
v->hn2 = v->horiz * (PI/180.0); |
80 |
> |
v->vn2 = v->vert * (PI/180.0); |
81 |
|
break; |
82 |
|
case VT_HEM: /* hemispherical fisheye */ |
83 |
|
if (v->horiz > 180.0+FTINY) |
91 |
|
return("unknown view type"); |
92 |
|
} |
93 |
|
if (v->type != VT_ANG) { |
94 |
< |
v->hvec[0] *= v->hn2; |
95 |
< |
v->hvec[1] *= v->hn2; |
96 |
< |
v->hvec[2] *= v->hn2; |
94 |
> |
if (v->type != VT_CYL) { |
95 |
> |
v->hvec[0] *= v->hn2; |
96 |
> |
v->hvec[1] *= v->hn2; |
97 |
> |
v->hvec[2] *= v->hn2; |
98 |
> |
} |
99 |
|
v->vvec[0] *= v->vn2; |
100 |
|
v->vvec[1] *= v->vn2; |
101 |
|
v->vvec[2] *= v->vn2; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
+ |
void |
111 |
|
normaspect(va, ap, xp, yp) /* fix pixel aspect or resolution */ |
112 |
|
double va; /* view aspect ratio */ |
113 |
|
double *ap; /* pixel aspect in (or out if 0) */ |
142 |
|
orig[2] = v->vp[2] + v->vfore*v->vdir[2] |
143 |
|
+ x*v->hvec[2] + y*v->vvec[2]; |
144 |
|
VCOPY(direc, v->vdir); |
145 |
< |
return(v->vaft - v->vfore); |
145 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
146 |
|
case VT_PER: /* perspective view */ |
147 |
|
direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
148 |
|
direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
151 |
|
orig[1] = v->vp[1] + v->vfore*direc[1]; |
152 |
|
orig[2] = v->vp[2] + v->vfore*direc[2]; |
153 |
|
d = normalize(direc); |
154 |
< |
return((v->vaft - v->vfore)*d); |
154 |
> |
return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0); |
155 |
|
case VT_HEM: /* hemispherical fisheye */ |
156 |
|
z = 1.0 - x*x*v->hn2 - y*y*v->vn2; |
157 |
|
if (z < 0.0) |
163 |
|
orig[0] = v->vp[0] + v->vfore*direc[0]; |
164 |
|
orig[1] = v->vp[1] + v->vfore*direc[1]; |
165 |
|
orig[2] = v->vp[2] + v->vfore*direc[2]; |
166 |
< |
return(v->vaft - v->vfore); |
166 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
167 |
> |
case VT_CYL: /* cylindrical panorama */ |
168 |
> |
d = x * v->horiz * (PI/180.0); |
169 |
> |
z = cos(d); |
170 |
> |
x = sin(d); |
171 |
> |
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
172 |
> |
direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
173 |
> |
direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
174 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
175 |
> |
orig[1] = v->vp[1] + v->vfore*direc[1]; |
176 |
> |
orig[2] = v->vp[2] + v->vfore*direc[2]; |
177 |
> |
d = normalize(direc); |
178 |
> |
return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0); |
179 |
|
case VT_ANG: /* angular fisheye */ |
180 |
|
x *= v->horiz/180.0; |
181 |
|
y *= v->vert/180.0; |
193 |
|
orig[0] = v->vp[0] + v->vfore*direc[0]; |
194 |
|
orig[1] = v->vp[1] + v->vfore*direc[1]; |
195 |
|
orig[2] = v->vp[2] + v->vfore*direc[2]; |
196 |
< |
return(v->vaft - v->vfore); |
196 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
197 |
|
} |
198 |
|
return(-1.0); |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
+ |
void |
203 |
|
viewloc(ip, v, p) /* find image location for point */ |
204 |
|
FVECT ip; |
205 |
|
register VIEW *v; |
206 |
|
FVECT p; |
207 |
|
{ |
208 |
< |
double d; |
208 |
> |
double d, d2; |
209 |
|
FVECT disp; |
210 |
|
|
211 |
|
disp[0] = p[0] - v->vp[0]; |
218 |
|
break; |
219 |
|
case VT_PER: /* perspective view */ |
220 |
|
d = DOT(disp,v->vdir); |
221 |
< |
ip[2] = sqrt(DOT(disp,disp)); |
221 |
> |
ip[2] = VLEN(disp); |
222 |
|
if (d < 0.0) { /* fold pyramid */ |
223 |
|
ip[2] = -ip[2]; |
224 |
|
d = -d; |
239 |
|
ip[2] = d; |
240 |
|
ip[2] -= v->vfore; |
241 |
|
break; |
242 |
+ |
case VT_CYL: /* cylindrical panorama */ |
243 |
+ |
d = DOT(disp,v->hvec); |
244 |
+ |
d2 = DOT(disp,v->vdir); |
245 |
+ |
ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff; |
246 |
+ |
d = 1.0/sqrt(d*d + d2*d2); |
247 |
+ |
ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff; |
248 |
+ |
ip[2] = VLEN(disp); |
249 |
+ |
ip[2] *= (1.0 - v->vfore*d); |
250 |
+ |
return; |
251 |
|
case VT_ANG: /* angular fisheye */ |
252 |
|
ip[0] = 0.5 - v->hoff; |
253 |
|
ip[1] = 0.5 - v->voff; |
257 |
|
return; |
258 |
|
if (d <= -(1.0-FTINY)) { |
259 |
|
ip[0] += 180.0/v->horiz; |
227 |
– |
ip[1] += 180.0/v->vert; |
260 |
|
return; |
261 |
|
} |
262 |
|
d = acos(d)/PI / sqrt(1.0 - d*d); |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
+ |
void |
273 |
|
pix2loc(loc, rp, px, py) /* compute image location from pixel pos. */ |
274 |
< |
FLOAT loc[2]; |
274 |
> |
RREAL loc[2]; |
275 |
|
register RESOLU *rp; |
276 |
|
int px, py; |
277 |
|
{ |
278 |
|
register int x, y; |
279 |
|
|
280 |
< |
if (rp->or & YMAJOR) { |
280 |
> |
if (rp->rt & YMAJOR) { |
281 |
|
x = px; |
282 |
|
y = py; |
283 |
|
} else { |
284 |
|
x = py; |
285 |
|
y = px; |
286 |
|
} |
287 |
< |
if (rp->or & XDECR) |
287 |
> |
if (rp->rt & XDECR) |
288 |
|
x = rp->xr-1 - x; |
289 |
< |
if (rp->or & YDECR) |
289 |
> |
if (rp->rt & YDECR) |
290 |
|
y = rp->yr-1 - y; |
291 |
|
loc[0] = (x+.5)/rp->xr; |
292 |
|
loc[1] = (y+.5)/rp->yr; |
293 |
|
} |
294 |
|
|
295 |
|
|
296 |
+ |
void |
297 |
|
loc2pix(pp, rp, lx, ly) /* compute pixel pos. from image location */ |
298 |
|
int pp[2]; |
299 |
|
register RESOLU *rp; |
303 |
|
|
304 |
|
x = lx * rp->xr; |
305 |
|
y = ly * rp->yr; |
306 |
< |
if (rp->or & XDECR) |
306 |
> |
if (rp->rt & XDECR) |
307 |
|
x = rp->xr-1 - x; |
308 |
< |
if (rp->or & YDECR) |
308 |
> |
if (rp->rt & YDECR) |
309 |
|
y = rp->yr-1 - y; |
310 |
< |
if (rp->or & YMAJOR) { |
310 |
> |
if (rp->rt & YMAJOR) { |
311 |
|
pp[0] = x; |
312 |
|
pp[1] = y; |
313 |
|
} else { |
394 |
|
int na; |
395 |
|
int nvopts = 0; |
396 |
|
|
397 |
+ |
while (*s == ' ') |
398 |
+ |
s++; |
399 |
|
if (*s != '-') |
400 |
< |
s = sskip(s); |
400 |
> |
s = sskip2(s,1); |
401 |
|
while (*s) { |
402 |
|
ac = 0; |
403 |
|
do { |
418 |
|
} |
419 |
|
|
420 |
|
|
421 |
+ |
void |
422 |
|
fprintview(vp, fp) /* write out view parameters */ |
423 |
|
register VIEW *vp; |
424 |
|
FILE *fp; |
433 |
|
} |
434 |
|
|
435 |
|
|
436 |
+ |
char * |
437 |
+ |
viewopt(vp) /* translate to minimal view string */ |
438 |
+ |
register VIEW *vp; |
439 |
+ |
{ |
440 |
+ |
static char vwstr[128]; |
441 |
+ |
register char *cp = vwstr; |
442 |
+ |
|
443 |
+ |
if (vp->type != stdview.type) { |
444 |
+ |
sprintf(cp, " -vt%c", vp->type); |
445 |
+ |
cp += strlen(cp); |
446 |
+ |
} |
447 |
+ |
if (!VEQ(vp->vp,stdview.vp)) { |
448 |
+ |
sprintf(cp, " -vp %.6g %.6g %.6g", |
449 |
+ |
vp->vp[0], vp->vp[1], vp->vp[2]); |
450 |
+ |
cp += strlen(cp); |
451 |
+ |
} |
452 |
+ |
if (!VEQ(vp->vdir,stdview.vdir)) { |
453 |
+ |
sprintf(cp, " -vd %.6g %.6g %.6g", |
454 |
+ |
vp->vdir[0], vp->vdir[1], vp->vdir[2]); |
455 |
+ |
cp += strlen(cp); |
456 |
+ |
} |
457 |
+ |
if (!VEQ(vp->vup,stdview.vup)) { |
458 |
+ |
sprintf(cp, " -vu %.6g %.6g %.6g", |
459 |
+ |
vp->vup[0], vp->vup[1], vp->vup[2]); |
460 |
+ |
cp += strlen(cp); |
461 |
+ |
} |
462 |
+ |
if (!FEQ(vp->horiz,stdview.horiz)) { |
463 |
+ |
sprintf(cp, " -vh %.6g", vp->horiz); |
464 |
+ |
cp += strlen(cp); |
465 |
+ |
} |
466 |
+ |
if (!FEQ(vp->vert,stdview.vert)) { |
467 |
+ |
sprintf(cp, " -vv %.6g", vp->vert); |
468 |
+ |
cp += strlen(cp); |
469 |
+ |
} |
470 |
+ |
if (!FEQ(vp->vfore,stdview.vfore)) { |
471 |
+ |
sprintf(cp, " -vo %.6g", vp->vfore); |
472 |
+ |
cp += strlen(cp); |
473 |
+ |
} |
474 |
+ |
if (!FEQ(vp->vaft,stdview.vaft)) { |
475 |
+ |
sprintf(cp, " -va %.6g", vp->vaft); |
476 |
+ |
cp += strlen(cp); |
477 |
+ |
} |
478 |
+ |
if (!FEQ(vp->hoff,stdview.hoff)) { |
479 |
+ |
sprintf(cp, " -vs %.6g", vp->hoff); |
480 |
+ |
cp += strlen(cp); |
481 |
+ |
} |
482 |
+ |
if (!FEQ(vp->voff,stdview.voff)) { |
483 |
+ |
sprintf(cp, " -vl %.6g", vp->voff); |
484 |
+ |
cp += strlen(cp); |
485 |
+ |
} |
486 |
+ |
return(vwstr); |
487 |
+ |
} |
488 |
+ |
|
489 |
+ |
|
490 |
|
int |
491 |
|
isview(s) /* is this a view string? */ |
492 |
|
char *s; |
522 |
|
}; |
523 |
|
|
524 |
|
|
525 |
< |
static |
525 |
> |
static int |
526 |
|
gethview(s, v) /* get view from header */ |
527 |
|
char *s; |
528 |
|
register struct myview *v; |
529 |
|
{ |
530 |
|
if (isview(s) && sscanview(v->hv, s) > 0) |
531 |
|
v->ok++; |
532 |
+ |
return(0); |
533 |
|
} |
534 |
|
|
535 |
|
|
542 |
|
struct myview mvs; |
543 |
|
FILE *fp; |
544 |
|
|
545 |
< |
if ((fp = fopen(fname, "r")) == NULL) |
545 |
> |
if (fname == NULL || !strcmp(fname, "-")) |
546 |
> |
fp = stdin; |
547 |
> |
else if ((fp = fopen(fname, "r")) == NULL) |
548 |
|
return(-1); |
549 |
|
|
550 |
|
mvs.hv = vp; |
551 |
|
mvs.ok = 0; |
552 |
|
|
553 |
< |
getheader(fp, gethview, &mvs); |
553 |
> |
getheader(fp, (int (*)(char *, char *))&gethview, (char *)&mvs); |
554 |
|
|
555 |
|
if (rp != NULL && !fgetsresolu(rp, fp)) |
556 |
|
mvs.ok = 0; |