Skip to content

Commit

Permalink
fix infinite loop in segment_plane if num_points < ransac_n
Browse files Browse the repository at this point in the history
  • Loading branch information
rxba committed Oct 26, 2024
1 parent fcc396e commit 4a17608
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions cpp/open3d/geometry/PointCloudSegmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class RandomSampler {
valid_sample++;
}
}

return samples;
}

Expand Down Expand Up @@ -156,21 +155,7 @@ std::tuple<Eigen::Vector4d, std::vector<size_t>> PointCloud::SegmentPlane(
utility::LogError("Probability must be > 0 and <= 1.0");
}

RANSACResult result;

// Initialize the best plane model.
Eigen::Vector4d best_plane_model = Eigen::Vector4d(0, 0, 0, 0);

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) {
utility::LogError(
"ransac_n should be set to higher than or equal to 3.");
Expand All @@ -183,6 +168,16 @@ std::tuple<Eigen::Vector4d, std::vector<size_t>> PointCloud::SegmentPlane(
std::vector<size_t>{});
}

RANSACResult result;
Eigen::Vector4d best_plane_model = Eigen::Vector4d(0, 0, 0, 0);

RandomSampler<size_t> sampler(num_points);
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));
}

// Use size_t here to avoid large integer which acceed max of int.
size_t break_iteration = std::numeric_limits<size_t>::max();
int iteration_count = 0;
Expand Down

0 comments on commit 4a17608

Please sign in to comment.