Skip to content

Instantly share code, notes, and snippets.

@aniongithub
Created July 28, 2021 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniongithub/1588e1018464606c1218133f689571f9 to your computer and use it in GitHub Desktop.
Save aniongithub/1588e1018464606c1218133f689571f9 to your computer and use it in GitHub Desktop.
Recipe by Romain Dartigues to encode in a single number two or three numbers
# Adapted from recipes by Sean Eron Anderson and Fabian “ryg” Giesen
# all credits goes to the respective authors.
# http://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN
# http://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
# Encoding DEM using Morton coding: https://www.klokantech.com/labs/dem-color-encoding/
# Mirror of code from: https://code.activestate.com/recipes/577558/
def part1by1(n):
n&= 0x0000ffff
n = (n | (n << 8)) & 0x00FF00FF
n = (n | (n << 4)) & 0x0F0F0F0F
n = (n | (n << 2)) & 0x33333333
n = (n | (n << 1)) & 0x55555555
return n
def unpart1by1(n):
n&= 0x55555555
n = (n ^ (n >> 1)) & 0x33333333
n = (n ^ (n >> 2)) & 0x0f0f0f0f
n = (n ^ (n >> 4)) & 0x00ff00ff
n = (n ^ (n >> 8)) & 0x0000ffff
return n
def interleave2(x, y):
return part1by1(x) | (part1by1(y) << 1)
def deinterleave2(n):
return unpart1by1(n), unpart1by1(n >> 1)
def part1by2(n):
n&= 0x000003ff
n = (n ^ (n << 16)) & 0xff0000ff
n = (n ^ (n << 8)) & 0x0300f00f
n = (n ^ (n << 4)) & 0x030c30c3
n = (n ^ (n << 2)) & 0x09249249
return n
def unpart1by2(n):
n&= 0x09249249
n = (n ^ (n >> 2)) & 0x030c30c3
n = (n ^ (n >> 4)) & 0x0300f00f
n = (n ^ (n >> 8)) & 0xff0000ff
n = (n ^ (n >> 16)) & 0x000003ff
return n
def interleave3(x, y, z):
return part1by2(x) | (part1by2(y) << 1) | (part1by2(z) << 2)
def deinterleave3(n):
return unpart1by2(n), unpart1by2(n >> 1), unpart1by2(n >> 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment