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