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  using Ptr = boost::shared_ptr<Filter<PointT> >;
90  using ConstPtr = boost::shared_ptr<const Filter<PointT> >;
91 
92 
94  using PointCloudPtr = typename PointCloud::Ptr;
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  extract_removed_indices_ (extract_removed_indices)
104  {
105  }
106 
107  /** \brief Empty destructor */
108  ~Filter () {}
109 
110  /** \brief Get the point indices being removed */
111  inline IndicesConstPtr const
113  {
114  return (removed_indices_);
115  }
116 
117  /** \brief Get the point indices being removed
118  * \param[out] pi the resultant point indices that have been removed
119  */
120  inline void
122  {
124  }
125 
126  /** \brief Calls the filtering method and returns the filtered dataset in output.
127  * \param[out] output the resultant filtered point cloud dataset
128  */
129  inline void
130  filter (PointCloud &output)
131  {
132  if (!initCompute ())
133  return;
134 
135  if (input_.get () == &output) // cloud_in = cloud_out
136  {
137  PointCloud output_temp;
138  applyFilter (output_temp);
139  output_temp.header = input_->header;
140  output_temp.sensor_origin_ = input_->sensor_origin_;
141  output_temp.sensor_orientation_ = input_->sensor_orientation_;
142  pcl::copyPointCloud (output_temp, output);
143  }
144  else
145  {
146  output.header = input_->header;
147  output.sensor_origin_ = input_->sensor_origin_;
148  output.sensor_orientation_ = input_->sensor_orientation_;
149  applyFilter (output);
150  }
151 
152  deinitCompute ();
153  }
154 
155  protected:
156 
159 
160  /** \brief Indices of the points that are removed */
162 
163  /** \brief The filter name. */
164  std::string filter_name_;
165 
166  /** \brief Set to true if we want to return the indices of the removed points. */
168 
169  /** \brief Abstract filter method.
170  *
171  * The implementation needs to set output.{points, width, height, is_dense}.
172  *
173  * \param[out] output the resultant filtered point cloud
174  */
175  virtual void
176  applyFilter (PointCloud &output) = 0;
177 
178  /** \brief Get a string representation of the name of this class. */
179  inline const std::string&
180  getClassName () const
181  {
182  return (filter_name_);
183  }
184  };
185 
186  ////////////////////////////////////////////////////////////////////////////////////////////
187  /** \brief Filter represents the base filter class. All filters must inherit from this interface.
188  * \author Radu B. Rusu
189  * \ingroup filters
190  */
191  template<>
192  class PCL_EXPORTS Filter<pcl::PCLPointCloud2> : public PCLBase<pcl::PCLPointCloud2>
193  {
194  public:
195  using Ptr = boost::shared_ptr<Filter<pcl::PCLPointCloud2> >;
196  using ConstPtr = boost::shared_ptr<const Filter<pcl::PCLPointCloud2> >;
197 
201 
202  /** \brief Empty constructor.
203  * \param[in] extract_removed_indices set to true if the filtered data indices should be saved in a
204  * separate list. Default: false.
205  */
206  Filter (bool extract_removed_indices = false) :
207  removed_indices_ (new std::vector<int>),
208  extract_removed_indices_ (extract_removed_indices)
209  {
210  }
211 
212  /** \brief Empty destructor */
213  ~Filter () {}
214 
215  /** \brief Get the point indices being removed */
216  inline IndicesConstPtr const
218  {
219  return (removed_indices_);
220  }
221 
222  /** \brief Get the point indices being removed
223  * \param[out] pi the resultant point indices that have been removed
224  */
225  inline void
227  {
229  }
230 
231  /** \brief Calls the filtering method and returns the filtered dataset in output.
232  * \param[out] output the resultant filtered point cloud dataset
233  */
234  void
235  filter (PCLPointCloud2 &output);
236 
237  protected:
238 
239  /** \brief Indices of the points that are removed */
241 
242  /** \brief Set to true if we want to return the indices of the removed points. */
244 
245  /** \brief The filter name. */
246  std::string filter_name_;
247 
248  /** \brief Abstract filter method.
249  *
250  * The implementation needs to set output.{data, row_step, point_step, width, height, is_dense}.
251  *
252  * \param[out] output the resultant filtered point cloud
253  */
254  virtual void
255  applyFilter (PCLPointCloud2 &output) = 0;
256 
257  /** \brief Get a string representation of the name of this class. */
258  inline const std::string&
259  getClassName () const
260  {
261  return (filter_name_);
262  }
263  };
264 }
265 
266 #ifdef PCL_NO_PRECOMPILE
267 #include <pcl/filters/impl/filter.hpp>
268 #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:226
boost::shared_ptr< const Filter< pcl::PointXYZRGBL > > ConstPtr
Definition: filter.h:90
std::string filter_name_
The filter name.
Definition: filter.h:246
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
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
boost::shared_ptr< PCLPointCloud2 const > PCLPointCloud2ConstPtr
Definition: pcl_base.h:190
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:130
~Filter()
Empty destructor.
Definition: filter.h:108
Filter(bool extract_removed_indices=false)
Empty constructor.
Definition: filter.h:101
bool initCompute()
This method should get called before starting the actual computation.
Definition: pcl_base.hpp:138
IndicesConstPtr const getRemovedIndices() const
Get the point indices being removed.
Definition: filter.h:217
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:434
Filter(bool extract_removed_indices=false)
Empty constructor.
Definition: filter.h:206
boost::shared_ptr< Filter< pcl::PCLPointCloud2 > > Ptr
Definition: filter.h:195
boost::shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:61
Filter represents the base filter class.
Definition: filter.h:83
void getRemovedIndices(PointIndices &pi)
Get the point indices being removed.
Definition: filter.h:121
boost::shared_ptr< const Filter< pcl::PCLPointCloud2 > > ConstPtr
Definition: filter.h:196
boost::shared_ptr< PCLPointCloud2 > PCLPointCloud2Ptr
Definition: pcl_base.h:189
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
PCL base class.
Definition: pcl_base.h:69
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition: filter.h:161
IndicesConstPtr const getRemovedIndices() const
Get the point indices being removed.
Definition: filter.h:112
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:436
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:441
boost::shared_ptr< const Indices > IndicesConstPtr
Definition: pcl_base.h:62
bool deinitCompute()
This method should get called after finishing the actual computation.
Definition: pcl_base.hpp:171
PointCloud represents the base class in PCL for storing collections of 3D points. ...
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:420
boost::shared_ptr< Filter< pcl::PointXYZRGBL > > Ptr
Definition: filter.h:89
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:442
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: filter.h:180
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition: filter.h:243
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition: filter.h:167
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: filter.h:259
virtual void applyFilter(PointCloud &output)=0
Abstract filter method.
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:151
std::string filter_name_
The filter name.
Definition: filter.h:164
boost::shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
#define PCL_EXPORTS
Definition: pcl_macros.h:226
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition: filter.h:240
~Filter()
Empty destructor.
Definition: filter.h:213