ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 2.30
Committed: Tue Sep 16 21:14:11 2008 UTC (15 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.29: +7 -8 lines
Log Message:
Cosmetic changes

File Contents

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