morton.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #ifndef _SCTL_MORTON_
  2. #define _SCTL_MORTON_
  3. #include SCTL_INCLUDE(common.hpp)
  4. #include <cstdint>
  5. #ifndef SCTL_MAX_DEPTH
  6. #define SCTL_MAX_DEPTH 15
  7. #endif
  8. namespace SCTL_NAMESPACE {
  9. template <class ValueType> class Vector;
  10. template <Integer DIM = 3> class Morton {
  11. public:
  12. #if SCTL_MAX_DEPTH < 7
  13. typedef uint8_t UINT_T;
  14. typedef int8_t INT_T;
  15. #elif SCTL_MAX_DEPTH < 15
  16. typedef uint16_t UINT_T;
  17. typedef int16_t INT_T;
  18. #elif SCTL_MAX_DEPTH < 31
  19. typedef uint32_t UINT_T;
  20. typedef int32_t INT_T;
  21. #elif SCTL_MAX_DEPTH < 63
  22. typedef uint64_t UINT_T;
  23. typedef int64_t INT_T;
  24. #endif
  25. static const Integer MAX_DEPTH = SCTL_MAX_DEPTH;
  26. Morton() {
  27. depth = 0;
  28. for (Integer i = 0; i < DIM; i++) x[i] = 0;
  29. }
  30. template <class T> Morton(ConstIterator<T> coord, uint8_t depth_ = MAX_DEPTH) {
  31. depth = depth_;
  32. assert(depth <= MAX_DEPTH);
  33. UINT_T mask = ~((((UINT_T)1) << (MAX_DEPTH - depth)) - 1);
  34. for (Integer i = 0; i < DIM; i++) x[i] = mask & (UINT_T)floor(coord[i] * maxCoord);
  35. }
  36. uint8_t Depth() const { return depth; }
  37. template <class T> void Coord(Iterator<T> coord) const {
  38. static const T s = 1.0 / ((T)maxCoord);
  39. for (Integer i = 0; i < DIM; i++) coord[i] = x[i] * s;
  40. }
  41. Morton Next() const {
  42. UINT_T mask = ((UINT_T)1) << (MAX_DEPTH - depth);
  43. Integer d, i;
  44. Morton m = *this;
  45. for (d = depth; d >= 0; d--) {
  46. for (i = 0; i < DIM; i++) {
  47. m.x[i] = (m.x[i] ^ mask);
  48. if ((m.x[i] & mask)) break;
  49. }
  50. if (i < DIM) break;
  51. mask = (mask << 1);
  52. }
  53. if (d < 0) d = 0;
  54. m.depth = (uint8_t)d;
  55. return m;
  56. }
  57. Morton Ancestor(uint8_t ancestor_level) const {
  58. UINT_T mask = ~((((UINT_T)1) << (MAX_DEPTH - ancestor_level)) - 1);
  59. Morton m;
  60. for (Integer i = 0; i < DIM; i++) m.x[i] = x[i] & mask;
  61. m.depth = ancestor_level;
  62. return m;
  63. }
  64. /**
  65. * \brief Returns the deepest first descendant.
  66. */
  67. Morton DFD(uint8_t level = MAX_DEPTH) const {
  68. Morton m = *this;
  69. m.depth = level;
  70. return m;
  71. }
  72. void NbrList(Vector<Morton> &nlst, uint8_t level, bool periodic) const {
  73. nlst.ReInit(1);
  74. UINT_T mask = ~((((UINT_T)1) << (MAX_DEPTH - level)) - 1);
  75. for (Integer i = 0; i < DIM; i++) nlst[0].x[i] = x[i] & mask;
  76. nlst[0].depth = level;
  77. Morton m;
  78. Integer k = 1;
  79. mask = (((UINT_T)1) << (MAX_DEPTH - level));
  80. if (periodic) {
  81. for (Integer i = 0; i < DIM; i++) {
  82. for (Integer j = 0; j < k; j++) {
  83. m = nlst[j];
  84. INT_T xi = (INT_T)m.x[i] + (INT_T)mask;
  85. if (xi >= maxCoord) xi -= maxCoord;
  86. m.x[i] = xi;
  87. nlst.PushBack(m);
  88. }
  89. for (Integer j = 0; j < k; j++) {
  90. m = nlst[j];
  91. INT_T xi = (INT_T)m.x[i] - (INT_T)mask;
  92. if (xi < 0) xi += maxCoord;
  93. m.x[i] = xi;
  94. nlst.PushBack(m);
  95. }
  96. k = nlst.Dim();
  97. }
  98. } else {
  99. for (Integer i = 0; i < DIM; i++) {
  100. for (Integer j = 0; j < k; j++) {
  101. m = nlst[j];
  102. INT_T xi = (INT_T)m.x[i] + (INT_T)mask;
  103. m.x[i] = xi;
  104. if (xi < maxCoord) nlst.PushBack(m);
  105. }
  106. for (Integer j = 0; j < k; j++) {
  107. m = nlst[j];
  108. INT_T xi = (INT_T)m.x[i] - (INT_T)mask;
  109. m.x[i] = xi;
  110. if (xi >= 0) nlst.PushBack(m);
  111. }
  112. k = nlst.Dim();
  113. }
  114. }
  115. }
  116. void Children(Vector<Morton> &nlst) const {
  117. static const Integer cnt = (1UL << DIM);
  118. if (nlst.Dim() != cnt) nlst.ReInit(cnt);
  119. for (Integer i = 0; i < DIM; i++) nlst[0].x[i] = x[i];
  120. nlst[0].depth = (uint8_t)(depth + 1);
  121. Integer k = 1;
  122. UINT_T mask = (((UINT_T)1) << (MAX_DEPTH - (depth + 1)));
  123. for (Integer i = 0; i < DIM; i++) {
  124. for (Integer j = 0; j < k; j++) {
  125. nlst[j + k] = nlst[j];
  126. nlst[j + k].x[i] += mask;
  127. }
  128. k = (k << 1);
  129. }
  130. }
  131. bool operator<(const Morton &m) const {
  132. UINT_T diff = 0;
  133. for (Integer i = 0; i < DIM; i++) diff = diff | (x[i] ^ m.x[i]);
  134. if (!diff) return depth < m.depth;
  135. UINT_T mask = 1;
  136. for (Integer i = 4 * sizeof(UINT_T); i > 0; i = (i >> 1)) {
  137. UINT_T mask_ = (mask << i);
  138. if (mask_ <= diff) mask = mask_;
  139. }
  140. for (Integer i = DIM - 1; i >= 0; i--) {
  141. if (mask & (x[i] ^ m.x[i])) return x[i] < m.x[i];
  142. }
  143. }
  144. bool operator>(const Morton &m) const { return m < (*this); }
  145. bool operator!=(const Morton &m) const {
  146. for (Integer i = 0; i < DIM; i++)
  147. if (x[i] != m.x[i]) return true;
  148. return (depth != m.depth);
  149. }
  150. bool operator==(const Morton &m) const { return !(*this != m); }
  151. bool operator<=(const Morton &m) const { return !(*this > m); }
  152. bool operator>=(const Morton &m) const { return !(*this < m); }
  153. bool isAncestor(Morton const &descendant) const { return descendant.depth > depth && descendant.Ancestor(depth) == *this; }
  154. Long operator-(const Morton<DIM> &I) const {
  155. // Intersecting -1
  156. // Touching 0
  157. Long offset0 = 1 << (MAX_DEPTH - depth - 1);
  158. Long offset1 = 1 << (MAX_DEPTH - I.depth - 1);
  159. Long diff = 0;
  160. for (Integer i = 0; i < DIM; i++) {
  161. diff = std::max<Long>(diff, abs(((Long)x[i] + offset0) - ((Long)I.x[i] + offset1)));
  162. }
  163. if (diff < offset0 + offset1) return -1;
  164. Integer max_depth = std::max(depth, I.depth);
  165. diff = (diff - offset0 - offset1) >> (MAX_DEPTH - max_depth);
  166. return diff;
  167. }
  168. friend std::ostream &operator<<(std::ostream &out, const Morton &mid) {
  169. double a = 0;
  170. double s = 1u << DIM;
  171. for (Integer j = MAX_DEPTH; j >= 0; j--) {
  172. for (Integer i = DIM - 1; i >= 0; i--) {
  173. s = s * 0.5;
  174. if (mid.x[i] & (((UINT_T)1) << j)) a += s;
  175. }
  176. }
  177. out << "(";
  178. for (Integer i = 0; i < DIM; i++) {
  179. out << mid.x[i] * 1.0 / maxCoord << ",";
  180. }
  181. out << (int)mid.depth << "," << a << ")";
  182. return out;
  183. }
  184. private:
  185. static constexpr UINT_T maxCoord = ((UINT_T)1) << (MAX_DEPTH);
  186. // StaticArray<UINT_T,DIM> x;
  187. UINT_T x[DIM];
  188. uint8_t depth;
  189. };
  190. }
  191. #endif //_SCTL_MORTON_