39 #ifndef OCTREE_TREE_BASE_H
40 #define OCTREE_TREE_BASE_H
62 template<
typename DataT,
typename LeafT = OctreeContainerDataT<DataT>,
63 typename BranchT = OctreeContainerEmpty<DataT> >
105 leafCount_ (source.leafCount_),
106 branchCount_ (source.branchCount_),
107 objectCount_ (source.objectCount_),
108 rootNode_ (new (
BranchNode) (*(source.rootNode_))),
109 depthMask_ (source.depthMask_),
110 octreeDepth_ (source.octreeDepth_),
111 maxKey_ (source.maxKey_),
121 leafCount_ = source.leafCount_;
122 branchCount_ = source.branchCount_;
123 objectCount_ = source.objectCount_;
124 rootNode_ =
new (
BranchNode) (*(source.rootNode_));
125 depthMask_ = source.depthMask_;
126 maxKey_ = source.maxKey_;
127 octreeDepth_ = source.octreeDepth_;
149 return this->octreeDepth_;
159 assert(leafCount_==0);
160 maxObjsPerLeaf_ = maxObjsPerLeaf;
170 addData (
unsigned int idxX_arg,
unsigned int idxY_arg,
unsigned int idxZ_arg,
171 const DataT& data_arg);
181 getData (
unsigned int idxX_arg,
unsigned int idxY_arg,
unsigned int idxZ_arg, DataT& data_arg)
const ;
190 existLeaf (
unsigned int idxX_arg,
unsigned int idxY_arg,
unsigned int idxZ_arg)
const ;
198 removeLeaf (
unsigned int idxX_arg,
unsigned int idxY_arg,
unsigned int idxZ_arg);
235 serializeTree (std::vector<char>& binaryTreeOut_arg, std::vector<DataT>& dataVector_arg);
254 deserializeTree (std::vector<char>& binaryTreeIn_arg, std::vector<DataT>& dataVector_arg);
268 genOctreeKeyForDataT (
const DataT &,
OctreeKey &)
const
280 genDataTByOctreeKey (
const OctreeKey &, DataT &)
const
291 addData (
const OctreeKey& key_arg,
const DataT& data_arg)
293 addDataToLeafRecursive (key_arg, depthMask_,data_arg, rootNode_);
301 findLeaf (
const OctreeKey& key_arg)
const
304 findLeafRecursive (key_arg, depthMask_, rootNode_, result);
313 existLeaf (
const OctreeKey& key_arg)
const
316 if (key_arg <= maxKey_)
317 findLeafRecursive (key_arg, depthMask_, rootNode_, leafNode);
318 return (leafNode != 0);
327 if (key_arg <= maxKey_)
328 deleteLeafRecursive (key_arg, depthMask_, rootNode_);
339 return this->rootNode_;
348 branchHasChild (
const BranchNode& branch_arg,
unsigned char childIdx_arg)
const
351 return (branch_arg.getChildPtr(childIdx_arg) != 0);
360 getBranchChildPtr (
const BranchNode& branch_arg,
361 unsigned char childIdx_arg)
const
371 inline void setBranchChildPtr (
BranchNode& branch_arg,
372 unsigned char childIdx_arg, OctreeNode* newChild_arg)
374 branch_arg[childIdx_arg] = newChild_arg;
381 inline void getDataFromOctreeNode (
const OctreeNode* node_arg,
384 if (node_arg->getNodeType () ==
LEAF_NODE)
386 const LeafT* leafContainer =
dynamic_cast<const LeafT*
> (node_arg);
387 leafContainer->getData (data_arg);
391 const BranchT* branchContainer =
392 dynamic_cast<const BranchT*
> (node_arg);
393 branchContainer->getData (data_arg);
401 inline void getDataFromOctreeNode (
const OctreeNode* node_arg,
402 std::vector<DataT>& data_arg)
404 if (node_arg->getNodeType () ==
LEAF_NODE)
406 const LeafT* leafContainer =
dynamic_cast<const LeafT*
> (node_arg);
407 leafContainer->getData (data_arg);
411 const BranchT* branchContainer =
412 dynamic_cast<const BranchT*
> (node_arg);
413 branchContainer->getData (data_arg);
421 inline size_t getDataSizeFromOctreeNode (
const OctreeNode* node_arg)
424 if (node_arg->getNodeType () ==
LEAF_NODE)
426 const LeafT* leafContainer =
dynamic_cast<const LeafT*
> (node_arg);
427 nodeSize = leafContainer->getSize ();
431 const BranchT* branchContainer =
432 dynamic_cast<const BranchT*
> (node_arg);
433 nodeSize = branchContainer->getSize ();
442 getBranchBitPattern (
const BranchNode& branch_arg)
const
449 for (i = 0; i < 8; i++) {
450 const OctreeNode* child = branch_arg.getChildPtr(i);
451 nodeBits |=
static_cast<char> ((!!child) << i);
462 deleteBranchChild (
BranchNode& branch_arg,
unsigned char childIdx_arg)
464 if (branch_arg.hasChild(childIdx_arg))
466 OctreeNode* branchChild = branch_arg[childIdx_arg];
468 switch (branchChild->getNodeType ())
473 deleteBranch (*static_cast<BranchNode*> (branchChild));
475 branchNodePool_.pushNode (
476 static_cast<BranchNode*> (branchChild));
483 leafNodePool_.pushNode (static_cast<LeafNode*> (branchChild));
491 branch_arg[childIdx_arg] = 0;
504 for (i = 0; i < 8; i++)
505 deleteBranchChild (branch_arg, i);
513 inline void createBranchChild (
BranchNode& branch_arg,
514 unsigned char childIdx_arg,
BranchNode*& newBranchChild_arg)
516 newBranchChild_arg = branchNodePool_.popNode ();
517 branch_arg[childIdx_arg] =
518 static_cast<OctreeNode*
> (newBranchChild_arg);
527 createLeafChild (
BranchNode& branch_arg,
unsigned char childIdx_arg,
LeafNode*& newLeafChild_arg)
529 newLeafChild_arg = leafNodePool_.popNode();
531 branch_arg[childIdx_arg] =
static_cast<OctreeNode*
> (newLeafChild_arg);
548 branchNodePool_.deletePool();
549 leafNodePool_.deletePool();
563 addDataToLeafRecursive (
const OctreeKey& key_arg,
unsigned int depthMask_arg,
const DataT& data_arg,
BranchNode* branch_arg);
573 findLeafRecursive (
const OctreeKey& key_arg,
unsigned int depthMask_arg,
BranchNode* branch_arg,
LeafNode*& result_arg)
const;
582 deleteLeafRecursive (
const OctreeKey& key_arg,
unsigned int depthMask_arg,
BranchNode* branch_arg);
591 serializeTreeRecursive (
const BranchNode* branch_arg, OctreeKey& key_arg,
592 std::vector<char>* binaryTreeOut_arg,
593 typename std::vector<DataT>* dataVector_arg)
const;
605 deserializeTreeRecursive (
BranchNode* branch_arg,
606 unsigned int depthMask_arg, OctreeKey& key_arg,
607 typename std::vector<char>::const_iterator& binaryTreeIT_arg,
608 typename std::vector<char>::const_iterator& binaryTreeIT_End_arg,
609 typename std::vector<DataT>::const_iterator* dataVectorIterator_arg,
610 typename std::vector<DataT>::const_iterator* dataVectorEndIterator_arg);
619 virtual void serializeTreeCallback (
const LeafNode &,
620 const OctreeKey &)
const
627 virtual void deserializeTreeCallback (
LeafNode&,
const OctreeKey&)
643 return log( n_arg ) / log( 2.0 );
660 std::size_t leafCount_;
663 std::size_t branchCount_;
666 std::size_t objectCount_;
674 std::size_t maxObjsPerLeaf_;
677 unsigned int depthMask_;
680 unsigned int octreeDepth_;
686 OctreeNodePool<BranchNode> branchNodePool_;
689 OctreeNodePool<LeafNode> leafNodePool_;