Point Cloud Library (PCL)  1.9.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 #pragma once
39 
40 #include <pcl/filters/filter_indices.h>
41 #include <pcl/ModelCoefficients.h>
42 
43 // Sample Consensus models
44 #include <pcl/sample_consensus/model_types.h>
45 #include <pcl/sample_consensus/sac_model.h>
46 
47 namespace pcl
48 {
49  /** \brief @b ModelOutlierRemoval filters points in a cloud based on the distance between model and point.
50  * \details Iterates through the entire input once, automatically filtering non-finite points and the points outside
51  * the model specified by setSampleConsensusModelPointer() and the threshold specified by setThreholdFunctionPointer().
52  * <br><br>
53  * Usage example:
54  * \code
55  * pcl::ModelCoefficients model_coeff;
56  * model_coeff.values.resize(4);
57  * model_coeff.values[0] = 0; model_coeff.values[1] = 0; model_coeff.values[2] = 1.5; model_coeff.values[3] = 0.5;
58  * pcl::ModelOutlierRemoval<pcl::PointXYZ> filter;
59  * filter.setModelCoefficients (model_coeff);
60  * filter.setThreshold (0.1);
61  * filter.setModelType (pcl::SACMODEL_PLANE);
62  * filter.setInputCloud (*cloud_in);
63  * filter.setFilterLimitsNegative (false);
64  * filter.filter (*cloud_out);
65  * \endcode
66  */
67  template <typename PointT>
68  class ModelOutlierRemoval : public FilterIndices<PointT>
69  {
70  protected:
72  using PointCloudPtr = typename PointCloud::Ptr;
75 
76  public:
79 
80  /** \brief Constructor.
81  * \param[in] extract_removed_indices Set to true if you want to be able to extract the indices of points being removed (default = false).
82  */
83  inline
84  ModelOutlierRemoval (bool extract_removed_indices = false) :
85  FilterIndices<PointT>::FilterIndices (extract_removed_indices)
86  {
87  thresh_ = 0;
89  filter_name_ = "ModelOutlierRemoval";
91  }
92 
93  /** \brief sets the models coefficients */
94  inline void
95  setModelCoefficients (const pcl::ModelCoefficients model_coefficients)
96  {
97  model_coefficients_.resize (model_coefficients.values.size ());
98  for (size_t i = 0; i < model_coefficients.values.size (); i++)
99  {
100  model_coefficients_[i] = model_coefficients.values[i];
101  }
102  }
103 
104  /** \brief returns the models coefficients
105  */
108  {
110  mc.values.resize (model_coefficients_.size ());
111  for (size_t i = 0; i < mc.values.size (); i++)
112  mc.values[i] = model_coefficients_[i];
113  return (mc);
114  }
115 
116  /** \brief Set the type of SAC model used. */
117  inline void
119  {
120  model_type_ = model;
121  }
122 
123  /** \brief Get the type of SAC model used. */
124  inline pcl::SacModel
125  getModelType () const
126  {
127  return (model_type_);
128  }
129 
130  /** \brief Set the thresholdfunction*/
131  inline void
132  setThreshold (float thresh)
133  {
134  thresh_ = thresh;
135  }
136 
137  /** \brief Get the thresholdfunction*/
138  inline float
139  getThreshold () const
140  {
141  return (thresh_);
142  }
143 
144  /** \brief Set the normals cloud*/
145  inline void
147  {
148  cloud_normals_ = normals_ptr;
149  }
150 
151  /** \brief Get the normals cloud*/
152  inline PointCloudNConstPtr
154  {
155  return (cloud_normals_);
156  }
157 
158  /** \brief Set the normals distance weight*/
159  inline void
160  setNormalDistanceWeight (const double weight)
161  {
162  normals_distance_weight_ = weight;
163  }
164 
165  /** \brief get the normal distance weight*/
166  inline double
168  {
169  return (normals_distance_weight_);
170  }
171 
172  /** \brief Register a different threshold function
173  * \param[in] thresh pointer to a threshold function
174  */
175  void
176  setThresholdFunction (std::function<bool (double)> thresh)
177  {
178  threshold_function_ = thresh;
179  }
180 
181  /** \brief Register a different threshold function
182  * \param[in] thresh_function pointer to a threshold function
183  * \param[in] instance
184  */
185  template <typename T> void
186  setThresholdFunction (bool (T::*thresh_function) (double), T& instance)
187  {
188  setThresholdFunction ([=, &instance] (double threshold) { return (instance.*thresh_function) (threshold); });
189  }
190 
191  protected:
201 
202  /** \brief Filtered results are stored in a separate point cloud.
203  * \param[out] output The resultant point cloud.
204  */
205  void
206  applyFilter (PointCloud &output) override;
207 
208  /** \brief Filtered results are indexed by an indices array.
209  * \param[out] indices The resultant indices.
210  */
211  void
212  applyFilter (std::vector<int> &indices) override
213  {
214  applyFilterIndices (indices);
215  }
216 
217  /** \brief Filtered results are indexed by an indices array.
218  * \param[out] indices The resultant indices.
219  */
220  void
221  applyFilterIndices (std::vector<int> &indices);
222 
223  protected:
226 
227  /** \brief The model used to calculate distances */
229 
230  /** \brief The threshold used to separate outliers (removed_indices) from inliers (indices) */
231  float thresh_;
232 
233  /** \brief The model coefficients */
234  Eigen::VectorXf model_coefficients_;
235 
236  /** \brief The type of model to use (user given parameter). */
238  std::function<bool (double)> threshold_function_;
239 
240  inline bool
241  checkSingleThreshold (double value)
242  {
243  return (value < thresh_);
244  }
245 
246  private:
247  virtual bool
248  initSACModel (pcl::SacModel model_type);
249  };
250 }
251 
252 #ifdef PCL_NO_PRECOMPILE
253 #include <pcl/filters/impl/model_outlier_removal.hpp>
254 #endif
ModelOutlierRemoval(bool extract_removed_indices=false)
Constructor.
void setThreshold(float thresh)
Set the thresholdfunction.
Eigen::VectorXf model_coefficients_
The model coefficients.
pcl::SacModel getModelType() const
Get the type of SAC model used.
boost::shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:75
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
std::vector< float > values
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:83
float thresh_
The threshold used to separate outliers (removed_indices) from inliers (indices)
PCL base class.
Definition: pcl_base.h:69
float getThreshold() const
Get the thresholdfunction.
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:429
std::function< bool(double)> threshold_function_
void setThresholdFunction(bool(T::*thresh_function)(double), T &instance)
Register a different threshold function.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
SacModel
Definition: model_types.h:45
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< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:430
void setInputNormals(const PointCloudNConstPtr normals_ptr)
Set the normals cloud.
void applyFilter(std::vector< int > &indices) override
Filtered results are indexed by an indices array.
std::string filter_name_
The filter name.
Definition: filter.h:164
A point structure representing Euclidean xyz coordinates, and the RGB color.
void setNormalDistanceWeight(const double weight)
Set the normals distance weight.
void setThresholdFunction(std::function< bool(double)> thresh)
Register a different threshold function.
PointCloudNConstPtr cloud_normals_
double getNormalDistanceWeight() const
get the normal distance weight
typename SampleConsensusModel< PointT >::Ptr SampleConsensusModelPtr
pcl::PointCloud< pcl::Normal >::Ptr PointCloudNPtr
pcl::PointCloud< pcl::Normal >::ConstPtr PointCloudNConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
pcl::ModelCoefficients getModelCoefficients() const
returns the models coefficients
void applyFilter(PointCloud &output) override
Filtered results are stored in a separate point cloud.