How to compute a 3D Morton number (interleave the bits of 3 ints)

后端 未结 9 2144
旧巷少年郎
旧巷少年郎 2020-11-29 18:26

I\'m looking for a fast way to compute a 3D Morton number. This site has a magic-number based trick for doing it for 2D Morton numbers, but it doesn\'t seem obvious how to e

9条回答
  •  自闭症患者
    2020-11-29 18:52

    Following is the code snippet to generate Morton key of size 64 bits for 3-D point.

    using namespace std;
    
    unsigned long long spreadBits(unsigned long long x)
    {
        x=(x|(x<<20))&0x000001FFC00003FF;
        x=(x|(x<<10))&0x0007E007C00F801F;
        x=(x|(x<<4))&0x00786070C0E181C3;
        x=(x|(x<<2))&0x0199219243248649;
        x=(x|(x<<2))&0x0649249249249249;
        x=(x|(x<<2))&0x1249249249249249;
        return x;
    }
    
    int main()
    {
        unsigned long long x,y,z,con=1;
        con=con<<63;
        printf("%#llx\n",(spreadBits(x)|(spreadBits(y)<<1)|(spreadBits(z)<<2))|con);    
    }
    

提交回复
热议问题