Point Cloud Library (PCL)  1.14.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  * <br><br>
52  * Usage example:
53  * \code
54  *
55  * pcl::ModelCoefficients model_coeff;
56  * model_coeff.values.resize(4);
57  * model_coeff.values[0] = 0;
58  * model_coeff.values[1] = 0;
59  * model_coeff.values[2] = 1;
60  * model_coeff.values[3] = 0.5;
61  * pcl::ModelOutlierRemoval<pcl::PointXYZ> filter;
62  * filter.setModelCoefficients (model_coeff);
63  * filter.setThreshold (0.1);
64  * filter.setModelType (pcl::SACMODEL_PLANE);
65  * filter.setInputCloud (*cloud_in);
66  * filter.setNegative (false);
67  * filter.filter (*cloud_out);
68 
69  * \endcode
70  * \ingroup filters
71  */
72  template <typename PointT>
73  class ModelOutlierRemoval : public FilterIndices<PointT>
74  {
75  protected:
77  using PointCloudPtr = typename PointCloud::Ptr;
80 
81  public:
84 
85  /** \brief Constructor.
86  * \param[in] extract_removed_indices Set to true if you want to be able to extract the indices of points being removed (default = false).
87  */
88  inline
89  ModelOutlierRemoval (bool extract_removed_indices = false) :
90  FilterIndices<PointT> (extract_removed_indices)
91  {
92  thresh_ = 0;
94  filter_name_ = "ModelOutlierRemoval";
96  }
97 
98  /** \brief sets the models coefficients */
99  inline void
100  setModelCoefficients (const pcl::ModelCoefficients& model_coefficients)
101  {
102  model_coefficients_.resize (model_coefficients.values.size ());
103  for (std::size_t i = 0; i < model_coefficients.values.size (); i++)
104  {
105  model_coefficients_[i] = model_coefficients.values[i];
106  }
107  }
108 
109  /** \brief returns the models coefficients
110  */
113  {
115  mc.values.resize (model_coefficients_.size ());
116  for (std::size_t i = 0; i < mc.values.size (); i++)
117  mc.values[i] = model_coefficients_[i];
118  return (mc);
119  }
120 
121  /** \brief Set the type of SAC model used. */
122  inline void
124  {
125  model_type_ = model;
126  }
127 
128  /** \brief Get the type of SAC model used. */
129  inline pcl::SacModel
130  getModelType () const
131  {
132  return (model_type_);
133  }
134 
135  /** \brief Set the thresholdfunction*/
136  inline void
137  setThreshold (float thresh)
138  {
139  thresh_ = thresh;
140  }
141 
142  /** \brief Get the thresholdfunction*/
143  inline float
144  getThreshold () const
145  {
146  return (thresh_);
147  }
148 
149  /** \brief Set the normals cloud*/
150  inline void
152  {
153  cloud_normals_ = normals_ptr;
154  }
155 
156  /** \brief Get the normals cloud*/
157  inline PointCloudNConstPtr
159  {
160  return (cloud_normals_);
161  }
162 
163  /** \brief Set the normals distance weight*/
164  inline void
165  setNormalDistanceWeight (const double weight)
166  {
167  normals_distance_weight_ = weight;
168  }
169 
170  /** \brief get the normal distance weight*/
171  inline double
173  {
174  return (normals_distance_weight_);
175  }
176 
177  /** \brief Register a different threshold function
178  * \param[in] thresh pointer to a threshold function
179  */
180  void
181  setThresholdFunction (std::function<bool (double)> thresh)
182  {
183  threshold_function_ = thresh;
184  }
185 
186  /** \brief Register a different threshold function
187  * \param[in] thresh_function pointer to a threshold function
188  * \param[in] instance
189  */
190  template <typename T> void
191  setThresholdFunction (bool (T::*thresh_function) (double), T& instance)
192  {
193  setThresholdFunction ([=, &instance] (double threshold) { return (instance.*thresh_function) (threshold); });
194  }
195 
196  protected:
206 
207  /** \brief Filtered results are indexed by an indices array.
208  * \param[out] indices The resultant indices.
209  */
210  void
211  applyFilter (Indices &indices) override
212  {
213  applyFilterIndices (indices);
214  }
215 
216  /** \brief Filtered results are indexed by an indices array.
217  * \param[out] indices The resultant indices.
218  */
219  void
220  applyFilterIndices (Indices &indices);
221 
222  protected:
225 
226  /** \brief The model used to calculate distances */
228 
229  /** \brief The threshold used to separate outliers (removed_indices) from inliers (indices) */
230  float thresh_;
231 
232  /** \brief The model coefficients */
233  Eigen::VectorXf model_coefficients_;
234 
235  /** \brief The type of model to use (user given parameter). */
237  std::function<bool (double)> threshold_function_;
238 
239  inline bool
240  checkSingleThreshold (double value)
241  {
242  return (value < thresh_);
243  }
244 
245  private:
246  virtual bool
247  initSACModel (pcl::SacModel model_type);
248  };
249 }
250 
251 #ifdef PCL_NO_PRECOMPILE
252 #include <pcl/filters/impl/model_outlier_removal.hpp>
253 #endif
Filter represents the base filter class.
Definition: filter.h:81
std::string filter_name_
The filter name.
Definition: filter.h:158
FilterIndices represents the base class for filters that are about binary point removal.
ModelOutlierRemoval filters points in a cloud based on the distance between model and point.
typename SampleConsensusModel< PointT >::Ptr SampleConsensusModelPtr
Eigen::VectorXf model_coefficients_
The model coefficients.
void setInputNormals(const PointCloudNConstPtr normals_ptr)
Set the normals cloud.
std::function< bool(double)> threshold_function_
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
void setModelType(pcl::SacModel model)
Set the type of SAC model used.
float thresh_
The threshold used to separate outliers (removed_indices) from inliers (indices)
float getThreshold() const
Get the thresholdfunction.
pcl::SacModel model_type_
The type of model to use (user given parameter).
pcl::PointCloud< pcl::Normal >::Ptr PointCloudNPtr
double getNormalDistanceWeight() const
get the normal distance weight
pcl::PointCloud< pcl::Normal >::ConstPtr PointCloudNConstPtr
ModelOutlierRemoval(bool extract_removed_indices=false)
Constructor.
void applyFilter(Indices &indices) override
Filtered results are indexed by an indices array.
void setThresholdFunction(bool(T::*thresh_function)(double), T &instance)
Register a different threshold function.
pcl::ModelCoefficients getModelCoefficients() const
returns the models coefficients
PointCloudNConstPtr getInputNormals() const
Get the normals cloud.
void setNormalDistanceWeight(const double weight)
Set the normals distance weight.
SampleConsensusModelPtr model_
The model used to calculate distances.
void setThresholdFunction(std::function< bool(double)> thresh)
Register a different threshold function.
pcl::SacModel getModelType() const
Get the type of SAC model used.
void setModelCoefficients(const pcl::ModelCoefficients &model_coefficients)
sets the models coefficients
PointCloudNConstPtr cloud_normals_
bool checkSingleThreshold(double value)
void setThreshold(float thresh)
Set the thresholdfunction.
PCL base class.
Definition: pcl_base.h:70
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:78
SacModel
Definition: model_types.h:46
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
std::vector< float > values
A point structure representing Euclidean xyz coordinates, and the RGB color.