Loading...
Searching...
No Matches
SpaceFilling.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
25
26#ifndef GPR_AUXILIARY_SPACEFILLING_H_
27#define GPR_AUXILIARY_SPACEFILLING_H_
28
29#include <algorithm>
30#include <cstdint>
31#include <vector>
32
33#ifdef __BMI2__
34#include <immintrin.h>
35#endif
36
37namespace gpr {
38namespace sfc {
39
51inline uint32_t morton_3d(uint32_t x, uint32_t y, uint32_t z)
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}
68
76// clang-format off
77inline constexpr uint8_t MORTON_TO_HILBERT_LUT[96] = {
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};
91// clang-format on
92
100inline uint32_t morton_to_hilbert_3d(uint32_t morton, uint32_t bits)
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}
112
124inline uint32_t hilbert_3d(double x, double y, double z, double min_coord,
125 double scale, uint32_t bits = 10)
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}
143
156inline std::vector<size_t> hilbert_permutation(const double* coords,
157 size_t n_atoms,
158 uint32_t bits = 10)
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}
205
206} // namespace sfc
207} // namespace gpr
208
209#endif // GPR_AUXILIARY_SPACEFILLING_H_
@ 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 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.
constexpr uint8_t MORTON_TO_HILBERT_LUT[96]
96-byte LUT for Morton->Hilbert conversion.
uint32_t morton_to_hilbert_3d(uint32_t morton, uint32_t bits)
Convert Morton index to Hilbert index using 96-byte LUT.
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.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16