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.11 by greg, Thu Apr 28 17:46:25 2011 UTC vs.
Revision 3.31 by greg, Thu Aug 8 05:26:56 2013 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 25 | Line 26 | typedef int    SDtreCallback(float val, const double *cmi
26  
27                                          /* reference width maximum (1.0) */
28   static const unsigned   iwbits = sizeof(unsigned)*4;
29 < static const unsigned   iwmax = (1<<(sizeof(unsigned)*4))-1;
29 > static const unsigned   iwmax = 1<<(sizeof(unsigned)*4);
30                                          /* maximum cumulative value */
31   static const unsigned   cumlmax = ~0;
32 +                                        /* constant z-vector */
33 + static const FVECT      zvec = {.0, .0, 1.};
34 +                                        /* quantization value */
35 + static double           quantum = 1./256.;
36  
37   /* Struct used for our distribution-building callback */
38   typedef struct {
39 <        int             nic;            /* number of input coordinates */
39 >        short           nic;            /* number of input coordinates */
40 >        short           rev;            /* reversing query */
41          unsigned        alen;           /* current array length */
42          unsigned        nall;           /* number of allocated entries */
43          unsigned        wmin;           /* minimum square size so far */
# Line 61 | Line 67 | SDnewNode(int nd, int lg)
67          if (lg < 0) {
68                  st = (SDNode *)malloc(sizeof(SDNode) +
69                                  sizeof(st->u.t[0])*((1<<nd) - 1));
70 <                if (st != NULL)
65 <                        memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
66 <        } else
67 <                st = (SDNode *)malloc(sizeof(SDNode) +
68 <                                sizeof(st->u.v[0])*((1 << nd*lg) - 1));
69 <                
70 <        if (st == NULL) {
71 <                if (lg < 0)
70 >                if (st == NULL) {
71                          sprintf(SDerrorDetail,
72                                  "Cannot allocate %d branch BSDF tree", 1<<nd);
73 <                else
73 >                        return NULL;
74 >                }
75 >                memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
76 >        } else {
77 >                st = (SDNode *)malloc(sizeof(SDNode) +
78 >                                sizeof(st->u.v[0])*((1 << nd*lg) - 1));        
79 >                if (st == NULL) {
80                          sprintf(SDerrorDetail,
81                                  "Cannot allocate %d BSDF leaves", 1 << nd*lg);
82 <                return NULL;
82 >                        return NULL;
83 >                }
84          }
85          st->ndim = nd;
86          st->log2GR = lg;
# Line 85 | Line 91 | SDnewNode(int nd, int lg)
91   static void
92   SDfreeTre(SDNode *st)
93   {
94 <        int     i;
94 >        int     n;
95  
96          if (st == NULL)
97                  return;
98 <        for (i = (st->log2GR < 0) << st->ndim; i--; )
99 <                SDfreeTre(st->u.t[i]);
100 <        free((void *)st);
98 >        for (n = (st->log2GR < 0) << st->ndim; n--; )
99 >                SDfreeTre(st->u.t[n]);
100 >        free(st);
101   }
102  
103   /* Free a variable-resolution BSDF */
# Line 125 | Line 131 | fill_grid_branch(float *dptr, const float *sptr, int n
131   static float *
132   grid_branch_start(SDNode *st, int n)
133   {
134 <        unsigned        skipsiz = 1 << st->log2GR;
134 >        unsigned        skipsiz = 1 << (st->log2GR - 1);
135          float           *vptr = st->u.v;
136          int             i;
137  
138 <        for (i = 0; i < st->ndim; skipsiz <<= st->log2GR)
139 <                if (1<<i++ & n)
140 <                        vptr += skipsiz >> 1;
138 >        for (i = st->ndim; i--; skipsiz <<= st->log2GR)
139 >                if (1<<i & n)
140 >                        vptr += skipsiz;
141          return vptr;
142   }
143  
# Line 190 | Line 196 | SDiterSum(const float *va, int nd, int shft, const int
196          const unsigned  skipsiz = 1 << --nd*shft;
197          double          sum = .0;
198          int             i;
199 <        
199 >
200 >        va += *imin * skipsiz;
201 >
202          if (skipsiz == 1)
203                  for (i = *imin; i < *imax; i++)
204 <                        sum += va[i];
204 >                        sum += *va++;
205          else
206 <                for (i = *imin; i < *imax; i++)
207 <                        sum += SDiterSum(va + i*skipsiz, nd, shft, imin+1, imax+1);
206 >                for (i = *imin; i < *imax; i++, va += skipsiz)
207 >                        sum += SDiterSum(va, nd, shft, imin+1, imax+1);
208          return sum;
209   }
210  
# Line 204 | Line 212 | SDiterSum(const float *va, int nd, int shft, const int
212   static double
213   SDavgTreBox(const SDNode *st, const double *bmin, const double *bmax)
214   {
207        int             imin[SD_MAXDIM], imax[SD_MAXDIM];
215          unsigned        n;
216          int             i;
217  
# Line 214 | Line 221 | SDavgTreBox(const SDNode *st, const double *bmin, cons
221          for (i = st->ndim; i--; ) {
222                  if (bmin[i] >= 1.)
223                          return .0;
224 <                if (bmax[i] <= .0)
224 >                if (bmax[i] <= 0)
225                          return .0;
226                  if (bmin[i] >= bmax[i])
227                          return .0;
# Line 222 | Line 229 | SDavgTreBox(const SDNode *st, const double *bmin, cons
229          if (st->log2GR < 0) {           /* iterate on subtree */
230                  double          sum = .0, wsum = 1e-20;
231                  double          sbmin[SD_MAXDIM], sbmax[SD_MAXDIM], w;
225
232                  for (n = 1 << st->ndim; n--; ) {
233                          w = 1.;
234                          for (i = st->ndim; i--; ) {
# Line 234 | Line 240 | SDavgTreBox(const SDNode *st, const double *bmin, cons
240                                  }
241                                  if (sbmin[i] < .0) sbmin[i] = .0;
242                                  if (sbmax[i] > 1.) sbmax[i] = 1.;
243 +                                if (sbmin[i] >= sbmax[i]) {
244 +                                        w = .0;
245 +                                        break;
246 +                                }
247                                  w *= sbmax[i] - sbmin[i];
248                          }
249                          if (w > 1e-10) {
# Line 242 | Line 252 | SDavgTreBox(const SDNode *st, const double *bmin, cons
252                          }
253                  }
254                  return sum / wsum;
255 +        } else {                        /* iterate over leaves */
256 +                int             imin[SD_MAXDIM], imax[SD_MAXDIM];
257 +
258 +                n = 1;
259 +                for (i = st->ndim; i--; ) {
260 +                        imin[i] = (bmin[i] <= 0) ? 0 :
261 +                                        (int)((1 << st->log2GR)*bmin[i]);
262 +                        imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) :
263 +                                (int)((1 << st->log2GR)*bmax[i] + .999999);
264 +                        n *= imax[i] - imin[i];
265 +                }
266 +                if (n)
267 +                        return SDiterSum(st->u.v, st->ndim,
268 +                                        st->log2GR, imin, imax) / (double)n;
269          }
270 <        n = 1;                          /* iterate over leaves */
247 <        for (i = st->ndim; i--; ) {
248 <                imin[i] = (bmin[i] <= 0) ? 0
249 <                                : (int)((1 << st->log2GR)*bmin[i]);
250 <                imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR)
251 <                                : (int)((1 << st->log2GR)*bmax[i] + .999999);
252 <                n *= imax[i] - imin[i];
253 <        }
254 <        if (!n)
255 <                return .0;
256 <        
257 <        return SDiterSum(st->u.v, st->ndim, st->log2GR, imin, imax) / (double)n;
270 >        return .0;
271   }
272  
273   /* Recursive call for SDtraverseTre() */
# Line 269 | Line 282 | SDdotravTre(const SDNode *st, const double *pos, int c
282                                          /* in branches? */
283          if (st->log2GR < 0) {
284                  unsigned        skipmask = 0;
272
285                  csiz *= .5;
286                  for (i = st->ndim; i--; )
287                          if (1<<i & cmask)
288                                  if (pos[i] < cmin[i] + csiz)
289 <                                        for (n = 1 << st->ndim; n--; )
289 >                                        for (n = 1 << st->ndim; n--; ) {
290                                                  if (n & 1<<i)
291                                                          skipmask |= 1<<n;
292 +                                        }
293                                  else
294 <                                        for (n = 1 << st->ndim; n--; )
294 >                                        for (n = 1 << st->ndim; n--; ) {
295                                                  if (!(n & 1<<i))
296                                                          skipmask |= 1<<n;
297 +                                        }
298                  for (n = 1 << st->ndim; n--; ) {
299                          if (1<<n & skipmask)
300                                  continue;
# Line 314 | Line 328 | SDdotravTre(const SDNode *st, const double *pos, int c
328                                  clim[i][0] = 0;
329                                  clim[i][1] = 1 << st->log2GR;
330                          }
317                                        /* fill in unused dimensions */
318                for (i = SD_MAXDIM; i-- > st->ndim; ) {
319                        clim[i][0] = 0; clim[i][1] = 1;
320                }
331   #if (SD_MAXDIM == 4)
332                  bmin[0] = cmin[0] + csiz*clim[0][0];
333                  for (cpos[0] = clim[0][0]; cpos[0] < clim[0][1]; cpos[0]++) {
334                      bmin[1] = cmin[1] + csiz*clim[1][0];
335                      for (cpos[1] = clim[1][0]; cpos[1] < clim[1][1]; cpos[1]++) {
336                          bmin[2] = cmin[2] + csiz*clim[2][0];
337 <                        for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
338 <                            bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
337 >                        if (st->ndim == 3) {
338 >                            cpos[2] = clim[2][0];
339                              n = cpos[0];
340 <                            for (i = 1; i < st->ndim; i++)
340 >                            for (i = 1; i < 3; i++)
341                                  n = (n << st->log2GR) + cpos[i];
342 <                            for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
342 >                            for ( ; cpos[2] < clim[2][1]; cpos[2]++) {
343                                  rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
344                                  if (rv < 0)
345                                      return rv;
346 <                                bmin[3] += csiz;
346 >                                bmin[2] += csiz;
347                              }
348 <                            bmin[2] += csiz;
348 >                        } else {
349 >                            for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
350 >                                bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
351 >                                n = cpos[0];
352 >                                for (i = 1; i < 4; i++)
353 >                                    n = (n << st->log2GR) + cpos[i];
354 >                                for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
355 >                                    rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
356 >                                    if (rv < 0)
357 >                                        return rv;
358 >                                    bmin[3] += csiz;
359 >                                }
360 >                                bmin[2] += csiz;
361 >                            }
362                          }
363                          bmin[1] += csiz;
364                      }
# Line 410 | Line 433 | SDlookupTre(const SDNode *st, const double *pos, doubl
433   static float
434   SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc)
435   {
436 <        static const FVECT      zvec = {.0, .0, 1.};
437 <        FVECT                   rOutVec;
438 <        double                  gridPos[4];
436 >        const RREAL     *vtmp;
437 >        FVECT           rOutVec;
438 >        double          gridPos[4];
439  
440          switch (sdt->sidef) {           /* whose side are you on? */
441 <        case SD_UFRONT:
441 >        case SD_FREFL:
442                  if ((outVec[2] < 0) | (inVec[2] < 0))
443                          return -1.;
444                  break;
445 <        case SD_UBACK:
445 >        case SD_BREFL:
446                  if ((outVec[2] > 0) | (inVec[2] > 0))
447                          return -1.;
448                  break;
449 <        case SD_XMIT:
450 <                if ((outVec[2] > 0) == (inVec[2] > 0))
449 >        case SD_FXMIT:
450 >                if (outVec[2] > 0) {
451 >                        if (inVec[2] > 0)
452 >                                return -1.;
453 >                        vtmp = outVec; outVec = inVec; inVec = vtmp;
454 >                } else if (inVec[2] < 0)
455                          return -1.;
456                  break;
457 +        case SD_BXMIT:
458 +                if (inVec[2] > 0) {
459 +                        if (outVec[2] > 0)
460 +                                return -1.;
461 +                        vtmp = outVec; outVec = inVec; inVec = vtmp;
462 +                } else if (outVec[2] < 0)
463 +                        return -1.;
464 +                break;
465          default:
466                  return -1.;
467          }
468                                          /* convert vector coordinates */
469          if (sdt->st->ndim == 3) {
470 <                spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0]));
471 <                gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
470 >                spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0]));
471 >                gridPos[0] = (.5-FTINY) -
472 >                                .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
473                  SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]);
474          } else if (sdt->st->ndim == 4) {
475                  SDdisk2square(gridPos, -inVec[0], -inVec[1]);
# Line 464 | Line 500 | build_scaffold(float val, const double *cmin, double c
500   {
501          SDdistScaffold  *sp = (SDdistScaffold *)cptr;
502          int             wid = csiz*(double)iwmax + .5;
503 +        double          revcmin[2];
504          bitmask_t       bmin[2], bmax[2];
505  
506 <        cmin += sp->nic;                /* skip to output coords */
506 >        if (sp->rev) {                  /* need to reverse sense? */
507 >                revcmin[0] = 1. - cmin[0] - csiz;
508 >                revcmin[1] = 1. - cmin[1] - csiz;
509 >                cmin = revcmin;
510 >        } else {
511 >                cmin += sp->nic;        /* else skip to output coords */
512 >        }
513          if (wid < sp->wmin)             /* new minimum width? */
514                  sp->wmin = wid;
515          if (wid > sp->wmax)             /* new maximum? */
516                  sp->wmax = wid;
517          if (sp->alen >= sp->nall) {     /* need more space? */
518                  struct outdir_s *ndarr;
519 <                sp->nall += 8192;
519 >                sp->nall += 1024;
520                  ndarr = (struct outdir_s *)realloc(sp->darr,
521                                          sizeof(struct outdir_s)*sp->nall);
522 <                if (ndarr == NULL)
522 >                if (ndarr == NULL) {
523 >                        sprintf(SDerrorDetail,
524 >                                "Cannot grow scaffold to %u entries", sp->nall);
525                          return -1;      /* abort build */
526 +                }
527                  sp->darr = ndarr;
528          }
529                                          /* find Hilbert entry index */
# Line 509 | Line 555 | sscmp(const void *p1, const void *p2)
555  
556   /* Create a new cumulative distribution for the given input direction */
557   static SDTreCDst *
558 < make_cdist(const SDTre *sdt, const double *pos)
558 > make_cdist(const SDTre *sdt, const double *invec, int rev)
559   {
560          SDdistScaffold  myScaffold;
561 +        double          pos[4];
562 +        int             cmask;
563          SDTreCDst       *cd;
564          struct outdir_s *sp;
565          double          scale, cursum;
# Line 520 | Line 568 | make_cdist(const SDTre *sdt, const double *pos)
568          myScaffold.wmin = iwmax;
569          myScaffold.wmax = 0;
570          myScaffold.nic = sdt->st->ndim - 2;
571 +        myScaffold.rev = rev;
572          myScaffold.alen = 0;
573 <        myScaffold.nall = 8192;
573 >        myScaffold.nall = 512;
574          myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
575                                                          myScaffold.nall);
576          if (myScaffold.darr == NULL)
577                  return NULL;
578 +                                        /* set up traversal */
579 +        cmask = (1<<myScaffold.nic) - 1;
580 +        for (i = myScaffold.nic; i--; )
581 +                        pos[i+2*rev] = invec[i];
582 +        cmask <<= 2*rev;
583                                          /* grow the distribution */
584 <        if (SDtraverseTre(sdt->st, pos, (1<<myScaffold.nic)-1,
584 >        if (SDtraverseTre(sdt->st, pos, cmask,
585                                  &build_scaffold, &myScaffold) < 0) {
586                  free(myScaffold.darr);
587                  return NULL;
# Line 536 | Line 590 | make_cdist(const SDTre *sdt, const double *pos)
590          cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) +
591                                  sizeof(cd->carr[0])*myScaffold.alen);
592          if (cd == NULL) {
593 +                sprintf(SDerrorDetail,
594 +                        "Cannot allocate %u entry cumulative distribution",
595 +                                myScaffold.alen);
596                  free(myScaffold.darr);
597                  return NULL;
598          }
599 +        cd->isodist = (myScaffold.nic == 1);
600                                          /* sort the distribution */
601          qsort(myScaffold.darr, cd->calen = myScaffold.alen,
602                                  sizeof(struct outdir_s), &sscmp);
# Line 546 | Line 604 | make_cdist(const SDTre *sdt, const double *pos)
604                                          /* record input range */
605          scale = myScaffold.wmin / (double)iwmax;
606          for (i = myScaffold.nic; i--; ) {
607 <                cd->clim[i][0] = floor(pos[i]/scale) * scale;
607 >                cd->clim[i][0] = floor(pos[i+2*rev]/scale) * scale;
608                  cd->clim[i][1] = cd->clim[i][0] + scale;
609          }
610 +        if (cd->isodist) {              /* avoid issue in SDqueryTreProjSA() */
611 +                cd->clim[1][0] = cd->clim[0][0];
612 +                cd->clim[1][1] = cd->clim[0][1];
613 +        }
614          cd->max_psa = myScaffold.wmax / (double)iwmax;
615          cd->max_psa *= cd->max_psa * M_PI;
616 <        cd->sidef = sdt->sidef;
616 >        if (rev)
617 >                cd->sidef = (sdt->sidef==SD_BXMIT) ? SD_FXMIT : SD_BXMIT;
618 >        else
619 >                cd->sidef = sdt->sidef;
620          cd->cTotal = 1e-20;             /* compute directional total */
621          sp = myScaffold.darr;
622          for (i = myScaffold.alen; i--; sp++)
# Line 578 | Line 643 | SDgetTreCDist(const FVECT inVec, SDComponent *sdc)
643   {
644          const SDTre     *sdt;
645          double          inCoord[2];
581        int             vflags;
646          int             i;
647 +        int             mode;
648          SDTreCDst       *cd, *cdlast;
649                                          /* check arguments */
650          if ((inVec == NULL) | (sdc == NULL) ||
651                          (sdt = (SDTre *)sdc->dist) == NULL)
652                  return NULL;
653 <        if (sdt->st->ndim == 3)         /* isotropic BSDF? */
654 <                inCoord[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
655 <        else if (sdt->st->ndim == 4)
656 <                SDdisk2square(inCoord, -inVec[0], -inVec[1]);
657 <        else
653 >        switch (mode = sdt->sidef) {    /* check direction */
654 >        case SD_FREFL:
655 >                if (inVec[2] < 0)
656 >                        return NULL;
657 >                break;
658 >        case SD_BREFL:
659 >                if (inVec[2] > 0)
660 >                        return NULL;
661 >                break;
662 >        case SD_FXMIT:
663 >                if (inVec[2] < 0)
664 >                        mode = SD_BXMIT;
665 >                break;
666 >        case SD_BXMIT:
667 >                if (inVec[2] > 0)
668 >                        mode = SD_FXMIT;
669 >                break;
670 >        default:
671 >                return NULL;
672 >        }
673 >        if (sdt->st->ndim == 3) {       /* isotropic BSDF? */
674 >                if (mode != sdt->sidef) /* XXX unhandled reciprocity */
675 >                        return &SDemptyCD;
676 >                inCoord[0] = (.5-FTINY) -
677 >                                .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
678 >        } else if (sdt->st->ndim == 4) {
679 >                if (mode != sdt->sidef) /* use reciprocity? */
680 >                        SDdisk2square(inCoord, inVec[0], inVec[1]);
681 >                else
682 >                        SDdisk2square(inCoord, -inVec[0], -inVec[1]);
683 >        } else
684                  return NULL;            /* should be internal error */
685 +                                        /* quantize to avoid f.p. errors */
686 +        for (i = sdt->st->ndim - 2; i--; )
687 +                inCoord[i] = floor(inCoord[i]/quantum)*quantum + .5*quantum;
688          cdlast = NULL;                  /* check for direction in cache list */
689          for (cd = (SDTreCDst *)sdc->cdList; cd != NULL;
690 <                                cdlast = cd, cd = (SDTreCDst *)cd->next) {
690 >                                        cdlast = cd, cd = cd->next) {
691 >                if (cd->sidef != mode)
692 >                        continue;
693                  for (i = sdt->st->ndim - 2; i--; )
694                          if ((cd->clim[i][0] > inCoord[i]) |
695                                          (inCoord[i] >= cd->clim[i][1]))
# Line 602 | Line 698 | SDgetTreCDist(const FVECT inVec, SDComponent *sdc)
698                          break;          /* means we have a match */
699          }
700          if (cd == NULL)                 /* need to create new entry? */
701 <                cdlast = cd = make_cdist(sdt, inCoord);
701 >                cdlast = cd = make_cdist(sdt, inCoord, mode != sdt->sidef);
702          if (cdlast != NULL) {           /* move entry to head of cache list */
703                  cdlast->next = cd->next;
704 <                cd->next = sdc->cdList;
704 >                cd->next = (SDTreCDst *)sdc->cdList;
705                  sdc->cdList = (SDCDst *)cd;
706          }
707          return (SDCDst *)cd;            /* ready to go */
# Line 634 | Line 730 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
730          } else {
731                  const SDTreCDst *cd = (const SDTreCDst *)SDgetTreCDist(v1, sdc);
732                  if (cd == NULL)
733 <                        return SDEmemory;
734 <                myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) *
735 <                                (cd->clim[1][1] - cd->clim[1][0]);
736 <                myPSA[1] = cd->max_psa;
733 >                        myPSA[0] = myPSA[1] = 0;
734 >                else {
735 >                        myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) *
736 >                                        (cd->clim[1][1] - cd->clim[1][0]);
737 >                        myPSA[1] = cd->max_psa;
738 >                }
739          }
740          switch (qflags) {               /* record based on flag settings */
741          case SDqueryVal:
# Line 652 | Line 750 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
750                          psa[1] = myPSA[1];
751                  /* fall through */
752          case SDqueryMin:
753 <                if (myPSA[0] < psa[0])
753 >                if ((myPSA[0] > 0) & (myPSA[0] < psa[0]))
754                          psa[0] = myPSA[0];
755                  break;
756          }
# Line 668 | Line 766 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
766          const SDTreCDst *cd = (const SDTreCDst *)cdp;
767          const unsigned  target = randX*cumlmax;
768          bitmask_t       hndx, hcoord[2];
769 <        double          gpos[3];
769 >        double          gpos[3], rotangle;
770          int             i, iupper, ilower;
771                                          /* check arguments */
772          if ((ioVec == NULL) | (cd == NULL))
773                  return SDEargument;
774 +        if (!cd->sidef)
775 +                return SDEnone;         /* XXX should never happen */
776          if (ioVec[2] > 0) {
777 <                if (!(cd->sidef & SD_UFRONT))
777 >                if ((cd->sidef != SD_FREFL) & (cd->sidef != SD_FXMIT))
778                          return SDEargument;
779 <        } else if (!(cd->sidef & SD_UBACK))
779 >        } else if ((cd->sidef != SD_BREFL) & (cd->sidef != SD_BXMIT))
780                  return SDEargument;
781                                          /* binary search to find position */
782          ilower = 0; iupper = cd->calen;
783          while ((i = (iupper + ilower) >> 1) != ilower)
784 <                if ((long)target >= (long)cd->carr[i].cuml)
784 >                if (target >= cd->carr[i].cuml)
785                          ilower = i;
786                  else
787                          iupper = i;
# Line 702 | Line 802 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
802          if (gpos[2] > 0)                /* paranoia, I hope */
803                  gpos[2] = sqrt(gpos[2]);
804                                          /* emit from back? */
805 <        if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT)
805 >        if ((cd->sidef == SD_BREFL) | (cd->sidef == SD_FXMIT))
806                  gpos[2] = -gpos[2];
807 <        VCOPY(ioVec, gpos);
807 >        if (cd->isodist) {              /* rotate isotropic result */
808 >                rotangle = atan2(-ioVec[1],-ioVec[0]);
809 >                VCOPY(ioVec, gpos);
810 >                spinvector(ioVec, ioVec, zvec, rotangle);
811 >        } else
812 >                VCOPY(ioVec, gpos);
813          return SDEnone;
814   }
815  
# Line 717 | Line 822 | next_token(char **spp)
822          return **spp;
823   }
824  
825 < #define eat_token(spp,c)        (next_token(spp)==(c) ? *(*(spp))++ : 0)
825 > /* Advance pointer past matching token (or any token if c==0) */
826 > #define eat_token(spp,c)        (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0)
827  
828   /* Count words from this point in string to '}' */
829   static int
# Line 773 | Line 879 | load_tree_data(char **spp, int nd)
879          } else {                        /* else load value grid */
880                  int     bsiz;
881                  n = count_values(*spp); /* see how big the grid is */
882 <                for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd)
882 >                for (bsiz = 0; bsiz < 8*sizeof(size_t); bsiz += nd)
883                          if (1<<bsiz == n)
884                                  break;
885                  if (bsiz >= 8*sizeof(size_t)) {
# Line 806 | Line 912 | get_extrema(SDSpectralDF *df)
912          double  stepWidth, dhemi, bmin[4], bmax[4];
913  
914          stepWidth = SDsmallestLeaf(st);
915 +        if (quantum > stepWidth)        /* adjust quantization factor */
916 +                quantum = stepWidth;
917          df->minProjSA = M_PI*stepWidth*stepWidth;
918          if (stepWidth < .03125)
919                  stepWidth = .03125;     /* 1/32 resolution good enough */
# Line 845 | Line 953 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
953          SDSpectralDF    *df;
954          SDTre           *sdt;
955          char            *sdata;
848        int             i;
956                                          /* allocate BSDF component */
957          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
958          if (!sdata)
# Line 853 | Line 960 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
960          /*
961           * Remember that front and back are reversed from WINDOW 6 orientations
962           */
963 <        if (!strcasecmp(sdata, "Transmission")) {
963 >        if (!strcasecmp(sdata, "Transmission Front")) {
964 >                if (sd->tb != NULL)
965 >                        SDfreeSpectralDF(sd->tb);
966 >                if ((sd->tb = SDnewSpectralDF(1)) == NULL)
967 >                        return SDEmemory;
968 >                df = sd->tb;
969 >        } else if (!strcasecmp(sdata, "Transmission Back")) {
970                  if (sd->tf != NULL)
971                          SDfreeSpectralDF(sd->tf);
972                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
973                          return SDEmemory;
974                  df = sd->tf;
975          } else if (!strcasecmp(sdata, "Reflection Front")) {
976 <                if (sd->rb != NULL)     /* note back-front reversal */
976 >                if (sd->rb != NULL)
977                          SDfreeSpectralDF(sd->rb);
978                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
979                          return SDEmemory;
980                  df = sd->rb;
981          } else if (!strcasecmp(sdata, "Reflection Back")) {
982 <                if (sd->rf != NULL)     /* note front-back reversal */
982 >                if (sd->rf != NULL)
983                          SDfreeSpectralDF(sd->rf);
984                  if ((sd->rf = SDnewSpectralDF(1)) == NULL)
985                          return SDEmemory;
# Line 886 | Line 999 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
999          if (sdt == NULL)
1000                  return SDEmemory;
1001          if (df == sd->rf)
1002 <                sdt->sidef = SD_UFRONT;
1002 >                sdt->sidef = SD_FREFL;
1003          else if (df == sd->rb)
1004 <                sdt->sidef = SD_UBACK;
1005 <        else
1006 <                sdt->sidef = SD_XMIT;
1004 >                sdt->sidef = SD_BREFL;
1005 >        else if (df == sd->tf)
1006 >                sdt->sidef = SD_FXMIT;
1007 >        else /* df == sd->tb */
1008 >                sdt->sidef = SD_BXMIT;
1009          sdt->st = NULL;
1010          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
1011          df->comp[0].dist = sdt;
# Line 950 | Line 1065 | SDsubtractTreVal(SDNode *st, float val)
1065                          SDsubtractTreVal(st->u.t[n], val);
1066          } else {
1067                  for (n = 1<<(st->ndim*st->log2GR); n--; )
1068 <                        st->u.v[n] -= val;
1068 >                        if ((st->u.v[n] -= val) < 0)
1069 >                                st->u.v[n] = .0f;
1070          }
1071   }
1072  
# Line 964 | Line 1080 | subtract_min(SDNode *st)
1080                  int     n;
1081                  vmin = 1./M_PI;
1082                  if (st->log2GR < 0) {
1083 <                        for (n = 0; n < 4; n++) {
1083 >                        for (n = 0; n < 8; n += 2) {
1084                                  float   v = SDgetTreMin(st->u.t[n]);
1085                                  if (v < vmin)
1086                                          vmin = v;
# Line 1051 | Line 1167 | SDloadTre(SDData *sd, ezxml_t wtl)
1167                                          /* separate diffuse components */
1168          extract_diffuse(&sd->rLambFront, sd->rf);
1169          extract_diffuse(&sd->rLambBack, sd->rb);
1170 <        extract_diffuse(&sd->tLamb, sd->tf);
1170 >        if (sd->tf != NULL)
1171 >                extract_diffuse(&sd->tLamb, sd->tf);
1172 >        if (sd->tb != NULL)
1173 >                extract_diffuse(&sd->tLamb, sd->tb);
1174                                          /* return success */
1175          return SDEnone;
1176   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines