1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* image.c - routines for image generation. |
9 |
* |
10 |
* 10/17/85 |
11 |
*/ |
12 |
|
13 |
#include "standard.h" |
14 |
|
15 |
#include "view.h" |
16 |
|
17 |
VIEW stdview = STDVIEW; /* default view parameters */ |
18 |
|
19 |
|
20 |
char * |
21 |
setview(v) /* set hvec and vvec, return message on error */ |
22 |
register VIEW *v; |
23 |
{ |
24 |
extern double tan(), normalize(); |
25 |
|
26 |
if (normalize(v->vdir) == 0.0) /* normalize direction */ |
27 |
return("zero view direction"); |
28 |
|
29 |
fcross(v->hvec, v->vdir, v->vup); /* compute horiz dir */ |
30 |
|
31 |
if (normalize(v->hvec) == 0.0) |
32 |
return("illegal view up vector"); |
33 |
|
34 |
fcross(v->vvec, v->hvec, v->vdir); /* compute vert dir */ |
35 |
|
36 |
if (v->type == VT_PAR) |
37 |
v->hn2 = v->horiz; |
38 |
else if (v->type == VT_PER) |
39 |
v->hn2 = 2.0 * tan(v->horiz*(PI/180.0/2.0)); |
40 |
else |
41 |
return("unknown view type"); |
42 |
|
43 |
if (v->hn2 <= FTINY || v->hn2 >= FHUGE) |
44 |
return("illegal horizontal view size"); |
45 |
|
46 |
v->hvec[0] *= v->hn2; |
47 |
v->hvec[1] *= v->hn2; |
48 |
v->hvec[2] *= v->hn2; |
49 |
v->hn2 *= v->hn2; |
50 |
|
51 |
if (v->type == VT_PAR) |
52 |
v->vn2 = v->vert; |
53 |
else |
54 |
v->vn2 = 2.0 * tan(v->vert*(PI/180.0/2.0)); |
55 |
|
56 |
if (v->vn2 <= FTINY || v->vn2 >= FHUGE) |
57 |
return("illegal vertical view size"); |
58 |
|
59 |
v->vvec[0] *= v->vn2; |
60 |
v->vvec[1] *= v->vn2; |
61 |
v->vvec[2] *= v->vn2; |
62 |
v->vn2 *= v->vn2; |
63 |
|
64 |
return(NULL); |
65 |
} |
66 |
|
67 |
|
68 |
viewray(orig, direc, v, x, y) /* compute ray origin and direction */ |
69 |
FVECT orig, direc; |
70 |
register VIEW *v; |
71 |
double x, y; |
72 |
{ |
73 |
x += v->hoff - 0.5; |
74 |
y += v->voff - 0.5; |
75 |
|
76 |
if (v->type == VT_PAR) { /* parallel view */ |
77 |
orig[0] = v->vp[0] + x*v->hvec[0] + y*v->vvec[0]; |
78 |
orig[1] = v->vp[1] + x*v->hvec[1] + y*v->vvec[1]; |
79 |
orig[2] = v->vp[2] + x*v->hvec[2] + y*v->vvec[2]; |
80 |
VCOPY(direc, v->vdir); |
81 |
} else { /* perspective view */ |
82 |
VCOPY(orig, v->vp); |
83 |
direc[0] = v->vdir[0] + x*v->hvec[0] + y*v->vvec[0]; |
84 |
direc[1] = v->vdir[1] + x*v->hvec[1] + y*v->vvec[1]; |
85 |
direc[2] = v->vdir[2] + x*v->hvec[2] + y*v->vvec[2]; |
86 |
normalize(direc); |
87 |
} |
88 |
} |
89 |
|
90 |
|
91 |
viewpixel(xp, yp, zp, v, p) /* find image location for point */ |
92 |
double *xp, *yp, *zp; |
93 |
register VIEW *v; |
94 |
FVECT p; |
95 |
{ |
96 |
extern double sqrt(); |
97 |
double d; |
98 |
FVECT disp; |
99 |
|
100 |
disp[0] = p[0] - v->vp[0]; |
101 |
disp[1] = p[1] - v->vp[1]; |
102 |
disp[2] = p[2] - v->vp[2]; |
103 |
|
104 |
if (v->type == VT_PAR) { /* parallel view */ |
105 |
if (zp != NULL) |
106 |
*zp = DOT(disp,v->vdir); |
107 |
} else { /* perspective view */ |
108 |
d = 1.0/DOT(disp,v->vdir); |
109 |
if (zp != NULL) { |
110 |
*zp = sqrt(DOT(disp,disp)); |
111 |
if (d < 0.0) |
112 |
*zp = -*zp; |
113 |
} |
114 |
disp[0] *= d; |
115 |
disp[1] *= d; |
116 |
disp[2] *= d; |
117 |
} |
118 |
*xp = DOT(disp,v->hvec)/v->hn2 + 0.5 - v->hoff; |
119 |
*yp = DOT(disp,v->vvec)/v->vn2 + 0.5 - v->voff; |
120 |
} |
121 |
|
122 |
|
123 |
int |
124 |
getviewopt(v, ac, av) /* process view argument */ |
125 |
register VIEW *v; |
126 |
int ac; |
127 |
register char *av[]; |
128 |
{ |
129 |
#define check(c,n) if ((av[0][c]&&av[0][c]!=' ') || n>=ac) return(-1) |
130 |
extern double atof(); |
131 |
|
132 |
if (av[0][0] != '-' || av[0][1] != 'v') |
133 |
return(-1); |
134 |
switch (av[0][2]) { |
135 |
case 't': /* type */ |
136 |
if (!av[0][3] || av[0][3]==' ') |
137 |
return(-1); |
138 |
check(4,0); |
139 |
v->type = av[0][3]; |
140 |
return(0); |
141 |
case 'p': /* point */ |
142 |
check(3,3); |
143 |
v->vp[0] = atof(av[1]); |
144 |
v->vp[1] = atof(av[2]); |
145 |
v->vp[2] = atof(av[3]); |
146 |
return(3); |
147 |
case 'd': /* direction */ |
148 |
check(3,3); |
149 |
v->vdir[0] = atof(av[1]); |
150 |
v->vdir[1] = atof(av[2]); |
151 |
v->vdir[2] = atof(av[3]); |
152 |
return(3); |
153 |
case 'u': /* up */ |
154 |
check(3,3); |
155 |
v->vup[0] = atof(av[1]); |
156 |
v->vup[1] = atof(av[2]); |
157 |
v->vup[2] = atof(av[3]); |
158 |
return(3); |
159 |
case 'h': /* horizontal size */ |
160 |
check(3,1); |
161 |
v->horiz = atof(av[1]); |
162 |
return(1); |
163 |
case 'v': /* vertical size */ |
164 |
check(3,1); |
165 |
v->vert = atof(av[1]); |
166 |
return(1); |
167 |
case 's': /* shift */ |
168 |
check(3,1); |
169 |
v->hoff = atof(av[1]); |
170 |
return(1); |
171 |
case 'l': /* lift */ |
172 |
check(3,1); |
173 |
v->voff = atof(av[1]); |
174 |
return(1); |
175 |
default: |
176 |
return(-1); |
177 |
} |
178 |
#undef check |
179 |
} |
180 |
|
181 |
|
182 |
int |
183 |
sscanview(vp, s) /* get view parameters from string */ |
184 |
VIEW *vp; |
185 |
register char *s; |
186 |
{ |
187 |
int ac; |
188 |
char *av[4]; |
189 |
int na; |
190 |
int nvopts = 0; |
191 |
|
192 |
while (*s == ' ') |
193 |
s++; |
194 |
do { |
195 |
ac = 0; |
196 |
do { |
197 |
av[ac++] = s; |
198 |
while (*s && *s != ' ') |
199 |
s++; |
200 |
while (*s == ' ') |
201 |
s++; |
202 |
} while (*s && ac < 4); |
203 |
if ((na = getviewopt(vp, ac, av)) >= 0) { |
204 |
if (na+1 < ac) |
205 |
s = av[na+1]; |
206 |
nvopts++; |
207 |
} |
208 |
} while (*s); |
209 |
return(nvopts); |
210 |
} |
211 |
|
212 |
|
213 |
fprintview(vp, fp) /* write out view parameters */ |
214 |
register VIEW *vp; |
215 |
FILE *fp; |
216 |
{ |
217 |
fprintf(fp, " -vt%c", vp->type); |
218 |
fprintf(fp, " -vp %.6g %.6g %.6g", vp->vp[0], vp->vp[1], vp->vp[2]); |
219 |
fprintf(fp, " -vd %.6g %.6g %.6g", vp->vdir[0], vp->vdir[1], vp->vdir[2]); |
220 |
fprintf(fp, " -vu %.6g %.6g %.6g", vp->vup[0], vp->vup[1], vp->vup[2]); |
221 |
fprintf(fp, " -vh %.6g -vv %.6g", vp->horiz, vp->vert); |
222 |
fprintf(fp, " -vs %d -vl %d", vp->hoff, vp->voff); |
223 |
} |
224 |
|
225 |
|
226 |
static VIEW *hview; /* view from header */ |
227 |
static int gothview; /* success indicator */ |
228 |
static char *altname[] = {NULL,"rpict","rview","pinterp",VIEWSTR,NULL}; |
229 |
|
230 |
|
231 |
static |
232 |
gethview(s) /* get view from header */ |
233 |
char *s; |
234 |
{ |
235 |
register char **an; |
236 |
|
237 |
for (an = altname; *an != NULL; an++) |
238 |
if (!strncmp(*an, s, strlen(*an))) { |
239 |
if (sscanview(hview, s+strlen(*an)) > 0) |
240 |
gothview++; |
241 |
return; |
242 |
} |
243 |
} |
244 |
|
245 |
|
246 |
int |
247 |
viewfile(fname, vp) /* get view from file */ |
248 |
char *fname; |
249 |
VIEW *vp; |
250 |
{ |
251 |
extern char *progname; |
252 |
FILE *fp; |
253 |
|
254 |
if ((fp = fopen(fname, "r")) == NULL) |
255 |
return(-1); |
256 |
|
257 |
altname[0] = progname; |
258 |
hview = vp; |
259 |
gothview = 0; |
260 |
|
261 |
getheader(fp, gethview); |
262 |
|
263 |
fclose(fp); |
264 |
|
265 |
return(gothview); |
266 |
} |