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$
00037  */
00038 
00039 #ifndef PCL_OCTREE_POINTCLOUD_H
00040 #define PCL_OCTREE_POINTCLOUD_H
00041 
00042 #include "octree_base.h"
00043 //#include "octree2buf_base.h"
00044 
00045 #include <pcl/point_cloud.h>
00046 #include <pcl/point_types.h>
00047 
00048 #include <queue>
00049 #include <vector>
00050 #include <algorithm>
00051 #include <iostream>
00052 
00053 namespace pcl
00054 {
00055   namespace octree
00056   {
00057 
00059 
00072 
00073     template<typename PointT, typename LeafContainerT = OctreeContainerPointIndices,
00074         typename BranchContainerT = OctreeContainerEmpty,
00075         typename OctreeT = OctreeBase<LeafContainerT, BranchContainerT> >
00076 
00077     class OctreePointCloud : public OctreeT
00078     {
00079         // iterators are friends
00080         friend class OctreeIteratorBase<OctreeT> ;
00081         friend class OctreeDepthFirstIterator<OctreeT> ;
00082         friend class OctreeBreadthFirstIterator<OctreeT> ;
00083         friend class OctreeLeafNodeIterator<OctreeT> ;
00084 
00085       public:
00086         typedef OctreeT Base;
00087 
00088         typedef typename OctreeT::LeafNode LeafNode;
00089         typedef typename OctreeT::BranchNode BranchNode;
00090 
00091         // Octree default iterators
00092         typedef OctreeDepthFirstIterator<OctreeT> Iterator;
00093         typedef const OctreeDepthFirstIterator<OctreeT> ConstIterator;
00094 
00095         // Octree leaf node iterators
00096         typedef OctreeLeafNodeIterator<OctreeT> LeafNodeIterator;
00097         typedef const OctreeLeafNodeIterator<OctreeT> ConstLeafNodeIterator;
00098 
00099         // Octree depth-first iterators
00100         typedef OctreeDepthFirstIterator<OctreeT> DepthFirstIterator;
00101         typedef const OctreeDepthFirstIterator<OctreeT> ConstDepthFirstIterator;
00102 
00103         // Octree breadth-first iterators
00104         typedef OctreeBreadthFirstIterator<OctreeT> BreadthFirstIterator;
00105         typedef const OctreeBreadthFirstIterator<OctreeT> ConstBreadthFirstIterator;
00106 
00110         OctreePointCloud (const double resolution_arg);
00111 
00113         virtual
00114         ~OctreePointCloud ();
00115 
00116         // public typedefs
00117         typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
00118         typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
00119 
00120         typedef pcl::PointCloud<PointT> PointCloud;
00121         typedef boost::shared_ptr<PointCloud> PointCloudPtr;
00122         typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
00123 
00124         // public typedefs for single/double buffering
00125         typedef OctreePointCloud<PointT, LeafContainerT, BranchContainerT, OctreeBase<LeafContainerT> > SingleBuffer;
00126        // typedef OctreePointCloud<PointT, LeafContainerT, BranchContainerT, Octree2BufBase<LeafContainerT> > DoubleBuffer;
00127 
00128         // Boost shared pointers
00129         typedef boost::shared_ptr<OctreePointCloud<PointT, LeafContainerT, BranchContainerT, OctreeT> > Ptr;
00130         typedef boost::shared_ptr<const OctreePointCloud<PointT, LeafContainerT, BranchContainerT, OctreeT> > ConstPtr;
00131 
00132         // Eigen aligned allocator
00133         typedef std::vector<PointT, Eigen::aligned_allocator<PointT> > AlignedPointTVector;
00134         typedef std::vector<PointXYZ, Eigen::aligned_allocator<PointXYZ> > AlignedPointXYZVector;
00135 
00140         inline void setInputCloud (const PointCloudConstPtr &cloud_arg,
00141             const IndicesConstPtr &indices_arg = IndicesConstPtr ())
00142         {
00143           input_ = cloud_arg;
00144           indices_ = indices_arg;
00145         }
00146 
00150         inline IndicesConstPtr const getIndices () const
00151         {
00152           return (indices_);
00153         }
00154 
00158         inline PointCloudConstPtr getInputCloud () const
00159         {
00160           return (input_);
00161         }
00162 
00166         inline void setEpsilon (double eps)
00167         {
00168           epsilon_ = eps;
00169         }
00170 
00172         inline double getEpsilon () const
00173         {
00174           return (epsilon_);
00175         }
00176 
00180         inline void setResolution (double resolution_arg)
00181         {
00182           // octree needs to be empty to change its resolution
00183           assert( this->leaf_count_ == 0);
00184 
00185           resolution_ = resolution_arg;
00186 
00187           getKeyBitSize ();
00188         }
00189 
00193         inline double getResolution () const
00194         {
00195           return (resolution_);
00196         }
00197 
00201         inline unsigned int getTreeDepth () const
00202         {
00203           return this->octree_depth_;
00204         }
00205 
00207         void
00208         addPointsFromInputCloud ();
00209 
00214         void
00215         addPointFromCloud (const int point_idx_arg, IndicesPtr indices_arg);
00216 
00221         void
00222         addPointToCloud (const PointT& point_arg, PointCloudPtr cloud_arg);
00223 
00229         void
00230         addPointToCloud (const PointT& point_arg, PointCloudPtr cloud_arg, IndicesPtr indices_arg);
00231 
00236         bool
00237         isVoxelOccupiedAtPoint (const PointT& point_arg) const;
00238 
00241         void deleteTree ()
00242         {
00243           // reset bounding box
00244           min_x_ = min_y_ = max_y_ = min_z_ = max_z_ = 0;
00245           this->bounding_box_defined_ = false;
00246 
00247           OctreeT::deleteTree ();
00248         }
00249 
00256         bool
00257         isVoxelOccupiedAtPoint (const double point_x_arg, const double point_y_arg, const double point_z_arg) const;
00258 
00263         bool
00264         isVoxelOccupiedAtPoint (const int& point_idx_arg) const;
00265 
00270         int
00271         getOccupiedVoxelCenters (AlignedPointTVector &voxel_center_list_arg) const;
00272 
00283         int
00284         getApproxIntersectedVoxelCentersBySegment (
00285             const Eigen::Vector3f& origin, const Eigen::Vector3f& end,
00286             AlignedPointTVector &voxel_center_list, float precision = 0.2);
00287 
00291         void
00292         deleteVoxelAtPoint (const PointT& point_arg);
00293 
00297         void
00298         deleteVoxelAtPoint (const int& point_idx_arg);
00299 
00301         // Bounding box methods
00303 
00305         void
00306         defineBoundingBox ();
00307 
00317         void
00318         defineBoundingBox (const double min_x_arg, const double min_y_arg, const double min_z_arg,
00319                            const double max_x_arg, const double max_y_arg, const double max_z_arg);
00320 
00328         void
00329         defineBoundingBox (const double max_x_arg, const double max_y_arg, const double max_z_arg);
00330 
00336         void
00337         defineBoundingBox (const double cubeLen_arg);
00338 
00348         void
00349         getBoundingBox (double& min_x_arg, double& min_y_arg, double& min_z_arg,
00350                         double& max_x_arg, double& max_y_arg, double& max_z_arg) const;
00351 
00356         double
00357         getVoxelSquaredDiameter (unsigned int tree_depth_arg) const;
00358 
00362         inline double
00363         getVoxelSquaredDiameter () const
00364         {
00365           return getVoxelSquaredDiameter (this->octree_depth_);
00366         }
00367 
00372         double
00373         getVoxelSquaredSideLen (unsigned int tree_depth_arg) const;
00374 
00378         inline double getVoxelSquaredSideLen () const
00379         {
00380           return getVoxelSquaredSideLen (this->octree_depth_);
00381         }
00382 
00388         inline void
00389         getVoxelBounds (OctreeIteratorBase<OctreeT>& iterator, Eigen::Vector3f &min_pt, Eigen::Vector3f &max_pt)
00390         {
00391           this->genVoxelBoundsFromOctreeKey (iterator.getCurrentOctreeKey (),
00392               iterator.getCurrentOctreeDepth (), min_pt, max_pt);
00393         }
00394 
00399         inline void
00400         enableDynamicDepth ( size_t maxObjsPerLeaf )
00401         {
00402           assert(this->leaf_count_==0);
00403           max_objs_per_leaf_ = maxObjsPerLeaf;
00404 
00405           this->dynamic_depth_enabled_ = static_cast<bool> (max_objs_per_leaf_>0);
00406         }
00407 
00408 
00409       protected:
00410 
00414         virtual void
00415         addPointIdx (const int point_idx_arg);
00416 
00423         void
00424         expandLeafNode (LeafNode* leaf_node, BranchNode* parent_branch, unsigned char child_idx, unsigned int depth_mask);
00425 
00430         const PointT&
00431         getPointByIndex (const unsigned int index_arg) const;
00432 
00437         LeafContainerT*
00438         findLeafAtPoint (const PointT& point_arg) const
00439         {
00440           OctreeKey key;
00441 
00442           // generate key for point
00443           this->genOctreeKeyforPoint (point_arg, key);
00444 
00445           return (this->findLeaf (key));
00446         }
00447 
00449         // Protected octree methods based on octree keys
00451 
00453         void
00454         getKeyBitSize ();
00455 
00459         void
00460         adoptBoundingBoxToPoint (const PointT& point_idx_arg);
00461 
00466         inline bool isPointWithinBoundingBox (const PointT& point_idx_arg) const
00467         {
00468           return (! ( (point_idx_arg.x < min_x_) || (point_idx_arg.y < min_y_)
00469                    || (point_idx_arg.z < min_z_) || (point_idx_arg.x >= max_x_)
00470                    || (point_idx_arg.y >= max_y_) || (point_idx_arg.z >= max_z_)));
00471         }
00472 
00477         void
00478         genOctreeKeyforPoint (const PointT & point_arg,
00479             OctreeKey &key_arg) const;
00480 
00487         void
00488         genOctreeKeyforPoint (const double point_x_arg, const double point_y_arg, const double point_z_arg,
00489                               OctreeKey & key_arg) const;
00490 
00497         virtual bool
00498         genOctreeKeyForDataT (const int& data_arg, OctreeKey & key_arg) const;
00499 
00504         void
00505         genLeafNodeCenterFromOctreeKey (const OctreeKey & key_arg,
00506             PointT& point_arg) const;
00507 
00513         void
00514         genVoxelCenterFromOctreeKey (const OctreeKey & key_arg,
00515             unsigned int tree_depth_arg, PointT& point_arg) const;
00516 
00523         void
00524         genVoxelBoundsFromOctreeKey (const OctreeKey & key_arg,
00525             unsigned int tree_depth_arg, Eigen::Vector3f &min_pt,
00526             Eigen::Vector3f &max_pt) const;
00527 
00534         int
00535         getOccupiedVoxelCentersRecursive (const BranchNode* node_arg,
00536             const OctreeKey& key_arg,
00537             AlignedPointTVector &voxel_center_list_arg) const;
00538 
00540         // Globals
00542 
00543         PointCloudConstPtr input_;
00544 
00546         IndicesConstPtr indices_;
00547 
00549         double epsilon_;
00550 
00552         double resolution_;
00553 
00554         // Octree bounding box coordinates
00555         double min_x_;
00556         double max_x_;
00557 
00558         double min_y_;
00559         double max_y_;
00560 
00561         double min_z_;
00562         double max_z_;
00563 
00565         bool bounding_box_defined_;
00566 
00570         std::size_t max_objs_per_leaf_;
00571     };
00572 
00573   }
00574 }
00575 
00576 #ifdef PCL_NO_PRECOMPILE
00577 #include <pcl/octree/impl/octree_pointcloud.hpp>
00578 #endif
00579 
00580 #endif
00581 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:26:17