Skip to content

Instantly share code, notes, and snippets.

@jovo
Last active December 31, 2015 14:29
Show Gist options
  • Save jovo/8000269 to your computer and use it in GitHub Desktop.
Save jovo/8000269 to your computer and use it in GitHub Desktop.
code to find XYZ coordinates from morton code
function [x,y,z] = mortonXYZ(morton)
% translated from openconnectome
xmask = 1;
ymask = 2;
zmask = 4;
x=0;y=0;z=0;
% 21 triads of 3 bits each
for i = 0:20 %range(21):
x = x +bitshift( bitand(xmask, morton), i);
y = y +bitshift( bitshift(bitand(ymask, morton), i), -1);
z = z +bitshift( bitshift(bitand(zmask, morton), i), -2);
morton = bitshift(morton, -3);
end
% original code:
% cdef int xmask = 0x001
% cdef int ymask = 0x002
% cdef int zmask = 0x004
%
% cdef int xp = 0x00
% cdef int yp = 0x00
% cdef int zp = 0x00
%
% cdef int i
%
% # 21 triads of 3 bits each
% for i in range(21):
%
% xp += (xmask & morton) << i
% yp += ((ymask & morton) << i ) >> 1
% zp += ((zmask & morton) << i) >> 2
% morton >>= 3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment