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 "octree.h" |
16 |
|
17 |
#include "rpaint.h" |
18 |
|
19 |
#include "random.h" |
20 |
|
21 |
#ifndef WFLUSH |
22 |
#define WFLUSH 30 /* flush after this many rays */ |
23 |
#endif |
24 |
|
25 |
|
26 |
getrect(s, r) /* get a box */ |
27 |
char *s; |
28 |
register RECT *r; |
29 |
{ |
30 |
int x0, y0, x1, y1; |
31 |
|
32 |
if (*s && !strncmp(s, "all", strlen(s))) { |
33 |
r->l = r->d = 0; |
34 |
r->r = hresolu; |
35 |
r->u = vresolu; |
36 |
return(0); |
37 |
} |
38 |
if (sscanf(s, "%d %d %d %d", &x0, &y0, &x1, &y1) != 4) { |
39 |
if (dev->getcur == NULL) |
40 |
return(-1); |
41 |
(*dev->comout)("Pick first corner\n"); |
42 |
if ((*dev->getcur)(&x0, &y0) == ABORT) |
43 |
return(-1); |
44 |
(*dev->comout)("Pick second corner\n"); |
45 |
if ((*dev->getcur)(&x1, &y1) == ABORT) |
46 |
return(-1); |
47 |
} |
48 |
if (x0 < x1) { |
49 |
r->l = x0; |
50 |
r->r = x1; |
51 |
} else { |
52 |
r->l = x1; |
53 |
r->r = x0; |
54 |
} |
55 |
if (y0 < y1) { |
56 |
r->d = y0; |
57 |
r->u = y1; |
58 |
} else { |
59 |
r->d = y1; |
60 |
r->u = y0; |
61 |
} |
62 |
if (r->l < 0) r->l = 0; |
63 |
if (r->d < 0) r->d = 0; |
64 |
if (r->r > hresolu) r->r = hresolu; |
65 |
if (r->u > vresolu) r->u = vresolu; |
66 |
if (r->l > r->r) r->l = r->r; |
67 |
if (r->d > r->u) r->d = r->u; |
68 |
return(0); |
69 |
} |
70 |
|
71 |
|
72 |
getinterest(s, direc, vec, mp) /* get area of interest */ |
73 |
char *s; |
74 |
int direc; |
75 |
FVECT vec; |
76 |
double *mp; |
77 |
{ |
78 |
int x, y; |
79 |
RAY thisray; |
80 |
register int i; |
81 |
|
82 |
if (sscanf(s, "%lf", mp) != 1) |
83 |
*mp = 1.0; |
84 |
else if (*mp < -FTINY) /* negative zoom is reduction */ |
85 |
*mp = -1.0 / *mp; |
86 |
else if (*mp <= FTINY) { /* too small */ |
87 |
error(COMMAND, "illegal magnification"); |
88 |
return(-1); |
89 |
} |
90 |
if (sscanf(s, "%*lf %lf %lf %lf", &vec[0], &vec[1], &vec[2]) != 3) { |
91 |
if (dev->getcur == NULL) |
92 |
return(-1); |
93 |
(*dev->comout)("Pick view center\n"); |
94 |
if ((*dev->getcur)(&x, &y) == ABORT) |
95 |
return(-1); |
96 |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
97 |
(x+.5)/hresolu, (y+.5)/vresolu) < 0) { |
98 |
error(COMMAND, "not on image"); |
99 |
return(-1); |
100 |
} |
101 |
if (!direc || ourview.type == VT_PAR) { |
102 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
103 |
if (!localhit(&thisray, &thescene)) { |
104 |
error(COMMAND, "not a local object"); |
105 |
return(-1); |
106 |
} |
107 |
} |
108 |
if (direc) |
109 |
if (ourview.type == VT_PAR) |
110 |
for (i = 0; i < 3; i++) |
111 |
vec[i] = thisray.rop[i] - ourview.vp[i]; |
112 |
else |
113 |
VCOPY(vec, thisray.rdir); |
114 |
else |
115 |
VCOPY(vec, thisray.rop); |
116 |
} else if (direc) |
117 |
for (i = 0; i < 3; i++) |
118 |
vec[i] -= ourview.vp[i]; |
119 |
return(0); |
120 |
} |
121 |
|
122 |
|
123 |
float * /* keep consistent with COLOR typedef */ |
124 |
greyof(col) /* convert color to greyscale */ |
125 |
register COLOR col; |
126 |
{ |
127 |
static COLOR gcol; |
128 |
double b; |
129 |
|
130 |
b = bright(col); |
131 |
setcolor(gcol, b, b, b); |
132 |
return(gcol); |
133 |
} |
134 |
|
135 |
|
136 |
paint(p, xmin, ymin, xmax, ymax) /* compute and paint a rectangle */ |
137 |
register PNODE *p; |
138 |
int xmin, ymin, xmax, ymax; |
139 |
{ |
140 |
extern long nrays; |
141 |
static long lastflush = 0; |
142 |
static RAY thisray; |
143 |
double h, v; |
144 |
|
145 |
if (xmax - xmin <= 0 || ymax - ymin <= 0) { /* empty */ |
146 |
p->x = xmin; |
147 |
p->y = ymin; |
148 |
setcolor(p->v, 0.0, 0.0, 0.0); |
149 |
return; |
150 |
} |
151 |
/* jitter ray direction */ |
152 |
h = xmin + (xmax-xmin)*frandom(); |
153 |
v = ymin + (ymax-ymin)*frandom(); |
154 |
|
155 |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
156 |
h/hresolu, v/vresolu) < 0) { |
157 |
setcolor(thisray.rcol, 0.0, 0.0, 0.0); |
158 |
} else { |
159 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
160 |
rayvalue(&thisray); |
161 |
} |
162 |
|
163 |
p->x = h; |
164 |
p->y = v; |
165 |
copycolor(p->v, thisray.rcol); |
166 |
scalecolor(p->v, exposure); |
167 |
|
168 |
(*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax); |
169 |
|
170 |
if (dev->flush != NULL && nrays - lastflush >= WFLUSH) { |
171 |
lastflush = nrays; |
172 |
(*dev->flush)(); |
173 |
} |
174 |
} |
175 |
|
176 |
|
177 |
newimage() /* start a new image */ |
178 |
{ |
179 |
/* free old image */ |
180 |
freepkids(&ptrunk); |
181 |
/* compute resolution */ |
182 |
hresolu = dev->xsiz; |
183 |
vresolu = dev->ysiz; |
184 |
normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu); |
185 |
pframe.l = pframe.d = 0; |
186 |
pframe.r = hresolu; pframe.u = vresolu; |
187 |
pdepth = 0; |
188 |
/* clear device */ |
189 |
(*dev->clear)(hresolu, vresolu); |
190 |
/* get first value */ |
191 |
paint(&ptrunk, 0, 0, hresolu, vresolu); |
192 |
} |
193 |
|
194 |
|
195 |
redraw() /* redraw the image */ |
196 |
{ |
197 |
(*dev->clear)(hresolu, vresolu); |
198 |
(*dev->comout)("redrawing...\n"); |
199 |
repaint(0, 0, hresolu, vresolu); |
200 |
(*dev->comout)("\n"); |
201 |
} |
202 |
|
203 |
|
204 |
repaint(xmin, ymin, xmax, ymax) /* repaint a region */ |
205 |
int xmin, ymin, xmax, ymax; |
206 |
{ |
207 |
RECT reg; |
208 |
|
209 |
reg.l = xmin; reg.r = xmax; |
210 |
reg.d = ymin; reg.u = ymax; |
211 |
|
212 |
paintrect(&ptrunk, 0, 0, hresolu, vresolu, ®); |
213 |
} |
214 |
|
215 |
|
216 |
paintrect(p, xmin, ymin, xmax, ymax, r) /* paint picture rectangle */ |
217 |
register PNODE *p; |
218 |
int xmin, ymin, xmax, ymax; |
219 |
register RECT *r; |
220 |
{ |
221 |
int mx, my; |
222 |
|
223 |
if (xmax - xmin <= 0 || ymax - ymin <= 0) |
224 |
return; |
225 |
|
226 |
if (p->kid == NULL) { |
227 |
(*dev->paintr)(greyscale?greyof(p->v):p->v, |
228 |
xmin, ymin, xmax, ymax); /* do this */ |
229 |
return; |
230 |
} |
231 |
mx = (xmin + xmax) >> 1; /* do kids */ |
232 |
my = (ymin + ymax) >> 1; |
233 |
if (mx > r->l) { |
234 |
if (my > r->d) |
235 |
paintrect(p->kid+DL, xmin, ymin, mx, my, r); |
236 |
if (my < r->u) |
237 |
paintrect(p->kid+UL, xmin, my, mx, ymax, r); |
238 |
} |
239 |
if (mx < r->r) { |
240 |
if (my > r->d) |
241 |
paintrect(p->kid+DR, mx, ymin, xmax, my, r); |
242 |
if (my < r->u) |
243 |
paintrect(p->kid+UR, mx, my, xmax, ymax, r); |
244 |
} |
245 |
} |
246 |
|
247 |
|
248 |
PNODE * |
249 |
findrect(x, y, p, r, pd) /* find a rectangle */ |
250 |
int x, y; |
251 |
register PNODE *p; |
252 |
register RECT *r; |
253 |
int pd; |
254 |
{ |
255 |
int mx, my; |
256 |
|
257 |
while (p->kid != NULL && pd--) { |
258 |
|
259 |
mx = (r->l + r->r) >> 1; |
260 |
my = (r->d + r->u) >> 1; |
261 |
|
262 |
if (x < mx) { |
263 |
r->r = mx; |
264 |
if (y < my) { |
265 |
r->u = my; |
266 |
p = p->kid+DL; |
267 |
} else { |
268 |
r->d = my; |
269 |
p = p->kid+UL; |
270 |
} |
271 |
} else { |
272 |
r->l = mx; |
273 |
if (y < my) { |
274 |
r->u = my; |
275 |
p = p->kid+DR; |
276 |
} else { |
277 |
r->d = my; |
278 |
p = p->kid+UR; |
279 |
} |
280 |
} |
281 |
} |
282 |
return(p); |
283 |
} |
284 |
|
285 |
|
286 |
scalepict(p, sf) /* scale picture values */ |
287 |
register PNODE *p; |
288 |
double sf; |
289 |
{ |
290 |
scalecolor(p->v, sf); /* do this node */ |
291 |
|
292 |
if (p->kid == NULL) |
293 |
return; |
294 |
/* do children */ |
295 |
scalepict(p->kid+DL, sf); |
296 |
scalepict(p->kid+DR, sf); |
297 |
scalepict(p->kid+UL, sf); |
298 |
scalepict(p->kid+UR, sf); |
299 |
} |
300 |
|
301 |
|
302 |
getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */ |
303 |
int yoff; |
304 |
register COLR *scan; |
305 |
register PNODE *p; |
306 |
int xsiz, ysiz; |
307 |
{ |
308 |
register int mx; |
309 |
int my; |
310 |
|
311 |
if (p->kid == NULL) { /* do this node */ |
312 |
setcolr(scan[0], colval(p->v,RED), |
313 |
colval(p->v,GRN), |
314 |
colval(p->v,BLU)); |
315 |
for (mx = 1; mx < xsiz; mx++) |
316 |
copycolr(scan[mx], scan[0]); |
317 |
return; |
318 |
} |
319 |
/* do kids */ |
320 |
mx = xsiz >> 1; |
321 |
my = ysiz >> 1; |
322 |
if (yoff < my) { |
323 |
getpictcolrs(yoff, scan, p->kid+DL, mx, my); |
324 |
getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my); |
325 |
} else { |
326 |
getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my); |
327 |
getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my); |
328 |
} |
329 |
} |
330 |
|
331 |
|
332 |
pcopy(p1, p2) /* copy paint node p1 into p2 */ |
333 |
register PNODE *p1, *p2; |
334 |
{ |
335 |
copycolor(p2->v, p1->v); |
336 |
p2->x = p1->x; |
337 |
p2->y = p1->y; |
338 |
} |
339 |
|
340 |
|
341 |
freepkids(p) /* free pnode's children */ |
342 |
register PNODE *p; |
343 |
{ |
344 |
if (p->kid == NULL) |
345 |
return; |
346 |
freepkids(p->kid+DL); |
347 |
freepkids(p->kid+DR); |
348 |
freepkids(p->kid+UL); |
349 |
freepkids(p->kid+UR); |
350 |
free((char *)p->kid); |
351 |
p->kid = NULL; |
352 |
} |
353 |
|
354 |
|
355 |
newview(vp) /* change viewing parameters */ |
356 |
register VIEW *vp; |
357 |
{ |
358 |
char *err; |
359 |
|
360 |
if ((err = setview(vp)) != NULL) { |
361 |
sprintf(errmsg, "view not set - %s", err); |
362 |
error(COMMAND, errmsg); |
363 |
} else if (bcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) { |
364 |
copystruct(&oldview, &ourview); |
365 |
copystruct(&ourview, vp); |
366 |
newimage(); |
367 |
} |
368 |
} |
369 |
|
370 |
|
371 |
moveview(angle, elev, mag, vc) /* move viewpoint */ |
372 |
double angle, elev, mag; |
373 |
FVECT vc; |
374 |
{ |
375 |
extern double sqrt(), dist2(); |
376 |
double d; |
377 |
FVECT v1; |
378 |
VIEW nv; |
379 |
register int i; |
380 |
|
381 |
VCOPY(nv.vup, ourview.vup); |
382 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
383 |
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
384 |
if (elev != 0.0) { |
385 |
fcross(v1, ourview.vup, nv.vdir); |
386 |
normalize(v1); |
387 |
spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); |
388 |
} |
389 |
if ((nv.type = ourview.type) == VT_PAR) { |
390 |
nv.horiz = ourview.horiz / mag; |
391 |
nv.vert = ourview.vert / mag; |
392 |
d = 0.0; /* don't move closer */ |
393 |
for (i = 0; i < 3; i++) |
394 |
d += (vc[i] - ourview.vp[i])*ourview.vdir[i]; |
395 |
} else { |
396 |
nv.horiz = ourview.horiz; |
397 |
nv.vert = ourview.vert; |
398 |
d = sqrt(dist2(ourview.vp, vc)) / mag; |
399 |
} |
400 |
for (i = 0; i < 3; i++) |
401 |
nv.vp[i] = vc[i] - d*nv.vdir[i]; |
402 |
newview(&nv); |
403 |
} |
404 |
|
405 |
|
406 |
zoomview(vp, zf) /* zoom in our out */ |
407 |
register VIEW *vp; |
408 |
double zf; |
409 |
{ |
410 |
switch (vp->type) { |
411 |
case VT_PAR: /* parallel view */ |
412 |
case VT_ANG: /* angular fisheye */ |
413 |
vp->horiz /= zf; |
414 |
vp->vert /= zf; |
415 |
return; |
416 |
case VT_PER: /* perspective view */ |
417 |
vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) / |
418 |
(PI/180./2.); |
419 |
vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / |
420 |
(PI/180./2.); |
421 |
return; |
422 |
case VT_HEM: /* hemispherical fisheye */ |
423 |
vp->horiz = sin(vp->horiz*(PI/180./2.))/zf; |
424 |
if (vp->horiz >= 1.0-FTINY) |
425 |
vp->horiz = 180.; |
426 |
else |
427 |
vp->horiz = asin(vp->horiz) / (PI/180./2.); |
428 |
vp->vert = sin(vp->vert*(PI/180./2.))/zf; |
429 |
if (vp->vert >= 1.0-FTINY) |
430 |
vp->vert = 180.; |
431 |
else |
432 |
vp->vert = asin(vp->vert) / (PI/180./2.); |
433 |
return; |
434 |
} |
435 |
} |
436 |
|
437 |
|
438 |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
439 |
FVECT vres, vorig, vnorm; |
440 |
double theta; |
441 |
{ |
442 |
extern double sin(), cos(); |
443 |
double sint, cost, dotp; |
444 |
FVECT vperp; |
445 |
register int i; |
446 |
|
447 |
if (theta == 0.0) { |
448 |
VCOPY(vres, vorig); |
449 |
return; |
450 |
} |
451 |
sint = sin(theta); |
452 |
cost = cos(theta); |
453 |
dotp = DOT(vorig, vnorm); |
454 |
fcross(vperp, vnorm, vorig); |
455 |
for (i = 0; i < 3; i++) |
456 |
vres[i] = vnorm[i]*dotp*(1.-cost) + |
457 |
vorig[i]*cost + vperp[i]*sint; |
458 |
} |