Skip to content

Commit

Permalink
Make plane segmentation from point cloud deterministic (#6580)
Browse files Browse the repository at this point in the history
feat: move sampling outside parallel for loop
this change makes the results deterministic when a seed is used

Co-authored-by: Ben Thompson <benjamin.thompson@dexterity.ai>
Co-authored-by: Jonathan Kuck <jonathan@dexterity.ai>
  • Loading branch information
3 people authored Feb 6, 2024
1 parent 4214a0d commit 2e47a12
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cpp/open3d/geometry/PointCloudSegmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ std::tuple<Eigen::Vector4d, std::vector<size_t>> PointCloud::SegmentPlane(

size_t num_points = points_.size();
RandomSampler<size_t> sampler(num_points);
// Pre-generate all random samples before entering the parallel region
std::vector<std::vector<size_t>> all_sampled_indices;
all_sampled_indices.reserve(num_iterations);
for (int i = 0; i < num_iterations; i++) {
all_sampled_indices.push_back(sampler(ransac_n));
}

// Return if ransac_n is less than the required plane model parameters.
if (ransac_n < 3) {
Expand All @@ -187,8 +193,8 @@ std::tuple<Eigen::Vector4d, std::vector<size_t>> PointCloud::SegmentPlane(
continue;
}

const std::vector<size_t> sampled_indices = sampler(ransac_n);
std::vector<size_t> inliers = sampled_indices;
// Access the pre-generated sampled indices
std::vector<size_t> inliers = all_sampled_indices[itr];

// Fit model to num_model_parameters randomly selected points among the
// inliers.
Expand Down

0 comments on commit 2e47a12

Please sign in to comment.