1 |
< |
/* Copyright (c) 1992 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" |
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->vfore < -FTINY || v->vaft < -FTINY || |
34 |
+ |
(v->vaft > FTINY && v->vaft <= v->vfore)) |
35 |
+ |
return("illegal fore/aft clipping plane"); |
36 |
+ |
|
37 |
|
if (normalize(v->vdir) == 0.0) /* normalize direction */ |
38 |
|
return("zero view direction"); |
39 |
|
|
65 |
|
v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0)); |
66 |
|
v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0)); |
67 |
|
break; |
68 |
+ |
case VT_CYL: /* cylindrical panorama */ |
69 |
+ |
if (v->horiz > 360.0+FTINY) |
70 |
+ |
return(ill_horiz); |
71 |
+ |
if (v->vert >= 180.0-FTINY) |
72 |
+ |
return(ill_vert); |
73 |
+ |
v->hn2 = v->horiz * (PI/180.0); |
74 |
+ |
v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0)); |
75 |
+ |
break; |
76 |
|
case VT_ANG: /* angular fisheye */ |
77 |
|
if (v->horiz > 360.0+FTINY) |
78 |
|
return(ill_horiz); |
79 |
|
if (v->vert > 360.0+FTINY) |
80 |
|
return(ill_vert); |
81 |
< |
v->hn2 = v->horiz / 90.0; |
82 |
< |
v->vn2 = v->vert / 90.0; |
81 |
> |
v->hn2 = v->horiz * (PI/180.0); |
82 |
> |
v->vn2 = v->vert * (PI/180.0); |
83 |
|
break; |
84 |
|
case VT_HEM: /* hemispherical fisheye */ |
85 |
|
if (v->horiz > 180.0+FTINY) |
93 |
|
return("unknown view type"); |
94 |
|
} |
95 |
|
if (v->type != VT_ANG) { |
96 |
< |
v->hvec[0] *= v->hn2; |
97 |
< |
v->hvec[1] *= v->hn2; |
98 |
< |
v->hvec[2] *= v->hn2; |
96 |
> |
if (v->type != VT_CYL) { |
97 |
> |
v->hvec[0] *= v->hn2; |
98 |
> |
v->hvec[1] *= v->hn2; |
99 |
> |
v->hvec[2] *= v->hn2; |
100 |
> |
} |
101 |
|
v->vvec[0] *= v->vn2; |
102 |
|
v->vvec[1] *= v->vn2; |
103 |
|
v->vvec[2] *= v->vn2; |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
+ |
double |
127 |
|
viewray(orig, direc, v, x, y) /* compute ray origin and direction */ |
128 |
|
FVECT orig, direc; |
129 |
|
register VIEW *v; |
136 |
|
|
137 |
|
switch(v->type) { |
138 |
|
case VT_PAR: /* parallel view */ |
139 |
< |
orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0]; |
140 |
< |
orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1]; |
141 |
< |
orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2]; |
139 |
> |
orig[0] = v->vp[0] + v->vfore*v->vdir[0] |
140 |
> |
+ x*v->hvec[0] + y*v->vvec[0]; |
141 |
> |
orig[1] = v->vp[1] + v->vfore*v->vdir[1] |
142 |
> |
+ x*v->hvec[1] + y*v->vvec[1]; |
143 |
> |
orig[2] = v->vp[2] + v->vfore*v->vdir[2] |
144 |
> |
+ x*v->hvec[2] + y*v->vvec[2]; |
145 |
|
VCOPY(direc, v->vdir); |
146 |
< |
return(0); |
146 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
147 |
|
case VT_PER: /* perspective view */ |
128 |
– |
VCOPY(orig, v->vp); |
148 |
|
direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
149 |
|
direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
150 |
|
direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
151 |
< |
normalize(direc); |
152 |
< |
return(0); |
151 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
152 |
> |
orig[1] = v->vp[1] + v->vfore*direc[1]; |
153 |
> |
orig[2] = v->vp[2] + v->vfore*direc[2]; |
154 |
> |
d = normalize(direc); |
155 |
> |
return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0); |
156 |
|
case VT_HEM: /* hemispherical fisheye */ |
157 |
|
z = 1.0 - x*x*v->hn2 - y*y*v->vn2; |
158 |
|
if (z < 0.0) |
159 |
< |
return(-1); |
159 |
> |
return(-1.0); |
160 |
|
z = sqrt(z); |
139 |
– |
VCOPY(orig, v->vp); |
161 |
|
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
162 |
|
direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
163 |
|
direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
164 |
< |
return(0); |
164 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
165 |
> |
orig[1] = v->vp[1] + v->vfore*direc[1]; |
166 |
> |
orig[2] = v->vp[2] + v->vfore*direc[2]; |
167 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
168 |
> |
case VT_CYL: /* cylindrical panorama */ |
169 |
> |
d = x * v->horiz * (PI/180.0); |
170 |
> |
z = cos(d); |
171 |
> |
x = sin(d); |
172 |
> |
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
173 |
> |
direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
174 |
> |
direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
175 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
176 |
> |
orig[1] = v->vp[1] + v->vfore*direc[1]; |
177 |
> |
orig[2] = v->vp[2] + v->vfore*direc[2]; |
178 |
> |
d = normalize(direc); |
179 |
> |
return(v->vaft > FTINY ? (v->vaft - v->vfore)*d : 0.0); |
180 |
|
case VT_ANG: /* angular fisheye */ |
181 |
|
x *= v->horiz/180.0; |
182 |
|
y *= v->vert/180.0; |
183 |
|
d = x*x + y*y; |
184 |
|
if (d > 1.0) |
185 |
< |
return(-1); |
150 |
< |
VCOPY(orig, v->vp); |
151 |
< |
if (d <= FTINY) { |
152 |
< |
VCOPY(direc, v->vdir); |
153 |
< |
return(0); |
154 |
< |
} |
185 |
> |
return(-1.0); |
186 |
|
d = sqrt(d); |
187 |
|
z = cos(PI*d); |
188 |
< |
d = sqrt(1 - z*z)/d; |
188 |
> |
d = d <= FTINY ? PI : sqrt(1 - z*z)/d; |
189 |
|
x *= d; |
190 |
|
y *= d; |
191 |
|
direc[0] = z*v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
192 |
|
direc[1] = z*v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
193 |
|
direc[2] = z*v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
194 |
< |
return(0); |
194 |
> |
orig[0] = v->vp[0] + v->vfore*direc[0]; |
195 |
> |
orig[1] = v->vp[1] + v->vfore*direc[1]; |
196 |
> |
orig[2] = v->vp[2] + v->vfore*direc[2]; |
197 |
> |
return(v->vaft > FTINY ? v->vaft - v->vfore : 0.0); |
198 |
|
} |
199 |
< |
return(-1); |
199 |
> |
return(-1.0); |
200 |
|
} |
201 |
|
|
202 |
|
|
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]; |
214 |
|
|
215 |
|
switch (v->type) { |
216 |
|
case VT_PAR: /* parallel view */ |
217 |
< |
ip[2] = DOT(disp,v->vdir); |
217 |
> |
ip[2] = DOT(disp,v->vdir) - v->vfore; |
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; |
225 |
< |
} else if (d > FTINY) { |
225 |
> |
} |
226 |
> |
if (d > FTINY) { |
227 |
|
d = 1.0/d; |
228 |
|
disp[0] *= d; |
229 |
|
disp[1] *= d; |
230 |
|
disp[2] *= d; |
231 |
|
} |
232 |
+ |
ip[2] *= (1.0 - v->vfore*d); |
233 |
|
break; |
234 |
|
case VT_HEM: /* hemispherical fisheye */ |
235 |
|
d = normalize(disp); |
237 |
|
ip[2] = -d; |
238 |
|
else |
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 |
+ |
if (v->vfore > FTINY) |
250 |
+ |
ip[2] *= (1.0 - v->vfore*d); |
251 |
+ |
return; |
252 |
|
case VT_ANG: /* angular fisheye */ |
253 |
|
ip[0] = 0.5 - v->hoff; |
254 |
|
ip[1] = 0.5 - v->voff; |
255 |
< |
ip[2] = normalize(disp); |
255 |
> |
ip[2] = normalize(disp) - v->vfore; |
256 |
|
d = DOT(disp,v->vdir); |
257 |
|
if (d >= 1.0-FTINY) |
258 |
|
return; |
259 |
|
if (d <= -(1.0-FTINY)) { |
260 |
< |
ip[1] += 180.0/v->horiz; |
214 |
< |
ip[2] += 180.0/v->vert; |
260 |
> |
ip[0] += 180.0/v->horiz; |
261 |
|
return; |
262 |
|
} |
263 |
|
d = acos(d)/PI / sqrt(1.0 - d*d); |
360 |
|
check(3,"f"); |
361 |
|
v->vert = atof(av[1]); |
362 |
|
return(1); |
363 |
+ |
case 'o': /* fore clipping plane */ |
364 |
+ |
check(3,"f"); |
365 |
+ |
v->vfore = atof(av[1]); |
366 |
+ |
return(1); |
367 |
+ |
case 'a': /* aft clipping plane */ |
368 |
+ |
check(3,"f"); |
369 |
+ |
v->vaft = atof(av[1]); |
370 |
+ |
return(1); |
371 |
|
case 's': /* shift */ |
372 |
|
check(3,"f"); |
373 |
|
v->hoff = atof(av[1]); |
394 |
|
int nvopts = 0; |
395 |
|
|
396 |
|
if (*s != '-') |
397 |
< |
s = sskip(s); |
397 |
> |
s = sskip2(s,1); |
398 |
|
while (*s) { |
399 |
|
ac = 0; |
400 |
|
do { |
424 |
|
fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]); |
425 |
|
fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]); |
426 |
|
fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert); |
427 |
+ |
fprintf(fp, " -vo %.6g -va %.6g", vp->vfore, vp->vaft); |
428 |
|
fprintf(fp, " -vs %.6g -vl %.6g", vp->hoff, vp->voff); |
429 |
+ |
} |
430 |
+ |
|
431 |
+ |
|
432 |
+ |
char * |
433 |
+ |
viewopt(vp) /* translate to minimal view string */ |
434 |
+ |
register VIEW *vp; |
435 |
+ |
{ |
436 |
+ |
static char vwstr[128]; |
437 |
+ |
register char *cp = vwstr; |
438 |
+ |
|
439 |
+ |
if (vp->type != stdview.type) { |
440 |
+ |
sprintf(cp, " -vt%c", vp->type); |
441 |
+ |
cp += strlen(cp); |
442 |
+ |
} |
443 |
+ |
if (!VEQ(vp->vp,stdview.vp)) { |
444 |
+ |
sprintf(cp, " -vp %.6g %.6g %.6g", |
445 |
+ |
vp->vp[0], vp->vp[1], vp->vp[2]); |
446 |
+ |
cp += strlen(cp); |
447 |
+ |
} |
448 |
+ |
if (!VEQ(vp->vdir,stdview.vdir)) { |
449 |
+ |
sprintf(cp, " -vd %.6g %.6g %.6g", |
450 |
+ |
vp->vdir[0], vp->vdir[1], vp->vdir[2]); |
451 |
+ |
cp += strlen(cp); |
452 |
+ |
} |
453 |
+ |
if (!VEQ(vp->vup,stdview.vup)) { |
454 |
+ |
sprintf(cp, " -vu %.6g %.6g %.6g", |
455 |
+ |
vp->vup[0], vp->vup[1], vp->vup[2]); |
456 |
+ |
cp += strlen(cp); |
457 |
+ |
} |
458 |
+ |
if (!FEQ(vp->horiz,stdview.horiz)) { |
459 |
+ |
sprintf(cp, " -vh %.6g", vp->horiz); |
460 |
+ |
cp += strlen(cp); |
461 |
+ |
} |
462 |
+ |
if (!FEQ(vp->vert,stdview.vert)) { |
463 |
+ |
sprintf(cp, " -vv %.6g", vp->vert); |
464 |
+ |
cp += strlen(cp); |
465 |
+ |
} |
466 |
+ |
if (!FEQ(vp->vfore,stdview.vfore)) { |
467 |
+ |
sprintf(cp, " -vo %.6g", vp->vfore); |
468 |
+ |
cp += strlen(cp); |
469 |
+ |
} |
470 |
+ |
if (!FEQ(vp->vaft,stdview.vaft)) { |
471 |
+ |
sprintf(cp, " -va %.6g", vp->vaft); |
472 |
+ |
cp += strlen(cp); |
473 |
+ |
} |
474 |
+ |
if (!FEQ(vp->hoff,stdview.hoff)) { |
475 |
+ |
sprintf(cp, " -vs %.6g", vp->hoff); |
476 |
+ |
cp += strlen(cp); |
477 |
+ |
} |
478 |
+ |
if (!FEQ(vp->voff,stdview.voff)) { |
479 |
+ |
sprintf(cp, " -vl %.6g", vp->voff); |
480 |
+ |
cp += strlen(cp); |
481 |
+ |
} |
482 |
+ |
return(vwstr); |
483 |
|
} |
484 |
|
|
485 |
|
|