pcl_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  *
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: pcl_base.h 5295 2012-03-25 19:03:32Z rusu $
00037  *
00038  */
00039 #ifndef PCL_PCL_BASE_H_
00040 #define PCL_PCL_BASE_H_
00041 
00042 #include <cstddef>
00043 // Eigen includes
00044 #include <pcl/common/eigen.h>
00045 // STD includes
00046 #include <vector>
00047 
00048 // Include PCL macros such as PCL_ERROR, etc
00049 #include <pcl/pcl_macros.h>
00050 
00051 // Boost includes. Needed everywhere.
00052 #include <boost/shared_ptr.hpp>
00053 
00054 // Point Cloud message includes. Needed everywhere.
00055 #include <sensor_msgs/PointCloud2.h>
00056 #include <pcl/point_cloud.h>
00057 #include <pcl/PointIndices.h>
00058 #include <pcl/console/print.h>
00059 
00060 namespace pcl
00061 {
00062   // definitions used everywhere
00063   typedef boost::shared_ptr <std::vector<int> > IndicesPtr;
00064   typedef boost::shared_ptr <const std::vector<int> > IndicesConstPtr;
00065 
00067 
00070   template <typename PointT>
00071   class PCLBase
00072   {
00073     public:
00074       typedef pcl::PointCloud<PointT> PointCloud;
00075       typedef typename PointCloud::Ptr PointCloudPtr;
00076       typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00077 
00078       typedef PointIndices::Ptr PointIndicesPtr;
00079       typedef PointIndices::ConstPtr PointIndicesConstPtr;
00080 
00082       PCLBase () : input_ (), indices_ (), use_indices_ (false), fake_indices_ (false) {}
00083       
00085       PCLBase (const PCLBase& base)
00086         : input_ (base.input_)
00087         , indices_ (base.indices_)
00088         , use_indices_ (base.use_indices_)
00089         , fake_indices_ (base.fake_indices_)
00090       {}
00091 
00093       virtual ~PCLBase() 
00094       {
00095         input_.reset ();
00096         indices_.reset ();
00097       }
00098       
00102       virtual inline void 
00103       setInputCloud (const PointCloudConstPtr &cloud) { input_ = cloud; }
00104 
00106       inline PointCloudConstPtr const 
00107       getInputCloud () { return (input_); }
00108 
00112       inline void
00113       setIndices (const IndicesPtr &indices)
00114       {
00115         indices_ = indices;
00116         fake_indices_ = false;
00117         use_indices_  = true;
00118       }
00119 
00123       inline void
00124       setIndices (const IndicesConstPtr &indices)
00125       {
00126         indices_.reset (new std::vector<int> (*indices));
00127         fake_indices_ = false;
00128         use_indices_  = true;
00129       }
00130 
00134       inline void
00135       setIndices (const PointIndicesConstPtr &indices)
00136       {
00137         indices_.reset (new std::vector<int> (indices->indices));
00138         fake_indices_ = false;
00139         use_indices_  = true;
00140       }
00141 
00150       inline void 
00151       setIndices (size_t row_start, size_t col_start, size_t nb_rows, size_t nb_cols)
00152       {
00153         if ((nb_rows > input_->height) || (row_start > input_->height))
00154         {
00155           PCL_ERROR ("[PCLBase::setIndices] cloud is only %d height", input_->height);
00156           return;
00157         }
00158 
00159         if ((nb_cols > input_->width) || (col_start > input_->width))
00160         {
00161           PCL_ERROR ("[PCLBase::setIndices] cloud is only %d width", input_->width);
00162           return;
00163         }
00164 
00165         size_t row_end = row_start + nb_rows;
00166         if (row_end > input_->height)
00167         {
00168           PCL_ERROR ("[PCLBase::setIndices] %d is out of rows range %d", row_end, input_->height);
00169           return;
00170         }
00171 
00172         size_t col_end = col_start + nb_cols;
00173         if (col_end > input_->width)
00174         {
00175           PCL_ERROR ("[PCLBase::setIndices] %d is out of columns range %d", col_end, input_->width);
00176           return;
00177         }
00178 
00179         indices_.reset (new std::vector<int>);
00180         indices_->reserve (nb_cols * nb_rows);
00181         for(size_t i = row_start; i < row_end; i++)
00182           for(size_t j = col_start; j < col_end; j++)
00183             indices_->push_back (static_cast<int> ((i * input_->width) + j));
00184         fake_indices_ = false;
00185         use_indices_  = true;
00186       }
00187 
00189       inline IndicesPtr const 
00190       getIndices () { return (indices_); }
00191 
00197       const PointT& operator[] (size_t pos)
00198       {
00199         return ((*input_)[(*indices_)[pos]]);
00200       }
00201 
00202     protected:
00204       PointCloudConstPtr input_;
00205 
00207       IndicesPtr indices_;
00208 
00210       bool use_indices_;
00211 
00213       bool fake_indices_;
00214 
00224       inline bool
00225       initCompute ()
00226       {
00227         // Check if input was set
00228         if (!input_)
00229           return (false);
00230 
00231         // If no point indices have been given, construct a set of indices for the entire input point cloud
00232         if (!indices_)
00233         {
00234           fake_indices_ = true;
00235           indices_.reset (new std::vector<int>);
00236           try
00237           {
00238             indices_->resize (input_->points.size ());
00239           }
00240           catch (std::bad_alloc)
00241           {
00242             PCL_ERROR ("[initCompute] Failed to allocate %zu indices.\n", input_->points.size ());
00243           }
00244           for (size_t i = 0; i < indices_->size (); ++i) { (*indices_)[i] = static_cast<int>(i); }
00245         }
00246 
00247         // If we have a set of fake indices, but they do not match the number of points in the cloud, update them
00248         if (fake_indices_ && indices_->size () != input_->points.size ())
00249         {
00250           size_t indices_size = indices_->size ();
00251           indices_->resize (input_->points.size ());
00252           for (size_t i = indices_size; i < indices_->size (); ++i) { (*indices_)[i] = static_cast<int>(i); }
00253         }
00254 
00255         return (true);
00256       }
00257 
00261       inline bool
00262       deinitCompute ()
00263       {
00264         return (true);
00265       }
00266     public:
00267       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00268   };
00269 
00271   template <>
00272   class PCL_EXPORTS PCLBase<sensor_msgs::PointCloud2>
00273   {
00274     public:
00275       typedef sensor_msgs::PointCloud2 PointCloud2;
00276       typedef PointCloud2::Ptr PointCloud2Ptr;
00277       typedef PointCloud2::ConstPtr PointCloud2ConstPtr;
00278 
00279       typedef PointIndices::Ptr PointIndicesPtr;
00280       typedef PointIndices::ConstPtr PointIndicesConstPtr;
00281 
00283       PCLBase () : input_ (), indices_ (), use_indices_ (false), fake_indices_ (false),
00284                    field_sizes_ (0), x_idx_ (-1), y_idx_ (-1), z_idx_ (-1),
00285                    x_field_name_ ("x"), y_field_name_ ("y"), z_field_name_ ("z")
00286       {};
00287 
00289       virtual ~PCLBase() 
00290       {
00291         input_.reset ();
00292         indices_.reset ();
00293       }
00294 
00298       void 
00299       setInputCloud (const PointCloud2ConstPtr &cloud);
00300 
00302       inline PointCloud2ConstPtr const 
00303       getInputCloud () { return (input_); }
00304 
00308       inline void
00309       setIndices (const IndicesPtr &indices)
00310       {
00311         indices_ = indices;
00312         fake_indices_ = false;
00313         use_indices_  = true;
00314       }
00315 
00319       inline void
00320       setIndices (const PointIndicesConstPtr &indices)
00321       {
00322         indices_.reset (new std::vector<int> (indices->indices));
00323         fake_indices_ = false;
00324         use_indices_  = true;
00325       }
00326 
00328       inline IndicesPtr const 
00329       getIndices () { return (indices_); }
00330 
00331     protected:
00333       PointCloud2ConstPtr input_;
00334 
00336       IndicesPtr indices_;
00337 
00339       bool use_indices_;
00340 
00342       bool fake_indices_;
00343 
00345       std::vector<int> field_sizes_;
00346 
00348       int x_idx_, y_idx_, z_idx_;
00349 
00351       std::string x_field_name_, y_field_name_, z_field_name_;
00352 
00353       bool initCompute ();
00354       bool deinitCompute ();
00355     public:
00356       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00357   };
00358 }
00359 
00360 #endif  //#ifndef PCL_PCL_BASE_H_


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:16:27