octree_base.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-2012, Willow Garage, Inc.
00006  *  Copyright (c) 2012, Urban Robotics, Inc.
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of Willow Garage, Inc. nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  *  $Id$
00038  */
00039 
00040 #ifndef PCL_OUTOFCORE_OUTOFCOREOCTREEBASE_H_
00041 #define PCL_OUTOFCORE_OUTOFCOREOCTREEBASE_H_
00042 
00043 #include <pcl/outofcore/boost.h>
00044 #include <pcl/common/io.h>
00045 
00046 //outofcore classes
00047 #include <pcl/outofcore/octree_base_node.h>
00048 #include <pcl/outofcore/octree_disk_container.h>
00049 #include <pcl/outofcore/octree_ram_container.h>
00050 
00051 //outofcore iterators
00052 #include <pcl/outofcore/outofcore_iterator_base.h>
00053 #include <pcl/outofcore/outofcore_breadth_first_iterator.h>
00054 #include <pcl/outofcore/outofcore_depth_first_iterator.h>
00055 #include <pcl/outofcore/impl/outofcore_breadth_first_iterator.hpp>
00056 #include <pcl/outofcore/impl/outofcore_depth_first_iterator.hpp>
00057 
00058 //outofcore metadata
00059 #include <pcl/outofcore/metadata.h>
00060 #include <pcl/outofcore/outofcore_base_data.h>
00061 
00062 #include <pcl/filters/filter.h>
00063 #include <pcl/filters/random_sample.h>
00064 
00065 #include <pcl/PCLPointCloud2.h>
00066 
00067 namespace pcl
00068 {
00069   namespace outofcore
00070   {
00071     struct OutofcoreParams
00072     {
00073       std::string node_index_basename_;
00074       std::string node_container_basename_;
00075       std::string node_index_extension_;
00076       std::string node_container_extension_;
00077       double sample_percent;
00078     };
00079     
00148     template<typename ContainerT = OutofcoreOctreeDiskContainer<pcl::PointXYZ>, typename PointT = pcl::PointXYZ>
00149     class OutofcoreOctreeBase
00150     {
00151       friend class OutofcoreOctreeBaseNode<ContainerT, PointT>;
00152       friend class pcl::outofcore::OutofcoreIteratorBase<PointT, ContainerT>;
00153 
00154       public:
00155 
00156         // public typedefs
00157         typedef OutofcoreOctreeBase<OutofcoreOctreeDiskContainer<PointT>, PointT > octree_disk;
00158         typedef OutofcoreOctreeBaseNode<OutofcoreOctreeDiskContainer<PointT>, PointT > octree_disk_node;
00159 
00160         typedef OutofcoreOctreeBase<OutofcoreOctreeRamContainer<PointT>, PointT> octree_ram;
00161         typedef OutofcoreOctreeBaseNode<OutofcoreOctreeRamContainer<PointT>, PointT> octree_ram_node;
00162 
00163         typedef OutofcoreOctreeBaseNode<ContainerT, PointT> OutofcoreNodeType;
00164 
00165         typedef OutofcoreOctreeBaseNode<ContainerT, PointT> BranchNode;
00166         typedef OutofcoreOctreeBaseNode<ContainerT, PointT> LeafNode;
00167 
00168         typedef OutofcoreDepthFirstIterator<PointT, ContainerT> Iterator;
00169         typedef const OutofcoreDepthFirstIterator<PointT, ContainerT> ConstIterator;
00170 
00171         typedef OutofcoreBreadthFirstIterator<PointT, ContainerT> BreadthFirstIterator;
00172         typedef const OutofcoreBreadthFirstIterator<PointT, ContainerT> BreadthFirstConstIterator;
00173 
00174         typedef OutofcoreDepthFirstIterator<PointT, ContainerT> DepthFirstIterator;
00175         typedef const OutofcoreDepthFirstIterator<PointT, ContainerT> DepthFirstConstIterator;
00176 
00177         typedef boost::shared_ptr<OutofcoreOctreeBase<ContainerT, PointT> > Ptr;
00178         typedef boost::shared_ptr<const OutofcoreOctreeBase<ContainerT, PointT> > ConstPtr;
00179 
00180         typedef pcl::PointCloud<PointT> PointCloud;
00181 
00182         typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
00183         typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
00184 
00185         typedef boost::shared_ptr<PointCloud> PointCloudPtr;
00186         typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
00187 
00188         typedef std::vector<PointT, Eigen::aligned_allocator<PointT> > AlignedPointTVector;
00189 
00190         // Constructors
00191         // -----------------------------------------------------------------------
00192 
00203         OutofcoreOctreeBase (const boost::filesystem::path &root_node_name, const bool load_all);
00204 
00218         OutofcoreOctreeBase (const Eigen::Vector3d& min, const Eigen::Vector3d& max, const double resolution_arg, const boost::filesystem::path &root_node_name, const std::string &coord_sys);
00219 
00233         OutofcoreOctreeBase (const boost::uint64_t max_depth, const Eigen::Vector3d &min, const Eigen::Vector3d &max, const boost::filesystem::path &root_node_name, const std::string &coord_sys);
00234 
00235         virtual
00236         ~OutofcoreOctreeBase ();
00237 
00238         // Point/Region INSERTION methods
00239         // --------------------------------------------------------------------------------
00243         boost::uint64_t
00244         addDataToLeaf (const AlignedPointTVector &p);
00245 
00252         boost::uint64_t
00253         addPointCloud (PointCloudConstPtr point_cloud);
00254 
00261         boost::uint64_t
00262         addPointCloud (pcl::PCLPointCloud2::Ptr &input_cloud, const bool skip_bb_check = false);
00263 
00281         boost::uint64_t
00282         addPointCloud_and_genLOD (pcl::PCLPointCloud2::Ptr &input_cloud);
00283 
00284         boost::uint64_t
00285         addPointCloud (pcl::PCLPointCloud2::Ptr &input_cloud);
00286         
00287         boost::uint64_t
00288         addPointCloud_and_genLOD (PointCloudConstPtr point_cloud);
00289 
00294         boost::uint64_t
00295         addDataToLeaf_and_genLOD (AlignedPointTVector &p);
00296 
00297         // Frustrum/Box/Region REQUESTS/QUERIES: DB Accessors
00298         // -----------------------------------------------------------------------
00299         void
00300         queryFrustum (const double *planes, std::list<std::string>& file_names) const;
00301 
00302               void
00303         queryFrustum (const double *planes, std::list<std::string>& file_names, const boost::uint32_t query_depth) const;
00304 
00305               void
00306         queryFrustum (const double *planes, const Eigen::Vector3d &eye, const Eigen::Matrix4d &view_projection_matrix,
00307                       std::list<std::string>& file_names, const boost::uint32_t query_depth) const;
00308         
00309         //--------------------------------------------------------------------------------
00310         //templated PointT methods
00311         //--------------------------------------------------------------------------------
00312 
00320         void
00321         queryBBIntersects (const Eigen::Vector3d &min, const Eigen::Vector3d &max, const boost::uint32_t query_depth, std::list<std::string> &bin_name) const;
00322 
00334         void
00335         queryBBIncludes (const Eigen::Vector3d &min, const Eigen::Vector3d &max, const boost::uint64_t query_depth, AlignedPointTVector &dst) const;
00336 
00344         void
00345         queryBBIncludes (const Eigen::Vector3d &min, const Eigen::Vector3d &max, const boost::uint64_t query_depth, const pcl::PCLPointCloud2::Ptr &dst_blob) const;
00346         
00355         void
00356         queryBBIncludes_subsample (const Eigen::Vector3d &min, const Eigen::Vector3d &max, uint64_t query_depth, const double percent, AlignedPointTVector &dst) const;
00357 
00358         //--------------------------------------------------------------------------------
00359         //PCLPointCloud2 methods
00360         //--------------------------------------------------------------------------------
00361 
00369         virtual void
00370         queryBoundingBox (const Eigen::Vector3d &min, const Eigen::Vector3d &max, const int query_depth, const pcl::PCLPointCloud2::Ptr &dst_blob, double percent = 1.0);
00371         
00377         inline virtual void
00378         queryBoundingBox (const Eigen::Vector3d &min, const Eigen::Vector3d &max, const int query_depth, std::list<std::string> &filenames) const
00379         {
00380           boost::shared_lock < boost::shared_mutex > lock (read_write_mutex_);
00381           filenames.clear ();
00382           this->root_node_->queryBBIntersects (min, max, query_depth, filenames);
00383         }
00384 
00385         // Parameterization: getters and setters
00386         // --------------------------------------------------------------------------------
00387 
00390         bool
00391         getBoundingBox (Eigen::Vector3d &min, Eigen::Vector3d &max) const;
00392 
00397         inline boost::uint64_t
00398         getNumPointsAtDepth (const boost::uint64_t& depth_index) const
00399         {
00400           return (metadata_->getLODPoints (depth_index));
00401         }
00402 
00411         boost::uint64_t
00412         queryBoundingBoxNumPoints (const Eigen::Vector3d& min, const Eigen::Vector3d& max, const int query_depth, bool load_from_disk = true);
00413         
00414 
00418         inline const std::vector<boost::uint64_t>&
00419         getNumPointsVector () const
00420         {
00421           return (metadata_->getLODPoints ());
00422         }
00423 
00426         inline boost::uint64_t
00427         getDepth () const
00428         {
00429           return (metadata_->getDepth ());
00430         }
00431 
00432         inline boost::uint64_t
00433         getTreeDepth () const
00434         {
00435           return (this->getDepth ());
00436         }
00437 
00440         bool
00441         getBinDimension (double &x, double &y) const;
00442 
00447         double
00448         getVoxelSideLength (const boost::uint64_t& depth) const;
00449 
00453         double
00454         getVoxelSideLength () const
00455         {
00456           return (this->getVoxelSideLength (metadata_->getDepth ()));
00457         }
00458 
00461         const std::string&
00462         getCoordSystem () const
00463         {
00464           return (metadata_->getCoordinateSystem ());
00465         }
00466 
00467         // Mutators
00468         // -----------------------------------------------------------------------
00469 
00472         void
00473         buildLOD ();
00474 
00477         void
00478         printBoundingBox (const size_t query_depth) const;
00479 
00481         void
00482         printBoundingBox (OutofcoreNodeType& node) const;
00483 
00486         inline void
00487         printBoundingBox() const
00488         {
00489           this->printBoundingBox (metadata_->getDepth ());
00490         }
00491 
00496         void
00497         getOccupiedVoxelCenters(AlignedPointTVector &voxel_centers, size_t query_depth) const;
00498 
00503         void
00504         getOccupiedVoxelCenters(std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &voxel_centers, size_t query_depth) const;
00505 
00507         void
00508         getOccupiedVoxelCenters(AlignedPointTVector &voxel_centers) const
00509         {
00510           getOccupiedVoxelCenters(voxel_centers, metadata_->getDepth ());
00511         }
00512 
00516         void
00517         getOccupiedVoxelCenters(std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > &voxel_centers) const
00518         {
00519           getOccupiedVoxelCenters(voxel_centers, metadata_->getDepth ());
00520         }
00521 
00522         // Serializers
00523         // -----------------------------------------------------------------------
00524 
00526         void
00527         convertToXYZ ();
00528 
00531         void
00532         writeVPythonVisual (const boost::filesystem::path filename);
00533 
00534         OutofcoreNodeType*
00535         getBranchChildPtr (const BranchNode& branch_arg, unsigned char childIdx_arg) const;
00536 
00537         pcl::Filter<pcl::PCLPointCloud2>::Ptr
00538         getLODFilter ();
00539 
00540         const pcl::Filter<pcl::PCLPointCloud2>::ConstPtr
00541         getLODFilter () const;
00542 
00544         void
00545         setLODFilter (const pcl::Filter<pcl::PCLPointCloud2>::Ptr& filter_arg);
00546 
00548         double 
00549         getSamplePercent () const
00550         {
00551           return (sample_percent_);
00552         }
00553         
00556         inline void 
00557         setSamplePercent (const double sample_percent_arg)
00558         {
00559           this->sample_percent_ = std::fabs (sample_percent_arg) > 1.0 ? 1.0 : std::fabs (sample_percent_arg);
00560         }
00561         
00562       protected:
00563         void
00564         init (const boost::uint64_t& depth, const Eigen::Vector3d& min, const Eigen::Vector3d& max, const boost::filesystem::path& root_name, const std::string& coord_sys);
00565 
00566         OutofcoreOctreeBase (OutofcoreOctreeBase &rval);
00567 
00568         OutofcoreOctreeBase (const OutofcoreOctreeBase &rval);
00569 
00570         OutofcoreOctreeBase&
00571         operator= (OutofcoreOctreeBase &rval);
00572 
00573         OutofcoreOctreeBase&
00574         operator= (const OutofcoreOctreeBase &rval);
00575 
00576         inline OutofcoreNodeType*
00577         getRootNode ()
00578         {
00579           return (this->root_node_);
00580         }
00581 
00583         void
00584         DeAllocEmptyNodeCache (OutofcoreNodeType* current);
00585 
00587         void
00588         saveToFile ();
00589 
00591         void
00592         buildLODRecursive (const std::vector<BranchNode*>& current_branch);
00593 
00596         inline void
00597         incrementPointsInLOD (boost::uint64_t depth, boost::uint64_t inc);
00598 
00603         bool
00604         checkExtension (const boost::filesystem::path& path_name);
00605 
00606 
00610         void
00611         flushToDisk ();
00612 
00616         void
00617         flushToDiskLazy ();
00618 
00622         void
00623         DeAllocEmptyNodeCache ();
00624 
00626         OutofcoreNodeType* root_node_;
00627 
00629         mutable boost::shared_mutex read_write_mutex_;
00630 
00631         boost::shared_ptr<OutofcoreOctreeBaseMetadata> metadata_;
00632         
00636         const static std::string TREE_EXTENSION_;
00637         const static int OUTOFCORE_VERSION_;
00638 
00639         const static uint64_t LOAD_COUNT_ = static_cast<uint64_t>(2e9);
00640 
00641       private:    
00642 
00644         void
00645         enlargeToCube (Eigen::Vector3d &bb_min, Eigen::Vector3d &bb_max);
00646 
00648         boost::uint64_t
00649         calculateDepth (const Eigen::Vector3d& min_bb, const Eigen::Vector3d& max_bb, const double leaf_resolution);
00650 
00651         double sample_percent_;
00652 
00653         pcl::RandomSample<pcl::PCLPointCloud2>::Ptr lod_filter_ptr_;
00654         
00655     };
00656   }
00657 }
00658 
00659   
00660 #endif // PCL_OUTOFCORE_OUTOFCOREOCTREEBASE_H_


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