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 1.17 by greg, Sun Oct 14 11:06:27 1990 UTC vs.
Revision 2.24 by greg, Fri Aug 22 17:39:26 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 < #define WFLUSH          30              /* flush after this many rays */
19 > #define WFLUSH          64              /* flush after this many rays */
20   #endif
21  
22 + #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  
28 < getrect(s, r)                           /* get a box */
29 < char  *s;
30 < register RECT  *r;
28 > static unsigned long  niflush;          /* flushes since newimage() */
29 >
30 > int
31 > getrect(                                /* get a box */
32 >        char  *s,
33 >        RECT  *r
34 > )
35   {
36          int  x0, y0, x1, y1;
37  
# Line 69 | Line 75 | register RECT  *r;
75   }
76  
77  
78 < getinterest(s, direc, vec, mp)          /* get area of interest */
79 < char  *s;
80 < int  direc;
81 < FVECT  vec;
82 < double  *mp;
78 > int
79 > getinterest(            /* get area of interest */
80 >        char  *s,
81 >        int  direc,
82 >        FVECT  vec,
83 >        double  *mp
84 > )
85   {
86          int  x, y;
87          RAY  thisray;
88 <        register int  i;
88 >        int  i;
89  
90          if (sscanf(s, "%lf", mp) != 1)
91                  *mp = 1.0;
# Line 87 | Line 95 | double  *mp;
95                  error(COMMAND, "illegal magnification");
96                  return(-1);
97          }
98 <        if (sscanf(s, "%*lf %lf %lf %lf", &vec[0], &vec[1], &vec[2]) != 3) {
98 >        if (!sscanvec(sskip(s), vec)) {
99                  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 <                if (viewray(thisray.rorg, thisray.rdir, &ourview,
105 <                                (x+.5)/hresolu, (y+.5)/vresolu) < 0) {
104 >                if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
105 >                        &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
106                          error(COMMAND, "not on image");
107                          return(-1);
108                  }
109                  if (!direc || ourview.type == VT_PAR) {
110 <                        rayorigin(&thisray, NULL, PRIMARY, 1.0);
110 >                        rayorigin(&thisray, PRIMARY, NULL, NULL);
111                          if (!localhit(&thisray, &thescene)) {
112                                  error(COMMAND, "not a local object");
113                                  return(-1);
# Line 113 | Line 121 | double  *mp;
121                                  VCOPY(vec, thisray.rdir);
122                  else
123                          VCOPY(vec, thisray.rop);
124 <        } else if (direc)
125 <                        for (i = 0; i < 3; i++)
126 <                                vec[i] -= ourview.vp[i];
124 >        } 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          return(0);
133   }
134  
135  
136   float *         /* keep consistent with COLOR typedef */
137 < greyof(col)                             /* convert color to greyscale */
138 < register COLOR  col;
137 > greyof(                         /* convert color to greyscale */
138 >        COLOR  col
139 > )
140   {
141          static COLOR  gcol;
142          double  b;
# Line 133 | Line 147 | register COLOR  col;
147   }
148  
149  
150 < paint(p, xmin, ymin, xmax, ymax)        /* compute and paint a rectangle */
151 < register PNODE  *p;
152 < int  xmin, ymin, xmax, ymax;
150 > int
151 > paint(                  /* compute and paint a rectangle */
152 >        PNODE  *p
153 > )
154   {
155 <        extern long  nrays;
156 <        static long  lastflush = 0;
155 >        extern int  ray_pnprocs;
156 >        static unsigned long  lastflush = 0;
157          static RAY  thisray;
158 +        int     flushintvl;
159          double  h, v;
160  
161 <        if (xmax - xmin <= 0 || ymax - ymin <= 0) {     /* empty */
162 <                p->x = xmin;
163 <                p->y = ymin;
161 >        if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0) { /* empty */
162 >                p->x = p->xmin;
163 >                p->y = p->ymin;
164                  setcolor(p->v, 0.0, 0.0, 0.0);
165 <                return;
165 >                return(0);
166          }
167                                                  /* jitter ray direction */
168 <        h = xmin + (xmax-xmin)*frandom();
169 <        v = ymin + (ymax-ymin)*frandom();
168 >        p->x = h = p->xmin + (p->xmax-p->xmin)*frandom();
169 >        p->y = v = p->ymin + (p->ymax-p->ymin)*frandom();
170          
171 <        if (viewray(thisray.rorg, thisray.rdir, &ourview,
172 <                        h/hresolu, v/vresolu) < 0) {
171 >        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
172 >                        h/hresolu, v/vresolu)) < -FTINY) {
173                  setcolor(thisray.rcol, 0.0, 0.0, 0.0);
174          } else {
175 <                rayorigin(&thisray, NULL, PRIMARY, 1.0);
176 <                rayvalue(&thisray);
175 >                int  rval;
176 >                rayorigin(&thisray, PRIMARY, NULL, NULL);
177 >                thisray.rno = (unsigned long)p;
178 >                rval = ray_pqueue(&thisray);
179 >                if (!rval)
180 >                        return(0);
181 >                if (rval < 0)
182 >                        return(-1);
183 >                p = (PNODE *)thisray.rno;
184          }
185  
163        p->x = h;
164        p->y = v;
186          copycolor(p->v, thisray.rcol);
187          scalecolor(p->v, exposure);
188  
189 <        (*dev->paintr)(greyscale?greyof(p->v):p->v, xmin, ymin, xmax, ymax);
189 >        (*dev->paintr)(greyscale?greyof(p->v):p->v,
190 >                        p->xmin, p->ymin, p->xmax, p->ymax);
191  
192 <        if (dev->flush != NULL && nrays - lastflush >= WFLUSH) {
193 <                lastflush = nrays;
192 >        if (ambounce <= 0)                      /* shall we check for input? */
193 >                flushintvl = ray_pnprocs*WFLUSH;
194 >        else if (niflush < WFLUSH)
195 >                flushintvl = ray_pnprocs*niflush/(ambounce+1);
196 >        else
197 >                flushintvl = ray_pnprocs*WFLUSH/(ambounce+1);
198 >
199 >        if (dev->flush != NULL && raynum - lastflush >= flushintvl) {
200 >                lastflush = raynum;
201                  (*dev->flush)();
202 +                niflush++;
203          }
204 +        return(1);
205   }
206  
207  
208 < newimage()                              /* start a new image */
208 > int
209 > waitrays(void)                                  /* finish up pending rays */
210   {
211 +        int     nwaited = 0;
212 +        int     rval;
213 +        RAY     raydone;
214 +        
215 +        while ((rval = ray_presult(&raydone, 0)) > 0) {
216 +                PNODE  *p = (PNODE *)raydone.rno;
217 +                copycolor(p->v, raydone.rcol);
218 +                scalecolor(p->v, exposure);
219 +                (*dev->paintr)(greyscale?greyof(p->v):p->v,
220 +                                p->xmin, p->ymin, p->xmax, p->ymax);
221 +                nwaited++;
222 +        }
223 +        if (rval < 0)
224 +                return(-1);
225 +        return(nwaited);
226 + }
227 +
228 +
229 + void
230 + newimage(                                       /* start a new image */
231 +        char *s
232 + )
233 + {
234 +        int     newnp;
235 +                                                /* change in nproc? */
236 +        if (s != NULL && sscanf(s, "%d", &newnp) == 1 && newnp > 0) {
237 +                if (!newparam) {
238 +                        if (newnp < nproc)
239 +                                ray_pclose(nproc - newnp);
240 +                        else
241 +                                ray_popen(newnp - nproc);
242 +                }
243 +                nproc = newnp;
244 +        }
245                                                  /* free old image */
246          freepkids(&ptrunk);
247                                                  /* compute resolution */
248          hresolu = dev->xsiz;
249          vresolu = dev->ysiz;
250          normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
251 <        pframe.l = pframe.d = 0;
252 <        pframe.r = hresolu; pframe.u = vresolu;
251 >        ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
252 >        ptrunk.xmax = pframe.r = hresolu;
253 >        ptrunk.ymax = pframe.u = vresolu;
254          pdepth = 0;
255                                                  /* clear device */
256          (*dev->clear)(hresolu, vresolu);
257 <                                                /* get first value */
258 <        paint(&ptrunk, 0, 0, hresolu, vresolu);
257 >
258 >        if (newparam) {                         /* (re)start rendering procs */
259 >                ray_pclose(0);
260 >                ray_popen(nproc);
261 >                newparam = 0;
262 >        }
263 >        niflush = 0;                            /* get first value */
264 >        paint(&ptrunk);
265   }
266  
267  
268 < redraw()                                /* redraw the image */
268 > void
269 > redraw(void)                            /* redraw the image */
270   {
271          (*dev->clear)(hresolu, vresolu);
272          (*dev->comout)("redrawing...\n");
# Line 201 | Line 275 | redraw()                               /* redraw the image */
275   }
276  
277  
278 < repaint(xmin, ymin, xmax, ymax)                 /* repaint a region */
279 < int  xmin, ymin, xmax, ymax;
278 > void
279 > repaint(                                /* repaint a region */
280 >        int  xmin,
281 >        int  ymin,
282 >        int  xmax,
283 >        int  ymax
284 > )
285   {
286          RECT  reg;
287  
288          reg.l = xmin; reg.r = xmax;
289          reg.d = ymin; reg.u = ymax;
290  
291 <        paintrect(&ptrunk, 0, 0, hresolu, vresolu, &reg);
291 >        paintrect(&ptrunk, &reg);
292   }
293  
294  
295 < paintrect(p, xmin, ymin, xmax, ymax, r)         /* paint picture rectangle */
296 < register PNODE  *p;
297 < int  xmin, ymin, xmax, ymax;
298 < register RECT  *r;
295 > void
296 > paintrect(                              /* paint picture rectangle */
297 >        PNODE  *p,
298 >        RECT  *r
299 > )
300   {
301          int  mx, my;
302  
303 <        if (xmax - xmin <= 0 || ymax - ymin <= 0)
303 >        if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
304                  return;
305  
306          if (p->kid == NULL) {
307                  (*dev->paintr)(greyscale?greyof(p->v):p->v,
308 <                                xmin, ymin, xmax, ymax);        /* do this */
308 >                        p->xmin, p->ymin, p->xmax, p->ymax);    /* do this */
309                  return;
310          }
311 <        mx = (xmin + xmax) >> 1;                                /* do kids */
312 <        my = (ymin + ymax) >> 1;
311 >        mx = (p->xmin + p->xmax) >> 1;                          /* do kids */
312 >        my = (p->ymin + p->ymax) >> 1;
313          if (mx > r->l) {
314                  if (my > r->d)
315 <                        paintrect(p->kid+DL, xmin, ymin, mx, my, r);
315 >                        paintrect(p->kid+DL, r);
316                  if (my < r->u)
317 <                        paintrect(p->kid+UL, xmin, my, mx, ymax, r);
317 >                        paintrect(p->kid+UL, r);
318          }
319          if (mx < r->r) {
320                  if (my > r->d)
321 <                        paintrect(p->kid+DR, mx, ymin, xmax, my, r);
321 >                        paintrect(p->kid+DR, r);
322                  if (my < r->u)
323 <                        paintrect(p->kid+UR, mx, my, xmax, ymax, r);
323 >                        paintrect(p->kid+UR, r);
324          }
325   }
326  
327  
328   PNODE *
329 < findrect(x, y, p, r, pd)                /* find a rectangle */
330 < int  x, y;
331 < register PNODE  *p;
332 < register RECT  *r;
333 < int  pd;
329 > findrect(                               /* find a rectangle */
330 >        int  x,
331 >        int  y,
332 >        PNODE  *p,
333 >        int  pd
334 > )
335   {
336          int  mx, my;
337  
338          while (p->kid != NULL && pd--) {
339  
340 <                mx = (r->l + r->r) >> 1;
341 <                my = (r->d + r->u) >> 1;
340 >                mx = (p->xmin + p->xmax) >> 1;
341 >                my = (p->ymin + p->ymax) >> 1;
342  
343                  if (x < mx) {
263                        r->r = mx;
344                          if (y < my) {
265                                r->u = my;
345                                  p = p->kid+DL;
346                          } else {
268                                r->d = my;
347                                  p = p->kid+UL;
348                          }
349                  } else {
272                        r->l = mx;
350                          if (y < my) {
274                                r->u = my;
351                                  p = p->kid+DR;
352                          } else {
277                                r->d = my;
353                                  p = p->kid+UR;
354                          }
355                  }
# Line 283 | Line 358 | int  pd;
358   }
359  
360  
361 < scalepict(p, sf)                        /* scale picture values */
362 < register PNODE  *p;
363 < double  sf;
361 > void
362 > scalepict(                              /* scale picture values */
363 >        PNODE  *p,
364 >        double  sf
365 > )
366   {
367          scalecolor(p->v, sf);           /* do this node */
368  
# Line 299 | Line 376 | double  sf;
376   }
377  
378  
379 < getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */
380 < int  yoff;
381 < register COLR  *scan;
382 < register PNODE  *p;
383 < int  xsiz, ysiz;
379 > void
380 > getpictcolrs(                           /* get scanline from picture */
381 >        int  yoff,
382 >        COLR  *scan,
383 >        PNODE  *p,
384 >        int  xsiz,
385 >        int  ysiz
386 > )
387   {
388 <        register int  mx;
388 >        int  mx;
389          int  my;
390  
391          if (p->kid == NULL) {                   /* do this node */
# Line 329 | Line 409 | int  xsiz, ysiz;
409   }
410  
411  
412 < pcopy(p1, p2)                           /* copy paint node p1 into p2 */
413 < register PNODE  *p1, *p2;
412 > void
413 > freepkids(                              /* free pnode's children */
414 >        PNODE  *p
415 > )
416   {
335        copycolor(p2->v, p1->v);
336        p2->x = p1->x;
337        p2->y = p1->y;
338 }
339
340
341 freepkids(p)                            /* free pnode's children */
342 register PNODE  *p;
343 {
417          if (p->kid == NULL)
418                  return;
419          freepkids(p->kid+DL);
420          freepkids(p->kid+DR);
421          freepkids(p->kid+UL);
422          freepkids(p->kid+UR);
423 <        free((char *)p->kid);
423 >        free((void *)p->kid);
424          p->kid = NULL;
425   }
426  
427  
428 < newview(vp)                             /* change viewing parameters */
429 < register VIEW  *vp;
428 > void
429 > newview(                                        /* change viewing parameters */
430 >        VIEW  *vp
431 > )
432   {
433          char  *err;
434  
435          if ((err = setview(vp)) != NULL) {
436                  sprintf(errmsg, "view not set - %s", err);
437                  error(COMMAND, errmsg);
438 <        } else if (bcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
439 <                copystruct(&oldview, &ourview);
440 <                copystruct(&ourview, vp);
441 <                newimage();
438 >        } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
439 >                oldview = ourview;
440 >                ourview = *vp;
441 >                newimage(NULL);
442          }
443   }
444  
445  
446 < moveview(angle, elev, mag, vc)                  /* move viewpoint */
447 < double  angle, elev, mag;
448 < FVECT  vc;
446 > void
447 > moveview(                                       /* move viewpoint */
448 >        double  angle,
449 >        double  elev,
450 >        double  mag,
451 >        FVECT  vc
452 > )
453   {
375        extern double  sqrt(), dist2();
454          double  d;
455          FVECT  v1;
456 <        VIEW  nv;
457 <        register int  i;
456 >        VIEW  nv = ourview;
457 >        int  i;
458  
381        VCOPY(nv.vup, ourview.vup);
382        nv.hoff = ourview.hoff; nv.voff = ourview.voff;
459          spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
460          if (elev != 0.0) {
461                  fcross(v1, ourview.vup, nv.vdir);
462                  normalize(v1);
463                  spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
464          }
465 <        if ((nv.type = ourview.type) == VT_PAR) {
466 <                nv.horiz = ourview.horiz / mag;
467 <                nv.vert = ourview.vert / mag;
465 >        if (nv.type == VT_PAR) {
466 >                nv.horiz /= mag;
467 >                nv.vert /= mag;
468                  d = 0.0;                        /* don't move closer */
469                  for (i = 0; i < 3; i++)
470                          d += (vc[i] - ourview.vp[i])*ourview.vdir[i];
471          } else {
396                nv.horiz = ourview.horiz;
397                nv.vert = ourview.vert;
472                  d = sqrt(dist2(ourview.vp, vc)) / mag;
473 +                if (nv.vfore > FTINY) {
474 +                        nv.vfore += d - d*mag;
475 +                        if (nv.vfore < 0.0) nv.vfore = 0.0;
476 +                }
477 +                if (nv.vaft > FTINY) {
478 +                        nv.vaft += d - d*mag;
479 +                        if (nv.vaft <= nv.vfore) nv.vaft = 0.0;
480 +                }
481 +                nv.vdist /= mag;
482          }
483          for (i = 0; i < 3; i++)
484                  nv.vp[i] = vc[i] - d*nv.vdir[i];
# Line 403 | Line 486 | FVECT  vc;
486   }
487  
488  
489 < zoomview(vp, zf)                        /* zoom in our out */
490 < register VIEW  *vp;
491 < double  zf;
489 > void
490 > pcopy(                                  /* copy paint node p1 into p2 */
491 >        PNODE  *p1,
492 >        PNODE  *p2
493 > )
494   {
495 +        copycolor(p2->v, p1->v);
496 +        p2->x = p1->x;
497 +        p2->y = p1->y;
498 + }
499 +
500 +
501 + void
502 + zoomview(                               /* zoom in or out */
503 +        VIEW  *vp,
504 +        double  zf
505 + )
506 + {
507          switch (vp->type) {
508          case VT_PAR:                            /* parallel view */
509 +                vp->horiz /= zf;
510 +                vp->vert /= zf;
511 +                return;
512          case VT_ANG:                            /* angular fisheye */
513                  vp->horiz /= zf;
514 +                if (vp->horiz > 360.)
515 +                        vp->horiz = 360.;
516                  vp->vert /= zf;
517 +                if (vp->vert > 360.)
518 +                        vp->vert = 360.;
519                  return;
520 +        case VT_PLS:                            /* planisphere fisheye */
521 +                vp->horiz = sin((PI/180./2.)*vp->horiz) /
522 +                                (1.0 + cos((PI/180./2.)*vp->horiz)) / zf;
523 +                vp->horiz *= vp->horiz;
524 +                vp->horiz = (2.*180./PI)*acos((1. - vp->horiz) /
525 +                                                (1. + vp->horiz));
526 +                vp->vert = sin((PI/180./2.)*vp->vert) /
527 +                                (1.0 + cos((PI/180./2.)*vp->vert)) / zf;
528 +                vp->vert *= vp->vert;
529 +                vp->vert = (2.*180./PI)*acos((1. - vp->vert) /
530 +                                                (1. + vp->vert));
531 +                return;
532 +        case VT_CYL:                            /* cylindrical panorama */
533 +                vp->horiz /= zf;
534 +                if (vp->horiz > 360.)
535 +                        vp->horiz = 360.;
536 +                vp->vert = atan(tan(vp->vert*(PI/180./2.))/zf) / (PI/180./2.);
537 +                return;
538          case VT_PER:                            /* perspective view */
539                  vp->horiz = atan(tan(vp->horiz*(PI/180./2.))/zf) /
540                                  (PI/180./2.);
# Line 432 | Line 554 | double  zf;
554                          vp->vert = asin(vp->vert) / (PI/180./2.);
555                  return;
556          }
435 }
436
437
438 spinvector(vres, vorig, vnorm, theta)   /* rotate vector around normal */
439 FVECT  vres, vorig, vnorm;
440 double  theta;
441 {
442        extern double  sin(), cos();
443        double  sint, cost, dotp;
444        FVECT  vperp;
445        register int  i;
446        
447        if (theta == 0.0) {
448                VCOPY(vres, vorig);
449                return;
450        }
451        sint = sin(theta);
452        cost = cos(theta);
453        dotp = DOT(vorig, vnorm);
454        fcross(vperp, vnorm, vorig);
455        for (i = 0; i < 3; i++)
456                vres[i] = vnorm[i]*dotp*(1.-cost) +
457                                vorig[i]*cost + vperp[i]*sint;
557   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines