Loading...
Searching...
No Matches
gpr::sfc Namespace Reference

Functions

uint32_t morton_3d (uint32_t x, uint32_t y, uint32_t z)
 Compute 3D Morton (Z-order) index from integer coordinates.
uint32_t morton_to_hilbert_3d (uint32_t morton, uint32_t bits)
 Convert Morton index to Hilbert index using 96-byte LUT.
uint32_t hilbert_3d (double x, double y, double z, double min_coord, double scale, uint32_t bits=10)
 Compute 3D Hilbert index from floating-point coordinates.
std::vector< size_t > hilbert_permutation (const double *coords, size_t n_atoms, uint32_t bits=10)
 Compute Hilbert-ordered permutation for a set of 3D points.

Variables

constexpr uint8_t MORTON_TO_HILBERT_LUT [96]
 96-byte LUT for Morton->Hilbert conversion.

Function Documentation

◆ hilbert_3d()

uint32_t gpr::sfc::hilbert_3d ( double x,
double y,
double z,
double min_coord,
double scale,
uint32_t bits = 10 )
inline

Compute 3D Hilbert index from floating-point coordinates.

Parameters
xX coordinate
yY coordinate
zZ coordinate
min_coordMinimum coordinate value (for normalization)
scaleCoordinate range (max - min, for normalization)
bitsPrecision bits per dimension (default 10, giving 1024 levels)
Returns
Hilbert index

Definition at line 124 of file SpaceFilling.h.

126{
127 // Normalize to [0, 1] then scale to integer grid
128 const double inv_scale = (scale > 1e-12) ? 1.0 / scale : 1.0;
129 const uint32_t grid_size = (1u << bits) - 1;
130
131 auto to_grid = [=](double v) -> uint32_t {
132 double normalized = (v - min_coord) * inv_scale;
133 normalized = std::max(0.0, std::min(1.0, normalized));
134 return static_cast<uint32_t>(normalized * grid_size);
135 };
136
137 uint32_t xi = to_grid(x);
138 uint32_t yi = to_grid(y);
139 uint32_t zi = to_grid(z);
140
141 return morton_to_hilbert_3d(morton_3d(xi, yi, zi), bits);
142}
@ z
Definition Enums.h:28
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
uint32_t morton_3d(uint32_t x, uint32_t y, uint32_t z)
Compute 3D Morton (Z-order) index from integer coordinates.
uint32_t morton_to_hilbert_3d(uint32_t morton, uint32_t bits)
Convert Morton index to Hilbert index using 96-byte LUT.

◆ hilbert_permutation()

std::vector< size_t > gpr::sfc::hilbert_permutation ( const double * coords,
size_t n_atoms,
uint32_t bits = 10 )
inline

Compute Hilbert-ordered permutation for a set of 3D points.

Returns a permutation array such that iterating over atoms in permutation order visits them in Hilbert curve order (spatially coherent).

Parameters
coordsPointer to xyz coordinates, packed as [x0,y0,z0,x1,y1,z1,...]
n_atomsNumber of atoms
bitsHilbert precision (default 10)
Returns
Permutation array where perm[i] is the original index of the i-th atom in Hilbert order

Definition at line 156 of file SpaceFilling.h.

159{
160 if (n_atoms == 0) {
161 return {};
162 }
163
164 // Find bounding box
165 double min_x = coords[0], max_x = coords[0];
166 double min_y = coords[1], max_y = coords[1];
167 double min_z = coords[2], max_z = coords[2];
168
169 for (size_t i = 1; i < n_atoms; ++i) {
170 double x = coords[i * 3];
171 double y = coords[i * 3 + 1];
172 double z = coords[i * 3 + 2];
173 min_x = std::min(min_x, x);
174 max_x = std::max(max_x, x);
175 min_y = std::min(min_y, y);
176 max_y = std::max(max_y, y);
177 min_z = std::min(min_z, z);
178 max_z = std::max(max_z, z);
179 }
180
181 // Use max dimension for uniform scaling
182 double scale =
183 std::max({max_x - min_x, max_y - min_y, max_z - min_z, 1e-12});
184 double min_coord = std::min({min_x, min_y, min_z});
185
186 // Compute Hilbert indices
187 std::vector<std::pair<uint32_t, size_t>> indexed(n_atoms);
188 for (size_t i = 0; i < n_atoms; ++i) {
189 double x = coords[i * 3];
190 double y = coords[i * 3 + 1];
191 double z = coords[i * 3 + 2];
192 indexed[i] = {hilbert_3d(x, y, z, min_coord, scale, bits), i};
193 }
194
195 // Sort by Hilbert index
196 std::sort(indexed.begin(), indexed.end());
197
198 // Extract permutation
199 std::vector<size_t> perm(n_atoms);
200 for (size_t i = 0; i < n_atoms; ++i) {
201 perm[i] = indexed[i].second;
202 }
203 return perm;
204}
uint32_t hilbert_3d(double x, double y, double z, double min_coord, double scale, uint32_t bits=10)
Compute 3D Hilbert index from floating-point coordinates.

◆ morton_3d()

uint32_t gpr::sfc::morton_3d ( uint32_t x,
uint32_t y,
uint32_t z )
inline

Compute 3D Morton (Z-order) index from integer coordinates.

Morton index interleaves bits: xyz -> ...z2y2x2z1y1x1z0y0x0 Uses pdep intrinsic on BMI2-capable CPUs, otherwise bit manipulation.

Parameters
xX coordinate (0 to 2^bits - 1)
yY coordinate (0 to 2^bits - 1)
zZ coordinate (0 to 2^bits - 1)
Returns
Morton index

Definition at line 51 of file SpaceFilling.h.

52{
53#ifdef __BMI2__
54 return static_cast<uint32_t>(_pdep_u32(x, 0x09249249u) |
55 _pdep_u32(y, 0x12492492u) |
56 _pdep_u32(z, 0x24924924u));
57#else
58 auto spread = [](uint32_t v) -> uint32_t {
59 v = (v | (v << 16)) & 0x030000FFu;
60 v = (v | (v << 8)) & 0x0300F00Fu;
61 v = (v | (v << 4)) & 0x030C30C3u;
62 v = (v | (v << 2)) & 0x09249249u;
63 return v;
64 };
65 return spread(x) | (spread(y) << 1) | (spread(z) << 2);
66#endif
67}

◆ morton_to_hilbert_3d()

uint32_t gpr::sfc::morton_to_hilbert_3d ( uint32_t morton,
uint32_t bits )
inline

Convert Morton index to Hilbert index using 96-byte LUT.

Parameters
mortonMorton (Z-order) index
bitsNumber of bits per dimension (max 10 for 32-bit output)
Returns
Hilbert index

Definition at line 100 of file SpaceFilling.h.

101{
102 uint32_t transform = 0;
103 uint32_t hilbert = 0;
104 for (int32_t i = 3 * static_cast<int32_t>(bits - 1); i >= 0; i -= 3) {
105 uint32_t idx = transform | ((morton >> i) & 7u);
106 transform = MORTON_TO_HILBERT_LUT[idx];
107 hilbert = (hilbert << 3) | (transform & 7u);
108 transform &= ~7u; // Clear lower 3 bits, keep transform state
109 }
110 return hilbert;
111}
constexpr uint8_t MORTON_TO_HILBERT_LUT[96]
96-byte LUT for Morton->Hilbert conversion.

Variable Documentation

◆ MORTON_TO_HILBERT_LUT

uint8_t gpr::sfc::MORTON_TO_HILBERT_LUT[96]
inlineconstexpr
Initial value:
= {
48, 33, 27, 34, 47, 78, 28, 77,
66, 29, 51, 52, 65, 30, 72, 63,
76, 95, 75, 24, 53, 54, 82, 81,
18, 3, 17, 80, 61, 4, 62, 15,
0, 59, 71, 60, 49, 50, 86, 85,
84, 83, 5, 90, 79, 56, 6, 89,
32, 23, 1, 94, 11, 12, 2, 93,
42, 41, 13, 14, 35, 88, 36, 31,
92, 37, 87, 38, 91, 74, 8, 73,
46, 45, 9, 10, 7, 20, 64, 19,
70, 25, 39, 16, 69, 26, 44, 43,
22, 55, 21, 68, 57, 40, 58, 67,
}

96-byte LUT for Morton->Hilbert conversion.

12 transform states x 8 octants = 96 entries. Upper bits encode next transform state, lower 3 bits encode Hilbert sub-index.

Definition at line 77 of file SpaceFilling.h.

77 {
78 48, 33, 27, 34, 47, 78, 28, 77, // transform 0
79 66, 29, 51, 52, 65, 30, 72, 63, // transform 1
80 76, 95, 75, 24, 53, 54, 82, 81, // transform 2
81 18, 3, 17, 80, 61, 4, 62, 15, // transform 3
82 0, 59, 71, 60, 49, 50, 86, 85, // transform 4
83 84, 83, 5, 90, 79, 56, 6, 89, // transform 5
84 32, 23, 1, 94, 11, 12, 2, 93, // transform 6
85 42, 41, 13, 14, 35, 88, 36, 31, // transform 7
86 92, 37, 87, 38, 91, 74, 8, 73, // transform 8
87 46, 45, 9, 10, 7, 20, 64, 19, // transform 9
88 70, 25, 39, 16, 69, 26, 44, 43, // transform 10
89 22, 55, 21, 68, 57, 40, 58, 67, // transform 11
90};