Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
radius_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 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of the copyright holder(s) nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 * $Id$
37 *
38 */
39
40#ifndef PCL_FILTERS_IMPL_RADIUS_OUTLIER_REMOVAL_H_
41#define PCL_FILTERS_IMPL_RADIUS_OUTLIER_REMOVAL_H_
42
43#include <pcl/common/point_tests.h> // for isXYZFinite
44#include <pcl/filters/radius_outlier_removal.h>
45#include <pcl/search/auto.h> // for autoSelectMethod
46
47////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
48template <typename PointT> void
50{
51 if (search_radius_ == 0.0)
52 {
53 PCL_ERROR ("[pcl::%s::applyFilter] No radius defined!\n", getClassName ().c_str ());
54 indices.clear ();
55 removed_indices_->clear ();
56 return;
57 }
58
59 // Initialize the search class
60 if (!searcher_)
61 {
62 searcher_.reset (pcl::search::autoSelectMethod<PointT>(input_, false, input_->is_dense ? pcl::search::Purpose::many_knn_search : pcl::search::Purpose::radius_search));
63 }
64 else if (!searcher_->setInputCloud (input_))
65 {
66 PCL_ERROR ("[pcl::%s::applyFilter] Error when initializing search method!\n", getClassName ().c_str ());
67 indices.clear ();
68 removed_indices_->clear ();
69 return;
70 }
71
72 // Note: k includes the query point, so is always at least 1
73 const int mean_k = min_pts_radius_ + 1;
74 const double nn_dists_max = search_radius_ * search_radius_;
75
76 // The arrays to be used
77 Indices nn_indices (mean_k);
78 std::vector<float> nn_dists(mean_k);
79 // Set to keep all points and in the filtering set those we don't want to keep, assuming
80 // we want to keep the majority of the points.
81 // 0 = remove, 1 = keep
82 std::vector<std::uint8_t> to_keep(indices_->size(), 1);
83 indices.resize (indices_->size ());
84 removed_indices_->resize (indices_->size ());
85 int oii = 0, rii = 0; // oii = output indices iterator, rii = removed indices iterator
86
87 // If the data is dense => use nearest-k search
88 if (input_->is_dense)
89 {
90 #pragma omp parallel for \
91 schedule(dynamic,64) \
92 firstprivate(nn_indices, nn_dists) \
93 shared(to_keep) \
94 num_threads(num_threads_)
95 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(indices_->size()); i++)
96 {
97 const auto& index = (*indices_)[i];
98 // Perform the nearest-k search
99 const int k = searcher_->nearestKSearch (index, mean_k, nn_indices, nn_dists);
100
101 // Check the number of neighbors
102 // Note: nn_dists is sorted, so check the last item
103 if (k == mean_k)
104 {
105 // if negative_ is false and a neighbor is further away than max distance, remove the point
106 // or
107 // if negative is true and a neighbor is closer than max distance, remove the point
108 if ((!negative_ && nn_dists_max < nn_dists[k-1]) || (negative_ && nn_dists_max >= nn_dists[k - 1]))
109 {
110 to_keep[i] = 0;
111 continue;
112 }
113 }
114 else
115 {
116 if (!negative_)
117 {
118 // if too few neighbors, remove the point
119 to_keep[i] = 0;
120 continue;
121 }
122 }
123 }
124 }
125 // NaN or Inf values could exist => use radius search
126 else
127 {
128 #pragma omp parallel for \
129 schedule(dynamic, 64) \
130 firstprivate(nn_indices, nn_dists) \
131 shared(to_keep) \
132 num_threads(num_threads_)
133 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(indices_->size()); i++)
134 {
135 const auto& index = (*indices_)[i];
136 if (!pcl::isXYZFinite((*input_)[index]))
137 {
138 to_keep[i] = 0;
139 continue;
140 }
141
142 // Perform the radius search
143 // Note: k includes the query point, so is always at least 1
144 // last parameter (max_nn) is the maximum number of neighbors returned. If enough neighbors are found so that point can not be an outlier, we stop searching.
145 const int k = searcher_->radiusSearch (index, search_radius_, nn_indices, nn_dists, min_pts_radius_ + 1);
146
147 // Points having too few neighbors are removed
148 // or if negative_ is true, then if it has too many neighbors
149 if ((!negative_ && k <= min_pts_radius_) || (negative_ && k > min_pts_radius_))
150 {
151 to_keep[i] = 0;
152 continue;
153 }
154 }
155 }
156
157 for (index_t i=0; i < static_cast<index_t>(to_keep.size()); i++)
158 {
159 if (to_keep[i] == 0)
160 {
161 if (extract_removed_indices_)
162 (*removed_indices_)[rii++] = (*indices_)[i];
163 continue;
164 }
165
166 // Otherwise it was a normal point for output (inlier)
167 indices[oii++] = (*indices_)[i];
168 }
169
170 // Resize the output arrays
171 indices.resize (oii);
172 removed_indices_->resize (rii);
173}
174
175#define PCL_INSTANTIATE_RadiusOutlierRemoval(T) template class PCL_EXPORTS pcl::RadiusOutlierRemoval<T>;
176
177#endif // PCL_FILTERS_IMPL_RADIUS_OUTLIER_REMOVAL_H_
178
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
@ radius_search
The search method will mainly be used for radiusSearch.
@ many_knn_search
The search method will mainly be used for nearestKSearch where k is larger than 1.
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition types.h:112
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
constexpr bool isXYZFinite(const PointT &) noexcept