Point Cloud Library (PCL)  1.10.0-dev
passthrough.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, 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/filters/filter_indices.h>
43 
44 namespace pcl
45 {
46  /** \brief @b PassThrough passes points in a cloud based on constraints for one particular field of the point type.
47  * \details Iterates through the entire input once, automatically filtering non-finite points and the points outside
48  * the interval specified by setFilterLimits(), which applies only to the field specified by setFilterFieldName().
49  * <br><br>
50  * Usage example:
51  * \code
52  * pcl::PassThrough<PointType> ptfilter (true); // Initializing with true will allow us to extract the removed indices
53  * ptfilter.setInputCloud (cloud_in);
54  * ptfilter.setFilterFieldName ("x");
55  * ptfilter.setFilterLimits (0.0, 1000.0);
56  * ptfilter.filter (*indices_x);
57  * // The indices_x array indexes all points of cloud_in that have x between 0.0 and 1000.0
58  * indices_rem = ptfilter.getRemovedIndices ();
59  * // The indices_rem array indexes all points of cloud_in that have x smaller than 0.0 or larger than 1000.0
60  * // and also indexes all non-finite points of cloud_in
61  * ptfilter.setIndices (indices_x);
62  * ptfilter.setFilterFieldName ("z");
63  * ptfilter.setFilterLimits (-10.0, 10.0);
64  * ptfilter.setNegative (true);
65  * ptfilter.filter (*indices_xz);
66  * // The indices_xz array indexes all points of cloud_in that have x between 0.0 and 1000.0 and z larger than 10.0 or smaller than -10.0
67  * ptfilter.setIndices (indices_xz);
68  * ptfilter.setFilterFieldName ("intensity");
69  * ptfilter.setFilterLimits (FLT_MIN, 0.5);
70  * ptfilter.setNegative (false);
71  * ptfilter.filter (*cloud_out);
72  * // The resulting cloud_out contains all points of cloud_in that are finite and have:
73  * // x between 0.0 and 1000.0, z larger than 10.0 or smaller than -10.0 and intensity smaller than 0.5.
74  * \endcode
75  * \author Radu Bogdan Rusu
76  * \ingroup filters
77  */
78  template <typename PointT>
79  class PassThrough : public FilterIndices<PointT>
80  {
81  protected:
83  using PointCloudPtr = typename PointCloud::Ptr;
86 
87  public:
88 
91 
92 
93  /** \brief Constructor.
94  * \param[in] extract_removed_indices Set to true if you want to be able to extract the indices of points being removed (default = false).
95  */
96  PassThrough (bool extract_removed_indices = false) :
97  FilterIndices<PointT>::FilterIndices (extract_removed_indices),
98  filter_field_name_ (""),
99  filter_limit_min_ (FLT_MIN),
100  filter_limit_max_ (FLT_MAX)
101  {
102  filter_name_ = "PassThrough";
103  }
104 
105  /** \brief Provide the name of the field to be used for filtering data.
106  * \details In conjunction with setFilterLimits(), points having values outside this interval for this field will be discarded.
107  * \param[in] field_name The name of the field that will be used for filtering.
108  */
109  inline void
110  setFilterFieldName (const std::string &field_name)
111  {
112  filter_field_name_ = field_name;
113  }
114 
115  /** \brief Retrieve the name of the field to be used for filtering data.
116  * \return The name of the field that will be used for filtering.
117  */
118  inline std::string const
120  {
121  return (filter_field_name_);
122  }
123 
124  /** \brief Set the numerical limits for the field for filtering data.
125  * \details In conjunction with setFilterFieldName(), points having values outside this interval for this field will be discarded.
126  * \param[in] limit_min The minimum allowed field value (default = FLT_MIN).
127  * \param[in] limit_max The maximum allowed field value (default = FLT_MAX).
128  */
129  inline void
130  setFilterLimits (const float &limit_min, const float &limit_max)
131  {
132  filter_limit_min_ = limit_min;
133  filter_limit_max_ = limit_max;
134  }
135 
136  /** \brief Get the numerical limits for the field for filtering data.
137  * \param[out] limit_min The minimum allowed field value (default = FLT_MIN).
138  * \param[out] limit_max The maximum allowed field value (default = FLT_MAX).
139  */
140  inline void
141  getFilterLimits (float &limit_min, float &limit_max) const
142  {
143  limit_min = filter_limit_min_;
144  limit_max = filter_limit_max_;
145  }
146 
147  /** \brief Set to true if we want to return the data outside the interval specified by setFilterLimits (min, max)
148  * Default: false.
149  * \warning This method will be removed in the future. Use setNegative() instead.
150  * \param[in] limit_negative return data inside the interval (false) or outside (true)
151  */
152  inline void
153  setFilterLimitsNegative (const bool limit_negative)
154  {
155  negative_ = limit_negative;
156  }
157 
158  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
159  * \warning This method will be removed in the future. Use getNegative() instead.
160  * \param[out] limit_negative true if data \b outside the interval [min; max] is to be returned, false otherwise
161  */
162  inline void
163  getFilterLimitsNegative (bool &limit_negative) const
164  {
165  limit_negative = negative_;
166  }
167 
168  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
169  * \warning This method will be removed in the future. Use getNegative() instead.
170  * \return true if data \b outside the interval [min; max] is to be returned, false otherwise
171  */
172  inline bool
174  {
175  return (negative_);
176  }
177 
178  protected:
188 
189  /** \brief Filtered results are stored in a separate point cloud.
190  * \param[out] output The resultant point cloud.
191  */
192  void
193  applyFilter (PointCloud &output) override;
194 
195  /** \brief Filtered results are indexed by an indices array.
196  * \param[out] indices The resultant indices.
197  */
198  void
199  applyFilter (std::vector<int> &indices) override
200  {
201  applyFilterIndices (indices);
202  }
203 
204  /** \brief Filtered results are indexed by an indices array.
205  * \param[out] indices The resultant indices.
206  */
207  void
208  applyFilterIndices (std::vector<int> &indices);
209 
210  private:
211  /** \brief The name of the field that will be used for filtering. */
212  std::string filter_field_name_;
213 
214  /** \brief The minimum allowed field value (default = FLT_MIN). */
215  float filter_limit_min_;
216 
217  /** \brief The maximum allowed field value (default = FLT_MIN). */
218  float filter_limit_max_;
219  };
220 
221  ////////////////////////////////////////////////////////////////////////////////////////////
222  /** \brief PassThrough uses the base Filter class methods to pass through all data that satisfies the user given
223  * constraints.
224  * \author Radu B. Rusu
225  * \ingroup filters
226  */
227  template<>
228  class PCL_EXPORTS PassThrough<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
229  {
233 
236 
237  public:
238  /** \brief Constructor. */
239  PassThrough (bool extract_removed_indices = false) :
240  FilterIndices<pcl::PCLPointCloud2>::FilterIndices (extract_removed_indices),
241  filter_field_name_ (""), filter_limit_min_ (-FLT_MAX), filter_limit_max_ (FLT_MAX)
242  {
243  filter_name_ = "PassThrough";
244  }
245 
246  /** \brief Provide the name of the field to be used for filtering data. In conjunction with \a setFilterLimits,
247  * points having values outside this interval will be discarded.
248  * \param[in] field_name the name of the field that contains values used for filtering
249  */
250  inline void
251  setFilterFieldName (const std::string &field_name)
252  {
253  filter_field_name_ = field_name;
254  }
255 
256  /** \brief Get the name of the field used for filtering. */
257  inline std::string const
259  {
260  return (filter_field_name_);
261  }
262 
263  /** \brief Set the field filter limits. All points having field values outside this interval will be discarded.
264  * \param[in] limit_min the minimum allowed field value
265  * \param[in] limit_max the maximum allowed field value
266  */
267  inline void
268  setFilterLimits (const double &limit_min, const double &limit_max)
269  {
270  filter_limit_min_ = limit_min;
271  filter_limit_max_ = limit_max;
272  }
273 
274  /** \brief Get the field filter limits (min/max) set by the user. The default values are -FLT_MAX, FLT_MAX.
275  * \param[out] limit_min the minimum allowed field value
276  * \param[out] limit_max the maximum allowed field value
277  */
278  inline void
279  getFilterLimits (double &limit_min, double &limit_max) const
280  {
281  limit_min = filter_limit_min_;
282  limit_max = filter_limit_max_;
283  }
284 
285  /** \brief Set to true if we want to return the data outside the interval specified by setFilterLimits (min, max).
286  * Default: false.
287  * \param[in] limit_negative return data inside the interval (false) or outside (true)
288  */
289  [[deprecated("use inherited FilterIndices::setNegative() instead")]]
290  inline void
291  setFilterLimitsNegative (const bool limit_negative)
292  {
293  negative_ = limit_negative;
294  }
295 
296  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
297  * \param[out] limit_negative true if data \b outside the interval [min; max] is to be returned, false otherwise
298  */
299  [[deprecated("use inherited FilterIndices::getNegative() instead")]]
300  inline void
301  getFilterLimitsNegative (bool &limit_negative) const
302  {
303  limit_negative = negative_;
304  }
305 
306  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
307  * \return true if data \b outside the interval [min; max] is to be returned, false otherwise
308  */
309  [[deprecated("use inherited FilterIndices::getNegative() instead")]]
310  inline bool
312  {
313  return (negative_);
314  }
315 
316  protected:
317  void
318  applyFilter (PCLPointCloud2 &output) override;
319 
320  void
321  applyFilter (std::vector<int> &indices) override;
322 
323  private:
324  /** \brief The desired user filter field name. */
325  std::string filter_field_name_;
326 
327  /** \brief The minimum allowed filter value a point will be considered from. */
328  double filter_limit_min_;
329 
330  /** \brief The maximum allowed filter value a point will be considered from. */
331  double filter_limit_max_;
332 
333  };
334 }
335 
336 #ifdef PCL_NO_PRECOMPILE
337 #include <pcl/filters/impl/passthrough.hpp>
338 #endif
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
PassThrough(bool extract_removed_indices=false)
Constructor.
Definition: passthrough.h:96
std::string const getFilterFieldName() const
Retrieve the name of the field to be used for filtering data.
Definition: passthrough.h:119
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:415
void setFilterLimits(const double &limit_min, const double &limit_max)
Set the field filter limits.
Definition: passthrough.h:268
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
typename pcl::traits::fieldList< PointInT >::type FieldList
Definition: passthrough.h:85
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
Definition: passthrough.h:251
void setFilterLimitsNegative(const bool limit_negative)
Set to true if we want to return the data outside the interval specified by setFilterLimits (min...
Definition: passthrough.h:153
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
Definition: passthrough.h:110
void getFilterLimits(double &limit_min, double &limit_max) const
Get the field filter limits (min/max) set by the user.
Definition: passthrough.h:279
void getFilterLimitsNegative(bool &limit_negative) const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:301
bool negative_
False = normal filter behavior (default), true = inverted behavior.
PassThrough(bool extract_removed_indices=false)
Constructor.
Definition: passthrough.h:239
void applyFilter(std::vector< int > &indices) override
Filtered results are indexed by an indices array.
Definition: passthrough.h:199
void getFilterLimitsNegative(bool &limit_negative) const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:163
bool getFilterLimitsNegative() const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:173
FilterIndices represents the base class for filters that are about binary point removal.
Filter represents the base filter class.
Definition: filter.h:83
void setFilterLimits(const float &limit_min, const float &limit_max)
Set the numerical limits for the field for filtering data.
Definition: passthrough.h:130
shared_ptr< Filter< PointInT > > Ptr
Definition: filter.h:89
PCL base class.
Definition: pcl_base.h:69
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
PassThrough passes points in a cloud based on constraints for one particular field of the point type...
Definition: passthrough.h:79
bool getFilterLimitsNegative() const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:311
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: pcl_base.h:190
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:416
void applyFilterIndices(std::vector< int > &indices)
Filtered results are indexed by an indices array.
Definition: passthrough.hpp:74
std::string filter_name_
The filter name.
Definition: filter.h:164
std::string const getFilterFieldName() const
Get the name of the field used for filtering.
Definition: passthrough.h:258
shared_ptr< const Filter< PointInT > > ConstPtr
Definition: filter.h:90
A point structure representing Euclidean xyz coordinates, and the RGB color.
void getFilterLimits(float &limit_min, float &limit_max) const
Get the numerical limits for the field for filtering data.
Definition: passthrough.h:141
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
void applyFilter(PointCloud &output) override
Filtered results are stored in a separate point cloud.
Definition: passthrough.hpp:48
#define PCL_EXPORTS
Definition: pcl_macros.h:253
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: pcl_base.h:189
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
void setFilterLimitsNegative(const bool limit_negative)
Set to true if we want to return the data outside the interval specified by setFilterLimits (min...
Definition: passthrough.h:291