#include <stdio.h> | |
#include <stdlib.h> | |
typedef unsigned int uint32; | |
// "Insert" a 0 bit after each of the 16 low bits of x | |
uint32 Part1By1(uint32 x) | |
{ | |
x &= 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210 | |
x = (x ^ (x << 8)) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210 | |
x = (x ^ (x << 4)) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210 | |
x = (x ^ (x << 2)) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10 | |
x = (x ^ (x << 1)) & 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0 | |
return x; | |
} | |
// "Insert" two 0 bits after each of the 10 low bits of x | |
uint32 Part1By2(uint32 x) | |
{ | |
x &= 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210 | |
x = (x ^ (x << 16)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210 | |
x = (x ^ (x << 8)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210 | |
x = (x ^ (x << 4)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10 | |
x = (x ^ (x << 2)) & 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0 | |
return x; | |
} | |
// Inverse of Part1By1 - "delete" all odd-indexed bits | |
uint32 Compact1By1(uint32 x) | |
{ | |
x &= 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0 | |
x = (x ^ (x >> 1)) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10 | |
x = (x ^ (x >> 2)) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210 | |
x = (x ^ (x >> 4)) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210 | |
x = (x ^ (x >> 8)) & 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210 | |
return x; | |
} | |
// Inverse of Part1By2 - "delete" all bits not at positions divisible by 3 | |
uint32 Compact1By2(uint32 x) | |
{ | |
x &= 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0 | |
x = (x ^ (x >> 2)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10 | |
x = (x ^ (x >> 4)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210 | |
x = (x ^ (x >> 8)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210 | |
x = (x ^ (x >> 16)) & 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210 | |
return x; | |
} | |
uint32 EncodeMorton2(uint32 x, uint32 y) | |
{ | |
return (Part1By1(y) << 1) + Part1By1(x); | |
} | |
uint32 EncodeMorton3(uint32 x, uint32 y, uint32 z) | |
{ | |
return (Part1By2(z) << 2) + (Part1By2(y) << 1) + Part1By2(x); | |
} | |
uint32 DecodeMorton2X(uint32 code) | |
{ | |
return Compact1By1(code >> 0); | |
} | |
uint32 DecodeMorton2Y(uint32 code) | |
{ | |
return Compact1By1(code >> 1); | |
} | |
uint32 DecodeMorton3X(uint32 code) | |
{ | |
return Compact1By2(code >> 0); | |
} | |
uint32 DecodeMorton3Y(uint32 code) | |
{ | |
return Compact1By2(code >> 1); | |
} | |
uint32 DecodeMorton3Z(uint32 code) | |
{ | |
return Compact1By2(code >> 2); | |
} | |
int BitSeparate(int n) | |
{ | |
printf("Bitseparate %d.\n",n); | |
printf("1.n:%x\n",n); | |
printf("0x00ff00ff:%x\n",0x00ff00ff); | |
printf("n<<8:%x\n",n<<8); | |
printf("n|n<<8:%x\n",(n|(n<<8))); | |
n = (n|(n<<8)) & 0x00ff00ff; | |
printf("2.n:%x\n",n); | |
printf("n<<4:%x\n",n<<4); | |
printf("n|n<<4:%x\n",(n|(n<<4))); | |
n = (n|(n<<4)) & 0x0f0f0f0f; | |
printf("3.n:%x\n",n); | |
printf("n<<2:%x\n",n<<2); | |
printf("n|n<<2:%x\n",(n|(n<<2))); | |
n = (n|(n<<2)) & 0x33333333; | |
printf("n:%x\n",n); | |
printf("n<<1:%x\n",n<<1); | |
printf("n|n<<1:%x\n",(n|(n<<1))); | |
printf("4.return:%x\n",(n|(n<<1)) & 0x55555555); | |
return (n|(n<<1)) & 0x55555555; | |
} | |
int getMortonNum(int x, int y){ | |
return (BitSeparate(x) | (BitSeparate(y)<<1)); | |
} | |
int main(){ | |
uint32 a = 0xFF; | |
uint32 b = 0xF0; | |
printf("EncodeMorton2(%d,%d):%d\n",a,b,EncodeMorton2(a,b)); | |
uint32 result = EncodeMorton2(a,b); | |
printf("DecodeMorton2X,2Y(%d):%d,%d\n",result,DecodeMorton2X(result),DecodeMorton2Y(result)); | |
/* printf("Morton(%d,%d):%d\n",a,b,getMortonNum(a,b)); */ | |
return 0; | |
} |
morton code 相关
最新推荐文章于 2023-06-12 10:01:22 发布