39 #ifndef OCTREE_BASE_HPP
40 #define OCTREE_BASE_HPP
49 #define OCT_MAXTREEDEPTH ( sizeof(size_t) * 8 )
56 template<
typename DataT,
typename LeafT,
typename BranchT>
72 template<
typename DataT,
typename LeafT,
typename BranchT>
82 template<
typename DataT,
typename LeafT,
typename BranchT>
void
85 unsigned int treeDepth;
87 assert (maxVoxelIndex_arg>0);
90 treeDepth = std::max ((std::min (static_cast<unsigned int> (
OCT_MAXTREEDEPTH),
91 static_cast<unsigned int> (std::ceil (
Log2 (maxVoxelIndex_arg))))),
92 static_cast<unsigned int> (0));
95 depthMask_ = (1 << (treeDepth - 1));
99 template<
typename DataT,
typename LeafT,
typename BranchT>
106 octreeDepth_ = depth_arg;
109 depthMask_ = (1 << (depth_arg - 1));
112 maxKey_.x = maxKey_.y = maxKey_.z = (1 << depth_arg) - 1;
116 template<
typename DataT,
typename LeafT,
typename BranchT>
void
118 unsigned int idxZ_arg,
const DataT& data_arg)
121 OctreeKey key (idxX_arg, idxY_arg, idxZ_arg);
124 addData (key, data_arg);
128 template<
typename DataT,
typename LeafT,
typename BranchT>
bool
130 unsigned int idxZ_arg, DataT& data_arg)
const
133 OctreeKey key (idxX_arg, idxY_arg, idxZ_arg);
140 leaf->getData (data_arg);
148 template<
typename DataT,
typename LeafT,
typename BranchT>
bool
150 unsigned int idxZ_arg)
const
153 OctreeKey key (idxX_arg, idxY_arg, idxZ_arg);
156 return ( existLeaf (key));
160 template<
typename DataT,
typename LeafT,
typename BranchT>
void
162 unsigned int idxZ_arg)
165 OctreeKey key (idxX_arg, idxY_arg, idxZ_arg);
168 deleteLeafRecursive (key, depthMask_, rootNode_);
172 template<
typename DataT,
typename LeafT,
typename BranchT>
void
179 deleteBranch (*rootNode_);
193 template<
typename DataT,
typename LeafT,
typename BranchT>
void
198 assert (!maxObjsPerLeaf_);
203 binaryTreeOut_arg.clear ();
204 binaryTreeOut_arg.reserve (this->branchCount_);
206 serializeTreeRecursive (rootNode_, newKey, &binaryTreeOut_arg, 0 );
210 template<
typename DataT,
typename LeafT,
typename BranchT>
void
215 assert (!maxObjsPerLeaf_);
220 binaryTreeOut_arg.clear ();
221 dataVector_arg.clear ();
223 dataVector_arg.reserve (this->objectCount_);
224 binaryTreeOut_arg.reserve (this->branchCount_);
226 serializeTreeRecursive (rootNode_, newKey, &binaryTreeOut_arg, &dataVector_arg );
230 template<
typename DataT,
typename LeafT,
typename BranchT>
void
235 assert (!maxObjsPerLeaf_);
240 dataVector_arg.clear ();
242 dataVector_arg.reserve(this->objectCount_);
244 serializeTreeRecursive (rootNode_, newKey, 0, &dataVector_arg );
248 template<
typename DataT,
typename LeafT,
typename BranchT>
void
253 assert (!maxObjsPerLeaf_);
261 std::vector<char>::const_iterator binaryTreeVectorIterator = binaryTreeIn_arg.begin ();
262 std::vector<char>::const_iterator binaryTreeVectorIteratorEnd = binaryTreeIn_arg.end ();
264 deserializeTreeRecursive (rootNode_, depthMask_, newKey,
265 binaryTreeVectorIterator, binaryTreeVectorIteratorEnd, 0, 0);
270 template<
typename DataT,
typename LeafT,
typename BranchT>
void
272 std::vector<DataT>& dataVector_arg)
276 assert (!maxObjsPerLeaf_);
281 typename std::vector<DataT>::const_iterator dataVectorIterator = dataVector_arg.begin ();
284 typename std::vector<DataT>::const_iterator dataVectorEndIterator = dataVector_arg.end ();
290 std::vector<char>::const_iterator binaryTreeVectorIterator = binaryTreeIn_arg.begin ();
291 std::vector<char>::const_iterator binaryTreeVectorIteratorEnd = binaryTreeIn_arg.end ();
293 deserializeTreeRecursive (rootNode_, depthMask_, newKey,
294 binaryTreeVectorIterator, binaryTreeVectorIteratorEnd, &dataVectorIterator, &dataVectorEndIterator);
299 template<
typename DataT,
typename LeafT,
typename BranchT>
void OctreeBase<
300 DataT, LeafT, BranchT>::addDataToLeafRecursive (
301 const OctreeKey& key_arg,
unsigned int depthMask_arg,
302 const DataT& data_arg, BranchNode* branch_arg)
305 unsigned char childIdx;
311 branch_arg->setData (data_arg);
313 OctreeNode* childNode = (*branch_arg)[childIdx];
317 if ((!maxObjsPerLeaf_) && (depthMask_arg > 1)) {
319 BranchNode* childBranch;
320 createBranchChild (*branch_arg, childIdx, childBranch);
325 addDataToLeafRecursive (key_arg, depthMask_arg / 2, data_arg, childBranch);
331 createLeafChild (*branch_arg, childIdx, childLeaf);
335 childLeaf->setData (data_arg);
344 addDataToLeafRecursive (key_arg, depthMask_arg / 2, data_arg, static_cast<BranchNode*> (childNode));
348 LeafNode* childLeaf =
static_cast<LeafNode*
> (childNode);
350 if ( (!maxObjsPerLeaf_) || (!depthMask_arg) )
353 childLeaf->setData (data_arg);
356 size_t leafObjCount = childLeaf->getSize();
358 if (leafObjCount<maxObjsPerLeaf_) {
360 childLeaf->setData (data_arg);
366 std::vector<DataT> leafData;
367 leafData.reserve(leafObjCount);
369 childLeaf->getData (leafData);
372 deleteBranchChild(*branch_arg,childIdx);
376 BranchNode* childBranch;
377 createBranchChild (*branch_arg, childIdx, childBranch);
380 typename std::vector<DataT>::const_iterator lData = leafData.begin();
381 typename std::vector<DataT>::const_iterator lDataEnd = leafData.end();
385 while (lData!=lDataEnd) {
387 const DataT& data = *lData++;
390 if (this->genOctreeKeyForDataT(data, dataKey)) {
391 addDataToLeafRecursive (dataKey, depthMask_arg / 2, data, childBranch);
396 addDataToLeafRecursive (key_arg, depthMask_arg / 2, data_arg, childBranch);
399 objectCount_ -= leafObjCount;
408 template<
typename DataT,
typename LeafT,
typename BranchT>
void
409 OctreeBase<DataT, LeafT, BranchT>::findLeafRecursive (
410 const OctreeKey& key_arg,
unsigned int depthMask_arg, BranchNode* branch_arg, LeafNode*& result_arg)
const
413 unsigned char childIdx;
416 childIdx = key_arg.getChildIdxWithDepthMask(depthMask_arg);
418 OctreeNode* childNode = (*branch_arg)[childIdx];
421 switch (childNode->getNodeType()) {
424 BranchNode* childBranch;
425 childBranch =
static_cast<BranchNode*
> (childNode);
427 findLeafRecursive (key_arg, depthMask_arg / 2, childBranch, result_arg);
433 childLeaf =
static_cast<LeafNode*
> (childNode);
434 result_arg = childLeaf;
441 template<
typename DataT,
typename LeafT,
typename BranchT>
bool
442 OctreeBase<DataT, LeafT, BranchT>::deleteLeafRecursive (
const OctreeKey& key_arg,
unsigned int depthMask_arg,
443 BranchNode* branch_arg)
446 unsigned char childIdx;
451 childIdx = key_arg.getChildIdxWithDepthMask(depthMask_arg);
453 OctreeNode* childNode = (*branch_arg)[childIdx];
456 switch (childNode->getNodeType()) {
459 BranchNode* childBranch;
460 childBranch =
static_cast<BranchNode*
> (childNode);
463 bNoChilds = deleteLeafRecursive (key_arg, depthMask_arg / 2, childBranch);
468 deleteBranchChild(*branch_arg, childIdx);
477 deleteBranchChild (*branch_arg, childIdx);
485 for (childIdx = 0; (!bNoChilds) && (childIdx < 8); childIdx++)
487 bNoChilds = branch_arg->hasChild(childIdx);
494 template<
typename DataT,
typename LeafT,
typename BranchT>
void OctreeBase<
495 DataT, LeafT, BranchT>::serializeTreeRecursive (
const BranchNode* branch_arg, OctreeKey& key_arg,
496 std::vector<char>* binaryTreeOut_arg,
497 typename std::vector<DataT>* dataVector_arg)
const
501 unsigned char childIdx;
505 nodeBitPattern = getBranchBitPattern (*branch_arg);
508 if (binaryTreeOut_arg)
509 binaryTreeOut_arg->push_back (nodeBitPattern);
512 for (childIdx = 0; childIdx < 8; childIdx++)
516 if (branch_arg->hasChild(childIdx))
519 key_arg.pushBranch(childIdx);
521 const OctreeNode *childNode = branch_arg->getChildPtr(childIdx);
523 switch (childNode->getNodeType ())
528 serializeTreeRecursive (
529 static_cast<const BranchNode*> (childNode), key_arg,
530 binaryTreeOut_arg, dataVector_arg);
535 const LeafNode* childLeaf =
static_cast<const LeafNode*
> (childNode);
538 childLeaf->getData (*dataVector_arg);
541 serializeTreeCallback (*childLeaf, key_arg);
557 template<
typename DataT,
typename LeafT,
typename BranchT>
void
558 OctreeBase<DataT, LeafT, BranchT>::deserializeTreeRecursive (BranchNode* branch_arg,
559 unsigned int depthMask_arg, OctreeKey& key_arg,
560 typename std::vector<char>::const_iterator& binaryTreeIT_arg,
561 typename std::vector<char>::const_iterator& binaryTreeIT_End_arg,
562 typename std::vector<DataT>::const_iterator* dataVectorIterator_arg,
563 typename std::vector<DataT>::const_iterator* dataVectorEndIterator_arg)
566 unsigned char childIdx;
569 if (binaryTreeIT_arg != binaryTreeIT_End_arg ) {
571 nodeBits = (*binaryTreeIT_arg);
575 for (childIdx = 0; childIdx < 8; childIdx++)
578 if (nodeBits & (1 << childIdx))
581 key_arg.pushBranch(childIdx);
583 if (depthMask_arg > 1)
586 BranchNode * newBranch;
589 createBranchChild (*branch_arg, childIdx, newBranch);
594 deserializeTreeRecursive (newBranch, depthMask_arg / 2, key_arg, binaryTreeIT_arg,binaryTreeIT_End_arg, dataVectorIterator_arg,
595 dataVectorEndIterator_arg);
604 createLeafChild (*branch_arg, childIdx, childLeaf);
607 bool bKeyBasedEncoding =
false;
609 if (dataVectorIterator_arg
610 && (*dataVectorIterator_arg != *dataVectorEndIterator_arg))
614 while ( ( (*dataVectorIterator_arg)
615 != (*dataVectorEndIterator_arg))
616 && (this->genOctreeKeyForDataT (**dataVectorIterator_arg,
617 dataKey) && (dataKey == key_arg)))
619 childLeaf->setData (**dataVectorIterator_arg);
620 (*dataVectorIterator_arg)++;
621 bKeyBasedEncoding =
true;
626 if (!bKeyBasedEncoding)
628 childLeaf->setData (**dataVectorIterator_arg);
629 (*dataVectorIterator_arg)++;
637 deserializeTreeCallback (*childLeaf, key_arg);
650 #define PCL_INSTANTIATE_OctreeBase(T) template class PCL_EXPORTS pcl::octree::OctreeBase<T>;