morton.c 871 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include "hilbert.h"
  5. #include "morton.h"
  6. static uint32_t mortonLUT[3][256];
  7. uint32_t preshift(uint32_t val, int bits) {
  8. int i;
  9. uint32_t o;
  10. o = 0;
  11. for(i = 0; i < 8; i++) {
  12. o |= (val & (1U << i)) << (i + bits);
  13. }
  14. return o;
  15. }
  16. void initMortonLUT() {
  17. int i;
  18. for(i = 0; i < 256; i++) {
  19. mortonLUT[0][i] = preshift(i, 0);
  20. mortonLUT[1][i] = preshift(i, 1);
  21. mortonLUT[2][i] = preshift(i, 2);
  22. }
  23. }
  24. uint64_t mortonIndex(int x, int y, int z) {
  25. uint64_t o;
  26. o = mortonLUT[0][(x >> 16) & 0xFF] | mortonLUT[1][(y >> 16) & 0xFF] | mortonLUT[2][(z >> 16) & 0xFF];
  27. o <<= 24;
  28. o |= mortonLUT[0][(x >> 8) & 0xFF] | mortonLUT[1][(y >> 8) & 0xFF] | mortonLUT[2][(z >> 8) & 0xFF];
  29. o <<= 24;
  30. o |= mortonLUT[0][x & 0xFF] | mortonLUT[1][y & 0xFF] | mortonLUT[2][z & 0xFF];
  31. return o;
  32. }