39 #ifndef PCL_OCTREE_BASE_HPP
40 #define PCL_OCTREE_BASE_HPP
47 template <
typename LeafContainerT,
typename BranchContainerT>
53 template <
typename LeafContainerT,
typename BranchContainerT>
62 template <
typename LeafContainerT,
typename BranchContainerT>
69 if (max_voxel_index_arg <= 0) {
70 PCL_ERROR(
"[pcl::octree::OctreeBase::setMaxVoxelIndex] Max voxel index (%lu) must "
79 static_cast<uindex_t>(std::ceil(std::log2(max_voxel_index_arg))));
81 setTreeDepth(tree_depth);
85 template <
typename LeafContainerT,
typename BranchContainerT>
90 PCL_ERROR(
"[pcl::octree::OctreeBase::setTreeDepth] Tree depth (%lu) must be > 0!\n",
95 PCL_ERROR(
"[pcl::octree::OctreeBase::setTreeDepth] Tree depth (%lu) must be <= max "
103 octree_depth_ = depth_arg;
106 depth_mask_ = (1 << (depth_arg - 1));
109 max_key_.x = max_key_.y = max_key_.z = (1 << depth_arg) - 1;
113 template <
typename LeafContainerT,
typename BranchContainerT>
120 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
123 return (findLeaf(key));
127 template <
typename LeafContainerT,
typename BranchContainerT>
134 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
137 return (createLeaf(key));
141 template <
typename LeafContainerT,
typename BranchContainerT>
148 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
151 return (existLeaf(key));
155 template <
typename LeafContainerT,
typename BranchContainerT>
162 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
165 deleteLeafRecursive(key, depth_mask_, root_node_);
169 template <
typename LeafContainerT,
typename BranchContainerT>
176 deleteBranch(*root_node_);
183 template <
typename LeafContainerT,
typename BranchContainerT>
186 std::vector<char>& binary_tree_out_arg)
const
192 binary_tree_out_arg.clear();
193 binary_tree_out_arg.reserve(this->branch_count_);
195 serializeTreeRecursive(root_node_, new_key, &binary_tree_out_arg,
nullptr);
199 template <
typename LeafContainerT,
typename BranchContainerT>
202 std::vector<char>& binary_tree_out_arg,
203 std::vector<LeafContainerT*>& leaf_container_vector_arg)
const
209 binary_tree_out_arg.clear();
210 leaf_container_vector_arg.clear();
212 binary_tree_out_arg.reserve(this->branch_count_);
213 leaf_container_vector_arg.reserve(this->leaf_count_);
215 serializeTreeRecursive(
216 root_node_, new_key, &binary_tree_out_arg, &leaf_container_vector_arg);
220 template <
typename LeafContainerT,
typename BranchContainerT>
223 std::vector<LeafContainerT*>& leaf_container_vector_arg)
228 leaf_container_vector_arg.clear();
230 leaf_container_vector_arg.reserve(this->leaf_count_);
232 serializeTreeRecursive(root_node_, new_key,
nullptr, &leaf_container_vector_arg);
236 template <
typename LeafContainerT,
typename BranchContainerT>
239 std::vector<char>& binary_tree_out_arg)
247 auto binary_tree_out_it = binary_tree_out_arg.cbegin();
248 auto binary_tree_out_it_end = binary_tree_out_arg.cend();
250 deserializeTreeRecursive(root_node_,
254 binary_tree_out_it_end,
260 template <
typename LeafContainerT,
typename BranchContainerT>
263 std::vector<char>& binary_tree_in_arg,
264 std::vector<LeafContainerT*>& leaf_container_vector_arg)
269 auto leaf_vector_it = leaf_container_vector_arg.cbegin();
272 auto leaf_vector_it_end = leaf_container_vector_arg.cend();
278 auto binary_tree_input_it = binary_tree_in_arg.cbegin();
279 auto binary_tree_input_it_end = binary_tree_in_arg.cend();
281 deserializeTreeRecursive(root_node_,
284 binary_tree_input_it,
285 binary_tree_input_it_end,
287 &leaf_vector_it_end);
291 template <
typename LeafContainerT,
typename BranchContainerT>
301 unsigned char child_idx;
306 OctreeNode* child_node = (*branch_arg)[child_idx];
309 if ((!dynamic_depth_enabled_) && (depth_mask_arg > 1)) {
311 BranchNode* childBranch = createBranchChild(*branch_arg, child_idx);
316 return createLeafRecursive(key_arg,
323 LeafNode* leaf_node = createLeafChild(*branch_arg, child_idx);
324 return_leaf_arg = leaf_node;
325 parent_of_leaf_arg = branch_arg;
333 return createLeafRecursive(key_arg,
335 static_cast<BranchNode*
>(child_node),
341 return_leaf_arg =
static_cast<LeafNode*
>(child_node);
342 parent_of_leaf_arg = branch_arg;
347 return (depth_mask_arg >> 1);
351 template <
typename LeafContainerT,
typename BranchContainerT>
357 LeafContainerT*& result_arg)
const
360 unsigned char child_idx;
365 OctreeNode* child_node = (*branch_arg)[child_idx];
374 findLeafRecursive(key_arg, depth_mask_arg >> 1, child_branch, result_arg);
380 child_leaf =
static_cast<LeafNode*
>(child_node);
389 template <
typename LeafContainerT,
typename BranchContainerT>
395 unsigned char child_idx;
402 OctreeNode* child_node = (*branch_arg)[child_idx];
409 child_branch =
static_cast<BranchNode*
>(child_node);
412 b_has_children = deleteLeafRecursive(key_arg, depth_mask_arg >> 1, child_branch);
414 if (!b_has_children) {
416 deleteBranchChild(*branch_arg, child_idx);
425 deleteBranchChild(*branch_arg, child_idx);
432 b_has_children =
false;
433 for (child_idx = 0; (!b_has_children) && (child_idx < 8); child_idx++) {
434 b_has_children = branch_arg->
hasChild(child_idx);
437 return (b_has_children);
441 template <
typename LeafContainerT,
typename BranchContainerT>
446 std::vector<char>* binary_tree_out_arg,
447 typename std::vector<LeafContainerT*>* leaf_container_vector_arg)
const
449 char node_bit_pattern;
452 node_bit_pattern = getBranchBitPattern(*branch_arg);
455 if (binary_tree_out_arg)
456 binary_tree_out_arg->push_back(node_bit_pattern);
459 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
462 if (branch_arg->
hasChild(child_idx)) {
471 serializeTreeRecursive(
static_cast<const BranchNode*
>(childNode),
474 leaf_container_vector_arg);
478 auto* child_leaf =
static_cast<LeafNode*
>(childNode);
480 if (leaf_container_vector_arg)
481 leaf_container_vector_arg->push_back(child_leaf->getContainerPtr());
484 serializeTreeCallback(**child_leaf, key_arg);
498 template <
typename LeafContainerT,
typename BranchContainerT>
504 typename std::vector<char>::const_iterator& binary_tree_input_it_arg,
505 typename std::vector<char>::const_iterator& binary_tree_input_it_end_arg,
506 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_arg,
507 typename std::vector<LeafContainerT*>::const_iterator*
508 leaf_container_vector_it_end_arg)
511 if (binary_tree_input_it_arg != binary_tree_input_it_end_arg) {
513 char node_bits = (*binary_tree_input_it_arg);
514 binary_tree_input_it_arg++;
517 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
519 if (node_bits & (1 << child_idx)) {
523 if (depth_mask_arg > 1) {
525 BranchNode* newBranch = createBranchChild(*branch_arg, child_idx);
530 deserializeTreeRecursive(newBranch,
533 binary_tree_input_it_arg,
534 binary_tree_input_it_end_arg,
535 leaf_container_vector_it_arg,
536 leaf_container_vector_it_end_arg);
541 LeafNode* child_leaf = createLeafChild(*branch_arg, child_idx);
543 if (leaf_container_vector_it_arg &&
544 (*leaf_container_vector_it_arg != *leaf_container_vector_it_end_arg)) {
545 LeafContainerT& container = **child_leaf;
546 LeafContainerT* src_container_ptr = **leaf_container_vector_it_arg;
547 container = *src_container_ptr;
548 ++*leaf_container_vector_it_arg;
554 deserializeTreeCallback(**child_leaf, key_arg);
567 #define PCL_INSTANTIATE_OctreeBase(T) \
568 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
static const unsigned char maxDepth
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
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.