1 |
– |
/* Copyright (c) 1987 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* rv3.c - miscellaneous routines for rview. |
6 |
|
* |
7 |
< |
* 5/11/87 |
7 |
> |
* External symbols declared in rpaint.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include "ray.h" |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "octree.h" |
12 |
> |
#include <string.h> |
13 |
|
|
14 |
+ |
#include "ray.h" |
15 |
|
#include "rpaint.h" |
18 |
– |
|
16 |
|
#include "random.h" |
17 |
|
|
18 |
|
#ifndef WFLUSH |
19 |
< |
#define WFLUSH 30 /* flush after this many rays */ |
19 |
> |
#ifdef SPEED |
20 |
> |
#define WFLUSH (5*SPEED) |
21 |
> |
#else |
22 |
> |
#define WFLUSH 100 /* flush after this many rays */ |
23 |
|
#endif |
24 |
+ |
#endif |
25 |
|
|
26 |
+ |
#ifdef SMLFLT |
27 |
+ |
#define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3) |
28 |
+ |
#else |
29 |
+ |
#define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3) |
30 |
+ |
#endif |
31 |
|
|
32 |
+ |
|
33 |
+ |
int |
34 |
|
getrect(s, r) /* get a box */ |
35 |
|
char *s; |
36 |
|
register RECT *r; |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
+ |
int |
81 |
|
getinterest(s, direc, vec, mp) /* get area of interest */ |
82 |
|
char *s; |
83 |
|
int direc; |
96 |
|
error(COMMAND, "illegal magnification"); |
97 |
|
return(-1); |
98 |
|
} |
99 |
< |
if (sscanf(s, "%*lf %lf %lf %lf", &vec[0], &vec[1], &vec[2]) != 3) { |
99 |
> |
if (!sscanvec(sskip(s), vec)) { |
100 |
|
if (dev->getcur == NULL) |
101 |
|
return(-1); |
102 |
|
(*dev->comout)("Pick view center\n"); |
103 |
|
if ((*dev->getcur)(&x, &y) == ABORT) |
104 |
|
return(-1); |
105 |
< |
viewray(thisray.rorg, thisray.rdir, &ourview, |
106 |
< |
(x+.5)/hresolu, (y+.5)/vresolu); |
105 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, |
106 |
> |
&ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) { |
107 |
> |
error(COMMAND, "not on image"); |
108 |
> |
return(-1); |
109 |
> |
} |
110 |
|
if (!direc || ourview.type == VT_PAR) { |
111 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
111 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
112 |
|
if (!localhit(&thisray, &thescene)) { |
113 |
|
error(COMMAND, "not a local object"); |
114 |
|
return(-1); |
122 |
|
VCOPY(vec, thisray.rdir); |
123 |
|
else |
124 |
|
VCOPY(vec, thisray.rop); |
125 |
< |
} else if (direc) |
126 |
< |
for (i = 0; i < 3; i++) |
127 |
< |
vec[i] -= ourview.vp[i]; |
125 |
> |
} else if (direc) { |
126 |
> |
for (i = 0; i < 3; i++) |
127 |
> |
vec[i] -= ourview.vp[i]; |
128 |
> |
if (normalize(vec) == 0.0) { |
129 |
> |
error(COMMAND, "point at view origin"); |
130 |
> |
return(-1); |
131 |
> |
} |
132 |
> |
} |
133 |
|
return(0); |
134 |
|
} |
135 |
|
|
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
+ |
void |
151 |
|
paint(p, xmin, ymin, xmax, ymax) /* compute and paint a rectangle */ |
152 |
|
register PNODE *p; |
153 |
|
int xmin, ymin, xmax, ymax; |
154 |
|
{ |
155 |
< |
extern long nrays; |
138 |
< |
static long lastflush = 0; |
155 |
> |
static unsigned long lastflush = 0; |
156 |
|
static RAY thisray; |
157 |
|
double h, v; |
141 |
– |
register int i; |
158 |
|
|
159 |
|
if (xmax - xmin <= 0 || ymax - ymin <= 0) { /* empty */ |
160 |
|
p->x = xmin; |
163 |
|
return; |
164 |
|
} |
165 |
|
/* jitter ray direction */ |
166 |
< |
p->x = h = xmin + (xmax-xmin)*frandom(); |
167 |
< |
h /= hresolu; |
152 |
< |
p->y = v = ymin + (ymax-ymin)*frandom(); |
153 |
< |
v /= vresolu; |
166 |
> |
h = xmin + (xmax-xmin)*frandom(); |
167 |
> |
v = ymin + (ymax-ymin)*frandom(); |
168 |
|
|
169 |
< |
viewray(thisray.rorg, thisray.rdir, &ourview, h, v); |
169 |
> |
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview, |
170 |
> |
h/hresolu, v/vresolu)) < -FTINY) { |
171 |
> |
setcolor(thisray.rcol, 0.0, 0.0, 0.0); |
172 |
> |
} else { |
173 |
> |
rayorigin(&thisray, PRIMARY, NULL, NULL); |
174 |
> |
samplendx = rand_samp ? random() : samplendx+1; |
175 |
> |
rayvalue(&thisray); |
176 |
> |
} |
177 |
|
|
178 |
< |
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
179 |
< |
|
159 |
< |
rayvalue(&thisray); |
160 |
< |
|
178 |
> |
p->x = h; |
179 |
> |
p->y = v; |
180 |
|
copycolor(p->v, thisray.rcol); |
162 |
– |
|
181 |
|
scalecolor(p->v, exposure); |
182 |
|
|
183 |
|
(*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax); |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
+ |
void |
193 |
|
newimage() /* start a new image */ |
194 |
|
{ |
195 |
|
/* free old image */ |
196 |
|
freepkids(&ptrunk); |
197 |
+ |
/* save reserve memory */ |
198 |
+ |
fillreserves(); |
199 |
|
/* compute resolution */ |
200 |
|
hresolu = dev->xsiz; |
201 |
|
vresolu = dev->ysiz; |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
+ |
void |
214 |
|
redraw() /* redraw the image */ |
215 |
|
{ |
216 |
|
(*dev->clear)(hresolu, vresolu); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
+ |
void |
224 |
|
repaint(xmin, ymin, xmax, ymax) /* repaint a region */ |
225 |
|
int xmin, ymin, xmax, ymax; |
226 |
|
{ |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
+ |
void |
237 |
|
paintrect(p, xmin, ymin, xmax, ymax, r) /* paint picture rectangle */ |
238 |
|
register PNODE *p; |
239 |
|
int xmin, ymin, xmax, ymax; |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
+ |
void |
308 |
|
scalepict(p, sf) /* scale picture values */ |
309 |
|
register PNODE *p; |
310 |
|
double sf; |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
+ |
void |
325 |
|
getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */ |
326 |
|
int yoff; |
327 |
|
register COLR *scan; |
352 |
|
} |
353 |
|
|
354 |
|
|
355 |
< |
pcopy(p1, p2) /* copy paint node p1 into p2 */ |
330 |
< |
register PNODE *p1, *p2; |
331 |
< |
{ |
332 |
< |
copycolor(p2->v, p1->v); |
333 |
< |
p2->x = p1->x; |
334 |
< |
p2->y = p1->y; |
335 |
< |
} |
336 |
< |
|
337 |
< |
|
355 |
> |
void |
356 |
|
freepkids(p) /* free pnode's children */ |
357 |
|
register PNODE *p; |
358 |
|
{ |
362 |
|
freepkids(p->kid+DR); |
363 |
|
freepkids(p->kid+UL); |
364 |
|
freepkids(p->kid+UR); |
365 |
< |
free((char *)p->kid); |
365 |
> |
free((void *)p->kid); |
366 |
|
p->kid = NULL; |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
+ |
void |
371 |
|
newview(vp) /* change viewing parameters */ |
372 |
|
register VIEW *vp; |
373 |
|
{ |
376 |
|
if ((err = setview(vp)) != NULL) { |
377 |
|
sprintf(errmsg, "view not set - %s", err); |
378 |
|
error(COMMAND, errmsg); |
379 |
< |
} else if (bcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) { |
380 |
< |
copystruct(&oldview, &ourview); |
381 |
< |
copystruct(&ourview, vp); |
379 |
> |
} else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) { |
380 |
> |
oldview = ourview; |
381 |
> |
ourview = *vp; |
382 |
|
newimage(); |
383 |
|
} |
384 |
|
} |
385 |
|
|
386 |
|
|
387 |
+ |
void |
388 |
|
moveview(angle, elev, mag, vc) /* move viewpoint */ |
389 |
|
double angle, elev, mag; |
390 |
|
FVECT vc; |
391 |
|
{ |
372 |
– |
extern double sqrt(), dist2(); |
392 |
|
double d; |
393 |
|
FVECT v1; |
394 |
< |
VIEW nv; |
394 |
> |
VIEW nv = ourview; |
395 |
|
register int i; |
396 |
|
|
378 |
– |
VCOPY(nv.vup, ourview.vup); |
379 |
– |
nv.hoff = ourview.hoff; nv.voff = ourview.voff; |
397 |
|
spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); |
398 |
|
if (elev != 0.0) { |
399 |
|
fcross(v1, ourview.vup, nv.vdir); |
400 |
|
normalize(v1); |
401 |
|
spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); |
402 |
|
} |
403 |
< |
if ((nv.type = ourview.type) == VT_PAR) { |
404 |
< |
nv.horiz = ourview.horiz / mag; |
405 |
< |
nv.vert = ourview.vert / mag; |
403 |
> |
if (nv.type == VT_PAR) { |
404 |
> |
nv.horiz /= mag; |
405 |
> |
nv.vert /= mag; |
406 |
|
d = 0.0; /* don't move closer */ |
407 |
|
for (i = 0; i < 3; i++) |
408 |
|
d += (vc[i] - ourview.vp[i])*ourview.vdir[i]; |
409 |
|
} else { |
393 |
– |
nv.horiz = ourview.horiz; |
394 |
– |
nv.vert = ourview.vert; |
410 |
|
d = sqrt(dist2(ourview.vp, vc)) / mag; |
411 |
+ |
if (nv.vfore > FTINY) { |
412 |
+ |
nv.vfore += d - d*mag; |
413 |
+ |
if (nv.vfore < 0.0) nv.vfore = 0.0; |
414 |
+ |
} |
415 |
+ |
if (nv.vaft > FTINY) { |
416 |
+ |
nv.vaft += d - d*mag; |
417 |
+ |
if (nv.vaft <= nv.vfore) nv.vaft = 0.0; |
418 |
+ |
} |
419 |
+ |
nv.vdist /= mag; |
420 |
|
} |
421 |
|
for (i = 0; i < 3; i++) |
422 |
|
nv.vp[i] = vc[i] - d*nv.vdir[i]; |
424 |
|
} |
425 |
|
|
426 |
|
|
427 |
< |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
428 |
< |
FVECT vres, vorig, vnorm; |
429 |
< |
double theta; |
427 |
> |
void |
428 |
> |
pcopy(p1, p2) /* copy paint node p1 into p2 */ |
429 |
> |
register PNODE *p1, *p2; |
430 |
|
{ |
431 |
< |
extern double sin(), cos(); |
432 |
< |
double sint, cost, dotp; |
433 |
< |
FVECT vperp; |
434 |
< |
register int i; |
435 |
< |
|
436 |
< |
if (theta == 0.0) { |
437 |
< |
VCOPY(vres, vorig); |
431 |
> |
copycolor(p2->v, p1->v); |
432 |
> |
p2->x = p1->x; |
433 |
> |
p2->y = p1->y; |
434 |
> |
} |
435 |
> |
|
436 |
> |
|
437 |
> |
void |
438 |
> |
zoomview(vp, zf) /* zoom in or out */ |
439 |
> |
register VIEW *vp; |
440 |
> |
double zf; |
441 |
> |
{ |
442 |
> |
switch (vp->type) { |
443 |
> |
case VT_PAR: /* parallel view */ |
444 |
> |
vp->horiz /= zf; |
445 |
> |
vp->vert /= zf; |
446 |
|
return; |
447 |
+ |
case VT_ANG: /* angular fisheye */ |
448 |
+ |
vp->horiz /= zf; |
449 |
+ |
if (vp->horiz > 360.) |
450 |
+ |
vp->horiz = 360.; |
451 |
+ |
vp->vert /= zf; |
452 |
+ |
if (vp->vert > 360.) |
453 |
+ |
vp->vert = 360.; |
454 |
+ |
return; |
455 |
+ |
case VT_CYL: /* cylindrical panorama */ |
456 |
+ |
vp->horiz /= zf; |
457 |
+ |
if (vp->horiz > 360.) |
458 |
+ |
vp->horiz = 360.; |
459 |
+ |
vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / (PI/180./2.); |
460 |
+ |
return; |
461 |
+ |
case VT_PER: /* perspective view */ |
462 |
+ |
vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) / |
463 |
+ |
(PI/180./2.); |
464 |
+ |
vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / |
465 |
+ |
(PI/180./2.); |
466 |
+ |
return; |
467 |
+ |
case VT_HEM: /* hemispherical fisheye */ |
468 |
+ |
vp->horiz = sin(vp->horiz*(PI/180./2.))/zf; |
469 |
+ |
if (vp->horiz >= 1.0-FTINY) |
470 |
+ |
vp->horiz = 180.; |
471 |
+ |
else |
472 |
+ |
vp->horiz = asin(vp->horiz) / (PI/180./2.); |
473 |
+ |
vp->vert = sin(vp->vert*(PI/180./2.))/zf; |
474 |
+ |
if (vp->vert >= 1.0-FTINY) |
475 |
+ |
vp->vert = 180.; |
476 |
+ |
else |
477 |
+ |
vp->vert = asin(vp->vert) / (PI/180./2.); |
478 |
+ |
return; |
479 |
|
} |
416 |
– |
sint = sin(theta); |
417 |
– |
cost = cos(theta); |
418 |
– |
dotp = DOT(vorig, vnorm); |
419 |
– |
fcross(vperp, vnorm, vorig); |
420 |
– |
for (i = 0; i < 3; i++) |
421 |
– |
vres[i] = vnorm[i]*dotp*(1.-cost) + |
422 |
– |
vorig[i]*cost + vperp[i]*sint; |
480 |
|
} |