39 #ifndef OCTREE_TREE_2BUF_BASE_H
40 #define OCTREE_TREE_2BUF_BASE_H
58 template<
typename ContainerT>
61 using ContainerT::getSize;
62 using ContainerT::getData;
63 using ContainerT::setData;
85 memset (childNodeArray_, 0,
sizeof(childNodeArray_));
87 for (b = 0; b < 2; ++b)
88 for (i = 0; i < 8; ++i)
89 if (source_arg.childNodeArray_[b][i])
90 childNodeArray_[b][i] = source_arg.childNodeArray_[b][i]->
deepCopy ();
114 getChildPtr (
unsigned char buffer_arg,
unsigned char index_arg)
const
116 assert( (buffer_arg<2) && (index_arg<8));
117 return childNodeArray_[buffer_arg][index_arg];
125 inline void setChildPtr (
unsigned char buffer_arg,
unsigned char index_arg,
128 assert( (buffer_arg<2) && (index_arg<8));
129 childNodeArray_[buffer_arg][index_arg] = newNode_arg;
137 inline bool hasChild (
unsigned char buffer_arg,
unsigned char index_arg)
const
139 assert( (buffer_arg<2) && (index_arg<8));
140 return (childNodeArray_[buffer_arg][index_arg] != 0);
152 memset (&childNodeArray_[0][0], 0,
sizeof(
OctreeNode*) * 8 * 2);
153 ContainerT::reset ();
176 template<
typename DataT,
typename LeafT = OctreeContainerDataT<DataT>,
177 typename BranchT = OctreeContainerEmpty<DataT> >
215 leafCount_ (source.leafCount_), branchCount_ (source.branchCount_), objectCount_ (
216 source.objectCount_), rootNode_ (
217 new (
BranchNode) (* (source.rootNode_))), depthMask_ (
218 source.depthMask_), maxKey_ (source.maxKey_), branchNodePool_ (), leafNodePool_ (), bufferSelector_ (
219 source.bufferSelector_), treeDirtyFlag_ (source.treeDirtyFlag_), octreeDepth_ (
228 leafCount_ = source.leafCount_;
229 branchCount_ = source.branchCount_;
230 objectCount_ = source.objectCount_;
231 rootNode_ =
new (
BranchNode) (* (source.rootNode_));
232 depthMask_ = source.depthMask_;
233 maxKey_ = source.maxKey_;
234 bufferSelector_ = source.bufferSelector_;
235 treeDirtyFlag_ = source.treeDirtyFlag_;
236 octreeDepth_ = source.octreeDepth_;
257 return this->octreeDepth_;
267 addData (
unsigned int idxX_arg,
unsigned int idxY_arg,
268 unsigned int idxZ_arg,
const DataT& data_arg);
278 getData (
unsigned int idxX_arg,
unsigned int idxY_arg,
279 unsigned int idxZ_arg, DataT& data_arg)
const;
288 existLeaf (
unsigned int idxX_arg,
unsigned int idxY_arg,
289 unsigned int idxZ_arg)
const;
297 removeLeaf (
unsigned int idxX_arg,
unsigned int idxY_arg,
298 unsigned int idxZ_arg);
305 return (static_cast<unsigned int> (leafCount_));
313 return (static_cast<unsigned int> (branchCount_));
325 treeCleanUpRecursive (rootNode_);
331 bufferSelector_ = !bufferSelector_;
332 treeCleanUpRecursive (rootNode_);
346 bool doXOREncoding_arg =
false);
355 std::vector<DataT>& dataVector_arg,
bool doXOREncoding_arg =
false);
369 const int minPointsPerLeaf_arg = 0);
377 bool doXORDecoding_arg =
false);
386 std::vector<DataT>& dataVector_arg,
bool doXORDecoding_arg =
false);
398 return (this->rootNode_);
406 virtual bool genOctreeKeyForDataT (
const DataT &, OctreeKey &)
const
417 virtual bool genDataTByOctreeKey (
const OctreeKey &, DataT &)
const
427 inline void addData (
const OctreeKey& key_arg,
const DataT& data_arg)
430 LeafT* leaf = createLeaf (key_arg);
435 leaf->setData (data_arg);
445 findLeaf (
const OctreeKey& key_arg)
const
447 return findLeafRecursive (key_arg, depthMask_, rootNode_);
456 createLeaf (
const OctreeKey& key_arg)
460 result = createLeafRecursive (key_arg, depthMask_, rootNode_,
false);
463 treeDirtyFlag_ =
true;
472 inline bool existLeaf (
const OctreeKey& key_arg)
const
474 return ( (key_arg <= maxKey_)
475 && (findLeafRecursive (key_arg, depthMask_, rootNode_) != 0));
481 inline void removeLeaf (
const OctreeKey& key_arg)
483 if (key_arg <= maxKey_)
485 deleteLeafRecursive (key_arg, depthMask_, rootNode_);
488 treeDirtyFlag_ =
true;
502 branchHasChild (
const BranchNode& branch_arg,
unsigned char childIdx_arg)
const
505 return (branch_arg.getChildPtr(bufferSelector_, childIdx_arg) != 0);
514 getBranchChildPtr (
const BranchNode& branch_arg,
515 unsigned char childIdx_arg)
const
517 return branch_arg.getChildPtr(bufferSelector_, childIdx_arg);
525 inline void setBranchChildPtr (
BranchNode& branch_arg,
526 unsigned char childIdx_arg, OctreeNode* newChild_arg)
528 branch_arg.setChildPtr(bufferSelector_, childIdx_arg, newChild_arg);
535 inline void getDataFromOctreeNode (
const OctreeNode* node_arg,
538 if (node_arg->getNodeType () ==
LEAF_NODE)
540 const LeafT* leafContainer =
dynamic_cast<const LeafT*
> (node_arg);
541 leafContainer->getData (data_arg);
545 const BranchT* branchContainer =
546 dynamic_cast<const BranchT*
> (node_arg);
547 branchContainer->getData (data_arg);
555 inline void getDataFromOctreeNode (
const OctreeNode* node_arg,
556 std::vector<DataT>& data_arg)
558 if (node_arg->getNodeType () ==
LEAF_NODE)
560 const LeafT* leafContainer =
dynamic_cast<const LeafT*
> (node_arg);
561 leafContainer->getData (data_arg);
565 const BranchT* branchContainer =
566 dynamic_cast<const BranchT*
> (node_arg);
567 branchContainer->getData (data_arg);
575 inline size_t getDataSizeFromOctreeNode (
const OctreeNode* node_arg)
578 if (node_arg->getNodeType () ==
LEAF_NODE)
580 const LeafT* leafContainer =
dynamic_cast<const LeafT*
> (node_arg);
581 nodeSize = leafContainer->getSize ();
585 const BranchT* branchContainer =
586 dynamic_cast<const BranchT*
> (node_arg);
587 nodeSize = branchContainer->getSize ();
596 inline char getBranchBitPattern (
const BranchNode& branch_arg)
const
603 for (i = 0; i < 8; i++)
605 nodeBits |=
static_cast<char> ( (!!branch_arg.getChildPtr (
606 bufferSelector_, i)) << i);
617 inline char getBranchBitPattern (
const BranchNode& branch_arg,
618 unsigned char bufferSelector_arg)
const
625 for (i = 0; i < 8; i++)
627 nodeBits |=
static_cast<char> ( (!!branch_arg.getChildPtr (
628 bufferSelector_arg, i)) << i);
638 inline char getBranchXORBitPattern (
645 nodeBits[0] = nodeBits[1] = 0;
647 for (i = 0; i < 8; i++)
649 nodeBits[0] |=
static_cast<char> ( (!!branch_arg.getChildPtr (0, i))
651 nodeBits[1] |=
static_cast<char> ( (!!branch_arg.getChildPtr (1, i))
655 return nodeBits[0] ^ nodeBits[1];
662 inline bool hasBranchChanges (
const BranchNode& branch_arg)
const
664 return (getBranchXORBitPattern (branch_arg) > 0);
672 inline void deleteBranchChild (
BranchNode& branch_arg,
673 unsigned char bufferSelector_arg,
674 unsigned char childIdx_arg)
676 if (branch_arg.hasChild(bufferSelector_arg, childIdx_arg))
678 OctreeNode* branchChild = branch_arg.getChildPtr(bufferSelector_arg, childIdx_arg);
680 switch (branchChild->getNodeType ())
685 deleteBranch (*static_cast<BranchNode*> (branchChild));
688 branchNodePool_.pushNode (
689 static_cast<BranchNode*> (branchChild));
696 leafNodePool_.pushNode(
697 static_cast<LeafNode*> (branchChild));
705 branch_arg.setChildPtr(bufferSelector_arg, childIdx_arg, 0);
712 inline void deleteBranch (
BranchNode& branch_arg)
717 for (i = 0; i < 8; i++)
720 if (branch_arg.getChildPtr(0, i) == branch_arg.getChildPtr(1, i))
723 deleteBranchChild (branch_arg, 0, i);
726 branch_arg.setChildPtr(0, i, 0);
727 branch_arg.setChildPtr(1, i, 0);
731 deleteBranchChild (branch_arg, 0, i);
732 deleteBranchChild (branch_arg, 1, i);
742 inline void createBranchChild (
BranchNode& branch_arg,
743 unsigned char childIdx_arg,
BranchNode*& newBranchChild_arg)
746 newBranchChild_arg = branchNodePool_.popNode();
748 branch_arg.setChildPtr (bufferSelector_, childIdx_arg,
749 static_cast<OctreeNode*> (newBranchChild_arg));
757 inline void createLeafChild (
BranchNode& branch_arg,
758 unsigned char childIdx_arg,
LeafNode*& newLeafChild_arg)
760 newLeafChild_arg = leafNodePool_.popNode();
762 branch_arg.setChildPtr(bufferSelector_, childIdx_arg, newLeafChild_arg);
767 inline void poolCleanUp ()
769 branchNodePool_.deletePool();
770 leafNodePool_.deletePool();
785 createLeafRecursive (
const OctreeKey& key_arg,
786 unsigned int depthMask_arg,
BranchNode* branch_arg,
787 bool branchReset_arg);
797 findLeafRecursive (
const OctreeKey& key_arg,
unsigned int depthMask_arg,
807 deleteLeafRecursive (
const OctreeKey& key_arg,
808 unsigned int depthMask_arg,
BranchNode* branch_arg);
820 serializeTreeRecursive (
BranchNode* branch_arg, OctreeKey& key_arg,
821 std::vector<char>* binaryTreeOut_arg,
822 typename std::vector<DataT>* dataVector_arg,
bool doXOREncoding_arg =
false,
823 bool newLeafsFilter_arg =
false, std::size_t minPointsPerLeaf_arg = 0);
836 deserializeTreeRecursive (
BranchNode* branch_arg,
837 unsigned int depthMask_arg, OctreeKey& key_arg,
838 typename std::vector<char>::const_iterator& binaryTreeIT_arg,
839 typename std::vector<char>::const_iterator& binaryTreeIT_End_arg,
840 typename std::vector<DataT>::const_iterator* dataVectorIterator_arg,
841 typename std::vector<DataT>::const_iterator* dataVectorEndIterator_arg,
842 bool branchReset_arg =
false,
bool doXORDecoding_arg =
false);
851 virtual void serializeTreeCallback (
LeafNode &,
const OctreeKey &)
858 virtual void deserializeTreeCallback (
LeafNode&,
const OctreeKey&)
871 treeCleanUpRecursive (
BranchNode* branch_arg);
877 inline double Log2 (
double n_arg)
879 return log (n_arg) / log (2.0);
885 inline bool octreeCanResize ()
893 inline void printBinary (
char data_arg)
895 unsigned char mask = 1;
898 for (
int i = 0; i < 8; i++)
901 std::cout << ((data_arg & (mask << i)) ?
"1" :
"0");
903 std::cout << std::endl;
911 std::size_t leafCount_;
914 std::size_t branchCount_;
917 std::size_t objectCount_;
923 unsigned int depthMask_;
929 OctreeNodePool<BranchNode> branchNodePool_;
932 OctreeNodePool<LeafNode> leafNodePool_;
935 unsigned char bufferSelector_;
941 unsigned int octreeDepth_;