Z阶曲线坐标

时间:2012-08-28 10:49:05

标签: c++

我怎样才能访问使用Z-order存储的数据,其中数组的时间复杂度为O(1)?我需要通过坐标快速访问每个元素。我有更快的方式来访问这些数据而不是使用while来移位位吗?

一种方法是使用查找表(我有静态大小的数据)

修改

我现在有一个想法是使用y * SIZE + x

按顺序存储叶子

编辑2。:

我在std :: bitset中的四叉树中编辑故事。我正在尝试检查是否有一些数据可用。在矩阵128 * 128的矩阵中。所以我可以跳过bruteforce矩阵搜索空数据。

2 个答案:

答案 0 :(得分:7)

您可以使用以下代码计算z阶曲线值:

uint32_t calcZOrder(uint16_t xPos, uint16_t yPos)
{
    static const uint32_t MASKS[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF};
    static const uint32_t SHIFTS[] = {1, 2, 4, 8};

    uint32_t x = xPos;  // Interleave lower 16 bits of x and y, so the bits of x
    uint32_t y = yPos;  // are in the even positions and bits from y in the odd;

    x = (x | (x << SHIFTS[3])) & MASKS[3];
    x = (x | (x << SHIFTS[2])) & MASKS[2];
    x = (x | (x << SHIFTS[1])) & MASKS[1];
    x = (x | (x << SHIFTS[0])) & MASKS[0];

    y = (y | (y << SHIFTS[3])) & MASKS[3];
    y = (y | (y << SHIFTS[2])) & MASKS[2];
    y = (y | (y << SHIFTS[1])) & MASKS[1];
    y = (y | (y << SHIFTS[0])) & MASKS[0];

    const uint32_t result = x | (y << 1);
    return result;
}

摘自Bit Twiddling Hacks

从128x128数组(或任何其他大小),您可以轻松计算任何位置的z阶曲线值。例如:

xPos = 2, yPos = 3 -> z order curve value = 7

示例代码的最大数组大小为65536 * 65536。只需使用2的幂就可以轻松,在这种情况下,最大浪费的空间大约是。 3/4

答案 1 :(得分:-1)

https://en.wikipedia.org/wiki/Z-order_curve

复制Wiki结果。
#include <stdio.h>
static const unsigned int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF};
static const unsigned int S[] = {1, 2, 4, 8};
unsigned int zorder2D(unsigned x, unsigned y){
    
    x = (x | (x << S[3])) & B[3];
    x = (x | (x << S[2])) & B[2];
    x = (x | (x << S[1])) & B[1];
    x = (x | (x << S[0])) & B[0];

    y = (y | (y << S[3])) & B[3];
    y = (y | (y << S[2])) & B[2];
    y = (y | (y << S[1])) & B[1];
    y = (y | (y << S[0])) & B[0];
    return x | (y << 1);
}

int main()
{    
    const unsigned nx=8,ny=8;
    unsigned res[ny][nx];

    for(unsigned y=0; y<ny; y++){
        for(unsigned x=0; x<nx; x++){
            res[y][x] = zorder2D(x,y);
            printf("yx=%d %d z=%d\n",y,x,res[y][x]);    
        }
    }
    for(unsigned y=0; y<ny; y++){
        for(unsigned x=0; x<nx; x++){
            printf("%-4d",res[y][x]);
        }
        printf("\n");
    }
    return 0;
}
相关问题