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