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.20 by greg, Tue Sep 18 19:51:07 2007 UTC vs.
Revision 2.21 by greg, Fri Sep 21 05:53:21 2007 UTC

# Line 13 | Line 13 | static const char      RCSid[] = "$Id$";
13   #include  "random.h"
14  
15  
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);
16 > COLORV *        distarr = NULL;         /* distribution array */
17 > int             distsiz = 0;
18  
19  
20 < static COLORV * distarr = NULL;         /* distribution array */
22 < static int      distsiz = 0;
23 <
24 <
25 < static void
20 > void
21   newdist(                        /* allocate & clear distribution array */
22          int siz
23   )
# Line 35 | Line 30 | newdist(                       /* allocate & clear distribution array */
30                  return;
31          }
32          if (distsiz < siz) {
33 <                free((void *)distarr);
34 <                distarr = (COLORV *)malloc(sizeof(COLORV)*3*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(COLORV)*3*siz);
40 >        memset(distarr, '\0', sizeof(COLOR)*siz);
41   }
42  
43  
44 < static int
44 > int
45   process_ray(RAY *r, int rv)
46   {
47          COLORV  *colp;
# Line 62 | Line 58 | process_ray(RAY *r, int rv)
58   }
59  
60  
61 < static void
61 > void
62   raysamp(        /* queue a ray sample */
63          int  ndx,
64          FVECT  org,
# Line 84 | Line 80 | raysamp(       /* queue a ray sample */
80   }
81  
82  
83 < static void
83 > void
84   rayclean()                      /* finish all pending rays */
85   {
86          RAY     myRay;
# Line 94 | Line 90 | rayclean()                     /* finish all pending rays */
90   }
91  
92  
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,
# Line 117 | Line 167 | my_face(               /* make an illum face */
167   )
168   {
169   #define MAXMISS         (5*n*il->nsamps)
170 <        int  dim[3];
171 <        int  n, nalt, nazi, h;
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);
# Line 133 | Line 184 | my_face(               /* make an illum face */
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;
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++) {
# Line 170 | Line 229 | my_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]++)
174 <            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 194 | Line 258 | my_face(               /* make an illum face */
258                          objerror(ob, WARNING, "bad aspect");
259                          rayclean();
260                          freeface(ob);
197                        free((void *)distarr);
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] += .0001*fa->norm[j];
268 >                        org[j] += r1*fa->norm[j];
269                                          /* send sample */
270 <                    raysamp(dim[1]*nazi+dim[2], org, dir);
270 >                    raysamp(dim[1], org, dir);
271                  }
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 241 | Line 310 | my_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          newdist(n);
317          dim[0] = random();
# Line 271 | Line 342 | my_sphere(     /* make an illum sphere */
342                  }
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 291 | Line 362 | my_ring(               /* make an illum ring */
362          char  *nm
363   )
364   {
365 <        int  dim[3];
366 <        int  n, nalt, nazi;
367 <        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 308 | Line 381 | my_ring(               /* make an illum ring */
381                  nalt = sqrt(n/PI) + .5;
382                  nazi = PI*nalt + .5;
383          }
384 <        n = nalt*nazi;
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]++)
317 <            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 <                                                .0001*co->ad[j];
426 >                                                r3*co->ad[j];
427                                          /* send sample */
428 <                    raysamp(dim[1]*nazi+dim[2], org, dir);
428 >                    raysamp(dim[1], org, dir);
429                  }
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 347 | Line 440 | my_ring(               /* make an illum ring */
440                                  /* clean up */
441          freecone(ob);
442          return(1);
350 }
351
352
353 static void
354 mkaxes(                 /* compute u and v to go with n */
355        FVECT  u,
356        FVECT  v,
357        FVECT  n
358 )
359 {
360        register int  i;
361
362        v[0] = v[1] = v[2] = 0.0;
363        for (i = 0; i < 3; i++)
364                if (n[i] < 0.6 && n[i] > -0.6)
365                        break;
366        v[i] = 1.0;
367        fcross(u, v, n);
368        normalize(u);
369        fcross(v, n, u);
370 }
371
372
373 static void
374 rounddir(               /* compute uniform spherical direction */
375        register FVECT  dv,
376        double  alt,
377        double  azi
378 )
379 {
380        double  d1, d2;
381
382        dv[2] = 1. - 2.*alt;
383        d1 = sqrt(1. - dv[2]*dv[2]);
384        d2 = 2.*PI * azi;
385        dv[0] = d1*cos(d2);
386        dv[1] = d1*sin(d2);
387 }
388
389
390 static void
391 flatdir(                /* compute uniform hemispherical direction */
392        register FVECT  dv,
393        double  alt,
394        double  azi
395 )
396 {
397        double  d1, d2;
398
399        d1 = sqrt(alt);
400        d2 = 2.*PI * azi;
401        dv[0] = d1*cos(d2);
402        dv[1] = d1*sin(d2);
403        dv[2] = sqrt(1. - alt);
443   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines