Point Cloud Library (PCL)  1.8.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 #ifndef PCL_FILTERS_PASSTHROUGH_H_
41 #define PCL_FILTERS_PASSTHROUGH_H_
42 
43 #include <pcl/filters/filter_indices.h>
44 
45 namespace pcl
46 {
47  /** \brief @b PassThrough passes points in a cloud based on constraints for one particular field of the point type.
48  * \details Iterates through the entire input once, automatically filtering non-finite points and the points outside
49  * the interval specified by setFilterLimits(), which applies only to the field specified by setFilterFieldName().
50  * <br><br>
51  * Usage example:
52  * \code
53  * pcl::PassThrough<PointType> ptfilter (true); // Initializing with true will allow us to extract the removed indices
54  * ptfilter.setInputCloud (cloud_in);
55  * ptfilter.setFilterFieldName ("x");
56  * ptfilter.setFilterLimits (0.0, 1000.0);
57  * ptfilter.filter (*indices_x);
58  * // The indices_x array indexes all points of cloud_in that have x between 0.0 and 1000.0
59  * indices_rem = ptfilter.getRemovedIndices ();
60  * // The indices_rem array indexes all points of cloud_in that have x smaller than 0.0 or larger than 1000.0
61  * // and also indexes all non-finite points of cloud_in
62  * ptfilter.setIndices (indices_x);
63  * ptfilter.setFilterFieldName ("z");
64  * ptfilter.setFilterLimits (-10.0, 10.0);
65  * ptfilter.setNegative (true);
66  * ptfilter.filter (*indices_xz);
67  * // 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
68  * ptfilter.setIndices (indices_xz);
69  * ptfilter.setFilterFieldName ("intensity");
70  * ptfilter.setFilterLimits (FLT_MIN, 0.5);
71  * ptfilter.setNegative (false);
72  * ptfilter.filter (*cloud_out);
73  * // The resulting cloud_out contains all points of cloud_in that are finite and have:
74  * // x between 0.0 and 1000.0, z larger than 10.0 or smaller than -10.0 and intensity smaller than 0.5.
75  * \endcode
76  * \author Radu Bogdan Rusu
77  * \ingroup filters
78  */
79  template <typename PointT>
80  class PassThrough : public FilterIndices<PointT>
81  {
82  protected:
84  typedef typename PointCloud::Ptr PointCloudPtr;
87 
88  public:
89 
90  typedef boost::shared_ptr< PassThrough<PointT> > Ptr;
91  typedef boost::shared_ptr< const PassThrough<PointT> > ConstPtr;
92 
93 
94  /** \brief Constructor.
95  * \param[in] extract_removed_indices Set to true if you want to be able to extract the indices of points being removed (default = false).
96  */
97  PassThrough (bool extract_removed_indices = false) :
98  FilterIndices<PointT>::FilterIndices (extract_removed_indices),
99  filter_field_name_ (""),
100  filter_limit_min_ (FLT_MIN),
101  filter_limit_max_ (FLT_MAX)
102  {
103  filter_name_ = "PassThrough";
104  }
105 
106  /** \brief Provide the name of the field to be used for filtering data.
107  * \details In conjunction with setFilterLimits(), points having values outside this interval for this field will be discarded.
108  * \param[in] field_name The name of the field that will be used for filtering.
109  */
110  inline void
111  setFilterFieldName (const std::string &field_name)
112  {
113  filter_field_name_ = field_name;
114  }
115 
116  /** \brief Retrieve the name of the field to be used for filtering data.
117  * \return The name of the field that will be used for filtering.
118  */
119  inline std::string const
121  {
122  return (filter_field_name_);
123  }
124 
125  /** \brief Set the numerical limits for the field for filtering data.
126  * \details In conjunction with setFilterFieldName(), points having values outside this interval for this field will be discarded.
127  * \param[in] limit_min The minimum allowed field value (default = FLT_MIN).
128  * \param[in] limit_max The maximum allowed field value (default = FLT_MAX).
129  */
130  inline void
131  setFilterLimits (const float &limit_min, const float &limit_max)
132  {
133  filter_limit_min_ = limit_min;
134  filter_limit_max_ = limit_max;
135  }
136 
137  /** \brief Get the numerical limits for the field for filtering data.
138  * \param[out] limit_min The minimum allowed field value (default = FLT_MIN).
139  * \param[out] limit_max The maximum allowed field value (default = FLT_MAX).
140  */
141  inline void
142  getFilterLimits (float &limit_min, float &limit_max)
143  {
144  limit_min = filter_limit_min_;
145  limit_max = filter_limit_max_;
146  }
147 
148  /** \brief Set to true if we want to return the data outside the interval specified by setFilterLimits (min, max)
149  * Default: false.
150  * \warning This method will be removed in the future. Use setNegative() instead.
151  * \param[in] limit_negative return data inside the interval (false) or outside (true)
152  */
153  inline void
154  setFilterLimitsNegative (const bool limit_negative)
155  {
156  negative_ = limit_negative;
157  }
158 
159  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
160  * \warning This method will be removed in the future. Use getNegative() instead.
161  * \param[out] limit_negative true if data \b outside the interval [min; max] is to be returned, false otherwise
162  */
163  inline void
164  getFilterLimitsNegative (bool &limit_negative)
165  {
166  limit_negative = negative_;
167  }
168 
169  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
170  * \warning This method will be removed in the future. Use getNegative() instead.
171  * \return true if data \b outside the interval [min; max] is to be returned, false otherwise
172  */
173  inline bool
175  {
176  return (negative_);
177  }
178 
179  protected:
189 
190  /** \brief Filtered results are stored in a separate point cloud.
191  * \param[out] output The resultant point cloud.
192  */
193  void
194  applyFilter (PointCloud &output);
195 
196  /** \brief Filtered results are indexed by an indices array.
197  * \param[out] indices The resultant indices.
198  */
199  void
200  applyFilter (std::vector<int> &indices)
201  {
202  applyFilterIndices (indices);
203  }
204 
205  /** \brief Filtered results are indexed by an indices array.
206  * \param[out] indices The resultant indices.
207  */
208  void
209  applyFilterIndices (std::vector<int> &indices);
210 
211  private:
212  /** \brief The name of the field that will be used for filtering. */
213  std::string filter_field_name_;
214 
215  /** \brief The minimum allowed field value (default = FLT_MIN). */
216  float filter_limit_min_;
217 
218  /** \brief The maximum allowed field value (default = FLT_MIN). */
219  float filter_limit_max_;
220  };
221 
222  ////////////////////////////////////////////////////////////////////////////////////////////
223  /** \brief PassThrough uses the base Filter class methods to pass through all data that satisfies the user given
224  * constraints.
225  * \author Radu B. Rusu
226  * \ingroup filters
227  */
228  template<>
229  class PCL_EXPORTS PassThrough<pcl::PCLPointCloud2> : public Filter<pcl::PCLPointCloud2>
230  {
234 
237 
238  public:
239  /** \brief Constructor. */
240  PassThrough (bool extract_removed_indices = false) :
241  Filter<pcl::PCLPointCloud2>::Filter (extract_removed_indices), keep_organized_ (false),
242  user_filter_value_ (std::numeric_limits<float>::quiet_NaN ()),
243  filter_field_name_ (""), filter_limit_min_ (-FLT_MAX), filter_limit_max_ (FLT_MAX),
244  filter_limit_negative_ (false)
245  {
246  filter_name_ = "PassThrough";
247  }
248 
249  /** \brief Set whether the filtered points should be kept and set to the
250  * value given through \a setUserFilterValue (default: NaN), or removed
251  * from the PointCloud, thus potentially breaking its organized
252  * structure. By default, points are removed.
253  *
254  * \param[in] val set to true whether the filtered points should be kept and
255  * set to a given user value (default: NaN)
256  */
257  inline void
258  setKeepOrganized (bool val)
259  {
260  keep_organized_ = val;
261  }
262 
263  /** \brief Obtain the value of the internal \a keep_organized_ parameter. */
264  inline bool
266  {
267  return (keep_organized_);
268  }
269 
270  /** \brief Provide a value that the filtered points should be set to
271  * instead of removing them. Used in conjunction with \a
272  * setKeepOrganized ().
273  * \param[in] val the user given value that the filtered point dimensions should be set to
274  */
275  inline void
276  setUserFilterValue (float val)
277  {
278  user_filter_value_ = val;
279  }
280 
281  /** \brief Provide the name of the field to be used for filtering data. In conjunction with \a setFilterLimits,
282  * points having values outside this interval will be discarded.
283  * \param[in] field_name the name of the field that contains values used for filtering
284  */
285  inline void
286  setFilterFieldName (const std::string &field_name)
287  {
288  filter_field_name_ = field_name;
289  }
290 
291  /** \brief Get the name of the field used for filtering. */
292  inline std::string const
294  {
295  return (filter_field_name_);
296  }
297 
298  /** \brief Set the field filter limits. All points having field values outside this interval will be discarded.
299  * \param[in] limit_min the minimum allowed field value
300  * \param[in] limit_max the maximum allowed field value
301  */
302  inline void
303  setFilterLimits (const double &limit_min, const double &limit_max)
304  {
305  filter_limit_min_ = limit_min;
306  filter_limit_max_ = limit_max;
307  }
308 
309  /** \brief Get the field filter limits (min/max) set by the user. The default values are -FLT_MAX, FLT_MAX.
310  * \param[out] limit_min the minimum allowed field value
311  * \param[out] limit_max the maximum allowed field value
312  */
313  inline void
314  getFilterLimits (double &limit_min, double &limit_max)
315  {
316  limit_min = filter_limit_min_;
317  limit_max = filter_limit_max_;
318  }
319 
320  /** \brief Set to true if we want to return the data outside the interval specified by setFilterLimits (min, max).
321  * Default: false.
322  * \param[in] limit_negative return data inside the interval (false) or outside (true)
323  */
324  inline void
325  setFilterLimitsNegative (const bool limit_negative)
326  {
327  filter_limit_negative_ = limit_negative;
328  }
329 
330  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
331  * \param[out] limit_negative true if data \b outside the interval [min; max] is to be returned, false otherwise
332  */
333  inline void
334  getFilterLimitsNegative (bool &limit_negative)
335  {
336  limit_negative = filter_limit_negative_;
337  }
338 
339  /** \brief Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
340  * \return true if data \b outside the interval [min; max] is to be returned, false otherwise
341  */
342  inline bool
344  {
345  return (filter_limit_negative_);
346  }
347 
348  protected:
349  void
350  applyFilter (PCLPointCloud2 &output);
351 
352  private:
353  /** \brief Keep the structure of the data organized, by setting the
354  * filtered points to the a user given value (NaN by default).
355  */
356  bool keep_organized_;
357 
358  /** \brief User given value to be set to any filtered point. Casted to
359  * the correct field type.
360  */
361  float user_filter_value_;
362 
363  /** \brief The desired user filter field name. */
364  std::string filter_field_name_;
365 
366  /** \brief The minimum allowed filter value a point will be considered from. */
367  double filter_limit_min_;
368 
369  /** \brief The maximum allowed filter value a point will be considered from. */
370  double filter_limit_max_;
371 
372  /** \brief Set to true if we want to return the data outside (\a filter_limit_min_;\a filter_limit_max_). Default: false. */
373  bool filter_limit_negative_;
374 
375  };
376 }
377 
378 #ifdef PCL_NO_PRECOMPILE
379 #include <pcl/filters/impl/passthrough.hpp>
380 #endif
381 
382 #endif // PCL_FILTERS_PASSTHROUGH_H_
383 
PassThrough(bool extract_removed_indices=false)
Constructor.
Definition: passthrough.h:97
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
FilterIndices< PointT >::PointCloud PointCloud
Definition: passthrough.h:83
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
void applyFilter(PointCloud &output)
Filtered results are stored in a separate point cloud.
Definition: passthrough.hpp:48
PointCloud::ConstPtr PointCloudConstPtr
Definition: passthrough.h:85
void setFilterLimits(const double &limit_min, const double &limit_max)
Set the field filter limits.
Definition: passthrough.h:303
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
Definition: passthrough.h:286
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:154
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
Definition: passthrough.h:111
pcl::traits::fieldList< PointT >::type FieldList
Definition: passthrough.h:86
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:240
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
std::string const getFilterFieldName()
Get the name of the field used for filtering.
Definition: passthrough.h:293
FilterIndices represents the base class for filters that are about binary point removal.
Filter represents the base filter class.
Definition: filter.h:84
void setFilterLimits(const float &limit_min, const float &limit_max)
Set the numerical limits for the field for filtering data.
Definition: passthrough.h:131
bool getKeepOrganized()
Obtain the value of the internal keep_organized_ parameter.
Definition: passthrough.h:265
boost::shared_ptr< PassThrough< PointT > > Ptr
Definition: passthrough.h:90
bool getFilterLimitsNegative()
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:174
PCL base class.
Definition: pcl_base.h:68
void getFilterLimitsNegative(bool &limit_negative)
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:334
void getFilterLimits(double &limit_min, double &limit_max)
Get the field filter limits (min/max) set by the user.
Definition: passthrough.h:314
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:80
boost::shared_ptr< ::pcl::PCLPointCloud2 > PCLPointCloud2Ptr
void applyFilter(std::vector< int > &indices)
Filtered results are indexed by an indices array.
Definition: passthrough.h:200
void setUserFilterValue(float val)
Provide a value that the filtered points should be set to instead of removing them.
Definition: passthrough.h:276
void getFilterLimits(float &limit_min, float &limit_max)
Get the numerical limits for the field for filtering data.
Definition: passthrough.h:142
void applyFilterIndices(std::vector< int > &indices)
Filtered results are indexed by an indices array.
Definition: passthrough.hpp:74
PointCloud::Ptr PointCloudPtr
Definition: passthrough.h:84
std::string filter_name_
The filter name.
Definition: filter.h:166
A point structure representing Euclidean xyz coordinates, and the RGB color.
void getFilterLimitsNegative(bool &limit_negative)
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:164
boost::shared_ptr< const PassThrough< PointT > > ConstPtr
Definition: passthrough.h:91
std::string const getFilterFieldName()
Retrieve the name of the field to be used for filtering data.
Definition: passthrough.h:120
void setKeepOrganized(bool val)
Set whether the filtered points should be kept and set to the value given through setUserFilterValue ...
Definition: passthrough.h:258
bool getFilterLimitsNegative()
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false)...
Definition: passthrough.h:343
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:325