Skip to content

Commit fa5b250

Browse files
authored
Merge pull request #5572 from mvieth/local_maximum_fix
[filters] Fix bug in LocalMaximum
2 parents 319e433 + 4dbd5c3 commit fa5b250

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

filters/include/pcl/filters/impl/local_maximum.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ pcl::LocalMaximum<PointT>::applyFilterIndices (Indices &indices)
121121
// not be maximal in their own neighborhood
122122
if (point_is_visited[iii] && !point_is_max[iii])
123123
{
124+
if (negative_) {
125+
if (extract_removed_indices_) {
126+
(*removed_indices_)[rii++] = iii;
127+
}
128+
}
129+
else {
130+
indices[oii++] = iii;
131+
}
124132
continue;
125133
}
126134

@@ -146,7 +154,7 @@ pcl::LocalMaximum<PointT>::applyFilterIndices (Indices &indices)
146154

147155
// Check to see if a neighbor is higher than the query point
148156
float query_z = (*input_)[iii].z;
149-
for (std::size_t k = 1; k < radius_indices.size (); ++k) // k = 1 is the first neighbor
157+
for (std::size_t k = 0; k < radius_indices.size (); ++k) // the query point itself is in the (unsorted) radius_indices, but that is okay since we compare with ">"
150158
{
151159
if ((*input_)[radius_indices[k]].z > query_z)
152160
{
@@ -160,7 +168,7 @@ pcl::LocalMaximum<PointT>::applyFilterIndices (Indices &indices)
160168
// visited, excluding them from future consideration as local maxima
161169
if (point_is_max[iii])
162170
{
163-
for (std::size_t k = 1; k < radius_indices.size (); ++k) // k = 1 is the first neighbor
171+
for (std::size_t k = 0; k < radius_indices.size (); ++k) // the query point itself is in the (unsorted) radius_indices, but it must also be marked as visited
164172
{
165173
point_is_visited[radius_indices[k]] = true;
166174
}

test/filters/test_local_maximum.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@
4545

4646
using namespace pcl;
4747

48-
PointCloud<PointXYZ> cloud;
49-
5048
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5149
TEST (Filters, LocalMaximum)
5250
{
5351
PointCloud<PointXYZ> cloud_in, cloud_out;
5452

5553
cloud_in.height = 1;
56-
cloud_in.width = 3;
54+
cloud_in.width = 4;
5755
cloud_in.is_dense = true;
5856
cloud_in.resize (4);
5957

@@ -73,6 +71,31 @@ TEST (Filters, LocalMaximum)
7371
EXPECT_EQ (3, cloud_out.size ());
7472
}
7573

74+
TEST (Filters, LocalMaximum2) // Same as the "LocalMaximum" test above, but the points have a different order
75+
{
76+
PointCloud<PointXYZ> cloud_in, cloud_out;
77+
78+
cloud_in.height = 1;
79+
cloud_in.width = 4;
80+
cloud_in.is_dense = true;
81+
cloud_in.resize (4);
82+
83+
cloud_in[0].x = 0.5; cloud_in[0].y = 0.5; cloud_in[0].z = 1; // this one should be removed
84+
cloud_in[1].x = 0; cloud_in[1].y = 0; cloud_in[1].z = 0.25;
85+
cloud_in[2].x = 0.25; cloud_in[2].y = 0.25; cloud_in[2].z = 0.5;
86+
cloud_in[3].x = 5; cloud_in[3].y = 5; cloud_in[3].z = 2;
87+
88+
LocalMaximum<PointXYZ> lm;
89+
lm.setInputCloud (cloud_in.makeShared ());
90+
lm.setRadius (1.0f);
91+
lm.filter (cloud_out);
92+
93+
EXPECT_EQ (0.25f, cloud_out[0].z);
94+
EXPECT_EQ (0.50f, cloud_out[1].z);
95+
EXPECT_EQ (2.00f, cloud_out[2].z);
96+
EXPECT_EQ (3, cloud_out.size ());
97+
}
98+
7699
/* ---[ */
77100
int
78101
main (int argc, char** argv)

0 commit comments

Comments
 (0)