39 #ifndef PCL_OCTREE_BASE_HPP
40 #define PCL_OCTREE_BASE_HPP
47 template <
typename LeafContainerT,
typename BranchContainerT>
54 , dynamic_depth_enabled_(false)
58 template <
typename LeafContainerT,
typename BranchContainerT>
67 template <
typename LeafContainerT,
typename BranchContainerT>
74 if (max_voxel_index_arg <= 0) {
75 PCL_ERROR(
"[pcl::octree::OctreeBase::setMaxVoxelIndex] Max voxel index (%lu) must "
84 static_cast<uindex_t>(std::ceil(std::log2(max_voxel_index_arg))));
86 setTreeDepth(tree_depth);
90 template <
typename LeafContainerT,
typename BranchContainerT>
95 PCL_ERROR(
"[pcl::octree::OctreeBase::setTreeDepth] Tree depth (%lu) must be > 0!\n",
100 PCL_ERROR(
"[pcl::octree::OctreeBase::setTreeDepth] Tree depth (%lu) must be <= max "
108 octree_depth_ = depth_arg;
111 depth_mask_ = (1 << (depth_arg - 1));
114 max_key_.x = max_key_.y = max_key_.z = (1 << depth_arg) - 1;
118 template <
typename LeafContainerT,
typename BranchContainerT>
125 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
128 return (findLeaf(key));
132 template <
typename LeafContainerT,
typename BranchContainerT>
139 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
142 return (createLeaf(key));
146 template <
typename LeafContainerT,
typename BranchContainerT>
153 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
156 return (existLeaf(key));
160 template <
typename LeafContainerT,
typename BranchContainerT>
167 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
170 deleteLeafRecursive(key, depth_mask_, root_node_);
174 template <
typename LeafContainerT,
typename BranchContainerT>
181 deleteBranch(*root_node_);
188 template <
typename LeafContainerT,
typename BranchContainerT>
191 std::vector<char>& binary_tree_out_arg)
const
197 binary_tree_out_arg.clear();
198 binary_tree_out_arg.reserve(this->branch_count_);
200 serializeTreeRecursive(root_node_, new_key, &binary_tree_out_arg,
nullptr);
204 template <
typename LeafContainerT,
typename BranchContainerT>
207 std::vector<char>& binary_tree_out_arg,
208 std::vector<LeafContainerT*>& leaf_container_vector_arg)
const
214 binary_tree_out_arg.clear();
215 leaf_container_vector_arg.clear();
217 binary_tree_out_arg.reserve(this->branch_count_);
218 leaf_container_vector_arg.reserve(this->leaf_count_);
220 serializeTreeRecursive(
221 root_node_, new_key, &binary_tree_out_arg, &leaf_container_vector_arg);
225 template <
typename LeafContainerT,
typename BranchContainerT>
228 std::vector<LeafContainerT*>& leaf_container_vector_arg)
233 leaf_container_vector_arg.clear();
235 leaf_container_vector_arg.reserve(this->leaf_count_);
237 serializeTreeRecursive(root_node_, new_key,
nullptr, &leaf_container_vector_arg);
241 template <
typename LeafContainerT,
typename BranchContainerT>
244 std::vector<char>& binary_tree_out_arg)
252 std::vector<char>::const_iterator binary_tree_out_it = binary_tree_out_arg.begin();
253 std::vector<char>::const_iterator binary_tree_out_it_end = binary_tree_out_arg.end();
255 deserializeTreeRecursive(root_node_,
259 binary_tree_out_it_end,
265 template <
typename LeafContainerT,
typename BranchContainerT>
268 std::vector<char>& binary_tree_in_arg,
269 std::vector<LeafContainerT*>& leaf_container_vector_arg)
274 typename std::vector<LeafContainerT*>::const_iterator leaf_vector_it =
275 leaf_container_vector_arg.begin();
278 typename std::vector<LeafContainerT*>::const_iterator leaf_vector_it_end =
279 leaf_container_vector_arg.end();
285 std::vector<char>::const_iterator binary_tree_input_it = binary_tree_in_arg.begin();
286 std::vector<char>::const_iterator binary_tree_input_it_end = binary_tree_in_arg.end();
288 deserializeTreeRecursive(root_node_,
291 binary_tree_input_it,
292 binary_tree_input_it_end,
294 &leaf_vector_it_end);
298 template <
typename LeafContainerT,
typename BranchContainerT>
308 unsigned char child_idx;
313 OctreeNode* child_node = (*branch_arg)[child_idx];
316 if ((!dynamic_depth_enabled_) && (depth_mask_arg > 1)) {
318 BranchNode* childBranch = createBranchChild(*branch_arg, child_idx);
323 return createLeafRecursive(key_arg,
330 LeafNode* leaf_node = createLeafChild(*branch_arg, child_idx);
331 return_leaf_arg = leaf_node;
332 parent_of_leaf_arg = branch_arg;
340 return createLeafRecursive(key_arg,
342 static_cast<BranchNode*
>(child_node),
348 return_leaf_arg =
static_cast<LeafNode*
>(child_node);
349 parent_of_leaf_arg = branch_arg;
354 return (depth_mask_arg >> 1);
358 template <
typename LeafContainerT,
typename BranchContainerT>
364 LeafContainerT*& result_arg)
const
367 unsigned char child_idx;
379 child_branch =
static_cast<BranchNode*
>(child_node);
381 findLeafRecursive(key_arg, depth_mask_arg >> 1, child_branch, result_arg);
387 child_leaf =
static_cast<LeafNode*
>(child_node);
396 template <
typename LeafContainerT,
typename BranchContainerT>
402 unsigned char child_idx;
409 OctreeNode* child_node = (*branch_arg)[child_idx];
416 child_branch =
static_cast<BranchNode*
>(child_node);
419 b_has_children = deleteLeafRecursive(key_arg, depth_mask_arg >> 1, child_branch);
421 if (!b_has_children) {
423 deleteBranchChild(*branch_arg, child_idx);
432 deleteBranchChild(*branch_arg, child_idx);
439 b_has_children =
false;
440 for (child_idx = 0; (!b_has_children) && (child_idx < 8); child_idx++) {
444 return (b_has_children);
448 template <
typename LeafContainerT,
typename BranchContainerT>
453 std::vector<char>* binary_tree_out_arg,
454 typename std::vector<LeafContainerT*>* leaf_container_vector_arg)
const
456 char node_bit_pattern;
459 node_bit_pattern = getBranchBitPattern(*branch_arg);
462 if (binary_tree_out_arg)
463 binary_tree_out_arg->push_back(node_bit_pattern);
466 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
469 if (branch_arg->
hasChild(child_idx)) {
478 serializeTreeRecursive(
static_cast<const BranchNode*
>(childNode),
481 leaf_container_vector_arg);
485 auto* child_leaf =
static_cast<LeafNode*
>(childNode);
487 if (leaf_container_vector_arg)
488 leaf_container_vector_arg->push_back(child_leaf->getContainerPtr());
491 serializeTreeCallback(**child_leaf, key_arg);
505 template <
typename LeafContainerT,
typename BranchContainerT>
511 typename std::vector<char>::const_iterator& binary_tree_input_it_arg,
512 typename std::vector<char>::const_iterator& binary_tree_input_it_end_arg,
513 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_arg,
514 typename std::vector<LeafContainerT*>::const_iterator*
515 leaf_container_vector_it_end_arg)
518 if (binary_tree_input_it_arg != binary_tree_input_it_end_arg) {
520 char node_bits = (*binary_tree_input_it_arg);
521 binary_tree_input_it_arg++;
524 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
526 if (node_bits & (1 << child_idx)) {
530 if (depth_mask_arg > 1) {
532 BranchNode* newBranch = createBranchChild(*branch_arg, child_idx);
537 deserializeTreeRecursive(newBranch,
540 binary_tree_input_it_arg,
541 binary_tree_input_it_end_arg,
542 leaf_container_vector_it_arg,
543 leaf_container_vector_it_end_arg);
548 LeafNode* child_leaf = createLeafChild(*branch_arg, child_idx);
550 if (leaf_container_vector_it_arg &&
551 (*leaf_container_vector_it_arg != *leaf_container_vector_it_end_arg)) {
552 LeafContainerT& container = **child_leaf;
553 LeafContainerT* src_container_ptr = **leaf_container_vector_it_arg;
554 container = *src_container_ptr;
555 ++*leaf_container_vector_it_arg;
561 deserializeTreeCallback(**child_leaf, key_arg);
574 #define PCL_INSTANTIATE_OctreeBase(T) \
575 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.