#include <cstdint>
#include <vector>
#define MAXMORTONKEY 321685687669321 //21 bits MAX morton key
std::vector<uint32_t> mortonkeyX;
std::vector<uint32_t> mortonkeyY;
std::vector<uint32_t> mortonkeyZ;
/*Compute a 256 array of morton keys at compile time*/
template <uint32_t i, uint32_t offset>
struct morton
{
enum
{
//Use a little trick to calculate next morton key
//mortonkey(x+1) = (mortonkey(x) - MAXMORTONKEY) & MAXMORTONKEY
value = (morton<i - 1, offset>::value - MAXMORTONKEY) & MAXMORTONKEY
};
static void add_values(std::vector<uint32_t>& v)
{
morton<i - 1, offset>::add_values(v);
v.push_back(value<<offset);
}
};
template <uint32_t offset>
struct morton<0, offset>
{
enum
{
value = 0
};
static void add_values(std::vector<uint32_t>& v)
{
v.push_back(value);
}
};
uint32_t encodeMortonKey(uint8_t x, uint8_t y, uint8_t z)
{
/*uint64_t result = 0;
result = mortonkeyZ[(z >> 16) & 0xFF] |
mortonkeyY[(y >> 16) & 0xFF] |
mortonkeyX[(x >> 16) & 0xFF];
result = result << 48 |
mortonkeyZ[(z >> 8) & 0xFF] |
mortonkeyY[(y >> 8) & 0xFF] |
mortonkeyX[(x >> 8) & 0xFF];
result = result << 24 |
mortonkeyZ[(z)& 0xFF] |
mortonkeyY[(y)& 0xFF] |
mortonkeyX[(x)& 0xFF];*/
//return result;
return mortonkeyX[x] | mortonkeyY[y] | mortonkeyZ[z];
}
#include <iostream>
#define TESTSIZE 512
int main() {
//init vectors with precomputed morton keys
morton<256, 0>::add_values(mortonkeyX);
morton<256, 1>::add_values(mortonkeyY);
morton<256, 2>::add_values(mortonkeyZ);
uint32_t key;
for (int x = 0; x <= TESTSIZE; ++x)
{
for (int y = 0; y <= TESTSIZE; ++y)
{
for (int z = 0; z <= TESTSIZE; ++z)
{
key = encodeMortonKey(x, y, z);
}
}
}
/*std::cout << encodeMortonKey(0, 0, 0) << std::endl;
std::cout << encodeMortonKey(1, 0, 0) << std::endl;
std::cout << encodeMortonKey(0, 1, 0) << std::endl;
std::cout << encodeMortonKey(0, 0, 1) << std::endl;
std::cout << encodeMortonKey(5, 2, 5) << std::endl;
std::cout << encodeMortonKey(512, 512, 512) << std::endl;
std::cout << encodeMortonKey(695837, 6583067, 1836573) << std::endl;*/
uint32_t last = 0;
for(int i=1; i<32; ++i)
{
std::cout << mortonkeyX[i]-last << ",";
last = mortonkeyX[i];
}
}
I2luY2x1ZGUgPGNzdGRpbnQ+CiNpbmNsdWRlIDx2ZWN0b3I+CgojZGVmaW5lIE1BWE1PUlRPTktFWSAzMjE2ODU2ODc2NjkzMjEgLy8yMSBiaXRzIE1BWCBtb3J0b24ga2V5CgpzdGQ6OnZlY3Rvcjx1aW50MzJfdD4gbW9ydG9ua2V5WDsKc3RkOjp2ZWN0b3I8dWludDMyX3Q+IG1vcnRvbmtleVk7CnN0ZDo6dmVjdG9yPHVpbnQzMl90PiBtb3J0b25rZXlaOwovKkNvbXB1dGUgYSAyNTYgYXJyYXkgb2YgbW9ydG9uIGtleXMgYXQgY29tcGlsZSB0aW1lKi8KdGVtcGxhdGUgPHVpbnQzMl90IGksIHVpbnQzMl90IG9mZnNldD4Kc3RydWN0IG1vcnRvbgp7CgllbnVtCgl7CgkJLy9Vc2UgYSBsaXR0bGUgdHJpY2sgdG8gY2FsY3VsYXRlIG5leHQgbW9ydG9uIGtleQoJCS8vbW9ydG9ua2V5KHgrMSkgPSAobW9ydG9ua2V5KHgpIC0gTUFYTU9SVE9OS0VZKSAmIE1BWE1PUlRPTktFWQoJCXZhbHVlID0gKG1vcnRvbjxpIC0gMSwgb2Zmc2V0Pjo6dmFsdWUgLSBNQVhNT1JUT05LRVkpICYgTUFYTU9SVE9OS0VZCgl9OwoJc3RhdGljIHZvaWQgYWRkX3ZhbHVlcyhzdGQ6OnZlY3Rvcjx1aW50MzJfdD4mIHYpCgl7CgkJbW9ydG9uPGkgLSAxLCBvZmZzZXQ+OjphZGRfdmFsdWVzKHYpOwoJCXYucHVzaF9iYWNrKHZhbHVlPDxvZmZzZXQpOwoJ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