ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 2.24
Committed: Fri Aug 22 17:39:26 2008 UTC (15 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.23: +12 -5 lines
Log Message:
Further improvements to interactivity

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rv3.c,v 2.23 2008/08/21 16:13:00 greg Exp $";
3 #endif
4 /*
5 * rv3.c - miscellaneous routines for rview.
6 *
7 * External symbols declared in rpaint.h
8 */
9
10 #include "copyright.h"
11
12 #include <string.h>
13
14 #include "ray.h"
15 #include "rpaint.h"
16 #include "random.h"
17
18 #ifndef WFLUSH
19 #define WFLUSH 64 /* flush after this many rays */
20 #endif
21
22 #ifdef SMLFLT
23 #define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3)
24 #else
25 #define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3)
26 #endif
27
28 static unsigned long niflush; /* flushes since newimage() */
29
30 int
31 getrect( /* get a box */
32 char *s,
33 RECT *r
34 )
35 {
36 int x0, y0, x1, y1;
37
38 if (*s && !strncmp(s, "all", strlen(s))) {
39 r->l = r->d = 0;
40 r->r = hresolu;
41 r->u = vresolu;
42 return(0);
43 }
44 if (sscanf(s, "%d %d %d %d", &x0, &y0, &x1, &y1) != 4) {
45 if (dev->getcur == NULL)
46 return(-1);
47 (*dev->comout)("Pick first corner\n");
48 if ((*dev->getcur)(&x0, &y0) == ABORT)
49 return(-1);
50 (*dev->comout)("Pick second corner\n");
51 if ((*dev->getcur)(&x1, &y1) == ABORT)
52 return(-1);
53 }
54 if (x0 < x1) {
55 r->l = x0;
56 r->r = x1;
57 } else {
58 r->l = x1;
59 r->r = x0;
60 }
61 if (y0 < y1) {
62 r->d = y0;
63 r->u = y1;
64 } else {
65 r->d = y1;
66 r->u = y0;
67 }
68 if (r->l < 0) r->l = 0;
69 if (r->d < 0) r->d = 0;
70 if (r->r > hresolu) r->r = hresolu;
71 if (r->u > vresolu) r->u = vresolu;
72 if (r->l > r->r) r->l = r->r;
73 if (r->d > r->u) r->d = r->u;
74 return(0);
75 }
76
77
78 int
79 getinterest( /* get area of interest */
80 char *s,
81 int direc,
82 FVECT vec,
83 double *mp
84 )
85 {
86 int x, y;
87 RAY thisray;
88 int i;
89
90 if (sscanf(s, "%lf", mp) != 1)
91 *mp = 1.0;
92 else if (*mp < -FTINY) /* negative zoom is reduction */
93 *mp = -1.0 / *mp;
94 else if (*mp <= FTINY) { /* too small */
95 error(COMMAND, "illegal magnification");
96 return(-1);
97 }
98 if (!sscanvec(sskip(s), vec)) {
99 if (dev->getcur == NULL)
100 return(-1);
101 (*dev->comout)("Pick view center\n");
102 if ((*dev->getcur)(&x, &y) == ABORT)
103 return(-1);
104 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
105 &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
106 error(COMMAND, "not on image");
107 return(-1);
108 }
109 if (!direc || ourview.type == VT_PAR) {
110 rayorigin(&thisray, PRIMARY, NULL, NULL);
111 if (!localhit(&thisray, &thescene)) {
112 error(COMMAND, "not a local object");
113 return(-1);
114 }
115 }
116 if (direc)
117 if (ourview.type == VT_PAR)
118 for (i = 0; i < 3; i++)
119 vec[i] = thisray.rop[i] - ourview.vp[i];
120 else
121 VCOPY(vec, thisray.rdir);
122 else
123 VCOPY(vec, thisray.rop);
124 } else if (direc) {
125 for (i = 0; i < 3; i++)
126 vec[i] -= ourview.vp[i];
127 if (normalize(vec) == 0.0) {
128 error(COMMAND, "point at view origin");
129 return(-1);
130 }
131 }
132 return(0);
133 }
134
135
136 float * /* keep consistent with COLOR typedef */
137 greyof( /* convert color to greyscale */
138 COLOR col
139 )
140 {
141 static COLOR gcol;
142 double b;
143
144 b = bright(col);
145 setcolor(gcol, b, b, b);
146 return(gcol);
147 }
148
149
150 int
151 paint( /* compute and paint a rectangle */
152 PNODE *p
153 )
154 {
155 extern int ray_pnprocs;
156 static unsigned long lastflush = 0;
157 static RAY thisray;
158 int flushintvl;
159 double h, v;
160
161 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0) { /* empty */
162 p->x = p->xmin;
163 p->y = p->ymin;
164 setcolor(p->v, 0.0, 0.0, 0.0);
165 return(0);
166 }
167 /* jitter ray direction */
168 p->x = h = p->xmin + (p->xmax-p->xmin)*frandom();
169 p->y = v = p->ymin + (p->ymax-p->ymin)*frandom();
170
171 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
172 h/hresolu, v/vresolu)) < -FTINY) {
173 setcolor(thisray.rcol, 0.0, 0.0, 0.0);
174 } else {
175 int rval;
176 rayorigin(&thisray, PRIMARY, NULL, NULL);
177 thisray.rno = (unsigned long)p;
178 rval = ray_pqueue(&thisray);
179 if (!rval)
180 return(0);
181 if (rval < 0)
182 return(-1);
183 p = (PNODE *)thisray.rno;
184 }
185
186 copycolor(p->v, thisray.rcol);
187 scalecolor(p->v, exposure);
188
189 (*dev->paintr)(greyscale?greyof(p->v):p->v,
190 p->xmin, p->ymin, p->xmax, p->ymax);
191
192 if (ambounce <= 0) /* shall we check for input? */
193 flushintvl = ray_pnprocs*WFLUSH;
194 else if (niflush < WFLUSH)
195 flushintvl = ray_pnprocs*niflush/(ambounce+1);
196 else
197 flushintvl = ray_pnprocs*WFLUSH/(ambounce+1);
198
199 if (dev->flush != NULL && raynum - lastflush >= flushintvl) {
200 lastflush = raynum;
201 (*dev->flush)();
202 niflush++;
203 }
204 return(1);
205 }
206
207
208 int
209 waitrays(void) /* finish up pending rays */
210 {
211 int nwaited = 0;
212 int rval;
213 RAY raydone;
214
215 while ((rval = ray_presult(&raydone, 0)) > 0) {
216 PNODE *p = (PNODE *)raydone.rno;
217 copycolor(p->v, raydone.rcol);
218 scalecolor(p->v, exposure);
219 (*dev->paintr)(greyscale?greyof(p->v):p->v,
220 p->xmin, p->ymin, p->xmax, p->ymax);
221 nwaited++;
222 }
223 if (rval < 0)
224 return(-1);
225 return(nwaited);
226 }
227
228
229 void
230 newimage( /* start a new image */
231 char *s
232 )
233 {
234 int newnp;
235 /* change in nproc? */
236 if (s != NULL && sscanf(s, "%d", &newnp) == 1 && newnp > 0) {
237 if (!newparam) {
238 if (newnp < nproc)
239 ray_pclose(nproc - newnp);
240 else
241 ray_popen(newnp - nproc);
242 }
243 nproc = newnp;
244 }
245 /* free old image */
246 freepkids(&ptrunk);
247 /* compute resolution */
248 hresolu = dev->xsiz;
249 vresolu = dev->ysiz;
250 normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
251 ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
252 ptrunk.xmax = pframe.r = hresolu;
253 ptrunk.ymax = pframe.u = vresolu;
254 pdepth = 0;
255 /* clear device */
256 (*dev->clear)(hresolu, vresolu);
257
258 if (newparam) { /* (re)start rendering procs */
259 ray_pclose(0);
260 ray_popen(nproc);
261 newparam = 0;
262 }
263 niflush = 0; /* get first value */
264 paint(&ptrunk);
265 }
266
267
268 void
269 redraw(void) /* redraw the image */
270 {
271 (*dev->clear)(hresolu, vresolu);
272 (*dev->comout)("redrawing...\n");
273 repaint(0, 0, hresolu, vresolu);
274 (*dev->comout)("\n");
275 }
276
277
278 void
279 repaint( /* repaint a region */
280 int xmin,
281 int ymin,
282 int xmax,
283 int ymax
284 )
285 {
286 RECT reg;
287
288 reg.l = xmin; reg.r = xmax;
289 reg.d = ymin; reg.u = ymax;
290
291 paintrect(&ptrunk, &reg);
292 }
293
294
295 void
296 paintrect( /* paint picture rectangle */
297 PNODE *p,
298 RECT *r
299 )
300 {
301 int mx, my;
302
303 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
304 return;
305
306 if (p->kid == NULL) {
307 (*dev->paintr)(greyscale?greyof(p->v):p->v,
308 p->xmin, p->ymin, p->xmax, p->ymax); /* do this */
309 return;
310 }
311 mx = (p->xmin + p->xmax) >> 1; /* do kids */
312 my = (p->ymin + p->ymax) >> 1;
313 if (mx > r->l) {
314 if (my > r->d)
315 paintrect(p->kid+DL, r);
316 if (my < r->u)
317 paintrect(p->kid+UL, r);
318 }
319 if (mx < r->r) {
320 if (my > r->d)
321 paintrect(p->kid+DR, r);
322 if (my < r->u)
323 paintrect(p->kid+UR, r);
324 }
325 }
326
327
328 PNODE *
329 findrect( /* find a rectangle */
330 int x,
331 int y,
332 PNODE *p,
333 int pd
334 )
335 {
336 int mx, my;
337
338 while (p->kid != NULL && pd--) {
339
340 mx = (p->xmin + p->xmax) >> 1;
341 my = (p->ymin + p->ymax) >> 1;
342
343 if (x < mx) {
344 if (y < my) {
345 p = p->kid+DL;
346 } else {
347 p = p->kid+UL;
348 }
349 } else {
350 if (y < my) {
351 p = p->kid+DR;
352 } else {
353 p = p->kid+UR;
354 }
355 }
356 }
357 return(p);
358 }
359
360
361 void
362 scalepict( /* scale picture values */
363 PNODE *p,
364 double sf
365 )
366 {
367 scalecolor(p->v, sf); /* do this node */
368
369 if (p->kid == NULL)
370 return;
371 /* do children */
372 scalepict(p->kid+DL, sf);
373 scalepict(p->kid+DR, sf);
374 scalepict(p->kid+UL, sf);
375 scalepict(p->kid+UR, sf);
376 }
377
378
379 void
380 getpictcolrs( /* get scanline from picture */
381 int yoff,
382 COLR *scan,
383 PNODE *p,
384 int xsiz,
385 int ysiz
386 )
387 {
388 int mx;
389 int my;
390
391 if (p->kid == NULL) { /* do this node */
392 setcolr(scan[0], colval(p->v,RED),
393 colval(p->v,GRN),
394 colval(p->v,BLU));
395 for (mx = 1; mx < xsiz; mx++)
396 copycolr(scan[mx], scan[0]);
397 return;
398 }
399 /* do kids */
400 mx = xsiz >> 1;
401 my = ysiz >> 1;
402 if (yoff < my) {
403 getpictcolrs(yoff, scan, p->kid+DL, mx, my);
404 getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my);
405 } else {
406 getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my);
407 getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my);
408 }
409 }
410
411
412 void
413 freepkids( /* free pnode's children */
414 PNODE *p
415 )
416 {
417 if (p->kid == NULL)
418 return;
419 freepkids(p->kid+DL);
420 freepkids(p->kid+DR);
421 freepkids(p->kid+UL);
422 freepkids(p->kid+UR);
423 free((void *)p->kid);
424 p->kid = NULL;
425 }
426
427
428 void
429 newview( /* change viewing parameters */
430 VIEW *vp
431 )
432 {
433 char *err;
434
435 if ((err = setview(vp)) != NULL) {
436 sprintf(errmsg, "view not set - %s", err);
437 error(COMMAND, errmsg);
438 } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
439 oldview = ourview;
440 ourview = *vp;
441 newimage(NULL);
442 }
443 }
444
445
446 void
447 moveview( /* move viewpoint */
448 double angle,
449 double elev,
450 double mag,
451 FVECT vc
452 )
453 {
454 double d;
455 FVECT v1;
456 VIEW nv = ourview;
457 int i;
458
459 spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
460 if (elev != 0.0) {
461 fcross(v1, ourview.vup, nv.vdir);
462 normalize(v1);
463 spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
464 }
465 if (nv.type == VT_PAR) {
466 nv.horiz /= mag;
467 nv.vert /= mag;
468 d = 0.0; /* don't move closer */
469 for (i = 0; i < 3; i++)
470 d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
471 } else {
472 d = sqrt(dist2(ourview.vp, vc)) / mag;
473 if (nv.vfore > FTINY) {
474 nv.vfore += d - d*mag;
475 if (nv.vfore < 0.0) nv.vfore = 0.0;
476 }
477 if (nv.vaft > FTINY) {
478 nv.vaft += d - d*mag;
479 if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
480 }
481 nv.vdist /= mag;
482 }
483 for (i = 0; i < 3; i++)
484 nv.vp[i] = vc[i] - d*nv.vdir[i];
485 newview(&nv);
486 }
487
488
489 void
490 pcopy( /* copy paint node p1 into p2 */
491 PNODE *p1,
492 PNODE *p2
493 )
494 {
495 copycolor(p2->v, p1->v);
496 p2->x = p1->x;
497 p2->y = p1->y;
498 }
499
500
501 void
502 zoomview( /* zoom in or out */
503 VIEW *vp,
504 double zf
505 )
506 {
507 switch (vp->type) {
508 case VT_PAR: /* parallel view */
509 vp->horiz /= zf;
510 vp->vert /= zf;
511 return;
512 case VT_ANG: /* angular fisheye */
513 vp->horiz /= zf;
514 if (vp->horiz > 360.)
515 vp->horiz = 360.;
516 vp->vert /= zf;
517 if (vp->vert > 360.)
518 vp->vert = 360.;
519 return;
520 case VT_PLS: /* planisphere fisheye */
521 vp->horiz = sin((PI/180./2.)*vp->horiz) /
522 (1.0 + cos((PI/180./2.)*vp->horiz)) / zf;
523 vp->horiz *= vp->horiz;
524 vp->horiz = (2.*180./PI)*acos((1. - vp->horiz) /
525 (1. + vp->horiz));
526 vp->vert = sin((PI/180./2.)*vp->vert) /
527 (1.0 + cos((PI/180./2.)*vp->vert)) / zf;
528 vp->vert *= vp->vert;
529 vp->vert = (2.*180./PI)*acos((1. - vp->vert) /
530 (1. + vp->vert));
531 return;
532 case VT_CYL: /* cylindrical panorama */
533 vp->horiz /= zf;
534 if (vp->horiz > 360.)
535 vp->horiz = 360.;
536 vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / (PI/180./2.);
537 return;
538 case VT_PER: /* perspective view */
539 vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) /
540 (PI/180./2.);
541 vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) /
542 (PI/180./2.);
543 return;
544 case VT_HEM: /* hemispherical fisheye */
545 vp->horiz = sin(vp->horiz*(PI/180./2.))/zf;
546 if (vp->horiz >= 1.0-FTINY)
547 vp->horiz = 180.;
548 else
549 vp->horiz = asin(vp->horiz) / (PI/180./2.);
550 vp->vert = sin(vp->vert*(PI/180./2.))/zf;
551 if (vp->vert >= 1.0-FTINY)
552 vp->vert = 180.;
553 else
554 vp->vert = asin(vp->vert) / (PI/180./2.);
555 return;
556 }
557 }