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.21 by greg, Tue Mar 11 02:21:47 2008 UTC vs.
Revision 2.29 by greg, Tue Sep 16 18:35:56 2008 UTC

# Line 16 | Line 16 | static const char      RCSid[] = "$Id$";
16   #include  "random.h"
17  
18   #ifndef WFLUSH
19 < #define WFLUSH          2048            /* 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   int
35 < getrect(s, r)                           /* get a box */
36 < char  *s;
37 < register RECT  *r;
35 > getrect(                                /* get a box */
36 >        char  *s,
37 >        RECT  *r
38 > )
39   {
40          int  x0, y0, x1, y1;
41  
# Line 74 | Line 80 | register RECT  *r;
80  
81  
82   int
83 < getinterest(s, direc, vec, mp)          /* get area of interest */
84 < char  *s;
85 < int  direc;
86 < FVECT  vec;
87 < double  *mp;
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 131 | Line 138 | double  *mp;
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 < void
171 < paint(p, xmin, ymin, xmax, ymax)        /* compute and paint a rectangle */
172 < register PNODE  *p;
173 < 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   {
151        static unsigned 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 ((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 {
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 <                samplendx++;
201 <                rayvalue(&thisray);
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 + 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 */
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 && newnp > 0) {
269 +                if (!newparam) {
270 +                        if (nproc == 1)
271 +                                nproc = 0;      /* actually running */
272 +                        if (newnp == 1)
273 +                                ray_pclose(0);
274 +                        else if (newnp < nproc)
275 +                                ray_pclose(nproc - newnp);
276 +                        else
277 +                                ray_popen(newnp - nproc);
278 +                }
279 +                nproc = newnp;
280 +        }
281                                                  /* free old image */
282          freepkids(&ptrunk);
193                                                /* save reserve memory */
194        fillreserves();
283                                                  /* compute resolution */
284          hresolu = dev->xsiz;
285          vresolu = dev->ysiz;
286          normaspect(viewaspect(&ourview), &dev->pixaspect, &hresolu, &vresolu);
287 <        pframe.l = pframe.d = 0;
288 <        pframe.r = hresolu; pframe.u = vresolu;
287 >        ptrunk.xmin = ptrunk.ymin = pframe.l = pframe.d = 0;
288 >        ptrunk.xmax = pframe.r = hresolu;
289 >        ptrunk.ymax = pframe.u = vresolu;
290          pdepth = 0;
291                                                  /* clear device */
292          (*dev->clear)(hresolu, vresolu);
293 <                                                /* get first value */
294 <        paint(&ptrunk, 0, 0, hresolu, vresolu);
293 >
294 >        if (newparam) {                         /* (re)start rendering procs */
295 >                if (ray_pnprocs > 0)
296 >                        ray_pclose(0);
297 >                if (nproc > 1)
298 >                        ray_popen(nproc);
299 >                newparam = 0;
300 >        }
301 >        niflush = 0;                            /* get first value */
302 >        paint(&ptrunk);
303   }
304  
305  
306   void
307 < redraw()                                /* redraw the image */
307 > redraw(void)                            /* redraw the image */
308   {
309          (*dev->clear)(hresolu, vresolu);
310          (*dev->comout)("redrawing...\n");
# Line 217 | Line 314 | redraw()                               /* redraw the image */
314  
315  
316   void
317 < repaint(xmin, ymin, xmax, ymax)                 /* repaint a region */
318 < int  xmin, ymin, xmax, ymax;
317 > repaint(                                /* repaint a region */
318 >        int  xmin,
319 >        int  ymin,
320 >        int  xmax,
321 >        int  ymax
322 > )
323   {
324          RECT  reg;
325  
326          reg.l = xmin; reg.r = xmax;
327          reg.d = ymin; reg.u = ymax;
328  
329 <        paintrect(&ptrunk, 0, 0, hresolu, vresolu, &reg);
329 >        paintrect(&ptrunk, &reg);
330   }
331  
332  
333   void
334 < paintrect(p, xmin, ymin, xmax, ymax, r)         /* paint picture rectangle */
335 < register PNODE  *p;
336 < int  xmin, ymin, xmax, ymax;
337 < register RECT  *r;
334 > paintrect(                              /* paint picture rectangle */
335 >        PNODE  *p,
336 >        RECT  *r
337 > )
338   {
339          int  mx, my;
340  
341 <        if (xmax - xmin <= 0 || ymax - ymin <= 0)
341 >        if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0)
342                  return;
343  
344          if (p->kid == NULL) {
345                  (*dev->paintr)(greyscale?greyof(p->v):p->v,
346 <                                xmin, ymin, xmax, ymax);        /* do this */
346 >                        p->xmin, p->ymin, p->xmax, p->ymax);    /* do this */
347                  return;
348          }
349 <        mx = (xmin + xmax) >> 1;                                /* do kids */
350 <        my = (ymin + ymax) >> 1;
349 >        mx = (p->xmin + p->xmax) >> 1;                          /* do kids */
350 >        my = (p->ymin + p->ymax) >> 1;
351          if (mx > r->l) {
352                  if (my > r->d)
353 <                        paintrect(p->kid+DL, xmin, ymin, mx, my, r);
353 >                        paintrect(p->kid+DL, r);
354                  if (my < r->u)
355 <                        paintrect(p->kid+UL, xmin, my, mx, ymax, r);
355 >                        paintrect(p->kid+UL, r);
356          }
357          if (mx < r->r) {
358                  if (my > r->d)
359 <                        paintrect(p->kid+DR, mx, ymin, xmax, my, r);
359 >                        paintrect(p->kid+DR, r);
360                  if (my < r->u)
361 <                        paintrect(p->kid+UR, mx, my, xmax, ymax, r);
361 >                        paintrect(p->kid+UR, r);
362          }
363   }
364  
365  
366   PNODE *
367 < findrect(x, y, p, r, pd)                /* find a rectangle */
368 < int  x, y;
369 < register PNODE  *p;
370 < register RECT  *r;
371 < int  pd;
367 > findrect(                               /* find a rectangle */
368 >        int  x,
369 >        int  y,
370 >        PNODE  *p,
371 >        int  pd
372 > )
373   {
374          int  mx, my;
375  
376          while (p->kid != NULL && pd--) {
377  
378 <                mx = (r->l + r->r) >> 1;
379 <                my = (r->d + r->u) >> 1;
378 >                mx = (p->xmin + p->xmax) >> 1;
379 >                my = (p->ymin + p->ymax) >> 1;
380  
381                  if (x < mx) {
280                        r->r = mx;
382                          if (y < my) {
282                                r->u = my;
383                                  p = p->kid+DL;
384                          } else {
285                                r->d = my;
385                                  p = p->kid+UL;
386                          }
387                  } else {
289                        r->l = mx;
388                          if (y < my) {
291                                r->u = my;
389                                  p = p->kid+DR;
390                          } else {
294                                r->d = my;
391                                  p = p->kid+UR;
392                          }
393                  }
# Line 301 | Line 397 | int  pd;
397  
398  
399   void
400 < scalepict(p, sf)                        /* scale picture values */
401 < register PNODE  *p;
402 < double  sf;
400 > compavg(                                /* recompute averages */
401 >        PNODE   *p
402 > )
403   {
404 +        int     i, navg;
405 +        
406 +        if (p->kid == NULL)
407 +                return;
408 +
409 +        setcolor(p->v, .0, .0, .0);
410 +        navg = 0;
411 +        for (i = 0; i < 4; i++) {
412 +                if (p->kid[i].xmin >= p->kid[i].xmax) continue;
413 +                if (p->kid[i].ymin >= p->kid[i].ymax) continue;
414 +                compavg(p->kid+i);
415 +                addcolor(p->v, p->kid[i].v);
416 +                navg++;
417 +        }
418 +        if (navg > 1)
419 +                scalecolor(p->v, 1./navg);
420 + }
421 +
422 +
423 + void
424 + scalepict(                              /* scale picture values */
425 +        PNODE  *p,
426 +        double  sf
427 + )
428 + {
429          scalecolor(p->v, sf);           /* do this node */
430  
431          if (p->kid == NULL)
# Line 318 | Line 439 | double  sf;
439  
440  
441   void
442 < getpictcolrs(yoff, scan, p, xsiz, ysiz) /* get scanline from picture */
443 < int  yoff;
444 < register COLR  *scan;
445 < register PNODE  *p;
446 < int  xsiz, ysiz;
442 > getpictcolrs(                           /* get scanline from picture */
443 >        int  yoff,
444 >        COLR  *scan,
445 >        PNODE  *p,
446 >        int  xsiz,
447 >        int  ysiz
448 > )
449   {
450 <        register int  mx;
450 >        int  mx;
451          int  my;
452  
453          if (p->kid == NULL) {                   /* do this node */
# Line 349 | Line 472 | int  xsiz, ysiz;
472  
473  
474   void
475 < freepkids(p)                            /* free pnode's children */
476 < register PNODE  *p;
475 > freepkids(                              /* free pnode's children */
476 >        PNODE  *p
477 > )
478   {
479          if (p->kid == NULL)
480                  return;
# Line 364 | Line 488 | register PNODE  *p;
488  
489  
490   void
491 < newview(vp)                             /* change viewing parameters */
492 < register VIEW  *vp;
491 > newview(                                        /* change viewing parameters */
492 >        VIEW  *vp
493 > )
494   {
495          char  *err;
496  
# Line 375 | Line 500 | register VIEW  *vp;
500          } else if (memcmp((char *)vp, (char *)&ourview, sizeof(VIEW))) {
501                  oldview = ourview;
502                  ourview = *vp;
503 <                newimage();
503 >                newimage(NULL);
504          }
505   }
506  
507  
508   void
509 < moveview(angle, elev, mag, vc)                  /* move viewpoint */
510 < double  angle, elev, mag;
511 < FVECT  vc;
509 > moveview(                                       /* move viewpoint */
510 >        double  angle,
511 >        double  elev,
512 >        double  mag,
513 >        FVECT  vc
514 > )
515   {
516          double  d;
517          FVECT  v1;
518          VIEW  nv = ourview;
519 <        register int  i;
519 >        int  i;
520  
521          spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
522          if (elev != 0.0) {
# Line 421 | Line 549 | FVECT  vc;
549  
550  
551   void
552 < pcopy(p1, p2)                           /* copy paint node p1 into p2 */
553 < register PNODE  *p1, *p2;
552 > pcopy(                                  /* copy paint node p1 into p2 */
553 >        PNODE  *p1,
554 >        PNODE  *p2
555 > )
556   {
557          copycolor(p2->v, p1->v);
558          p2->x = p1->x;
# Line 431 | Line 561 | register PNODE  *p1, *p2;
561  
562  
563   void
564 < zoomview(vp, zf)                        /* zoom in or out */
565 < register VIEW  *vp;
566 < double  zf;
564 > zoomview(                               /* zoom in or out */
565 >        VIEW  *vp,
566 >        double  zf
567 > )
568   {
569          switch (vp->type) {
570          case VT_PAR:                            /* parallel view */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines