Point Cloud Library (PCL)  1.14.0-dev
local_maximum.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2012, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  * Copyright (c) 2014, RadiantBlue Technologies, Inc.
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above
18  * copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided
20  * with the distribution.
21  * * Neither the name of the copyright holder(s) nor the names of its
22  * contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  * $Id$
39  *
40  */
41 
42 #pragma once
43 
44 #include <pcl/common/io.h>
45 #include <pcl/common/point_tests.h> // for pcl::isFinite
46 #include <pcl/filters/local_maximum.h>
47 #include <pcl/filters/project_inliers.h>
48 #include <pcl/ModelCoefficients.h>
49 #include <pcl/search/organized.h> // for OrganizedNeighbor
50 #include <pcl/search/kdtree.h> // for KdTree
51 
52 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53 template <typename PointT> void
55 {
56  // Has the input dataset been set already?
57  if (!input_)
58  {
59  PCL_WARN ("[pcl::%s::applyFilter] No input dataset given!\n", getClassName ().c_str ());
60  output.width = output.height = 0;
61  output.clear ();
62  return;
63  }
64 
65  Indices indices;
66 
67  output.is_dense = true;
68  applyFilterIndices (indices);
69  pcl::copyPointCloud<PointT> (*input_, indices, output);
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73 template <typename PointT> void
75 {
76  typename PointCloud::Ptr cloud_projected (new PointCloud);
77 
78  // Create a set of planar coefficients with X=Y=0,Z=1
80  coefficients->values.resize (4);
81  coefficients->values[0] = coefficients->values[1] = 0;
82  coefficients->values[2] = 1.0;
83  coefficients->values[3] = 0;
84 
85  // Create the filtering object and project input into xy plane
88  proj.setInputCloud (input_);
89  proj.setModelCoefficients (coefficients);
90  proj.filter (*cloud_projected);
91 
92  // Initialize the search class
93  if (!searcher_)
94  {
95  if (input_->isOrganized ())
96  searcher_.reset (new pcl::search::OrganizedNeighbor<PointT> ());
97  else
98  searcher_.reset (new pcl::search::KdTree<PointT> (false));
99  }
100  if (!searcher_->setInputCloud (cloud_projected))
101  {
102  PCL_ERROR ("[pcl::%s::applyFilter] Error when initializing search method!\n", getClassName ().c_str ());
103  indices.clear ();
104  removed_indices_->clear ();
105  return;
106  }
107 
108  // The arrays to be used
109  indices.resize (indices_->size ());
110  removed_indices_->resize (indices_->size ());
111  int oii = 0, rii = 0; // oii = output indices iterator, rii = removed indices iterator
112 
113  std::vector<bool> point_is_max (indices_->size (), false);
114  std::vector<bool> point_is_visited (indices_->size (), false);
115 
116  // Find all points within xy radius (i.e., a vertical cylinder) of the query
117  // point, removing those that are locally maximal (i.e., highest z within the
118  // cylinder)
119  for (const auto& iii : (*indices_))
120  {
121  if (!isFinite ((*input_)[iii]))
122  {
123  continue;
124  }
125 
126  // Points in the neighborhood of a previously identified local max, will
127  // not be maximal in their own neighborhood
128  if (point_is_visited[iii] && !point_is_max[iii])
129  {
130  if (negative_) {
131  if (extract_removed_indices_) {
132  (*removed_indices_)[rii++] = iii;
133  }
134  }
135  else {
136  indices[oii++] = iii;
137  }
138  continue;
139  }
140 
141  // Assume the current query point is the maximum, mark as visited
142  point_is_max[iii] = true;
143  point_is_visited[iii] = true;
144 
145  // Perform the radius search in the projected cloud
146  Indices radius_indices;
147  std::vector<float> radius_dists;
148  PointT p = (*cloud_projected)[iii];
149  if (searcher_->radiusSearch (p, radius_, radius_indices, radius_dists) == 0)
150  {
151  PCL_WARN ("[pcl::%s::applyFilter] Searching for neighbors within radius %f failed.\n", getClassName ().c_str (), radius_);
152  continue;
153  }
154 
155  // If query point is alone, we retain it regardless
156  if (radius_indices.size () == 1)
157  {
158  point_is_max[iii] = false;
159  }
160 
161  // Check to see if a neighbor is higher than the query point
162  float query_z = (*input_)[iii].z;
163  for (const auto& radius_index : radius_indices) // the query point itself is in the (unsorted) radius_indices, but that is okay since we compare with ">"
164  {
165  if ((*input_)[radius_index].z > query_z)
166  {
167  // Query point is not the local max, no need to check others
168  point_is_max[iii] = false;
169  break;
170  }
171  }
172 
173  // If the query point was a local max, all neighbors can be marked as
174  // visited, excluding them from future consideration as local maxima
175  if (point_is_max[iii])
176  {
177  for (const auto& radius_index : radius_indices) // the query point itself is in the (unsorted) radius_indices, but it must also be marked as visited
178  {
179  point_is_visited[radius_index] = true;
180  }
181  }
182 
183  // Points that are local maxima are passed to removed indices
184  // Unless negative was set, then it's the opposite condition
185  if ((!negative_ && point_is_max[iii]) || (negative_ && !point_is_max[iii]))
186  {
187  if (extract_removed_indices_)
188  {
189  (*removed_indices_)[rii++] = iii;
190  }
191 
192  continue;
193  }
194 
195  // Otherwise it was a normal point for output (inlier)
196  indices[oii++] = iii;
197  }
198 
199  // Resize the output arrays
200  indices.resize (oii);
201  removed_indices_->resize (rii);
202 }
203 
204 #define PCL_INSTANTIATE_LocalMaximum(T) template class PCL_EXPORTS pcl::LocalMaximum<T>;
205 
void filter(PointCloud &output)
Calls the filtering method and returns the filtered dataset in output.
Definition: filter.h:121
void applyFilter(PointCloud &output) override
Downsample a Point Cloud by eliminating points that are locally maximal in z.
void applyFilterIndices(Indices &indices)
Filtered results are indexed by an indices array.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:65
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
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:403
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
void clear()
Removes all points in a cloud and sets the width and height to 0.
Definition: point_cloud.h:885
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
ProjectInliers uses a model and a set of inlier indices from a PointCloud to project them into a sepa...
void setModelCoefficients(const ModelCoefficientsConstPtr &model)
Provide a pointer to the model coefficients.
void setModelType(int model)
The type of model to use (user given parameter).
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:62
OrganizedNeighbor is a class for optimized nearest neighbor search in organized projectable point clo...
Definition: organized.h:65
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_PLANE
Definition: model_types.h:47
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
shared_ptr< ::pcl::ModelCoefficients > Ptr
A point structure representing Euclidean xyz coordinates, and the RGB color.