Point Cloud Library (PCL) 1.13.1
Loading...
Searching...
No Matches
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//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53template <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////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73template <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 searcher_->setInputCloud (cloud_projected);
101
102 // The arrays to be used
103 indices.resize (indices_->size ());
104 removed_indices_->resize (indices_->size ());
105 int oii = 0, rii = 0; // oii = output indices iterator, rii = removed indices iterator
106
107 std::vector<bool> point_is_max (indices_->size (), false);
108 std::vector<bool> point_is_visited (indices_->size (), false);
109
110 // Find all points within xy radius (i.e., a vertical cylinder) of the query
111 // point, removing those that are locally maximal (i.e., highest z within the
112 // cylinder)
113 for (const auto& iii : (*indices_))
114 {
115 if (!isFinite ((*input_)[iii]))
116 {
117 continue;
118 }
119
120 // Points in the neighborhood of a previously identified local max, will
121 // not be maximal in their own neighborhood
122 if (point_is_visited[iii] && !point_is_max[iii])
123 {
124 if (negative_) {
125 if (extract_removed_indices_) {
126 (*removed_indices_)[rii++] = iii;
127 }
128 }
129 else {
130 indices[oii++] = iii;
131 }
132 continue;
133 }
134
135 // Assume the current query point is the maximum, mark as visited
136 point_is_max[iii] = true;
137 point_is_visited[iii] = true;
138
139 // Perform the radius search in the projected cloud
140 Indices radius_indices;
141 std::vector<float> radius_dists;
142 PointT p = (*cloud_projected)[iii];
143 if (searcher_->radiusSearch (p, radius_, radius_indices, radius_dists) == 0)
144 {
145 PCL_WARN ("[pcl::%s::applyFilter] Searching for neighbors within radius %f failed.\n", getClassName ().c_str (), radius_);
146 continue;
147 }
148
149 // If query point is alone, we retain it regardless
150 if (radius_indices.size () == 1)
151 {
152 point_is_max[iii] = false;
153 }
154
155 // Check to see if a neighbor is higher than the query point
156 float query_z = (*input_)[iii].z;
157 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 ">"
158 {
159 if ((*input_)[radius_index].z > query_z)
160 {
161 // Query point is not the local max, no need to check others
162 point_is_max[iii] = false;
163 break;
164 }
165 }
166
167 // If the query point was a local max, all neighbors can be marked as
168 // visited, excluding them from future consideration as local maxima
169 if (point_is_max[iii])
170 {
171 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
172 {
173 point_is_visited[radius_index] = true;
174 }
175 }
176
177 // Points that are local maxima are passed to removed indices
178 // Unless negative was set, then it's the opposite condition
179 if ((!negative_ && point_is_max[iii]) || (negative_ && !point_is_max[iii]))
180 {
181 if (extract_removed_indices_)
182 {
183 (*removed_indices_)[rii++] = iii;
184 }
185
186 continue;
187 }
188
189 // Otherwise it was a normal point for output (inlier)
190 indices[oii++] = iii;
191 }
192
193 // Resize the output arrays
194 indices.resize (oii);
195 removed_indices_->resize (rii);
196}
197
198#define PCL_INSTANTIATE_LocalMaximum(T) template class PCL_EXPORTS pcl::LocalMaximum<T>;
199
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.
typename FilterIndices< PointT >::PointCloud PointCloud
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition pcl_base.hpp:65
shared_ptr< PointCloud< PointT > > Ptr
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 point clouds.
Definition organized.h:61
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.