Point Cloud Library (PCL)  1.9.1-dev
filter.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/pcl_base.h>
43 #include <pcl/common/io.h>
44 #include <pcl/conversions.h>
45 #include <pcl/filters/boost.h>
46 #include <cfloat>
47 #include <pcl/PointIndices.h>
48 
49 namespace pcl
50 {
51  /** \brief Removes points with x, y, or z equal to NaN
52  * \param[in] cloud_in the input point cloud
53  * \param[out] cloud_out the output point cloud
54  * \param[out] index the mapping (ordered): cloud_out.points[i] = cloud_in.points[index[i]]
55  * \note The density of the point cloud is lost.
56  * \note Can be called with cloud_in == cloud_out
57  * \ingroup filters
58  */
59  template<typename PointT> void
61  pcl::PointCloud<PointT> &cloud_out,
62  std::vector<int> &index);
63 
64  /** \brief Removes points that have their normals invalid (i.e., equal to NaN)
65  * \param[in] cloud_in the input point cloud
66  * \param[out] cloud_out the output point cloud
67  * \param[out] index the mapping (ordered): cloud_out.points[i] = cloud_in.points[index[i]]
68  * \note The density of the point cloud is lost.
69  * \note Can be called with cloud_in == cloud_out
70  * \ingroup filters
71  */
72  template<typename PointT> void
74  pcl::PointCloud<PointT> &cloud_out,
75  std::vector<int> &index);
76 
77  ////////////////////////////////////////////////////////////////////////////////////////////
78  /** \brief Filter represents the base filter class. All filters must inherit from this interface.
79  * \author Radu B. Rusu
80  * \ingroup filters
81  */
82  template<typename PointT>
83  class Filter : public PCLBase<PointT>
84  {
85  public:
88 
89  typedef boost::shared_ptr< Filter<PointT> > Ptr;
90  typedef boost::shared_ptr< const Filter<PointT> > ConstPtr;
91 
92 
94  typedef typename PointCloud::Ptr PointCloudPtr;
96 
97  /** \brief Empty constructor.
98  * \param[in] extract_removed_indices set to true if the filtered data indices should be saved in a
99  * separate list. Default: false.
100  */
101  Filter (bool extract_removed_indices = false) :
102  removed_indices_ (new std::vector<int>),
103  filter_name_ (),
104  extract_removed_indices_ (extract_removed_indices)
105  {
106  }
107 
108  /** \brief Empty destructor */
109  ~Filter () {}
110 
111  /** \brief Get the point indices being removed */
112  inline IndicesConstPtr const
114  {
115  return (removed_indices_);
116  }
117 
118  /** \brief Get the point indices being removed
119  * \param[out] pi the resultant point indices that have been removed
120  */
121  inline void
123  {
125  }
126 
127  /** \brief Calls the filtering method and returns the filtered dataset in output.
128  * \param[out] output the resultant filtered point cloud dataset
129  */
130  inline void
131  filter (PointCloud &output)
132  {
133  if (!initCompute ())
134  return;
135 
136  if (input_.get () == &output) // cloud_in = cloud_out
137  {
138  PointCloud output_temp;
139  applyFilter (output_temp);
140  output_temp.header = input_->header;
141  output_temp.sensor_origin_ = input_->sensor_origin_;
142  output_temp.sensor_orientation_ = input_->sensor_orientation_;
143  pcl::copyPointCloud (output_temp, output);
144  }
145  else
146  {
147  output.header = input_->header;
148  output.sensor_origin_ = input_->sensor_origin_;
149  output.sensor_orientation_ = input_->sensor_orientation_;
150  applyFilter (output);
151  }
152 
153  deinitCompute ();
154  }
155 
156  protected:
157 
160 
161  /** \brief Indices of the points that are removed */
163 
164  /** \brief The filter name. */
165  std::string filter_name_;
166 
167  /** \brief Set to true if we want to return the indices of the removed points. */
169 
170  /** \brief Abstract filter method.
171  *
172  * The implementation needs to set output.{points, width, height, is_dense}.
173  *
174  * \param[out] output the resultant filtered point cloud
175  */
176  virtual void
177  applyFilter (PointCloud &output) = 0;
178 
179  /** \brief Get a string representation of the name of this class. */
180  inline const std::string&
181  getClassName () const
182  {
183  return (filter_name_);
184  }
185  };
186 
187  ////////////////////////////////////////////////////////////////////////////////////////////
188  /** \brief Filter represents the base filter class. All filters must inherit from this interface.
189  * \author Radu B. Rusu
190  * \ingroup filters
191  */
192  template<>
193  class PCL_EXPORTS Filter<pcl::PCLPointCloud2> : public PCLBase<pcl::PCLPointCloud2>
194  {
195  public:
196  typedef boost::shared_ptr< Filter<pcl::PCLPointCloud2> > Ptr;
197  typedef boost::shared_ptr< const Filter<pcl::PCLPointCloud2> > ConstPtr;
198 
202 
203  /** \brief Empty constructor.
204  * \param[in] extract_removed_indices set to true if the filtered data indices should be saved in a
205  * separate list. Default: false.
206  */
207  Filter (bool extract_removed_indices = false) :
208  removed_indices_ (new std::vector<int>),
209  extract_removed_indices_ (extract_removed_indices),
210  filter_name_ ()
211  {
212  }
213 
214  /** \brief Empty destructor */
215  ~Filter () {}
216 
217  /** \brief Get the point indices being removed */
218  inline IndicesConstPtr const
220  {
221  return (removed_indices_);
222  }
223 
224  /** \brief Get the point indices being removed
225  * \param[out] pi the resultant point indices that have been removed
226  */
227  inline void
229  {
231  }
232 
233  /** \brief Calls the filtering method and returns the filtered dataset in output.
234  * \param[out] output the resultant filtered point cloud dataset
235  */
236  void
237  filter (PCLPointCloud2 &output);
238 
239  protected:
240 
241  /** \brief Indices of the points that are removed */
243 
244  /** \brief Set to true if we want to return the indices of the removed points. */
246 
247  /** \brief The filter name. */
248  std::string filter_name_;
249 
250  /** \brief Abstract filter method.
251  *
252  * The implementation needs to set output.{data, row_step, point_step, width, height, is_dense}.
253  *
254  * \param[out] output the resultant filtered point cloud
255  */
256  virtual void
257  applyFilter (PCLPointCloud2 &output) = 0;
258 
259  /** \brief Get a string representation of the name of this class. */
260  inline const std::string&
261  getClassName () const
262  {
263  return (filter_name_);
264  }
265  };
266 }
267 
268 #ifdef PCL_NO_PRECOMPILE
269 #include <pcl/filters/impl/filter.hpp>
270 #endif
void removeNaNNormalsFromPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, std::vector< int > &index)
Removes points that have their normals invalid (i.e., equal to NaN)
Definition: filter.hpp:99
void getRemovedIndices(PointIndices &pi)
Get the point indices being removed.
Definition: filter.h:228
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: filter.h:201
std::string filter_name_
The filter name.
Definition: filter.h:248
boost::shared_ptr< std::vector< int > > IndicesPtr
Definition: pcl_base.h:60
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
void removeNaNFromPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, std::vector< int > &index)
Removes points with x, y, or z equal to NaN.
Definition: filter.hpp:46
pcl::PointCloud< PointT > PointCloud
Definition: filter.h:93
pcl::PCLPointCloud2 PCLPointCloud2
Definition: filter.h:199
boost::shared_ptr< const Filter< pcl::PCLPointCloud2 > > ConstPtr
Definition: filter.h:197
std::vector< int > indices
Definition: PointIndices.h:19
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
void filter(PointCloud &output)
Calls the filtering method and returns the filtered dataset in output.
Definition: filter.h:131
~Filter()
Empty destructor.
Definition: filter.h:109
Filter(bool extract_removed_indices=false)
Empty constructor.
Definition: filter.h:101
boost::shared_ptr< Filter< PointT > > Ptr
Definition: filter.h:89
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
bool initCompute()
This method should get called before starting the actual computation.
Definition: pcl_base.hpp:139
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:427
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:420
Filter(bool extract_removed_indices=false)
Empty constructor.
Definition: filter.h:207
Filter represents the base filter class.
Definition: filter.h:83
void getRemovedIndices(PointIndices &pi)
Get the point indices being removed.
Definition: filter.h:122
PCL base class.
Definition: pcl_base.h:68
IndicesConstPtr const getRemovedIndices() const
Get the point indices being removed.
Definition: filter.h:113
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition: filter.h:162
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:422
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:428
PointCloud::ConstPtr PointCloudConstPtr
Definition: filter.h:95
boost::shared_ptr< ::pcl::PCLPointCloud2 const > ConstPtr
bool deinitCompute()
This method should get called after finishing the actual computation.
Definition: pcl_base.hpp:174
PointCloud represents the base class in PCL for storing collections of 3D points. ...
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:406
boost::shared_ptr< Filter< pcl::PCLPointCloud2 > > Ptr
Definition: filter.h:196
boost::shared_ptr< const std::vector< int > > IndicesConstPtr
Definition: pcl_base.h:61
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition: filter.h:245
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition: filter.h:168
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: filter.h:200
PointCloud::Ptr PointCloudPtr
Definition: filter.h:94
virtual void applyFilter(PointCloud &output)=0
Abstract filter method.
IndicesConstPtr const getRemovedIndices() const
Get the point indices being removed.
Definition: filter.h:219
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:150
std::string filter_name_
The filter name.
Definition: filter.h:165
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: filter.h:261
boost::shared_ptr< const Filter< PointT > > ConstPtr
Definition: filter.h:90
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: filter.h:181
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition: filter.h:242
~Filter()
Empty destructor.
Definition: filter.h:215