Point Cloud Library (PCL)  1.8.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 #ifndef PCL_FILTER_H_
41 #define PCL_FILTER_H_
42 
43 #include <pcl/pcl_base.h>
44 #include <pcl/common/io.h>
45 #include <pcl/conversions.h>
46 #include <pcl/filters/boost.h>
47 #include <cfloat>
48 #include <pcl/PointIndices.h>
49 
50 namespace pcl
51 {
52  /** \brief Removes points with x, y, or z equal to NaN
53  * \param[in] cloud_in the input point cloud
54  * \param[out] cloud_out the input point cloud
55  * \param[out] index the mapping (ordered): cloud_out.points[i] = cloud_in.points[index[i]]
56  * \note The density of the point cloud is lost.
57  * \note Can be called with cloud_in == cloud_out
58  * \ingroup filters
59  */
60  template<typename PointT> void
62  pcl::PointCloud<PointT> &cloud_out,
63  std::vector<int> &index);
64 
65  /** \brief Removes points that have their normals invalid (i.e., equal to NaN)
66  * \param[in] cloud_in the input point cloud
67  * \param[out] cloud_out the input point cloud
68  * \param[out] index the mapping (ordered): cloud_out.points[i] = cloud_in.points[index[i]]
69  * \note The density of the point cloud is lost.
70  * \note Can be called with cloud_in == cloud_out
71  * \ingroup filters
72  */
73  template<typename PointT> void
75  pcl::PointCloud<PointT> &cloud_out,
76  std::vector<int> &index);
77 
78  ////////////////////////////////////////////////////////////////////////////////////////////
79  /** \brief Filter represents the base filter class. All filters must inherit from this interface.
80  * \author Radu B. Rusu
81  * \ingroup filters
82  */
83  template<typename PointT>
84  class Filter : public PCLBase<PointT>
85  {
86  public:
89 
90  typedef boost::shared_ptr< Filter<PointT> > Ptr;
91  typedef boost::shared_ptr< const Filter<PointT> > ConstPtr;
92 
93 
95  typedef typename PointCloud::Ptr PointCloudPtr;
97 
98  /** \brief Empty constructor.
99  * \param[in] extract_removed_indices set to true if the filtered data indices should be saved in a
100  * separate list. Default: false.
101  */
102  Filter (bool extract_removed_indices = false) :
103  removed_indices_ (new std::vector<int>),
104  filter_name_ (),
105  extract_removed_indices_ (extract_removed_indices)
106  {
107  }
108 
109  /** \brief Empty destructor */
110  virtual ~Filter () {}
111 
112  /** \brief Get the point indices being removed */
113  inline IndicesConstPtr const
115  {
116  return (removed_indices_);
117  }
118 
119  /** \brief Get the point indices being removed
120  * \param[out] pi the resultant point indices that have been removed
121  */
122  inline void
124  {
126  }
127 
128  /** \brief Calls the filtering method and returns the filtered dataset in output.
129  * \param[out] output the resultant filtered point cloud dataset
130  */
131  inline void
132  filter (PointCloud &output)
133  {
134  if (!initCompute ())
135  return;
136 
137  if (input_.get () == &output) // cloud_in = cloud_out
138  {
139  PointCloud output_temp;
140  applyFilter (output_temp);
141  output_temp.header = input_->header;
142  output_temp.sensor_origin_ = input_->sensor_origin_;
143  output_temp.sensor_orientation_ = input_->sensor_orientation_;
144  pcl::copyPointCloud (output_temp, output);
145  }
146  else
147  {
148  output.header = input_->header;
149  output.sensor_origin_ = input_->sensor_origin_;
150  output.sensor_orientation_ = input_->sensor_orientation_;
151  applyFilter (output);
152  }
153 
154  deinitCompute ();
155  }
156 
157  protected:
158 
161 
162  /** \brief Indices of the points that are removed */
164 
165  /** \brief The filter name. */
166  std::string filter_name_;
167 
168  /** \brief Set to true if we want to return the indices of the removed points. */
170 
171  /** \brief Abstract filter method.
172  *
173  * The implementation needs to set output.{points, width, height, is_dense}.
174  *
175  * \param[out] output the resultant filtered point cloud
176  */
177  virtual void
178  applyFilter (PointCloud &output) = 0;
179 
180  /** \brief Get a string representation of the name of this class. */
181  inline const std::string&
182  getClassName () const
183  {
184  return (filter_name_);
185  }
186  };
187 
188  ////////////////////////////////////////////////////////////////////////////////////////////
189  /** \brief Filter represents the base filter class. All filters must inherit from this interface.
190  * \author Radu B. Rusu
191  * \ingroup filters
192  */
193  template<>
194  class PCL_EXPORTS Filter<pcl::PCLPointCloud2> : public PCLBase<pcl::PCLPointCloud2>
195  {
196  public:
197  typedef boost::shared_ptr< Filter<pcl::PCLPointCloud2> > Ptr;
198  typedef boost::shared_ptr< const Filter<pcl::PCLPointCloud2> > ConstPtr;
199 
203 
204  /** \brief Empty constructor.
205  * \param[in] extract_removed_indices set to true if the filtered data indices should be saved in a
206  * separate list. Default: false.
207  */
208  Filter (bool extract_removed_indices = false) :
209  removed_indices_ (new std::vector<int>),
210  extract_removed_indices_ (extract_removed_indices),
211  filter_name_ ()
212  {
213  }
214 
215  /** \brief Empty destructor */
216  virtual ~Filter () {}
217 
218  /** \brief Get the point indices being removed */
219  inline IndicesConstPtr const
221  {
222  return (removed_indices_);
223  }
224 
225  /** \brief Get the point indices being removed
226  * \param[out] pi the resultant point indices that have been removed
227  */
228  inline void
230  {
231  pi.indices = *removed_indices_;
232  }
233 
234  /** \brief Calls the filtering method and returns the filtered dataset in output.
235  * \param[out] output the resultant filtered point cloud dataset
236  */
237  void
238  filter (PCLPointCloud2 &output);
239 
240  protected:
241 
242  /** \brief Indices of the points that are removed */
244 
245  /** \brief Set to true if we want to return the indices of the removed points. */
247 
248  /** \brief The filter name. */
249  std::string filter_name_;
250 
251  /** \brief Abstract filter method.
252  *
253  * The implementation needs to set output.{data, row_step, point_step, width, height, is_dense}.
254  *
255  * \param[out] output the resultant filtered point cloud
256  */
257  virtual void
258  applyFilter (PCLPointCloud2 &output) = 0;
259 
260  /** \brief Get a string representation of the name of this class. */
261  inline const std::string&
262  getClassName () const
263  {
264  return (filter_name_);
265  }
266  };
267 }
268 
269 #ifdef PCL_NO_PRECOMPILE
270 #include <pcl/filters/impl/filter.hpp>
271 #endif
272 
273 #endif //#ifndef PCL_FILTER_H_
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:97
void getRemovedIndices(PointIndices &pi)
Get the point indices being removed.
Definition: filter.h:229
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: filter.h:202
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:421
std::string filter_name_
The filter name.
Definition: filter.h:249
boost::shared_ptr< std::vector< int > > IndicesPtr
Definition: pcl_base.h:60
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
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:94
virtual ~Filter()
Empty destructor.
Definition: filter.h:110
pcl::PCLPointCloud2 PCLPointCloud2
Definition: filter.h:200
boost::shared_ptr< const Filter< pcl::PCLPointCloud2 > > ConstPtr
Definition: filter.h:198
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:132
Filter(bool extract_removed_indices=false)
Empty constructor.
Definition: filter.h:102
boost::shared_ptr< Filter< PointT > > Ptr
Definition: filter.h:90
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:407
bool initCompute()
This method should get called before starting the actual computation.
Definition: pcl_base.hpp:139
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:423
Filter(bool extract_removed_indices=false)
Empty constructor.
Definition: filter.h:208
Filter represents the base filter class.
Definition: filter.h:84
IndicesConstPtr const getRemovedIndices()
Get the point indices being removed.
Definition: filter.h:114
void getRemovedIndices(PointIndices &pi)
Get the point indices being removed.
Definition: filter.h:123
IndicesConstPtr const getRemovedIndices()
Get the point indices being removed.
Definition: filter.h:220
PCL base class.
Definition: pcl_base.h:68
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition: filter.h:163
PointCloud::ConstPtr PointCloudConstPtr
Definition: filter.h:96
boost::shared_ptr< ::pcl::PCLPointCloud2 const > ConstPtr
bool deinitCompute()
This method should get called after finishing the actual computation.
Definition: pcl_base.hpp:174
boost::shared_ptr< Filter< pcl::PCLPointCloud2 > > Ptr
Definition: filter.h:197
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:246
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition: filter.h:169
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: filter.h:201
PointCloud::Ptr PointCloudPtr
Definition: filter.h:95
virtual void applyFilter(PointCloud &output)=0
Abstract filter method.
virtual ~Filter()
Empty destructor.
Definition: filter.h:216
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:150
std::string filter_name_
The filter name.
Definition: filter.h:166
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: filter.h:262
boost::shared_ptr< const Filter< PointT > > ConstPtr
Definition: filter.h:91
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: filter.h:182
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition: filter.h:243