Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:34

0001 // Copyright (c) 2012 Leonhard Gruenschloss (leonhard@gruenschloss.org)
0002 //
0003 // Permission is hereby granted, free of charge, to any person obtaining a copy
0004 // of this software and associated documentation files (the "Software"), to deal
0005 // in the Software without restriction, including without limitation the rights to
0006 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
0007 // of the Software, and to permit persons to whom the Software is furnished to do
0008 // so, subject to the following conditions:
0009 //
0010 // The above copyright notice and this permission notice shall be included in
0011 // all copies or substantial portions of the Software.
0012 //
0013 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0014 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0015 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0016 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0017 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0018 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0019 // SOFTWARE.
0020 
0021 #ifndef _OpenGl_HaltonSampler_H
0022 #define _OpenGl_HaltonSampler_H
0023 
0024 #include <vector>
0025 
0026 //! Compute points of the Halton sequence with digit-permutations for different bases.
0027 class OpenGl_HaltonSampler
0028 {
0029 public:
0030 
0031   //! Return the number of supported dimensions.
0032   static unsigned get_num_dimensions() { return 3u; }
0033 
0034 public:
0035 
0036   //! Init the permutation arrays using Faure-permutations.
0037   OpenGl_HaltonSampler()
0038   {
0039     initFaure();
0040   }
0041 
0042   //! Return the Halton sample for the given dimension (component) and index.
0043   //! The client must have called initFaure() at least once before.
0044   //! dimension must be smaller than the value returned by get_num_dimensions().
0045   float sample (unsigned theDimension, unsigned theIndex) const
0046   {
0047     switch (theDimension)
0048     {
0049       case 0: return halton2 (theIndex);
0050       case 1: return halton3 (theIndex);
0051       case 2: return halton5 (theIndex);
0052     }
0053     return 0.0f;
0054   }
0055 
0056 private:
0057 
0058   //! Init the permutation arrays using Faure-permutations.
0059   void initFaure();
0060 
0061   static unsigned short invert (unsigned short theBase, unsigned short theDigits,
0062                                 unsigned short theIndex, const std::vector<unsigned short>& thePerm)
0063   {
0064     unsigned short aResult = 0;
0065     for (unsigned short i = 0; i < theDigits; ++i)
0066     {
0067       aResult = aResult * theBase + thePerm[theIndex % theBase];
0068       theIndex /= theBase;
0069     }
0070     return aResult;
0071   }
0072 
0073   void initTables (const std::vector<std::vector<unsigned short> >& thePerm)
0074   {
0075     for (unsigned short i = 0; i < 243; ++i)
0076     {
0077       myPerm3[i] = invert (3, 5, i, thePerm[3]);
0078     }
0079     for (unsigned short i = 0; i < 125; ++i)
0080     {
0081       myPerm5[i] = invert (5, 3, i, thePerm[5]);
0082     }
0083   }
0084 
0085   //! Special case: radical inverse in base 2, with direct bit reversal.
0086   float halton2 (unsigned theIndex) const
0087   {
0088     theIndex = (theIndex << 16) | (theIndex >> 16);
0089     theIndex = ((theIndex & 0x00ff00ff) << 8) | ((theIndex & 0xff00ff00) >> 8);
0090     theIndex = ((theIndex & 0x0f0f0f0f) << 4) | ((theIndex & 0xf0f0f0f0) >> 4);
0091     theIndex = ((theIndex & 0x33333333) << 2) | ((theIndex & 0xcccccccc) >> 2);
0092     theIndex = ((theIndex & 0x55555555) << 1) | ((theIndex & 0xaaaaaaaa) >> 1);
0093     union Result { unsigned u; float f; } aResult; // Write reversed bits directly into floating-point mantissa.
0094     aResult.u = 0x3f800000u | (theIndex >> 9);
0095     return aResult.f - 1.0f;
0096   }
0097 
0098   float halton3 (unsigned theIndex) const
0099   {
0100     return (myPerm3[theIndex % 243u] * 14348907u
0101           + myPerm3[(theIndex / 243u)      % 243u] * 59049u
0102           + myPerm3[(theIndex / 59049u)    % 243u] * 243u
0103           + myPerm3[(theIndex / 14348907u) % 243u]) * float(0.999999999999999 / 3486784401u); // Results in [0,1).
0104   }
0105 
0106   float halton5 (unsigned theIndex) const
0107   {
0108     return (myPerm5[theIndex % 125u] * 1953125u
0109           + myPerm5[(theIndex / 125u)     % 125u] * 15625u
0110           + myPerm5[(theIndex / 15625u)   % 125u] * 125u
0111           + myPerm5[(theIndex / 1953125u) % 125u]) * float(0.999999999999999 / 244140625u); // Results in [0,1).
0112   }
0113 
0114 private:
0115 
0116   unsigned short myPerm3[243];
0117   unsigned short myPerm5[125];
0118 
0119 };
0120 
0121 inline void OpenGl_HaltonSampler::initFaure()
0122 {
0123   const unsigned THE_MAX_BASE = 5u;
0124   std::vector<std::vector<unsigned short> > aPerms(THE_MAX_BASE + 1);
0125   for (unsigned k = 1; k <= 3; ++k) // Keep identity permutations for base 1, 2, 3.
0126   {
0127     aPerms[k].resize(k);
0128     for (unsigned i = 0; i < k; ++i)
0129     {
0130       aPerms[k][i] = static_cast<unsigned short> (i);
0131     }
0132   }
0133 
0134   for (unsigned aBase = 4; aBase <= THE_MAX_BASE; ++aBase)
0135   {
0136     aPerms[aBase].resize(aBase);
0137     const unsigned b = aBase / 2;
0138     if (aBase & 1) // odd
0139     {
0140       for (unsigned i = 0; i < aBase - 1; ++i)
0141       {
0142         aPerms[aBase][i + (i >= b)] = aPerms[aBase - 1][i] + (aPerms[aBase - 1][i] >= b);
0143       }
0144       aPerms[aBase][b] = static_cast<unsigned short> (b);
0145     }
0146     else // even
0147     {
0148       for (unsigned i = 0; i < b; ++i)
0149       {
0150         aPerms[aBase][i]     = 2 * aPerms[b][i];
0151         aPerms[aBase][b + i] = 2 * aPerms[b][i] + 1;
0152       }
0153     }
0154   }
0155   initTables (aPerms);
0156 }
0157 
0158 #endif // _OpenGl_HaltonSampler_H