@@ -16,7 +16,7 @@ namespace core
16
16
namespace impl
17
17
{
18
18
template <typename T>
19
- constexpr T morton2d_mask (uint32_t _n)
19
+ constexpr T morton2d_mask (uint8_t _n)
20
20
{
21
21
constexpr uint64_t mask[5 ] =
22
22
{
@@ -28,6 +28,31 @@ namespace impl
28
28
};
29
29
return static_cast <T>(mask[_n]);
30
30
}
31
+ template <typename T>
32
+ constexpr T morton3d_mask (uint8_t _n)
33
+ {
34
+ constexpr uint64_t mask[5 ] =
35
+ {
36
+ 0x1249249249249249ull ,
37
+ 0x10C30C30C30C30C3ull ,
38
+ 0x010F00F00F00F00Full ,
39
+ 0x001F0000FF0000FFull ,
40
+ 0x001F00000000FFFFull
41
+ };
42
+ return static_cast <T>(mask[_n]);
43
+ }
44
+ template <typename T>
45
+ constexpr T morton4d_mask (uint8_t _n)
46
+ {
47
+ constexpr uint64_t mask[4 ] =
48
+ {
49
+ 0x1111111111111111ull ,
50
+ 0x0303030303030303ull ,
51
+ 0x000F000F000F000Full ,
52
+ 0x000000FF000000FFull
53
+ };
54
+ return static_cast <T>(mask[_n]);
55
+ }
31
56
32
57
template <typename T, uint32_t bitDepth>
33
58
inline T morton2d_decode (T x)
@@ -58,7 +83,7 @@ namespace impl
58
83
{
59
84
x = (x | (x << 16 )) & morton2d_mask<T>(4 );
60
85
}
61
- if constexpr (bitDepth > 16u )
86
+ if constexpr (bitDepth> 16u )
62
87
{
63
88
x = (x | (x << 8 )) & morton2d_mask<T>(3 );
64
89
}
@@ -71,15 +96,43 @@ namespace impl
71
96
72
97
return x;
73
98
}
99
+ template <typename T, uint32_t bitDepth>
100
+ inline T separate_bits_3d (T x)
101
+ {
102
+ if constexpr (bitDepth>32u )
103
+ {
104
+ x = (x | (x << 32 )) & morton3d_mask<T>(4 );
105
+ }
106
+ if constexpr (bitDepth>16u )
107
+ {
108
+ x = (x | (x << 16 )) & morton3d_mask<T>(3 );
109
+ }
110
+ if constexpr (bitDepth>8u )
111
+ {
112
+ x = (x | (x << 8 )) & morton3d_mask<T>(2 );
113
+ }
114
+ x = (x | (x << 4 )) & morton3d_mask<T>(1 );
115
+ x = (x | (x << 2 )) & morton3d_mask<T>(0 );
74
116
75
- inline uint64_t separate_bits_3d (uint64_t x)
117
+ return x;
118
+ }
119
+ template <typename T, uint32_t bitDepth>
120
+ inline T separate_bits_4d (T x)
76
121
{
77
- x &= 0x00000000001fffff ;
78
- x = (x | x << 32 ) & 0x001f00000000ffff ;
79
- x = (x | x << 16 ) & 0x001f0000ff0000ff ;
80
- x = (x | x << 8 ) & 0x010f00f00f00f00f ;
81
- x = (x | x << 4 ) & 0x10c30c30c30c30c3 ;
82
- x = (x | x << 2 ) & 0x1249249249249249 ;
122
+ if constexpr (bitDepth>32u )
123
+ {
124
+ x = (x | (x << 24 )) & morton4d_mask<T>(3 );
125
+ }
126
+ if constexpr (bitDepth>16u )
127
+ {
128
+ x = (x | (x << 12 )) & morton4d_mask<T>(2 );
129
+ }
130
+ if constexpr (bitDepth>8u )
131
+ {
132
+ x = (x | (x << 6 )) & morton4d_mask<T>(1 );
133
+ }
134
+ x = (x | (x << 3 )) & morton4d_mask<T>(0 );
135
+
83
136
return x;
84
137
}
85
138
}
@@ -91,8 +144,10 @@ T morton2d_decode_y(T _morton) { return impl::morton2d_decode<T,bitDepth>(_morto
91
144
92
145
template <typename T, uint32_t bitDepth=sizeof (T)*8u >
93
146
T morton2d_encode (T x, T y) { return impl::separate_bits_2d<T,bitDepth>(x) | (impl::separate_bits_2d<T,bitDepth>(y)<<1 ); }
94
-
95
- inline uint64_t morton3d_encode (uint64_t x, uint64_t y, uint64_t z) { return impl::separate_bits_3d (x) | (impl::separate_bits_3d (y) << 1 ) | (impl::separate_bits_3d (z) << 2 ); }
147
+ template <typename T, uint32_t bitDepth=sizeof (T)*8u >
148
+ T morton3d_encode (T x, T y, T z) { return impl::separate_bits_3d<T,bitDepth>(x) | (impl::separate_bits_3d<T,bitDepth>(y)<<1 ) | (impl::separate_bits_3d<T,bitDepth>(z)<<2 ); }
149
+ template <typename T, uint32_t bitDepth=sizeof (T)*8u >
150
+ T morton4d_encode (T x, T y, T z, T w) { return impl::separate_bits_4d<T,bitDepth>(x) | (impl::separate_bits_4d<T,bitDepth>(y)<<1 ) | (impl::separate_bits_4d<T,bitDepth>(z)<<2 ) | (impl::separate_bits_4d<T,bitDepth>(w)<<3 ); }
96
151
97
152
}}
98
153
0 commit comments