C++ header-only library with methods to efficiently encode/decode Morton codes in/from 2D/3D coordinates
C++ Objective-C C Other

README.md

Libmorton

license

Usage

Just include libmorton/morton.h. This will always have functions that point to the most efficient way to encode/decode Morton codes. If you want to test out alternative (and possibly slower) methods, you can find them in libmorton/morton2D.h and libmorton/morton3D.h.

// ENCODING 2D / 3D morton codes, of length 32 and 64 bits
inline uint_fast32_t morton2D_32_encode(const uint_fast16_t x, const uint_fast16_t y);
inline uint_fast64_t morton2D_64_encode(const uint_fast32_t x, const uint_fast32_t y);
inline uint_fast32_t morton3D_32_encode(const uint_fast16_t x, const uint_fast16_t y, const uint_fast16_t z);
inline uint_fast64_t morton3D_64_encode(const uint_fast32_t x, const uint_fast32_t y, const uint_fast32_t z);
// DECODING 2D / 3D morton codes, of length 32 and 64 bits
inline void morton2D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y);
inline void morton2D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y);
inline void morton3D_32_decode(const uint_fast32_t morton, uint_fast16_t& x, uint_fast16_t& y, uint_fast16_t& z);
inline void morton3D_64_decode(const uint_fast64_t morton, uint_fast32_t& x, uint_fast32_t& y, uint_fast32_t& z);

If you want to take advantage of the BMI2 instruction set (only available on Intel Haswell processors and newer), make sure __BMI2__ is defined before you include morton.h.

Testing

The test folder contains tools I use to test correctness and performance of the libmorton implementation. This section is under heavy re-writing, but might contain some useful code for advanced usage.

Thanks

  • To @gnzlbg and his Rust implementation bitwise for finding bugs in the Magicbits code

TODO

  • Write better test suite (with L1/L2 trashing, better tests, ...)
  • BMI2 implementation for 2D methods
  • A better naming system for the functions, because m3D_e_sLUT_shifted? That escalated quickly.