Point Cloud Library (PCL)  1.8.1-dev
model_outlier_removal.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  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef PCL_FILTERS_MODEL_OUTLIER_REMOVAL_H_
39 #define PCL_FILTERS_MODEL_OUTLIER_REMOVAL_H_
40 
41 #include <pcl/filters/filter_indices.h>
42 #include <pcl/ModelCoefficients.h>
43 
44 // Sample Consensus models
45 #include <pcl/sample_consensus/model_types.h>
46 #include <pcl/sample_consensus/sac_model.h>
47 
48 namespace pcl
49 {
50  /** \brief @b ModelOutlierRemoval filters points in a cloud based on the distance between model and point.
51  * \details Iterates through the entire input once, automatically filtering non-finite points and the points outside
52  * the model specified by setSampleConsensusModelPointer() and the threshold specified by setThreholdFunctionPointer().
53  * <br><br>
54  * Usage example:
55  * \code
56  * pcl::ModelCoefficients model_coeff;
57  * model_coeff.values.resize(4);
58  * model_coeff.values[0] = 0; model_coeff.values[1] = 0; model_coeff.values[2] = 1.5; model_coeff.values[3] = 0.5;
59  * pcl::ModelOutlierRemoval<pcl::PointXYZ> filter;
60  * filter.setModelCoefficients (model_coeff);
61  * filter.setThreshold (0.1);
62  * filter.setModelType (pcl::SACMODEL_PLANE);
63  * filter.setInputCloud (*cloud_in);
64  * filter.setFilterLimitsNegative (false);
65  * filter.filter (*cloud_out);
66  * \endcode
67  */
68  template <typename PointT>
69  class ModelOutlierRemoval : public FilterIndices<PointT>
70  {
71  protected:
73  typedef typename PointCloud::Ptr PointCloudPtr;
76 
77  public:
80 
81  /** \brief Constructor.
82  * \param[in] extract_removed_indices Set to true if you want to be able to extract the indices of points being removed (default = false).
83  */
84  inline
85  ModelOutlierRemoval (bool extract_removed_indices = false) :
86  FilterIndices<PointT>::FilterIndices (extract_removed_indices)
87  {
88  thresh_ = 0;
90  filter_name_ = "ModelOutlierRemoval";
92  }
93 
94  /** \brief sets the models coefficients */
95  inline void
96  setModelCoefficients (const pcl::ModelCoefficients model_coefficients)
97  {
98  model_coefficients_.resize (model_coefficients.values.size ());
99  for (unsigned int i = 0; i < model_coefficients.values.size (); i++)
100  {
101  model_coefficients_[i] = model_coefficients.values[i];
102  }
103  }
104 
105  /** \brief returns the models coefficients
106  */
109  {
111  mc.values.resize (model_coefficients_.size ());
112  for (unsigned int i = 0; i < mc.values.size (); i++)
113  mc.values[i] = model_coefficients_[i];
114  return (mc);
115  }
116 
117  /** \brief Set the type of SAC model used. */
118  inline void
120  {
121  model_type_ = model;
122  }
123 
124  /** \brief Get the type of SAC model used. */
125  inline pcl::SacModel
126  getModelType () const
127  {
128  return (model_type_);
129  }
130 
131  /** \brief Set the thresholdfunction*/
132  inline void
133  setThreshold (float thresh)
134  {
135  thresh_ = thresh;
136  }
137 
138  /** \brief Get the thresholdfunction*/
139  inline float
140  getThreshold () const
141  {
142  return (thresh_);
143  }
144 
145  /** \brief Set the normals cloud*/
146  inline void
148  {
149  cloud_normals_ = normals_ptr;
150  }
151 
152  /** \brief Get the normals cloud*/
153  inline PointCloudNConstPtr
155  {
156  return (cloud_normals_);
157  }
158 
159  /** \brief Set the normals distance weight*/
160  inline void
161  setNormalDistanceWeight (const double weight)
162  {
163  normals_distance_weight_ = weight;
164  }
165 
166  /** \brief get the normal distance weight*/
167  inline double
169  {
170  return (normals_distance_weight_);
171  }
172 
173  /** \brief Register a different threshold function
174  * \param[in] thresh pointer to a threshold function
175  */
176  void
177  setThresholdFunction (boost::function<bool (double)> thresh)
178  {
179  threshold_function_ = thresh;
180  }
181 
182  /** \brief Register a different threshold function
183  * \param[in] thresh_function pointer to a threshold function
184  * \param[in] instance
185  */
186  template <typename T> void
187  setThresholdFunction (bool (T::*thresh_function) (double), T& instance)
188  {
189  setThresholdFunction (boost::bind (thresh_function, boost::ref (instance), _1));
190  }
191 
192  protected:
202 
203  /** \brief Filtered results are stored in a separate point cloud.
204  * \param[out] output The resultant point cloud.
205  */
206  void
207  applyFilter (PointCloud &output);
208 
209  /** \brief Filtered results are indexed by an indices array.
210  * \param[out] indices The resultant indices.
211  */
212  void
213  applyFilter (std::vector<int> &indices)
214  {
215  applyFilterIndices (indices);
216  }
217 
218  /** \brief Filtered results are indexed by an indices array.
219  * \param[out] indices The resultant indices.
220  */
221  void
222  applyFilterIndices (std::vector<int> &indices);
223 
224  protected:
227 
228  /** \brief The model used to calculate distances */
230 
231  /** \brief The threshold used to seperate outliers (removed_indices) from inliers (indices) */
232  float thresh_;
233 
234  /** \brief The model coefficients */
235  Eigen::VectorXf model_coefficients_;
236 
237  /** \brief The type of model to use (user given parameter). */
239  boost::function<bool (double)> threshold_function_;
240 
241  inline bool
242  checkSingleThreshold (double value)
243  {
244  return (value < thresh_);
245  }
246 
247  private:
248  virtual bool
249  initSACModel (pcl::SacModel model_type);
250  };
251 }
252 
253 #ifdef PCL_NO_PRECOMPILE
254 #include <pcl/filters/impl/model_outlier_removal.hpp>
255 #endif
256 
257 #endif // PCL_FILTERS_MODEL_OUTLIER_REMOVAL_H_
ModelOutlierRemoval(bool extract_removed_indices=false)
Constructor.
pcl::PointCloud< pcl::Normal >::ConstPtr PointCloudNConstPtr
void setThreshold(float thresh)
Set the thresholdfunction.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
Eigen::VectorXf model_coefficients_
The model coefficients.
PointCloud::ConstPtr PointCloudConstPtr
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
pcl::SacModel getModelType() const
Get the type of SAC model used.
std::vector< float > values
pcl::PointCloud< pcl::Normal >::Ptr PointCloudNPtr
boost::function< bool(double)> threshold_function_
PointCloudNConstPtr getInputNormals() const
Get the normals cloud.
FilterIndices represents the base class for filters that are about binary point removal.
SampleConsensusModelPtr model_
The model used to calculate distances.
ModelOutlierRemoval filters points in a cloud based on the distance between model and point...
bool checkSingleThreshold(double value)
void setModelCoefficients(const pcl::ModelCoefficients model_coefficients)
sets the models coefficients
Filter represents the base filter class.
Definition: filter.h:84
float thresh_
The threshold used to seperate outliers (removed_indices) from inliers (indices)
PCL base class.
Definition: pcl_base.h:68
float getThreshold() const
Get the thresholdfunction.
void setThresholdFunction(bool(T::*thresh_function)(double), T &instance)
Register a different threshold function.
SacModel
Definition: model_types.h:46
void applyFilterIndices(std::vector< int > &indices)
Filtered results are indexed by an indices array.
pcl::SacModel model_type_
The type of model to use (user given parameter).
void setModelType(pcl::SacModel model)
Set the type of SAC model used.
boost::shared_ptr< SampleConsensusModel > Ptr
Definition: sac_model.h:74
void applyFilter(std::vector< int > &indices)
Filtered results are indexed by an indices array.
void setInputNormals(const PointCloudNConstPtr normals_ptr)
Set the normals cloud.
void setThresholdFunction(boost::function< bool(double)> thresh)
Register a different threshold function.
std::string filter_name_
The filter name.
Definition: filter.h:166
A point structure representing Euclidean xyz coordinates, and the RGB color.
void setNormalDistanceWeight(const double weight)
Set the normals distance weight.
PointCloudNConstPtr cloud_normals_
double getNormalDistanceWeight() const
get the normal distance weight
void applyFilter(PointCloud &output)
Filtered results are stored in a separate point cloud.
SampleConsensusModel< PointT >::Ptr SampleConsensusModelPtr
FilterIndices< PointT >::PointCloud PointCloud
pcl::ModelCoefficients getModelCoefficients() const
returns the models coefficients