Skip to content

Instantly share code, notes, and snippets.

@eug
Created May 22, 2018 01:42
Show Gist options
  • Save eug/5c814e0469a277260dac4dbca2c40316 to your computer and use it in GitHub Desktop.
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).
#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