ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 1.7
Committed: Fri Oct 27 11:48:23 1989 UTC (34 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +95 -0 lines
Log Message:
added repaint command

File Contents

# Content
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
22 getrect(s, r) /* get a box */
23 char *s;
24 register RECT *r;
25 {
26 int x0, y0, x1, y1;
27
28 if (*s && !strncmp(s, "all", strlen(s))) {
29 r->l = r->d = 0;
30 r->r = ourview.hresolu;
31 r->u = ourview.vresolu;
32 return(0);
33 }
34 if (sscanf(s, "%d %d %d %d", &x0, &y0, &x1, &y1) != 4) {
35 if (dev->getcur == NULL)
36 return(-1);
37 (*dev->comout)("Pick first corner\n");
38 if ((*dev->getcur)(&x0, &y0) == ABORT)
39 return(-1);
40 (*dev->comout)("Pick second corner\n");
41 if ((*dev->getcur)(&x1, &y1) == ABORT)
42 return(-1);
43 }
44 if (x0 < x1) {
45 r->l = x0;
46 r->r = x1;
47 } else {
48 r->l = x1;
49 r->r = x0;
50 }
51 if (y0 < y1) {
52 r->d = y0;
53 r->u = y1;
54 } else {
55 r->d = y1;
56 r->u = y0;
57 }
58 if (r->l < 0) r->l = 0;
59 if (r->d < 0) r->d = 0;
60 if (r->r > ourview.hresolu) r->r = ourview.hresolu;
61 if (r->u > ourview.vresolu) r->u = ourview.vresolu;
62 if (r->l > r->r) r->l = r->r;
63 if (r->d > r->u) r->d = r->u;
64 return(0);
65 }
66
67
68 getinterest(s, direc, vec, mp) /* get area of interest */
69 char *s;
70 int direc;
71 FVECT vec;
72 double *mp;
73 {
74 int x, y;
75 RAY thisray;
76 register int i;
77
78 if (sscanf(s, "%lf", mp) != 1)
79 *mp = 1.0;
80 else if (*mp < -FTINY) /* negative zoom is reduction */
81 *mp = -1.0 / *mp;
82 else if (*mp <= FTINY) { /* too small */
83 error(COMMAND, "illegal magnification");
84 return(-1);
85 }
86 if (sscanf(s, "%*lf %lf %lf %lf", &vec[0], &vec[1], &vec[2]) != 3) {
87 if (dev->getcur == NULL)
88 return(-1);
89 (*dev->comout)("Pick view center\n");
90 if ((*dev->getcur)(&x, &y) == ABORT)
91 return(-1);
92 rayview(thisray.rorg, thisray.rdir, &ourview, x+.5, y+.5);
93 if (!direc || ourview.type == VT_PAR) {
94 rayorigin(&thisray, NULL, PRIMARY, 1.0);
95 if (!localhit(&thisray, &thescene)) {
96 error(COMMAND, "not a local object");
97 return(-1);
98 }
99 }
100 if (direc)
101 if (ourview.type == VT_PAR)
102 for (i = 0; i < 3; i++)
103 vec[i] = thisray.rop[i] - ourview.vp[i];
104 else
105 VCOPY(vec, thisray.rdir);
106 else
107 VCOPY(vec, thisray.rop);
108 } else if (direc)
109 for (i = 0; i < 3; i++)
110 vec[i] -= ourview.vp[i];
111 return(0);
112 }
113
114
115 float * /* keep consistent with COLOR typedef */
116 greyof(col) /* convert color to greyscale */
117 register COLOR col;
118 {
119 static COLOR gcol;
120 double b;
121
122 b = bright(col);
123 setcolor(gcol, b, b, b);
124 return(gcol);
125 }
126
127
128 paint(p, xmin, ymin, xmax, ymax) /* compute and paint a rectangle */
129 register PNODE *p;
130 int xmin, ymin, xmax, ymax;
131 {
132 static RAY thisray;
133 double h, v;
134 register int i;
135
136 if (xmax - xmin <= 0 || ymax - ymin <= 0) { /* empty */
137 p->x = xmin;
138 p->y = ymin;
139 setcolor(p->v, 0.0, 0.0, 0.0);
140 return;
141 }
142 /* jitter ray direction */
143 p->x = h = xmin + (xmax-xmin)*frandom();
144 p->y = v = ymin + (ymax-ymin)*frandom();
145
146 rayview(thisray.rorg, thisray.rdir, &ourview, h, v);
147
148 rayorigin(&thisray, NULL, PRIMARY, 1.0);
149
150 rayvalue(&thisray);
151
152 copycolor(p->v, thisray.rcol);
153
154 scalecolor(p->v, exposure);
155
156 (*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax);
157 }
158
159
160 newimage() /* start a new image */
161 {
162 /* free old image */
163 freepkids(&ptrunk);
164 /* set up frame */
165 if (ourview.hresolu > dev->xsiz || ourview.vresolu > dev->ysiz)
166 newview(&ourview); /* beware recursive loop! */
167 pframe.l = pframe.d = 0;
168 pframe.r = ourview.hresolu; pframe.u = ourview.vresolu;
169 pdepth = 0;
170 /* clear device */
171 (*dev->clear)(ourview.hresolu, ourview.vresolu);
172 /* get first value */
173 paint(&ptrunk, 0, 0, ourview.hresolu, ourview.vresolu);
174 }
175
176
177 redraw() /* redraw the image */
178 {
179 (*dev->clear)(ourview.hresolu, ourview.vresolu);
180 (*dev->comout)("redrawing...\n");
181 repaint(0, 0, ourview.hresolu, ourview.vresolu);
182 (*dev->comout)("\n");
183 }
184
185
186 repaint(xmin, ymin, xmax, ymax) /* repaint a region */
187 int xmin, ymin, xmax, ymax;
188 {
189 RECT reg;
190
191 reg.l = xmin; reg.r = xmax;
192 reg.d = ymin; reg.u = ymax;
193
194 paintrect(&ptrunk, 0, 0, ourview.hresolu, ourview.vresolu, &reg);
195 }
196
197
198 paintrect(p, xmin, ymin, xmax, ymax, r) /* paint picture rectangle */
199 register PNODE *p;
200 int xmin, ymin, xmax, ymax;
201 register RECT *r;
202 {
203 int mx, my;
204
205 if (dev->inpready)
206 return; /* break for input */
207
208 if (xmax - xmin <= 0 || ymax - ymin <= 0)
209 return;
210
211 if (p->kid == NULL) {
212 (*dev->paintr)(greyscale?greyof(p->v):p->v,
213 xmin, ymin, xmax, ymax); /* do this */
214 return;
215 }
216 mx = (xmin + xmax) >> 1; /* do kids */
217 my = (ymin + ymax) >> 1;
218 if (mx > r->l) {
219 if (my > r->d)
220 paintrect(p->kid+DL, xmin, ymin, mx, my, r);
221 if (my < r->u)
222 paintrect(p->kid+UL, xmin, my, mx, ymax, r);
223 }
224 if (mx < r->r) {
225 if (my > r->d)
226 paintrect(p->kid+DR, mx, ymin, xmax, my, r);
227 if (my < r->u)
228 paintrect(p->kid+UR, mx, my, xmax, ymax, r);
229 }
230 }
231
232
233 PNODE *
234 findrect(x, y, p, r, pd) /* find a rectangle */
235 int x, y;
236 register PNODE *p;
237 register RECT *r;
238 int pd;
239 {
240 int mx, my;
241
242 while (p->kid != NULL && pd--) {
243
244 mx = (r->l + r->r) >> 1;
245 my = (r->d + r->u) >> 1;
246
247 if (x < mx) {
248 r->r = mx;
249 if (y < my) {
250 r->u = my;
251 p = p->kid+DL;
252 } else {
253 r->d = my;
254 p = p->kid+UL;
255 }
256 } else {
257 r->l = mx;
258 if (y < my) {
259 r->u = my;
260 p = p->kid+DR;
261 } else {
262 r->d = my;
263 p = p->kid+UR;
264 }
265 }
266 }
267 return(p);
268 }
269
270
271 scalepict(p, sf) /* scale picture values */
272 register PNODE *p;
273 double sf;
274 {
275 scalecolor(p->v, sf); /* do this node */
276
277 if (p->kid == NULL)
278 return;
279 /* do children */
280 scalepict(p->kid+DL, sf);
281 scalepict(p->kid+DR, sf);
282 scalepict(p->kid+UL, sf);
283 scalepict(p->kid+UR, sf);
284 }
285
286
287 getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */
288 int yoff;
289 register COLR *scan;
290 register PNODE *p;
291 int xsiz, ysiz;
292 {
293 register int mx;
294 int my;
295
296 if (p->kid == NULL) { /* do this node */
297 setcolr(scan[0], colval(p->v,RED),
298 colval(p->v,GRN),
299 colval(p->v,BLU));
300 for (mx = 1; mx < xsiz; mx++)
301 copycolr(scan[mx], scan[0]);
302 return;
303 }
304 /* do kids */
305 mx = xsiz >> 1;
306 my = ysiz >> 1;
307 if (yoff < my) {
308 getpictcolrs(yoff, scan, p->kid+DL, mx, my);
309 getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my);
310 } else {
311 getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my);
312 getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my);
313 }
314 }
315
316
317 pcopy(p1, p2) /* copy paint node p1 into p2 */
318 register PNODE *p1, *p2;
319 {
320 copycolor(p2->v, p1->v);
321 p2->x = p1->x;
322 p2->y = p1->y;
323 }
324
325
326 freepkids(p) /* free pnode's children */
327 register PNODE *p;
328 {
329 if (p->kid == NULL)
330 return;
331 freepkids(p->kid+DL);
332 freepkids(p->kid+DR);
333 freepkids(p->kid+UL);
334 freepkids(p->kid+UR);
335 free((char *)p->kid);
336 p->kid = NULL;
337 }
338
339
340 newview(vp) /* change viewing parameters */
341 register VIEW *vp;
342 {
343 char *err;
344
345 if (vp->hresolu > dev->xsiz || vp->vresolu > dev->ysiz) /* shrink */
346 if (vp->vresolu * dev->xsiz < vp->hresolu * dev->ysiz) {
347 vp->vresolu = dev->xsiz * vp->vresolu / vp->hresolu;
348 vp->hresolu = dev->xsiz;
349 } else {
350 vp->hresolu = dev->ysiz * vp->hresolu / vp->vresolu;
351 vp->vresolu = dev->ysiz;
352 }
353 if ((err = setview(vp)) != NULL) {
354 sprintf(errmsg, "view not set - %s", err);
355 error(COMMAND, errmsg);
356 } else if (bcmp(vp, &ourview, sizeof(VIEW))) {
357 bcopy(&ourview, &oldview, sizeof(VIEW));
358 bcopy(vp, &ourview, sizeof(VIEW));
359 newimage(); /* newimage() calls with vp=&ourview! */
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.hresolu = ourview.hresolu; nv.vresolu = ourview.vresolu;
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 }