supereight
key_util.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016-2019 Emanuele Vespa
3  * SPDX-FileCopyrightText: 2021-2023 Smart Robotics Lab, Imperial College London, Technical University of Munich
4  * SPDX-FileCopyrightText: 2021-2023 Nils Funk
5  * SPDX-FileCopyrightText: 2021-2023 Sotiris Papatheodorou
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef SE_KEY_UTIL_HPP
10 #define SE_KEY_UTIL_HPP
11 
12 #include <algorithm>
13 
16 
17 
18 
19 namespace se {
20 
21 #define NUM_DIM 3
22 #define SCALE_OFFSET 5
23 #define KEY_SCALE_LIMIT 19
24 
39 constexpr uint64_t CODE_MASK[] = {
40  0x1ffffffffffffff,
41  0x1fffffffffffff8,
42  0x1ffffffffffffc0,
43  0x1fffffffffffe00,
44  0x1fffffffffff000,
45  0x1ffffffffff8000,
46  0x1fffffffffc0000,
47  0x1ffffffffe00000,
48  0x1ffffffff000000,
49  0x1fffffff8000000,
50  0x1ffffffc0000000,
51  0x1fffffe00000000,
52  0x1fffff000000000,
53  0x1ffff8000000000,
54  0x1fffc0000000000,
55  0x1ffe00000000000,
56  0x1ff000000000000,
57  0x1f8000000000000,
58  0x1c0000000000000};
59 
60 constexpr uint64_t SCALE_MASK = 0x1F;
61 
62 namespace keyops {
63 
70 inline bool is_valid(const se::key_t key, const se::scale_t limit = KEY_SCALE_LIMIT);
71 
78 inline bool is_valid(const Eigen::Vector3i& coord);
79 
87 inline se::code_t expand(unsigned long long value);
88 
96 inline se::key_t compact(uint64_t value);
97 
111 inline bool encode_key(const Eigen::Vector3i& coord, const se::scale_t scale, se::key_t& key);
112 
125 inline se::key_t encode_key(const Eigen::Vector3i& coord, const se::scale_t scale);
126 
140 inline bool encode_key(const se::key_t code, const se::scale_t scale, se::key_t& key);
141 
154 inline se::key_t encode_key(const se::key_t code, const se::scale_t scale);
155 
163 inline void decode_key(const se::key_t key, Eigen::Vector3i& coord, scale_t& scale);
164 
171 inline void encode_code(const Eigen::Vector3i& coord, se::code_t& code);
172 
179 inline se::code_t encode_code(const Eigen::Vector3i& coord);
180 
187 inline void decode_code(const se::code_t code, Eigen::Vector3i& coord);
188 
189 
190 
199 inline idx_t code_to_child_idx(const se::code_t code, const scale_t scale);
200 
208 inline se::code_t key_to_code(const se::key_t key);
209 
217 inline Eigen::Vector3i key_to_coord(const se::key_t key);
218 
226 inline scale_t key_to_scale(const se::key_t key);
227 
237 inline bool key_at_scale(const se::key_t key, const se::scale_t scale, se::key_t& key_at_scale);
238 
248 inline bool code_at_scale(const se::key_t key, const se::scale_t scale, se::code_t& code_at_scale);
249 
256 inline void parent_key(const se::key_t key, se::key_t& parent_key);
257 
268 inline se::key_t block_key(const se::key_t key, const se::scale_t max_block_scale);
269 
280 inline se::code_t block_code(const se::key_t key, const se::scale_t max_block_scale);
281 
291 inline void parent_to_child_key(const se::key_t parent_key,
292  const se::code_t code_at_scale,
293  se::key_t& child_key);
294 
306 inline bool is_child(const se::key_t parent_key, const se::key_t child_key);
307 
316 inline bool is_siblings(const se::key_t sibling_1_key, const se::key_t sibling_2_key);
317 
321 template<Sort = Sort::SmallToLarge>
322 inline void sort_keys(std::vector<se::key_t>& keys);
323 
331 template<>
332 inline void sort_keys<Sort::SmallToLarge>(std::vector<se::key_t>& keys);
333 
341 template<>
342 inline void sort_keys<Sort::LargeToSmall>(std::vector<se::key_t>& keys);
343 
350 template<se::Safe SafeB>
351 inline void unique_keys(const std::vector<se::key_t>& keys, std::vector<se::key_t>& unique_keys);
352 
359 template<se::Safe SafeB>
360 inline void unique_codes(const std::vector<se::key_t>& keys, std::vector<se::key_t>& unique_keys);
361 
370 template<se::Safe SafeB>
371 inline void unique_allocation(const std::vector<se::key_t>& keys,
372  const scale_t max_block_scale,
373  std::vector<se::key_t>& unique_keys);
374 
384 template<se::Safe SafeB>
385 inline void unique_at_scale(const std::vector<se::key_t>& keys,
386  const se::scale_t scale,
387  std::vector<se::key_t>& unique_keys);
388 
396 inline void face_neighbours(const se::key_t key, std::array<se::key_t, 6> face_neighbour_keys);
397 
405 inline void neighbours(const se::key_t key, std::array<se::key_t, 26> neighbour_keys);
406 
414 inline void siblings(const se::key_t key, std::array<se::key_t, 8> sibling_keys);
415 
416 } // namespace keyops
417 } // namespace se
418 
419 #include "impl/key_util_impl.hpp"
420 
421 #endif // SE_KEY_UTIL_HPP
unsigned int idx_t
Child or voxel index type.
Definition: type_util.hpp:48
uint64_t scale_t
The type of the scale in the morton code.
Definition: type_util.hpp:46
constexpr uint64_t CODE_MASK[]
#define NUM_DIM 3 uint64_t MASK[64]; MASK[0] = 0x1c0000000000000;
Definition: key_util.hpp:39
void unique_keys(const std::vector< se::key_t > &keys, std::vector< se::key_t > &unique_keys)
Filter keys based on the whole key (i.e.
void unique_allocation(const std::vector< se::key_t > &keys, const scale_t max_block_scale, std::vector< se::key_t > &unique_keys)
Filter keys based on unique allocation.
void encode_code(const Eigen::Vector3i &coord, se::code_t &code)
Compute the Morton code for given x,y,z coordinates.
void sort_keys(std::vector< se::key_t > &keys)
Sorting template.
se::key_t block_key(const se::key_t key, const se::scale_t max_block_scale)
Removes the voxel position detail within a block from a key while maintainig the scale information...
se::code_t expand(unsigned long long value)
Expands a value that can be expressed by <= 19 bits E.g.
void face_neighbours(const se::key_t key, std::array< se::key_t, 6 > face_neighbour_keys)
TODO: 6-connectivity + centre.
void unique_at_scale(const std::vector< se::key_t > &keys, const se::scale_t scale, std::vector< se::key_t > &unique_keys)
Filter keys at a given scale.
bool key_at_scale(const se::key_t key, const se::scale_t scale, se::key_t &key_at_scale)
For a given key, change the key scale and reduce detail from Morton code up to given the scale...
void parent_to_child_key(const se::key_t parent_key, const se::code_t code_at_scale, se::key_t &child_key)
Compute the child key for a given parent key and child index.
void parent_key(const se::key_t key, se::key_t &parent_key)
Compute the direct parent key for a given key.
bool encode_key(const Eigen::Vector3i &coord, const se::scale_t scale, se::key_t &key)
Encodes given coordinates and scale in a key.
se::code_t key_to_code(const se::key_t key)
Reduce a key to only its Morton code.
uint64_t key_t
key = 1 bit buffer + 57 bits of morton code + 6 bits of scale information The maxium scale is limited...
Definition: type_util.hpp:44
void unique_codes(const std::vector< se::key_t > &keys, std::vector< se::key_t > &unique_keys)
Filter keys based on their code and keep the key with the smallest scale.
uint64_t code_t
The type of the Morton code.
Definition: type_util.hpp:45
void decode_key(const se::key_t key, Eigen::Vector3i &coord, scale_t &scale)
Extracts the 3D coordinates and scale from a given key.
#define KEY_SCALE_LIMIT
Definition: key_util.hpp:23
bool is_siblings(const se::key_t sibling_1_key, const se::key_t sibling_2_key)
Verify if two keys encode sibling nodes/voxels.
se::code_t block_code(const se::key_t key, const se::scale_t max_block_scale)
Removes the voxel position detail within a block from a code.
bool is_valid(const se::key_t key, const se::scale_t limit=KEY_SCALE_LIMIT)
Verify if a key is valid.
bool code_at_scale(const se::key_t key, const se::scale_t scale, se::code_t &code_at_scale)
For a given key, reduce detail from Morton code up to given a scale.
constexpr uint64_t SCALE_MASK
11 111
Definition: key_util.hpp:60
scale_t key_to_scale(const se::key_t key)
Reduce a key to only its scale.
idx_t code_to_child_idx(const se::code_t code, const scale_t scale)
Extracts the child index from a Morton code for a given scale.
bool is_child(const se::key_t parent_key, const se::key_t child_key)
Verify if a key is a child of a different key.
void neighbours(const se::key_t key, std::array< se::key_t, 26 > neighbour_keys)
TODO: 26-connectivity.
void siblings(const se::key_t key, std::array< se::key_t, 8 > sibling_keys)
TODO: 4 siblings (includes key)
Eigen::Vector3i key_to_coord(const se::key_t key)
Reduce a key to only its Morton code.
void decode_code(const se::code_t code, Eigen::Vector3i &coord)
Compute the x,y,z coordinates for a given Morton code.
se::key_t compact(uint64_t value)
Compresses a value E.g.
Helper wrapper to allocate and de-allocate octants in the octree.
Definition: colour_utils.hpp:17
static const std::vector< Eigen::Vector3f, Eigen::aligned_allocator< Eigen::Vector3f > > scale
The colours used for the various integration scales.
Definition: colour_utils.hpp:22