Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segment plane non deterministic issue #6308

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_);
theNded marked this conversation as resolved.
Show resolved Hide resolved
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
Loading