ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv3.c
(Generate patch)

Comparing ray/src/rt/rv3.c (file contents):
Revision 2.4 by greg, Thu Feb 11 16:42:22 1993 UTC vs.
Revision 2.30 by greg, Tue Sep 16 21:14:11 2008 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1987 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  rv3.c - miscellaneous routines for rview.
6   *
7 < *     5/11/87
7 > *  External symbols declared in rpaint.h
8   */
9  
10 < #include  "ray.h"
10 > #include "copyright.h"
11  
12 < #include  "octree.h"
12 > #include <string.h>
13  
14 + #include  "ray.h"
15   #include  "rpaint.h"
18
16   #include  "random.h"
17  
18   #ifndef WFLUSH
19 < #ifdef SPEED
23 < #define WFLUSH          (5*SPEED)
24 < #else
25 < #define WFLUSH          100             /* flush after this many rays */
19 > #define WFLUSH          64              /* flush after this many primary rays */
20   #endif
21 + #ifndef WFLUSH1
22 + #define WFLUSH1         512             /* or this many total rays */
23   #endif
24  
25 +
26   #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  
32 + static unsigned long  niflush;          /* flushes since newimage() */
33  
34 < getrect(s, r)                           /* get a box */
35 < char  *s;
36 < register RECT  *r;
34 > int
35 > getrect(                                /* get a box */
36 >        char  *s,
37 >        RECT  *r
38 > )
39   {
40          int  x0, y0, x1, y1;
41  
# Line 79 | Line 79 | register RECT  *r;
79   }
80  
81  
82 < getinterest(s, direc, vec, mp)          /* get area of interest */
83 < char  *s;
84 < int  direc;
85 < FVECT  vec;
86 < double  *mp;
82 > int
83 > getinterest(            /* get area of interest */
84 >        char  *s,
85 >        int  direc,
86 >        FVECT  vec,
87 >        double  *mp
88 > )
89   {
90          int  x, y;
91          RAY  thisray;
92 <        register int  i;
92 >        int  i;
93  
94          if (sscanf(s, "%lf", mp) != 1)
95                  *mp = 1.0;
# Line 103 | Line 105 | double  *mp;
105                  (*dev->comout)("Pick view center\n");
106                  if ((*dev->getcur)(&x, &y) == ABORT)
107                          return(-1);
108 <                if (viewray(thisray.rorg, thisray.rdir, &ourview,
109 <                                (x+.5)/hresolu, (y+.5)/vresolu) < 0) {
108 >                if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
109 >                        &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
110                          error(COMMAND, "not on image");
111                          return(-1);
112                  }
113                  if (!direc || ourview.type == VT_PAR) {
114 <                        rayorigin(&thisray, NULL, PRIMARY, 1.0);
114 >                        rayorigin(&thisray, PRIMARY, NULL, NULL);
115                          if (!localhit(&thisray, &thescene)) {
116                                  error(COMMAND, "not a local object");
117                                  return(-1);
# Line 123 | Line 125 | double  *mp;
125                                  VCOPY(vec, thisray.rdir);
126                  else
127                          VCOPY(vec, thisray.rop);
128 <        } else if (direc)
129 <                        for (i = 0; i < 3; i++)
130 <                                vec[i] -= ourview.vp[i];
128 >        } 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          return(0);
137   }
138  
139  
140   float *         /* keep consistent with COLOR typedef */
141 < greyof(col)                             /* convert color to greyscale */
142 < register COLOR  col;
141 > greyof(                         /* convert color to greyscale */
142 >        COLOR  col
143 > )
144   {
145          static COLOR  gcol;
146          double  b;
# Line 142 | Line 150 | register COLOR  col;
150          return(gcol);
151   }
152  
153 + 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 < paint(p, xmin, ymin, xmax, ymax)        /* compute and paint a rectangle */
171 < register PNODE  *p;
172 < int  xmin, ymin, xmax, ymax;
170 >        (*dev->paintr)(greyscale?greyof(p->v):p->v,
171 >                        p->xmin, p->ymin, p->xmax, p->ymax);
172 > }
173 >
174 > int
175 > paint(                  /* compute and paint a rectangle */
176 >        PNODE  *p
177 > )
178   {
150        extern long  nrays;
151        static long  lastflush = 0;
179          static RAY  thisray;
180          double  h, v;
181  
182 <        if (xmax - xmin <= 0 || ymax - ymin <= 0) {     /* empty */
183 <                p->x = xmin;
184 <                p->y = ymin;
182 >        if ((p->xmax <= p->xmin) | (p->ymax <= p->ymin)) {      /* empty */
183 >                p->x = p->xmin;
184 >                p->y = p->ymin;
185                  setcolor(p->v, 0.0, 0.0, 0.0);
186 <                return;
186 >                return(0);
187          }
188                                                  /* jitter ray direction */
189 <        h = xmin + (xmax-xmin)*frandom();
190 <        v = ymin + (ymax-ymin)*frandom();
189 >        p->x = h = p->xmin + (p->xmax-p->xmin)*frandom();
190 >        p->y = v = p->ymin + (p->ymax-p->ymin)*frandom();
191          
192 <        if (viewray(thisray.rorg, thisray.rdir, &ourview,
193 <                        h/hresolu, v/vresolu) < 0) {
192 >        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
193 >                        h/hresolu, v/vresolu)) < -FTINY) {
194                  setcolor(thisray.rcol, 0.0, 0.0, 0.0);
195 <        } else {
196 <                rayorigin(&thisray, NULL, PRIMARY, 1.0);
197 <                samplendx++;
198 <                rayvalue(&thisray);
195 >        } else if (nproc == 1) {                /* immediate mode */
196 >                ray_trace(&thisray);
197 >        } else {                                /* queuing mode */
198 >                int     rval;
199 >                rayorigin(&thisray, PRIMARY, NULL, NULL);
200 >                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          }
208  
174        p->x = h;
175        p->y = v;
209          copycolor(p->v, thisray.rcol);
210          scalecolor(p->v, exposure);
211  
212 <        (*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax);
212 >        recolor(p);                             /* paint it */
213  
214 <        if (dev->flush != NULL && nrays - lastflush >= WFLUSH) {
215 <                lastflush = nrays;
216 <                (*dev->flush)();
214 >        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          }
234 +        return(1);
235   }
236  
237  
238 < newimage()                              /* start a new image */
238 > int
239 > waitrays(void)                                  /* finish up pending rays */
240   {
241 +        int     nwaited = 0;
242 +        int     rval;
243 +        RAY     raydone;
244 +
245 +        if (nproc <= 1)                         /* immediate mode? */
246 +                return(0);
247 +        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 +                recolor(p);
252 +                nwaited++;
253 +        }
254 +        if (rval < 0)
255 +                return(-1);
256 +        return(nwaited);
257 + }
258 +
259 +
260 + void
261 + newimage(                                       /* start a new image */
262 +        char *s
263 + )
264 + {
265 +        extern int      ray_pnprocs;
266 +        int             newnp;
267 +                                                /* change in nproc? */
268 +        if (s != NULL && sscanf(s, "%d", &newnp) == 1 &&
269 +                        (newnp > 0) & (newnp != nproc)) {
270 +                if (!newparam) {
271 +                        if (newnp == 1)
272 +                                ray_pclose(0);
273 +                        else if (newnp < ray_pnprocs)
274 +                                ray_pclose(ray_pnprocs - newnp);
275 +                        else
276 +                                ray_popen(newnp - ray_pnprocs);
277 +                }
278 +                nproc = newnp;
279 +        }
280                                                  /* free old image */
281          freepkids(&ptrunk);
192                                                /* save reserve memory */
193        fillreserves();
282                                                  /* compute resolution */
283          hresolu = dev->xsiz;
284          vresolu = dev->ysiz;
285          normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
286 <        pframe.l = pframe.d = 0;
287 <        pframe.r = hresolu; pframe.u = vresolu;
286 >        ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
287 >        ptrunk.xmax = pframe.r = hresolu;
288 >        ptrunk.ymax = pframe.u = vresolu;
289          pdepth = 0;
290                                                  /* clear device */
291          (*dev->clear)(hresolu, vresolu);
292 <                                                /* get first value */
293 <        paint(&ptrunk, 0, 0, hresolu, vresolu);
292 >
293 >        if (newparam) {                         /* (re)start rendering procs */
294 >                if (ray_pnprocs > 0)
295 >                        ray_pclose(0);
296 >                if (nproc > 1)
297 >                        ray_popen(nproc);
298 >                newparam = 0;
299 >        }
300 >        niflush = 0;                            /* get first value */
301 >        paint(&ptrunk);
302   }
303  
304  
305 < redraw()                                /* redraw the image */
305 > void
306 > redraw(void)                            /* redraw the image */
307   {
308          (*dev->clear)(hresolu, vresolu);
309          (*dev->comout)("redrawing...\n");
# Line 214 | Line 312 | redraw()                               /* redraw the image */
312   }
313  
314  
315 < repaint(xmin, ymin, xmax, ymax)                 /* repaint a region */
316 < int  xmin, ymin, xmax, ymax;
315 > void
316 > repaint(                                /* repaint a region */
317 >        int  xmin,
318 >        int  ymin,
319 >        int  xmax,
320 >        int  ymax
321 > )
322   {
323          RECT  reg;
324  
325          reg.l = xmin; reg.r = xmax;
326          reg.d = ymin; reg.u = ymax;
327  
328 <        paintrect(&ptrunk, 0, 0, hresolu, vresolu, &reg);
328 >        paintrect(&ptrunk, &reg);
329   }
330  
331  
332 < paintrect(p, xmin, ymin, xmax, ymax, r)         /* paint picture rectangle */
333 < register PNODE  *p;
334 < int  xmin, ymin, xmax, ymax;
335 < register RECT  *r;
332 > void
333 > paintrect(                              /* paint picture rectangle */
334 >        PNODE  *p,
335 >        RECT  *r
336 > )
337   {
338          int  mx, my;
339  
340 <        if (xmax - xmin <= 0 || ymax - ymin <= 0)
340 >        if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
341                  return;
342  
343          if (p->kid == NULL) {
344                  (*dev->paintr)(greyscale?greyof(p->v):p->v,
345 <                                xmin, ymin, xmax, ymax);        /* do this */
345 >                        p->xmin, p->ymin, p->xmax, p->ymax);    /* do this */
346                  return;
347          }
348 <        mx = (xmin + xmax) >> 1;                                /* do kids */
349 <        my = (ymin + ymax) >> 1;
348 >        mx = (p->xmin + p->xmax) >> 1;                          /* do kids */
349 >        my = (p->ymin + p->ymax) >> 1;
350          if (mx > r->l) {
351                  if (my > r->d)
352 <                        paintrect(p->kid+DL, xmin, ymin, mx, my, r);
352 >                        paintrect(p->kid+DL, r);
353                  if (my < r->u)
354 <                        paintrect(p->kid+UL, xmin, my, mx, ymax, r);
354 >                        paintrect(p->kid+UL, r);
355          }
356          if (mx < r->r) {
357                  if (my > r->d)
358 <                        paintrect(p->kid+DR, mx, ymin, xmax, my, r);
358 >                        paintrect(p->kid+DR, r);
359                  if (my < r->u)
360 <                        paintrect(p->kid+UR, mx, my, xmax, ymax, r);
360 >                        paintrect(p->kid+UR, r);
361          }
362   }
363  
364  
365   PNODE *
366 < findrect(x, y, p, r, pd)                /* find a rectangle */
367 < int  x, y;
368 < register PNODE  *p;
369 < register RECT  *r;
370 < int  pd;
366 > findrect(                               /* find a rectangle */
367 >        int  x,
368 >        int  y,
369 >        PNODE  *p,
370 >        int  pd
371 > )
372   {
373          int  mx, my;
374  
375          while (p->kid != NULL && pd--) {
376  
377 <                mx = (r->l + r->r) >> 1;
378 <                my = (r->d + r->u) >> 1;
377 >                mx = (p->xmin + p->xmax) >> 1;
378 >                my = (p->ymin + p->ymax) >> 1;
379  
380                  if (x < mx) {
276                        r->r = mx;
381                          if (y < my) {
278                                r->u = my;
382                                  p = p->kid+DL;
383                          } else {
281                                r->d = my;
384                                  p = p->kid+UL;
385                          }
386                  } else {
285                        r->l = mx;
387                          if (y < my) {
287                                r->u = my;
388                                  p = p->kid+DR;
389                          } else {
290                                r->d = my;
390                                  p = p->kid+UR;
391                          }
392                  }
# Line 296 | Line 395 | int  pd;
395   }
396  
397  
398 < scalepict(p, sf)                        /* scale picture values */
399 < register PNODE  *p;
400 < double  sf;
398 > void
399 > compavg(                                /* recompute averages */
400 >        PNODE   *p
401 > )
402   {
403 +        int     i, navg;
404 +        
405 +        if (p->kid == NULL)
406 +                return;
407 +
408 +        setcolor(p->v, .0, .0, .0);
409 +        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 + }
420 +
421 +
422 + void
423 + scalepict(                              /* scale picture values */
424 +        PNODE  *p,
425 +        double  sf
426 + )
427 + {
428          scalecolor(p->v, sf);           /* do this node */
429  
430          if (p->kid == NULL)
# Line 312 | Line 437 | double  sf;
437   }
438  
439  
440 < getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */
441 < int  yoff;
442 < register COLR  *scan;
443 < register PNODE  *p;
444 < int  xsiz, ysiz;
440 > void
441 > getpictcolrs(                           /* get scanline from picture */
442 >        int  yoff,
443 >        COLR  *scan,
444 >        PNODE  *p,
445 >        int  xsiz,
446 >        int  ysiz
447 > )
448   {
449 <        register int  mx;
449 >        int  mx;
450          int  my;
451  
452          if (p->kid == NULL) {                   /* do this node */
# Line 342 | Line 470 | int  xsiz, ysiz;
470   }
471  
472  
473 < pcopy(p1, p2)                           /* copy paint node p1 into p2 */
474 < register PNODE  *p1, *p2;
473 > void
474 > freepkids(                              /* free pnode's children */
475 >        PNODE  *p
476 > )
477   {
348        copycolor(p2->v, p1->v);
349        p2->x = p1->x;
350        p2->y = p1->y;
351 }
352
353
354 freepkids(p)                            /* free pnode's children */
355 register PNODE  *p;
356 {
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 <        free((char *)p->kid);
484 >        free((void *)p->kid);
485          p->kid = NULL;
486   }
487  
488  
489 < newview(vp)                             /* change viewing parameters */
490 < register VIEW  *vp;
489 > void
490 > newview(                                        /* change viewing parameters */
491 >        VIEW  *vp
492 > )
493   {
494          char  *err;
495  
496          if ((err = setview(vp)) != NULL) {
497                  sprintf(errmsg, "view not set - %s", err);
498                  error(COMMAND, errmsg);
499 <        } else if (bcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
500 <                copystruct(&oldview, &ourview);
501 <                copystruct(&ourview, vp);
502 <                newimage();
499 >        } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
500 >                oldview = ourview;
501 >                ourview = *vp;
502 >                newimage(NULL);
503          }
504   }
505  
506  
507 < moveview(angle, elev, mag, vc)                  /* move viewpoint */
508 < double  angle, elev, mag;
509 < FVECT  vc;
507 > void
508 > moveview(                                       /* move viewpoint */
509 >        double  angle,
510 >        double  elev,
511 >        double  mag,
512 >        FVECT  vc
513 > )
514   {
515          double  d;
516          FVECT  v1;
517 <        VIEW  nv;
518 <        register int  i;
517 >        VIEW  nv = ourview;
518 >        int  i;
519  
393        VCOPY(nv.vup, ourview.vup);
394        nv.hoff = ourview.hoff; nv.voff = ourview.voff;
520          spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
521          if (elev != 0.0) {
522                  fcross(v1, ourview.vup, nv.vdir);
523                  normalize(v1);
524                  spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
525          }
526 <        if ((nv.type = ourview.type) == VT_PAR) {
527 <                nv.horiz = ourview.horiz / mag;
528 <                nv.vert = ourview.vert / mag;
526 >        if (nv.type == VT_PAR) {
527 >                nv.horiz /= mag;
528 >                nv.vert /= mag;
529                  d = 0.0;                        /* don't move closer */
530                  for (i = 0; i < 3; i++)
531                          d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
532          } else {
408                nv.horiz = ourview.horiz;
409                nv.vert = ourview.vert;
533                  d = sqrt(dist2(ourview.vp, vc)) / mag;
534 +                if (nv.vfore > FTINY) {
535 +                        nv.vfore += d - d*mag;
536 +                        if (nv.vfore < 0.0) nv.vfore = 0.0;
537 +                }
538 +                if (nv.vaft > FTINY) {
539 +                        nv.vaft += d - d*mag;
540 +                        if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
541 +                }
542 +                nv.vdist /= mag;
543          }
544          for (i = 0; i < 3; i++)
545                  nv.vp[i] = vc[i] - d*nv.vdir[i];
# Line 415 | Line 547 | FVECT  vc;
547   }
548  
549  
550 < zoomview(vp, zf)                        /* zoom in our out */
551 < register VIEW  *vp;
552 < double  zf;
550 > void
551 > pcopy(                                  /* copy paint node p1 into p2 */
552 >        PNODE  *p1,
553 >        PNODE  *p2
554 > )
555   {
556 +        copycolor(p2->v, p1->v);
557 +        p2->x = p1->x;
558 +        p2->y = p1->y;
559 + }
560 +
561 +
562 + void
563 + zoomview(                               /* zoom in or out */
564 +        VIEW  *vp,
565 +        double  zf
566 + )
567 + {
568          switch (vp->type) {
569          case VT_PAR:                            /* parallel view */
570 +                vp->horiz /= zf;
571 +                vp->vert /= zf;
572 +                return;
573          case VT_ANG:                            /* angular fisheye */
574                  vp->horiz /= zf;
575 +                if (vp->horiz > 360.)
576 +                        vp->horiz = 360.;
577                  vp->vert /= zf;
578 +                if (vp->vert > 360.)
579 +                        vp->vert = 360.;
580 +                return;
581 +        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 +        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                  return;
599          case VT_PER:                            /* perspective view */
600                  vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) /

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines