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 |
samplendx++; |
161 |
rayvalue(&thisray); |
162 |
} |
163 |
|
164 |
p->x = h; |
165 |
p->y = v; |
166 |
copycolor(p->v, thisray.rcol); |
167 |
scalecolor(p->v, exposure); |
168 |
|
169 |
(*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax); |
170 |
|
171 |
if (dev->flush != NULL && nrays - lastflush >= WFLUSH) { |
172 |
lastflush = nrays; |
173 |
(*dev->flush)(); |
174 |
} |
175 |
} |
176 |
|
177 |
|
178 |
newimage() /* start a new image */ |
179 |
{ |
180 |
/* free old image */ |
181 |
freepkids(&ptrunk); |
182 |
/* save reserve memory */ |
183 |
fillreserves(); |
184 |
/* compute resolution */ |
185 |
hresolu = dev->xsiz; |
186 |
vresolu = dev->ysiz; |
187 |
normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu); |
188 |
pframe.l = pframe.d = 0; |
189 |
pframe.r = hresolu; pframe.u = vresolu; |
190 |
pdepth = 0; |
191 |
/* clear device */ |
192 |
(*dev->clear)(hresolu, vresolu); |
193 |
/* get first value */ |
194 |
paint(&ptrunk, 0, 0, hresolu, vresolu); |
195 |
} |
196 |
|
197 |
|
198 |
redraw() /* redraw the image */ |
199 |
{ |
200 |
(*dev->clear)(hresolu, vresolu); |
201 |
(*dev->comout)("redrawing...\n"); |
202 |
repaint(0, 0, hresolu, vresolu); |
203 |
(*dev->comout)("\n"); |
204 |
} |
205 |
|
206 |
|
207 |
repaint(xmin, ymin, xmax, ymax) /* repaint a region */ |
208 |
int xmin, ymin, xmax, ymax; |
209 |
{ |
210 |
RECT reg; |
211 |
|
212 |
reg.l = xmin; reg.r = xmax; |
213 |
reg.d = ymin; reg.u = ymax; |
214 |
|
215 |
paintrect(&ptrunk, 0, 0, hresolu, vresolu, ®); |
216 |
} |
217 |
|
218 |
|
219 |
paintrect(p, xmin, ymin, xmax, ymax, r) /* paint picture rectangle */ |
220 |
register PNODE *p; |
221 |
int xmin, ymin, xmax, ymax; |
222 |
register RECT *r; |
223 |
{ |
224 |
int mx, my; |
225 |
|
226 |
if (xmax - xmin <= 0 || ymax - ymin <= 0) |
227 |
return; |
228 |
|
229 |
if (p->kid == NULL) { |
230 |
(*dev->paintr)(greyscale?greyof(p->v):p->v, |
231 |
xmin, ymin, xmax, ymax); /* do this */ |
232 |
return; |
233 |
} |
234 |
mx = (xmin + xmax) >> 1; /* do kids */ |
235 |
my = (ymin + ymax) >> 1; |
236 |
if (mx > r->l) { |
237 |
if (my > r->d) |
238 |
paintrect(p->kid+DL, xmin, ymin, mx, my, r); |
239 |
if (my < r->u) |
240 |
paintrect(p->kid+UL, xmin, my, mx, ymax, r); |
241 |
} |
242 |
if (mx < r->r) { |
243 |
if (my > r->d) |
244 |
paintrect(p->kid+DR, mx, ymin, xmax, my, r); |
245 |
if (my < r->u) |
246 |
paintrect(p->kid+UR, mx, my, xmax, ymax, r); |
247 |
} |
248 |
} |
249 |
|
250 |
|
251 |
PNODE * |
252 |
findrect(x, y, p, r, pd) /* find a rectangle */ |
253 |
int x, y; |
254 |
register PNODE *p; |
255 |
register RECT *r; |
256 |
int pd; |
257 |
{ |
258 |
int mx, my; |
259 |
|
260 |
while (p->kid != NULL && pd--) { |
261 |
|
262 |
mx = (r->l + r->r) >> 1; |
263 |
my = (r->d + r->u) >> 1; |
264 |
|
265 |
if (x < mx) { |
266 |
r->r = mx; |
267 |
if (y < my) { |
268 |
r->u = my; |
269 |
p = p->kid+DL; |
270 |
} else { |
271 |
r->d = my; |
272 |
p = p->kid+UL; |
273 |
} |
274 |
} else { |
275 |
r->l = mx; |
276 |
if (y < my) { |
277 |
r->u = my; |
278 |
p = p->kid+DR; |
279 |
} else { |
280 |
r->d = my; |
281 |
p = p->kid+UR; |
282 |
} |
283 |
} |
284 |
} |
285 |
return(p); |
286 |
} |
287 |
|
288 |
|
289 |
scalepict(p, sf) /* scale picture values */ |
290 |
register PNODE *p; |
291 |
double sf; |
292 |
{ |
293 |
scalecolor(p->v, sf); /* do this node */ |
294 |
|
295 |
if (p->kid == NULL) |
296 |
return; |
297 |
/* do children */ |
298 |
scalepict(p->kid+DL, sf); |
299 |
scalepict(p->kid+DR, sf); |
300 |
scalepict(p->kid+UL, sf); |
301 |
scalepict(p->kid+UR, sf); |
302 |
} |
303 |
|
304 |
|
305 |
getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */ |
306 |
int yoff; |
307 |
register COLR *scan; |
308 |
register PNODE *p; |
309 |
int xsiz, ysiz; |
310 |
{ |
311 |
register int mx; |
312 |
int my; |
313 |
|
314 |
if (p->kid == NULL) { /* do this node */ |
315 |
setcolr(scan[0], colval(p->v,RED), |
316 |
colval(p->v,GRN), |
317 |
colval(p->v,BLU)); |
318 |
for (mx = 1; mx < xsiz; mx++) |
319 |
copycolr(scan[mx], scan[0]); |
320 |
return; |
321 |
} |
322 |
/* do kids */ |
323 |
mx = xsiz >> 1; |
324 |
my = ysiz >> 1; |
325 |
if (yoff < my) { |
326 |
getpictcolrs(yoff, scan, p->kid+DL, mx, my); |
327 |
getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my); |
328 |
} else { |
329 |
getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my); |
330 |
getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my); |
331 |
} |
332 |
} |
333 |
|
334 |
|
335 |
pcopy(p1, p2) /* copy paint node p1 into p2 */ |
336 |
register PNODE *p1, *p2; |
337 |
{ |
338 |
copycolor(p2->v, p1->v); |
339 |
p2->x = p1->x; |
340 |
p2->y = p1->y; |
341 |
} |
342 |
|
343 |
|
344 |
freepkids(p) /* free pnode's children */ |
345 |
register PNODE *p; |
346 |
{ |
347 |
if (p->kid == NULL) |
348 |
return; |
349 |
freepkids(p->kid+DL); |
350 |
freepkids(p->kid+DR); |
351 |
freepkids(p->kid+UL); |
352 |
freepkids(p->kid+UR); |
353 |
free((char *)p->kid); |
354 |
p->kid = NULL; |
355 |
} |
356 |
|
357 |
|
358 |
newview(vp) /* change viewing parameters */ |
359 |
register VIEW *vp; |
360 |
{ |
361 |
char *err; |
362 |
|
363 |
if ((err = setview(vp)) != NULL) { |
364 |
sprintf(errmsg, "view not set - %s", err); |
365 |
error(COMMAND, errmsg); |
366 |
} else if (bcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) { |
367 |
copystruct(&oldview, &ourview); |
368 |
copystruct(&ourview, vp); |
369 |
newimage(); |
370 |
} |
371 |
} |
372 |
|
373 |
|
374 |
moveview(angle, elev, mag, vc) /* move viewpoint */ |
375 |
double angle, elev, mag; |
376 |
FVECT vc; |
377 |
{ |
378 |
extern double sqrt(), dist2(); |
379 |
double d; |
380 |
FVECT v1; |
381 |
VIEW nv; |
382 |
register int i; |
383 |
|
384 |
VCOPY(nv.vup, ourview.vup); |
385 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
386 |
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
387 |
if (elev != 0.0) { |
388 |
fcross(v1, ourview.vup, nv.vdir); |
389 |
normalize(v1); |
390 |
spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); |
391 |
} |
392 |
if ((nv.type = ourview.type) == VT_PAR) { |
393 |
nv.horiz = ourview.horiz / mag; |
394 |
nv.vert = ourview.vert / mag; |
395 |
d = 0.0; /* don't move closer */ |
396 |
for (i = 0; i < 3; i++) |
397 |
d += (vc[i] - ourview.vp[i])*ourview.vdir[i]; |
398 |
} else { |
399 |
nv.horiz = ourview.horiz; |
400 |
nv.vert = ourview.vert; |
401 |
d = sqrt(dist2(ourview.vp, vc)) / mag; |
402 |
} |
403 |
for (i = 0; i < 3; i++) |
404 |
nv.vp[i] = vc[i] - d*nv.vdir[i]; |
405 |
newview(&nv); |
406 |
} |
407 |
|
408 |
|
409 |
zoomview(vp, zf) /* zoom in our out */ |
410 |
register VIEW *vp; |
411 |
double zf; |
412 |
{ |
413 |
switch (vp->type) { |
414 |
case VT_PAR: /* parallel view */ |
415 |
case VT_ANG: /* angular fisheye */ |
416 |
vp->horiz /= zf; |
417 |
vp->vert /= zf; |
418 |
return; |
419 |
case VT_PER: /* perspective view */ |
420 |
vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) / |
421 |
(PI/180./2.); |
422 |
vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / |
423 |
(PI/180./2.); |
424 |
return; |
425 |
case VT_HEM: /* hemispherical fisheye */ |
426 |
vp->horiz = sin(vp->horiz*(PI/180./2.))/zf; |
427 |
if (vp->horiz >= 1.0-FTINY) |
428 |
vp->horiz = 180.; |
429 |
else |
430 |
vp->horiz = asin(vp->horiz) / (PI/180./2.); |
431 |
vp->vert = sin(vp->vert*(PI/180./2.))/zf; |
432 |
if (vp->vert >= 1.0-FTINY) |
433 |
vp->vert = 180.; |
434 |
else |
435 |
vp->vert = asin(vp->vert) / (PI/180./2.); |
436 |
return; |
437 |
} |
438 |
} |