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.20 by greg, Tue Sep 18 19:51:07 2007 UTC

# Line 13 | Line 13 | static const char      RCSid[] = "$Id$";
13   #include  "random.h"
14  
15  
16 < int o_default(FUN_ARGLIST);
17 < int o_face(FUN_ARGLIST);
18 < 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);
16 > static void mkaxes(FVECT u, FVECT v, FVECT n);
17 > static void rounddir(FVECT dv, double alt, double azi);
18 > static void flatdir(FVECT dv, double alt, double azi);
19  
20  
21 < int /* XXX type conflict with otypes.h */
22 < o_default(      /* default illum action */
21 > static COLORV * distarr = NULL;         /* distribution array */
22 > static int      distsiz = 0;
23 >
24 >
25 > static void
26 > newdist(                        /* allocate & clear distribution array */
27 >        int siz
28 > )
29 > {
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 newdist");
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
98 > my_default(     /* default illum action */
99          OBJREC  *ob,
100          struct illum_args  *il,
31        struct rtproc  *rt,
101          char  *nm
102   )
103   {
# Line 41 | 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,
47        struct rtproc  *rt,
116          char  *nm
117   )
118   {
119   #define MAXMISS         (5*n*il->nsamps)
120          int  dim[3];
121          int  n, nalt, nazi, h;
54        float  *distarr;
122          double  sp[2], r1, r2;
123          FVECT  dn, org, dir;
124          FVECT  u, v;
# Line 63 | 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, rt, nm));
133 >                return(my_default(ob, il, nm));
134          }
135                                  /* set up sampling */
136          if (il->sampdens <= 0)
# Line 74 | 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));
145 <        if (distarr == NULL)
79 <                error(SYSTEM, "out of memory in o_face");
80 <                                /* take first edge longer than sqrt(area) */
144 >        newdist(n);
145 >                                /* take first edge >= 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];
148                  u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1];
# Line 127 | 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 <                        rt->nrays = 0;
195 >                        rayclean();
196                          freeface(ob);
197                          free((void *)distarr);
198 <                        return(o_default(ob, il, rt, nm));
198 >                        return(my_default(ob, il, nm));
199                      }
200                      for (j = 0; j < 3; j++)
201 <                        org[j] += .001*fa->norm[j];
201 >                        org[j] += .0001*fa->norm[j];
202                                          /* send sample */
203 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
203 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
204                  }
205 <        rayflush(rt);
205 >        rayclean();
206                                  /* write out the face and its distribution */
207          if (average(il, distarr, nalt*nazi)) {
208                  if (il->sampdens > 0)
# Line 147 | Line 212 | o_face(                /* make an illum face */
212                  printobj(il->altmat, ob);
213                                  /* clean up */
214          freeface(ob);
215 <        free((void *)distarr);
215 >        return(0);
216   #undef MAXMISS
152        /* XXX we need to return something here. what is it? */
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,
160        struct rtproc  *rt,
224          char  *nm
225   )
226   {
227          int  dim[3];
228          int  n, nalt, nazi;
166        float  *distarr;
229          double  sp[4], r1, r2, r3;
230          FVECT  org, dir;
231          FVECT  u, v;
# Line 180 | 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));
184 <        if (distarr == NULL)
185 <                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 207 | 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, rt);
270 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
271                  }
272 <        rayflush(rt);
272 >        rayclean();
273                                  /* write out the sphere and its distribution */
274          if (average(il, distarr, nalt*nazi)) {
275                  if (il->sampdens > 0)
# Line 220 | Line 280 | o_sphere(      /* make an illum sphere */
280          } else
281                  printobj(il->altmat, ob);
282                                  /* clean up */
223        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,
232        struct rtproc  *rt,
291          char  *nm
292   )
293   {
294          int  dim[3];
295          int  n, nalt, nazi;
238        float  *distarr;
296          double  sp[4], r1, r2, r3;
297          FVECT  dn, org, dir;
298          FVECT  u, v;
# Line 252 | 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));
256 <        if (distarr == NULL)
257 <                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 277 | Line 332 | o_ring(                /* make an illum ring */
332                      r2 = r3*sin(r2);
333                      for (j = 0; j < 3; j++)
334                          org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] +
335 <                                        .001*co->ad[j];
281 <
335 >                                                .0001*co->ad[j];
336                                          /* send sample */
337 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
337 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
338                  }
339 <        rayflush(rt);
339 >        rayclean();
340                                  /* write out the ring and its distribution */
341          if (average(il, distarr, nalt*nazi)) {
342                  if (il->sampdens > 0)
# Line 292 | Line 346 | o_ring(                /* make an illum ring */
346                  printobj(il->altmat, ob);
347                                  /* clean up */
348          freecone(ob);
295        free((void *)distarr);
349          return(1);
350   }
351  
352  
353 < void
301 < raysamp(        /* compute a ray sample */
302 <        float  res[3],
303 <        FVECT  org,
304 <        FVECT  dir,
305 <        register struct rtproc  *rt
306 < )
307 < {
308 <        register float  *fp;
309 <
310 <        if (rt->nrays == rt->bsiz)
311 <                rayflush(rt);
312 <        rt->dest[rt->nrays] = res;
313 <        fp = rt->buf + 6*rt->nrays++;
314 <        *fp++ = org[0]; *fp++ = org[1]; *fp++ = org[2];
315 <        *fp++ = dir[0]; *fp++ = dir[1]; *fp = dir[2];
316 < }
317 <
318 <
319 < void
320 < rayflush(                       /* flush buffered rays */
321 <        register struct rtproc  *rt
322 < )
323 < {
324 <        register int  i;
325 <
326 <        if (rt->nrays <= 0)
327 <                return;
328 <        memset(rt->buf+6*rt->nrays, '\0', 6*sizeof(float));
329 <        errno = 0;
330 <        if ( process(&(rt->pd), (char *)rt->buf, (char *)rt->buf,
331 <                        3*sizeof(float)*(rt->nrays+1),
332 <                        6*sizeof(float)*(rt->nrays+1)) <
333 <                        3*sizeof(float)*(rt->nrays+1) )
334 <                error(SYSTEM, "error reading from rtrace process");
335 <        i = rt->nrays;
336 <        while (i--) {
337 <                rt->dest[i][0] += rt->buf[3*i];
338 <                rt->dest[i][1] += rt->buf[3*i+1];
339 <                rt->dest[i][2] += rt->buf[3*i+2];
340 <        }
341 <        rt->nrays = 0;
342 < }
343 <
344 <
345 < void
353 > static void
354   mkaxes(                 /* compute u and v to go with n */
355          FVECT  u,
356          FVECT  v,
# Line 362 | Line 370 | mkaxes(                        /* compute u and v to go with n */
370   }
371  
372  
373 < void
373 > static void
374   rounddir(               /* compute uniform spherical direction */
375          register FVECT  dv,
376          double  alt,
# Line 379 | Line 387 | rounddir(              /* compute uniform spherical direction */
387   }
388  
389  
390 < void
390 > static void
391   flatdir(                /* compute uniform hemispherical direction */
392          register FVECT  dv,
393          double  alt,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines