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>
70 unsigned int max_voxel_index_arg)
72 unsigned int tree_depth;
74 assert(max_voxel_index_arg > 0);
78 std::min(
static_cast<unsigned int>(OctreeKey::maxDepth),
79 static_cast<unsigned int>(std::ceil(std::log2(max_voxel_index_arg))));
82 depth_mask_ = (1 << (tree_depth - 1));
86 template <
typename LeafContainerT,
typename BranchContainerT>
90 assert(depth_arg > 0);
93 octree_depth_ = depth_arg;
96 depth_mask_ = (1 << (depth_arg - 1));
99 max_key_.x = max_key_.y = max_key_.z = (1 << depth_arg) - 1;
103 template <
typename LeafContainerT,
typename BranchContainerT>
106 unsigned int idx_y_arg,
107 unsigned int idx_z_arg)
110 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
113 return (findLeaf(key));
117 template <
typename LeafContainerT,
typename BranchContainerT>
120 unsigned int idx_y_arg,
121 unsigned int idx_z_arg)
124 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
127 return (createLeaf(key));
131 template <
typename LeafContainerT,
typename BranchContainerT>
134 unsigned int idx_y_arg,
135 unsigned int idx_z_arg)
const
138 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
141 return (existLeaf(key));
145 template <
typename LeafContainerT,
typename BranchContainerT>
148 unsigned int idx_y_arg,
149 unsigned int idx_z_arg)
152 OctreeKey key(idx_x_arg, idx_y_arg, idx_z_arg);
155 deleteLeafRecursive(key, depth_mask_, root_node_);
159 template <
typename LeafContainerT,
typename BranchContainerT>
166 deleteBranch(*root_node_);
173 template <
typename LeafContainerT,
typename BranchContainerT>
176 std::vector<char>& binary_tree_out_arg)
182 binary_tree_out_arg.clear();
183 binary_tree_out_arg.reserve(this->branch_count_);
185 serializeTreeRecursive(root_node_, new_key, &binary_tree_out_arg,
nullptr);
189 template <
typename LeafContainerT,
typename BranchContainerT>
192 std::vector<char>& binary_tree_out_arg,
193 std::vector<LeafContainerT*>& leaf_container_vector_arg)
199 binary_tree_out_arg.clear();
200 leaf_container_vector_arg.clear();
202 binary_tree_out_arg.reserve(this->branch_count_);
203 leaf_container_vector_arg.reserve(this->leaf_count_);
205 serializeTreeRecursive(
206 root_node_, new_key, &binary_tree_out_arg, &leaf_container_vector_arg);
210 template <
typename LeafContainerT,
typename BranchContainerT>
213 std::vector<LeafContainerT*>& leaf_container_vector_arg)
218 leaf_container_vector_arg.clear();
220 leaf_container_vector_arg.reserve(this->leaf_count_);
222 serializeTreeRecursive(root_node_, new_key,
nullptr, &leaf_container_vector_arg);
226 template <
typename LeafContainerT,
typename BranchContainerT>
229 std::vector<char>& binary_tree_out_arg)
237 std::vector<char>::const_iterator binary_tree_out_it = binary_tree_out_arg.begin();
238 std::vector<char>::const_iterator binary_tree_out_it_end = binary_tree_out_arg.end();
240 deserializeTreeRecursive(root_node_,
244 binary_tree_out_it_end,
250 template <
typename LeafContainerT,
typename BranchContainerT>
253 std::vector<char>& binary_tree_in_arg,
254 std::vector<LeafContainerT*>& leaf_container_vector_arg)
259 typename std::vector<LeafContainerT*>::const_iterator leaf_vector_it =
260 leaf_container_vector_arg.begin();
263 typename std::vector<LeafContainerT*>::const_iterator leaf_vector_it_end =
264 leaf_container_vector_arg.end();
270 std::vector<char>::const_iterator binary_tree_input_it = binary_tree_in_arg.begin();
271 std::vector<char>::const_iterator binary_tree_input_it_end = binary_tree_in_arg.end();
273 deserializeTreeRecursive(root_node_,
276 binary_tree_input_it,
277 binary_tree_input_it_end,
279 &leaf_vector_it_end);
283 template <
typename LeafContainerT,
typename BranchContainerT>
287 unsigned int depth_mask_arg,
293 unsigned char child_idx;
298 OctreeNode* child_node = (*branch_arg)[child_idx];
301 if ((!dynamic_depth_enabled_) && (depth_mask_arg > 1)) {
303 BranchNode* childBranch = createBranchChild(*branch_arg, child_idx);
308 return createLeafRecursive(key_arg,
315 LeafNode* leaf_node = createLeafChild(*branch_arg, child_idx);
316 return_leaf_arg = leaf_node;
317 parent_of_leaf_arg = branch_arg;
325 return createLeafRecursive(key_arg,
333 return_leaf_arg =
static_cast<LeafNode*
>(child_node);
334 parent_of_leaf_arg = branch_arg;
339 return (depth_mask_arg >> 1);
343 template <
typename LeafContainerT,
typename BranchContainerT>
347 unsigned int depth_mask_arg,
349 LeafContainerT*& result_arg)
const
352 unsigned char child_idx;
357 OctreeNode* child_node = (*branch_arg)[child_idx];
364 child_branch =
static_cast<BranchNode*
>(child_node);
366 findLeafRecursive(key_arg, depth_mask_arg / 2, child_branch, result_arg);
372 child_leaf =
static_cast<LeafNode*
>(child_node);
381 template <
typename LeafContainerT,
typename BranchContainerT>
387 unsigned char child_idx;
394 OctreeNode* child_node = (*branch_arg)[child_idx];
401 child_branch =
static_cast<BranchNode*
>(child_node);
404 b_no_children = deleteLeafRecursive(key_arg, depth_mask_arg / 2, child_branch);
406 if (!b_no_children) {
408 deleteBranchChild(*branch_arg, child_idx);
417 deleteBranchChild(*branch_arg, child_idx);
424 b_no_children =
false;
425 for (child_idx = 0; (!b_no_children) && (child_idx < 8); child_idx++) {
426 b_no_children = branch_arg->
hasChild(child_idx);
429 return (b_no_children);
433 template <
typename LeafContainerT,
typename BranchContainerT>
438 std::vector<char>* binary_tree_out_arg,
439 typename std::vector<LeafContainerT*>* leaf_container_vector_arg)
const
441 char node_bit_pattern;
444 node_bit_pattern = getBranchBitPattern(*branch_arg);
447 if (binary_tree_out_arg)
448 binary_tree_out_arg->push_back(node_bit_pattern);
451 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
454 if (branch_arg->
hasChild(child_idx)) {
463 serializeTreeRecursive(
static_cast<const BranchNode*
>(childNode),
466 leaf_container_vector_arg);
472 if (leaf_container_vector_arg)
476 serializeTreeCallback(**child_leaf, key_arg);
490 template <
typename LeafContainerT,
typename BranchContainerT>
494 unsigned int depth_mask_arg,
496 typename std::vector<char>::const_iterator& binary_tree_input_it_arg,
497 typename std::vector<char>::const_iterator& binary_tree_input_it_end_arg,
498 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_arg,
499 typename std::vector<LeafContainerT*>::const_iterator*
500 leaf_container_vector_it_end_arg)
503 if (binary_tree_input_it_arg != binary_tree_input_it_end_arg) {
505 char node_bits = (*binary_tree_input_it_arg);
506 binary_tree_input_it_arg++;
509 for (
unsigned char child_idx = 0; child_idx < 8; child_idx++) {
511 if (node_bits & (1 << child_idx)) {
515 if (depth_mask_arg > 1) {
517 BranchNode* newBranch = createBranchChild(*branch_arg, child_idx);
522 deserializeTreeRecursive(newBranch,
525 binary_tree_input_it_arg,
526 binary_tree_input_it_end_arg,
527 leaf_container_vector_it_arg,
528 leaf_container_vector_it_end_arg);
533 LeafNode* child_leaf = createLeafChild(*branch_arg, child_idx);
535 if (leaf_container_vector_it_arg &&
536 (*leaf_container_vector_it_arg != *leaf_container_vector_it_end_arg)) {
537 LeafContainerT& container = **child_leaf;
538 container = ***leaf_container_vector_it_arg;
539 ++*leaf_container_vector_it_arg;
545 deserializeTreeCallback(**child_leaf, key_arg);
558 #define PCL_INSTANTIATE_OctreeBase(T) \
559 template class PCL_EXPORTS pcl::octree::OctreeBase<T>;