Point Cloud Library (PCL)  1.15.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 #include <pcl/point_types.h> // for pcl::Normal
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  * <br><br>
53  * Usage example:
54  * \code
55  *
56  * pcl::ModelCoefficients model_coeff;
57  * model_coeff.values.resize(4);
58  * model_coeff.values[0] = 0;
59  * model_coeff.values[1] = 0;
60  * model_coeff.values[2] = 1;
61  * model_coeff.values[3] = 0.5;
62  * pcl::ModelOutlierRemoval<pcl::PointXYZ> filter;
63  * filter.setModelCoefficients (model_coeff);
64  * filter.setThreshold (0.1);
65  * filter.setModelType (pcl::SACMODEL_PLANE);
66  * filter.setInputCloud (*cloud_in);
67  * filter.setNegative (false);
68  * filter.filter (*cloud_out);
69 
70  * \endcode
71  * \ingroup filters
72  */
73  template <typename PointT>
74  class ModelOutlierRemoval : public FilterIndices<PointT>
75  {
76  protected:
78  using PointCloudPtr = typename PointCloud::Ptr;
81 
82  public:
85 
86  /** \brief Constructor.
87  * \param[in] extract_removed_indices Set to true if you want to be able to extract the indices of points being removed (default = false).
88  */
89  inline
90  ModelOutlierRemoval (bool extract_removed_indices = false) :
91  FilterIndices<PointT> (extract_removed_indices)
92  {
93  thresh_ = 0;
95  filter_name_ = "ModelOutlierRemoval";
97  }
98 
99  /** \brief sets the models coefficients */
100  inline void
101  setModelCoefficients (const pcl::ModelCoefficients& model_coefficients)
102  {
103  model_coefficients_.resize (model_coefficients.values.size ());
104  for (std::size_t i = 0; i < model_coefficients.values.size (); i++)
105  {
106  model_coefficients_[i] = model_coefficients.values[i];
107  }
108  }
109 
110  /** \brief returns the models coefficients
111  */
114  {
116  mc.values.resize (model_coefficients_.size ());
117  for (std::size_t i = 0; i < mc.values.size (); i++)
118  mc.values[i] = model_coefficients_[i];
119  return (mc);
120  }
121 
122  /** \brief Set the type of SAC model used. */
123  inline void
125  {
126  model_type_ = model;
127  }
128 
129  /** \brief Get the type of SAC model used. */
130  inline pcl::SacModel
131  getModelType () const
132  {
133  return (model_type_);
134  }
135 
136  /** \brief Set the thresholdfunction*/
137  inline void
138  setThreshold (float thresh)
139  {
140  thresh_ = thresh;
141  }
142 
143  /** \brief Get the thresholdfunction*/
144  inline float
145  getThreshold () const
146  {
147  return (thresh_);
148  }
149 
150  /** \brief Set the normals cloud*/
151  inline void
153  {
154  cloud_normals_ = normals_ptr;
155  }
156 
157  /** \brief Get the normals cloud*/
158  inline PointCloudNConstPtr
160  {
161  return (cloud_normals_);
162  }
163 
164  /** \brief Set the normals distance weight*/
165  inline void
166  setNormalDistanceWeight (const double weight)
167  {
168  normals_distance_weight_ = weight;
169  }
170 
171  /** \brief get the normal distance weight*/
172  inline double
174  {
175  return (normals_distance_weight_);
176  }
177 
178  /** \brief Register a different threshold function
179  * \param[in] thresh pointer to a threshold function
180  */
181  void
182  setThresholdFunction (std::function<bool (double)> thresh)
183  {
184  threshold_function_ = thresh;
185  }
186 
187  /** \brief Register a different threshold function
188  * \param[in] thresh_function pointer to a threshold function
189  * \param[in] instance
190  */
191  template <typename T> void
192  setThresholdFunction (bool (T::*thresh_function) (double), T& instance)
193  {
194  setThresholdFunction ([=, &instance] (double threshold) { return (instance.*thresh_function) (threshold); });
195  }
196 
197  protected:
207 
208  /** \brief Filtered results are indexed by an indices array.
209  * \param[out] indices The resultant indices.
210  */
211  void
212  applyFilter (Indices &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 (Indices &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
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:174
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:414
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:415
shared_ptr< SampleConsensusModel< PointT > > Ptr
Definition: sac_model.h:79
Defines all the PCL implemented PointT point type structures.
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.