EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_SFC.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
11#ifndef EBGEOMETRY_SFC_HPP
12#define EBGEOMETRY_SFC_HPP
13
14// Std includes
15#include <cstdint>
16#include <vector>
17
18// Our includes
19#include "EBGeometry_Array.hpp"
20#include "EBGeometry_GPU.hpp"
21#include "EBGeometry_Math.hpp"
22#include "EBGeometry_Vec.hpp"
23
24namespace EBGeometry {
25
47namespace SFC {
48
52using Code = uint64_t;
53
58
63static constexpr unsigned int ValidBits = 21;
64
68static constexpr Code ValidSpan = (static_cast<uint64_t>(1) << ValidBits) - 1;
69
73struct Morton
74{
80 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
81 inline static uint64_t
82 encode(const Index& a_point) noexcept;
83
90 [[nodiscard]] inline static Index
91 decode(const uint64_t& a_code) noexcept;
92
93protected:
97 static constexpr uint_fast64_t Mask_64[6]{
98 0x1fffff, 0x1f00000000ffff, 0x1f0000ff0000ff, 0x100f00f00f00f00f, 0x10c30c30c30c30c3, 0x1249249249249249};
99};
100
106struct Nested
107{
113 [[nodiscard]] inline static uint64_t
114 encode(const Index& a_point) noexcept;
115
122 [[nodiscard]] inline static Index
123 decode(const uint64_t& a_code) noexcept;
124};
125
138{
144 [[nodiscard]] inline static uint64_t
145 encode(const Index& a_point) noexcept;
146
153 [[nodiscard]] inline static Index
154 decode(const uint64_t& a_code) noexcept;
155};
156
173template <class T>
174[[nodiscard]] inline std::vector<Index>
175computeBins(const std::vector<Vec3T<T>>& a_points);
176
190template <class Curve = Morton, class T>
191[[nodiscard]] inline std::vector<uint32_t>
192order(const std::vector<Vec3T<T>>& a_points);
193
194} // namespace SFC
195
201namespace SFCDetail {
202
208{
210 uint64_t m_code;
211
213 uint32_t m_index;
214
220 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
221 inline bool
222 operator<(const MortonKey& a_other) const noexcept
223 {
224 return m_code < a_other.m_code || (m_code == a_other.m_code && m_index < a_other.m_index);
225 }
226};
227
235template <class T>
237{
240
243
250 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
251 static MortonGrid
252 over(const Vec3T<T>& a_lo, const Vec3T<T>& a_hi) noexcept
253 {
254 const Vec3T<T> halfLo = T(0.5) * a_lo;
255 const Vec3T<T> extent = T(0.5) * a_hi - halfLo;
256 const T side = Math::max(extent[0], Math::max(extent[1], extent[2]));
257
258 return MortonGrid{halfLo, (side > T(0)) ? T(SFC::ValidSpan) / side : T(0)};
259 }
260
267 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
269 key(const Vec3T<T>& a_point, const uint32_t a_index) const noexcept
270 {
271 SFC::Index cell;
272
273 for (int dir = 0; dir < 3; dir++) {
274 const T scaled = (T(0.5) * a_point[dir] - m_halfLo[dir]) * m_scale;
275
276 cell[dir] = static_cast<unsigned int>(Math::clamp(scaled, T(0), T(SFC::ValidSpan)));
277 }
278
279 return MortonKey{SFC::Morton::encode(cell), a_index};
280 }
281};
282
283} // namespace SFCDetail
284
285} // namespace EBGeometry
286
287#include "EBGeometry_SFCImplem.hpp"
288
289#endif
A fixed-size array that compiles for both host and device.
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_HOST_DEVICE
Mark a function as callable from both host and device code (no-op on host compilers).
Definition EBGeometry_GPU.hpp:80
Scalar helpers and numeric limits that compile for both host and device.
Declaration of 2D and 3D point/vector classes with templated precision, and free functions on them.
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
constexpr T clamp(const T &a_value, const T &a_lo, const T &a_hi) noexcept
A value limited to [lo, hi].
Definition EBGeometry_Math.hpp:78
constexpr T max(const T &a_a, const T &a_b) noexcept
The larger of two values.
Definition EBGeometry_Math.hpp:61
static constexpr unsigned int ValidBits
Maximum available bits per dimension. 21 is the largest value for which 3*ValidBits (63) still fits i...
Definition EBGeometry_SFC.hpp:63
std::vector< Index > computeBins(const std::vector< Vec3T< T > > &a_points)
Bin a set of points into the space-filling curve's integer grid, normalizing by their own bounding ra...
uint64_t Code
Alias for SFC code.
Definition EBGeometry_SFC.hpp:52
static constexpr Code ValidSpan
Maximum permitted span along any spatial coordinate.
Definition EBGeometry_SFC.hpp:68
std::vector< uint32_t > order(const std::vector< Vec3T< T > > &a_points)
Return the index permutation that orders a_points along a space-filling curve.
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
Fixed-size array with host- and device-callable members.
Definition EBGeometry_Array.hpp:33
The Morton grid over a box: a point's cell is (p / 2 - lo / 2) * scale, clamped to [0,...
Definition EBGeometry_SFC.hpp:237
MortonKey key(const Vec3T< T > &a_point, const uint32_t a_index) const noexcept
A point's key.
Definition EBGeometry_SFC.hpp:269
Vec3T< T > m_halfLo
Half the box's lower corner.
Definition EBGeometry_SFC.hpp:239
T m_scale
Cells per unit of halved length (0 for a box of one point).
Definition EBGeometry_SFC.hpp:242
static MortonGrid over(const Vec3T< T > &a_lo, const Vec3T< T > &a_hi) noexcept
The grid over a box.
Definition EBGeometry_SFC.hpp:252
A point's sort key: its cell's Morton code, then its index, so that no two keys are equal and every s...
Definition EBGeometry_SFC.hpp:208
bool operator<(const MortonKey &a_other) const noexcept
Whether this key sorts before a_other: by code, then by index.
Definition EBGeometry_SFC.hpp:222
uint32_t m_index
The point's index.
Definition EBGeometry_SFC.hpp:213
uint64_t m_code
The Morton code of the point's cell.
Definition EBGeometry_SFC.hpp:210
Implementation of the 3D Hilbert space-filling curve.
Definition EBGeometry_SFC.hpp:138
static Index decode(const uint64_t &a_code) noexcept
Decode the linear Hilbert distance back into an Index.
static uint64_t encode(const Index &a_point) noexcept
Encode the input point into the linear Hilbert distance.
Implementation of the Morton SFC.
Definition EBGeometry_SFC.hpp:74
static Index decode(const uint64_t &a_code) noexcept
Decode the 64-bit Morton code into an Index.
static constexpr uint_fast64_t Mask_64[6]
Mask for magic-bits encoding of 3D Morton code.
Definition EBGeometry_SFC.hpp:97
static uint64_t encode(const Index &a_point) noexcept
Encode an input point into a Morton index with a 64-bit representation.
Implementation of a nested index SFC.
Definition EBGeometry_SFC.hpp:107
static Index decode(const uint64_t &a_code) noexcept
Decode the 64-bit SFC code into an Index.
static uint64_t encode(const Index &a_point) noexcept
Encode the input point into the SFC code.