Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
octree_nodes.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: octree_nodes.h 4702 2012-02-23 09:39:33Z gedikli $
00037  */
00038 
00039 #ifndef OCTREE_NODE_H
00040 #define OCTREE_NODE_H
00041 
00042 // maximum depth of octree as we are using "unsigned int" octree keys / bit masks
00043 #define OCT_MAXTREEDEPTH ( sizeof(unsigned int) * 8  )
00044 
00045 #include <string.h>
00046 
00047 #include <pcl/pcl_macros.h>
00048 
00049 namespace pcl
00050 {
00051   namespace octree
00052   {
00053 
00054     // enum of node types within the octree
00055     enum node_type_t
00056     {
00057       BRANCH_NODE, LEAF_NODE, LEAF_BRANCH
00058     };
00059 
00061 
00065     class PCL_EXPORTS OctreeNode
00066     {
00067     public:
00068 
00070       virtual node_type_t getNodeType () const = 0;
00071 
00073       virtual OctreeNode* deepCopy () const = 0;
00074     };
00075 
00077 
00081     template<typename DataT>
00082     class OctreeLeafAbstract : public OctreeNode
00083     {
00084     public:
00085 
00086       typedef DataT leaf_data_t;
00087 
00089       OctreeLeafAbstract () 
00090       {
00091       }
00092 
00094       ~OctreeLeafAbstract () 
00095       {
00096       }
00097 
00099       virtual OctreeNode* deepCopy () const = 0;
00100 
00102       inline virtual node_type_t getNodeType () const {return LEAF_NODE;}
00103 
00107       virtual void
00108         setData (const leaf_data_t& data_arg) = 0;
00109 
00113       virtual void
00114         getData (const DataT*& data_arg) const = 0;
00115 
00119       virtual void
00120         getData (std::vector<leaf_data_t>& dataVector_arg) const = 0;
00121 
00123       virtual void
00124         reset () = 0;
00125 
00126     };
00127 
00129 
00133     template<typename DataT>
00134     class OctreeLeafEmpty : public OctreeLeafAbstract<DataT>
00135     {
00136     public:
00138       OctreeLeafEmpty ()
00139       {
00140       }
00141 
00143       ~OctreeLeafEmpty ()
00144       {
00145       }
00146 
00148       virtual OctreeNode *
00149       deepCopy () const
00150       {
00151         return (OctreeNode*) new OctreeLeafEmpty (*this);
00152       }
00153 
00158       virtual void
00159         setData (const DataT& data_arg)
00160       {
00161       }
00162 
00166       virtual void
00167         getData (const DataT*& data_arg) const
00168       {
00169         data_arg = 0;
00170       }
00171 
00175       virtual void
00176         getData (std::vector<DataT>& dataVector_arg) const
00177       {
00178       }
00179 
00181       virtual void
00182         reset ()
00183       {
00184       }
00185     };
00186 
00188 
00192     template<typename DataT>
00193     class OctreeLeafDataT : public OctreeLeafAbstract<DataT>
00194     {
00195     public:
00196 
00198       OctreeLeafDataT ()
00199       {
00200       }
00201 
00203       ~OctreeLeafDataT ()
00204       {
00205       }
00206 
00208       virtual OctreeNode*
00209       deepCopy () const
00210       {
00211         return (OctreeNode*) new OctreeLeafDataT (*this);
00212       }
00213 
00217       virtual void
00218         setData (const DataT& data_arg)
00219       {
00220         this->data_ = data_arg;
00221       }
00222 
00226       virtual void
00227         getData (const DataT*& data_arg) const
00228       {
00229         data_arg = &data_;
00230       }
00231 
00235       virtual void
00236         getData (std::vector<DataT>& dataVector_arg) const
00237       {
00238         dataVector_arg.push_back (this->data_);
00239       }
00240 
00242       virtual void
00243         reset ()
00244       {
00245         memset (&data_, 0, sizeof(data_));
00246       }
00247 
00248     protected:
00250       DataT data_;
00251 
00252     };
00253 
00255 
00259     template<typename DataT>
00260     class OctreeLeafDataTVector : public OctreeLeafAbstract<DataT>
00261     {
00262       public:
00263 
00265       OctreeLeafDataTVector ()
00266       {
00267       }
00268 
00270       ~OctreeLeafDataTVector ()
00271       {
00272       }
00273 
00275       virtual OctreeNode *
00276       deepCopy () const
00277       {
00278         return (OctreeNode*) new OctreeLeafDataTVector (*this);
00279       }
00280 
00284       virtual void
00285         setData (const DataT& data_arg)
00286       {
00287         leafDataTVector_.push_back (data_arg);
00288       }
00289 
00293       virtual void
00294         getData (const DataT*& data_arg) const
00295       {
00296         DataT* result = 0;
00297 
00298         if (leafDataTVector_.size () > 0)
00299           result = (DataT*)&leafDataTVector_.back ();
00300 
00301         data_arg = result;
00302       }
00303 
00307       virtual void
00308         getData (std::vector<DataT>& dataVector_arg) const
00309       {
00310         dataVector_arg.insert (dataVector_arg.end (), leafDataTVector_.begin (), leafDataTVector_.end ());
00311       }
00312 
00316       virtual const std::vector<DataT>&
00317         getIdxVector ()
00318       {
00319         return leafDataTVector_;
00320       }
00321 
00323       virtual void
00324         reset ()
00325       {
00326         leafDataTVector_.clear ();
00327       }
00328 
00329     protected:
00331       std::vector<DataT> leafDataTVector_;
00332 
00333     };
00334 
00336 
00340 
00341     class OctreeBranch : public OctreeNode
00342     {
00343       
00344     public:
00345 
00346       typedef const OctreeNode * octree_node_ptr;
00347 
00349       OctreeBranch ()
00350       {
00351         memset (this->subNodes_, 0, sizeof(this->subNodes_));
00352       }
00353       
00355       OctreeBranch (const OctreeBranch& source)
00356       {
00357         memset (subNodes_, 0, sizeof(subNodes_));
00358         // iterate over the 8 child nodes
00359         for (unsigned int i=0; i<8; i++)
00360         {
00361           if (source.subNodes_[i] != 0)
00362             subNodes_[i] = source.subNodes_[i]->deepCopy ();
00363         }
00364       }
00365 
00367       virtual OctreeNode *
00368       deepCopy () const
00369       {
00370         return (OctreeNode*) new OctreeBranch (*this);
00371       }
00372 
00374       virtual ~OctreeBranch () {}
00375 
00379       inline virtual node_type_t
00380         getNodeType () const {return BRANCH_NODE;}
00381 
00385       inline const octree_node_ptr& operator[] (size_t pos) const {return subNodes_[pos];}
00386 
00390       inline octree_node_ptr& operator[] (size_t pos) {return subNodes_[pos];}
00391 
00393       inline void reset () {memset (subNodes_, 0, sizeof (subNodes_));}
00394 
00395     private:
00397       octree_node_ptr subNodes_[8];
00398     };
00399 
00400 
00401   }
00402 }
00403 
00404 #endif