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 |
viewray(thisray.rorg, thisray.rdir, &ourview, |
97 |
(x+.5)/hresolu, (y+.5)/vresolu); |
98 |
if (!direc || ourview.type == VT_PAR) { |
99 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
100 |
if (!localhit(&thisray, &thescene)) { |
101 |
error(COMMAND, "not a local object"); |
102 |
return(-1); |
103 |
} |
104 |
} |
105 |
if (direc) |
106 |
if (ourview.type == VT_PAR) |
107 |
for (i = 0; i < 3; i++) |
108 |
vec[i] = thisray.rop[i] - ourview.vp[i]; |
109 |
else |
110 |
VCOPY(vec, thisray.rdir); |
111 |
else |
112 |
VCOPY(vec, thisray.rop); |
113 |
} else if (direc) |
114 |
for (i = 0; i < 3; i++) |
115 |
vec[i] -= ourview.vp[i]; |
116 |
return(0); |
117 |
} |
118 |
|
119 |
|
120 |
float * /* keep consistent with COLOR typedef */ |
121 |
greyof(col) /* convert color to greyscale */ |
122 |
register COLOR col; |
123 |
{ |
124 |
static COLOR gcol; |
125 |
double b; |
126 |
|
127 |
b = bright(col); |
128 |
setcolor(gcol, b, b, b); |
129 |
return(gcol); |
130 |
} |
131 |
|
132 |
|
133 |
paint(p, xmin, ymin, xmax, ymax) /* compute and paint a rectangle */ |
134 |
register PNODE *p; |
135 |
int xmin, ymin, xmax, ymax; |
136 |
{ |
137 |
extern long nrays; |
138 |
static long lastflush = 0; |
139 |
static RAY thisray; |
140 |
double h, v; |
141 |
|
142 |
if (xmax - xmin <= 0 || ymax - ymin <= 0) { /* empty */ |
143 |
p->x = xmin; |
144 |
p->y = ymin; |
145 |
setcolor(p->v, 0.0, 0.0, 0.0); |
146 |
return; |
147 |
} |
148 |
/* jitter ray direction */ |
149 |
h = xmin + (xmax-xmin)*frandom(); |
150 |
v = ymin + (ymax-ymin)*frandom(); |
151 |
|
152 |
viewray(thisray.rorg, thisray.rdir, &ourview, h/hresolu, v/vresolu); |
153 |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
154 |
rayvalue(&thisray); |
155 |
|
156 |
p->x = h; |
157 |
p->y = v; |
158 |
copycolor(p->v, thisray.rcol); |
159 |
scalecolor(p->v, exposure); |
160 |
|
161 |
(*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax); |
162 |
|
163 |
if (dev->flush != NULL && nrays - lastflush >= WFLUSH) { |
164 |
lastflush = nrays; |
165 |
(*dev->flush)(); |
166 |
} |
167 |
} |
168 |
|
169 |
|
170 |
newimage() /* start a new image */ |
171 |
{ |
172 |
/* free old image */ |
173 |
freepkids(&ptrunk); |
174 |
/* compute resolution */ |
175 |
hresolu = dev->xsiz; |
176 |
vresolu = dev->ysiz; |
177 |
normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu); |
178 |
pframe.l = pframe.d = 0; |
179 |
pframe.r = hresolu; pframe.u = vresolu; |
180 |
pdepth = 0; |
181 |
/* clear device */ |
182 |
(*dev->clear)(hresolu, vresolu); |
183 |
/* get first value */ |
184 |
paint(&ptrunk, 0, 0, hresolu, vresolu); |
185 |
} |
186 |
|
187 |
|
188 |
redraw() /* redraw the image */ |
189 |
{ |
190 |
(*dev->clear)(hresolu, vresolu); |
191 |
(*dev->comout)("redrawing...\n"); |
192 |
repaint(0, 0, hresolu, vresolu); |
193 |
(*dev->comout)("\n"); |
194 |
} |
195 |
|
196 |
|
197 |
repaint(xmin, ymin, xmax, ymax) /* repaint a region */ |
198 |
int xmin, ymin, xmax, ymax; |
199 |
{ |
200 |
RECT reg; |
201 |
|
202 |
reg.l = xmin; reg.r = xmax; |
203 |
reg.d = ymin; reg.u = ymax; |
204 |
|
205 |
paintrect(&ptrunk, 0, 0, hresolu, vresolu, ®); |
206 |
} |
207 |
|
208 |
|
209 |
paintrect(p, xmin, ymin, xmax, ymax, r) /* paint picture rectangle */ |
210 |
register PNODE *p; |
211 |
int xmin, ymin, xmax, ymax; |
212 |
register RECT *r; |
213 |
{ |
214 |
int mx, my; |
215 |
|
216 |
if (xmax - xmin <= 0 || ymax - ymin <= 0) |
217 |
return; |
218 |
|
219 |
if (p->kid == NULL) { |
220 |
(*dev->paintr)(greyscale?greyof(p->v):p->v, |
221 |
xmin, ymin, xmax, ymax); /* do this */ |
222 |
return; |
223 |
} |
224 |
mx = (xmin + xmax) >> 1; /* do kids */ |
225 |
my = (ymin + ymax) >> 1; |
226 |
if (mx > r->l) { |
227 |
if (my > r->d) |
228 |
paintrect(p->kid+DL, xmin, ymin, mx, my, r); |
229 |
if (my < r->u) |
230 |
paintrect(p->kid+UL, xmin, my, mx, ymax, r); |
231 |
} |
232 |
if (mx < r->r) { |
233 |
if (my > r->d) |
234 |
paintrect(p->kid+DR, mx, ymin, xmax, my, r); |
235 |
if (my < r->u) |
236 |
paintrect(p->kid+UR, mx, my, xmax, ymax, r); |
237 |
} |
238 |
} |
239 |
|
240 |
|
241 |
PNODE * |
242 |
findrect(x, y, p, r, pd) /* find a rectangle */ |
243 |
int x, y; |
244 |
register PNODE *p; |
245 |
register RECT *r; |
246 |
int pd; |
247 |
{ |
248 |
int mx, my; |
249 |
|
250 |
while (p->kid != NULL && pd--) { |
251 |
|
252 |
mx = (r->l + r->r) >> 1; |
253 |
my = (r->d + r->u) >> 1; |
254 |
|
255 |
if (x < mx) { |
256 |
r->r = mx; |
257 |
if (y < my) { |
258 |
r->u = my; |
259 |
p = p->kid+DL; |
260 |
} else { |
261 |
r->d = my; |
262 |
p = p->kid+UL; |
263 |
} |
264 |
} else { |
265 |
r->l = mx; |
266 |
if (y < my) { |
267 |
r->u = my; |
268 |
p = p->kid+DR; |
269 |
} else { |
270 |
r->d = my; |
271 |
p = p->kid+UR; |
272 |
} |
273 |
} |
274 |
} |
275 |
return(p); |
276 |
} |
277 |
|
278 |
|
279 |
scalepict(p, sf) /* scale picture values */ |
280 |
register PNODE *p; |
281 |
double sf; |
282 |
{ |
283 |
scalecolor(p->v, sf); /* do this node */ |
284 |
|
285 |
if (p->kid == NULL) |
286 |
return; |
287 |
/* do children */ |
288 |
scalepict(p->kid+DL, sf); |
289 |
scalepict(p->kid+DR, sf); |
290 |
scalepict(p->kid+UL, sf); |
291 |
scalepict(p->kid+UR, sf); |
292 |
} |
293 |
|
294 |
|
295 |
getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */ |
296 |
int yoff; |
297 |
register COLR *scan; |
298 |
register PNODE *p; |
299 |
int xsiz, ysiz; |
300 |
{ |
301 |
register int mx; |
302 |
int my; |
303 |
|
304 |
if (p->kid == NULL) { /* do this node */ |
305 |
setcolr(scan[0], colval(p->v,RED), |
306 |
colval(p->v,GRN), |
307 |
colval(p->v,BLU)); |
308 |
for (mx = 1; mx < xsiz; mx++) |
309 |
copycolr(scan[mx], scan[0]); |
310 |
return; |
311 |
} |
312 |
/* do kids */ |
313 |
mx = xsiz >> 1; |
314 |
my = ysiz >> 1; |
315 |
if (yoff < my) { |
316 |
getpictcolrs(yoff, scan, p->kid+DL, mx, my); |
317 |
getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my); |
318 |
} else { |
319 |
getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my); |
320 |
getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my); |
321 |
} |
322 |
} |
323 |
|
324 |
|
325 |
pcopy(p1, p2) /* copy paint node p1 into p2 */ |
326 |
register PNODE *p1, *p2; |
327 |
{ |
328 |
copycolor(p2->v, p1->v); |
329 |
p2->x = p1->x; |
330 |
p2->y = p1->y; |
331 |
} |
332 |
|
333 |
|
334 |
freepkids(p) /* free pnode's children */ |
335 |
register PNODE *p; |
336 |
{ |
337 |
if (p->kid == NULL) |
338 |
return; |
339 |
freepkids(p->kid+DL); |
340 |
freepkids(p->kid+DR); |
341 |
freepkids(p->kid+UL); |
342 |
freepkids(p->kid+UR); |
343 |
free((char *)p->kid); |
344 |
p->kid = NULL; |
345 |
} |
346 |
|
347 |
|
348 |
newview(vp) /* change viewing parameters */ |
349 |
register VIEW *vp; |
350 |
{ |
351 |
char *err; |
352 |
|
353 |
if ((err = setview(vp)) != NULL) { |
354 |
sprintf(errmsg, "view not set - %s", err); |
355 |
error(COMMAND, errmsg); |
356 |
} else if (bcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) { |
357 |
copystruct(&oldview, &ourview); |
358 |
copystruct(&ourview, vp); |
359 |
newimage(); |
360 |
} |
361 |
} |
362 |
|
363 |
|
364 |
moveview(angle, elev, mag, vc) /* move viewpoint */ |
365 |
double angle, elev, mag; |
366 |
FVECT vc; |
367 |
{ |
368 |
extern double sqrt(), dist2(); |
369 |
double d; |
370 |
FVECT v1; |
371 |
VIEW nv; |
372 |
register int i; |
373 |
|
374 |
VCOPY(nv.vup, ourview.vup); |
375 |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
376 |
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
377 |
if (elev != 0.0) { |
378 |
fcross(v1, ourview.vup, nv.vdir); |
379 |
normalize(v1); |
380 |
spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); |
381 |
} |
382 |
if ((nv.type = ourview.type) == VT_PAR) { |
383 |
nv.horiz = ourview.horiz / mag; |
384 |
nv.vert = ourview.vert / mag; |
385 |
d = 0.0; /* don't move closer */ |
386 |
for (i = 0; i < 3; i++) |
387 |
d += (vc[i] - ourview.vp[i])*ourview.vdir[i]; |
388 |
} else { |
389 |
nv.horiz = ourview.horiz; |
390 |
nv.vert = ourview.vert; |
391 |
d = sqrt(dist2(ourview.vp, vc)) / mag; |
392 |
} |
393 |
for (i = 0; i < 3; i++) |
394 |
nv.vp[i] = vc[i] - d*nv.vdir[i]; |
395 |
newview(&nv); |
396 |
} |
397 |
|
398 |
|
399 |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
400 |
FVECT vres, vorig, vnorm; |
401 |
double theta; |
402 |
{ |
403 |
extern double sin(), cos(); |
404 |
double sint, cost, dotp; |
405 |
FVECT vperp; |
406 |
register int i; |
407 |
|
408 |
if (theta == 0.0) { |
409 |
VCOPY(vres, vorig); |
410 |
return; |
411 |
} |
412 |
sint = sin(theta); |
413 |
cost = cos(theta); |
414 |
dotp = DOT(vorig, vnorm); |
415 |
fcross(vperp, vnorm, vorig); |
416 |
for (i = 0; i < 3; i++) |
417 |
vres[i] = vnorm[i]*dotp*(1.-cost) + |
418 |
vorig[i]*cost + vperp[i]*sint; |
419 |
} |