Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef PCL_PCL_BASE_H_
00040 #define PCL_PCL_BASE_H_
00041
00042 #include <cstddef>
00043
00044 #include <pcl/common/eigen.h>
00045
00046 #include <vector>
00047
00048
00049 #include <pcl/pcl_macros.h>
00050
00051
00052 #include <boost/shared_ptr.hpp>
00053
00054
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
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
00228 if (!input_)
00229 return (false);
00230
00231
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
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_