Point Cloud Library (PCL)  1.11.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> (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 (std::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 (std::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 indexed by an indices array.
203  * \param[out] indices The resultant indices.
204  */
205  void
206  applyFilter (Indices &indices) override
207  {
208  applyFilterIndices (indices);
209  }
210 
211  /** \brief Filtered results are indexed by an indices array.
212  * \param[out] indices The resultant indices.
213  */
214  void
215  applyFilterIndices (Indices &indices);
216 
217  protected:
220 
221  /** \brief The model used to calculate distances */
223 
224  /** \brief The threshold used to separate outliers (removed_indices) from inliers (indices) */
225  float thresh_;
226 
227  /** \brief The model coefficients */
228  Eigen::VectorXf model_coefficients_;
229 
230  /** \brief The type of model to use (user given parameter). */
232  std::function<bool (double)> threshold_function_;
233 
234  inline bool
235  checkSingleThreshold (double value)
236  {
237  return (value < thresh_);
238  }
239 
240  private:
241  virtual bool
242  initSACModel (pcl::SacModel model_type);
243  };
244 }
245 
246 #ifdef PCL_NO_PRECOMPILE
247 #include <pcl/filters/impl/model_outlier_removal.hpp>
248 #endif
pcl::ModelOutlierRemoval::getInputNormals
PointCloudNConstPtr getInputNormals() const
Get the normals cloud.
Definition: model_outlier_removal.h:153
pcl
Definition: convolution.h:46
pcl::ModelOutlierRemoval::model_coefficients_
Eigen::VectorXf model_coefficients_
The model coefficients.
Definition: model_outlier_removal.h:228
pcl::PCLBase::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
pcl::ModelOutlierRemoval::setModelCoefficients
void setModelCoefficients(const pcl::ModelCoefficients model_coefficients)
sets the models coefficients
Definition: model_outlier_removal.h:95
pcl::PCLBase::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
pcl::ModelOutlierRemoval::setThresholdFunction
void setThresholdFunction(std::function< bool(double)> thresh)
Register a different threshold function.
Definition: model_outlier_removal.h:176
pcl::ModelOutlierRemoval::getModelCoefficients
pcl::ModelCoefficients getModelCoefficients() const
returns the models coefficients
Definition: model_outlier_removal.h:107
pcl::ModelOutlierRemoval::applyFilter
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
Definition: model_outlier_removal.h:206
pcl::ModelOutlierRemoval::normals_distance_weight_
double normals_distance_weight_
Definition: model_outlier_removal.h:218
pcl::ModelOutlierRemoval::model_
SampleConsensusModelPtr model_
The model used to calculate distances.
Definition: model_outlier_removal.h:222
pcl::PCLBase
PCL base class.
Definition: pcl_base.h:69
pcl::ModelOutlierRemoval::PointCloudNPtr
pcl::PointCloud< pcl::Normal >::Ptr PointCloudNPtr
Definition: model_outlier_removal.h:77
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::ModelOutlierRemoval::setThresholdFunction
void setThresholdFunction(bool(T::*thresh_function)(double), T &instance)
Register a different threshold function.
Definition: model_outlier_removal.h:186
pcl::ModelCoefficients::values
std::vector< float > values
Definition: ModelCoefficients.h:19
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::ModelOutlierRemoval::thresh_
float thresh_
The threshold used to separate outliers (removed_indices) from inliers (indices)
Definition: model_outlier_removal.h:225
pcl::ModelOutlierRemoval
ModelOutlierRemoval filters points in a cloud based on the distance between model and point.
Definition: model_outlier_removal.h:68
pcl::ModelOutlierRemoval::SampleConsensusModelPtr
typename SampleConsensusModel< PointT >::Ptr SampleConsensusModelPtr
Definition: model_outlier_removal.h:74
pcl::ModelCoefficients
Definition: ModelCoefficients.h:11
pcl::ModelOutlierRemoval::setThreshold
void setThreshold(float thresh)
Set the thresholdfunction.
Definition: model_outlier_removal.h:132
pcl::ModelOutlierRemoval::setNormalDistanceWeight
void setNormalDistanceWeight(const double weight)
Set the normals distance weight.
Definition: model_outlier_removal.h:160
pcl::SacModel
SacModel
Definition: model_types.h:45
pcl::ModelOutlierRemoval::cloud_normals_
PointCloudNConstPtr cloud_normals_
Definition: model_outlier_removal.h:219
pcl::SampleConsensusModel::Ptr
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:77
pcl::FilterIndices
FilterIndices represents the base class for filters that are about binary point removal.
Definition: filter_indices.h:74
pcl::Filter
Filter represents the base filter class.
Definition: filter.h:80
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
pcl::ModelOutlierRemoval::model_type_
pcl::SacModel model_type_
The type of model to use (user given parameter).
Definition: model_outlier_removal.h:231
pcl::ModelOutlierRemoval::getNormalDistanceWeight
double getNormalDistanceWeight() const
get the normal distance weight
Definition: model_outlier_removal.h:167
pcl::ModelOutlierRemoval::checkSingleThreshold
bool checkSingleThreshold(double value)
Definition: model_outlier_removal.h:235
pcl::ModelOutlierRemoval::PointCloudNConstPtr
pcl::PointCloud< pcl::Normal >::ConstPtr PointCloudNConstPtr
Definition: model_outlier_removal.h:78
pcl::Filter::filter_name_
std::string filter_name_
The filter name.
Definition: filter.h:158
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:406
pcl::ModelOutlierRemoval::getThreshold
float getThreshold() const
Get the thresholdfunction.
Definition: model_outlier_removal.h:139
pcl::ModelOutlierRemoval::applyFilterIndices
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
Definition: model_outlier_removal.hpp:145
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:407
pcl::ModelOutlierRemoval::setModelType
void setModelType(pcl::SacModel model)
Set the type of SAC model used.
Definition: model_outlier_removal.h:118
pcl::ModelOutlierRemoval::threshold_function_
std::function< bool(double)> threshold_function_
Definition: model_outlier_removal.h:232
pcl::ModelOutlierRemoval::ModelOutlierRemoval
ModelOutlierRemoval(bool extract_removed_indices=false)
Constructor.
Definition: model_outlier_removal.h:84
pcl::ModelOutlierRemoval::getModelType
pcl::SacModel getModelType() const
Get the type of SAC model used.
Definition: model_outlier_removal.h:125
pcl::ModelOutlierRemoval::setInputNormals
void setInputNormals(const PointCloudNConstPtr normals_ptr)
Set the normals cloud.
Definition: model_outlier_removal.h:146