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