Skip to content

Commit

Permalink
fix segment plane non deterministic sampler issue and add unit test (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yuecideng authored Aug 14, 2023
1 parent c46e64a commit 75dccb4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cpp/open3d/geometry/PointCloudSegmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RandomSampler {
explicit RandomSampler(const size_t total_size) : total_size_(total_size) {}

std::vector<T> operator()(size_t sample_size) {
std::lock_guard<std::mutex> lock(mutex_);
std::vector<T> samples;
samples.reserve(sample_size);

Expand All @@ -48,6 +49,7 @@ class RandomSampler {

private:
size_t total_size_;
std::mutex mutex_;
};

/// \class RANSACResult
Expand Down
26 changes: 26 additions & 0 deletions cpp/tests/geometry/PointCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "open3d/io/ImageIO.h"
#include "open3d/io/PinholeCameraTrajectoryIO.h"
#include "open3d/io/PointCloudIO.h"
#include "open3d/utility/Random.h"
#include "open3d/visualization/utility/DrawGeometry.h"
#include "tests/Tests.h"

Expand Down Expand Up @@ -1371,6 +1372,31 @@ TEST(PointCloud, SegmentPlaneSpecialCase) {
EXPECT_ANY_THROW(pcd.SegmentPlane(0.01, 3, 10, 1.5));
}

TEST(PointCloud, SegmentPlaneDeterministic) {
geometry::PointCloud pcd;
data::PCDPointCloud pointcloud_pcd;
io::ReadPointCloud(pointcloud_pcd.GetPath(), pcd);
EXPECT_EQ(pcd.points_.size(), 113662);

// Hard-coded test
Eigen::Vector4d plane_model;
std::vector<size_t> inliers;
utility::random::Seed(0);
std::tie(plane_model, inliers) = pcd.SegmentPlane(0.01, 3, 1000, 1.0);
ExpectEQ(plane_model, Eigen::Vector4d(-0.06, -0.10, 0.99, -1.06), 0.1);

// Test segment plane for 10 times with the same random seed.
for (int i = 0; i < 10; ++i) {
// Reset random seed.
utility::random::Seed(0);
Eigen::Vector4d plane_model_d;
std::vector<size_t> inliers_d;
std::tie(plane_model_d, inliers_d) =
pcd.SegmentPlane(0.01, 3, 1000, 1.0);
ExpectEQ(plane_model, plane_model_d);
}
}

TEST(PointCloud, DetectPlanarPatches) {
geometry::PointCloud pcd;
data::PCDPointCloud pointcloud_pcd;
Expand Down

0 comments on commit 75dccb4

Please sign in to comment.