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

Comparing ray/src/common/bsdf_t.c (file contents):
Revision 3.13 by greg, Wed Jun 1 00:29:40 2011 UTC vs.
Revision 3.19 by greg, Thu Jul 7 15:25:09 2011 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   *
11   */
12  
13 + #define _USE_MATH_DEFINES
14   #include "rtio.h"
15   #include <stdlib.h>
16   #include <math.h>
# Line 28 | Line 29 | static const unsigned  iwbits = sizeof(unsigned)*4;
29   static const unsigned   iwmax = (1<<(sizeof(unsigned)*4))-1;
30                                          /* maximum cumulative value */
31   static const unsigned   cumlmax = ~0;
32 +                                        /* constant z-vector */
33 + static const FVECT      zvec = {.0, .0, 1.};
34  
35   /* Struct used for our distribution-building callback */
36   typedef struct {
# Line 91 | Line 94 | SDfreeTre(SDNode *st)
94                  return;
95          for (n = (st->log2GR < 0) << st->ndim; n--; )
96                  SDfreeTre(st->u.t[n]);
97 <        free((void *)st);
97 >        free(st);
98   }
99  
100   /* Free a variable-resolution BSDF */
# Line 125 | Line 128 | fill_grid_branch(float *dptr, const float *sptr, int n
128   static float *
129   grid_branch_start(SDNode *st, int n)
130   {
131 <        unsigned        skipsiz = 1 << st->log2GR;
131 >        unsigned        skipsiz = 1 << (st->log2GR - 1);
132          float           *vptr = st->u.v;
133          int             i;
134  
135 <        for (i = 0; i < st->ndim; skipsiz <<= st->log2GR)
136 <                if (1<<i++ & n)
137 <                        vptr += skipsiz >> 1;
135 >        for (i = st->ndim; i--; skipsiz <<= st->log2GR)
136 >                if (1<<i & n)
137 >                        vptr += skipsiz;
138          return vptr;
139   }
140  
# Line 190 | Line 193 | SDiterSum(const float *va, int nd, int shft, const int
193          const unsigned  skipsiz = 1 << --nd*shft;
194          double          sum = .0;
195          int             i;
196 <        
196 >
197 >        va += *imin * skipsiz;
198 >
199          if (skipsiz == 1)
200                  for (i = *imin; i < *imax; i++)
201 <                        sum += va[i];
201 >                        sum += *va++;
202          else
203 <                for (i = *imin; i < *imax; i++)
204 <                        sum += SDiterSum(va + i*skipsiz, nd, shft, imin+1, imax+1);
203 >                for (i = *imin; i < *imax; i++, va += skipsiz)
204 >                        sum += SDiterSum(va, nd, shft, imin+1, imax+1);
205          return sum;
206   }
207  
# Line 204 | Line 209 | SDiterSum(const float *va, int nd, int shft, const int
209   static double
210   SDavgTreBox(const SDNode *st, const double *bmin, const double *bmax)
211   {
207        int             imin[SD_MAXDIM], imax[SD_MAXDIM];
212          unsigned        n;
213          int             i;
214  
# Line 222 | Line 226 | SDavgTreBox(const SDNode *st, const double *bmin, cons
226          if (st->log2GR < 0) {           /* iterate on subtree */
227                  double          sum = .0, wsum = 1e-20;
228                  double          sbmin[SD_MAXDIM], sbmax[SD_MAXDIM], w;
225
229                  for (n = 1 << st->ndim; n--; ) {
230                          w = 1.;
231                          for (i = st->ndim; i--; ) {
# Line 246 | Line 249 | SDavgTreBox(const SDNode *st, const double *bmin, cons
249                          }
250                  }
251                  return sum / wsum;
252 +        } else {                        /* iterate over leaves */
253 +                int             imin[SD_MAXDIM], imax[SD_MAXDIM];
254 +
255 +                n = 1;
256 +                for (i = st->ndim; i--; ) {
257 +                        imin[i] = (bmin[i] <= 0) ? 0 :
258 +                                        (int)((1 << st->log2GR)*bmin[i]);
259 +                        imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) :
260 +                                (int)((1 << st->log2GR)*bmax[i] + .999999);
261 +                        n *= imax[i] - imin[i];
262 +                }
263 +                if (n)
264 +                        return SDiterSum(st->u.v, st->ndim,
265 +                                        st->log2GR, imin, imax) / (double)n;
266          }
267 <        n = 1;                          /* iterate over leaves */
251 <        for (i = st->ndim; i--; ) {
252 <                imin[i] = (bmin[i] <= 0) ? 0
253 <                                : (int)((1 << st->log2GR)*bmin[i]);
254 <                imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR)
255 <                                : (int)((1 << st->log2GR)*bmax[i] + .999999);
256 <                n *= imax[i] - imin[i];
257 <        }
258 <        if (!n)
259 <                return .0;
260 <        
261 <        return SDiterSum(st->u.v, st->ndim, st->log2GR, imin, imax) / (double)n;
267 >        return .0;
268   }
269  
270   /* Recursive call for SDtraverseTre() */
# Line 319 | Line 325 | SDdotravTre(const SDNode *st, const double *pos, int c
325                                  clim[i][0] = 0;
326                                  clim[i][1] = 1 << st->log2GR;
327                          }
322                                        /* fill in unused dimensions */
323                for (i = SD_MAXDIM; i-- > st->ndim; ) {
324                        clim[i][0] = 0; clim[i][1] = 1;
325                }
328   #if (SD_MAXDIM == 4)
329                  bmin[0] = cmin[0] + csiz*clim[0][0];
330                  for (cpos[0] = clim[0][0]; cpos[0] < clim[0][1]; cpos[0]++) {
331                      bmin[1] = cmin[1] + csiz*clim[1][0];
332                      for (cpos[1] = clim[1][0]; cpos[1] < clim[1][1]; cpos[1]++) {
333                          bmin[2] = cmin[2] + csiz*clim[2][0];
334 <                        for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
335 <                            bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
334 >                        if (st->ndim == 3) {
335 >                            cpos[2] = clim[2][0];
336                              n = cpos[0];
337 <                            for (i = 1; i < st->ndim; i++)
337 >                            for (i = 1; i < 3; i++)
338                                  n = (n << st->log2GR) + cpos[i];
339 <                            for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
339 >                            for ( ; cpos[2] < clim[2][1]; cpos[2]++) {
340                                  rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
341                                  if (rv < 0)
342                                      return rv;
343 <                                bmin[3] += csiz;
343 >                                bmin[2] += csiz;
344                              }
345 <                            bmin[2] += csiz;
345 >                        } else {
346 >                            for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
347 >                                bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
348 >                                n = cpos[0];
349 >                                for (i = 1; i < 4; i++)
350 >                                    n = (n << st->log2GR) + cpos[i];
351 >                                for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
352 >                                    rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
353 >                                    if (rv < 0)
354 >                                        return rv;
355 >                                    bmin[3] += csiz;
356 >                                }
357 >                                bmin[2] += csiz;
358 >                            }
359                          }
360                          bmin[1] += csiz;
361                      }
# Line 415 | Line 430 | SDlookupTre(const SDNode *st, const double *pos, doubl
430   static float
431   SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc)
432   {
418        static const FVECT      zvec = {.0, .0, 1.};
433          FVECT                   rOutVec;
434          double                  gridPos[4];
435  
# Line 437 | Line 451 | SDqueryTre(const SDTre *sdt, const FVECT outVec, const
451          }
452                                          /* convert vector coordinates */
453          if (sdt->st->ndim == 3) {
454 <                spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0]));
454 >                spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0]));
455                  gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
456                  SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]);
457          } else if (sdt->st->ndim == 4) {
# Line 550 | Line 564 | make_cdist(const SDTre *sdt, const double *pos)
564                  free(myScaffold.darr);
565                  return NULL;
566          }
567 +        cd->isodist = (myScaffold.nic == 1);
568                                          /* sort the distribution */
569          qsort(myScaffold.darr, cd->calen = myScaffold.alen,
570                                  sizeof(struct outdir_s), &sscmp);
# Line 560 | Line 575 | make_cdist(const SDTre *sdt, const double *pos)
575                  cd->clim[i][0] = floor(pos[i]/scale) * scale;
576                  cd->clim[i][1] = cd->clim[i][0] + scale;
577          }
578 +        if (cd->isodist) {              /* avoid issue in SDqueryTreProjSA() */
579 +                cd->clim[1][0] = cd->clim[0][0];
580 +                cd->clim[1][1] = cd->clim[0][1];
581 +        }
582          cd->max_psa = myScaffold.wmax / (double)iwmax;
583          cd->max_psa *= cd->max_psa * M_PI;
584          cd->sidef = sdt->sidef;
# Line 589 | Line 608 | SDgetTreCDist(const FVECT inVec, SDComponent *sdc)
608   {
609          const SDTre     *sdt;
610          double          inCoord[2];
592        int             vflags;
611          int             i;
612          SDTreCDst       *cd, *cdlast;
613                                          /* check arguments */
# Line 679 | Line 697 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
697          const SDTreCDst *cd = (const SDTreCDst *)cdp;
698          const unsigned  target = randX*cumlmax;
699          bitmask_t       hndx, hcoord[2];
700 <        double          gpos[3];
700 >        double          gpos[3], rotangle;
701          int             i, iupper, ilower;
702                                          /* check arguments */
703          if ((ioVec == NULL) | (cd == NULL))
# Line 692 | Line 710 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
710                                          /* binary search to find position */
711          ilower = 0; iupper = cd->calen;
712          while ((i = (iupper + ilower) >> 1) != ilower)
713 <                if ((long)target >= (long)cd->carr[i].cuml)
713 >                if (target >= cd->carr[i].cuml)
714                          ilower = i;
715                  else
716                          iupper = i;
# Line 715 | Line 733 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
733                                          /* emit from back? */
734          if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT)
735                  gpos[2] = -gpos[2];
736 <        VCOPY(ioVec, gpos);
736 >        if (cd->isodist) {              /* rotate isotropic result */
737 >                rotangle = atan2(-ioVec[1],-ioVec[0]);
738 >                VCOPY(ioVec, gpos);
739 >                spinvector(ioVec, ioVec, zvec, rotangle);
740 >        } else
741 >                VCOPY(ioVec, gpos);
742          return SDEnone;
743   }
744  
# Line 785 | Line 808 | load_tree_data(char **spp, int nd)
808          } else {                        /* else load value grid */
809                  int     bsiz;
810                  n = count_values(*spp); /* see how big the grid is */
811 <                for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd)
811 >                for (bsiz = 0; bsiz < 8*sizeof(size_t); bsiz += nd)
812                          if (1<<bsiz == n)
813                                  break;
814                  if (bsiz >= 8*sizeof(size_t)) {
# Line 857 | Line 880 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
880          SDSpectralDF    *df;
881          SDTre           *sdt;
882          char            *sdata;
860        int             i;
883                                          /* allocate BSDF component */
884          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
885          if (!sdata)
# Line 962 | Line 984 | SDsubtractTreVal(SDNode *st, float val)
984                          SDsubtractTreVal(st->u.t[n], val);
985          } else {
986                  for (n = 1<<(st->ndim*st->log2GR); n--; )
987 <                        st->u.v[n] -= val;
987 >                        if ((st->u.v[n] -= val) < 0)
988 >                                st->u.v[n] = .0f;
989          }
990   }
991  
# Line 976 | Line 999 | subtract_min(SDNode *st)
999                  int     n;
1000                  vmin = 1./M_PI;
1001                  if (st->log2GR < 0) {
1002 <                        for (n = 0; n < 4; n++) {
1002 >                        for (n = 0; n < 8; n += 2) {
1003                                  float   v = SDgetTreMin(st->u.t[n]);
1004                                  if (v < vmin)
1005                                          vmin = v;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines