Point Cloud Library (PCL)  1.9.1-dev
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/filters/radius_outlier_removal.h>
44 #include <pcl/common/io.h>
45 
46 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointT> void
49 {
50  std::vector<int> indices;
51  if (keep_organized_)
52  {
53  bool temp = extract_removed_indices_;
54  extract_removed_indices_ = true;
55  applyFilterIndices (indices);
56  extract_removed_indices_ = temp;
57 
58  output = *input_;
59  for (int rii = 0; rii < static_cast<int> (removed_indices_->size ()); ++rii) // rii = removed indices iterator
60  output.points[(*removed_indices_)[rii]].x = output.points[(*removed_indices_)[rii]].y = output.points[(*removed_indices_)[rii]].z = user_filter_value_;
61  if (!std::isfinite (user_filter_value_))
62  output.is_dense = false;
63  }
64  else
65  {
66  applyFilterIndices (indices);
67  copyPointCloud (*input_, indices, output);
68  }
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72 template <typename PointT> void
74 {
75  if (search_radius_ == 0.0)
76  {
77  PCL_ERROR ("[pcl::%s::applyFilter] No radius defined!\n", getClassName ().c_str ());
78  indices.clear ();
79  removed_indices_->clear ();
80  return;
81  }
82 
83  // Initialize the search class
84  if (!searcher_)
85  {
86  if (input_->isOrganized ())
87  searcher_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
88  else
89  searcher_.reset (new pcl::search::KdTree<PointT> (false));
90  }
91  searcher_->setInputCloud (input_);
92 
93  // The arrays to be used
94  std::vector<int> nn_indices (indices_->size ());
95  std::vector<float> nn_dists (indices_->size ());
96  indices.resize (indices_->size ());
97  removed_indices_->resize (indices_->size ());
98  int oii = 0, rii = 0; // oii = output indices iterator, rii = removed indices iterator
99 
100  // If the data is dense => use nearest-k search
101  if (input_->is_dense)
102  {
103  // Note: k includes the query point, so is always at least 1
104  int mean_k = min_pts_radius_ + 1;
105  double nn_dists_max = search_radius_ * search_radius_;
106 
107  for (std::vector<int>::const_iterator it = indices_->begin (); it != indices_->end (); ++it)
108  {
109  // Perform the nearest-k search
110  int k = searcher_->nearestKSearch (*it, mean_k, nn_indices, nn_dists);
111 
112  // Check the number of neighbors
113  // Note: nn_dists is sorted, so check the last item
114  bool chk_neighbors = true;
115  if (k == mean_k)
116  {
117  if (negative_)
118  {
119  chk_neighbors = false;
120  if (nn_dists_max < nn_dists[k-1])
121  {
122  chk_neighbors = true;
123  }
124  }
125  else
126  {
127  chk_neighbors = true;
128  if (nn_dists_max < nn_dists[k-1])
129  {
130  chk_neighbors = false;
131  }
132  }
133  }
134  else
135  {
136  if (negative_)
137  chk_neighbors = true;
138  else
139  chk_neighbors = false;
140  }
141 
142  // Points having too few neighbors are outliers and are passed to removed indices
143  // Unless negative was set, then it's the opposite condition
144  if (!chk_neighbors)
145  {
146  if (extract_removed_indices_)
147  (*removed_indices_)[rii++] = *it;
148  continue;
149  }
150 
151  // Otherwise it was a normal point for output (inlier)
152  indices[oii++] = *it;
153  }
154  }
155  // NaN or Inf values could exist => use radius search
156  else
157  {
158  for (std::vector<int>::const_iterator it = indices_->begin (); it != indices_->end (); ++it)
159  {
160  // Perform the radius search
161  // Note: k includes the query point, so is always at least 1
162  int k = searcher_->radiusSearch (*it, search_radius_, nn_indices, nn_dists);
163 
164  // Points having too few neighbors are outliers and are passed to removed indices
165  // Unless negative was set, then it's the opposite condition
166  if ((!negative_ && k <= min_pts_radius_) || (negative_ && k > min_pts_radius_))
167  {
168  if (extract_removed_indices_)
169  (*removed_indices_)[rii++] = *it;
170  continue;
171  }
172 
173  // Otherwise it was a normal point for output (inlier)
174  indices[oii++] = *it;
175  }
176  }
177 
178  // Resize the output arrays
179  indices.resize (oii);
180  removed_indices_->resize (rii);
181 }
182 
183 #define PCL_INSTANTIATE_RadiusOutlierRemoval(T) template class PCL_EXPORTS pcl::RadiusOutlierRemoval<T>;
184 
185 #endif // PCL_FILTERS_IMPL_RADIUS_OUTLIER_REMOVAL_H_
186 
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:423
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
void applyFilter(PointCloud &output) override
Filtered results are stored in a separate point cloud.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:431
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds...
Definition: organized.h:62
void applyFilterIndices(std::vector< int > &indices)
Filtered results are indexed by an indices array.