Skip to content

Commit fe59f84

Browse files
authored
Merge pull request #4973 from tin1254/fix_mls_voxel_grid_dilation
Fix mls voxel grid hashing out of bound
2 parents 71b6d1e + 5c11ef3 commit fe59f84

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

surface/include/pcl/surface/impl/mls.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ pcl::MovingLeastSquares<PointInT, PointOutT>::performUpsampling (PointCloudOut &
403403
{
404404
corresponding_input_indices_.reset (new PointIndices);
405405

406-
MLSVoxelGrid voxel_grid (input_, indices_, voxel_size_);
406+
MLSVoxelGrid voxel_grid (input_, indices_, voxel_size_, dilation_iteration_num_);
407407
for (int iteration = 0; iteration < dilation_iteration_num_; ++iteration)
408408
voxel_grid.dilate ();
409409

@@ -805,15 +805,18 @@ pcl::MLSResult::computeMLSSurface (const pcl::PointCloud<PointT> &cloud,
805805
template <typename PointInT, typename PointOutT>
806806
pcl::MovingLeastSquares<PointInT, PointOutT>::MLSVoxelGrid::MLSVoxelGrid (PointCloudInConstPtr& cloud,
807807
IndicesPtr &indices,
808-
float voxel_size) :
808+
float voxel_size,
809+
int dilation_iteration_num) :
809810
voxel_grid_ (), data_size_ (), voxel_size_ (voxel_size)
810811
{
811812
pcl::getMinMax3D (*cloud, *indices, bounding_min_, bounding_max_);
813+
bounding_min_ -= Eigen::Vector4f::Constant(voxel_size_ * (dilation_iteration_num + 1));
814+
bounding_max_ += Eigen::Vector4f::Constant(voxel_size_ * (dilation_iteration_num + 1));
812815

813816
Eigen::Vector4f bounding_box_size = bounding_max_ - bounding_min_;
814817
const double max_size = (std::max) ((std::max)(bounding_box_size.x (), bounding_box_size.y ()), bounding_box_size.z ());
815818
// Put initial cloud in voxel grid
816-
data_size_ = static_cast<std::uint64_t> (1.5 * max_size / voxel_size_);
819+
data_size_ = static_cast<std::uint64_t> (std::ceil(max_size / voxel_size_));
817820
for (std::size_t i = 0; i < indices->size (); ++i)
818821
if (std::isfinite ((*cloud)[(*indices)[i]].x))
819822
{

surface/include/pcl/surface/mls.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ namespace pcl
591591

592592
MLSVoxelGrid (PointCloudInConstPtr& cloud,
593593
IndicesPtr &indices,
594-
float voxel_size);
594+
float voxel_size,
595+
int dilation_iteration_num);
595596

596597
void
597598
dilate ();

test/surface/test_moving_least_squares.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <pcl/io/vtk_io.h>
4545
#include <pcl/features/normal_3d.h>
4646
#include <pcl/surface/mls.h>
47+
#include <pcl/common/common.h> // getMinMax3D
4748

4849
using namespace pcl;
4950
using namespace pcl::io;
@@ -127,17 +128,29 @@ TEST (PCL, MovingLeastSquares)
127128
// EXPECT_NEAR ((*mls_normals)[10].curvature, 0.019003, 1e-3);
128129
// EXPECT_EQ (mls_normals->size (), 457);
129130

130-
131+
const float voxel_size = 0.005f;
132+
const int num_dilations = 5;
131133
mls_upsampling.setUpsamplingMethod (MovingLeastSquares<PointXYZ, PointNormal>::VOXEL_GRID_DILATION);
132-
mls_upsampling.setDilationIterations (5);
133-
mls_upsampling.setDilationVoxelSize (0.005f);
134+
mls_upsampling.setDilationIterations (num_dilations);
135+
mls_upsampling.setDilationVoxelSize (voxel_size);
134136
mls_normals->clear ();
135137
mls_upsampling.process (*mls_normals);
136-
EXPECT_NEAR ((*mls_normals)[10].x, -0.070005938410758972, 2e-3);
137-
EXPECT_NEAR ((*mls_normals)[10].y, 0.028887597844004631, 2e-3);
138+
EXPECT_NEAR ((*mls_normals)[10].x, -0.086348414421081543, 2e-3);
139+
EXPECT_NEAR ((*mls_normals)[10].y, 0.080920584499835968, 2e-3);
138140
EXPECT_NEAR ((*mls_normals)[10].z, 0.01788550429046154, 2e-3);
139141
EXPECT_NEAR ((*mls_normals)[10].curvature, 0.107273, 1e-1);
140-
EXPECT_NEAR (double (mls_normals->size ()), 29394, 2);
142+
EXPECT_NEAR (double (mls_normals->size ()), 25483, 2);
143+
144+
// Check the boundary
145+
Eigen::Vector4f original_min_pt, original_max_pt, min_pt, max_pt;
146+
pcl::getMinMax3D(*cloud, original_min_pt, original_max_pt);
147+
pcl::getMinMax3D(*mls_normals, min_pt, max_pt);
148+
EXPECT_TRUE(
149+
(original_min_pt.array() - (num_dilations + 3) * voxel_size <= min_pt.array())
150+
.all());
151+
EXPECT_TRUE(
152+
(max_pt.array() <= original_max_pt.array() + (num_dilations + 4) * voxel_size)
153+
.all());
141154
}
142155

143156
#ifdef _OPENMP

0 commit comments

Comments
 (0)