00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef PCL_OCTREE_TREE_BASE_H
00040 #define PCL_OCTREE_TREE_BASE_H
00041
00042 #include <cstddef>
00043 #include <vector>
00044
00045 #include "octree_nodes.h"
00046 #include "octree_container.h"
00047 #include "octree_key.h"
00048 #include "octree_iterator.h"
00049
00050 namespace pcl
00051 {
00052 namespace octree
00053 {
00061 template<typename LeafContainerT = int,
00062 typename BranchContainerT = OctreeContainerEmpty >
00063 class OctreeBase
00064 {
00065
00066 public:
00067
00068 typedef OctreeBase<LeafContainerT, BranchContainerT> OctreeT;
00069
00070 typedef OctreeBranchNode<BranchContainerT> BranchNode;
00071 typedef OctreeLeafNode<LeafContainerT> LeafNode;
00072
00073 typedef BranchContainerT BranchContainer;
00074 typedef LeafContainerT LeafContainer;
00075
00076
00077 friend class OctreeIteratorBase<OctreeT> ;
00078 friend class OctreeDepthFirstIterator<OctreeT> ;
00079 friend class OctreeBreadthFirstIterator<OctreeT> ;
00080 friend class OctreeLeafNodeIterator<OctreeT> ;
00081
00082
00083 typedef OctreeDepthFirstIterator<OctreeT> Iterator;
00084 typedef const OctreeDepthFirstIterator<OctreeT> ConstIterator;
00085 Iterator begin(unsigned int max_depth_arg = 0) {return Iterator(this, max_depth_arg);};
00086 const Iterator end() {return Iterator();};
00087
00088
00089 typedef OctreeLeafNodeIterator<OctreeT> LeafNodeIterator;
00090 typedef const OctreeLeafNodeIterator<OctreeT> ConstLeafNodeIterator;
00091 LeafNodeIterator leaf_begin(unsigned int max_depth_arg = 0) {return LeafNodeIterator(this, max_depth_arg);};
00092 const LeafNodeIterator leaf_end() {return LeafNodeIterator();};
00093
00094
00095 typedef OctreeDepthFirstIterator<OctreeT> DepthFirstIterator;
00096 typedef const OctreeDepthFirstIterator<OctreeT> ConstDepthFirstIterator;
00097 DepthFirstIterator depth_begin(unsigned int max_depth_arg = 0) {return DepthFirstIterator(this, max_depth_arg);};
00098 const DepthFirstIterator depth_end() {return DepthFirstIterator();};
00099
00100
00101 typedef OctreeBreadthFirstIterator<OctreeT> BreadthFirstIterator;
00102 typedef const OctreeBreadthFirstIterator<OctreeT> ConstBreadthFirstIterator;
00103 BreadthFirstIterator breadth_begin(unsigned int max_depth_arg = 0) {return BreadthFirstIterator(this, max_depth_arg);};
00104 const BreadthFirstIterator breadth_end() {return BreadthFirstIterator();};
00105
00106
00108 OctreeBase ();
00109
00111 virtual
00112 ~OctreeBase ();
00113
00115 OctreeBase (const OctreeBase& source) :
00116 leaf_count_ (source.leaf_count_),
00117 branch_count_ (source.branch_count_),
00118 root_node_ (new (BranchNode) (*(source.root_node_))),
00119 depth_mask_ (source.depth_mask_),
00120 octree_depth_ (source.octree_depth_),
00121 dynamic_depth_enabled_(source.dynamic_depth_enabled_),
00122 max_key_ (source.max_key_)
00123 {
00124 }
00125
00127 OctreeBase&
00128 operator = (const OctreeBase &source)
00129 {
00130 leaf_count_ = source.leaf_count_;
00131 branch_count_ = source.branch_count_;
00132 root_node_ = new (BranchNode) (*(source.root_node_));
00133 depth_mask_ = source.depth_mask_;
00134 max_key_ = source.max_key_;
00135 octree_depth_ = source.octree_depth_;
00136 return (*this);
00137 }
00138
00142 void
00143 setMaxVoxelIndex (unsigned int max_voxel_index_arg);
00144
00148 void
00149 setTreeDepth (unsigned int max_depth_arg);
00150
00154 unsigned int
00155 getTreeDepth () const
00156 {
00157 return this->octree_depth_;
00158 }
00159
00167 LeafContainerT*
00168 createLeaf (unsigned int idx_x_arg, unsigned int idx_y_arg, unsigned int idx_z_arg);
00169
00177 LeafContainerT*
00178 findLeaf (unsigned int idx_x_arg, unsigned int idx_y_arg, unsigned int idx_z_arg);
00179
00186 bool
00187 existLeaf (unsigned int idx_x_arg, unsigned int idx_y_arg, unsigned int idx_z_arg) const ;
00188
00194 void
00195 removeLeaf (unsigned int idx_x_arg, unsigned int idx_y_arg, unsigned int idx_z_arg);
00196
00200 std::size_t
00201 getLeafCount () const
00202 {
00203 return leaf_count_;
00204 }
00205
00209 std::size_t
00210 getBranchCount () const
00211 {
00212 return branch_count_;
00213 }
00214
00217 void
00218 deleteTree ( );
00219
00223 void
00224 serializeTree (std::vector<char>& binary_tree_out_arg);
00225
00230 void
00231 serializeTree (std::vector<char>& binary_tree_out_arg, std::vector<LeafContainerT*>& leaf_container_vector_arg);
00232
00236 void
00237 serializeLeafs (std::vector<LeafContainerT*>& leaf_container_vector_arg);
00238
00242 void
00243 deserializeTree (std::vector<char>& binary_tree_input_arg);
00244
00249 void
00250 deserializeTree (std::vector<char>& binary_tree_input_arg, std::vector<LeafContainerT*>& leaf_container_vector_arg);
00251
00252 protected:
00253
00255
00257
00262 LeafContainerT*
00263 createLeaf (const OctreeKey& key_arg)
00264 {
00265
00266 LeafNode* leaf_node;
00267 BranchNode* leaf_node_parent;
00268
00269 createLeafRecursive (key_arg, depth_mask_ ,root_node_, leaf_node, leaf_node_parent);
00270
00271 LeafContainerT* ret = leaf_node->getContainerPtr();
00272
00273 return ret;
00274 }
00275
00280 LeafContainerT*
00281 findLeaf (const OctreeKey& key_arg) const
00282 {
00283 LeafContainerT* result = 0;
00284 findLeafRecursive (key_arg, depth_mask_, root_node_, result);
00285 return result;
00286 }
00287
00292 bool
00293 existLeaf (const OctreeKey& key_arg) const
00294 {
00295 return (findLeaf(key_arg) != 0);
00296 }
00297
00301 void
00302 removeLeaf (const OctreeKey& key_arg)
00303 {
00304 if (key_arg <= max_key_)
00305 deleteLeafRecursive (key_arg, depth_mask_, root_node_);
00306 }
00307
00309
00311
00313 OctreeNode*
00314 getRootNode () const
00315 {
00316 return this->root_node_;
00317 }
00318
00324 bool
00325 branchHasChild (const BranchNode& branch_arg,
00326 unsigned char child_idx_arg) const
00327 {
00328
00329 return (branch_arg.getChildPtr(child_idx_arg) != 0);
00330 }
00331
00337 OctreeNode*
00338 getBranchChildPtr (const BranchNode& branch_arg,
00339 unsigned char child_idx_arg) const
00340 {
00341 return branch_arg.getChildPtr(child_idx_arg);
00342 }
00343
00349 void setBranchChildPtr (BranchNode& branch_arg,
00350 unsigned char child_idx_arg,
00351 OctreeNode* new_child_arg)
00352 {
00353 branch_arg[child_idx_arg] = new_child_arg;
00354 }
00355
00360 char
00361 getBranchBitPattern (const BranchNode& branch_arg) const
00362 {
00363 unsigned char i;
00364 char node_bits;
00365
00366
00367 node_bits = 0;
00368 for (i = 0; i < 8; i++) {
00369 const OctreeNode* child = branch_arg.getChildPtr(i);
00370 node_bits |= static_cast<char> ((!!child) << i);
00371 }
00372
00373 return (node_bits);
00374 }
00375
00380 void
00381 deleteBranchChild (BranchNode& branch_arg, unsigned char child_idx_arg)
00382 {
00383 if (branch_arg.hasChild(child_idx_arg))
00384 {
00385 OctreeNode* branch_child = branch_arg[child_idx_arg];
00386
00387 switch (branch_child->getNodeType ())
00388 {
00389 case BRANCH_NODE:
00390 {
00391
00392 deleteBranch (*static_cast<BranchNode*> (branch_child));
00393
00394 delete branch_child;
00395 }
00396 break;
00397
00398 case LEAF_NODE:
00399 {
00400
00401 delete branch_child;
00402 break;
00403 }
00404 default:
00405 break;
00406 }
00407
00408
00409 branch_arg[child_idx_arg] = 0;
00410 }
00411 }
00412
00416 void
00417 deleteBranch (BranchNode& branch_arg)
00418 {
00419 char i;
00420
00421
00422 for (i = 0; i < 8; i++)
00423 deleteBranchChild (branch_arg, i);
00424 }
00425
00431 BranchNode* createBranchChild (BranchNode& branch_arg,
00432 unsigned char child_idx_arg)
00433 {
00434 BranchNode* new_branch_child = new BranchNode();
00435 branch_arg[child_idx_arg] = static_cast<OctreeNode*> (new_branch_child);
00436
00437 return new_branch_child;
00438 }
00439
00445 LeafNode*
00446 createLeafChild (BranchNode& branch_arg, unsigned char child_idx_arg)
00447 {
00448 LeafNode* new_leaf_child = new LeafNode();
00449 branch_arg[child_idx_arg] = static_cast<OctreeNode*> (new_leaf_child);
00450
00451 return new_leaf_child;
00452 }
00453
00455
00457
00466 unsigned int
00467 createLeafRecursive (const OctreeKey& key_arg,
00468 unsigned int depth_mask_arg,
00469 BranchNode* branch_arg,
00470 LeafNode*& return_leaf_arg,
00471 BranchNode*& parent_of_leaf_arg);
00472
00480 void
00481 findLeafRecursive (const OctreeKey& key_arg,
00482 unsigned int depth_mask_arg,
00483 BranchNode* branch_arg,
00484 LeafContainerT*& result_arg) const;
00485
00492 bool
00493 deleteLeafRecursive (const OctreeKey& key_arg,
00494 unsigned int depth_mask_arg,
00495 BranchNode* branch_arg);
00496
00503 void
00504 serializeTreeRecursive (const BranchNode* branch_arg,
00505 OctreeKey& key_arg,
00506 std::vector<char>* binary_tree_out_arg,
00507 typename std::vector<LeafContainerT*>* leaf_container_vector_arg) const;
00508
00518 void
00519 deserializeTreeRecursive (BranchNode* branch_arg, unsigned int depth_mask_arg, OctreeKey& key_arg,
00520 typename std::vector<char>::const_iterator& binary_tree_input_it_arg,
00521 typename std::vector<char>::const_iterator& binary_tree_input_it_end_arg,
00522 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_arg,
00523 typename std::vector<LeafContainerT*>::const_iterator* leaf_container_vector_it_end_arg);
00524
00525
00527
00529
00532 virtual void
00533 serializeTreeCallback (LeafContainerT&, const OctreeKey &) const
00534 {
00535
00536 }
00537
00540 virtual void
00541 deserializeTreeCallback (LeafContainerT&, const OctreeKey&)
00542 {
00543
00544 }
00545
00547
00549
00554 double
00555 Log2 (double n_arg)
00556 {
00557 return log( n_arg ) / log( 2.0 );
00558 }
00559
00563 bool
00564 octreeCanResize ()
00565 {
00566 return (true);
00567 }
00568
00570
00572
00574 std::size_t leaf_count_;
00575
00577 std::size_t branch_count_;
00578
00580 BranchNode* root_node_;
00581
00583 unsigned int depth_mask_;
00584
00586 unsigned int octree_depth_;
00587
00589 bool dynamic_depth_enabled_;
00590
00592 OctreeKey max_key_;
00593 };
00594 }
00595 }
00596
00597
00598
00599 #endif
00600