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

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.24 static const char RCSid[] = "$Id: rv3.c,v 2.23 2008/08/21 16:13:00 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * rv3.c - miscellaneous routines for rview.
6     *
7 greg 2.10 * External symbols declared in rpaint.h
8     */
9    
10 greg 2.11 #include "copyright.h"
11 greg 1.1
12 schorsch 2.12 #include <string.h>
13    
14 greg 1.1 #include "ray.h"
15     #include "rpaint.h"
16     #include "random.h"
17 greg 1.7
18 greg 1.13 #ifndef WFLUSH
19 greg 2.22 #define WFLUSH 64 /* flush after this many rays */
20 greg 1.13 #endif
21 greg 1.7
22 greg 2.2 #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 greg 1.13
28 greg 2.24 static unsigned long niflush; /* flushes since newimage() */
29 greg 2.2
30 greg 2.10 int
31 greg 2.22 getrect( /* get a box */
32     char *s,
33     RECT *r
34     )
35 greg 1.7 {
36     int x0, y0, x1, y1;
37    
38     if (*s && !strncmp(s, "all", strlen(s))) {
39     r->l = r->d = 0;
40 greg 1.9 r->r = hresolu;
41     r->u = vresolu;
42 greg 1.7 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 greg 1.9 if (r->r > hresolu) r->r = hresolu;
71     if (r->u > vresolu) r->u = vresolu;
72 greg 1.7 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 greg 2.10 int
79 greg 2.22 getinterest( /* get area of interest */
80     char *s,
81     int direc,
82     FVECT vec,
83     double *mp
84     )
85 greg 1.7 {
86     int x, y;
87     RAY thisray;
88 greg 2.22 int i;
89 greg 1.7
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 greg 2.2 if (!sscanvec(sskip(s), vec)) {
99 greg 1.7 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 greg 2.6 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
105     &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
106 greg 1.17 error(COMMAND, "not on image");
107     return(-1);
108     }
109 greg 1.7 if (!direc || ourview.type == VT_PAR) {
110 greg 2.17 rayorigin(&thisray, PRIMARY, NULL, NULL);
111 greg 1.7 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 greg 2.15 } 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 greg 1.7 return(0);
133     }
134 greg 1.1
135    
136     float * /* keep consistent with COLOR typedef */
137 greg 2.22 greyof( /* convert color to greyscale */
138     COLOR col
139     )
140 greg 1.1 {
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 greg 2.22 int
151     paint( /* compute and paint a rectangle */
152     PNODE *p
153     )
154 greg 1.1 {
155 greg 2.22 extern int ray_pnprocs;
156 greg 2.5 static unsigned long lastflush = 0;
157 greg 1.1 static RAY thisray;
158 greg 2.24 int flushintvl;
159 greg 1.1 double h, v;
160    
161 greg 2.22 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0) { /* empty */
162     p->x = p->xmin;
163     p->y = p->ymin;
164 greg 1.1 setcolor(p->v, 0.0, 0.0, 0.0);
165 greg 2.22 return(0);
166 greg 1.1 }
167     /* jitter ray direction */
168 greg 2.22 p->x = h = p->xmin + (p->xmax-p->xmin)*frandom();
169     p->y = v = p->ymin + (p->ymax-p->ymin)*frandom();
170 greg 1.1
171 greg 2.6 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
172     h/hresolu, v/vresolu)) < -FTINY) {
173 greg 1.17 setcolor(thisray.rcol, 0.0, 0.0, 0.0);
174     } else {
175 greg 2.22 int rval;
176 greg 2.17 rayorigin(&thisray, PRIMARY, NULL, NULL);
177 greg 2.22 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 greg 1.17 }
185 greg 1.1
186     copycolor(p->v, thisray.rcol);
187     scalecolor(p->v, exposure);
188    
189 greg 2.23 (*dev->paintr)(greyscale?greyof(p->v):p->v,
190     p->xmin, p->ymin, p->xmax, p->ymax);
191 greg 1.13
192 greg 2.24 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 greg 2.22 lastflush = raynum;
201 greg 1.13 (*dev->flush)();
202 greg 2.23 niflush++;
203 greg 1.13 }
204 greg 2.22 return(1);
205     }
206    
207    
208     int
209 greg 2.24 waitrays(void) /* finish up pending rays */
210 greg 2.22 {
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 greg 1.1 }
227    
228    
229 greg 2.10 void
230 greg 2.22 newimage( /* start a new image */
231     char *s
232     )
233 greg 1.1 {
234 greg 2.22 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 greg 1.1 /* free old image */
246     freepkids(&ptrunk);
247 greg 1.9 /* compute resolution */
248 greg 1.10 hresolu = dev->xsiz;
249     vresolu = dev->ysiz;
250 greg 1.11 normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
251 greg 2.22 ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
252     ptrunk.xmax = pframe.r = hresolu;
253     ptrunk.ymax = pframe.u = vresolu;
254 greg 1.1 pdepth = 0;
255     /* clear device */
256 greg 1.9 (*dev->clear)(hresolu, vresolu);
257 greg 2.22
258     if (newparam) { /* (re)start rendering procs */
259     ray_pclose(0);
260     ray_popen(nproc);
261     newparam = 0;
262     }
263 greg 2.23 niflush = 0; /* get first value */
264 greg 2.22 paint(&ptrunk);
265 greg 1.1 }
266    
267    
268 greg 2.10 void
269 greg 2.22 redraw(void) /* redraw the image */
270 greg 1.1 {
271 greg 1.9 (*dev->clear)(hresolu, vresolu);
272 greg 1.1 (*dev->comout)("redrawing...\n");
273 greg 1.9 repaint(0, 0, hresolu, vresolu);
274 greg 1.1 (*dev->comout)("\n");
275     }
276    
277    
278 greg 2.10 void
279 greg 2.22 repaint( /* repaint a region */
280     int xmin,
281     int ymin,
282     int xmax,
283     int ymax
284     )
285 greg 1.1 {
286     RECT reg;
287    
288     reg.l = xmin; reg.r = xmax;
289     reg.d = ymin; reg.u = ymax;
290    
291 greg 2.22 paintrect(&ptrunk, &reg);
292 greg 1.1 }
293    
294    
295 greg 2.10 void
296 greg 2.22 paintrect( /* paint picture rectangle */
297     PNODE *p,
298     RECT *r
299     )
300 greg 1.1 {
301     int mx, my;
302    
303 greg 2.22 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
304 greg 1.1 return;
305    
306     if (p->kid == NULL) {
307     (*dev->paintr)(greyscale?greyof(p->v):p->v,
308 greg 2.22 p->xmin, p->ymin, p->xmax, p->ymax); /* do this */
309 greg 1.1 return;
310     }
311 greg 2.22 mx = (p->xmin + p->xmax) >> 1; /* do kids */
312     my = (p->ymin + p->ymax) >> 1;
313 greg 1.1 if (mx > r->l) {
314     if (my > r->d)
315 greg 2.22 paintrect(p->kid+DL, r);
316 greg 1.1 if (my < r->u)
317 greg 2.22 paintrect(p->kid+UL, r);
318 greg 1.1 }
319     if (mx < r->r) {
320     if (my > r->d)
321 greg 2.22 paintrect(p->kid+DR, r);
322 greg 1.1 if (my < r->u)
323 greg 2.22 paintrect(p->kid+UR, r);
324 greg 1.1 }
325     }
326    
327    
328     PNODE *
329 greg 2.22 findrect( /* find a rectangle */
330     int x,
331     int y,
332     PNODE *p,
333     int pd
334     )
335 greg 1.1 {
336     int mx, my;
337    
338     while (p->kid != NULL && pd--) {
339    
340 greg 2.22 mx = (p->xmin + p->xmax) >> 1;
341     my = (p->ymin + p->ymax) >> 1;
342 greg 1.1
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 greg 2.10 void
362 greg 2.22 scalepict( /* scale picture values */
363     PNODE *p,
364     double sf
365     )
366 greg 1.1 {
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 greg 2.10 void
380 greg 2.22 getpictcolrs( /* get scanline from picture */
381     int yoff,
382     COLR *scan,
383     PNODE *p,
384     int xsiz,
385     int ysiz
386     )
387 greg 1.1 {
388 greg 2.22 int mx;
389 greg 1.1 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 greg 2.10 void
413 greg 2.22 freepkids( /* free pnode's children */
414     PNODE *p
415     )
416 greg 1.1 {
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 greg 2.10 free((void *)p->kid);
424 greg 1.1 p->kid = NULL;
425     }
426    
427    
428 greg 2.10 void
429 greg 2.22 newview( /* change viewing parameters */
430     VIEW *vp
431     )
432 greg 1.1 {
433     char *err;
434    
435 greg 1.6 if ((err = setview(vp)) != NULL) {
436 greg 1.1 sprintf(errmsg, "view not set - %s", err);
437     error(COMMAND, errmsg);
438 schorsch 2.12 } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
439 schorsch 2.13 oldview = ourview;
440     ourview = *vp;
441 greg 2.22 newimage(NULL);
442 greg 1.1 }
443     }
444    
445    
446 greg 2.10 void
447 greg 2.22 moveview( /* move viewpoint */
448     double angle,
449     double elev,
450     double mag,
451     FVECT vc
452     )
453 greg 1.1 {
454     double d;
455 greg 1.4 FVECT v1;
456 greg 2.14 VIEW nv = ourview;
457 greg 2.22 int i;
458 greg 1.1
459     spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
460 greg 1.4 if (elev != 0.0) {
461 greg 1.5 fcross(v1, ourview.vup, nv.vdir);
462 greg 1.4 normalize(v1);
463     spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
464     }
465 greg 2.15 if (nv.type == VT_PAR) {
466     nv.horiz /= mag;
467     nv.vert /= mag;
468 greg 1.3 d = 0.0; /* don't move closer */
469 greg 1.2 for (i = 0; i < 3; i++)
470 greg 1.3 d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
471 greg 1.1 } else {
472 greg 1.2 d = sqrt(dist2(ourview.vp, vc)) / mag;
473 greg 2.15 if (nv.vfore > FTINY) {
474 greg 2.8 nv.vfore += d - d*mag;
475     if (nv.vfore < 0.0) nv.vfore = 0.0;
476     }
477 greg 2.15 if (nv.vaft > FTINY) {
478 greg 2.8 nv.vaft += d - d*mag;
479     if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
480     }
481 greg 2.16 nv.vdist /= mag;
482 greg 1.1 }
483 greg 1.2 for (i = 0; i < 3; i++)
484     nv.vp[i] = vc[i] - d*nv.vdir[i];
485 greg 1.1 newview(&nv);
486 greg 1.17 }
487    
488    
489 greg 2.10 void
490 greg 2.22 pcopy( /* copy paint node p1 into p2 */
491     PNODE *p1,
492     PNODE *p2
493     )
494 greg 2.10 {
495     copycolor(p2->v, p1->v);
496     p2->x = p1->x;
497     p2->y = p1->y;
498     }
499    
500    
501     void
502 greg 2.22 zoomview( /* zoom in or out */
503     VIEW *vp,
504     double zf
505     )
506 greg 1.17 {
507     switch (vp->type) {
508     case VT_PAR: /* parallel view */
509 greg 2.9 vp->horiz /= zf;
510     vp->vert /= zf;
511     return;
512 greg 1.17 case VT_ANG: /* angular fisheye */
513     vp->horiz /= zf;
514 greg 2.9 if (vp->horiz > 360.)
515     vp->horiz = 360.;
516 greg 1.17 vp->vert /= zf;
517 greg 2.9 if (vp->vert > 360.)
518     vp->vert = 360.;
519     return;
520 greg 2.21 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 greg 2.9 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 greg 1.17 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 greg 1.1 }