39 #ifndef PCL_OCTREE_TREE_2BUF_BASE_H 40 #define PCL_OCTREE_TREE_2BUF_BASE_H 44 #include "octree_nodes.h" 45 #include "octree_container.h" 46 #include "octree_key.h" 47 #include "octree_iterator.h" 57 template<
typename ContainerT>
83 for (b = 0; b < 2; ++b)
84 for (i = 0; i < 8; ++i)
110 getChildPtr (
unsigned char buffer_arg,
unsigned char index_arg)
const 112 assert( (buffer_arg<2) && (index_arg<8));
121 inline void setChildPtr (
unsigned char buffer_arg,
unsigned char index_arg,
124 assert( (buffer_arg<2) && (index_arg<8));
133 inline bool hasChild (
unsigned char buffer_arg,
unsigned char index_arg)
const 135 assert( (buffer_arg<2) && (index_arg<8));
225 template<
typename LeafContainerT = int,
249 Iterator
begin(
unsigned int max_depth_arg = 0) {
return Iterator(
this, max_depth_arg);};
250 const Iterator end() {
return Iterator();};
255 LeafNodeIterator
leaf_begin(
unsigned int max_depth_arg = 0) {
return LeafNodeIterator(
this, max_depth_arg);};
256 const LeafNodeIterator
leaf_end() {
return LeafNodeIterator();};
261 DepthFirstIterator
depth_begin(
unsigned int maxDepth_arg = 0) {
return DepthFirstIterator(
this, maxDepth_arg);};
262 const DepthFirstIterator
depth_end() {
return DepthFirstIterator();};
267 BreadthFirstIterator
breadth_begin(
unsigned int max_depth_arg = 0) {
return BreadthFirstIterator(
this, max_depth_arg);};
268 const BreadthFirstIterator
breadth_end() {
return BreadthFirstIterator();};
279 leaf_count_ (source.leaf_count_),
280 branch_count_ (source.branch_count_),
281 root_node_ (new (BranchNode) (*(source.root_node_))),
282 depth_mask_ (source.depth_mask_),
283 max_key_ (source.max_key_),
284 buffer_selector_ (source.buffer_selector_),
285 tree_dirty_flag_ (source.tree_dirty_flag_),
286 octree_depth_ (source.octree_depth_),
287 dynamic_depth_enabled_(source.dynamic_depth_enabled_)
297 root_node_ =
new (BranchNode) (* (source.
root_node_));
311 setMaxVoxelIndex (
unsigned int max_voxel_index_arg);
317 setTreeDepth (
unsigned int depth_arg);
324 return this->octree_depth_;
335 createLeaf (
unsigned int idx_x_arg,
unsigned int idx_y_arg,
unsigned int idx_z_arg);
345 findLeaf (
unsigned int idx_x_arg,
unsigned int idx_y_arg,
unsigned int idx_z_arg);
354 existLeaf (
unsigned int idx_x_arg,
unsigned int idx_y_arg,
unsigned int idx_z_arg)
const;
362 removeLeaf (
unsigned int idx_x_arg,
unsigned int idx_y_arg,
unsigned int idx_z_arg);
369 return (leaf_count_);
377 return (branch_count_);
388 treeCleanUpRecursive (root_node_);
394 buffer_selector_ = !buffer_selector_;
395 treeCleanUpRecursive (root_node_);
408 serializeTree (std::vector<char>& binary_tree_out_arg,
409 bool do_XOR_encoding_arg =
false);
417 serializeTree (std::vector<char>& binary_tree_out_arg,
418 std::vector<LeafContainerT*>& leaf_container_vector_arg,
419 bool do_XOR_encoding_arg =
false);
425 serializeLeafs (std::vector<LeafContainerT*>& leaf_container_vector_arg);
431 serializeNewLeafs (std::vector<LeafContainerT*>& leaf_container_vector_arg);
438 deserializeTree (std::vector<char>& binary_tree_in_arg,
439 bool do_XOR_decoding_arg =
false);
447 deserializeTree (std::vector<char>& binary_tree_in_arg,
448 std::vector<LeafContainerT*>& leaf_container_vector_arg,
449 bool do_XOR_decoding_arg =
false);
461 return (this->root_node_);
468 inline LeafContainerT*
471 LeafContainerT* result = 0;
472 findLeafRecursive (key_arg, depth_mask_, root_node_, result);
481 inline LeafContainerT*
485 BranchNode* leaf_node_parent;
487 createLeafRecursive (key_arg, depth_mask_ ,root_node_, leaf_node, leaf_node_parent,
false);
500 return (findLeaf(key_arg) != 0);
508 if (key_arg <= max_key_)
510 deleteLeafRecursive (key_arg, depth_mask_, root_node_);
513 tree_dirty_flag_ =
true;
530 return (branch_arg.
getChildPtr(buffer_selector_, child_idx_arg) != 0);
540 unsigned char child_idx_arg)
const 542 return branch_arg.
getChildPtr(buffer_selector_, child_idx_arg);
553 branch_arg.
setChildPtr (buffer_selector_, child_idx_arg, new_child_arg);
567 for (i = 0; i < 8; i++)
570 node_bits |=
static_cast<char> ( (!!child) << i);
582 unsigned char bufferSelector_arg)
const 589 for (i = 0; i < 8; i++)
592 node_bits |=
static_cast<char> ( (!!child) << i);
603 const BranchNode& branch_arg)
const 609 node_bits[0] = node_bits[1] = 0;
611 for (i = 0; i < 8; i++)
616 node_bits[0] |=
static_cast<char> ( (!!childA) << i);
617 node_bits[1] |=
static_cast<char> ( (!!childB) << i);
620 return node_bits[0] ^ node_bits[1];
629 return (getBranchXORBitPattern (branch_arg) > 0);
638 unsigned char buffer_selector_arg,
639 unsigned char child_idx_arg)
641 if (branch_arg.
hasChild(buffer_selector_arg, child_idx_arg))
650 deleteBranch (*static_cast<BranchNode*> (branchChild));
653 delete (branchChild);
660 delete (branchChild);
668 branch_arg.
setChildPtr(buffer_selector_arg, child_idx_arg, 0);
678 deleteBranchChild(branch_arg, buffer_selector_, child_idx_arg);
689 for (i = 0; i < 8; i++)
695 deleteBranchChild (branch_arg, 0, i);
703 deleteBranchChild (branch_arg, 0, i);
704 deleteBranchChild (branch_arg, 1, i);
715 unsigned char child_idx_arg)
717 BranchNode* new_branch_child =
new BranchNode();
719 branch_arg.
setChildPtr (buffer_selector_, child_idx_arg,
720 static_cast<OctreeNode*> (new_branch_child));
722 return new_branch_child;
733 LeafNode* new_leaf_child =
new LeafNode();
735 branch_arg.
setChildPtr(buffer_selector_, child_idx_arg, new_leaf_child);
737 return new_leaf_child;
754 createLeafRecursive (
const OctreeKey& key_arg,
755 unsigned int depth_mask_arg,
756 BranchNode* branch_arg,
757 LeafNode*& return_leaf_arg,
758 BranchNode*& parent_of_leaf_arg,
759 bool branch_reset_arg =
false);
770 findLeafRecursive (
const OctreeKey& key_arg,
771 unsigned int depth_mask_arg,
772 BranchNode* branch_arg,
773 LeafContainerT*& result_arg)
const;
783 deleteLeafRecursive (
const OctreeKey& key_arg,
784 unsigned int depth_mask_arg,
785 BranchNode* branch_arg);
796 serializeTreeRecursive (BranchNode* branch_arg,
798 std::vector<char>* binary_tree_out_arg,
799 typename std::vector<LeafContainerT*>* leaf_container_vector_arg,
800 bool do_XOR_encoding_arg =
false,
801 bool new_leafs_filter_arg =
false);
815 deserializeTreeRecursive (BranchNode* branch_arg,
816 unsigned int depth_mask_arg,
818 typename std::vector<char>::const_iterator& binary_tree_in_it_arg,
819 typename std::vector<char>::const_iterator& binary_tree_in_it_end_arg,
820 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_arg,
821 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_end_arg,
822 bool branch_reset_arg =
false,
823 bool do_XOR_decoding_arg =
false);
852 treeCleanUpRecursive (BranchNode* branch_arg);
858 inline double Log2 (
double n_arg)
860 return log (n_arg) / log (2.0);
876 unsigned char mask = 1;
879 for (
int i = 0; i < 8; i++)
882 std::cout << ((data_arg & (mask << i)) ?
"1" :
"0");
884 std::cout << std::endl;
LeafContainerT * createLeaf(const OctreeKey &key_arg)
Create a leaf node.
double Log2(double n_arg)
Helper function to calculate the binary logarithm.
OctreeNode * getChildPtr(unsigned char buffer_arg, unsigned char index_arg) const
Get child pointer in current branch node.
LeafNodeIterator leaf_begin(unsigned int max_depth_arg=0)
const OctreeLeafNodeIterator< OctreeT > ConstLeafNodeIterator
virtual node_type_t getNodeType() const
Get the type of octree node.
bool octreeCanResize()
Test if octree is able to dynamically change its depth.
ContainerT * getContainerPtr()
Get pointer to container.
virtual ~BufferedBranchNode()
Empty constructor.
unsigned int octree_depth_
Octree depth.
Octree2BufBase(const Octree2BufBase &source)
Copy constructor.
const OctreeBreadthFirstIterator< OctreeT > ConstBreadthFirstIterator
char getBranchBitPattern(const BranchNode &branch_arg) const
Generate bit pattern reflecting the existence of child node pointers for current buffer.
virtual BufferedBranchNode * deepCopy() const
Method to perform a deep copy of the octree.
virtual node_type_t getNodeType() const =0
Pure virtual method for receiving the type of octree node (branch or leaf)
const DepthFirstIterator depth_end()
const ContainerT & operator*() const
Get const reference to container.
void setBranchChildPtr(BranchNode &branch_arg, unsigned char child_idx_arg, OctreeNode *new_child_arg)
Assign new child node to branch.
bool branchHasChild(const BranchNode &branch_arg, unsigned char child_idx_arg) const
Check if branch is pointing to a particular child node.
LeafNode * createLeafChild(BranchNode &branch_arg, unsigned char child_idx_arg)
Fetch and add a new leaf child to a branch class.
OctreeNode * getRootNode() const
Retrieve root node.
OctreeKey max_key_
key range
const OctreeDepthFirstIterator< OctreeT > ConstDepthFirstIterator
void deleteBranch(BranchNode &branch_arg)
Delete branch and all its subchilds from octree (both buffers)
LeafContainerT LeafContainer
Octree leaf node iterator class.
void deleteBranchChild(BranchNode &branch_arg, unsigned char child_idx_arg)
Delete child node and all its subchilds from octree in current buffer.
const OctreeDepthFirstIterator< OctreeT > ConstIterator
BranchContainerT BranchContainer
OctreeLeafNodeIterator< OctreeT > LeafNodeIterator
BreadthFirstIterator breadth_begin(unsigned int max_depth_arg=0)
void deleteBranchChild(BranchNode &branch_arg, unsigned char buffer_selector_arg, unsigned char child_idx_arg)
Delete child node and all its subchilds from octree in specific buffer.
bool dynamic_depth_enabled_
Enable dynamic_depth.
std::size_t getLeafCount() const
Return the amount of existing leafs in the octree.
virtual void deserializeTreeCallback(LeafContainerT &, const OctreeKey &)
Callback executed for every leaf node data during deserialization.
BufferedBranchNode()
Empty constructor.
const ContainerT & getContainer() const
Get const reference to container.
OctreeLeafNode< LeafContainerT > LeafNode
void deleteCurrentBuffer()
Delete the octree structure in the current buffer.
std::size_t branch_count_
Amount of branch nodes.
void deletePreviousBuffer()
Delete octree structure of previous buffer.
const BreadthFirstIterator breadth_end()
OctreeDepthFirstIterator< OctreeT > Iterator
OctreeNode * getBranchChildPtr(const BranchNode &branch_arg, unsigned char child_idx_arg) const
Retrieve a child node pointer for child node at child_idx.
char getBranchXORBitPattern(const BranchNode &branch_arg) const
Generate XOR bit pattern reflecting differences between the two octree buffers.
virtual OctreeNode * deepCopy() const =0
Pure virtual method to perform a deep copy of the octree.
Abstract octree leaf class
const ContainerT * operator->() const
Get const pointer to container.
const LeafNodeIterator leaf_end()
unsigned char buffer_selector_
Currently active octree buffer.
BufferedBranchNode< BranchContainerT > BranchNode
Iterator begin(unsigned int max_depth_arg=0)
void printBinary(char data_arg)
Prints binary representation of a byte - used for debugging.
const ContainerT * getContainerPtr() const
Get const pointer to container.
virtual void serializeTreeCallback(LeafContainerT &, const OctreeKey &)
Callback executed for every leaf node data during serialization.
Abstract octree iterator class
ContainerT & getContainer()
Get reference to container.
Octree double buffer class
void removeLeaf(const OctreeKey &key_arg)
Remove leaf node from octree.
BranchNode * createBranchChild(BranchNode &branch_arg, unsigned char child_idx_arg)
Fetch and add a new branch child to a branch class in current buffer.
std::size_t getBranchCount() const
Return the amount of existing branches in the octree.
BranchNode * root_node_
Pointer to root branch node of octree.
unsigned int depth_mask_
Depth mask based on octree depth.
char getBranchBitPattern(const BranchNode &branch_arg, unsigned char bufferSelector_arg) const
Generate bit pattern reflecting the existence of child node pointers in specific buffer.
void setChildPtr(unsigned char buffer_arg, unsigned char index_arg, OctreeNode *newNode_arg)
Set child pointer in current branch node.
bool hasBranchChanges(const BranchNode &branch_arg) const
Test if branch changed between previous and current buffer.
DepthFirstIterator depth_begin(unsigned int maxDepth_arg=0)
LeafContainerT * findLeaf(const OctreeKey &key_arg) const
Find leaf node.
Octree container class that does not store any information.
bool existLeaf(const OctreeKey &key_arg) const
Check for leaf not existance in the octree.
const ContainerT * getContainerPtr() const
Get const pointer to container.
OctreeNode * child_node_array_[2][8]
Abstract octree node class
Octree2BufBase< LeafContainerT, BranchContainerT > OctreeT
std::size_t leaf_count_
Amount of leaf nodes.
unsigned int getTreeDepth() const
Get the maximum depth of the octree.
BufferedBranchNode(const BufferedBranchNode &source)
Copy constructor.
void reset()
Reset branch node container for every branch buffer.
BufferedBranchNode & operator=(const BufferedBranchNode &source_arg)
Copy operator.
bool hasChild(unsigned char buffer_arg, unsigned char index_arg) const
Check if branch is pointing to a particular child node.