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 infinite loop in segment_plane if num_points < ransac_n #7032

Merged
merged 3 commits into from
Nov 13, 2024

Conversation

rxba
Copy link
Contributor

@rxba rxba commented Oct 26, 2024

Type

  • Bug fix (non-breaking change which fixes an issue): Fixes #
  • New feature (non-breaking change which adds functionality). Resolves #
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) Resolves #

Motivation and Context

Just a small fix, but if the number of points in the pointcloud is smaller than ransac_n, segment_plane is caught in an infinite loop and does not terminate.
It's a bit of an unlikely scenario, but I did stumble across it because I was fitting planes on clustering results, and one of those point clusters only had two points.

The reason for the infinite loop is because RandomSampler is called with num_points

size_t num_points = points_.size();
RandomSampler<size_t> sampler(num_points);

, so in the case of num_points < ransac_n the condition can never become true in RandomSampler:

while (valid_sample < sample_size) {
const size_t idx = utility::random::RandUint32() % total_size_;
// Well, this is slow. But typically the sample_size is small.
if (std::find(samples.begin(), samples.end(), idx) ==
samples.end()) {
samples.push_back(idx);
valid_sample++;
}
}

Code to reproduce:

import open3d as o3d
import numpy as np

num_points = 2
points = np.random.rand(num_points, 3)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points)
plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,
                                         ransac_n=3,
                                         num_iterations=1000)

Checklist:

  • I have run python util/check_style.py --apply to apply Open3D code style
    to my code.
  • This PR changes Open3D behavior or adds new functionality.
    • Both C++ (Doxygen) and Python (Sphinx / Google style) documentation is
      updated accordingly.
    • I have added or updated C++ and / or Python unit tests OR included test
      results
      (e.g. screenshots or numbers) here.
  • I will follow up and update the code if CI fails.
  • For fork PRs, I have selected Allow edits from maintainers.

Description

  • I just moved the already existing checks for this to before the RandomSampler is called so this error can actually be caught
  • I also removed some of the comments around this part of the code, as they are redundant in my opinion

Copy link

update-docs bot commented Oct 26, 2024

Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes.

@ssheorey ssheorey requested a review from benjaminum November 6, 2024 15:41
@benjaminum benjaminum merged commit 9d0cfc8 into isl-org:main Nov 13, 2024
36 of 39 checks passed
@benjaminum
Copy link
Contributor

Thanks for this bugfix @rxba !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants