fork(15) download
  1. #include <cstdint>
  2. #include <vector>
  3.  
  4. #define MAXMORTONKEY 321685687669321 //21 bits MAX morton key
  5.  
  6. std::vector<uint32_t> mortonkeyX;
  7. std::vector<uint32_t> mortonkeyY;
  8. std::vector<uint32_t> mortonkeyZ;
  9. /*Compute a 256 array of morton keys at compile time*/
  10. template <uint32_t i, uint32_t offset>
  11. struct morton
  12. {
  13. enum
  14. {
  15. //Use a little trick to calculate next morton key
  16. //mortonkey(x+1) = (mortonkey(x) - MAXMORTONKEY) & MAXMORTONKEY
  17. value = (morton<i - 1, offset>::value - MAXMORTONKEY) & MAXMORTONKEY
  18. };
  19. static void add_values(std::vector<uint32_t>& v)
  20. {
  21. morton<i - 1, offset>::add_values(v);
  22. v.push_back(value<<offset);
  23. }
  24. };
  25. template <uint32_t offset>
  26. struct morton<0, offset>
  27. {
  28. enum
  29. {
  30. value = 0
  31. };
  32. static void add_values(std::vector<uint32_t>& v)
  33. {
  34. v.push_back(value);
  35. }
  36. };
  37.  
  38. uint32_t encodeMortonKey(uint8_t x, uint8_t y, uint8_t z)
  39. {
  40. /*uint64_t result = 0;
  41. result = mortonkeyZ[(z >> 16) & 0xFF] |
  42. mortonkeyY[(y >> 16) & 0xFF] |
  43. mortonkeyX[(x >> 16) & 0xFF];
  44. result = result << 48 |
  45. mortonkeyZ[(z >> 8) & 0xFF] |
  46. mortonkeyY[(y >> 8) & 0xFF] |
  47. mortonkeyX[(x >> 8) & 0xFF];
  48. result = result << 24 |
  49. mortonkeyZ[(z)& 0xFF] |
  50. mortonkeyY[(y)& 0xFF] |
  51. mortonkeyX[(x)& 0xFF];*/
  52. //return result;
  53. return mortonkeyX[x] | mortonkeyY[y] | mortonkeyZ[z];
  54. }
  55.  
  56. #include <iostream>
  57.  
  58. #define TESTSIZE 512
  59.  
  60. int main() {
  61.  
  62. //init vectors with precomputed morton keys
  63. morton<256, 0>::add_values(mortonkeyX);
  64. morton<256, 1>::add_values(mortonkeyY);
  65. morton<256, 2>::add_values(mortonkeyZ);
  66.  
  67. uint32_t key;
  68. for (int x = 0; x <= TESTSIZE; ++x)
  69. {
  70. for (int y = 0; y <= TESTSIZE; ++y)
  71. {
  72. for (int z = 0; z <= TESTSIZE; ++z)
  73. {
  74. key = encodeMortonKey(x, y, z);
  75. }
  76. }
  77. }
  78. /*std::cout << encodeMortonKey(0, 0, 0) << std::endl;
  79. std::cout << encodeMortonKey(1, 0, 0) << std::endl;
  80. std::cout << encodeMortonKey(0, 1, 0) << std::endl;
  81. std::cout << encodeMortonKey(0, 0, 1) << std::endl;
  82. std::cout << encodeMortonKey(5, 2, 5) << std::endl;
  83. std::cout << encodeMortonKey(512, 512, 512) << std::endl;
  84. std::cout << encodeMortonKey(695837, 6583067, 1836573) << std::endl;*/
  85.  
  86. uint32_t last = 0;
  87. for(int i=1; i<32; ++i)
  88. {
  89. std::cout << mortonkeyX[i]-last << ",";
  90. last = mortonkeyX[i];
  91. }
  92. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1,7,1,55,1,7,1,439,1,7,1,55,1,7,1,3511,1,7,1,55,1,7,1,439,1,7,1,55,1,7,1,