morton.hpp 6.1 KB

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