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.3 by greg, Fri Oct 2 16:19:56 1992 UTC vs.
Revision 2.37 by greg, Tue Mar 20 03:37:08 2012 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 < #define WFLUSH          30              /* 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 + extern int      ray_pnprocs;
33  
34 < getrect(s, r)                           /* get a box */
35 < char  *s;
36 < register RECT  *r;
34 > static RNUMBER  niflush;                /* flushes since newimage() */
35 >
36 > int
37 > getrect(                                /* get a box */
38 >        char  *s,
39 >        RECT  *r
40 > )
41   {
42          int  x0, y0, x1, y1;
43  
# Line 75 | Line 81 | register RECT  *r;
81   }
82  
83  
84 < getinterest(s, direc, vec, mp)          /* get area of interest */
85 < char  *s;
86 < int  direc;
87 < FVECT  vec;
88 < double  *mp;
84 > int
85 > getinterest(            /* get area of interest */
86 >        char  *s,
87 >        int  direc,
88 >        FVECT  vec,
89 >        double  *mp
90 > )
91   {
92          int  x, y;
93          RAY  thisray;
94 <        register int  i;
94 >        int  i;
95  
96          if (sscanf(s, "%lf", mp) != 1)
97                  *mp = 1.0;
# Line 99 | Line 107 | double  *mp;
107                  (*dev->comout)("Pick view center\n");
108                  if ((*dev->getcur)(&x, &y) == ABORT)
109                          return(-1);
110 <                if (viewray(thisray.rorg, thisray.rdir, &ourview,
111 <                                (x+.5)/hresolu, (y+.5)/vresolu) < 0) {
110 >                if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
111 >                        &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
112                          error(COMMAND, "not on image");
113                          return(-1);
114                  }
115                  if (!direc || ourview.type == VT_PAR) {
116 <                        rayorigin(&thisray, NULL, PRIMARY, 1.0);
116 >                        rayorigin(&thisray, PRIMARY, NULL, NULL);
117                          if (!localhit(&thisray, &thescene)) {
118                                  error(COMMAND, "not a local object");
119                                  return(-1);
# Line 119 | Line 127 | double  *mp;
127                                  VCOPY(vec, thisray.rdir);
128                  else
129                          VCOPY(vec, thisray.rop);
130 <        } else if (direc)
131 <                        for (i = 0; i < 3; i++)
132 <                                vec[i] -= ourview.vp[i];
130 >        } else if (direc) {
131 >                for (i = 0; i < 3; i++)
132 >                        vec[i] -= ourview.vp[i];
133 >                if (normalize(vec) == 0.0) {
134 >                        error(COMMAND, "point at view origin");
135 >                        return(-1);
136 >                }
137 >        }
138          return(0);
139   }
140  
141  
142   float *         /* keep consistent with COLOR typedef */
143 < greyof(col)                             /* convert color to greyscale */
144 < register COLOR  col;
143 > greyof(                         /* convert color to greyscale */
144 >        COLOR  col
145 > )
146   {
147          static COLOR  gcol;
148          double  b;
# Line 138 | Line 152 | register COLOR  col;
152          return(gcol);
153   }
154  
155 + static void
156 + recolor(                                        /* recolor the given node */
157 +        PNODE *p
158 + )
159 + {
160 +        while (p->kid != NULL) {                /* need to propogate down */
161 +                int  mx = (p->xmin + p->xmax) >> 1;
162 +                int  my = (p->ymin + p->ymax) >> 1;
163 +                int  ki;
164 +                if (p->x >= mx)
165 +                        ki = (p->y >= my) ? UR : DR;
166 +                else
167 +                        ki = (p->y >= my) ? UL : DL;
168 +                pcopy(p, p->kid+ki);
169 +                p = p->kid + ki;
170 +        }
171  
172 < paint(p, xmin, ymin, xmax, ymax)        /* compute and paint a rectangle */
173 < register PNODE  *p;
174 < int  xmin, ymin, xmax, ymax;
172 >        (*dev->paintr)(greyscale?greyof(p->v):p->v,
173 >                        p->xmin, p->ymin, p->xmax, p->ymax);
174 > }
175 >
176 > int
177 > paint(                  /* compute and paint a rectangle */
178 >        PNODE  *p
179 > )
180   {
146        extern long  nrays;
147        static long  lastflush = 0;
181          static RAY  thisray;
182          double  h, v;
183  
184 <        if (xmax - xmin <= 0 || ymax - ymin <= 0) {     /* empty */
185 <                p->x = xmin;
186 <                p->y = ymin;
184 >        if ((p->xmax <= p->xmin) | (p->ymax <= p->ymin)) {      /* empty */
185 >                p->x = p->xmin;
186 >                p->y = p->ymin;
187                  setcolor(p->v, 0.0, 0.0, 0.0);
188 <                return;
188 >                return(0);
189          }
190                                                  /* jitter ray direction */
191 <        h = xmin + (xmax-xmin)*frandom();
192 <        v = ymin + (ymax-ymin)*frandom();
191 >        p->x = h = p->xmin + (p->xmax-p->xmin)*frandom();
192 >        p->y = v = p->ymin + (p->ymax-p->ymin)*frandom();
193          
194 <        if (viewray(thisray.rorg, thisray.rdir, &ourview,
195 <                        h/hresolu, v/vresolu) < 0) {
194 >        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
195 >                        h/hresolu, v/vresolu)) < -FTINY) {
196                  setcolor(thisray.rcol, 0.0, 0.0, 0.0);
197 <        } else {
198 <                rayorigin(&thisray, NULL, PRIMARY, 1.0);
199 <                samplendx++;
200 <                rayvalue(&thisray);
197 >        } else if (!ray_pnprocs) {              /* immediate mode */
198 >                ray_trace(&thisray);
199 >        } else {                                /* queuing mode */
200 >                int     rval;
201 >                rayorigin(&thisray, PRIMARY, NULL, NULL);
202 >                thisray.rno = (RNUMBER)p;
203 >                rval = ray_pqueue(&thisray);
204 >                if (!rval)
205 >                        return(0);
206 >                if (rval < 0)
207 >                        return(-1);
208 >                                                /* get node for returned ray */
209 >                p = (PNODE *)thisray.rno;
210          }
211  
170        p->x = h;
171        p->y = v;
212          copycolor(p->v, thisray.rcol);
213          scalecolor(p->v, exposure);
214  
215 <        (*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax);
215 >        recolor(p);                             /* paint it */
216  
217 <        if (dev->flush != NULL && nrays - lastflush >= WFLUSH) {
218 <                lastflush = nrays;
219 <                (*dev->flush)();
217 >        if (dev->flush != NULL) {               /* shall we check for input? */
218 >                static RNUMBER  lastflush = 0;
219 >                RNUMBER         counter = raynum;
220 >                int             flushintvl;
221 >                if (!ray_pnprocs) {
222 >                        counter = nrays;
223 >                        flushintvl = WFLUSH1;
224 >                } else if (ambounce == 0)
225 >                        flushintvl = ray_pnprocs*WFLUSH;
226 >                else if (niflush < WFLUSH)
227 >                        flushintvl = ray_pnprocs*niflush/(ambounce+1);
228 >                else
229 >                        flushintvl = ray_pnprocs*WFLUSH/(ambounce+1);
230 >                if (lastflush > counter)
231 >                        lastflush = 0;          /* counter wrapped */
232 >
233 >                if (counter - lastflush >= flushintvl) {
234 >                        lastflush = counter;
235 >                        (*dev->flush)();
236 >                        niflush++;
237 >                }
238          }
239 +        return(1);
240   }
241  
242  
243 < newimage()                              /* start a new image */
243 > int
244 > waitrays(void)                                  /* finish up pending rays */
245   {
246 +        int     nwaited = 0;
247 +        int     rval;
248 +        RAY     raydone;
249 +
250 +        if (!ray_pnprocs)                       /* immediate mode? */
251 +                return(0);
252 +        while ((rval = ray_presult(&raydone, 0)) > 0) {
253 +                PNODE  *p = (PNODE *)raydone.rno;
254 +                copycolor(p->v, raydone.rcol);
255 +                scalecolor(p->v, exposure);
256 +                recolor(p);
257 +                nwaited++;
258 +        }
259 +        if (rval < 0)
260 +                return(-1);
261 +        return(nwaited);
262 + }
263 +
264 +
265 + void
266 + newimage(                                       /* start a new image */
267 +        char *s
268 + )
269 + {
270 +        int             newnp = 0;
271 +                                                /* # rendering procs arg? */
272 +        if (s != NULL)
273 +                sscanf(s, "%d", &newnp);
274                                                  /* free old image */
275          freepkids(&ptrunk);
188                                                /* save reserve memory */
189        fillreserves();
276                                                  /* compute resolution */
277          hresolu = dev->xsiz;
278          vresolu = dev->ysiz;
279          normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
280 <        pframe.l = pframe.d = 0;
281 <        pframe.r = hresolu; pframe.u = vresolu;
280 >        ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
281 >        ptrunk.xmax = pframe.r = hresolu;
282 >        ptrunk.ymax = pframe.u = vresolu;
283          pdepth = 0;
284                                                  /* clear device */
285          (*dev->clear)(hresolu, vresolu);
286 <                                                /* get first value */
287 <        paint(&ptrunk, 0, 0, hresolu, vresolu);
286 >
287 >        if (newparam) {                         /* (re)start rendering procs */
288 >                if (ray_pnprocs)
289 >                        ray_pclose(0);          /* should already be closed */
290 >                if (newnp > 0)
291 >                        nproc = newnp;
292 >                if (nproc > 1)
293 >                        ray_popen(nproc);
294 >                newparam = 0;
295 >        } else if ((newnp > 0) & (newnp != nproc)) {
296 >                if (newnp == 1)                 /* change # rendering procs */
297 >                        ray_pclose(0);
298 >                else if (newnp < ray_pnprocs)
299 >                        ray_pclose(ray_pnprocs - newnp);
300 >                else
301 >                        ray_popen(newnp - ray_pnprocs);
302 >                nproc = newnp;
303 >        }
304 >        niflush = 0;                            /* get first value */
305 >        paint(&ptrunk);
306   }
307  
308  
309 < redraw()                                /* redraw the image */
309 > void
310 > redraw(void)                            /* redraw the image */
311   {
312          (*dev->clear)(hresolu, vresolu);
313          (*dev->comout)("redrawing...\n");
# Line 210 | Line 316 | redraw()                               /* redraw the image */
316   }
317  
318  
319 < repaint(xmin, ymin, xmax, ymax)                 /* repaint a region */
320 < int  xmin, ymin, xmax, ymax;
319 > void
320 > repaint(                                /* repaint a region */
321 >        int  xmin,
322 >        int  ymin,
323 >        int  xmax,
324 >        int  ymax
325 > )
326   {
327          RECT  reg;
328  
329          reg.l = xmin; reg.r = xmax;
330          reg.d = ymin; reg.u = ymax;
331  
332 <        paintrect(&ptrunk, 0, 0, hresolu, vresolu, &reg);
332 >        paintrect(&ptrunk, &reg);
333   }
334  
335  
336 < paintrect(p, xmin, ymin, xmax, ymax, r)         /* paint picture rectangle */
337 < register PNODE  *p;
338 < int  xmin, ymin, xmax, ymax;
339 < register RECT  *r;
336 > void
337 > paintrect(                              /* paint picture rectangle */
338 >        PNODE  *p,
339 >        RECT  *r
340 > )
341   {
342          int  mx, my;
343  
344 <        if (xmax - xmin <= 0 || ymax - ymin <= 0)
344 >        if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
345                  return;
346  
347          if (p->kid == NULL) {
348                  (*dev->paintr)(greyscale?greyof(p->v):p->v,
349 <                                xmin, ymin, xmax, ymax);        /* do this */
349 >                        p->xmin, p->ymin, p->xmax, p->ymax);    /* do this */
350                  return;
351          }
352 <        mx = (xmin + xmax) >> 1;                                /* do kids */
353 <        my = (ymin + ymax) >> 1;
352 >        mx = (p->xmin + p->xmax) >> 1;                          /* do kids */
353 >        my = (p->ymin + p->ymax) >> 1;
354          if (mx > r->l) {
355                  if (my > r->d)
356 <                        paintrect(p->kid+DL, xmin, ymin, mx, my, r);
356 >                        paintrect(p->kid+DL, r);
357                  if (my < r->u)
358 <                        paintrect(p->kid+UL, xmin, my, mx, ymax, r);
358 >                        paintrect(p->kid+UL, r);
359          }
360          if (mx < r->r) {
361                  if (my > r->d)
362 <                        paintrect(p->kid+DR, mx, ymin, xmax, my, r);
362 >                        paintrect(p->kid+DR, r);
363                  if (my < r->u)
364 <                        paintrect(p->kid+UR, mx, my, xmax, ymax, r);
364 >                        paintrect(p->kid+UR, r);
365          }
366   }
367  
368  
369   PNODE *
370 < findrect(x, y, p, r, pd)                /* find a rectangle */
371 < int  x, y;
372 < register PNODE  *p;
373 < register RECT  *r;
374 < int  pd;
370 > findrect(                               /* find a rectangle */
371 >        int  x,
372 >        int  y,
373 >        PNODE  *p,
374 >        int  pd
375 > )
376   {
377          int  mx, my;
378  
379          while (p->kid != NULL && pd--) {
380  
381 <                mx = (r->l + r->r) >> 1;
382 <                my = (r->d + r->u) >> 1;
381 >                mx = (p->xmin + p->xmax) >> 1;
382 >                my = (p->ymin + p->ymax) >> 1;
383  
384                  if (x < mx) {
272                        r->r = mx;
385                          if (y < my) {
274                                r->u = my;
386                                  p = p->kid+DL;
387                          } else {
277                                r->d = my;
388                                  p = p->kid+UL;
389                          }
390                  } else {
281                        r->l = mx;
391                          if (y < my) {
283                                r->u = my;
392                                  p = p->kid+DR;
393                          } else {
286                                r->d = my;
394                                  p = p->kid+UR;
395                          }
396                  }
# Line 292 | Line 399 | int  pd;
399   }
400  
401  
402 < scalepict(p, sf)                        /* scale picture values */
403 < register PNODE  *p;
404 < double  sf;
402 > void
403 > compavg(                                /* recompute averages */
404 >        PNODE   *p
405 > )
406   {
407 +        int     i, navg;
408 +        
409 +        if (p->kid == NULL)
410 +                return;
411 +
412 +        setcolor(p->v, .0, .0, .0);
413 +        navg = 0;
414 +        for (i = 0; i < 4; i++) {
415 +                if (p->kid[i].xmin >= p->kid[i].xmax) continue;
416 +                if (p->kid[i].ymin >= p->kid[i].ymax) continue;
417 +                compavg(p->kid+i);
418 +                addcolor(p->v, p->kid[i].v);
419 +                navg++;
420 +        }
421 +        if (navg > 1)
422 +                scalecolor(p->v, 1./navg);
423 + }
424 +
425 +
426 + void
427 + scalepict(                              /* scale picture values */
428 +        PNODE  *p,
429 +        double  sf
430 + )
431 + {
432          scalecolor(p->v, sf);           /* do this node */
433  
434          if (p->kid == NULL)
# Line 308 | Line 441 | double  sf;
441   }
442  
443  
444 < getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */
445 < int  yoff;
446 < register COLR  *scan;
447 < register PNODE  *p;
448 < int  xsiz, ysiz;
444 > void
445 > getpictcolrs(                           /* get scanline from picture */
446 >        int  yoff,
447 >        COLR  *scan,
448 >        PNODE  *p,
449 >        int  xsiz,
450 >        int  ysiz
451 > )
452   {
453 <        register int  mx;
453 >        int  mx;
454          int  my;
455  
456          if (p->kid == NULL) {                   /* do this node */
# Line 338 | Line 474 | int  xsiz, ysiz;
474   }
475  
476  
477 < pcopy(p1, p2)                           /* copy paint node p1 into p2 */
478 < register PNODE  *p1, *p2;
477 > void
478 > freepkids(                              /* free pnode's children */
479 >        PNODE  *p
480 > )
481   {
344        copycolor(p2->v, p1->v);
345        p2->x = p1->x;
346        p2->y = p1->y;
347 }
348
349
350 freepkids(p)                            /* free pnode's children */
351 register PNODE  *p;
352 {
482          if (p->kid == NULL)
483                  return;
484          freepkids(p->kid+DL);
485          freepkids(p->kid+DR);
486          freepkids(p->kid+UL);
487          freepkids(p->kid+UR);
488 <        free((char *)p->kid);
488 >        free((void *)p->kid);
489          p->kid = NULL;
490   }
491  
492  
493 < newview(vp)                             /* change viewing parameters */
494 < register VIEW  *vp;
493 > void
494 > newview(                                        /* change viewing parameters */
495 >        VIEW  *vp
496 > )
497   {
498          char  *err;
499  
500          if ((err = setview(vp)) != NULL) {
501                  sprintf(errmsg, "view not set - %s", err);
502                  error(COMMAND, errmsg);
503 <        } else if (bcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
504 <                copystruct(&oldview, &ourview);
505 <                copystruct(&ourview, vp);
506 <                newimage();
503 >        } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
504 >                oldview = ourview;
505 >                ourview = *vp;
506 >                newimage(NULL);
507          }
508   }
509  
510  
511 < moveview(angle, elev, mag, vc)                  /* move viewpoint */
512 < double  angle, elev, mag;
513 < FVECT  vc;
511 > void
512 > moveview(                                       /* move viewpoint */
513 >        double  angle,
514 >        double  elev,
515 >        double  mag,
516 >        FVECT  vc
517 > )
518   {
519          double  d;
520          FVECT  v1;
521 <        VIEW  nv;
522 <        register int  i;
521 >        VIEW  nv = ourview;
522 >        int  i;
523  
389        VCOPY(nv.vup, ourview.vup);
390        nv.hoff = ourview.hoff; nv.voff = ourview.voff;
524          spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
525          if (elev != 0.0) {
526                  fcross(v1, ourview.vup, nv.vdir);
527                  normalize(v1);
528                  spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
529          }
530 <        if ((nv.type = ourview.type) == VT_PAR) {
531 <                nv.horiz = ourview.horiz / mag;
532 <                nv.vert = ourview.vert / mag;
530 >        if (nv.type == VT_PAR) {
531 >                nv.horiz /= mag;
532 >                nv.vert /= mag;
533                  d = 0.0;                        /* don't move closer */
534                  for (i = 0; i < 3; i++)
535                          d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
536          } else {
404                nv.horiz = ourview.horiz;
405                nv.vert = ourview.vert;
537                  d = sqrt(dist2(ourview.vp, vc)) / mag;
538 +                if (nv.vfore > FTINY) {
539 +                        nv.vfore += d - d*mag;
540 +                        if (nv.vfore < 0.0) nv.vfore = 0.0;
541 +                }
542 +                if (nv.vaft > FTINY) {
543 +                        nv.vaft += d - d*mag;
544 +                        if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
545 +                }
546 +                nv.vdist /= mag;
547          }
548          for (i = 0; i < 3; i++)
549                  nv.vp[i] = vc[i] - d*nv.vdir[i];
# Line 411 | Line 551 | FVECT  vc;
551   }
552  
553  
554 < zoomview(vp, zf)                        /* zoom in our out */
555 < register VIEW  *vp;
556 < double  zf;
554 > void
555 > pcopy(                                  /* copy paint node p1 into p2 */
556 >        PNODE  *p1,
557 >        PNODE  *p2
558 > )
559   {
560 +        copycolor(p2->v, p1->v);
561 +        p2->x = p1->x;
562 +        p2->y = p1->y;
563 + }
564 +
565 +
566 + void
567 + zoomview(                               /* zoom in or out */
568 +        VIEW  *vp,
569 +        double  zf
570 + )
571 + {
572          switch (vp->type) {
573          case VT_PAR:                            /* parallel view */
574 +                vp->horiz /= zf;
575 +                vp->vert /= zf;
576 +                return;
577          case VT_ANG:                            /* angular fisheye */
578                  vp->horiz /= zf;
579 +                if (vp->horiz > 360.)
580 +                        vp->horiz = 360.;
581                  vp->vert /= zf;
582 +                if (vp->vert > 360.)
583 +                        vp->vert = 360.;
584 +                return;
585 +        case VT_PLS:                            /* planisphere fisheye */
586 +                vp->horiz = sin((PI/180./2.)*vp->horiz) /
587 +                                (1.0 + cos((PI/180./2.)*vp->horiz)) / zf;
588 +                vp->horiz *= vp->horiz;
589 +                vp->horiz = (2.*180./PI)*acos((1. - vp->horiz) /
590 +                                                (1. + vp->horiz));
591 +                vp->vert = sin((PI/180./2.)*vp->vert) /
592 +                                (1.0 + cos((PI/180./2.)*vp->vert)) / zf;
593 +                vp->vert *= vp->vert;
594 +                vp->vert = (2.*180./PI)*acos((1. - vp->vert) /
595 +                                                (1. + vp->vert));
596 +                return;
597 +        case VT_CYL:                            /* cylindrical panorama */
598 +                vp->horiz /= zf;
599 +                if (vp->horiz > 360.)
600 +                        vp->horiz = 360.;
601 +                vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / (PI/180./2.);
602                  return;
603          case VT_PER:                            /* perspective view */
604                  vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) /

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines