Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
octree_pointcloud.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_pointcloud.h 4702 2012-02-23 09:39:33Z gedikli $
00037  */
00038 
00039 #ifndef OCTREE_POINTCLOUD_H
00040 #define OCTREE_POINTCLOUD_H
00041 
00042 #include "octree_base.h"
00043 #include "octree2buf_base.h"
00044 #include "octree_lowmemory_base.h"
00045 
00046 #include <pcl/point_cloud.h>
00047 #include <pcl/point_types.h>
00048 
00049 #include "octree_nodes.h"
00050 #include "octree_iterator.h"
00051 
00052 #include <queue>
00053 #include <vector>
00054 #include <algorithm>
00055 #include <iostream>
00056 
00057 namespace pcl
00058 {
00059   namespace octree
00060   {
00061 
00063 
00075 
00076     template<typename PointT, typename LeafT = OctreeLeafDataTVector<int> , typename OctreeT = OctreeBase<int, LeafT> >
00077     class OctreePointCloud : public OctreeT
00078     {
00079       // iterators are friends
00080       friend class OctreeIteratorBase<int, LeafT, OctreeT> ;
00081       friend class OctreeDepthFirstIterator<int, LeafT, OctreeT> ;
00082       friend class OctreeBreadthFirstIterator<int, LeafT, OctreeT> ;
00083       friend class OctreeLeafNodeIterator<int, LeafT, OctreeT> ;
00084 
00085       public:
00086         typedef OctreeT Base;
00087         typedef typename OctreeT::OctreeLeaf OctreeLeaf;
00088 
00089         // Octree iterators
00090         typedef OctreeDepthFirstIterator<int, LeafT, OctreeT> Iterator;
00091         typedef const OctreeDepthFirstIterator<int, LeafT, OctreeT> ConstIterator;
00092 
00093         typedef OctreeLeafNodeIterator<int, LeafT, OctreeT> LeafNodeIterator;
00094         typedef const OctreeLeafNodeIterator<int, LeafT, OctreeT> ConstLeafNodeIterator;
00095 
00096         typedef OctreeDepthFirstIterator<int, LeafT, OctreeT> DepthFirstIterator;
00097         typedef const OctreeDepthFirstIterator<int, LeafT, OctreeT> ConstDepthFirstIterator;
00098         typedef OctreeBreadthFirstIterator<int, LeafT, OctreeT> BreadthFirstIterator;
00099         typedef const OctreeBreadthFirstIterator<int, LeafT, OctreeT> ConstBreadthFirstIterator;
00100 
00104         OctreePointCloud (const double resolution_arg);
00105 
00107         virtual
00108         ~OctreePointCloud ();
00109 
00110         // public typedefs
00111         typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
00112         typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
00113 
00114         typedef pcl::PointCloud<PointT> PointCloud;
00115         typedef boost::shared_ptr<PointCloud> PointCloudPtr;
00116         typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
00117 
00118         // public typedefs for single/double buffering
00119         typedef OctreePointCloud<PointT, LeafT, OctreeBase<int, LeafT> > SingleBuffer;
00120         typedef OctreePointCloud<PointT, LeafT, Octree2BufBase<int, LeafT> > DoubleBuffer;
00121         typedef OctreePointCloud<PointT, LeafT, OctreeLowMemBase<int, LeafT> > LowMem;
00122 
00123         // Boost shared pointers
00124         typedef boost::shared_ptr<OctreePointCloud<PointT, LeafT, OctreeT> > Ptr;
00125         typedef boost::shared_ptr<const OctreePointCloud<PointT, LeafT, OctreeT> > ConstPtr;
00126 
00127         // Eigen aligned allocator
00128         typedef std::vector<PointT, Eigen::aligned_allocator<PointT> > AlignedPointTVector;
00129 
00134         inline void
00135         setInputCloud (const PointCloudConstPtr &cloud_arg, const IndicesConstPtr &indices_arg = IndicesConstPtr ())
00136         {
00137           assert (this->leafCount_==0);
00138 
00139           if ((input_ != cloud_arg) && (this->leafCount_ == 0))
00140           {
00141             input_ = cloud_arg;
00142             indices_ = indices_arg;
00143           }
00144         }
00145 
00149         inline IndicesConstPtr const
00150         getIndices () const
00151         {
00152           return (indices_);
00153         }
00154 
00158         inline PointCloudConstPtr
00159         getInputCloud () const
00160         {
00161           return (input_);
00162         }
00163 
00167         inline void
00168         setEpsilon (double eps)
00169         {
00170           epsilon_ = eps;
00171         }
00172 
00174         inline double
00175         getEpsilon () const
00176         {
00177           return (epsilon_);
00178         }
00179 
00183         inline void
00184         setResolution (double resolution_arg)
00185         {
00186           // octree needs to be empty to change its resolution
00187           assert( this->leafCount_ == 0 );
00188 
00189           resolution_ = resolution_arg;
00190 
00191           getKeyBitSize();
00192         }
00193 
00197         inline double
00198         getResolution () const
00199         {
00200           return (resolution_);
00201         }
00202 
00206         inline unsigned int
00207         getTreeDepth () const
00208         {
00209           return this->octreeDepth_;
00210         }
00211 
00213         void
00214         addPointsFromInputCloud ();
00215 
00220         void
00221         addPointFromCloud (const int pointIdx_arg, IndicesPtr indices_arg);
00222 
00227         void
00228         addPointToCloud (const PointT& point_arg, PointCloudPtr cloud_arg);
00229 
00235         void
00236         addPointToCloud (const PointT& point_arg, PointCloudPtr cloud_arg, IndicesPtr indices_arg);
00237 
00242         bool
00243         isVoxelOccupiedAtPoint (const PointT& point_arg) const;
00244 
00246         void
00247         deleteTree ()
00248         {
00249           // reset bounding box
00250           minX_ = minY_ = maxY_ = minZ_ = maxZ_ = 0;
00251           maxKeys_ = 1;
00252           this->boundingBoxDefined_ = false;
00253 
00254           OctreeT::deleteTree ();
00255         }
00256 
00263         bool
00264         isVoxelOccupiedAtPoint (const double pointX_arg, const double pointY_arg, const double pointZ_arg) const;
00265 
00270         bool
00271         isVoxelOccupiedAtPoint (const int& pointIdx_arg) const;
00272 
00277         int
00278         getOccupiedVoxelCenters (AlignedPointTVector &voxelCenterList_arg) const;
00279 
00280 
00291         int
00292         getApproxIntersectedVoxelCentersBySegment (const Eigen::Vector3f& origin,
00293                                                    const Eigen::Vector3f& end,
00294                                                    AlignedPointTVector &voxel_center_list,
00295                                                    float precision = 0.2);
00296 
00300         void
00301         deleteVoxelAtPoint (const PointT& point_arg);
00302 
00306         void
00307         deleteVoxelAtPoint (const int& pointIdx_arg);
00308 
00310         // Bounding box methods
00312 
00313 
00315         void
00316         defineBoundingBox ();
00317 
00327         void
00328         defineBoundingBox (const double minX_arg, const double minY_arg, const double minZ_arg, const double maxX_arg,
00329                            const double maxY_arg, const double maxZ_arg);
00330 
00338         void
00339         defineBoundingBox (const double maxX_arg, const double maxY_arg, const double maxZ_arg);
00340 
00346         void
00347         defineBoundingBox (const double cubeLen_arg);
00348 
00358         void
00359         getBoundingBox (double& minX_arg, double& minY_arg, double& minZ_arg, double& maxX_arg, double& maxY_arg,
00360                         double& maxZ_arg) const ;
00361 
00366         double
00367         getVoxelSquaredDiameter (unsigned int treeDepth_arg) const ;
00368 
00372         inline double
00373         getVoxelSquaredDiameter () const
00374         {
00375           return getVoxelSquaredDiameter (this->octreeDepth_);
00376         }
00377 
00382         double
00383         getVoxelSquaredSideLen (unsigned int treeDepth_arg) const ;
00384 
00388         inline double
00389         getVoxelSquaredSideLen () const
00390         {
00391           return getVoxelSquaredSideLen (this->octreeDepth_);
00392         }
00393 
00394 
00400         inline void
00401         getVoxelBounds (OctreeIteratorBase<int,LeafT,OctreeT>& iterator, Eigen::Vector3f &min_pt, Eigen::Vector3f &max_pt)
00402         {
00403           this->genVoxelBoundsFromOctreeKey (iterator.getCurrentOctreeKey(), iterator.getCurrentOctreeDepth(), min_pt, max_pt);
00404         }
00405 
00406 
00407       protected:
00408 
00412         void
00413         addPointIdx (const int pointIdx_arg);
00414 
00419         const PointT&
00420         getPointByIndex (const unsigned int index_arg) const;
00421 
00426         LeafT*
00427         findLeafAtPoint (const PointT& point_arg) const ;
00428 
00430         // Protected octree methods based on octree keys
00432 
00433         typedef typename OctreeT::OctreeKey OctreeKey;
00434         typedef typename OctreeT::OctreeBranch OctreeBranch;
00435 
00437         void
00438         getKeyBitSize ();
00439 
00443         void
00444         adoptBoundingBoxToPoint (const PointT& pointIdx_arg);
00445 
00450         void
00451         genOctreeKeyforPoint (const PointT & point_arg, OctreeKey &key_arg) const ;
00452 
00459         void
00460         genOctreeKeyforPoint (const double pointX_arg, const double pointY_arg, const double pointZ_arg,
00461                               OctreeKey & key_arg) const;
00462 
00469         virtual bool
00470         genOctreeKeyForDataT (const int& data_arg, OctreeKey & key_arg) const;
00471 
00476         void
00477         genLeafNodeCenterFromOctreeKey (const OctreeKey & key_arg, PointT& point_arg) const ;
00478 
00484         void
00485         genVoxelCenterFromOctreeKey (const OctreeKey & key_arg, unsigned int treeDepth_arg, PointT& point_arg) const ;
00486 
00493         void
00494         genVoxelBoundsFromOctreeKey (const OctreeKey & key_arg, unsigned int treeDepth_arg, Eigen::Vector3f &min_pt,
00495                                      Eigen::Vector3f &max_pt) const;
00496 
00503         int
00504         getOccupiedVoxelCentersRecursive (const OctreeBranch* node_arg,
00505                                           const OctreeKey& key_arg,
00506                                           std::vector<PointT, Eigen::aligned_allocator<PointT> > &voxelCenterList_arg) const;
00507 
00509         // Globals
00511 
00512         PointCloudConstPtr input_;
00513 
00515         IndicesConstPtr indices_;
00516 
00518         double epsilon_;
00519 
00521         double resolution_;
00522 
00523         // Octree bounding box coordinates
00524         double minX_;
00525         double maxX_;
00526 
00527         double minY_;
00528         double maxY_;
00529 
00530         double minZ_;
00531         double maxZ_;
00532 
00534         unsigned int maxKeys_;
00535 
00537         bool boundingBoxDefined_;
00538     };
00539   }
00540 }
00541 
00542 #endif
00543