Point Cloud Library (PCL)  1.9.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 <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  typedef typename PointCloud::Ptr PointCloudPtr;
86 
87  public:
88 
89  typedef boost::shared_ptr< PassThrough<PointT> > Ptr;
90  typedef boost::shared_ptr< const PassThrough<PointT> > ConstPtr;
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 Filter<pcl::PCLPointCloud2>
229  {
233 
236 
237  public:
238  /** \brief Constructor. */
239  PassThrough (bool extract_removed_indices = false) :
240  Filter<pcl::PCLPointCloud2>::Filter (extract_removed_indices), keep_organized_ (false),
241  user_filter_value_ (std::numeric_limits<float>::quiet_NaN ()),
242  filter_field_name_ (""), filter_limit_min_ (-FLT_MAX), filter_limit_max_ (FLT_MAX),
243  filter_limit_negative_ (false)
244  {
245  filter_name_ = "PassThrough";
246  }
247 
248  /** \brief Set whether the filtered points should be kept and set to the
249  * value given through \a setUserFilterValue (default: NaN), or removed
250  * from the PointCloud, thus potentially breaking its organized
251  * structure. By default, points are removed.
252  *
253  * \param[in] val set to true whether the filtered points should be kept and
254  * set to a user given value (default: NaN)
255  */
256  inline void
257  setKeepOrganized (bool val)
258  {
259  keep_organized_ = val;
260  }
261 
262  /** \brief Obtain the value of the internal \a keep_organized_ parameter. */
263  inline bool
265  {
266  return (keep_organized_);
267  }
268 
269  /** \brief Provide a value that the filtered points should be set to
270  * instead of removing them. Used in conjunction with \a
271  * setKeepOrganized ().
272  * \param[in] val the user given value that the filtered point dimensions should be set to
273  */
274  inline void
275  setUserFilterValue (float val)
276  {
277  user_filter_value_ = val;
278  }
279 
280  /** \brief Provide the name of the field to be used for filtering data. In conjunction with \a setFilterLimits,
281  * points having values outside this interval will be discarded.
282  * \param[in] field_name the name of the field that contains values used for filtering
283  */
284  inline void
285  setFilterFieldName (const std::string &field_name)
286  {
287  filter_field_name_ = field_name;
288  }
289 
290  /** \brief Get the name of the field used for filtering. */
291  inline std::string const
293  {
294  return (filter_field_name_);
295  }
296 
297  /** \brief Set the field filter limits. All points having field values outside this interval will be discarded.
298  * \param[in] limit_min the minimum allowed field value
299  * \param[in] limit_max the maximum allowed field value
300  */
301  inline void
302  setFilterLimits (const double &limit_min, const double &limit_max)
303  {
304  filter_limit_min_ = limit_min;
305  filter_limit_max_ = limit_max;
306  }
307 
308  /** \brief Get the field filter limits (min/max) set by the user. The default values are -FLT_MAX, FLT_MAX.
309  * \param[out] limit_min the minimum allowed field value
310  * \param[out] limit_max the maximum allowed field value
311  */
312  inline void
313  getFilterLimits (double &limit_min, double &limit_max) const
314  {
315  limit_min = filter_limit_min_;
316  limit_max = filter_limit_max_;
317  }
318 
319  /** \brief Set to true if we want to return the data outside the interval specified by setFilterLimits (min, max).
320  * Default: false.
321  * \param[in] limit_negative return data inside the interval (false) or outside (true)
322  */
323  inline void
324  setFilterLimitsNegative (const bool limit_negative)
325  {
326  filter_limit_negative_ = limit_negative;
327  }
328 
329  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
330  * \param[out] limit_negative true if data \b outside the interval [min; max] is to be returned, false otherwise
331  */
332  inline void
333  getFilterLimitsNegative (bool &limit_negative) const
334  {
335  limit_negative = filter_limit_negative_;
336  }
337 
338  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
339  * \return true if data \b outside the interval [min; max] is to be returned, false otherwise
340  */
341  inline bool
343  {
344  return (filter_limit_negative_);
345  }
346 
347  protected:
348  void
349  applyFilter (PCLPointCloud2 &output) override;
350 
351  private:
352  /** \brief Keep the structure of the data organized, by setting the
353  * filtered points to a user given value (NaN by default).
354  */
355  bool keep_organized_;
356 
357  /** \brief User given value to be set to any filtered point. Casted to
358  * the correct field type.
359  */
360  float user_filter_value_;
361 
362  /** \brief The desired user filter field name. */
363  std::string filter_field_name_;
364 
365  /** \brief The minimum allowed filter value a point will be considered from. */
366  double filter_limit_min_;
367 
368  /** \brief The maximum allowed filter value a point will be considered from. */
369  double filter_limit_max_;
370 
371  /** \brief Set to true if we want to return the data outside (\a filter_limit_min_;\a filter_limit_max_). Default: false. */
372  bool filter_limit_negative_;
373 
374  };
375 }
376 
377 #ifdef PCL_NO_PRECOMPILE
378 #include <pcl/filters/impl/passthrough.hpp>
379 #endif
PassThrough(bool extract_removed_indices=false)
Constructor.
Definition: passthrough.h:96
FilterIndices< PointT >::PointCloud PointCloud
Definition: passthrough.h:82
PointCloud::ConstPtr PointCloudConstPtr
Definition: passthrough.h:84
void setFilterLimits(const double &limit_min, const double &limit_max)
Set the field filter limits.
Definition: passthrough.h:302
std::string const getFilterFieldName() const
Retrieve the name of the field to be used for filtering data.
Definition: passthrough.h:119
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
Definition: passthrough.h:285
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
pcl::traits::fieldList< PointT >::type FieldList
Definition: passthrough.h:85
float user_filter_value_
The user given value that the filtered point dimensions should be set to (default = NaN)...
boost::shared_ptr< ::pcl::PCLPointCloud2 const > PCLPointCloud2ConstPtr
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
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
bool getKeepOrganized() const
Obtain the value of the internal keep_organized_ parameter.
Definition: passthrough.h:264
boost::shared_ptr< PointCloud< PointInT > > Ptr
Definition: point_cloud.h:427
FilterIndices represents the base class for filters that are about binary point removal.
bool keep_organized_
False = remove points (default), true = redefine points, keep structure.
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
boost::shared_ptr< PassThrough< PointT > > Ptr
Definition: passthrough.h:89
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:333
PCL base class.
Definition: pcl_base.h:68
bool getFilterLimitsNegative() const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:342
bool getFilterLimitsNegative() const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:173
boost::shared_ptr< const PointCloud< PointInT > > ConstPtr
Definition: point_cloud.h:428
boost::shared_ptr< ::pcl::PCLPointCloud2 const > ConstPtr
PassThrough passes points in a cloud based on constraints for one particular field of the point type...
Definition: passthrough.h:79
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void getFilterLimits(double &limit_min, double &limit_max) const
Get the field filter limits (min/max) set by the user.
Definition: passthrough.h:313
std::string const getFilterFieldName() const
Get the name of the field used for filtering.
Definition: passthrough.h:292
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< ::pcl::PCLPointCloud2 > PCLPointCloud2Ptr
void setUserFilterValue(float val)
Provide a value that the filtered points should be set to instead of removing them.
Definition: passthrough.h:275
void applyFilterIndices(std::vector< int > &indices)
Filtered results are indexed by an indices array.
Definition: passthrough.hpp:74
PointCloud::Ptr PointCloudPtr
Definition: passthrough.h:83
std::string filter_name_
The filter name.
Definition: filter.h:165
A point structure representing Euclidean xyz coordinates, and the RGB color.
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
boost::shared_ptr< const PassThrough< PointT > > ConstPtr
Definition: passthrough.h:90
void applyFilter(PointCloud &output) override
Filtered results are stored in a separate point cloud.
Definition: passthrough.hpp:48
void setKeepOrganized(bool val)
Set whether the filtered points should be kept and set to the value given through setUserFilterValue ...
Definition: passthrough.h:257
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:324