39 #ifndef PCL_OCTREE_BASE_HPP
40 #define PCL_OCTREE_BASE_HPP
42 #include <pcl/octree/octree_base.h>
43 #include <pcl/octree/octree_key.h>
50 template <
typename LeafContainerT,
typename BranchContainerT>
56 template <
typename LeafContainerT,
typename BranchContainerT>
65 template <
typename LeafContainerT,
typename BranchContainerT>
72 if (max_voxel_index_arg <= 0) {
73 PCL_ERROR(
"[pcl::octree::OctreeBase::setMaxVoxelIndex] Max voxel index (%lu) must "
82 static_cast<uindex_t>(std::ceil(std::log2(max_voxel_index_arg))));
84 setTreeDepth(tree_depth);
88 template <
typename LeafContainerT,
typename BranchContainerT>
93 PCL_ERROR(
"[pcl::octree::OctreeBase::setTreeDepth] Tree depth (%lu) must be > 0!\n",
98 PCL_ERROR(
"[pcl::octree::OctreeBase::setTreeDepth] Tree depth (%lu) must be <= max "
106 octree_depth_ = depth_arg;
109 depth_mask_ = (1 << (depth_arg - 1));
112 max_key_.x = max_key_.y = max_key_.z = (1 << depth_arg) - 1;
116 template <
typename LeafContainerT,
typename BranchContainerT>
123 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
126 return (findLeaf(key));
130 template <
typename LeafContainerT,
typename BranchContainerT>
137 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
140 return (createLeaf(key));
144 template <
typename LeafContainerT,
typename BranchContainerT>
151 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
154 return (existLeaf(key));
158 template <
typename LeafContainerT,
typename BranchContainerT>
165 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
168 deleteLeafRecursive(key, depth_mask_, root_node_);
172 template <
typename LeafContainerT,
typename BranchContainerT>
179 deleteBranch(*root_node_);
186 template <
typename LeafContainerT,
typename BranchContainerT>
189 std::vector<char>& binary_tree_out_arg)
const
195 binary_tree_out_arg.clear();
196 binary_tree_out_arg.reserve(this->branch_count_);
198 serializeTreeRecursive(root_node_, new_key, &binary_tree_out_arg,
nullptr);
202 template <
typename LeafContainerT,
typename BranchContainerT>
205 std::vector<char>& binary_tree_out_arg,
206 std::vector<LeafContainerT*>& leaf_container_vector_arg)
const
212 binary_tree_out_arg.clear();
213 leaf_container_vector_arg.clear();
215 binary_tree_out_arg.reserve(this->branch_count_);
216 leaf_container_vector_arg.reserve(this->leaf_count_);
218 serializeTreeRecursive(
219 root_node_, new_key, &binary_tree_out_arg, &leaf_container_vector_arg);
223 template <
typename LeafContainerT,
typename BranchContainerT>
226 std::vector<LeafContainerT*>& leaf_container_vector_arg)
231 leaf_container_vector_arg.clear();
233 leaf_container_vector_arg.reserve(this->leaf_count_);
235 serializeTreeRecursive(root_node_, new_key,
nullptr, &leaf_container_vector_arg);
239 template <
typename LeafContainerT,
typename BranchContainerT>
242 std::vector<char>& binary_tree_out_arg)
250 auto binary_tree_out_it = binary_tree_out_arg.cbegin();
251 auto binary_tree_out_it_end = binary_tree_out_arg.cend();
253 deserializeTreeRecursive(root_node_,
257 binary_tree_out_it_end,
263 template <
typename LeafContainerT,
typename BranchContainerT>
266 std::vector<char>& binary_tree_in_arg,
267 std::vector<LeafContainerT*>& leaf_container_vector_arg)
272 auto leaf_vector_it = leaf_container_vector_arg.cbegin();
275 auto leaf_vector_it_end = leaf_container_vector_arg.cend();
281 auto binary_tree_input_it = binary_tree_in_arg.cbegin();
282 auto binary_tree_input_it_end = binary_tree_in_arg.cend();
284 deserializeTreeRecursive(root_node_,
287 binary_tree_input_it,
288 binary_tree_input_it_end,
290 &leaf_vector_it_end);
294 template <
typename LeafContainerT,
typename BranchContainerT>
304 unsigned char child_idx;
309 OctreeNode* child_node = (*branch_arg)[child_idx];
312 if ((!dynamic_depth_enabled_) && (depth_mask_arg > 1)) {
314 BranchNode* childBranch = createBranchChild(*branch_arg, child_idx);
319 return createLeafRecursive(key_arg,
326 LeafNode* leaf_node = createLeafChild(*branch_arg, child_idx);
327 return_leaf_arg = leaf_node;
328 parent_of_leaf_arg = branch_arg;
336 return createLeafRecursive(key_arg,
338 static_cast<BranchNode*
>(child_node),
344 return_leaf_arg =
static_cast<LeafNode*
>(child_node);
345 parent_of_leaf_arg = branch_arg;
350 return (depth_mask_arg >> 1);
354 template <
typename LeafContainerT,
typename BranchContainerT>
360 LeafContainerT*& result_arg)
const
363 unsigned char child_idx;
368 OctreeNode* child_node = (*branch_arg)[child_idx];
375 child_branch =
static_cast<BranchNode*
>(child_node);
377 findLeafRecursive(key_arg, depth_mask_arg >> 1, child_branch, result_arg);
383 child_leaf =
static_cast<LeafNode*
>(child_node);
392 template <
typename LeafContainerT,
typename BranchContainerT>
398 unsigned char child_idx;
405 OctreeNode* child_node = (*branch_arg)[child_idx];
412 child_branch =
static_cast<BranchNode*
>(child_node);
415 b_has_children = deleteLeafRecursive(key_arg, depth_mask_arg >> 1, child_branch);
417 if (!b_has_children) {
419 deleteBranchChild(*branch_arg, child_idx);
428 deleteBranchChild(*branch_arg, child_idx);
435 b_has_children =
false;
436 for (child_idx = 0; (!b_has_children) && (child_idx < 8); child_idx++) {
437 b_has_children = branch_arg->
hasChild(child_idx);
440 return (b_has_children);
444 template <
typename LeafContainerT,
typename BranchContainerT>
449 std::vector<char>* binary_tree_out_arg,
450 typename std::vector<LeafContainerT*>* leaf_container_vector_arg)
const
452 char node_bit_pattern;
455 node_bit_pattern = getBranchBitPattern(*branch_arg);
458 if (binary_tree_out_arg)
459 binary_tree_out_arg->push_back(node_bit_pattern);
462 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
465 if (branch_arg->
hasChild(child_idx)) {
474 serializeTreeRecursive(
static_cast<const BranchNode*
>(childNode),
477 leaf_container_vector_arg);
481 auto* child_leaf =
static_cast<LeafNode*
>(childNode);
483 if (leaf_container_vector_arg)
484 leaf_container_vector_arg->push_back(child_leaf->getContainerPtr());
487 serializeTreeCallback(**child_leaf, key_arg);
501 template <
typename LeafContainerT,
typename BranchContainerT>
507 typename std::vector<char>::const_iterator& binary_tree_input_it_arg,
508 typename std::vector<char>::const_iterator& binary_tree_input_it_end_arg,
509 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_arg,
510 typename std::vector<LeafContainerT*>::const_iterator*
511 leaf_container_vector_it_end_arg)
514 if (binary_tree_input_it_arg != binary_tree_input_it_end_arg) {
516 char node_bits = (*binary_tree_input_it_arg);
517 binary_tree_input_it_arg++;
520 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
522 if (node_bits & (1 << child_idx)) {
526 if (depth_mask_arg > 1) {
528 BranchNode* newBranch = createBranchChild(*branch_arg, child_idx);
533 deserializeTreeRecursive(newBranch,
536 binary_tree_input_it_arg,
537 binary_tree_input_it_end_arg,
538 leaf_container_vector_it_arg,
539 leaf_container_vector_it_end_arg);
544 LeafNode* child_leaf = createLeafChild(*branch_arg, child_idx);
546 if (leaf_container_vector_it_arg &&
547 (*leaf_container_vector_it_arg != *leaf_container_vector_it_end_arg)) {
548 LeafContainerT& container = **child_leaf;
549 LeafContainerT* src_container_ptr = **leaf_container_vector_it_arg;
550 container = *src_container_ptr;
551 ++*leaf_container_vector_it_arg;
557 deserializeTreeCallback(**child_leaf, key_arg);
570 #define PCL_INSTANTIATE_OctreeBase(T) \
571 template class PCL_EXPORTS pcl::octree::OctreeBase<T>;
void findLeafRecursive(const OctreeKey &key_arg, uindex_t depth_mask_arg, BranchNode *branch_arg, LeafContainerT *&result_arg) const
Recursively search for a given leaf node and return a pointer.
void setTreeDepth(uindex_t max_depth_arg)
Set the maximum depth of the octree.
void deserializeTreeRecursive(BranchNode *branch_arg, uindex_t depth_mask_arg, OctreeKey &key_arg, typename std::vector< char >::const_iterator &binary_tree_input_it_arg, typename std::vector< char >::const_iterator &binary_tree_input_it_end_arg, typename std::vector< LeafContainerT * >::const_iterator *leaf_container_vector_it_arg, typename std::vector< LeafContainerT * >::const_iterator *leaf_container_vector_it_end_arg)
Recursive method for deserializing octree structure.
void serializeLeafs(std::vector< LeafContainerT * > &leaf_container_vector_arg)
Outputs a vector of all LeafContainerT elements that are stored within the octree leaf nodes.
LeafContainerT * createLeaf(uindex_t idx_x_arg, uindex_t idx_y_arg, uindex_t idx_z_arg)
Create new leaf node at (idx_x_arg, idx_y_arg, idx_z_arg).
virtual ~OctreeBase()
Empty deconstructor.
bool deleteLeafRecursive(const OctreeKey &key_arg, uindex_t depth_mask_arg, BranchNode *branch_arg)
Recursively search and delete leaf node.
uindex_t createLeafRecursive(const OctreeKey &key_arg, uindex_t depth_mask_arg, BranchNode *branch_arg, LeafNode *&return_leaf_arg, BranchNode *&parent_of_leaf_arg)
Create a leaf node at octree key.
void deserializeTree(std::vector< char > &binary_tree_input_arg)
Deserialize a binary octree description vector and create a corresponding octree structure.
void deleteTree()
Delete the octree structure and its leaf nodes.
void serializeTree(std::vector< char > &binary_tree_out_arg) const
Serialize octree into a binary output vector describing its branch node structure.
bool existLeaf(uindex_t idx_x_arg, uindex_t idx_y_arg, uindex_t idx_z_arg) const
idx_x_arg for the existence of leaf node at (idx_x_arg, idx_y_arg, idx_z_arg).
void serializeTreeRecursive(const BranchNode *branch_arg, OctreeKey &key_arg, std::vector< char > *binary_tree_out_arg, typename std::vector< LeafContainerT * > *leaf_container_vector_arg) const
Recursively explore the octree and output binary octree description together with a vector of leaf no...
LeafContainerT * findLeaf(uindex_t idx_x_arg, uindex_t idx_y_arg, uindex_t idx_z_arg) const
Find leaf node at (idx_x_arg, idx_y_arg, idx_z_arg).
void removeLeaf(uindex_t idx_x_arg, uindex_t idx_y_arg, uindex_t idx_z_arg)
Remove leaf node at (idx_x_arg, idx_y_arg, idx_z_arg).
void setMaxVoxelIndex(uindex_t max_voxel_index_arg)
Set the maximum amount of voxels per dimension.
OctreeBase()
Empty constructor.
Abstract octree branch class
bool hasChild(unsigned char child_idx_arg) const
Check if branch is pointing to a particular child node.
OctreeNode * getChildPtr(unsigned char child_idx_arg) const
Get pointer to child.
void popBranch()
pop child node from octree key
void pushBranch(unsigned char childIndex)
push a child node to the octree key
unsigned char getChildIdxWithDepthMask(uindex_t depthMask) const
get child node index using depthMask
static unsigned char getMaxDepth()
Abstract octree leaf class
const ContainerT * getContainerPtr() const
Get const pointer to container.
Abstract octree node class
virtual node_type_t getNodeType() const =0
Pure virtual method for retrieving the type of octree node (branch or leaf)
detail::int_type_t< detail::index_type_size, false > uindex_t
Type used for an unsigned index in PCL.