ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
Revision: 2.28
Committed: Fri Sep 5 21:34:09 2008 UTC (15 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.27: +14 -10 lines
Log Message:
Really fixed the exposure bug this time

File Contents

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