Point Cloud Library (PCL) 1.12.0
model_outlier_removal.hpp
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/model_outlier_removal.h>
41#include <pcl/common/point_tests.h> // for pcl::isFinite
42#include <pcl/sample_consensus/sac_model_circle.h>
43#include <pcl/sample_consensus/sac_model_cylinder.h>
44#include <pcl/sample_consensus/sac_model_cone.h>
45#include <pcl/sample_consensus/sac_model_line.h>
46#include <pcl/sample_consensus/sac_model_normal_plane.h>
47#include <pcl/sample_consensus/sac_model_normal_sphere.h>
48#include <pcl/sample_consensus/sac_model_parallel_plane.h>
49#include <pcl/sample_consensus/sac_model_normal_parallel_plane.h>
50#include <pcl/sample_consensus/sac_model_parallel_line.h>
51#include <pcl/sample_consensus/sac_model_perpendicular_plane.h>
52#include <pcl/sample_consensus/sac_model_plane.h>
53#include <pcl/sample_consensus/sac_model_sphere.h>
54
55////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56template <typename PointT> bool
58{
59 // Build the model
60 switch (model_type)
61 {
62 case SACMODEL_PLANE:
63 {
64 PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: modelPLANE\n", getClassName ().c_str ());
65 model_.reset (new SampleConsensusModelPlane<PointT> (input_));
66 break;
67 }
68 case SACMODEL_LINE:
69 {
70 PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: modelLINE\n", getClassName ().c_str ());
71 model_.reset (new SampleConsensusModelLine<PointT> (input_));
72 break;
73 }
75 {
76 PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: modelCIRCLE2D\n", getClassName ().c_str ());
77 model_.reset (new SampleConsensusModelCircle2D<PointT> (input_));
78 break;
79 }
80 case SACMODEL_SPHERE:
81 {
82 PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: modelSPHERE\n", getClassName ().c_str ());
83 model_.reset (new SampleConsensusModelSphere<PointT> (input_));
84 break;
85 }
87 {
88 PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: modelPARALLEL_LINE\n", getClassName ().c_str ());
89 model_.reset (new SampleConsensusModelParallelLine<PointT> (input_));
90 break;
91 }
93 {
94 PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: modelPERPENDICULAR_PLANE\n", getClassName ().c_str ());
95 model_.reset (new SampleConsensusModelPerpendicularPlane<PointT> (input_));
96 break;
97 }
99 {
100 PCL_DEBUG ("[pcl::%s::segment] Using a model of type: modelCYLINDER\n", getClassName ().c_str ());
101 model_.reset (new SampleConsensusModelCylinder<PointT, pcl::Normal> (input_));
102 break;
103 }
105 {
106 PCL_DEBUG ("[pcl::%s::segment] Using a model of type: modelNORMAL_PLANE\n", getClassName ().c_str ());
107 model_.reset (new SampleConsensusModelNormalPlane<PointT, pcl::Normal> (input_));
108 break;
109 }
110 case SACMODEL_CONE:
111 {
112 PCL_DEBUG ("[pcl::%s::segment] Using a model of type: modelCONE\n", getClassName ().c_str ());
113 model_.reset (new SampleConsensusModelCone<PointT, pcl::Normal> (input_));
114 break;
115 }
117 {
118 PCL_DEBUG ("[pcl::%s::segment] Using a model of type: modelNORMAL_SPHERE\n", getClassName ().c_str ());
119 model_.reset (new SampleConsensusModelNormalSphere<PointT, pcl::Normal> (input_));
120 break;
121 }
123 {
124 PCL_DEBUG ("[pcl::%s::segment] Using a model of type: modelNORMAL_PARALLEL_PLANE\n", getClassName ().c_str ());
125 model_.reset (new SampleConsensusModelNormalParallelPlane<PointT, pcl::Normal> (input_));
126 break;
127 }
129 {
130 PCL_DEBUG ("[pcl::%s::segment] Using a model of type: modelPARALLEL_PLANE\n", getClassName ().c_str ());
131 model_.reset (new SampleConsensusModelParallelPlane<PointT> (input_));
132 break;
133 }
134 default:
135 {
136 PCL_ERROR ("[pcl::%s::initSACModel] No valid model given!\n", getClassName ().c_str ());
137 return (false);
138 }
139 }
140 return (true);
141}
142
143////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
144template <typename PointT> void
146{
147 //The arrays to be used
148 indices.resize (indices_->size ());
149 removed_indices_->resize (indices_->size ());
150 int oii = 0, rii = 0; // oii = output indices iterator, rii = removed indices iterator
151 //is the filtersetup correct?
152 bool valid_setup = true;
153
154 valid_setup &= initSACModel (model_type_);
155
157 // Returns NULL if cast isn't possible
158 SACModelFromNormals *model_from_normals = dynamic_cast<SACModelFromNormals *> (& (*model_));
159
160 if (model_from_normals)
161 {
162 if (!cloud_normals_)
163 {
164 valid_setup = false;
165 PCL_ERROR ("[pcl::ModelOutlierRemoval::applyFilterIndices]: no normals cloud set.\n");
166 }
167 else
168 {
169 model_from_normals->setNormalDistanceWeight (normals_distance_weight_);
170 model_from_normals->setInputNormals (cloud_normals_);
171 }
172 }
173
174 //if the filter setup is invalid filter for nan and return;
175 if (!valid_setup)
176 {
177 for (const auto& iii : (*indices_)) // iii = input indices iterator
178 {
179 // Non-finite entries are always passed to removed indices
180 if (!isFinite ((*input_)[iii]))
181 {
182 if (extract_removed_indices_)
183 (*removed_indices_)[rii++] = iii;
184 continue;
185 }
186 indices[oii++] = iii;
187 }
188 return;
189 }
190 // check distance of pointcloud to model
191 std::vector<double> distances;
192 //TODO: get signed distances !
193 model_->setIndices(indices_); // added to reduce computation and arrange distances with indices
194 model_->getDistancesToModel (model_coefficients_, distances);
195
196 bool thresh_result;
197
198 // Filter for non-finite entries and the specified field limits
199 for (int iii = 0; iii < static_cast<int> (indices_->size ()); ++iii) // iii = input indices iterator
200 {
201 // Non-finite entries are always passed to removed indices
202 if (!isFinite ((*input_)[ (*indices_)[iii]]))
203 {
204 if (extract_removed_indices_)
205 (*removed_indices_)[rii++] = (*indices_)[iii];
206 continue;
207 }
208
209 // use threshold function to separate outliers from inliers:
210 thresh_result = threshold_function_ (distances[iii]);
211
212 // in normal mode: define outliers as false thresh_result
213 if (!negative_ && !thresh_result)
214 {
215 if (extract_removed_indices_)
216 (*removed_indices_)[rii++] = (*indices_)[iii];
217 continue;
218 }
219
220 // in negative_ mode: define outliers as true thresh_result
221 if (negative_ && thresh_result)
222 {
223 if (extract_removed_indices_)
224 (*removed_indices_)[rii++] = (*indices_)[iii];
225 continue;
226 }
227
228 // Otherwise it was a normal point for output (inlier)
229 indices[oii++] = (*indices_)[iii];
230
231 }
232
233 // Resize the output arrays
234 indices.resize (oii);
235 removed_indices_->resize (rii);
236
237}
238
239#define PCL_INSTANTIATE_ModelOutlierRemoval(T) template class PCL_EXPORTS pcl::ModelOutlierRemoval<T>;
240
ModelOutlierRemoval filters points in a cloud based on the distance between model and point.
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
SampleConsensusModelFromNormals represents the base model class for models that require the use of su...
Definition: sac_model.h:612
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
SacModel
Definition: model_types.h:46
@ SACMODEL_CYLINDER
Definition: model_types.h:52
@ SACMODEL_PLANE
Definition: model_types.h:47
@ SACMODEL_PARALLEL_PLANE
Definition: model_types.h:62
@ SACMODEL_SPHERE
Definition: model_types.h:51
@ SACMODEL_PARALLEL_LINE
Definition: model_types.h:55
@ SACMODEL_NORMAL_PARALLEL_PLANE
Definition: model_types.h:63
@ SACMODEL_PERPENDICULAR_PLANE
Definition: model_types.h:56
@ SACMODEL_NORMAL_SPHERE
Definition: model_types.h:59
@ SACMODEL_CIRCLE2D
Definition: model_types.h:49
@ SACMODEL_NORMAL_PLANE
Definition: model_types.h:58
@ SACMODEL_CONE
Definition: model_types.h:53
@ SACMODEL_LINE
Definition: model_types.h:48
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133