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.21 by greg, Fri Sep 21 05:53: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);
17 < static void rounddir(FVECT dv, double alt, double azi);
19 < static void flatdir(FVECT dv, double alt, double azi);
16 > COLORV *        distarr = NULL;         /* distribution array */
17 > int             distsiz = 0;
18  
19  
20 < static void
21 < rayclean(                       /* finish all pending rays */
22 <        struct rtproc *rt0
20 > void
21 > newdist(                        /* allocate & clear distribution array */
22 >        int siz
23   )
24   {
25 <        rayflush(rt0, 1);
26 <        while (raywait(rt0) != NULL)
25 >        if (siz == 0) {
26 >                if (distsiz > 0)
27 >                        free((void *)distarr);
28 >                distarr = NULL;
29 >                distsiz = 0;
30 >                return;
31 >        }
32 >        if (distsiz < siz) {
33 >                if (distsiz > 0)
34 >                        free((void *)distarr);
35 >                distarr = (COLORV *)malloc(sizeof(COLOR)*siz);
36 >                if (distarr == NULL)
37 >                        error(SYSTEM, "out of memory in newdist");
38 >                distsiz = siz;
39 >        }
40 >        memset(distarr, '\0', sizeof(COLOR)*siz);
41 > }
42 >
43 >
44 > int
45 > process_ray(RAY *r, int rv)
46 > {
47 >        COLORV  *colp;
48 >
49 >        if (rv == 0)
50 >                return(0);
51 >        if (rv < 0)
52 >                error(USER, "ray tracing process died");
53 >        if (r->rno >= distsiz)
54 >                error(INTERNAL, "bad returned index in process_ray");
55 >        colp = &distarr[r->rno * 3];
56 >        addcolor(colp, r->rcol);
57 >        return(1);
58 > }
59 >
60 >
61 > void
62 > raysamp(        /* queue a ray sample */
63 >        int  ndx,
64 >        FVECT  org,
65 >        FVECT  dir
66 > )
67 > {
68 >        RAY     myRay;
69 >        int     rv;
70 >
71 >        if ((ndx < 0) | (ndx >= distsiz))
72 >                error(INTERNAL, "bad index in raysamp");
73 >        VCOPY(myRay.rorg, org);
74 >        VCOPY(myRay.rdir, dir);
75 >        myRay.rmax = .0;
76 >        rayorigin(&myRay, PRIMARY, NULL, NULL);
77 >        myRay.rno = ndx;
78 >                                        /* queue ray, check result */
79 >        process_ray(&myRay, ray_pqueue(&myRay));
80 > }
81 >
82 >
83 > void
84 > rayclean()                      /* finish all pending rays */
85 > {
86 >        RAY     myRay;
87 >
88 >        while (process_ray(&myRay, ray_presult(&myRay, 0)))
89                  ;
90   }
91  
92  
93 < int /* XXX type conflict with otypes.h */
94 < o_default(      /* default illum action */
93 > static void
94 > mkaxes(                 /* compute u and v to go with n */
95 >        FVECT  u,
96 >        FVECT  v,
97 >        FVECT  n
98 > )
99 > {
100 >        register int  i;
101 >
102 >        v[0] = v[1] = v[2] = 0.0;
103 >        for (i = 0; i < 3; i++)
104 >                if (n[i] < 0.6 && n[i] > -0.6)
105 >                        break;
106 >        v[i] = 1.0;
107 >        fcross(u, v, n);
108 >        normalize(u);
109 >        fcross(v, n, u);
110 > }
111 >
112 >
113 > static void
114 > rounddir(               /* compute uniform spherical direction */
115 >        register FVECT  dv,
116 >        double  alt,
117 >        double  azi
118 > )
119 > {
120 >        double  d1, d2;
121 >
122 >        dv[2] = 1. - 2.*alt;
123 >        d1 = sqrt(1. - dv[2]*dv[2]);
124 >        d2 = 2.*PI * azi;
125 >        dv[0] = d1*cos(d2);
126 >        dv[1] = d1*sin(d2);
127 > }
128 >
129 >
130 > static void
131 > flatdir(                /* compute uniform hemispherical direction */
132 >        register FVECT  dv,
133 >        double  alt,
134 >        double  azi
135 > )
136 > {
137 >        double  d1, d2;
138 >
139 >        d1 = sqrt(alt);
140 >        d2 = 2.*PI * azi;
141 >        dv[0] = d1*cos(d2);
142 >        dv[1] = d1*sin(d2);
143 >        dv[2] = sqrt(1. - alt);
144 > }
145 >
146 >
147 > int
148 > my_default(     /* default illum action */
149          OBJREC  *ob,
150          struct illum_args  *il,
37        struct rtproc  *rt0,
151          char  *nm
152   )
153   {
# Line 47 | Line 160 | o_default(     /* default illum action */
160  
161  
162   int
163 < o_face(         /* make an illum face */
163 > my_face(                /* make an illum face */
164          OBJREC  *ob,
165          struct illum_args  *il,
53        struct rtproc  *rt0,
166          char  *nm
167   )
168   {
169   #define MAXMISS         (5*n*il->nsamps)
170 <        int  dim[3];
171 <        int  n, nalt, nazi, h;
60 <        float  *distarr;
170 >        int  dim[2];
171 >        int  n, nalt, nazi, h, alti;
172          double  sp[2], r1, r2;
173          FVECT  dn, org, dir;
174          FVECT  u, v;
175          double  ur[2], vr[2];
176 +        MAT4  xfm;
177          int  nmisses;
178 <        register FACE  *fa;
178 >        FACE  *fa;
179          register int  i, j;
180                                  /* get/check arguments */
181          fa = getface(ob);
182          if (fa->area == 0.0) {
183                  freeface(ob);
184 <                return(o_default(ob, il, rt0, nm));
184 >                return(my_default(ob, il, nm));
185          }
186                                  /* set up sampling */
187 <        if (il->sampdens <= 0)
188 <                nalt = nazi = 1;
189 <        else {
187 >        if (il->sampdens <= 0) {
188 >                nalt = nazi = 1;        /* diffuse assumption */
189 >        } else {
190                  n = PI * il->sampdens;
191                  nalt = sqrt(n/PI) + .5;
192                  nazi = PI*nalt + .5;
193          }
194 <        n = nalt*nazi;
195 <        distarr = (float *)calloc(n, 3*sizeof(float));
196 <        if (distarr == NULL)
197 <                error(SYSTEM, "out of memory in o_face");
198 <                                /* take first edge longer than sqrt(area) */
194 >        if (il->sd != NULL) {
195 >                if (!getBSDF_xfm(xfm, fa->norm, il->udir)) {
196 >                        objerror(ob, WARNING, "illegal up direction");
197 >                        freeface(ob);
198 >                        return(my_default(ob, il, nm));
199 >                }
200 >                n = il->sd->ninc;
201 >        } else
202 >                n = nazi*nalt;
203 >        newdist(n);
204 >                                /* take first edge >= sqrt(area) */
205          for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) {
206                  u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0];
207                  u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1];
# Line 111 | Line 229 | o_face(                /* make an illum face */
229          dim[0] = random();
230                                  /* sample polygon */
231          nmisses = 0;
232 <        for (dim[1] = 0; dim[1] < nalt; dim[1]++)
115 <            for (dim[2] = 0; dim[2] < nazi; dim[2]++)
232 >        for (dim[1] = 0; dim[1] < n; dim[1]++)
233                  for (i = 0; i < il->nsamps; i++) {
234                                          /* random direction */
235 <                    h = ilhash(dim, 3) + i;
236 <                    multisamp(sp, 2, urand(h));
237 <                    r1 = (dim[1] + sp[0])/nalt;
238 <                    r2 = (dim[2] + sp[1] - .5)/nazi;
239 <                    flatdir(dn, r1, r2);
240 <                    for (j = 0; j < 3; j++)
241 <                        dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j];
235 >                    h = ilhash(dim, 2) + i;
236 >                    if (il->sd != NULL) {
237 >                        r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm);
238 >                    } else {
239 >                        multisamp(sp, 2, urand(h));
240 >                        alti = dim[1]/nazi;
241 >                        r1 = (alti + sp[0])/nalt;
242 >                        r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi;
243 >                        flatdir(dn, r1, r2);
244 >                        for (j = 0; j < 3; j++)
245 >                            dir[j] = -dn[0]*u[j] - dn[1]*v[j] -
246 >                                                dn[2]*fa->norm[j];
247 >                    }
248                                          /* random location */
249                      do {
250                          multisamp(sp, 2, urand(h+4862+nmisses));
# Line 133 | Line 256 | o_face(                /* make an illum face */
256                      } while (!inface(org, fa) && nmisses++ < MAXMISS);
257                      if (nmisses > MAXMISS) {
258                          objerror(ob, WARNING, "bad aspect");
259 <                        rayclean(rt0);
259 >                        rayclean();
260                          freeface(ob);
261 <                        free((void *)distarr);
139 <                        return(o_default(ob, il, rt0, nm));
261 >                        return(my_default(ob, il, nm));
262                      }
263 +                    if (il->sd != NULL && DOT(dir, fa->norm) < -FTINY)
264 +                        r1 = -1.0001*il->thick - .0001;
265 +                    else
266 +                        r1 = .0001;
267                      for (j = 0; j < 3; j++)
268 <                        org[j] += .001*fa->norm[j];
268 >                        org[j] += r1*fa->norm[j];
269                                          /* send sample */
270 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
270 >                    raysamp(dim[1], org, dir);
271                  }
272 <        rayclean(rt0);
272 >        rayclean();
273 >        if (il->sd != NULL)     /* run distribution through BSDF */
274 >                redistribute(il->sd, nalt, nazi, u, v, fa->norm, xfm);
275                                  /* write out the face and its distribution */
276 <        if (average(il, distarr, nalt*nazi)) {
276 >        if (average(il, distarr, n)) {
277                  if (il->sampdens > 0)
278                          flatout(il, distarr, nalt, nazi, u, v, fa->norm);
279                  illumout(il, ob);
# Line 153 | Line 281 | o_face(                /* make an illum face */
281                  printobj(il->altmat, ob);
282                                  /* clean up */
283          freeface(ob);
156        free((void *)distarr);
284          return(0);
285   #undef MAXMISS
286   }
287  
288  
289   int
290 < o_sphere(       /* make an illum sphere */
290 > my_sphere(      /* make an illum sphere */
291          register OBJREC  *ob,
292          struct illum_args  *il,
166        struct rtproc  *rt0,
293          char  *nm
294   )
295   {
296          int  dim[3];
297          int  n, nalt, nazi;
172        float  *distarr;
298          double  sp[4], r1, r2, r3;
299          FVECT  org, dir;
300          FVECT  u, v;
# Line 185 | Line 310 | o_sphere(      /* make an illum sphere */
310                  nalt = sqrt(2./PI*n) + .5;
311                  nazi = PI/2.*nalt + .5;
312          }
313 +        if (il->sd != NULL)
314 +                objerror(ob, WARNING, "BSDF ignored");
315          n = nalt*nazi;
316 <        distarr = (float *)calloc(n, 3*sizeof(float));
190 <        if (distarr == NULL)
191 <                error(SYSTEM, "out of memory in o_sphere");
316 >        newdist(n);
317          dim[0] = random();
318                                  /* sample sphere */
319          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
# Line 213 | Line 338 | o_sphere(      /* make an illum sphere */
338                          dir[j] = -dir[j];
339                      }
340                                          /* send sample */
341 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
341 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
342                  }
343 <        rayclean(rt0);
343 >        rayclean();
344                                  /* write out the sphere and its distribution */
345 <        if (average(il, distarr, nalt*nazi)) {
345 >        if (average(il, distarr, n)) {
346                  if (il->sampdens > 0)
347                          roundout(il, distarr, nalt, nazi);
348                  else
# Line 226 | Line 351 | o_sphere(      /* make an illum sphere */
351          } else
352                  printobj(il->altmat, ob);
353                                  /* clean up */
229        free((void *)distarr);
354          return(1);
355   }
356  
357  
358   int
359 < o_ring(         /* make an illum ring */
359 > my_ring(                /* make an illum ring */
360          OBJREC  *ob,
361          struct illum_args  *il,
238        struct rtproc  *rt0,
362          char  *nm
363   )
364   {
365 <        int  dim[3];
366 <        int  n, nalt, nazi;
367 <        float  *distarr;
368 <        double  sp[4], r1, r2, r3;
365 >        int  dim[2];
366 >        int  n, nalt, nazi, alti;
367 >        double  sp[2], r1, r2, r3;
368 >        int  h;
369          FVECT  dn, org, dir;
370          FVECT  u, v;
371 <        register CONE  *co;
371 >        MAT4  xfm;
372 >        CONE  *co;
373          register int  i, j;
374                                  /* get/check arguments */
375          co = getcone(ob, 0);
# Line 257 | Line 381 | o_ring(                /* make an illum ring */
381                  nalt = sqrt(n/PI) + .5;
382                  nazi = PI*nalt + .5;
383          }
384 <        n = nalt*nazi;
385 <        distarr = (float *)calloc(n, 3*sizeof(float));
386 <        if (distarr == NULL)
387 <                error(SYSTEM, "out of memory in o_ring");
384 >        if (il->sd != NULL) {
385 >                if (!getBSDF_xfm(xfm, co->ad, il->udir)) {
386 >                        objerror(ob, WARNING, "illegal up direction");
387 >                        freecone(ob);
388 >                        return(my_default(ob, il, nm));
389 >                }
390 >                n = il->sd->ninc;
391 >        } else
392 >                n = nazi*nalt;
393 >        newdist(n);
394          mkaxes(u, v, co->ad);
395          dim[0] = random();
396                                  /* sample disk */
397 <        for (dim[1] = 0; dim[1] < nalt; dim[1]++)
268 <            for (dim[2] = 0; dim[2] < nazi; dim[2]++)
397 >        for (dim[1] = 0; dim[1] < n; dim[1]++)
398                  for (i = 0; i < il->nsamps; i++) {
399                                          /* next sample point */
400 <                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
400 >                    h = ilhash(dim,2) + i;
401                                          /* random direction */
402 <                    r1 = (dim[1] + sp[0])/nalt;
403 <                    r2 = (dim[2] + sp[1] - .5)/nazi;
404 <                    flatdir(dn, r1, r2);
405 <                    for (j = 0; j < 3; j++)
402 >                    if (il->sd != NULL) {
403 >                        r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm);
404 >                    } else {
405 >                        multisamp(sp, 2, urand(h));
406 >                        alti = dim[1]/nazi;
407 >                        r1 = (alti + sp[0])/nalt;
408 >                        r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi;
409 >                        flatdir(dn, r1, r2);
410 >                        for (j = 0; j < 3; j++)
411                          dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j];
412 +                    }
413                                          /* random location */
414 +                    multisamp(sp, 2, urand(h+8371));
415                      r3 = sqrt(CO_R0(co)*CO_R0(co) +
416 <                            sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
417 <                    r2 = 2.*PI*sp[3];
416 >                            sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
417 >                    r2 = 2.*PI*sp[1];
418                      r1 = r3*cos(r2);
419                      r2 = r3*sin(r2);
420 +                    if (il->sd != NULL && DOT(dir, co->ad) < -FTINY)
421 +                        r3 = -1.0001*il->thick - .0001;
422 +                    else
423 +                        r3 = .0001;
424                      for (j = 0; j < 3; j++)
425                          org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] +
426 <                                        .001*co->ad[j];
287 <
426 >                                                r3*co->ad[j];
427                                          /* send sample */
428 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt0);
428 >                    raysamp(dim[1], org, dir);
429                  }
430 <        rayclean(rt0);
430 >        rayclean();
431 >        if (il->sd != NULL)     /* run distribution through BSDF */
432 >                redistribute(il->sd, nalt, nazi, u, v, co->ad, xfm);
433                                  /* write out the ring and its distribution */
434 <        if (average(il, distarr, nalt*nazi)) {
434 >        if (average(il, distarr, n)) {
435                  if (il->sampdens > 0)
436                          flatout(il, distarr, nalt, nazi, u, v, co->ad);
437                  illumout(il, ob);
# Line 298 | Line 439 | o_ring(                /* make an illum ring */
439                  printobj(il->altmat, ob);
440                                  /* clean up */
441          freecone(ob);
301        free((void *)distarr);
442          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);
403 }
404
405
406 static void
407 mkaxes(                 /* compute u and v to go with n */
408        FVECT  u,
409        FVECT  v,
410        FVECT  n
411 )
412 {
413        register int  i;
414
415        v[0] = v[1] = v[2] = 0.0;
416        for (i = 0; i < 3; i++)
417                if (n[i] < 0.6 && n[i] > -0.6)
418                        break;
419        v[i] = 1.0;
420        fcross(u, v, n);
421        normalize(u);
422        fcross(v, n, u);
423 }
424
425
426 static void
427 rounddir(               /* compute uniform spherical direction */
428        register FVECT  dv,
429        double  alt,
430        double  azi
431 )
432 {
433        double  d1, d2;
434
435        dv[2] = 1. - 2.*alt;
436        d1 = sqrt(1. - dv[2]*dv[2]);
437        d2 = 2.*PI * azi;
438        dv[0] = d1*cos(d2);
439        dv[1] = d1*sin(d2);
440 }
441
442
443 static void
444 flatdir(                /* compute uniform hemispherical direction */
445        register FVECT  dv,
446        double  alt,
447        double  azi
448 )
449 {
450        double  d1, d2;
451
452        d1 = sqrt(alt);
453        d2 = 2.*PI * azi;
454        dv[0] = d1*cos(d2);
455        dv[1] = d1*sin(d2);
456        dv[2] = sqrt(1. - alt);
443   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines