1 |
greg |
1.1 |
/* Copyright (c) 1987 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* rv3.c - miscellaneous routines for rview. |
9 |
|
|
* |
10 |
|
|
* 5/11/87 |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include "ray.h" |
14 |
|
|
|
15 |
|
|
#include "rpaint.h" |
16 |
|
|
|
17 |
|
|
#include "random.h" |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
float * /* keep consistent with COLOR typedef */ |
21 |
|
|
greyof(col) /* convert color to greyscale */ |
22 |
|
|
register COLOR col; |
23 |
|
|
{ |
24 |
|
|
static COLOR gcol; |
25 |
|
|
double b; |
26 |
|
|
|
27 |
|
|
b = bright(col); |
28 |
|
|
setcolor(gcol, b, b, b); |
29 |
|
|
return(gcol); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
paint(p, xmin, ymin, xmax, ymax) /* compute and paint a rectangle */ |
34 |
|
|
register PNODE *p; |
35 |
|
|
int xmin, ymin, xmax, ymax; |
36 |
|
|
{ |
37 |
|
|
static RAY thisray; |
38 |
|
|
double h, v; |
39 |
|
|
register int i; |
40 |
|
|
|
41 |
|
|
if (xmax - xmin <= 0 || ymax - ymin <= 0) { /* empty */ |
42 |
|
|
p->x = xmin; |
43 |
|
|
p->y = ymin; |
44 |
|
|
setcolor(p->v, 0.0, 0.0, 0.0); |
45 |
|
|
return; |
46 |
|
|
} |
47 |
|
|
/* jitter ray direction */ |
48 |
|
|
p->x = h = xmin + (xmax-xmin)*frandom(); |
49 |
|
|
p->y = v = ymin + (ymax-ymin)*frandom(); |
50 |
|
|
|
51 |
|
|
rayview(thisray.rorg, thisray.rdir, &ourview, h, v); |
52 |
|
|
|
53 |
|
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
54 |
|
|
|
55 |
|
|
rayvalue(&thisray); |
56 |
|
|
|
57 |
|
|
copycolor(p->v, thisray.rcol); |
58 |
|
|
|
59 |
|
|
scalecolor(p->v, exposure); |
60 |
|
|
|
61 |
|
|
(*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
newimage() /* start a new image */ |
66 |
|
|
{ |
67 |
|
|
/* free old image */ |
68 |
|
|
freepkids(&ptrunk); |
69 |
|
|
/* set up frame */ |
70 |
|
|
if (ourview.hresolu > dev->xsiz || ourview.vresolu > dev->ysiz) |
71 |
greg |
1.6 |
newview(&ourview); /* beware recursive loop! */ |
72 |
greg |
1.1 |
pframe.l = pframe.d = 0; |
73 |
|
|
pframe.r = ourview.hresolu; pframe.u = ourview.vresolu; |
74 |
|
|
pdepth = 0; |
75 |
|
|
/* clear device */ |
76 |
|
|
(*dev->clear)(ourview.hresolu, ourview.vresolu); |
77 |
|
|
/* get first value */ |
78 |
|
|
paint(&ptrunk, 0, 0, ourview.hresolu, ourview.vresolu); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
redraw() /* redraw the image */ |
83 |
|
|
{ |
84 |
|
|
(*dev->clear)(ourview.hresolu, ourview.vresolu); |
85 |
|
|
(*dev->comout)("redrawing...\n"); |
86 |
|
|
repaint(0, 0, ourview.hresolu, ourview.vresolu); |
87 |
|
|
(*dev->comout)("\n"); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
repaint(xmin, ymin, xmax, ymax) /* repaint a region */ |
92 |
|
|
int xmin, ymin, xmax, ymax; |
93 |
|
|
{ |
94 |
|
|
RECT reg; |
95 |
|
|
|
96 |
|
|
reg.l = xmin; reg.r = xmax; |
97 |
|
|
reg.d = ymin; reg.u = ymax; |
98 |
|
|
|
99 |
|
|
paintrect(&ptrunk, 0, 0, ourview.hresolu, ourview.vresolu, ®); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
|
|
paintrect(p, xmin, ymin, xmax, ymax, r) /* paint picture rectangle */ |
104 |
|
|
register PNODE *p; |
105 |
|
|
int xmin, ymin, xmax, ymax; |
106 |
|
|
register RECT *r; |
107 |
|
|
{ |
108 |
|
|
int mx, my; |
109 |
|
|
|
110 |
|
|
if (dev->inpready) |
111 |
|
|
return; /* break for input */ |
112 |
|
|
|
113 |
|
|
if (xmax - xmin <= 0 || ymax - ymin <= 0) |
114 |
|
|
return; |
115 |
|
|
|
116 |
|
|
if (p->kid == NULL) { |
117 |
|
|
(*dev->paintr)(greyscale?greyof(p->v):p->v, |
118 |
|
|
xmin, ymin, xmax, ymax); /* do this */ |
119 |
|
|
return; |
120 |
|
|
} |
121 |
|
|
mx = (xmin + xmax) >> 1; /* do kids */ |
122 |
|
|
my = (ymin + ymax) >> 1; |
123 |
|
|
if (mx > r->l) { |
124 |
|
|
if (my > r->d) |
125 |
|
|
paintrect(p->kid+DL, xmin, ymin, mx, my, r); |
126 |
|
|
if (my < r->u) |
127 |
|
|
paintrect(p->kid+UL, xmin, my, mx, ymax, r); |
128 |
|
|
} |
129 |
|
|
if (mx < r->r) { |
130 |
|
|
if (my > r->d) |
131 |
|
|
paintrect(p->kid+DR, mx, ymin, xmax, my, r); |
132 |
|
|
if (my < r->u) |
133 |
|
|
paintrect(p->kid+UR, mx, my, xmax, ymax, r); |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
|
138 |
|
|
PNODE * |
139 |
|
|
findrect(x, y, p, r, pd) /* find a rectangle */ |
140 |
|
|
int x, y; |
141 |
|
|
register PNODE *p; |
142 |
|
|
register RECT *r; |
143 |
|
|
int pd; |
144 |
|
|
{ |
145 |
|
|
int mx, my; |
146 |
|
|
|
147 |
|
|
while (p->kid != NULL && pd--) { |
148 |
|
|
|
149 |
|
|
mx = (r->l + r->r) >> 1; |
150 |
|
|
my = (r->d + r->u) >> 1; |
151 |
|
|
|
152 |
|
|
if (x < mx) { |
153 |
|
|
r->r = mx; |
154 |
|
|
if (y < my) { |
155 |
|
|
r->u = my; |
156 |
|
|
p = p->kid+DL; |
157 |
|
|
} else { |
158 |
|
|
r->d = my; |
159 |
|
|
p = p->kid+UL; |
160 |
|
|
} |
161 |
|
|
} else { |
162 |
|
|
r->l = mx; |
163 |
|
|
if (y < my) { |
164 |
|
|
r->u = my; |
165 |
|
|
p = p->kid+DR; |
166 |
|
|
} else { |
167 |
|
|
r->d = my; |
168 |
|
|
p = p->kid+UR; |
169 |
|
|
} |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
return(p); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
scalepict(p, sf) /* scale picture values */ |
177 |
|
|
register PNODE *p; |
178 |
|
|
double sf; |
179 |
|
|
{ |
180 |
|
|
scalecolor(p->v, sf); /* do this node */ |
181 |
|
|
|
182 |
|
|
if (p->kid == NULL) |
183 |
|
|
return; |
184 |
|
|
/* do children */ |
185 |
|
|
scalepict(p->kid+DL, sf); |
186 |
|
|
scalepict(p->kid+DR, sf); |
187 |
|
|
scalepict(p->kid+UL, sf); |
188 |
|
|
scalepict(p->kid+UR, sf); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
|
192 |
|
|
getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */ |
193 |
|
|
int yoff; |
194 |
|
|
register COLR *scan; |
195 |
|
|
register PNODE *p; |
196 |
|
|
int xsiz, ysiz; |
197 |
|
|
{ |
198 |
|
|
register int mx; |
199 |
|
|
int my; |
200 |
|
|
|
201 |
|
|
if (p->kid == NULL) { /* do this node */ |
202 |
|
|
setcolr(scan[0], colval(p->v,RED), |
203 |
|
|
colval(p->v,GRN), |
204 |
|
|
colval(p->v,BLU)); |
205 |
|
|
for (mx = 1; mx < xsiz; mx++) |
206 |
|
|
copycolr(scan[mx], scan[0]); |
207 |
|
|
return; |
208 |
|
|
} |
209 |
|
|
/* do kids */ |
210 |
|
|
mx = xsiz >> 1; |
211 |
|
|
my = ysiz >> 1; |
212 |
|
|
if (yoff < my) { |
213 |
|
|
getpictcolrs(yoff, scan, p->kid+DL, mx, my); |
214 |
|
|
getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my); |
215 |
|
|
} else { |
216 |
|
|
getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my); |
217 |
|
|
getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my); |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
|
222 |
|
|
pcopy(p1, p2) /* copy paint node p1 into p2 */ |
223 |
|
|
register PNODE *p1, *p2; |
224 |
|
|
{ |
225 |
|
|
copycolor(p2->v, p1->v); |
226 |
|
|
p2->x = p1->x; |
227 |
|
|
p2->y = p1->y; |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
|
231 |
|
|
freepkids(p) /* free pnode's children */ |
232 |
|
|
register PNODE *p; |
233 |
|
|
{ |
234 |
|
|
if (p->kid == NULL) |
235 |
|
|
return; |
236 |
|
|
freepkids(p->kid+DL); |
237 |
|
|
freepkids(p->kid+DR); |
238 |
|
|
freepkids(p->kid+UL); |
239 |
|
|
freepkids(p->kid+UR); |
240 |
|
|
free((char *)p->kid); |
241 |
|
|
p->kid = NULL; |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
|
245 |
|
|
newview(vp) /* change viewing parameters */ |
246 |
|
|
register VIEW *vp; |
247 |
|
|
{ |
248 |
|
|
char *err; |
249 |
|
|
|
250 |
greg |
1.6 |
if (vp->hresolu > dev->xsiz || vp->vresolu > dev->ysiz) /* shrink */ |
251 |
|
|
if (vp->vresolu * dev->xsiz < vp->hresolu * dev->ysiz) { |
252 |
|
|
vp->vresolu = dev->xsiz * vp->vresolu / vp->hresolu; |
253 |
|
|
vp->hresolu = dev->xsiz; |
254 |
|
|
} else { |
255 |
|
|
vp->hresolu = dev->ysiz * vp->hresolu / vp->vresolu; |
256 |
|
|
vp->vresolu = dev->ysiz; |
257 |
|
|
} |
258 |
|
|
if ((err = setview(vp)) != NULL) { |
259 |
greg |
1.1 |
sprintf(errmsg, "view not set - %s", err); |
260 |
|
|
error(COMMAND, errmsg); |
261 |
|
|
} else if (bcmp(vp, &ourview, sizeof(VIEW))) { |
262 |
|
|
bcopy(&ourview, &oldview, sizeof(VIEW)); |
263 |
|
|
bcopy(vp, &ourview, sizeof(VIEW)); |
264 |
greg |
1.6 |
newimage(); /* newimage() calls with vp=&ourview! */ |
265 |
greg |
1.1 |
} |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
|
269 |
greg |
1.4 |
moveview(angle, elev, mag, vc) /* move viewpoint */ |
270 |
|
|
double angle, elev, mag; |
271 |
greg |
1.1 |
FVECT vc; |
272 |
|
|
{ |
273 |
|
|
extern double sqrt(), dist2(); |
274 |
|
|
double d; |
275 |
greg |
1.4 |
FVECT v1; |
276 |
greg |
1.1 |
VIEW nv; |
277 |
|
|
register int i; |
278 |
|
|
|
279 |
|
|
VCOPY(nv.vup, ourview.vup); |
280 |
|
|
nv.hresolu = ourview.hresolu; nv.vresolu = ourview.vresolu; |
281 |
|
|
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
282 |
greg |
1.4 |
if (elev != 0.0) { |
283 |
greg |
1.5 |
fcross(v1, ourview.vup, nv.vdir); |
284 |
greg |
1.4 |
normalize(v1); |
285 |
|
|
spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); |
286 |
|
|
} |
287 |
greg |
1.1 |
if ((nv.type = ourview.type) == VT_PAR) { |
288 |
|
|
nv.horiz = ourview.horiz / mag; |
289 |
|
|
nv.vert = ourview.vert / mag; |
290 |
greg |
1.3 |
d = 0.0; /* don't move closer */ |
291 |
greg |
1.2 |
for (i = 0; i < 3; i++) |
292 |
greg |
1.3 |
d += (vc[i] - ourview.vp[i])*ourview.vdir[i]; |
293 |
greg |
1.1 |
} else { |
294 |
|
|
nv.horiz = ourview.horiz; |
295 |
|
|
nv.vert = ourview.vert; |
296 |
greg |
1.2 |
d = sqrt(dist2(ourview.vp, vc)) / mag; |
297 |
greg |
1.1 |
} |
298 |
greg |
1.2 |
for (i = 0; i < 3; i++) |
299 |
|
|
nv.vp[i] = vc[i] - d*nv.vdir[i]; |
300 |
greg |
1.1 |
newview(&nv); |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
|
304 |
|
|
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
305 |
|
|
FVECT vres, vorig, vnorm; |
306 |
|
|
double theta; |
307 |
|
|
{ |
308 |
|
|
extern double sin(), cos(); |
309 |
|
|
double sint, cost, dotp; |
310 |
|
|
FVECT vperp; |
311 |
|
|
register int i; |
312 |
|
|
|
313 |
|
|
if (theta == 0.0) { |
314 |
|
|
VCOPY(vres, vorig); |
315 |
|
|
return; |
316 |
|
|
} |
317 |
|
|
sint = sin(theta); |
318 |
|
|
cost = cos(theta); |
319 |
|
|
dotp = DOT(vorig, vnorm); |
320 |
|
|
fcross(vperp, vnorm, vorig); |
321 |
|
|
for (i = 0; i < 3; i++) |
322 |
|
|
vres[i] = vnorm[i]*dotp*(1.-cost) + |
323 |
|
|
vorig[i]*cost + vperp[i]*sint; |
324 |
|
|
} |