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

Comparing ray/src/gen/mkillum2.c (file contents):
Revision 2.14 by schorsch, Sun Mar 28 20:33:12 2004 UTC vs.
Revision 2.17 by greg, Sun Sep 19 08:42:22 2004 UTC

# Line 11 | Line 11 | static const char      RCSid[] = "$Id$";
11   #include  "face.h"
12   #include  "cone.h"
13   #include  "random.h"
14 + #include  "selcall.h"
15  
16  
17 < int o_default(FUN_ARGLIST);
18 < int o_face(FUN_ARGLIST);
19 < int o_sphere(FUN_ARGLIST);
19 < int o_ring(FUN_ARGLIST);
20 < void raysamp(float res[3], FVECT org, FVECT dir, struct rtproc *rt);
21 < void rayflush(struct rtproc *rt);
22 < void mkaxes(FVECT u, FVECT v, FVECT n);
23 < void rounddir(FVECT dv, double alt, double azi);
24 < void flatdir(FVECT dv, double alt, double azi);
17 > static void mkaxes(FVECT u, FVECT v, FVECT n);
18 > static void rounddir(FVECT dv, double alt, double azi);
19 > static void flatdir(FVECT dv, double alt, double azi);
20  
21  
22 + static void
23 + rayclean(                       /* finish all pending rays */
24 +        struct rtproc *rt0
25 + )
26 + {
27 +        rayflush(rt0, 1);
28 +        while (raywait(rt0) != NULL)
29 +                ;
30 + }
31 +
32 +
33   int /* XXX type conflict with otypes.h */
34   o_default(      /* default illum action */
35          OBJREC  *ob,
36          struct illum_args  *il,
37 <        struct rtproc  *rt,
37 >        struct rtproc  *rt0,
38          char  *nm
39   )
40   {
# Line 44 | Line 50 | int
50   o_face(         /* make an illum face */
51          OBJREC  *ob,
52          struct illum_args  *il,
53 <        struct rtproc  *rt,
53 >        struct rtproc  *rt0,
54          char  *nm
55   )
56   {
# Line 63 | Line 69 | o_face(                /* make an illum face */
69          fa = getface(ob);
70          if (fa->area == 0.0) {
71                  freeface(ob);
72 <                return(o_default(ob, il, rt, nm));
72 >                return(o_default(ob, il, rt0, nm));
73          }
74                                  /* set up sampling */
75          if (il->sampdens <= 0)
# Line 127 | Line 133 | o_face(                /* make an illum face */
133                      } while (!inface(org, fa) && nmisses++ < MAXMISS);
134                      if (nmisses > MAXMISS) {
135                          objerror(ob, WARNING, "bad aspect");
136 <                        rt->nrays = 0;
136 >                        rayclean(rt0);
137                          freeface(ob);
138                          free((void *)distarr);
139 <                        return(o_default(ob, il, rt, nm));
139 >                        return(o_default(ob, il, rt0, nm));
140                      }
141                      for (j = 0; j < 3; j++)
142                          org[j] += .001*fa->norm[j];
143                                          /* send sample */
144 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
144 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
145                  }
146 <        rayflush(rt);
146 >        rayclean(rt0);
147                                  /* write out the face and its distribution */
148          if (average(il, distarr, nalt*nazi)) {
149                  if (il->sampdens > 0)
# Line 148 | Line 154 | o_face(                /* make an illum face */
154                                  /* clean up */
155          freeface(ob);
156          free((void *)distarr);
157 +        return(0);
158   #undef MAXMISS
152        /* XXX we need to return something here. what is it? */
159   }
160  
161  
# Line 157 | Line 163 | int
163   o_sphere(       /* make an illum sphere */
164          register OBJREC  *ob,
165          struct illum_args  *il,
166 <        struct rtproc  *rt,
166 >        struct rtproc  *rt0,
167          char  *nm
168   )
169   {
# Line 207 | Line 213 | o_sphere(      /* make an illum sphere */
213                          dir[j] = -dir[j];
214                      }
215                                          /* send sample */
216 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
216 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
217                  }
218 <        rayflush(rt);
218 >        rayclean(rt0);
219                                  /* write out the sphere and its distribution */
220          if (average(il, distarr, nalt*nazi)) {
221                  if (il->sampdens > 0)
# Line 229 | Line 235 | int
235   o_ring(         /* make an illum ring */
236          OBJREC  *ob,
237          struct illum_args  *il,
238 <        struct rtproc  *rt,
238 >        struct rtproc  *rt0,
239          char  *nm
240   )
241   {
# Line 280 | Line 286 | o_ring(                /* make an illum ring */
286                                          .001*co->ad[j];
287  
288                                          /* send sample */
289 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
289 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
290                  }
291 <        rayflush(rt);
291 >        rayclean(rt0);
292                                  /* write out the ring and its distribution */
293          if (average(il, distarr, nalt*nazi)) {
294                  if (il->sampdens > 0)
# Line 298 | Line 304 | o_ring(                /* make an illum ring */
304  
305  
306   void
307 < raysamp(        /* compute a ray sample */
307 > raysamp(        /* queue a ray sample */
308          float  res[3],
309          FVECT  org,
310          FVECT  dir,
311 <        register struct rtproc  *rt
311 >        struct rtproc *rt0
312   )
313   {
314 +        register struct rtproc  *rt;
315          register float  *fp;
316  
317 <        if (rt->nrays == rt->bsiz)
318 <                rayflush(rt);
319 <        rt->dest[rt->nrays] = res;
320 <        fp = rt->buf + 6*rt->nrays++;
317 >        for (rt = rt0; rt != NULL; rt = rt->next)
318 >                if (rt->nrays < rt->bsiz && rt->dest[rt->nrays] == NULL)
319 >                        break;
320 >        if (rt == NULL)         /* need to free up buffer? */
321 >                rt = raywait(rt0);
322 >        if (rt == NULL)
323 >                error(SYSTEM, "raywait() returned NULL in raysamp()");
324 >        fp = rt->buf + 6*rt->nrays;
325          *fp++ = org[0]; *fp++ = org[1]; *fp++ = org[2];
326          *fp++ = dir[0]; *fp++ = dir[1]; *fp = dir[2];
327 +        rt->dest[rt->nrays++] = res;
328 +        if (rt->nrays == rt->bsiz)
329 +                rayflush(rt, 0);
330   }
331  
332  
333   void
334 < rayflush(                       /* flush buffered rays */
335 <        register struct rtproc  *rt
334 > rayflush(                       /* flush queued rays to rtrace */
335 >        register struct rtproc  *rt,
336 >        int  doall
337   )
338   {
339 <        register int  i;
339 >        int     nw;
340  
341 <        if (rt->nrays <= 0)
342 <                return;
343 <        memset(rt->buf+6*rt->nrays, '\0', 6*sizeof(float));
344 <        errno = 0;
345 <        if ( process(&(rt->pd), (char *)rt->buf, (char *)rt->buf,
346 <                        3*sizeof(float)*(rt->nrays+1),
347 <                        6*sizeof(float)*(rt->nrays+1)) <
348 <                        3*sizeof(float)*(rt->nrays+1) )
349 <                error(SYSTEM, "error reading from rtrace process");
350 <        i = rt->nrays;
351 <        while (i--) {
352 <                rt->dest[i][0] += rt->buf[3*i];
353 <                rt->dest[i][1] += rt->buf[3*i+1];
354 <                rt->dest[i][2] += rt->buf[3*i+2];
341 >        do {
342 >                if (rt->nrays <= 0)
343 >                        continue;
344 >                memset(rt->buf+6*rt->nrays, 0, 6*sizeof(float));
345 >                nw = 6*sizeof(float)*(rt->nrays+1);
346 >                errno = 0;
347 >                if (writebuf(rt->pd.w, (char *)rt->buf, nw) < nw)
348 >                        error(SYSTEM, "error writing to rtrace process");
349 >                rt->nrays = 0;          /* flag buffer as flushed */
350 >        } while (doall && (rt = rt->next) != NULL);
351 > }
352 >
353 >
354 > struct rtproc *
355 > raywait(                        /* retrieve rtrace results */
356 >        struct rtproc *rt0
357 > )
358 > {
359 >        fd_set  readset, errset;
360 >        int     nr;
361 >        struct rtproc   *rtfree;
362 >        register struct rtproc  *rt;
363 >        register int    n;
364 >                                /* prepare select call */
365 >        FD_ZERO(&readset); FD_ZERO(&errset); n = 0;
366 >        nr = 0;
367 >        for (rt = rt0; rt != NULL; rt = rt->next) {
368 >                if (rt->nrays == 0 && rt->dest[0] != NULL) {
369 >                        FD_SET(rt->pd.r, &readset);
370 >                        ++nr;
371 >                }
372 >                FD_SET(rt->pd.r, &errset);
373 >                if (rt->pd.r >= n)
374 >                        n = rt->pd.r + 1;
375          }
376 <        rt->nrays = 0;
376 >        if (!nr)                /* no rays pending */
377 >                return(NULL);
378 >        if (nr > 1) {           /* call select for multiple processes */
379 >                errno = 0;
380 >                if (select(n, &readset, NULL, &errset, NULL) < 0)
381 >                        error(SYSTEM, "select call error in raywait()");
382 >        } else
383 >                FD_ZERO(&errset);
384 >        rtfree = NULL;          /* read from ready process(es) */
385 >        for (rt = rt0; rt != NULL; rt = rt->next) {
386 >                if (!FD_ISSET(rt->pd.r, &readset) &&
387 >                                !FD_ISSET(rt->pd.r, &errset))
388 >                        continue;
389 >                for (n = 0; n < rt->bsiz && rt->dest[n] != NULL; n++)
390 >                        ;
391 >                nr = 3*sizeof(float)*(n+1);
392 >                if (readbuf(rt->pd.r, (char *)rt->buf, nr) < nr)
393 >                        error(USER, "rtrace process died");
394 >                while (n-- > 0) {
395 >                        rt->dest[n][0] += rt->buf[3*n];
396 >                        rt->dest[n][1] += rt->buf[3*n+1];
397 >                        rt->dest[n][2] += rt->buf[3*n+2];
398 >                        rt->dest[n] = NULL;
399 >                }
400 >                rtfree = rt;
401 >        }
402 >        return(rtfree);
403   }
404  
405  
406 < void
406 > static void
407   mkaxes(                 /* compute u and v to go with n */
408          FVECT  u,
409          FVECT  v,
# Line 362 | Line 423 | mkaxes(                        /* compute u and v to go with n */
423   }
424  
425  
426 < void
426 > static void
427   rounddir(               /* compute uniform spherical direction */
428          register FVECT  dv,
429          double  alt,
# Line 379 | Line 440 | rounddir(              /* compute uniform spherical direction */
440   }
441  
442  
443 < void
443 > static void
444   flatdir(                /* compute uniform hemispherical direction */
445          register FVECT  dv,
446          double  alt,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines