morton code 相关

#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;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值