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 std::vector<char>::const_iterator binary_tree_out_it = binary_tree_out_arg.begin();
248 std::vector<char>::const_iterator binary_tree_out_it_end = binary_tree_out_arg.end();
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 typename std::vector<LeafContainerT*>::const_iterator leaf_vector_it =
270 leaf_container_vector_arg.begin();
273 typename std::vector<LeafContainerT*>::const_iterator leaf_vector_it_end =
274 leaf_container_vector_arg.end();
280 std::vector<char>::const_iterator binary_tree_input_it = binary_tree_in_arg.begin();
281 std::vector<char>::const_iterator binary_tree_input_it_end = binary_tree_in_arg.end();
283 deserializeTreeRecursive(root_node_,
286 binary_tree_input_it,
287 binary_tree_input_it_end,
289 &leaf_vector_it_end);
293 template <
typename LeafContainerT,
typename BranchContainerT>
303 unsigned char child_idx;
308 OctreeNode* child_node = (*branch_arg)[child_idx];
311 if ((!dynamic_depth_enabled_) && (depth_mask_arg > 1)) {
313 BranchNode* childBranch = createBranchChild(*branch_arg, child_idx);
318 return createLeafRecursive(key_arg,
325 LeafNode* leaf_node = createLeafChild(*branch_arg, child_idx);
326 return_leaf_arg = leaf_node;
327 parent_of_leaf_arg = branch_arg;
335 return createLeafRecursive(key_arg,
337 static_cast<BranchNode*
>(child_node),
343 return_leaf_arg =
static_cast<LeafNode*
>(child_node);
344 parent_of_leaf_arg = branch_arg;
349 return (depth_mask_arg >> 1);
353 template <
typename LeafContainerT,
typename BranchContainerT>
359 LeafContainerT*& result_arg)
const
362 unsigned char child_idx;
367 OctreeNode* child_node = (*branch_arg)[child_idx];
374 child_branch =
static_cast<BranchNode*
>(child_node);
376 findLeafRecursive(key_arg, depth_mask_arg >> 1, child_branch, result_arg);
391 template <
typename LeafContainerT,
typename BranchContainerT>
397 unsigned char child_idx;
404 OctreeNode* child_node = (*branch_arg)[child_idx];
411 child_branch =
static_cast<BranchNode*
>(child_node);
414 b_has_children = deleteLeafRecursive(key_arg, depth_mask_arg >> 1, child_branch);
416 if (!b_has_children) {
418 deleteBranchChild(*branch_arg, child_idx);
427 deleteBranchChild(*branch_arg, child_idx);
434 b_has_children =
false;
435 for (child_idx = 0; (!b_has_children) && (child_idx < 8); child_idx++) {
436 b_has_children = branch_arg->
hasChild(child_idx);
439 return (b_has_children);
443 template <
typename LeafContainerT,
typename BranchContainerT>
448 std::vector<char>* binary_tree_out_arg,
449 typename std::vector<LeafContainerT*>* leaf_container_vector_arg)
const
451 char node_bit_pattern;
454 node_bit_pattern = getBranchBitPattern(*branch_arg);
457 if (binary_tree_out_arg)
458 binary_tree_out_arg->push_back(node_bit_pattern);
461 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
464 if (branch_arg->
hasChild(child_idx)) {
473 serializeTreeRecursive(
static_cast<const BranchNode*
>(childNode),
476 leaf_container_vector_arg);
480 auto* child_leaf =
static_cast<LeafNode*
>(childNode);
482 if (leaf_container_vector_arg)
483 leaf_container_vector_arg->push_back(child_leaf->getContainerPtr());
486 serializeTreeCallback(**child_leaf, key_arg);
500 template <
typename LeafContainerT,
typename BranchContainerT>
506 typename std::vector<char>::const_iterator& binary_tree_input_it_arg,
507 typename std::vector<char>::const_iterator& binary_tree_input_it_end_arg,
508 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_arg,
509 typename std::vector<LeafContainerT*>::const_iterator*
510 leaf_container_vector_it_end_arg)
513 if (binary_tree_input_it_arg != binary_tree_input_it_end_arg) {
515 char node_bits = (*binary_tree_input_it_arg);
516 binary_tree_input_it_arg++;
519 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
521 if (node_bits & (1 << child_idx)) {
525 if (depth_mask_arg > 1) {
527 BranchNode* newBranch = createBranchChild(*branch_arg, child_idx);
532 deserializeTreeRecursive(newBranch,
535 binary_tree_input_it_arg,
536 binary_tree_input_it_end_arg,
537 leaf_container_vector_it_arg,
538 leaf_container_vector_it_end_arg);
543 LeafNode* child_leaf = createLeafChild(*branch_arg, child_idx);
545 if (leaf_container_vector_it_arg &&
546 (*leaf_container_vector_it_arg != *leaf_container_vector_it_end_arg)) {
547 LeafContainerT& container = **child_leaf;
548 LeafContainerT* src_container_ptr = **leaf_container_vector_it_arg;
549 container = *src_container_ptr;
550 ++*leaf_container_vector_it_arg;
556 deserializeTreeCallback(**child_leaf, key_arg);
569 #define PCL_INSTANTIATE_OctreeBase(T) \
570 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.