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

Comparing ray/src/gen/mkillum4.c (file contents):
Revision 2.2 by greg, Fri Sep 21 05:53:21 2007 UTC vs.
Revision 2.5 by greg, Wed Dec 5 20:07:34 2007 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7  
8   #include "mkillum.h"
9   #include "paths.h"
10 + #include "random.h"
11   #include "ezxml.h"
12  
13  
# Line 31 | Line 32 | load_BSDF(             /* load BSDF data from file */
32                  error(WARNING, errmsg);
33                  return(NULL);
34          }
35 <        dp = (struct BSDF_data *)malloc(sizeof(struct BSDF_data));
35 >        dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
36          if (dp == NULL)
37                  goto memerr;
38          for (wld = ezxml_child(fl, "WavelengthData");
# Line 60 | Line 61 | free_BSDF(             /* free BSDF data structure */
61   {
62          if (b == NULL)
63                  return;
63        free(b->inc_dir);
64        free(b->inc_rad);
65        free(b->out_dir);
66        free(b->out_rad);
64          free(b->bsdf);
65          free(b);
66   }
# Line 211 | Line 208 | redistribute(          /* pass distarr ray sums through BSDF *
208          MAT4 xm
209   )
210   {
211 <        MAT4    mymat;
212 <        COLORV  *outarr;
216 <        float   *inpcoef;
211 >        MAT4    mymat, inmat;
212 >        COLORV  *idist;
213          COLORV  *cp, *csum;
218        uint16  *distcnt;
214          FVECT   dv;
215 <        double  oom, wt;
216 <        int     i, j, o;
217 <        int     cnt;
218 <        COLOR   col;
219 <                                        /* allocate temporary memory */
220 <        outarr = (COLORV *)malloc(b->nout * sizeof(COLOR));
221 <        distcnt = (uint16 *)calloc(nalt*nazi, sizeof(uint16));
222 <        inpcoef = (float *)malloc(b->ninc * sizeof(float));
228 <        if ((outarr == NULL) | (distcnt == NULL) | (inpcoef == NULL))
215 >        double  wt;
216 >        int     i, j, k, h;
217 >        COLOR   col, cinc;
218 >                                        /* copy incoming distribution */
219 >        if (b->ninc != distsiz)
220 >                error(INTERNAL, "error 1 in redistribute");
221 >        idist = (COLORV *)malloc(sizeof(COLOR)*distsiz);
222 >        if (idist == NULL)
223                  error(SYSTEM, "out of memory in redistribute");
224 <                                        /* compose matrix */
224 >        memcpy(idist, distarr, sizeof(COLOR)*distsiz);
225 >                                        /* compose direction transform */
226          for (i = 3; i--; ) {
227                  mymat[i][0] = u[i];
228                  mymat[i][1] = v[i];
# Line 245 | Line 240 | redistribute(          /* pass distarr ray sums through BSDF *
240                  for (j = 3; j--; )
241                          mymat[j][i] *= wt;
242          }
243 <                                        /* pass through BSDF */
244 <        for (i = b->ninc; i--; ) {              /* get input coefficients */
245 <                getBSDF_incvec(dv, b, i);
243 >        if (!invmat4(inmat, mymat))     /* need inverse as well */
244 >                error(INTERNAL, "cannot invert BSDF transform");
245 >        newdist(nalt*nazi);             /* resample distribution */
246 >        for (i = b->ninc; i--; ) {
247 >                getBSDF_incvec(dv, b, i);       /* compute incident irrad. */
248                  multv3(dv, dv, mymat);
249 +                if (dv[2] < 0.0) dv[2] = -dv[2];
250                  wt = getBSDF_incrad(b, i);
251 <                inpcoef[i] = PI*wt*wt * dv[2];  /* solid_angle*cosine(theta) */
252 <        }
253 <        for (o = b->nout; o--; ) {
254 <                csum = &outarr[3*o];
255 <                setcolor(csum, 0., 0., 0.);
256 <                oom = getBSDF_outrad(b, o);
257 <                oom *= oom * PI;
258 <                for (i = b->ninc; i--; ) {
259 <                        wt = BSDF_data(b,i,o) * inpcoef[i] / oom;
260 <                        cp = &distarr[3*i];
261 <                        copycolor(col, cp);
251 >                wt *= wt*PI * dv[2];            /* solid_angle*cosine(theta) */
252 >                cp = &idist[3*i];
253 >                copycolor(cinc, cp);
254 >                scalecolor(cinc, wt);
255 >                for (k = nalt; k--; )           /* loop over distribution */
256 >                    for (j = nazi; j--; ) {
257 >                        flatdir(dv, (k + .5)/nalt, (double)j/nazi);
258 >                        multv3(dv, dv, inmat);
259 >                                                /* evaluate BSDF @ outgoing */
260 >                        wt = BSDF_visible(b, i, getBSDF_outndx(b, dv));
261 >                        copycolor(col, cinc);
262                          scalecolor(col, wt);
263 <                        addcolor(csum, col);
264 <                }
267 <                wt = 1./b->ninc;
268 <                scalecolor(csum, wt);
269 <        }
270 <        free(inpcoef);
271 <        newdist(nalt*nazi);             /* resample distribution */
272 <        for (o = b->nout; o--; ) {
273 <                getBSDF_outvec(dv, b, o);
274 <                multv3(dv, dv, mymat);
275 <                j = (.5 + atan2(dv[1],dv[0])*(.5/PI))*nazi + .5;
276 <                if (j >= nazi) j = 0;
277 <                i = (0.9999 - dv[2]*dv[2])*nalt;
278 <                csum = &distarr[3*(i*nazi + j)];
279 <                cp = &outarr[3*o];
280 <                addcolor(csum, cp);
281 <                ++distcnt[i*nazi + j];
282 <        }
283 <        free(outarr);
284 <                                        /* fill in missing bits */
285 <        for (i = nalt; i--; )
286 <            for (j = nazi; j--; ) {
287 <                int     ii, jj, alt, azi;
288 <                if (distcnt[i*nazi + j])
289 <                        continue;
290 <                csum = &distarr[3*(i*nazi + j)];
291 <                setcolor(csum, 0., 0., 0.);
292 <                cnt = 0;
293 <                for (o = 0; !cnt; o++)
294 <                    for (ii = -o; ii <= o; ii++) {
295 <                        alt = i + ii;
296 <                        if (alt < 0) continue;
297 <                        if (alt >= nalt) break;
298 <                        for (jj = -o; jj <= o; jj++) {
299 <                            if (ii*ii + jj*jj != o*o)
300 <                                continue;
301 <                            azi = j + jj;
302 <                            if (azi >= nazi) azi -= nazi;
303 <                            else if (azi < 0) azi += nazi;
304 <                            if (!distcnt[alt*nazi + azi])
305 <                                continue;
306 <                            cp = &distarr[3*(alt*nazi + azi)];
307 <                            addcolor(csum, cp);
308 <                            cnt += distcnt[alt*nazi + azi];
309 <                        }
263 >                        csum = &distarr[3*(k*nazi + j)];
264 >                        addcolor(csum, col);    /* sum into distribution */
265                      }
266 <                wt = 1./cnt;
267 <                scalecolor(csum, wt);
313 <            }
314 <                                        /* finish averages */
315 <        for (i = nalt; i--; )
316 <            for (j = nazi; j--; ) {
317 <                if ((cnt = distcnt[i*nazi + j]) <= 1)
318 <                        continue;
319 <                csum = &distarr[3*(i*nazi + j)];
320 <                wt = 1./cnt;
321 <                scalecolor(csum, wt);
322 <            }
323 <        free(distcnt);
266 >        }
267 >        free(idist);                    /* free temp space */
268   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines