Point Cloud Library (PCL)  1.11.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 <cfloat> // for FLT_MIN, FLT_MAX
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 (FLT_MIN, 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_ (FLT_MIN),
102  filter_limit_max_ (FLT_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 = FLT_MIN).
129  * \param[in] limit_max The maximum allowed field value (default = FLT_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 = FLT_MIN).
140  * \param[out] limit_max The maximum allowed field value (default = FLT_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 = FLT_MIN). */
213  float filter_limit_min_;
214 
215  /** \brief The maximum allowed field value (default = FLT_MIN). */
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),
239  filter_field_name_ (""), filter_limit_min_ (-FLT_MAX), filter_limit_max_ (FLT_MAX)
240  {
241  filter_name_ = "PassThrough";
242  }
243 
244  /** \brief Provide the name of the field to be used for filtering data. In conjunction with \a setFilterLimits,
245  * points having values outside this interval will be discarded.
246  * \param[in] field_name the name of the field that contains values used for filtering
247  */
248  inline void
249  setFilterFieldName (const std::string &field_name)
250  {
251  filter_field_name_ = field_name;
252  }
253 
254  /** \brief Get the name of the field used for filtering. */
255  inline std::string const
257  {
258  return (filter_field_name_);
259  }
260 
261  /** \brief Set the field filter limits. All points having field values outside this interval will be discarded.
262  * \param[in] limit_min the minimum allowed field value
263  * \param[in] limit_max the maximum allowed field value
264  */
265  inline void
266  setFilterLimits (const double &limit_min, const double &limit_max)
267  {
268  filter_limit_min_ = limit_min;
269  filter_limit_max_ = limit_max;
270  }
271 
272  /** \brief Get the field filter limits (min/max) set by the user. The default values are -FLT_MAX, FLT_MAX.
273  * \param[out] limit_min the minimum allowed field value
274  * \param[out] limit_max the maximum allowed field value
275  */
276  inline void
277  getFilterLimits (double &limit_min, double &limit_max) const
278  {
279  limit_min = filter_limit_min_;
280  limit_max = filter_limit_max_;
281  }
282 
283  protected:
284  void
285  applyFilter (PCLPointCloud2 &output) override;
286 
287  void
288  applyFilter (Indices &indices) override;
289 
290  private:
291  /** \brief The desired user filter field name. */
292  std::string filter_field_name_;
293 
294  /** \brief The minimum allowed filter value a point will be considered from. */
295  double filter_limit_min_;
296 
297  /** \brief The maximum allowed filter value a point will be considered from. */
298  double filter_limit_max_;
299 
300  };
301 }
302 
303 #ifdef PCL_NO_PRECOMPILE
304 #include <pcl/filters/impl/passthrough.hpp>
305 #endif
pcl_macros.h
Defines all the PCL and non-PCL macros used.
pcl
Definition: convolution.h:46
pcl::Filter< PointInT >::Ptr
shared_ptr< Filter< PointInT > > Ptr
Definition: filter.h:83
pcl::PassThrough< pcl::PCLPointCloud2 >::PassThrough
PassThrough(bool extract_removed_indices=false)
Constructor.
Definition: passthrough.h:237
pcl::PCLBase< PointInT >::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
pcl::PCLBase< PointInT >::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
pcl::PassThrough::setFilterFieldName
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
Definition: passthrough.h:112
pcl::PCLPointCloud2::Ptr
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
Definition: PCLPointCloud2.h:35
pcl::PassThrough::PassThrough
PassThrough(bool extract_removed_indices=false)
Constructor.
Definition: passthrough.h:98
pcl::PassThrough::applyFilterIndices
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
Definition: passthrough.hpp:47
pcl::PCLBase
PCL base class.
Definition: pcl_base.h:69
pcl::PCLBase< pcl::PCLPointCloud2 >::PCLPointCloud2ConstPtr
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: pcl_base.h:186
pcl::PointCloud< PointInT >
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::PassThrough::applyFilter
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
Definition: passthrough.h:197
PCL_DEPRECATED
#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
pcl::PCLBase< pcl::PCLPointCloud2 >::PCLPointCloud2Ptr
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: pcl_base.h:185
pcl::PCLPointCloud2::ConstPtr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
Definition: PCLPointCloud2.h:36
pcl::PassThrough< pcl::PCLPointCloud2 >::setFilterLimits
void setFilterLimits(const double &limit_min, const double &limit_max)
Set the field filter limits.
Definition: passthrough.h:266
pcl::Filter< PointInT >::ConstPtr
shared_ptr< const Filter< PointInT > > ConstPtr
Definition: filter.h:84
pcl::PassThrough< PointInT >::FieldList
typename pcl::traits::fieldList< PointInT >::type FieldList
Definition: passthrough.h:87
pcl::PassThrough< pcl::PCLPointCloud2 >::getFilterFieldName
const std::string getFilterFieldName() const
Get the name of the field used for filtering.
Definition: passthrough.h:256
pcl::FilterIndices
FilterIndices represents the base class for filters that are about binary point removal.
Definition: filter_indices.h:74
pcl::Filter
Filter represents the base filter class.
Definition: filter.h:80
pcl::PassThrough::getFilterLimitsNegative
bool getFilterLimitsNegative() const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
Definition: passthrough.h:177
pcl::PassThrough< pcl::PCLPointCloud2 >::setFilterFieldName
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
Definition: passthrough.h:249
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
pcl::PCLPointCloud2
Definition: PCLPointCloud2.h:16
pcl::PassThrough< pcl::PCLPointCloud2 >::getFilterLimits
void getFilterLimits(double &limit_min, double &limit_max) const
Get the field filter limits (min/max) set by the user.
Definition: passthrough.h:277
pcl::PassThrough
PassThrough passes points in a cloud based on constraints for one particular field of the point type.
Definition: passthrough.h:81
pcl::PassThrough::setFilterLimitsNegative
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
pcl::Filter::filter_name_
std::string filter_name_
The filter name.
Definition: filter.h:158
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:407
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:408
pcl::PassThrough::getFilterFieldName
const std::string getFilterFieldName() const
Retrieve the name of the field to be used for filtering data.
Definition: passthrough.h:121
pcl::PassThrough::setFilterLimits
void setFilterLimits(const float &limit_min, const float &limit_max)
Set the numerical limits for the field for filtering data.
Definition: passthrough.h:132
pcl::FilterIndices::negative_
bool negative_
False = normal filter behavior (default), true = inverted behavior.
Definition: filter_indices.h:168
pcl::PassThrough::getFilterLimits
void getFilterLimits(float &limit_min, float &limit_max) const
Get the numerical limits for the field for filtering data.
Definition: passthrough.h:143
PCL_EXPORTS
#define PCL_EXPORTS
Definition: pcl_macros.h:323