ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 2.23
Committed: Thu Aug 21 16:13:00 2008 UTC (15 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.22: +8 -4 lines
Log Message:
Improved responsiveness to command input

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.23 static const char RCSid[] = "$Id: rv3.c,v 2.22 2008/08/21 07:05:59 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.23 static int 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     double h, v;
159    
160 greg 2.22 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0) { /* empty */
161     p->x = p->xmin;
162     p->y = p->ymin;
163 greg 1.1 setcolor(p->v, 0.0, 0.0, 0.0);
164 greg 2.22 return(0);
165 greg 1.1 }
166     /* jitter ray direction */
167 greg 2.22 p->x = h = p->xmin + (p->xmax-p->xmin)*frandom();
168     p->y = v = p->ymin + (p->ymax-p->ymin)*frandom();
169 greg 1.1
170 greg 2.6 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
171     h/hresolu, v/vresolu)) < -FTINY) {
172 greg 1.17 setcolor(thisray.rcol, 0.0, 0.0, 0.0);
173     } else {
174 greg 2.22 int rval;
175 greg 2.17 rayorigin(&thisray, PRIMARY, NULL, NULL);
176 greg 2.22 thisray.rno = (unsigned long)p;
177     rval = ray_pqueue(&thisray);
178     if (!rval)
179     return(0);
180     if (rval < 0)
181     return(-1);
182     p = (PNODE *)thisray.rno;
183 greg 1.17 }
184 greg 1.1
185     copycolor(p->v, thisray.rcol);
186     scalecolor(p->v, exposure);
187    
188 greg 2.23 (*dev->paintr)(greyscale?greyof(p->v):p->v,
189     p->xmin, p->ymin, p->xmax, p->ymax);
190 greg 1.13
191 greg 2.23 if (dev->flush != NULL && raynum - lastflush >= ray_pnprocs *
192     (ambounce > 0 && niflush < WFLUSH ? niflush : WFLUSH)) {
193 greg 2.22 lastflush = raynum;
194 greg 1.13 (*dev->flush)();
195 greg 2.23 niflush++;
196 greg 1.13 }
197 greg 2.22 return(1);
198     }
199    
200    
201     int
202     waitrays(void) /* finish up pending rays */
203     {
204     int nwaited = 0;
205     int rval;
206     RAY raydone;
207    
208     while ((rval = ray_presult(&raydone, 0)) > 0) {
209     PNODE *p = (PNODE *)raydone.rno;
210     copycolor(p->v, raydone.rcol);
211     scalecolor(p->v, exposure);
212     (*dev->paintr)(greyscale?greyof(p->v):p->v,
213     p->xmin, p->ymin, p->xmax, p->ymax);
214     nwaited++;
215     }
216     if (rval < 0)
217     return(-1);
218     return(nwaited);
219 greg 1.1 }
220    
221    
222 greg 2.10 void
223 greg 2.22 newimage( /* start a new image */
224     char *s
225     )
226 greg 1.1 {
227 greg 2.22 int newnp;
228     /* change in nproc? */
229     if (s != NULL && sscanf(s, "%d", &newnp) == 1 && newnp > 0) {
230     if (!newparam) {
231     if (newnp < nproc)
232     ray_pclose(nproc - newnp);
233     else
234     ray_popen(newnp - nproc);
235     }
236     nproc = newnp;
237     }
238 greg 1.1 /* free old image */
239     freepkids(&ptrunk);
240 greg 1.9 /* compute resolution */
241 greg 1.10 hresolu = dev->xsiz;
242     vresolu = dev->ysiz;
243 greg 1.11 normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
244 greg 2.22 ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
245     ptrunk.xmax = pframe.r = hresolu;
246     ptrunk.ymax = pframe.u = vresolu;
247 greg 1.1 pdepth = 0;
248     /* clear device */
249 greg 1.9 (*dev->clear)(hresolu, vresolu);
250 greg 2.22
251     if (newparam) { /* (re)start rendering procs */
252     ray_pclose(0);
253     ray_popen(nproc);
254     newparam = 0;
255     }
256 greg 2.23 niflush = 0; /* get first value */
257 greg 2.22 paint(&ptrunk);
258 greg 1.1 }
259    
260    
261 greg 2.10 void
262 greg 2.22 redraw(void) /* redraw the image */
263 greg 1.1 {
264 greg 1.9 (*dev->clear)(hresolu, vresolu);
265 greg 1.1 (*dev->comout)("redrawing...\n");
266 greg 1.9 repaint(0, 0, hresolu, vresolu);
267 greg 1.1 (*dev->comout)("\n");
268     }
269    
270    
271 greg 2.10 void
272 greg 2.22 repaint( /* repaint a region */
273     int xmin,
274     int ymin,
275     int xmax,
276     int ymax
277     )
278 greg 1.1 {
279     RECT reg;
280    
281     reg.l = xmin; reg.r = xmax;
282     reg.d = ymin; reg.u = ymax;
283    
284 greg 2.22 paintrect(&ptrunk, &reg);
285 greg 1.1 }
286    
287    
288 greg 2.10 void
289 greg 2.22 paintrect( /* paint picture rectangle */
290     PNODE *p,
291     RECT *r
292     )
293 greg 1.1 {
294     int mx, my;
295    
296 greg 2.22 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
297 greg 1.1 return;
298    
299     if (p->kid == NULL) {
300     (*dev->paintr)(greyscale?greyof(p->v):p->v,
301 greg 2.22 p->xmin, p->ymin, p->xmax, p->ymax); /* do this */
302 greg 1.1 return;
303     }
304 greg 2.22 mx = (p->xmin + p->xmax) >> 1; /* do kids */
305     my = (p->ymin + p->ymax) >> 1;
306 greg 1.1 if (mx > r->l) {
307     if (my > r->d)
308 greg 2.22 paintrect(p->kid+DL, r);
309 greg 1.1 if (my < r->u)
310 greg 2.22 paintrect(p->kid+UL, r);
311 greg 1.1 }
312     if (mx < r->r) {
313     if (my > r->d)
314 greg 2.22 paintrect(p->kid+DR, r);
315 greg 1.1 if (my < r->u)
316 greg 2.22 paintrect(p->kid+UR, r);
317 greg 1.1 }
318     }
319    
320    
321     PNODE *
322 greg 2.22 findrect( /* find a rectangle */
323     int x,
324     int y,
325     PNODE *p,
326     int pd
327     )
328 greg 1.1 {
329     int mx, my;
330    
331     while (p->kid != NULL && pd--) {
332    
333 greg 2.22 mx = (p->xmin + p->xmax) >> 1;
334     my = (p->ymin + p->ymax) >> 1;
335 greg 1.1
336     if (x < mx) {
337     if (y < my) {
338     p = p->kid+DL;
339     } else {
340     p = p->kid+UL;
341     }
342     } else {
343     if (y < my) {
344     p = p->kid+DR;
345     } else {
346     p = p->kid+UR;
347     }
348     }
349     }
350     return(p);
351     }
352    
353    
354 greg 2.10 void
355 greg 2.22 scalepict( /* scale picture values */
356     PNODE *p,
357     double sf
358     )
359 greg 1.1 {
360     scalecolor(p->v, sf); /* do this node */
361    
362     if (p->kid == NULL)
363     return;
364     /* do children */
365     scalepict(p->kid+DL, sf);
366     scalepict(p->kid+DR, sf);
367     scalepict(p->kid+UL, sf);
368     scalepict(p->kid+UR, sf);
369     }
370    
371    
372 greg 2.10 void
373 greg 2.22 getpictcolrs( /* get scanline from picture */
374     int yoff,
375     COLR *scan,
376     PNODE *p,
377     int xsiz,
378     int ysiz
379     )
380 greg 1.1 {
381 greg 2.22 int mx;
382 greg 1.1 int my;
383    
384     if (p->kid == NULL) { /* do this node */
385     setcolr(scan[0], colval(p->v,RED),
386     colval(p->v,GRN),
387     colval(p->v,BLU));
388     for (mx = 1; mx < xsiz; mx++)
389     copycolr(scan[mx], scan[0]);
390     return;
391     }
392     /* do kids */
393     mx = xsiz >> 1;
394     my = ysiz >> 1;
395     if (yoff < my) {
396     getpictcolrs(yoff, scan, p->kid+DL, mx, my);
397     getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my);
398     } else {
399     getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my);
400     getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my);
401     }
402     }
403    
404    
405 greg 2.10 void
406 greg 2.22 freepkids( /* free pnode's children */
407     PNODE *p
408     )
409 greg 1.1 {
410     if (p->kid == NULL)
411     return;
412     freepkids(p->kid+DL);
413     freepkids(p->kid+DR);
414     freepkids(p->kid+UL);
415     freepkids(p->kid+UR);
416 greg 2.10 free((void *)p->kid);
417 greg 1.1 p->kid = NULL;
418     }
419    
420    
421 greg 2.10 void
422 greg 2.22 newview( /* change viewing parameters */
423     VIEW *vp
424     )
425 greg 1.1 {
426     char *err;
427    
428 greg 1.6 if ((err = setview(vp)) != NULL) {
429 greg 1.1 sprintf(errmsg, "view not set - %s", err);
430     error(COMMAND, errmsg);
431 schorsch 2.12 } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
432 schorsch 2.13 oldview = ourview;
433     ourview = *vp;
434 greg 2.22 newimage(NULL);
435 greg 1.1 }
436     }
437    
438    
439 greg 2.10 void
440 greg 2.22 moveview( /* move viewpoint */
441     double angle,
442     double elev,
443     double mag,
444     FVECT vc
445     )
446 greg 1.1 {
447     double d;
448 greg 1.4 FVECT v1;
449 greg 2.14 VIEW nv = ourview;
450 greg 2.22 int i;
451 greg 1.1
452     spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
453 greg 1.4 if (elev != 0.0) {
454 greg 1.5 fcross(v1, ourview.vup, nv.vdir);
455 greg 1.4 normalize(v1);
456     spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
457     }
458 greg 2.15 if (nv.type == VT_PAR) {
459     nv.horiz /= mag;
460     nv.vert /= mag;
461 greg 1.3 d = 0.0; /* don't move closer */
462 greg 1.2 for (i = 0; i < 3; i++)
463 greg 1.3 d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
464 greg 1.1 } else {
465 greg 1.2 d = sqrt(dist2(ourview.vp, vc)) / mag;
466 greg 2.15 if (nv.vfore > FTINY) {
467 greg 2.8 nv.vfore += d - d*mag;
468     if (nv.vfore < 0.0) nv.vfore = 0.0;
469     }
470 greg 2.15 if (nv.vaft > FTINY) {
471 greg 2.8 nv.vaft += d - d*mag;
472     if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
473     }
474 greg 2.16 nv.vdist /= mag;
475 greg 1.1 }
476 greg 1.2 for (i = 0; i < 3; i++)
477     nv.vp[i] = vc[i] - d*nv.vdir[i];
478 greg 1.1 newview(&nv);
479 greg 1.17 }
480    
481    
482 greg 2.10 void
483 greg 2.22 pcopy( /* copy paint node p1 into p2 */
484     PNODE *p1,
485     PNODE *p2
486     )
487 greg 2.10 {
488     copycolor(p2->v, p1->v);
489     p2->x = p1->x;
490     p2->y = p1->y;
491     }
492    
493    
494     void
495 greg 2.22 zoomview( /* zoom in or out */
496     VIEW *vp,
497     double zf
498     )
499 greg 1.17 {
500     switch (vp->type) {
501     case VT_PAR: /* parallel view */
502 greg 2.9 vp->horiz /= zf;
503     vp->vert /= zf;
504     return;
505 greg 1.17 case VT_ANG: /* angular fisheye */
506     vp->horiz /= zf;
507 greg 2.9 if (vp->horiz > 360.)
508     vp->horiz = 360.;
509 greg 1.17 vp->vert /= zf;
510 greg 2.9 if (vp->vert > 360.)
511     vp->vert = 360.;
512     return;
513 greg 2.21 case VT_PLS: /* planisphere fisheye */
514     vp->horiz = sin((PI/180./2.)*vp->horiz) /
515     (1.0 + cos((PI/180./2.)*vp->horiz)) / zf;
516     vp->horiz *= vp->horiz;
517     vp->horiz = (2.*180./PI)*acos((1. - vp->horiz) /
518     (1. + vp->horiz));
519     vp->vert = sin((PI/180./2.)*vp->vert) /
520     (1.0 + cos((PI/180./2.)*vp->vert)) / zf;
521     vp->vert *= vp->vert;
522     vp->vert = (2.*180./PI)*acos((1. - vp->vert) /
523     (1. + vp->vert));
524     return;
525 greg 2.9 case VT_CYL: /* cylindrical panorama */
526     vp->horiz /= zf;
527     if (vp->horiz > 360.)
528     vp->horiz = 360.;
529     vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / (PI/180./2.);
530 greg 1.17 return;
531     case VT_PER: /* perspective view */
532     vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) /
533     (PI/180./2.);
534     vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) /
535     (PI/180./2.);
536     return;
537     case VT_HEM: /* hemispherical fisheye */
538     vp->horiz = sin(vp->horiz*(PI/180./2.))/zf;
539     if (vp->horiz >= 1.0-FTINY)
540     vp->horiz = 180.;
541     else
542     vp->horiz = asin(vp->horiz) / (PI/180./2.);
543     vp->vert = sin(vp->vert*(PI/180./2.))/zf;
544     if (vp->vert >= 1.0-FTINY)
545     vp->vert = 180.;
546     else
547     vp->vert = asin(vp->vert) / (PI/180./2.);
548     return;
549     }
550 greg 1.1 }