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

Add Tensor-based implementation of FarthestPointDownSample #6948

Merged
merged 2 commits into from
Oct 9, 2024
Merged
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
36 changes: 31 additions & 5 deletions cpp/open3d/t/geometry/PointCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,37 @@ PointCloud PointCloud::RandomDownSample(double sampling_ratio) const {
}

PointCloud PointCloud::FarthestPointDownSample(size_t num_samples) const {
// We want the sampled points has the attributes of the original point
// cloud, so full copy is needed.
const open3d::geometry::PointCloud lpcd = ToLegacy();
return FromLegacy(*lpcd.FarthestPointDownSample(num_samples),
GetPointPositions().GetDtype(), GetDevice());
const core::Dtype dtype = GetPointPositions().GetDtype();
const int64_t num_points = GetPointPositions().GetLength();
if (num_samples == 0) {
return PointCloud(GetDevice());
} else if (num_samples == size_t(num_points)) {
return Clone();
} else if (num_samples > size_t(num_points)) {
utility::LogError(
"Illegal number of samples: {}, must <= point size: {}",
num_samples, num_points);
}
core::Tensor selection_mask =
core::Tensor::Zeros({num_points}, core::Bool, GetDevice());
core::Tensor smallest_distances = core::Tensor::Full(
{num_points}, std::numeric_limits<double>::infinity(), dtype,
GetDevice());

int64_t farthest_index = 0;

for (size_t i = 0; i < num_samples; i++) {
selection_mask[farthest_index] = true;
core::Tensor selected = GetPointPositions()[farthest_index];

core::Tensor diff = GetPointPositions() - selected;
core::Tensor distances_to_selected = (diff * diff).Sum({1});
smallest_distances = open3d::core::Minimum(distances_to_selected,
smallest_distances);

farthest_index = smallest_distances.ArgMax({0}).Item<int64_t>();
}
return SelectByMask(selection_mask);
}

std::tuple<PointCloud, core::Tensor> PointCloud::RemoveRadiusOutliers(
Expand Down
Loading