Skip to content

Instantly share code, notes, and snippets.

@paniq
Created March 10, 2015 20:49
Embed
What would you like to do?
morton2d3d_brick.py
# encode a 32x32x16 brick coordinate as a 128x128 coordinate, with
# the z value sorted in morton order
def part2to4(x):
x = x & 3 # not strictly necessary if value is clean
return (x ^ (x << 1)) & 5
def compact4to2(x):
x = x & 5
return (x ^ (x >> 1)) & 3
def xy2z(x,y):
return (part2to4(y)<<1) | part2to4(x)
def z2xy(z):
return compact4to2(z), compact4to2(z>>1)
def xyz2xy(x,y,z):
u,v = z2xy(z)
return (x<<2)|u, (y<<2)|v
def xy2xyz(x,y):
z = xy2z(x,y)
return x>>2, y>>2, z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment