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 |
error(USER, "resolution mismatch"); |
72 |
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 |
if (vp->hresolu > dev->xsiz || vp->vresolu > dev->ysiz) { |
251 |
error(COMMAND, "view not set - resolution mismatch"); |
252 |
} else if ((err = setview(vp)) != NULL) { |
253 |
sprintf(errmsg, "view not set - %s", err); |
254 |
error(COMMAND, errmsg); |
255 |
} else if (bcmp(vp, &ourview, sizeof(VIEW))) { |
256 |
bcopy(&ourview, &oldview, sizeof(VIEW)); |
257 |
bcopy(vp, &ourview, sizeof(VIEW)); |
258 |
newimage(); |
259 |
} |
260 |
} |
261 |
|
262 |
|
263 |
moveview(angle, mag, vc) /* move viewpoint */ |
264 |
double angle, mag; |
265 |
FVECT vc; |
266 |
{ |
267 |
extern double sqrt(), dist2(); |
268 |
double d; |
269 |
VIEW nv; |
270 |
register int i; |
271 |
|
272 |
VCOPY(nv.vup, ourview.vup); |
273 |
nv.hresolu = ourview.hresolu; nv.vresolu = ourview.vresolu; |
274 |
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
275 |
if ((nv.type = ourview.type) == VT_PAR) { |
276 |
nv.horiz = ourview.horiz / mag; |
277 |
nv.vert = ourview.vert / mag; |
278 |
d = 0.0; /* don't move closer */ |
279 |
for (i = 0; i < 3; i++) |
280 |
d += (vc[i] - ourview.vp[i])*ourview.vdir[i]; |
281 |
} else { |
282 |
nv.horiz = ourview.horiz; |
283 |
nv.vert = ourview.vert; |
284 |
d = sqrt(dist2(ourview.vp, vc)) / mag; |
285 |
} |
286 |
for (i = 0; i < 3; i++) |
287 |
nv.vp[i] = vc[i] - d*nv.vdir[i]; |
288 |
newview(&nv); |
289 |
} |
290 |
|
291 |
|
292 |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
293 |
FVECT vres, vorig, vnorm; |
294 |
double theta; |
295 |
{ |
296 |
extern double sin(), cos(); |
297 |
double sint, cost, dotp; |
298 |
FVECT vperp; |
299 |
register int i; |
300 |
|
301 |
if (theta == 0.0) { |
302 |
VCOPY(vres, vorig); |
303 |
return; |
304 |
} |
305 |
sint = sin(theta); |
306 |
cost = cos(theta); |
307 |
dotp = DOT(vorig, vnorm); |
308 |
fcross(vperp, vnorm, vorig); |
309 |
for (i = 0; i < 3; i++) |
310 |
vres[i] = vnorm[i]*dotp*(1.-cost) + |
311 |
vorig[i]*cost + vperp[i]*sint; |
312 |
} |