ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 2.35
Committed: Sat Oct 16 15:08:14 2010 UTC (13 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R1
Changes since 2.34: +15 -15 lines
Log Message:
Simplified process handling logic in newimage()

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.35 static const char RCSid[] = "$Id: rv3.c,v 2.34 2010/10/09 22:38:35 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.32 static RNUMBER 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.32 thisray.rno = (RNUMBER)p;
201 greg 2.22 rval = ray_pqueue(&thisray);
202     if (!rval)
203     return(0);
204     if (rval < 0)
205     return(-1);
206 greg 2.33 /* get node for returned ray */
207 greg 2.22 p = (PNODE *)thisray.rno;
208 greg 1.17 }
209 greg 1.1
210     copycolor(p->v, thisray.rcol);
211     scalecolor(p->v, exposure);
212    
213 greg 2.26 recolor(p); /* paint it */
214 greg 1.13
215 greg 2.29 if (dev->flush != NULL) { /* shall we check for input? */
216 greg 2.32 static RNUMBER lastflush = 0;
217     RNUMBER counter = raynum;
218     int flushintvl;
219 greg 2.29 if (nproc == 1) {
220     counter = nrays;
221     flushintvl = WFLUSH1;
222     } else if (ambounce == 0)
223     flushintvl = nproc*WFLUSH;
224     else if (niflush < WFLUSH)
225     flushintvl = nproc*niflush/(ambounce+1);
226     else
227     flushintvl = nproc*WFLUSH/(ambounce+1);
228 greg 2.31 if (lastflush > counter)
229     lastflush = 0; /* counter wrapped */
230 greg 2.29
231     if (counter - lastflush >= flushintvl) {
232     lastflush = counter;
233     (*dev->flush)();
234     niflush++;
235     }
236 greg 1.13 }
237 greg 2.22 return(1);
238     }
239    
240    
241     int
242 greg 2.24 waitrays(void) /* finish up pending rays */
243 greg 2.22 {
244     int nwaited = 0;
245     int rval;
246     RAY raydone;
247 greg 2.29
248 greg 2.30 if (nproc <= 1) /* immediate mode? */
249 greg 2.29 return(0);
250 greg 2.22 while ((rval = ray_presult(&raydone, 0)) > 0) {
251     PNODE *p = (PNODE *)raydone.rno;
252     copycolor(p->v, raydone.rcol);
253     scalecolor(p->v, exposure);
254 greg 2.26 recolor(p);
255 greg 2.22 nwaited++;
256     }
257     if (rval < 0)
258     return(-1);
259     return(nwaited);
260 greg 1.1 }
261    
262    
263 greg 2.10 void
264 greg 2.22 newimage( /* start a new image */
265     char *s
266     )
267 greg 1.1 {
268 greg 2.29 extern int ray_pnprocs;
269 greg 2.35 int newnp = 0;
270     /* # rendering procs arg? */
271     if (s != NULL)
272     sscanf(s, "%d", &newnp);
273 greg 1.1 /* free old image */
274     freepkids(&ptrunk);
275 greg 1.9 /* compute resolution */
276 greg 1.10 hresolu = dev->xsiz;
277     vresolu = dev->ysiz;
278 greg 1.11 normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
279 greg 2.22 ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
280     ptrunk.xmax = pframe.r = hresolu;
281     ptrunk.ymax = pframe.u = vresolu;
282 greg 1.1 pdepth = 0;
283     /* clear device */
284 greg 1.9 (*dev->clear)(hresolu, vresolu);
285 greg 2.22
286     if (newparam) { /* (re)start rendering procs */
287 greg 2.29 if (ray_pnprocs > 0)
288     ray_pclose(0);
289 greg 2.35 if (newnp > 0)
290     nproc = newnp;
291 greg 2.29 if (nproc > 1)
292     ray_popen(nproc);
293 greg 2.22 newparam = 0;
294 greg 2.35 } else if ((newnp > 0) & (newnp != nproc)) {
295     if (newnp == 1) /* change # rendering procs */
296     ray_pclose(0);
297     else if (newnp < ray_pnprocs)
298     ray_pclose(ray_pnprocs - newnp);
299     else
300     ray_popen(newnp - ray_pnprocs);
301     nproc = newnp;
302 greg 2.22 }
303 greg 2.23 niflush = 0; /* get first value */
304 greg 2.22 paint(&ptrunk);
305 greg 1.1 }
306    
307    
308 greg 2.10 void
309 greg 2.22 redraw(void) /* redraw the image */
310 greg 1.1 {
311 greg 1.9 (*dev->clear)(hresolu, vresolu);
312 greg 1.1 (*dev->comout)("redrawing...\n");
313 greg 1.9 repaint(0, 0, hresolu, vresolu);
314 greg 1.1 (*dev->comout)("\n");
315     }
316    
317    
318 greg 2.10 void
319 greg 2.22 repaint( /* repaint a region */
320     int xmin,
321     int ymin,
322     int xmax,
323     int ymax
324     )
325 greg 1.1 {
326     RECT reg;
327    
328     reg.l = xmin; reg.r = xmax;
329     reg.d = ymin; reg.u = ymax;
330    
331 greg 2.22 paintrect(&ptrunk, &reg);
332 greg 1.1 }
333    
334    
335 greg 2.10 void
336 greg 2.22 paintrect( /* paint picture rectangle */
337     PNODE *p,
338     RECT *r
339     )
340 greg 1.1 {
341     int mx, my;
342    
343 greg 2.22 if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
344 greg 1.1 return;
345    
346     if (p->kid == NULL) {
347     (*dev->paintr)(greyscale?greyof(p->v):p->v,
348 greg 2.22 p->xmin, p->ymin, p->xmax, p->ymax); /* do this */
349 greg 1.1 return;
350     }
351 greg 2.22 mx = (p->xmin + p->xmax) >> 1; /* do kids */
352     my = (p->ymin + p->ymax) >> 1;
353 greg 1.1 if (mx > r->l) {
354     if (my > r->d)
355 greg 2.22 paintrect(p->kid+DL, r);
356 greg 1.1 if (my < r->u)
357 greg 2.22 paintrect(p->kid+UL, r);
358 greg 1.1 }
359     if (mx < r->r) {
360     if (my > r->d)
361 greg 2.22 paintrect(p->kid+DR, r);
362 greg 1.1 if (my < r->u)
363 greg 2.22 paintrect(p->kid+UR, r);
364 greg 1.1 }
365     }
366    
367    
368     PNODE *
369 greg 2.22 findrect( /* find a rectangle */
370     int x,
371     int y,
372     PNODE *p,
373     int pd
374     )
375 greg 1.1 {
376     int mx, my;
377    
378     while (p->kid != NULL && pd--) {
379    
380 greg 2.22 mx = (p->xmin + p->xmax) >> 1;
381     my = (p->ymin + p->ymax) >> 1;
382 greg 1.1
383     if (x < mx) {
384     if (y < my) {
385     p = p->kid+DL;
386     } else {
387     p = p->kid+UL;
388     }
389     } else {
390     if (y < my) {
391     p = p->kid+DR;
392     } else {
393     p = p->kid+UR;
394     }
395     }
396     }
397     return(p);
398     }
399    
400    
401 greg 2.10 void
402 greg 2.27 compavg( /* recompute averages */
403     PNODE *p
404     )
405     {
406 greg 2.28 int i, navg;
407    
408 greg 2.27 if (p->kid == NULL)
409     return;
410 greg 2.28
411 greg 2.27 setcolor(p->v, .0, .0, .0);
412 greg 2.28 navg = 0;
413     for (i = 0; i < 4; i++) {
414     if (p->kid[i].xmin >= p->kid[i].xmax) continue;
415     if (p->kid[i].ymin >= p->kid[i].ymax) continue;
416     compavg(p->kid+i);
417     addcolor(p->v, p->kid[i].v);
418     navg++;
419     }
420     if (navg > 1)
421     scalecolor(p->v, 1./navg);
422 greg 2.27 }
423    
424    
425     void
426 greg 2.22 scalepict( /* scale picture values */
427     PNODE *p,
428     double sf
429     )
430 greg 1.1 {
431     scalecolor(p->v, sf); /* do this node */
432    
433     if (p->kid == NULL)
434     return;
435     /* do children */
436     scalepict(p->kid+DL, sf);
437     scalepict(p->kid+DR, sf);
438     scalepict(p->kid+UL, sf);
439     scalepict(p->kid+UR, sf);
440     }
441    
442    
443 greg 2.10 void
444 greg 2.22 getpictcolrs( /* get scanline from picture */
445     int yoff,
446     COLR *scan,
447     PNODE *p,
448     int xsiz,
449     int ysiz
450     )
451 greg 1.1 {
452 greg 2.22 int mx;
453 greg 1.1 int my;
454    
455     if (p->kid == NULL) { /* do this node */
456     setcolr(scan[0], colval(p->v,RED),
457     colval(p->v,GRN),
458     colval(p->v,BLU));
459     for (mx = 1; mx < xsiz; mx++)
460     copycolr(scan[mx], scan[0]);
461     return;
462     }
463     /* do kids */
464     mx = xsiz >> 1;
465     my = ysiz >> 1;
466     if (yoff < my) {
467     getpictcolrs(yoff, scan, p->kid+DL, mx, my);
468     getpictcolrs(yoff, scan+mx, p->kid+DR, xsiz-mx, my);
469     } else {
470     getpictcolrs(yoff-my, scan, p->kid+UL, mx, ysiz-my);
471     getpictcolrs(yoff-my, scan+mx, p->kid+UR, xsiz-mx, ysiz-my);
472     }
473     }
474    
475    
476 greg 2.10 void
477 greg 2.22 freepkids( /* free pnode's children */
478     PNODE *p
479     )
480 greg 1.1 {
481     if (p->kid == NULL)
482     return;
483     freepkids(p->kid+DL);
484     freepkids(p->kid+DR);
485     freepkids(p->kid+UL);
486     freepkids(p->kid+UR);
487 greg 2.10 free((void *)p->kid);
488 greg 1.1 p->kid = NULL;
489     }
490    
491    
492 greg 2.10 void
493 greg 2.22 newview( /* change viewing parameters */
494     VIEW *vp
495     )
496 greg 1.1 {
497     char *err;
498    
499 greg 1.6 if ((err = setview(vp)) != NULL) {
500 greg 1.1 sprintf(errmsg, "view not set - %s", err);
501     error(COMMAND, errmsg);
502 schorsch 2.12 } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
503 schorsch 2.13 oldview = ourview;
504     ourview = *vp;
505 greg 2.22 newimage(NULL);
506 greg 1.1 }
507     }
508    
509    
510 greg 2.10 void
511 greg 2.22 moveview( /* move viewpoint */
512     double angle,
513     double elev,
514     double mag,
515     FVECT vc
516     )
517 greg 1.1 {
518     double d;
519 greg 1.4 FVECT v1;
520 greg 2.14 VIEW nv = ourview;
521 greg 2.22 int i;
522 greg 1.1
523     spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
524 greg 1.4 if (elev != 0.0) {
525 greg 1.5 fcross(v1, ourview.vup, nv.vdir);
526 greg 1.4 normalize(v1);
527     spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
528     }
529 greg 2.15 if (nv.type == VT_PAR) {
530     nv.horiz /= mag;
531     nv.vert /= mag;
532 greg 1.3 d = 0.0; /* don't move closer */
533 greg 1.2 for (i = 0; i < 3; i++)
534 greg 1.3 d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
535 greg 1.1 } else {
536 greg 1.2 d = sqrt(dist2(ourview.vp, vc)) / mag;
537 greg 2.15 if (nv.vfore > FTINY) {
538 greg 2.8 nv.vfore += d - d*mag;
539     if (nv.vfore < 0.0) nv.vfore = 0.0;
540     }
541 greg 2.15 if (nv.vaft > FTINY) {
542 greg 2.8 nv.vaft += d - d*mag;
543     if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
544     }
545 greg 2.16 nv.vdist /= mag;
546 greg 1.1 }
547 greg 1.2 for (i = 0; i < 3; i++)
548     nv.vp[i] = vc[i] - d*nv.vdir[i];
549 greg 1.1 newview(&nv);
550 greg 1.17 }
551    
552    
553 greg 2.10 void
554 greg 2.22 pcopy( /* copy paint node p1 into p2 */
555     PNODE *p1,
556     PNODE *p2
557     )
558 greg 2.10 {
559     copycolor(p2->v, p1->v);
560     p2->x = p1->x;
561     p2->y = p1->y;
562     }
563    
564    
565     void
566 greg 2.22 zoomview( /* zoom in or out */
567     VIEW *vp,
568     double zf
569     )
570 greg 1.17 {
571     switch (vp->type) {
572     case VT_PAR: /* parallel view */
573 greg 2.9 vp->horiz /= zf;
574     vp->vert /= zf;
575     return;
576 greg 1.17 case VT_ANG: /* angular fisheye */
577     vp->horiz /= zf;
578 greg 2.9 if (vp->horiz > 360.)
579     vp->horiz = 360.;
580 greg 1.17 vp->vert /= zf;
581 greg 2.9 if (vp->vert > 360.)
582     vp->vert = 360.;
583     return;
584 greg 2.21 case VT_PLS: /* planisphere fisheye */
585     vp->horiz = sin((PI/180./2.)*vp->horiz) /
586     (1.0 + cos((PI/180./2.)*vp->horiz)) / zf;
587     vp->horiz *= vp->horiz;
588     vp->horiz = (2.*180./PI)*acos((1. - vp->horiz) /
589     (1. + vp->horiz));
590     vp->vert = sin((PI/180./2.)*vp->vert) /
591     (1.0 + cos((PI/180./2.)*vp->vert)) / zf;
592     vp->vert *= vp->vert;
593     vp->vert = (2.*180./PI)*acos((1. - vp->vert) /
594     (1. + vp->vert));
595     return;
596 greg 2.9 case VT_CYL: /* cylindrical panorama */
597     vp->horiz /= zf;
598     if (vp->horiz > 360.)
599     vp->horiz = 360.;
600     vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / (PI/180./2.);
601 greg 1.17 return;
602     case VT_PER: /* perspective view */
603     vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) /
604     (PI/180./2.);
605     vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) /
606     (PI/180./2.);
607     return;
608     case VT_HEM: /* hemispherical fisheye */
609     vp->horiz = sin(vp->horiz*(PI/180./2.))/zf;
610     if (vp->horiz >= 1.0-FTINY)
611     vp->horiz = 180.;
612     else
613     vp->horiz = asin(vp->horiz) / (PI/180./2.);
614     vp->vert = sin(vp->vert*(PI/180./2.))/zf;
615     if (vp->vert >= 1.0-FTINY)
616     vp->vert = 180.;
617     else
618     vp->vert = asin(vp->vert) / (PI/180./2.);
619     return;
620     }
621 greg 1.1 }