Point Cloud Library (PCL)  1.14.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 
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 Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
150  * \warning This method will be removed in the future. Use getNegative() instead.
151  * \return true if data \b outside the interval [min; max] is to be returned, false otherwise
152  */
153  inline bool
155  {
156  return (negative_);
157  }
158 
159  protected:
169 
170  /** \brief Filtered results are indexed by an indices array.
171  * \param[out] indices The resultant indices.
172  */
173  void
174  applyFilter (Indices &indices) override
175  {
176  applyFilterIndices (indices);
177  }
178 
179  /** \brief Filtered results are indexed by an indices array.
180  * \param[out] indices The resultant indices.
181  */
182  void
183  applyFilterIndices (Indices &indices);
184 
185  private:
186  /** \brief The name of the field that will be used for filtering. */
187  std::string filter_field_name_;
188 
189  /** \brief The minimum allowed field value (default = std::numeric_limits<float>::lowest()). */
190  float filter_limit_min_;
191 
192  /** \brief The maximum allowed field value (default = std::numeric_limits<float>::max()). */
193  float filter_limit_max_;
194  };
195 
196  ////////////////////////////////////////////////////////////////////////////////////////////
197  /** \brief PassThrough uses the base Filter class methods to pass through all data that satisfies the user given
198  * constraints.
199  * \author Radu B. Rusu
200  * \ingroup filters
201  */
202  template<>
203  class PCL_EXPORTS PassThrough<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
204  {
208 
211 
212  public:
213  /** \brief Constructor. */
214  PassThrough (bool extract_removed_indices = false) :
215  FilterIndices<pcl::PCLPointCloud2>::FilterIndices (extract_removed_indices),
216  filter_limit_min_(std::numeric_limits<float>::lowest())
217  , filter_limit_max_(std::numeric_limits<float>::max())
218  {
219  filter_name_ = "PassThrough";
220  }
221 
222  /** \brief Provide the name of the field to be used for filtering data. In conjunction with \a setFilterLimits,
223  * points having values outside this interval will be discarded.
224  * \param[in] field_name the name of the field that contains values used for filtering
225  */
226  inline void
227  setFilterFieldName (const std::string &field_name)
228  {
229  filter_field_name_ = field_name;
230  }
231 
232  /** \brief Get the name of the field used for filtering. */
233  inline std::string const
235  {
236  return (filter_field_name_);
237  }
238 
239  /** \brief Set the field filter limits. All points having field values outside this interval will be discarded.
240  * \param[in] limit_min the minimum allowed field value
241  * \param[in] limit_max the maximum allowed field value
242  */
243  inline void
244  setFilterLimits (const double &limit_min, const double &limit_max)
245  {
246  filter_limit_min_ = limit_min;
247  filter_limit_max_ = limit_max;
248  }
249 
250  /** \brief Get the field filter limits (min/max) set by the user. The default values are
251  * std::numeric_limits<float>::lowest(), std::numeric_limits<float>::max().
252  * \param[out] limit_min the minimum allowed field value
253  * \param[out] limit_max the maximum allowed field value
254  */
255  inline void
256  getFilterLimits (double &limit_min, double &limit_max) const
257  {
258  limit_min = filter_limit_min_;
259  limit_max = filter_limit_max_;
260  }
261 
262  protected:
263  void
264  applyFilter (PCLPointCloud2 &output) override;
265 
266  void
267  applyFilter (Indices &indices) override;
268 
269  private:
270  /** \brief The desired user filter field name. */
271  std::string filter_field_name_;
272 
273  /** \brief The minimum allowed filter value a point will be considered from. */
274  double filter_limit_min_;
275 
276  /** \brief The maximum allowed filter value a point will be considered from. */
277  double filter_limit_max_;
278 
279  };
280 }
281 
282 #ifdef PCL_NO_PRECOMPILE
283 #include <pcl/filters/impl/passthrough.hpp>
284 #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:234
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
Definition: passthrough.h:227
void getFilterLimits(double &limit_min, double &limit_max) const
Get the field filter limits (min/max) set by the user.
Definition: passthrough.h:256
PassThrough(bool extract_removed_indices=false)
Constructor.
Definition: passthrough.h:214
void setFilterLimits(const double &limit_min, const double &limit_max)
Set the field filter limits.
Definition: passthrough.h:244
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:174
PassThrough(bool extract_removed_indices=false)
Constructor.
Definition: passthrough.h:98
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:154
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
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
A point structure representing Euclidean xyz coordinates, and the RGB color.