1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
6 |
|
|
7 |
|
/* |
8 |
|
* image.c - routines for image generation. |
9 |
– |
* |
10 |
– |
* 10/17/85 |
9 |
|
*/ |
10 |
|
|
11 |
|
#include "standard.h" |
14 |
|
|
15 |
|
#include "resolu.h" |
16 |
|
|
17 |
+ |
#include "paths.h" |
18 |
+ |
|
19 |
+ |
#define FEQ(x,y) (fabs((x)-(y)) <= FTINY) |
20 |
+ |
#define VEQ(v,w) (FEQ((v)[0],(w)[0]) && FEQ((v)[1],(w)[1]) \ |
21 |
+ |
&& FEQ((v)[2],(w)[2])) |
22 |
+ |
|
23 |
|
VIEW stdview = STDVIEW; /* default view parameters */ |
24 |
|
|
25 |
|
|
30 |
|
static char ill_horiz[] = "illegal horizontal view size"; |
31 |
|
static char ill_vert[] = "illegal vertical view size"; |
32 |
|
|
33 |
+ |
if (v->vaft < -FTINY || (v->vaft > FTINY && v->vaft <= v->vfore)) |
34 |
+ |
return("illegal fore/aft clipping plane"); |
35 |
+ |
|
36 |
|
if (normalize(v->vdir) == 0.0) /* normalize direction */ |
37 |
|
return("zero view direction"); |
38 |
|
|
64 |
|
v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0)); |
65 |
|
v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0)); |
66 |
|
break; |
67 |
+ |
case VT_CYL: /* cylindrical panorama */ |
68 |
+ |
if (v->horiz > 360.0+FTINY) |
69 |
+ |
return(ill_horiz); |
70 |
+ |
if (v->vert >= 180.0-FTINY) |
71 |
+ |
return(ill_vert); |
72 |
+ |
v->hn2 = v->horiz * (PI/180.0); |
73 |
+ |
v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0)); |
74 |
+ |
break; |
75 |
|
case VT_ANG: /* angular fisheye */ |
76 |
|
if (v->horiz > 360.0+FTINY) |
77 |
|
return(ill_horiz); |
78 |
|
if (v->vert > 360.0+FTINY) |
79 |
|
return(ill_vert); |
80 |
< |
v->hn2 = v->horiz / 90.0; |
81 |
< |
v->vn2 = v->vert / 90.0; |
80 |
> |
v->hn2 = v->horiz * (PI/180.0); |
81 |
> |
v->vn2 = v->vert * (PI/180.0); |
82 |
|
break; |
83 |
|
case VT_HEM: /* hemispherical fisheye */ |
84 |
|
if (v->horiz > 180.0+FTINY) |
92 |
|
return("unknown view type"); |
93 |
|
} |
94 |
|
if (v->type != VT_ANG) { |
95 |
< |
v->hvec[0] *= v->hn2; |
96 |
< |
v->hvec[1] *= v->hn2; |
97 |
< |
v->hvec[2] *= v->hn2; |
95 |
> |
if (v->type != VT_CYL) { |
96 |
> |
v->hvec[0] *= v->hn2; |
97 |
> |
v->hvec[1] *= v->hn2; |
98 |
> |
v->hvec[2] *= v->hn2; |
99 |
> |
} |
100 |
|
v->vvec[0] *= v->vn2; |
101 |
|
v->vvec[1] *= v->vn2; |
102 |
|
v->vvec[2] *= v->vn2; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
+ |
double |
126 |
|
viewray(orig, direc, v, x, y) /* compute ray origin and direction */ |
127 |
|
FVECT orig, direc; |
128 |
|
register VIEW *v; |
135 |
|
|
136 |
|
switch(v->type) { |
137 |
|
case VT_PAR: /* parallel view */ |
138 |
< |
orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0]; |
139 |
< |
orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1]; |
140 |
< |
orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2]; |
138 |
> |
orig[0] = v->vp[0] + v->vfore*v->vdir[0] |
139 |
> |
+ x*v->hvec[0] + y*v->vvec[0]; |
140 |
> |
orig[1] = v->vp[1] + v->vfore*v->vdir[1] |
141 |
> |
+ x*v->hvec[1] + y*v->vvec[1]; |
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(0); |
145 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
146 |
|
case VT_PER: /* perspective view */ |
126 |
– |
VCOPY(orig, v->vp); |
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]; |
149 |
|
direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
150 |
< |
normalize(direc); |
151 |
< |
return(0); |
150 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
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 > 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) |
158 |
< |
return(-1); |
158 |
> |
return(-1.0); |
159 |
|
z = sqrt(z); |
137 |
– |
VCOPY(orig, v->vp); |
160 |
|
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
161 |
|
direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
162 |
|
direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
163 |
< |
return(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 > 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; |
182 |
|
d = x*x + y*y; |
183 |
|
if (d > 1.0) |
184 |
< |
return(-1); |
148 |
< |
VCOPY(orig, v->vp); |
149 |
< |
if (d <= FTINY) { |
150 |
< |
VCOPY(direc, v->vdir); |
151 |
< |
return(0); |
152 |
< |
} |
184 |
> |
return(-1.0); |
185 |
|
d = sqrt(d); |
186 |
|
z = cos(PI*d); |
187 |
< |
d = sqrt(1 - z*z)/d; |
187 |
> |
d = d <= FTINY ? PI : sqrt(1 - z*z)/d; |
188 |
|
x *= d; |
189 |
|
y *= d; |
190 |
|
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
191 |
|
direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
192 |
|
direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
193 |
< |
return(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 > FTINY ? v->vaft - v->vfore : 0.0); |
197 |
|
} |
198 |
< |
return(-1); |
198 |
> |
return(-1.0); |
199 |
|
} |
200 |
|
|
201 |
|
|
204 |
|
register VIEW *v; |
205 |
|
FVECT p; |
206 |
|
{ |
207 |
< |
double d; |
207 |
> |
double d, d2; |
208 |
|
FVECT disp; |
209 |
|
|
210 |
|
disp[0] = p[0] - v->vp[0]; |
213 |
|
|
214 |
|
switch (v->type) { |
215 |
|
case VT_PAR: /* parallel view */ |
216 |
< |
ip[2] = DOT(disp,v->vdir); |
216 |
> |
ip[2] = DOT(disp,v->vdir) - v->vfore; |
217 |
|
break; |
218 |
|
case VT_PER: /* perspective view */ |
219 |
|
d = DOT(disp,v->vdir); |
220 |
< |
ip[2] = sqrt(DOT(disp,disp)); |
220 |
> |
ip[2] = VLEN(disp); |
221 |
|
if (d < 0.0) { /* fold pyramid */ |
222 |
|
ip[2] = -ip[2]; |
223 |
|
d = -d; |
224 |
< |
} else if (d > FTINY) { |
224 |
> |
} |
225 |
> |
if (d > FTINY) { |
226 |
|
d = 1.0/d; |
227 |
|
disp[0] *= d; |
228 |
|
disp[1] *= d; |
229 |
|
disp[2] *= d; |
230 |
|
} |
231 |
+ |
ip[2] *= (1.0 - v->vfore*d); |
232 |
|
break; |
233 |
|
case VT_HEM: /* hemispherical fisheye */ |
234 |
|
d = normalize(disp); |
236 |
|
ip[2] = -d; |
237 |
|
else |
238 |
|
ip[2] = d; |
239 |
+ |
ip[2] -= v->vfore; |
240 |
|
break; |
241 |
+ |
case VT_CYL: /* cylindrical panorama */ |
242 |
+ |
d = DOT(disp,v->hvec); |
243 |
+ |
d2 = DOT(disp,v->vdir); |
244 |
+ |
ip[0] = 180.0/PI * atan2(d,d2) / v->horiz + 0.5 - v->hoff; |
245 |
+ |
d = 1.0/sqrt(d*d + d2*d2); |
246 |
+ |
ip[1] = DOT(disp,v->vvec)*d/v->vn2 + 0.5 - v->voff; |
247 |
+ |
ip[2] = VLEN(disp); |
248 |
+ |
ip[2] *= (1.0 - v->vfore*d); |
249 |
+ |
return; |
250 |
|
case VT_ANG: /* angular fisheye */ |
251 |
|
ip[0] = 0.5 - v->hoff; |
252 |
|
ip[1] = 0.5 - v->voff; |
253 |
< |
ip[2] = normalize(disp); |
253 |
> |
ip[2] = normalize(disp) - v->vfore; |
254 |
|
d = DOT(disp,v->vdir); |
255 |
|
if (d >= 1.0-FTINY) |
256 |
|
return; |
257 |
|
if (d <= -(1.0-FTINY)) { |
258 |
< |
ip[1] += 180.0/v->horiz; |
212 |
< |
ip[2] += 180.0/v->vert; |
258 |
> |
ip[0] += 180.0/v->horiz; |
259 |
|
return; |
260 |
|
} |
261 |
|
d = acos(d)/PI / sqrt(1.0 - d*d); |
322 |
|
{ |
323 |
|
#define check(c,l) if ((av[0][c]&&av[0][c]!=' ') || \ |
324 |
|
badarg(ac-1,av+1,l)) return(-1) |
279 |
– |
extern double atof(); |
325 |
|
|
326 |
|
if (ac <= 0 || av[0][0] != '-' || av[0][1] != 'v') |
327 |
|
return(-1); |
358 |
|
check(3,"f"); |
359 |
|
v->vert = atof(av[1]); |
360 |
|
return(1); |
361 |
+ |
case 'o': /* fore clipping plane */ |
362 |
+ |
check(3,"f"); |
363 |
+ |
v->vfore = atof(av[1]); |
364 |
+ |
return(1); |
365 |
+ |
case 'a': /* aft clipping plane */ |
366 |
+ |
check(3,"f"); |
367 |
+ |
v->vaft = atof(av[1]); |
368 |
+ |
return(1); |
369 |
|
case 's': /* shift */ |
370 |
|
check(3,"f"); |
371 |
|
v->hoff = atof(av[1]); |
391 |
|
int na; |
392 |
|
int nvopts = 0; |
393 |
|
|
394 |
< |
while (*s == ' ') |
395 |
< |
s++; |
394 |
> |
if (*s != '-') |
395 |
> |
s = sskip2(s,1); |
396 |
|
while (*s) { |
397 |
|
ac = 0; |
398 |
|
do { |
422 |
|
fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]); |
423 |
|
fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]); |
424 |
|
fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert); |
425 |
+ |
fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft); |
426 |
|
fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff); |
427 |
|
} |
428 |
|
|
429 |
|
|
430 |
< |
static char *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL}; |
430 |
> |
char * |
431 |
> |
viewopt(vp) /* translate to minimal view string */ |
432 |
> |
register VIEW *vp; |
433 |
> |
{ |
434 |
> |
static char vwstr[128]; |
435 |
> |
register char *cp = vwstr; |
436 |
|
|
437 |
+ |
if (vp->type != stdview.type) { |
438 |
+ |
sprintf(cp, " -vt%c", vp->type); |
439 |
+ |
cp += strlen(cp); |
440 |
+ |
} |
441 |
+ |
if (!VEQ(vp->vp,stdview.vp)) { |
442 |
+ |
sprintf(cp, " -vp %.6g %.6g %.6g", |
443 |
+ |
vp->vp[0], vp->vp[1], vp->vp[2]); |
444 |
+ |
cp += strlen(cp); |
445 |
+ |
} |
446 |
+ |
if (!VEQ(vp->vdir,stdview.vdir)) { |
447 |
+ |
sprintf(cp, " -vd %.6g %.6g %.6g", |
448 |
+ |
vp->vdir[0], vp->vdir[1], vp->vdir[2]); |
449 |
+ |
cp += strlen(cp); |
450 |
+ |
} |
451 |
+ |
if (!VEQ(vp->vup,stdview.vup)) { |
452 |
+ |
sprintf(cp, " -vu %.6g %.6g %.6g", |
453 |
+ |
vp->vup[0], vp->vup[1], vp->vup[2]); |
454 |
+ |
cp += strlen(cp); |
455 |
+ |
} |
456 |
+ |
if (!FEQ(vp->horiz,stdview.horiz)) { |
457 |
+ |
sprintf(cp, " -vh %.6g", vp->horiz); |
458 |
+ |
cp += strlen(cp); |
459 |
+ |
} |
460 |
+ |
if (!FEQ(vp->vert,stdview.vert)) { |
461 |
+ |
sprintf(cp, " -vv %.6g", vp->vert); |
462 |
+ |
cp += strlen(cp); |
463 |
+ |
} |
464 |
+ |
if (!FEQ(vp->vfore,stdview.vfore)) { |
465 |
+ |
sprintf(cp, " -vo %.6g", vp->vfore); |
466 |
+ |
cp += strlen(cp); |
467 |
+ |
} |
468 |
+ |
if (!FEQ(vp->vaft,stdview.vaft)) { |
469 |
+ |
sprintf(cp, " -va %.6g", vp->vaft); |
470 |
+ |
cp += strlen(cp); |
471 |
+ |
} |
472 |
+ |
if (!FEQ(vp->hoff,stdview.hoff)) { |
473 |
+ |
sprintf(cp, " -vs %.6g", vp->hoff); |
474 |
+ |
cp += strlen(cp); |
475 |
+ |
} |
476 |
+ |
if (!FEQ(vp->voff,stdview.voff)) { |
477 |
+ |
sprintf(cp, " -vl %.6g", vp->voff); |
478 |
+ |
cp += strlen(cp); |
479 |
+ |
} |
480 |
+ |
return(vwstr); |
481 |
+ |
} |
482 |
+ |
|
483 |
+ |
|
484 |
+ |
int |
485 |
+ |
isview(s) /* is this a view string? */ |
486 |
+ |
char *s; |
487 |
+ |
{ |
488 |
+ |
static char *altname[]={NULL,VIEWSTR,"rpict","rview","pinterp",NULL}; |
489 |
+ |
extern char *progname; |
490 |
+ |
register char *cp; |
491 |
+ |
register char **an; |
492 |
+ |
/* add program name to list */ |
493 |
+ |
if (altname[0] == NULL) { |
494 |
+ |
for (cp = progname; *cp; cp++) |
495 |
+ |
; |
496 |
+ |
while (cp > progname && !ISDIRSEP(cp[-1])) |
497 |
+ |
cp--; |
498 |
+ |
altname[0] = cp; |
499 |
+ |
} |
500 |
+ |
/* skip leading path */ |
501 |
+ |
cp = s; |
502 |
+ |
while (*cp && *cp != ' ') |
503 |
+ |
cp++; |
504 |
+ |
while (cp > s && !ISDIRSEP(cp[-1])) |
505 |
+ |
cp--; |
506 |
+ |
for (an = altname; *an != NULL; an++) |
507 |
+ |
if (!strncmp(*an, cp, strlen(*an))) |
508 |
+ |
return(1); |
509 |
+ |
return(0); |
510 |
+ |
} |
511 |
+ |
|
512 |
+ |
|
513 |
|
struct myview { |
514 |
|
VIEW *hv; |
515 |
|
int ok; |
516 |
|
}; |
517 |
|
|
518 |
|
|
519 |
< |
static |
519 |
> |
static int |
520 |
|
gethview(s, v) /* get view from header */ |
521 |
|
char *s; |
522 |
|
register struct myview *v; |
523 |
|
{ |
524 |
< |
register char **an; |
525 |
< |
|
526 |
< |
for (an = altname; *an != NULL; an++) |
392 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
393 |
< |
if (sscanview(v->hv, s+strlen(*an)) > 0) |
394 |
< |
v->ok++; |
395 |
< |
return; |
396 |
< |
} |
524 |
> |
if (isview(s) && sscanview(v->hv, s) > 0) |
525 |
> |
v->ok++; |
526 |
> |
return(0); |
527 |
|
} |
528 |
|
|
529 |
|
|
533 |
|
VIEW *vp; |
534 |
|
RESOLU *rp; |
535 |
|
{ |
406 |
– |
extern char *progname; |
536 |
|
struct myview mvs; |
537 |
|
FILE *fp; |
538 |
|
|
539 |
|
if ((fp = fopen(fname, "r")) == NULL) |
540 |
|
return(-1); |
541 |
|
|
413 |
– |
altname[0] = progname; |
542 |
|
mvs.hv = vp; |
543 |
|
mvs.ok = 0; |
544 |
|
|