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.17 by greg, Sun Sep 19 08:42:22 2004 UTC vs.
Revision 2.18 by greg, Thu Sep 13 06:31:21 2007 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"
14  
15  
16   static void mkaxes(FVECT u, FVECT v, FVECT n);
# Line 19 | Line 18 | static void rounddir(FVECT dv, double alt, double azi)
18   static void flatdir(FVECT dv, double alt, double azi);
19  
20  
21 + static COLORV * distarr = NULL;         /* distribution array */
22 + static int      distsiz = 0;
23 +
24 +
25   static void
26 < rayclean(                       /* finish all pending rays */
27 <        struct rtproc *rt0
26 > newdist(                        /* allocate & clear distribution array */
27 >        int siz
28   )
29   {
30 <        rayflush(rt0, 1);
31 <        while (raywait(rt0) != NULL)
30 >        if (siz == 0) {
31 >                if (distsiz > 0)
32 >                        free((void *)distarr);
33 >                distarr = NULL;
34 >                distsiz = 0;
35 >                return;
36 >        }
37 >        if (distsiz < siz) {
38 >                free((void *)distarr);
39 >                distarr = (COLORV *)malloc(sizeof(COLORV)*3*siz);
40 >                if (distarr == NULL)
41 >                        error(SYSTEM, "Out of memory in distalloc");
42 >                distsiz = siz;
43 >        }
44 >        memset(distarr, '\0', sizeof(COLORV)*3*siz);
45 > }
46 >
47 >
48 > static int
49 > process_ray(RAY *r, int rv)
50 > {
51 >        COLORV  *colp;
52 >
53 >        if (rv == 0)
54 >                return(0);
55 >        if (rv < 0)
56 >                error(USER, "Ray tracing process died");
57 >        if (r->rno >= distsiz)
58 >                error(INTERNAL, "Bad returned index in process_ray");
59 >        colp = &distarr[r->rno * 3];
60 >        addcolor(colp, r->rcol);
61 >        return(1);
62 > }
63 >
64 >
65 > static void
66 > raysamp(        /* queue a ray sample */
67 >        int  ndx,
68 >        FVECT  org,
69 >        FVECT  dir
70 > )
71 > {
72 >        RAY     myRay;
73 >        int     rv;
74 >
75 >        if ((ndx < 0) | (ndx >= distsiz))
76 >                error(INTERNAL, "Bad index in raysamp");
77 >        VCOPY(myRay.rorg, org);
78 >        VCOPY(myRay.rdir, dir);
79 >        myRay.rmax = .0;
80 >        rayorigin(&myRay, PRIMARY, NULL, NULL);
81 >        myRay.rno = ndx;
82 >                                        /* queue ray, check result */
83 >        process_ray(&myRay, ray_pqueue(&myRay));
84 > }
85 >
86 >
87 > static void
88 > rayclean()                      /* finish all pending rays */
89 > {
90 >        RAY     myRay;
91 >
92 >        while (process_ray(&myRay, ray_presult(&myRay, 0)))
93                  ;
94   }
95  
96  
97   int /* XXX type conflict with otypes.h */
98 < o_default(      /* default illum action */
98 > my_default(     /* default illum action */
99          OBJREC  *ob,
100          struct illum_args  *il,
37        struct rtproc  *rt0,
101          char  *nm
102   )
103   {
# Line 47 | Line 110 | o_default(     /* default illum action */
110  
111  
112   int
113 < o_face(         /* make an illum face */
113 > my_face(                /* make an illum face */
114          OBJREC  *ob,
115          struct illum_args  *il,
53        struct rtproc  *rt0,
116          char  *nm
117   )
118   {
119   #define MAXMISS         (5*n*il->nsamps)
120          int  dim[3];
121          int  n, nalt, nazi, h;
60        float  *distarr;
122          double  sp[2], r1, r2;
123          FVECT  dn, org, dir;
124          FVECT  u, v;
# Line 69 | Line 130 | o_face(                /* make an illum face */
130          fa = getface(ob);
131          if (fa->area == 0.0) {
132                  freeface(ob);
133 <                return(o_default(ob, il, rt0, nm));
133 >                return(o_default(ob, il, nm));
134          }
135                                  /* set up sampling */
136          if (il->sampdens <= 0)
# Line 80 | Line 141 | o_face(                /* make an illum face */
141                  nazi = PI*nalt + .5;
142          }
143          n = nalt*nazi;
144 <        distarr = (float *)calloc(n, 3*sizeof(float));
84 <        if (distarr == NULL)
85 <                error(SYSTEM, "out of memory in o_face");
144 >        newdist(n);
145                                  /* take first edge longer than sqrt(area) */
146          for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) {
147                  u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0];
# Line 133 | Line 192 | o_face(                /* make an illum face */
192                      } while (!inface(org, fa) && nmisses++ < MAXMISS);
193                      if (nmisses > MAXMISS) {
194                          objerror(ob, WARNING, "bad aspect");
195 <                        rayclean(rt0);
195 >                        rayclean();
196                          freeface(ob);
197                          free((void *)distarr);
198 <                        return(o_default(ob, il, rt0, nm));
198 >                        return(o_default(ob, il, nm));
199                      }
200                      for (j = 0; j < 3; j++)
201                          org[j] += .001*fa->norm[j];
202                                          /* send sample */
203 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
203 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
204                  }
205 <        rayclean(rt0);
205 >        rayclean();
206                                  /* write out the face and its distribution */
207          if (average(il, distarr, nalt*nazi)) {
208                  if (il->sampdens > 0)
# Line 153 | Line 212 | o_face(                /* make an illum face */
212                  printobj(il->altmat, ob);
213                                  /* clean up */
214          freeface(ob);
156        free((void *)distarr);
215          return(0);
216   #undef MAXMISS
217   }
218  
219  
220   int
221 < o_sphere(       /* make an illum sphere */
221 > my_sphere(      /* make an illum sphere */
222          register OBJREC  *ob,
223          struct illum_args  *il,
166        struct rtproc  *rt0,
224          char  *nm
225   )
226   {
227          int  dim[3];
228          int  n, nalt, nazi;
172        float  *distarr;
229          double  sp[4], r1, r2, r3;
230          FVECT  org, dir;
231          FVECT  u, v;
# Line 186 | Line 242 | o_sphere(      /* make an illum sphere */
242                  nazi = PI/2.*nalt + .5;
243          }
244          n = nalt*nazi;
245 <        distarr = (float *)calloc(n, 3*sizeof(float));
190 <        if (distarr == NULL)
191 <                error(SYSTEM, "out of memory in o_sphere");
245 >        newdist(n);
246          dim[0] = random();
247                                  /* sample sphere */
248          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
# Line 213 | Line 267 | o_sphere(      /* make an illum sphere */
267                          dir[j] = -dir[j];
268                      }
269                                          /* send sample */
270 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
270 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
271                  }
272 <        rayclean(rt0);
272 >        rayclean();
273                                  /* write out the sphere and its distribution */
274          if (average(il, distarr, nalt*nazi)) {
275                  if (il->sampdens > 0)
# Line 226 | Line 280 | o_sphere(      /* make an illum sphere */
280          } else
281                  printobj(il->altmat, ob);
282                                  /* clean up */
229        free((void *)distarr);
283          return(1);
284   }
285  
286  
287   int
288 < o_ring(         /* make an illum ring */
288 > my_ring(                /* make an illum ring */
289          OBJREC  *ob,
290          struct illum_args  *il,
238        struct rtproc  *rt0,
291          char  *nm
292   )
293   {
294          int  dim[3];
295          int  n, nalt, nazi;
244        float  *distarr;
296          double  sp[4], r1, r2, r3;
297          FVECT  dn, org, dir;
298          FVECT  u, v;
# Line 258 | Line 309 | o_ring(                /* make an illum ring */
309                  nazi = PI*nalt + .5;
310          }
311          n = nalt*nazi;
312 <        distarr = (float *)calloc(n, 3*sizeof(float));
262 <        if (distarr == NULL)
263 <                error(SYSTEM, "out of memory in o_ring");
312 >        newdist(n);
313          mkaxes(u, v, co->ad);
314          dim[0] = random();
315                                  /* sample disk */
# Line 286 | Line 335 | o_ring(                /* make an illum ring */
335                                          .001*co->ad[j];
336  
337                                          /* send sample */
338 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
338 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
339                  }
340 <        rayclean(rt0);
340 >        rayclean();
341                                  /* write out the ring and its distribution */
342          if (average(il, distarr, nalt*nazi)) {
343                  if (il->sampdens > 0)
# Line 298 | Line 347 | o_ring(                /* make an illum ring */
347                  printobj(il->altmat, ob);
348                                  /* clean up */
349          freecone(ob);
301        free((void *)distarr);
350          return(1);
303 }
304
305
306 void
307 raysamp(        /* queue a ray sample */
308        float  res[3],
309        FVECT  org,
310        FVECT  dir,
311        struct rtproc *rt0
312 )
313 {
314        register struct rtproc  *rt;
315        register float  *fp;
316
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 queued rays to rtrace */
335        register struct rtproc  *rt,
336        int  doall
337 )
338 {
339        int     nw;
340
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        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);
351   }
352  
353  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines