Created
May 22, 2018 01:42
-
-
Save eug/5c814e0469a277260dac4dbca2c40316 to your computer and use it in GitHub Desktop.
Converts (interleave) a vector of integer (bitset) coordinates to a Morton code (represented as a bitset).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <boost/dynamic_bitset.hpp> | |
namespace morton { | |
/** | |
Interleaves a vector of bitsets into a unique bitset. | |
@param axes the vector of axis. | |
@return a interleaved bitset. | |
*/ | |
boost::dynamic_bitset<> interleave(const std::vector< boost::dynamic_bitset<> >& axes) | |
{ | |
const std::size_t naxes = axes.size(); | |
const std::size_t nbits = axes[0].size(); | |
const std::size_t size = naxes * nbits; | |
boost::dynamic_bitset<> value(size); | |
for (int k = 0, i = 0; i < nbits; i++) | |
for (int j = naxes - 1; j >= 0; j--, k++) | |
value[k] = axes[j][i]; | |
return value; | |
} | |
/** | |
Deinterleaves a bitset into a vector of bitsets. | |
@param value the bitset to deinterleave. | |
@param naxes the number of axis to deinterleave. | |
@return a vector of bitsets. | |
*/ | |
std::vector< boost::dynamic_bitset<> > deinterleave(const boost::dynamic_bitset<>& value, | |
const unsigned int naxes) | |
{ | |
const std::size_t size = value.size(); | |
const std::size_t nbits = size / naxes; | |
std::vector< boost::dynamic_bitset<> > axes; | |
for (int i = 0; i < naxes; i++) { | |
boost::dynamic_bitset<> axis(nbits); | |
axes.push_back(axis); | |
} | |
for (int k = size - 1, i = nbits - 1; i >= 0; i--) | |
for (int j = 0; j < naxes; j++, k--) | |
axes[j][i] = value[k]; | |
return axes; | |
} | |
} // namespace morton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment