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.15 by greg, Fri Jun 3 18:12:58 2011 UTC vs.
Revision 3.33 by greg, Fri Mar 21 17:49:53 2014 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 280 | Line 284 | SDdotravTre(const SDNode *st, const double *pos, int c
284                  unsigned        skipmask = 0;
285                  csiz *= .5;
286                  for (i = st->ndim; i--; )
287 <                        if (1<<i & cmask)
287 >                        if (1<<i & cmask) {
288                                  if (pos[i] < cmin[i] + csiz)
289                                          for (n = 1 << st->ndim; n--; ) {
290                                                  if (n & 1<<i)
# Line 291 | Line 295 | SDdotravTre(const SDNode *st, const double *pos, int c
295                                                  if (!(n & 1<<i))
296                                                          skipmask |= 1<<n;
297                                          }
298 +                        }
299                  for (n = 1 << st->ndim; n--; ) {
300                          if (1<<n & skipmask)
301                                  continue;
# Line 299 | Line 304 | SDdotravTre(const SDNode *st, const double *pos, int c
304                                          bmin[i] = cmin[i] + csiz;
305                                  else
306                                          bmin[i] = cmin[i];
302                        for (i = SD_MAXDIM; i-- > st->ndim; )
303                                bmin[i] = .0;
307  
308                          rval += rv = SDdotravTre(st->u.t[n], pos, cmask,
309                                                          cf, cptr, bmin, csiz);
# Line 326 | Line 329 | SDdotravTre(const SDNode *st, const double *pos, int c
329                                  clim[i][0] = 0;
330                                  clim[i][1] = 1 << st->log2GR;
331                          }
329                                        /* fill in unused dimensions */
330                for (i = SD_MAXDIM; i-- > st->ndim; ) {
331                        clim[i][0] = 0; clim[i][1] = 1;
332                }
332   #if (SD_MAXDIM == 4)
333                  bmin[0] = cmin[0] + csiz*clim[0][0];
334                  for (cpos[0] = clim[0][0]; cpos[0] < clim[0][1]; cpos[0]++) {
335                      bmin[1] = cmin[1] + csiz*clim[1][0];
336                      for (cpos[1] = clim[1][0]; cpos[1] < clim[1][1]; cpos[1]++) {
337                          bmin[2] = cmin[2] + csiz*clim[2][0];
338 <                        for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
339 <                            bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
338 >                        if (st->ndim == 3) {
339 >                            cpos[2] = clim[2][0];
340                              n = cpos[0];
341 <                            for (i = 1; i < st->ndim; i++)
341 >                            for (i = 1; i < 3; i++)
342                                  n = (n << st->log2GR) + cpos[i];
343 <                            for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
343 >                            for ( ; cpos[2] < clim[2][1]; cpos[2]++) {
344                                  rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
345                                  if (rv < 0)
346                                      return rv;
347 <                                bmin[3] += csiz;
347 >                                bmin[2] += csiz;
348                              }
349 <                            bmin[2] += csiz;
349 >                        } else {
350 >                            for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
351 >                                bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
352 >                                n = cpos[0];
353 >                                for (i = 1; i < 4; i++)
354 >                                    n = (n << st->log2GR) + cpos[i];
355 >                                for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
356 >                                    rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
357 >                                    if (rv < 0)
358 >                                        return rv;
359 >                                    bmin[3] += csiz;
360 >                                }
361 >                                bmin[2] += csiz;
362 >                            }
363                          }
364                          bmin[1] += csiz;
365                      }
# Line 422 | Line 434 | SDlookupTre(const SDNode *st, const double *pos, doubl
434   static float
435   SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc)
436   {
437 <        FVECT                   rOutVec;
438 <        double                  gridPos[4];
437 >        const RREAL     *vtmp;
438 >        FVECT           rOutVec;
439 >        double          gridPos[4];
440  
441          switch (sdt->sidef) {           /* whose side are you on? */
442 <        case SD_UFRONT:
442 >        case SD_FREFL:
443                  if ((outVec[2] < 0) | (inVec[2] < 0))
444                          return -1.;
445                  break;
446 <        case SD_UBACK:
446 >        case SD_BREFL:
447                  if ((outVec[2] > 0) | (inVec[2] > 0))
448                          return -1.;
449                  break;
450 <        case SD_XMIT:
451 <                if ((outVec[2] > 0) == (inVec[2] > 0))
450 >        case SD_FXMIT:
451 >                if (outVec[2] > 0) {
452 >                        if (inVec[2] > 0)
453 >                                return -1.;
454 >                        vtmp = outVec; outVec = inVec; inVec = vtmp;
455 >                } else if (inVec[2] < 0)
456                          return -1.;
457                  break;
458 +        case SD_BXMIT:
459 +                if (inVec[2] > 0) {
460 +                        if (outVec[2] > 0)
461 +                                return -1.;
462 +                        vtmp = outVec; outVec = inVec; inVec = vtmp;
463 +                } else if (outVec[2] < 0)
464 +                        return -1.;
465 +                break;
466          default:
467                  return -1.;
468          }
469                                          /* convert vector coordinates */
470          if (sdt->st->ndim == 3) {
471                  spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0]));
472 <                gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
472 >                gridPos[0] = (.5-FTINY) -
473 >                                .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
474                  SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]);
475          } else if (sdt->st->ndim == 4) {
476                  SDdisk2square(gridPos, -inVec[0], -inVec[1]);
# Line 475 | Line 501 | build_scaffold(float val, const double *cmin, double c
501   {
502          SDdistScaffold  *sp = (SDdistScaffold *)cptr;
503          int             wid = csiz*(double)iwmax + .5;
504 +        double          revcmin[2];
505          bitmask_t       bmin[2], bmax[2];
506  
507 <        cmin += sp->nic;                /* skip to output coords */
507 >        if (sp->rev) {                  /* need to reverse sense? */
508 >                revcmin[0] = 1. - cmin[0] - csiz;
509 >                revcmin[1] = 1. - cmin[1] - csiz;
510 >                cmin = revcmin;
511 >        } else {
512 >                cmin += sp->nic;        /* else skip to output coords */
513 >        }
514          if (wid < sp->wmin)             /* new minimum width? */
515                  sp->wmin = wid;
516          if (wid > sp->wmax)             /* new maximum? */
# Line 523 | Line 556 | sscmp(const void *p1, const void *p2)
556  
557   /* Create a new cumulative distribution for the given input direction */
558   static SDTreCDst *
559 < make_cdist(const SDTre *sdt, const double *pos)
559 > make_cdist(const SDTre *sdt, const double *invec, int rev)
560   {
561          SDdistScaffold  myScaffold;
562 +        double          pos[4];
563 +        int             cmask;
564          SDTreCDst       *cd;
565          struct outdir_s *sp;
566          double          scale, cursum;
# Line 534 | Line 569 | make_cdist(const SDTre *sdt, const double *pos)
569          myScaffold.wmin = iwmax;
570          myScaffold.wmax = 0;
571          myScaffold.nic = sdt->st->ndim - 2;
572 +        myScaffold.rev = rev;
573          myScaffold.alen = 0;
574          myScaffold.nall = 512;
575          myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
576                                                          myScaffold.nall);
577          if (myScaffold.darr == NULL)
578                  return NULL;
579 +                                        /* set up traversal */
580 +        cmask = (1<<myScaffold.nic) - 1;
581 +        for (i = myScaffold.nic; i--; )
582 +                        pos[i+2*rev] = invec[i];
583 +        cmask <<= 2*rev;
584                                          /* grow the distribution */
585 <        if (SDtraverseTre(sdt->st, pos, (1<<myScaffold.nic)-1,
585 >        if (SDtraverseTre(sdt->st, pos, cmask,
586                                  &build_scaffold, &myScaffold) < 0) {
587                  free(myScaffold.darr);
588                  return NULL;
# Line 564 | Line 605 | make_cdist(const SDTre *sdt, const double *pos)
605                                          /* record input range */
606          scale = myScaffold.wmin / (double)iwmax;
607          for (i = myScaffold.nic; i--; ) {
608 <                cd->clim[i][0] = floor(pos[i]/scale) * scale;
608 >                cd->clim[i][0] = floor(pos[i+2*rev]/scale) * scale;
609                  cd->clim[i][1] = cd->clim[i][0] + scale;
610          }
611          if (cd->isodist) {              /* avoid issue in SDqueryTreProjSA() */
# Line 573 | Line 614 | make_cdist(const SDTre *sdt, const double *pos)
614          }
615          cd->max_psa = myScaffold.wmax / (double)iwmax;
616          cd->max_psa *= cd->max_psa * M_PI;
617 <        cd->sidef = sdt->sidef;
617 >        if (rev)
618 >                cd->sidef = (sdt->sidef==SD_BXMIT) ? SD_FXMIT : SD_BXMIT;
619 >        else
620 >                cd->sidef = sdt->sidef;
621          cd->cTotal = 1e-20;             /* compute directional total */
622          sp = myScaffold.darr;
623          for (i = myScaffold.alen; i--; sp++)
# Line 600 | Line 644 | SDgetTreCDist(const FVECT inVec, SDComponent *sdc)
644   {
645          const SDTre     *sdt;
646          double          inCoord[2];
603        int             vflags;
647          int             i;
648 +        int             mode;
649          SDTreCDst       *cd, *cdlast;
650                                          /* check arguments */
651          if ((inVec == NULL) | (sdc == NULL) ||
652                          (sdt = (SDTre *)sdc->dist) == NULL)
653                  return NULL;
654 <        if (sdt->st->ndim == 3)         /* isotropic BSDF? */
655 <                inCoord[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
656 <        else if (sdt->st->ndim == 4)
657 <                SDdisk2square(inCoord, -inVec[0], -inVec[1]);
658 <        else
654 >        switch (mode = sdt->sidef) {    /* check direction */
655 >        case SD_FREFL:
656 >                if (inVec[2] < 0)
657 >                        return NULL;
658 >                break;
659 >        case SD_BREFL:
660 >                if (inVec[2] > 0)
661 >                        return NULL;
662 >                break;
663 >        case SD_FXMIT:
664 >                if (inVec[2] < 0)
665 >                        mode = SD_BXMIT;
666 >                break;
667 >        case SD_BXMIT:
668 >                if (inVec[2] > 0)
669 >                        mode = SD_FXMIT;
670 >                break;
671 >        default:
672 >                return NULL;
673 >        }
674 >        if (sdt->st->ndim == 3) {       /* isotropic BSDF? */
675 >                if (mode != sdt->sidef) /* XXX unhandled reciprocity */
676 >                        return &SDemptyCD;
677 >                inCoord[0] = (.5-FTINY) -
678 >                                .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
679 >        } else if (sdt->st->ndim == 4) {
680 >                if (mode != sdt->sidef) /* use reciprocity? */
681 >                        SDdisk2square(inCoord, inVec[0], inVec[1]);
682 >                else
683 >                        SDdisk2square(inCoord, -inVec[0], -inVec[1]);
684 >        } else
685                  return NULL;            /* should be internal error */
686 +                                        /* quantize to avoid f.p. errors */
687 +        for (i = sdt->st->ndim - 2; i--; )
688 +                inCoord[i] = floor(inCoord[i]/quantum)*quantum + .5*quantum;
689          cdlast = NULL;                  /* check for direction in cache list */
690          for (cd = (SDTreCDst *)sdc->cdList; cd != NULL;
691 <                                cdlast = cd, cd = (SDTreCDst *)cd->next) {
691 >                                        cdlast = cd, cd = cd->next) {
692 >                if (cd->sidef != mode)
693 >                        continue;
694                  for (i = sdt->st->ndim - 2; i--; )
695                          if ((cd->clim[i][0] > inCoord[i]) |
696                                          (inCoord[i] >= cd->clim[i][1]))
# Line 624 | Line 699 | SDgetTreCDist(const FVECT inVec, SDComponent *sdc)
699                          break;          /* means we have a match */
700          }
701          if (cd == NULL)                 /* need to create new entry? */
702 <                cdlast = cd = make_cdist(sdt, inCoord);
702 >                cdlast = cd = make_cdist(sdt, inCoord, mode != sdt->sidef);
703          if (cdlast != NULL) {           /* move entry to head of cache list */
704                  cdlast->next = cd->next;
705 <                cd->next = sdc->cdList;
705 >                cd->next = (SDTreCDst *)sdc->cdList;
706                  sdc->cdList = (SDCDst *)cd;
707          }
708          return (SDCDst *)cd;            /* ready to go */
# Line 656 | Line 731 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
731          } else {
732                  const SDTreCDst *cd = (const SDTreCDst *)SDgetTreCDist(v1, sdc);
733                  if (cd == NULL)
734 <                        return SDEmemory;
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;
734 >                        myPSA[0] = myPSA[1] = 0;
735 >                else {
736 >                        myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) *
737 >                                        (cd->clim[1][1] - cd->clim[1][0]);
738 >                        myPSA[1] = cd->max_psa;
739 >                }
740          }
741          switch (qflags) {               /* record based on flag settings */
742          case SDqueryVal:
# Line 674 | Line 751 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
751                          psa[1] = myPSA[1];
752                  /* fall through */
753          case SDqueryMin:
754 <                if (myPSA[0] < psa[0])
754 >                if ((myPSA[0] > 0) & (myPSA[0] < psa[0]))
755                          psa[0] = myPSA[0];
756                  break;
757          }
# Line 695 | Line 772 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
772                                          /* check arguments */
773          if ((ioVec == NULL) | (cd == NULL))
774                  return SDEargument;
775 +        if (!cd->sidef)
776 +                return SDEnone;         /* XXX should never happen */
777          if (ioVec[2] > 0) {
778 <                if (!(cd->sidef & SD_UFRONT))
778 >                if ((cd->sidef != SD_FREFL) & (cd->sidef != SD_FXMIT))
779                          return SDEargument;
780 <        } else if (!(cd->sidef & SD_UBACK))
780 >        } else if ((cd->sidef != SD_BREFL) & (cd->sidef != SD_BXMIT))
781                  return SDEargument;
782                                          /* binary search to find position */
783          ilower = 0; iupper = cd->calen;
784          while ((i = (iupper + ilower) >> 1) != ilower)
785 <                if ((long)target >= (long)cd->carr[i].cuml)
785 >                if (target >= cd->carr[i].cuml)
786                          ilower = i;
787                  else
788                          iupper = i;
# Line 721 | Line 800 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
800          SDsquare2disk(gpos, gpos[0], gpos[1]);
801                                          /* compute Z-coordinate */
802          gpos[2] = 1. - gpos[0]*gpos[0] - gpos[1]*gpos[1];
803 <        if (gpos[2] > 0)                /* paranoia, I hope */
725 <                gpos[2] = sqrt(gpos[2]);
803 >        gpos[2] = sqrt(gpos[2]*(gpos[2]>0));
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          if (cd->isodist) {              /* rotate isotropic result */
808                  rotangle = atan2(-ioVec[1],-ioVec[0]);
# Line 771 | Line 849 | load_values(char **spp, float *va, int n)
849          char    *svnext;
850  
851          while (n-- > 0 && (svnext = fskip(*spp)) != NULL) {
852 <                *v++ = atof(*spp);
852 >                if ((*v++ = atof(*spp)) < 0)
853 >                        v[-1] = 0;
854                  *spp = svnext;
855                  eat_token(spp, ',');
856          }
# Line 834 | Line 913 | get_extrema(SDSpectralDF *df)
913          double  stepWidth, dhemi, bmin[4], bmax[4];
914  
915          stepWidth = SDsmallestLeaf(st);
916 +        if (quantum > stepWidth)        /* adjust quantization factor */
917 +                quantum = stepWidth;
918          df->minProjSA = M_PI*stepWidth*stepWidth;
919          if (stepWidth < .03125)
920                  stepWidth = .03125;     /* 1/32 resolution good enough */
# Line 873 | Line 954 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
954          SDSpectralDF    *df;
955          SDTre           *sdt;
956          char            *sdata;
876        int             i;
957                                          /* allocate BSDF component */
958          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
959          if (!sdata)
# Line 881 | Line 961 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
961          /*
962           * Remember that front and back are reversed from WINDOW 6 orientations
963           */
964 <        if (!strcasecmp(sdata, "Transmission")) {
964 >        if (!strcasecmp(sdata, "Transmission Front")) {
965 >                if (sd->tb != NULL)
966 >                        SDfreeSpectralDF(sd->tb);
967 >                if ((sd->tb = SDnewSpectralDF(1)) == NULL)
968 >                        return SDEmemory;
969 >                df = sd->tb;
970 >        } else if (!strcasecmp(sdata, "Transmission Back")) {
971                  if (sd->tf != NULL)
972                          SDfreeSpectralDF(sd->tf);
973                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
974                          return SDEmemory;
975                  df = sd->tf;
976          } else if (!strcasecmp(sdata, "Reflection Front")) {
977 <                if (sd->rb != NULL)     /* note back-front reversal */
977 >                if (sd->rb != NULL)
978                          SDfreeSpectralDF(sd->rb);
979                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
980                          return SDEmemory;
981                  df = sd->rb;
982          } else if (!strcasecmp(sdata, "Reflection Back")) {
983 <                if (sd->rf != NULL)     /* note front-back reversal */
983 >                if (sd->rf != NULL)
984                          SDfreeSpectralDF(sd->rf);
985                  if ((sd->rf = SDnewSpectralDF(1)) == NULL)
986                          return SDEmemory;
# Line 914 | Line 1000 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
1000          if (sdt == NULL)
1001                  return SDEmemory;
1002          if (df == sd->rf)
1003 <                sdt->sidef = SD_UFRONT;
1003 >                sdt->sidef = SD_FREFL;
1004          else if (df == sd->rb)
1005 <                sdt->sidef = SD_UBACK;
1006 <        else
1007 <                sdt->sidef = SD_XMIT;
1005 >                sdt->sidef = SD_BREFL;
1006 >        else if (df == sd->tf)
1007 >                sdt->sidef = SD_FXMIT;
1008 >        else /* df == sd->tb */
1009 >                sdt->sidef = SD_BXMIT;
1010          sdt->st = NULL;
1011          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
1012          df->comp[0].dist = sdt;
# Line 1080 | Line 1168 | SDloadTre(SDData *sd, ezxml_t wtl)
1168                                          /* separate diffuse components */
1169          extract_diffuse(&sd->rLambFront, sd->rf);
1170          extract_diffuse(&sd->rLambBack, sd->rb);
1171 <        extract_diffuse(&sd->tLamb, sd->tf);
1171 >        if (sd->tf != NULL)
1172 >                extract_diffuse(&sd->tLamb, sd->tf);
1173 >        if (sd->tb != NULL)
1174 >                extract_diffuse(&sd->tLamb, sd->tb);
1175                                          /* return success */
1176          return SDEnone;
1177   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines